relationlist Documentation¶
What is relationlist?
Sometimes we want more from list. Say you have elements 1 and 4. You want to assign 5 to the values, but don’t want a dict or something like [(1, 4, 5)]. relationlist is here to help you.
Installing is easy. Run this in the command line:
$ python3 -m pip install relationlist
Or this in shell:
>>> __import__('os').system('python3 -m pip install relationlist')
After that, test it:
>>> import relationlist
>>> relationlist.ver.raw
'1.2.1'
>>> # You're ready to use relationlist!
-
class
relationlist.RelationList(iterable=(), /)¶ A class that supports relations.
iterable: An iterable that is converted into a
listand stored invalue.Changed in version 1.1.0: The lis parameter is renamed to iterable and accepts any iterable.
Changed in version 1.1.0: The default value is changed to an empty
tupleinstead ofNoneto reflect defaults oflist()andtuple().
These are the instances of RelationList:
-
RelationList.add(val, /, index=-1)¶ Inserts val to the index given. Equal to
list.insert()/list.append().
-
RelationList.clear_relations()¶ Clears all relations.
-
RelationList.erase_relations()¶ Alias of
clear_relations().
-
RelationList.copy(*, deep=True)¶ Returns
copy.deepcopy()if deep, or elsecopy.copy().
-
RelationList.delete(val, /, *, error='raise') → bool¶ Deletes val from
value.error='raise':ValueErroris raised when val is not invalue.error='ignore': The error is suppressed, andFalseis returned.Trueis returned when the error is not raised or suppressed.
-
RelationList.generator(*, func=False)¶ If
funcisFalse, returns a generator ofvalue, else returns a function that returns the generator when called.Deprecated since version 1.2.0: The
funcparameter is deprecated and will be removed in v1.4. Usegenerator_func()instead.
-
RelationList.generator_func()¶ Returns a function that returns a generator of
valuewhen called.New in version 1.2.0.
-
RelationList.get_relation(val1, val2, /) → tuple¶ Returns a
tupleof the form(val1, val2, mode, assignment)representing the relation between val1 and val2. RaisesValueErrorapon failure.Changed in version 1.1.0:
get_relate()is renamed toget_relation(). The former is kept for backwards compatibility, but warns aDeprecationWarningand will be removed in v1.4.
-
RelationList.get_relations(key, /) → list¶ Returns a
listthat contains all relations related to key in atupleof the form(val1, val2, mode, assignment). RaisesValueErrorapon failure.Changed in version 1.1.0:
ValueErroris raised rather thanKeyErrorto match other methods. Programs that need to support v1.0 and newer should catch both.Changed in version 1.1.0:
get_relates()is renamed toget_relations(). The former is kept for backwards compatibility, but warns aDeprecationWarningand will be removed in v1.4.
-
RelationList.relate(val1, val2, /, mode='break', assignment=None)¶ Creates a relation between val1 and val2.
mode:
'break'or'delete'.mode='break': When val1 or val2 is deleted, the relation is deleted. The other value will not be affected.mode='delete': When val1 or val2 is deleted, the relation is deleted. The other value will be deleted together.assignment: Assign a value to the relation, default
None.
-
RelationList.relate_all(mode='break', assignments=None)¶ Creates relations between all elements.
mode: See
relate()for information.assignments: If not an iterable, assigned to all relations. If is,
assignments[order]is assigned to each relation. The last element will be used ifIndexErroroccurs.Order (
value=[1, 2, 3] assignments=[4, 5, 6]):1↔2: 4 | 1↔3: 5 | 2↔3: 6
Checks if val1 and val2 are related.
Changed in version 1.1.1:
related()now uses the newget_relation()instead of the oldget_relate().
-
RelationList.relations¶ Returns a
listthat contains all relations in atupleof the form(val1, val2, mode, assignment).
-
RelationList.remove_relation(val1, val2, /) → bool¶ Deletes the relation from
value.error='raise':ValueErroris raised when val1 and val2 are not related.error='ignore': The error is suppressed, andFalseis returned.Trueis returned when the error is not raised or suppressed.Changed in version 1.1.0: A
boolis returned instead ofNoneto matchdelete().
-
RelationList.value¶ Returns the base
listin the class. Note changes to this will not effect the one inside.
-
RelationList.__add__(other)¶ Called with
self + other.If other is a
RelationList, returns a deepcopy ofselfthat__iadd__s other.If other is a
list, returns returns a deepcopy ofselfthat extendedvaluewith other.
-
RelationList.__radd__(other)¶ Called with
list + self.
-
RelationList.__iadd__(other)¶ Called with
self += other.If other is a
RelationList, extendsvalue,relationsandelmwith the ones in other.If other is a
list, extendsvaluewith other.
-
RelationList.__delitem__(index)¶ Called with
del self[x:y:z]. Callsdelete()for each element in the range ofvalue.
-
RelationList.__eq__(other) → bool¶ Called with
self == other. If other is aRelationList, returnsTrueifvalueandrelationsare equal, elseFalse.
-
RelationList.__format__(fmt='%L') → str¶ Called with
format(self, fmt).Format table:
Format Code Definition %LInserts value.%RInserts relations.%EInserts elm.%TInserts tuple(value).
-
RelationList.__iter__()¶ Called with
iter(), and converted to different types bylist(),tuple(),dict()andset(). Returnsiter(value).Changed in version 1.1.0:
iter(value)is returned instead ofiter(elm). Programs that need to support older versions should simply switch toiter(elm).
-
RelationList.__or__(other)¶ This is not called by
self or other(That’s done by__bool__()). It’s called byself | other.If other is a
RelationList, returns a newRelationListwithvalueequaling the twovaluesadded, having no relations.If other is a
list, returns a newRelationListwithvalueequalingvalueplus other.
-
RelationList.__ror__(other)¶ This is not called by
other or self(That’s done byother.__bool__()). It’s called byother | self. Same as__or__, but other is inserted beforevalue.
-
RelationList.__repr__() → str¶ Called by
repr(). ReturnsRelationList(value), with[]removed.Changed in version 1.1.0: The
RelationListprefix is added, andrelationsandelmare no longer added. This is an incompatible change.
-
RelationList.__reversed__()¶ Called by
reversed(). Returns a newRelationListwithvaluereversed. Relations are not preserved.
-
RelationList.__setitem__(key, value)¶ Called by
self.[x:y:z] = [...]. Same asvalue[x:y:z] = [...]. All relations related to the replaced values will be processed bydelete().
Context Manager¶
RelationList has __enter__() and __exit__() methods, so it can be used as a context manager. All relations are cleared after the block is exited, and then exceptions will be re-raised if raised.
Example:
>>> import relationlist
>>> with relationlist.RelationList([1, 2, 3]) as x:
... x.relate(1, 2, 'break', 3)
... x.relate(1, 3, 'delete', 4)
... x.relate(1, 4, 'break', 5)
... print(x.relations)
... raise ValueError
...
[(1, 2, 'break', 3), (1, 3, 'delete', 4), (2, 3, 'break', 5)]
Traceback (most recent call last):
File ??, line 6, in <module>
raise ValueError
ValueError
>>> x.relations
[]
relationlist.ver¶
relationlist.ver is a module about relationlist’s version information.
-
ver.raw¶ The raw string of the version, same as the
Version:content inpython3 -m pip show relationlist.
-
ver.version¶ A
namedtuplethat matches the names ofsys.versioninfo, with('major', 'minor', 'micro', 'releaselevel', 'serial').
