Hello sqlalchemy users and developers!

I was wondering if there was a way to store a list of strings (or
unicode strings) and access them using a normal Python list when using
SQLAlchemy?

I've pasted my attempt at implementing a simple book database below
this message. You can specify a book's name and description, as well
tags as a list of unicode strings like this:

book = Book()
book.name = "Python Cookbook"
book.description = "It's delicious!"
book.tags = [UnicodeItem("python"), UnicodeItem("programming")]

Running the code should output the book details:

$ python unicode_list.py
       name: Python Cookbook
description: It's delicious!
       tags: [python, programming]


Is there anyway I can specify the list of tags like this, instead?

book.tags = [u"python", u"programming"]

What I'd like to do is be able to have other parts of my code use this
class like any other, and hide away the implementation details of my
sqlalchemy database.

Also, does anyone have any suggestions about how I could maintain the
order of this list as well?

Thanks,

Robert

------------------------------------------------------------------------------------------------
from sqlalchemy import *

###################
# SQLAlchemy code
###################
unicode_item_table = Table('unicode_item',
    Column('item_id', Integer, primary_key=True),
    Column('value', Unicode(255)),
)
class UnicodeItem(object):
    def __init__(self, value):
        self.value = value
    def __repr__(self):
        return self.value
    def __cmp__(self, other):
        if isinstance(other, UnicodeItem):
            return cmp(self.value, other.value)
mapper(UnicodeItem, unicode_item_table)
#this table specifies the many-many relationship between books and
tags
book_tags_table = Table('book_tags',
    Column('book_id', Integer, ForeignKey("book.book_id")),
    Column('unicode_item_id', Integer,
ForeignKey("unicode_item.item_id")),
)

book_table = Table('book',
    Column('book_id', Integer, primary_key=True),
    Column('title', Unicode(255)),
    Column('description', Unicode(255)),
)
class Book(object):
    pass
mapper(Book, book_table, properties = {
        'tags' : relation(UnicodeItem, secondary=book_tags_table,
lazy=False)
    }
)

###################
# main
###################

#create database and session
global_connect("sqlite:///")
default_metadata.create_all()
session = create_session()

#insert some books/tags
book = Book()
book.name = "Python Cookbook"
book.description = "It's delicious!"
#book.tags = [u"python", u"programming"]
book.tags = [UnicodeItem("python"), UnicodeItem("programming")]
session.save(book)
session.flush()

#print out book details
print """\
       name: %s
description: %s
       tags: %s""" % (book.name, book.description, book.tags)


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