Python package for calculations
Project description
calcpy: Facility for Python Calculation
calcpy is a Python package for calculations.
Main features:
- Extended Python built-in functions, including extended
setoperations, extendedstroperations, extended math functions. - Argument decorators: Reorganize function arguments.
- Function compositions: Combine multiple callable into one callable.
- Return-value decorations: If the function raises an error or returns invalid values such as
Noneandnan, fill the values with designated values. - Unified APIs supporting both Python built-in types and numpy&pandas types.
- Order theory: Implementations of some concepts and algorithms in order theory.
Documentation
Online Documentation: https://calcpy.github.io/
Installation
It is recommended to install calcpy via pip.
pip install --upgrade calcpy
Python 3.8+ is required. why
Features
Function Composition
Function compositions combines multiple callable into one callable.
Documentation: https://calcpy.github.io/compo.html
calcpy.componentize(how)decorates a callable function so that it becomes a building block of function composition.
Examples:
>>> from calcpy import itemgetter
>>> enable_composition()
>>> max_ = componentize(max)
>>> max_(itemgetter(1), 7, itemgetter(2, default=3))([3, 4, 5])
7
>>> min_ = componentize(min)
>>> min_(itemgetter(1), 7, itemgetter(2, default=3))([3, 4, 5])
4
>>> min_(3, 4, 5)
3
calcpy also provides many ready-made components for function compositions.
List of APIs: https://calcpy.github.io/compo.html#shorthand-functions-that-support-composition
Examples:
>>> from calcpy import add, enable_composition, itemgetter
>>> enable_composition()
>>> add(itemgetter(1), itemgetter(6, default=3), 10)([3, 4, 5])
19
>>> from calcpy import arggetter
>>> add(arggetter(0), arggetter(1))(3, 4)
7
Reorganize Arguments of Callable
Documentation: https://calcpy.github.io/fun.html#reorder-arguments
calcpy provides several decorators to re-organize parameters of a callable.
calcpy.fun.cycleperm(cycle=())is a decorator that swaps positional arguments of a callable according to a cyclc notation. By default, it does not permutate anything.calcpy.fun.swap(i=0, j=1)is a decorator that swapsi-th positional argument andj-th positional argument of a callable. By default, it permutates the first two arguments of the callable.calcpy.fun.dispatch(dispatcher, agg=None, fix_begin=0)is a decorator that dispatches a callable according to a dispatcher, with parameters to fix first few positional arguments, and an aggregation function to aggregate the final result.calcpy.fun.packis a decorator that merges all positional arguments of a function to a single tuple argument.calcpy.fun.unpackis a decorator that converts a single tuple/list argument to many positional arguments.calcpy.fun.prioritize()is a decorator that move some positional arguments to the beginning of argument list.
Examples:
>>> from calcpy.fun import swap
>>> @swap() # the decorator that swaps the first two positional arguments
... def g(a, b):
... return range(a, b)
>>> g(4, 2)
range(2, 4)
>>> # Equivalent to:
>>> swapper = swap()
>>> swapped_range = swapper(range)
>>> swapped_range(4, 2)
range(2, 4)
>>> from calcpy.fun import dispatch
>>> from itertools import pairwise # For old Python version: from more_itertools import pairwise
>>> @dispatch(pairwise, agg=all) # the decorator that extends a binop bool checker to arbitrary number of arguments
... def fraceq(a, b):
... return (a % 1) == (b % 1)
>>> fraceq() # return True when there are <=1 arugments, since pairwise() returns an empty iterator
True
>>> fraceq(1.1, 2.1)
True
>>> fraceq(1.1, 2.1, 3.1, 4.1)
True
Extended Set Functions
Set-alike APIs operate objects with original order reserved and optional customized comparison function.
Documentation: https://calcpy.github.io/set.html
calcpy.union(*args, key=None)returns the union of multiple arguments.calcpy.intersect(*args, key=None)returns intersection of multiple arguments.calcpy.difference(arg, *args, key=None)removes follow-up arguments from the first.calcpy.symmetric_difference(*args, key=None)returns the symmetric difference (a.k.a exclusive-or) of multiple arguments.calcpy.isdisjoint(*args, key=None)checks whether arguments are disjoint.calcpy.ispropersubset(*args, key=None)checks whether the former parameter is a proper subset of the later parameter.calcpy.ispropersuperset(*args, key=None)checks whether the former parameter is a proper superset of the later parameter.calcpy.issubset(*args, key=None)checks whether the former parameter is a subset of the later parameter.calcpy.issuperset(*args, key=None)checks whether the former parameter is a superset of the later parameter.
Examples:
>>> from calcpy import union
>>> union([4, 3], [7, 3], [4])
[4, 3, 7]
Other functions with similar signatures:
calcpy.eq(*args, key=None)checks whether positional arguments are all equal.calcpy.ne(*args, key=None)checks whether positional arguments are all distinct.calcpy.concat(*args, key=None)concatenates multiple arguments.
Functions apply on the only positional argument:
calcpy.same(values, key=None)checks whether all elements in the first positional argument are all equal.calcpy.distinct(values, key=None)checks whether all elements in the first positional argument are all distinct.calcpy.unique(values, key=None)removes duplicated entries.
Examples:
>>> from calcpy import unique
>>> unique([4, 3, 7, 3, 4])
[4, 3, 7]
Supported object types:
list, tuple, set, str, np.ndarray, pd.Series, pd.DataFrame, and more.
union(), intersection(), and difference() for dict:
-
calcpy.intersection()andcalcpy.difference()support first argument of typedictand follow-up arguments as typelist. In those cases, it means that limit the keys of thedictwith a list or exclude lists out of the dict. -
calcpy.union()can merge multipledicts into onedict. If twodictsd1andd2have the same keyk,union(d1, d2)will use the value ofd1[k]rather thand2[k], which differs fromd1 | d2who takesd2[k].
Examples:
>>> from calcpy import intersection, difference
>>> d = {"a": 1, "b": 2}
>>> l = ["a", "c"]
>>> intersection(d, l)
{"a": 1}
>>> difference(d, l)
{"b": 2}
Customized comparison:
The keyword parameter key is used for customized comparison function.
By default, it is None, which uses the default way to compare two objects, i.e. compare them as a whole. It is the same as calcpy.overall_equal(). The function calcpy.overall_equal() behaves like np.array_equal() or np.ndarrays, and like loper.equals(roper) for pd.Seriess and pd.DataFrames.
calcpy supports customized key functions for object comparison. For example, the following examples compare strs according to their lowercases:
>>> from calcpy import eq
>>> eq("Hello", "hello", "HELLO", key=str.lower)
True
Enhanced Functional APIs
Documentation: https://calcpy.github.io/builtin.html
calcpy.argchecker(*keys, *, default)instantiates a callable object that checks whether position arguments and/or keyword arguments are all present, with optional default value for missing arguments.calcpy.arggetter(*keys, *, default)instantiates a callable object that fetches position arguments and/or keyword arguments, with optional default value for missing arguments.calcpy.attrchecker(*attrs, *, default)instantiates a callable object that checks whether the given attribute(s) are all present in its operand, with optional default value for missing attributes.calcpy.attrgetter(*attrs, *, default)instantiates a callable object that fetches the given attribute(s) from its operand, with optional default value for missing attributes.calcpy.constantcreator(value, /, *, copy=False)instantiates a callable object that returns the same constant when it is called.calcpy.itemchecker(*items, *, default)instantiates a callable object that checks whether the given item(s) are all present in its operand, with muti-level keys and optional default value for missing items.calcpy.itemgetter(*items, *, default)instantiates a callable object that fetches the given item(s) from its operand, with muti-level keys and optional default value for missing items.calcpy.methodcaller(name, *args, **kwargs)instantiates a callable object that calls a method with the given name and arguments, supportingpandasaccessor.
Examples:
>>> from calcpy import arggetter
>>> getter = arggetter(0) # get the first positional argument
>>> getter("value", "other_input", key="other_keyword_input")
'value'
>>> from calcpy import attrgetter
>>> getter = attrgetter("upper")
>>> getter("test")
'TEST'
>>> from calcpy import constantcreator
>>> creator = constantcreator("value")
>>> creator()
'value'
Decorators for Callable Outputs
Documentation: https://calcpy.github.io/fill.html
calcpy.fillerr(fun, value=None)instantiates a callable object that returnsvalueif the functionfunraises an exception.calcpy.fillnone(fun, value=None)instantiates a callable object that returnsvalueif the functionfunreturnsNone.calcpy.fillnan(fun, value=None)instantiates a callable object that returnsvalueif the functionfunreturnsnan.
Examples:
>>> from calcpy import fillerr, fillnone, fillnan
>>> @fillerr("Error")
... @fillnone("None")
... @fillnan("NaN")
... def fun(x):
... if x > 0:
... return nan
... if x <= 0:
... return 1 / x
>>> fun(0)
'Error'
>>> fun(-1)
-1.0
>>> fun(nan)
'None'
>>> fun(1)
'NaN'
Extensions of Built-in Functions
Similar to Python built-in functions all() and any(), we provide the functions with keyword parameter empty to specify the behavior when the sequence is empty.
calcpy.all_(iterable, *, empty=True)checks whether all elements in a sequence are truthy. Returnemptyif the sequence is empty. (It is equivalent toall(iterable)when the default value ofemptyis used.)calcpy.any_(iterable, *, empty=False)checks whether any element in a sequence is truthy. Returnemptyif the sequence is empty. (It is equivalent toany(iterable)when the default value ofemptyis used.)calcpy.never(iterable, *, empty=True)checks whether all elements in a sequence are not truthy. Returnemptyif the sequence is empty.calcpy.odd(iterable, *, empty=False)checks whether an odd number of items in the iterable are truthy. Returnemptyif the sequence is empty.
Examples:
>>> from calcpy import never, odd
>>> never([])
True
>>> never([False, False, False])
True
>>> odd([])
False
>>> odd([], empty=True)
True
We also extend the following functions in built-in modules operator so that it can accept an arbitrary number of arguments: and_(), or_(), xor(), add(), mul(), lt(), le(), gt(), eq().
For examples, we have the following comparers. They support multiple numbers of arguments (including zero arguments and one argument).
calcpy.lt(*args, key=None)calcpy.le(*args, key=None)calcpy.ge(*args, key=None)calcpy.gt(*args, key=None)calcpy.eq(*args, key=None)calcpy.ne(*args, key=None)
Examples:
>>> from calcpy import lt
>>> lt()
True
>>> lt(1)
True
>>> lt(1, 2)
True
>>> lt(1, 2, 3)
True
>>> lt(1, 1)
False
>>> lt(1, 1, 2)
False
Table Compare functions with multiple arguments and functions with a single iterable argument.
| Function with arbitrary number of positional arguments | Function of only one iterable positional argument | Support customized comparison |
|---|---|---|
calcpy.eq() |
calcpy.same() |
Yes |
calcpy.ne() |
calcpy.distinct() |
Yes |
calcpy.and_() |
calcpy.all_() |
Yes |
calcpy.or_() |
calcpy.any_() |
Yes |
calcpy.xor() |
calcpy.odd() |
Yes |
calcpy.add() |
calcpy.sum_() |
|
calcpy.mul() |
calcpy.prod() |
|
calcpy.matmul() |
calcpy.matprod() |
Relationship among Classes
Documentation: https://calcpy.github.io/cls.html
The following APIs are provided to compare classes:
calcpy.ispropersubclass()calcpy.ispropersuperclass()calcpy.issubclass_()calcpy.issuperclass()
Consistent APIs across Python built-in modules, numpy, and pandas
Documentation: https://calcpy.github.io/nppd.html
We provide the following APIs that can accept arguments that are numbers.Number, np.ndarray, pd.Series, and/or pd.DataFrame.
calcpy.shape(arg)calcpy.ndim(arg)calcpy.size(arg)calcpy.full_like(template, fill_value, **kwargs)supports create an object aliketemplate. The parametertemplatecan be anumbers.Number,np.ndarray,pd.Series, orpd.DataFrame.calcpy.mapi(arg, f)applies a function to every element in the inputs.calcpy.overall_equal(loper, roper)support comparison two arguments as a whole. The two arguments can benumbers.Number,np.ndarray,pd.Series, orpd.DataFrame.
We provide the following decorator to extend existing functions:
calcpy.broadcast_first()extends an existing function so that the first parameter can benumber.Number,np.ndarray,pd.Series, orpd.DataFrame.
Sequence APIs
Repetend Analysis
Documentation: https://calcpy.github.io/seq.html#repetend-analysis
calcpy.min_repetend_len()calculates the minimum length of repetends.
Examples:
>>> from calcpy import min_repetend_len
>>> min_repetend_len([1, 2, 3, 1, 2, 3, 1, 2])
3
Permutation
Documentation: https://calcpy.github.io/seq.html#permutation
calcpy.cycleperm()permutates a list according to cycle notation.calcpy.swap()swaps two elements in a list.calcpy.prioritize()moves some positional arguments to the beginning.
Example:
>>> from calcpy import cycleperm
>>> cycleperm(["a", "b", "c", "d", "e", "f", "g"], [1, 2, 4])
['a', 'c', 'e', 'd', 'b', 'f', 'g']
Sequence Generator
Documentation: http://calcpy.github.io/seq.html#sequence-generator
calcpy.oeis.A276128()generates elements of the sequence OEIS A276128.
Combinatics
Documentation: http://calcpy.github.io/comb.html
calcpy.bellpartition()generates all subset partitions of a set.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file calcpy-1.3.0.tar.gz.
File metadata
- Download URL: calcpy-1.3.0.tar.gz
- Upload date:
- Size: 47.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d0b5d1d389b34273696d7d0eaae2f75b0f5e9b70eb011ea809fcd6fb92efd7a6
|
|
| MD5 |
743c6f911facff2d0b3740f36dd0c1d5
|
|
| BLAKE2b-256 |
85c706d70b30947379ff73fd55ded81edf78e491ef5ff63375af1071f821acde
|
Provenance
The following attestation bundles were made for calcpy-1.3.0.tar.gz:
Publisher:
python-publish.yml on calcpy/calcpy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
calcpy-1.3.0.tar.gz -
Subject digest:
d0b5d1d389b34273696d7d0eaae2f75b0f5e9b70eb011ea809fcd6fb92efd7a6 - Sigstore transparency entry: 371566028
- Sigstore integration time:
-
Permalink:
calcpy/calcpy@3eeff2f636b452a336d22738cebeb47aefc43fb6 -
Branch / Tag:
refs/tags/v1.3.0 - Owner: https://github.com/calcpy
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@3eeff2f636b452a336d22738cebeb47aefc43fb6 -
Trigger Event:
release
-
Statement type:
File details
Details for the file calcpy-1.3.0-py3-none-any.whl.
File metadata
- Download URL: calcpy-1.3.0-py3-none-any.whl
- Upload date:
- Size: 44.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
969e140b5c61fedfd5d01901b933282540ec036a5fdd0035ca593ff0b0202ae0
|
|
| MD5 |
94aceb71cab4b430a7f5932764d9fa3f
|
|
| BLAKE2b-256 |
737509f113487fbd814cab8a82674862ea613a1ceab40d9bcb247b775cb37d8e
|
Provenance
The following attestation bundles were made for calcpy-1.3.0-py3-none-any.whl:
Publisher:
python-publish.yml on calcpy/calcpy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
calcpy-1.3.0-py3-none-any.whl -
Subject digest:
969e140b5c61fedfd5d01901b933282540ec036a5fdd0035ca593ff0b0202ae0 - Sigstore transparency entry: 371566055
- Sigstore integration time:
-
Permalink:
calcpy/calcpy@3eeff2f636b452a336d22738cebeb47aefc43fb6 -
Branch / Tag:
refs/tags/v1.3.0 - Owner: https://github.com/calcpy
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@3eeff2f636b452a336d22738cebeb47aefc43fb6 -
Trigger Event:
release
-
Statement type: