File: utils.py

package info (click to toggle)
python-leather 0.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 652 kB
  • sloc: python: 2,385; makefile: 117; sh: 5
file content (134 lines) | stat: -rw-r--r-- 2,342 bytes parent folder | download
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
from collections import namedtuple
from datetime import date, datetime, timedelta
from decimal import Decimal

try:
    __IPYTHON__
    from IPython.display import SVG as IPythonSVG
except (NameError, ImportError):
    def IPythonSVG(x):
        return x


# Shorthand
ZERO = Decimal('0')
NINE_PLACES = Decimal('1e-9')

#: X data dimension index
X = 0

#: Y data dimension index
Y = 1

#: Z data dimension index
Z = 2


DIMENSION_NAMES = ['X', 'Y', 'Z']

#: Data structure for representing margins or other CSS-edge like properties
Box = namedtuple('Box', ['top', 'right', 'bottom', 'left'])

#: Data structure for a single series data point
Datum = namedtuple('Datum', ['i', 'x', 'y', 'z', 'row'])

#: Dummy object used in place of a series when rendering legends for categories
DummySeries = namedtuple('DummySeries', ['name'])


def to_year_count(d):
    """
    date > n years
    """
    return d.year


def from_year_count(n, t=date):
    """
    n years > date
    """
    return t(n, 1, 1)


def to_month_count(d):
    """
    date > n months
    """
    return (d.year * 12) + d.month


def from_month_count(n, t=date):
    """
    n months > date
    """
    return t(n // 12, (n % 12) + 1, 1)


def to_day_count(d):
    """
    date > n days
    """
    return (d - type(d).min).days


def from_day_count(n, t=date):
    """
    n days > date
    """
    return t.min + timedelta(days=n)


def to_hour_count(d):
    """
    date > n hours
    """
    return (d - datetime.min).total_seconds() / (60 * 60)


def from_hour_count(n, t=datetime):
    """
    n hours > date
    """
    return t.min + timedelta(hours=n)


def to_minute_count(d):
    """
    date > n minutes
    """
    return (d - datetime.min).total_seconds() / 60


def from_minute_count(n, t=datetime):
    """
    n minutes > date
    """
    return t.min + timedelta(minutes=n)


def to_second_count(d):
    """
    date > n seconds
    """
    return (d - datetime.min).total_seconds()


def from_second_count(n, t=datetime):
    """
    n seconds > date
    """
    return t.min + timedelta(seconds=n)


def to_microsecond_count(d):
    """
    date > n microseconds
    """
    return (d - datetime.min).total_seconds() * 1000


def from_microsecond_count(n, t=datetime):
    """
    n microseconds > date
    """
    return t.min + timedelta(microseconds=n)