Python C API String Memory Consumption

2009-04-07 Thread k3xji
When I run the following function, I seem to have a mem leak, a 20 mb
of memory
is allocated and is not freed. Here is the code I run:

>>> import esauth
>>> for i in range(100):

... ss = esauth.penc('sumer')
...
>>> for i in range(100):

... ss = esauth.penc('sumer')
...

And here is the penc() function.

static PyObject *
penc(PyObject *self, PyObject *args)
{
unsigned char *s= NULL;
unsigned char *buf = NULL;
PyObject * result = NULL;
unsigned int v,len,i = 0;

if (!PyArg_ParseTuple(args, "s#", &s, &len))
return NULL;

buf = strdup(s);
if (!buf) {
PyErr_SetString(PyExc_MemoryError,
"Out of memory: strdup failed");
return NULL;
}

/*string manipulation*/

result = PyString_FromString(buf);
free(buf);
return result;

}

Am I doing something wrong?

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


Re: How can I change size of GUI?

2009-04-07 Thread Eric Brunel
Muddy Coder wrote:
> Hi Folks,
>
> I copied code from book:
>
> class ScrolledText(Frame):
> def __init__(self, parent=None, text='', file=None):
> Frame.__init__(self, parent)
> self.pack(expand=YES, fill=BOTH)
> self.makeWidgets()
> self.settext(text, file)
> def makeWidgets(self):
> sbar = Scrollbar(self)
> text = Text(self, relief=SUNKEN, width=120)
> sbar.config(command=text.yview)
> text.config(yscrollcommand=sbar.set)
> sbar.pack(side=RIGHT, fill=Y)
> text.pack(side=LEFT, expand=YES, fill=BOTH)
> self.text = text
>
> It works, of course. But, the GUI is small, and I want to enlarge it.
> I tried to add in options of width=120 for Text(), but it did not
> work. Can somebody drop me a couple of lines for help? Thanks!

For what definition of 'did not work'? Seems perfectly fine to me. Just try to
add the lines:

root = Tk()
frm = ScrolledFrame(root)
frm.pack()
root.mainloop()

to the end of your code above, remove the line self.settext(text, file) which
calls a method that doesn't exist, then run it with your option width=120,
then without, and I can guarantee you'll see a difference (I do anyway...).

So I guess this code is part of a much bigger one, and that the problem lies
in the other part...

HTH anyway...
 - Eric -
--
http://mail.python.org/mailman/listinfo/python-list


Re: Anyone mannaged to access parallel port on windows xp?

2009-04-07 Thread Daniel Fetchinson
> I have a switch  that I should connect to the parallel port, but had no luck
> with it. Tha guy that made it for me told me that it would be easyer to
> connect via parallel instead the USB
> So did anyone have success? I only get suckess!! :-))
> tryed giveio.sys but it doesn't wort (can't figure out what is it)


Have you tried http://pyserial.wiki.sourceforge.net/pyParallel ?

Cheers,
Daniel

-- 
Psss, psss, put it down! - http://www.cafepress.com/putitdown
--
http://mail.python.org/mailman/listinfo/python-list


Re: in place list modification necessary? What's a better idiom?

2009-04-07 Thread Peter Otten
MooMaster wrote:

> Now we can't calculate a meaningful Euclidean distance for something
> like "Iris-setosa" and "Iris-versicolor" unless we use string-edit
> distance or something overly complicated, so instead we'll use a
> simple quantization scheme of enumerating the set of values within the
> column domain and replacing the strings with numbers (i.e. Iris-setosa
> = 1, iris-versicolor=2).

I'd calculate the distance as

def string_dist(x, y, weight=1):
return weight * (x == y)

You don't get a high resolution in that dimension, but you don't introduce
an element of randomness, either.

Peter

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


Re: in place list modification necessary? What's a better idiom?

2009-04-07 Thread Peter Otten
Peter Otten wrote:

> MooMaster wrote:
> 
>> Now we can't calculate a meaningful Euclidean distance for something
>> like "Iris-setosa" and "Iris-versicolor" unless we use string-edit
>> distance or something overly complicated, so instead we'll use a
>> simple quantization scheme of enumerating the set of values within the
>> column domain and replacing the strings with numbers (i.e. Iris-setosa
>> = 1, iris-versicolor=2).
> 
> I'd calculate the distance as
> 
> def string_dist(x, y, weight=1):
> return weight * (x == y)

oops, this must of course be (x != y).
 
> You don't get a high resolution in that dimension, but you don't introduce
> an element of randomness, either.
> 
> Peter
> 

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


Re: in place list modification necessary? What's a better idiom?

2009-04-07 Thread Carl Banks
On Apr 7, 12:38 am, Peter Otten <[email protected]> wrote:
> MooMaster wrote:
> > Now we can't calculate a meaningful Euclidean distance for something
> > like "Iris-setosa" and "Iris-versicolor" unless we use string-edit
> > distance or something overly complicated, so instead we'll use a
> > simple quantization scheme of enumerating the set of values within the
> > column domain and replacing the strings with numbers (i.e. Iris-setosa
> > = 1, iris-versicolor=2).
>
> I'd calculate the distance as
>
> def string_dist(x, y, weight=1):
>     return weight * (x == y)
>
> You don't get a high resolution in that dimension, but you don't introduce
> an element of randomness, either.

Does the algorithm require well-ordered data along the dimensions?
Though I've never heard of it, the fact that it's called "bisecting
Kmeans" suggests to me that it does, which means this wouldn't work.

However, the OP better be sure to set the scales for the quantized
dimensions high enough so that no clusters form containing points with
different discrete values.

That, in turn, suggests he might as well not even bother sending the
discrete values to the clustering algorithm, but instead to call it
for each unique set of discretes.  (However, I could imagine the
marginal cost of more dimensions is less than that of multiple runs;
I've been dealing with such a case at work.)

I'll leave it to the OP to decide.


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


Re: Anyone mannaged to access parallel port on windows xp?

2009-04-07 Thread alejandro
I have a PC at work and at home. I wanted to write the errors it throws out 
when i try to use the module but now it work on this PC(the one at work). I 
don't get it?!?! I tryed so many things and now I can't remember what I have 
done :-))) This is worse than before!
I will post the errors when i get home.
Thanks for the reply! 


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


Re: Anyone mannaged to access parallel port on windows xp?

2009-04-07 Thread alejandro
Yes that module is the one to blame for my headache! :-))

> Have you tried http://pyserial.wiki.sourceforge.net/pyParallel ?
>
> Cheers,
> Daniel
>
> -- 
> Psss, psss, put it down! - http://www.cafepress.com/putitdown 


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


Re: Python C API String Memory Consumption

2009-04-07 Thread k3xji
Interestaing I changed malloc()/free() usage with PyMem_xx APIs and
the problem resolved. However, I really cannot understand why the
first version does not work. Here is the latest code that has no
problems at all:

static PyObject *
penc(PyObject *self, PyObject *args)
{
PyObject * result = NULL;
unsigned char *s= NULL;
unsigned char *buf = NULL;
unsigned int v,len,i = 0;

if (!PyArg_ParseTuple(args, "s#", &s, &len))
return NULL;

buf = (unsigned char *) PyMem_Malloc(len);
if (buf == NULL) {
PyErr_NoMemory();
return NULL;
}

/* string manipulation. */

result = PyString_FromStringAndSize((char *)buf, len);
PyMem_Free(buf);
return result;
}

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


pseudo-code

2009-04-07 Thread r-w

If no internet connection:
if have files:
run anyway, with warning
else:
ERROR
else:
if error getting hash/files:
if have files:
run anyway, with warning
else:
ERROR
else:
run
--
http://mail.python.org/mailman/listinfo/python-list


Re: in place list modification necessary? What's a better idiom?

2009-04-07 Thread Peter Otten
Carl Banks wrote:

> On Apr 7, 12:38 am, Peter Otten <[email protected]> wrote:
>> MooMaster wrote:
>> > Now we can't calculate a meaningful Euclidean distance for something
>> > like "Iris-setosa" and "Iris-versicolor" unless we use string-edit
>> > distance or something overly complicated, so instead we'll use a
>> > simple quantization scheme of enumerating the set of values within the
>> > column domain and replacing the strings with numbers (i.e. Iris-setosa
>> > = 1, iris-versicolor=2).
>>
>> I'd calculate the distance as
>>
>> def string_dist(x, y, weight=1):
>> return weight * (x == y)
>>
>> You don't get a high resolution in that dimension, but you don't
>> introduce an element of randomness, either.
> 
> Does the algorithm require well-ordered data along the dimensions?
> Though I've never heard of it, the fact that it's called "bisecting
> Kmeans" suggests to me that it does, which means this wouldn't work.

I've read about K-Means in Segaran's "Collective Intelligence" which
describes it:

"K-Means clustering begins with k randomly placed centroids (points in space
that represent the center of the cluster), and assigns every item to the
nearest one. After the assignment, the centroids are moved to the average
location of all the nodes assigned to them, and the assignments are redone.
This process repeats until the assignments stop changing."
 
The book doesn't go into the theory, and "any distance would do" was my
assumption which may be wrong.

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


Re: Python C API String Memory Consumption

2009-04-07 Thread Carl Banks
On Apr 7, 12:01 am, k3xji  wrote:
> When I run the following function, I seem to have a mem leak, a 20 mb
> of memory
> is allocated and is not freed. Here is the code I run:
>
> >>> import esauth
> >>> for i in range(100):
>
> ...     ss = esauth.penc('sumer')
> ...
>
> >>> for i in range(100):
>
> ...     ss = esauth.penc('sumer')
> ...
>
> And here is the penc() function.
>
> static PyObject *
> penc(PyObject *self, PyObject *args)
> {
>         unsigned char *s= NULL;
>         unsigned char *buf = NULL;
>         PyObject * result = NULL;
>         unsigned int v,len,i = 0;
>
>         if (!PyArg_ParseTuple(args, "s#", &s, &len))
>         return NULL;
>
>         buf = strdup(s);
>         if (!buf) {
>                 PyErr_SetString(PyExc_MemoryError,
>                         "Out of memory: strdup failed");
>                 return NULL;
>         }
>
>         /*string manipulation*/
>
>         result = PyString_FromString(buf);
>         free(buf);
>         return result;
>
> }
>
> Am I doing something wrong?


It might just be an unfortunate case where malloc keeps allocating
memory higher and higher on the heap even though it frees all the
memory.  And since it doesn't give it back to the OS, it runs out.

However, Python apparently does leak a reference if passed a Unicode
object; PyArg_ParseTuple automatically creates an encoded string but
never decrefs it.  (That might be necessary evil to preserve
compatibility, though.  PyString_AS_STRING does it too.)


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


PIL\Tkinter and Transparencies, Rubber Lines, and Dragging Image Objects

2009-04-07 Thread W. eWatson
Basically, I'd like to know how one (broadly, e.g., references in Win-land) 
does IP (image processing) and drawing techniques such as rubber lines, and 
dragging image objects across the canvas. I know there are some pretty 
powerful toolkits out there, but I'd like to limit this to PIL and Tkinter. 
If it can't be done with them, then I'll consider other possibilities.  As a 
starter, on the topic of transparencies, consider this program that I pulled 
off the web and was posted in 1999. It purports to illustrate how one might 
produce a transparency.


   #!/usr/bin/python
   # see http://mail.python.org/pipermail/python-list/1999-May/003388.html
   from Tkinter import *
   import Image, ImageTk
   import tkFileDialog

   class Transparency:
def __init__(self, parent):
 self.canvas = Canvas(parent, bg='green')
 self.canvas.pack()
 b = Button(parent, command=self.open, text="Select graphics file")
 b.pack()

def open(self):
 self.canvas.delete(ALL)
 filename = tkFileDialog.askopenfilename()
 if filename != '':
  im = Image.open(filename)
  if im.mode != "RGBA":
   im = Image.open(filename).convert("RGBA")
   source = im.split()
   R, G, B, A = 0, 1, 2, 3
   mask = im.point(lambda i: i > 0 and 255) # use black as transparent
   source[A].paste(mask)
   im = Image.merge(im.mode, source)  # build a new multiband image

  self.graphic = ImageTk.PhotoImage(image=im)
  self.canvas.create_image(100, 100, image=self.graphic)
   if __name__ == "__main__":
root = Tk()
test = Transparency(root)
root.mainloop()

It colors the canvas green, and produces a black background. An image is
merged with the background. I tried out the program. It executes, but I
do not see where the transparency is apparent. I used a gif with a
picture of a telescope on a white background, and the result is what I
would see if I pasted the telescope and white background onto the green
canvas.

If there's something missing in my observation, I'd like to know what it is.

To further explore "drawing graphics", what roughly is the capability of 
Tkinter or PIL to allow one to place a transparent layer (mode, I guess in 
PIL may be roughly equivalent to a layer in tools like Photoshop) on top of 
an image and then move the transparency around over the image with a mouse?


--
   W. eWatson

 (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
  Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet

Web Page: 

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


Re: Issue with subprocess Module

2009-04-07 Thread Dave Angel

tarun wrote:

Hello All,

I've a batch file to be  invoke using a python script. The batch file has
pause, and the time, I need to send some command to the batch file from my
scripts. I placed both, the batch file (test.bat) and the python script
(test.py) in the same folder. And executed 'test.py'

(Please find the source files and error below).

*I get the following error:*
Traceback (most recent call last):
  File "", line 74, in run_nodebug
  File "D:\test.py", line 4, in 
proc = subprocess.Popen(my_bat,stdin=subprocess.PIPE)
  File "C:\Python25\lib\subprocess.py", line 588, in __init__
errread, errwrite) = self._get_handles(stdin, stdout, stderr)
  File "C:\Python25\lib\subprocess.py", line 717, in _get_handles
c2pwrite = self._make_inheritable(c2pwrite)
  File "C:\Python25\lib\subprocess.py", line 746, in _make_inheritable
DUPLICATE_SAME_ACCESS)
WindowsError: [Error 6] The handle is invalid

*Python Script:*
*test.py*
import subprocess,os
my_bat = os.getcwd()+'\\test.bat'
proc = subprocess.Popen(my_bat,stdin=subprocess.PIPE)
input = '\n'
proc.communicate(input)

*Batch File*
*test.bat*
echo "START'
pause
echo 'END'
Please help me with this issue.

Thanks In Advance,
Tarun

  
subprocess.Popen() is expecting the name of a program, which should 
normally have an extension of .exe   You're handing it a .bat file, 
which is not executable.  It only executes in the context of a command 
interpreter (shell), such as cmd.exe


You can probably do what you want by running "cmd.exe" and passing it 
"test.bat" as a parameter



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


Re: Some test fail on my new Python 2.6

2009-04-07 Thread R. David Murray
Sorin Schwimmer  wrote:
> 
> > Run 'make test' instead of running them one by one.
> 
> I did it *before* going one by one. I then tried individual tests in hope of 
> getting hints of what to look for. Actualy, for the first three tests, the 
> "make test" script commented that it is unusual for them to fail on my 
> platform (linux2).

Oh, yeah, I forgot that bit.

