Re: Converting 5.223701009526849e-05 to 5e-05

2015-05-07 Thread Alexander Blinne
Am 03.05.2015 um 10:48 schrieb Ben Finney:
> That's not as clear as it could be. Better is to be explicit about
> choosing “exponential” format::
> 
> >>> foo = 5.223701009526849e-05
> >>> "{foo:5.0e}".format(foo=foo)
> '5e-05'
> 

Or even better the "general" format, which also works for 0.:

>>> "{foo:.1g}".format(foo=5.223701009526849e-5)
'5e-05'
>>> "{foo:.1g}".format(foo=0.)
'1'

I guess all roads lead to Rome...

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: set environmental variable from python

2014-11-01 Thread Alexander Blinne
Am 31.10.2014 um 02:22 schrieb Artur Bercik:
> I have to set environmental variable in my windows PC as follows:
> 
> variable name: GISBASE
> 
> value: C:\GRASS-64
> 
> Is it possible to set it from python?
> 
> import sys
> 
> sys.path.append("C:\\GRASS-64")
> 
> But how to give variable name? I have to set both the variable name and
> value.

http://lmgtfy.com/?q=how+to+set+environment+variable+with+python

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Issue in printing top 20 dictionary items by dictionary value

2014-10-04 Thread Alexander Blinne
Am 04.10.2014 um 11:11 schrieb Shiva:
> Hi All,
> 
> I have written a function that 
> -reads a file 
> -splits the words and stores it in a dictionary as word(key) and the total
> count of word in file (value).
> 
> I want to print the words with top 20 occurrences in the file in reverse
> order - but can't figure it out.

As python is a high-level language with a comprehensive standard
library, there ought to be a built-in method to sort stuff... Wait, but
there ist:

sorted(iterable[, key][, reverse])

The tricky part is: a dictionary is not storing the order of its
entries. But you could just go with the list of your keys, sorted by the
count of your words and then go from there.

> Here is my function:
> 
> def print_top(filename):
> 
> #Open a file
> path = '/home/BCA/Documents/LearnPython/ic/'
> fname = path + filename
> print ('filename: ',fname)
> filetext = open(fname)
> 
> #Read the file
> textstorage={}
> 
> #print(type(textstorage))
> readall = filetext.read().lower()
> eachword = set(readall.split())
> 
> #store split words as keys in dictionary
> for w in eachword:
> textstorage[w] = readall.count(w)
> 
> #print top 20 items in dictionary by decending order of val
> # This bit is what I can't figure out.

orderedwords = sorted(textstorage.keys(), key=textstorage.get, reverse=True)
toptwenty = orderedwords[:20]

for dkey in toptwenty:
print(dkey,textstorage[dkey])

The interesting part is the "key=textstorage.get"-portion. This defines
according to what "key" two values of the iterable (the keys of the
dict) should be compared. The method textstorage.get will accept a word
and return the count of that word. This count is used to sort the words
- in reverse order.


Another remark: Your algorithm works, but imagine this: Your text could
be more than a few lines long and contain more than a few different
words. When you convert your text into the set of alle the words you
have to walk through it once. You only type one line into python, but
this is what python has to do at that point. Afterwards, for every
single word, you use the count-method of the complete text. This also
walks through the whole text - each time. So if your text has N
different words you walk through it N+2 times (your readall line also
walks completely through it!). It is possible to solve your problem
while only walking through the text only a few (independent of N) times
or even only a single time! And the larger your text gets, the more
important this gets. This is not an issue in python but in all of
computer science.

So try and rethink your algorithm with that goal in mind: Only walk
through the text once. Python makes strong use on iterators, also file
objects can be used as iterators. The count in your dictionary can be
updated while you walk through the text. The get-method has a keyword
argument for creating default values, which might be useful here.
Another thing worth of mentioning is, that python has exactly this kind
of machinery already built-in (collections.Counter), but you should try
and implement a simple version of it yourself as exercise.

Alexander
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Reference

2014-03-04 Thread Alexander Blinne
Am 04.03.2014 15:06, schrieb Chris Angelico:
> https://github.com/Rosuav/ExceptExpr/blob/master/find_except_expr.py

I have always found it quite nice that the python parser is so easy to
use from within python itself.

> Run across the Python stdlib, that tells me there are 4040 uses of
> is/is not, of which 16 compare against False, 18 against True (see?
> Python has a bias for truth above falsehood!), and 3386 against None.

I think i will have to rephrase my "is False" condition to make it more
truthy :)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Reference

2014-03-04 Thread Alexander Blinne
Am 03.03.2014 19:48, schrieb Terry Reedy:
> The 'is' operator has three uses, two intended and one not. In
> production code, 'is' tests that an object *is* a particular singular
> object, such as None or a sentinel instance of class object.

Just a bit of statistics on this one from a recent small project:

<13:51:20> alex@firefly$ grep ' is ' *.py | wc
 65 4153234
<13:51:35> alex@firefly$ grep ' is None' *.py | wc
 43 2431948
<13:51:40> alex@firefly$ grep ' is not None' *.py | wc
 21 1671241
<13:51:44> alex@firefly$ grep ' is False' *.py | wc
  1   5  45

No other uses if 'is' found in almost 3 KLOC...
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Can I iterate over a dictionary outside a function ?

2013-04-11 Thread Alexander Blinne
Am 11.04.2013 11:48, schrieb inshu chauhan:
> I have a prog in which a functions returns a dict but when I try to
> iterate over the dict using iterkeys, It shows an error.

1) Show us your code in form of a minimal "working" example, "working"
means that it should show us what you expect it to do but at the same
time shows the behaviour you complain about.

2) Show us your actual error message!

> I think its
> because only address of the dictionary is returned so cannot be iterated
> upon. 

Python does not use addresses, it uses references. And as long as you
have a valid reference to a dict assigned to some name in some namespace
you should be able to iterate over its keys using some_dict.iterkeys().

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: iterating over a list as if it were a circular list

2013-03-07 Thread Alexander Blinne
Am 08.03.2013 00:49, schrieb Alexander Blinne:
> http://docs.python.org/3/library/itertools.html#itertools.repeat

