[sqlalchemy] overriding collection methods

2007-08-19 Thread sdobrev
hi
i need to have a list collection with list.appender (in SA 0.4 terms) 
that accepts either one positional arg as the value, or keyword args 
which it uses to create the value. Each collection instance knows 
what type of values to create.

so i do:
class MyCollection( list):
factory = None
@collection.appender
def append( me, obj =_NOTSET, **kargs):
if obj is _NOTSET:#marker for notset
obj = me.factory( **kargs)
list.append( me, obj)
return obj

@classmethod
def myCollectionFactory( klas):
m = Association.MyCollection()
m.factory = klas
return m

and in the mapper, 
  ... relation( ..., 
   uselist = True,
   collection_class = assoc_klas.myCollectionFactory
 )

well, it doesnot work. 
all is well until in _instrument_class() the ABC decoration kicks in, 
and setup a preset append-wrapping decorator that has another 
interface (as in _list_decorators(): 
def append(self, item, _sa_initiator=None):...

Any idea to fix/enhance this, letting **kwargs through to my function?
The dynamic wrapper() can do this, while these preset ones cannot... 
while they should be equaly powerful.

There are 2 (different) uses of an appender, one is the SA itself, but 
the other is the programmer. SA will always use single 
arg/positionals, while i could use this or that or combination.

===
coupe of comments on orm.collections.py:
 - there are several lines like
   setattr(fn, '_sa_instrumented', True)
   why not just use fn._sa_instrumented= True ?

 - the repeated check/setup in _instrument_class() can be looped:

# ensure all roles are present, and apply implicit instrumentation 
if needed
for rolename,eventname in dict(
appender='fire_append_event',
remover ='fire_remove_event',
iterator=None,
).iteritems():
roler = roles.get( rolename, None)
if not role or not hasattr(cls, roler):
typename = cls.__name__
raise exceptions.ArgumentError(
"Type %(typename)s must elect an %(rolename)s method 
to be a collection class" % locals() )
elif (eventname and
roler not in methods and
not hasattr(getattr(cls, roler), '_sa_instrumented')):
methods[ roler] = ( eventname, 1, None)
 

patch attached.

--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---

Index: orm/collections.py
===
--- orm/collections.py	(revision )
+++ orm/collections.py	(working copy)
@@ -647,35 +655,32 @@
 
 # ensure all roles are present, and apply implicit instrumentation if
 # needed
-if 'appender' not in roles or not hasattr(cls, roles['appender']):
+for rolename,eventname in dict(
+appender='fire_append_event',
+remover ='fire_remove_event',
+iterator=None,
+).iteritems():
+roler = roles.get( rolename, None)
+if not role or not hasattr(cls, roler):
+typename = cls.__name__
 raise exceptions.ArgumentError(
-"Type %s must elect an appender method to be "
-"a collection class" % cls.__name__)
-elif (roles['appender'] not in methods and
-  not hasattr(getattr(cls, roles['appender']), '_sa_instrumented')):
-methods[roles['appender']] = ('fire_append_event', 1, None)
-
-if 'remover' not in roles or not hasattr(cls, roles['remover']):
-raise exceptions.ArgumentError(
-"Type %s must elect a remover method to be "
-"a collection class" % cls.__name__)
-elif (roles['remover'] not in methods and
-  not hasattr(getattr(cls, roles['remover']), '_sa_instrumented')):
-methods[roles['remover']] = ('fire_remove_event', 1, None)
-
-if 'iterator' not in roles or not hasattr(cls, roles['iterator']):
-raise exceptions.ArgumentError(
-"Type %s must elect an iterator method to be "
-"a collection class" % cls.__name__)
+"Type %(typename)s must elect an %(role)s method to be "
+"a collection class" % locals() )
+elif (eventname and
+roler not in methods and
+not hasattr(getattr(cls, roler), '_sa_instrumented')):
+methods[ roler] = ( eventname, 1, None)
 
 # apply ad-hoc instrumentation from decorators, class-level defaults
 # and implicit role declarations


[sqlalchemy] Editing row objects

2007-08-19 Thread Brendan Arnold

hi there,

i'm treating the row objects like dictionaries and i'd like to 'tag'
an extra key/value pair on each entry. i have experimented with the
__setattr__ function but this doesn't seem to be bulletproof, is there
a way to do this?

brendan

--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Ascii codec instead of unicode ?

2007-08-19 Thread jason kirtland

Arun Kumar PG wrote:
 >> Ok, you need to get that charset to the driver.  Try removing SET
 >> NAMES from your init_command, and instead pass charset=utf8 and
 >> use_unicode=0 in your database connection URL.
 >
> why do we want to say use_unicode=0 instead or use_unicode=True here?

You can go either way with that.  The MySQLdb driver's default behavior 
when given a 'charset' is to also turn on its "return all strings in 
Unicode" mode.  If you want all of your strings as Unicode that's just 
dandy, but if you expecting them to come back as regular strings encoded 
in the charset you requested you'd be in for a surprise...

In my own code I enable use_unicode and I don't specify any Unicode 
options or column types at the SQLAlchemy level.

-j


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Using 0.4 in anger

2007-08-19 Thread Paul Johnston

Hi,

Couple more things:
1) When specifying foreign_keys manually on a relation, you have to use 
table.c.column; table.column doesn't work.
2) I used to be able to do obj.delete() but now obj.query.delete() isn't 
available.

>You can make whatever changes needed to ActiveMapper, but keep in  
>mind the whole extension itself is deprecated ;) (of course it will  
>be around for awhile since turbogears depends on it)
>  
>
Changing AM to use scoped_session was pretty easy. However, this changes 
compatibility quite a lot - without the change, cls.get() works but 
gives a warning; with the change, cls.get() doesn't work. I still think 
the change is worthwhile, sticking closely to the 0.4 style, but I'll 
take your steer on whether this is to much change (we'll have to decide 
before 0.4 final though).

All the best,

Paul

--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Generate new id

2007-08-19 Thread Koen Bok

Dear Gennady,

I'm afraid flushing is your only option, as you need the next number
from the database sequence. What I do is flushing some objects in
their init methods already.

Kindest regards,

Koen Bok

On Aug 19, 7:22 am, Gennady <[EMAIL PROTECTED]> wrote:
> Hello,
>
> I'm a new sqlalchemy user. I have a question about generate id.
>
> I have a class with __init__ method, and after some initialization
> __init__ call events. Event handlers must know about id of new object.
>
> But if I use Sequence I don't know about new id in __init__ method
> before I flush changes to the database. And I can have two or more
> processes that connect to database. If I generate id in python may be
> a conflict on save objects in two processes.
>
> What is right way to resolve this problem? How to generate id and use
> transactions?
>
> Thank you.
>
> Gennady.


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



[sqlalchemy] Generate new id

2007-08-19 Thread Gennady

Hello,

I'm a new sqlalchemy user. I have a question about generate id.

I have a class with __init__ method, and after some initialization
__init__ call events. Event handlers must know about id of new object.

But if I use Sequence I don't know about new id in __init__ method
before I flush changes to the database. And I can have two or more
processes that connect to database. If I generate id in python may be
a conflict on save objects in two processes.

What is right way to resolve this problem? How to generate id and use
transactions?

Thank you.

Gennady.


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Ascii codec instead of unicode ?

2007-08-19 Thread Arun Kumar PG
why do we want to say use_unicode=0 instead or use_unicode=True here?

On 8/16/07, jason kirtland <[EMAIL PROTECTED]> wrote:
>
>
> Ok you need to get tArun wrote:
> > I am using mysqldb-1.2.2. I am passing 'SET NAMES' to connect
> > method as a value for "init_command" parameter. All tables have
> > utf8 charset. And I pass convert_unicode=True to engine.
> >
> > Let me know if anything else is required.
>
> Ok, you need to get that charset to the driver.  Try removing SET
> NAMES from your init_command, and instead pass charset=utf8 and
> use_unicode=0 in your database connection URL.
>
>
>
>
> >
>


-- 
Cheers,

- A

--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---