Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Wednesday, December 28, 2011

a piece of py: renaming files

I hate spaces in the names of files. It's a quirk of my personality. I prefer CamelCase as an alternative. I used to like a mix of lower case letters, hyphens and underscores, but that was before my world of Unix and Python was dominated by Java.

However, I am a member of the minority and so I often end up with folders and files whose names aspire to be sentences (or at least parts of speech like nouns, prepositions, pronouns and the occasional verb [To be DELETED: ugh!). Today was one of those days. I had a folder full of PDFs, each with a name of the form Last, First.pdf. I would have preferred a name like FirstLast.pdf. The easiest way to do this was with some shell-fu or Python-fu (with due apologies to the GIMP). I tossed a coin and chose Python. I fired up PythonWin and started working on fragments that would eventually unite into a single one-liner to rename them all (sorry JRR). I wasn't smart enough to reduce it all to one line (I needed the imports and I was not a lambda knight).

import fnmatch, os
# basedir is a string representing the full path to the folder
# containing the poor PDFs
for pdf in fnmatch.filter(os.listdir(basedir), '*.pdf'):
  new_name = ".".join(["".join(reversed(pdf.split('.')[0].split(', '))), 
                      pdf.split('.')[1]])
  os.rename(os.path.join(basedir, pdf), os.path.join(basedir, new_name))

Of cours,e I could have skipped using new_name completely:

import fnmatch, os
# basedir is a string representing the full path to the folder
# containing the poor PDFs
for pdf in fnmatch.filter(os.listdir(basedir), '*.pdf'):
  os.rename(os.path.join(basedir, pdf), 
         os.path.join(basedir, 
                 ".".join(["".join(reversed(pdf.split('.')[0].split(', '))), 
                 pdf.split('.')[1]])))

I think this might be the first time I used reversed(), but since it was a relatively recent addition (2.4 to be precise), I didn't feel too bad. Had I written this code before 2.4 had been released, I would have been forced to use reverse(): this also reversed the list, but did it in place and did not return the list; I would be forced to reverse the list first and then send it onto the join conveyor belt.

I also like how just adding brackets as bookends to items separated by commas, I get a list:

["".join(reversed(pdf.split('.')[0].split(', '))), pdf.split('.')[1]]

Mission accomplished. With some learning to boot. Not bad for a few minutes of work.

Wednesday, April 06, 2011

linky dinky

The Wall Street Journal has an article that tells us things we already know: India churns out a lot of graduates from a system that is rife with insufficient funding, peppered with corruption and laced with stale knowledge; these graduates don't really cut it when it comes to the jobs that have flooded the market and contributed to all the "prosperity" the nation seems to be enjoying (the call centre kind). I liked what Vijay Thadani of NIIT had to say: If you pay peanuts, you get monkeys.

I have been learning and using git for quite some time now and my experience with the DVCS (although not as D as I would have hoped it would be) has been fun so far (endorsements from various open source projects and enthusiastic presentations from the likes of Matthew McCullough have also helped). Windows is still a secondary platform for it and EGit offers IDE integration but is still in incubation. That's why I always wondered about that other DVCS that I had heard people talk about: Mercurial. I had heard about it first from a friend at Sun as they were moving to it. That was probably when the OpenJDK project moved over to Mercurial as well. Mercurial is written in Python and when Python moved over to Mercurial, it seemed to make a strange kind of sense (the dogfood kind). The reasons, however, were more interesting and backed by some thorough investigation. Simply put, the team chose something that would meet the needs of the developers as much as possible. The articles about the journey from PEP 374 to PEP 385 have been very interesting. They will also help me learn how to use Mercurial and MercurialEclipse.

Friday, January 02, 2004

search clustering

back in school, I had worked on a project exploring different approaches to clustering search engine results. nothing revolutionary happened (no startup, no IPO, etc etc). Now, there's a new clustering interface out there. It's called Mooter. and my favourite little test search (python) yields results that strike a heavy riff of déjà vu. {courtesy: researchbuzz}

Wednesday, October 15, 2003

misclynx (of a technical nature and then some)

python is a time bomb (in a good sense): "You didn't really mean to write it in Python, you say, it just happened." | open-source projects have a better sense of humour than corporate bloatware sites. case in point, nude pictures on the MiddleGen site | jon udell discusses software reuse, patterns and antipatterns (Dilbertian design patterns ... In the old days, we used to just call these 'bad ideas') ... and also revisits Nigerian spam with birthday greetings | time to link to ray ozzie's much-cited recent blog post on saving the browser (ref: the Eolas lawsuit against MSFT) | and another lesson in commenting code | using an agile software process with offshore development

{from The Guardian}:
The Catholic Church is telling people in countries stricken by Aids not to use condoms because they have tiny holes in them through which HIV can pass - potentially exposing thousands of people to risk.

The church is making the claims across four continents despite a widespread scientific consensus that condoms are impermeable to HIV.
... The WHO has condemned the Vatican's views, saying: "These incorrect statements about condoms and HIV are dangerous when we are facing a global pandemic which has already killed more than 20 million people, and currently affects at least 42 million.".

Friday, September 26, 2003

upside peter, imaginary book list and more

Apparently many of us are incapable of figuring just how incompetent we actually are ...

Jarno Virtanen has a wishlist of imaginary Python books (call it a wish-wishlist if you will).

Friday, May 23, 2003

friday feeds {Aside: How I wish this could be automated!!}

Served up piping hot: a selection of articles from all the feeds I read (when I get some time off work, of course)

Write and Get Fired (being a link to an account of one librarian fired from being a Google Researcher) | Prime Numbers in Python | Tim Bray on the Death of Scholarship (...when students are asked to look up something relevant to their academic work, 45% of them go to Google, 10% of them go to the local library catalog, and the rest scatter among other search engines...) | Whitespace is a language that works on the following principle: Any non whitespace characters are ignored; only spaces, tabs and newlines are considered syntax (PS: note the date of the posting)

Tuesday, May 20, 2003

for the day items

Debate: Is PageRank an OPINION? Google says NO

Social Software Idea: a whuffie is reputation currency that you earn as people credit you out of respect, gratitude, pity, lust, love, or any other motive for popularity. {the whuffie blog}

Invention: Cubicles

TODO: Index and search Outlook email {bamf moment: There's a Python port of Lucene called LuPy {O'Reilly article}

Exhaustive diatribe-cum-introduction: Generators
zeitgeist info-viz

The Python Community Server now has a search engine referral zeitgeist, courtesy of Georg Bauer, who was inspired by Everything Burns. People interested in wiring up something similar for their sites would be interested in the code for the Search Referral Zeitgeist. It looks really cool, and is a great usability test example for an Info-Viz class.
misc-e-rama

Mark Pilgrim decided to put together PyTechnorati, the inevitable Python wrapper for the Technorati API {previous post} | at long last and procrastination galore, I've decided to dive into the sea of RSS aggregation. I'm configuring Amphetadesk (my choice of news reader). Next up, adding channels furiously, to get myself a single page every day to monitor. | The Dano project at Blogger promises some interesting features including new template tags for archives, site feeds (wonder if this is only for premium members), owner metadata. The question (as always) is: when will things stablize? They're still running Microsoft-IIS/5.0 on Windows 2000 [Information courtesy of Netcraft]

Friday, April 12, 2002

Google API beta release
Google just released a beta of their Web APIs that will allow programmers to programmatically search the Web and build interesting applications. DaveNet @ Userland has jumped to it with an article detailing the different uses for it. {original slashdot post}

Using the Google API with Radio and Frontier
SoapWare's list of Google API implementations
O'Reilly Network: Google Web API.
PyGoogle: A Python Wrapper for the Google API
Google Ego Trip



This has even become the top item on DayPop

Friday, April 05, 2002

Lambda lists an old article Python Patterns - An Optimization Anecdote by Guido van Rossum. Still worth reading.

Wednesday, March 13, 2002

Some great news from comp.lang.python: David Mertz has made drafts of his forthcoming book "Text Processing in Python" available.

Tuesday, March 12, 2002

Python Plug for the Day
In case you have been nursing thoughts of becoming a Python developer (not a writer of Python code, but a contributor to the Python core), read this article by Andrew M. Kuchling.
And on that note, Mark Hammond, the writer of the Win32 extensions for each release of Python, is reported to be working on bringing Python into Microsoft's .NET realm.
For anyone interested in finding the times for sunrise and sunset in the US, check out this Sunrise/Sunset Calculator from the NOAA Surface Radiation Research Branch. For Python enthusiasts like me, here's some code from Mike C. Fletcher. Thanks to comp.lang.python for this.

Tuesday, February 26, 2002

Trying to convince your colleagues and managers to switch to Python? Google has always been using Python. Check out the original Google paper, their hiring page, or the First Annual Google Programming Contest. For more coal to burn in advocacy, try Mark Lutz's Python advocacy page or the Python advocacy how-to.

Movie for the night: KAALA SONA
Ravee Nagaich's (unintentionally) hilarious, (unintentionally) risque desi take on the Westerns is rich with inappropriate dialogue, bizarre cuts, awkward camera takes, incongruous close-ups, numerous fish-eye shots and has everyone vying for a hamming title. Starring suave Feroz Khan, miscast Parveen Babi (she looks out-of-place in traditional Indianwear and cannot shake a leg), dimunitive (the camera emphasizes this sometimes rudely and often hilariously) bubbly Farida Jalal, Danny Denzongpa (who even sings a duet for himself in the film with Asha Bhosle) a one-eyed Prem Chopra (as Poppy Singh) and includes inconsequential cameos by Helen (with a well-tuned badly picturised dance number), Satyen 'now you see me, now I'm dead' Kappu, Jairaj ('did you miss me? I'm dead') and an ever-inebriated Keshto Mukherjee playing an aptly named character called Sharaabi. Rakesh (Khan) abandons his lifestyle as a playboy and heads north on a horse to avenge his father's murder. The killer Prem Chopra is also responsible for a massacre in the village and the disappearance of Chetan, the son of Thakur Randhir Singh and little brother of Durga (Babi) and Bela (Jalal). Rakesh runs into Shera (Danny Denzongpa) and gains an ally and disciple in his quest for revenge. Along the way, Rakesh finds time to win Durga's heart, Bela and Shera fall in love (per force, if I may add), and there is an anti-climactic tail-ender that is both unnecessary and irritating. Good tunes by R. D. Burman, although picturisations are hilarious and often jaw-dropping. A definite candidate for the B-movie library, this film includes numerous instances of people hurling other people into the air or onto the ground without a 'by your leave'. Strange!

Daily Readings for Lent
Isaiah 1:10-20
Matthew 23:1-12

Sunday, February 24, 2002

Python has been my favourite programming language for quite some time now. Referred to by some as a competitor to Perl, the epitome of terse obfuscation, Python is endearing for its readability, modularity and mix of imperative and object-oriented programming paradigms. So, what about Java? I have tried to learn Java several times, but I probably started the wrong way. It's no surprise then that Mr. Preprocessor's page on the language that I stumbled on would tickle my funny bone.
 
Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.