A mathematical and financial function curve utility library.
Project description
curvepy
A mathematical and financial function curve utility library for Python.
Installation
With poetry:
poetry add curvepy
Or with pip:
pip3 install curvepy
Usage
Have a look at the documentation.
Basic usage:
# Create a line
from curvepy import Line
line = Line(const=1, slope=2)
assert line.y(0) == 1
assert line.y(1) == 3
# Function arithmetic
line2 = Line(const=-1, slope=-2)
line_sum = line1 + line2
assert line_sum.y(0) == 0
assert line_sum.y(1) == 0
JSON Format
Function values:
Functions are denoted by $
, for example, $add
. Available functions:
$add
: Adds values. Example:{ "$add": [1, 2] }
, which results in1 + 2
.$line
: Adds values. Example:{ "$line": { "points": [[1, 2], [2, 3]] } }
which results in a line joining the points(1, 2)
and(2, 3)
.
For other functions, refer to Curve
documentation.
Decorators modify applicable values and functions inside of them as well as deeply nested values. Available decorators:
@date
: Converts all string values to seconds from Unix epoch. If no GMT offset is given, uses the local time zone at the time of parsing.@log
: Converts all number literals to log space. On exit from the decorator, the result is raised bacj by the base into linear space. Available varaints:@log2
,@log10
.@args
: Allows using both named and unamed arguments in a function. For example:{ '$raised': { '@args': [4], 'base': 2 } }
, which will result inpow(2, 4)
.
A full example:
The following price will be in the form of a line (on a log chart) joining the prices 10080.2 and 8975.0 on dates 8 May 2020 11:01 am and 13 May 2020 5 am respectively.
{
"price": {
"@log": {
"$line": {
"points": [
[{"@date": "2020-05-08 11:01"}, 10080.2],
[{"@date": "2020-05-13 05:00"}, 8975.0]
]
}
}
}
}
Development
Setup
Clone repository and run:
poetry install
Running Unit Tests
poetry run test
Updating Documentation
The module pdoc3 is used to automatically generate documentation. To update the documentation:
- Install
pdoc3
if needed withpip3 install pdoc3
. - Navigate to project root and install dependencies:
poetry install
. - Generate documetation files with:
pdoc3 -o docs --html curvepy
. - The new files will be in
docs/curvepy
. Move them todocs/
and replace existing files.
If you get errors about missing modules, make sure you have activated the python enviroment (in .venv) and that there are no python version mismatches. If so, use poetry env use <python version>; poetry install
to fix.
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.