Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def format_date(date):
"""Format a date according to the current interval."""
if g.interval is Interval.YEAR:
return date.strftime("%Y")
if g.interval is Interval.QUARTER:
return "{}Q{}".format(date.year, (date.month - 1) // 3 + 1)
if g.interval is Interval.MONTH:
return date.strftime("%b %Y")
if g.interval is Interval.WEEK:
return date.strftime("%YW%W")
if g.interval is Interval.DAY:
return date.strftime("%Y-%m-%d")
return ""
def test_net_worth(app, example_ledger, snapshot):
with app.test_request_context():
app.preprocess_request()
g.conversion = "USD"
data = example_ledger.charts.net_worth(Interval.MONTH)
snapshot(data)
def test_interval_totals(app, small_example_ledger, snapshot):
with app.test_request_context(""):
g.conversion = None
data = small_example_ledger.charts.interval_totals(
Interval.MONTH, "Expenses"
)
snapshot(data)
("2016-12-31", Interval.MONTH, "2017-01-01"),
("2016-12-31", Interval.QUARTER, "2017-01-01"),
("2016-12-31", Interval.YEAR, "2017-01-01"),
("9999-12-31", Interval.QUARTER, "9999-12-31"),
("9999-12-31", Interval.YEAR, "9999-12-31"),
],
)
def test_get_next_interval(input_date_string, interval, expect):
get = get_next_interval(_to_date(input_date_string), interval)
assert get == _to_date(expect)
def get(string: str) -> enum.Enum:
"""Return the enum member for a string."""
try:
return Interval[string.upper()]
except KeyError:
return Interval.MONTH
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,
)
)
match = IS_RANGE_RE.match(string)
if match:
return (parse_date(match.group(1))[0], parse_date(match.group(2))[1])
match = YEAR_RE.match(string)
if match:
year = int(match.group(0))
start = datetime.date(year, 1, 1)
return start, get_next_interval(start, Interval.YEAR)
match = MONTH_RE.match(string)
if match:
year, month = map(int, match.group(1, 2))
start = datetime.date(year, month, 1)
return start, get_next_interval(start, Interval.MONTH)
match = DAY_RE.match(string)
if match:
year, month, day = map(int, match.group(1, 2, 3))
start = datetime.date(year, month, day)
return start, get_next_interval(start, Interval.DAY)
match = WEEK_RE.match(string)
if match:
year, week = map(int, match.group(1, 2))
date_str = "{}{}1".format(year, week)
first_week_day = datetime.datetime.strptime(date_str, "%Y%W%w").date()
return first_week_day, get_next_interval(first_week_day, Interval.WEEK)
match = QUARTER_RE.match(string)
if match:
interval: An interval.
Returns:
The start date of the next `interval` after `date`.
"""
# 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