Extra Functional Tools beyond Standard and Third-Party Libraries
Project description
Extra functional tools that go beyond standard libraries itertools
, functools
, etc. and popular third-party libraries like toolz
and fancy
.
- Like
toolz
, most of the tools are designed to be efficient, pure, and lazy.
This library is under active development, and new functions will be added on regular basis.
- Any idea or contribution is highly welcome.
Installation
This package is available on PyPi. Just use pip3 install -U extratools
to install it.
Available Tools
Please check individual source file for details.
seqtools
Tools for matching sequences (including strings), with or without gaps allowed between matching items. Note that empty sequence is always a sub-sequence of any other sequence.
-
findsubseq(a, b)
returns the first position wherea
is a sub-sequence ofb
, or-1
when not found. -
issubseq(a, b)
checks ifa
is a sub-sequence ofb
. -
findsubseqwithgap(a, b)
returns the matching positions wherea
is a sub-sequence ofb
, where gaps are allowed, orNone
when not found. -
issubseqwithgap(a, b)
checks ifa
is a sub-sequence ofb
, where gaps are allowed.
Tools for comparing sequences (including strings).
-
productcmp(x, y)
compares two sequencesx
andy
with equal length according to product order. Returns-1
if smaller,0
if equal,1
if greater, andNone
if not comparable.- Throw exception if
x
andy
have different lengths.
- Throw exception if
Tools for sorting sequences.
sortedbyrank(sth, ranks, reverse=False)
returns the sorted list ofsth
, according to the respective rank of each individual element inranks
.
sortedtools
Tools for sorted sequences.
-
sortedcommon(a, b, key=None)
returns the common elements betweena
andb
.- When both
a
andb
are sorted sets with no duplicate element, equal tosorted(set(a) & set(b))
but more efficient.
- When both
-
sortedalone(a, b, key=None)
returns the elements not in botha
andb
.- When both
a
andb
are sorted sets with no duplicate element, equal tosorted((set(a) | set(b)) - (set(a) & set(b)))
but more efficient.
- When both
-
sorteddiff(a, b, key=None)
returns the elements only ina
and not inb
.- When both
a
andb
are sorted sets with no duplicate element, equal tosorted(set(a) - set(b))
but more efficient.
- When both
-
issubsorted(a, b, key=None)
checks ifa
is a sorted sub-sequence ofb
.- When both
a
andb
are sorted sets with no duplicate element, equal toset(a) <= set(b)
but more efficient.
- When both
strtools
Tools for strings.
-
str2grams(s, n, pad=None)
returns the orderedn
-grams of strings
.- Optional padding at the start and end can be added by specifying
pad
- Optional padding at the start and end can be added by specifying
dicttools
Tools for inverting dictionaries.
-
invertdict(d)
inverts(Key, Value)
pairs to(Value, Key)
.- If multiple keys share the same value, the inverted directory keeps last of the respective keys.
-
invertdict_multiple(d)
inverts(Key, List[Value])
pairs to(Value, Key)
.- If multiple keys share the same value, the inverted directory keeps last of the respective keys.
-
invertdict_safe(d)
inverts(Key, Value)
pairs to(Value, List[Key])
.- If multiple keys share the same value, the inverted directory keeps a list of all the respective keys.
Tools for remapping elements.
-
remap(data, mapping, key=None)
remaps each unique element indata
according to functionkey
.mapping
is a dictionary recording all the mappings, optionally containing previous mappings to reuse.
wordmap = {} db = [list(remap(doc, wordmap)) for doc in docs]
- In default,
key
returns integers starting from0
.
settools
Tools for set operations.
addtoset(s, x)
checks whether addingx
to sets
is successful.
Tools for set similarities.
-
jaccard(a, b)
computes the Jaccard similarity between two setsa
andb
. -
multisetjaccard(a, b)
computes the Jaccard similarity between two multi-sets (Counters)a
andb
. -
weightedjaccard(a, b, key=sum)
computes the weighted Jaccard similarity between two setsa
andb
, using functionkey
to compute the total weight of the elements within a set.
tabletools
Tools for tables.
transpose(m)
returns the transpose of tablem
, i.e., switch rows and columns.
mathtools
Tools for math.
-
safediv(a, b)
avoids thedivision by zero
exception, by returning infinite with proper sign.- Closely referring IEEE Standard 754.
stattools
Tools for statistics.
-
medianabsdev(data)
computes the median absolute deviation of a list of floats. -
entropy(data)
computes the entropy of a list of any items.- You can also pass a dictionary of
(item, frequency)
as frequency distribution todata
.
- You can also pass a dictionary of
misctools
Tools for miscellaneous purposes.
-
cmp(a, b)
restores the usefulcmp
function previously in Python 2.- Implemented according to What's New in Python 3.0.
disjointsets
Disjoint sets with path compression. After d = DisjointSets()
:
-
d.add(x)
adds a new disjoint set containingx
. -
d[x]
returns the representing element of the disjoint set containingx
. -
d.disjoints()
returns all the representing elements and their respective disjoint sets. -
d.union(*xs)
union all the elements inxs
into a single disjoint set.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.