Simplifying the model with assign_mapper

Thanks to all comments to my previous post, I managed to further simplify the model.

Changes:

  • I don’t have to define any variables for the model classes.
  • The session and metadata are imported from TurboGears.

Same functionality as before, but shorter (model.py at the bottom of the post).

A developer who doesn’t write unit tests will for sure write a bunch of hard to find bugs with a dynamically typed language like Python (like setting a wrong variable before an update: project.ttle=title). But hey, If it’s not tested, it’s broken.

Bruce Eckel has a good article for Java developers about weak typing: Strong Typing vs. Strong Testing

model.py (compare to previous SQLAlchemy version ):

from sqlalchemy import *
from sqlalchemy.ext.assignmapper import assign_mapper
import turbogears
from turbogears.database import metadata, session
...
#same table definitions as before
...
class Project(object):
    pass
class Action(object):
    pass
class Tag(object):
    pass
class Context(object):
    pass

assign_mapper(session.context,Tag, tags)
actionmapper = assign_mapper(session.context,Action, actions, properties = {
    'tags' : relation(Tag, secondary=tagaction)
})
projectmapper = assign_mapper(session.context,Project, projects, properties={
    'actions' : relation(Action, backref='project', order_by=actions.c.priority),
})
contextmapper = assign_mapper(session.context,Context, contexts, properties={
    'actions' : relation(Action, backref='context'),
})
About these ads

13 Responses to Simplifying the model with assign_mapper

  1. Tim Lesher says:

    It looks as if the link to “previous SQLAlchemy version” is broken.

  2. Julie says:

    My brother suggested I may like this web site. He was once entirely right.
    This submit actually made my day. You can not believe simply how so much time I had spent for this information!
    Thanks!

  3. Hello! Someone in my Facebook group shared this website with us so I
    came to take a look. I’m definitely loving the information. I’m bookmarking and will be
    tweeting this to my followers! Wonderful blog and superb style and design.

  4. Darin says:

    I visited plenty of website but I this one this one holds lots of useful points.

  5. The other day, while I was at work, my sister stole my iPad
    and tested to see if it can survive a thirty foot drop, just so
    she can be a youtube sensation. My apple ipad is
    now broken and she has 83 views. I know this is completely off topic but I had
    to share it with someone!

  6. Mittie says:

    naturally like your web site but you need to check the spelling on several of your posts.
    Many of them are rife with spelling problems and I in finding it very
    bothersome to tell the reality on the other hand I’ll definitely come again again.

  7. Your style is very unique compared to other people I’ve read stuff from.
    I appreciate you for posting when you have the opportunity, Guess
    I’ll just bookmark this site.

  8. Amazing blog! Is your theme custom made or did you download it from somewhere?
    A theme like yours with a few simple tweeks would really make my blog stand out.
    Please let me know where you got your theme. Bless you

  9. homepage says:

    If you want to increase your familiarity just keep visiting this web site and be updated wkth the latest gossip posted here.

  10. 2 says:

    Howdy this is somewhat of off topic but I was wanting to know if
    blogs use WYSIWYG editors or if you have to manually code with HTML.
    I’m starting a blog soon but have no coding expertise so I wanted to
    get advice from someone with experience. Any help would be enormously appreciated!

  11. Benjamin says:

    Ƭremendous things here. I aam very happy tto peer yߋur
    article. Thank yoս a lot and I’m taking a llook ahezd to touch
    you. Willl you kindly Ԁrop me a mail?

  12. hey there and thank you for your information – I’ve certainly picked up something new from right here.
    I did however expertise some technical points using this site, as
    I experienced to reload the website many times previous to
    I could get it to load properly. I had been wondering if your web hosting is OK?
    Not that I am complaining, but slow loading instances times will often affect your
    placement in google and can damage your high-quality score if
    advertising and marketing with Adwords. Well I am adding this RSS to my e-mail and could look out for a lot more of your respective exciting content.
    Make sure you update this again very soon.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: