Skip to content

Commit

Permalink
Let's make a few examples
Browse files Browse the repository at this point in the history
  • Loading branch information
daquinteroflex committed Oct 17, 2024
1 parent f471b67 commit 97d1cee
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 31 deletions.
Empty file.
33 changes: 33 additions & 0 deletions autoflex/descriptors/field.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from dataclasses import field
from typing import Any, Optional
from pydantic import Field
from autoflex.types import AutoflexFieldInfo, AutoflexParameterTypes


def AutoflexField(
default: Any = ...,
*,
autoflex_parameters: Optional[AutoflexParameterTypes] = None,
**kwargs
) -> AutoflexFieldInfo:
"""
A wrapper around pydantic's Field function that returns an instance of AutoflexFieldInfo
instead of FieldInfo.
Args:
default: The default value of the field.
autoflex_parameters: An additional argument specific to AutoflexFieldInfo.
**kwargs: Any other keyword arguments passed to pydantic's Field.
Returns:
AutoflexFieldInfo: Custom field info object.
"""

field_info = Field(default=default, **kwargs) # Call pydantic's Field internally

# Return an instance of AutoflexFieldInfo instead of the default FieldInfo
return AutoflexFieldInfo(
default=default,
autoflex=autoflex_parameters,
**field_info.dict(exclude_none=True)
)
3 changes: 3 additions & 0 deletions autoflex/types/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from autoflex.types.parameters import PhysicalParameter, AutoflexParameterTypes
from autoflex.types.descriptors import Unit, Symbolic, SymbolicTypes
from autoflex.types.field import AutoflexFieldInfo
3 changes: 2 additions & 1 deletion autoflex/types/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
class AutoflexBaseModel(BaseModel):
"""
A base class that can be used for any model within the system.
It inherits from Pydantic's BaseModel to leverage data validation
It inherits from Pydantic BaseModel to leverage data validation
and parsing features.
"""
pass
22 changes: 3 additions & 19 deletions autoflex/types/descriptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class Symbolic(AutoflexBaseModel):
math: str = Field(..., description="Mathematical representation or equation")


SymbolicTypes = Union[str, Symbolic]

class Unit(AutoflexBaseModel):
"""
A class representing a physical unit.
Expand All @@ -28,23 +30,5 @@ class Unit(AutoflexBaseModel):
description: An optional description of the unit.
"""
name: str = Field(..., description="Name of the unit")
symbol: str | Symbolic = Field(..., description="Symbol for the unit")
symbol: SymbolicTypes = Field(..., description="Symbol for the unit")
description: str = Field(None, description="Optional description of the unit")

class PhysicalParameter(AutoflexBaseModel):
"""
A class representing a physical parameter, which includes both
the unit and its defining mathematical representation.
Attributes:
unit: Can either be a simple string, Symbolic class, or Unit class representing the unit.
equation: The equation associated with the physical parameter, which could be symbolic or a string.
"""
unit: Union[str, Symbolic, Unit] = Field(..., description="The unit of the physical parameter")
math: Union[str, Symbolic] = Field(..., description="The mathematical representation defining the physical parameter in latex")

AutoflexParameterTypes = PhysicalParameter


class AutoflexFieldInfo(pydantic.fields.FieldInfo):
autoflex: AutoflexParameterTypes
8 changes: 8 additions & 0 deletions autoflex/types/field.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import pydantic as pd
from autoflex.types.parameters import AutoflexParameterTypes

class AutoflexFieldInfo(pd.fields.FieldInfo):
"""
Each field should correspond to an individual parameter field that can represent it completely within the documentation.
"""
autoflex: AutoflexParameterTypes
20 changes: 20 additions & 0 deletions autoflex/types/parameters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from pydantic import Field
from autoflex.types.core import AutoflexBaseModel
from autoflex.types.descriptors import Unit, SymbolicTypes

class PhysicalParameter(AutoflexBaseModel):
"""
This structure instance is a representation of the relevant information that might want to represent in a parameter
row or in another container.
We need the parameter name, the type definition in a format we might want to represent (or even interact with)
a description which may be
"""
name: str = ""
types: str = ""
description: str = ""
math: SymbolicTypes = Field(..., description="The mathematical representation defining the physical parameter in raw string latex")
unit: Unit = Field(..., description="The unit of the physical parameter")


AutoflexParameterTypes = PhysicalParameter
9 changes: 0 additions & 9 deletions autoflex/types/structures.py

This file was deleted.

4 changes: 4 additions & 0 deletions docs/api/descriptors.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
Descriptors
------------

Part of the goal of using dedicated ``AutoflexField`` definitions is that we can compile this data into a nice way
to visualise it both in the terminal and on the web. It can be used as a more helpful visualisation tool than the standard
type descriptions.
4 changes: 2 additions & 2 deletions docs/structure.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Software Architecture Structure
================================
Implementation Structure
=========================

``autoflex`` is divided in the following conceptual structure:

Expand Down

0 comments on commit 97d1cee

Please sign in to comment.