Re: What is a shortcut to the Default home directory in Windows

2008-01-18 Thread Tim Golden
Christian Heimes wrote:
> Mike Driscoll wrote:
>> I personally use Tim Golden's excellent win32 API wrapper, the
>> winshell script. You can find it here:
>>
>> http://timgolden.me.uk/python/winshell.html
> 
> Yeah. Tim's winshell is fine but it's not using the official win32 api.

Umm... Is it not? The only thing I'm aware of doing is
retaining backwards compat. by using SHGetPathFromIDList
on the SHGetSpecialFolderLocation because I was writing against
Win9x at the time. Or are you saying something else?

(Admit I haven't checked all the docs since I wrote it
to see what's been "deprecated" this week).

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


Re: TopSort in Python?

2008-01-18 Thread Paddy
On Jan 19, 1:08 am, Carl Banks <[EMAIL PROTECTED]> wrote:
>
> Ten minutes later I saw it mentioned it on comp.lang.python.

It's almost as if you looking for it made it popular :-)

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


ANN: pyharu-2.0.8, the interface to libharu(PDF generate lib)

2008-01-18 Thread oyster
Pyharu is a pure python interface to haru(Haru Free PDF Library,
http://libharu.sourceforge.net/) via ctypes. All of the C API is
usable. All the example programs in haru C src has been ported. It
(should) run on windows/linux without modification.

Pyharu is only 1M, which is small and easy to use than ReportLab.

Download and discuss, please go to http://groups.google.com/group/pythoncia
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unique thread ID

2008-01-18 Thread Benjamin
On Jan 18, 8:31 pm, "Gabriel Genellina" <[EMAIL PROTECTED]>
wrote:
> En Fri, 18 Jan 2008 22:41:47 -0300, Benjamin <[EMAIL PROTECTED]>
> escribió:
>
> > On Jan 18, 2:31 am, Christian Heimes <[EMAIL PROTECTED]> wrote:
> >> Benjamin wrote:
> >> > Is there a way to obtain a unique ID for the current thread? I have an
> >> > object that I need to store local thread data in, and I don't want to
> >> > use threading.local because each thread might have multiple instances
> >> > of my object.
>
> >> threading.get_ident() but please use threading.local. Nobody is going to
> >> stop you if you use a list or dict in threading.local.
> > then, I have to figure out how to represent an instance of my object
> > in threading.local. (The data also won't be garbage collect when my
> > object is, will it?) I think the unique id is elegant in this case.
>
> I think you didn't get how threading.local works yet. It's a lot easier
> than you imply. Just store your instances as attributes of a
> threading.local object. You may use a list or dictionary if you want
> multiple instances.
> Read _threading_local.py, it contains a lot of examples.
You're correct. I misread the documentation. I now understand how it
works and am using it. Thanks for pointing me down the right path.
>
> --
> Gabriel Genellina

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


Re: Unique thread ID

2008-01-18 Thread Gabriel Genellina
En Fri, 18 Jan 2008 22:41:47 -0300, Benjamin <[EMAIL PROTECTED]>  
escribió:

> On Jan 18, 2:31 am, Christian Heimes <[EMAIL PROTECTED]> wrote:
>> Benjamin wrote:
>> > Is there a way to obtain a unique ID for the current thread? I have an
>> > object that I need to store local thread data in, and I don't want to
>> > use threading.local because each thread might have multiple instances
>> > of my object.
>>
>> threading.get_ident() but please use threading.local. Nobody is going to
>> stop you if you use a list or dict in threading.local.
> then, I have to figure out how to represent an instance of my object
> in threading.local. (The data also won't be garbage collect when my
> object is, will it?) I think the unique id is elegant in this case.

I think you didn't get how threading.local works yet. It's a lot easier  
than you imply. Just store your instances as attributes of a  
threading.local object. You may use a list or dictionary if you want  
multiple instances.
Read _threading_local.py, it contains a lot of examples.

-- 
Gabriel Genellina

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


Re: I don't understand what is happening in this threading code

2008-01-18 Thread Sergio Correia
This is what's happening:

 1) The chef thread releases the sema
 2) While the chef thread is saying "Andiamo", decreasing "i", and
ending the while loop, the waiter thread SERVES the dish and RUNS to
reacquire the lock
 3) Back in the main loop, chef.join() is run, and then the waiter's
variable is changed.

But it's already too late. The waiter is already locked. He'll wait
and wait forever for another dish, which will never come. So you will
have to kill him. All for a race condition.

---
I've just read Carl's post (just b4 hitting submit).

I agree with him, rewrite the logic. IMO, the objects should be the
key, THEY are the ones that should be joined. You should run the chef
and waiter as daemons, and just do something like

orders.join()
dishes.join()

And when the chefs finishes with the orders and the waiter finishes
serving them, the program ends.

HTH,
Sergio


On Jan 18, 2008 7:43 PM, Matthew Wilson <[EMAIL PROTECTED]> wrote:
> In this code, I tried to kill my thread object by setting a variable on it
> to False.
>
> Inside the run method of my thread object, it checks a different
> variable.
>
> I've already rewritten this code to use semaphores, but I'm just curious
> what is going on.
>
> Here's the code:
>
> import logging, threading, time
> logging.basicConfig(level=logging.DEBUG,
> format="%(threadName)s: %(message)s")
>
> class Waiter(threading.Thread):
> def __init__(self, hot_food):
> super(Waiter, self).__init__()
> self.should_keep_running = True
> self.hot_food = hot_food
>
> def run(self):
> while self.should_keep_running:
> logging.debug("Inside run, the id of should_keep_running is %s."
>   % id(self.should_keep_running))
> self.hot_food.acquire()
>
> def cook_food(hot_food):
> i = 5
> while i >= 0:
> logging.debug("I am cooking food...")
> time.sleep(1)
> hot_food.release()
> logging.debug("Andiamo!")
> i -= 1
>
> def main():
>
> hot_food = threading.Semaphore(value=0)
>
> chef = threading.Thread(name="chef", target=cook_food, args=(hot_food, ))
> chef.start()
>
> w = Waiter(hot_food)
> logging.debug("Initially, the id of w.should_keep_running is %s."
>   % id(w.should_keep_running))
> w.start()
> logging.debug("After start, the id of w.should_keep_running is %s."
>   % id(w.should_keep_running))
>
> # Wait for the chef to finish work.
> chef.join()
>
> # Now try to kill off the waiter by setting a variable inside the waiter.
> w.should_keep_running = False
> logging.debug("Now, the id of w.should_keep_running is %s."
>   % id(w.should_keep_running))
>
> if __name__ == "__main__":
> main()
>
> And here's what I get when I execute it.  I have to suspend the process
> with CTRL=Z and then kill -9 it.
>
> $ python foo.py
> MainThread: Initially, the id of w.should_keep_running is 135527852.
> MainThread: After start, the id of w.should_keep_running is 135527852.
> chef: I am cooking food...
> Thread-1: Inside run, the id of should_keep_running is 135527852.
> chef: Andiamo!
> chef: I am cooking food...
> Thread-1: Inside run, the id of should_keep_running is 135527852.
> chef: Andiamo!
> chef: I am cooking food...
> Thread-1: Inside run, the id of should_keep_running is 135527852.
> chef: Andiamo!
> chef: I am cooking food...
> Thread-1: Inside run, the id of should_keep_running is 135527852.
> chef: Andiamo!
> chef: I am cooking food...
> Thread-1: Inside run, the id of should_keep_running is 135527852.
> chef: Andiamo!
> chef: I am cooking food...
> Thread-1: Inside run, the id of should_keep_running is 135527852.
> chef: Andiamo!
> Thread-1: Inside run, the id of should_keep_running is 135527852.
> MainThread: Now, the id of w.should_keep_running is 135527840.
>
> [1]+  Stopped python foo.py
>
> $ kill -9 %1
>
> [1]+  Stopped python foo.py
>
> The memory address of should_keep_running seems to change when I set it
> from True to False, and inside the run method, I keep checking the old
> location.
>
> I am totally baffled what this means.
>
> Like I said earlier, I already rewrote this code to use semaphores, but
> I just want to know what is going on here.
>
> Any explanation is welcome.
>
> TIA
>
> Matt
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: I don't understand what is happening in this threading code

2008-01-18 Thread Carl Banks
On Jan 18, 7:43 pm, Matthew Wilson <[EMAIL PROTECTED]> wrote:
> In this code, I tried to kill my thread object by setting a variable on it
> to False.
>
> Inside the run method of my thread object, it checks a different
> variable.
>
> I've already rewritten this code to use semaphores, but I'm just curious
> what is going on.
>
> Here's the code:
>
> import logging, threading, time
> logging.basicConfig(level=logging.DEBUG,
> format="%(threadName)s: %(message)s")
>
> class Waiter(threading.Thread):
> def __init__(self, hot_food):
> super(Waiter, self).__init__()
> self.should_keep_running = True
> self.hot_food = hot_food
>
> def run(self):
> while self.should_keep_running:
> logging.debug("Inside run, the id of should_keep_running is %s."
>   % id(self.should_keep_running))
> self.hot_food.acquire()
>
> def cook_food(hot_food):
> i = 5
> while i >= 0:
> logging.debug("I am cooking food...")
> time.sleep(1)
> hot_food.release()
> logging.debug("Andiamo!")
> i -= 1
>
> def main():
>
> hot_food = threading.Semaphore(value=0)
>
> chef = threading.Thread(name="chef", target=cook_food, args=(hot_food, ))
> chef.start()
>
> w = Waiter(hot_food)
> logging.debug("Initially, the id of w.should_keep_running is %s."
>   % id(w.should_keep_running))
> w.start()
> logging.debug("After start, the id of w.should_keep_running is %s."
>   % id(w.should_keep_running))
>
> # Wait for the chef to finish work.
> chef.join()
>
> # Now try to kill off the waiter by setting a variable inside the waiter.
> w.should_keep_running = False
> logging.debug("Now, the id of w.should_keep_running is %s."
>   % id(w.should_keep_running))
>
> if __name__ == "__main__":
> main()


It looks like your program's hanging while the waiter is waits to
acquire another plate of hot food.

Quick and dirty solution is to have waiter timeout at intervals while
waiting for the chef and check the clock to see if his shift has
ended.  Better solution is to rewrite the logic, which you did.


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


Re: Unique thread ID

2008-01-18 Thread Benjamin
On Jan 18, 2:31 am, Christian Heimes <[EMAIL PROTECTED]> wrote:
> Benjamin wrote:
> > Is there a way to obtain a unique ID for the current thread? I have an
> > object that I need to store local thread data in, and I don't want to
> > use threading.local because each thread might have multiple instances
> > of my object.
>
> threading.get_ident() but please use threading.local. Nobody is going to
> stop you if you use a list or dict in threading.local.
then, I have to figure out how to represent an instance of my object
in threading.local. (The data also won't be garbage collect when my
object is, will it?) I think the unique id is elegant in this case.
>
> Christian

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


Re: TopSort in Python?

2008-01-18 Thread Carl Banks
On Jan 18, 7:01 pm, Paddy <[EMAIL PROTECTED]> wrote:
> On Jan 18, 9:47 pm, [EMAIL PROTECTED] wrote:> Tim,
>
> > Thanks for the topsort code.  It would be useful in a project I'm
> > working on.  Can I use the code for free under public domain?  Thanks!
>
> When I needed one I didn't know the name. I'm curious, how did you
> know to look for the topological sort algorithm by name?

I spent quite a bit of time looking for this one myself.  It was quite
a stumper.  Sometimes Google gets us in the habit of just firing
random search terms when we ought to be thinking it out.

After quite of bit of dead end searching--like a week--I stepped back,
thought about what I was looking for.  I wanted a sort of dependency
system: A must happen before B.  What other programs do that?  make,
of course.  I looked at the documents for make and found the term
"directed acyclic graph", and pretty much instantly knew I had it.
(It seems silly in retrospect that I didn't think of graphs before
that, but then it also seems silly no one invented movable type before
1436.)

Once I had that term, a quick Google search led me to the Wikipedia
article, which led me to the topsort algorithm.  I did a dance of joy.


Ten minutes later I saw it mentioned it on comp.lang.python.


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


Re: Efficient processing of large nuumeric data file

2008-01-18 Thread bearophileHUGS
...and just for fun this D code is about 3.2 times faster than the
Psyco version for the same dataset (30% lines with a space):


import std.stdio, std.conv, std.string, std.stream;

int[int] get_hist(string file_name) {
int[int] hist;

foreach(string line; new BufferedFile(file_name)) {
int pos = find(line, ' ');
if (pos == -1)
hist[toInt(line)]++;
else
hist[toInt(line[0 .. pos])] += toInt(line[pos+1 .. $]);
}

return hist;
}

void main(string[] args) {
writefln( get_hist(args[1]).length );
}


Bye,
bearophile
-- 
http://mail.python.org/mailman/listinfo/python-list


I don't understand what is happening in this threading code

2008-01-18 Thread Matthew Wilson
In this code, I tried to kill my thread object by setting a variable on it
to False.

Inside the run method of my thread object, it checks a different
variable.

I've already rewritten this code to use semaphores, but I'm just curious
what is going on.

