Skip to content

Commit

Permalink
Invoices no longer assign to self.data during filtering step
Browse files Browse the repository at this point in the history
Instead, all invoice classes will now assign to `self.export_data`
This removes ambiguity on whether the invoices will modify their
internal dataframe and the need to call `copy()` on the processed
dataframe
  • Loading branch information
QuanMPhm committed Dec 11, 2024
1 parent c81622d commit 23b3a8a
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 23 deletions.
6 changes: 3 additions & 3 deletions process_report/invoices/NERC_total_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ def output_s3_archive_key(self):
return f"Invoices/{self.invoice_month}/Archive/NERC-{self.invoice_month}-Total-Invoice {util.get_iso8601_time()}.csv"

def _prepare_export(self):
self.data = self.data[
self.export_data = self.data[
self.data[invoice.IS_BILLABLE_FIELD] & ~self.data[invoice.MISSING_PI_FIELD]
]
self.data = self.data[
self.data[invoice.INSTITUTION_FIELD].isin(self.INCLUDED_INSTITUTIONS)
self.export_data = self.export_data[
self.export_data[invoice.INSTITUTION_FIELD].isin(self.INCLUDED_INSTITUTIONS)
].copy()
2 changes: 1 addition & 1 deletion process_report/invoices/billable_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class BillableInvoice(invoice.Invoice):
]

def _prepare_export(self):
self.data = self.data[
self.export_data = self.data[
self.data[invoice.IS_BILLABLE_FIELD] & ~self.data[invoice.MISSING_PI_FIELD]
]
self.updated_old_pi_df = self.updated_old_pi_df.astype(
Expand Down
8 changes: 4 additions & 4 deletions process_report/invoices/bu_internal_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ class BUInternalInvoice(invoice.Invoice):
exported_columns_map = {invoice.PI_BALANCE_FIELD: "Balance"}

def _prepare_export(self):
self.data = self.data[
self.export_data = self.data[
self.data[invoice.IS_BILLABLE_FIELD] & ~self.data[invoice.MISSING_PI_FIELD]
]
self.data = self.data[
self.data[invoice.INSTITUTION_FIELD] == "Boston University"
self.export_data = self.export_data[
self.export_data[invoice.INSTITUTION_FIELD] == "Boston University"
]
self.data = self._sum_project_allocations(self.data)
self.export_data = self._sum_project_allocations(self.export_data)

def _sum_project_allocations(self, dataframe):
"""A project may have multiple allocations, and therefore multiple rows
Expand Down
7 changes: 4 additions & 3 deletions process_report/invoices/invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class Invoice:
name: str
invoice_month: str
data: pandas.DataFrame
export_data = None

def process(self):
self._prepare()
Expand Down Expand Up @@ -93,13 +94,13 @@ def _prepare_export(self):

def _filter_columns(self):
"""Filters and renames columns before exporting"""
return self.data.copy()[self.export_columns_list].rename(
self.export_data = self.export_data[self.export_columns_list].rename(
columns=self.exported_columns_map
)

def export(self):
export_data = self._filter_columns()
export_data.to_csv(self.output_path, index=False)
self._filter_columns()
self.export_data.to_csv(self.output_path, index=False)

def export_s3(self, s3_bucket):
s3_bucket.upload_file(self.output_path, self.output_s3_key)
Expand Down
2 changes: 1 addition & 1 deletion process_report/invoices/lenovo_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ class LenovoInvoice(invoice.Invoice):
exported_columns_map = {invoice.SU_HOURS_FIELD: "SU Hours"}

def _prepare_export(self):
self.data = self.data[
self.export_data = self.data[
self.data[invoice.SU_TYPE_FIELD].isin(self.LENOVO_SU_TYPES)
]
2 changes: 1 addition & 1 deletion process_report/invoices/nonbillable_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ class NonbillableInvoice(invoice.Invoice):
]

def _prepare_export(self):
self.data = self.data[~self.data[invoice.IS_BILLABLE_FIELD]]
self.export_data = self.data[~self.data[invoice.IS_BILLABLE_FIELD]]
4 changes: 2 additions & 2 deletions process_report/invoices/pi_specific_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ class PIInvoice(invoice.Invoice):
]

def _prepare(self):
self.data = self.data[
self.export_data = self.data[
self.data[invoice.IS_BILLABLE_FIELD] & ~self.data[invoice.MISSING_PI_FIELD]
]
self.pi_list = self.data[invoice.PI_FIELD].unique()
self.pi_list = self.export_data[invoice.PI_FIELD].unique()

def export(self):
def _export_pi_invoice(pi):
Expand Down
12 changes: 6 additions & 6 deletions process_report/process_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,12 @@ def main():
lenovo_inv = lenovo_invoice.LenovoInvoice(
name=args.Lenovo_file,
invoice_month=invoice_month,
data=processed_data.copy(),
data=processed_data,
)
nonbillable_inv = nonbillable_invoice.NonbillableInvoice(
name=args.nonbillable_file,
invoice_month=invoice_month,
data=processed_data.copy(),
data=processed_data,
nonbillable_pis=pi,
nonbillable_projects=projects,
)
Expand All @@ -271,25 +271,25 @@ def main():
billable_inv = billable_invoice.BillableInvoice(
name=args.output_file,
invoice_month=invoice_month,
data=processed_data.copy(),
data=processed_data,
old_pi_filepath=old_pi_file,
updated_old_pi_df=new_pi_credit_proc.updated_old_pi_df,
)

nerc_total_inv = NERC_total_invoice.NERCTotalInvoice(
name=args.NERC_total_invoice_file,
invoice_month=invoice_month,
data=processed_data.copy(),
data=processed_data,
)

bu_internal_inv = bu_internal_invoice.BUInternalInvoice(
name=args.BU_invoice_file,
invoice_month=invoice_month,
data=processed_data.copy(),
data=processed_data,
)

pi_inv = pi_specific_invoice.PIInvoice(
name=args.output_folder, invoice_month=invoice_month, data=processed_data.copy()
name=args.output_folder, invoice_month=invoice_month, data=processed_data
)

util.process_and_export_invoices(
Expand Down
6 changes: 4 additions & 2 deletions process_report/tests/unit_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1036,9 +1036,11 @@ class TestBaseInvoice(TestCase):
def test_filter_exported_columns(self):
test_invoice = pandas.DataFrame(columns=["C1", "C2", "C3", "C4", "C5"])
answer_invoice = pandas.DataFrame(columns=["C1", "C3R", "C5R"])
inv = test_utils.new_base_invoice(data=test_invoice)
inv = test_utils.new_base_invoice()
inv.export_data = test_invoice
inv.export_columns_list = ["C1", "C3", "C5"]
inv.exported_columns_map = {"C3": "C3R", "C5": "C5R"}
result_invoice = inv._filter_columns()

inv._filter_columns()
result_invoice = inv.export_data
self.assertTrue(result_invoice.equals(answer_invoice))

0 comments on commit 23b3a8a

Please sign in to comment.