Eureka moments in Python

S

Steven D'Aprano

I'd be interested in hearing people's stories of Eureka moments in Python,
moments where you suddenly realise that some task which seemed like it
would be hard work was easy with Python.

I had a recent one, where I had spent some time creating a function which
took a list as an argument, and then rearranged the items according to
what magicians call a Monge shuffle. I had started off with a piece of
code that walked the list, dropping items into two new lists, then
combining them again. I got the code down to eight lines or so, something
like this:

# from memory, untested
top, bottom = True, False # not strictly needed, but helps document code
where = bottom
holder = {top: [], bottom: []}
for item in reversed(alist):
holder[where].append(item)
where = not where
holder[bottom].reverse()
print holder[bottom] + holder[top]


And then it suddenly struck me that I could do it as a one-liner:

alist[1::2] + list(reversed(alist[0::2]))

Or if I wanted to be even more concise, if slightly less readable:

alist[1::2] + alist[0::2][::-1]

If only a real Monge shuffle with actual cards was so easy...
 
B

Brett g Porter

Steven said:
I'd be interested in hearing people's stories of Eureka moments in Python,
moments where you suddenly realise that some task which seemed like it
would be hard work was easy with Python.

Mine was definitely when I was first working with the xmlrpc module, and
I was puzzled to no end how it was that I was able to make a call to a
method of an object when that method didn't exist. All the talk about
Python being a dynamic language had bounced off of me until that moment,
when I walked through the __getattr__ method in the debugger and was
enlightened.

Not surprisingly, that was also the moment when C++ started feeling like
a straightjacket to me...
 
D

Dustan

Mine was definitely when I was first working with the xmlrpc module, and
I was puzzled to no end how it was that I was able to make a call to a
method of an object when that method didn't exist. All the talk about
Python being a dynamic language had bounced off of me until that moment,
when I walked through the __getattr__ method in the debugger and was
enlightened.

Not surprisingly, that was also the moment when C++ started feeling like
a straightjacket to me...

Started? Meaning there was a time when it DIDN'T feel like a
straightjacket? If I were a journalist, this would be all over the
news...
 
M

mensanator

I'd be interested in hearing people's stories of Eureka moments in Python,
moments where you suddenly realise that some task which seemed like it
would be hard work was easy with Python.

## I have this problem where, given a list of non-zero
## positive integers (sv), I need to calculate threee constants
## x, y, z where
##
## x is 2 raised to the power of the sum of the elements in sv,
##
## y is 3 raised to the power of the count of the elements in sv,
##
## and
##
## z is 3**a*2**A + 3**b*2**B + ... + 3**m*2**M + 3**n*2**N
##
## where a,b,...m,n are 0,1,...len(sv)-2,len(sv)-1 and
## N is the sum of all elements of sv except the last one,
## M is the sum of all elements of sv except the last two,
## ...
## B is the sum of all elements of sv except the last len(sv)-1,
## A is the sum of all elements of sv except the last len(sv).
##
## This turned out to be one of those things that's
## easier to code than to describe.
##
## And if you saw my original Excel spreadsheet where I
## developed it, you'd say Eureka too.

def calc_xyz(sv):
x = 2**sum(sv)
y = 3**len(sv)
z = 0
for i in xrange(len(sv)):
z += 3**i * 2**sum(sv[:-(i+1)])
return (x,y,z)

sv = [1,2,3,4]
print calc_xyz(sv)

## (1024, 81, 133)
 
B

Brett g Porter

Dustan said:
Started? Meaning there was a time when it DIDN'T feel like a
straightjacket? If I were a journalist, this would be all over the
news...

If you're born wearing a straightjacket, I'm sure that it feels natural
and fine until the first time someone unties it...
 
B

Ben Finney

Steven D'Aprano said:
I'd be interested in hearing people's stories of Eureka moments in
Python, moments where you suddenly realise that some task which
seemed like it would be hard work was easy with Python.

I don't recall the exact context, but Python was the language that
introduced me to the power of a unified type system.

The application I was writing was doing a whole lot of bookkeeping of
information about what kind of data was being processed, which had to
be kept updated in parallel with the data itself. Once I realised that
"unified type system" and "first-class objects" meant that I could
simply store the *type itself* as data, whether a basic type or one of
my own, things became astoundingly easier. Soon after that I learned
that functions are also objects, and I was hooked.

I also don't think I would have been at all interested in Test-Driven
Development if I wasn't already familiar with Python's object model,
since it makes introspection and unit-test stubs and shims a
breeze. So I lay a lot of the blame on Python for getting me test
infected :)
 
A

Alex Martelli

Steven D'Aprano said:
I'd be interested in hearing people's stories of Eureka moments in Python,
moments where you suddenly realise that some task which seemed like it
would be hard work was easy with Python.

