From 7772ec679ff1e21f9678e47ee88790276c1562f5 Mon Sep 17 00:00:00 2001 From: Binh Nguyen Date: Sat, 12 Nov 2011 13:50:14 -0600 Subject: [PATCH] Renamed "verify" functions to "validate". Another idea for defining the fields in records would be to create a class method that would instantiate the individual fields at instance creation rather than during class definition. This would use less memory when there are no Record objects being used. Storing each Field after it's instantiated into a List, as well as a Dict would remove the necessity for counting the Field instantiation order, since the List would hold them in their proper order. --- pyaccuwage/__init__.py | 12 ++++++------ pyaccuwage/model.py | 9 ++++++--- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/pyaccuwage/__init__.py b/pyaccuwage/__init__.py index 749fffa..4fbf87f 100644 --- a/pyaccuwage/__init__.py +++ b/pyaccuwage/__init__.py @@ -42,7 +42,7 @@ def test_record_order(): record.TotalRecord(), record.FinalRecord(), ] - verify_record_order(records) + validate_record_order(records) def test_load(fp): @@ -85,7 +85,7 @@ def dumps(records): # THIS WAS IN CONTROLLER, BUT UNLESS WE # REALLY NEED A CONTROLLER CLASS, IT'S SIMPLER # TO JUST KEEP IT IN HERE. -def verify_required_records(records): +def validate_required_records(records): types = [rec.__class__.__name__ for rec in records] req_types = [] @@ -101,7 +101,7 @@ def verify_required_records(records): else: req_types.remove(req) -def verify_record_order(records): +def validate_record_order(records): import record from fields import ValidationError @@ -142,9 +142,9 @@ def verify_record_order(records): if len(filter(lambda x:isinstance(x, record.FinalRecord), records)) != 1: raise ValidationError("Incorrect number of FinalRecords") -def verify_records(records): - verify_required_records(records) - verify_record_order(records) +def validate_records(records): + validate_required_records(records) + validate_record_order(records) def test_unique_fields(): r1 = EmployeeWageRecord() diff --git a/pyaccuwage/model.py b/pyaccuwage/model.py index be21055..bb040b3 100644 --- a/pyaccuwage/model.py +++ b/pyaccuwage/model.py @@ -1,4 +1,4 @@ -from fields import Field +from fields import Field, ValidationError import copy import pdb @@ -51,8 +51,11 @@ class Model(object): custom_validator(f) def output(self): - return ''.join([self.record_identifier] + [field.get_data() for field in self.get_sorted_fields()]) - + result = ''.join([self.record_identifier] + [field.get_data() for field in self.get_sorted_fields()]) + if len(result) != 512: + raise ValidationError("Record result length not equal to 512 bytes") + return result + def read(self, fp): for field in self.get_sorted_fields(): field.read(fp)