Opening a file in Python

I'm sure I read this somewhere recently, but my scratchy memory and command of Google can't bring it back to me.

Is there a Python idiom for accepting either a file name or a file object as a function parameter?

The closest I can get is this;

def my_function(file_name_or_object):



except TypeError:

    file = file_name_or_object

return file

Any improvements on this are more than welcome.

Trouble Getting a Date

I'm having trouble with dates. This can be summed up in a couple of high level issues;

1. Date support in relational databases is insane, or at the best inconsistent.

As far as I can tell the ANSI SQL-92 standard defines date, time, interval and timestamp data types. Which doesn't help when SQL Server only implements something called 'datetime' - at least I think so, have you tried accessing any sort of manual for a Microsoft product online? Blimey, I thought billg had embraced this web thing years ago. Oracle has the 'date' data type (which is actually a time stamp) and MySQL, well they've gone and outdone everyone by implementing DATETIME, DATE, TIMESTAMP, TIME, and YEAR.

2. The Python DB-API does not cope with date data type ambiguity well.

When it comes to the date question the Python DB-API states (and I quote) " ... may use mx.DateTime", which if you ask me isn't much of a standard. This needs to change so that all DB-API modules return consistent datetime objects, not such a big issue as datetime has been part of the standard library since, what, Python 2.3?

Sadly even if we fix this it won't work with Sqlite as it doesn't consistently support data typing. In my experiments regardless of what sort of date you insert into the database you get a unicode string back. Don't believe me? Try this in Python 2.5;

>>> from sqlite3 import dbapi2 

>>> db = dbapi2.connect('test_db')

>>> cursor = db.cursor()

>>> cursor.execute('create table date_test (id integer not null primary key autoincrement, sample_date DATE NOT NULL)'

>>> stmt = "INSERT INTO date_test (sample_date) VALUES (?)"

>>> cursor.execute(stmt, (1234, ))

>>> import datetime

>>> cursor.execute(stmt, (, 3, 10), ))

>>> cursor.execute(stmt, ('My name is Earl', ))

>>> db.commit()

>>> cursor.execute("SELECT * FROM date_test")

>>> results = cursor.fetchall()

>>> for item in results:

...     print item[1], type(item[1])



My name is Earl 


But note that it is fine for integers.

3. The people writing the Python standard library modules are on crack.

Outside of the database world and within the batteries included Python standard library some modules use datetime, others time and there are even uses of calendar.

O.K. I'll accept that maybe the module authors aren't on full strength crack, because the time module just exposes underlying posix functions. But the people who wrote those were on something strong and hallucinogenic. I table the following function signatures from section 14.2 of the Python Library Reference 2.5 as an example;

strftime(format[, t ]) 

strptime(string[, format ])

This has bitten me twice in the last twenty four hours and frankly I'm not happy.

I appreciate that there are historical reasons for having inconsistent function signatures but can someone please fix this in Python 3.0. All we need is a single module that can access the underlying system clock and then convert between a number of different representations of that and other epoch driven dates. How hard can it be? As far as I can tell this is not part of the proposed standard library re-organisation. I think it should be.

May Sydney Python Meeting

This Thursday, the 1st of May, 2008 from 6:30pm there will be a social gathering of the Sydney Python Users Group and any individuals interested in discussing Python, Web, Ruby, Perl etc.

Laptops, OLPC's, code review, show and tell etc allowed and encouraged.

We meet in the ground floor area next to P.J. O'Briens Pub internal entrance in the;

Grace Hotel at the corner of York and King Street in Sydney, New South Wales 2000. See you there

Small Administrative Note

As the feedback I got on the daily twitter posts was entirely negative they are gone. Sorry for that folks.

It seems that I'll have to write more original content instead, I will see what I can do.

Twitter Updates for 2008-04-24

    <li>Dodging the rain by ducking into cafes <a href="">#</a></li>
    <li>@alang I'm there. With bells on. <a href="">#</a></li>
    <li>Another skinny cappuccino? Oh alright then. <a href="">#</a></li>
    <li>Getting the barbecue ready? <a href="">#</a></li>
    <li>Preparing the barbecue to burn a load of meat and fish <a href="">#</a></li>

    Powered by Twitter Tools.

    Whither Relational Databases?

    Following on from a theme that Simon has been pursuing here is an interesting piece - How SimpleDB differs from a RDBMS. A thorough analysis of SimpleDB, but I think the extra value here is in the comments. I particularly liked Greg Jorgensen's submission that programmers just don't like RDBMS because they take some learning. Whilst I don't have empirical evidence to back up this supposition I can say that most Java programmers I've come across go slightly green if you suggest that they can solve most problems with a SQL statement (and yes, that was meant to be read ironically).

    If I can sum up the message of this post and it's comments it is that we should be thankful for having different tools available to us, because this isn't a one size fits all world. Where you've got a big list of simple things (tm) tools like BigTable and SimpleDB work well. Where you've got large pieces of unstructured data (sometimes referred to as 'documents') you can use CouchDB, and where you have complex, structured data that has to adhere to certain validation and usage rules use a relational database. Each of these will store up to terabytes of information so let's not even talk about (the myth of) scalability. Choose the right tool for the job and stop insisting that every problem is a nail.

    So to answer my question from the title of this post - still around, and still kicking arse.

    Fiddling not Blogging

    Whilst there has been a dearth of decent posts here recently I have been beavering away in the background. In the meantime, I've upgraded to WordPress 2.5 and installed the Twitter Tools plugin. This joins the wonderful worlds of Andrew Channels Dexter Pinion and Twitter into a value added multi media powerhouse.

    Now instead of large amounts of peace and quiet you will be treated to a daily summary of my inane babbling here on the blog and in the feed. Who said technology was no good?

    Command Line Meme

    Inspired by Simon, I thought I'd follow the crowd;

    andy47@Mort:~$ history|awk '{a[$2]++} END{for(i in a){printf "%5dt%sn",a[i],i}}'|sort -rn|head

    187 cd

    66 whoswho

    46 svn

    30 python

    20 rm

    16 ls

    15 open

    13 vi

    13 grep

    10 ipython