Re: Get tkinter text to the clipboard

2012-03-08 Thread bugzilla-mail-box
> How about
> 
> import tkinter
> root = tkinter.Tk()
> 
> root.clipboard_clear()
> root.clipboard_append("whatever")
> 

that works, thank you
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python recursive tree, linked list thingy

2012-03-08 Thread Enrico Franchi
Wanderer  wrote:

>  How
> do you handle this sort of thing in Python?

I believe that the best thing to do is a Union-Find algorithm.

Depending on the exact nature of your problem, you may also want to
check out the Hoshen-Kopelman Algorithm. Although the algorithm itself
is rather efficient, it was born in the context of percolation, that is
to say with the assumption that the "broken" (or colored) cells are much
more likely than in your context.

-- 
-riko
http://www.enrico-franchi.org/
http://rik0-techtemple.blogspot.com/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python recursive tree, linked list thingy

2012-03-08 Thread Robert Kern

On 3/8/12 9:12 AM, Enrico Franchi wrote:

Wanderer  wrote:


  How
do you handle this sort of thing in Python?


I believe that the best thing to do is a Union-Find algorithm.


Another term this problem is finding the "connected components". Here is some 
code from Stefan van der Walt for this:


  http://mentat.za.net/source/connected_components.tar.bz2
  http://mentat.za.net/cgi-bin/hgwebdir.cgi/ccomp

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

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


Re: Python site-packages permission denied?

2012-03-08 Thread Steven D'Aprano
On Wed, 07 Mar 2012 21:47:37 -0800, Ned Deily wrote:

> In article
> ,
>  Chris Rebert  wrote:
>> You generally shouldn't mess with Mac OS X's system copies of Python.
>> Typically, one installs a separate copy using MacPorts, Fink, or
>> whatever, and uses that instead.
> 
> I don't understand what you mean by "mess with".  Certainly one should
> not attempt alter standard library modules provided with the system
> Python but adding additional packages is fully supported.

I read Chris as making a general comment that one should be cautious 
about making changes to the system copy of Python, advice that holds for 
all OSes not just OS-X.


> Apple
> conveniently provides a special directory in user-controlled space
> (/Library/Python) as the default location for Distutils-based installs.
> They even provide versions of easy_install for the system Pythons.

Perhaps so, but it seems to have the permissions messed up, or some other 
problem, because the OP can't write to it. His error is:

copying build/lib/urllib2_file.py -> /Library/Python/2.7/site-packages
error: /Library/Python/2.7/site-packages/urllib2_file.py: 
Permission denied


I note also that Chris' final comment was:

"In any case, you generally need to `sudo` when installing stuff system-
wide."

which is probably the solution the OP is looking for.



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


Memory leak involving traceback objects

2012-03-08 Thread Ran Harel
I have the same problem with python 2.6.2.
I have upgraded to 2.7.1 and the leak is gone.
-- 
http://mail.python.org/mailman/listinfo/python-list


cython + scons + c++

2012-03-08 Thread Neal Becker
Is there a version of cython.py, pyext.py that will work with c++?

I asked this question some time ago, but never got an answer.

I tried the following code, but it doesn't work correctly.  If the commented 
lines are uncommented, the gcc command is totally mangled.

Although it did build my 1 test extension OK, I didn't use any libstdc++ - I 
suspect it won't link correctly in general because it doesn't seem to treat the 
code as c++ (treats it as c code).

cyenv = Environment(PYEXT_USE_DISTUTILS=True)
cyenv.Tool("pyext")
cyenv.Tool("cython")
import numpy

cyenv.Append(PYEXTINCPATH=[numpy.get_include()])
cyenv.Replace(CYTHONFLAGS=['--cplus'])
#cyenv.Replace(CXXFILESUFFIX='.cpp')
#cyenv.Replace(CYTHONCFILESUFFIX='.cpp')


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


Re: cython + scons + c++

2012-03-08 Thread Stefan Behnel
Neal Becker, 08.03.2012 15:23:
> Is there a version of cython.py, pyext.py that will work with c++?
> 
> I asked this question some time ago, but never got an answer.
> 
> I tried the following code, but it doesn't work correctly.  If the commented 
> lines are uncommented, the gcc command is totally mangled.
> 
> Although it did build my 1 test extension OK, I didn't use any libstdc++ - I 
> suspect it won't link correctly in general because it doesn't seem to treat 
> the 
> code as c++ (treats it as c code).
> 
> cyenv = Environment(PYEXT_USE_DISTUTILS=True)
> cyenv.Tool("pyext")
> cyenv.Tool("cython")
> import numpy
> 
> cyenv.Append(PYEXTINCPATH=[numpy.get_include()])
> cyenv.Replace(CYTHONFLAGS=['--cplus'])
> #cyenv.Replace(CXXFILESUFFIX='.cpp')
> #cyenv.Replace(CYTHONCFILESUFFIX='.cpp')

I don't use Scons, so I don't know if running the compiler at a command
line level is the best way to do it in that build system. But I know that
some people on the Cython users mailing list use it, so you may want to ask
over there.

https://groups.google.com/group/cython-users

Stefan

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


Re: cython + scons + c++

2012-03-08 Thread Dave Angel

On 03/08/2012 09:23 AM, Neal Becker wrote:

Is there a version of cython.py, pyext.py that will work with c++?

I asked this question some time ago, but never got an answer.

