oplist¶
-
class
sisl.
oplist
[source]¶ list with inter-operations
List-inherited class implementing direct element operations instead of list-extensions/compressions. When having multiple lists and one wishes to create a sum of individual elements, thus creating the summed elements list one could do:
>>> from functools import reduce >>> lists = list() >>> lists.append([1, 2]) >>> lists.append([3, 4]) >>> lists.append([5, 6]) >>> list_sum = reduce(lambda x, y: (x[0] + y[0], x[1] + y[1]), lists) >>> print(list_sum) [9, 12]
However, the above easily becomes tedious when the number of entries in the list becomes very large.
Instead it may be advantageous to allow operations like this:
>>> oplists = list() >>> oplists.append(oplist([1, 2])) >>> oplists.append(oplist([3, 4])) >>> oplists.append(oplist([5, 6])) >>> oplist_sum = reduce(sum, oplists) >>> print(oplist_sum) [9, 12]
This is more natural when dealing with multiple variables and wanting to add them.
>>> l1 = oplist([1, 2]) >>> l2 = oplist([3, 4]) >>> l3 = oplist([5, 6]) >>> l = l1 + l2 + l3 >>> print(l) [9, 12] >>> print(l * 2) [18, 24]
The class also implements a decorator for automatic returning of oplist lists.
>>> @oplist.decorate >>> def my_func(): ... return 1 >>> isinstance(my_func(), oplist) True
Currently this class implements the following elementwise mathematical operations
additions
subtractions
multiplications
divisions
powers
- Parameters
- iterabledata
elements in
oplist
Methods
__init__
(self, /, \*args, \*\*kwargs)Initialize self.
append
()clear
()copy
()count
()decorate
(func)Decorate a function to always return an
oplist
, regardless of return values from funcextend
()index
()Raises ValueError if the value is not present.
insert
()L.insert(index, object) – insert object before index
pop
()Raises IndexError if list is empty or index is out of range.
remove
()Raises ValueError if the value is not present.
reverse
()L.reverse() – reverse IN PLACE
sort
()-
append
()¶
-
clear
()¶
-
copy
()¶
-
count
()¶
-
classmethod
decorate
(func)[source]¶ Decorate a function to always return an
oplist
, regardless of return values from func- Parameters
- funccallable
- Returns
- callable
a wrapped function which ensures the returned argument is an instance of
oplist
Examples
>>> @oplist.decorate >>> def myret(): ... return 1 >>> a = myret() >>> isinstance(a, oplist) True >>> print(a) [1]
-
extend
()¶
-
index
()¶ Raises ValueError if the value is not present.
-
insert
()¶ L.insert(index, object) – insert object before index
-
pop
()¶ Raises IndexError if list is empty or index is out of range.
-
remove
()¶ Raises ValueError if the value is not present.
-
reverse
()¶ L.reverse() – reverse IN PLACE
-
sort
()¶