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

During the weekend I’ve created Djalog project hosted at the http://code.google.com/p/djalog/. It is simplest possible logging app aimed to work within Django context (or for prototyping as it is also logging-ready out of the box after import in any scripts).

I’ve also put it on the PyPi (http://pypi.python.org/pypi/Djalog) so in order to install it you would execute:

easy_install Djalog

and you are ready. If you want to quickly check how it works just type:

import djalog
import logging

logging.debug("Log some debug information.")
logging.info("Log some information.")
logging.warn("Log some warning.")
logging.error("Log some error.")
logging.critical("Log some critical information.")

from djalog import configure
configure(LOG_USE_COLORS=True, LOG_SQL=True, LOG_LEVEL=5)

logging.debug("Log some debug information.")
logging.info("Log some information.")
logging.warn("Log some warning.")
logging.error("Log some error.")
logging.critical("Log some critical information.")

# Log sql query
logging.sql("SELECT * FROM users WHERE username = 'admin'")

This is beta but it should be changed to “final” in two weeks. Remember that you may update projects from CheeseShop using “-U” option:

easy_install -U Djalog

Enjoy!



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 Ł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.



Filed Under (Django, Pylons, SqlAlchemy) by Marcin Kuźmiński on September-26-2009

Recently i had to create a lot of models using declarative mode with sqlalchemy 0.5.5 It was very time consuming especially for large databases. I started to think about an automated solution.

Sqlalchemy have this auto load feature which fetches info about tables by itself. I wanted to use this somehow to generate a string which can be easily  copy/paste to database model definition script. So i started a little project to write auto  model generator for sql alchemy based on auto load feature in sqlalchemy.

The script  is not perfect but it works and save a huge amount of time just codding the models. In fact is was able to setup a 186 table database model in 15minutes with this script.

Well the script helped me a lot and now on when i have to load some new existing database or changes i use it all the time.

The documentation is inside the script i tried to write it so anybody could use it out of the box.

I tested it on 4 schema 200+ tables database and i worked quite good. This script still have to be polished but current functionality is enough for most users.

My script is going to be also a part of django-alchemy project started by my friend Lukasz Balcerzak. You can visit the project site @ http://code.google.com/p/django-alchemy/

I used it with Pylons application and Django with modded sqlalchemy. but it can be basically used in any app which uses sqlalchemy. I did not test it with mysql though,but it should work, just specify mysql as dialect.

You can download this script from sqlalchemy model generator



Filed Under (Django, SqlAlchemy) by Marcin Kuźmiński on September-5-2009

One of my friends started an interesting project to integrate sqlalchemy into Django. Project name is django-alchemy.
You can visit it on Google code under http://code.google.com/p/django-alchemy/.

You may ask why to use sqlalchemy under Django if it already have an ORM ?
It’s very easy the django orm is not always the best choice. When using multiple schemas, or multiple database connection the Django ORM just does not provide such functionality.
SqlAlchemy have this built in so when you’re thinking of using multiple schemas or databases in your django project check out his project. Highly recommended



Filed Under (Django, Python) by Łukasz Balcerzak on July-29-2009

With almost 4 months late here it comes, Django 1.1. I’ve been waiting to see this at the final stage, as we use Django in our company, and I was sometimes forced to code down things that are already boundled now, like sql aggregation functionality for instance (well, in fact I just wrote a little django app using full force of SQLAlchemy instead of Django ORM). Additionally, some security patch was released yesterday, which is included with 1.1 too.

You may read more at release notes page . Stay tuned with Django and minor swing ;-)