Latest |Kites |Pictures |Programming |Life
Programming
Hacking code together. Is it art? Is it science? Can it be quantified? Are comments really essential? Who knows, I sure as hell don't.
Python chain methods

I don't know why I didn't know this, but you can chain methods in Python. I use this quite a bit in PHP and Zend Framework uses it a little. But here it is in Python:

class table_model:
    def __init__(self):
        pass

    def connect(self):
        print 'connect to DB'
        return self
       
    def run_query(self):
        print 'run query'
        return self

    def get_last_result(self):
        return 'Hello'


table = table_model()
res = table.connect().run_query().get_last_result()
print res

>>>
connect to DB
run query
Hello

 

That's it. Job done.

14th of August, 2008@2:34:47 PM
add a comment, permanent link to article
MySQL max of two columns

How to get the MAX or MIN value of two columns: GREATEST( col_1, col_2 )

Or a more interesting example. I have a client who may have some reports. I need to list the clients in 'most recently active' order. So if they have had a recent action they should be near the top of the list, or if they have recently had a report they should be near the top of the list:

SELECT clients.id, clients.name, clients.date, MAX(r.date), GREATEST(clients.date, IFNULL(MAX(r.date), clients.date)) AS date
FROM clients
LEFT JOIN reports r on r.clients_id=clients.id
GROUP BY clients.id
ORDER BY date DESC

So this uses MAX to get the most recent report (this may return NULL if they have no reports) then I use GREATEST to choose the most recent of the two dates (two columns). Job done.

 

11th of August, 2008@4:54:51 PM
add a comment, permanent link to article
since I put

Google analytics, page views from Feb 2007 to July 31 2008:

2,405,706

 

31st of July, 2008@11:29:06 PM
add a comment, permanent link to article
Part of the problem

Part of the problem is that programming is hard to teach. “Programming is a mixture of a highly technical skill and an aesthetic art. And that’s a very difficult combination.”

Not really. It's only a problem if you care...

30th of July, 2008@10:20:36 AM
add a comment, permanent link to article
Python static method staticmethod

Tagged: @staticmethod, staticmethod, class, def, static, static method, static variable, python, def, __init__, self

OK, I'm always forgetting how to add static methods to Python classes, so:

class C:
    val = 99

    def __init__(self,x):
        self.x = x

    @staticmethod
    def set_val(val):
        C.val = val

    @staticmethod
    def get_val():
        return C.val


a = C('a')
b = C('b')

print C.get_val()
print '-'*5

a.set_val(88)
print C.get_val()
print a.get_val(), a.x
print b.get_val(), b.x
print '-'*5

b.set_val(69)
print C.get_val()
print a.get_val(), a.x
print b.get_val(), b.x

 

>>>
99
-----
88
88 a
88 b
-----
69
69 a
69 b

Job done.

 

29th of July, 2008@10:00:05 AM
add a comment, permanent link to article
PHP Closeures

PHP 5.3 has closures in it! Finally we get some good stuff to play with: PHP 5.3 and Closures. This is excellent news, even if the closures look weird -- look at the strange use keyword.

 

22nd of July, 2008@11:33:41 AM
add a comment, permanent link to article
Zend Framework DB Insert NOW

Some keywords: SQL Insert Update Zend_Db_Table_Row NOW() now function Zend_Db_Table Zend_Db_Expr

Use the SQL function NOW() in ZF:

$k = new My_Table();
$row = $k->createRow();
$row->datetime        = new Zend_Db_Expr('NOW()');
$row->save();

This works because DB Expr doesn't escape the string 'NOW()'. Yay.

15th of July, 2008@3:29:22 PM
add a comment, permanent link to article
Viewing page 2 of 18.
Previous page, next page.
Unique hits [] : Total hits [] : Server Grind [0.0348 seconds]