I tried the following code, but it doesn't work correctly.  If the commented
lines are uncommented, the gcc command is totally mangled.

Although it did build my 1 test extension OK, I didn't use any libstdc++ - I
suspect it won't link correctly in general because it doesn't seem to treat the
code as c++ (treats it as c code).

cyenv = Environment(PYEXT_USE_DISTUTILS=True)
cyenv.Tool("pyext")
cyenv.Tool("cython")
import numpy

cyenv.Append(PYEXTINCPATH=[numpy.get_include()])
cyenv.Replace(CYTHONFLAGS=['--cplus'])
#cyenv.Replace(CXXFILESUFFIX='.cpp')
#cyenv.Replace(CYTHONCFILESUFFIX='.cpp')




I don't know anything about writing c/c++ code with Python.  I have 
plenty of experience with each, but not together.


But the usual C++ answer is to use an extern "C"  declaration for any 
function you need to be visible to the outside world.  It prevents the 
usual C++ name mangling.


(It therefore also prevents function overloading and can't generally be 
used on class member functions)




--

DaveA

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


newb __init__ inheritance

2012-03-08 Thread hyperboogie
Hello everyone.

This is my first post in this group.
I started learning python a week ago from the "dive into python" e-
book and thus far all was clear.
However today while reading chapter 5 about objects and object
orientation I ran into something that confused me.
it says here:
http://www.diveintopython.net/object_oriented_framework/defining_classes.html#fileinfo.class.example

"__init__ methods are optional, but when you define one, you must
remember to explicitly call the ancestor's __init__ method (if it
defines one). This is more generally true: whenever a descendant wants
to extend the behavior of the ancestor, the descendant method must
explicitly call the ancestor method at the proper time, with the
proper arguments. "

However later on in the chapter:
http://www.diveintopython.net/object_oriented_framework/userdict.html

it says:
"Methods are defined solely by their name, and there can be only one
method per class with a given name. So if a descendant class has an
__init__ method, it always overrides the ancestor __init__ method,
even if the descendant defines it with a different argument list. And
the same rule applies to any other method. "

My question is if __init__ in the descendant class overrides __init__
in the parent class how can I call the parent's __init__ from the
descendant class - I just overrode it didn't I?

Am I missing something more fundamental here?
Thanks
-- 
http://mail.python.org/mailman/listinfo/python-list


Can't get around HTTP/401 response using SUDS

2012-03-08 Thread Adam Tauno Williams
SUDS version 0.4 pn x86_64 Python 2.7

I'm having a bear of a time getting HTTP Basic Authentication to work
for a SOAP request via suds.  Also using an HTTP proxy server.

In WireShark I just see a request -
GET http://./services/services/JobService-0.0.1?wsdl HTTP/1.1
Accept-Encoding: identity
Host: ...
Connection: close
User-Agent: Python-urllib/2.7

This doesn't contain any authentication credentials so the response is
HTTP/401, and the client doesn't retry with credentials.  The response
does come from the remote as I can see there is a WWW-Authenticate
header and it is from an Apache Tomcat server - so it makes it through
the proxy server.

Code

 url = 'http://../services/services/JobService-0.0.1?wsdl'
 proxy = urllib2.ProxyHandler({'http': 'http://.:3128'})
 transport = suds.transport.http.HttpAuthenticated()
 transport.urlopener = urllib2.build_opener(proxy)
 client = suds.client.Client(url, transport=transport,
username='**', password='') 


I've tried the above as well as the method described at

  It has the same problem.


Back Trace

Traceback (most recent call last):
  File "etrace.py", line 30, in 
client = suds.client.Client(url, transport=transport,
username='***', password='*')  
  File
"/usr/local/lib/python2.7/site-packages/suds-0.4-py2.7.egg/suds/client.py", 
line 112, in __init__
self.wsdl = reader.open(url)
  File
"/usr/local/lib/python2.7/site-packages/suds-0.4-py2.7.egg/suds/reader.py", 
line 152, in open
d = self.fn(url, self.options)
  File
"/usr/local/lib/python2.7/site-packages/suds-0.4-py2.7.egg/suds/wsdl.py", line 
136, in __init__
d = reader.open(url)
  File
"/usr/local/lib/python2.7/site-packages/suds-0.4-py2.7.egg/suds/reader.py", 
line 79, in open
d = self.download(url)
  File
"/usr/local/lib/python2.7/site-packages/suds-0.4-py2.7.egg/suds/reader.py", 
line 95, in download
fp = self.options.transport.open(Request(url))
  File
"/usr/local/lib/python2.7/site-packages/suds-0.4-py2.7.egg/suds/transport/http.py",
 line 173, in open
return HttpTransport.open(self, request)
  File
"/usr/local/lib/python2.7/site-packages/suds-0.4-py2.7.egg/suds/transport/http.py",
 line 64, in open
raise TransportError(str(e), e.code, e.fp)
suds.transport.TransportError: HTTP Error 401: Unauthorized

-- 
System & Network Administrator [ LPI & NCLA ]

OpenGroupware Developer 
Adam Tauno Williams


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


Re: Python recursive tree, linked list thingy