obviously I was aiming for
http://docs.python.org/2/library/itertools.html#itertools.cycle
here

Greetings
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: iterating over a list as if it were a circular list

2013-03-07 Thread Alexander Blinne
Am 07.03.2013 10:27, schrieb Sven:
> Now I would like to iterate over P and place one N at each point.
> However if you run out of N I'd like to restart from N[0] and carry on
> until all the points have been populated.
> So far I've got (pseudo code)
> 
> i = 0
> for point in points:
> put N[i] at point
> if i > len(N):
> i = 0
> 
> is this the most pythonic way to accomplish this?

Sounds like
http://docs.python.org/3/library/itertools.html#itertools.repeat
to me.

> Additionally, what if I wanted to pull a random element from N, but I
> want to ensure all elements from N have been used before starting to
> pick already chosen random elements again.
> So far I thought of duplicating the list and removing the randomly
> chosen elements from the list, and when it's empty, re-copying it. But
> that seems a little "wrong" if you know what I mean.

This can be done with
http://docs.python.org/3/library/random.html#random.shuffle

untested:

import random

def repeated_random_permutation(iterable):
pool = list(iterable)
while True:
random.shuffle(pool)
yield from pool


Greetings
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python 3.3 vs. MSDOS Basic

2013-02-19 Thread Alexander Blinne
Am 19.02.2013 12:42, schrieb Piet van Oostrum:
> Terry Reedy  writes:
>> I find this surprising too. I am also surprised that it even works,
>> given that the highest intermediate value is about 57 billion and I do
>> not remember that Basic had infinite precision ints.
> 
> That may explain why the Basic version is faster: it gets overflow and
> then it may have taken some shortcuts.

Consider this C program

#include 

int main(void) {

  int max = 0;
  int m = 0;
  long int n;
  int count;
  int num;

  while(m<=100) {
m++;
n = m;
count = 0;

while(n != 1) {
  count++;
  if(n % 2 == 0) {
n = n / 2;
  }
  else {
n = n*3 + 1;
  }
}

if(count > max) {
  max = count;
  num = m;
}
  }

  printf("%d, %d\n", num, max);
}

If the line

long int n;

is changed into

unsigned int n;

the program runs in 0.68 sec instead of 0.79, so there is some shortcut.
If changed into

signed int n;

there is a veeery long, perhaps infinite loop.

Greetings
Alexander

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python 3.3 vs. MSDOS Basic

2013-02-18 Thread Alexander Blinne
Am 18.02.2013 20:13, schrieb John Immarino:
> I coded a Python solution for Problem #14 on the Project Euler website. I was 
> very surprised to find that it took 107 sec. to run even though it's a pretty 
> simple program.  I also coded an equivalent solution for the problem in the 
> old MSDOS basic. (That's the 16 bit app of 1980s vintage.)  It ran in 56 sec. 
> Is there a flaw in my coding, or is Python really this slow in this 
> particular application. MSDOS Basic usually runs at a snails pace compared to 
> Python.

> max=0
> m=0
> while m<=100:
> m+=1
> count=0
> n=m
> while n!=1:
> count+=1
> if n%2==0:
> n=n//2
> else:
> n=3*n+1
> if count>max:
>  max=count
>  num=m
> print(num,max)

I cannot compare my timings with basic but python 2.7.3 and python 3.2.3
are both equally slow hier (~50 sec).
pypy is a lot faster (only some old version 1.7.0, current versions
should be faster still) with about 5 sec.

The following C-Program:

#include 

int main(void) {

  int max = 0;
  int m = 0;
  long int n;
  int count;
  int num;

  while(m<=100) {
m++;
n = m;
count = 0;

while(n != 1) {
  count++;
  if(n % 2 == 0) {
n = n / 2;
  }
  else {
n = n*3 + 1;
  }
}

if(count > max) {
  max = count;
  num = m;
}
  }

  printf("%d, %d\n", num, max);
}

Does the job in just under 1 sec.

Greetings
Alexander






-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces)

2012-12-24 Thread Alexander Blinne
At this point I think i could just refer to my other 2 postings and urge
you to read them again. They offer the idea of encapsulating the
function QuerySqlite into a method of an object that can be passed over
to some object (possibly throu the __init__-method) and store it in an
attribute of that other object. Those other objects can then simply call
the method belonging to the object.
If you really don't understand what I mean by this maybe you should
learn a bit about the basics of object-oriented programming.
Some pseudo-code illustrating this idea (which differs a bit from the
first singleton-like suggestion):

datastore.py:

class Datastore(object):
def __init__(self, some_args):
#do all things needed to open datastore and store everything to
#self.something and self.someotherthing

def query(self, query, *values):
#execute query with values inserted
#using self.something and self.someotherting
#return result

modbus.py:

class Modbus(self):
def __init__(self, datastore):
#store the argument datastore to an attribute of the newly
#created object
self.datastore = datastore

def read_bus(self, sensor):
#read from bus the value of sensor and return value

def read_temp_and_store(self, sensor):
#read and store
value = self.read_bus(sensor)
self.datastore.query("some query string", value)

scheduler.py:

class Scheduler(object):
def __init__(self, datastore, modbus):
#store the arguments datastore and modbus to attributes
#of the newly created object
self.datastore = datastore
self.modbus = modbus
#maybe read some config data from datastore
self.config = self.datastore.query("some initialising query if
necessary")

def do_things(self):
#do things you wanna do, perhaps in some loop or in a thread or
#something, does not really matter.
#Threading may require locking of some kind, but this also is
#not really related to your problem as I understand ist.
self.modbus.read_temp_and_store("sensor1")

main.py:

from scheduler import Scheduler
from datastore import Datastore
from modbus import Modbus

def main():
datastore = Datastore(some_args)
modbus = Modbus(datastore)
scheduler = Scheduler(datastore, modbus)

scheduler.do_things()

if __name__=="__main__":
main()

Please feel free to ask specific questions about this approach.

merry christmas everyone
Alexander Blinne
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces)

2012-12-22 Thread Alexander Blinne
Am 22.12.2012 21:43, schrieb prilisa...@googlemail.com:
> I Think I describe my Situation wrong, the written Project is a
> Server, that should store sensor data, perfoms makros on lamps according
> a sequence stored in the DB and Rule systems schould regulate home devices 
> and plan scheduler jobs so on.

I really don't understand your problem and I don't think anyone else
does. A python programm written like I have explained could do all those
things with no problem whatsoever, if only the classes contain all the
relevant methods.

> The System Runs in a threated environment. It looks for me, like the limits 
> are at the end of file. my core problem also the only one I have is: I don't 
> know how to get over that limits and enable dataexchange like a backbone...

There is no limit at the end of a file. A module is only a namespace and
you can access the names of another module simply by importing it first.
You also can freely pass around objects as parameters in function/method
calls or even just store them to module-level global names. Threads
don't change anything about that.

Greetings.



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces)

2012-12-22 Thread Alexander Blinne
Am 22.12.2012 19:10, schrieb prilisa...@googlemail.com:
> It's for me a view of top side down, but how could the midlevel comunicate to 
> each oter... "not hirachical"

You could use something like the singleton pattern in order to get a
reference to the same datastore-object every time Datastore.Datastore()
is called. But you still need to close the connection properly at some
point, propably using a classmethod Datastore.close().

e.g.:

main.py:

from Datastore import Datastore
from ModbusClient import Modbus
from DaliBusClient import DaliBus

def main():
modbus = Modbus(...)
dalibus = DaliBus(...)

modbus.read_data_and_save_to_store()
dalibus.read_data_and_save_to_store()
Datastore.close()

if __name__=="__main__":
main()


ModbusClient.py:

import Datastore

class Modbus(object):
def read_data_and_save_to_store(self):
datastore = Datastore.Datastore()
#do something with datastore
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces)

2012-12-22 Thread Alexander Blinne
Am 22.12.2012 13:45, schrieb prilisa...@googlemail.com:
> Ps.: The Socket, the DB has to be kept allways open, because of it's Server 
> functionality, A lot of Sensors, Timers, User interaction, must recived , 
> Calculated, etc so a reaction must be send in about 16~100 ms, different 
> modules opens and closes Sockets or files, could result in a dead situation.
> 
> 
> Or do i didn't see any Tree's in the Wood?

I would strongly recommend an object oriented view:

Suppose Datastore.py contains a Class Datastore. You can instantiate
that class once in the beginning of your main file and the resulting
object has methods to store and retrieve data in/from the store.

ModbusClient.py contains a Class Modbus. This also can be instantiated
just once which opens a TCP connection to be used many times and you can
hand over a reference to the Instance of Datastore you created earlier,
so it can speak with the Datastore. The object has methods to do the
things you want it to do.

The Same for DaliBusClient.

Now your main.py could look something linke

from Datastore import Datastore
from ModbusClient import Modbus
from DaliBusClient import DaliBus

def main():
datastore = Datastore(...)
modbus = Modbus(..., datastore)
dalibus = DaliBus(..., datastore)

modbus.read_data_and_save_to_store()
dalibus.read_data_and_save_to_store()

if __name__=="__main__":
main()

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pattern-match & Replace - help required

2012-12-19 Thread Alexander Blinne
Am 19.12.2012 14:41, schrieb AT:
> Thanks a million
> Can you recommend a good online book/tutorial on regular expr. in python?

http://docs.python.org/3/howto/regex.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [newbie] problem making equally spaced value array with linspace

2012-12-18 Thread Alexander Blinne
Am 18.12.2012 13:37, schrieb Jean Dubois:
> I have trouble with the code beneath to make an array with equally
> spaced values
> When I enter 100e-6 as start value, 700e-6 as end value and 100e-6 I
> get the following result:
> [ 0.0001   0.00022  0.00034  0.00046  0.00058  0.0007 ]
> But I was hoping for:
> [ 0.0001   0.0002  0.0003  0.0004  0.0005  0.0006 0.0007]
> It works correctly for other values like 1,7,1 but not for 0.1,0.7,0.1
> then again for 0.01,0.07,0.01
> 
> What I find strange is that for the 1st example "1+abs(float(endvalue)-
> float(startvalue))/float(incr)" gives 7.0 but int() of this value
> gives 6
> can someone provide help with this issue?
> thanks
> jean
> 
> #!/usr/bin/python
> import math
> import numpy as np
> print "Enter start value as a float (e.g. 0.001) or in scientific
> notation (e.g. 1e-3): ",
> startvalue = raw_input()
> print "Enter end value: ",
> endvalue = raw_input()
> print "Enter step: ",
> incr = raw_input()
> #nom = number of measurements
> nom=int(1+abs(float(endvalue)-float(startvalue))/float(incr))
> array=np.linspace(float(startvalue), float(endvalue), float(nom))
> print "Array with current values: ",array

The Problem is the accuracy/precision of floating point operations

Python 2.7.3 (default, Aug  1 2012, 05:14:39)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 100e-6 #start
>>> b = 700e-6 #end
>>> c = 100e-6 #incr
>>> 1+(b-a)/c
6.999

and the fact that int() only takes the integer part of a floating point
number.

>>> int(1+(b-a)/c)
6

So you have to make a more detailed decision about the number of points
in the case that (end-start)/incr is not exactly an integer which it
will almost never be.

The np.arange(a,b,c) function chooses a simple rule: give a list of
numbers a + k * c with k running from 0 to the highest integer with a +
k * c < b.

>>> np.arange(a,b,c)
array([ 0.0001,  0.0002,  0.0003,  0.0004,  0.0005,  0.0006])

You can get your desired list by adding some epsilon to the value of b.
Just make sure your epsilon is quite small compared to c.

>>> np.arange(a,b+1e-15,c)
array([ 0.0001,  0.0002,  0.0003,  0.0004,  0.0005,  0.0006,  0.0007])

Greetings
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Preventing tread collisions

