Skip to content

Commit

Permalink
feat: [add] suporte a criar instâncias usando strings, [add] define u…
Browse files Browse the repository at this point in the history
…ma data minima padrão e também a utiliza na função range
  • Loading branch information
kelsoncm committed Feb 20, 2024
1 parent d550379 commit 01361c5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
17 changes: 7 additions & 10 deletions competencias/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@


class Competencia(object):
MIN_DATE = None
MIN_DATE = date(1970, 1, 1)
TIMEZONE = timezone.utc
__instances = {}
MESES = {
1: "Janeiro",
2: "Fevereiro",
Expand All @@ -28,6 +27,8 @@ class Competencia(object):
12: "Dezembro",
}

__instances = {}

def __init__(self, year: int, month: int):
"""Never create a Competencia directly, allways use get_instance.
Expand Down Expand Up @@ -66,20 +67,16 @@ def validate(cls, value: Union[int, date, datetime, float, str]) -> date:
return cls._validate(date(int(value[0:4]), int(value[4:6]), 1), value.__class__)

@classmethod
def get_instance(cls, value: Union[date, datetime, int, float]) -> Competencia:
def get_instance(cls, value: Union[date, datetime, int, float, str]) -> Competencia:
_date = cls.validate(value)
if _date not in cls.__instances:
cls.__instances[_date] = cls(_date.year, _date.month)
return cls.__instances[_date]

@classmethod
def range(
cls,
start: Optional[Union[date, datetime, int, float]] = None,
end: Optional[Union[date, datetime, int, float]] = None,
) -> List[Competencia]:
dtstart = cls.validate(start or datetime.now())
until = cls.validate(end or datetime.now())
def range(cls, start: Optional[Competencia] = None, end: Optional[Competencia] = None) -> List[Competencia]:
dtstart = start.first_date if start is not None else cls.MIN_DATE
until = end.first_date if end is not None else date.today()
return [cls.get_instance(dt) for dt in rrule(MONTHLY, dtstart=dtstart, until=until)]

@classmethod
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys
from setuptools import setup, find_packages

version = "1.0.4"
version = "1.0.5"
if len(sys.argv) >= 3 and sys.argv[1] == "validate_tag":
if sys.argv[2] != version:
raise Exception(f"A versão TAG [{sys.argv[2]}] é diferente da versão no arquivo setup.py [{version}].")
Expand Down

0 comments on commit 01361c5

Please sign in to comment.