Python kicks ass
October 18, 2007One-liner to sum up a list of times in the format MM:SS
sum([m* 60 + s for m,s in [[int(j) for j in z.split(':')] for z in [i.strip() for i in sys.stdin.readlines()]]])
Marking my territory with digital feces.
One-liner to sum up a list of times in the format MM:SS
sum([m* 60 + s for m,s in [[int(j) for j in z.split(':')] for z in [i.strip() for i in sys.stdin.readlines()]]])
The trouble with python is, it’s too easy.
I have been procrastinating from working on a little script I needed– to split up files into media-sized chunks for backup– because I figured it’d be a pain in the ass.
Then tonight I started idly investigating what libraries and crap I’d need in order to make it work, i.e. os.stat(path)[7] to get the file size, etc. I copied these into an emacs buffer so I didn’t forget them.
I looked at the emacs buffer, and the little script is done.
I found this scary. It was like doing automatic writing and having a coherent story come out.
Omniorthogonal occasionally muses on the "cambrian explosion" of open source software. Damn straight.
The Open Source CMS site just stunned me. It includes live demos of all the major PHP/Apache/MySQL web software thingies: CMS'es, CRM's, blogs, Wiki's, etc.
The one that particularly caught my attention was SugarCRM. Not 10 years ago I worked for corporations that paid millions of dollars on combinations f buying, writing, and integrating what used to be called "Sales Automation" (now "Customer Relationship Management"
systems like this. That's not even including the heavy-duty server hardware it had to run on. Even smaller-scalle tools like this were still really expensive, slow, and generally sucky.
And here's one for free, and overall way ahead of the ones we had to deal with. And the "hardware" to run it on can be rented for $1/GB of bandwidth. Astounding.
I found jwz's rant about the CADT model funny when I first read it years ago, and I accepted his conclusion that the CADT model was a weakness of Free Software. I'd spent 10 years in product management and product marketing in the tech industry, so I sympathised with jwz's complaints.
But now I understand the advantages of CADT. In order to rewrite an application, you have to largely abandon old features and code, and generally do things that users, engineering managers, product managers, marketing people, and especially salespeople (under intense quarterly quota pressure) would abhor. I've nearly completed a set of CADT-style rewrites which each have had no visible effect to the users except for the addition of new bugs and user-interface complexity– at least for the long periods until the rewrites have been completed.
When I started this particular project, I was under pressure to deliver something very quickly, which had to look good to the users rather than be good in its design, and on top of that I had no idea what I was doing. The old codebase was fragile and ridden with bugs. There is no way I could have broken free of it without a CADT-style rewrite, which I couldn't have justified were the "customer" paying me. My users have been annoyed for the last few years, but then I'm doing this as a volunteer project, so they have been very patient.
I expect this CADT behaviour is respobsible for the success of jwz's own progeny, Firefox. The 1994-vintage Mozilla code that he stayed up nights blasting out the door under intense commercial pressure was, AFAICT, crap, and has reportedly been more or less completely replaced. Mozilla was not the greatest thing jwz has created, the Mozilla project was the greatest thing jwz has created. By putting it into the Open Source community, and thus freeing it from commercial pressure, he enabled the "CADT's" to largely ignore the old codebase and instead create Firefox. I think that's a long-term good.
Sure, Free/Open Source developers can be rude, fickle, arrogant, and prone to short attention spans, but so can customers, managers, and salespeople: the main drivers behind commercial software. I'd argue that F/OSS neither creates nor eliminates "attention-deficit" behaviour, it simply redistributes it. Call it the Theory of the Conservation of Capriciousness, if you like.
As a user, I have always found it frustrating that so many Free Software and Open Source projects are evolutionary dead-ends. But some codebases really are evolutionary dead-ends, and should be allowed to die. They can't be allowed to die as long as the pressure of sales quotas and job security for maintenance engineers and PHB's keeps them alive, well past the date of brain-death, comatose and with feeding tube inserted, and still breeding "new! improved!" versions. Ugly metaphor, but what can I say? Maybe CADT is a tool for software euthanasia– software eugenics? In other words, maybe the whimsy of the "market" isn't always as efficient as the whimsy of the developers.
I toiled long enough in the tech industry to witness the very common phenomenon of crappy products succeeding commercially, and well-designed products failing miserably. I have managed products of both types. At the time, I didn't know of any alternative to that model. And now, having written a fair amount of software myself, I can see how, despite its weaknesses and frustrations, a Cascade of Attention-Deficit Teenagers can in at least some cases be a more efficient means of producing quality software than its alternative: the Cascade of Attention-Deficit Salesepeople, Managers, Customers, and Stockholders.
“PHP is not a language. PHP is a thin wrapper around C library functions” — Jeff Wheelhouse
And then have to go back to doing real work in an inferior language:
$this->obj->query(
preg_replace(
array_keys($queryfix),
array_values($queryfix),
implode('',
file(
implode('/',
array(
COOP_ABSOLUTE_FILE_PATH, 'sql',
$filename))))));
Oh yeah, another example:
$this->obj->whereAdd(
sprintf(
'coalesce(%s) = %d',
implode(',',
array_map(
create_function(
'$i',
'return($i . ".family_id");'),
$last)),
$this->page->userStruct['family_id']));.
Yes, I've been corrupted by functional programming and s-expressions… and parentheses.
And these beasts could each be rendered in one or two compact, almost beautiful lines with Python list comprehensions (I wuv list comprehensions!).
Bah.