Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Args:
custom_entries: the Custom entries to parse budgets from.
Returns:
A dict of accounts to lists of budgets.
Example:
2015-04-09 custom "budget" Expenses:Books "monthly" 20.00 EUR
"""
budgets = defaultdict(list)
errors = []
interval_map = {
"daily": Interval.DAY,
"weekly": Interval.WEEK,
"monthly": Interval.MONTH,
"quarterly": Interval.QUARTER,
"yearly": Interval.YEAR,
}
for entry in custom_entries:
if entry.type == "budget":
try:
interval = interval_map.get(str(entry.values[1].value))
if not interval:
errors.append(
BudgetError(
entry.meta,
"Invalid interval for budget entry",
entry,
interval: An interval.
date: A date.
Returns:
A number, the number of days surrounding the given date in the
interval.
"""
if interval is Interval.DAY:
return 1
if interval is Interval.WEEK:
return 7
if interval is Interval.MONTH:
date = datetime.date(date.year, date.month, 1)
return (get_next_interval(date, Interval.MONTH) - date).days
if interval is Interval.QUARTER:
quarter = (date.month - 1) / 3 + 1
date = datetime.date(date.year, int(quarter) * 3 - 2, 1)
return (get_next_interval(date, Interval.QUARTER) - date).days
if interval is Interval.YEAR:
date = datetime.date(date.year, 1, 1)
return (get_next_interval(date, Interval.YEAR) - date).days
raise NotImplementedError
def get(string: str) -> enum.Enum:
"""Return the enum member for a string."""
try:
return Interval[string.upper()]
except KeyError:
return Interval.MONTH
# pylint: disable=too-many-return-statements
try:
if interval is Interval.YEAR:
return datetime.date(date.year + 1, 1, 1)
if interval is Interval.QUARTER:
for i in [4, 7, 10]:
if date.month < i:
return datetime.date(date.year, i, 1)
return datetime.date(date.year + 1, 1, 1)
if interval is Interval.MONTH:
month = (date.month % 12) + 1
year = date.year + (date.month + 1 > 12)
return datetime.date(year, month, 1)
if interval is Interval.WEEK:
return date + datetime.timedelta(7 - date.weekday())
if interval is Interval.DAY:
return date + datetime.timedelta(1)
except (ValueError, OverflowError):
return datetime.date.max
raise NotImplementedError
Args:
custom_entries: the Custom entries to parse budgets from.
Returns:
A dict of accounts to lists of budgets.
Example:
2015-04-09 custom "budget" Expenses:Books "monthly" 20.00 EUR
"""
budgets = defaultdict(list)
errors = []
interval_map = {
"daily": Interval.DAY,
"weekly": Interval.WEEK,
"monthly": Interval.MONTH,
"quarterly": Interval.QUARTER,
"yearly": Interval.YEAR,
}
for entry in custom_entries:
if entry.type == "budget":
try:
interval = interval_map.get(str(entry.values[1].value))
if not interval:
errors.append(
BudgetError(
entry.meta,
"Invalid interval for budget entry",
entry,
)