Advertisement
HasteBin0

proto-arbegla type topology typology bootstraping function

Oct 13th, 2023 (edited)
1,012
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 12.91 KB | Source Code | 0 0
  1. ### IMPORTS..
  2.  
  3. class BasicArbTypeBundlePart(py_struct):  # component of BasicArbTypeBundleSet
  4.     atb_name: str  # enum member name
  5.     atb_enum_int: Type[IntEnum] | IntEnum  # enum member of integer values
  6.     atb_enum_str: Type[StrEnum] | StrEnum  # enum member of string values
  7.     atb_enum_base: Type[Enum] | Enum  # enum member general-purpose type specific values
  8.     atb_closure: callable  # the function or closure it represents!
  9.  
  10.  
  11. basic_symbol_bundle_parts_t = NewType('basic_symbol_bundle_parts_t', Tuple[str, IntEnum, StrEnum, Enum, callable])  # simple repr of BasicArbTypeBundlePart
  12. basic_symbol_bundle_union_t = NewType('basic_symbol_bundle_union_t', Union[str, IntEnum, StrEnum, Enum, callable, BasicArbTypeBundlePart])  # pre-EMUS domain type for BasicArbTypeBundlePart and its members
  13. basic_symbol_closure_table_t = NewType('basic_symbol_closure_table_t', Mapping[basic_symbol_bundle_union_t, BasicArbTypeBundlePart])  # # resolve any BasicArbTypeBundlePart uniquely through any of it members
  14.  
  15.  
  16. class BasicArbTypeBundleSet(py_struct):  # assemblage of BasicArbTypeBundlePart members
  17.     ats_name: str  # master_type_name
  18.     ats_enum_int: Type[IntEnum]  # the type's dedicated integer-value enum
  19.     ats_enum_str: Type[StrEnum]  # the type's dedicated string-value enum
  20.     ats_enum_base: Type[Enum]  # the type's dedicated reserve-value enum
  21.     ats_closure: basic_symbol_closure_table_t  # member to BasicArbTypeBundlePart resolver
  22.     ats_array: Tuple[basic_symbol_bundle_parts_t, ...]  # int-enum-ordered array of BasicArbTypeBundlePart members
  23.  
  24.  
  25. def new_basic_arb_type(master_type_name: str, *members: basic_symbol_bundle_parts_t, int_enum: Type[IntEnum], str_enum: Type[StrEnum], base_enum: Type[Enum]) -> BasicArbTypeBundleSet:
  26.     """
  27.     Create or retrieve a new Arbegla basic type bundle set based on provided parameters.
  28.  
  29.     Function Input:
  30.         - master_type_name (str): The master type name representing the Arbegla type.    It should be a unique string identifier and follow specific naming conventions.
  31.         - *members (basic_symbol_bundle_parts_t): A variable length tuple containing the member details for the Arbegla type. Each member should contain:
  32.             1. Enum member name (str)
  33.             2. Enum member of integer values (IntEnum)
  34.             3. Enum member of string values (StrEnum)
  35.             4. Enum member of general-purpose specific values (Enum)
  36.             5. A function or closure it represents (callable)
  37.         - int_enum (Type[IntEnum]): The dedicated integer-value enum type for the new Arbegla type.
  38.         - str_enum (Type[StrEnum]): The dedicated string-value enum type for the new Arbegla type.
  39.         - base_enum (Type[Enum]): The dedicated reserve-value enum type for the new Arbegla type.
  40.  
  41.     Returns:
  42.         - BasicArbTypeBundleSet: A data structure representing the complete Arbegla type, including all its components and members.
  43.  
  44.     Behavior:
  45.         - If an Arbegla type with the same master_type_name exists in the global type repository (__GLOBAL_PY_ARB_BASE_TYPE_REPO__), the function returns the existing type.
  46.         - Otherwise, it creates a new Arbegla type, ensuring that it adheres to naming conventions, has valid member definitions, and the member count matches the provided enum types.
  47.         - The function will raise RuntimeError with detailed descriptions if inconsistencies are found during type creation.
  48.         - Upon successful type creation, it updates the global type repository and another mapping (__GLOBAL_TOP_LEVEL_SYMBOL_TSS__) with the new type details.
  49.  
  50.     Notes:
  51.         - The function heavily relies on multiple global data structures and helper functions for error-checkin and validation, including __GLOBAL_PY_ARB_BASE_TYPE_REPO__, __GLOBAL_TOP_LEVEL_SYMBOL_TSS__, and more.
  52.         - It's important to ensure the integrity of these global structures and the consistency of provided members and enums when using this function.
  53.  
  54.     Raises:
  55.         - RuntimeError: When inconsistencies are found in the provided members, enums, or naming conventions.
  56.         - TypeError: When retrieving an existing type but the enums provided don't match the original ones.
  57.  
  58.     Dependencies:
  59.        - Uses several helper functions such as is_isb_enum_type, check_type_map_table, and more to assist with its operations.
  60.     """
  61.    
  62.     elem_tri_type: Tuple[Type[IntEnum], Type[StrEnum], Type[Enum]] = (int_enum, str_enum, base_enum)
  63.    
  64.     if (tmp_a := (__GLOBAL_PY_ARB_BASE_TYPE_REPO__.get(master_type_name, None))) is not None:
  65.         answer: BasicArbTypeBundleSet = tmp_a
  66.         ga_types: Type[elem_tri_type] = (answer.ats_enum_int, answer.ats_enum_str, answer.ats_enum_base)
  67.         if not (ga_types == elem_tri_type):  #
  68.             raise TypeError(f'In retrieving the basic Arbegla {repr(master_type_name)} type, the {repr(ga_types)} enum must be given to this function (got {repr(elem_tri_type)} instead).') \
  69.                 from ArbeglaBaseEntityError(answer, RuntimeError, elem_tri_type, ga_types, typing = True)
  70.         return answer
  71.     del tmp_a  # keep it tidy
  72.    
  73.     num_elements: int = len(members)
  74.     illegal_var_chars: Set[str] = {'(UNDETERMINED)'}
  75.     if not (num_elements >= 1 and isinstance(master_type_name, str) and len(master_type_name) >= 5 and
  76.             (master_type_name in __GLOBAL_TOP_LEVEL_SYMBOL_TSS__ or len(illegal_var_chars := (set(master_type_name).difference(valid_english_character_frozenset)))) == 0):
  77.         raise RuntimeError(
  78.             f'All basic Arbegla data types must have at least one member ({num_elements} given) and a type name string ({repr(master_type_name)} given) of ≥5 characters ({len(master_type_name)} given) '
  79.             f'from the string {repr(valid_english_character_string)} ({{({", ".join(map(repr, illegal_var_chars))}) don\'t conform}} has length {len(illegal_var_chars)}); '
  80.             f'and, it must be unique — the __GLOBAL_TOP_LEVEL_SYMBOL_TSS__ has {"an" if master_type_name in __GLOBAL_TOP_LEVEL_SYMBOL_TSS__ else "no"} entry.')
  81.     if illegal_var_chars == {'(UNDETERMINED)'} or len(illegal_var_chars) != 0:
  82.         raise RuntimeError(f'The Arbegla basic type name {repr(master_type_name)} must have characters only from {repr(valid_english_character_frozenset)}.')
  83.     del illegal_var_chars  # descope now unneeded set.
  84.    
  85.     tuple_enum_master_record: Tuple[Tuple[Type[IntEnum], Type[StrEnum], Type[Enum]], ...] = ()
  86.     if not (is_isb_enum_type(int_enum, str_enum, base_enum) and (len(int_enum) == len(str_enum) == len(base_enum) == num_elements) and
  87.             len((tuple_enum_master_record := (tuple(zip(int_enum, str_enum, base_enum)))) == num_elements) and
  88.             all((test for (ei, (xi, xs, xb)) in enumerate(tuple_enum_master_record) for test in
  89.                  (is_isb_enum_type(xi, xs, xb), (xi.__class__ is int_enum and xs.__class__ is str_enum and xb.__class__ is base_enum),
  90.                   (tuple(map(type, (xi, xs, xb))) == elem_tri_type), (xi.name == xs.name == xb.name), (ei == xi.value),))) and
  91.             all(isinstance(x_member, tuple) and len(x_member) == 5 for x_member in members)):
  92.         raise RuntimeError(
  93.             f'The (int_enum, str_enum, base_enum) ({repr(elem_tri_type)} given) must all have {num_elements} members and the int_enum must increment from zero [expected {repr(range(num_elements))}, got {repr(tuple(int_enum))}] and '
  94.             f'all three enums must have {repr(set(xv.name for xe in map(tuple, elem_tri_type) for xv in xe))} ({(repr(tuple(int_enum)))} in order for all three enums).')
  95.     del num_elements  # keep it tidy still
  96.    
  97.     if not (all((test for (xi, xs, xb), (member_name, member_xim, member_xsm, member_xbm, member_closure) in zip(tuple_enum_master_record, members) for test in (
  98.             (xi.name == member_xim.name == member_xsm.name == member_xbm.name == member_name), (callable(member_closure)),)))):
  99.         raise RuntimeError(
  100.             f'The members must have, for each triplet of ({repr(elem_tri_type)}), enum key names in {repr(tuple(ixe.name for ixe, _, __ in tuple_enum_master_record))} and in that order; and, '
  101.             f'the members must have a callable to which each tuple ({repr(elem_tri_type)}) [enum key names] is tied.')
  102.    
  103.     ats_tmp_closure: basic_symbol_closure_table_t = {}
  104.     ats_tmp_array: Deque[Optional[BasicArbTypeBundlePart]] = deque()
  105.     for (xi, xs, xb), (member_name, member_xim, member_xsm, member_xbm, member_closure) in zip(tuple_enum_master_record, members):
  106.         ats_tmp_array.append((new_member := BasicArbTypeBundlePart(member_name, member_xim, member_xsm, member_xbm, member_closure)))
  107.         ats_tmp_closure[xi.name] = new_member
  108.         for xt in xi, xs, xb:
  109.             ats_tmp_closure[xt.value] = new_member
  110.    
  111.     ats_tmp_closure: basic_symbol_closure_table_t = frozendict(ats_tmp_closure)  # freeze lookup mappings
  112.     ats_tmp_array: Tuple[basic_symbol_bundle_parts_t, ...] = tuple(ats_tmp_array)  # freeze the type's members' array
  113.    
  114.     check_lookup_tests_sequence: Tuple[Tuple[int, type_map_check_return_t], ...] = tuple(enumerate(map(check_type_map_table(ats_tmp_closure), ats_tmp_array)))
  115.    
  116.     x_bap_answers: Tuple[basic_symbol_bundle_union_t, ...]
  117.     x_bap_o: Optional[BasicArbTypeBundlePart] = None
  118.     x_bap_true: basic_symbol_bundle_parts_t
  119.     x_bap_test: basic_symbol_bundle_parts_t
  120.     for x_bap_i, (x_bap_answers, x_bap_o, x_bap_true, x_bap_test) in check_lookup_tests_sequence:
  121.         if not ((x_bap_o is not None and x_bap_i == x_bap_o.atb_enum_int.value) and (x_bap_true == x_bap_test) and (x_bap_answers == x_bap_test)):
  122.             res = (x_bap_answers, x_bap_true, x_bap_test)
  123.             raise RuntimeError(  # save all the progress for downstream debugging.
  124.                 f'While inspecting the new Arbegla type\'s {repr(x_bap_o) if x_bap_o is not None else "(unknown)"} at index {x_bap_i}, the mapping (x_bap_answers, x_bap_true, x_bap_test) of {repr(res)} need '
  125.                 f'to map to [0] all ({repr(x_bap_o)},) * {len(x_bap_answers)} (got {repr(x_bap_answers)}), [1] same (got {repr(x_bap_true)}), and [2] same (got {repr(x_bap_test)}).') \
  126.                 from ArbeglaBaseEntityError(master_type_name, RuntimeError, ats_tmp_closure, ats_tmp_array, *res,
  127.                                             x_objext = x_bap_o, x_index = x_bap_i, members = members, check_lookup_tests_sequence = check_lookup_tests_sequence)
  128.    
  129.     if x_bap_o is None:
  130.         raise RuntimeError(  # save all the progress for downstream debugging.
  131.             f'No iterations upon {repr(check_lookup_tests_sequence)} detected. Internal error.') \
  132.             from ArbeglaBaseEntityError(master_type_name, RuntimeError, ats_tmp_closure, ats_tmp_array,
  133.                                         members = members, check_lookup_tests_sequence = check_lookup_tests_sequence)
  134.    
  135.     new_arbegla_type_object = BasicArbTypeBundleSet(master_type_name, int_enum, str_enum, base_enum, ats_tmp_closure, ats_tmp_array)  # success!
  136.     __GLOBAL_PY_ARB_BASE_TYPE_REPO__[master_type_name] = new_arbegla_type_object  # BasicArbTypeBundleSet
  137.     __GLOBAL_TOP_LEVEL_SYMBOL_TSS__[master_type_name] = {new_arbegla_type_object}  # Set[basic_symbol_bundle_union_t] | FrozenSet[basic_symbol_bundle_union_t]
  138.     return new_arbegla_type_object
  139.  
  140.  
  141. __GLOBAL_PY_ARB_BASE_TYPE_REPO__: Dict[str, BasicArbTypeBundleSet] = {}  # fast name lookup
  142. __GLOBAL_TOP_LEVEL_SYMBOL_TSS__: MutableMapping[basic_symbol_bundle_union_t, Set[basic_symbol_bundle_union_t] | FrozenSet[basic_symbol_bundle_union_t]] = {}  # general member-wise lookup
  143.  
  144. type_map_check_return_t = NewType(  # (x_bap_answers, x_bap_o, x_bap_m_true, x_bap_m_test)
  145.     'type_map_check_return_t', Tuple[Tuple[basic_symbol_bundle_union_t, ...], Optional[BasicArbTypeBundlePart], basic_symbol_bundle_parts_t, basic_symbol_bundle_parts_t])
  146.  
  147.  
  148. def check_type_map_table(ats_tmp_closure: basic_symbol_closure_table_t) -> Callable[[basic_symbol_bundle_parts_t], type_map_check_return_t]:
  149.     def _i(bundle: basic_symbol_bundle_parts_t) -> type_map_check_return_t:  # (x_bap_answers, x_bap_o, x_bap_m_true, x_bap_m_test)
  150.         read_from_mapping = ats_tmp_closure.get  # a closure!
  151.         answers: Tuple[basic_symbol_bundle_union_t, ...] = tuple(map(read_from_mapping, bundle))
  152.         if None not in answers:
  153.             true_int_member: BasicArbTypeBundlePart = answers[1]  # load its IntEnum member
  154.             return answers, true_int_member, bundle, tuple(map(read_from_mapping, unpack_basic_type_bundle(true_int_member)))
  155.         return answers, None, bundle, ()
  156.    
  157.     return _i
  158.  
  159.  
  160. def unpack_basic_type_bundle(btb: BasicArbTypeBundlePart) -> basic_symbol_bundle_parts_t:
  161.     return btb.atb_name, btb.atb_enum_int, btb.atb_enum_str, btb.atb_enum_base, btb.atb_closure
  162.  
  163.  
  164. def is_isb_enum_type(int_enum: Type[IntEnum], str_enum: Type[StrEnum], base_enum: Type[Enum]) -> bool:
  165.     return (issubclass(int_enum.__class__, IntEnum) and
  166.             issubclass(str_enum.__class__, StrEnum) and
  167.             issubclass(base_enum.__class__, Enum))
  168.  
Tags: utility
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement