bsb.config package¶
Subpackages¶
Submodules¶
bsb.config.refs module¶
This module contains shorthand reference
definitions. References are used in the
configuration module to point to other locations in the Configuration object.
Minimally a reference is a function that takes the configuration root and the current node as arguments, and returns another node in the configuration object:
def some_reference(root, here):
return root.other.place
More advanced usage of references will include custom reference errors.
bsb.config.types module¶
- class bsb.config.types.PackageRequirement[source]¶
Bases:
TypeHandler
- class bsb.config.types.TypeHandler[source]¶
Bases:
ABC
Base class for any type handler that cannot be described as a single function.
Declare the __call__(self, value) method to convert the given value to the desired type, raising a TypeError if it failed in an expected manner.
Declare the __name__(self) method to return a name for the type handler to display in messages to the user such as errors.
Declare the optional __inv__ method to invert the given value back to its original value, the type of the original value will usually be lost but the type of the returned value can still serve as a suggestion.
- class bsb.config.types.class_(module_path=None)[source]¶
Bases:
object_
Type validator. Attempts to import the value as the name of a class, relative to the module_path entries, absolute or just returning it if it is already a class.
- class bsb.config.types.deg_to_radian[source]¶
Bases:
TypeHandler
Type validator. Type casts the value from degrees to radians.
- bsb.config.types.dict(type=<class 'str'>)[source]¶
Type validator for dicts. Type casts each element to the given type.
- Parameters:
type (Callable) – Type validator of the elements.
- Returns:
Type validator function
- Return type:
Callable
- class bsb.config.types.distribution[source]¶
Bases:
TypeHandler
Type validator. Type casts the value or node to a distribution.
- class bsb.config.types.evaluation[source]¶
Bases:
TypeHandler
Type validator. Provides a structured way to evaluate a python statement from the config. The evaluation context provides
numpy
asnp
.- Returns:
Type validator function
- Return type:
Callable
- get_original(value)[source]¶
Return the original configuration node associated with the given evaluated value.
- Parameters:
value (Any) – A value that was produced by this type handler.
- Raises:
NoneReferenceError when value is None, InvalidReferenceError when there is no config associated to the object id of this value.
- bsb.config.types.float(min=None, max=None)[source]¶
Type validator. Attempts to cast the value to an float, optionally within some bounds.
- bsb.config.types.fraction()[source]¶
Type validator. Type casts the value into a rational number between 0 and 1 (inclusive).
- Returns:
Type validator function
- Return type:
Callable
- class bsb.config.types.function_(module_path=None)[source]¶
Bases:
object_
Type validator. Attempts to import the value, absolute, or relative to the module_path entries, and verifies that it is callable.
- bsb.config.types.in_(container)[source]¶
Type validator. Checks whether the given value occurs in the given container. Uses the in operator.
- Parameters:
container (list) – List of possible values
- Returns:
Type validator function
- Return type:
Callable
- bsb.config.types.in_classmap()[source]¶
Type validator. Checks whether the given string occurs in the class map of a dynamic node.
- Returns:
Type validator function
- Return type:
Callable
- bsb.config.types.int(min=None, max=None)[source]¶
Type validator. Attempts to cast the value to an int, optionally within some bounds.
- bsb.config.types.key()[source]¶
Type handler for keys in configuration trees. Keys can be either int indices of a config list, or string keys of a config dict.
- Returns:
Type validator function
- Return type:
Callable
- bsb.config.types.list(type=<class 'str'>, size=None)[source]¶
Type validator for lists. Type casts each element to the given type and optionally validates the length of the list.
- Parameters:
type (Callable) – Type validator of the elements.
size (int) – Mandatory length of the list.
- Returns:
Type validator function
- Return type:
Callable
- bsb.config.types.list_or_scalar(scalar_type, size=None)[source]¶
Type validator that accepts a scalar or list of said scalars.
- bsb.config.types.mut_excl(*mutuals, required=True, max=1, shortform=False)[source]¶
Requirement handler for mutually exclusive attributes.
- Parameters:
- Returns:
Requirement function
- Return type:
Callable
- class bsb.config.types.ndarray(shape: tuple[int, ...] | None = None, dtype=None)[source]¶
Bases:
TypeHandler
Type validator numpy arrays.
- bsb.config.types.number(min=None, max=None)[source]¶
Type validator. If the given value is an int returns an int, tries to cast to float otherwise
- class bsb.config.types.object_(module_path=None)[source]¶
Bases:
TypeHandler
Type validator. Attempts to import the value, absolute, or relative to the module_path entries.
- bsb.config.types.or_(*type_args)[source]¶
Type validator. Attempts to cast the value to any of the given types in order.
- Parameters:
type_args (Callable) – Another type validator
- Returns:
Type validator function
- Raises:
TypeError if none of the given type validators can cast the value.
- Return type:
Callable
- bsb.config.types.scalar_expand(scalar_type, size=None, expand=None)[source]¶
Create a method that expands a scalar into an array with a specific size or uses an expansion function.
Module contents¶
bsb.config module
Contains the dynamic attribute system; Use @bsb.config.root/node/dynamic/pluggable
to
decorate your classes and add class attributes using x =
config.attr/dict/list/ref/reflist
to populate your classes with powerful attributes.
- class bsb.config.Configuration(*args, _parent=None, _key=None, **kwargs)¶
The main Configuration object containing the full definition of a scaffold model.
- class bsb.config.ConfigurationAttribute(type=None, default=None, call_default=None, required=False, key=False, unset=False, hint=<object object>)[source]¶
Bases:
object
Base implementation of all the different configuration attributes. Call the factory function
attr()
instead.
- class bsb.config.Distribution(*args, _parent=None, _key=None, **kwargs)[source]¶
Bases:
object
- distribution: str¶
Base implementation of all the different configuration attributes. Call the factory function
attr()
instead.
- get_node_name()¶
- bsb.config.after(hook, cls, essential=False)[source]¶
Register a class hook to run after the target method.
- bsb.config.attr(**kwargs)[source]¶
Create a configuration attribute.
Only works when used inside a class decorated with the
node
,dynamic
,root
orpluggable
decorators.- Parameters:
type (Callable) – Type of the attribute’s value.
required (bool) – Should an error be thrown if the attribute is not present?
default (Any) – Default value.
call_default (bool) – Should the default value be used (False) or called (True). Use this to prevent mutable default values.
key – If set, the key of the parent is stored on this attribute.
- bsb.config.before(hook, cls, essential=False)[source]¶
Register a class hook to run before the target method.
- bsb.config.catch_all(**kwargs)[source]¶
Catches any unknown key with a value that can be cast to the given type and collects them under the attribute name.
- bsb.config.compose_nodes(*node_classes)[source]¶
Create a composite mixin class of the given classes. Inherit from the returned class to inherit from more than one node class.
- bsb.config.copy_configuration_template(template, output='network_configuration.json', path=None)[source]¶
- bsb.config.dict(**kwargs)[source]¶
Create a configuration attribute that holds a key value pairs of configuration values. Best used only for configuration nodes. Use an
attr()
in combination with atypes.dict
type for simple values.
- bsb.config.dynamic(node_cls=None, attr_name='cls', classmap=None, auto_classmap=False, classmap_entry=None, **kwargs)[source]¶
Decorate a class to be castable to a dynamically configurable class using a class configuration attribute.
Example: Register a required string attribute
class
(this is the default):@dynamic class Example: pass
Example: Register a string attribute
type
with a default value ‘pkg.DefaultClass’ as dynamic attribute:@dynamic(attr_name='type', required=False, default='pkg.DefaultClass') class Example: pass
- bsb.config.format_configuration_content(parser_name: str, config: Configuration, **kwargs)[source]¶
Convert a configuration object to a string using the given parser.
- Parameters:
parser_name (str)
config (Configuration)
- bsb.config.has_hook(instance, hook)[source]¶
Checks the existence of a method or essential method on the
instance
.
- bsb.config.list(**kwargs)[source]¶
Create a configuration attribute that holds a list of configuration values. Best used only for configuration nodes. Use an
attr()
in combination with atypes.list
type for simple values.
- bsb.config.node(node_cls, root=False, dynamic=False, pluggable=False)[source]¶
Decorate a class as a configuration node.
- bsb.config.on(hook, cls, essential=False, before=False)[source]¶
Register a class hook.
- Parameters:
hook (str) – Name of the method to hook.
cls (type) – Class to hook.
essential (bool) – If the hook is essential, it will always be executed even in child classes that override the hook. Essential hooks are only lost if the method on
cls
is replaced.before (bool) – If
before
the hook is executed before the method, otherwise afterwards.
- bsb.config.pluggable(key, plugin_name=None)[source]¶
Create a node whose configuration is defined by a plugin.
Example: If you want to use the attr to chose from all the installed dbbs_scaffold.my_plugin plugins:
@pluggable('attr', 'my_plugin') class PluginNode: pass
This will then read attr, load the plugin and configure the node from the node class specified by the plugin.
- Parameters:
plugin_name (str) – The name of the category of the plugin endpoint
- bsb.config.property(val=None, /, type=None, **kwargs)[source]¶
Create a configuration property attribute. You may provide a value or a callable. Call setter on the return value as you would with a regular property.
- bsb.config.provide(value)[source]¶
Provide a value for a parent class’ attribute. Can be a value or a callable, a readonly configuration property will be created from it either way.
- bsb.config.ref(reference, **kwargs)[source]¶
Create a configuration reference.
Configuration references are attributes that transform their value into the value of another node or value in the document:
{ "keys": { "a": 3, "b": 5 }, "simple_ref": "a" }
With
simple_ref = config.ref(lambda root, here: here["keys"])
the valuea
will be looked up in the configuration object (after all values have been cast) at the location specified by the callable first argument.
- bsb.config.run_hook(obj, hook, *args, **kwargs)[source]¶
Execute the
hook
hook ofobj
.Runs the
hook
methodobj
but also looks through the class hierarchy for essential hooks with the name__<hook>__
.Note
Essential hooks are only ran if the method is called using
run_hook
while non-essential hooks are wrapped around the method and will always be executed when the method is called (see https://github.com/dbbs-lab/bsb/issues/158).
- bsb.config.slot(**kwargs)[source]¶
Create an attribute slot that is required to be overriden by child or plugin classes.
- bsb.config.walk_node_attributes(node)[source]¶
Walk over all of the child configuration nodes and attributes of
node
.- Returns:
attribute, node, parents
- Return type:
Tuple[
ConfigurationAttribute
, Any, Tuple]