Here's the code:

import logging, threading, time
logging.basicConfig(level=logging.DEBUG,
format="%(threadName)s: %(message)s")

class Waiter(threading.Thread):
def __init__(self, hot_food):
super(Waiter, self).__init__()
self.should_keep_running = True
self.hot_food = hot_food

def run(self):
while self.should_keep_running:
logging.debug("Inside run, the id of should_keep_running is %s." 
  % id(self.should_keep_running))
self.hot_food.acquire()

def cook_food(hot_food):
i = 5
while i >= 0: 
logging.debug("I am cooking food...")
time.sleep(1)
hot_food.release()
logging.debug("Andiamo!")
i -= 1

def main():

hot_food = threading.Semaphore(value=0)

chef = threading.Thread(name="chef", target=cook_food, args=(hot_food, ))
chef.start()

w = Waiter(hot_food)
logging.debug("Initially, the id of w.should_keep_running is %s." 
  % id(w.should_keep_running))
w.start()
logging.debug("After start, the id of w.should_keep_running is %s." 
  % id(w.should_keep_running))

# Wait for the chef to finish work.
chef.join()

# Now try to kill off the waiter by setting a variable inside the waiter.
w.should_keep_running = False
logging.debug("Now, the id of w.should_keep_running is %s." 
  % id(w.should_keep_running))

if __name__ == "__main__":
main()

And here's what I get when I execute it.  I have to suspend the process
with CTRL=Z and then kill -9 it.

$ python foo.py
MainThread: Initially, the id of w.should_keep_running is 135527852.
MainThread: After start, the id of w.should_keep_running is 135527852.
chef: I am cooking food...
Thread-1: Inside run, the id of should_keep_running is 135527852.
chef: Andiamo!
chef: I am cooking food...
Thread-1: Inside run, the id of should_keep_running is 135527852.
chef: Andiamo!
chef: I am cooking food...
Thread-1: Inside run, the id of should_keep_running is 135527852.
chef: Andiamo!
chef: I am cooking food...
Thread-1: Inside run, the id of should_keep_running is 135527852.
chef: Andiamo!
chef: I am cooking food...
Thread-1: Inside run, the id of should_keep_running is 135527852.
chef: Andiamo!
chef: I am cooking food...
Thread-1: Inside run, the id of should_keep_running is 135527852.
chef: Andiamo!
Thread-1: Inside run, the id of should_keep_running is 135527852.
MainThread: Now, the id of w.should_keep_running is 135527840.

[1]+  Stopped python foo.py

$ kill -9 %1

[1]+  Stopped python foo.py

The memory address of should_keep_running seems to change when I set it
from True to False, and inside the run method, I keep checking the old
location.

I am totally baffled what this means.

Like I said earlier, I already rewrote this code to use semaphores, but
I just want to know what is going on here.

Any explanation is welcome.

TIA

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


Re: Efficient processing of large nuumeric data file

2008-01-18 Thread bearophileHUGS
Matt:

> from collections import defaultdict
>
> def get_hist(file_name):
> hist = defaultdict(int)
> f = open(filename,"r")
> for line in f:
> vals = line.split()
> val = int(vals[0])
> try: # don't look to see if you will cause an error,
>  # just cause it and then deal with it
> cnt = int(vals[1])
> except IndexError:
> cnt = 1
> hist[val] += cnt
> return hist


But usually in tight loops exceptions slow down the Python code, so
this is quite faster (2.5 times faster with Psyco, about 2 times
without, with  about 30% of lines with a space in it):

import psyco
from collections import defaultdict

def get_hist(file_name):
hist = defaultdict(int)

for line in open(file_name):
if " " in line:
pair = line.split()
hist[int(pair[0])] += int(pair[1])
else:
hist[int(line)] += 1

return hist

psyco.bind(get_hist)

It doesn't work if lines may contain spurious spaces...

Bye,
bearophile
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: TopSort in Python?

2008-01-18 Thread Paul Rubin
Paddy <[EMAIL PROTECTED]> writes:
> When I needed one I didn't know the name. I'm curious, how did you
> know to look for the topological sort algorithm by name?

It's a well known algorithm in computer science, described in any
number of textbooks, for example probably 

   http://projects.csail.mit.edu/clrs/ 

There is also a wikipedia article:

  http://en.wikipedia.org/wiki/topological_sorting

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


Re: Core Python Programming . . .

2008-01-18 Thread Yu-Xi Lim
Mike Driscoll wrote:
> 
> 6-11 Conversion.
>   (a) Create a program that will convert from an integer to an
> Internet Protocol (IP) address in the four-octet format of WWW.XXX.YYY.ZZZ
>   (b) Update your program to be able to do the vice verse of the
> above.

I think it's is asking to convert a 32-bit int to the dotted form.

It's a little known fact, but IP addresses are valid in non-dotted 
long-int form.  Spammers commonly use this trick to disguise their IP 
addresses in emails from scanners.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: TopSort in Python?

2008-01-18 Thread Paddy
On Jan 18, 9:47 pm, [EMAIL PROTECTED] wrote:
> Tim,
>
> Thanks for the topsort code.  It would be useful in a project I'm
> working on.  Can I use the code for free under public domain?  Thanks!
>
When I needed one I didn't know the name. I'm curious, how did you
know to look for the topological sort algorithm by name?

(http://paddy3118.blogspot.com/2007/10/whats-in-name.html)

- Paddy.

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


Re: strange syntax rules on list comprehension conditions

2008-01-18 Thread Dustan
On Jan 18, 1:04 pm, "Chris Mellon" <[EMAIL PROTECTED]> wrote:
> On Jan 18, 2008 12:53 PM, Nicholas <[EMAIL PROTECTED]> wrote:
>
> > I was quite delighted today, after extensive searches yielded nothing, to
> > discover how to place an else condition in a list comprehension.
> > Trivial mask example:
> > >>> [True if i <5 else False for i in range(10)]   # A
> > [True, True, True, True, True, False, False, False, False, False]
>
> > I then experimented to drop the else statement which yields an error
> > >>> [i if i>3 for i in range(10)]

That would be:

[i for i in range(10) if i>3]

> > Traceback (  File "", line 1
> > this syntax works of course
> > >>> [i if i>3 else i for i in range(10)]
> > [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What is a shortcut to the Default home directory in Windows

2008-01-18 Thread Christian Heimes
Mike Driscoll wrote:
> I personally use Tim Golden's excellent win32 API wrapper, the
> winshell script. You can find it here:
> 
> http://timgolden.me.uk/python/winshell.html

Yeah. Tim's winshell is fine but it's not using the official win32 api.
However Python 2.6 will get an easier way to get a mapping of ids to
shell folders: os.path.getshellfolders() -> dict

Christian

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


Re: Using "pickle" for interprocess communication - some notes and things that ought to be documented.

2008-01-18 Thread Paul Boddie
On 18 Jan, 07:32, John Nagle <[EMAIL PROTECTED]> wrote:
>
> "Processing" is useful, but it uses named pipes and sockets,
> not ordinary pipes.  Also, it has C code, so all the usual build
> and version problems apply.

The pprocess module uses pickles over sockets, mostly because the
asynchronous aspects of the communication only appear to work reliably
with sockets. See here for the code:

http://www.python.org/pypi/pprocess

Unlike your approach, pprocess employs the fork system call. In
another project of mine - jailtools - I use some of the pprocess
functionality with the subprocess module:

http://www.python.org/pypi/jailtools

I seem to recall that a few things are necessary when dealing with
subprocesses, especially those which employ the python executable:
running in unbuffered mode is one of those things.

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


Re: Efficient processing of large nuumeric data file

2008-01-18 Thread Steven D'Aprano
On Fri, 18 Jan 2008 09:58:57 -0800, Paul Rubin wrote:

> David Sanders <[EMAIL PROTECTED]> writes:
>> The data files are large (~100 million lines), and this code takes a
>> long time to run (compared to just doing wc -l, for example).
> 
> wc is written in carefully optimized C and will almost certainly run
> faster than any python program.

However, wc -l doesn't do the same thing as what the Original Poster is 
trying to do. There is little comparison between counting the number of 
lines and building a histogram, except that both tasks have to see each 
line. Naturally the second task will take longer compared to wc.

("Why does it take so long to make a three-tier wedding cake? I can boil 
an egg in three minutes!!!")


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


Re: Efficient processing of large nuumeric data file

2008-01-18 Thread Steven D'Aprano
On Fri, 18 Jan 2008 12:06:56 -0600, Tim Chase wrote:

> I don't know how efficient len() is (if it's internally linearly
> counting the items in data, or if it's caching the length as data is
> created/assigned/modifed)

It depends on what argument you pass to len().

Lists, tuples and dicts (and maybe strings?) know how long they are, so 
len() takes essentially constant time.

Custom classes are free to define __len__ anyway they like.

class MyList(list):
"""Just like the built-in list, but stupider."""
def __len__(self):
# Untested, for obvious reasons.
import random
import sys
while True:
guess = random.randrange(0, sys.maxint)
count = 0
for i in self:
count += 1
if count == guess:
return count


Caching __len__ is left as an exercise to the reader...



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


Re: Is this a bug, or is it me?

2008-01-18 Thread Steven D'Aprano
On Fri, 18 Jan 2008 11:01:29 -0800, cptnwillard wrote:

> Now here is another one for your enjoyment:
> 
> class C:
>   @staticmethod
>   def f1(): pass
>   F = { '1' : f1 }
> 
> C().F['1']()
> 
 TypeError: 'staticmethod' object is not callable
> 
> 
> What do you think of this one?


The trick is to realise that the object stored in the dict is not the 
same object that you get back when you call C.f1().


>>> C().f1 is C().F['1']
False
>>> C().f1

>>> C().F['1']



What you've done in inadvertently bypassed Python's method-access 
mechanism:

>>> C.__dict__['f1'] is C().f1
False
>>> C.__dict__['f1'] is C().F['1']
True


Analogous results will occur without the decorator (although the error 
you get is different):

>>> class D:
... def foo(self): pass
... F = {'oo': foo}
...
>>> D().foo()
>>> D.F['oo']()
Traceback (most recent call last):
  File "", line 1, in 
TypeError: foo() takes exactly 1 argument (0 given)


So... not a bug, a feature :)




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


Re: strange syntax rules on list comprehension conditions

2008-01-18 Thread Paul McGuire
On Jan 18, 1:04 pm, "Chris Mellon" <[EMAIL PROTECTED]> wrote:
> On Jan 18, 2008 12:53 PM, Nicholas <[EMAIL PROTECTED]> wrote:
>
> > I was quite delighted today, after extensive searches yielded nothing, to
> > discover how to place an else condition in a list comprehension.
> > Trivial mask example:
> > >>> [True if i <5 else False for i in range(10)]       # A
> > [True, True, True, True, True, False, False, False, False, False]
>

I think this would be preferred over your ternary-ish expression:

>>> [ i<5 for i in range(10) ]
[True, True, True, True, True, False, False, False, False, False]

Do you also write code like:

if i<5 == True:
blah...

If so, please just write:

if i<5:
better blah...

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


Re: Is this a bug, or is it me?

2008-01-18 Thread Peter Otten
cptnwillard wrote:

> I filed a bug report, and here is the short answer to my question:
> genexps are code blocks, and code blocks cannot see variables in class
> scopes. Congrats to Neil Cerutti who figured it out.
> 
> Now here is another one for your enjoyment:
> 
> class C:
>   @staticmethod
>   def f1(): pass
>   F = { '1' : f1 }
> 
> C().F['1']()
> 
 TypeError: 'staticmethod' object is not callable
> 
> 
> What do you think of this one?

If you want it to be callable you can subclass:

>>> class static(staticmethod):
... def __call__(self, *args, **kw):
... return self.__get__(object)(*args, **kw)
... 
>>> class A(object):
... @static
... def f(x="yadda"): print x
... f()
... 
yadda
>>> A.f()
yadda
>>> A().f()
yadda

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


Re: What is a shortcut to the Default home directory in Windows

2008-01-18 Thread Mike Driscoll
On Jan 18, 2:19 pm, Daniel Folkes <[EMAIL PROTECTED]> wrote:
> I am trying to write a file to the users file system.
>
> I need it to be in there home directory in WINDOWS.  I know there is a
> "shortcut" to home in Linux("~"), but is there an equivalent to that
> in windows.  Or to get to their "Documents and Settings" directory?
>
> Thanks in advance for the help.
>
> -Daniel Folkes

I personally use Tim Golden's excellent win32 API wrapper, the
winshell script. You can find it here:

http://timgolden.me.uk/python/winshell.html

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


Re: Core Python Programming . . .

2008-01-18 Thread Mike Driscoll
On Jan 18, 1:55 pm, Paul Rubin  wrote:
> FireNWater <[EMAIL PROTECTED]> writes:
> > 1) convert a 4-digit Integer (WXYZ) to an IP address (WWW.XXX.YYY.ZZZ)
>
> > or
>
> > 2) convert an 8-digit Integer (WWWXXXYYYZZZ) to (WWW.XXX.YYY.ZZZ)
>
> > Thanks for anyone with the clue!!!
>
> Without being able to see the exercise I suspect it's turn a 4-byte
> string (i.e. 32 bits) into an IP address (int8.int8.int8.int8).
> Or, it might be turn a 32-bit int into such an address.

