Skip to content

Commit

Permalink
Replaced entity SORT arrays by pre-sorting transform arrays; added tr…
Browse files Browse the repository at this point in the history
…anslator.util.transform_dict() to generate entity dictionaries.
  • Loading branch information
sdj0 committed Feb 22, 2018
1 parent 6cafa1f commit ea268fd
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 337 deletions.
92 changes: 31 additions & 61 deletions entities/end_of_payer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
and support functions for conversion into different formats.
"""
from translator.util import rjust_zero
from translator.util import xform_entity, fire_entity
from translator.util import xform_entity, fire_entity, transform_dict

"""
_END_OF_PAYER_TRANSFORMS
Expand All @@ -17,68 +17,36 @@
WARNING
-------
any edits to the keys or key names must be reflected in the SORT
array.
array.
"""
_END_OF_PAYER_TRANSFORMS = {
"record_type":
("C", 1, "\x00", lambda x: (x)),
"number_of_payees":
("", 8, "0", lambda x: (x)),
"blank_1":
("", 6, "\x00", lambda x: (x)),
"payment_amount_1":
(18*"0", 18, "0", lambda x: rjust_zero(x, 18)),
"payment_amount_2":
(18*"0", 18, "0", lambda x: rjust_zero(x, 18)),
"payment_amount_3":
(18*"0", 18, "0", lambda x: rjust_zero(x, 18)),
"payment_amount_4":
(18*"0", 18, "0", lambda x: rjust_zero(x, 18)),
"payment_amount_5":
(18*"0", 18, "0", lambda x: rjust_zero(x, 18)),
"payment_amount_6":
(18*"0", 18, "0", lambda x: rjust_zero(x, 18)),
"payment_amount_7":
(18*"0", 18, "0", lambda x: rjust_zero(x, 18)),
"payment_amount_8":
(18*"0", 18, "0", lambda x: rjust_zero(x, 18)),
"payment_amount_9":
(18*"0", 18, "0", lambda x: rjust_zero(x, 18)),
"payment_amount_A":
(18*"0", 18, "0", lambda x: rjust_zero(x, 18)),
"payment_amount_B":
(18*"0", 18, "0", lambda x: rjust_zero(x, 18)),
"payment_amount_C":
(18*"0", 18, "0", lambda x: rjust_zero(x, 18)),
"payment_amount_D":
(18*"0", 18, "0", lambda x: rjust_zero(x, 18)),
"payment_amount_E":
(18*"0", 18, "0", lambda x: rjust_zero(x, 18)),
"payment_amount_F":
(18*"0", 18, "0", lambda x: rjust_zero(x, 18)),
"payment_amount_G":
(18*"0", 18, "0", lambda x: rjust_zero(x, 18)),
"blank_2":
("", 196, "\x00", lambda x: (x)),
"record_sequence_number":
("", 8, "0", lambda x: (x)),
"blank_3":
("", 241, "\x00", lambda x: (x)),
"blank_4":
("", 2, "\x00", lambda x: (x))
}

_END_OF_PAYER_SORT = [
"record_type", "blank_1", "number_of_payees",
"payment_amount_1", "payment_amount_2", "payment_amount_3",
"payment_amount_4", "payment_amount_5", "payment_amount_6",
"payment_amount_7", "payment_amount_8", "payment_amount_9",
"payment_amount_A", "payment_amount_B", "payment_amount_C",
"payment_amount_D", "payment_amount_E", "payment_amount_F",
"payment_amount_G", "blank_2", "record_sequence_number", "blank_3",
"blank_4"
_END_OF_PAYER_TRANSFORMS_ARR = [
("record_type", "C", 1, "\x00", lambda x: (x)),
("number_of_payees", "", 8, "0", lambda x: (x)),
("blank_1", "", 6, "\x00", lambda x: (x)),
("payment_amount_1", 18*"0", 18, "0", lambda x: rjust_zero(x, 18)),
("payment_amount_2", 18*"0", 18, "0", lambda x: rjust_zero(x, 18)),
("payment_amount_3", 18*"0", 18, "0", lambda x: rjust_zero(x, 18)),
("payment_amount_4", 18*"0", 18, "0", lambda x: rjust_zero(x, 18)),
("payment_amount_5", 18*"0", 18, "0", lambda x: rjust_zero(x, 18)),
("payment_amount_6", 18*"0", 18, "0", lambda x: rjust_zero(x, 18)),
("payment_amount_7", 18*"0", 18, "0", lambda x: rjust_zero(x, 18)),
("payment_amount_8", 18*"0", 18, "0", lambda x: rjust_zero(x, 18)),
("payment_amount_9", 18*"0", 18, "0", lambda x: rjust_zero(x, 18)),
("payment_amount_A", 18*"0", 18, "0", lambda x: rjust_zero(x, 18)),
("payment_amount_B", 18*"0", 18, "0", lambda x: rjust_zero(x, 18)),
("payment_amount_C", 18*"0", 18, "0", lambda x: rjust_zero(x, 18)),
("payment_amount_D", 18*"0", 18, "0", lambda x: rjust_zero(x, 18)),
("payment_amount_E", 18*"0", 18, "0", lambda x: rjust_zero(x, 18)),
("payment_amount_F", 18*"0", 18, "0", lambda x: rjust_zero(x, 18)),
("payment_amount_G", 18*"0", 18, "0", lambda x: rjust_zero(x, 18)),
("blank_2", "", 196, "\x00", lambda x: (x)),
("record_sequence_number", "", 8, "0", lambda x: (x)),
("blank_3", "", 241, "\x00", lambda x: (x)),
("blank_4", "", 2, "\x00", lambda x: (x))
]

_END_OF_PAYER_TRANSFORMS = transform_dict(_END_OF_PAYER_TRANSFORMS_ARR)

def xform(data):
"""
Applies transformation functions definted in _END_OF_PAYER_TRANSFORMS to
Expand Down Expand Up @@ -115,4 +83,6 @@ def fire(data):
str
String formatted to meet IRS Publication 1220
"""
return fire_entity(_END_OF_PAYER_TRANSFORMS, _END_OF_PAYER_SORT, data)
return fire_entity(_END_OF_PAYER_TRANSFORMS,
[field[0] for field in _END_OF_PAYER_TRANSFORMS_ARR],
data)
43 changes: 16 additions & 27 deletions entities/end_of_transmission.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
functions and support functions for conversion into different formats.
"""
from translator.util import rjust_zero
from translator.util import xform_entity, fire_entity
from translator.util import xform_entity, fire_entity, transform_dict

"""
_END_OF_TRANSMISSION_TRANSFORMS
Expand All @@ -19,33 +19,21 @@
any edits to the keys or key names must be reflected in the SORT
array.
"""
_END_OF_TRANSMISSION_TRANSFORMS = {
"record_type":
("F", 1, "\x00", lambda x: (x)),
"number_of_a_records":
("00000000", 8, "0", lambda x: rjust_zero(x, 8)),
"zeros":
(21*"0", 21, "0", lambda x: (x)),
"blank_1":
("", 19, "\x00", lambda x: (x)),
"total_number_of_payees":
("00000000", 8, "0", lambda x: rjust_zero(x, 8)),
"blank_2":
("", 442, "\x00", lambda x: (x)),
"record_sequence_number":
("", 8, "0", lambda x: (x)),
"blank_3":
("", 241, "\x00", lambda x: (x)),
"blank_4":
("", 2, "\x00", lambda x: (x))
}

_END_OF_TRANSMISSION_SORT = [
"record_type", "number_of_a_records", "zeros", "blank_1",
"total_number_of_payees", "blank_2", "record_sequence_number", "blank_3",
"blank_4"
_END_OF_TRANSMISSION_TRANSFORMS_ARR = [
("record_type", "F", 1, "\x00", lambda x: (x)),
("number_of_a_records", "00000000", 8, "0", lambda x: rjust_zero(x, 8)),
("zeros", 21*"0", 21, "0", lambda x: (x)),
("blank_1", "", 19, "\x00", lambda x: (x)),
("total_number_of_payees", "00000000", 8, "0", lambda x: rjust_zero(x, 8)),
("blank_2", "", 442, "\x00", lambda x: (x)),
("record_sequence_number", "", 8, "0", lambda x: (x)),
("blank_3", "", 241, "\x00", lambda x: (x)),
("blank_4", "", 2, "\x00", lambda x: (x))
]

_END_OF_TRANSMISSION_TRANSFORMS = \
transform_dict(_END_OF_TRANSMISSION_TRANSFORMS_ARR)

def xform(data):
"""
Applies transformation functions definted in _END_OF_TRANSMISSION_TRANSFORMS
Expand Down Expand Up @@ -84,4 +72,5 @@ def fire(data):
"""
return fire_entity(
_END_OF_TRANSMISSION_TRANSFORMS,
_END_OF_TRANSMISSION_SORT, data)
[field[0] for field in _END_OF_TRANSMISSION_TRANSFORMS_ARR],
data)
155 changes: 55 additions & 100 deletions entities/payees.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
an individual payee)
"""
from translator.util import digits_only, uppercase, rjust_zero
from translator.util import xform_entity, fire_entity
from translator.util import xform_entity, fire_entity, transform_dict
"""
_PAYEE_TRANSFORMS
-----------------------
Expand All @@ -21,103 +21,57 @@
any edits to the keys or key names must be reflected in the SORT
array.
"""
_PAYEE_TRANSFORMS = {
"record_type":
("B", 1, "\x00", lambda x: (x)),
"payment_year":
("", 4, "\x00", lambda x: (x)),
"corrected_return_indicator":
("", 1, "\x00", lambda x: uppercase(x)),
"payees_name_control":
("", 4, "\x00", lambda x: uppercase(x)),
"type_of_tin":
("1", 1, "\x00", lambda x: (x)),
"payees_tin":
("000000000", 9, "\x00", lambda x: digits_only(x)),
"payers_account_number_for_payee":
("", 20, "\x00", lambda x: (x)),
"payers_office_code":
("", 4, "\x00", lambda x: (x)),
"blank_1":
("", 10, "\x00", lambda x: (x)),
"payment_amount_1":
("000000000000", 12, "\x00", lambda x: rjust_zero(x, 12)),
"payment_amount_2":
("000000000000", 12, "\x00", lambda x: rjust_zero(x, 12)),
"payment_amount_3":
("000000000000", 12, "\x00", lambda x: rjust_zero(x, 12)),
"payment_amount_4":
("000000000000", 12, "\x00", lambda x: rjust_zero(x, 12)),
"payment_amount_5":
("000000000000", 12, "\x00", lambda x: rjust_zero(x, 12)),
"payment_amount_6":
("000000000000", 12, "\x00", lambda x: rjust_zero(x, 12)),
"payment_amount_7":
("000000000000", 12, "\x00", lambda x: rjust_zero(x, 12)),
"payment_amount_8":
("000000000000", 12, "\x00", lambda x: rjust_zero(x, 12)),
"payment_amount_9":
("000000000000", 12, "\x00", lambda x: rjust_zero(x, 12)),
"payment_amount_A":
("000000000000", 12, "\x00", lambda x: rjust_zero(x, 12)),
"payment_amount_B":
("000000000000", 12, "\x00", lambda x: rjust_zero(x, 12)),
"payment_amount_C":
("000000000000", 12, "\x00", lambda x: rjust_zero(x, 12)),
"payment_amount_D":
("000000000000", 12, "\x00", lambda x: rjust_zero(x, 12)),
"payment_amount_E":
("000000000000", 12, "\x00", lambda x: rjust_zero(x, 12)),
"payment_amount_F":
("000000000000", 12, "\x00", lambda x: rjust_zero(x, 12)),
"payment_amount_G":
("000000000000", 12, "\x00", lambda x: rjust_zero(x, 12)),
"foreign_country_indicator":
("", 1, "\x00", lambda x: (x)),
"first_payee_name_line":
("", 40, "\x00", lambda x: uppercase(x)),
"second_payee_name_line":
("", 40, "\x00", lambda x: uppercase(x)),
"blank_2":
("", 40, "\x00", lambda x: (x)),
"payee_mailing_address":
("", 40, "\x00", lambda x: (x)),
"blank_3":
("", 40, "\x00", lambda x: (x)),
"payee_city":
("", 40, "\x00", lambda x: (x)),
"payee_state":
("", 2, "\x00", lambda x: (x)),
"payee_zip_code":
("", 9, "\x00", lambda x: (x)),
"blank_4":
("", 1, "\x00", lambda x: (x)),
"record_sequence_number":
("00000003", 8, "\x00", lambda x: rjust_zero(x, 8)),
"blank_5":
("", 36, "\x00", lambda x: (x)),
"second_tin_notice":
("", 1, "\x00", lambda x: (x)),
"blank_6":
("", 2, "\x00", lambda x: (x)),
"direct_sales_indicator":
("", 1, "\x00", lambda x: (x)),
"fatca_filing_requirement_indicator":
("", 1, "\x00", lambda x: (x)),
"blank_7":
("", 114, "\x00", lambda x: (x)),
"special_data_entries":
("", 60, "\x00", lambda x: (x)),
"state_income_tax_withheld":
("", 12, "\x00", lambda x: (x)),
"local_income_tax_withheld":
("", 12, "\x00", lambda x: (x)),
"combined_federal_state_code":
("", 2, "\x00", lambda x: (x)),
"blank_8":
("", 2, "\x00", lambda x: (x))
}
_PAYEE_TRANSFORMS_ARR = [
("record_type","B",1,"\x00",lambda x: (x)),
("payment_year","",4,"\x00",lambda x: (x)),
("corrected_return_indicator","",1,"\x00",lambda x: uppercase(x)),
("payees_name_control","",4,"\x00",lambda x: uppercase(x)),
("type_of_tin","1",1,"\x00",lambda x: (x)),
("payees_tin","000000000",9,"\x00",lambda x: digits_only(x)),
("payers_account_number_for_payee","",20,"\x00",lambda x: (x)),
("payers_office_code","",4,"\x00",lambda x: (x)),
("blank_1","",10,"\x00",lambda x: (x)),
("payment_amount_1","000000000000",12,"\x00",lambda x: rjust_zero(x,12)),
("payment_amount_2","000000000000",12,"\x00",lambda x: rjust_zero(x,12)),
("payment_amount_3","000000000000",12,"\x00",lambda x: rjust_zero(x,12)),
("payment_amount_4","000000000000",12,"\x00",lambda x: rjust_zero(x,12)),
("payment_amount_5","000000000000",12,"\x00",lambda x: rjust_zero(x,12)),
("payment_amount_6","000000000000",12,"\x00",lambda x: rjust_zero(x,12)),
("payment_amount_7","000000000000",12,"\x00",lambda x: rjust_zero(x,12)),
("payment_amount_8","000000000000",12,"\x00",lambda x: rjust_zero(x,12)),
("payment_amount_9","000000000000",12,"\x00",lambda x: rjust_zero(x,12)),
("payment_amount_A","000000000000",12,"\x00",lambda x: rjust_zero(x,12)),
("payment_amount_B","000000000000",12,"\x00",lambda x: rjust_zero(x,12)),
("payment_amount_C","000000000000",12,"\x00",lambda x: rjust_zero(x,12)),
("payment_amount_D","000000000000",12,"\x00",lambda x: rjust_zero(x,12)),
("payment_amount_E","000000000000",12,"\x00",lambda x: rjust_zero(x,12)),
("payment_amount_F","000000000000",12,"\x00",lambda x: rjust_zero(x,12)),
("payment_amount_G","000000000000",12,"\x00",lambda x: rjust_zero(x,12)),
("foreign_country_indicator","",1,"\x00",lambda x: (x)),
("first_payee_name_line","",40,"\x00",lambda x: uppercase(x)),
("second_payee_name_line","",40,"\x00",lambda x: uppercase(x)),
("blank_2","",40,"\x00",lambda x: (x)),
("payee_mailing_address","",40,"\x00",lambda x: (x)),
("blank_3","",40,"\x00",lambda x: (x)),
("payee_city","",40,"\x00",lambda x: (x)),
("payee_state","",2,"\x00",lambda x: (x)),
("payee_zip_code","",9,"\x00",lambda x: (x)),
("blank_4","",1,"\x00",lambda x: (x)),
("record_sequence_number","00000003",8,"\x00",lambda x: rjust_zero(x,8)),
("blank_5","",36,"\x00",lambda x: (x)),
("second_tin_notice","",1,"\x00",lambda x: (x)),
("blank_6","",2,"\x00",lambda x: (x)),
("direct_sales_indicator","",1,"\x00",lambda x: (x)),
("fatca_filing_requirement_indicator","",1,"\x00",lambda x: (x)),
("blank_7","",114,"\x00",lambda x: (x)),
("special_data_entries","",60,"\x00",lambda x: (x)),
("state_income_tax_withheld","",12,"\x00",lambda x: (x)),
("local_income_tax_withheld","",12,"\x00",lambda x: (x)),
("combined_federal_state_code","",2,"\x00",lambda x: (x)),
("blank_8","",2,"\x00",lambda x: (x))
]

_PAYEE_TRANSFORMS = transform_dict(_PAYEE_TRANSFORMS_ARR)
_PAYEE_SORT = [
"record_type", "payment_year", "corrected_return_indicator",
"payees_name_control", "type_of_tin", "payees_tin",
Expand Down Expand Up @@ -176,7 +130,8 @@ def fire(data):
"""
payees_string = ""
for payee in data:
payees_string += fire_entity(_PAYEE_TRANSFORMS, _PAYEE_SORT, payee)
payees_string += fire_entity(
_PAYEE_TRANSFORMS,
[field[0] for field in _PAYEE_TRANSFORMS_ARR],
payee)
return payees_string


Loading

0 comments on commit ea268fd

Please sign in to comment.