Skip to content

pydantic

CamelModel

Bases: BaseModel

Allows models to be defined with camel case properties.

See

https://medium.com/analytics-vidhya/camel-case-models-with-fast-api-and-pydantic-5a8acb6c0eee

Source code in m/pydantic.py
class CamelModel(BaseModel):
    """Allows models to be defined with camel case properties.

    See:
        https://medium.com/analytics-vidhya/camel-case-models-with-fast-api-and-pydantic-5a8acb6c0eee
    """

    model_config = ConfigDict(
        alias_generator=to_camel,
        populate_by_name=True,
    )

KebabModel

Bases: BaseModel

Allows models to be defined with kebab case properties.

Inputs and outputs need to be written using a KebabModel as a base class. This is so that their definitions may be written using kebab casing in the final action.yaml.

from m.github.actions import KebabModel, InArg

class MyInput(KebabModel):
    my_input: str = InArg(help='description')
Source code in m/pydantic.py
class KebabModel(BaseModel):
    """Allows models to be defined with kebab case properties.

    Inputs and outputs need to be written using a `KebabModel` as a base class.
    This is so that their definitions may be written using kebab casing in the
    final `action.yaml`.

    ```python
    from m.github.actions import KebabModel, InArg

    class MyInput(KebabModel):
        my_input: str = InArg(help='description')
    ```
    """

    model_config = ConfigDict(
        alias_generator=to_kebab,
        populate_by_name=True,
    )

load_model(model, file_path, transform=None)

Load a model from a json or yaml file.

Parameters:

Name Type Description Default
model type[GenericModel]

The class to create an instance of.

required
file_path str

The path to the file.

required
transform DataTransformer | None

A function to transform the data before creating the model.

None

Returns:

Type Description
Res[GenericModel]

A OneOf with the model or an issue.

Source code in m/pydantic.py
def load_model(
    model: type[GenericModel],
    file_path: str,
    transform: DataTransformer | None = None,
) -> Res[GenericModel]:
    """Load a model from a json or yaml file.

    Args:
        model: The class to create an instance of.
        file_path: The path to the file.
        transform: A function to transform the data before creating the model.

    Returns:
        A `OneOf` with the model or an issue.
    """
    context = {'file_path': file_path, 'model': str(model)}
    transform_function = transform or Good
    return one_of(
        lambda: [
            model_inst
            for _ in assert_file_exists(file_path)
            for model_data in read_yson(file_path)
            for transformed_data in transform_function(model_data)
            for model_inst in parse_model(model, transformed_data)
        ],
    ).flat_map_bad(hone('pydantic.load_model_failure', context))

parse_model(model, model_data)

Parse a python object using pydantics TypeAdapter.

Parameters:

Name Type Description Default
model type[GenericModel]

The class to create an instance of.

required
model_data Any

The data to parse.

required

Returns:

Type Description
Res[GenericModel]

A OneOf with the model or an issue.

Source code in m/pydantic.py
def parse_model(model: type[GenericModel], model_data: Any) -> Res[GenericModel]:
    """Parse a python object using pydantics TypeAdapter.

    Args:
        model: The class to create an instance of.
        model_data: The data to parse.

    Returns:
        A `OneOf` with the model or an issue.
    """
    try:
        return Good(TypeAdapter(model).validate_python(model_data))
    except Exception as ex:
        return issue('parse_model_failure', cause=ex)

to_camel(snake_case)

Transform a string in snake_case to camel case.

Parameters:

Name Type Description Default
snake_case str

string in snake case.

required

Returns:

Type Description
str

string in camel case.

Source code in m/pydantic.py
def to_camel(snake_case: str) -> str:
    """Transform a string in snake_case to camel case.

    Args:
        snake_case: string in snake case.

    Returns:
        string in camel case.
    """
    s = sub('(_|-)+', ' ', snake_case).title().replace(' ', '')
    return ''.join([s[0].lower(), s[1:]])

to_kebab(snake_case)

Transform a string in snake_case to kebab case.

Parameters:

Name Type Description Default
snake_case str

string in snake case.

required

Returns:

Type Description
str

string in kebab case.

Source code in m/pydantic.py
def to_kebab(snake_case: str) -> str:
    """Transform a string in snake_case to kebab case.

    Args:
        snake_case: string in snake case.

    Returns:
        string in kebab case.
    """
    return snake_case.replace('_', '-', -1)