[sqlalchemy] Re: How to map columns into a tuple using SQLAlchemy?

2009-03-13 Thread batraone

Hi Stephen,

Not exactly what I'm looking for (unless I misunderstand you) as I'd
like to not have the individual attributes (lower, nominal, upper) in
the class directly and carry just the _limits variable.

I have a class Result as follows:

class Result:

  def __init__(self):
 self._limits = (None, None, None) # (lower, nominal,
upper)

  def setLimits(self, limits):
self._limits = limits

  ...

In the database, I have a Limits table with three columns:
lower_limit, nominal, upper_limit

I'd like to map these 3 values directly into the _limits variable:

E.g.

limits_table = Table('limits', metadata, Column('id', Integer,
primary_key = True,
metadata, Column
('lower_limit', Float),
metadata, Column('nominal',
Float),
metadata, Column
('upper_limit', Float))

mapper(Result, limits_table)

By default, the Result class will
have .id, .lower_limit, .nominal, .upper_limit created. Rather than
create the 3 attributes and map to the _limits variable as you have
suggested - is there a properties= technique in the mapper or
composite column technique (w/o modifying the Result class directly)
that supports this capability?

Thanks,

Raj


On Mar 13, 3:00 am, Stephen Emslie  wrote:
> You could try changing your _limit tuple to a property on the class
> that returns the tuple you want.
>
> For example:
>
> class Result(object):
>
>   def get_limit(self):
>     return (self.upper, self.lower, self.nominal)
>   _limit = property(get_limit)
>
> Is this what you were looking for?
>
> Stephen Emslie
>
>
>
> On Wed, Mar 11, 2009 at 1:01 AM, batraone  wrote:
>
> > Hi,
>
> >    I'm just starting to use SQLAlchemy and hit a roadblock.
>
> > I have a class Result which contains a tuple called _limits.
> > _limits = (Upper value, lower value, nominal)
>
> > I would like to map a the table's columns directly into this tuple.
>
> > From the documentation, all I can see is columns mapping directly to
> > python attributes (I.e. Result.upper_value, Result.lower_value,..).
> > Is there a way to map the three columns directly into the tuple?
>
> > I do not want to modify the Result class and therefore cannot
> > create it as composite column type.
>
> > I'm hoping there is a syntax that states "map these 3 columns" into
> > this tuple via the mapper.
>
> > Thanks,
>
> > Raj- Hide quoted text -
>
> - Show quoted text -
--~--~-~--~~~---~--~~
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 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: sqlalchemy 0.5.2 "'NoneType' object has no attribute 'get'" error

2009-03-12 Thread batraone

Hi Bruno,

I had received the same error message when I modified an attribute of
an instance. The instance was created before the map was made to the
class.
Not sure if you're doing something similar - but the sample code is in
the thread:
http://groups.google.com/group/sqlalchemy/browse_thread/thread/6f2fd14564172215

-Raj


On Mar 12, 8:57 am, "Michael Bayer"  wrote:
> it means mapper compilation has failed or you're doing something
> internally on an attribute before mappers have been compiled.  mappers can
> be compiled explicitly by saying compile_mappers().
>
>
>
> Bruno Zanchet wrote:
>
> > Hi all,
>
> > We're facing a weird, (apparently) hard to reproduce bug. When
> > accessing a property of an object:
> >     [...]
> >         sig.update('u=%s;%s;%s' % (token.user.id, token.user.yuid,
> > token.user.guid))
>
> >     File "/home/y/lib/python2.5/site-packages/sqlalchemy/orm/
> > attributes.py", line 159, in __get__
> >         return self.impl.get(instance_state(instance))
>
> >     AttributeError: 'NoneType' object has no attribute 'get'
>
> > User is a class which extends declarative_base().
>
> > The "token.user.id" statement is triggering the exception. It's
> > SQLAlchemy 0.5.2. The same code runs just fine with 0.5.0rc4. I didn't
> > dig deeper on this, yet, but... any ideas on what might be causing
> > this?
>
> > Bruno- Hide quoted text -
>
> - Show quoted text -
--~--~-~--~~~---~--~~
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 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Help! More Class->Mapper confusion!

2009-03-11 Thread batraone

Thanks. I don't have control over the instance creation. These pre-
created objects are handed down to me. I could create an adapter that
maps the original class to the one that has been mapped but this is
quite a bit of work as I have to manually copy over each of the source
class attributes to my mapped class. I could be clever and perhaps
look for common attribute names via the internal dictionary but this
too seems clunky.

I would think this is a common problem (augmenting a class to dump
it's contents to a db but keep the original class untouched (sort of
like shelve - but w/o the
restore capability)). Is there a better way to handle this?

Thanks,

Raj