The _tkinter error means you don't have TCL/TK installed.  Not a problem
unless you want to use tkinter.  Unfortunately I forget what your other
errors were.

Can you just post the Skip messages (and any real errors) from the
make test run?  I can run through them quickly for you then, and
then we can focus on the ones that are the real errors.

--
R. David Murray http://www.bitdance.com

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


On Integration Testing

2009-04-07 Thread Emanuele D'Arrigo
Hi everybody,

I just finished unit-testing a couple of tightly related modules and
it's now time to test them together to a) check if they do talk to
each other in the ways they should, b) check how fast they can talk to
each other. The problem? Well, the problem is that potentially they
can have some pretty varied conversations!! There are a lot of
different messages that they can exchange, from simple question-answer
messages to more complex [initial request, confirmation requested,
confirmation sent, final response, public announce] sequences of
messages. All this in a one-to-many context, that is one module is
effectively a server and can be connected to zero to N clients.

Right now I do not have a particularly cohesive, consistent approach
to this type of testing. In short, I've never done it before. I'll
start with some simple tests, i.e. adding and removing clients and
sending some simple sequences of messages, maybe pushing up the number
of clients to see how much gets done in, say, a second. But it feels
fairly "unscientific". =)

I've already googled "integration testing" and I'm in the process of
reading a variety of articles. Is there any one that the people of the
python community would like to recommend?

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


Scraping a web page

2009-04-07 Thread Ronn Ross
I'm using the following to scrape a web page: import urllib

f = urllib.urlopen("http://www.google.com";)
s = f.read()

It is working, but it's returning the source of the page. Is there anyway I
can get almost a screen capture of the page?

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


Re: Issue with subprocess Module

2009-04-07 Thread Tim Golden

Dave Angel wrote:

tarun wrote:

Hello All,

I've a batch file to be  invoke using a python script. The batch file has
pause, and the time, I need to send some command to the batch file 
from my

scripts. I placed both, the batch file (test.bat) and the python script
(test.py) in the same folder. And executed 'test.py'

(Please find the source files and error below).

*I get the following error:*
Traceback (most recent call last):
  File "", line 74, in run_nodebug
  File "D:\test.py", line 4, in 
proc = subprocess.Popen(my_bat,stdin=subprocess.PIPE)
  File "C:\Python25\lib\subprocess.py", line 588, in __init__
errread, errwrite) = self._get_handles(stdin, stdout, stderr)
  File "C:\Python25\lib\subprocess.py", line 717, in _get_handles
c2pwrite = self._make_inheritable(c2pwrite)
  File "C:\Python25\lib\subprocess.py", line 746, in _make_inheritable
DUPLICATE_SAME_ACCESS)
WindowsError: [Error 6] The handle is invalid

*Python Script:*
*test.py*
import subprocess,os
my_bat = os.getcwd()+'\\test.bat'
proc = subprocess.Popen(my_bat,stdin=subprocess.PIPE)
input = '\n'
proc.communicate(input)

*Batch File*
*test.bat*
echo "START'
pause
echo 'END'
Please help me with this issue.

Thanks In Advance,
Tarun

  
subprocess.Popen() is expecting the name of a program, which should 
normally have an extension of .exe   You're handing it a .bat file, 
which is not executable.  It only executes in the context of a command 
interpreter (shell), such as cmd.exe


You can probably do what you want by running "cmd.exe" and passing it 
"test.bat" as a parameter


Sounds reasonable, but isn't actually true. This works fine:


import subprocess

open ("t.bat", "w").write ("echo hello")
subprocess.Popen ("t.bat")



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


Re: Python C API String Memory Consumption

2009-04-07 Thread MRAB

k3xji wrote:

Interestaing I changed malloc()/free() usage with PyMem_xx APIs and
the problem resolved. However, I really cannot understand why the
first version does not work. Here is the latest code that has no
problems at all:

static PyObject *
penc(PyObject *self, PyObject *args)
{
PyObject * result = NULL;
unsigned char *s= NULL;
unsigned char *buf = NULL;
unsigned int v,len,i = 0;

if (!PyArg_ParseTuple(args, "s#", &s, &len))
return NULL;

buf = (unsigned char *) PyMem_Malloc(len);
if (buf == NULL) {
PyErr_NoMemory();
return NULL;
}

/* string manipulation. */

result = PyString_FromStringAndSize((char *)buf, len);
PyMem_Free(buf);
return result;
}


In general I'd say don't mix your memory allocators. I don't know
whether CPython implements PyMem_Malloc using malloc, but it's better to
stick with CPython's memory allocators when writing for CPython.
--
http://mail.python.org/mailman/listinfo/python-list


Re: in place list modification necessary? What's a better idiom?

2009-04-07 Thread andrew cooke
Carl Banks wrote:
> import collections
> import itertools
>
> def createInitialCluster(fileName):
> fixedPoints = []
> # quantization is a dict that assigns sequentially-increasing
> numbers
> # to values when reading keys that don't yet exit
> quantization = defaultdict.collections(itertools.count().next)
> with open(fileName, 'r') as f:
> for line in f:
> dimensions = []
> for s in line.rstrip('\n').split(","):
> if isNumeric(s):
> dimensions.append(float(s))
> else:
> dimensions.append(float(quantization[s]))
> fixedPoints.append(Point(dimensions))
> return Cluster(fixedPoints)

