model#
This submodule presents ndf entities in a form of easy(ish)-to-work structures.
Abstract Classes#
- class DeclListRow#
Abstract class for rows of data in list-like structures of the model.
It serves as a foundation for any classes that are meant to store assigned items along with any related metadata (variable names, visibility modifiers, mapping keys etc.)
- parent#
List-like object to which this row belongs.
- Type:
DeclarationsList[Self]
- edit(_strict=True, **kwargs) Self #
Edit current row with multiple values (except parent and index).
This method allows to edit a bunch of values in the row at once. It also accepts properties’ aliases as well as easily unpacking an output of an
ndf_parse.expression()
.Note
You cannot pass mutually exclusive keys at once, for example, you cannot pass
namespace=...
along withn=...
toListRow.edit()
, it will raise an error.- Parameters:
- Return type:
Self
Examples
>>> import ndf_parse as ndf >>> source = ndf.convert(b"Test is TObject(Member1: int = 1)") >>> obj = source[0].v # `v` is an alias for `value` >>> ndf.printer.print(obj) TObject ( Member1: int = 1 ) >>> # note there was no "Test is", because we've printed only the value, not the row >>> m1 = obj[0] >>> print(m1) # check the row Object[0](member='Member1', type='int', visibility=None, namespace=None, value='1') >>> expr = ndf.expression("MemberRenamed = Namespace is 2") >>> print(expr) {'value': '2', 'namespace': 'Namespace', 'member': 'MemberRenamed'} >>> m1.edit(**expr) # edit row via dict decomposition Object[0](member='MemberRenamed', type='int', visibility=None, namespace='Namespace', value='2') >>> ndf.printer.print(obj) TObject ( MemberRenamed: int = Namespace is 2 ) >>> m1.edit(v='3', n='Ns', vis="export") # edit using aliases Object[0](member='MemberRenamed', type='int', visibility='export', namespace='Ns', value='3') >>> m1.edit(nonexistent="4", _strict=False) # silently ignores Object[0](member='MemberRenamed', type='int', visibility='export', namespace='Ns', value='3') >>> m1.edit(nonexistent="4") # Raises an error TypeError: Cannot set MemberRow.nonexistent, attribute does not exist.
- as_dict()#
Outputs given row in a form of a dict.
Warning
It does not perform copy for it’s value, so avoid using it to copy a row from one place to another. More on referencing restrictions.
- class DeclarationsList#
Bases:
List
[DeclListRow_co
]Abstract class for list-like objects of this model. It is used to store rows that are subclasses of
DeclListRow
.- parent#
Parent of this object. All objects of this class and it’s subclasses are expected to have it NOT equal to
None
. Exceptions: root-levelList
and objects that are generated withndf_parse.expression()
and not yet added to some parent.- Type:
DeclarationsList | None
- add(_strict=True, **kwargs) DeclListRow #
Builds and adds a new row from given arguments.
- Parameters:
Examples
>>> import ndf_parse as ndf >>> source = ndf.model.List(is_root = True) >>> source [] # all DeclarationLists in model derive from list hence the look >>> # add an item >>> source.add(namespace="Two", value="2") List[0](visibility=None, namespace='Two', value='2') >>> # use aliases >>> source.add(vis='export', n="Text", v="'My text.'") List[1](visibility='export', namespace='Text', value="'My text.'") >>> # silently skip non-existent arguments >>> source.add(n="Four", v="4", nonexistent='blah', _strict = False) List[2](visibility=None, namespace='Four', value='4') >>> # fail on non-existent arguments (default behaviour) >>> source.add(n="Four", v="4", nonexistent='blah') TypeError: Cannot set ListRow.nonexistent, attribute does not exist. >>> ndf.printer.print(source) Two is 2 export Text is 'My text.' Four is 4
- insert(index, _strict=True, **kwargs) DeclListRow #
Builds and inserts a new row from given arguments into a given place.
Same logic applies as in
add()
method, just with addition of an index as first positional argument.
Model Classes#
Row Classes#
- class ListRow(parent, value, visibility=None, namespace=None)#
Bases:
DeclListRow
Row of data from a
List
object.- visibility#
Visibility modifier of the assignment. Should be one of these:
'unnamed'
|'export'
|'private'
|'public'
Keep in mind that it won’t protect from'unnamed'
actually having a name or appearing multiple times the List, see notes on checking strictness.- Type:
str | None
- vis#
An alias for visibility.
- n#
An alias for namespace.
- v#
An alias for value.
- class MapRow(parent, key, value)#
Bases:
DeclListRow
Row of data from a
Params
object.- k#
An alias for key.
- v#
An alias for value.
- class MemberRow(parent, value, member=None, type=None, visibility=None, namespace=None)#
Bases:
DeclListRow
Row of data from
Object
andTemplate
objects.- type#
Typing data for this object. Keep in mind, not all types are stored here.
- Type:
str | None
- visibility#
Visibility modifier of the assignment. Should be one of these:
'export'
|'private'
|'public'
- Type:
str | None
- vis#
An alias for visibility.
- m#
An alias for member.
- t#
An alias for type.
- n#
An alias for namespace.
- v#
An alias for value.
- class ParamRow(parent, param=None, type=None, value=None)#
Bases:
DeclListRow
Row of data from a
Params
object.- type#
Typing data for this parameter. Keep in mind, not all types are stored here.
- Type:
str | None
- p#
An alias for param.
- t#
An alias for type.
- v#
An alias for value.
List-like Classes#
- class List(is_root: bool = False, type: str | None = None)#
Bases:
DeclarationsList
[ListRow
]List represents ndf lists (
[]
), vector types (typename[]
) and a collection of root level statements (source root).- is_root#
Indicates whether this is a source root or any other nested item. Needed for
printer
format them differently.- Type:
bool, default=False
- type#
Stores type for vector types (like
RGBA[0, 0, 0, 1]
). See Typing Ambiguity in main documentation for more info.- Type:
str | None, default=None
- by_n(namespace: str) ListRow #
- by_n(namespace: str, strict: bool) ListRow | None
- by_name(namespace: str) ListRow #
- by_name(namespace: str, strict: bool) ListRow | None
- class Object#
Bases:
DeclarationsList
[MemberRow
]Object represents ndf objects as a list of members.
- type#
Stores object’s type (for
TObject( ... )
it’s type will be equal toTObject
). See Typing Ambiguity in main documentation for more info.- Type:
str | None, default=None
- by_n(namespace: str) MemberRow #
- by_n(namespace: str, strict: bool) MemberRow | None
- by_name(namespace: str) MemberRow #
- by_name(namespace: str, strict: bool) MemberRow | None
- by_namespace(namespace: str) MemberRow #
- by_namespace(namespace: str, strict: bool) MemberRow | None
Find row by it’s namespace. Returns first match that is found. If none found and strict is
True
then raises an error. IfFalse
then returns None. If strict is not set then it’sTrue
by default.
- remove_by_namespace(namespace: str)#
Find and remove row by it’s namespace. Removes first occurence if found. Raises error if nothing found.
- class Template#
Bases:
Object
Template represents ndf templates as a list of members and template params.
- type#
Stores template’s type (for
template [ ... ] Ns is TObject( ... )
it’s type will be equalTObject
). See Typing Ambiguity in main documentation for more info.- Type:
str | None, default=None
- by_n(namespace: str) MemberRow #
- by_n(namespace: str, strict: bool) MemberRow | None
- by_name(namespace: str) MemberRow #
- by_name(namespace: str, strict: bool) MemberRow | None
- by_namespace(namespace: str, strict: bool = True) MemberRow | None #
Find row by it’s namespace. Returns first match that is found. If none found and strict is
True
then raises an error. IfFalse
then returns None. If strict is not set then it’sTrue
by default.
- remove_by_namespace(namespace: str)#
Find and remove row by it’s namespace. Removes first occurence if found. Raises error if nothing found.
- class Params#
Bases:
DeclarationsList
[ParamRow
]Params represents a list of generic parameters to be used in a template.
- class Map#
Bases:
DeclarationsList
[MapRow
]Map represents ndf maps as a list of pairs represented as a
MapRow
. It supports checking if key is inside of it in pythonic way:>>> pairs = Map() >>> pairs.add('test', 'some_value') >>> 'test' in pairs True >>> 'test2' in pairs False >>> 'some_value' in pairs # checks only keys, not values! False