2012-12-13 Thread Alexander Blinne
Am 12.12.2012 21:29, schrieb Dave Angel:
> On 12/12/2012 03:11 PM, Wanderer wrote:
>> I have a program that has a main GUI and a camera. In the main GUI, you can 
>> manipulate the images taken by the camera. You can also use the menu to 
>> check the camera's settings. Images are taken by the camera in a separate 
>> thread, so the long exposures don't block the GUI. I block conflicts between 
>> the camera snapshot thread and the main thread by setting a flag called 
>> self.cameraActive. I check to see if the cameraActive flag is false and set 
>> the cameraActive to True just before starting the thread. I generate an 
>> event on exiting the thread which sets the cameraActive flag to False. I 
>> also check and set and reset the flag in all the menu commands that access 
>> the camera. Like this.
>>
>> def onProperties(self, event):
>> """ Display a message window with the camera properties
>> event -- The camera properties menu event
>> """
>> # Update the temperature
>> if not self.cameraActive:
>> self.cameraActive = True
>> self.camera.getTemperature()
>> camDict = self.camera.getPropertyDict()
>> self.cameraActive = False
>> else:
>> camDict = {'Error': 'Camera Busy'}
>> dictMessage(camDict, 'Camera Properties')
>>
>> This works 
> 
> I don't think so.  in between the if and the assignment, another thread
> could get in there and also set the flag.  Then when either one of them
> finishes, it'll clear the flag and the other code is unprotected.

I have a general question about this kinds of things. I see that the
above is a common use case for some kind of lock which does this
testing/locking atomically. But the question is: if I know for sure that
there is no other thread that might get in the way this solution would
be fine, right?

In one of my applications i have a somewhat different case: i have a
list of objects and call the same method of each object, each in its own
thread (which is created and later joined just for this purpose). The
objects are thus only used by that one thread, the main thread waits for
all threads to be finished before accessing those objects again. Do i
really need some kind of locking for those objects?

Greetings
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Noob Question.

2012-12-10 Thread Alexander Blinne
Am 05.12.2012 21:24, schrieb Owatch:
> Thanks a TON for your answer thought, this is exactly what I really hoped for.
> The problem for me is that I don't actually know anything about writing a 
> function that opens a network socket, and "connects to that plugin und asks 
> it for the 
> information you require."

That plugin should have some documentation which should tell you
something about how to connect to it and how to request information.
When you know that you can turn to the python documentation and find out
how to do this in python.

> That's all really beyond me, all I can do is what I did so far, which is make 
> it ask for your temperature value, and then test it to see if its an integer
> 
> Then (I added this for testing) It asks for any temperature value. And if it 
> exceeds the given limit, it rings an alarm. Until it freezes and becomes 
> unresponsive :D

If you have specific problems with code you have written, try to build
up a minimal "working" example that shows the problem plus any error
messages/exceptions/stack traces you get back. We might be able to help
you with your code.

> I don't know how to make it 'query' or grab values constantly, if you don't 
> mind my potentially incorrect terminology. 

This is typically done with some kind of loop, e.g.

run = True
while run:
#do something repeatedly and do "run = False" if you want to stop
pass

Greetings
Alexander

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Good use for itertools.dropwhile and itertools.takewhile

2012-12-06 Thread Alexander Blinne
Am 05.12.2012 18:04, schrieb Nick Mellor:
> Sample data

Well let's see what

def split_product(p):
p = p.strip()
w = p.split(" ")
try:
j = next(i for i,v in enumerate(w) if v.upper() != v)
except StopIteration:
return p, ''
return " ".join(w[:j]), " ".join(w[j:])

(which i still find a very elegant solution) has to say about those
sample data:

>>> for line in open('test.dat', 'r'):
... print(split_product(line))
('BEANS', 'hand picked')
('BEETROOT', 'certified organic')
('BOK CHOY', '(bunch)')
('BROCCOLI', 'Mornington Peninsula')
('BRUSSEL  SPROUTS', '')
('CABBAGE', 'green')
('CABBAGE', 'Red')
('CAPSICUM RED', '')
('CARROTS', '')
('CARROTS', 'loose')
('CARROTS', 'juicing, certified organic')
('CARROTS', 'Trentham, large seconds, certified organic')
('CARROTS', 'Trentham, firsts, certified organic')
('CAULIFLOWER', '')
('CELERY', 'Mornington Peninsula IPM grower')
('CELERY', 'Mornington Peninsula IPM grower')
('CUCUMBER', '')
('EGGPLANT', '')
('FENNEL', '')
('GARLIC', '(from Argentina)')
('GINGER', 'fresh uncured')
('KALE', '(bunch)')
('KOHL RABI', 'certified organic')
('LEEKS', '')
('LETTUCE', 'iceberg')
('MUSHROOM', 'cup or flat')
('MUSHROOM', 'Swiss brown')
('ONION', 'brown')
('ONION', 'red')
('ONION', 'spring (bunch)')
('PARSNIP,', 'certified organic')
('POTATOES', 'certified organic')
('POTATOES', 'Sebago')
('POTATOES', 'Desiree')
('POTATOES', 'Bullarto chemical free')
('POTATOES', 'Dutch Cream')
('POTATOES', 'Nicola')
('POTATOES', 'Pontiac')
('POTATOES', 'Otway Red')
('POTATOES', 'teardrop')
('PUMPKIN', 'certified organic')
('SCHALLOTS', 'brown')
('SNOW PEAS', '')
('SPINACH', "I'll try to get certified organic (bunch)")
('SWEET POTATO', 'gold certified organic')
('SWEET POTATO', 'red small')
('SWEDE', 'certified organic')
('TOMATOES ', 'Qld')
('TURMERIC', 'fresh certified organic')
('ZUCCHINI', '')
('APPLES', 'Harcourt  Pink Lady, Fuji, Granny Smith')
('APPLES', 'Harcourt 2 kg bags, Pink Lady or Fuji (bag)')
('AVOCADOS', '')
('AVOCADOS', 'certified organic, seconds')
('BANANAS', 'Qld, organic')
('GRAPEFRUIT', '')
('GRAPES', 'crimson seedless')
('KIWI FRUIT', 'Qld certified organic')
('LEMONS', '')
('LIMES', '')
('MANDARINS', '')
('ORANGES', 'Navel')
('PEARS', 'Beurre Bosc Harcourt new season')
('PEARS', 'Packham, Harcourt new season')
('SULTANAS', '350g pre-packed bags')
('EGGS', "Melita free range, Barker's Creek")
('BASIL', '(bunch)')
('CORIANDER', '(bunch)')
('DILL', '(bunch)')
('MINT', '(bunch)')
('PARSLEY', '(bunch)')
('', 'Spring ONION from QLD')

I think the only thing one is left to think about is the
('PARSNIP,', 'certified organic')
case. What about that extra comma? Perhaps it could even be considered
an "error" in the original data? I don't see a good general way to deal
with those which does not have to handle trailing punctuation on the
product name explicitly as a special case.

Greetings
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Good use for itertools.dropwhile and itertools.takewhile

2012-12-04 Thread Alexander Blinne
Am 04.12.2012 20:37, schrieb Ian Kelly:
> >>> def split_product(p):
> ... w = p.split(" ")
> ... j = next(i for i,v in enumerate(w) if v.upper() != v)
> ... return " ".join(w[:j]), " ".join(w[j:])
> 
> 
> It still fails if the product description is empty.

That's true... let's see, next() takes a default value in case the
iterator is empty and then we could use some special value and test for
it. But i think it would be more elegant to just handle the excepten
ourselves, so:

>>> def split_product(p):
... w = p.split(" ")
... try:
... j = next(i for i,v in enumerate(w) if v.upper() != v)
... except StopIteration:
... return p, ''
... return " ".join(w[:j]), " ".join(w[j:])

> I'm not meaning to pick on you; some of the other solutions in this
> thread also fail in that case.

It's ok, opening the eye for edge cases is always a good idea :)

Greetings
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Good use for itertools.dropwhile and itertools.takewhile

2012-12-04 Thread Alexander Blinne
Am 04.12.2012 19:28, schrieb DJC:
 (i for i,v in enumerate(w) if v.upper() != v).next()
> Traceback (most recent call last):
>   File "", line 1, in 
> AttributeError: 'generator' object has no attribute 'next'

Yeah, i saw this problem right after i sent the posting. It now is
supposed to read like this

>>> def split_product(p):
... w = p.split(" ")
... j = next(i for i,v in enumerate(w) if v.upper() != v)
... return " ".join(w[:j]), " ".join(w[j:])

Greetings
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Good use for itertools.dropwhile and itertools.takewhile

2012-12-04 Thread Alexander Blinne
Another neat solution with a little help from

http://stackoverflow.com/questions/1701211/python-return-the-index-of-the-first-element-of-a-list-which-makes-a-passed-fun

>>> def split_product(p):
... w = p.split(" ")
... j = (i for i,v in enumerate(w) if v.upper() != v).next()
... return " ".join(w[:j]), " ".join(w[j:])

Greetings
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Conversion of List of Tuples

2012-12-04 Thread Alexander Blinne
Am 03.12.2012 20:58, schrieb subhabangal...@gmail.com:
> Dear Group,
> 
> I have a tuple of list as,
> 
> tup_list=[(1,2), (3,4)]
> Now if I want to covert as a simple list,
> 
> list=[1,2,3,4]
> 
> how may I do that?

Another approach that has not yet been mentioned here:

>>> a=[(1,2), (3,4)]
>>> b=[]
>>> map(b.extend, a)
[None, None]
>>> b
[1, 2, 3, 4]

map returns [None, None] because extend returns nothing, but now
b==[1,2,3,4].

There are more ways:

>>> from operator import add
>>> reduce(add, a)
(1, 2, 3, 4)

or

>>> reduce(operator.add, (list(t) for t in a))
[1, 2, 3, 4]

I didn't do any performance testing, i guess the first one should be
about as fast es the for-loop approach with .extend() and the other two
might be quite slow. Although this only really matters if you have large
lists.

Greetings
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Noob Question.

2012-12-03 Thread Alexander Blinne
Hello,

by having a quick look at their website i found a plugin for CoreTemp
which acts as a server and can be asked for status information of the
cpu. Now your task is really simple: write a little function or class
that opens a network socket, connects to that plugin und asks it for the
information you require. You just need to find out what network protocol
this plugin uses to communicate. If it is no standard protocol for which
a higher level module is present (xmlrpc or something), see
http://docs.python.org/3/library/socket.html for low level sockets.

Greetings
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: creating size-limited tar files

2012-11-07 Thread Alexander Blinne
I don't know the best way to find the current size, I only have a
general remark.
This solution is not so good if you have to impose a hard limit on the
resulting file size. You could end up having a tar file of size "limit +
size of biggest file - 1 + overhead" in the worst case if the tar is at
limit - 1 and the next file is the biggest file. Of course that may be
acceptable in many cases or it may be acceptable to do something about
it by adjusting the limit.

My Idea:
Assuming tar_file works on some object with a file-like interface one
could implement a "transparent splitting file" class which would have to
use some kind of buffering mechanism. It would represent a virtual big
file that is stored in many pieces of fixed size (except the last) and
would allow you to just add all files to one tar_file and have it split
up transparently by the underlying file-object, something like

tar_file = TarFile(SplittingFile(names='archiv.tar-%03d', chunksize=
chunksize, mode='wb'))
while remaining_files:
tar_file.addfile(remaining_files.pop())

and the splitting_file would automatically create chunks with size
chunksize and filenames archiv.tar-001, archiv.tar-002, ...

The same class could be used to put it back together, it may even
implement transparent seeking over a set of pieces of a big file. I
would like to have such a class around for general usage.

greetings
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: write binary with struct.pack_into

2012-10-06 Thread Alexander Blinne
First, you should consider reading the documentation of
struct.unpack_from and struct.pack_into at
http://docs.python.org/library/struct.html quite carefully. It says,
that these commands take a parameter called offset, which names the
location of the data in a buffer (e.g. an opened file).