nice reply (i didn't know defaultdict worked like that - very neat).

two small things i noticed:

1 - do you need a separate quantization for each column?  the code above
might give, for example, non-contiguous ranges of integers for a
particular column if a string occurs ("by accident" perhaps) in more than
one.

2 - don't bother with isNumeric.  just return the cast value or catch the
exception:

  [...]
  try:
dimensions.append(float(s))
  except:
dimensions.append(float(quantization[s]))

(not sure float() is needed there either if you're using a recent version
of python - only reason i can think of is to avoid integer division in
older versions).

andrew


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


Re: building release - assert-free python library

2009-04-07 Thread Ben Finney
grbgooglefan  writes:

> I would like to avoid [exceptions] when compiling the libpython.a
> library because when this libpython gets used for production purpose
> and it aborts, the application goes down without any means of
> graceful handling that error condition. How can we handle this?

Exceptions are a fundamental control flow mechanism in Python, and
Python programs need them enabled in order to function properly.

What you actually want is (as you say) to properly handle any
un-caught exceptions. This is done by writing an exception handler
function and then binding the name ‘sys.excepthook’
http://docs.python.org/library/sys#sys.excepthook> to your
function as the default exception handler.

-- 
 \“Somebody told me how frightening it was how much topsoil we |
  `\   are losing each year, but I told that story around the campfire |
_o__) and nobody got scared.” —Jack Handey |
Ben Finney
--
http://mail.python.org/mailman/listinfo/python-list


Re: [Python-Dev] PEP 382: Namespace Packages

2009-04-07 Thread M.-A. Lemburg
[Resent due to a python.org mail server problem]

On 2009-04-03 22:07, Martin v. Löwis wrote:
>> I'd like to extend the proposal to Python 2.7 and later.
> 
> I don't object, but I also don't want to propose this, so
> I added it to the discussion.
> 
> My (and perhaps other people's) concern is that 2.7 might
> well be the last release of the 2.x series. If so, adding
> this feature to it would make 2.7 an odd special case for
> users and providers of third party tools.

I certainly hope that we'll see more useful features backported
from 3.x to the 2.x series or forward ported from 2.x to 3.x
(depending on what the core developer preferences are).

Regarding this particular PEP, it is well possible to implement
an importer that provides the functionality for Python 2.3-2.7
versions, so it doesn't have to be an odd special case.

>> That's going to slow down Python package detection a lot - you'd
>> replace an O(1) test with an O(n) scan.
> 
> I question that claim. In traditional Unix systems, the file system
> driver performs a linear search of the directory, so it's rather
> O(n)-in-kernel vs. O(n)-in-Python. Even for advanced file systems,
> you need at least O(log n) to determine whether a specific file is
> in a directory. For all practical purposes, the package directory
> will fit in a single disk block (containing a single .pkg file, and
> one or few subpackages), making listdir complete as fast as stat.

On second thought, you're right, it won't be that costly. It
requires an os.listdir() scan due to the wildcard approach and in
some cases, such a scan may not be possible, e.g. when using
frozen packages. Indeed, the freeze mechanism would not even add
the .pkg files - it only handles .py file content.

The same is true for distutils, MANIFEST generators and other
installer mechanisms - it would have to learn to package
the .pkg files along with the Python files.

Another problem with the .pkg file approach is that the file extension
is already in use for e.g. Mac OS X installers.

You don't have those issues with the __pkg__.py file approach
I suggested.

>> Wouldn't it be better to stick with a simpler approach and look for
>> "__pkg__.py" files to detect namespace packages using that O(1) check ?
> 
> Again - this wouldn't be O(1). More importantly, it breaks system
> packages, which now again have to deal with the conflicting file names
> if they want to install all portions into a single location.

True, but since that means changing the package infrastructure, I think
it's fair to ask distributors who want to use that approach to also take
care of looking into the __pkg__.py files and merging them if
necessary.

Most of the time the __pkg__.py files will be empty, so that's not
really much to ask for.

>> This would also avoid any issues you'd otherwise run into if you want
>> to maintain this scheme in an importer that doesn't have access to a list
>> of files in a package directory, but is well capable for the checking
>> the existence of a file.
> 
> Do you have a specific mechanism in mind?

Yes: frozen modules and imports straight from a web resource.

The .pkg file approach requires a directory scan and additional
support from all importers.

The __pkg__.py approach I suggested can use existing importers
without modifications by checking for the existence of such
a Python module in an importer managed resource.

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Apr 07 2009)
>>> Python/Zope Consulting and Support ...http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...http://python.egenix.com/

2009-03-19: Released mxODBC.Connect 1.0.1  http://python.egenix.com/

::: Try our new mxODBC.Connect Python Database Interface for free ! 


   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
   Registered at Amtsgericht Duesseldorf: HRB 46611
   http://www.egenix.com/company/contact/
--
http://mail.python.org/mailman/listinfo/python-list


cgi file limit size?

2009-04-07 Thread R. David Murray
davidj411  wrote:
> I am wondering where the limitation of filesize comes from when i
> upload a large file.
> it uploads when the filesize is less than 20 MB (but not if larger).
> the script does not limit the filesize so it is either an HTTP
> specification or a webserver limit, right?
> maybe my connection to the server is timing out during the upload?
> web server is IIS 6.0.
> python is 2.5.2.
> IIS webmapping does not use "-u" b/c nothing works when that option is
> used.

What are you using to do the upload?  What error message do you get?

--
R. David Murray http://www.bitdance.com

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


Re: building release - assert-free python library

2009-04-07 Thread Ulrich Eckhardt
grbgooglefan wrote:
> How can I build a release and not the debug version of libpython.a?
> I have seen that there are assert, abort statements in lot many
> functions in Python code. I would like to avoid those when compiling
> the libpython.a library because when this libpython gets used for
> production purpose and it aborts, the application goes down without
> any means of graceful handling that error condition.

You are misunderstanding assertions. If an assertion fires, you always have
a programming error that can not be recovered from. So, your program will
go down anyway, the difference is a defined way from an assertion compared
to an undefined one that can mean random corruption sometime sooner or
later. That said, the only reason to go without assertions is to improve
performance.

Uli

-- 
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

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


Re: pseudo-code

2009-04-07 Thread Chris Rebert
On Tue, Apr 7, 2009 at 2:00 AM, r-w  wrote:
> If no internet connection:
>    if have files:
>        run anyway, with warning
>    else:
>        ERROR
> else:
>    if error getting hash/files:
>        if have files:
>            run anyway, with warning
>        else:
>            ERROR
>    else:
>        run

Please refrain from re-posting the /exact same thing/ without waiting
for a reasonable amount of time to pass, and then only when you don't
receive any useful replies, neither of which is the case. It's basic
netiquette.
See my reply to your original post.

Cheers,
Chris

-- 
I have a blog:
http://blog.rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Issue with subprocess Module

2009-04-07 Thread Albert Hopkins
On Tue, 2009-04-07 at 07:53 -0400, Dave Angel wrote:

> >   
> subprocess.Popen() is expecting the name of a program, which should 
> normally have an extension of .exe   You're handing it a .bat file, 
> which is not executable.  It only executes in the context of a command 
> interpreter (shell), such as cmd.exe
> 
> You can probably do what you want by running "cmd.exe" and passing it 
> "test.bat" as a parameter
> 

It seems to me that most people who have trouble with the subprocess
module are Windows users.  And indeed it does have a more *nix-like
interface.  But I'm wondering if there is another module that is more
Windows-friendly (thinking out loud)?



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


Re: Some test fail on my new Python 2.6

2009-04-07 Thread Sorin Schwimmer

> Run 'make test' instead of running them one by one.

I did it *before* going one by one. I then tried individual tests in hope of 
getting hints of what to look for. Actualy, for the first three tests, the 
"make test" script commented that it is unusual for them to fail on my platform 
(linux2).

SxN


  __
Instant Messaging, free SMS, sharing photos and more... Try the new Yahoo! 
Canada Messenger at http://ca.beta.messenger.yahoo.com/
--
http://mail.python.org/mailman/listinfo/python-list


Issue with subprocess Module

2009-04-07 Thread tarun
Hello All,

I've a batch file to be  invoke using a python script. The batch file has
pause, and the time, I need to send some command to the batch file from my
scripts. I placed both, the batch file (test.bat) and the python script
(test.py) in the same folder. And executed 'test.py'

(Please find the source files and error below).

*I get the following error:*
Traceback (most recent call last):
  File "", line 74, in run_nodebug
  File "D:\test.py", line 4, in 
proc = subprocess.Popen(my_bat,stdin=subprocess.PIPE)
  File "C:\Python25\lib\subprocess.py", line 588, in __init__
errread, errwrite) = self._get_handles(stdin, stdout, stderr)
  File "C:\Python25\lib\subprocess.py", line 717, in _get_handles
c2pwrite = self._make_inheritable(c2pwrite)
  File "C:\Python25\lib\subprocess.py", line 746, in _make_inheritable
DUPLICATE_SAME_ACCESS)
WindowsError: [Error 6] The handle is invalid

*Python Script:*
*test.py*
import subprocess,os
my_bat = os.getcwd()+'\\test.bat'
proc = subprocess.Popen(my_bat,stdin=subprocess.PIPE)
input = '\n'
proc.communicate(input)

*Batch File*
*test.bat*
echo "START'
pause
echo 'END'
Please help me with this issue.

Thanks In Advance,
Tarun
--
http://mail.python.org/mailman/listinfo/python-list


Re: PIL\Tkinter and Transparencies, Rubber Lines, and Dragging Image Objects

2009-04-07 Thread Eric Brunel
W. eWatson wrote:
> Basically, I'd like to know how one (broadly, e.g., references in Win-land)
> does IP (image processing) and drawing techniques such as rubber lines, and
> dragging image objects across the canvas. I know there are some pretty
> powerful toolkits out there, but I'd like to limit this to PIL and Tkinter.
> If it can't be done with them, then I'll consider other possibilities.  As a
> starter, on the topic of transparencies, consider this program that I pulled
> off the web and was posted in 1999. It purports to illustrate how one might
> produce a transparency.

OK, maybe I'm dumb but:

> #!/usr/bin/python
> # see http://mail.python.org/pipermail/python-list/1999-May/003388.html
> from Tkinter import *
> import Image, ImageTk
> import tkFileDialog
>
> class Transparency:
>  def __init__(self, parent):
>   self.canvas = Canvas(parent, bg='green')
>   self.canvas.pack()
>   b = Button(parent, command=self.open, text="Select graphics file")
>   b.pack()
>
>  def open(self):
>   self.canvas.delete(ALL)
>   filename = tkFileDialog.askopenfilename()
>   if filename != '':
>im = Image.open(filename)
>if im.mode != "RGBA":
> im = Image.open(filename).convert("RGBA")
> source = im.split()
> R, G, B, A = 0, 1, 2, 3
> mask = im.point(lambda i: i > 0 and 255) # use black as transparent
 

> source[A].paste(mask)
> im = Image.merge(im.mode, source)  # build a new multiband image
>
>self.graphic = ImageTk.PhotoImage(image=im)
>self.canvas.create_image(100, 100, image=self.graphic)
> if __name__ == "__main__":
>  root = Tk()
>  test = Transparency(root)
>  root.mainloop()
>
> It colors the canvas green, and produces a black background. An image is
> merged with the background. I tried out the program. It executes, but I
> do not see where the transparency is apparent. I used a gif with a
> picture of a telescope on a white background, and the result is what I
  

> would see if I pasted the telescope and white background onto the green
> canvas.

I have neither PIL, nor the image you're using so I can just guess. But if you
want to use a white background, maybe you should use a mask defined with:

mask = im.point(lambda i: 255 if i >= 255 else 0)

(if I understood the mask construction correctly...).

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


Re: Eval Problem

2009-04-07 Thread Victor Subervi
Yes, Python is dynamic, but my server farm, in this era of big business
screwing the client and cutting all services and customer support, uses such
crappy hardware that all normal code breaks, so I am forced to find
work-arounds. Here is more complete code:

ourFile = string.split(__file__, "/")
p = ourFile[len(ourFile) - 1]
p = p[: - 9]
site = ourFile[4]
bitsFile = p + ".bits"
bitties = 0
bits = []

Above, I define a bits file with bits of code that the broken python
interpreter will not evaluate with normal text, so must be separated into
its own file and called line by line and blended with separated files that
quote only text. Pain in the hind end ;)

try:
  file = open(bitsFile, "r")
  for line in file:
if len(line) > 2:
  bits.append(line)
  bitties += 1
except:
  pass
x = 1
while x <= bitties:
  file = open(p + str(x) + ".txt")
  for line in file:
print line
  print eval(bits[x - 1])
  x += 1

I have excluded the code where I call the separate text files for printing
normal text. They work. It's my code that I cannot get to work. For example,
if I take out the "eval" part of the above, it will nicely print the
commands, such as this:

tableTop(123,456)

which is supposed to call said fn. If I place that line in the file calling
the text files and the bits file it will execute just fine, but that
inevitably makes my job harder. Ideas?
TIA,
Victor

On Mon, Apr 6, 2009 at 6:37 PM, J. Clifford Dyer wrote:

> On Mon, 2009-04-06 at 15:11 -0400, Victor Subervi wrote:
> > Hi:
> > I have this code:
> >
> > x = 1
> > while x <= bitties:
> >   file = open(p + str(x) + ".txt")
> >   for line in file:
> > print line
> >   print eval(bits[x - 1])
> >   x += 1
> >
> > which throws this error:
> >
> > [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221]
> > PythonHandler mod_python.cgihandler: Traceback (most recent call
> > last):
> > [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221]
> > PythonHandler mod_python.cgihandler: File
> > "/usr/lib64/python2.4/site-packages/mod_python/apache.py", line 299,
> > in HandlerDispatch\n result = object(req)
> > [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221]
> > PythonHandler mod_python.cgihandler: File
> > "/usr/lib64/python2.4/site-packages/mod_python/cgihandler.py", line
> > 96, in handler\n imp.load_module(module_name, fd, path, desc)
> > [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221]
> > PythonHandler mod_python.cgihandler: File
> > "/var/www/vhosts/articles.13gems.com/httpdocs/index_frame.py", line
> > 89, in ?\n print eval(bits[1])
> > [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221]
> > PythonHandler mod_python.cgihandler: File "", line 1
> > [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221]
> > PythonHandler mod_python.cgihandler: tableBottom(348,180)
> > [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221]
> > PythonHandler mod_python.cgihandler: ^
> > [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221]
> > PythonHandler mod_python.cgihandler: SyntaxError: invalid syntax
> >
>
> Hmm.  That's not the problem I get.  For me, your code raises:
> "NameError: name 'bitties' is not defined"
>
> It's easier for us to help you if you present a self-contained piece of
> code that exhibits the problem you've encountered.  Creating that code
> will often reveal the problem with the original code, and you will have
> solved your own problem.
>
> The other problem with your code is that you are using eval.  Eval leads
> to difficult-to-debug errors, and is usually unnecessary, given the
> dynamic nature of python.  If you need to access a class method using a
> string, you can use getattr.  If you need to dynamically select a
> function based on some condition known in another variable, you can use
> a dictionary to hash into the function.
>
> It's hard to say what might be appropriate for your situation, because
> your code fragment is so... fragmentary.
>
> Cheers,
> Cliff
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


nonlocal in Python 2.6

2009-04-07 Thread Kay Schluehr
I always wondered about the decision to omit the nonlocal statement
from the Python 2.X series because it seems to be orthogonal to Python
2.5. Are there any chances for it to be back ported?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Web validation

2009-04-07 Thread J. Clifford Dyer
On Tue, 2009-04-07 at 08:44 +1000, r-w wrote:
> If no internet connection:
>  if have files:
>  run anyway, with warning
>  else:
>  ERROR
> else:
>  if error getting hash/files:
>  if have files:
>  run anyway, with warning
>  else:
>  ERROR
>  else:
>  run
> --
> http://mail.python.org/mailman/listinfo/python-list
> 

import logging

def run_with_files(files):
if files:
logging.warn("warning")
run()
else:
logging.error("error")
raise IOError

if internet_connection() and got_hash():
run()
else:
run_with_files(files)


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


Re: How to go about. On read/write locks

2009-04-07 Thread Piet van Oostrum
> "Diez B. Roggisch"  (DBR) wrote:

>>> This is a classical synchronization problem with a classical solution:
>>> You treat the readers as a group, and the writers individually. So you
>>> have a write lock that each writer has to acquire and release, but it is
>>> acquired only by the first reader and released by the last one.
>>> Therefore you need a counter of the number of readers, and manipulations
>>> of this counter must be protected by another lock.
>>> 

>DBR> I was going to suggest a similar approach but refused to because of a
>DBR> problem I see with your code as well - if the readers are reading very 
>fast
>DBR> (the OP didn't state what his actual application is, so it might not be a
>DBR> consumer-producer scheme which I don't think a dict would be the natural
>DBR> choice anyway) they can block the writer from writing alltogether.


>DBR> Or do I miss something here?

Yes, you missed note 2 at the end of my posting.
-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: [email protected]
--
http://mail.python.org/mailman/listinfo/python-list


Re: in place list modification necessary? What's a better idiom?

2009-04-07 Thread R. David Murray
"andrew cooke"  wrote:
> Carl Banks wrote:
> > import collections
> > import itertools
> >
> > def createInitialCluster(fileName):
> > fixedPoints = []
> > # quantization is a dict that assigns sequentially-increasing
> > numbers
> > # to values when reading keys that don't yet exit
> > quantization = defaultdict.collections(itertools.count().next)
> > with open(fileName, 'r') as f:
> > for line in f:
> > dimensions = []
> > for s in line.rstrip('\n').split(","):
> > if isNumeric(s):
> > dimensions.append(float(s))
> > else:
> > dimensions.append(float(quantization[s]))
> > fixedPoints.append(Point(dimensions))
> > return Cluster(fixedPoints)
> 
> nice reply (i didn't know defaultdict worked like that - very neat).
> 
> two small things i noticed:
> 
> 1 - do you need a separate quantization for each column?  the code above
> might give, for example, non-contiguous ranges of integers for a
> particular column if a string occurs ("by accident" perhaps) in more than
> one.
> 
> 2 - don't bother with isNumeric.  just return the cast value or catch the
> exception:
> 
>   [...]
>   try:
> dimensions.append(float(s))
>   except:
> dimensions.append(float(quantization[s]))

No, no, no; never use a bare except! :)

Do it MRAB's way and catch ValueError.

--
R. David Murray http://www.bitdance.com

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


building release - assert-free python library

2009-04-07 Thread grbgooglefan
How can I build a release and not the debug version of libpython.a?
I have seen that there are assert, abort statements in lot many
functions in Python code. I would like to avoid those when compiling
the libpython.a library because when this libpython gets used for
production purpose and it aborts, the application goes down without
any means of graceful handling that error condition.
How can we handle this?
Please help
--
http://mail.python.org/mailman/listinfo/python-list


Re: Scraping a web page

2009-04-07 Thread Tim Chase

f = urllib.urlopen("http://www.google.com";)
s = f.read()

It is working, but it's returning the source of the page. Is there anyway I
can get almost a screen capture of the page?


This is the job of a browser -- to render the source HTML.  As 
such, you'd want to look into any of the browser-automation 
libraries to hook into IE, FireFox, Opera, or maybe using the 
WebKit/KHTML control.  You may then be able to direct it to 
render the HTML into a canvas you can then treat as an image.


Another alternative might be provided by some web-services that 
will render a page as HTML with various browsers and then send 
you the result.  However, these are usually either (1) 
asynchronous or (2) paid services (or both).


-tkc





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


Re: pseudo-code

2009-04-07 Thread Steven D'Aprano
On Tue, 07 Apr 2009 19:00:30 +1000, r-w wrote:

> If no internet connection:
>  if have files:
>  run anyway, with warning
>  else:
>  ERROR
> else:
>  if error getting hash/files:
>  if have files:
>  run anyway, with warning
>  else:
>  ERROR
>  else:
>  run

Very pretty. Did you have a question, or did you just want to show off 
your L33T pseudo-code writing 5K1LL5?



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


Re: extract Infobox contents

2009-04-07 Thread J. Clifford Dyer
On Mon, 2009-04-06 at 23:41 +0100, Rhodri James wrote:
> On Mon, 06 Apr 2009 23:12:14 +0100, Anish Chapagain  
>  wrote:
> 
> > Hi,
> > I was trying to extract wikipedia Infobox contents which is in format
> > like given below, from the opened URL page in Python.
> >
> > {{ Infobox Software
> > | name   = Bash
> > | logo   = [[Image:bash-org.png|165px]]
> > | screenshot = [[Image:Bash demo.png|250px]]
> > | caption= Screenshot of bash and [[Bourne shell|sh]]
> > sessions demonstrating some features
> > | developer  = [[Chet Ramey]]
> > | latest release version = 4.0
> > | latest release date= {{release date|mf=yes|2009|02|20}}
> > | programming language   = [[C (programming language)|C]]
> > | operating system   = [[Cross-platform]]
> > | platform   = [[GNU]]
> > | language   = English, multilingual ([[gettext]])
> > | status = Active
> > | genre  = [[Unix shell]]
> > | source model   = [[Free software]]
> > | license= [[GNU General Public License]]
> > | website= [http://tiswww.case.edu/php/chet/bash/
> > bashtop.html Home page]
> > }} //upto this line
> >
> > I need to extract all data between {{ Infobox ...to }}
> >
> > Thank's if anyone can help,
> > am trying with
> >
> > s1='{{ Infobox'
> > s2=len(s1)
> > pos1=data.find("{{ Infobox")
> > pos2=data.find("\n",pos2)
> >
> > pat1=data.find("}}")
> >
> > but am ending up getting one line at top only.
> 
> How are you getting your data?  Assuming that you can arrange to get
> it one line at a time, here's a quick and dirty way to extract the
> infoboxes on a page.
> 
> infoboxes = []
> infobox = []
> reading_infobox = False
> 
> for line in feed_me_lines_somehow():
>  if line.startswith("{{ Infobox"):
>  reading_infobox = True
>  if reading_infobox:
>  infobox.append(line)
>  if line.startswith("}}"):
>  reading_infobox = False
>  infoboxes.append(infobox)
>   infobox = []
> 
> You end up with 'infoboxes' containing a list of all the infoboxes
> on the page, each held as a list of the lines of their content.
> For safety's sake you really should be using regular expressions
> rather than 'startswith', but I leave that as an exercise for the
> reader :-)
> 

I agree that startswith isn't the right option, but for matching two
constant characters, I don't think re is necessary.  I'd just do:

if '}}' in line:
pass

Then, as the saying goes, you only have one problem.

Cheers,
Cliff


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


Re: [Python-Dev] PEP 382: Namespace Packages

2009-04-07 Thread M.-A. Lemburg
On 2009-04-03 02:44, P.J. Eby wrote:
> At 10:33 PM 4/2/2009 +0200, M.-A. Lemburg wrote:
>> Alternative Approach:
>> -
>>
>> Wouldn't it be better to stick with a simpler approach and look for
>> "__pkg__.py" files to detect namespace packages using that O(1) check ?
> 
>> One of the namespace packages, the defining namespace package, will have
>> to include a __init__.py file.
> 
> Note that there is no such thing as a "defining namespace package" --
> namespace package contents are symmetrical peers.

That was a definition :-)

Definition namespace package := the namespace package having the
__pkg__.py file

This is useful to have since packages allowing integration of
other sub-packages typically come as a base package with some
basic infra-structure in place which is required by all other
namespace packages.

If the __init__.py file is not found among the namespace directories,
the importer will have to raise an exception, since the result
would not be a proper Python package.

>> * It's possible to have a defining package dir and add-one package
>> dirs.
> 
> Also possible in the PEP, although the __init__.py must be in the first
> such directory on sys.path.  (However, such "defining" packages are not
> that common now, due to tool limitations.)

That's a strange limitation of the PEP. Why should the location of
the __init__.py file depend on the order of sys.path ?

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Apr 03 2009)
>>> Python/Zope Consulting and Support ...http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...http://python.egenix.com/

2009-03-19: Released mxODBC.Connect 1.0.1  http://python.egenix.com/

::: Try our new mxODBC.Connect Python Database Interface for free ! 


   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
   Registered at Amtsgericht Duesseldorf: HRB 46611
   http://www.egenix.com/company/contact/
--
http://mail.python.org/mailman/listinfo/python-list


Re: in place list modification necessary? What's a better idiom?

2009-04-07 Thread MRAB

Carl Banks wrote:

MooMaster wrote:

So I'm reading in values from a file, and for each column I need to
dynamically discover the range of possible values it can take and
quantize if necessary. This is the solution I've come up with:


[snip]

#harvested from http://www.rosettacode.org/wiki/IsNumeric#Python
def isNumeric(string):
try:
i = float(string)
except ValueError:
return False
return True



[snip]


import collections
import itertools

def createInitialCluster(fileName):
fixedPoints = []
# quantization is a dict that assigns sequentially-increasing
numbers
# to values when reading keys that don't yet exit
quantization = defaultdict.collections(itertools.count().next)
with open(fileName, 'r') as f:
for line in f:
dimensions = []
for s in line.rstrip('\n').split(","):
if isNumeric(s):
dimensions.append(float(s))
else:
dimensions.append(float(quantization[s]))
fixedPoints.append(Point(dimensions))
return Cluster(fixedPoints)


I'd also remove the isNumeric() function. You're just converting to a
float if you can successfully convert to a float!

try:
dimensions.append(float(s))
except ValueError:
dimensions.append(float(quantization[s]))
--
http://mail.python.org/mailman/listinfo/python-list


Re: in place list modification necessary? What's a better idiom?

2009-04-07 Thread andrew cooke

just playing around - doesn't work with 3.0 due to lack of pattern binding
(which i think is coming back in 3.1?)

>>> from collections import defaultdict
>>> from itertools import count
>>> def mkdict(): return defaultdict(count().next)
...
>>> converters = defaultdict(mkdict)
>>> def to_float((i, s)):
...   try: return float(s)
...   except: return converters[i][s]
...
>>> def convert(line):
...   return map(to_float, zip(count(), line.split(',')))
...
>>> convert('1,2,red')
[1.0, 2.0, 0]
>>> convert('1,2,red')
[1.0, 2.0, 0]
>>> convert('1,2,blue')
[1.0, 2.0, 1]
>>> convert('1,2,blue,blue')
[1.0, 2.0, 1, 0]


andrew cooke wrote:
> Carl Banks wrote:
>> import collections
>> import itertools
>>
>> def createInitialCluster(fileName):
>> fixedPoints = []
>> # quantization is a dict that assigns sequentially-increasing
>> numbers
>> # to values when reading keys that don't yet exit
>> quantization = defaultdict.collections(itertools.count().next)
>> with open(fileName, 'r') as f:
>> for line in f:
>> dimensions = []
>> for s in line.rstrip('\n').split(","):
>> if isNumeric(s):
>> dimensions.append(float(s))
>> else:
>> dimensions.append(float(quantization[s]))
>> fixedPoints.append(Point(dimensions))
>> return Cluster(fixedPoints)
>
> nice reply (i didn't know defaultdict worked like that - very neat).
>
> two small things i noticed:
>
> 1 - do you need a separate quantization for each column?  the code above
> might give, for example, non-contiguous ranges of integers for a
> particular column if a string occurs ("by accident" perhaps) in more than
> one.
>
> 2 - don't bother with isNumeric.  just return the cast value or catch the
> exception:
>
>   [...]
>   try:
> dimensions.append(float(s))
>   except:
> dimensions.append(float(quantization[s]))
>
> (not sure float() is needed there either if you're using a recent version
> of python - only reason i can think of is to avoid integer division in
> older versions).
>
> andrew
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>


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


Re: in place list modification necessary? What's a better idiom?

2009-04-07 Thread andrew cooke
R. David Murray wrote:
>>   [...]
>>   try:
>> dimensions.append(float(s))
>>   except:
>> dimensions.append(float(quantization[s]))
>
> No, no, no; never use a bare except! :)

can you explain why?  i can't think of any reason why the code would be
better catching a specific exception.

as a general rule, maybe, but in this particular case i can't see a reason
- so i'm not sure if you're just pedantically following rules or if i've
missed something i should know.

andrew

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


Re: Issue with subprocess Module

2009-04-07 Thread Tim Golden

tarun wrote:

Hello All,

I've a batch file to be  invoke using a python script. The batch file has
pause, and the time, I need to send some command to the batch file from my
scripts. I placed both, the batch file (test.bat) and the python script
(test.py) in the same folder. And executed 'test.py'


I can't reproduce this in Python 2.6.1. The following is the
result of cut-and-pasting your test.py & test.bat and running
them from the command line:


C:\temp>test.py

C:\temp>echo "START'
"START'

C:\temp>pause
Press any key to continue . . .

C:\temp>echo 'END'
'END'

C:\temp>



which is pretty much which I'd expected. I know there have been quite
a few changes to the subprocess module between Python 2.5 & 2.6 so
maybe you need to upgrade. (Failing that, we'd have to work a bit
harder to pin down a specific bug in the 2.5 code since 2.5 is now
in bugfix-release mode only, I think).

BTW, echo simply echoes whatever follows it, regardless of
(or including) quotes of any sort. So just do: echo START.

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


Re: Re: How can I change size of GUI?

2009-04-07 Thread John Posner



For what definition of 'did not work'? Seems perfectly fine to me. Just try to
add the lines:

root = Tk()
  


Try adding a line like this:

root.geometry("400x300+100+75")

... which means: "make the window 400 pixels wide and 300 pixels high,
with the upperleft corner at point (100,75)"


frm = ScrolledFrame(root)
frm.pack()
root.mainloop()
  

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


Re: Python C API String Memory Consumption

2009-04-07 Thread John Machin
On Apr 7, 9:19 pm, MRAB  wrote:
> k3xji wrote:
> > Interestaing I changed malloc()/free() usage with PyMem_xx APIs and
> > the problem resolved. However, I really cannot understand why the
> > first version does not work. Here is the latest code that has no
> > problems at all:
>
> > static PyObject *
> > penc(PyObject *self, PyObject *args)
> > {
> >    PyObject * result = NULL;
> >    unsigned char *s= NULL;
> >    unsigned char *buf = NULL;
> >    unsigned int v,len,i = 0;
>
> >    if (!PyArg_ParseTuple(args, "s#", &s, &len))
> >         return NULL;
>
> >    buf = (unsigned char *) PyMem_Malloc(len);
> >    if (buf == NULL) {
> >            PyErr_NoMemory();
> >            return NULL;
> >    }
>
> >         /* string manipulation. */
>
> >    result = PyString_FromStringAndSize((char *)buf, len);
> >    PyMem_Free(buf);
> >    return result;
> > }
>
> In general I'd say don't mix your memory allocators. I don't know
> whether CPython implements PyMem_Malloc using malloc,

The fantastic manual (http://docs.python.org/c-api/
memory.html#overview) says: """the C allocator and the Python memory
manager ... implement different algorithms and operate on different
heaps""".

> but it's better to
> stick with CPython's memory allocators when writing for CPython.

for the reasons given in the last paragraph of the above reference.

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


Re: Issue with subprocess Module

2009-04-07 Thread MRAB

Tim Golden wrote:

tarun wrote:

Hello All,

I've a batch file to be  invoke using a python script. The batch file has
pause, and the time, I need to send some command to the batch file 
from my

scripts. I placed both, the batch file (test.bat) and the python script
(test.py) in the same folder. And executed 'test.py'


I can't reproduce this in Python 2.6.1. The following is the
result of cut-and-pasting your test.py & test.bat and running
them from the command line:


C:\temp>test.py

C:\temp>echo "START'
"START'

C:\temp>pause
Press any key to continue . . .

C:\temp>echo 'END'
'END'

C:\temp>



which is pretty much which I'd expected. I know there have been quite
a few changes to the subprocess module between Python 2.5 & 2.6 so
maybe you need to upgrade. (Failing that, we'd have to work a bit
harder to pin down a specific bug in the 2.5 code since 2.5 is now
in bugfix-release mode only, I think).

BTW, echo simply echoes whatever follows it, regardless of
(or including) quotes of any sort. So just do: echo START.


I couldn't reproduce it either. I wonder whether one or both of the
files is open in an editor and whether that could cause the problem.
--
http://mail.python.org/mailman/listinfo/python-list


Re: in place list modification necessary? What's a better idiom?

2009-04-07 Thread MRAB

andrew cooke wrote:

R. David Murray wrote:

  [...]
  try:
dimensions.append(float(s))
  except:
dimensions.append(float(quantization[s]))

No, no, no; never use a bare except! :)


can you explain why?  i can't think of any reason why the code would be
better catching a specific exception.

as a general rule, maybe, but in this particular case i can't see a reason
- so i'm not sure if you're just pedantically following rules or if i've
missed something i should know.


A bare exception will catch _any_ exception, even those you didn't
expect (technical term: "bug" :-)); for example, if you had written:

try:
dimension.append(float(s))
except:
dimensions.append(float(quantization[s]))

it would silently catch "NameError: name 'dimension' is not defined" and
perform the fallback action.
--
http://mail.python.org/mailman/listinfo/python-list


hashing and collision detection

2009-04-07 Thread Aaron Brady
Hi group,

You are making good on your promise from a year ago: "This is a
helpful group. Give us more to go on, and you are likely to receive
thousands of dollars worth of consulting for free."
http://groups.google.com/group/comp.lang.python/msg/2e2906eaa804812c
- Bryan Olson

I have an idea, which has probably been invented a thousand times, and
is probably a nice smooth round wheel by now, but I didn't know how to
websearch for it, and didn't try.

I am trying to floating-point collide rectangles, axis-oriented, on a
large board.  I thought about having every rectangle participate in a
hash, several units big, and merely testing collisions within high
population hashes.  The pathological case is where rectangles are very
small, and thus will be able to fit too many in a bucket.  I'm willing
to rule it out for now... for-ever.  

Most hash slots will only have zero or one members; therefore, I only
need to check collision in a small fraction of slots: those that have
two or more members.

If slots are too small, moving an object is an expensive operation,
because it will need to add and remove itself from many buckets.  If
slots are too large, too many objects can fit in them, and a simple
rectangle collision will check too many.  But for my purposes, the
balance should be a fairly large plateau.

I am thinking of a simple dictionary, which maps tuples to a list of
members in that slot: { ( 0, 0 ), ( 0, 3 ), ( 0, 6 ), ( 3,
0 ), ... }.  Where space becomes a concern, empty slots can be removed
and re-added to and from the dictionary.

Last note: the entire rectangles participate in the list of members of
a slot, not just their subsegments which reside in the particular
slot.

What are your thoughts?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Weird Tk Canvas coordinate issue

2009-04-07 Thread Tim Shannon
Here's my actual code, but I've tried to strip out the irrelevant parts, but
it should compile.  FYI, I'm using 2.6 not 3.  The code is basically drawing
a grid with points in it.  I'm fairly new to Python, so constructive
criticism is appreciated.

class Editor:

GSPACE = 80
OFFSET = 50

def __init__(self, master):

#global variables
self.grid = ex_Grid.Grid("", 10, 10)
self.showPaths = IntVar()
self.pathValid = False
self.master = master
self.isSaved = False

master.title("ex_hack Editor")
master.columnconfigure(0, weight=1)
master.rowconfigure(0, weight=1)

vscroll = Scrollbar(master)
hscroll = Scrollbar(master)

self.canvas = Canvas(master,
 height=768,
 width=1024,
 yscrollcommand=vscroll.set,
 xscrollcommand=hscroll.set)

self.canvas.grid(column=0, row=0, sticky="wens")

vscroll.config(command=self.canvas.yview)
hscroll.config(command=self.canvas.xview,
   orient="horizontal")

vscroll.grid(column=1, row=0, stick="ns")
hscroll.grid(column=0, row=1, sticky="we")
   #truncated for brevity


def draw_grid(self):
"""Loads the current grid on the application canvas
as well as into the other fields"""

#print self.OFFSET
self.canvas.delete(ALL)

#draw Border
self.canvas.create_rectangle(self.OFFSET,
 self.OFFSET,
 (self.grid.height * self.GSPACE) +
self.OFFSET,
 (self.grid.width * self.GSPACE) +
self.OFFSET,
 width=2)
self.canvas.create_rectangle(self.OFFSET-4,
 self.OFFSET-4,
 (self.grid.height * self.GSPACE) +
self.OFFSET+4,
 (self.grid.width * self.GSPACE) +
self.OFFSET+4,
 width=2)
#draw limit areas
for l in self.grid.limitAreas:
self.canvas.create_rectangle(self.g2c(l["x1"]) -
(self.GSPACE/4),
 self.g2c(l["y1"]) -
(self.GSPACE/4),
 self.g2c(l["x2"]) +
(self.GSPACE/4),
 self.g2c(l["y2"]) +
(self.GSPACE/4),
 outline="gray",
 fill="gray",
 stipple="gray12")


#draw spaces
for space in self.grid.grid.values():
self.canvas.create_line(self.g2c(space.x),
self.g2c(space.y),
self.g2c(space.x)+1,
self.g2c(space.y)+1,
tags="space")
if not space.is_empty():
self.draw_space(space)


self.canvas.config(scrollregion=self.canvas.bbox(ALL))



def g2c(self, coord):
"""Converts grid locations to their actual coordinates on
the drawing canvas"""

return (coord * self.GSPACE) + self.OFFSET + (self.GSPACE / 2)






On Mon, Apr 6, 2009 at 5:11 PM, John Posner  wrote:

> Tim Shannon wrote:
>
>> I'm new to python, so keep that in mind.
>>  I have a tk Canvas that I'm trying to draw on, and I want to start my
>> drawing at an offset (from 0) location.  So I can tweak this as I code, I
>> set this offset as a class level variable:
>>  def ClassName:
>>OFFSET = 20
>>
>>def __init__(self, master)):
>>self.canvas = Canvas(master)
>>self.canvas.create_rectangle(self.OFFSET,
>> self.OFFSET,
>> 300 + self.OFFSET,
>> 300 + self.OFFSET,
>> width=2)
>>   The weird thing is, it doesn't offset.  If I set my offset to 10, it
>> still starts drawing at 0,0. Here's the really weird part (at least to me),
>> if I put a print line right about my drawing statements to print the value
>> of the offset, it works like it should, and offsets the drawing.
>> If I remove the print line, the offset goes away.
>>  This makes no sense to me.
>>
> Tim Shannon wrote:
>
>> I'm new to python, so keep that in mind.
>>  I have a tk Canvas that I'm trying to draw on, and I want to start my
>> drawing at an offset (from 0) location.  So I can tweak this as I code, I
>> set this offset as a class level variable:
>>  def ClassName:
>>OFFSET = 20
>>
>>def __init__(self, master)):
>>self.canvas = Canvas(master)
>>self.canvas.create_rectangle(self.OFFSET,
>> self.OFFSET,
>> 300 

Re: Eval Problem

2009-04-07 Thread J. Cliff Dyer
OK.

You still haven't shown the code where tableTop gets defined, so your
code is unrunnable.  However, I think your problem is that wherever
tableTop lives, it isn't part of your globals or locals in eval.  See
the documentation on evals here:

http://www.python.org/doc/1.4/lib/node26.html

Something like the following might work:

print eval(line, {'tableTop': tableTop})

Cheers,
Cliff

On Tue, 2009-04-07 at 08:38 -0400, Victor Subervi wrote:
> 
> I have excluded the code where I call the separate text files for
> printing normal text. They work. It's my code that I cannot get to
> work. For example, if I take out the "eval" part of the above, it will
> nicely print the commands, such as this:
> 
> tableTop(123,456)
> 
> which is supposed to call said fn. If I place that line in the file
> calling the text files and the bits file it will execute just fine,
> but that inevitably makes my job harder. Ideas?
> TIA,
> Victor

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


Re: in place list modification necessary? What's a better idiom?

2009-04-07 Thread R. David Murray

On Tue, 7 Apr 2009 at 09:01, andrew cooke wrote:

R. David Murray wrote:

  [...]
  try:
dimensions.append(float(s))
  except:
dimensions.append(float(quantization[s]))


No, no, no; never use a bare except! :)


can you explain why?  i can't think of any reason why the code would be
better catching a specific exception.

as a general rule, maybe, but in this particular case i can't see a reason
- so i'm not sure if you're just pedantically following rules or if i've
missed something i should know.


What if the user pressed ctl-c right when the float was being converted
and appended?

Never use a bare except unless you have a specific reason to do so,
and there are very few of those.  (Yes, I should have said it that way
to start with, my apologies for going hyperbolic.)  Using because it
doesn't _look_ like it will cause issues is just asking for hard to
track down bugs :).

--
R. David Murray http://www.bitdance.com
--
http://mail.python.org/mailman/listinfo/python-list


RE: Scraping a web page

2009-04-07 Thread Support Desk
You could do something like below to get the rendered page.

Import os
site = 'website.com'
X = os.popen('lynx --dump %s' % site).readlines()







-Original Message-
From: Tim Chase [mailto:[email protected]] 
Sent: Tuesday, April 07, 2009 7:45 AM
To: Ronn Ross
Cc: [email protected]
Subject: Re: Scraping a web page

> f = urllib.urlopen("http://www.google.com";)
> s = f.read()
> 
> It is working, but it's returning the source of the page. Is there anyway
I
> can get almost a screen capture of the page?

This is the job of a browser -- to render the source HTML.  As 
such, you'd want to look into any of the browser-automation 
libraries to hook into IE, FireFox, Opera, or maybe using the 
WebKit/KHTML control.  You may then be able to direct it to 
render the HTML into a canvas you can then treat as an image.

Another alternative might be provided by some web-services that 
will render a page as HTML with various browsers and then send 
you the result.  However, these are usually either (1) 
asynchronous or (2) paid services (or both).

-tkc







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


Re: PIL\Tkinter and Transparencies, Rubber Lines, and Dragging Image Objects

2009-04-07 Thread W. eWatson
You got it. That lamda did look a little odd. The white background is opaque 
and the telescope is seen as green. The program will ask for a file. I 
didn't write the code.


Eric Brunel wrote:

W. eWatson wrote:

Basically, I'd like to know how one (broadly, e.g., references in Win-land)
does IP (image processing) and drawing techniques such as rubber lines, and
dragging image objects across the canvas. I know there are some pretty
powerful toolkits out there, but I'd like to limit this to PIL and Tkinter.
If it can't be done with them, then I'll consider other possibilities.  As a
starter, on the topic of transparencies, consider this program that I pulled
off the web and was posted in 1999. It purports to illustrate how one might
produce a transparency.


OK, maybe I'm dumb but:


#!/usr/bin/python
# see http://mail.python.org/pipermail/python-list/1999-May/003388.html
from Tkinter import *
import Image, ImageTk

...

HTH
 - Eric -



--
   W. eWatson

 (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
  Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet

Web Page: 

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


Re: [Python-Dev] PEP 382: Namespace Packages

2009-04-07 Thread P.J. Eby

At 02:30 PM 4/7/2009 +0200, M.-A. Lemburg wrote:

>> Wouldn't it be better to stick with a simpler approach and look for
>> "__pkg__.py" files to detect namespace packages using that O(1) check ?
>
> Again - this wouldn't be O(1). More importantly, it breaks system
> packages, which now again have to deal with the conflicting file names
> if they want to install all portions into a single location.

True, but since that means changing the package infrastructure, I think
it's fair to ask distributors who want to use that approach to also take
care of looking into the __pkg__.py files and merging them if
necessary.

Most of the time the __pkg__.py files will be empty, so that's not
really much to ask for.


This means your proposal actually doesn't add any benefit over the 
status quo, where you can have an __init__.py that does nothing but 
declare the package a namespace.  We already have that now, and it 
doesn't need a new filename.  Why would we expect OS vendors to start 
supporting it, just because we name it __pkg__.py instead of __init__.py?


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


PyXML and Python-2.6

2009-04-07 Thread Andrew MacKeith
The Python.org "SIG for XML Processing in Python" page indicates that 
"The SIG, through the mailing list and the PyXML project hosted on 
SourceForge...".


The PyXML project on SourceForge " is no longer maintained. ", so 
perhaps the SIG page could be updated.


Is there a guide to porting projects that depend on PyXML to Python-2.6?

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


Re: nonlocal in Python 2.6

2009-04-07 Thread Benjamin Peterson
Kay Schluehr  gmx.net> writes:

> 
> I always wondered about the decision to omit the nonlocal statement
> from the Python 2.X series because it seems to be orthogonal to Python
> 2.5. Are there any chances for it to be back ported?

The only reason it was not backported was that either no one provided a patch or
it never got reviewed and applied.




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


Re: Python C API String Memory Consumption

2009-04-07 Thread Benjamin Peterson
Carl Banks  gmail.com> writes:
> However, Python apparently does leak a reference if passed a Unicode
> object; PyArg_ParseTuple automatically creates an encoded string but
> never decrefs it.  (That might be necessary evil to preserve
> compatibility, though.  PyString_AS_STRING does it too.)

Unicode objects cache a copy of themselves as default encoded strings. It is
deallocated when the unicode object its self is.




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


Re: Issue with subprocess Module

2009-04-07 Thread Dave Angel

Tim Golden wrote:
Dave 
Angel wrote:

tarun wrote:

Hello All,

I've a batch file to be  invoke using a python script. The batch 
file has
pause, and the time, I need to send some command to the batch file 
from my

scripts. I placed both, the batch file (test.bat) and the python script
(test.py) in the same folder. And executed 'test.py'

(Please find the source files and error below).

*I get the following error:*
Traceback (most recent call last):
  File "", line 74, in run_nodebug
  File "D:\test.py", line 4, in 
proc = subprocess.Popen(my_bat,stdin=subprocess.PIPE)
  File "C:\Python25\lib\subprocess.py", line 588, in __init__
errread, errwrite) = self._get_handles(stdin, stdout, stderr)
  File "C:\Python25\lib\subprocess.py", line 717, in _get_handles
c2pwrite = self._make_inheritable(c2pwrite)
  File "C:\Python25\lib\subprocess.py", line 746, in _make_inheritable
DUPLICATE_SAME_ACCESS)
WindowsError: [Error 6] The handle is invalid

*Python Script:*
*test.py*
import subprocess,os
my_bat = os.getcwd()+'\\test.bat'
proc = subprocess.Popen(my_bat,stdin=subprocess.PIPE)
input = '\n'
proc.communicate(input)

*Batch File*
*test.bat*
echo "START'
pause
echo 'END'
Please help me with this issue.

Thanks In Advance,
Tarun

  
subprocess.Popen() is expecting the name of a program, which should 
normally have an extension of .exe   You're handing it a .bat file, 
which is not executable.  It only executes in the context of a 
command interpreter (shell), such as cmd.exe


You can probably do what you want by running "cmd.exe" and passing it 
"test.bat" as a parameter


Sounds reasonable, but isn't actually true. This works fine:


import subprocess

open ("t.bat", "w").write ("echo hello")
subprocess.Popen ("t.bat")



TJG





The docs of Popen() state that it uses CreateProcess() on Windows, so I 
didn't even try it.  Thanks for informing me it works.  I see now the 
COMSPEC manipulation in _execute_child(), but I'm still puzzled.  When I 
step through, it skips that part because we didn't specify shell= as an 
argument.   It still had not put the cmd.exe /c into the args variable 
when it called CreateProcess().  So is the Python CreateProcess more 
than a thin wrapper around Windows version?


Another bug in this program is that it does not include quotes around 
the program name.  If the current directory has a space in it, and an 
appropriately named executable happens to be in the parent directory of 
the one with the space, it'll get run instead of the batch.  For 
example, if the current directory  is   c:\source code\testand 
there's a file in the route calledc:\source.exe then it'll get run.


But what about the OP problem?  I now see it runs okay for me, both 
stand-alone and inside Komodo.  But he's getting an exception inside 
make_inheritable(), which is apparently being passed an invalid handle.


Runs OK for me, in Python 2.6 running under XP, SP3.  2.6.1 
(r261:67517, Dec  4 2008, 16:51:00) [MSC v.1500 32 bit (Intel)]



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


Re: in place list modification necessary? What's a better idiom?

2009-04-07 Thread andrew cooke
MRAB wrote:
> andrew cooke wrote:
>> R. David Murray wrote:
   [...]
   try:
 dimensions.append(float(s))
   except:
 dimensions.append(float(quantization[s]))
>>> No, no, no; never use a bare except! :)
>>
>> can you explain why?  i can't think of any reason why the code would be
>> better catching a specific exception.
>>
>> as a general rule, maybe, but in this particular case i can't see a
>> reason
>> - so i'm not sure if you're just pedantically following rules or if i've
>> missed something i should know.
>>
> A bare exception will catch _any_ exception, even those you didn't
> expect (technical term: "bug" :-)); for example, if you had written:
>
>  try:
>  dimension.append(float(s))
>  except:
>  dimensions.append(float(quantization[s]))
>
> it would silently catch "NameError: name 'dimension' is not defined" and
> perform the fallback action.

true, i hadn't considered coding errors (i was thinking that something
global, like out of memory, would fail again anyway).  andrew

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


Python syntax

2009-04-07 Thread janus99
well i'm pretty much a newb on python, it's my first comp lang. and
i've been having some diffuclties, i want to get the hang of it and
i'm wondering if u could help me understand the syntax structure.

I messed around with my own comp (windos xp) command prompt and i
currently play a mud, but the python syntax is completly diffrent, i
was wondering if u could expalin how to syntax goes, i've tried the
"hello world" but it keep displaying syntax error, i've tried entering
random commands, e.g find,search, collect,...

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


Adding method to class at run-time: bad style?

2009-04-07 Thread Grant Edwards
I realize that technically all methods are added to classes at
"run-time", but what I'm talking about is this:

   import ClientForm

   def controlEqual(self,other):
   return (self.type == other.type) and \
  (self.name == other.name) and \
  (self.value == other.value) and \
  (self.disabled == other.disabled) and\
  (self.readonly == self.readonly)
   
   def controlNotEqual(self,other):
   return not (self==other)
   
   ClientForm.Control.__eq__ = controlEqual
   ClientForm.Control.__ne__ = controlNotEqual
   
   def formEqual(self,other):
   if len(self.controls) != len(other.controls):
   return False
   for (c1,c2) in zip(self.controls,other.controls):
   if c1 != c2:
   return False
   return True
   
   def formNotEqual(self,other):
   return not (self==other)
   
   ClientForm.HTMLForm.__eq__ = formEqual
   ClientForm.HTMLForm.__ne__ = formNotEqual

It works fine, but it seems like it might be dangerous (or just
bad form) to insert methods into existing classes like that.
Perhaps it would be safer to make sure that __eq__, __ne__,
and __cmp__ don't exist before adding my own?

-- 
Grant Edwards   grante Yow! NEWARK has been
  at   REZONED!!  DES MOINES has
   visi.combeen REZONED!!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Adding method to class at run-time: bad style?

2009-04-07 Thread Michele Simionato
On Apr 7, 4:37 pm, Grant Edwards  wrote:
> I realize that technically all methods are added to classes at
> "run-time", but what I'm talking about is this:


Raymond Hettinger solves this problem nicely with a class decorator:
http://code.activestate.com/recipes/576685/
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python syntax

2009-04-07 Thread dorzey
http://wiki.python.org/moin/BeginnersGuide is probably a good place to
start. I found lots of useful info from the links on this page when I
started programming in Python.

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


Re: PyXML and Python-2.6

2009-04-07 Thread Paul Boddie
On 7 Apr, 16:01, Andrew MacKeith  wrote:
> The Python.org "SIG for XML Processing in Python" page indicates that
> "The SIG, through the mailing list and the PyXML project hosted on
> SourceForge...".
>
> The PyXML project on SourceForge " is no longer maintained. ", so
> perhaps the SIG page could be updated.

I think the SIG pages should be migrated to the Wiki. There seems to
be some work proceeding on this:

http://wiki.python.org/moin/Special_Interest_Groups

> Is there a guide to porting projects that depend on PyXML to Python-2.6?

You could start here:

http://wiki.python.org/moin/PythonXml

Look under "W3C DOM-like libraries" for some candidates. I've
maintained libxml2dom for quite some time because I started out with
PyXML and wanted something whose maintenance was guaranteed, even if
it was me who was going to be doing it.

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


Re: [Python-Dev] PEP 382: Namespace Packages

2009-04-07 Thread M.-A. Lemburg
On 2009-04-07 16:05, P.J. Eby wrote:
> At 02:30 PM 4/7/2009 +0200, M.-A. Lemburg wrote:
>> >> Wouldn't it be better to stick with a simpler approach and look for
>> >> "__pkg__.py" files to detect namespace packages using that O(1)
>> check ?
>> >
>> > Again - this wouldn't be O(1). More importantly, it breaks system
>> > packages, which now again have to deal with the conflicting file names
>> > if they want to install all portions into a single location.
>>
>> True, but since that means changing the package infrastructure, I think
>> it's fair to ask distributors who want to use that approach to also take
>> care of looking into the __pkg__.py files and merging them if
>> necessary.
>>
>> Most of the time the __pkg__.py files will be empty, so that's not
>> really much to ask for.
> 
> This means your proposal actually doesn't add any benefit over the
> status quo, where you can have an __init__.py that does nothing but
> declare the package a namespace.  We already have that now, and it
> doesn't need a new filename.  Why would we expect OS vendors to start
> supporting it, just because we name it __pkg__.py instead of __init__.py?

I lost you there.

Since when do we support namespace packages in core Python without
the need to add some form of magic support code to __init__.py ?

My suggestion basically builds on the same idea as Martin's PEP,
but uses a single __pkg__.py file as opposed to some non-Python
file yaddayadda.pkg.

Here's a copy of the proposal, with some additional discussion
bullets added:

"""
Alternative Approach:
-

Wouldn't it be better to stick with a simpler approach and look for
"__pkg__.py" files to detect namespace packages using that O(1) check ?

This would also avoid any issues you'd otherwise run into if you want
to maintain this scheme in an importer that doesn't have access to a list
of files in a package directory, but is well capable for the checking
the existence of a file.

Mechanism:
--

If the import mechanism finds a matching namespace package (a directory
with a __pkg__.py file), it then goes into namespace package scan mode and
scans the complete sys.path for more occurrences of the same namespace
package.

The import loads all __pkg__.py files of matching namespace packages
having the same package name during the search.

One of the namespace packages, the defining namespace package, will have
to include a __init__.py file.

After having scanned all matching namespace packages and loading
the __pkg__.py files in the order of the search, the import mechanism
then sets the packages .__path__ attribute to include all namespace
package directories found on sys.path and finally executes the
__init__.py file.

(Please let me know if the above is not clear, I will then try to
follow up on it.)

Discussion:
---

The above mechanism allows the same kind of flexibility we already
have with the existing normal __init__.py mechanism.

* It doesn't add yet another .pth-style sys.path extension (which are
difficult to manage in installations).

* It always uses the same naive sys.path search strategy. The strategy
is not determined by some file contents.

* The search is only done once - on the first import of the package.

* It's possible to have a defining package dir and add-one package
dirs.

* The search does not depend on the order of directories in sys.path.
There's no requirement for the defining package to appear first
on sys.path.

* Namespace packages are easy to recognize by testing for a single
resource.

* There's no conflict with existing files using the .pkg extension
such as Mac OS X installer files or Solaris packages.

* Namespace __pkg__.py modules can provide extra meta-information,
logging, etc. to simplify debugging namespace package setups.

* It's possible to freeze such setups, to put them into ZIP files,
or only have parts of it in a ZIP file and the other parts in the
file-system.

* There's no need for a package directory scan, allowing the
mechanism to also work with resources that do not permit to
(easily and efficiently) scan the contents of a package "directory",
e.g. frozen packages or imports from web resources.

Caveats:

* Changes to sys.path will not result in an automatic rescan for
additional namespace packages, if the package was already loaded.
However, we could have a function to make such a rescan explicit.
"""

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Apr 07 2009)
>>> Python/Zope Consulting and Support ...http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...http://python.egenix.com/

2009-03-19: Released mxODBC.Connect 1.0.1  http://python.egenix.com/

::: Try our new mxODBC.Connect Python Database Interface for free ! 


   eGenix.com Software, Skills and Services Gmb

Expression

2009-04-07 Thread Lydia
I am working on calculating one of the fields in a feature class based on other 
2 fields. The logic is, 
A (the resulting field) is calculated from B, but C and D have part of the 
value that could fill the blank of B, which meaning that combine three fields 
of values can make A.

Field A is what I need.

The data looks like: .

A   B   C   D
 2  2
 5  5
 44
 6   6


 cur = gp.UpdateCursor(data)
row = cur.Next()
gp.CalculateField_management(data, "A", "[B]", "VB", "")
   
 while row:

cur.UpdateRow(row)

if  not(row.GetValue("C") == 'NULL'):
  row.SetValue("A",row.GetValue("C"));
  
elif not(row.GetValue("D") == 'NULL'):
  row.SetValue("A",row.GetValue("D"));

row = cur.Next()

del cur
del row

But the out looks like only B was calculated to A successfully. C&D are not in 
A. 

I guess there must be something wrong with the code, but I am very new to 
Python, and not familiar with the expression. Could anybody help ?  PS. I am 
coding Python with ARCGIS.

Thanks a lot.

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


RE: Scraping a web page

2009-04-07 Thread Support Desk
If your only interested in the Images, perhaps you want to use wget like:

 

wget -r --accept=jpg,jpeg www.xyz.org

 

or maybe this

 

http://www.vex.net/~x/python_stuff.html

 

BackCrawler   1.1

A crude web spider with only one purpose: mercilessly suck the background
images from all web pages it can find. Understands frames and redirects,
uses MD5 to elimate duplicates. Need web page backgrounds? This'll get lots
of them. Sadly, most are very tacky, and Backcrawler can't help with that.
Requires Threads.

 

 

  _  

From: Ronn Ross [mailto:[email protected]] 
Sent: Tuesday, April 07, 2009 9:37 AM
To: Support Desk
Subject: Re: Scraping a web page

 

This works great, but is there a way to do this with firefox or something
similar so I can also print the images from the site? 

On Tue, Apr 7, 2009 at 9:58 AM, Support Desk 
wrote:

You could do something like below to get the rendered page.

Import os
site = 'website.com'
X = os.popen('lynx --dump %s' % site).readlines()








-Original Message-
From: Tim Chase [mailto:[email protected]]
Sent: Tuesday, April 07, 2009 7:45 AM
To: Ronn Ross
Cc: [email protected]
Subject: Re: Scraping a web page

> f = urllib.urlopen("http://www.google.com";)
> s = f.read()
>
> It is working, but it's returning the source of the page. Is there anyway
I
> can get almost a screen capture of the page?

This is the job of a browser -- to render the source HTML.  As
such, you'd want to look into any of the browser-automation
libraries to hook into IE, FireFox, Opera, or maybe using the
WebKit/KHTML control.  You may then be able to direct it to
render the HTML into a canvas you can then treat as an image.

Another alternative might be provided by some web-services that
will render a page as HTML with various browsers and then send
you the result.  However, these are usually either (1)
asynchronous or (2) paid services (or both).

-tkc








 

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


Re: Scraping a web page

2009-04-07 Thread cgoldberg
> Is there anyway I
> can get almost a screen capture of the page?

I'm not sure exactly what you mean by "screen capture".  But the
webbrowser module in the standard lib might be of some help.  You can
use it to drive a web browser from Python.

to load a page in your browser, you can do something like this:

---
#! /usr/bin/env python

import webbrowser

url = 'http://www.google.com'
webbrowser.open(url)
---

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


Re: New online docs broken?

2009-04-07 Thread Jim Garrison

Ye Liu wrote:

On Apr 6, 6:33 pm, Jim Garrison  wrote:

I notice the online docs (at docs.python.org/3.0/index.html) were
updated today.  It seems some of the top-level pages, like
Tutorial, "Using Python", "Language Reference" are truncated
after the first few paragraphs.


Yea, same here. Hope it's going to be fixed soon.


Still broken.  I've emailed webmaster(at)python.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: [Python-Dev] PEP 382: Namespace Packages

2009-04-07 Thread David Cournapeau
On Tue, Apr 7, 2009 at 11:58 PM, M.-A. Lemburg  wrote:

>>
>> This means your proposal actually doesn't add any benefit over the
>> status quo, where you can have an __init__.py that does nothing but
>> declare the package a namespace.  We already have that now, and it
>> doesn't need a new filename.  Why would we expect OS vendors to start
>> supporting it, just because we name it __pkg__.py instead of __init__.py?
>
> I lost you there.
>
> Since when do we support namespace packages in core Python without
> the need to add some form of magic support code to __init__.py ?

I think P. Eby refers to the problem that most packaging systems don't
like several packages to have the same file - be it empty or not.
That's my main personal grip against namespace packages, and from this
POV, I think it is fair to say the proposal does not solve anything.
Not that I have a solution, of course :)

cheers,

David
>
> My suggestion basically builds on the same idea as Martin's PEP,
> but uses a single __pkg__.py file as opposed to some non-Python
> file yaddayadda.pkg.
>
> Here's a copy of the proposal, with some additional discussion
> bullets added:
>
> """
> Alternative Approach:
> -
>
> Wouldn't it be better to stick with a simpler approach and look for
> "__pkg__.py" files to detect namespace packages using that O(1) check ?
>
> This would also avoid any issues you'd otherwise run into if you want
> to maintain this scheme in an importer that doesn't have access to a list
> of files in a package directory, but is well capable for the checking
> the existence of a file.
>
> Mechanism:
> --
>
> If the import mechanism finds a matching namespace package (a directory
> with a __pkg__.py file), it then goes into namespace package scan mode and
> scans the complete sys.path for more occurrences of the same namespace
> package.
>
> The import loads all __pkg__.py files of matching namespace packages
> having the same package name during the search.
>
> One of the namespace packages, the defining namespace package, will have
> to include a __init__.py file.
>
> After having scanned all matching namespace packages and loading
> the __pkg__.py files in the order of the search, the import mechanism
> then sets the packages .__path__ attribute to include all namespace
> package directories found on sys.path and finally executes the
> __init__.py file.
>
> (Please let me know if the above is not clear, I will then try to
> follow up on it.)
>
> Discussion:
> ---
>
> The above mechanism allows the same kind of flexibility we already
> have with the existing normal __init__.py mechanism.
>
> * It doesn't add yet another .pth-style sys.path extension (which are
> difficult to manage in installations).
>
> * It always uses the same naive sys.path search strategy. The strategy
> is not determined by some file contents.
>
> * The search is only done once - on the first import of the package.
>
> * It's possible to have a defining package dir and add-one package
> dirs.
>
> * The search does not depend on the order of directories in sys.path.
> There's no requirement for the defining package to appear first
> on sys.path.
>
> * Namespace packages are easy to recognize by testing for a single
> resource.
>
> * There's no conflict with existing files using the .pkg extension
> such as Mac OS X installer files or Solaris packages.
>
> * Namespace __pkg__.py modules can provide extra meta-information,
> logging, etc. to simplify debugging namespace package setups.
>
> * It's possible to freeze such setups, to put them into ZIP files,
> or only have parts of it in a ZIP file and the other parts in the
> file-system.
>
> * There's no need for a package directory scan, allowing the
> mechanism to also work with resources that do not permit to
> (easily and efficiently) scan the contents of a package "directory",
> e.g. frozen packages or imports from web resources.
>
> Caveats:
>
> * Changes to sys.path will not result in an automatic rescan for
> additional namespace packages, if the package was already loaded.
> However, we could have a function to make such a rescan explicit.
> """
>
> --
> Marc-Andre Lemburg
> eGenix.com
>
> Professional Python Services directly from the Source  (#1, Apr 07 2009)
 Python/Zope Consulting and Support ...        http://www.egenix.com/
 mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
 mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
> 
> 2009-03-19: Released mxODBC.Connect 1.0.1      http://python.egenix.com/
>
> ::: Try our new mxODBC.Connect Python Database Interface for free ! 
>
>
>   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
>    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
>           Registered at Amtsgericht Duesseldorf: HRB 46611
>               http://www.egenix.com/company/contact/
> 

SoHo Book Problem

2009-04-07 Thread David C. Ullrich
Just curious - has anyone else bought the printed
Python 3 Reference Manual published by SoHo Books?

Talking about what they call "Part 2" of their Python
Documentation. I haven't looked in detail - Part 1
seems fine, but the typesetting in Part 2 is totally
screwed up.

I mean totally - on more or less every page there are lines
that are unreadable because there are words printed on top
of each other instead of next to each other!

On the one hand I don't see how the problem could be with
just my copy. On the other hand I don't see how they could
_all_ be like mine - that would mean nobody even _glanced_
at what was coming out of the press. So I'm curious whether
anyone else has a copy.

(I know it's all online. Some people like _books_...)

DU.

-- 
David C. Ullrich
--
http://mail.python.org/mailman/listinfo/python-list


Re: Best way to start

2009-04-07 Thread Tairic
I was looking for a language that seemed easier to learn and that was
powerful, so I just started learning Python myself. I'm using some
online tutorials and The book "Python Power!" by Matt Telles. Some of
the O'Reilley books looked ok (I spent a while in the bookstore
deciding), but this one was a bit cheaper, and seemed to fit the bill;
it covers most of what I wanted to know, and I found it pretty easy to
read. My only complaint is it doesn't have you spend much time coding,
so I scoured the internet for CS classes with python 'homework' and
did that. Anyways, good luck!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python C API String Memory Consumption

2009-04-07 Thread Floris Bruynooghe
On Apr 7, 2:10 pm, John Machin  wrote:
> On Apr 7, 9:19 pm, MRAB  wrote:
>
>
>
> > k3xji wrote:
> > > Interestaing I changed malloc()/free() usage with PyMem_xx APIs and
> > > the problem resolved. However, I really cannot understand why the
> > > first version does not work. Here is the latest code that has no
> > > problems at all:
>
> > > static PyObject *
> > > penc(PyObject *self, PyObject *args)
> > > {
> > >    PyObject * result = NULL;
> > >    unsigned char *s= NULL;
> > >    unsigned char *buf = NULL;
> > >    unsigned int v,len,i = 0;
>
> > >    if (!PyArg_ParseTuple(args, "s#", &s, &len))
> > >         return NULL;
>
> > >    buf = (unsigned char *) PyMem_Malloc(len);
> > >    if (buf == NULL) {
> > >            PyErr_NoMemory();
> > >            return NULL;
> > >    }
>
> > >         /* string manipulation. */
>
> > >    result = PyString_FromStringAndSize((char *)buf, len);
> > >    PyMem_Free(buf);
> > >    return result;
> > > }

I assume you're doing a memcpy() somewhere in there...  This is also
safer then your first version since the python string can contain an
embeded \0 and the strdup() of the first version would not copy that.
But maybe you're sure your input doesn't have NULLs in them so it
might be fine.

>
> > In general I'd say don't mix your memory allocators. I don't know
> > whether CPython implements PyMem_Malloc using malloc,
>
> The fantastic manual (http://docs.python.org/c-api/
> memory.html#overview) says: """the C allocator and the Python memory
> manager ... implement different algorithms and operate on different
> heaps""".
>
> > but it's better to
> > stick with CPython's memory allocators when writing for CPython.
>
> for the reasons given in the last paragraph of the above reference.

That document explictly says you're allowed to use malloc() and free()
in extensions.  There is nothing wrong with allocating things on
different heaps, I've done and seen it many times and never had
trouble.

Why the original problem ocurred I don't understand either tough.

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


Re: Delicious API and urllib2

2009-04-07 Thread Max Erickson
Bill  wrote:

> The delicious api requires http authorization (actually https). A
> generic delicious api post url is "https://
> username:[email protected]/v1/posts/add?url=http://
> example.com/&description=interesting&tags=whatever".
> 

The simplest way is probably to manually add the authentication 
header, as shown here:

http://code.activestate.com/recipes/267197/#c2

This article discusses the above strategy, and a strategy using 
HTTPBasicAuthHandler with a password manager default realm (I think 
getting the realm correct is what has stymied me in my attempts to use 
handlers, rather than injecting the header):

http://www.voidspace.org.uk/python/articles/authentication.shtml 


Max

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


Re: Issue with subprocess Module

2009-04-07 Thread R. David Murray
Tim Golden  wrote:
> tarun wrote:
> > Hello All,
> > 
> > I've a batch file to be  invoke using a python script. The batch file has
> > pause, and the time, I need to send some command to the batch file from my
> > scripts. I placed both, the batch file (test.bat) and the python script
> > (test.py) in the same folder. And executed 'test.py'
> 
> I can't reproduce this in Python 2.6.1. The following is the
> result of cut-and-pasting your test.py & test.bat and running
> them from the command line:
> 
> 
> C:\temp>test.py
> 
> C:\temp>echo "START'
> "START'
> 
> C:\temp>pause
> Press any key to continue . . .
> 
> C:\temp>echo 'END'
> 'END'
> 
> C:\temp>
> 
> 
> 
> which is pretty much which I'd expected. I know there have been quite
> a few changes to the subprocess module between Python 2.5 & 2.6 so
> maybe you need to upgrade. (Failing that, we'd have to work a bit
> harder to pin down a specific bug in the 2.5 code since 2.5 is now
> in bugfix-release mode only, I think).

2.5 is in security-fix-only mode.

--
R. David Murray http://www.bitdance.com

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


Re: On Integration Testing

2009-04-07 Thread Scott David Daniels

Emanuele D'Arrigo wrote:

Hi everybody,

I just finished unit-testing a couple of tightly related modules and
it's now time to test them together to a) check if they do talk to
each other in the ways they should, b) check how fast they can talk to
each other. The problem? Well, the problem is that potentially they
can have some pretty varied conversations!! There are a lot of
different messages that they can exchange, from simple question-answer
messages to more complex [initial request, confirmation requested,
confirmation sent, final response, public announce] sequences of
messages. All this in a one-to-many context, that is one module is
effectively a server and can be connected to zero to N clients.

Right now I do not have a particularly cohesive, consistent approach
to this type of testing. In short, I've never done it before. I'll
start with some simple tests, i.e. adding and removing clients and
sending some simple sequences of messages, maybe pushing up the number
of clients to see how much gets done in, say, a second. But it feels
fairly "unscientific". =)

I've already googled "integration testing" and I'm in the process of
reading a variety of articles. Is there any one that the people of the
python community would like to recommend?


Seems like what you want here is Function tests.  At this point you are
testing the whole system behavior.  I presume the modules are meant to
conspire to produce a result; check that that result is produced.  If
you really have a nice set of unit tests, the integration is testing
against a misunderstanding of the interface. Your functional tests begin
with simple straightforward tests, then push at definitional boundaries.
One thing to vary is client-server interaction lifetime.  Make some
clients slow and others fast in your mix.

Performance testing (seeing how much you can accomplish in a second) is,
I'd argue, a later phase (where you need a good model of actual use).
First, go for correctness under "strange" interactions.

--Scott David Daniels
[email protected]
--
http://mail.python.org/mailman/listinfo/python-list


Re: Adding method to class at run-time: bad style?

2009-04-07 Thread Scott David Daniels

Grant Edwards wrote:

I realize that technically all methods are added to classes at
"run-time", but what I'm talking about is this:

   import ClientForm

   def controlEqual(self,other):
   return (self.type == other.type) and \
  (self.name == other.name) and \
  (self.value == other.value) and \
  (self.disabled == other.disabled) and\
  (self.readonly == self.readonly)

Note:
Here you are checking that self.readonly == self.readonly!
I also dislike the trailing backslashes and over-parenthesizing.
So, I'd do it like this (using the pare to avoid the backslash):
 def controlEqual(self,other):
 return (self.type == other.type
 and self.name == other.name
 and self.value == other.value
 and self.disabled == other.disabled
 and self.readonly == self.readonly)
...

   ClientForm.Control.__eq__ = controlEqual
   ClientForm.Control.__ne__ = controlNotEqual
   
   def formEqual(self,other):

   if len(self.controls) != len(other.controls):
   return False
   for (c1,c2) in zip(self.controls,other.controls):
   if c1 != c2:
   return False
   return True
   
   def formNotEqual(self,other):

   return not (self==other)
   
   ClientForm.HTMLForm.__eq__ = formEqual

   ClientForm.HTMLForm.__ne__ = formNotEqual



It works fine, but it seems like it might be dangerous (or just
bad form) to insert methods into existing classes like that.
Perhaps it would be safer to make sure that __eq__, __ne__,
and __cmp__ don't exist before adding my own?


Well, a more standard approach would be:
class FancyControl(ClientForm.Control):
def __eq__(self, other):
return (self.type == other.type
and self.name == other.name
and self.value == other.value
and self.disabled == other.disabled
and self.readonly == self.readonly)

def __ne__(self, other):
return not self.__eq__(other)

class FancyHTMLForm(ClientForm.HTMLForm):
def __eq__(self,other):
...

def __ne__(self, other):
return not self.__eq__(other)

You'd have to make sure FancyControl and FancyClientForm get used in
the app.  The latter could be insured by monkey-patching:
...
ClientForm.Control = FancyControl
ClientForm.HTMLForm = FancyHTMLForm

But substituting monkey-patching for class method insertion seems like
you are going from one quick and dirty technique to a slightly nicer
quick and dirty technique.


--Scott David Daniels
[email protected]
--
http://mail.python.org/mailman/listinfo/python-list


named pipe and Linux

2009-04-07 Thread akineko
Hello everyone,

I'm trying to use named pipes to fuse a Python program and a C
program.
One side creates pipes using os.mkfifo() and both sides use the same
named pipes (one side reads, another side writes). The read side uses
select.select() to wait for incoming messages and read the message
when select.select() says it is ready.

The length of the message is unknown to the read side.
I cannot use file.read() because it will block waiting for an EOF.
I cannot use file.readline() because how many lines have arrived is
unknown.
So, I needed to use os.read() with the exact number of characters to
read.

Under Solaris environment, os.fstat() provides the exact size of the
message that has arrived.
Thus, two processes can communicate each other through the named pipes
without blocking.

However, the above scheme didn't work under Linux.
Linux os.fstat() returns size=0 even the message is pending.
(I think Linux buffers the message in memory while Solaris buffers the
message in a file system)

My question is, how can I make the named pipe scheme work under Linux?
Is there any way to read the message without getting blocked?

I know this is more Linux question than Python question but I believe
many Python programmers are strong Linux programmers.

Any suggestions will be appreciated.

Best regards,
Aki Niimura
--
http://mail.python.org/mailman/listinfo/python-list


extracting plain text from RTF-file ?

2009-04-07 Thread Stef Mientki

hello,

I'm looking for a library to extract plain text from RTF-files.
I found these

only RTF generation
http://pyrtf.sourceforge.net/

should be able to parse, but no download files
http://code.google.com/p/pyrtf-ng/


any suggestions ?
thanks,
Stef Mientki
--
http://mail.python.org/mailman/listinfo/python-list


Re: extracting plain text from RTF-file ?

2009-04-07 Thread Brent Bloxam

Stef Mientki wrote:

hello,

I'm looking for a library to extract plain text from RTF-files.
I found these

only RTF generation
http://pyrtf.sourceforge.net/

should be able to parse, but no download files
http://code.google.com/p/pyrtf-ng/


any suggestions ?
thanks,
Stef Mientki
--
http://mail.python.org/mailman/listinfo/python-list


Checkout from SVN, there's a release tagged as 0.45, but the trunk is 
newer by ~4 months

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


Re: Call to PyEval_EvalCodeEx crashes in PyFrame_New with abort

2009-04-07 Thread Aahz
[posted and e-mailed]

In article <72593fd7-4500-4ea9-b54e-1637d07b5...@s12g2000prc.googlegroups.com>,
grbgooglefan   wrote:
>
>I've emabedded Python(2.6) in my C++ application and using on Solaris
>5.10.  When the application calls Py_Eval, it causes an abort &
>application core dumps. But this is at random times & no pattern for
>this.

Given the lack of response, I suggest asking on the capi-sig.  The
likeliest problem is some kind of refcount error when the embedded code
calls back into your application.  Do you ever get problems when you use
pure Python code?
-- 
Aahz ([email protected])   <*> http://www.pythoncraft.com/

"...string iteration isn't about treating strings as sequences of strings, 
it's about treating strings as sequences of characters.  The fact that
characters are also strings is the reason we have problems, but characters 
are strings for other good reasons."  --Aahz
--
http://mail.python.org/mailman/listinfo/python-list


Re: UnknownTimeZoneError

2009-04-07 Thread Aahz
[posted & e-mailed]

In article <[email protected]>,
Brian   wrote:
>
>I'm running App Engine with Django. I'm having troubles executing
>timezone conversion via pytz. 

What version of Python are you using?  IIRC, GAE uses Python 2.3.  Do
other timezones work?  I suspect you might get more responses from a GAE
forum.
-- 
Aahz ([email protected])   <*> http://www.pythoncraft.com/

"...string iteration isn't about treating strings as sequences of strings, 
it's about treating strings as sequences of characters.  The fact that
characters are also strings is the reason we have problems, but characters 
are strings for other good reasons."  --Aahz
--
http://mail.python.org/mailman/listinfo/python-list


Re: [Python-Dev] PEP 382: Namespace Packages

2009-04-07 Thread P.J. Eby

At 04:58 PM 4/7/2009 +0200, M.-A. Lemburg wrote:

On 2009-04-07 16:05, P.J. Eby wrote:
> At 02:30 PM 4/7/2009 +0200, M.-A. Lemburg wrote:
>> >> Wouldn't it be better to stick with a simpler approach and look for
>> >> "__pkg__.py" files to detect namespace packages using that O(1)
>> check ?
>> >
>> > Again - this wouldn't be O(1). More importantly, it breaks system
>> > packages, which now again have to deal with the conflicting file names
>> > if they want to install all portions into a single location.
>>
>> True, but since that means changing the package infrastructure, I think
>> it's fair to ask distributors who want to use that approach to also take
>> care of looking into the __pkg__.py files and merging them if
>> necessary.
>>
>> Most of the time the __pkg__.py files will be empty, so that's not
>> really much to ask for.
>
> This means your proposal actually doesn't add any benefit over the
> status quo, where you can have an __init__.py that does nothing but
> declare the package a namespace.  We already have that now, and it
> doesn't need a new filename.  Why would we expect OS vendors to start
> supporting it, just because we name it __pkg__.py instead of __init__.py?

I lost you there.

Since when do we support namespace packages in core Python without
the need to add some form of magic support code to __init__.py ?

My suggestion basically builds on the same idea as Martin's PEP,
but uses a single __pkg__.py file as opposed to some non-Python
file yaddayadda.pkg.


Right... which completely obliterates the primary benefit of the 
original proposal compared to the status quo.  That is, that the PEP 
382 way is more compatible with system packaging tools.


Without that benefit, there's zero gain in your proposal over having 
__init__.py files just call pkgutil.extend_path() (in the stdlib 
since 2.3, btw) or pkg_resources.declare_namespace() (similar 
functionality, but with zipfile support and some other niceties).


IOW, your proposal doesn't actually improve the status quo in any way 
that I am able to determine, except that it calls for loading all the 
__pkg__.py modules, rather than just the first one.  (And the 
setuptools implementation of namespace packages actually *does* load 
multiple __init__.py's, so that's still no change over the status quo 
for setuptools-using packages.)


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


Re: python for loop

2009-04-07 Thread Aahz
In article ,
Lou Pecora   wrote:
>In article 
><5c92e9bd-1fb4-4c01-a928-04d7f6733...@e21g2000yqb.googlegroups.com>,
> Aaron Brady  wrote:
>> 
>> Did I tell you guys that 'natural' has 38 definitions at
>> dictionary.com?
>
>Amazing.  I suggest you pick the one that fits best.

"The wonderful thing about standards is that there are so many to choose
from."
-- 
Aahz ([email protected])   <*> http://www.pythoncraft.com/

"...string iteration isn't about treating strings as sequences of strings, 
it's about treating strings as sequences of characters.  The fact that
characters are also strings is the reason we have problems, but characters 
are strings for other good reasons."  --Aahz
--
http://mail.python.org/mailman/listinfo/python-list


Re: PyXML and Python-2.6

2009-04-07 Thread Stefan Behnel
Andrew MacKeith wrote:
> The Python.org "SIG for XML Processing in Python" page indicates that
> "The SIG, through the mailing list and the PyXML project hosted on
> SourceForge...".
> 
> The PyXML project on SourceForge " is no longer maintained. ", so
> perhaps the SIG page could be updated.
> 
> Is there a guide to porting projects that depend on PyXML to Python-2.6?

Depending on how large your project is (and how well you encapsulated the
XML code), a partial rewrite using ElementTree or lxml might be worth it.
You'd be rewarded by simple and very fast code.

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


Re: Q: "Best" book for teaching

2009-04-07 Thread Mike Driscoll
On Apr 6, 9:37 am, [email protected] wrote:
> I am considering teaching an "introduction to programming" course for
> continuing education adults at a local  community college. These would
> people with no programming experience, but I will require a reasonable
> facility with computers.
>
> What would be a good book to use as the text for the course?
>
> Thanks.

If they ever release it, this book might be good:

http://www.amazon.com/Hello-World-Computer-Programming-Beginners/dp/1933988495/ref=pd_bbs_sr_1?ie=UTF8&s=books&qid=1239129498&sr=8-1

Zelle's Python Programming book is pretty good (and was written by a
college professor) and I've heard good things about ORielly's Learning
Python by Lutz.

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


Re: Adding method to class at run-time: bad style?

2009-04-07 Thread Grant Edwards
On 2009-04-07, Grant Edwards  wrote:
> On 2009-04-07, Scott David Daniels  wrote:
>> Grant Edwards wrote:
>>> I realize that technically all methods are added to classes at
>>> "run-time", but what I'm talking about is this:
> ...
>>
>>>ClientForm.Control.__eq__ = controlEqual
>>>ClientForm.Control.__ne__ = controlNotEqual
> ...
>
>> Well, a more standard approach would be:
>>  class FancyControl(ClientForm.Control):
>>  def __eq__(self, other):
>>  return (self.type == other.type
>>  and self.name == other.name
>>  and self.value == other.value
>>  and self.disabled == other.disabled
>>  and self.readonly == self.readonly)
>>
>>  def __ne__(self, other):
>>  return not self.__eq__(other)

I like that, except it doesn't work.  Objects instantiated from
sub-classes of ClientForm.Control break.  I get errors like
this:

   Traceback (most recent call last):
 File "./testit.py", line 141, in 
   f1 = getSocketForm(0)
 File "./testit.py", line 99, in getSocketForm
   return getForm("Socket",n)
 File "./testit.py", line 88, in getForm
   forms = ClientForm.ParseResponse(response,backwards_compat=False)
 File "/usr/lib/python2.5/site-packages/ClientForm.py", line 1057, in 
ParseResponse
   return _ParseFileEx(response, response.geturl(), *args, **kwds)[1:]
 File "/usr/lib/python2.5/site-packages/ClientForm.py", line 1128, in 
_ParseFileEx
   type, name, attrs, select_default=select_default, index=ii*10)
 File "/usr/lib/python2.5/site-packages/ClientForm.py", line 2843, in 
new_control
   control.add_to_form(self)
 File "/usr/lib/python2.5/site-packages/ClientForm.py", line 2016, in 
add_to_form
   Control.add_to_form(self, form)
   TypeError: unbound method add_to_form() must be called with FancyControl 
instance as first argument (got CheckboxControl instance instead)

-- 
Grant Edwards   grante Yow! I want a WESSON OIL
  at   lease!!
   visi.com
--
http://mail.python.org/mailman/listinfo/python-list


3D plotting in a GUI

2009-04-07 Thread Baris Demir

Hi all,

I need to develop a GUI for some scientific data processing operations 
and this GUI should work well with a 3D plotting module, also with NumPy 
and SciPy for sure. I made a search about packages but, there are plenty 
of these modules available. What kind of a package or a package of 
packages might be the best way  for this  work. BTW I do not want to use 
MayaVi. It is too much actually.


Thanks in advance.
BD

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


Re: Tkinter, tkFileDialog - how to select multiple files ?

2009-04-07 Thread Steve Offutt
Hmm ... sorry folks - this works for me everywhere except on the
machine I wrote it on ...

Must be some sort of configuration problem...

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


Re: Issue with subprocess Module

2009-04-07 Thread Tim Golden

Tim Golden wrote:

So it looks as though the MS docs are simply wrong? I haven't
tried it with ctypes or a native C program, but I can't
see that they should be any different. If I get a chance
later I'll knock something up in C.



OK, copying the standard MS example for creating a process:

http://msdn.microsoft.com/en-us/library/ms682512(VS.85).aspx

and changing only enough to substitute "c:/temp/t.bat"
for the argv[1], it runs perfectly well. Doesn't answer
the OP's question, but does suggest the docs are wrong.

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


Re: Python syntax

2009-04-07 Thread Dave Angel

janus99 wrote:

well i'm pretty much a newb on python, it's my first comp lang. and
i've been having some diffuclties, i want to get the hang of it and
i'm wondering if u could help me understand the syntax structure.

I messed around with my own comp (windos xp) command prompt and i
currently play a mud, but the python syntax is completly diffrent, i
was wondering if u could expalin how to syntax goes, i've tried the
"hello world" but it keep displaying syntax error, i've tried entering
random commands, e.g find,search, collect,...


  
dorzey's suggestion is good, as is the tutorial available with your 
install.  Look for the file  python-docs-pdf-letter.zip, and inside it 
tutorial.pdf
If you didn't download that yet, you can get it from 
http://docs.python.org/download
I prefer the pdf form, but your preferences may be different.   Just try 
to get one that matches the version of the python you downloaded.



The other thing that you need to learn how to do is to cut & paste in 
your command window.  If you drag the mouse around the command window it 
should highlight some text.  Then you right click to put that on the 
clipboard.  Now you can go to some other program (like your mail 
program) and Ctrl-V to paste it.  If that mode isn't enabled on your 
command windows, fix it, although you can manually use the system menu 
to accomplish the same thing (which I do when I'm using someone else's 
machine).


When you ask for help here, please be specific.  What did you try, and 
exactly what was the error.  For all we know, you weren't even 
successful in getting the python.exe program to run at all.  So it could 
be a path problem, not a language syntax problem.  Here's a sample 
session.  If you can't get this far, show us exactly what you did get.


E:\>c:\ProgFiles\Python26\python.exe
Python 2.6.1 (r261:67517, Dec  4 2008, 16:51:00) [MSC v.1500 32 bit 
(Intel)] on

win32
Type "help", "copyright", "credits" or "license" for more information.
>>> "hello world"
'hello world'
>>> 3*12
36
>>> import sys
>>> sys.exit(0)

E:\>






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


PyCon Italy 2009: Early Bird Deadline

2009-04-07 Thread Alan Franzoni
The early-bird registration deadline for PyCon Tre (PyCon Italy 2009) is
April 13th, just a few days from now.

The conference will take place in Florence from May 8th till May 10th 2009,
and features guests like Guido Van Rossum, Alex Martelli, Raymond
Hettinger, Fredrik Lundh and Antonio Cangiano.

Feel free to take a look at the schedule:
http://www.pycon.it/pycon3/schedule/

A simultaneous interpretation service is available for the main track:

http://www.pycon.it/pycon3/non-italians

See you in Florence!


-- 
Alan Franzoni 
-
Remove .xyzz from my email in order to contact me.
-
GPG Key Fingerprint:
5C77 9DC3 BD5B 3A28 E7BC 921A 0255 42AA FE06 8F3E
--
http://mail.python.org/mailman/listinfo/python-list


PyCon Italy 2009: Early Bird Deadline

2009-04-07 Thread Alan Franzoni
The early-bird registration deadline for PyCon Tre (PyCon Italy 2009) is 
April 13th, just a few days from now.

The conference will take place in Florence from May 8th till May 10th 2009, 
and features guests like Guido Van Rossum, Alex Martelli, Raymond 
Hettinger, Fredrik Lundh and Antonio Cangiano.

Feel free to take a look at the schedule:
http://www.pycon.it/pycon3/schedule/

A simultaneous interpretation service is available for the main track:

http://www.pycon.it/pycon3/non-italians

See you in Florence!


-- 
Alan Franzoni 
-
Remove .xyzz from my email in order to contact me.
-
GPG Key Fingerprint:
5C77 9DC3 BD5B 3A28 E7BC 921A 0255 42AA FE06 8F3E
--
http://mail.python.org/mailman/listinfo/python-list


Re: Issue with subprocess Module

2009-04-07 Thread Tim Golden

Tim Golden wrote:

Sounds reasonable, but isn't actually true. This works fine:


import subprocess

open ("t.bat", "w").write ("echo hello")
subprocess.Popen ("t.bat")



TJG




Dave Angel wrote:
The docs of Popen() state that it uses CreateProcess() on Windows, so I 
didn't even try it.  


I only knew because I'd answered a similar question
recently and had simply tried it out!

Thanks for informing me it works.  I see now the 
COMSPEC manipulation in _execute_child(), but I'm still puzzled.  When I 
step through, it skips that part because we didn't specify shell= as an 
argument.   It still had not put the cmd.exe /c into the args variable 
when it called CreateProcess().  So is the Python CreateProcess more 
than a thin wrapper around Windows version?


Doesn't look like it. And using win32process.CreateProcess from
pywin32 does exactly the same. (Haven't checked their source
but I doubt they're finicking around with the parameters):


import win32process

open ("t.bat", "w").write ("echo hello")

win32process.CreateProcess (
 None,
 "t.bat",
 None,
 None,
 1,
 0,
 None,
 None,
 win32process.STARTUPINFO ()
)



gives


(, , 6208, 5732)




c:\temp>echo hello
hello







So it looks as though the MS docs are simply wrong? I haven't
tried it with ctypes or a native C program, but I can't
see that they should be any different. If I get a chance
later I'll knock something up in C.



But what about the OP problem?  I now see it runs okay for me, both 
stand-alone and inside Komodo.  But he's getting an exception inside 
make_inheritable(), which is apparently being passed an invalid handle.


Runs OK for me, in Python 2.6 running under XP, SP3.  2.6.1 
(r261:67517, Dec  4 2008, 16:51:00) [MSC v.1500 32 bit (Intel)]


Works under python 26/25/24 and the current subversion trunk,
and on 3.1 once I'd modified the input to be a bytes string.

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


Re: Returning different types based on input parameters

2009-04-07 Thread Adam Olsen
On Apr 6, 3:02 pm, George Sakkis  wrote:
> For example, it is common for a function f(x) to expect x to be simply
> iterable, without caring of its exact type. Is it ok though for f to
> return a list for some types/values of x, a tuple for others and a
> generator for everything else (assuming it's documented), or it should
> always return the most general (iterator in this example) ?

For list/tuple/iterable the correlation with the argument's type is
purely superficial, *because* they're so compatible.  Why should only
tuples and lists get special behaviour?  Why shouldn't every other
argument type return a list as well?

A counter example is python 3.0's str/bytes functions.  They're
mutually incompatible and there's no default.


> To take it further, what if f wants to return different types,
> differing even in a duck-type sense? That's easier to illustrate in a
> API-extension scenario. Say that there is an existing function `solve
> (x)` that returns `Result` instances.  Later someone wants to extend f
> by allowing an extra optional parameter `foo`, making the signature
> `solve(x, foo=None)`. As long as the return value remains backward
> compatible, everything's fine. However, what if in the extended case,
> solve() has to return some *additional* information apart from
> `Result`, say the confidence that the result is correct ? In short,
> the extended API would be:
>
>     def solve(x, foo=None):
>         '''
>         @rtype: `Result` if foo is None; (`Result`, confidence)
> otherwise.
>         '''
>
> Strictly speaking, the extension is backwards compatible; previous
> code that used `solve(x)` will still get back `Result`s. The problem
> is that in new code you can't tell what `solve(x,y)` returns unless
> you know something about `y`. My question is, is this totally
> unacceptable and should better be replaced by a new function `solve2
> (x, foo=None)` that always returns (`Result`, confidence) tuples, or
> it might be a justifiable cost ? Any other API extension approaches
> that are applicable to such situations ?

At a minimum it's highly undesirable.  You lose a lot of readability/
maintainability.  solve2/solve_ex is a little ugly, but that's less
overall, so it's the better option.

If your tuple gets to 3 or more I'd start wondering if you should
return a single instance, with the return values as attributes.  If
Result is already such a thing I'd look even with a tuple of 2 to see
if that's appropriate.
--
http://mail.python.org/mailman/listinfo/python-list


Tkinter, tkFileDialog - how to select multiple files ?

2009-04-07 Thread Steve Offutt
Let me preface by saying that I don't do much Python, however, I am in
my spare time, attempting to become somewhat comfortable with Python/
Tkinter.

I just can't seem to understand what is the problem with this:

--
import Tkinter, tkFileDialog

root = Tkinter.Tk()

files = tkFileDialog.askopenfilenames(parent = root, filetypes =
[('JPEG', '*.jpg'), ('ICON', '*.ico'), ('GIF', '*.gif')], title =
"Select files...", multiple = 1 )

print files
-

Any help greatly appreciated!

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


Request For Comment

2009-04-07 Thread Aahz
How RFC1 got created:

http://www.nytimes.com/2009/04/07/opinion/07crocker.html
-- 
Aahz ([email protected])   <*> http://www.pythoncraft.com/

"...string iteration isn't about treating strings as sequences of strings, 
it's about treating strings as sequences of characters.  The fact that
characters are also strings is the reason we have problems, but characters 
are strings for other good reasons."  --Aahz
--
http://mail.python.org/mailman/listinfo/python-list


Re: Adding method to class at run-time: bad style?

2009-04-07 Thread Grant Edwards
On 2009-04-07, Scott David Daniels  wrote:
> Grant Edwards wrote:
>> I realize that technically all methods are added to classes at
>> "run-time", but what I'm talking about is this:
>> 
>>import ClientForm
>> 
>>def controlEqual(self,other):
>>return (self.type == other.type) and \
>>   (self.name == other.name) and \
>>   (self.value == other.value) and \
>>   (self.disabled == other.disabled) and\
>>   (self.readonly == self.readonly)
> Note:
> Here you are checking that self.readonly == self.readonly!

Doh!

> I also dislike the trailing backslashes and over-parenthesizing.
> So, I'd do it like this (using the pare to avoid the backslash):

The paren to avoid the backslashes is a good tip.

>   def controlEqual(self,other):
>   return (self.type == other.type
>   and self.name == other.name
>   and self.value == other.value
>   and self.disabled == other.disabled
>   and self.readonly == self.readonly)

I don't really like to rely on my memory of operator
precidence, but that's a personal problem.

> ...
>>ClientForm.Control.__eq__ = controlEqual
>>ClientForm.Control.__ne__ = controlNotEqual
>>
>>def formEqual(self,other):
>>if len(self.controls) != len(other.controls):
>>return False
>>for (c1,c2) in zip(self.controls,other.controls):
>>if c1 != c2:
>>return False
>>return True
>>
>>def formNotEqual(self,other):
>>return not (self==other)
>>
>>ClientForm.HTMLForm.__eq__ = formEqual
>>ClientForm.HTMLForm.__ne__ = formNotEqual
>
>> It works fine, but it seems like it might be dangerous (or just
>> bad form) to insert methods into existing classes like that.
>> Perhaps it would be safer to make sure that __eq__, __ne__,
>> and __cmp__ don't exist before adding my own?
>
> Well, a more standard approach would be:
>  class FancyControl(ClientForm.Control):
>  def __eq__(self, other):
>  return (self.type == other.type
>  and self.name == other.name
>  and self.value == other.value
>  and self.disabled == other.disabled
>  and self.readonly == self.readonly)
>
>  def __ne__(self, other):
>  return not self.__eq__(other)
>
>  class FancyHTMLForm(ClientForm.HTMLForm):
>  def __eq__(self,other):
>  ...
>
>  def __ne__(self, other):
>  return not self.__eq__(other)
>
> You'd have to make sure FancyControl and FancyClientForm get used in
> the app.

The problem is that my app never instanciates either the
Control or the HTMLForm class: that's all done by module
functions that the app calls, and they don't know about the new
classes without some trickery (e.g. below)

> The latter could be insured by monkey-patching:
>  ...
>  ClientForm.Control = FancyControl
>  ClientForm.HTMLForm = FancyHTMLForm
>
> But substituting monkey-patching

:) How did I not know that phrase until today?

> for class method insertion seems like you are going from one
> quick and dirty technique to a slightly nicer quick and dirty
> technique.

I think like your monkey-patching solution better.  It feels a
bit cleaner.

-- 
Grant Edwards   grante Yow! !  Up ahead!  It's a
  at   DONUT HUT!!
   visi.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Creating Linked Lists in Python

2009-04-07 Thread andrew cooke
J-Burns wrote:
> How can I do this? And if I could do this by some other way than using
> linked lists than do tell me about that as well.

replying to this dead thread mainly for anyone using google.  there's now
a python regular expression engine in lepl and the source code can be seen
via http://www.acooke.org/lepl/api/lepl.regexp-module.html

in particular, i found that the critical collection was a map from
interval to values which automatically fragments (i think discussion of
this thread ended up looking at maps).  so, for example, if (1,10) maps to
'foo' then adding (4,5) -> 'bar' gives:

(1,3) -> foo
(4,5) -> foo, bar
(6,10) -> foo

in simple terms that lets you construct transitions for ranges of values -
typically a range of character with a mapped value that is the new state
in the machine.  so if you have a-d going to state 5, and then add d-f
going to state 6, you want d (ie ('d','d')) to go to both 5 and 6 when
constructing a nfa.

that structure is here -
http://www.acooke.org/lepl/api/lepl.regexp.interval-pysrc.html#TaggedFragments
(minimal docs at
http://www.acooke.org/lepl/api/lepl.regexp.interval.TaggedFragments-class.html)

hope that makes sense.  it could probably be more efficient (does an O(n)
scan of all intervals each time a new interval is added so is O(n^2)), but
since this is used in the "compile" part of a regexp, speed may not be as
important as in the "match" phase.

andrew


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


xml.dom.minidom getElementsByTagName white space issue

2009-04-07 Thread Leonardo lozanne
Hi,
 
I'm getting some XML tags with white spaces from a web service and when I try 
to get them with the getElements ByTagName I'm not able to do so. I'm getting 
an empty list. What I'm doing is:
 
#XML_response is an xml string
xml_msg = xml.dom.minidom.parseString(XML_response)
 
nodes = xml_msg.getElementsByTagName("tag ten")  #tag name is "tag ten" with a 
whitespace
 
It all works fine with tags like tag_seven but NOT for tag names with a white 
space. I've tried some escape chars but it doesnt seems to work. 
 
Does anybody has the escape char sequence I should be using or a work around 
for this? Thanks in advanced for your replies. 


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


  1   2   >