I've got the book and I think the OP may be referring to this:

6-11 Conversion.
  (a) Create a program that will convert from an integer to an
Internet Protocol (IP) address in the four-octet format of WWW.XXX.YYY.ZZZ
  (b) Update your program to be able to do the vice verse of the
above.


It could be a 12 digit int...or it could be what you (Paul) are
referring to.

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


Re: Is this a bug, or is it me?

2008-01-18 Thread Neil Cerutti
On 1/18/08, Ross Ridge <[EMAIL PROTECTED]> wrote:
> Neil Cerutti <[EMAIL PROTECTED]> wrote:
> >The decoration is setting the class type's f1 attribute correctly, but
> >doing something strange in the local namespace.
> >
>  class C:
> >...   @staticmethod
> >...   def f1(): pass
> >...   print f1
> >...
> >
>  print C.f1
> >
> >The class statement's local namespace is pretty strange. I think I
> >mightl go back to pretending there isn't one.
>
> When class attributes are referenced, functions are turned into unbound
> methods and staticmethod objects get turned into functions.

OK, thank you. So the local namespace is just fine, you just can't
call the staticmethod using that name directly.

-- 
Neil Cerutti <[EMAIL PROTECTED]>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: TopSort in Python?

2008-01-18 Thread startech84
Tim,

Thanks for the topsort code.  It would be useful in a project I'm
working on.  Can I use the code for free under public domain?  Thanks!

On Jun 30 1999, 11:00 pm, "Tim Peters"
<[EMAIL PROTECTED]> wrote:
> From: "Tim Peters" <[EMAIL PROTECTED]>
>
> [Dinu C. Gherman]
>
> > Does anybody have a simple-minded-but-working full-Python
> > implementation of topsort, the topological sorting algorithm?
> > Or maybe *any* topsort? I remember only one such algorithm...
> > python.org/search doesn't reveal any...
>
> Searching for "topological" instead turns up two hits there, a few more on
> DejaNews.  Apparently "the std" algorithm I posted years ago predates
> DejaNews, though.  So here's a fancier version ...
>
> check-out-aaron's-kjbuckets-too-ly y'rs  - tim
>
> # topsort takes a list of pairs, where each pair (x, y) is taken to
> # mean that x <= y wrt some abstract partial ordering.  The return
> # value is a list, representing a total ordering that respects all
> # the input constraints.
> # E.g.,
> #topsort( [(1,2), (3,3)] )
> # may return any of (but nothing other than)
> #[3, 1, 2]
> #[1, 3, 2]
> #[1, 2, 3]
> # because those are the permutations of the input elements that
> # respect the "1 precedes 2" and "3 precedes 3" input constraints.
> # Note that a constraint of the form (x, x) is really just a trick
> # to make sure x appears *somewhere* in the output list.
> #
> # If there's a cycle in the constraints, say
> #topsort( [(1,2), (2,1)] )
> # then CycleError is raised, and the exception object supports
> # many methods to help analyze and break the cycles.  This requires
> # a good deal more code than topsort itself!
>
> from exceptions import Exception
>
> class CycleError(Exception):
> def __init__(self, sofar, numpreds, succs):
> Exception.__init__(self, "cycle in constraints",
>sofar, numpreds, succs)
> self.preds = None
>
> # return as much of the total ordering as topsort was able to
> # find before it hit a cycle
> def get_partial(self):
> return self[1]
>
> # return remaining elt -> count of predecessors map
> def get_pred_counts(self):
> return self[2]
>
> # return remaining elt -> list of successors map
> def get_succs(self):
> return self[3]
>
> # return remaining elements (== those that don't appear in
> # get_partial())
> def get_elements(self):
> return self.get_pred_counts().keys()
>
> # Return a list of pairs representing the full state of what's
> # remaining (if you pass this list back to topsort, it will raise
> # CycleError again, and if you invoke get_pairlist on *that*
> # exception object, the result will be isomorphic to *this*
> # invocation of get_pairlist).
> # The idea is that you can use pick_a_cycle to find a cycle,
> # through some means or another pick an (x,y) pair in the cycle
> # you no longer want to respect, then remove that pair from the
> # output of get_pairlist and try topsort again.
> def get_pairlist(self):
> succs = self.get_succs()
> answer = []
> for x in self.get_elements():
> if succs.has_key(x):
> for y in succs[x]:
> answer.append( (x, y) )
> else:
> # make sure x appears in topsort's output!
> answer.append( (x, x) )
> return answer
>
> # return remaining elt -> list of predecessors map
> def get_preds(self):
> if self.preds is not None:
> return self.preds
> self.preds = preds = {}
> remaining_elts = self.get_elements()
> for x in remaining_elts:
> preds[x] = []
> succs = self.get_succs()
>
> for x in remaining_elts:
> if succs.has_key(x):
> for y in succs[x]:
> preds[y].append(x)
>
> if __debug__:
> for x in remaining_elts:
> assert len(preds[x]) > 0
> return preds
>
> # return a cycle [x, ..., x] at random
> def pick_a_cycle(self):
> remaining_elts = self.get_elements()
>
> # We know that everything in remaining_elts has a predecessor,
> # but don't know that everything in it has a successor.  So
> # crawling forward over succs may hit a dead end.  Instead we
> # crawl backward over the preds until we hit a duplicate, then
> # reverse the path.
> preds = self.get_preds()
> from random import choice
> x = choice(remaining_elts)
> answer = []
> index = {}
> in_answer = index.has_key
> while not in_answer(x):
> index[x] = len(answer) # index of x in answer
> answer.append(x)
> x = choice(preds[x])
> answer.append(x)
> answer = answer[index[x]:]
> answer.reverse()
> return answer
>
> def topsort(pairlist

Re: Bit Scan Forward and Reverse

2008-01-18 Thread [EMAIL PROTECTED]
On Jan 18, 2:01 pm, Thomas Dybdahl Ahle <[EMAIL PROTECTED]> wrote:
> Hi, I'm writing an app in python, and I'm storing some a lot of data in
> bitmaps.
> I need a way to find the first or latest set bit in a 64bit number, and
> for that I've implemented a small routine.
>
> Thing is that this routine is not as fast as I'd wish. I know most
> processors implement BSF and BSR calls to do this efficiently.
> Is there anyway to access those calls from python?
>
> I'd still have my routine as a fallback on nonsupporting architectures.

Get a copy of the gmpy module.

Help on module gmpy:

NAME
gmpy
FILE
c:\program files\pygtk\python\lib\site-packages\gmpy.pyd
DESCRIPTION
gmpy 1.04 - General Multiprecision arithmetic for PYthon:
exposes functionality from the GMP 4 library to Python 2.{2,3,4}.

Allows creation of multiprecision integer (mpz), float (mpf),
and rational (mpq) numbers, conversion between them and to/from
Python numbers/strings, arithmetic, bitwise, and some other
higher-level mathematical operations; also, pretty good-quality
linear-congruential random number generation and shuffling.

mpz has comparable functionality to Python's builtin longs, but
can be faster for some operations (particularly multiplication
and raising-to-power) and has many further useful and speedy
functions (prime testing and generation, factorial, fibonacci,
binary-coefficients, gcd, lcm, square and other roots, ...).

mpf and mpq only offer basic arithmetic abilities, but they
do add the ability to have floating-point numbers ensuring at
least a predefined number of bits' worth of precision (and with
potentially-huge or extremely-tiny magnitudes), as well as
unlimited-precision rationals, with reasonably-fast operations,
which are not built-in features of Python.

FUNCTIONS (selected operations for binary use)

getbit(...)
getbit(x,n): returns 0 or 1, the bit-value of bit n of x;
n must be an ordinary Python int, >=0; x is an mpz, or else
gets coerced to one.

hamdist(...)
hamdist(x,y): returns the Hamming distance (number of bit-
positions
where the bits differ) between x and y.  x and y must be mpz,
or else
get coerced to mpz.

lowbits(...)
lowbits(x,n): returns the n lowest bits of x; n must be an
ordinary Python int, >0; x must be an mpz, or else gets
coerced to one.

popcount(...)
popcount(x): returns the number of 1-bits set in x; note that
this is 'infinite' if x<0, and in that case, -1 is returned.
x must be an mpz, or else gets coerced to one.

scan0(...)
scan0(x, n=0): returns the bit-index of the first 0-bit of x
(that
is at least n); n must be an ordinary Python int, >=0.  If no
more
0-bits are in x at or above bit-index n (which can only happen
for
x<0, notionally extended with infinite 1-bits), None is
returned.
x must be an mpz, or else gets coerced to one.

scan1(...)
scan1(x, n=0): returns the bit-index of the first 1-bit of x
(that
is at least n); n must be an ordinary Python int, >=0.  If no
more
1-bits are in x at or above bit-index n (which can only happen
for
x>=0, notionally extended with infinite 0-bits), None is
returned.
x must be an mpz, or else gets coerced to one.

setbit(...)
setbit(x,n,v=1): returns a copy of the value of x, with bit n
set
to value v; n must be an ordinary Python int, >=0; v, 0 or !
=0;
x must be an mpz, or else gets coerced to one.

These work quite nicely. I use scan1() in the following idiom
which removes all factors of 2 in one fell swoop.

n = 3*n + 1   # always even, but how even?
f = gmpy.scan1(n) # bit position of LS 1-bit
n = n >> f# ...which is number of 0-bits



>
> --
> Med venlig hilsen,
> Best regards,
> Thomas

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


Re: Is this a bug, or is it me?

2008-01-18 Thread Ross Ridge
Neil Cerutti <[EMAIL PROTECTED]> wrote:
>The decoration is setting the class type's f1 attribute correctly, but
>doing something strange in the local namespace.
>
 class C:
>...   @staticmethod
>...   def f1(): pass
>...   print f1
>...
>
 print C.f1
>

It might help understand the problem if you do the something similar
without using @staticmethod:

class C:
def f1(): pass
print "f1", f1

print "C.f1", C.f1
print "C().f1", C().f1

You should see output something like:

f1 
C.f1 
C().f1 >

>The class statement's local namespace is pretty strange. I think I
>mightl go back to pretending there isn't one.

When class attributes are referenced, functions are turned into unbound
methods and staticmethod objects get turned into functions.

Something like the following should work:

class C:
def f1(): pass
F = {'1': f1}
f1 = staticmethod(f1)   # if you need C.f1() to work as well

If you don't need C.f1() to work you can replace the last line with
"del f1".

Ross Ridge

-- 
 l/  //   Ross Ridge -- The Great HTMU
[oo][oo]  [EMAIL PROTECTED]
-()-/()/  http://www.csclub.uwaterloo.ca/~rridge/ 
 db  //   
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: "Code Friendly" Blog?

2008-01-18 Thread Hai Vu
Miki,
Why don't you try to use Code Colorizer:
http://www.chamisplace.com/colorizer/cc.asp

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


Re: Using "pickle" for interprocess communication - some notes and things that ought to be documented.

2008-01-18 Thread John Nagle
Carl Banks wrote:
> On Jan 17, 2:28 pm, John Nagle <[EMAIL PROTECTED]> wrote:

> 
>> It's also necessary to call Pickle's "clear_memo" before each "dump"
>> call, since objects might change between successive "dump" calls.
>> "Unpickle" doesn't have a "clear_memo" function.  It should, because
>> if you keep reusing the "Unpickle" object, the memo dictionary
>> fills up with old objects which can't be garbage collected.
>> This creates a memory leak in long-running programs.
> 
> This is all good to know.  I agree that this is a good use case for a
> clear_memo on a pickle unloader.

reader = pickle.Unpickler(self.datain)  # set up reader

reader.memo = {}# no memory from cycle to cycle

> 
> 
>> Then, on Windows, there's a CR LF problem. This can be fixed by
>> launching the subprocess with
>>
>> proc = subprocess.Popen(launchargs,
>> stdin=subprocess.PIPE, stdout=subprocess.PIPE,
>> universal_newlines=True)
>>
>> Failure to do this produces the useful error message "Insecure string 
>> pickle".
>> Binary "pickle" protocol modes won't work at all in this situation; 
>> "universal
>> newline" translation is compatible, not transparent.  On Unix/Linux, this
>> just works, but the code isn't portable.
> 
> I would think a better solution would be to use the -u switch to
> launch the subprocess, or the PYTHONUNBUFFERED environment variable if
> you want to invoke the Python script directly.  It opens up stdin and
> stdout in binary, unbuffered mode.

Ah.  That works.  I wasn't aware that "unbuffered" mode also implied
binary transparency.  I did that, and now cPickle works in both text (0)
and binary (2) protocol modes.  Turned off "Universal Newline" mode.

> Thanks: this was an informative post

Thanks.  We have this working well now.  After a while, I'll publish
the module, which is called "subprocesscall.py".

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


Re: Bit Scan Forward and Reverse

2008-01-18 Thread Matimus
On Jan 18, 12:01 pm, Thomas Dybdahl Ahle <[EMAIL PROTECTED]> wrote:
> Hi, I'm writing an app in python, and I'm storing some a lot of data in
> bitmaps.
> I need a way to find the first or latest set bit in a 64bit number, and
> for that I've implemented a small routine.
>
> Thing is that this routine is not as fast as I'd wish. I know most
> processors implement BSF and BSR calls to do this efficiently.
> Is there anyway to access those calls from python?
>
> I'd still have my routine as a fallback on nonsupporting architectures.
>
> --
> Med venlig hilsen,
> Best regards,
> Thomas

There isn't a simple way, but you have a couple of options:

1. put the code in a dll and call it from ctypes.
2. see extending and embedding in the documentation.

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


IRC bot threading dilemma

2008-01-18 Thread bakermi
Hello,

I have coded an IRC bot in Python. Each inbound packet is parsed, and
once the bot decides whether it is a command directed at the bot or
not, it will either
discard the packet or make a function call to an access control
checker function. If the invoking user is found to have sufficient
access to run this command, another function call is made. This
function call depends on what command the user in question has
invoked.

Try to imagine making one of these functions send out a WHOIS query to
the server, and wait for the burst of WHOIS-response packets from it.
How would this function wait? Yes, I know, I am going to have to
create another thread to loop while it waits for this reply packet.
The problem is, I can't simply tell my command function to loop until
the "waiter thread" raises a flag to say that it has received the
packet. During this looping time, program flow would be stuck here,
thus preventing the bot from replying to any vital duties such as
"pingponging" (server verifies that you are still there by sending
"PING" and expects a "PONG" in return.)

For this reason I was thinking: do you think I should run a new thread
whenever a new command is invoked by a user? And have the thread
delete itself when it's completed execution? This way the bot would
*always* be free to do its duties.

Any help is much appreciated.

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


Re: What is a shortcut to the Default home directory in Windows

2008-01-18 Thread Jerry Hill
On Jan 18, 2008 3:19 PM, Daniel Folkes <[EMAIL PROTECTED]> wrote:
> I am trying to write a file to the users file system.
>
> I need it to be in there home directory in WINDOWS.  I know there is a
> "shortcut" to home in Linux("~"), but is there an equivalent to that
> in windows.  Or to get to their "Documents and Settings" directory?

The easiest way is os.path.expanduser('~')

That works on both unix and windows.  It gets you the user's home
directory on unix, and on windows I think it is based on the
environment variables %HOMEDRIVE% and %HOMEPATH%.  If that doesn't
give you what you're looking for, you'll need to look into the
environment variables, the registry, or windows system calls,
depending on exactly what you need.

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


Re: Is this a bug, or is it me?

2008-01-18 Thread Neil Cerutti
On Jan 18, 2008 2:01 PM,  <[EMAIL PROTECTED]> wrote:
> Now here is another one for your enjoyment:
>
> class C:
> @staticmethod
> def f1(): pass
> F = { '1' : f1 }
>
> C().F['1']()
>
> >>> TypeError: 'staticmethod' object is not callable
>
>
> What do you think of this one?

I'll get the ball rolling again, and hopefully it won't roll over me. ;)

The decoration is setting the class type's f1 attribute correctly, but
doing something strange in the local namespace.

>>> class C:
...   @staticmethod
...   def f1(): pass
...   print f1
...

>>> print C.f1


The class statement's local namespace is pretty strange. I think I
mightl go back to pretending there isn't one.

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


Re: What is a shortcut to the Default home directory in Windows

2008-01-18 Thread Christian Heimes
Daniel Folkes wrote:
> I am trying to write a file to the users file system.
> 
> I need it to be in there home directory in WINDOWS.  I know there is a
> "shortcut" to home in Linux("~"), but is there an equivalent to that
> in windows.  Or to get to their "Documents and Settings" directory?
> 
> Thanks in advance for the help.

os.path.expanduser("~") works on Windows, too. For application related
data like config files please use os.environ["APPDATA"]. For all
remaining shell folders like My Documents you *have* to use the win32
api. All shell folder names are localized and depend on the language
version.

Christian

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


What is a shortcut to the Default home directory in Windows

2008-01-18 Thread Daniel Folkes
I am trying to write a file to the users file system.

I need it to be in there home directory in WINDOWS.  I know there is a
"shortcut" to home in Linux("~"), but is there an equivalent to that
in windows.  Or to get to their "Documents and Settings" directory?

Thanks in advance for the help.

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


Re: how to resolve Windows pathnames into cygwin ones

2008-01-18 Thread [EMAIL PROTECTED]
Well yes, I was hoping for a library function, but none provides it.
Thanks for the code. Works nicely.

On Jan 18, 12:12 pm, apatheticagnostic <[EMAIL PROTECTED]>
wrote:
> Here we go then (are forward slashes valid in a filename in windows?)
> def path_into_cygpath(path):
> drive, destination = path.replace('\\','/').split(':')
> return '/cygdrive/' + drive.lower() + destination

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


Bit Scan Forward and Reverse

2008-01-18 Thread Thomas Dybdahl Ahle
Hi, I'm writing an app in python, and I'm storing some a lot of data in
bitmaps.
I need a way to find the first or latest set bit in a 64bit number, and
for that I've implemented a small routine.

Thing is that this routine is not as fast as I'd wish. I know most
processors implement BSF and BSR calls to do this efficiently.
Is there anyway to access those calls from python?

I'd still have my routine as a fallback on nonsupporting architectures.

-- 
Med venlig hilsen,
Best regards,
Thomas

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


Re: Using "pickle" for interprocess communication - some notes and things that ought to be documented.

2008-01-18 Thread Carl Banks
On Jan 17, 2:28 pm, John Nagle <[EMAIL PROTECTED]> wrote:
> It's possible to use "pickle" for interprocess communication over
> pipes, but it's not straightforward.
>
> First, "pickle" output is self-delimiting.
> Each dump ends with ".", and, importantly, "load" doesn't read
> any characters after the "."  So "pickle" can be used repeatedly
> on the same pipe, and one can do repeated message-passing this way.  This
> is a useful, but undocumented, feature.
>
> It almost works.
>
> Pickle's "dump" function doesn't flush output after dumping, so
> there's still some data left to be written.  The sender has to
> flush the underlying output stream after each call to "dump",
> or the receiver will stall. The "dump" function probably ought to flush
> its output file.


But... you can also write multiple pickles to the same file.

Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import cPickle
>>> f = open('xxx.pkl','wb')
>>> cPickle.dump(1,f)
>>> cPickle.dump('hello, world',f)
>>> cPickle.dump([1,2,3,4],f)
>>> f.close()
>>> f = open('xxx.pkl','rb')
>>> cPickle.load(f)
1
>>> cPickle.load(f)
'hello, world'
>>> cPickle.load(f)
[1, 2, 3, 4]

An automatic flush would be very undesirable there.  Best to let those
worrying about IPC to flush the output file themselves: which they
ought to be doing regardless (either by explicitly flushing or using
an unbuffered stream).


> It's also necessary to call Pickle's "clear_memo" before each "dump"
> call, since objects might change between successive "dump" calls.
> "Unpickle" doesn't have a "clear_memo" function.  It should, because
> if you keep reusing the "Unpickle" object, the memo dictionary
> fills up with old objects which can't be garbage collected.
> This creates a memory leak in long-running programs.

This is all good to know.  I agree that this is a good use case for a
clear_memo on a pickle unloader.


> Then, on Windows, there's a CR LF problem. This can be fixed by
> launching the subprocess with
>
> proc = subprocess.Popen(launchargs,
> stdin=subprocess.PIPE, stdout=subprocess.PIPE,
> universal_newlines=True)
>
> Failure to do this produces the useful error message "Insecure string pickle".
> Binary "pickle" protocol modes won't work at all in this situation; "universal
> newline" translation is compatible, not transparent.  On Unix/Linux, this
> just works, but the code isn't portable.

I would think a better solution would be to use the -u switch to
launch the subprocess, or the PYTHONUNBUFFERED environment variable if
you want to invoke the Python script directly.  It opens up stdin and
stdout in binary, unbuffered mode.

Using "univeral newlines" in a non-text format seems like it's not a
good idea.

For text-format pickles it'd be the right thing, of course.


> Incidentally, in the subprocess, it's useful to do
>
> sys.stdout = sys.stderr
>
> after setting up the Pickle objects.  This prevents any stray print statements
> from interfering with the structured Pickle output.

Nice idea.


> Then there's end of file detection.  When "load" reaches an end of
> file, it properly raises EOFError.  So it's OK to do "load" after
> "load" until EOFerror is raised.
>
> "pickle" and "cPickle" seem to be interchangeable in this application,
> so that works.
>
> It's a useful way to talk to a subprocess, but you need to know all the
> issues above to make it work.

Thanks: this was an informative post


Carl Banks

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


Re: Core Python Programming . . .

2008-01-18 Thread Paul Rubin
FireNWater <[EMAIL PROTECTED]> writes:
> 1) convert a 4-digit Integer (WXYZ) to an IP address (WWW.XXX.YYY.ZZZ)
> 
> or
> 
> 2) convert an 8-digit Integer (WWWXXXYYYZZZ) to (WWW.XXX.YYY.ZZZ)
> 
> Thanks for anyone with the clue!!!

Without being able to see the exercise I suspect it's turn a 4-byte
string (i.e. 32 bits) into an IP address (int8.int8.int8.int8).
Or, it might be turn a 32-bit int into such an address.
-- 
http://mail.python.org/mailman/listinfo/python-list


Core Python Programming . . .

2008-01-18 Thread FireNWater
I'm working my way thru the book "Core Python Programming" 2d Edition
- Wesley Chun. . .

Trying to figure out what he's looking for on Page 248, Exercise 6-11
(a).

Is it supposed to be:

1) convert a 4-digit Integer (WXYZ) to an IP address (WWW.XXX.YYY.ZZZ)

or

2) convert an 8-digit Integer (WWWXXXYYYZZZ) to (WWW.XXX.YYY.ZZZ)

Thanks for anyone with the clue!!!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: array and list

2008-01-18 Thread Paul Rubin
Travis Jensen <[EMAIL PROTECTED]> writes:
> I wouldn't call it unfortunate.  The list semantics follow LISP's
> semantics far more closely than C's array semantics.  I would hazard
> to guess that they are called lists because that is what every
> functional language calls them and this aspect of python was modeled
> after functional languages.

1. I don't think python was modelled after functional languages--it has
always been imperative, while slowly gaining functional features,
sometimes against opposition.

2. In functional langagues, "list" traditionally means a linked
structure while "array" is a linear structure, so python's use of
the term "list" is actually a little bit confusing.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is this a bug, or is it me?

2008-01-18 Thread cptnwillard
I filed a bug report, and here is the short answer to my question:
genexps are code blocks, and code blocks cannot see variables in class
scopes. Congrats to Neil Cerutti who figured it out.

Now here is another one for your enjoyment:

class C:
@staticmethod
def f1(): pass
F = { '1' : f1 }

C().F['1']()

>>> TypeError: 'staticmethod' object is not callable


What do you think of this one?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pythonland documentation

2008-01-18 Thread Mike Driscoll
On Jan 18, 12:51 pm, Simon Pickles <[EMAIL PROTECTED]> wrote:
> Hi
>
> I am new to python (fairly) but can't stop pythonning.
>
> c++ seems so far away now from here it looks like a horrid scribble :)
>
> Anyway my question is really about doc tools. I've been used to
> doxygen in c++ land, and although it makes a reasonable stab with a
> python project in java mode, the output is a bit messy.
>
> Can any one suggest a more tailored tool? or do I risk a flamewar? :)
>
> Thanks
>
> SiPi
>
> --
> Linux user #458601 -http://counter.li.org.

Well, there's DocUtils:

http://docutils.sourceforge.net/

And here are some others:

http://peak.telecommunity.com/protocol_ref/protocols-example1.html

And finally, there's this:

http://www.python.org/community/sigs/current/doc-sig/otherlangs/

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


Re: strange syntax rules on list comprehension conditions

2008-01-18 Thread Chris Mellon
On Jan 18, 2008 12:53 PM, Nicholas <[EMAIL PROTECTED]> wrote:
> I was quite delighted today, after extensive searches yielded nothing, to
> discover how to place an else condition in a list comprehension.
> Trivial mask example:
> >>> [True if i <5 else False for i in range(10)]   # A
> [True, True, True, True, True, False, False, False, False, False]
>
> I then experimented to drop the else statement which yields an error
> >>> [i if i>3 for i in range(10)]
> Traceback (  File "", line 1
> this syntax works of course
> >>> [i if i>3 else i for i in range(10)]
> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>
> Does anybody else find this lack of symmetry odd?
>

"x if y else x" is an expression - it's the Python equivalent of C's
ternary operator. The mechanism for filtering in a list comp is [x for
x in y if x].

Your stumbling upon the ternary expression was a happy accident, and
your confusing comes from trying to generalize the wrong operation.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: array and list

2008-01-18 Thread Travis Jensen


On Jan 18, 2008, at 2:48 AM, [EMAIL PROTECTED] wrote:

> J. Peng>why perl call it array and python call it list?<
>
> Unfortunate naming, I'd say. Calling list a dynamic array is silly,
> despite all the things you may say about abstract data types having
> the correct methods, etc.


I wouldn't call it unfortunate.  The list semantics follow LISP's  
semantics far more closely than C's array semantics.  I would hazard  
to guess that they are called lists because that is what every  
functional language calls them and this aspect of python was modeled  
after functional languages.

tj

Travis Jensen
[EMAIL PROTECTED]

http://softwaremaven.innerbrane.com/

You should read my blog; it is more interesting than my signiature.

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


strange syntax rules on list comprehension conditions

2008-01-18 Thread Nicholas
I was quite delighted today, after extensive searches yielded nothing, to
discover how to place an else condition in a list comprehension.
Trivial mask example:
>>> [True if i <5 else False for i in range(10)]   # A
[True, True, True, True, True, False, False, False, False, False]

I then experimented to drop the else statement which yields an error
>>> [i if i>3 for i in range(10)]
Traceback (  File "", line 1
this syntax works of course
>>> [i if i>3 else i for i in range(10)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Does anybody else find this lack of symmetry odd?

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

Pythonland documentation

2008-01-18 Thread Simon Pickles
Hi

I am new to python (fairly) but can't stop pythonning.

c++ seems so far away now from here it looks like a horrid scribble :)

Anyway my question is really about doc tools. I've been used to 
doxygen in c++ land, and although it makes a reasonable stab with a 
python project in java mode, the output is a bit messy.

Can any one suggest a more tailored tool? or do I risk a flamewar? :)

Thanks

SiPi

-- 
Linux user #458601 - http://counter.li.org.



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


Re: Efficient processing of large nuumeric data file

2008-01-18 Thread Paul Rubin
Tim Chase <[EMAIL PROTECTED]> writes:
>   first = int(data[0])
>   try:
> count = int(data[1])
>   except:
> count = 0

By the time you're down to this kind of thing making a difference,
it's probably more important to compile with pyrex or psyco.
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Filtering two files with uncommon column

2008-01-18 Thread Reedick, Andrew
> -Original Message-
> From: [EMAIL PROTECTED] [mailto:python-
> [EMAIL PROTECTED] On Behalf Of Madhur
> Sent: Friday, January 18, 2008 4:23 AM
> To: python-list@python.org
> Subject: Filtering two files with uncommon column
> 
> 
> Basically I want to compare the two files based on second column. If
> the second
> column matches on both the files do not print anything, else if there
> is no matc
> h in for the second column for first file in second file then print it
> under Fil
> e1 header, else if there is no match for the second column for second
> file in fi
> rst file print it under File2 header.
> 


I often do this to compare property files between environments.  The
follow algorithm works for any number of files by creating a dictionary
of lists (or hash of arrays in Perl-ese.)

Create a dictionary
Index = -1
For file in files
Index++
For line in file
col = match/split/regex the column
If col not in dictionary
Dictionary[col] = []

extend dictionary[col] to length of index
dictionary[col][index] = col

for col in sort(dictionary.keys()):
extend dictionary[col] to length of index
print dictionary[col]   




*

The information transmitted is intended only for the person or entity to which 
it is addressed and may contain confidential, proprietary, and/or privileged 
material. Any review, retransmission, dissemination or other use of, or taking 
of any action in reliance upon this information by persons or entities other 
than the intended recipient is prohibited. If you received this in error, 
please contact the sender and delete the material from all computers. GA622


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


Re: Bug in __init__?

2008-01-18 Thread Neil Cerutti
On Jan 18, 2008 12:33 PM, Zbigniew Braniecki
<[EMAIL PROTECTED]> wrote:
> > class A:
> >def __init__ (self, val=[]):
> >  print val
> >  self.lst = val
> >
> > val is created only *once* and shared across all instaces of A.
>
> Thanks for help guys!
>
> It's really a nice pitfall, I can hardly imagine anyone expecting this,
> or how easily could I find this info (e.g. what query should I give to
> google to get it without bothering people on this group)

In this unfortunate case, useful searches were "default arguments",
which would be hard to guess without already knowing what's going
wrong, or "python gotchas pitfalls", which is a good general purpose
search for when you can't understand what's happening in simple code.

-- 
Neil Cerutti <[EMAIL PROTECTED]>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Bug in __init__?

2008-01-18 Thread Fredrik Lundh
Zbigniew Braniecki wrote:

> It's really a nice pitfall, I can hardly imagine anyone expecting this, 
> or how easily could I find this info (e.g. what query should I give to 
> google to get it without bothering people on this group)

looking things up in the documentation *before* deciding that you might
have done something that nobody's done before is often a good idea:

http://docs.python.org/tut/node6.html#SECTION00671

 "Important warning: The default value is evaluated only once.
 This makes a difference when the default is a mutable object
 such as a list, dictionary, or instances of most classes.
 /.../"

http://docs.python.org/ref/function.html

 "Default parameter values are evaluated when the function
 definition is executed. This means that the expression is
 evaluated once, when the function is defined, and that
 that same ``pre-computed'' value is used for each call.
 This is especially important to understand when a default
 parameter is a mutable object, such as a list or a
 dictionary /.../

(to be precise, the default values are evaluated when the "def" state- 
ment is executed, in the same scope as the "def" statement itself.  if 
you execute the same "def" statement multiple times, the values are 
recalculated.)



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


Re: Efficient processing of large nuumeric data file

2008-01-18 Thread Tim Chase
> for line in file:

The first thing I would try is just doing a

  for line in file:
pass

to see how much time is consumed merely by iterating over the
file.  This should give you a baseline from which you can base
your timings

>   data = line.split()
>   first = int(data[0])
> 
>   if len(data) == 1:
>   count = 1
>   else:
>   count = int(data[1])# more than one repetition

Well, some experiments I might try:

  try:
first, count = map(int, data)
  except:
first = int(data[0])
count = 1

or possibly

  first = int(data[0])
  try:
count = int(data[1])
  except:
count = 0

or even

  # pad it to contain at least two items
  # then slice off the first two
  # and then map() calls to int()
  first, count = map(int,(data + [1])[:2])

I don't know how efficient len() is (if it's internally linearly
counting the items in data, or if it's caching the length as data
is created/assigned/modifed) and how that efficiency compares to
try/except blocks, map() or int() calls.

I'm not sure any of them is more or less "pythonic", but they
should all do the same thing.

>   if first in hist:   # add the information to the histogram
>   hist[first]+=count
>   else:
>   hist[first]=count

This might also be written as

  hist[first] = hist.get(first, 0) + count

> Is a dictionary the right way to do this?  In any given file, there is
> an upper bound on the data, so it seems to me that some kind of array
> (numpy?) would be more efficient, but the upper bound changes in each
> file.

I'm not sure an array would net you great savings here, since the
upper-bound seems to be an unknown.  If "first" has a known
maximum (surely, the program generating this file has an idea to
the range of allowed values), you could just create an array the
length of the span of numbers, initialized to zero, which would
reduce the hist.get() call to just

  hist[first] += count

and then you'd iterate over hist (which would already be sorted
because it's in index order) and use those where count != 0 to
avoid the holes.

Otherwise, your code looks good...the above just riff on various
ways of rewriting your code in case one nets you extra
time-savings per loop.

-tkc


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


Re: Perl Template Toolkit: Now in spicy new Python flavor

2008-01-18 Thread [EMAIL PROTECTED]
On Jan 16, 12:01 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:
> On Jan 15, 1:45 pm, George Sakkis <[EMAIL PROTECTED]> wrote:
> > Unless I missed it, the documentation
> > covers the Perl version only.
>
> The online documentation, yes.  All source-level documentation (from
> which the online documentation is largely drawn) has been converted
> into Python docstrings in the source code.

Augh!  And I forgot to mention that there's a file README.python at
the top level of the repository that discusses several of the issues
that arose in converting a large Perl project into Python, and how I
addressed them.  Possibly interesting reading.

http://tt2.org/svnweb/Template-Python/view/trunk/README.python
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Efficient processing of large nuumeric data file

2008-01-18 Thread Paul Rubin
David Sanders <[EMAIL PROTECTED]> writes:
> The data files are large (~100 million lines), and this code takes a
> long time to run (compared to just doing wc -l, for example).

wc is written in carefully optimized C and will almost certainly
run faster than any python program.

> Am I doing something very inefficient?  (Any general comments on my
> pythonic (or otherwise) style are also appreciated!)  Is
> "line.split()" efficient, for example?

Your implementation's efficiency is not too bad.  Stylistically it's
not quite fluent but there's nothing to really criticize--you may
develop a more concise style with experience, or maybe not.
One small optimization you could make is to use collections.defaultdict
to hold the counters instead of a regular dict, so you can get rid of
the test for whether a key is in the dict.  

Keep an eye on your program's memory consumption as it runs.  The
overhead of a pair of python ints and a dictionary cell to hold them
is some dozens of bytes at minimum.  If you have a lot of distinct
keys and not enough memory to hold them all in the large dict, your
system may be thrashing.  If that is happening, the two basic
solutions are 1) buy more memory; or, 2) divide the input into smaller
pieces, attack them separately, and merge the results.

If I were writing this program and didn't have to run it too often,
I'd probably use the unix "sort" utility to sort the input (that
utility does an external disk sort if the input is large enough to
require it) then make a single pass over the sorted list to count up
each group of keys (see itertools.groupby for a convenient way to do
that).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [python] How to detect a remote webpage is accessible? (in HTTP)

2008-01-18 Thread John Nagle
?? wrote:
> Howdy, all,
>  I want to use python to detect the accessibility of website.
> Currently, I use urllib
> to obtain the remote webpage, and see whether it fails. But the problem is 
> that
> the webpage may be very large; it takes too long time. Certainly, it
> is no need to download
> the entire page. Could you give me a good and fast solution?
> Thank you.
> --
> ShenLei

If you can get through "urlopen", you've already received the HTTP headers.
Just open, then use "info()" on the file descriptor to get the header info.
Don't read the content at all.

Setting the socket timeout will shorten the timeout when the requested
domain won't respond at all.  But if the remote host opens an HTTP connection,
then sends nothing, the socket timeout is ineffective and you wait for a while.
This is rare, but it happens.

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


Re: Efficient processing of large nuumeric data file

2008-01-18 Thread Matimus
On Jan 18, 9:15 am, David Sanders <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I am processing large files of numerical data.  Each line is either a
> single (positive) integer, or a pair of positive integers, where the
> second represents the number of times that the first number is
> repeated in the data -- this is to avoid generating huge raw files,
> since one particular number is often repeated in the data generation
> step.
>
> My question is how to process such files efficiently to obtain a
> frequency histogram of the data (how many times each number occurs in
> the data, taking into account the repetitions).  My current code is as
> follows:
>
> ---
> #!/usr/bin/env python
> # Counts the occurrences of integers in a file and makes a histogram
> of them
> # Allows for a second field which gives the number of counts of each
> datum
>
> import sys
> args = sys.argv
> num_args = len(args)
>
> if num_args < 2:
> print "Syntaxis: count.py archivo"
> sys.exit();
>
> name = args[1]
> file = open(name, "r")
>
> hist = {}   # dictionary for histogram
> num = 0
>
> for line in file:
> data = line.split()
> first = int(data[0])
>
> if len(data) == 1:
> count = 1
> else:
> count = int(data[1])# more than one repetition
>
> if first in hist:   # add the information to the histogram
> hist[first]+=count
> else:
> hist[first]=count
>
> num+=count
>
> keys = hist.keys()
> keys.sort()
>
> print "# i  fraction   hist[i]"
> for i in keys:
> print i, float(hist[i])/num, hist[i]
> -
>
> The data files are large (~100 million lines), and this code takes a
> long time to run (compared to just doing wc -l, for example).
>
> Am I doing something very inefficient?  (Any general comments on my
> pythonic (or otherwise) style are also appreciated!)  Is
> "line.split()" efficient, for example?
>
> Is a dictionary the right way to do this?  In any given file, there is
> an upper bound on the data, so it seems to me that some kind of array
> (numpy?) would be more efficient, but the upper bound changes in each
> file.


My first suggestion is to wrap your code in a function. Functions run
much faster in python than module level code, so that will give you a
speed up right away. My second suggestion is to look into using
defaultdict for your histogram. A dictionary is a very appropriate way
to store this data. There has been some mention of a bag type, which
would do exactly what you need, but unfortunately there is not a built
in bag type (yet). I would write it something like this:

from collections import defaultdict

def get_hist(file_name):
hist = defaultdict(int)
f = open(filename,"r")
for line in f:
vals = line.split()
val = int(vals[0])
try: # don't look to see if you will cause an error,
 # just cause it and then deal with it
cnt = int(vals[1])
except IndexError:
cnt = 1
hist[val] += cnt
return hist

HTH

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


Re: Efficient processing of large nuumeric data file

2008-01-18 Thread George Sakkis
On Jan 18, 12:15 pm, David Sanders <[EMAIL PROTECTED]> wrote:

> Hi,
>
> I am processing large files of numerical data.  Each line is either a
> single (positive) integer, or a pair of positive integers, where the
> second represents the number of times that the first number is
> repeated in the data -- this is to avoid generating huge raw files,
> since one particular number is often repeated in the data generation
> step.
>
> My question is how to process such files efficiently to obtain a
> frequency histogram of the data (how many times each number occurs in
> the data, taking into account the repetitions).  My current code is as
> follows:
>
> ---
> #!/usr/bin/env python
> # Counts the occurrences of integers in a file and makes a histogram
> of them
> # Allows for a second field which gives the number of counts of each
> datum
>
> import sys
> args = sys.argv
> num_args = len(args)
>
> if num_args < 2:
> print "Syntaxis: count.py archivo"
> sys.exit();
>
> name = args[1]
> file = open(name, "r")
>
> hist = {}   # dictionary for histogram
> num = 0
>
> for line in file:
> data = line.split()
> first = int(data[0])
>
> if len(data) == 1:
> count = 1
> else:
> count = int(data[1])# more than one repetition
>
> if first in hist:   # add the information to the histogram
> hist[first]+=count
> else:
> hist[first]=count
>
> num+=count
>
> keys = hist.keys()
> keys.sort()
>
> print "# i  fraction   hist[i]"
> for i in keys:
> print i, float(hist[i])/num, hist[i]
> -
>
> The data files are large (~100 million lines), and this code takes a
> long time to run (compared to just doing wc -l, for example).
>
> Am I doing something very inefficient?  (Any general comments on my
> pythonic (or otherwise) style are also appreciated!)  Is
> "line.split()" efficient, for example?

Without further information, I don't see anything particularly
inefficient. What may help here is if you have any a priori knowledge
about the data, specifically:

- How often does a single number occur compared to a pair of numbers ?
E.g. if a single number is much more common that a pair, you can avoid
split() most of the times:
try: first,count = int(line), 1
except ValueError:
first,count = map(int,line.split())

Similarly if the pair is much more frequent than the single number,
just invert the above so that the common case is in the 'try' block
and the infrequent in 'except'. However if the two cases have similar
frequency, or if you have no a priori knowledge, try/except will
likely be slower.

- What proportion of the first numbers is unique ? If it's small
enough, a faster way to update the dict is:
try: hist[first]+=count
except KeyError:
hist[first] = 1

> Is a dictionary the right way to do this?  In any given file, there is
> an upper bound on the data, so it seems to me that some kind of array
> (numpy?) would be more efficient, but the upper bound changes in each
> file.

Yes, dict is the right data structure; since Python 2.5,
collections.defaultdict is an alternative. numpy is good for
processing numeric data once they are already in arrays, not for
populating them.

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


Re: Using "pickle" for interprocess communication - some notes and things that ought to be documented.

2008-01-18 Thread John Nagle
John Nagle wrote:
> Irmen de Jong wrote:
>> Christian Heimes wrote:
>>> John Nagle wrote:
 It's possible to use "pickle" for interprocess communication over
 pipes, but it's not straightforward.

Another "gotcha".  The "pickle" module seems to be OK with the
translations of "universal newlines" on Windows, but the "cPickle" module
is not.  If I pickle

Exception("Test")

send it across the Windows pipe to the parent in universal newlines
mode, and read it with cPickle's

load()

function, I get

ImportError: No module named exceptions

If I read it with "pickle"'s "load()", it works.  And if I read the input
one character at a time until I see ".", then feed that to cPickle's "loads()",
that works.  So cPickle doesn't read the same thing Python does in "universal
newline" mode.

Is there any way within Python to get the pipe from a child process to the
parent to be completely transparent under Windows?

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


RE: How to use only a sub shell to execute many commands in python

2008-01-18 Thread Reedick, Andrew
> -Original Message-
> From: [EMAIL PROTECTED] [mailto:python-
> [EMAIL PROTECTED] On Behalf Of raocheng
> Sent: Friday, January 18, 2008 6:31 AM
> To: python-list@python.org
> Subject: How to use only a sub shell to execute many commands in
python
> 
> Please see the following code.
> Suppose I have many shell commands to be executed. And I don't want to
> fork a sub shell for each command(eg: status,output =
> commands.getstatusoutput(cmd)) because it is too expensive. I want to
> use only one sub shell to execute all these commands and want to get
> each command's output. How can I accomplish this task ? Thanks in
> advance.
> 
> ===
> #!/usr/bin/env python
> import os
> fi, fo = os.popen2(
> '''
> while read line
> do
>   eval $line
> done
> ''',   't')
> 
> #Suppose I have many commands to execute, but I don't want to fork a
> sub shell for each command
> cmds = ['date','uptime','pwd','ls -rltF','who']
> 
> for cmd in cmds:
> #pseudocode
> fi.executeCmd(cmd)
> output = fo.readResult()
> 
> print output


Have each command write to a unique temp file.  
Create temp files in python
cmd = 'date > /tmp/date_temp 2>&1 ; uptime > /tmp/uptime_temp
2>&1; ...'
execute cmd
for file in tempfiles:
...

You can also get the return value of each command
cmd = 'date > /tmp/date_temp 2>&1; echo $? >> /tmp/date_temp;
uptime > /tmp/uptime_temp 2>&1; echo $? >> ...'




*

The information transmitted is intended only for the person or entity to which 
it is addressed and may contain confidential, proprietary, and/or privileged 
material. Any review, retransmission, dissemination or other use of, or taking 
of any action in reliance upon this information by persons or entities other 
than the intended recipient is prohibited. If you received this in error, 
please contact the sender and delete the material from all computers. GA622


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


Re: Bug in __init__?

2008-01-18 Thread Zbigniew Braniecki
Christian Heimes wrote:
> Zbigniew Braniecki wrote:
>> Any clue on what's going on here, and/if where I should report it?
> 
> Congratulations! You've stumbled over a well known gotcha. Most newbies
> fall for the trap.
> 
> class A:
>def __init__ (self, val=[]):
>  print val
>  self.lst = val
> 
> val is created only *once* and shared across all instaces of A.

Thanks for help guys!

It's really a nice pitfall, I can hardly imagine anyone expecting this, 
or how easily could I find this info (e.g. what query should I give to 
google to get it without bothering people on this group)

Anyway, thanks :)

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


Re: import from question

2008-01-18 Thread Tobiah

>> Ok, I get it.  I was locally importing a pointer to an integer 
> 
> Really? What language were you using? Python doesn't have pointers.

What term do you prefer?  Reference?  Object id holder?

-- 
Posted via a free Usenet account from http://www.teranews.com

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


RE: how to resolve Windows pathnames into cygwin ones

2008-01-18 Thread Reedick, Andrew
> -Original Message-
> From: [EMAIL PROTECTED] [mailto:python-
> [EMAIL PROTECTED] On Behalf Of
[EMAIL PROTECTED]
> Sent: Friday, January 18, 2008 11:11 AM
> To: python-list@python.org
> Subject: how to resolve Windows pathnames into cygwin ones
> 
> I am looking for a function to resolve 'F:/foo/bar' into '/cygdrive/f/
> foo/bar'. I get the original dirpath from tkFileDialog.askdirectory in
> a Windows form and none of os.path.* functions seem to resolve it to a
> cygwin form. Rather they _append_ it to the current directory,
> resulting at best in a monster '/cygdrive/c/whatever/f/foo/bar'.
> It's all being developed under cygwin currently (so it is a kind of
> mixed environment), but I would like the fix to work correctly in any
> environment.
> 

Firstly, '/cygdrive' is overrated.  Cygwin will accept:  'ls c:\\foo'
and 'ls c:/foo'


s= 'f:\\foo\\bar'

print re.sub(r'\\', '/', s)

m = re.match('^(.):(.*)$', s)
print '/cygdrive/' + m.group(1) + re.sub(r'\\', '/', m.group(2))

*

The information transmitted is intended only for the person or entity to which 
it is addressed and may contain confidential, proprietary, and/or privileged 
material. Any review, retransmission, dissemination or other use of, or taking 
of any action in reliance upon this information by persons or entities other 
than the intended recipient is prohibited. If you received this in error, 
please contact the sender and delete the material from all computers. GA621


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


Re: Bug in __init__?

2008-01-18 Thread Martin Blume
"Zbigniew Braniecki"  schrieb
> I found a bug in my code today, and spent an hour trying to locate
it
> and then minimize the testcase.
>   [...]
>def __init__ (self, val=[]):
>  [...]
> Any clue on what's going on here, and/if where I should report it?
>

I think this has to do with
http://docs.python.org/tut/node6.html#SECTION00671
especially the "Important Warning"

Regards
Martin



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


Re: Bug in __init__?

2008-01-18 Thread Matimus
On Jan 18, 9:09 am, Zbigniew Braniecki <[EMAIL PROTECTED]>
wrote:
> I found a bug in my code today, and spent an hour trying to locate it
> and then minimize the testcase.
>
> Once I did it, I'm still confused about the behavior and I could not
> find any reference to this behavior in docs.
>
> testcase:
>
> class A():
>
>def add (self, el):
>  self.lst.extend(el)
>
>def __init__ (self, val=[]):
>  print val
>  self.lst = val
>
> def test ():
>x = A()
>x.add(["foo1","foo2"])
>b = A()
>
> So, what I would expect here is that I will create two instances of
> class A with empty self.lst property. Right?
>
> In fact (at least with my Python 2.5)
>
> [EMAIL PROTECTED]:~/projects/pyl10n$ ./scripts/test.py
> []
> ['foo1', 'foo2']
>
> This bug does not happen when I switch to __init__ (self, *args) and
> assign self.lst= args[0].
>
> Any clue on what's going on here, and/if where I should report it?
>
> Greetings
> Zbigniew Braniecki

Look at item number 5. http://zephyrfalcon.org/labs/python_pitfalls.html

People run into this quite often. Basicly, you created a list as a
default argument. This doesn't create a new empty list ever time
__init__ is called. Everyone is getting the same list. How to get
around this:

class A(object): # derive from object for new-style classes
   def add (self, el):
 self.lst.extend(el)

   def __init__ (self, val=None): # Use None instead
 if val is None:
 val = [] # create a new list if needed each time
 print val
 self.lst = val


HTH

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


Re: Bug in __init__?

2008-01-18 Thread Eduardo O. Padoan
On Jan 18, 2008 3:09 PM, Zbigniew Braniecki
<[EMAIL PROTECTED]> wrote:
> I found a bug in my code today, and spent an hour trying to locate it
> and then minimize the testcase.
>
> Once I did it, I'm still confused about the behavior and I could not
> find any reference to this behavior in docs.
>
> testcase:
>
> class A():
>
>def add (self, el):
>  self.lst.extend(el)
>
>def __init__ (self, val=[]):
>  print val
>  self.lst = val
>
>
> def test ():
>x = A()
>x.add(["foo1","foo2"])
>b = A()
>
>
> So, what I would expect here is that I will create two instances of
> class A with empty self.lst property. Right?
>
> In fact (at least with my Python 2.5)
>
> [EMAIL PROTECTED]:~/projects/pyl10n$ ./scripts/test.py
> []
> ['foo1', 'foo2']
>
> This bug does not happen when I switch to __init__ (self, *args) and
> assign self.lst= args[0].
>
> Any clue on what's going on here, and/if where I should report it?

It is a FAQ, not a bug:
http://www.python.org/doc/faq/general/#why-are-default-values-shared-between-objects
http://effbot.org/pyfaq/why-are-default-values-shared-between-objects.htm

-- 
http://www.advogato.org/person/eopadoan/
Bookmarks: http://del.icio.us/edcrypt
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Bug in __init__?

2008-01-18 Thread Christian Heimes
Zbigniew Braniecki wrote:
> Any clue on what's going on here, and/if where I should report it?

Congratulations! You've stumbled over a well known gotcha. Most newbies
fall for the trap.

class A:
   def __init__ (self, val=[]):
 print val
 self.lst = val

val is created only *once* and shared across all instaces of A.

Christian

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


Efficient processing of large nuumeric data file

2008-01-18 Thread David Sanders
Hi,

I am processing large files of numerical data.  Each line is either a
single (positive) integer, or a pair of positive integers, where the
second represents the number of times that the first number is
repeated in the data -- this is to avoid generating huge raw files,
since one particular number is often repeated in the data generation
step.

My question is how to process such files efficiently to obtain a
frequency histogram of the data (how many times each number occurs in
the data, taking into account the repetitions).  My current code is as
follows:

---
#!/usr/bin/env python
# Counts the occurrences of integers in a file and makes a histogram
of them
# Allows for a second field which gives the number of counts of each
datum

import sys
args = sys.argv
num_args = len(args)

if num_args < 2:
print "Syntaxis: count.py archivo"
sys.exit();

name = args[1]
file = open(name, "r")

hist = {}   # dictionary for histogram
num = 0

for line in file:
data = line.split()
first = int(data[0])

if len(data) == 1:
count = 1
else:
count = int(data[1])# more than one repetition

if first in hist:   # add the information to the histogram
hist[first]+=count
else:
hist[first]=count

num+=count

keys = hist.keys()
keys.sort()

print "# i  fraction   hist[i]"
for i in keys:
print i, float(hist[i])/num, hist[i]
-

The data files are large (~100 million lines), and this code takes a
long time to run (compared to just doing wc -l, for example).

Am I doing something very inefficient?  (Any general comments on my
pythonic (or otherwise) style are also appreciated!)  Is
"line.split()" efficient, for example?

Is a dictionary the right way to do this?  In any given file, there is
an upper bound on the data, so it seems to me that some kind of array
(numpy?) would be more efficient, but the upper bound changes in each
file.

Thanks and best wishes,
David.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to resolve Windows pathnames into cygwin ones

2008-01-18 Thread apatheticagnostic
On Jan 18, 11:48 am, Grant Edwards <[EMAIL PROTECTED]> wrote:
> On 2008-01-18, apatheticagnostic <[EMAIL PROTECTED]> wrote:
>
>
>
> > On Jan 18, 11:10 am, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:
> >> I am looking for a function to resolve 'F:/foo/bar' into '/cygdrive/f/
> >> foo/bar'. I get the original dirpath from tkFileDialog.askdirectory in
> >> a Windows form and none of os.path.* functions seem to resolve it to a
> >> cygwin form. Rather they _append_ it to the current directory,
> >> resulting at best in a monster '/cygdrive/c/whatever/f/foo/bar'.
> >> It's all being developed under cygwin currently (so it is a kind of
> >> mixed environment), but I would like the fix to work correctly in any
> >> environment.
>
> >> Thanks,
> >> Marcin
>
> > Well, you could write it yourself
>
> > (assuming path is a string, here's a first go at it...)
> > def path_into_cygpath(path):
> > drive, destination = path.split(':')
> > newpath = '/cygdrive/' + drive.lower() + destination
> > return newpath
>
> Don't forget to convert backslashes into forward slashes.
>
> --
> Grant Edwards   grante Yow! TONY RANDALL!  Is YOUR
>   at   life a PATIO of FUN??
>visi.com

Whoops.

Here we go then (are forward slashes valid in a filename in windows?)
def path_into_cygpath(path):
drive, destination = path.replace('\\','/').split(':')
return '/cygdrive/' + drive.lower() + destination
-- 
http://mail.python.org/mailman/listinfo/python-list


Bug in __init__?

2008-01-18 Thread Zbigniew Braniecki
I found a bug in my code today, and spent an hour trying to locate it 
and then minimize the testcase.

Once I did it, I'm still confused about the behavior and I could not 
find any reference to this behavior in docs.

testcase:

class A():

   def add (self, el):
 self.lst.extend(el)

   def __init__ (self, val=[]):
 print val
 self.lst = val


def test ():
   x = A()
   x.add(["foo1","foo2"])
   b = A()


So, what I would expect here is that I will create two instances of 
class A with empty self.lst property. Right?

In fact (at least with my Python 2.5)

[EMAIL PROTECTED]:~/projects/pyl10n$ ./scripts/test.py
[]
['foo1', 'foo2']

This bug does not happen when I switch to __init__ (self, *args) and 
assign self.lst= args[0].

Any clue on what's going on here, and/if where I should report it?

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


Re: how to resolve Windows pathnames into cygwin ones

2008-01-18 Thread Grant Edwards
On 2008-01-18, apatheticagnostic <[EMAIL PROTECTED]> wrote:
> On Jan 18, 11:10 am, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:
>> I am looking for a function to resolve 'F:/foo/bar' into '/cygdrive/f/
>> foo/bar'. I get the original dirpath from tkFileDialog.askdirectory in
>> a Windows form and none of os.path.* functions seem to resolve it to a
>> cygwin form. Rather they _append_ it to the current directory,
>> resulting at best in a monster '/cygdrive/c/whatever/f/foo/bar'.
>> It's all being developed under cygwin currently (so it is a kind of
>> mixed environment), but I would like the fix to work correctly in any
>> environment.
>>
>> Thanks,
>> Marcin
>
> Well, you could write it yourself
>
> (assuming path is a string, here's a first go at it...)
> def path_into_cygpath(path):
> drive, destination = path.split(':')
> newpath = '/cygdrive/' + drive.lower() + destination
> return newpath

Don't forget to convert backslashes into forward slashes.

-- 
Grant Edwards   grante Yow! TONY RANDALL!  Is YOUR
  at   life a PATIO of FUN??
   visi.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: plz help how to print python variable using os.system()

2008-01-18 Thread Grant Edwards
On 2008-01-16, Lutz Horn <[EMAIL PROTECTED]> wrote:
> Hi,
>
> On Wed, 16 Jan 2008 05:29:08 -0800 (PST), [EMAIL PROTECTED] said:
>> var = "/home/anonymous"
>> os.system("echo $var)
>
> os.system("echo %s" % var)

Though one wonders why one would do that instead of simply doing

print var

-- 
Grant Edwards   grante Yow! I'm ZIPPY the PINHEAD
  at   and I'm totally committed
   visi.comto the festive mode.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to use only a sub shell to execute many commands in python

2008-01-18 Thread Gabriel Genellina
En Fri, 18 Jan 2008 09:31:25 -0200, raocheng <[EMAIL PROTECTED]> escribi�:

> Please see the following code.
> Suppose I have many shell commands to be executed. And I don't want to
> fork a sub shell for each command(eg: status,output =
> commands.getstatusoutput(cmd)) because it is too expensive. I want to
> use only one sub shell to execute all these commands and want to get
> each command's output. How can I accomplish this task ? Thanks in
> advance.

The hard part is to determine WHEN the command has completed its  
execution, because there is no indication of that. One way would be to set  
a relatively uncommon prompt, and look for that string in stdout. You  
can't read beyond that, else the code would block.
Another way is to use a separate thread to read from stdout/stderr, and  
set a timeout; when no more output comes whithin the timeout, you assume  
the command has finished.

The code below uses the first approach, changing the prompt to <$$$>\r\n.  
I've tested it on Windows only, but should work on Linux too with some  
minor modifications.

import subprocess
 from os import getenv

# Windows only, this is to determine the shell in use
# Linux users may try with getenv("SHELL", "sh")
shell = getenv("COMSPEC", "cmd")
p = subprocess.Popen(shell, stdin=subprocess.PIPE,
 stdout=subprocess.PIPE,
 stderr=subprocess.STDOUT)

expected = '<$$$>\r\n'
cmds = ['prompt $L$$$G$_','date /t','cd','dir','ipconfig /all']
# The first command above sets an uncommon prompt, ending with a newline.
# On Linux use PS1=whatever, but make sure it ends in \n and set the
# expected variable accordingly.

for cmd in cmds:
 print ">>>",cmd
 p.stdin.write('%s\n' % cmd)
 while True:
 line = p.stdout.readline()
 if line.endswith(expected): break
 print line,

print "Waiting for subshell to terminate"
p.stdin.write("exit\n")
p.wait()
print "Done"


-- 
Gabriel Genellina

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

Re: how to resolve Windows pathnames into cygwin ones

2008-01-18 Thread apatheticagnostic
On Jan 18, 11:10 am, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:
> I am looking for a function to resolve 'F:/foo/bar' into '/cygdrive/f/
> foo/bar'. I get the original dirpath from tkFileDialog.askdirectory in
> a Windows form and none of os.path.* functions seem to resolve it to a
> cygwin form. Rather they _append_ it to the current directory,
> resulting at best in a monster '/cygdrive/c/whatever/f/foo/bar'.
> It's all being developed under cygwin currently (so it is a kind of
> mixed environment), but I would like the fix to work correctly in any
> environment.
>
> Thanks,
> Marcin

Well, you could write it yourself

(assuming path is a string, here's a first go at it...)
def path_into_cygpath(path):
drive, destination = path.split(':')
newpath = '/cygdrive/' + drive.lower() + destination
return newpath
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to resolve Windows pathnames into cygwin ones

2008-01-18 Thread Gerhard Häring
[EMAIL PROTECTED] wrote:
> I am looking for a function to resolve 'F:/foo/bar' into '/cygdrive/f/
> foo/bar'. I get the original dirpath from tkFileDialog.askdirectory in
> a Windows form and none of os.path.* functions seem to resolve it to a
> cygwin form. Rather they _append_ it to the current directory,
> resulting at best in a monster '/cygdrive/c/whatever/f/foo/bar'.
> It's all being developed under cygwin currently (so it is a kind of
> mixed environment), but I would like the fix to work correctly in any
> environment.

You can call the cygpath utility:

 >>> import commands
 >>> commands.getoutput("cygpath c:/windows")
'/c/windows'

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


Re: Interesting Thread Gotcha

2008-01-18 Thread Hendrik van Rooyen
 "Dan"  wrote:


> Would it be possible to have pychecker (or some such) warn that there
> is an insufficient parameter count to start_new_thread? I guess that
> would require knowing the type of thread. . .

I think this is the hub of the thing - its not only start_new_thread, but
the way that parameters are evaluated before being counted, generally.

See my reply to Diez's post

- Hendrik

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


how to resolve Windows pathnames into cygwin ones

2008-01-18 Thread [EMAIL PROTECTED]
I am looking for a function to resolve 'F:/foo/bar' into '/cygdrive/f/
foo/bar'. I get the original dirpath from tkFileDialog.askdirectory in
a Windows form and none of os.path.* functions seem to resolve it to a
cygwin form. Rather they _append_ it to the current directory,
resulting at best in a monster '/cygdrive/c/whatever/f/foo/bar'.
It's all being developed under cygwin currently (so it is a kind of
mixed environment), but I would like the fix to work correctly in any
environment.

Thanks,
Marcin
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: get the size of a dynamically changing file fast ?

2008-01-18 Thread Mike Driscoll
On Jan 17, 3:56 pm, Stef Mientki <[EMAIL PROTECTED]> wrote:
> hello,
>
> I've a program (not written in Python) that generates a few thousands
> bytes per second,
> these files are dumped in 2 buffers (files), at in interval time of 50 msec,
> the files can be read by another program, to do further processing.
>
> A program written in VB or delphi can handle the data in the 2 buffers
> perfectly.
> Sometimes Python is also able to process the data correctly,
> but often it can't :-(
>
> I keep one of the files open en test the size of the open datafile each
> 50 msec.
> I have tried
> os.stat ( ) [ ST_SIZE]
> os.path.getsize ( ... )
> but they both have the same behaviour, sometimes it works, and the data
> is collected each 50 .. 100 msec,
> sometimes 1 .. 1.5 seconds is needed to detect a change in filesize.
>
> I'm using python 2.4 on winXP.
>
> Is there a solution for this problem ?
>
> thanks,
> Stef Mientki

Tim Golden has a method to watch for changes in a directory on his
website:

http://tgolden.sc.sabren.com/python/win32_how_do_i/watch_directory_for_changes.html

This old post also mentions something similar:

http://mail.python.org/pipermail/python-list/2007-October/463065.html

And here's a cookbook recipe that claims to do it as well using
decorators:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/426620

Hopefully that will get you going.

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


Re: Filtering two files with uncommon column

2008-01-18 Thread Neil Cerutti
On Jan 18, 2008 4:23 AM, Madhur <[EMAIL PROTECTED]> wrote:
> I would like to know the best way of generating filter of two files
> based upon the following condition

As a bit of friendly advice, you'll get much more useful assistance if
you post your code.

If you don't have any code to show, write some. Unless it's a quine, a
program won't write itself.

-- 
Neil Cerutti <[EMAIL PROTECTED]>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ANN:proxysocket(socks4,socks5)v0.1

2008-01-18 Thread Samuel
On 1月18日, 下午3时04分, Tim Roberts <[EMAIL PROTECTED]> wrote:
> Samuel <[EMAIL PROTECTED]> wrote:
>
> >http://code.google.com/p/proxysocket/downloads/list
>
> Allow me to introduce you to the concept of comments.  Python allows you to
> include descriptive sentences in your program that explain what the
> functions do, what your intentions were, what the variables do, what the
> states mean, etc.  It's easy to do; you just start the text with # signs.
>
>   # This function allows you to ...
>
>   # These variables define the connection state as the connection is
>   # made.
>
> They're great.  You should try them.
> --
> Tim Roberts, [EMAIL PROTECTED]
> Providenza & Boekelheide, Inc.

My English writing ability is bad, could you join this project?
-- 
http://mail.python.org/mailman/listinfo/python-list

Free amazing softwares and virus protect

2008-01-18 Thread bajagu
H,

Free download latest softwares... Hurrryyy upp

http://freesoftwaredownloaded.blogspot.com/

http://freedownload.co.in

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


Re: Some Berkeley DB questions (being maintained? queries?)

2008-01-18 Thread Aahz
In article <[EMAIL PROTECTED]>,
Terry Jones  <[EMAIL PROTECTED]> wrote:
>
>I'm also interested in any ongoing or planned work on the Python interface.

Someone recently volunteered to take over primary maintenance, but I
can't find the mailing list post.
-- 
Aahz ([EMAIL PROTECTED])   <*> http://www.pythoncraft.com/

"All problems in computer science can be solved by another level of 
indirection."  --Butler Lampson
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: array and list

2008-01-18 Thread Paddy

> Paddy:
>
> > I guess 'under the hood' Python (& Perl?), arrays might be more like
> > an implementation of what the C programmer might call a linked list,
> > but even then there are differences as  most linked list examples
> > given in C tutorials are lists of the same type of object,
>
> Both Python "array.array" and "list" are implemented as dyn arrays,
> not as lists. Only the good "deque" by Hettinger is something similar
> to a list (of small arrays).
>
> Bye,
> bearophile

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


Re: Cannot catch _mysql_exceptions.OperationalError

2008-01-18 Thread Jeffrey Froman
Bob wrote:

> Here's the code that did not work:
> 
> import _mysql_exceptions
> from _mysql_exceptions import OperationalError
> 
> try:
> database_code()
> except (_mysql_exceptions.OperationalError, OperationalError), e:
> error_handling()

Both of the above forms work fine here, as does using
MySQLdb.OperationalError:

>>> import MySQLdb
>>> try:
... db = MySQLdb.connect(user='jeffrey', host='localhost')
... except MySQLdb.OperationalError:
... print 'caught'
...
caught

>>> import _mysql_exceptions
>>> try:
... db = MySQLdb.connect(user='jeffrey', host='localhost')
... except _mysql_exceptions.OperationalError:
... print 'caught'
...
caught

>>> from _mysql_exceptions import OperationalError
>>> try:
... db = MySQLdb.connect(user='jeffrey', host='localhost')
... except OperationalError:
... print 'caught'
...
caught

>>> MySQLdb.OperationalError is _mysql_exceptions.OperationalError
True
>>> MySQLdb.__version__
'1.2.2'



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


Re: MySQLdb and compatibility with vista 64 bits

2008-01-18 Thread revuesbio
On 5 jan, 20:00, revuesbio <[EMAIL PROTECTED]> wrote:
> Hello,
> I try to installmysqldbon windows vista 64bits but there is a
> failure when i try to importmysqldbin python 2.5 :
> "DLL load failed with error code 193"
>
> Is there a solution to this problem ?
> Thank you

is there another solution to interact mysql and python with vista x64 ?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: "Code Friendly" Blog?

2008-01-18 Thread Guy Rutenberg
On Jan 18, 8:56 am, Jeroen Ruigrok van der Werven <[EMAIL PROTECTED]
nomine.org> wrote:

> I personally use a Wordpress installation on my own machine with an additional
> plugin for syntax highlighting.http://wordpress.org/extend/plugins/wp-syntax/
>

I use the same configuration (Wordpress + wp-syntax) too. I can say
I'm pretty satisfied with it. I had no problem posting python code.
IIRC Wordpresss will automatically escape < and > for you (if it's no
part of tag).

Regards,
Guy

http://www.guyrutenberg.com/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to detect a remote webpage is accessible? (in HTTP)

2008-01-18 Thread grflanagan
On Jan 18, 6:22 am, "甜瓜" <[EMAIL PROTECTED]> wrote:
> Howdy, all,
>  I want to use python to detect the accessibility of website.
> Currently, I use urllib
> to obtain the remote webpage, and see whether it fails. But the problem is 
> that
> the webpage may be very large; it takes too long time. Certainly, it
> is no need to download
> the entire page. Could you give me a good and fast solution?
> Thank you.
> --
> ShenLei

http://groups.google.com/group/comp.lang.python/browse_frm/thread/bbac82df3d64d48e/da75e45a8da2f6c1
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Filtering two files with uncommon column

2008-01-18 Thread Martin Blume
"Madhur" schrieb
> I would like to know the best way of generating filter 
> of two files based upon the following condition
> [...]
>
Sounds like homework. Here some suggestions:

- for each file, create a dictionary (see help(dict)
in the python shell for details) and populate it with 
the values, so that e.g.
d1['def'] = 'abc def hij'
(help("".split), perhaps help("".strip))

- for each key in the first dictionary, look whether
it exists in the second, if not, write the value (the 
line extracted in the first step) out.
(help(dict.iteritems), help(dict.has_key))
(Note that for 
if a_dict.has_key("def"): pass
one can also write 
if "def" in a_dict: pass
but you won't find this in the simple on-line help,
at least in my version)

HTH
Martin

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


How to use only a sub shell to execute many commands in python

2008-01-18 Thread raocheng
Please see the following code.
Suppose I have many shell commands to be executed. And I don't want to
fork a sub shell for each command(eg: status,output =
commands.getstatusoutput(cmd)) because it is too expensive. I want to
use only one sub shell to execute all these commands and want to get
each command's output. How can I accomplish this task ? Thanks in
advance.

===
#!/usr/bin/env python
import os
fi, fo = os.popen2(
'''
while read line
do
  eval $line
done
''',   't')

#Suppose I have many commands to execute, but I don't want to fork a
sub shell for each command
cmds = ['date','uptime','pwd','ls -rltF','who']

for cmd in cmds:
#pseudocode
fi.executeCmd(cmd)
output = fo.readResult()

print output
===
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Filtering two files with uncommon column

2008-01-18 Thread Chris
On Jan 18, 12:08 pm, Madhur <[EMAIL PROTECTED]> wrote:
> On Jan 18, 2:37 pm, Chris <[EMAIL PROTECTED]> wrote:
>
>
>
> > On Jan 18, 11:23 am, Madhur <[EMAIL PROTECTED]> wrote:
>
> > > I would like to know the best way of generating filter of two files
> > > based upon the following condition
>
> > > I have two files. Contents of the first file is
>
> > > File 1
> > > abc def hij
> > > asd sss lmn
> > > hig pqr mno
>
> > > File 2
>
> > > jih def asd
> > > poi iuu wer
> > > wer pqr jjj
>
> > > I would like have the output as
> > > Output
>
> > > File1
> > > asd sss lmn
> > > File2
> > > poi iuu wer
>
> > > Basically I want to compare the two files based on second column. If
> > > the second
> > > column matches on both the files do not print anything, else if there
> > > is no matc
> > > h in for the second column for first file in second file then print it
> > > under Fil
> > > e1 header, else if there is no match for the second column for second
> > > file in fi
> > > rst file print it under File2 header.
>
> > > Thankyou
> > > Madhur
>
> > file1 = open('file1.txt','rb')
> > file2 = open('file2.txt','rb')
>
> > file1_line = file1.next()
> > file2_line = file2.next()
>
> > while file1_line and file2_line:
> > try:
> > f1_col2 = file1_line.split(' ')[1]
> > except IndexError:
> > print 'Not enough delimiters in line.'
> > try:
> > f2_col2 = file2_line.split(' ')[2]
> > except IndexError:
> > print 'Not enough delimiters in line.'
>
> > if f1_col2 != f2_col2:
> > outfile_data_to_relevant_files()
>
> > file1_line = file1.next()
> > file2_line = file2.next()
>
> > HTH
> > Chris
>
> If the files2 is unordered, then the above logic does not work. How to
> takle it?

Take a look at *nix's sort command, it can also sort based on a key
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: XOR encryption

2008-01-18 Thread joe jacob
On Jan 18, 4:11 pm, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:
> On Fri, 18 Jan 2008 03:06:51 -0800, joe jacob wrote:
> > I wrote a python script to perform XOR encryption on a text and write
> > the encrypted text to a file. But when I try to read the file as the
> > encrypted text contains an EOF in between the file is read only to the
> > first EOF and remaining part of the text is not read.
>
> You have to open the file in binary mode ('rb'/'wb') instead of text mode
> ('r'/'w') to stop Windows from interpreting the EOF character this way.
>
> Ciao,
> Marc 'BlackJack' Rintsch

Thanks a lot for the information now it is working fine.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: XOR encryption

2008-01-18 Thread Marc 'BlackJack' Rintsch
On Fri, 18 Jan 2008 03:06:51 -0800, joe jacob wrote:

> I wrote a python script to perform XOR encryption on a text and write
> the encrypted text to a file. But when I try to read the file as the
> encrypted text contains an EOF in between the file is read only to the
> first EOF and remaining part of the text is not read.

You have to open the file in binary mode ('rb'/'wb') instead of text mode
('r'/'w') to stop Windows from interpreting the EOF character this way.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


XOR encryption

2008-01-18 Thread joe jacob
I wrote a python script to perform XOR encryption on a text and write
the encrypted text to a file. But when I try to read the file as the
encrypted text contains an EOF in between the file is read only to the
first EOF and remaining part of the text is not read.

 I used the text "hello world" and code 109. The ASCII value of w is
119 which is XOR ed with 109 gives EOF (ascii 26). So when I read the
file next time and decrypts it with 109; I'm getting result as "hello
" as the encrypted w is considered as EOF and remaining pare is not
read. I'm posting the code below. Please give me some inputs ot sort
this out.

name='a.txt'
f1=open(name,"w")
text='hello world'
data=[]
code=109
coded=""
for i in range(0,len(text)):
coded = coded+chr(ord(text[i]) ^ code)
f1.write(coded)
print coded
f1.close()
f1=open(name,"r")
while 1:
char = f1.read(1)
if not char:
break
else:
char = chr(ord(char) ^ int(code))
data.append(char)
f1.close()
print data
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Filtering two files with uncommon column

2008-01-18 Thread Paul Rubin
Madhur <[EMAIL PROTECTED]> writes:
> If the files2 is unordered, then the above logic does not work. How to
> takle it?

This sounds like a homework problem.  Also, you are trying to reimplement
the unix "comm" command.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Loop in a loop?

2008-01-18 Thread Sacred Heart
On Jan 17, 7:39 pm, Paul Rubin  wrote:
> Sacred Heart <[EMAIL PROTECTED]> writes:
> > array1 = ['one','two','three','four']
> > array2 = ['a','b','c','d']
>
> > I want to loop through array1 and add elements from array2 at the end,
> > so it looks like this:
>
> > one a
> > two b
> > three c
> > four c
>
> The "functional" style is to use the zip function that someone
> described.  The old-fashioned way is simply:
>
>n = len(array1)
>for i in xrange(n):
>print array1[i], array2[i]
>
> You can modify this in various ways if the lengths of the lists are
> not equal.  E.g.

Thank you Paul, and everybody else contributing with answers of
various complexity. Although a whole bunch of them was way too complex
for my simple problem, but that's ok.

I now know how to use map and zip, and also learned some tips and
tricks.

Thanks.

All the best,
SH
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Tutorial.

2008-01-18 Thread Sacred Heart
On Jan 17, 11:30 pm, Rizwan <[EMAIL PROTECTED]> wrote:
> Hiya,
>
> I found one good website for python tutorial. just thought to share
> with community.
>
> Hope you all also like it..
>
>  http://python.objectis.net
>
> -MR

Thanks, looks like a nice collection of links. I've bookmarked the
page.

R,
SH
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Filtering two files with uncommon column

2008-01-18 Thread Madhur
On Jan 18, 2:37 pm, Chris <[EMAIL PROTECTED]> wrote:
> On Jan 18, 11:23 am, Madhur <[EMAIL PROTECTED]> wrote:
>
>
>
> > I would like to know the best way of generating filter of two files
> > based upon the following condition
>
> > I have two files. Contents of the first file is
>
> > File 1
> > abc def hij
> > asd sss lmn
> > hig pqr mno
>
> > File 2
>
> > jih def asd
> > poi iuu wer
> > wer pqr jjj
>
> > I would like have the output as
> > Output
>
> > File1
> > asd sss lmn
> > File2
> > poi iuu wer
>
> > Basically I want to compare the two files based on second column. If
> > the second
> > column matches on both the files do not print anything, else if there
> > is no matc
> > h in for the second column for first file in second file then print it
> > under Fil
> > e1 header, else if there is no match for the second column for second
> > file in fi
> > rst file print it under File2 header.
>
> > Thankyou
> > Madhur
>
> file1 = open('file1.txt','rb')
> file2 = open('file2.txt','rb')
>
> file1_line = file1.next()
> file2_line = file2.next()
>
> while file1_line and file2_line:
> try:
> f1_col2 = file1_line.split(' ')[1]
> except IndexError:
> print 'Not enough delimiters in line.'
> try:
> f2_col2 = file2_line.split(' ')[2]
> except IndexError:
> print 'Not enough delimiters in line.'
>
> if f1_col2 != f2_col2:
> outfile_data_to_relevant_files()
>
> file1_line = file1.next()
> file2_line = file2.next()
>
> HTH
> Chris

If the files2 is unordered, then the above logic does not work. How to
takle it?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [python] How to detect a remote webpage is accessible? (in HTTP)

2008-01-18 Thread Jarek Zgoda
甜瓜 napisał(a):
> Howdy, all,
>  I want to use python to detect the accessibility of website.
> Currently, I use urllib
> to obtain the remote webpage, and see whether it fails. But the problem is 
> that
> the webpage may be very large; it takes too long time. Certainly, it
> is no need to download
> the entire page. Could you give me a good and fast solution?
> Thank you.

Issue HTTP HEAD request.

-- 
Jarek Zgoda
Skype: jzgoda | GTalk: [EMAIL PROTECTED] | voice: +48228430101

"We read Knuth so you don't have to." (Tim Peters)
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Loop in a loop?

2008-01-18 Thread cokofreedom
> Hehe.. I remember seeing a similar one for Java and "Hello world"
> using more and more elaborate abstractions and design patterns but I
> can't find the link.
>
> George

This is not linked to Java but deals with Hello World

http://www.ariel.com.au/jokes/The_Evolution_of_a_Programmer.html
-- 
http://mail.python.org/mailman/listinfo/python-list


  1   2   >