Archive

Archive for October, 2011

advWhy prnI vDon’t vLike daThe adjHungarian nNotation

October 21st, 2011 4 comments

vNote cThat pThis vIs prpTo vDo prpWith nProgramming adjOnly advNot daThe nCountry pm! sml:-)

nTwo naThings vPrompted prnMe prpInto nWriting pThis pm.

prnI’ve adjRecently vSeen iaA adjPromising nJavaScript nLibrary cnjAnd prnI vWas intrjOh advSo advClose prpTo vStart vUsing cnjExcept prnThat cnjWhen prnI vHad iaA vLook prpAt adjIts nAPI adjMy nJaw vFell prpTo daThe nFloor pm- daThe nThing vWas vUsing daThe adjHungarian nNotation, vIncluding vPrefixing naFunctions prpWith iaAn nFn pm- advSo prnI vConsulted iaA nCalendar cnjAnd advYes pm, prnIt vWas advStill daThe nNear n2011 pm. 

prnI vAm vWriting prnThis nPost advSo prnThat adjPotential adjHungarian nNotation naUsers vCan vUnderstand advHow adjSuch iaAn nAPI vLooks prpLike prpIn daThe nEye prpOf daThe nBeholder pm, prnI vAm advPerfectly adjSure penThey advJust vDo advNot vRealise adjWhat nSort prpOf nCommotion prnThey vCause pm. prnThere vIs adjNo nBeauty prpIn prnIt pm, vDon’t prnYou vAgree pm? intrjSo vPlease pm, vCan pwnWe vStop prnIt advNow pm?

prnI vRemember vSeeing adjSuch naThings prpLike n15 naYears advAgo cnjAnd advReally pm, adThe nWorld vHas advSince vChanged pm, prnWe vDon’t advLonger vUse nVi cnjOr nEd pm- intrjWell, advNot prpTo daThe advSame nExtent prpAt advLeast pm- prnWe vHave nAuto-completion pm, nSyntax vHighlighting pm, nCode vRefactoring pm, nUnit vTesting pm, adjContinuous nIntegration cnjAnd nWhatnot pm.

vLet’s vLeave daThe adjMundane nJob prpOf nType nInference prpTo naCompilers cnjAnd prpUnless daThe adjParticular nLanguage vDoesn’t vEnforce daThe nUsage prpOf prnThese naPrefixes pm, intrjWell pm, vLet’s advNot vDo prnIt advThen pm, advAright pm? naThanks pm!

prnAnother nThing vWas iaAn nSQLite nDatabase prnI vHad iaA nMisfortune prpOf vHaving vHad iaA vLook prpAt prnWhich vHad adjEach nTable conjAnd nColumn vPrefixed prpWith nTbl cnjAnd nCol advRespectively pm. prnI vMean pm, prnI’ve vSeen prpAt advLeast naDozens prpOf naDatabases pm, prpFrom nFirebird prpOn nWindows prpTo nDB2 prpOn nMainframe cnjAnd prnI’m advYet prpTo vConfuse iaA nTable prpWith iaA nColumn pm- prnIt vIs advOK prpTo vUse nUq or nIdx prpIn daThe naNames cnjBut prpBy nApollo’s adjHoly naTrousers, vLet’s advNot vDo daThe prnSame prpWith naTables cnjAnd naAolumns prnThemselves pm!

@fourthrealm

Share
Categories: Software Tags: ,

Read about securing web services with Python using UserNameTokens

October 20th, 2011 No comments

András Veres-Szentkirályi has started a series on securing web services with Python; the first part deals with UserNameTokens and mentions sec-wall, the security proxy, yay! :-)

@fourthrealm

Share

Sort of tired of long constant names – Bunch to the rescue!

October 14th, 2011 10 comments

I’ve gotten a little bit tired of long constant names, like imagine there’s constants.py module which has the following:

 
MESSAGE_CREATE_FOO = '1000'
MESSAGE_DELETE_FOO = '1001'
 
# And so on..

Now the trouble is with importing it all. I can do either:

from constants import *

Which may be considered a bad style on one hand and on the other hand it becomes a mess if your IDE, like Wing IDE, autocompletes the names for you, so you type ME and then have to scroll through several dozens of them. Not to mention that it’s hardly ever the case that all of the constants are needed in any single module.

I can also carefully pick the names I need, like so:

from constants import MESSAGE_CREATE_FOO, MESSAGE_DELETE_FOO # And so on..

But that’s yet another place that needs to be kept in sync with the rest of the code.

So I’ve finally settled on using the Bunch class, which basically is a regular dictionary with an extra attribute-style access, meaning I can now write it all like so:

from bunch import Bunch
 
MESSAGE = Bunch()
MESSAGE.CREATE_FOO = '1000'
MESSAGE.DELETE_FOO = '1001'
 
# And so on..
# Yay, we're importing one thing only, the only one needed
# in this module, and if the code below ever starts deleting
# FOO, we won't have to update the import statement!
from constants import MESSAGE
 
print(MESSAGE.CREATE_FOO)

Isn’t that nicer? :-)

@fourthrealm

Share
Categories: Software Tags: ,

ON DELETE CASCADE and LEFT JOIN in SQLAlchemy

October 10th, 2011 Comments off

Here’s another one in the hoping-it-saves-someone-at-least-half-an-hour-worth-of-searching-around category :-) which shows nothing but how to define an ON DELETE CASCADE constraint and then how to issue a LEFT JOIN in SQLAlchemy.

# -*- coding: utf-8 -*-
 
# SQLAlchemy
from sqlalchemy import create_engine
from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import backref, relationship, sessionmaker
 
Base = declarative_base()
 
# The definitions, note the use of both 'ondelete' and 'cascade'.
 
class Mom(Base):
    __tablename__ = 'mom'
 
    id = Column(Integer, primary_key=True)
    name = Column(String(60))
 
class Daughter(Base):
    __tablename__ = 'daughter'
 
    id = Column(Integer, primary_key=True)
    name = Column(String(60))
 
    mom_id = Column(Integer, ForeignKey('mom.id', ondelete='CASCADE'), 
                nullable=False)
    mom = relationship(Mom, 
        backref=backref('daughters', cascade='all, delete, delete-orphan'))
 
class Son(Base):
    __tablename__ = 'son'
 
    id = Column(Integer, primary_key=True)    
    name = Column(String(60))
 
    mom_id = Column(Integer, ForeignKey('mom.id', ondelete='CASCADE'), 
                nullable=False)
    mom = relationship(Mom, 
                backref=backref('sons', cascade='all, delete, delete-orphan'))
 
# Create an in-memory database.
engine = create_engine('sqlite:///:memory:', echo=True)
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()
 
# Let's find out how LEFT JOIN works, note the usage of .outerjoin
 
mom1 = Mom()
mom1.name = 'Molly'
 
mom2 = Mom()
mom2.name = 'Sarah'
 
mom3 = Mom()
mom3.name = 'Martha'
 
daughter = Daughter()
daughter.name = 'Matilda'
daughter.mom = mom1
 
son1 = Son()
son1.name = 'Robert'
son1.mom = mom1
 
son2 = Son()
son2.name = 'Tom'
son2.mom = mom2
 
session.add_all([mom1, mom2, mom3])
session.commit()
 
session.delete(mom2)
session.commit()
 
# All moms and their children.
rows1 = session.query(Mom.id, Mom.name, 
            Daughter.name.label('daughter_name'),
            Son.name.label('son_name')).\
              outerjoin(Daughter, Mom.id==Daughter.mom_id).\
              outerjoin(Son, Mom.id==Son.mom_id).\
              order_by('mom.name').\
              all()
 
# Sarah doesn't like us anymore so Tom should be gone as well.
rows2 = session.query(Son.id, Son.name).all()
 
for row in rows1:
    print(row)
 
print('')    
 
for row in rows2:
    print(row)

As expected, the result is

(3, u'Martha', None, None)
(1, u'Molly', u'Matilda', u'Robert')

(1, u'Robert')

We can also confirm the SQL code that’s being generated – notice the ON DELETE CASCADE clause ..

CREATE TABLE mom (
	id INTEGER NOT NULL, 
	name VARCHAR(60), 
	PRIMARY KEY (id)
)
 
CREATE TABLE daughter (
	id INTEGER NOT NULL, 
	name VARCHAR(60), 
	mom_id INTEGER NOT NULL, 
	PRIMARY KEY (id), 
	FOREIGN KEY(mom_id) REFERENCES mom (id) ON DELETE CASCADE
)
 
CREATE TABLE son (
	id INTEGER NOT NULL, 
	name VARCHAR(60), 
	mom_id INTEGER NOT NULL, 
	PRIMARY KEY (id), 
	FOREIGN KEY(mom_id) REFERENCES mom (id) ON DELETE CASCADE
)

.. and the LEFT JOIN is here indeed.

SELECT mom.id AS mom_id, mom.name AS mom_name, 
    daughter.name AS daughter_name, son.name AS son_name 
FROM mom 
    LEFT OUTER JOIN daughter ON mom.id = daughter.mom_id 
    LEFT OUTER JOIN son ON mom.id = son.mom_id
ORDER BY mom.name

@fourthrealm

Share
Categories: Software Tags: , , ,

If version control systems were vehicles…

October 2nd, 2011 Comments off

Here’s a short visualisation of how I perceive various revision control systems, given in the order of my being exposed to each of them throughout all those years.

BitKeeper

 

Many winters ago, it was the first RCS I used and I remember it was pretty powerful yet somewhat clumsy to steer and handle not to mention the thick armour that wouldn’t let me peek inside and have a look at its source code. I hear it’s still being sold to customers around the world who don’t mind to trade a little bit of the manoeuvrability in exchange for its main gun’s fire-power.

CVS

 

A funny-looking though not really forgiving system that would bring a lot of projects home. The crucial point was its low price (opensource) yet the lack of features (like no real ‘reverse gear’, one couldn’t easily delete empty directories) meant I was looking for something else pretty soon.

Subversion (svn)

 

Now, I spent a couple of years working with svn for various corporate clients and it’s clear that’s what the prevailing majority of them can easily understand. It’s a centrally-managed system with no push and pull steps like in those fancy modern distributed ones and usually does its job sufficiently well. The feature most important for all the selfish ‘enterprise’ clients is that it has a built-in lock command with the power to break the locks if need be.

Bazaar (bzr)

 

A very versatile modern system whose only issue was the maker’s marketing department who in the system’s early days wasn’t able to stop warning people that features were first and the speed advancements would come later so in the end pretty much everyone still thinks the system’s slow. Well, it isn’t. It’s called being ‘fast enough’ my friend. Oh, and the maker’s move to hire a couple of experienced user interface designers means the system’s a pure pleasure to use with no surprising commands required for everyday usage.

Mercurial (hg)

 

Another system that’s good all-around, fast, fuel efficient and very comfortable to drive. Doesn’t have as many fans as it deserves to because it’s not called Volkswagen Golf even though 99% of the features are precisely the same.

Git

 

So now it’s git. A prime example of why space rocket engineers shouldn’t really design end user-facing technologies. It’s fast and that’s all one can say. There’s no hood so all of the gory details are left exposed and you’re supposed to deal with it (like it’s natural that one needs to check out two consecutive dashes when there’s a need to revert something). Just notice the number of times the words ‘unusual’, ‘surprising’, ‘unexpected’ get mentioned in books on git. Really, the only thing that makes git usable is GitHub, which indeed is a very good service, like git done correctly only in the browser.

I guess you can easily tell who my heart is with, eh? ;-)

@fourthrealm

Share
Categories: Software Tags: , , , , , ,

Inserting new ‘<tr>’ and ‘<br/>’ elements with PrototypeJS

October 1st, 2011 Comments off

Apparently one can’t insert two and more ‘<tr>’ elements into a ‘<tbody>’ using PrototypeJS 1.7.0.0 and 1.6.0.3 if there’s also a ‘<br/>’ element involved in the conspiracy so that code simply won’t work at all:

var new_content = 'foo
bar';
$('my-tbody').insert(new_content);

One needs to leave out the poor ‘<br/>’ and I guess that applies to inserting ‘<br/>’ in general, not only in this situation but thankfully right now I only need to dynamically add new table rows not line breaks..

@fourthrealm

Share
Categories: Software Tags: ,