initial checkin

This commit is contained in:
Binh 2011-03-26 14:56:00 -05:00
commit e12557db2d
5 changed files with 105 additions and 0 deletions

BIN
docs/10efw2.pdf Normal file

Binary file not shown.

1
docs/link Normal file
View file

@ -0,0 +1 @@
http://www.ssa.gov/employer/efw/10efw2.pdf

4
docs/plans Normal file
View file

@ -0,0 +1,4 @@
the goal of this library is to take data from our records
and convert them into accuwage format for e-file to the IRS.

57
fields.py Normal file
View file

@ -0,0 +1,57 @@
class ValidationError(Exception):
pass
class Field(object):
creation_counter = 0
def __init__(self, name=None, max_length=0, required=True):
self.name = name
self._value = None
self.max_length = max_length
self.required = required
self.creation_counter = Field.creation_counter
Field.creation_counter += 1
def validate(self):
raise NotImplemented()
def get_data(self):
raise NotImplemented()
def __setvalue(self, value):
self._value = value
def __getvalue(self):
return self._value
value = property(__getvalue, __setvalue)
def __repr__(self):
return self.name
class TextField(Field):
def validate(self):
if self.value == None and self.required:
raise ValidationError("value required")
if len(self.value) > self.max_length:
raise ValidationError("value is too long")
def get_data(self):
return (self.value or "").encode('ascii').ljust(self.max_length)
class StateField(TextField):
def __init__(self, name=None, required=True):
return super(StateField, self).__init__(name=name, max_length=2, required=required)
class MoneyField(Field):
def validate(self):
if self.value == None and self.required:
raise ValidationError("value required")
if len(str(int((self.value or 0)*100))) > self.max_length:
raise ValidationError("value is too long")
def get_data(self):
return str(int((self.value or 0)*100)).encode('ascii').zfill(self.max_length)

43
model.py Normal file
View file

@ -0,0 +1,43 @@
from fields import Field, TextField, MoneyField, StateField
class Model(object):
def __init__(self):
for (key, value) in self.__class__.__dict__.items():
if isinstance(value, Field):
field = getattr(self, key)
if not field.name:
setattr(field, 'name', key)
def __setattr__(self, key, value):
if hasattr(self, key) and isinstance(getattr(self, key), Field):
getattr(self, key).value = value
else:
self.__dict__[key] = value
def get_fields(self):
fields = []
for key in self.__class__.__dict__.keys():
attr = getattr(self, key)
if isinstance(attr, Field):
fields.append(attr)
return fields
def get_sorted_fields(self):
fields = self.get_fields()
fields.sort(key=lambda x:x.creation_counter)
return fields
def validate(self):
for f in self.get_fields():
f.validate()
def output(self):
return ''.join([field.get_data() for field in self.get_sorted_fields()])
class TestModel(Model):
field_a = TextField(max_length=20)
field_b = MoneyField(max_length=10)
state = StateField()