Skip to content

Commit caab4c9

Browse files
Merge pull request #40 from nickmaccarthy/feat/python-3-type-hints
feat: many many fixes and additions
2 parents 6d047b3 + 42cd737 commit caab4c9

File tree

10 files changed

+187
-117
lines changed

10 files changed

+187
-117
lines changed

.github/workflows/tests.yaml

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,19 @@ on:
77
branches:
88
- master
99
jobs:
10-
tests-python2:
11-
runs-on: ubuntu-20.04
12-
steps:
13-
- uses: actions/checkout@v2
14-
- run: sudo apt install python2
15-
- run: curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py
16-
- run: sudo python2 get-pip.py
17-
- run: pip2 install -r requirements-2.txt
18-
- run: python2 tests.py
1910
tests-python3:
20-
runs-on: ubuntu-20.04
11+
runs-on: ubuntu-20.04 # this should probably get updated to a python 3.9+ image
2112
steps:
2213
- uses: actions/checkout@v2
14+
- name: print python version
15+
run: python3 --version
2316
- name: install requirements
2417
run: pip3 install -r requirements-3.txt
2518
- name: run the tests
2619
run: python3 tests.py
20+
- name: verify type hints
21+
run: mypy datemath
22+
- name: verify package install
23+
run: python3 setup.py install --user
24+
- name: verify we can import
25+
run: python3 -c "from datemath import datemath; print(datemath('now-1d'))"

CHANGELOG.md

