API Reference¶
- class chide.Collection(mapping: dict[Type[Any], dict[str, Any]] | None = None)¶
A collection of attributes to use to make sample objects.
- Parameters:
mapping – A dictionary mapping object types to a dictionary of attributes to make a sample object of that type.
- add(obj: T, simplifier: Simplifier[T] = <chide.simplifiers.ObjectSimplifier object>, annotated: Type[T] | None = None, constructor: Type[T] | None = None) None¶
Add the attributes from the supplied object to this collection and use them when samples of the type of that object are required.
- Parameters:
obj – The sample object from which to extract attributes.
simplifier – The
Simplifierto use to extract attributes fromobj.annotated – If
objis an instance of a simple type such as adict, it may be necessary to provided the annotated type for data instance such that it can be correctly added to the collection with that type.constructor – The class to use when constructing objects of this type. This is useful when the type is a parameterized generic but you want to construct using the origin class to avoid
__orig_class__being set.
- attributes(type_: Type[T], **attrs: Any) dict[str, Any]¶
Make the attributes for a sample object of the specified
type_using the default attributes for that type in thisCollection.The
attrsmapping will be overlaid onto the sample attributes and returned as adict.
- make(type_: Type[T], override: Type[T] | None = None, /, **attrs: Any) T¶
Make a sample object of the specified
type_using the default attributes for that type in thisCollection.The
attrsmapping will be overlaid onto the sample attributes before being used withtype_to instantiate and return a new sample object.If
overrideis provided, it will be used to construct the object in place of the supplied type.
- class chide.Set(collection: Collection, identify: Callable[[Type[Any], dict[str, Any]], Hashable | None] | None = None)¶
A collection of sample objects where only one object with a given identity may exist at one time.
- Parameters:
collection – The
Collectioninstance used to create sample objects when necessary.identify –
An
Identifiercallable that takes type_ and attrs parameters.type_, usually a class, is the type of the sample object being requested.
attrs is a
dictof the attributes being requested for the sample object to have.The callable should return a hashable value that indicates the identity to use for the requested sample object. For each unique hashable value, only one sample object will be instantiated and returned each time a sample is requested where this callable returns the given identity.
Nonemay be returned to indicate that a new object should always be returned for the provided parameters.
- identify: Callable[[Type[Any], dict[str, Any]], Hashable | None]¶
You may also want to subclass
Setand implement anidentify()method, seechide.sqlalchemy.Setfor an example.
- get(type_: Type[T], **attrs: Any) T¶
Return an appropriate sample object of the specified
type_.The
attrsmapping will be overlaid onto the sample attributes found in this set’sCollectionbefore checking if an appropriate sample object already exists in the set.If one exists, it is returned. If not, one is created using this set’s
Collection, added to the set and then returned.
- chide.call(factory: Callable[[], T]) T¶
Mark a zero-argument callable so that it is invoked fresh each time a sample object is made via
make().- Parameters:
factory – A zero-argument callable returning
T.- Returns:
A marker object typed as
Tso that mypy accepts it at call sites.
- chide.nest(type_: type[T]) T¶
Mark a registered type so that it is resolved to a sample object when an enclosing sample is made via
make().- Parameters:
type – The type to resolve via the collection at make-time.
- Returns:
A marker object typed as
Tso that mypy accepts it at call sites.
- class chide.factory.Factory(collection: Collection, type_: Type[T], attrs: dict[str, Any])¶
A factory for objects of a particular type. These are created by either
chide.Collection.bind()orchide.factory.Factory.bind().- attributes(**attrs: Any) dict[str, Any]¶
Make the attributes for a sample object of this factory’s type using the default attributes for the type in this factory’s
Collectionoverlaid with any attributes bound into the factory.The
attrsmapping will be overlaid onto those attributes and returned as adict.
- make(type_: Type[T] | None = None, /, **attrs: Any) T¶
Make a sample object of this factory’s type using the default attributes for the type in this factory’s
Collectionoverlaid with any attributes bound into the factory.The
attrsmapping will be overlaid onto the sample attributes before being used to instantiate and return a new sample object of this factory’s type.If a type is provided, it will be used to construct the object in place of the factory’s type.
Markers¶
Typing¶
- chide.typing.Attrs¶
A dictionary of attributes that can be used to create a sample object
Formats¶
- chide.formats.ParseMapping¶
A mapping of column or type names to the
ValueParseto use.
- chide.formats.TypeRenderMapping¶
A mapping of data types to the
ValueRenderto use.
- chide.formats.TypeNameMapping¶
A mapping of data types to the name to use for them when rendering a table.
- chide.formats.ColumnRenderMapping¶
A mapping of column name to the
ValueRenderto use.
- chide.formats.default_parse(text: str) Any¶
The default
ValueParse.
- chide.formats.default_render(value: Any) str¶
The default
ValueRender.
- class chide.formats.TypeLocation(value)¶
Bases:
Enum- HEADER = 1¶
The types are located in parentheses, after the column name, in the row containing the column names.
- ROW = 2¶
The types are located in their own row.
- chide.formats.HEADER = TypeLocation.HEADER¶
Shortcut for
TypeLocation.HEADER.
- chide.formats.ROW = TypeLocation.ROW¶
Shortcut for
TypeLocation.ROW.
- class chide.formats.TabularFormat(type_parse: dict[str, ~typing.Callable[[str], ~typing.Any]] | None = None, default_type_parse: ~typing.Callable[[str], ~typing.Any] = <function default_parse>, column_parse: dict[str, ~typing.Callable[[str], ~typing.Any]] | None = None, type_render: dict[~typing.Type[~typing.Any], ~typing.Callable[[~typing.Any], str]] | None = None, default_type_render: ~typing.Callable[[~typing.Any], str] = <function default_render>, type_names: dict[~typing.Type[~typing.Any], str | None] | None = None, column_render: dict[str, ~typing.Callable[[~typing.Any], str]] | None = None, types_location: ~chide.formats.TypeLocation | None = None)¶
Bases:
FormatA base class for tabular formats.
- class chide.formats.PrettyParsed(attrs: list[dict[str, Any]], widths: list[int])¶
-
A list of
Attrsthat also keeps track of thewidthsrequired to render the columns, if they are known.
- class chide.formats.PrettyFormat(type_parse: dict[str, ~typing.Callable[[str], ~typing.Any]] | None = None, default_type_parse: ~typing.Callable[[str], ~typing.Any] = <function default_parse>, column_parse: dict[str, ~typing.Callable[[str], ~typing.Any]] | None = None, type_render: dict[~typing.Type[~typing.Any], ~typing.Callable[[~typing.Any], str]] | None = None, default_type_render: ~typing.Callable[[~typing.Any], str] = <function default_render>, type_names: dict[~typing.Type[~typing.Any], str | None] | None = None, column_render: dict[str, ~typing.Callable[[~typing.Any], str]] | None = None, types_location: ~chide.formats.TypeLocation | None = None, minimum_column_widths: dict[str, int] | None = None, padding: int = 1)¶
Bases:
TabularFormatA pretty
Formatfor tabular data, where tables look something like:+-----+------+ | x | y | +-----+------+ |float| str | +-----+------+ | 1 | foo | | 2 | bar | +-----+------+
- Parameters:
type_parse – A mapping of type name, as found in either a row or column heading, dependent on the
types_location, to a function that parses the text of a cell into a value.default_type_parse – The default function to use when parsing the text of a cell into a value.
column_parse – A mapping of column names to functions that will be used to parse the text of cells in that column into values.
type_render – A mapping of type objects to functions that will be used to render values of that type to text for cells.
default_type_render – The default function to use when rendingering values to text for cells.
type_names – A mapping of type objects to names to use for those types when including types in either column headings or their own own. If a type is mapped to
None, then no type name will be rendered for columns containing date of that type.column_render – A mapping of column names to functions that will be used to render values in that column to text for cells.
types_location – An optional location from which type information will be parsed or to which it will be rendered. Must be
HEADER,ROWorNone.minimum_column_widths – An optional mapping of column name to the minimum width to use for a column.
padding – The number of space to put to the left and right of values of cells.
- parse(text: str) PrettyParsed¶
Parse the supplied
textinto aPrettyParsed.
- render(attrs: Iterable[dict[str, Any]], ref: list[dict[str, Any]] | PrettyParsed | None = None) str¶
Render the supplied
Attrsinto astr.If supplied,
refis used for reference to make sure:the reference columns are always present.
columns are rendered in the order specified in the reference.
columns widths will be at least as wide as those in the reference.
- class chide.formats.CSVFormat(type_parse: dict[str, ~typing.Callable[[str], ~typing.Any]] | None = None, default_type_parse: ~typing.Callable[[str], ~typing.Any] = <function default_parse>, column_parse: dict[str, ~typing.Callable[[str], ~typing.Any]] | None = None, type_render: dict[~typing.Type[~typing.Any], ~typing.Callable[[~typing.Any], str]] | None = None, default_type_render: ~typing.Callable[[~typing.Any], str] = <function default_render>, type_names: dict[~typing.Type[~typing.Any], str | None] | None = None, column_render: dict[str, ~typing.Callable[[~typing.Any], str]] | None = None, types_location: ~chide.formats.TypeLocation | None = None)¶
Bases:
TabularFormatA
Formatthat parses and renders comma separated values.- Parameters:
type_parse – A mapping of type name, as found in either a row or column heading, dependent on the
types_location, to a function that parses the text of a cell into a value.default_type_parse – The default function to use when parsing the text of a cell into a value.
column_parse – A mapping of column names to functions that will be used to parse the text of cells in that column into values.
type_render – A mapping of type objects to functions that will be used to render values of that type to text for cells.
default_type_render – The default function to use when rendingering values to text for cells.
type_names – A mapping of type objects to names to use for those types when including types in either column headings or their own own. If a type is mapped to
None, then no type name will be rendered for columns containing date of that type.column_render – A mapping of column names to functions that will be used to render values in that column to text for cells.
types_location – An optional location from which type information will be parsed or to which it will be rendered. Must be
HEADER,ROWorNone.
Simplifiers¶
- class chide.simplifiers.Simplifier(*args, **kwargs)¶
Bases:
Protocol[T]Protocol for simplifiers.
- class chide.simplifiers.ObjectSimplifier¶
Bases:
Simplifier[object]A simplifier that can extract attributes from
object-based classes that have either a__dict__or__slots__.
SQLAlchemy¶
- class chide.sqlalchemy.Set(collection: Collection, identify: Callable[[Type[Any], dict[str, Any]], Hashable | None] | None = None)¶
Bases:
SetA specialised
chide.Setfor getting sample declaratively mapped objects when using SQLAlchemy.- static identify(type_: Type[Any], attrs: dict[str, Any]) tuple[Any, ...] | None¶
This method returns the primary key that will be used for the returned object, meaning that only one sample object will exist and be returned for each primary key in a table.
If any element of the primary key is
None, a new object is always returned.
- get(type_: Type[T], **attrs: Any) T¶
Return an appropriate sample object of the specified
type_.The
attrsmapping will be overlaid onto the sample attributes found in this set’sCollectionbefore checking if an appropriate sample object already exists in the set.If one exists, it is returned. If not, one is created using this set’s
Collection, added to the set and then returned.
- class chide.sqlalchemy.RowSimplifier(*args, **kwargs)¶
Bases:
Simplifier[Row[Any]]A simplifier for SQLAlchemy
Rowobjects.