Make Rdate into something typedload can handle
This commit is contained in:
parent
c4bc74d612
commit
4d81858c8b
@ -24,6 +24,7 @@ import datetime
|
|||||||
import keyword
|
import keyword
|
||||||
import re
|
import re
|
||||||
from typing import Union, Set, Any, Callable
|
from typing import Union, Set, Any, Callable
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
|
||||||
RELATION_NAME_REGEXP = re.compile(r'^[_a-z][_a-z0-9]*$', re.IGNORECASE)
|
RELATION_NAME_REGEXP = re.compile(r'^[_a-z][_a-z0-9]*$', re.IGNORECASE)
|
||||||
@ -56,17 +57,30 @@ def cast(value: str, guesses: Set) -> CastValue:
|
|||||||
if int in guesses:
|
if int in guesses:
|
||||||
return int(value)
|
return int(value)
|
||||||
if Rdate in guesses:
|
if Rdate in guesses:
|
||||||
return Rdate(value)
|
return Rdate.create(value)
|
||||||
if float in guesses:
|
if float in guesses:
|
||||||
return float(value)
|
return float(value)
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
class Rdate:
|
class Rdate:
|
||||||
'''Represents a date'''
|
'''Represents a date'''
|
||||||
|
year: int
|
||||||
|
month: int
|
||||||
|
day: int
|
||||||
|
|
||||||
def __init__(self, date):
|
@property
|
||||||
'''date: A string representing a date'''
|
def intdate(self) -> datetime.date:
|
||||||
|
return datetime.date(self.year, self.month, self.day)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def weekday(self) -> int:
|
||||||
|
return self.intdate.weekday()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def create(date: str) -> 'Rdate':
|
||||||
|
'''date: A string representing a date YYYY-MM-DD'''
|
||||||
r = _date_regexp.match(date)
|
r = _date_regexp.match(date)
|
||||||
if not r:
|
if not r:
|
||||||
raise ValueError(f'{date} is not a valid date')
|
raise ValueError(f'{date} is not a valid date')
|
||||||
@ -74,25 +88,11 @@ class Rdate:
|
|||||||
year = int(r.group(1))
|
year = int(r.group(1))
|
||||||
month = int(r.group(3))
|
month = int(r.group(3))
|
||||||
day = int(r.group(5))
|
day = int(r.group(5))
|
||||||
d = datetime.date(year, month, day)
|
return Rdate(year, month, day)
|
||||||
|
|
||||||
self.intdate = d
|
|
||||||
self.day = d.day
|
|
||||||
self.month = d.month
|
|
||||||
self.weekday = d.weekday()
|
|
||||||
self.year = d.year
|
|
||||||
|
|
||||||
def __hash__(self):
|
|
||||||
return self.intdate.__hash__()
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.intdate.__str__()
|
return self.intdate.__str__()
|
||||||
|
|
||||||
def __eq__(self, other):
|
|
||||||
if not isinstance(other, Rdate):
|
|
||||||
return False
|
|
||||||
return self.intdate == other.intdate
|
|
||||||
|
|
||||||
def __ge__(self, other):
|
def __ge__(self, other):
|
||||||
return self.intdate >= other.intdate
|
return self.intdate >= other.intdate
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user