Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def generate_word_data():
"""
Download actual words from duden corresponding to test words from `TEST_DATA_DIR`
"""
data = []
for filename in os.listdir(TEST_DATA_DIR):
full_path = os.path.join(TEST_DATA_DIR, filename)
# read only yaml files
if not filename.endswith('.yaml'):
continue
# store real and expected result
with open(full_path, 'r') as f:
expected_dict = yaml.load(f, Loader=yaml.SafeLoader)
parsed_word = get(expected_dict['urlname'])
record = WordTestRecord(parsed_word, expected_dict)
data.append(record)
return data
def setUpClass(cls):
"""
Load json data used for testing
"""
cls.samples = []
for filename in os.listdir(JSON_DIR):
full_path = os.path.join(JSON_DIR, filename)
if filename.endswith('.json'):
with open(full_path, 'r') as fh:
word_json = json.load(fh)
word_obj = duden.get(word_json['urlname'])
cls.samples.append((word_json, word_obj))
def test_load_soup(self):
"""
Test if the load_soup function works correctly
"""
word = 'laufen'
url = duden.URL_FORM.format(word=word)
r = requests.get(url)
soup = bs4.BeautifulSoup(r.text, 'html.parser')
dword = duden.load_soup(soup)
self.assertEqual(word, dword.title)
def test_load_soup(self):
"""
Test if the load_soup function works correctly
"""
word = 'laufen'
url = duden.URL_FORM.format(word=word)
r = requests.get(url)
soup = bs4.BeautifulSoup(r.text, 'html.parser')
dword = duden.load_soup(soup)
self.assertEqual(word, dword.title)
def test_get(self):
"""
Test if the word laufen can be parsed
"""
word = 'laufen'
dword = duden.get('laufen')
self.assertEqual(word, dword.title)
for i, word in enumerate(words, 1):
print('{} {}'.format(blue('{})'.format(i)), word))
sys.exit(1)
result_index = args.result if args.result is not None else 1
# choose the correct result
try:
word_url_suffix = words[result_index - 1]
except IndexError:
print(red(_("No result with number {}.")).format(result_index))
sys.exit(1)
# fetch and parse the word
try:
word = get(word_url_suffix, cache=args.cache)
except Exception as exception: # pylint: disable=broad-except
print(red(exception))
sys.exit(1)
display_word(word, args)
def get_search_link_variants(link_text):
"""
Lists possible interpretations of link text on search page.
Used for determining whether a search page entry matches the search term.
"""
return clear_text(link_text).split(', ')
('Läufchen', 'Laeufchen'),
('Läufel', 'Laeufel')],
'Im Alphabet danach': [('laufend', 'laufend'),
('laufen lassen, laufenlassen', 'laufen_lassen'),
('Laufer', 'Laufer'),
('Läufer', 'Laeufer'),
('Lauferei', 'Lauferei')]}
"""
result = {}
section = self.soup.find('div', id='block-beforeafterblock-2')
for group in section.find_all('nav', class_='hookup__group'):
h3title = group.h3.text
result[h3title] = []
for item in group.find_all('li'):
link = item.a.attrs['href'].split('/')[-1]
result[h3title].append((clear_text(item.text), link))
return result
def main():
"""
Take the first CLI argument and describe the corresponding word
"""
# handle the --version switch
if '--version' in sys.argv or '-V' in sys.argv:
print('duden ' + __version__)
sys.exit(0)
# parse normal arguments
args = parse_args()
# search all words matching the string
words = search(args.word, return_words=False, exact=not args.fuzzy,
cache=args.cache)
# exit if the word wasn't found
if not words:
print(red(_("Word '{}' not found")).format(args.word))
sys.exit(1)
# list the options when there is more than one matching word
if len(words) > 1 and args.result is None:
print(_('Found {} matching words. Use the -r/--result argument to '
'specify which one to display.').format(white(len(words),
bold=True)))
for i, word in enumerate(words, 1):
print('{} {}'.format(blue('{})'.format(i)), word))
sys.exit(1)
def main():
"""
Take the first CLI argument and describe the corresponding word
"""
# handle the --version switch
if '--version' in sys.argv or '-V' in sys.argv:
print('duden ' + __version__)
sys.exit(0)
# parse normal arguments
args = parse_args()
# search all words matching the string
words = search(args.word, return_words=False, exact=not args.fuzzy,
cache=args.cache)
# exit if the word wasn't found
if not words:
print(red(_("Word '{}' not found")).format(args.word))
sys.exit(1)
# list the options when there is more than one matching word
if len(words) > 1 and args.result is None: