-
Notifications
You must be signed in to change notification settings - Fork 0
/
README
119 lines (92 loc) · 3.05 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# pyperiods
A Python library for representing and manipulating specific periods in time such as years and year/month.
## How to Install?
`pyperiods` is available on [Pypi](https://pypi.python.org/pypi/pyperiods):
```
pip install pyperiods
```
## Some features
- Both specific years (ex: 2012) and specific months (ex: January 2012) can be represented, manipulated and compared.
- Simple add/subtract operations are supported (`month + 1` = next month)
- Utilities for iterations (i.e.: iterate through all previous 12 months)
- A Django ModelField is provided for storing months/years in Django models
- A Django REST Framework serializer is provided for convenience as well
- Compatible with **Python 3** only
## Concepts
The main concept of the library is a **Period**, an abstract base class which can represent either a specific year (ex: 2012), or a specific month and its year (ex: January 2012).
Specific years are represented by instances of the **`YearPeriod`** class while specific months are represented by instances of the **`MonthPeriod`** class.
Periods are comparable and can be sorted. All periods have a string representation which corresponds to either the year only as 4 digits (ex: `2012`) or the year and the month as 6 digits (ex: `201201`)
## Examples
The basics:
``` python
>>> from pyperiods.months import MonthPeriod
>>> start = MonthPeriod(year=2016, month=5)
>>> start.year
2016
>>> start.month
5
>>> next_month = start + 1
>>> str(next_month)
'201606'
>>> next_month.format_long()
'June 2016'
>>> next_month.first_day()
datetime.date(2016, 5, 1)
>>> next_month.last_day()
datetime.date(2016, 5, 30)
>>> from pyperiods.years import YearPeriod
>>> current_year = YearPeriod()
>>> str(current_year)
'2016'
>>> list(current_year.months)
[201601, 201602, 201603, 201604, 201605, 201606, 201607, 201608, 201609, 201610, 201611, 201612]
```
### Basic arithmetic
``` python
>>> MonthPeriod('201611') + 2
201701
>>> MonthPeriod('201611') - 12
201511
```
### Sorting & Comparison
``` python
>>> periods = [
MonthPeriod('201607'),
MonthPeriod('201601'),
MonthPeriod('201501'),
MonthPeriod('201507'),
]
>>> list(periods.sort())
[201501, 201507, 201601, 201607]
>>> MonthPeriod('201502') < MonthPeriod('201601')
True
>>> MonthPeriod('201502') == MonthPeriod('201503') - 1
True
```
### Iteration
``` python
>>> list(MonthPeriod('201605').range(stop=6))
[201605, 201606, 201607, 201608, 201609, 201610, 201611]
>>> list(MonthPeriod('201605').range(start=-2))
[201603, 201604, 201605]
>>> list(MonthPeriod('201605').range(start=-2, stop=2))
[201603, 201604, 201605, 201606, 201607]
```
### Creation
``` python
>>> MonthPeriod('201605')
201605
>>> MonthPeriod(year=2016)
201601
>>> MonthPeriod(year=2016, month=2)
201602
>>> YearPeriod(2016)
2016
>>> from pyperiods.factory import period_from_string
>>> type(period_from_string('2016'))
<class 'pyperiods.years.YearPeriod'>
>>> type(period_from_string('201612'))
<class 'pyperiods.months.MonthPeriod'>
```
## APIs
See all unit tests in the `tests` folder for details on supported APIs.