example:

bloco='>%df' % (252)# Format string (252 floats)
fa = open('testIN.bin', 'rb')   # open for reading in binary mode
off = 0   # suppose i want to read block at beginning of file
my_array=struct.unpack_from(bloco, fa, off)  #read data


now write them to another file:

fb = open('testOUT.bin', 'r+b')   # open for updating in binary mode
off = 0   # suppose i want to write block at beginning of file
struct.pack_into(bloco, fb, off, *my_array)  #write data to testOUT


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Functional way to compare things inside a list

2012-09-21 Thread Alexander Blinne
On 21.09.2012 00:58, thorso...@lavabit.com wrote:
> Hi,
> 
> list = [{'1': []}, {'2': []}, {'3': ['4', '5']}]
> 
> I want to check for a value (e.g. '4'), and get the key of the dictionary
> that contains that value.
> (Yep, this is bizarre.)
> 
> some_magic(list, '4')
> => '3'
> 
> What's the functional way to do it?
> Is it possible to do it with a one-liner?

simple, but possibly slow solution:

import itertools

def some_magic(list, search):
return (key for key, val in itertools.chain(*(d.iteritems() for d in
list)) if search in val).next()

one-liner, yeah...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python presentations

2012-09-17 Thread Alexander Blinne
On 16.09.2012 19:35, Steven D'Aprano wrote:
> On Sun, 16 Sep 2012 18:13:36 +0200, Alexander Blinne wrote:
>> def powerlist2(x,n):
>> if n==1:
>> return [1]
>> p = powerlist3(x,n-1)
>> p.append(p[-1]*x)
>> return p
> 
> Is that a typo? I think you mean to make a recursive call to powerlist2, 
> not a non-recursive call to powerlist3.

Yes, it is a typo. I originally tested 2 more versions, but tried to
change the numbering before posting. Bad idea :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python presentations

2012-09-16 Thread Alexander Blinne
On 14.09.2012 14:19, Chris Angelico wrote:
> Err, yes, I did mean ** there. The extra multiplications may be
> slower, but which is worse? Lots of list additions, or lots of integer
> powers? In the absence of clear and compelling evidence, I'd be
> inclined to go with the list comp - and what's more, to skip this
> function altogether and use the list comp directly where it's needed.

I did some timing with the following versions of the function:

def powerlist1(x, n):
result=[1]
for i in xrange(n-1):
result.append(result[-1]*x)
return result

def powerlist2(x,n):
if n==1:
return [1]
p = powerlist3(x,n-1)
p.append(p[-1]*x)
return p

def powerlist3(x,n):
  return [x**i for i in xrange(n)]

with Python 2.7 you are quite right, i used x=4. Below n=26 powerlist3
is the fastest version, for n>26 powerlist1 is faster, powerlist2 is
always slower than both.

With Pypy there is a completely different picture, with n<30 powerlist2
is way faster then the other two, but then powerlist1 is again faster
for greater n, somehow because now C long int cannot be used any longer.

for really big n powerlist3 always takes very much time :)

Alex
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python presentations

2012-09-14 Thread Alexander Blinne
On 14.09.2012 00:38, Chris Angelico wrote:
> On Fri, Sep 14, 2012 at 8:33 AM, Alexander Blinne  wrote:
>> def powerlist(x,n):
>> if n==1:
>> return [1]
>> p = powerlist(x,n-1)
>> return p + [p[-1]*x]
> 
> Eh, much simpler.
> 
> def powerlist(x,n):
>   return [x*i for i in xrange(n-1)]

I suppose you meant:

def powerlist(x,n):
  return [x**i for i in xrange(n-1)]

But this is less efficient, because it needs more multiplications (see
Horner's method)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python presentations

2012-09-13 Thread Alexander Blinne
On 13.09.2012 21:01, 8 Dihedral wrote:
> def powerlist(x, n):
> # n is a natural number
>  result=[]
>  y=1
>  for i in xrange(n):
> result.append(y) 
> y*=x
>  return result # any object in the local function can be returned

def powerlist(x, n):
result=[1]
for i in xrange(n-1):
result.append(result[-1]*x)
return result

def powerlist(x,n):
if n==1:
return [1]
p = powerlist(x,n-1)
return p + [p[-1]*x]

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: issue with struct.unpack

2012-08-26 Thread Alexander Blinne
On 26.08.2012 01:31, Dennis Lee Bieber wrote:
> The struct module relies upon the user knowing the format of the data.
> If your problem is that you have some null-terminated string data in a
> variable width field, you will have to locate the position of the null
> FIRST, and specify the appropriate "count" for the s format.

This gave me the idea of an Enhancement of the Struct class with an
additional format character (perhaps 'n') which corresponds to a
null-terminated string:

-
# -*- coding: utf-8 -*-

import struct

class Nstr(object):
def __init__(self, ncount):
self.ncount = ncount

def unpack(self, s):
s = s.split('\0')
return s[:self.ncount], '\0'.join(s[self.ncount:])

def pack(self, *s):
if len(s)!=self.ncount:
raise ValueError
for st in s:
if '\0' in st:
raise ValueError
return '\0'.join(s)+'\0'

def __repr__(self):
return 'Nstr('+repr(self.ncount)+')'

class NStruct(object):
def __init__(self, format):
self.format = format
if format[0] in '!=<>@':
self.endianness = format[0]
format = format[1:]
else:
self.endianness = ''
self.chunks = []
while len(format)>0:
j = format.find('n')
if j > -1:
k = j-1
while format[k].isdigit():
k-=1
chunkformat, ncount, format = format[:k+1],\
format[k+1:j], format[j+1:]
ncount = 1 if len(ncount)==0 else int(ncount)
else:
chunkformat, ncount, format = format, '', ''
ncount = 0
stru = struct.Struct(self.endianness+chunkformat)
l = len(stru.unpack("0"*stru.size))
self.chunks.append((stru, l))
if ncount > 0:
self.chunks.append((Nstr(ncount), ncount))

def unpack(self, data):
res = []
for sth, n in self.chunks:
if isinstance(sth, struct.Struct):
chunk, data = data[:sth.size], data[sth.size:]
res.extend(sth.unpack(chunk))
elif isinstance(sth, Nstr):
chunk, data = sth.unpack(data)
res.extend(chunk)
return res

def pack(self, *data):
res = []
for sth, n in self.chunks:
chunk, data = data[:n], data[n:]
res.append(sth.pack(*chunk))
return ''.join(res)

def __repr__(self):
return 'NStruct('+repr(self.format)+')'

if __name__=="__main__":
a = NStruct('h b 2n 2h')
print repr(a)
d = 'asdblah blah\0haha\0asdf'
r = a.unpack(d)
assert r == [29537, 100, 'blah blah', 'haha', 29537, 26212]
print repr(d), repr(r)
dd = a.pack(*r)
print r, repr(dd)
assert dd == d

-

beware of bugs in the above code, i haven't testet it much yet.

Alex
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Objects in Python

2012-08-24 Thread Alexander Blinne
On 23.08.2012 20:30, Dennis Lee Bieber wrote:
> On Thu, 23 Aug 2012 15:33:33 +1000, Chris Angelico 
> declaimed the following in gmane.comp.python.general:
>> x = 1;
>>
>> In C, this means: Assign the integer 1 to the variable x (possibly
>> with implicit type casting, eg to floating point).
>>
>   Or, at an even lower level...
> 
>   Convert the decimal literal "1" to binary (including type casting)
> to the predeclared type given to the variable "x", and store that binary
> value into the predetermined memory associated with "x".

Not really the way i would view it. The conversion to binary of the
string "1" is part of the parsers and compilers work in order to do what
the language reference says about the meaning of x=1;. The resulting
code would simply store the binary value of an integer 1 (which is
contained in the code as is, nothing has to be converted or typecasted)
into the location corresponding to the variable x. So in C x=1; really
means store integer 1 to the variable x.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [CGI] Why is HTML not rendered?

2012-08-17 Thread Alexander Blinne
On 17.08.2012 15:27, Gilles wrote:
> For some reason, this CGI script that I found on Google displays the
> contents of the variable but the HTML surrounding it is displayed
> as-is by the browser instead of being rendered:

> print "Content-Type: text/plain;charset=utf-8"

With this line you tell the browser to expect a simple plain text file
and no html. Change the line to

print "Content-Type: text/html;charset=utf-8"

and it should work.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: 2 + 2 = 5

2012-07-05 Thread Alexander Blinne
On 05.07.2012 16:34, Laszlo Nagy wrote:
 five.contents[five.contents[:].index(5)] = 4
 5
> 4
 5 is 4
> True

That's surprising, because even after changing 5 to 4 both objects still
have different id()s (tested on Py2.7), so 5 is 4 /should/ still be
False (But isn't on my 2.7). But that's some implementation detail we
are not supposed to play with ;)

> But this I don't understand:
> 
 5+0
> 4
 5+1
> 4
 5+2
> 6

That's easy:

5+0 is actually 4+0, because 5 == 4, so 5+0 gives 4.
5+1 is actually 4+1, which is 5, but 5 is again 4.
5+2 is 4+2 which is 6.

Greetings

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python equivalent to the "A" or "a" output conversions in C

2012-06-19 Thread Alexander Blinne
On 19.06.2012 18:23, Edward C. Jones wrote:
> Consider the following line in C:
>printf('%a\n', x);
> where x is a float or double.  This outputs a hexadecimal representation
> of x.  Can I do this in Python?

Don't know why there is no format character %a or %A in python, but the
conversion is done by float method hex():

a = 3.1415
print a.hex()

Greetings
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: which one do you prefer? python with C# or java?

2012-06-15 Thread Alexander Blinne
On 15.06.2012 09:00, Paul Rubin wrote:
> Alexander Blinne  writes:
>>> def gen_s():
>>>   s = [1]
>>>   m = skipdups(heapq.merge(*[(lambda j: (k*j for k in s))(n) for n in 
>>> [2,3,5]]))
>>>   yield s[0]
>>>   while True:
>>>   k = m.next()
>>>   s.append(k)
>>>   yield k
> 
> Nice.  I wouldn't have been sure that "for k in s" worked properly when
> s was changing like that.

I just tried it and it worked. Not sure if it is guaranteed.

> There is a space complexity problem compared to the Scheme or Haskell
> version: all the s[i]'s are saved in the s array, instead of being
> discarded once they are yielded.  That means generating n elements needs
> O(n) space instead of O(n**0.7) or something like that.  I guess you can
> get around it with collections.deque instead of a list.

An Element of s could be discarded, after every one of the three (k*j
for k in s)-generators went over it. I don't think that this is possible
with one deque (at least with the built-in merger of heapq, a
self-written one could be adapted). Storing everything three times (one
deque for every generator) would be a mess as well.

"Manual" garbage collection could be done by discarding all elements
smaller one fifth of the current element of s, because the three
generators already went by them. This could be done with a deque.

How do Haskell or Scheme determine when elements are not longer needed?

Greetings


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: which one do you prefer? python with C# or java?

2012-06-11 Thread Alexander Blinne
On 10.06.2012 23:27, Paul Rubin wrote:
> Here is an exercise from the book that you might like to try in Python:
> 
>   http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-24.html#%_idx_3894
> 
> It's not easy ;-)

I liked this exercize. At first I wrote my own merger.

> def merge(*iterables):
> iterables = list(iterables)
> current = [i.next() for i in iterables]
> last = None
> while True:
> m = min(current)
> while last == m:
> p = current.index(m)
> try:
> current[p] = iterables[p].next()
> except StopIteration:
> del current[p]
> del iterables[p]
> if len(current) == 0:
> raise StopIteration
> m = min(current)
> yield m
> last = m

But then I realised the vast library of python already contained (a
faster) one (propably based upon
), which
just needed to be enhanced a little bit to allow duplicate items to be
removed:

> import heapq
> 
> def skipdups(m):
> l = k = m.next()
> yield k
> while True:
> while l == k:
> k = m.next()
> yield k
> l = k
> 
> def gen_s():
>   s = [1]
>   m = skipdups(heapq.merge(*[(lambda j: (k*j for k in s))(n) for n in 
> [2,3,5]]))
>   yield s[0]
>   while True:
>   k = m.next()
>   s.append(k)
>   yield k

Now gen_s() generates the wanted sequence.

Greetings
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: why () is () and [] is [] work in other way?

2012-04-23 Thread Alexander Blinne
Am 21.04.2012 14:51, schrieb gst:
> Hi,
> 
> I played (python3.2) a bit on that and :
> 
> case 1) Ok to me (right hand side is a tuple, whose elements are evaluated 
> one per one and have same effect as your explanation (first [] is destroyed 
> right before the second one is created) :
> 
 x, y = id([]), id([])
 x == y
> True

> 
> 
> case 2) also ok to me:
> 
 x = id([]) ; y = id([])
 x == y
> True

> 
> 
> case 3) NOT ok to me :
> 
 x = id([])
 y = id([])
 x == y
> False

> 
> 
> case 4) ok to me :
> 
 def f():
>   x = id([])
>   y = id([])
>   return x == y
> 
 f()
> True

> 
> 
> I'd have thought that cases 2&3 are totally, while 3&4 nearly, syntactically 
> equal and that case 3 is the incorrect result..
> 
> how would you explain case 3 vs cases 2 and 4 ??

It is simply not defined if, after creation and destruction of an empty
list with some id, a newly created empty list should carry the same id
or not. In cases 1,2 and 4 it happens, but in case 3 it doesn't. This is
simply an implementation detail and neither behaviour is right or wrong.

Alex
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: why () is () and [] is [] work in other way?

2012-04-21 Thread Alexander Blinne
Am 21.04.2012 05:25, schrieb Rotwang:
> On 21/04/2012 01:01, Roy Smith wrote:
>> In article<877gxajit0@dpt-info.u-strasbg.fr>,
>>   Alain Ketterlin  wrote:
>>
>>> Tuples are immutable, while lists are not.
>>
>> If you really want to have fun, consider this classic paradox:
>>
> [] is []
>> False
> id([]) == id([])
>> True
> 
> Huh. This is not what I would have expected. What gives?

This happens only because the first [] gets destroyed after evaluation
of id([]). The second [] then by accident gets the same id as the first
one had.

>>> a = []
>>> b = []
>>> id(a) == id(b)
False

Greetings
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Zipping a dictionary whose values are lists

2012-04-13 Thread Alexander Blinne
Am 12.04.2012 18:38, schrieb Kiuhnm:
> Almost. Since d.values() = [[1,2], [1,2,3], [1,2,3,4]], you need to use
> list(zip(*d.values()))
> which is equivalent to
> list(zip([1,2], [1,2,3], [1,2,3,4]))
> 
> Kiuhnm

While this accidently works in this case, let me remind you that
d.values() does not return the elements of the d in any specific order.
(It is a non-random but implementation-specific order, see
.) Thus if you
need the correct order (as suggested by the dict keys) an explicit
sorting step is required, for example

zip(*[x[1] for x in sorted(d.items(), key=lambda y: y[0])])

Greetings
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Documentation, assignment in expression.

2012-03-25 Thread Alexander Blinne
I am not sure I understand your argument. The doc section states that

" [...] in Python you’re forced to write this:

while True:
line = f.readline()
if not line:
break
... # do something with line".

That simply isn't true as one can simply write:

for line in f:
#do stuff

which is the exact counter of the C idiom that C or perl people try to
use and complained about when they were really forced to write the above
lengthy while True loop. So there is no reason to want to do "assignment
in expression", so no real reason to use this example to explain why
they aren't there in python. I totally agree with the
error-handling-reason not allowing them.

I agree with Roy Smith saying that documentation shouldn't refer to the
"current version".
-- 
http://mail.python.org/mailman/listinfo/python-list


Documentation, assignment in expression.

2012-03-23 Thread Alexander Blinne
Hi,

I think this section of the docs needs some kind of rewrite:



While it is great to discuss the reasons for not allowing an assignment
in an expression, I feel that the given example is some kind of
outdated. The last sentence "For example, in the current version of
Python file objects support the iterator protocol, so you can now write
simply (for line in file:)" makes me think that this section was written
while that syntax was still new. No one I know would ever write
something like this:

> while True:
> line = f.readline()
> if not line:
> break
> ... # do something with line

I think at least we need a new example. Any ideas?

Greetings
Alexander
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python recursive tree, linked list thingy

2012-03-07 Thread Alexander Blinne

Am 07.03.2012 20:49, schrieb Wanderer:

I have a list of defective CCD pixels and I need to find clusters
where a cluster is a group of adjacent defective pixels. This seems to
me to be a classic linked list tree search.I take a pixel from the
defective list and check if an adjacent pixel is in the list. If it is
I add the pixel to the cluster and remove it from the defective list.
I then check an adjacent pixel of the new pixel and so on down the
branch until I don't find a defective pixel. The I move up to the
previous pixel and check the next adjacent pixel  and so on until I'm
back at the head I can't find any more defective adjacent pixels. How
do you handle this sort of thing in Python?


I'd do something like (code not tested):

defective_list = [(x1, y1), (x2, y2), ...]   #list of coordinates of
 #defective pixel
#build one cluster:
cluster_start = defective_list.pop() #starting point
buf = [] #buffer for added pixels
buf.push(cluster_start)
cluster = []
cluster.push(cluster_start)
while len(buf)>0:
i = buf.pop()
for b, d in itertools.product(xrange(2), [-1,1]):  #4 neighbours
j = list(i)
j[b] += d
j = tuple(j)
if outside_lcd(j) or j in cluster or j not in defective_list:
continue
defective_list.remove(j)
cluster.push(j)
buf.push(j)
return cluster

and repeat it until defective_list is empty.
--
http://mail.python.org/mailman/listinfo/python-list