Hi, I have been working on a little project to transform excel docs
(actually them saved as csv files) into SQLAlchemy objects.
Of course this is tailored for my own database which I need to import
but I have been splitting it into api more and more and eventually
plan to release it as some configurable importer for SA. But first I
need to clean up the code!
currently I have this type of configuration.
_excel_to_field = {
'First Name' : 'first_name',
'Last Name' : 'last_name',
'Address' : 'address',
'City' : 'city',
'State' : 'state',
'Zip' : 'zip_code',
}
and the bulk of the system is run as
for k,v in row:
if not k in [NoModelField,NoCSVField]:
if v:
log.debug('Adding Field %s:%s' % (k,v))
else:
log.warning('Adding Empty Field for %s at line %i'
% (k,index))
setattr(contact,k,v)
else:
log.warning("Field not accounted for (%s,%s)" % (k,v))
so currently that for is very big for (see below)
So in order to make code a lot more readable I was thinking I could
use the syntax SA uses for filter and such so I can write the
_excel_to_field as
_excel_to_field = {
'First Name' : 'Contact.first_name',
'Last Name' : 'Contact.last_name',
'Address' : 'Location.address',
'City' : 'Location.city',
'State' : 'Location.state',
'Zip' : 'Location.zip_code',
}
please note the Location obj is a fabrication of this email
but how can I replace the setatrr call? I guess I could parse things
around but I assume there is some kind of api in SA to accomplish
this.
My plan is to add two more parameter to this dict, #1 a typeConverter
#2 a headerReader, I'll explain those in due time. I'm working on
transforming the forloop into a more elegant forloop with a callback.
So the final plan is to have something like this
_excel_to_field = {
'First Name' : 'Contact.first_name',string,
'Last Name' : 'Contact.last_name','string',
'Address' : 'Location.address',address,
'City' : 'Location.city',string
'State' : 'Location.state',state
'Zip' : 'Location.zip_code',int
}
this third callback will be either a build-in, I have several already
for date,float,us_money,etc also more complex ones like always create
(adds a new record all the time) and query_create (looks for one if
that fails create it) fields but you can code your own as they are
simply python functions with a common interface. Ideally this will
grow into a number big number of defaults that will live inside the
package. So you will never have to code it yourself just look for
them, but I want to do this string-python formatting First.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---