Erik Grinaker is sharing code with you
Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.
Don't show this againpython-chrono / chrono / utility.py
- Tag
- 0.3.0
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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | # -*- coding: utf-8 -*-
#
# python-chrono - a Python module for easy and convenient date/time handling
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
Various utility functions used by the other modules.
"""
from __future__ import absolute_import
from . import error
def cmp(a, b):
"""
Compares a to b, returns -1 if smaller, 0 if equal, and 1 if greater.
"""
if a > b:
return 1
elif a == b:
return 0
else:
return -1
def int_day(value):
"""
Converts a day value to an integer. If *value* is invalid
(non-numeric string, or invalid type), :exc:`chrono.error.DayError`
is raised.
"""
try:
return int(value)
except (TypeError, ValueError):
raise error.DayError("Invalid day value '{0}'".format(value))
def int_hour(value):
"""
Converts an hour value to an integer. If *value* is invalid
(non-numeric string, or invalid type), :exc:`chrono.error.HourError`
is raised.
"""
try:
return int(value)
except (TypeError, ValueError):
raise error.HourError("Invalid hour value '{0}'".format(value))
def int_minute(value):
"""
Converts a minute value to an integer. If *value* is invalid
(non-numeric string, or invalid type), :exc:`chrono.error.MinuteError`
is raised.
"""
try:
return int(value)
except (TypeError, ValueError):
raise error.MinuteError("Invalid minute value '{0}'".format(value))
def int_month(value):
"""
Converts a month value to an integer. If *value* is invalid
(non-numeric string, or invalid type), :exc:`chrono.error.MonthError`
is raised.
"""
try:
return int(value)
except (TypeError, ValueError):
raise error.MonthError("Invalid month value '{0}'".format(value))
def int_second(value):
"""
Converts a second value to an integer. If *value* is invalid
(non-numeric string, or invalid type), :exc:`chrono.error.SecondError`
is raised.
"""
try:
return int(value)
except (TypeError, ValueError):
raise error.SecondError("Invalid second value '{0}'".format(value))
def int_week(value):
"""
Converts a week value to an integer. If *value* is invalid
(non-numeric string, or invalid type), :exc:`chrono.error.WeekError`
is raised.
"""
try:
return int(value)
except (TypeError, ValueError):
raise error.WeekError("Invalid week value '{0}'".format(value))
def int_year(value):
"""
Converts a year value to an integer. If *value* is invalid
(non-numeric string, or invalid type), :exc:`chrono.error.YearError`
is raised.
"""
try:
return int(value)
except (TypeError, ValueError):
raise error.YearError("Invalid year value '{0}'".format(value))
def integer(value):
"""
Converts *value* to an integer. If *value* is a sequence or
dictionary, members will be recursively converted to integers.
Raises :exc:`TypeError` if *value* (or members) isn't a string or
integer, or :exc:`ValueError` if *value* couldn't be converted.
"""
if value is None:
return None
elif type(value) == list:
value = [integer(v) for v in value]
elif type(value) == tuple:
value = tuple([integer(v) for v in value])
elif type(value) == dict:
for k, v in value.items():
value[k] = integer(v)
else:
value = int(value)
return value
|