biosppy.utils¶
biosppy.utils¶
This module provides several frequently used functions and hacks.
- copyright:
2015-2026 by Instituto de Telecomunicacoes
- license:
BSD 3-clause, see LICENSE for more details.
Functions
|
split a file path into its directory, name, and extension. |
|
Join one or more file path components, assuming the last is the extension. |
|
Allocate k seats proportionally using the Highest Averages Method. |
|
Normalize a path. |
|
Select a random fraction of an input list of elements. |
|
Allocate k seats proportionally using the Remainder Method. |
|
Iterator to recursively descend a directory and return all files matching the spec. |
Classes
|
A named tuple to use as a hybrid tuple-dict return object. |
- class biosppy.utils.ReturnTuple(values, names=None)[source]¶
Bases:
tupleA named tuple to use as a hybrid tuple-dict return object.
- Parameters:
values (iterable) – Return values.
names (iterable, optional) – Names for return values.
- Raises:
ValueError – If the number of values differs from the number of names.
ValueError – If any of the items in names: * contain non-alphanumeric characters; * are Python keywords; * start with a number; * are duplicates.
- append(new_values: (<class 'int'>, <class 'float'>, <class 'complex'>, <class 'str'>, typing.Collection, <Mock id='138576840327328'>), new_keys=None)[source]¶
Returns a new ReturnTuple with the new values and keys appended to the original object.
- Parameters:
new_values (int, float, list, tuple, dict, array) – Values to append. If given a dict and new_keys is None, the correspondent keys and values will be used.
new_keys (str, list, tuple, optional) – Keys to append.
- Returns:
object (ReturnTuple) – A ReturnTuple with the values and keys appended.
- as_dict()[source]¶
Convert to an ordered dictionary.
- Returns:
out (OrderedDict) – An OrderedDict representing the return values.
- delete(key)[source]¶
Returns a ReturnTuple without the specified key.
- Parameters:
key (str) – ReturnTuple key to be deleted.
- Returns:
object (ReturnTuple) – The ReturnTuple with the key removed.
- join(new_tuple)[source]¶
Returns a ReturnTuple with the new ReturnTuple appended.
- Parameters:
new_tuple (ReturnTuple) – ReturnTuple to be joined.
- Returns:
object (ReturnTuple) – The joined ReturnTuple.
- biosppy.utils.fileparts(path)[source]¶
split a file path into its directory, name, and extension.
- Parameters:
path (str) – Input file path.
- Returns:
dirname (str) – File directory.
fname (str) – File name.
ext (str) – File extension.
Notes
Removes the dot (‘.’) from the extension.
- biosppy.utils.fullfile(*args)[source]¶
Join one or more file path components, assuming the last is the extension.
- Parameters:
*args (list, optional) – Components to concatenate.
- Returns:
fpath (str) – The concatenated file path.
- biosppy.utils.highestAveragesAllocator(votes, k, divisor='dHondt', check=False)[source]¶
Allocate k seats proportionally using the Highest Averages Method.
- Parameters:
votes (list) – Number of votes for each class/party/cardinal.
k (int) – Total number o seats to allocate.
divisor (str, optional) – Divisor method; one of ‘dHondt’, ‘Huntington-Hill’, ‘Sainte-Lague’, ‘Imperiali’, or ‘Danish’.
check (bool, optional) – If True, limits the number of seats to the total number of votes.
- Returns:
seats (list) – Number of seats for each class/party/cardinal.
- biosppy.utils.normpath(path)[source]¶
Normalize a path.
- Parameters:
path (str) – The path to normalize.
- Returns:
npath (str) – The normalized path.
- biosppy.utils.random_fraction(indx, fraction, sort=True)[source]¶
Select a random fraction of an input list of elements.
- Parameters:
indx (list, array) – Elements to partition.
fraction (int, float) – Fraction to select.
sort (bool, optional) – If True, output lists will be sorted.
- Returns:
use (list, array) – Selected elements.
unuse (list, array) – Remaining elements.
- biosppy.utils.remainderAllocator(votes, k, reverse=True, check=False)[source]¶
Allocate k seats proportionally using the Remainder Method.
Also known as Hare-Niemeyer Method. Uses the Hare quota.
- Parameters:
votes (list) – Number of votes for each class/party/cardinal.
k (int) – Total number o seats to allocate.
reverse (bool, optional) – If True, allocates remaining seats largest quota first.
check (bool, optional) – If True, limits the number of seats to the total number of votes.
- Returns:
seats (list) – Number of seats for each class/party/cardinal.
- biosppy.utils.walktree(top=None, spec=None)[source]¶
Iterator to recursively descend a directory and return all files matching the spec.
- Parameters:
top (str, optional) – Starting directory; if None, defaults to the current working directoty.
spec (str, optional) – Regular expression to match the desired files; if None, matches all files; typical patterns: * r’.txt$’ - matches files with ‘.txt’ extension; * r’^File_’ - matches files starting with ‘File_’ * r’^File_.+.txt$’ - matches files starting with ‘File_’ and ending with the ‘.txt’ extension.
- Yields:
fpath (str) – Absolute file path.
Notes
Partial matches are also selected.