Skip to content

Improved repr #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 35 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
a857f9c
Test Fix
orgh0 May 27, 2018
2e96190
Issue Fix
orgh0 May 27, 2018
9f4da27
Update __init__.py
orgh0 May 27, 2018
451a772
generator module fix
orgh0 May 27, 2018
10263d9
minor fixes
orgh0 May 27, 2018
e0987ad
translation module boilerplate
orgh0 May 31, 2018
00a210c
translation module boilerplate
orgh0 May 31, 2018
35747d1
changes for the translation module
orgh0 May 31, 2018
583263d
translation module boilerplate
orgh0 Jun 2, 2018
c7b4899
Translation intial module
Jun 2, 2018
f12f5e3
Translation module added
Jun 6, 2018
17d64b8
README.md update
Jun 6, 2018
874701f
Resolve merge conflicts with master
Jun 6, 2018
5f5db04
Changes requested in translation PR
Jun 6, 2018
dd928cf
remove flush code
Jun 6, 2018
2fbe068
Change in pipeline architecture
Jun 9, 2018
7bba995
Translation module update
Jun 10, 2018
8dcfd27
Translation stable commit
Jun 11, 2018
59b625e
README.md update
Jun 11, 2018
763fcdc
Added independent analyze and generate functions
Jun 17, 2018
fe61e50
Update README.md
orgh0 Jun 17, 2018
e6da4ed
changes requested in review
Jun 19, 2018
e4ea1d9
Merge branch 'translate' of https://github.com/apertium/apertium-pyth…
Jun 19, 2018
87aac5d
inline function error
Jun 21, 2018
557522f
inline function error fix
Jun 22, 2018
ed2590c
Error fixes in type annotation
Jun 22, 2018
8864ca2
get_pair_or_error function fixed
Jun 24, 2018
1bd0be2
edit for the changes requested
Jun 27, 2018
0ccd937
imporoved repr
Jul 2, 2018
5d8c21a
minor change
Jul 2, 2018
36c0b03
Update README.md
Jul 2, 2018
646b831
improved repr
Jul 8, 2018
d041201
improved repr
Jul 8, 2018
ce3838f
Tests for improved repr added
Jul 9, 2018
d664cba
repr fix
Jul 11, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,16 @@ In [2]: apertium.append_pair_path('..')

### Translation
Performing Translations
Method 1:
```python
In [1]: import apertium
In [2]: t = apertium.Translator('eng', 'spa')
In [3]: t.translate('cats')
Out[3]: 'Gatos'
```
Method 2:
```python
In [1]: import apertium
In [2]: apertium.translate('eng', 'spa', 'I love you')
Out[2]: 'Te quieres'
```
16 changes: 8 additions & 8 deletions apertium/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class ModeNotInstalled(ValueError):
pass


def update_modes(pair_path): # type: (str) -> None
modes = search_path(pair_path)
def update_modes(path): # type: (str) -> None
modes = search_path(path)
if modes['pair']:
for path, lang_src, lang_trg in modes['pair']:
pairs['%s-%s' % (lang_src, lang_trg)] = path
Expand All @@ -25,14 +25,14 @@ def update_modes(pair_path): # type: (str) -> None
generators[lang_pair] = (dirpath, modename)


def append_pair_path(pair_path): # type: (str) -> None
pair_paths.append(pair_path)
update_modes(pair_path)
def append_path(path): # type: (str) -> None
paths.append(path)
update_modes(path)


pair_paths = ['/usr/share/apertium', '/usr/local/share/apertium']
paths = ['/usr/share/apertium', '/usr/local/share/apertium']
analyzers = {} # type: Dict[str, Tuple[str, str]]
generators = {} # type: Dict[str, Tuple[str, str]]
pairs = {} # type: Dict[str, str]
for pair_path in pair_paths:
update_modes(pair_path)
for path in paths:
update_modes(path)
12 changes: 12 additions & 0 deletions apertium/analysis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ def __init__(self, lang): # type: (Analyzer, str) -> None
else:
self.path, self.mode = apertium.analyzers[self.lang]

def __repr__(self): # type: (Analyzer) -> str
"""
returns the representation of this Analyzer class object
"""
return "Analyzer(lang='%s')" % self.lang

