Пример #1
0
def _map_attr_to_type_for_class(
    cls: Type,
) -> Dict[attr.Attribute, Tuple[BuildableAttrFieldType, Optional[Type[Enum]]]]:
    """Helper function for _attribute_field_type_reference_for_class to map attributes
    to their BuildableAttrFieldType for a class if the attributes of the class aren't
    yet in the cached _class_structure_reference.
    """
    attr_field_types: Dict[
        attr.Attribute, Tuple[BuildableAttrFieldType, Optional[Type[Enum]]]
    ] = {}

    for attribute in attr.fields_dict(cls).values():
        if is_forward_ref(attribute):
            attr_field_types[attribute] = (BuildableAttrFieldType.FORWARD_REF, None)
        elif is_enum(attribute):
            enum_cls = get_enum_cls(attribute)

            if not enum_cls:
                raise ValueError(
                    f"Did not find enum class for enum attribute [{attribute}]"
                )

            attr_field_types[attribute] = (BuildableAttrFieldType.ENUM, enum_cls)
        elif is_date(attribute):
            attr_field_types[attribute] = (BuildableAttrFieldType.DATE, None)
        else:
            attr_field_types[attribute] = (BuildableAttrFieldType.OTHER, None)

    return attr_field_types
    def _convert_enum(src: SrcBaseType, field: str, attribute: attr.Attribute) -> Enum:
        if isinstance(src, DatabaseEntity):
            enum_cls = get_enum_cls(attribute)
            if enum_cls is None:
                raise ValueError(f"Could not retrieve enum class for {attribute}")
            return enum_cls(getattr(src, field))

        return getattr(src, field).value
Пример #3
0
def _invalid_admitted_from_supervision_admissions_view_builder() -> str:
    applicable_tables = [
        SELECT_FROM_METRICS_TEMPLATE.format(metric_table=table)
        for metric, table in DATAFLOW_METRICS_TO_TABLES.items() if
        (field := attr.fields_dict(metric).get("admission_reason")) is not None
        and get_enum_cls(field) == StateIncarcerationPeriodAdmissionReason
    ]

    return "\n UNION ALL \n".join(applicable_tables)
Пример #4
0
    def extract_enum_value(cls, build_dict: Dict[str, str], field: str, attribute: attr.Attribute) -> Optional[Enum]:
        enum_cls = get_enum_cls(attribute)

        if not enum_cls:
            raise ValueError(f'Did not find enum class for attribute [{attribute}]')

        value = build_dict.get(field)

        if value is None:
            return None

        if not isinstance(value, str) and not isinstance(value, enum_cls):
            raise ValueError(f'Unexpected type [{type(value)}] for value [{value}]')

        return enum_cls(value)
Пример #5
0
    def extract_enum_value(cls, build_dict, field, attribute):
        enum_cls = get_enum_cls(attribute)

        value = build_dict.get(field)

        return enum_cls(value) if value else None
    def _convert_enum(src, field, attribute):
        if isinstance(src, DatabaseEntity):
            enum_cls = get_enum_cls(attribute)
            return enum_cls(getattr(src, field))

        return getattr(src, field).value