Make Rdate into something typedload can handle

master
Salvo 'LtWorf' Tomaselli 2020-08-24 18:01:08 +07:00
parent c4bc74d612
commit 4d81858c8b
No known key found for this signature in database
GPG Key ID: B3A7CF0C801886CF
1 changed files with 18 additions and 18 deletions

@ -24,6 +24,7 @@ import datetime
import keyword
import re
from typing import Union, Set, Any, Callable
from dataclasses import dataclass
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:
return int(value)
if Rdate in guesses:
return Rdate(value)
return Rdate.create(value)
if float in guesses:
return float(value)
return value
@dataclass(frozen=True)
class Rdate:
'''Represents a date'''
year: int
month: int
day: int
def __init__(self, date):
'''date: A string representing a date'''
@property
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)
if not r:
raise ValueError(f'{date} is not a valid date')
@ -74,25 +88,11 @@ class Rdate:
year = int(r.group(1))
month = int(r.group(3))
day = int(r.group(5))
d = datetime.date(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__()
return Rdate(year, month, day)
def __str__(self):
return self.intdate.__str__()
def __eq__(self, other):
if not isinstance(other, Rdate):
return False
return self.intdate == other.intdate
def __ge__(self, other):
return self.intdate >= other.intdate