def __str__(self): # type: (Analyzer) -> str
"""
returns the printable str representation of the object
"""
return "<Analyzer: '%s'>" % self.mode

def _get_commands(self): # type: (Analyzer) -> List[List[str]]
if self.lang not in self.analyzer_cmds:
mode_path, mode = apertium.analyzers[self.lang]
Expand Down
19 changes: 18 additions & 1 deletion apertium/generation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,24 @@
class Generator:
def __init__(self, lang): # type: (Generator, str) -> None
self.generator_cmds = {} # type: Dict[str, List[List[str]]]
self.lang = lang # type: str
self.lang = to_alpha3_code(lang) # type: str
if self.lang in apertium.generators:
self.path, self.mode = apertium.generators[self.lang]
self.commands = list(self._get_commands())
else:
raise apertium.ModeNotInstalled(self.lang)

def __repr__(self): # type: (Generator) -> str
"""
returns the representation of this Generator class object
"""
return "Generator(lang='%s')" % self.lang

def __str__(self): # type: (Generator) -> str
"""
returns the printable str representation of the object
"""
return "<Generator: '%s'>" % self.mode

def _get_commands(self): # type: (Generator) -> List[List[str]]
if self.lang not in self.generator_cmds:
Expand Down
12 changes: 12 additions & 0 deletions apertium/translation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ def __init__(self, l1, l2): # type: (Translator, str, str) -> None
self.l1 = l1
self.l2 = l2

def __repr__(self): # type: (Translator) -> str
"""
returns the representation of this Translator class object
"""
return "Translator(pair='%s-%s')" % (self.l1, self.l2)

def __str__(self): # type: (Translator) -> str
"""
returns the printable str representation of the Translator object
"""
return "<Translator: '%s'>" % apertium.pairs['%s-%s' % (self.l1, self.l2)].split('/')[-1]

def _get_commands(self, l1, l2): # type: (Translator, str, str) -> List[List[str]]
if (l1, l2) not in self.translation_cmds:
mode_path = apertium.pairs['%s-%s' % (l1, l2)]
Expand Down
27 changes: 25 additions & 2 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ def test_uninstalled_mode(self):
with self.assertRaises(apertium.ModeNotInstalled):
analyzer = apertium.Analyzer('spa')

def test_repr(self):
analyzer = apertium.Analyzer('en')
self.assertEqual(repr(analyzer), "Analyzer(lang='eng')")

def test_str(self):
analyzer = apertium.Analyzer('en')
self.assertEqual(str(analyzer), "<Analyzer: 'eng-morph'>")


class TestGenerate(unittest.TestCase):
def test_generator_single(self):
Expand All @@ -48,9 +56,8 @@ def test_generator_bare(self):
self.assertEqual(lexical_units, 'cat<n><pl>')

def test_generator_uninstalled_mode(self):
generator = apertium.Generator('spa')
with self.assertRaises(apertium.ModeNotInstalled):
generator.generate('cat<n><pl>')
generator = apertium.Generator('spa')

def test_single(self):
wordform = apertium.generate('en', '^cat<n><pl>$')
Expand All @@ -68,6 +75,14 @@ def test_uninstalled_mode(self):
with self.assertRaises(apertium.ModeNotInstalled):
apertium.generate('spa', 'cat<n><pl>')

def test_repr(self):
generator = apertium.Generator('eng')
self.assertEqual(repr(generator), "Generator(lang='eng')")

def test_str(self):
generator = apertium.Generator('eng')
self.assertEqual(str(generator), "<Generator: 'eng-gener'>")


class TestTranslate(unittest.TestCase):
def test_translator_en_spa(self):
Expand All @@ -78,3 +93,11 @@ def test_translator_en_spa(self):
def test_en_spa(self):
translated = apertium.translate('eng', 'spa', 'cats')
self.assertEqual(translated, 'Gatos')

def test_repr(self):
translator = apertium.Translator('eng', 'spa')
self.assertEqual(repr(translator), "Translator(pair='eng-spa')")

def test_str(self):
translator = apertium.Translator('eng', 'spa')
self.assertEqual(str(translator), "<Translator: 'en-es.mode'>")