473,473 Members | 1,894 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Eureka moments in Python

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...


--
Steven D'Aprano

Mar 13 '07 #1
11 1619
Steven D'Aprano wrote:
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...

Mar 13 '07 #2
On Mar 13, 10:05 am, Brett g Porter <bgpor...@acm.orgwrote:
Steven D'Aprano wrote:
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...
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...

Mar 13 '07 #3
On Mar 13, 2:16 am, Steven D'Aprano <s...@REMOVEME.cybersource.com.au>
wrote:
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)

Mar 13 '07 #4
Dustan wrote:
On Mar 13, 10:05 am, Brett g Porter <bgpor...@acm.orgwrote:
>Steven D'Aprano wrote:
>>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...

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...

Mar 13 '07 #5
"Steven D'Aprano" <st***@REMOVEME.cybersource.com.auwrites:
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 :-)

--
\ "I may disagree with what you say, but I will defend to the |
`\ death your right to mis-attribute this quote to Voltaire." -- |
_o__) Avram Grumer, rec.arts.sf.written, May 2000 |
Ben Finney

Mar 14 '07 #6
Steven D'Aprano <st***@REMOVEME.cybersource.com.auwrote:
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
Mar 16 '07 #7
On Mar 13, 2:16 am, Steven D'Aprano <s...@REMOVEME.cybersource.com.au>
wrote:
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

Mar 16 '07 #8
Ben Finney napisa³(a):
>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.
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... ;)

--
Jarek Zgoda

"We read Knuth so you don't have to."
Mar 16 '07 #9
Paul McGuire <pt***@austin.rr.comwrote:
>On Mar 13, 2:16 am, Steven D'Aprano <s...@REMOVEME.cybersource.com.au>
wrote:
>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.

--
\S -- si***@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
___ | "Frankly I have no feelings towards penguins one way or the other"
\X/ | -- Arthur C. Clarke
her nu becomež se bera eadward ofdun hlęddre heafdes bęce bump bump bump
Mar 16 '07 #10
On Tue, 13 Mar 2007 18:16:15 +1100, Steven D'Aprano wrote:
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...m_Python.shtml

Doug Gray
Mar 20 '07 #11
Sion Arrowsmith <si***@chiark.greenend.org.ukwrote:
>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]))"

--
\S -- si***@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
___ | "Frankly I have no feelings towards penguins one way or the other"
\X/ | -- Arthur C. Clarke
her nu becomež se bera eadward ofdun hlęddre heafdes bęce bump bump bump
Mar 20 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tim Churches | last post by:
See http://www.amonline.net.au/eureka/communications_technology/2005_winner.htm A Google search on "scamseek python" reveals the nexus with Python - see for example...
12
by: bluesteel | last post by:
I was asked to make a program which rang a bell at certain at certain moments. The one way i can think of making it is by using a loop, but i should try to avoid using it and i don't have any idea. I...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.