Archive for the ‘Python’ Category

1337

Calculating how many lines my project is at currently (not including tester code or templates):

(kserve)@haven:~/kserve/fresh/kserver$ find lib/ controllers/ model/ -type f -name “*.py”  | grep -v template.py | grep -v auth.py | grep -v error.py | grep -v __init__ |grep -v app_globals.py| grep -v base.py |xargs cat  | wc -l
1337

l33t.. no bs.

Unfortunately, it is not done. I am going to to have to code 30,000 more lines now.. damnit!

Jrod.

Woe to the client…

that triggers this exception! Woe to them indeed!

SQLAlchemy declarative_base: setting the storage engine and other table options

Using mysql for your Engine.

DeclarativeBase = declarative_base()
class TableClass(DeclarativeBase):
    __tablename__ = 'table_name'
    __table_args__ =  {'mysql_engine':'InnoDB', 'mysql_charset':'utf8'}
    col_id = Column(Integer, primary_key=True)
    ...

Exposing sqlalchemy engine in turbogears 2.1

Getting access to the internals of the sqlalchemy stack in turbogears can be difficult if you do not know where to look. The use of declarative_base() to create model classes further obfuscates the location of the table and metadata objects.

In order to create/drop a table using an SQLAlchemy object, you need access to the table object, the table metadata, and a database engine object. When extending DeclarativeBase, the table object is exposed by the __table__ attribute of every model class. For instance:

class Distro(DeclarativeBase):
__tablename__ = ‘distro’
id = Column(Integer, primary_key=True, unique=True)
label = Column(Unicode(35), nullable=False)

In [1]: from yourapp.model import *

In [2]: import transaction

In [3]: distro_obj = Distro()

In [4]: print distro_obj.__table__.__doc__
——> print(distro_obj.__table__.__doc__)
Represent a table in a database.

e.g.::

mytable = Table(“mytable”, metadata,
Column(‘mytable_id’, Integer, primary_key=True),
Column(‘value’, String(50))
)

As you can see, you may access the methods of Table() from the DeclarativeBase __table__ attribute. In order to use the model to create/drop the table it describes, you need to bind the table metadata to an instantiated SQLAlchemy engine object. Pylons, the framework which Turbogears 2+ is based, stores the engine (defined in the paster .ini configuration) in the config global configuration dictionary, specifically:

config['pylons.app_globals'].sa_engine

Going back to the model we created, we can create the table using the Table() method create.

distro_obj.__table__.create(config['pylons.app_globals'].sa_engine)

Now that the table is created you may use the session object to interact with the table.

In [5]: distro_obj.label = 'rhel5_minimal'
In [6]: DBSession.add(distro_obj)
In [7]: transaction.commit()
In [8]: distro_label = DBSession.query(Distro.label).filter(Distro.label=='rhel5_minimal').one()[0]
In [9]: print distro_label
-------> print(distro_label)
rhel5_minimal

For more information, take a look at the documentation for TG2.1 and SQLAlchemy declarative syntax.

sqlalchemy, MySQLdb, debian

(in virtualevn)

$ sudo apt-get install libmysqlclient-dev python-dev
$ easy_install SQLAlchemy mysql-python

Not sure why the package is labeled mysql-python. Also, you need the development headers because the MySQLdb module is written in c.

Learning python

I have started to rewrite some of my existing code in python in an earnest effort to master the language (I have been putting it off for a long while now). Anyway, I was working on a parser for a # commented file, this is how I stripped the comments from each line in C:


while(fgets(line, sizeof(line)-1, fp)) {
  char *hash; //ptr to hash mark denoting a comment
  if(line[0] == '#' || line[0] == '\n')
    continue;
  if((hash = strchr(line,'#')) != NULL)
  line[hash - line] = '\0'; //line + hash = memory location of hash
  printf("%s", line);
}

Now that I am beginning to get a handle on python, I am really starting to appreciate its elegance. This line of code accomplishes the same task:

line.strip()[:line.find("#")].strip()

I have not compared the performance of the above line to my c code , but I would assume using all the nested objects would incur a performance hit. I may test this later, but I am pretty sure my c code would be much faster.

Return top