Skip to content

Commit

Permalink
Remove calendar for now, replacing with different index in future
Browse files Browse the repository at this point in the history
  • Loading branch information
kirkedev committed Jul 20, 2019
1 parent e07e539 commit f1da420
Show file tree
Hide file tree
Showing 8 changed files with 871 additions and 855 deletions.
1,546 changes: 858 additions & 688 deletions cash_prices.ipynb

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions mpr/api.py
Expand Up @@ -74,7 +74,7 @@ async def fetch(report: Report, start: date, end=date.today()) -> Iterator[Attri
return parse_elements(elements)


def parse_elements(elements: Iterator[ParsedElement], max_depth=4) -> Iterator[Attributes]:
def parse_elements(elements: Iterator[ParsedElement], min_depth=1, max_depth=4) -> Iterator[Attributes]:
"""
Parses a USDA report by saving metadata from parent elements to a dictionary while traversing down the tree.
When at the maximum depth, yield all collected metadata with each child element's attributes.
Expand All @@ -86,28 +86,28 @@ def parse_elements(elements: Iterator[ParsedElement], max_depth=4) -> Iterator[A
<report label>
<record ...attributes/>
Usually all we care about is the report date (depth=2); the report label (depth=3), for finding sections;
and the record attributes (depth=4), which contains the data.
Usually all we care about is the report date (depth=2); the report section label (depth=3);
and the record data attributes (depth=4).
"""
depth = 0
metadata: Attributes = dict()

for event, element in elements:
if event == 'start':
if 1 <= depth < max_depth:
if min_depth <= depth < max_depth:
# Parsing a parent element: merge its properties into the metadata
metadata.update(element.items())

elif depth == max_depth:
# Parsing a child element: combine its properties with the metadata and yield
# Parsing a child element: generate a dict combining metadata and child attributes
yield dict(metadata.items() | element.items())

depth += 1

if event == 'end':
depth -= 1

if depth == 2:
# Finished parsing one day's data: clear the metadata and element tree
if depth == min_depth:
# clear the metadata and element tree after each report section
element.clear()
metadata.clear()
89 changes: 0 additions & 89 deletions mpr/calendar.py

This file was deleted.

7 changes: 3 additions & 4 deletions mpr/cash_index/__init__.py
@@ -1,10 +1,9 @@
from datetime import date
from datetime import timedelta
from functools import singledispatch
from pandas import DataFrame

from ..calendar import recent_report_dates
from ..slaughter.api import fetch_slaughter

from .report import cash_index_report


Expand All @@ -16,6 +15,6 @@ async def get_cash_prices(start: date, end=date.today()) -> DataFrame:

@get_cash_prices.register(int)
async def get_recent_cash_prices(n: int) -> DataFrame:
first, *_, last = recent_report_dates(n + 3)
cash_prices = await get_cash_prices(first, last)
today = date.today()
cash_prices = await get_cash_prices(today - timedelta(days=n + 10), today)
return cash_prices.tail(n)
6 changes: 3 additions & 3 deletions mpr/cutout_index/__init__.py
@@ -1,8 +1,8 @@
from datetime import date
from datetime import timedelta
from functools import singledispatch
from pandas import DataFrame

from ..calendar import recent_report_dates
from ..cutout.api import pk602

from .report import cutout_report
Expand All @@ -16,6 +16,6 @@ async def get_cutout_index(start: date, end=date.today()) -> DataFrame:

@get_cutout_index.register(int)
async def get_recent_cash_prices(n: int) -> DataFrame:
first, *_, last = recent_report_dates(n + 8)
cutout = await get_cutout_index(first, last)
today = date.today()
cutout = await get_cutout_index(today - timedelta(days=n + 12), today)
return cutout.tail(n)
17 changes: 0 additions & 17 deletions mpr/reports.py
@@ -1,20 +1,11 @@
from typing import Union
from typing import Tuple
from typing import Iterator
from typing import List
from enum import Enum
from datetime import date

import pandas as pd
from pandas import Series
from pandas import DataFrame

from .calendar import DateInterval
from .calendar import date_diff
from .calendar import recent_report_dates
from .calendar import report_date_range
from .calendar import report_date_intervals

pd.options.display.float_format = '{:,.2f}'.format


Expand All @@ -27,14 +18,6 @@ class Report(Enum):
CUTOUT_AFTERNOON = 'lm_pk602'


def request_range(start: date, end: date, dates: Iterator[date]) -> List[DateInterval]:
return list(report_date_intervals(date_diff(dates, report_date_range(start, end))))


def request_recent_reports(days: int) -> List[DateInterval]:
return list(report_date_intervals(date_diff([], recent_report_dates(days))))


def compute_change(values: Series) -> Series:
return values - values.shift(1)

Expand Down
46 changes: 0 additions & 46 deletions test/reports/test_date_range.py

This file was deleted.

1 change: 0 additions & 1 deletion test/resources/reports/report_dates.xml

This file was deleted.

0 comments on commit f1da420

Please sign in to comment.