pass any field as a keyword param to the constructor?

4 views
Skip to first unread message

Lukasz Szybalski

unread,
Nov 25, 2009, 11:36:23 PM11/25/09
to sqlalchemy
http://www.sqlalchemy.org/trac/wiki/UsageRecipes/GenericOrmBaseClass

class Recall(OrmObject):pass

mapper(Recall,recall_table)

record=Recall(RECORD_ID=RECORD_ID,CAMPNO=CAMPNO,MAKETXT=MAKETXT)
session.add(record)
session.flush()

This is not working if using the example set in the url. Is setattr
still working.....? What is the proper way to do this.....for SA
0.5.6?

SQLAlchemy 0.5 Implementation:

class OrmObject(object):

def __init__(self, **kw):
for key in kw:
if not key.startswith('_') and key in self.__dict__:
setattr(self, key, kw[key])

def __repr__(self):
attrs = []
for key in self.__dict__:
if not key.startswith('_'):
attrs.append((key, getattr(self, key)))
return self.__class__.__name__ + '(' + ', '.join(x[0] + '=' +
repr(x[1]) for x in attrs) + ')'

Thanks,
Lucas

Lukasz Szybalski

unread,
Nov 26, 2009, 4:24:48 PM11/26/09
to sqlalchemy
Any idea how should I be set the None Type argument to str.?

x=Recall()
type(x.MAKETXT) is <type 'NoneType'>

set in OrmObject when it does the setattr it probably fails because
you cannot setattr on NoneType objects?

setattr(x.MAKETXT,'somevalue')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: attribute name must be string, not 'NoneType'


What is the proper way to do this?
Thanks,
Lucas
--
Setup CalendarServer for your company.
http://lucasmanual.com/mywiki/CalendarServer
Automotive Recall Database - See if you vehicle has a recall
http://lucasmanual.com/recall

Lukasz Szybalski

unread,
Nov 29, 2009, 2:17:03 PM11/29/09
to sqlalchemy
I guess the proper solution is to setup your python class mapper like
this, and use the update method of the __dict__ instead of setattr.

class Recall(object):
def __init__(self, **kw):
self.__dict__.update(kw)
pass


Lucas

Michael Bayer

unread,
Nov 29, 2009, 4:58:58 PM11/29/09
to sqlal...@googlegroups.com

On Nov 29, 2009, at 2:17 PM, Lukasz Szybalski wrote:

> I guess the proper solution is to setup your python class mapper like
> this, and use the update method of the __dict__ instead of setattr.
>
> class Recall(object):
> def __init__(self, **kw):
> self.__dict__.update(kw)
> pass

if Recall is ORM-mapped, the above won't work. use setattr(), not __dict__ access.

>
>
> Lucas
>
> On Thu, Nov 26, 2009 at 3:24 PM, Lukasz Szybalski <szyb...@gmail.com> wrote:
>> Any idea how should I be set the None Type argument to str.?
>>
>> x=Recall()
>> type(x.MAKETXT) is <type 'NoneType'>
>>
>> set in OrmObject when it does the setattr it probably fails because
>> you cannot setattr on NoneType objects?
>>
>> setattr(x.MAKETXT,'somevalue')

don't you mean setattr(x, 'MAKETXT', 'somevalue') here?
> --
>
> You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
> To post to this group, send email to sqlal...@googlegroups.com.
> To unsubscribe from this group, send email to sqlalchemy+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
>
>

Lukasz Szybalski

unread,
Nov 29, 2009, 7:24:27 PM11/29/09
to sqlal...@googlegroups.com
On Sun, Nov 29, 2009 at 3:58 PM, Michael Bayer <mik...@zzzcomputing.com> wrote:
>
> On Nov 29, 2009, at 2:17 PM, Lukasz Szybalski wrote:
>
>> I guess the proper solution is to setup your python class mapper like
>> this, and use the update method of the __dict__ instead of setattr.
>>
>> class Recall(object):
>>    def __init__(self, **kw):
>>        self.__dict__.update(kw)
>>    pass
>

mapper(Recall,recall_table)

then.....

x=Recall(column1=column1.....)
session.add(Recall)

> if Recall is ORM-mapped, the above won't work. use setattr(), not __dict__ access.

This seems to work in 0.5.6? Does that change in 0.6? or



Yes.

>>
>>
>> Lucas
>>
>> On Thu, Nov 26, 2009 at 3:24 PM, Lukasz Szybalski <szyb...@gmail.com> wrote:
>>> Any idea how should I be set the None Type argument to str.?
>>>
>>> x=Recall()
>>> type(x.MAKETXT) is <type 'NoneType'>
>>>
>>> set in OrmObject when it does the setattr it probably fails because
>>> you cannot setattr on NoneType objects?
>>>
>>> setattr(x.MAKETXT,'somevalue')
>
> don't you mean setattr(x, 'MAKETXT', 'somevalue') here?

yes....that was my mistake...

Michael Bayer

unread,
Nov 29, 2009, 11:29:24 PM11/29/09
to sqlal...@googlegroups.com

On Nov 29, 2009, at 7:24 PM, Lukasz Szybalski wrote:

> On Sun, Nov 29, 2009 at 3:58 PM, Michael Bayer <mik...@zzzcomputing.com> wrote:
>>
>> On Nov 29, 2009, at 2:17 PM, Lukasz Szybalski wrote:
>>
>>> I guess the proper solution is to setup your python class mapper like
>>> this, and use the update method of the __dict__ instead of setattr.
>>>
>>> class Recall(object):
>>> def __init__(self, **kw):
>>> self.__dict__.update(kw)
>>> pass
>>
>
> mapper(Recall,recall_table)
>
> then.....
>
> x=Recall(column1=column1.....)
> session.add(Recall)
>
>> if Recall is ORM-mapped, the above won't work. use setattr(), not __dict__ access.
>
> This seems to work in 0.5.6? Does that change in 0.6? or

its not been safe to modify __dict__ directly since at least 0.3.



>
>
>
> Yes.
>
>>>
>>>
>>> Lucas
>>>
>>> On Thu, Nov 26, 2009 at 3:24 PM, Lukasz Szybalski <szyb...@gmail.com> wrote:
>>>> Any idea how should I be set the None Type argument to str.?
>>>>
>>>> x=Recall()
>>>> type(x.MAKETXT) is <type 'NoneType'>
>>>>
>>>> set in OrmObject when it does the setattr it probably fails because
>>>> you cannot setattr on NoneType objects?
>>>>
>>>> setattr(x.MAKETXT,'somevalue')
>>
>> don't you mean setattr(x, 'MAKETXT', 'somevalue') here?
>
> yes....that was my mistake...
>
Reply all
Reply to author
Forward
0 new messages