On Mar 11, 2:48 pm, "Michael Bayer"  wrote:
> batraone wrote:
> >     def map(self, myfoo):
> >         'Creates the map. '
>
> >         orm.mapper(Foo, self.t_foo, column_prefix = '_')
>
> >         # Add foo to the database
> >         print dir(myfoo) # where did _name go?, _phone is there!
>
> >         mf2 = Foo() # ok, let's create a new one.
> >         print dir(mf2) # same problem
>
> > if __name__ == '__main__':
> >     f = Foo()
> >     fs = FooStore()
> >     fs.map(f)
>
> The column_prefix wasn't being honored when the mapper checked for
> existing names, this is fixed in trunk r5839.
>
> But also, don't create instances of the object before the class is mapped.
>  In particular, it's bad form to create tables and mappers inside of class
> methods.  Create class-level constructs like tables and mappers at the
> module level, in the same scope in which you create your classes.
>
>
>
> >         mf2 = Foo.Foo() # ok, let's create a new one.
> >         # AttributeError: 'Foo' object has no attribute
> > '_sa_instance_state'
> >         myfoo._phone = '555-1212' #
>
> > if __name__ == '__main__':
> >     orm.clear_mappers()
> >     f = Foo.Foo()
> >     fs = FooStore()
> >     fs.map(f)
>
> don't create instances of the object before the class is mapped.- Hide quoted 
> text -
>
> - Show quoted text -
--~--~-~--~~~---~--~~
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 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Help! More Class->Mapper confusion!

2009-03-11 Thread batraone

Hi,

OK - I ran into another thing that I do not understand about
sqlalchemy. I am a newbie, so hopefully this will be
straightforward.

There are two issues here:

ISSUE1:
I create a class called foo w/ a method called name().
I map a table that has a column called 'name' but use
the column_prefix = '_' but I do not see _name get added
to the class! But other labels, such as _phone which do
not have a corresponding method name does get added.

Here's the code:

import sqlalchemy as sa
from sqlalchemy import orm
from sqlite3 import dbapi2 as sqlite

class Foo(object):
def __init__(self):
self._myname = 'Bar'

def name(self):
return(self._myname)

class FooStore:
def __init__(self):
self.metadata = sa.MetaData()

# table to map class to
self.t_foo = sa.Table('table_foo', self.metadata,
sa.Column('id', sa.types.Integer, primary_key=True),
sa.Column('name', sa.types.String(100)),
sa.Column('phone', sa.types.String(100))
)

def map(self, myfoo):
'Creates the map. '

orm.mapper(Foo, self.t_foo, column_prefix = '_')

# Add foo to the database
print dir(myfoo) # where did _name go?, _phone is there!

mf2 = Foo() # ok, let's create a new one.
print dir(mf2) # same problem

if __name__ == '__main__':
f = Foo()
fs = FooStore()
fs.map(f)


ISSUE2: I have an object that will be given to me that I want to
store into a database. The class definition is located in a package.
When I map this class to a table and set the attribute - I get
an exception:

AttributeError: 'NoneType' object has no attribute 'set'

This can be seen by modifying the above example - where I put
Foo into a package called 'foo':

import sqlalchemy as sa
from sqlalchemy import orm
from sqlite3 import dbapi2 as sqlite

import sys

import foo.Foo as Foo

class FooStore:
def __init__(self):
self.metadata = sa.MetaData()

# table to map class to
self.t_foo = sa.Table('table_foo', self.metadata,
sa.Column('id', sa.types.Integer, primary_key=True),
sa.Column('name', sa.types.String(100)),
sa.Column('phone', sa.types.String(100))
)

def map(self, myfoo):
'Creates the map. te is the test engine'

orm.mapper(Foo.Foo, self.t_foo, column_prefix = '_')

# Add foo to the database

try:
myfoo._phone = '555-1212' # exception is thrown!

except:
#AttributeError: 'NoneType' object has no attribute 'set'
print sys.exc_info()

mf2 = Foo.Foo() # ok, let's create a new one.
# AttributeError: 'Foo' object has no attribute
'_sa_instance_state'
myfoo._phone = '555-1212' #

if __name__ == '__main__':
orm.clear_mappers()
f = Foo.Foo()
fs = FooStore()
fs.map(f)



What's the right way to adapt this class to a table?

Thanks!

-Raj


--~--~-~--~~~---~--~~
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 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] How to map columns into a tuple using SQLAlchemy?

2009-03-11 Thread batraone

Hi,

I'm just starting to use SQLAlchemy and hit a roadblock.

I have a class Result which contains a tuple called _limits.
_limits = (Upper value, lower value, nominal)

I would like to map a the table's columns directly into this tuple.

>From the documentation, all I can see is columns mapping directly to
python attributes (I.e. Result.upper_value, Result.lower_value,..).
Is there a way to map the three columns directly into the tuple?

I do not want to modify the Result class and therefore cannot
create it as composite column type.

I'm hoping there is a syntax that states "map these 3 columns" into
this tuple via the mapper.

Thanks,

Raj

--~--~-~--~~~---~--~~
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 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] How to map columns into a tuple using SQLAlchemy?

2009-03-11 Thread batraone

Hi,

I'm just starting to use SQLAlchemy and hit a roadblock.

I have a class Result which contains a tuple called _limits.
_limits = (Upper value, lower value, nominal)

I would like to map a the table's columns directly into this tuple.

>From the documentation, all I can see is columns mapping directly to
python attributes (I.e. Result.upper_value, Result.lower_value,..).
Is there a way to map the three columns directly into the tuple?

I do not want to modify the Result class and therefore cannot create
it as composite column type.

I'm hoping there is a syntax that states "map these 3 columns" into
this tuple via the mapper.

Thanks,

Raj

--~--~-~--~~~---~--~~
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 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---