clockwork 0.1.3__tar.gz → 0.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: clockwork
3
- Version: 0.1.3
3
+ Version: 0.2.0
4
4
  Summary: Toolkit for time-related operations including scheduling, logging, date manipulation, and more.
5
5
  Home-page: https://github.com/zteinck/clockwork
6
6
  License: MIT
@@ -0,0 +1,4 @@
1
+ from .core import *
2
+
3
+ __version__ = '0.2.0'
4
+ __author__ = 'Zachary Einck <zacharyeinck@gmail.com>'
@@ -1,9 +1,8 @@
1
1
  import datetime
2
2
  import calendar
3
3
  import time
4
- from functools import cached_property
5
4
  from dateutil.relativedelta import relativedelta
6
- import holidays
5
+ import holidays as hd
7
6
  import pandas as pd
8
7
  import numpy as np
9
8
 
@@ -19,6 +18,11 @@ class DateBase(object):
19
18
  --------------------
20
19
  factory : func
21
20
  initializes new instances as the correct subclass
21
+ weekdays : dict
22
+ dictionary where keys are the days of the week and values
23
+ are the corresponding index values
24
+ holidays : holidays.countries.united_states.UnitedStates
25
+ comprehensive list of U.S. holidays
22
26
 
23
27
  Instance Attributes
24
28
  --------------------
@@ -26,6 +30,15 @@ class DateBase(object):
26
30
  date and time (if applicable)
27
31
  '''
28
32
 
33
+ #╭-------------------------------------------------------------------------╮
34
+ #| Class Attributes |
35
+ #╰-------------------------------------------------------------------------╯
36
+
37
+ holidays = hd.UnitedStates()
38
+ weekdays = {k: i for i, k in enumerate(calendar.day_name)}
39
+ weekdays.update({k[:3]: v for k, v in weekdays.items()})
40
+
41
+
29
42
  #╭-------------------------------------------------------------------------╮
30
43
  #| Initialize Instance |
31
44
  #╰-------------------------------------------------------------------------╯
@@ -119,7 +132,7 @@ class DateBase(object):
119
132
  ''' spawn new date instance '''
120
133
 
121
134
  def wrapper(self, *args, **kwargs):
122
- return cls.spawn(func(self, *args, **kwargs))
135
+ return self.spawn(func(self, *args, **kwargs))
123
136
 
124
137
  return wrapper
125
138
 
@@ -145,9 +158,9 @@ class DateBase(object):
145
158
  def wrapper(self, other):
146
159
  try:
147
160
  other = datetime.timedelta(float(other))
148
- return cls.spawn(func(self, other))
161
+ return self.spawn(func(self, other))
149
162
  except:
150
- other = cls.spawn(other).dt
163
+ other = self.spawn(other).dt
151
164
  return func(self, other)
152
165
 
153
166
  return wrapper
@@ -300,20 +313,6 @@ class DateBase(object):
300
313
  ''' returns the current holiday, if applicable '''
301
314
  return self.holidays.get(self.ymd)
302
315
 
303
- @cached_property
304
- def weekdays(self):
305
- ''' dictionary where keys are the days of the week and values are the corresponding index values '''
306
- out = {k: i for i, k in enumerate((
307
- 'Monday','Tuesday','Wednesday','Thursday',
308
- 'Friday','Saturday','Sunday'))}
309
- out.update({k[:3]: v for k, v in out.items()})
310
- return out
311
-
312
- @cached_property
313
- def holidays(self):
314
- ''' comprehensive list of U.S. holidays '''
315
- return holidays.UnitedStates()
316
-
317
316
 
318
317
  #╭-------------------------------------------------------------------------╮
319
318
  #| Magic Methods |
@@ -326,7 +325,7 @@ class DateBase(object):
326
325
  components = ['%Y-%m-%d']
327
326
  if self.dt.hour + self.dt.second + self.dt.microsecond > 0:
328
327
  components.append('%I:%M:%S.%f %p')
329
- return self.str(' '.join(components))
328
+ return self.__class__.__name__ + '(%s)' % self.str(' '.join(components))
330
329
 
331
330
  def __int__(self):
332
331
  return self.timestamp
@@ -12,7 +12,7 @@ from ._month_end import MonthEnd
12
12
  #| Functions |
13
13
  #╰-------------------------------------------------------------------------╯
14
14
 
15
- def Date(arg=None, normalize=False, week_offset=0):
15
+ def Date(arg=None, normalize=False, format=None, week_offset=0):
16
16
  '''
17
17
  Description
18
18
  ------------
@@ -30,11 +30,13 @@ def Date(arg=None, normalize=False, week_offset=0):
30
30
  • string
31
31
  ► day of the week fully spelled out or first 3 letters (not case sensitive)
32
32
  (e.g. 'Monday', 'monday', 'mon')
33
- ► quarter in #QYY or YYYYQ# format
33
+ ► quarter in #QYY, YYYYQ#, or Q# format
34
34
  ► any string format supported by pd.to_datetime
35
35
  • DateBase object or DateBase polymorphism
36
36
  normalize : bool
37
37
  if True, only the year, month, and day are retained (hours, minutes, seconds, microseconds are set to zero)
38
+ format : str
39
+ if 'arg' is a string, this format is used to parse it (e.g. '%Y%m%d %H%S').
38
40
  week_offset : int
39
41
  by default, if a day of the week is supplied (e.g. 'Monday') then the date returned will be that day of the week
40
42
  for the current week. This argument is used to override this behavior by shifting the week backwards or forwards
@@ -51,20 +53,31 @@ def Date(arg=None, normalize=False, week_offset=0):
51
53
  def qtr_label_to_dt(x):
52
54
  ''' attempts to convert quarter expressed as string to datetime.
53
55
  Suported formats include #QYY and YYYYQ# '''
56
+ x = x.upper()
54
57
  try:
55
- qtr, year = re.findall(r'(\d{1})Q(\d{2})', x)[0]
58
+ qtr, year = re.findall(r'^(\d{1})Q(\d{2})$', x)[0]
56
59
  except:
57
60
  try:
58
- year, qtr = re.findall(r'(\d{4})Q(\d{1})', x)[0]
61
+ year, qtr = re.findall(r'^(\d{4})Q(\d{1})$', x)[0]
59
62
  except:
60
- return
63
+ try:
64
+ qtr = re.findall(r'^Q(\d{1})$', x)[0]
65
+ year = datetime.datetime.now().year
66
+ except:
67
+ return
61
68
 
62
- inverse = {v: k for k,v in qtr_map.items()}
69
+ inverse = {v: k for k, v in qtr_map.items()}
63
70
  month, day = inverse[int(qtr)]
64
71
  out = DateBase.to_datetime(f'{month}-{day}-{year}')
65
72
  return out
66
73
 
67
74
 
75
+ if format is not None:
76
+ if not isinstance(arg, str):
77
+ raise TypeError(f"When 'format' argument is not None, 'arg' must be a string, not {type(arg)}.")
78
+ arg = DateBase.to_datetime(arg, format=format)
79
+ return Date(arg, normalize=normalize)
80
+
68
81
  if arg is None:
69
82
  dt = datetime.datetime.now()
70
83
  elif hasattr(arg, 'to_pydatetime'): # pandas
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "clockwork"
3
- version = "0.1.3"
3
+ version = "0.2.0"
4
4
  description = "Toolkit for time-related operations including scheduling, logging, date manipulation, and more."
5
5
  authors = ["Zachary Einck <zacharyeinck@gmail.com>"]
6
6
  license = "MIT"
@@ -1,4 +0,0 @@
1
- from .core import *
2
-
3
- __version__ = '0.1.3'
4
- __author__ = 'Zachary Einck <zacharyeinck@gmail.com>'
File without changes
File without changes
File without changes