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 list and stored in value.

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 tuple instead of None to reflect defaults of list() and tuple().

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 else copy.copy().

RelationList.delete(val, /, *, error='raise') → bool

Deletes val from value.

error='raise': ValueError is raised when val is not in value.

error='ignore': The error is suppressed, and False is returned.

True is returned when the error is not raised or suppressed.

RelationList.elm

A dict that contains count of relations for values in value.

RelationList.generator(*, func=False)

If func is False, returns a generator of value, else returns a function that returns the generator when called.

Deprecated since version 1.2.0: The func parameter is deprecated and will be removed in v1.4. Use generator_func() instead.

RelationList.generator_func()

Returns a function that returns a generator of value when called.

New in version 1.2.0.

RelationList.get_relation(val1, val2, /) → tuple

Returns a tuple of the form (val1, val2, mode, assignment) representing the relation between val1 and val2. Raises ValueError apon failure.

Changed in version 1.1.0: get_relate() is renamed to get_relation(). The former is kept for backwards compatibility, but warns a DeprecationWarning and will be removed in v1.4.

RelationList.get_relations(key, /) → list

Returns a list that contains all relations related to key in a tuple of the form (val1, val2, mode, assignment). Raises ValueError apon failure.

Changed in version 1.1.0: ValueError is raised rather than KeyError to 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 to get_relations(). The former is kept for backwards compatibility, but warns a DeprecationWarning and 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 if IndexError occurs.

Order (value=[1, 2, 3] assignments=[4, 5, 6]):

1↔2: 4 | 1↔3: 5 | 2↔3: 6

RelationList.related(val1, val2, /) → bool

Checks if val1 and val2 are related.

Changed in version 1.1.1: related() now uses the new get_relation() instead of the old get_relate().

RelationList.relations

Returns a list that contains all relations in a tuple of the form (val1, val2, mode, assignment).

RelationList.remove_relation(val1, val2, /) → bool

Deletes the relation from value.

error='raise': ValueError is raised when val1 and val2 are not related.

error='ignore': The error is suppressed, and False is returned.

True is returned when the error is not raised or suppressed.

Changed in version 1.1.0: A bool is returned instead of None to match delete().

RelationList.value

Returns the base list in 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 of self that __iadd__ s other.

If other is a list, returns returns a deepcopy of self that extended value with other.

RelationList.__radd__(other)

Called with list + self.

Same as __add__(), but the list is inserted before value.

RelationList.__iadd__(other)

Called with self += other.

If other is a RelationList, extends value, relations and elm with the ones in other.

If other is a list, extends value with other.

RelationList.__bool__() → bool

Called with bool(). Returns bool(value).

RelationList.__contains__(other) → bool

Called with other in self. Equal to other in value.

RelationList.__delitem__(index)

Called with del self[x:y:z]. Calls delete() for each element in the range of value.

RelationList.__eq__(other) → bool

Called with self == other. If other is a RelationList, returns True if value and relations are equal, else False.

RelationList.__format__(fmt='%L') → str

Called with format(self, fmt).

Format table:

Format Code Definition
%L Inserts value.
%R Inserts relations.
%E Inserts elm.
%T Inserts tuple(value).
RelationList.__getitem__(index)

Called with self[index]. Returns value[index].

RelationList.__iter__()

Called with iter(), and converted to different types by list(), tuple(), dict() and set(). Returns iter(value).

Changed in version 1.1.0: iter(value) is returned instead of iter(elm). Programs that need to support older versions should simply switch to iter(elm).

RelationList.__len__() → int

Called with len(). Returns len(value).

RelationList.__or__(other)

This is not called by self or other (That’s done by __bool__()). It’s called by self | other.

If other is a RelationList, returns a new RelationList with value equaling the two values added, having no relations.

If other is a list, returns a new RelationList with value equaling value plus other.

RelationList.__ror__(other)

This is not called by other or self (That’s done by other.__bool__()). It’s called by other | self. Same as __or__, but other is inserted before value.

RelationList.__repr__() → str

Called by repr(). Returns RelationList(value), with [] removed.

Changed in version 1.1.0: The RelationList prefix is added, and relations and elm are no longer added. This is an incompatible change.

RelationList.__reversed__()

Called by reversed(). Returns a new RelationList with value reversed. Relations are not preserved.

RelationList.__setitem__(key, value)

Called by self.[x:y:z] = [...]. Same as value[x:y:z] = [...]. All relations related to the replaced values will be processed by delete().

RelationList.__str__() → str

Called by str(). Returns str(value).

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 in python3 -m pip show relationlist.

ver.md5

The MD5 hexdigest of ver.raw.

ver.version

A namedtuple that matches the names of sys.versioninfo, with ('major', 'minor', 'micro', 'releaselevel', 'serial').


Changelog →