Skip to main content

Libraries for capturing and using expressions in python

Project description

DExpr - general and date expressions library

A little library for capturing and using various types of expressions in python.

magic Op

magic Op captures expressions that use ParameterOp objects via operator overloads and creates a data structure to represent operations captured:

_1 = ParameterOp(_index=0)
_2 = ParameterOp(_index=1)

expr1 = _1 + _2

here expr1 is an instance of Op subclass that catures that an add operator was called with the _1 and _2 variables. If we want to evaluate that expression with arbitrary arguments we use the calc function:

assert calc(expr1, 1, 2) == 3

The library supports all overloadable operators in python, including attribute access and calls:

expr2 = _1.fn(_2)

if expr2 is evaluated with an object that provides fn method as a first parameter it will call it and pass in the value of the second parameter

Op supports list and dict comprehensions

expr = [i for i in lazy(range)(_0)]
assert calc(expr, 4) == [0, 1, 2, 3]

Note thelazy call to make the range function an Op

DGen - date generators

The DGen simplifies producing lists of dates from an expression. The usual DGen expression looks like this:

last_sundays_in_march = '1980-01-01' < years.months[2].weeks[-1].sun < '2030-01-01'

The main parts of a DGen expression are the date expression itself that uses years, months, weeks, weekdays, weekends, days series to compose a rule for which date is it going to generate and bounding conditions that limit the series to specific bounds. The last bit does not have to be put into the expression itself but can be supplied when we ask it to generate:

last_sundays_in_march = years.months[2].weeks[-1].sun
list_of_last_sundays_in_march = list(last_sundays_in_march(after='1980-01-01', before='2030-01-01'))

There is also a support for adding and subtracting tenors from dates:

my_dates = months.days[14] + '1w'

Tenors supported are y, m, w, d and b. b is only valid if a calendar is supplied.

dataclass Extensions

The dataclassex library allows the magic Ops and lamdas to be used as defaults for fields in dataclasses. The same can be done with properties, this library just makes it super simple to do:

@dataclassex
class OrderWithLambda:
    order_date: date
    shipping_time: Tenor
    expected_delivery_date: date = lambda self: self.order_date + self.shipping_time

o = OrderWithLambda(order_date=make_date('2020-02-02'), shipping_time=Tenor('3d'))
assert o.expected_delivery_date == make_date('2020-02-05')

adding Op expressoins:

Self = ParameterOp(_name='Self')

@dataclassex
class OrderWithOp:
    order_date: date
    shipping_time: Tenor
    expected_delivery_date: date = Self.order_date + Self.shipping_time

o = OrderWithLambda(order_date=make_date('2020-02-02'), shipping_time=Tenor('3d'))
assert o.expected_delivery_date == make_date('2020-02-05')

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

dexpr-0.0.5.tar.gz (14.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dexpr-0.0.5-py3-none-any.whl (14.1 kB view details)

Uploaded Python 3

File details

Details for the file dexpr-0.0.5.tar.gz.

File metadata

  • Download URL: dexpr-0.0.5.tar.gz
  • Upload date:
  • Size: 14.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.18

File hashes

Hashes for dexpr-0.0.5.tar.gz
Algorithm Hash digest
SHA256 339ed8823847572b6952e922f92fb19879bb69bbd6166efabc495506b89c653e
MD5 781b7d3edbbd2577ed93db055e74bcbe
BLAKE2b-256 e90ac2c46d1fc6ce047a7b33ee81d2fc1524aec6bcf9f1068a642d1ddfcbe018

See more details on using hashes here.

File details

Details for the file dexpr-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: dexpr-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 14.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.18

File hashes

Hashes for dexpr-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 fbb43a78f67ea03e95b1cde361c7bc0a551abc18c9d81be908ffcdb8e7d165f9
MD5 fb410e9627ad1a3576dfdffebbbdedbc
BLAKE2b-256 fc1bd188f4b4ac75a17feaac9572e9395c7be3053770d6dd9f6a688734c28a33

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page