On 7/19/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>
> On Jul 19, 3:38 am, "Gaetan de Menten" <[EMAIL PROTECTED]> wrote:
> > > My question is how could I make the Class intelligently figure out
> > > what data I am passing and what not during instantiation, so I don't
> > > have to explicitly write code for missing or not missing data. Ideally
> > > what I would like to do, is pass just a dictionary that is of the form
> > > { 'field_name':field_data }, and let the class figure what goes where
> > > and what is missing.
> >
> > Sorry, I don't understand what you want here. Could you provide a more
> > complete example?
>
> Here goes an example
>
> class Person(Entity):
>     has_field('name',Unicode,required=True)
>     has_field('lastname',Unicode,required=True)
>     has_field('title',Unicode)
>
>
> Assuming I have a list of like this dictionaries: people =
> [ { 'title':'Mr','name':'Darth','lastname':'Vader' },
> {'name':'Luke','lastname':'Skywalker'} ]
>
> for person in people:
>     Person(person)
>

If I understand your problem correctly, you can use Python double-star syntax.

people = [ { 'title':'Mr','name':'Darth','lastname':'Vader' },
{'name':'Luke','lastname':'Skywalker'} ]

for person in people:
    Person(**person))

It will "open" the dict into keyword arguments, if the argument is not
there (like in your second item in the list, that is missing title),
it will simply not pass it, making Elixir create it as None (if the
field is not required).

-- 
Best regards
Leandro Lameiro

Blog: http://lameiro.wordpress.com

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"SQLElixir" 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/sqlelixir?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to