sisl.oplist.oplist

class sisl.oplist.oplist(iterable=(), /)

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

Methods

append(object, /)

Append object to the end of the list.

clear()

Remove all items from list.

copy()

Return a shallow copy of the list.

count(value, /)

Return number of occurrences of value.

decorate(func)

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

extend(iterable, /)

Extend list by appending elements from the iterable.

index(value[, start, stop])

Return first index of value.

insert(index, object, /)

Insert object before index.

pop([index])

Remove and return item at index (default last).

remove(value, /)

Remove first occurrence of value.

reverse()

Reverse IN PLACE.

sort(*[, key, reverse])

Sort the list in ascending order and return None.

__init__(*args, **kwargs)
append(object, /)

Append object to the end of the list.

clear()

Remove all items from list.

copy()

Return a shallow copy of the list.

count(value, /)

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(iterable, /)

Extend list by appending elements from the iterable.

index(value, start=0, stop=9223372036854775807, /)

Return first index of value.

Raises ValueError if the value is not present.

insert(index, object, /)

Insert object before index.

pop(index=-1, /)

Remove and return item at index (default last).

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

remove(value, /)

Remove first occurrence of value.

Raises ValueError if the value is not present.

reverse()

Reverse IN PLACE.

sort(*, key=None, reverse=False)

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.