2012-03-08 Thread Wanderer
On Mar 7, 3:27 pm, Ian Kelly  wrote:
> On Wed, Mar 7, 2012 at 1:03 PM, Ian Kelly  wrote:
> > A set of defective pixels would be the probable choice, since it
> > offers efficient membership testing.
>
> Some actual code, using a recursive generator:
>
> def get_cluster(defective, pixel):
>     yield pixel
>     (row, column) = pixel
>     for adjacent in [(row - 1, column), (row, column - 1),
>                      (row, column + 1), (row + 1, column)]:
>         if adjacent in defective:
>             defective.remove(adjacent)
>             for cluster_pixel in get_cluster(defective, adjacent):
>                 yield cluster_pixel
>
> defective = {(327, 415), (180, 97), (326, 415), (42, 15),
>              (180, 98), (325, 414), (325, 415)}
> clusters = []
>
> while defective:
>     pixel = defective.pop()
>     clusters.append(list(get_cluster(defective, pixel)))
>
> from pprint import pprint
> pprint(clusters)
>
> Cheers,
> Ian

This works for me and I can modify it to look for column defects also.
It also shows I know less about Python then I thought I did. I had to
read up on generators and iterators to understand the code.

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


Re: newb __init__ inheritance

2012-03-08 Thread Maarten
On Thursday, March 8, 2012 4:25:06 PM UTC+1, hyperboogie wrote:

> My question is if __init__ in the descendant class overrides __init__
> in the parent class how can I call the parent's __init__ from the
> descendant class - I just overrode it didn't I?
> 
> Am I missing something more fundamental here?

No, you're not. 

However, you can explicitly call the __init__() method of a particular class. 
Hard-coding the class gives you:

class A(object):
def __init__(self):
print("In A")

class B(A):
def __init__(self):
A.__init__(self)
print("In B")

Alternatively you can figure out the parent class with a call to super:

class C(A):
def __init__(self):
super(self.__class__, self).__init__()
print("In C")

a = A() (prints "In A")
b = B() (prints "In A", "In B" on two lines)
c = C() (prints "In A", "In C" on two lines)

Hope this helps.

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


Re: newb __init__ inheritance

2012-03-08 Thread Peter Otten
Maarten wrote:

> Alternatively you can figure out the parent class with a call to super:

This is WRONG:

> super(self.__class__, self).__init__()

You have to name the current class explicitly. Consider:

>> class A(object):
... def __init__(self):
... print "in a"
... 
>>> class B(A):
... def __init__(self):
... print "in b"
... super(self.__class__, self).__init__() # wrong
... 
>>> class C(B): pass
... 
>>> 

Can you figure out what C() will print? Try it out if you can't.
The corrected code:

>>> class B(A):
... def __init__(self):
... print "in b"
... super(B, self).__init__()
... 
>>> class C(B): pass
... 
>>> C()
in b
in a
<__main__.C object at 0x7fcfafd52b10>

In Python 3 you can call super() with no args; super().__init__() do the 
right thing there.

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


Re: newb __init__ inheritance

2012-03-08 Thread Ethan Furman

hyperboogie wrote:

Hello everyone.

This is my first post in this group.
I started learning python a week ago from the "dive into python" e-
book and thus far all was clear.
However today while reading chapter 5 about objects and object
orientation I ran into something that confused me.
it says here:
http://www.diveintopython.net/object_oriented_framework/defining_classes.html#fileinfo.class.example

"__init__ methods are optional, but when you define one, you must
remember to explicitly call the ancestor's __init__ method (if it
defines one). This is more generally true: whenever a descendant wants
to extend the behavior of the ancestor, the descendant method must
explicitly call the ancestor method at the proper time, with the
proper arguments. "

However later on in the chapter:
http://www.diveintopython.net/object_oriented_framework/userdict.html

it says:
"Methods are defined solely by their name, and there can be only one
method per class with a given name. So if a descendant class has an
__init__ method, it always overrides the ancestor __init__ method,
even if the descendant defines it with a different argument list. And
the same rule applies to any other method. "

My question is if __init__ in the descendant class overrides __init__
in the parent class how can I call the parent's __init__ from the
descendant class - I just overrode it didn't I?

Am I missing something more fundamental here?
Thanks


An excellent question.

What you subclass you are creating a new, different class.

class A(object):
def __init__(self):
print("this is class A's __init__")
def method1(self, value):
print(value)

class B(A):
def __init__(self):
print("this is class B's __init__")

test = B()
test.method1('42')

When it says that the subclass overrides methods of the same name, it 
means that if it finds the method in the subclass, it will stop looking 
and use the one it found.


So in the example above when Python creates test it will find __init__ 
in B and so won't bother looking in A for it.  However, when looking for 
'method1' Python does not find it in B, and so looks in A for it and, 
finding it there, uses that as B's method1.


If you want B's __init__ to also call A's __init__, you have to so 
explicity:


def __init__(self):
A.__init__(self)

or

def __init__(self):
super(B, self).__init__()

or with Python 3

def __init__(self):
super().__init__()

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


Re: GUI components in python

2012-03-08 Thread Adam Tauno Williams
On Wed, 2012-03-07 at 19:14 +0530, janaki rajamani wrote:
> I am stuck with the brain workshop software implemented using python.
> The code involves a lot of GUI elements and i am familar only with the
> basic python programming.
> I would like to know whether there are built in classes to support GUI
> elements or arethey project dependant.

There are wrappers for various GUI toolkits/technologies such as PyGTK
for using Gtk in Python.  These wrappers often provide quite a bit of
assistance and tools for working with that technology.

You need to ask more specifically to get a more useful answer?

-- 
System & Network Administrator [ LPI & NCLA ]

OpenGroupware Developer 
Adam Tauno Williams


