# 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:
```pytohn
_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:
```python
assert calc(expr1, 1, 2) == 3
```
The library supports all overloadable operators in python, including attribute access and calls:
```python
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
```python
expr = [i for i in lazy(range)(_0)]
assert calc(expr, 4) == [0, 1, 2, 3]
```
Note the`lazy` 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:
```python
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:
```python
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:
```python
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:
```python
@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:
```python
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:
```python
@dataclass
@exprclass
class OrderWithOp:
...
```
And
```python
@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
Raw data
{
"_id": null,
"home_page": null,
"name": "dexpr",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.11",
"maintainer_email": null,
"keywords": "expressions, date generators, extended dataclass",
"author": null,
"author_email": "wukrur <wukrur@github.com>",
"download_url": "https://files.pythonhosted.org/packages/e3/16/4379664b7996ae1eb9366db8bcb54c14e5cff9d374ebe1c97cce0239fdc1/dexpr-0.0.6.tar.gz",
"platform": null,
"description": "# DExpr - general and date expressions library\nA little library for capturing and using various types of expressions in python.\n\n\n## magic Op\nmagic Op captures expressions that use ParameterOp objects via operator overloads and creates a data structure\nto represent operations captured:\n\n```pytohn\n_1 = ParameterOp(_index=0)\n_2 = ParameterOp(_index=1)\n\nexpr1 = _1 + _2\n```\nhere `expr1` is an instance of Op subclass that catures that an __add__ operator was called with the `_1` and `_2` \nvariables. If we want to evaluate that expression with arbitrary arguments we use the `calc` function:\n\n```python\nassert calc(expr1, 1, 2) == 3\n```\n The library supports all overloadable operators in python, including attribute access and calls:\n\n```python\nexpr2 = _1.fn(_2)\n```\nif `expr2` is evaluated with an object that provides `fn` method as a first parameter it will call it and pass \nin the value of the second parameter\n\nOp supports list and dict comprehensions\n```python\nexpr = [i for i in lazy(range)(_0)]\nassert calc(expr, 4) == [0, 1, 2, 3]\n```\nNote the`lazy` call to make the `range` function an Op\n\n### DGen - date generators\n\nThe DGen simplifies producing lists of dates from an expression. The usual DGen expression\nlooks like this:\n\n```python\nlast_sundays_in_march = '1980-01-01' < years.months[2].weeks[-1].sun < '2030-01-01'\n```\n\nThe main parts of a DGen expression are the date expression itself that uses `years`, `months`, `weeks`, \n`weekdays`, `weekends`, `days` series to compose a rule for which date is it going to generate and \nbounding conditions that limit the series to specific bounds. The last bit does not have to be put into \nthe expression itself but can be supplied when we ask it to generate:\n\n```python\nlast_sundays_in_march = years.months[2].weeks[-1].sun\nlist_of_last_sundays_in_march = list(last_sundays_in_march(after='1980-01-01', before='2030-01-01'))\n```\n\nThere is also a support for adding and subtracting tenors from dates:\n\n```python\nmy_dates = months.days[14] + '1w'\n```\n\nTenors supported are y, m, w, d and b. b is only valid if a calendar is supplied.\n\n\n### dataclass Extensions\n\nThe `dataclassex` library allows the magic Ops and lamdas to be used as defaults for fields \nin dataclasses. The same can be done with properties, this library just makes it super simple \nto do:\n\n```python\n@dataclassex\nclass OrderWithLambda:\n order_date: date\n shipping_time: Tenor\n expected_delivery_date: date = lambda self: self.order_date + self.shipping_time\n\no = OrderWithLambda(order_date=make_date('2020-02-02'), shipping_time=Tenor('3d'))\nassert o.expected_delivery_date == make_date('2020-02-05')\n```\n\nadding Op expressions:\n\n```python\nSelf = ParameterOp(_name='Self')\n\n@dataclassex\nclass OrderWithOp:\n Self: 'OrderWithOp'\n \n order_date: date\n shipping_time: Tenor\n expected_delivery_date: date = Self.order_date + Self.shipping_time\n\no = OrderWithLambda(order_date=make_date('2020-02-02'), shipping_time=Tenor('3d'))\nassert o.expected_delivery_date == make_date('2020-02-05')\n```\n\nNote the `Self` member - it is there for enable autocomplete in the IDE when writing the expressions and is removed from\nprocessed annotations\n\nThere are two other variants of syntax supported for declaring dataclasses with support for Ops and lambdas:\n\n```python\n@dataclass\n@exprclass\nclass OrderWithOp:\n ...\n```\n\nAnd\n\n```python\n@dataclass\nclass OrderWithOp(ExprClass):\n ...\n```\n\nThey exist for cases when the developer wants to keep the @dataclass annotations as is \nto use PyCharm's built in support for it hence enabling autocompletion and other IDE functionality\n\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Libraries for capturing and using expressions in python",
"version": "0.0.6",
"project_urls": null,
"split_keywords": [
"expressions",
" date generators",
" extended dataclass"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "44404d6b96e53259d449539f96d0acb378c69c9e5bd05beb07379f6bad7e3cf1",
"md5": "2af8de95f9f74121fd9ce92486fbe5aa",
"sha256": "3c272a5afc6f5b383d770747286456f0b3cd12255f44f0cfce7cc1ea2afe413a"
},
"downloads": -1,
"filename": "dexpr-0.0.6-py3-none-any.whl",
"has_sig": false,
"md5_digest": "2af8de95f9f74121fd9ce92486fbe5aa",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.11",
"size": 14629,
"upload_time": "2024-04-10T08:41:41",
"upload_time_iso_8601": "2024-04-10T08:41:41.946332Z",
"url": "https://files.pythonhosted.org/packages/44/40/4d6b96e53259d449539f96d0acb378c69c9e5bd05beb07379f6bad7e3cf1/dexpr-0.0.6-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "e3164379664b7996ae1eb9366db8bcb54c14e5cff9d374ebe1c97cce0239fdc1",
"md5": "11338f1e7f0a8d462430b41d845a3bef",
"sha256": "ad9fd86574bcdcbd1e34e3b2b07d8dfe32bd1691c6b838619124333164a201c8"
},
"downloads": -1,
"filename": "dexpr-0.0.6.tar.gz",
"has_sig": false,
"md5_digest": "11338f1e7f0a8d462430b41d845a3bef",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.11",
"size": 14938,
"upload_time": "2024-04-10T08:41:42",
"upload_time_iso_8601": "2024-04-10T08:41:42.945434Z",
"url": "https://files.pythonhosted.org/packages/e3/16/4379664b7996ae1eb9366db8bcb54c14e5cff9d374ebe1c97cce0239fdc1/dexpr-0.0.6.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-04-10 08:41:42",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "dexpr"
}