Archive for the ‘Python’ Category

Filed Under (Python) by Marcin Kuźmiński on January-1-2010

I always loved how the print_r() function worked in php. In python print does print everything but when large collections (ie huge dicts) are printed using print you could get lost in the output. I just discovered a little smarter print function called pprint from module pprint

from pprint import pprint

This is great for debugging something in collections. Pprint prints in a nicer human readable form types of data like : list,dict,tuples.

l = [x for x in range(100)]
l.insert(0, l[:])
pprint(l)
l = dict([(x, x) for x in range(100)])

pprint(l)
#this will print this list in more convinient way
{0: 0,
 1: 1,
 2: 2,
 3: 3,
 4: 4,
 5: 5,
 6: 6,
 7: 7,
 8: 8,
 9: 9,
 ...
 98: 98,
 99: 99}

There are two interesting arguments in pprint function the:
width
Attempted maximum number of columns in the output.
width=100 will wrap everything that outputs longer than 100 chars.
and
depth
The maximum depth to print out nested structures.
ie:

pprint.pprint(stuff, depth=3)
['aaaaaaaaaa',
 ('spam', ('eggs', (...))),
 ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'],
 ['cccccccccccccccccccc', 'dddddddddddddddddddd']]
#now as you can see the tuples at index 1 have a depth of more than 3 and #they are being reduced by the pprint. Imagine how this helps with debuggin a
#really complicated structures.


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

Few days ago there was again a major update on pydev eclipse plugin.
The version 1.5.1. Comes with new refactoring engine PEPTIC, it’s way better than any one before.
Complete list below: visit http://pydev.org for download.

Release 1.5.1

  • Improvements in the AST rewriter
  • Improvements on the refactoring engine:
    • No longer using BRM
    • Merged with the latest PEPTIC
    • Inline local available
    • Extract method bug-fixes
    • Extract local on multi-line
    • Generating properties using coding style defined in preferences
    • Add after current method option added to extract method
    • A bunch of other corner-case situations were fixed
  • Bug-fixes:
    • Minor editor improvements
    • Adding default forced builtins on all platforms (e.g.: time, math, etc) which wouldn’t be on sys.builtin_module_names on some python installations
    • Adding ‘numpy’ and ‘Image’ to the forced builtins always
    • Ctrl+1: Generate docstring minor fixes
    • Ctrl+1: Assign to local now follows coding style preferences properly
    • Exponential with uppercase E working on code-formatting
    • When a set/get method is found in code-completion for a java class an NPE is no longer thrown
    • Backspace properly treated in block mode
    • Setting IRONPYTHONPATH when dealing with Iron Python (projects could not be referenced)
    • No longer giving spurious ’statement has no effect’ inside of lambda and decorators
    • Fixed new exec in python 3k
    • Fixed NPE when breakpoint is related to a resource in a removed project
    • Fixed import problem on regexp that could lead to a recursion.
    • No longer giving NPE when debugging with the register view open
    • List access be treated as __getitem__() in the list — patch from Tassilo Barth
    • Fix for invalid auto-self added when typing


Filed Under (Editors, Python) by Marcin Kuźmiński on November-15-2009

I installed new eclipse on ubuntu 9.10, and noticed that manu of FINISH,
NEXT buttons are not working correctly ( you can’t click them).

So it found out it’s eclipse bug, but fortunately there is a simple
solution to this problem.

Write a startup script:

#!/bin/sh
export GDK_NATIVE_WINDOWS=1
/path_to_eclipse/eclipse

Save it as eclipse-run.sh make it +x, and the buttons will work now if you
run your eclipse via the run script.



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



Filed Under (Python) by Marcin Kuźmiński on September-13-2009

I moved the blog to a new server. I think I’m going to move permanently from slicehost to hosteurope.de for the same price i have 1gb ram compared to 256 and 25 50 times the traffic. Also i compiled php 5.3 with php-fpm for this server, and updated mysql to newest available. I hope I’ll gain some performance with this as well. So enjoy a new theme and the server whitch i hope will be faster.



Filed Under (Python) by Marcin Kuźmiński on August-6-2009

python -m compileall .

This simple command will enforce all files in the current directory will be compiled to .pyc files. Python by default creates .pyc files only on imported modules.
(python -B option is not to compile them at all)
You can also compile in python code using the py_compile module

import py_compile
py_compile.compile("somemodule.py")

You can used it for speed or to protect your source code from being changed.
Compiled python files (.pyc) are little harder( but not impossible) to edit. You would have to decompile it first to edit.



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 ;-)