signature.asc
Description: This is a digitally signed message part
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: GUI components in python

2012-03-08 Thread Prasad, Ramit
> > I am stuck with the brain workshop software implemented using python.
> > The code involves a lot of GUI elements and i am familar only with the
> > basic python programming.
> > I would like to know whether there are built in classes to support GUI
> > elements or arethey project dependant.
> 
> There are wrappers for various GUI toolkits/technologies such as PyGTK
> for using Gtk in Python.  These wrappers often provide quite a bit of
> assistance and tools for working with that technology.
> 
> You need to ask more specifically to get a more useful answer?

The TKinter library ships with Python.  So I guess that would be the
"built-in" classes. Usually though the UI is project dependent and
there are several frameworks depending on the features you want.

This may help list some of the frameworks and help you choose: 
http://www.awaretek.com/toolkits.html 


Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Buffering in Wing and IDLE 3

2012-03-08 Thread Franck Ditter
In article ,
 Ned Deily  wrote:

> http://www.activestate.com/activetcl/downloads

GREAT ! It seems to work.
At least, I can now get the ~ char in France from within IDLE.
A big step for manking :-)
Thanks folks,

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


Re: What's the best way to write this regular expression?

2012-03-08 Thread John Salerno
Alright, I'm simply lost about how to install these modules. I
extracted the folders from the .tar.gz files and then went into those
folders in my command prompt. I typed:

C:\Python32\python setup.py install

and for a while something was happening (I was doing the lxml one) and
then it stopped with an error that it couldn't find a file. So I have
no idea.

Next I installed the "distribute" module, which seemed to install
okay. But now I don't understand how to use easy_install. Where do I
call it from? What do I do with the .tar.gz files at this point? The
instructions for lxml say to run this command:

easy_install --allow-hosts=lxml.de,*.python.org lxml

but WHERE do I run it? I tried it in the Python directory, and then
further in the lxml site-packages directory, but it doesn't work. What
do I do with it? Where do I put the .tar files?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What's the best way to write this regular expression?

2012-03-08 Thread John Salerno
On Mar 8, 3:33 pm, John Salerno  wrote:
> Alright, I'm simply lost about how to install these modules. I
> extracted the folders from the .tar.gz files and then went into those
> folders in my command prompt. I typed:
>
> C:\Python32\python setup.py install
>
> and for a while something was happening (I was doing the lxml one) and
> then it stopped with an error that it couldn't find a file. So I have
> no idea.
>
> Next I installed the "distribute" module, which seemed to install
> okay. But now I don't understand how to use easy_install. Where do I
> call it from? What do I do with the .tar.gz files at this point? The
> instructions for lxml say to run this command:
>
> easy_install --allow-hosts=lxml.de,*.python.org lxml
>
> but WHERE do I run it? I tried it in the Python directory, and then
> further in the lxml site-packages directory, but it doesn't work. What
> do I do with it? Where do I put the .tar files?

Well, after a bit of experimentation, I got it to run, but I seem to
have run into the same error as when I used setup.py:

http://i271.photobucket.com/albums/jj138/JohnJSal/lxml_error.png

Now I have no idea what to do.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What's the best way to write this regular expression?

2012-03-08 Thread John Gordon
In <21519dbf-4097-4780-874d-41d76f645...@x17g2000yqj.googlegroups.com> John 
Salerno  writes:

> Well, after a bit of experimentation, I got it to run, but I seem to
> have run into the same error as when I used setup.py:

> http://i271.photobucket.com/albums/jj138/JohnJSal/lxml_error.png

> Now I have no idea what to do.

The first error on that screen is that "xslt-config" is not found
as a command.

Try a web search for "xslt-config not found", there seemed to be some
helpful results.

-- 
John Gordon   A is for Amy, who fell down the stairs
gor...@panix.com  B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

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


Re: What's the best way to write this regular expression?

2012-03-08 Thread John Salerno
On Mar 8, 3:40 pm, John Salerno  wrote:

> Now I have no idea what to do.

Hmph, I suppose I should have more patience. I realized that the
easy_install for lxml only tried to install a binary version, which
doesn't exist for the version it found (the latest, 2.3.3). I just had
to look through the previous versions and find the one with a binary
installation for Windows (2.3) and it was as simple as a single click
to install!

And the easy_install method did work for Beautiful Soup, so I should
be all set now!
-- 
http://mail.python.org/mailman/listinfo/python-list


Finding MIME type for a data stream

2012-03-08 Thread Tobiah
I'm pulling image data from a database blob, and serving
it from a web2py app.  I have to send the correct
Content-Type header, so I need to detect the image type.

Everything that I've found on the web so far, needs a file
name on the disk, but I only have the data.

It looks like the 'magic' package might be of use, but
I can't find any documentation for it.

Also, it seems like image/png works for other types 
of image data, while image/foo does not, yet I'm afraid
that not every browser will play along as nicely.

Thanks!

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


Re: Finding MIME type for a data stream

2012-03-08 Thread Dave Angel

On 03/08/2012 04:55 PM, Tobiah wrote:

I'm pulling image data from a database blob, and serving
it from a web2py app.  I have to send the correct
Content-Type header, so I need to detect the image type.

Everything that I've found on the web so far, needs a file
name on the disk, but I only have the data.

It looks like the 'magic' package might be of use, but
I can't find any documentation for it.

Also, it seems like image/png works for other types
of image data, while image/foo does not, yet I'm afraid
that not every browser will play along as nicely.

