Nidelven IT - All about Python, Zope & Plone - and Open Source!

Here you'll find issues related to our services. Mostly about Python, Zope and Plone, as well as hosting-related issues.

"Keeping IT real"






Older entries



Atom - Subscribe - Categories
Previous | Next

Minimalistic Python, Python as a system administration language

Some months ago there was a leap year day and I didn't have a lot to do right then, so I was following a guy on Twitter that is into command line tricks. So I started writing an as small as possible script that could display leap years and would fit in a Twitter message.

Python has always been called a scripting language I guess, but I've used it for serious programming for many years, dabbled a bit with Perl and other things but Python is the language I'm most comfortable with. Javascript is a language I work with now and then, it has its uses in web pages and apps, although I've been daydreaming at times about writing a Python interpreter in Javascript. [:P

Anyway, after some iterations refining the code I ended up with this:

echo "r=range(-8,2020,4);l=lambda y:((y in r and (y%100 or not y%400)) and str(y));print filter(None,[l(y) for y in r])"|python

[Edit: It could be a lot shorter, but takes into account more advanced rules for leap years]

It's something that should work instantly on most recent Linux distributions (all have Python don't they?) The python command at the end accepts what's sent through a pipe with echo, and with some builtin Python functions, a lambda function, modulo, Python's evaluate object to boolean and list comprehensions features one can build such a script (program) in a limited amount of space. There are only 127 characters that could go into any shell.


[Permalink] [By morphex] [Python-only or > 0.5 Python related (Atom feed)] [2012 08 May 15:17 GMT+2]

Comments

More portable

By: Matt Williams

More portable:
  python -c"r=range(-8,2020,4);l=lambda y:((y in r and (y%100 or not y%400)) and str(y));print filter(None,[l(y) for y in r])"

-2 Magoo!

By: Greg A

python -c"r=range(-8,2020,4);l=lambda y:(y in r and (y%100 or not y%400)) and str(y);print filter(None,[l(y) for y in r])"

[http://www.nntp.perl.org/group/perl.golf/2002/09/msg1681.html]

shorter

By: bkc

echo "r=range(-8,2020,4);print [str(y) for y in r if (y%100 or not y%400)]"|python

more golf

By: Greg A

PS. why str()?
I like:
python -c"r=range(-8,2020,4);l=lambda y:(y in r and(y%100or not y%400))and y;print[l(y)for y in r if l(y)]"

More of the same

By: Greg A

python -c"print[y for y in range(-8,2020,4)if y%100or not y%400]"

Use the stdlib Luke

By: bmispelon

python -c "import calendar; print filter(calendar.isleap, range(-8, 2020, 4))"

Wow

By: Morten W. Petersen

Hey guys, thanks for all the comments. :)

Matt, yes - that is indeed more portable. I think I tried to use something like that but ended up using an echo to stdin.

bkc, a lot shorter, now I know about that.

Greg, the str is there to always print the year (year 0 needs it IIRC). But now it looks like it is unecessary.

bmispelon, that's the smartest one I've seen yet ;)

bmispelon was faster

By: Henning

I wanted to suggest

echo "print [year for year in range(-8,2020) if __import__('calendar').isleap(year)]"|python

Good one

By: Morten W. Petersen

Ah, good one. At first glance I thought __import__ for each year in the range would such up a lot of time, but I guess it is only really "heavy" on the first pass.


Add comment (text format)

Passphrase

A passphrase is required to comment on this weblog. It is required to make sure that bots aren't doing automatic spamming. It is: nit is the best!.

Title

Name

Email

Comment