From 17137365c6e9aea91175b793afafc858dc1ebd16 Mon Sep 17 00:00:00 2001 From: Emerson Rocha Date: Sun, 21 Nov 2021 04:12:09 -0300 Subject: [PATCH] linguacodex.py (#4): LinguaCodexQuid class, reorganization to prepare to simulate the cli with docs --- scripts/fn/linguacodex.py | 221 ++++++++++++++++++++++++++++++++------ 1 file changed, 189 insertions(+), 32 deletions(-) diff --git a/scripts/fn/linguacodex.py b/scripts/fn/linguacodex.py index dfc1d92..9fb085c 100755 --- a/scripts/fn/linguacodex.py +++ b/scripts/fn/linguacodex.py @@ -31,12 +31,29 @@ # CHANGED: 2021-11-21 04:59 UTC v0.5 renamed as linguacodex.py # ============================================================================== """ -pytest ./scripts/fn/linguacodex.py +python3 -m doctest ./scripts/fn/linguacodex.py +python3 -m doctest -v ./scripts/fn/linguacodex.py + + +>>> LinguaCodex(de_codex='pt').quid() +'{"de_codex": "pt", "de_codex_norma": "BCP47"}' """ import sys import argparse from pathlib import Path import json +from dataclasses import dataclass, InitVar +from typing import ( + Any, + Dict, + Iterable, + Optional, + List, + TextIO, + Type, + Union, +) + import langcodes @@ -76,16 +93,16 @@ 'language_code', help='The language code. Requires at least one option, like --info') parser.add_argument( - '--de-codex', action='store', help=""" + '--de_codex', action='store', help=""" The main natural language to inspect using some well know language code. """) # This is just in case we start to add new code standards parser.add_argument( - '--de-codex-norma', action='store', default='BCP47', help=""" + '--de_codex_norma', action='store', default='BCP47', help=""" When using the code, specify the coding standard used. Defaults to BCP47 """) parser.add_argument( - '--de-nomen', action='store', help=""" + '--de_nomen', action='store', help=""" The main natural language to inspect using the title of the language in some natural language. """) @@ -118,6 +135,139 @@ parser.add_argument('--version', action='version', version='1.0.0') +class LinguaCodex: + """ + _[eng-Latn] + Command line to process language codes + + Trivia: + - lingua cōdex + - https://en.wiktionary.org/wiki/lingua#Latin + - https://en.wiktionary.org/wiki/codex#Latin + + [eng-Latn]_ + """ + de_codex: str = None + de_nomen: str = None + de_exemplum: str = None + de_codex_norma: str = 'BCP47' + # nomen_lingua: str = None + + def __init__( + self, de_codex: str = None, + de_nomen: str = None, + de_exemplum: str = None, + de_codex_norma: str = 'BCP47' + ): + """LinguaCodex initiāle + """ + if de_codex: + self.de_codex = de_codex + if de_nomen: + self.de_nomen = de_nomen + if de_exemplum: + self.de_exemplum = de_exemplum + if de_codex_norma: + self.de_codex_norma = de_codex_norma + + # def quid(self): + # return LinguaCodexQuid.in_textum_json(self.__dict__) + + def quid(self, language_code, info_in_lang=False): + result = langcodes.Language.get(language_code) + if info_in_lang: + if info_in_lang == 'autonym': + result_item = result.describe(language_code) + else: + result_item = result.describe(info_in_lang) + else: + result_item = result.describe() + + result_item['bcp47'] = langcodes.standardize_tag(language_code) + result_item['autonym'] = langcodes.Language.get( + language_code).autonym() + result_item['speaking_population'] = result.speaking_population() + result_item['writing_population'] = result.writing_population() + result_item['is_valid_syntax'] = langcodes.tag_is_valid(language_code) + + print(json.dumps(result_item)) + # print('ooi', result) + + +@dataclass +class LinguaCodexQuid: + """LinguaCodexQuid + + Trivia: + - fōrmātum, https://en.wiktionary.org/wiki/formatus#Latin + [extended_summary] + """ + lingua_codex: InitVar[Type['LinguaCodex']] = None + + def __init__(self, lingua_codex: Type['LinguaCodex']): + """LinguaCodexQuid initiāle + """ + self.lingua_codex = lingua_codex + + @staticmethod + def in_textum_json( + rem: Any, + formosum: Union[bool, int] = None, + clavem_sortem: bool = False, + imponendum_praejudicium: bool = False + ) -> str: + """Trānslātiōnem: rem in textum JSON + + Trivia: + - rem, https://en.wiktionary.org/wiki/res#Latin + - in, https://en.wiktionary.org/wiki/in#Latin + - json, https://www.json.org/ + - fōrmōsum, https://en.wiktionary.org/wiki/formosus + - impōnendum, https://en.wiktionary.org/wiki/enforcier#Old_French + - praejūdicium, https://en.wiktionary.org/wiki/praejudicium#Latin + - sortem, https://en.wiktionary.org/wiki/sors#Latin + - clāvem, https://en.wiktionary.org/wiki/clavis#Latin + + Args: + rem ([Any]): Rem + + Returns: + [str]: Rem in JSON textum + + Exemplōrum gratiā (et Python doctest, id est, testum automata): + +>>> rem = {"b": 2, "a": ['ت', 'ツ', '😊']} + +>>> LinguaCodexQuid.in_textum_json(rem) +'{"b": 2, "a": ["ت", "ツ", "😊"]}' + +# >>> LinguaCodexQuid.in_textum_json(rem, clavem_sortem=True) +# '{"a": ["ت", "ツ", "😊"], "b": 2}' +# +# >>> LinguaCodexQuid.in_textum_json(rem, imponendum_praejudicium=True) +# '{"b": 2, "a": ["\\\u062a", "\\\u30c4", "\\\ud83d\\\ude0a"]}' +# +# >>> LinguaCodexQuid.in_textum_json(rem, formosum=True) +# '{\\n "b": 2,\\n \ +# "a": [\\n "ت",\\n "ツ",\\n "😊"\\n ]\\n}' + + """ + + # print = json.dumps() + + if formosum is True: + formosum = 4 + + json_textum = json.dumps( + rem, + indent=formosum, + sort_keys=clavem_sortem, + ensure_ascii=imponendum_praejudicium + ) + + return json_textum + + def info(language_code, info_in_lang=False): result = langcodes.Language.get(language_code) if info_in_lang: @@ -182,37 +332,44 @@ def run_cli(args): return info(args.language_code) -class LinguaCodex: - """ - _[eng-Latn] - Command line to process language codes +def run_cli(args): + if args.bcp47: + return bcp47(args.language_code) + if args.is_valid_syntax: + return is_valid_syntax(args.language_code) + if args.speaking_population: + return speaking_population(args.language_code) + if args.writing_population: + return writing_population(args.language_code) + if args.info_in_lang: + return info(args.language_code, args.info_in_lang) + if args.info_in_autonym: + return info(args.language_code, 'autonym') + if args.info: + return info(args.language_code) - Trivia: - - lingua cōdex - - https://en.wiktionary.org/wiki/lingua#Latin - - https://en.wiktionary.org/wiki/codex#Latin + # parser.print_help() + # sys.exit(1) + return info(args.language_code) - [eng-Latn]_ - """ - codex: str = None - nomen: str = None - codex_norma: str = 'BCP47' - nomen_lingua: str = None +# TODO: create a class just to simulate the cli interface +# @see https://stackoverflow.com/questions/50886471 +# /simulating-argparse-command-line-arguments-input-while-debugging - def __init__( - self, codex: str, nomen: str = None, - codex_norma: str = 'BCP47', nomen_lingua: str = None - ): - """LinguaCodex initiāle - """ - if codex: - self.codicem = codex - if nomen: - self.nomen = nomen - if codex_norma: - self.codex_norma = codex_norma - if nomen_lingua: - self.nomen_lingua = nomen_lingua + +# def linguacodex_cli(args): +# https://en.wiktionary.org/wiki/simulatio#Latin + +# https://stackoverflow.com/questions/50886471 +# /simulating-argparse-command-line-arguments-input-while-debugging +# /50886791#50886791 + + +def linguacodex(argumenta: str): + parts = 'linguacodex.py' + ' '.split(argumenta) + sys.argv = parts + args = parser.parse_args() + print(run_cli(args)) if __name__ == '__main__':