Usage of the parsing and types referencesΒΆ

Requires httpx package to be installed.

  1"""
  2Detailed overlook of parser and types references.
  3"""
  4
  5import freedictionaryapi
  6# module is requiring external dependency:
  7# so it can not be set in __init__ files
  8# for easier importing
  9from freedictionaryapi.clients.sync_client import DictionaryApiClient
 10
 11
 12def main():
 13    # Note:
 14    # # look at https://dictionaryapi.dev/
 15    # # to understand structure of the API response
 16    # # and eventually of implemented types
 17
 18    # firstly, fetch one parser to look on it
 19    with DictionaryApiClient() as client:
 20        parser: freedictionaryapi.parsers.DictionaryApiParser = client.fetch_parser('hello')
 21
 22    # simple navigating throw the API response
 23    # with implemented types
 24    word: freedictionaryapi.types.Word = parser.word
 25
 26    # if you need wor object exactly - use .fetch_word() method
 27
 28    # ---------------------------------------------------------
 29
 30    # so, word:
 31
 32    # # actually, word
 33    word_name: str = word.word
 34    print('{:*^20}'.format(' Word '))
 35    print(f'word = {word_name}')
 36
 37    # # Phonetics section
 38    print('{:*^20}'.format(' Phonetic '))
 39    phonetics: list[freedictionaryapi.types.Phonetic] = word.phonetics
 40    for index, phonetic in enumerate(phonetics):
 41        index += 1
 42
 43        text: str = phonetic.text
 44        # alias of the `text` - `transcription`
 45        audio: str = phonetic.audio
 46        # alias of the `audio` - `link_on_audio_with_pronunciation`
 47        print(f'{index}. text = {text}; audio = {audio}')
 48
 49    # # Meanings section
 50    print('{:*^20}'.format(' Meaning '))
 51    meanings: list[freedictionaryapi.types.Meaning] = word.meanings
 52    for index_m, meaning in enumerate(meanings):
 53        index_m += 1
 54
 55        part_of_speech: str = meaning.part_of_speech
 56
 57        print(f'{index_m}. part of speech = {part_of_speech}')
 58
 59        # # Definitions section
 60        definitions: list[freedictionaryapi.types.Definition] = meaning.definitions
 61        for index_d, definition in enumerate(definitions):
 62            index_d += 1
 63            # definition_= definition phrase that telling about meaning
 64            # naming is repeated by API response :)
 65            definition_: str = definition.definition
 66            synonyms: list[str] = definition.synonyms
 67            synonyms_joined_in_string = ', '.join(synonyms) if synonyms is not None else ''
 68            example: str = definition.example
 69
 70            message = (
 71                f'\t{index_m}.{index_d}. '
 72                f'definition = {definition_}; synonyms = {synonyms_joined_in_string!r}; example = {example!r}'
 73            )
 74            print(message)
 75
 76    # ---------------------------------------------------------
 77
 78    # so, parser...
 79
 80    # # we have some shortcuts
 81    phonetics: list[freedictionaryapi.types.Phonetic] = parser.phonetics    # parser.word.phonetics
 82    meanings: list[freedictionaryapi.types.Meaning] = parser.meanings      # # parser.word.meanings
 83
 84    # # parser methods
 85    print('{:*^20}'.format(' Parser methods '))
 86
 87    # # # some methods for phonetic section
 88    transcription: str = parser.get_transcription()
 89    print(f'.get_transcription() - {transcription!r}')
 90    all_transcriptions: list[str] = parser.get_all_transcriptions()
 91    print(f'.get_all_transcriptions() - {all_transcriptions!r}')
 92    link_on_audio_with_pronunciation: str = parser.get_link_on_audio_with_pronunciation()
 93    print(f'.get_link_on_audio_with_pronunciation() - {link_on_audio_with_pronunciation!r}')
 94
 95    # # # some methods for meaning section
 96    all_parts_of_speech: list[str] = parser.get_all_parts_of_speech()
 97    print(f'.get_all_parts_of_speech() - {all_parts_of_speech!r}')
 98    all_definitions: list[str] = parser.get_all_definitions()
 99    print(f'.get_all_definitions() - {all_definitions!r}')
100    all_examples: list[str] = parser.get_all_examples()
101    print(f'.get_all_examples() - {all_examples!r}')
102    all_synonyms: list[str] = parser.get_all_synonyms()
103    print(f'.get_all_synonyms() - {all_synonyms!r}')
104
105
106if __name__ == '__main__':
107    main()