Archive for October, 2009

Filed Under (Python) by Łukasz Balcerzak on October-21-2009

While I love many things about SQLAlchemy this was something I just missed sooooo much – defining model’s methods without having to pass session object.
While using Django ORM for such action it is extra easy as there is no session objects at all (well, there is connection object but it’s much simpler and less sophisticated).
Assume you have BlogEntry and BlogComment mappers and you want to define function at entry model which returns related comments – I know, it is as simple as it could be, but it’s only an example.

class BlogEntry(Base):
    __tablename__ = 'blog_entry'

    title = Column(String)
    content = Column(Text)
    created_at = Column(DateTime, default=datetime.datetime.now)

    def __repr__(self):
        return self.title

class BlogComment(Base):
    __tablename__ = 'blog_comment'

    content = Column(Text)
    created_at = Column(DateTime, default=datetime.datetime.now)
    entry_id = Column(Integer, ForeignKey('blog_entry.id'))
    entry = relation('BlogEntry',
        primaryjoin='BlogEntry.id==BlogComment.entry_id',
        backref='comment_set')

You now can of course get comments using comment_set attribute on blog entry. But sometimes we want start querying for other objects, for some reasons.
With Django ORM, no problem, just define method on the model and start querying – it just works. With SQLAlchemy on the other hand we need to create query
using Session object. But how does mapper use proper session if you call ‘comment_set’? Well, I’ve found the answer here: http://www.sqlalchemy.org/docs/05/reference/orm/sessions.html . There is ‘Session.object_session’ class method which returns session object binded to an model object.

So now we can simply define method for BlogComment:

    def get_related_comments(self):
        session = Session.object_session(self)
        comment_list = session.query(BlogComment)\
            .filter(BlogComment.entry_id==BlogEntry.id)\
            .all()
        return comment_list

Just remember that object which is passed to ‘Session.object_session’ method has to be binded with some session first.



Filed Under (Python) by Marcin Kuźmiński on October-20-2009

I needed rot47 in python which is AFAIK not available so i made small function to implement rot47

def rot47(str):
    return ''.join([chr(33+((ord(c)-33+47)\
%(47*2))) if c!=" "else " " for c in str])

There is an rot13 implementation in python just encode a string using ‘rot13′ encoding

'string'.encode('rot13')


Filed Under (Python) by Łukasz Balcerzak on October-9-2009

Finally, I have released my second python project. It is small toolkit for browsing repositories (only svn is supported now) in simple way.
You can find it here: Version Control Browser.

A while ago I started to create django based, software management tracking project. I wanted to implement something similar to the browser from Trac. Long story, short – I’ve reorganized codes and ended up with needs of Django-independant repositories browsing library. There are some modules available right now but most of them focus on mirroring commit/update/pull/push commands – not on browsing which is much easier and should be more lightweight in my opinion.

If anyone who reads this would like to help me with the project – let me know.