This is an idiot mistake.

I wanted to create 3 Entities, all sharing a certain amount of behaviour and properties (that is methods and attributes). So I decided to a create a BaseClass where the common code is shared, then create 3 Classes that subclass the later.

So how’s the code gonna look like ?

At first guess, I thought “why would I subclass all my classes from Entity AND BaseClass ? wouldn’t it be better to just subclass BaseClass and then subclass BaseClass from Entity ? thus allowing all the subclasses of BaseClass be Entities ?”

class BaseClass(Entity):
    #Some useful methods and attributes here
    #…

class SubClassOne(BaseClass):
    # Your actual class
    # …

class SubClassTwo(BaseClass):
    # Your class here
    # …
#… more subclasses
 

Bad idea ! If you’re doing this, you are defining BaseClass as an entity, that will be created in the database. And so you’ll get an Exception like

Exception: Column ‘type’ already exist in ‘BaseClass’

when elixir will try to create the second subclass.

So, no, the only way to do it right is to subclass from Entity AND BaseClass.

class BaseClass:
    #Some useful methods and attributes here
    #…

class SubClassOne(BaseClass,Entity):
    # Your class here
    # …

class SubClassTwo(BaseClass,Entity):
    # Your class here
    # …
#… more subclasses
 

With the help of Ben bangert, I rewrote my code this way :

class UniqueMeta(EntityMeta):

    def __call__(cls,key):
        “”
        If it’s in the cache, return the cached version
        If not in the cache :
            If it’s in the database, retrieve it, cache it and return it
            If it’s not there, create it, cache it and return it
        “
“”
        thecache = cache.get_cache(cls.__name__,type=“memory”)
        def makeTag():
            theTag = cls.query.filter_by(**{cls.filterby:key}).first()
            if not theTag:
                #not in the database either
                theTag = type.__call__(cls,key)
                session.add(theTag)
            return theTag
        return thecache.get_value(key=key,createfunc=makeTag)
   
class Ville(Entity):
    using_options (tablename=“Villes”)
    __metaclass__ = UniqueMeta
    nom           = Field(Unicode(64))
    filterby      = “nom”
    def __init__(self,nom,*args,**kw):
        Entity.__init__(self,*args,**kw)
        self.nom = nom

    def __repr__(self):
        return “”self.nom
   
class TypeBien(Entity):
    using_options (tablename=“TypesBiens”)
    __metaclass__ = UniqueMeta
    type          = Field(Unicode(64))
    filterby      = “type”

    def __init__(self,type,*args,**kw):
        Entity.__init__(self,*args,**kw)
        self.type = type

    def __repr__(self):
        return “” % self.type

There’s an improvement here over the UniqueObject recipe : more classes can use the UniqueMeta metaclass if they define a filterby class attribute.

Today’s video :

My notes :

SQLite introduction

db_row vs dtuple

August 29th, 2008

MySQLdb result sets do not offer a nice way to acess information : tuples. This means that whenever you change your SQL query, you have to modify the code that extract information from the data set because the position of elements may have changed.

result[0] was for example Name but now it is Password.

If you google a bit, you will pass by dtuple and db_row. Which one should you use ? I googled for a comparison of the two but found nothing in the first results. So here’s a little description of what I have found so far after testing the two :

dtuple is flexible, but column names are case sensitive. That means that row.Name or row[’Name’] is ok but not row.name nor row[’name’]. In the other hand, the results are mutable, this means that you can add some other field in the result set if another function or method needs it. On the contrary, db_row does not allow you to change the result set because they are immutable (they use python’s __slot__ thing that prevent you from adding new attributes to your object). The good thing is that this is faster because attribute lookup is boosted. Besides, db_row is memory friendly because it does not create an object per row. This means that if your SELECT returns 100.000 lines, it wont create 100.000 objects. db_row allows also case-insensitive key/attribute fetching, as a little “syntactic sugar”.

There’s a third alternative, sqlWrap, which offers very nice reporting solutions, like export to xhtml, pretty printing, and export to restructuredText. I did not try this one but looks fun.

Choose wisely.

Show current queries being processed :

$mysqladmin processlist

Then kill the desired command

$mysqladmin kill 2109

I tried to install DBDesigner on my ubuntu linux laptop today and through some issues regarding the libborq library.

In fact, the package DBDesigner4.0.5.4.tar.gz contains the binary compiled for Linux, and simply ran

root@bughunter:~/DOWNLOAD/APPLICATIONS/DBDesigner4# ./DBDesigner4

But I got something like

root@bughunter:~/DOWNLOAD/APPLICATIONS/DBDesigner4# ./DBDesigner4
libborqt-6.9-qt2.3.so: Ne peut ouvrir le fichier d’objet partagé: Aucun fichier ou répertoire de ce type

(sorry, french locales :) )

Basically “no such file or directory”.

So I googled for it a bit and here’s how to fix this problem :

Download the kylixlibs3-borqt-3.0-2.tar.gz tarball from sf, extract it then, if you follow first google ranked search results, you just have to copy the extracted libborqt-6.9.0-qt2.3.so to /usr/lib/ and voilà… Nope. Things must have changed since, because the library versionning has changed : it is libborqt-6.9.0-qt2.3.so (notice it is version 6.9.0 and not 6.9), so you must also change it’s name when you copy it in /usr/lib/.

root@bughunter:~/DOWNLOAD/LIBS/kylixlibs3-borqt# cp libborqt-6.9.0-qt2.3.so /usr/lib/libborqt-6.9-qt2.3.so

And voilà !

Voici une page qui m’a bien servi :

http://fadace.developpez.com/oracle/nls/

Il suffit de mettre la bonne variable d’environnement. Dans les shell qui lancent mon zope (bin/runzope et bin/zopectl) , voici ce que j’ai ajouté pour régler le problème :

# Mon système et mon zope sont configurés en UTF-8. Je laisse american parce que la base est en American.

NLS_LANG=AMERICAN_AMERICA.AL32UTF8

export  NLS_LANG

Oracle 10g do not have the equivalent of the powerful MySQL’s REPLACE command. You have to use the MERGE command instead, which is WAY more boring and lengthy :

MERGE INTO TableToMergeTo TTMT
USING ( SELECT SelectClause FROM DUAL or any other table ) U
ON (U.ID= TTMT.ID)
WHEN NOT MATCHED THEN
INSERT (InsertClause)
VALUES (ValuesClause)
WHEN MATCHED THEN
UPDATE SET (SetClause)

While you can do this in MySQL

REPLACE INTO TableName SELECT (SelectClause);

No comment…

Some dudes from denemark asked IBM to install an oracle database for them, IBM said that this operation is quite complicated and demands a total working amount of 50 hours.

So the guys decided to make 2 little videos to help IBM and others install Oracle in a less painful way…Quite impressing :)

Part 1 : you’re doing it worng…

Part 2 : no appologies