Compare commits

..

18 Commits

Author SHA1 Message Date
kennethreitz d50dc8701c simply test suite 2017-05-27 12:07:57 -04:00
Christian Stade-Schuldt 6c1cc24ad5 minor 2017-05-26 01:14:59 -07:00
Christian Stade-Schuldt 46b7e369b3 adapt compat.py style to the one from requests 2017-05-25 12:14:58 -07:00
Christian Stade-Schuldt 2b60e817ea add compatibility for Python3 2017-05-25 12:01:01 -07:00
kennethreitz 0fea886c00 Merge pull request #57 from c17r/patch-1
Regenerate lock file for 3.6
2017-05-25 08:57:55 -07:00
Christian Sauer ef52cb4b7d Regenerate lock file for 3.6
Maya uses `ruamel.yaml` which needs `ruamel.ordereddict` under 2.7 but does not under 3.x.  `ruamel.ordereddict` doesn't build under 3.x.  Maya's Pipfile.lock file was created under 2.7 so `ruamel.ordereddict` is listed as a dependency.
2017-05-25 11:56:25 -04:00
kennethreitz f1be1585d3 Update README.rst 2017-05-25 11:21:11 -04:00
kennethreitz 89ee548ec1 Update README.rst 2017-05-25 11:21:01 -04:00
kennethreitz 09351afc68 Update README.rst 2017-05-25 10:41:23 -04:00
kennethreitz 5d4a217fd6 Update README.rst 2017-05-25 10:41:03 -04:00
kennethreitz 27950e6d35 fixes 2017-05-23 11:45:33 -07:00
David Gouldin f2c52af853 Merge branch 'fix-interval-contains' 2017-05-23 11:44:57 -07:00
David Gouldin 01cc151b5c Correcting order of 'in' operator in code and tests 2017-05-23 11:43:48 -07:00
kennethreitz eda4a92da9 Merge branch 'master' of github.com:kennethreitz/maya 2017-05-23 11:38:24 -07:00
kennethreitz 6fbf8f2d74 fix the bug 2017-05-23 11:38:09 -07:00
kennethreitz 343abed679 fix the bug 2017-05-23 11:36:39 -07:00
kennethreitz 704af5a3a0 Update README.rst 2017-05-23 11:27:47 -07:00
kennethreitz f9f5b97f62 Update README.rst 2017-05-23 11:27:03 -07:00
6 changed files with 236 additions and 107 deletions
+1 -1
View File
@@ -4,6 +4,6 @@ python:
- "3.6"
# command to install dependencies
install: pip install pipenv; pipenv install --dev
install: pip install pipenv; pipenv lock; pipenv install --dev
# command to run tests
script: pipenv run pytest
+27 -3
View File
@@ -1,5 +1,5 @@
Maya: Timestamps for Humans™
============================
Maya: Datetimes for Humans™
===========================
.. image:: https://img.shields.io/pypi/v/maya.svg
:target: https://pypi.python.org/pypi/maya
@@ -77,6 +77,28 @@ Behold, datetimes for humans!
>>> maya.interval(start=maya.now(), end=maya.now().add(days=1), interval=60*60)
<generator object intervals at 0x105ba5820>
☤ Advanced Usage of Maya
------------------------
In addition to timestamps, Maya also includes a wonderfuly powerful ``MayaInterval`` class, which represents a range of time (e.g. an event). With this class, you can perform a multitude of advanced calendar calculations with finese and ease.
For example:
.. code-block:: pycon
>>> from maya import MayaInterval
# Create an event that is one hour long, starting now.
>>> event_start = maya.now()
>>> event_end = event_start.add(hours=1)
>>> event = MayaInterval(start=event_start, end=event_end)
From here, there a a number of methods available to you, which you can use to compare this event to another event.
☤ Why is this useful?
---------------------
@@ -92,11 +114,13 @@ Behold, datetimes for humans!
☤ What about Delorean, Arrow, & Pendulum?
-----------------------------------------
All these project complement eachother, and are friends. Pendulum, for example, helps power Maya's parsing.
Arrow, for example, is a fantastic library, but isn't what I wanted in a datetime library. In many ways, it's better than Maya for certain things. In some ways, in my opinion, it's not.
I simply desire a sane API for datetimes that made sense to me for all the things I'd ever want to do—especially when dealing with timezone algebra. Arrow doesn't do all of the things I need (but it does a lot more!). Maya does do exactly what I need.
I think these projects complement each-other, personally. Maya is great for parsing websites!
I think these projects complement each-other, personally. Maya is great for parsing websites, and dealing with calendar events!
☤ Installing Maya
+100
View File
@@ -0,0 +1,100 @@
# -*- coding: utf-8 -*-
"""
maya.compat
~~~~~~~~~~~~~~~
This module handles import compatibility issues between Python 2 and
Python 3.
"""
import sys
# -------
# Pythons
# -------
# Syntax sugar.
_ver = sys.version_info
#: Python 2.x?
is_py2 = (_ver[0] == 2)
#: Python 3.x?
is_py3 = (_ver[0] == 3)
# ---------
# Specifics
# ---------
if is_py2:
cmp = cmp
elif is_py3:
def cmp(a, b):
"""
Compare two objects.
Returns a negative number if C{a < b}, zero if they are equal, and a
positive number if C{a > b}.
"""
if a < b:
return -1
elif a == b:
return 0
else:
return 1
def comparable(klass):
"""
Class decorator that ensures support for the special C{__cmp__} method.
On Python 2 this does nothing.
On Python 3, C{__eq__}, C{__lt__}, etc. methods are added to the class,
relying on C{__cmp__} to implement their comparisons.
"""
# On Python 2, __cmp__ will just work, so no need to add extra methods:
if not is_py3:
return klass
def __eq__(self, other):
c = self.__cmp__(other)
if c is NotImplemented:
return c
return c == 0
def __ne__(self, other):
c = self.__cmp__(other)
if c is NotImplemented:
return c
return c != 0
def __lt__(self, other):
c = self.__cmp__(other)
if c is NotImplemented:
return c
return c < 0
def __le__(self, other):
c = self.__cmp__(other)
if c is NotImplemented:
return c
return c <= 0
def __gt__(self, other):
c = self.__cmp__(other)
if c is NotImplemented:
return c
return c > 0
def __ge__(self, other):
c = self.__cmp__(other)
if c is NotImplemented:
return c
return c >= 0
klass.__lt__ = __lt__
klass.__gt__ = __gt__
klass.__le__ = __le__
klass.__ge__ = __ge__
klass.__eq__ = __eq__
klass.__ne__ = __ne__
return klass
+15 -13
View File
@@ -1,4 +1,3 @@
# ___ __ ___ _ _ ___
# || \/ | ||=|| \\// ||=||
# || | || || // || ||
@@ -6,19 +5,21 @@
# Ignore warnings for yaml usage.
import warnings
import ruamel.yaml
warnings.simplefilter('ignore', ruamel.yaml.error.UnsafeLoaderWarning)
warnings.simplefilter('ignore', ruamel.yaml.error.UnsafeLoaderWarning)
import email.utils
import time
from datetime import timedelta, datetime as Datetime
import functools
import pytz
import humanize
import dateparser
import pendulum
from tzlocal import get_localzone
from compat import cmp, comparable
_EPOCH_START = (1970, 1, 1)
@@ -61,7 +62,7 @@ class MayaDT(object):
@validate_class_type_arguments('==')
def __eq__(self, maya_dt):
return self._epoch == maya_dt._epoch
return int(self._epoch) == int(maya_dt._epoch)
@validate_class_type_arguments('!=')
def __ne__(self, maya_dt):
@@ -84,7 +85,7 @@ class MayaDT(object):
return self._epoch >= maya_dt._epoch
def __hash__(self):
return hash(self.epoch)
return hash(int(self.epoch))
def __add__(self, item):
return self.add(seconds=seconds_or_timedelta(item).total_seconds())
@@ -276,15 +277,17 @@ def to_iso8601(dt):
def end_of_day_midnight(dt):
return dt if dt.time() == time.min else\
return dt if dt.time() == time.min else \
(dt.replace(hour=0, minute=0, second=0, microsecond=0) + timedelta(days=1))
@comparable
class MayaInterval(object):
"""
A MayaInterval represents a range between two datetimes, inclusive of the start
and exclusive of the end.
"""
def __init__(self, start=None, end=None, duration=None):
try:
# Ensure that proper arguments were passed.
@@ -314,7 +317,7 @@ class MayaInterval(object):
self.end = end
def __repr__(self):
return '<MayaInterval start={!r0} end={!r1}>'.format(self.start, self.end)
return '<MayaInterval start={0!r} end={1!r}>'.format(self.start, self.end)
def iso8601(self):
"""Returns an ISO 8601 representation of the MayaInterval."""
@@ -335,8 +338,7 @@ class MayaInterval(object):
# # Start and duration, such as "2007-03-01T13:00:00Z/P1Y2M10DT2H30M"
# # Duration and end, such as "P1Y2M10DT2H30M/2008-05-11T15:30:00Z"
raise NotImplentedError()
raise NotImplementedError()
def __and__(self, i):
return self.intersection(i)
@@ -463,7 +465,7 @@ class MayaInterval(object):
self.start == i.start or
start <= end
)
if ((either_instant and instant_overlap) or (start < end)):
if (either_instant and instant_overlap) or (start < end):
return MayaInterval(start, end)
def contains(self, i):
@@ -476,7 +478,7 @@ class MayaInterval(object):
if isinstance(item, MayaDT):
return self.contains_dt(item)
return item.contains(self)
return self.contains(item)
def contains_dt(self, dt):
return self.start <= dt < self.end
@@ -505,7 +507,7 @@ class MayaInterval(object):
@staticmethod
def flatten(ii):
return reduce(lambda reduced, i: (
return functools.reduce(lambda reduced, i: (
(reduced[:-1] + i.combine(reduced[-1]))
if reduced else [i]
), sorted(ii), [])
@@ -536,7 +538,8 @@ def when(string, timezone='UTC'):
timezone -- timezone referenced from (default: 'UTC')
"""
dt = dateparser.parse(string, settings={'TIMEZONE': timezone, 'RETURN_AS_TIMEZONE_AWARE': True, 'TO_TIMEZONE': 'UTC'})
dt = dateparser.parse(string,
settings={'TIMEZONE': timezone, 'RETURN_AS_TIMEZONE_AWARE': True, 'TO_TIMEZONE': 'UTC'})
if dt is None:
raise ValueError('invalid datetime input specified.')
@@ -558,7 +561,6 @@ def parse(string, day_first=False):
def seconds_or_timedelta(s):
# Convert seconds into timedelta.
if isinstance(s, int):
s = timedelta(seconds=s)
+1 -1
View File
@@ -35,7 +35,7 @@ required = [
setup(
name='maya',
version='0.3.0',
version='0.3.1',
description='Datetimes for Humans.',
long_description=long_description,
author='Kenneth Reitz',
+92 -89
View File
@@ -5,7 +5,7 @@ import pytest
import pytz
import maya
from compat import cmp
Los_Angeles = pytz.timezone('America/Los_Angeles')
New_York = pytz.timezone('America/New_York')
@@ -34,9 +34,9 @@ def test_interval_requires_2_of_start_end_duration():
def test_interval_requires_end_time_after_or_on_start_time():
maya.MayaInterval(start=maya.now(), duration=0)
with pytest.raises(ValueError):
maya.MayaInterval(start=maya.now(), duration=0)
maya.MayaInterval(start=maya.now(), duration=-1)
@@ -65,32 +65,32 @@ def test_interval_init_end_duration():
@pytest.mark.parametrize('start_doy1,end_doy1,start_doy2,end_doy2,intersection_doys', (
(0, 2, 1, 3, (1, 2)),
(0, 2, 3, 4, None),
(0, 2, 2, 3, None),
(0, 1, 0, 1, (0, 1)),
(1, 1, 1, 3, (1, 1)),
(1, 1, 1, 1, (1, 1)),
(1, 1, 2, 3, None),
(2, 2, 1, 3, (2, 2)),
(1, 3, 1, 1, (1, 1)),
(2, 3, 1, 1, None),
(1, 3, 2, 2, (2, 2)),
(0, 2, 1, 3, (1, 2)),
(0, 2, 3, 4, None),
(0, 2, 2, 3, None),
(0, 1, 0, 1, (0, 1)),
(1, 1, 1, 3, (1, 1)),
(1, 1, 1, 1, (1, 1)),
(1, 1, 2, 3, None),
(2, 2, 1, 3, (2, 2)),
(1, 3, 1, 1, (1, 1)),
(2, 3, 1, 1, None),
(1, 3, 2, 2, (2, 2)),
), ids=(
'overlapping',
'non-overlapping',
'adjacent',
'equal',
'instant overlapping start only',
'instant equal',
'instant disjoint',
'instant overlapping',
'instant overlapping start only (left)',
'instant disjoint (left)',
'instant overlapping (left)'
'overlapping',
'non-overlapping',
'adjacent',
'equal',
'instant overlapping start only',
'instant equal',
'instant disjoint',
'instant overlapping',
'instant overlapping start only (left)',
'instant disjoint (left)',
'instant overlapping (left)'
))
def test_interval_intersection(
start_doy1, end_doy1, start_doy2, end_doy2, intersection_doys
start_doy1, end_doy1, start_doy2, end_doy2, intersection_doys
):
base = maya.MayaDT.from_datetime(datetime(2016, 1, 1))
interval1 = maya.MayaInterval(
@@ -161,20 +161,20 @@ def test_interval_duration():
@pytest.mark.parametrize('start_doy1,end_doy1,start_doy2,end_doy2,expected', (
(0, 2, 1, 3, False),
(0, 2, 3, 4, False),
(0, 2, 2, 3, False),
(0, 1, 0, 1, True),
(0, 3, 1, 2, True),
(0, 2, 1, 3, False),
(0, 2, 3, 4, False),
(0, 2, 2, 3, False),
(0, 1, 0, 1, True),
(0, 3, 1, 2, True),
), ids=(
'overlapping',
'non-overlapping',
'adjacent',
'equal',
'subset',
'overlapping',
'non-overlapping',
'adjacent',
'equal',
'subset',
))
def test_interval_contains(
start_doy1, end_doy1, start_doy2, end_doy2, expected
start_doy1, end_doy1, start_doy2, end_doy2, expected
):
base = maya.MayaDT.from_datetime(datetime(2016, 1, 1))
interval1 = maya.MayaInterval(
@@ -187,24 +187,24 @@ def test_interval_contains(
)
assert interval1.contains(interval2) is expected
assert (interval1 in interval2) is expected
assert (interval2 in interval1) is expected
@pytest.mark.parametrize('start_doy,end_doy,dt_doy,expected', (
(2, 4, 1, False),
(2, 4, 2, True),
(2, 4, 3, True),
(2, 4, 4, False),
(2, 4, 5, False),
(2, 4, 1, False),
(2, 4, 2, True),
(2, 4, 3, True),
(2, 4, 4, False),
(2, 4, 5, False),
), ids=(
'before-start',
'on-start',
'during',
'on-end',
'after-end',
'before-start',
'on-start',
'during',
'on-end',
'after-end',
))
def test_interval_in_operator_maya_dt(
start_doy, end_doy, dt_doy, expected
start_doy, end_doy, dt_doy, expected
):
base = maya.MayaDT.from_datetime(datetime(2016, 1, 1))
interval = maya.MayaInterval(
@@ -239,10 +239,10 @@ def test_interval_iter():
(2, 4, 1, 3, 1),
(1, 2, 1, 3, -1),
], ids=(
'equal',
'less-than',
'greater-than',
'use-end-time-if-start-time-identical',
'equal',
'less-than',
'greater-than',
'use-end-time-if-start-time-identical',
))
def test_interval_cmp(start1, end1, start2, end2, expected):
base = maya.now()
@@ -263,10 +263,10 @@ def test_interval_cmp(start1, end1, start2, end2, expected):
(1, 2, 3, 4, [(1, 2), (3, 4)]),
(1, 5, 2, 3, [(1, 5)]),
], ids=(
'adjacent',
'overlapping',
'non-overlapping',
'contains',
'adjacent',
'overlapping',
'non-overlapping',
'contains',
))
def test_interval_combine(start1, end1, start2, end2, expected):
base = maya.now()
@@ -297,14 +297,14 @@ def test_interval_combine(start1, end1, start2, end2, expected):
(1, 4, 1, 2, [(2, 4)]),
(1, 4, 3, 4, [(1, 3)]),
], ids=(
'non-overlapping',
'adjacent',
'contains',
'splits',
'overlaps-left',
'overlaps-right',
'overlaps-left-identical-start',
'overlaps-right-identical-end',
'non-overlapping',
'adjacent',
'contains',
'splits',
'overlaps-left',
'overlaps-right',
'overlaps-left-identical-start',
'overlaps-right-identical-end',
))
def test_interval_subtract(start1, end1, start2, end2, expected):
base = maya.now()
@@ -330,10 +330,10 @@ def test_interval_subtract(start1, end1, start2, end2, expected):
(1, 3, 2, 3, False),
(2, 3, 4, 5, False),
], ids=(
'adjacent-right',
'adjacent-left',
'overlapping',
'non-overlapping',
'adjacent-right',
'adjacent-left',
'overlapping',
'non-overlapping',
))
def test_interval_is_adjacent(start1, end1, start2, end2, expected):
base = maya.now()
@@ -356,12 +356,12 @@ def test_interval_is_adjacent(start1, end1, start2, end2, expected):
(0, 2, 5, False, []),
(0, 2, 5, True, [(0, 2)]),
], ids=(
'even-split',
'even-split-include-partial',
'uneven-split-do-not-include-partial',
'uneven-split-include-partial',
'delta-larger-than-timepsan-do-not-include-partial',
'delta-larger-than-timepsan-include-partial',
'even-split',
'even-split-include-partial',
'uneven-split-do-not-include-partial',
'uneven-split-include-partial',
'delta-larger-than-timepsan-do-not-include-partial',
'delta-larger-than-timepsan-include-partial',
))
def test_interval_split(start, end, delta, include_remainder, expected):
base = maya.now()
@@ -406,16 +406,16 @@ def test_interval_split_non_positive_delta():
((6, 20), (6, 50), 60, 'America/Chicago', False, (6, 0), (6, 0)),
((6, 20), (6, 50), 60, 'America/Chicago', True, (6, 0), (7, 0)),
], ids=(
'normal',
'normal-snap_out',
'already-quantized',
'already-quantized-snap_out',
'with-timezone',
'with-timezone-snap_out',
'too-small',
'too-small-snap_out',
'too-small-with-timezone',
'too-small-with-timezone-snap_out',
'normal',
'normal-snap_out',
'already-quantized',
'already-quantized-snap_out',
'with-timezone',
'with-timezone-snap_out',
'too-small',
'too-small-snap_out',
'too-small-with-timezone',
'too-small-with-timezone-snap_out',
))
def test_quantize(start, end, minutes, timezone, snap_out, expected_start, expected_end):
base = maya.MayaDT.from_datetime(datetime(2017, 1, 1))
@@ -464,11 +464,14 @@ def test_interval_flatten_non_overlapping():
def test_interval_flatten_adjacent():
step = 2
max_hour = 20
base = maya.now()
intervals = [maya.MayaInterval(
start=base.add(hours=hour),
duration=timedelta(hours=step),
) for hour in range(0, max_hour, step)]
base = maya.when('jan/1/2011')
intervals = [
maya.MayaInterval(
start=base.add(hours=hour),
duration=timedelta(hours=step),
) for hour in range(0, max_hour, step)
]
random.shuffle(intervals)
assert maya.MayaInterval.flatten(intervals) == [maya.MayaInterval(