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 expressions:

Self = ParameterOp(_name='Self')

@dataclassex
class OrderWithOp:
    Self: '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')

Note the Self member - it is there for enable autocomplete in the IDE when writing the expressions and is removed from processed annotations

There are two other variants of syntax supported for declaring dataclasses with support for Ops and lambdas:

@dataclass
@exprclass
class OrderWithOp:
    ...

And

@dataclass
class OrderWithOp(ExprClass):
    ...

They exist for cases when the developer wants to keep the @dataclass annotations as is to use PyCharm's built in support for it hence enabling autocompletion and other IDE functionality

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.6.tar.gz (14.9 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.6-py3-none-any.whl (14.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for dexpr-0.0.6.tar.gz
Algorithm Hash digest
SHA256 ad9fd86574bcdcbd1e34e3b2b07d8dfe32bd1691c6b838619124333164a201c8
MD5 11338f1e7f0a8d462430b41d845a3bef
BLAKE2b-256 e3164379664b7996ae1eb9366db8bcb54c14e5cff9d374ebe1c97cce0239fdc1

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for dexpr-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 3c272a5afc6f5b383d770747286456f0b3cd12255f44f0cfce7cc1ea2afe413a
MD5 2af8de95f9f74121fd9ce92486fbe5aa
BLAKE2b-256 44404d6b96e53259d449539f96d0acb378c69c9e5bd05beb07379f6bad7e3cf1

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