Lines changed: 73 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,87 @@
11
# Changelog
2-
## 1.5.5 (2021-04-26)
3-
* [FIX] [Issue #28](https://github.com/nickmaccarthy/python-datemath/issues/28)
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
8+
## [Unreleased]
9+
### fixed
10+
- Fix: Race condition in timezone tests: https://github.com/nickmaccarthy/python-datemath/issues/36
11+
- Fix: Updated arrow version: https://github.com/nickmaccarthy/python-datemath/issues/32
12+
- Fix: mypy type hint checking in tests: https://github.com/nickmaccarthy/python-datemath/issues/31
13+
- Fix: SyntaxWarning: invalid escape sequence in `re.match()`: https://github.com/nickmaccarthy/python-datemath/pull/39
14+
- Fix: Licence Classifier: https://github.com/nickmaccarthy/python-datemath/pull/34
15+
- Fix: Bump certifi to latest: https://github.com/nickmaccarthy/python-datemath/pull/38
16+
### added
17+
- Feat: Typehint support: https://github.com/nickmaccarthy/python-datemath/issues/31
18+
- Feat: Revamed CHANGELOG.md to keepachangelog.org format
19+
20+
### todo
21+
- todo: Fix pypi: https://github.com/nickmaccarthy/python-datemath/issues/33
22+
23+
## deprecated
24+
- python 2.7 support. Python 3.8+ will only be supported going forward
25+
26+
## [1.5.5] - 2021-04-26
27+
### fixed
28+
- fix: [Issue #28](https://github.com/nickmaccarthy/python-datemath/issues/28)
429
* `datemath()` object now returns the expected `datetime` object instead of an `Arrow` object
530
* added tests to catch invalid object types of helpers
631

7-
## 1.5.4 (2021-04-20)
8-
* skipped due to name conflict on pypi, all changes in this are from `1.5.3`
32+
## [1.5.4] - 2021-04-20
33+
### Unused
34+
- skipped due to name conflict on pypi, all changes in this are from `1.5.3`
935

10-
## 1.5.3 (2021-04-16)
11-
* [FIX] [Issue #25](https://github.com/nickmaccarthy/python-datemath/issues/25) - Fixed an issue where if you provided an invalid timestamp, i.e. `datemath('2')` you would not get an DateMathException back. Also bumped dependencies.
36+
## [1.5.3] - 2021-04-16
37+
### fixed
38+
- FIX: [Issue #25](https://github.com/nickmaccarthy/python-datemath/issues/25) - Fixed an issue where if you provided an invalid timestamp, i.e. `datemath('2')` you would not get an DateMathException back. Also bumped dependencies.
1239

13-
## 1.5.2 (2020-10-01)
14-
* [FIX] [Issue #21](https://github.com/nickmaccarthy/python-datemath/issues/21) - Fixed an issue where if timezone offset was in a datetime string (ISO8601), the timezone of the returned datemath object would be UTC and not the timezone as specified in the datetime string.
40+
## [1.5.2] - 2020-10-01
41+
### fixed
42+
- FIX: [Issue #21](https://github.com/nickmaccarthy/python-datemath/issues/21) - Fixed an issue where if timezone offset was in a datetime string (ISO8601), the timezone of the returned datemath object would be UTC and not the timezone as specified in the datetime string.
1543

1644
## 1.5.1 (2020-03-25)
1745

18-
* [FIX] [Issue #15](https://github.com/nickmaccarthy/python-datemath/issues/15) - Fixed issue with parser finding invalid timeunits and throwing correct errors
19-
* [NEW] [Issue #16](https://github.com/nickmaccarthy/python-datemath/issues/16) - Added support for parser to accecpt a epoch/unix timestamp but throw an error on epoch milli's since arrow can't support that.
46+
### fixed
47+
- FIX: [Issue #15](https://github.com/nickmaccarthy/python-datemath/issues/15) - Fixed issue with parser finding invalid timeunits and throwing correct errors
48+
### added
49+
- Feat: [Issue #16](https://github.com/nickmaccarthy/python-datemath/issues/16) - Added support for parser to accecpt a epoch/unix timestamp but throw an error on epoch milli's since arrow can't support that.
2050

21-
## 1.5.0 (2019-11-09)
51+
## 1.5.0 - 2019-11-09
2252

23-
* [FIX] [Issue #12](https://github.com/nickmaccarthy/python-datemath/issues/12) - missing VERSION.txt. Added MANIFEST.in for sdist build
24-
* [FIX] [PR #13](https://github.com/nickmaccarthy/python-datemath/pull/13) - Fix `BaseException` to `Exception` inheritence, thank you for your contribution @yury-primer!
53+
### fixed
54+
- [Issue #12](https://github.com/nickmaccarthy/python-datemath/issues/12) - missing VERSION.txt. Added MANIFEST.in for sdist build
55+
- [PR #13](https://github.com/nickmaccarthy/python-datemath/pull/13) - Fix `BaseException` to `Exception` inheritence, thank you for your contribution @yury-primer!
2556

26-
## 1.4.9 (2019-10-26)
57+
## [1.4.9] - 2019-10-26
2758

2859
** PLEASE DO NOT USE THIS VERSION, use `1.5.0+` instead. This may not compile on your system due to a missing VERSION.txt which was fixed in `1.5.0+` **
29-
* [FIX] [Issue #9](https://github.com/nickmaccarthy/python-datemath/issues/9) && [Issue #8](https://github.com/nickmaccarthy/python-datemath/issues/8) - Fixing deprecated arrow `replace()` function with `shift()`.
30-
* [FIX] Arrow upgrade to `0.15.2` to fix the above mentioned issues
31-
* [NEW] Breakout of python2 and python3 requirements
32-
* [NEW] Breakout of python2 and python3 specific CICD pipelines
33-
* [NEW] Derecated the following python version (although they may still work, they are no longer supported) - `2.4`,`2.6`,`3.4`,`3.5`
34-
* [FIX] Modifed `tests.py` to account for the timestamp change (tz is now `+0000`, instead of `-0000`)
35-
* [FIX] replaced `ts = ts.replace(tzinfo=tz.gettz(timezone))` with `ts = ts.replace(tzinfo=timezone)` in `datemath.helpers.parseTime()` to fix [Issue #7](https://github.com/nickmaccarthy/python-datemath/issues/7)
36-
37-
## v1.4.8 (2019-10-25)
60+
61+
### fixed
62+
- [FIX] [Issue #9](https://github.com/nickmaccarthy/python-datemath/issues/9) && [Issue #8](https://github.com/nickmaccarthy/python-datemath/issues/8) - Fixing deprecated arrow `replace()` function with `shift()`.
63+
- [FIX] Arrow upgrade to `0.15.2` to fix the above mentioned issues
64+
- [FIX] Modifed `tests.py` to account for the timestamp change (tz is now `+0000`, instead of `-0000`)
65+
- [FIX] replaced `ts = ts.replace(tzinfo=tz.gettz(timezone))` with `ts = ts.replace(tzinfo=timezone)` in `datemath.helpers.parseTime()` to fix [Issue #7](https://github.com/nickmaccarthy/python-datemath/issues/7)
66+
### added
67+
68+
- [NEW] Breakout of python2 and python3 requirements
69+
- [NEW] Breakout of python2 and python3 specific CICD pipelines
70+
- [NEW] Derecated the following python version (although they may still work, they are no longer supported) - `2.4`,`2.6`,`3.4`,`3.5`
71+
72+
73+
## [1.4.8] - 2019-10-25
74+
** dont use this version **
3875
* skipped due to name conflict on pypi, all changes are in `1.4.9`
3976

40-
## v1.4.7 (2017-11-10)
41-
* [FIX] Fixed timezone for date strings: [Issue #6](https://github.com/nickmaccarthy/python-datemath/issues/6)
77+
## [1.4.7] - 2017-11-10
78+
### fixed
79+
- [FIX] Fixed timezone for date strings: [Issue #6](https://github.com/nickmaccarthy/python-datemath/issues/6)
4280

43-
## v1.4.5 (2017-03-21)
44-
* [NEW] Added roundDown functionality. Allows user to specify the default rounding for expressions such as `/d`.
45-
* example - assuming the time is currently 2016-01-01 12:00:00, we should get the following
81+
## [1.4.5] - 2017-03-21
82+
### added
83+
- [NEW] Added roundDown functionality. Allows user to specify the default rounding for expressions such as `/d`.
84+
- example - assuming the time is currently 2016-01-01 12:00:00, we should get the following
4685
```
4786
>>> # now = 2016-01-01 14:00:00+00:00
4887
>>> dm('now+/d', roundDown=False)
@@ -51,8 +90,10 @@
5190
<Arrow [2016-01-01T00:00:00+00:00]>
5291
```
5392

54-
## v1.4.4 (2016-12-28)
55-
* [FIX] Fixed bug with expression logic and rounding: https://github.com/nickmaccarthy/python-datemath/pull/2
93+
## [1.4.4] - 2016-12-28
94+
### fixed
95+
- [FIX] Fixed bug with expression logic and rounding: https://github.com/nickmaccarthy/python-datemath/pull/2
5696

57-
## 1.4.3 (2016-03-31)
58-
* [NEW] Floats are now supported for days, hours, and seconds units. Example ```now-2.5d```, ```now-3.2h```. Any other unit other than days, hours, or seconds that is a float will be converted to an int and floored due to the datetime() module not being able to handle them.
97+
## [1.4.3] - 2016-03-31
98+
### added
99+
[NEW] Floats are now supported for days, hours, and seconds units. Example ```now-2.5d```, ```now-3.2h```. Any other unit other than days, hours, or seconds that is a float will be converted to an int and floored due to the datetime() module not being able to handle them.

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
3+
tests:
4+
python3 tests.py
5+

README.md

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
1-
[![Build Status](https://travis-ci.org/nickmaccarthy/python-datemath.svg?branch=master)](https://travis-ci.org/nickmaccarthy/python-datemath.svg?branch=master)
2-
31
# Python Datemath
42

53
## What?
64

7-
A date math (aka datemath) parser compatiable with the elasticsearch 'date math' format
8-
9-
## Why?
10-
11-
Working with date objects in python has always been interesting. Having a background in php, I have been looking for quite some time ( no pun intended ) for a way to do date time interpolation similar to php's ```strtotime()``` function. While the arrow module comes close, I needed something that could turn date math type strings into datetime objects for use in [tattle.io](http://tattle.io) and other projects I use in elasticsearch. I have found even more uses for it, including AWS cloudwatch and various other projects and hopefully you will too.
5+
A date math (aka datemath) parser compatiable with the elasticsearch "date math" format
126

13-
## What is date math?
7+
## What is "date math"?
148

15-
Date Math is the short hand arithmetic to find relative time to fixed moments in date and time. Similar to the SOLR date math format, Elasticsearch has its own built in format for short hand date math and this module aims to support that same coverage in python.
16-
17-
Documentation from elasticsearch:
18-
[http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-date-format.html#date-math](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-date-format.html#date-math)
9+
Date Math is the short hand arithmetic to find relative time to fixed moments in date and time. Similar to the SOLR date math format, we aim to support that same coverage in python.
1910

2011
> The date type supports using date math expression when using it in a query/filter (mainly makes sense in range query/filter).
2112
>
@@ -31,6 +22,7 @@ Documentation from elasticsearch:
3122
3223
## Unit Maps
3324

25+
The "unit maps" here define the shorthand sytax for the dates/timeframes we are working with:
3426
```yaml
3527
y or Y = 'year'
3628
M = 'month'
@@ -39,17 +31,14 @@ d or D = 'day'
3931
w = 'week'
4032
h or H = 'hour'
4133
s or S = 'second'
42-
```
43-
44-
## Install
45-
46-
```python
47-
pip install python-datemath
34+
now = <current_time_and_date>
4835
```
4936

5037
## Examples
5138

52-
Assuming our datetime is currently: `2016-01-01T00:00:00-00:00`
39+
Here are some examples of using date math to find dates both in the past and in the future
40+
41+
Assuming our "now" datetime is currently: `2016-01-01T00:00:00-00:00`
5342

5443
```yaml
5544
Expression: Result:
@@ -73,7 +62,7 @@ now/Y 2016-12-31T23:59:59+00:00
7362

7463
## Usage
7564

76-
By default datemath return an arrow date object representing your timestamp.
65+
If you use the `dm` function in the datemath module, we will return an arrow date object representing your timestamp.
7766

7867
```python
7968
>>> from datemath import dm
@@ -118,8 +107,25 @@ If you would rather have a string, you can use arrow's ```.format()``` method.
118107
u'2015.12.18'
119108
```
120109

121-
Rather have a python datetime object instead? Just pass along the 'datetime' type
110+
If you would rather have your time object come back in standard python `datetime`, use the `datemath` function instead:
111+
112+
```python
113+
>>> from datemath import datemath
114+
>>> ## Assuming "now" is 2016-01-01T00:00:00
115+
>>> datemath("now-1h")
116+
datetime.datetime(2015, 12, 31, 23, 0, tzinfo=tzutc())
117+
# Cast it as a str() get a string of the timestamp back too
118+
>>> str(datemath("now-1h"))
119+
'2015-12-31 23:00:00+00:00'
120+
>>> # roundDown=True is default and implied
121+
>>> datemath('2016-01-01T16:20:00||/d')
122+
datetime.datetime(2016, 1, 1, 0, 0, tzinfo=tzutc())
123+
>>> # Using the roundDown option
124+
>>> datemath('2016-01-01T16:20:00||/d', roundDown=False)
125+
datetime.datetime(2016, 1, 1, 23, 59, 59, 999999, tzinfo=tzutc())
126+
```
122127

128+
Or you can use the `dm` function and set its `type` to `datetime`:
123129
```python
124130
from datemath import dm
125131
>>> dm('now', type='datetime')
@@ -129,21 +135,7 @@ datetime.datetime(2016, 1, 22, 22, 58, 28, 338060, tzinfo=tzutc())
129135
datetime.datetime(2016, 1, 24, 22, 57, 45, 394470, tzinfo=tzutc())
130136
```
131137

132-
Or you can just import the `datemath` module, this will always give us a native `datetime` object
133-
134-
```python
135-
>>> from datemath import datemath
136-
>>>
137-
>>> datemath('2016-01-01T16:20:00||/d', roundDown=False)
138-
datetime.datetime(2016, 1, 1, 23, 59, 59, 999999, tzinfo=tzutc())
139-
>>>
140-
>>>
141-
>>> # roundDown=True is default and implied
142-
>>> datemath('2016-01-01T16:20:00||/d')
143-
datetime.datetime(2016, 1, 1, 0, 0, tzinfo=tzutc())
144-
```
145-
146-
If you want a Epoch timestamp back instead, we can do that.
138+
If you want a Epoch timestamp back instead, we can do that too.
147139

148140
```python
149141
>>> dm('now+2d-1m', type='timestamp')
@@ -152,7 +144,7 @@ If you want a Epoch timestamp back instead, we can do that.
152144

153145
## What timezone are my objects in?
154146

155-
By default all object returned by datemath are in UTC.
147+
By default all objects returned by datemath are in UTC.
156148

157149
If you want them them back in a different timezone, just pass along the ```tz``` argument. Timezone list can be found here: [https://gist.github.com/pamelafox/986163](https://gist.github.com/pamelafox/986163)
158150

@@ -188,6 +180,12 @@ Note - currently timestrings with a timezone offset and the usage of the ```tz``
188180
<Arrow [2016-01-01T00:00:00-05:00]>
189181
```
190182

183+
## Install
184+
185+
```python
186+
pip install python-datemath
187+
```
188+
191189
## Debugging
192190

193191
If you would like more verbose output to debug the process of what datemath is doing, simply set `export DATEMATH_DEBUG=true` in your shell then run some datemath tests. To stop debugging, run `unset DATEMATH_DEBUG`.

RELEASE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# How to release
22

3-
* Create a new tag/release in github.
3+
* Create a new tag/release in github.
44
* Ensure new tag version matches VERSION.txt
55
* Actions should take care of the rest

VERSION.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.5.5
1+
3.0.1

0 commit comments

Comments
 (0)