As a part of my process to decide whether Python was the right language
for me to use, right after reading the 1st edition of "Learning Python"
hot off the presses, I decided to invest a weekend getting a start on a
specific task -- a web app (CGI) to compute conditional probabilities of
suit divisions (for many possible conditions) in contract bridge.
Hard-coded English output was to be the "first iteration" -- then in a
second iteration, besides adding fancy conditions, I'd develop some
templating system and pick the language to use from the browser to
switch to an appropriate directory of templates for the user's language.

I didn't really think I'd finish the first iteration in the weekend, but
the conditions were favorable: wife and kids were out of town and the
larder well stocked with coffee, prosciutto, parmigiano, and bread. So,
I could hack for a few hours before falling asleep on Friday night, and
about 16 hours each on Sat and Sun -- a total of 36 hours or such should
let me at least put some serious dent on the problem, if this language
did prove to be the right one for me.

I got home around 8pm), made myself prosciutto sandwiches, and got
going.

At about 4pm on Saturday (despite having gotten a normal night's sleep
in the meantime), I looked at what I had so far... the system as
originally envisioned, completed, with all kinds of conditions coded,
and working just fine, the general-purpose templating system all done
too as well as English, Italian and French templates to try it out, a
nice battery of unit-tests and integration-tests, and lots of
optimizations (such as, memoization for the factorial function) already
integrated too.

I had to face the facts: I had achieved in about 12 hours (including
time to brew myself coffee, prep sandwiches, and stroll to the bar for
an espresso to wake me up in the morning) over twice as much as I
thought, somewhat optimistically, I could have gotten done in 36. And
that, in a language completely new to me, with nobody else around to
help me out (at that time, I didn't have an always-on net connection,
and I hadn't dialed in during those hours either). [[the application
domain _was_ familiar to me, but I had factored that in as part of my
estimate of how long the whole task would take]].


_That_ was the "Eureka moment" that sold me on this newfangled
language...!


Alex
 
P

Paul McGuire

I'd be interested in hearing people's stories of Eureka moments in Python,
moments where you suddenly realise that some task which seemed like it
would be hard work was easy with Python.

The day I wrote a CORBA method intercepter in 8 lines of code using
__getattr__ to catch 2 or 3 interesting method calls out of 20 or 30
defined in the IDL. The interesting methods I handled, and the rest I
just forwarded to the remote CORBA object. Unfortunately, the client
expected this to be an expensive job, and I was only able to bill him
for 1/2 an hour (I had to test the code in addition to writing it).

-- Paul
 
J

Jarek Zgoda

Ben Finney napisa³(a):
I don't recall the exact context, but Python was the language that
introduced me to the power of a unified type system.

The application I was writing was doing a whole lot of bookkeeping of
information about what kind of data was being processed, which had to
be kept updated in parallel with the data itself. Once I realised that
"unified type system" and "first-class objects" meant that I could
simply store the *type itself* as data, whether a basic type or one of
my own, things became astoundingly easier. Soon after that I learned
that functions are also objects, and I was hooked.

This was exactly the same "eureka" in my case too -- functions are
objects. I can store in a tuple the function and its arguments, then the
only thing I have to do is to call first element with the rest of
elements as arguments. This code now scares people in my former job --
being Java and C++ heads they just cann't get it... ;)
 
S

Sion Arrowsmith

Paul McGuire said:
I'd be interested in hearing people's stories of Eureka moments in Python,
moments where you suddenly realise that some task which seemed like it
would be hard work was easy with Python.
The day I wrote a CORBA method intercepter in 8 lines of code [ ... ]

This is more a "batteries included" eureka moment than a Python one,
but writing a fetchmail substitute in 6 lines was an eye-opener.
 
D

Doug Gray

I'd be interested in hearing people's stories of Eureka moments in Python,
moments where you suddenly realise that some task which seemed like it
would be hard work was easy with Python.

Mine was the discovery of the pybluez module.

I had been sweating the task of interfacing a bluetooth GPS unit to my
python applicaton. I googled for 'python bluetooth GPS' and found 20 lines
of python code that had the interface up and running before breakfast.
This included the package install with 'yum install pybluez' in Fedora
Core 5.

See:
http://www.robertprice.co.uk/robblog/archive/2007/1/Using_A_Bluetooth_GPS_From_Python.shtml

Doug Gray
 
S

Sion Arrowsmith

Sion Arrowsmith said:
This is more a "batteries included" eureka moment than a Python one,
but writing a fetchmail substitute in 6 lines was an eye-opener.

On Fri, Mar 16, 2007 at 01:23:14PM -0500, someone emailed:
[This is a newsgroup/mailing list -- potentially useful information
like this should be shared, not taken to private email.]
Would you mind sharing your fetchmail substitute code with me? I'm
curious to know what that looks like.

Unfortunately, I threw it away, as it was so trivial and I only needed
a one-shot solution. I kept meaning to come back to it and add some
configurability and error checking and such (and most importantly
providing an envelope From), but never did. The general idea is to
take the POP3 example from the library reference
(http://docs.python.org/lib/pop3-example.html) and replace the inner
for loop with "smtplib.SMTP('localhost').sendmail('', 'user',
'\n'.join(M.retr(i+1)[1]))"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top