Thanks!

Tobiah


First step, ask the authors of the database what format of data this 
blob is in.


Failing that, write the same data locally as a binary file, and see what 
application can open it.  Or if you're on a Linux system, run file on 
it.  "file" can identify most data formats (not just images) just by 
looking at the data.


That assumes, of course, that there's any consistency in the data coming 
out of the database.  What happens if next time this blob is an Excel 
spreadsheet?


--

DaveA

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


Re: What's the best way to write this regular expression?

2012-03-08 Thread Dave Angel

On 03/08/2012 04:40 PM, John Salerno wrote:


http://i271.photobucket.com/albums/jj138/JohnJSal/lxml_error.png



Nothing to do with Python, but you'd save us all a lot of space and 
bandwidth if you learned how to copy/paste from a Windows cmd window.


If you're just doing it rarely, you can right click on the top bar to 
get a menu.  I think you want "mark".  Then you select the text you'd 
like to put in the clipboard.


Alternatively, you can put the console in quick-edit mode (I think it's 
called, it's been a long time since I ran Windows).  That's an option 
you set on one cmd window, and it sticks for future windows.


In quick-edit, you just right-click-drag on the cmd window to select a 
rectangle of text.  Then you can Ctrl-V to paste it into email, or into 
a text editor, or wherever else you need it.  Hard to imagine not using 
this mode, or its Linux equivalent, which is always available.


If that wasn't clear enough, or it doesn't work for you, somebody will 
explain it better.  Or ask me, and I'll launch a VirtualBox with Windows 
to get you going.


--

DaveA

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


Re: "Decoding unicode is not supported" in unusual situation

2012-03-08 Thread John Nagle

On 3/7/2012 6:18 PM, Ben Finney wrote:

Steven D'Aprano  writes:


On Thu, 08 Mar 2012 08:48:58 +1100, Ben Finney wrote:

I think that's a Python bug. If the latter succeeds as a no-op, the
former should also succeed as a no-op. Neither should ever get any
errors when ‘s’ is a ‘unicode’ object already.


No. The semantics of the unicode function (technically: a type
constructor) are well-defined, and there are two distinct behaviours:


   Right. The real problem is that Python 2.7 doesn't have distinct
"str" and "bytes" types.  type(bytes() returns 
"str" is assumed to be ASCII 0..127, but that's not enforced.
"bytes" and "str" should have been distinct types, but
that would have broken much old code.  If they were distinct, then
constructors could distinguish between string type conversion
(which requires no encoding information) and byte stream decoding.

   So it's possible to get junk characters in a "str", and they
won't convert to Unicode.  I've had this happen with databases which
were supposed to be ASCII, but occasionally a non-ASCII character
would slip through.

   This is all different in Python 3.x, where "str" is Unicode and
"bytes" really are a distinct type.

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


Re: What's the best way to write this regular expression?

2012-03-08 Thread John Salerno
Thanks, I had no idea about either option, since I don't use the
command prompt very much. Needless to say, the Linux console is much
nicer :)



On Thu, Mar 8, 2012 at 4:19 PM, Dave Angel  wrote:
> On 03/08/2012 04:40 PM, John Salerno wrote:
>>
>> 
>> http://i271.photobucket.com/albums/jj138/JohnJSal/lxml_error.png
>>
>
> Nothing to do with Python, but you'd save us all a lot of space and
> bandwidth if you learned how to copy/paste from a Windows cmd window.
>
> If you're just doing it rarely, you can right click on the top bar to get a
> menu.  I think you want "mark".  Then you select the text you'd like to put
> in the clipboard.
>
> Alternatively, you can put the console in quick-edit mode (I think it's
> called, it's been a long time since I ran Windows).  That's an option you
> set on one cmd window, and it sticks for future windows.
>
> In quick-edit, you just right-click-drag on the cmd window to select a
> rectangle of text.  Then you can Ctrl-V to paste it into email, or into a
> text editor, or wherever else you need it.  Hard to imagine not using this
> mode, or its Linux equivalent, which is always available.
>
> If that wasn't clear enough, or it doesn't work for you, somebody will
> explain it better.  Or ask me, and I'll launch a VirtualBox with Windows to
> get you going.
>
> --
>
> DaveA
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Finding MIME type for a data stream

2012-03-08 Thread Tobiah
On 03/08/2012 02:11 PM, Dave Angel wrote:
> On 03/08/2012 04:55 PM, Tobiah wrote:
>> I'm pulling image data from a database blob, and serving
>> it from a web2py app.  I have to send the correct
>> Content-Type header, so I need to detect the image type.
>>
>> Everything that I've found on the web so far, needs a file
>> name on the disk, but I only have the data.
>>
>> It looks like the 'magic' package might be of use, but
>> I can't find any documentation for it.
>>
>> Also, it seems like image/png works for other types
>> of image data, while image/foo does not, yet I'm afraid
>> that not every browser will play along as nicely.
>>
>> Thanks!
>>
>> Tobiah
> 
> First step, ask the authors of the database what format of data this 
> blob is in.
> 
> Failing that, write the same data locally as a binary file, and see what 
> application can open it.  Or if you're on a Linux system, run file on 
> it.  "file" can identify most data formats (not just images) just by 
> looking at the data.
> 
> That assumes, of course, that there's any consistency in the data coming 
> out of the database.  What happens if next time this blob is an Excel 
> spreadsheet?
> 


I should simplify my question.  Let's say I have a string
that contains image data called 'mystring'.

I want to do

mime_type = some_magic(mystring)

and get back 'image/jpg' or 'image/png' or whatever is
appropriate for the image data.

Thanks!

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


Re: Finding MIME type for a data stream

2012-03-08 Thread Tobiah
Also, I realize that I could write the data to a file
and then use one of the modules that want a file path.
I would prefer not to do that.

Thanks

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


Re: Finding MIME type for a data stream

2012-03-08 Thread Dave Angel

On 03/08/2012 05:28 PM, Tobiah wrote:




I should simplify my question.  Let's say I have a string
that contains image data called 'mystring'.

I want to do

mime_type = some_magic(mystring)

and get back 'image/jpg' or 'image/png' or whatever is
appropriate for the image data.

Thanks!

Tobiah


I have to assume you're talking python 2, since in python 3, strings 
cannot generally contain image data.  In python 2, characters are pretty 
much interchangeable with bytes.


Anyway, I don't know any way in the standard lib to distinguish 
arbitrary image formats.  (There very well could be one.)  The file 
program I referred to was an external utility, which you could run with 
the multiprocessing module.


if you're looking for a specific, small list of file formats, you could 
make yourself a signature list.  Most (not all) formats distinguish 
themselves in the first few bytes.  For example, a standard zip file 
starts with "PK" for Phil Katz.  A Windows  exe starts with "MZ" for 
Mark Zbikowsky.  And I believe a jpeg file starts  hex(d8) (ff) (e0) (ff)


If you'd like to see a list of available modules, help() is your 
friend.  You can start with help("modules") to see quite a long list.  
And I was surprised how many image related things already are there.  So 
maybe there's something I don't know about that could help.


--

DaveA

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


RE: "Decoding unicode is not supported" in unusual situation

2012-03-08 Thread Prasad, Ramit
> Right. The real problem is that Python 2.7 doesn't have distinct
> "str" and "bytes" types.  type(bytes() returns 
> "str" is assumed to be ASCII 0..127, but that's not enforced.
> "bytes" and "str" should have been distinct types, but
> that would have broken much old code.  If they were distinct, then
> constructors could distinguish between string type conversion
> (which requires no encoding information) and byte stream decoding.
> 
> So it's possible to get junk characters in a "str", and they
> won't convert to Unicode.  I've had this happen with databases which
> were supposed to be ASCII, but occasionally a non-ASCII character
> would slip through.

bytes and str are just aliases for each other. 

>>> id( bytes )
505366496
>>> id( str )
505366496
>>> type( bytes )

>>> type( str )

>>> bytes == str 
True
>>> bytes is str
True


And I do not think they were ever intended to be just 
ASCII because chr() takes 0 - 256 (non-inclusive) and 
returns a str.


Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--


> -Original Message-
> From: python-list-bounces+ramit.prasad=jpmorgan@python.org
> [mailto:python-list-bounces+ramit.prasad=jpmorgan@python.org] On Behalf
> Of John Nagle
> Sent: Thursday, March 08, 2012 4:24 PM
> To: python-list@python.org
> Subject: Re: "Decoding unicode is not supported" in unusual situation
> 
> On 3/7/2012 6:18 PM, Ben Finney wrote:
> > Steven D'Aprano  writes:
> >
> >> On Thu, 08 Mar 2012 08:48:58 +1100, Ben Finney wrote:
> >>> I think that's a Python bug. If the latter succeeds as a no-op, the
> >>> former should also succeed as a no-op. Neither should ever get any
> >>> errors when ‘s’ is a ‘unicode’ object already.
> >>
> >> No. The semantics of the unicode function (technically: a type
> >> constructor) are well-defined, and there are two distinct behaviours:
> 
> 
> This is all different in Python 3.x, where "str" is Unicode and
> "bytes" really are a distinct type.
> 
>   John Nagle
> --
> http://mail.python.org/mailman/listinfo/python-list

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Can't get around HTTP/401 response using SUDS

2012-03-08 Thread Rafael Durán Castañeda

El 08/03/12 16:44, Adam Tauno Williams escribió:

SUDS version 0.4 pn x86_64 Python 2.7

I'm having a bear of a time getting HTTP Basic Authentication to work
for a SOAP request via suds.  Also using an HTTP proxy server.

In WireShark I just see a request -
GET http://./services/services/JobService-0.0.1?wsdl HTTP/1.1
Accept-Encoding: identity
Host: ...
Connection: close
User-Agent: Python-urllib/2.7

This doesn't contain any authentication credentials so the response is
HTTP/401, and the client doesn't retry with credentials.  The response
does come from the remote as I can see there is a WWW-Authenticate
header and it is from an Apache Tomcat server - so it makes it through
the proxy server.

Code

  url = 'http://../services/services/JobService-0.0.1?wsdl'
  proxy = urllib2.ProxyHandler({'http': 'http://.:3128'})
  transport = suds.transport.http.HttpAuthenticated()
  transport.urlopener = urllib2.build_opener(proxy)
  client = suds.client.Client(url, transport=transport,
username='**', password='')


I've tried the above as well as the method described at

   It has the same problem.


Back Trace

Traceback (most recent call last):
   File "etrace.py", line 30, in
 client = suds.client.Client(url, transport=transport,
username='***', password='*')
   File
"/usr/local/lib/python2.7/site-packages/suds-0.4-py2.7.egg/suds/client.py", 
line 112, in __init__
 self.wsdl = reader.open(url)
   File
"/usr/local/lib/python2.7/site-packages/suds-0.4-py2.7.egg/suds/reader.py", 
line 152, in open
 d = self.fn(url, self.options)
   File
"/usr/local/lib/python2.7/site-packages/suds-0.4-py2.7.egg/suds/wsdl.py", line 
136, in __init__
 d = reader.open(url)
   File
"/usr/local/lib/python2.7/site-packages/suds-0.4-py2.7.egg/suds/reader.py", 
line 79, in open
 d = self.download(url)
   File
"/usr/local/lib/python2.7/site-packages/suds-0.4-py2.7.egg/suds/reader.py", 
line 95, in download
 fp = self.options.transport.open(Request(url))
   File
"/usr/local/lib/python2.7/site-packages/suds-0.4-py2.7.egg/suds/transport/http.py",
 line 173, in open
 return HttpTransport.open(self, request)
   File
"/usr/local/lib/python2.7/site-packages/suds-0.4-py2.7.egg/suds/transport/http.py",
 line 64, in open
 raise TransportError(str(e), e.code, e.fp)
suds.transport.TransportError: HTTP Error 401: Unauthorized

When I've got issues like yours, I usually try using a console client 
(telnet/curl/wget,...) adding all needed headers/data manually so I'm 
totally sure the request is OK, if everything goes Ok you know your 
python code need to be reviewed but when you are under proxies you can 
find a lot of not python related issues (bad gateways, using proxy when 
it shouldn't or vice-versa, requests changed by proxy,).


wireshark can be very useful but in situations like this I usually 
prefer tcpflow output, I think the request/response flow showed is 
really useful, but that's a personal preference.


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


RE: What's the best way to write this regular expression?

2012-03-08 Thread Prasad, Ramit
> > Alternatively, you can put the console in quick-edit mode (I think it's
> > called, it's been a long time since I ran Windows).  That's an option you
> > set on one cmd window, and it sticks for future windows.
> >
> > In quick-edit, you just right-click-drag on the cmd window to select a
> > rectangle of text.  Then you can Ctrl-V to paste it into email, or into a
> > text editor, or wherever else you need it.  Hard to imagine not using
> this
> > mode, or its Linux equivalent, which is always available.

Actually in quick-edit mode (XP and higher) you just select with
left click and then hit enter which copies it to the clipboard.
If you also enable insert mode (not sure if this is Win7 specific)
you can even right click to paste into the console, just like
Linux.

> Needless to say, the Linux console is much nicer :)
True. 

Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What's the best way to write this regular expression?

2012-03-08 Thread Dave Angel

On 03/08/2012 06:02 PM, Prasad, Ramit wrote:

Actually in quick-edit mode (XP and higher) you just select with
left click and then hit enter which copies it to the clipboard.
If you also enable insert mode (not sure if this is Win7 specific)
you can even right click to paste into the console, just like
Linux.


Needless to say, the Linux console is much nicer :)

True.




I was confusing the left-mouse-drag and the right-click.  You are 
correct about both the copying and the pasting, and they do both work 
from XP onwards.


As I said, I haven't used Windows much in quite a while.

--

DaveA

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


Re: What's the best way to write this regular expression?

2012-03-08 Thread Ethan Furman

Dave Angel wrote:

On 03/08/2012 04:40 PM, John Salerno wrote:


http://i271.photobucket.com/albums/jj138/JohnJSal/lxml_error.png



Nothing to do with Python, but you'd save us all a lot of space and 
bandwidth if you learned how to copy/paste from a Windows cmd window.


On Windows XP it is:

Mouse
-
Right-click on title bar
Left-click on Edit
Left-click on Mark
Right-click and drag to select desired text
 to copy text to clipboard

Keyboard

-
e
k or 
arrows to move cursor,  to select text
 to copy text to clipboard

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


Re: Finding MIME type for a data stream

2012-03-08 Thread Tobiah

> I have to assume you're talking python 2, since in python 3, strings 
> cannot generally contain image data.  In python 2, characters are pretty 
> much interchangeable with bytes.

Yeah, python 2


> if you're looking for a specific, small list of file formats, you could 
> make yourself a signature list.  Most (not all) formats distinguish 
> themselves in the first few bytes. 

Yeah, maybe I'll just do that.  I'm alowing users to paste
images into a rich-text editor, so I'm pretty much looking 
at .png, .gif, or .jpg.  Those should be pretty easy to 
distinguish by looking at the first few bytes.  

Pasting images may sound weird, but I'm using a jquery
widget called cleditor that takes image data from the
clipboard and replaces it with inline base64 data.  
The html from the editor ends up as an email, and the
inline images cause the emails to be tossed in the
spam folder for most people.  So I'm parsing the
emails, storing the image data, and replacing the
inline images with an img tag that points to a 
web2py app that takes arguments that tell it which 
image to pull from the database.  

Now that I think of it, I could use php to detect the
image type, and store that in the database.  Not quite
as clean, but that would work.

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


Re: Finding MIME type for a data stream

2012-03-08 Thread Irmen de Jong
On 8-3-2012 23:34, Tobiah wrote:
> Also, I realize that I could write the data to a file
> and then use one of the modules that want a file path.
> I would prefer not to do that.
> 
> Thanks
> 

Use StringIO then, instead of a file on disk

Irmen

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


Re: Finding MIME type for a data stream

2012-03-08 Thread Jon Clements
On Thursday, 8 March 2012 23:40:13 UTC, Tobiah  wrote:
> > I have to assume you're talking python 2, since in python 3, strings 
> > cannot generally contain image data.  In python 2, characters are pretty 
> > much interchangeable with bytes.
> 
> Yeah, python 2
> 
> 
> > if you're looking for a specific, small list of file formats, you could 
> > make yourself a signature list.  Most (not all) formats distinguish 
> > themselves in the first few bytes. 
> 
> Yeah, maybe I'll just do that.  I'm alowing users to paste
> images into a rich-text editor, so I'm pretty much looking 
> at .png, .gif, or .jpg.  Those should be pretty easy to 
> distinguish by looking at the first few bytes.  
> 
> Pasting images may sound weird, but I'm using a jquery
> widget called cleditor that takes image data from the
> clipboard and replaces it with inline base64 data.  
> The html from the editor ends up as an email, and the
> inline images cause the emails to be tossed in the
> spam folder for most people.  So I'm parsing the
> emails, storing the image data, and replacing the
> inline images with an img tag that points to a 
> web2py app that takes arguments that tell it which 
> image to pull from the database.  
> 
> Now that I think of it, I could use php to detect the
> image type, and store that in the database.  Not quite
> as clean, but that would work.
> 
> Tobiah

Something like the following might be worth a go:
(untested)

from PIL import Image
img = Image.open(StringIO(blob))
print img.format

HTH
Jon.

PIL: http://www.pythonware.com/library/pil/handbook/image.htm
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What's the best way to write this regular expression?

2012-03-08 Thread alex23
John Salerno  wrote:
> So much work just to get a 3rd party module installed!

"New! Try out the beta release of Beautiful Soup 4. (Last updated
February 28, 2012)
easy_install beautifulsoup4 or pip install beautifulsoup4 or download
a tarball."

http://www.crummy.com/software/BeautifulSoup/

Worked fine under both Python 2.7 & 3.2 using pip.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What's the best way to write this regular expression?

2012-03-08 Thread John Salerno
On Thursday, March 8, 2012 9:38:51 PM UTC-6, alex23 wrote:
> John Salerno  wrote:
> > So much work just to get a 3rd party module installed!
> 
> "New! Try out the beta release of Beautiful Soup 4. (Last updated
> February 28, 2012)
> easy_install beautifulsoup4 or pip install beautifulsoup4 or download
> a tarball."
> 
> http://www.crummy.com/software/BeautifulSoup/
> 
> Worked fine under both Python 2.7 & 3.2 using pip.

Yeah, but first I had to figure out how to install easy_install :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: GUI components in python

2012-03-08 Thread hackingKK

Hi Janaki,
Python will mostly use either the pygtk library or pyqt library for any 
rich and production quality GUI.

So you must try figuring out if either of these 2 libraries are used.
Other than this, there is tkinter which i guess comes with Python as 
default.

Happy hacking.
Krishnakant.


On 07/03/12 19:14, janaki rajamani wrote:

Hi

I am stuck with the brain workshop software implemented using python.
The code involves a lot of GUI elements and i am familar only with the
basic python programming.
I would like to know whether there are built in classes to support GUI
elements or arethey project dependant.



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


how to get plots made faster

2012-03-08 Thread amar Singh
The following is the part of my code which is running faster locally
and more slowly remotely via ssh on the same machine. Note I am trying
to generate a multi-page report.


## create plots and write to a pdf file
from scipy import *
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages

# open a multi-page pdf file
pp = PdfPages('history_plot.pdf')

F=loadtxt('hist.dat',comments='%')


t=F[:,0]
E=F[:,13]

plt.plot(t,E)

h1=plt.ylabel('Energy', fontsize=16)
h1=plt.xlabel('Time', fontsize=16)
pp.savefig()

plt.clf()
VdotB=F[:,14]
plt.plot(t,VdotB)

pp.savefig()
pp.close()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to get plots made faster

2012-03-08 Thread Mark Lawrence

On 09/03/2012 04:40, amar Singh wrote:

The following is the part of my code which is running faster locally
and more slowly remotely via ssh on the same machine. Note I am trying
to generate a multi-page report.


## create plots and write to a pdf file
from scipy import *
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages

# open a multi-page pdf file
pp = PdfPages('history_plot.pdf')

F=loadtxt('hist.dat',comments='%')


t=F[:,0]
E=F[:,13]

plt.plot(t,E)

h1=plt.ylabel('Energy', fontsize=16)
h1=plt.xlabel('Time', fontsize=16)
pp.savefig()

plt.clf()
VdotB=F[:,14]
plt.plot(t,VdotB)

pp.savefig()
pp.close()


I can't help directly but you may be better off asking on the matplotlib 
users mailing list see 
https://lists.sourceforge.net/lists/listinfo/matplotlib-users


--
Cheers.

Mark Lawrence.

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


Re: GUI components in python

2012-03-08 Thread Michael Torrie
On 03/07/2012 06:44 AM, janaki rajamani wrote:
> Hi
> 
> I am stuck with the brain workshop software implemented using python.
> The code involves a lot of GUI elements and i am familar only with the
> basic python programming.
> I would like to know whether there are built in classes to support GUI
> elements or arethey project dependant.

brain workshop appears to use the pyglet library:

http://www.pyglet.org/

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