Convert both translation and translation_type of Term to lists - wr - Translate a term via WordReference.com
(HTM) hg clone https://bitbucket.org/iamleot/wr
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
---
(DIR) changeset 3b2cf2967a257179ffbe0a02353508f7dcfffc5a
(DIR) parent b7eb697e8b8c4c3096ab4c0edd6cadac2a682859
(HTM) Author: Leonardo Taccari <iamleot@gmail.com>
Date: Mon, 21 Oct 2019 17:16:53
Convert both translation and translation_type of Term to lists
Diffstat:
wr.py | 29 +++++++++++++++++++----------
1 files changed, 19 insertions(+), 10 deletions(-)
---
diff -r b7eb697e8b8c -r 3b2cf2967a25 wr.py
--- a/wr.py Fri Oct 18 12:36:05 2019 +0200
+++ b/wr.py Mon Oct 21 17:16:53 2019 +0200
@@ -56,8 +56,8 @@
Term.term.__doc__ += 'Term'
Term.term_description.__doc__ += 'Description of the term'
Term.term_type.__doc__ += 'Type of the term'
-Term.translation.__doc__ += 'Translation'
-Term.translation_type.__doc__ += 'Type of the translation'
+Term.translation.__doc__ += 'List of all translations'
+Term.translation_type.__doc__ += 'List of types of all the translations'
def translate(dictionary: str, term: str) -> List[Term]:
@@ -125,12 +125,16 @@
if tr.find('td', class_='ToWrd'):
towrd = tr.find('td', class_='ToWrd')
pos2 = towrd.find('em', class_='POS2')
+ if not t.get('translation_type'):
+ t['translation_type'] = []
if pos2 and pos2.children and len(list(pos2.children)) > 0:
- t['translation_type'] = list(pos2.children)[0].strip()
+ t['translation_type'].append(list(pos2.children)[0].strip())
towrd.find('em', class_='POS2').clear()
else:
- t['translation_type'] = ''
- t['translation'] = towrd.text.strip()
+ t['translation_type'].append('')
+ if not t.get('translation'):
+ t['translation'] = []
+ t['translation'].append(towrd.text.strip())
if tr.find('td', class_='FrEx'):
frex = tr.find('td', class_='FrEx')
@@ -150,16 +154,21 @@
sfmt = '{term} '
if term.term_type:
sfmt += '[{term_type}] '
- sfmt += '{description}:\n{translation} '
- if term.translation_type:
- sfmt += '[{translation_type}]'
+ sfmt += '{description}:\n{translations}\n'
+
+ translations = ''
+ for translation, translation_type in zip(term.translation, term.translation_type):
+ if translations:
+ translations += ', '
+ translations += '{translation} [{translation_type}]'.format(
+ translation=translation,
+ translation_type=translation_type)
print(textwrap.fill(sfmt.format(
term=term.term,
term_type=term.term_type,
description=term.term_description,
- translation=term.translation,
- translation_type=term.translation_type).strip(),
+ translations=translations).strip(),
width=80,
break_long_words=False,
break_on_hyphens=False))