u want to autoassign a value to each column at __init__ or what?

u could have some Base2 that does the repr/init/whatever as mixin.

say have a property _my_attrs that could walk once the 
class.__dict__  - if u want only local stuff - or dir(self) - if u 
want all inherited stuff too - , and looking for 
isinstance(x,sa.Column); or walk 
class_mapper(self.__class__).properties (or whatever the current name 
of it) - if u want mapped things only. Then u can cache the resulting 
list, and reuse it later. u can do it in a metaclass (not the for 
mapper' way though).
beware, automatic setting or repr's can easy fall into recursion - 
just give an A pointing to B pointing to same A.

everybody needs something like this but then everyone wants it his own 
way so whatever u do.
see one in  
http://dbcook.svn.sourceforge.net/viewvc/dbcook/static_type/util/str.py?revision=178&view=markup
another in 
http://dbcook.svn.sourceforge.net/viewvc/dbcook/trunk/dbcook/baseobj.py?revision=224&view=markup

as of the declarative ... and typing five times same name... 
there's also elixir and dbcook.
first one is heavily used and well known. 
the other one is by me, and goes long way further in hiding the DB 
where it is not needed to be seen, but noone else is using it so far.
YMMV

On Saturday 21 June 2008 18:38:05 Werner F. Bruhin wrote:
> The debug problem (see traceback below) I had is due to the code I
> used from the wiki:
> http://www.sqlalchemy.org/trac/wiki/UsageRecipes/GenericOrmBaseClas
>s
>
> The code does not work in 0.5 anymore, one reason is that the
> MyClass.c attribute is no longer there.
>
> I looked at the declarative stuff and I am thinking of switching
> over to it, but it still has the "problem" that each column name
> has to be typed five times (including the init and repr method
> stuff).  O.K. this is only in the model, done once but still it
> opens things up for errors.
>
> Looking at
> "http://www.sqlalchemy.org/docs/05/ormtutorial.html#datamapping_dec
>larative" it would be really nice if the "init" and "repr" methods
> are automagically generated, or maybe something like a mixin could
> be provided.
>
> In my test I added the following to MyClass and it works.  But I am
> sure this is not the most elegant way of doing this and I am not
> sure how one changes this to a mixin without stuffing other things
> up.
>
> Would appreciate if anyone can push me in the right direction.
> Werner
>
> class Prefminimal(Base):
>     __tablename__ = 'preferences'
>
>     prefid = sa.Column(sa.Integer,
> sa.Sequence('gen_preferences_prefid'), primary_key=True,
> nullable=False) dbstructure = sa.Column(sa.Integer)
>     dbdata = sa.Column(sa.Integer)
>
>     # added stuff
>     myColumns = []
>     for att in dir():
>         if not att.startswith(('_', 'myColumns')):
>             myColumns.append(att)
>
>     def __repr__(self):
>         atts = []
>         for att in self.myColumns:
>             atts.append( (att, getattr(self, att)))
>
>         return self.__class__.__name__ + '(' + ', '.join(x[0] + '='
> + repr(x[1]) for x in atts) + ')'
>
> Werner F. Bruhin wrote:
> > Traceback (most recent call last):
> >   File
> > "C:\Python25\Lib\site-packages\boa\Debugger\IsolatedDebugger.py",
> > line 823, in run
> >     Bdb.run(self, cmd, globals, locals)
> >   File "C:\Python25\lib\bdb.py", line 366, in run
> >     exec cmd in globals, locals
> >   File "<string>", line 1, in <module>
> >   File "C:\Dev\twcb\Program\twcb.py", line 1108, in <module>
> >     main()
> >   File "C:\Dev\twcb\Program\twcb.py", line 1104, in main
> >     appl = BoaApp()
> >   File
> > "C:\Python25\Lib\site-packages\wx-2.8-msw-unicode\wx\_core.py",
> > line 7912, in __init__
> >     self._BootstrapApp()
> >   File
> > "C:\Python25\Lib\site-packages\wx-2.8-msw-unicode\wx\_core.py",
> > line 7487, in _BootstrapApp
> >     return _core_.PyApp__BootstrapApp(*args, **kwargs)
> >   File "C:\Dev\twcb\Program\twcb.py", line 266, in OnInit
> >     self.prefs = self.ds.query(db.prefminimal).get(1)
> >   File
> > "c:\python25\lib\site-packages\sqlalchemy-0.5.0beta1-py2.5.egg\sq
> >lalchemy\orm\session.py", line 894, in query
> >     return self._query_cls(entities, self, **kwargs)
> >   File
> > "c:\python25\lib\site-packages\sqlalchemy-0.5.0beta1-py2.5.egg\sq
> >lalchemy\orm\query.py", line 97, in __init__
> >     self.__setup_aliasizers(self._entities)
> >   File
> > "c:\python25\lib\site-packages\sqlalchemy-0.5.0beta1-py2.5.egg\sq
> >lalchemy\orm\query.py", line 111, in __setup_aliasizers
> >     mapper, selectable, is_aliased_class = _entity_info(entity,
> > ent.entity_name)
> >   File
> > "c:\python25\lib\site-packages\sqlalchemy-0.5.0beta1-py2.5.egg\sq
> >lalchemy\orm\util.py", line 401, in _entity_info
> >     mapper = entity.compile()
> >   File
> > "c:\python25\lib\site-packages\sqlalchemy-0.5.0beta1-py2.5.egg\sq
> >lalchemy\orm\mapper.py", line 370, in compile
> >     mapper.__initialize_properties()
> >   File
> > "c:\python25\lib\site-packages\sqlalchemy-0.5.0beta1-py2.5.egg\sq
> >lalchemy\orm\mapper.py", line 391, in __initialize_properties
> >     prop.init(key, self)
> >   File
> > "c:\python25\lib\site-packages\sqlalchemy-0.5.0beta1-py2.5.egg\sq
> >lalchemy\orm\interfaces.py", line 378, in init
> >     self.do_init()
> >   File
> > "c:\python25\lib\site-packages\sqlalchemy-0.5.0beta1-py2.5.egg\sq
> >lalchemy\orm\properties.py", line 193, in do_init
> >    
> > strategies.DefaultColumnLoader(self)._register_attribute(None,
> > None, False, comparator_callable, proxy_property=self.descriptor)
> > File
> > "c:\python25\lib\site-packages\sqlalchemy-0.5.0beta1-py2.5.egg\sq
> >lalchemy\orm\strategies.py", line 37, in _register_attribute
> >     proxy_property=proxy_property
> >   File
> > "c:\python25\lib\site-packages\sqlalchemy-0.5.0beta1-py2.5.egg\sq
> >lalchemy\orm\unitofwork.py", line 88, in register_attribute
> >     return attributes.register_attribute(class_, key, *args,
> > **kwargs) File
> > "c:\python25\lib\site-packages\sqlalchemy-0.5.0beta1-py2.5.egg\sq
> >lalchemy\orm\attributes.py", line 1407, in register_attribute
> >     descriptor = proxy_type(key, proxy_property, comparator,
> > parententity) File
> > "c:\python25\lib\site-packages\sqlalchemy-0.5.0beta1-py2.5.egg\sq
> >lalchemy\orm\attributes.py", line 144, in __init__
> >     self.descriptor = self.user_prop = descriptor
> > AttributeError: can't set attribute
>
> 


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to