Source code for olist

#
##
##  SPDX-FileCopyrightText: © 2007-2024 Benedict Verhegghe <bverheg@gmail.com>
##  SPDX-License-Identifier: GPL-3.0-or-later
##
##  This file is part of pyFormex 3.5  (Thu Feb  8 19:11:13 CET 2024)
##  pyFormex is a tool for generating, manipulating and transforming 3D
##  geometrical models by sequences of mathematical operations.
##  Home page: https://pyformex.org
##  Project page: https://savannah.nongnu.org/projects/pyformex/
##  Development: https://gitlab.com/bverheg/pyformex
##  Distributed under the GNU General Public License version 3 or later.
##
##  This program is free software: you can redistribute it and/or modify
##  it under the terms of the GNU General Public License as published by
##  the Free Software Foundation, either version 3 of the License, or
##  (at your option) any later version.
##
##  This program is distributed in the hope that it will be useful,
##  but WITHOUT ANY WARRANTY; without even the implied warranty of
##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##  GNU General Public License for more details.
##
##  You should have received a copy of the GNU General Public License
##  along with this program.  If not, see http://www.gnu.org/licenses/.
##
"""Some convenient shortcuts for common list operations.

While most of these functions look (and work) like set operations, their
result differs from using Python builtin Sets in that they preserve the
order of the items in the lists.
"""
import functools


[docs]def lrange(*args): """Return a range as a list.""" return list(range(*args))
[docs]def roll(a, n=1): """Roll the elements of a list n positions forward (backward if n < 0) >>> roll(lrange(5),2) [2, 3, 4, 0, 1] """ return a[n:] + a[:n]
[docs]def union(a, b): """Return a list with all items in a or in b, in the order of a,b. >>> union(lrange(3),lrange(1,4)) [0, 1, 2, 3] """ return a + [i for i in b if i not in a]
[docs]def difference(a, b): """Return a list with all items in a but not in b, in the order of a. >>> difference(lrange(3),lrange(1,4)) [0] """ return [i for i in a if i not in b]
[docs]def symdifference(a, b): """Return a list with all items in a or b but not in both. >>> symdifference(lrange(3),lrange(1,4)) [0, 3] """ return difference(a, b) + difference(b, a)
[docs]def intersection(a, b): """Return a list with all items in a and in b, in the order of a. >>> intersection(lrange(3),lrange(1,4)) [1, 2] """ return [i for i in a if i in b]
[docs]def concatenate(a): """Concatenate a list of lists. >>> concatenate([lrange(3), lrange(1,4), [2,5]]) [0, 1, 2, 1, 2, 3, 2, 5] """ return functools.reduce(list.__add__, a)
[docs]def flatten(a, recurse=False): """Flatten a nested list. By default, lists are flattened one level deep. If recurse=True, flattening recurses through all sublists. >>> flatten([[[3.,2,],6.5,],[5],6,'hi']) [[3.0, 2], 6.5, 5, 6, 'hi'] >>> flatten([[[3.,2,],6.5,],[5],6,'hi'],True) [3.0, 2, 6.5, 5, 6, 'hi'] """ r = [] for i in a: if isinstance(i, list): if recurse: r.extend(flatten(i, True)) else: r.extend(i) else: r.append(i) return r
[docs]def group(a, n): """Split a list in sequences of maximum n items. Parameters ---------- a: list The list to spliy n: int The (maximum) number of items in each sublist Returns ------- list of lists A list of lists containing all the items of the input list in the same order. Each sublist has length n, except for the last one, which may be shorter. Examples -------- >>> group( [3.0, 2, 6.5, 5, 'hi'],2) [[3.0, 2], [6.5, 5], ['hi']] """ return [a[i:i + n] for i in range(0, len(a), n)]
[docs]def select(a, b): """Return a subset of items from a list. Returns a list with the items of a for which the index is in b. >>> select(range(2,6),[1,3]) [3, 5] """ return [a[i] for i in b]
[docs]def remove(a, b): """Returns the complement of select(a,b). >>> remove(range(2,6),[1,3]) [2, 4] """ return [a[i] for i in range(len(a)) if i not in b]
# TODO: what if i is multiple times in l?
[docs]def toFront(l, i): """Add or move i to the front of list l l is a list. If i is in the list, it is moved to the front of the list. Else i is added at the front of the list. This changes the list inplace and does not return a value. >>> L = lrange(5) >>> toFront(L,3) >>> L [3, 0, 1, 2, 4] >>> toFront(L,7) >>> L [7, 3, 0, 1, 2, 4] """ if i in l: l.remove(i) l.insert(0, i)
[docs]class List(list): """A versatile list class. The List class extends the builtin list type with automatic calling of a method on all items in the list. All normal list methods remain applicable on the List. Any other method will return a new List with the method applied to each of the items, using the same arguments (if any). Parameters ---------- keepclass: bool If False (default), the result of a non-list method on the List will be a plain list. If True, the result will again be a List object. While the latter allows to chain multiple List methods, it may cause problems with methods that reduce the list to a single object. Examples -------- >>> L = List(['one', 'two']) >>> L.upper() ['ONE', 'TWO'] >>> LL = List(L, keepclass=True) >>> LL.upper() List(['ONE', 'TWO']) >>> LL.startswith('o') List([True, False]) >>> L + ['three'] List(['one', 'two', 'three']) >>> ['zero'] + L List(['zero', 'one', 'two']) >>> L += ['three'] >>> L List(['one', 'two', 'three']) Notes ----- For a List instance ``hasattr`` returns True for any attr. Use hasattr only on the List class or on the items contained in the List. """ def __init__(self, *args, keepclass=False): self._keep_ = keepclass super().__init__(*args) def __repr__(self): return f"List({super().__repr__()})" def __add__(self, other, /): """Implements List + List/list""" return List(super().__add__(other)) def __radd__(self, other, /): """Implements list + List""" return List(other) + self def __iadd__(self, other, /): """Implements List += List/list""" self.extend(other) return self def __getattr__(self, attr): """Any non-existing attribute will come here""" def on_all(*args, **kwargs): ret = [getattr(obj, attr)(*args, **kwargs) for obj in self] return self.__class__(ret) if self._keep_ else ret return on_all def __getstate__(self): """Allow a List to be pickled.""" return list(self) def __setstate__(self, state): """Allow a List to be set from pickle""" self.__init__(state)
# End