oplist

class sisl.oplist

Bases: list

list with element-wise 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

iterable (data) – elements in oplist

Attributes

__doc__

__hash__

__module__

__slots__

Methods

__abs__()

__add__(other)

__and__(other)

__contains__

Return key in self.

__delattr__

Implement delattr(self, name).

__delitem__

Delete self[key].

__dir__

Default dir() implementation.

__eq__(other)

__floordiv__(other)

__format__

Default object formatter.

__ge__(other)

__getattribute__

Return getattr(self, name).

__getitem__

x.__getitem__(y) <==> x[y]

__gt__(other)

__iadd__(other)

__iand__(other)

__ifloordiv__(other)

__imatmul__(other)

__imod__(other)

__imul__(other)

__init__

Initialize self.

__init_subclass__

This method is called when a class is subclassed.

__ior__(other)

__ipow__(other)

__isub__(other)

__iter__

Implement iter(self).

__itruediv__(other)

__le__(other)

__len__

Return len(self).

__lt__(other)

__matmul__(other)

__mod__(other)

__mul__(other)

__ne__

Return self!=value.

__neg__()

__new__

Create and return a new object.

__not__()

__or__(other)

__pos__()

__pow__(other)

__radd__(other)

__reduce__

Helper for pickle.

__reduce_ex__

Helper for pickle.

__repr__

Return repr(self).

__reversed__

Return a reverse iterator over the list.

__rfloordiv__(other)

__rmul__(other)

__rpow__(other)

__rsub__(other)

__rtruediv__(other)

__setattr__

Implement setattr(self, name, value).

__setitem__

Set self[key] to value.

__sizeof__

Return the size of the list in memory, in bytes.

__str__

Return str(self).

__sub__(other)

__subclasshook__

Abstract classes can override this to customize issubclass().

__truediv__(other)

__xor__(other)

append

Append object to the end of the list.

clear

Remove all items from list.

copy

Return a shallow copy of the list.

count

Return number of occurrences of value.

decorate(func)

Decorate a function to always return an oplist, regardless of return values from func

extend

Extend list by appending elements from the iterable.

index

Return first index of value.

insert

Insert object before index.

pop

Remove and return item at index (default last).

remove

Remove first occurrence of value.

reverse

Reverse IN PLACE.

sort

Sort the list in ascending order and return None.

append()

Append object to the end of the list.

clear()

Remove all items from list.

copy()

Return a shallow copy of the list.

count()

Return number of occurrences of value.

classmethod decorate(func)[source]

Decorate a function to always return an oplist, regardless of return values from func

Parameters

func (callable) –

Returns

a wrapped function which ensures the returned argument is an instance of oplist

Return type

callable

Examples

>>> @oplist.decorate
>>> def myret():
...    return 1
>>> a = myret()
>>> isinstance(a, oplist)
True
>>> print(a)
[1]
extend()

Extend list by appending elements from the iterable.

index()

Return first index of value.

Raises ValueError if the value is not present.

insert()

Insert object before index.

pop()

Remove and return item at index (default last).

Raises IndexError if list is empty or index is out of range.

remove()

Remove first occurrence of value.

Raises ValueError if the value is not present.

reverse()

Reverse IN PLACE.

sort()

Sort the list in ascending order and return None.

The sort is in-place (i.e. the list itself is modified) and stable (i.e. the order of two equal elements is maintained).

If a key function is given, apply it once to each list item and sort them, ascending or descending, according to their function values.

The reverse flag can be set to sort in descending order.