Re: Do I have to use threads?

2010-01-07 Thread Marco Salden
On Jan 6, 5:36 am, Philip Semanchuk  wrote:
> On Jan 5, 2010, at 11:26 PM, aditya shukla wrote:
>
> > Hello people,
>
> > I have 5 directories corresponding 5  different urls .I want to  
> > download
> > images from those urls and place them in the respective  
> > directories.I have
> > to extract the contents and download them simultaneously.I can  
> > extract the
> > contents and do then one by one. My questions is for doing it  
> > simultaneously
> > do I have to use threads?
>
> No. You could spawn 5 copies of wget (or curl or a Python program that  
> you've written). Whether or not that will perform better or be easier  
> to code, debug and maintain depends on the other aspects of your  
> program(s).
>
> bye
> Philip

Yep, the more easier and straightforward the approach, the better:
threads are always (programmers')-error-prone by nature.
But my question would be: does it REALLY need to be simultaneously:
the CPU/OS only has more overhead doing this in parallel with
processess. Measuring sequential processing and then trying to
optimize (e.g. for user response or whatever) would be my prefered way
to go. Less=More.

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


buffer interface problem

2010-01-07 Thread Andrew Gillanders
I have run into a problem running a Python script that is part of the  
TerraGear suite for building scenery for FlightGear. I am using Mac  
OS X 10.4, running Python (version 3.0.1) in a Unix terminal.


The purpose of the script is to walk a directory tree, unzipping  
files, and passing the contents to an executable C programme. The  
problem occurs here:


gzin = GzipFile(fname, 'rb')
data = gzin.readline()
min_x,min_y = map(atoi,data.split()[:2])

The input file, when uncompressed, is an ASCII file with a line with  
two numbers, then a line of four numbers, then many long lines of  
numbers. I can see what the last is trying to do: split the string  
into two words, convert them to integers, and assign them to min_x  
and min_y.


At the third line, I get the message "expected an object with the  
buffer interface". Which object is it referring to? Have some  
functions been changed to pass buffer objects instead of strings? How  
can I fix the source code to make it run?


Any help appreciated
Andrew
(Python newbie)

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


RE: How to reduce the memory size of python

2010-01-07 Thread Mishra Gopal-QBX634
 
Hi,

I use twisted framework too to handle the xmlrpc request. It takes
around 3-4MB of memory while importing itself.
Is there any python coding standard I should follow to save the memory.

Like import logging takes 1MB of memory.
We only use on function getLogger by 'from logging import getLogger'

But it still take the same 1 MB memory.

Instead of loading whole logging module only load the getLogger
function.

I there any way to save the memory with taking care of small things in
code..

Thanks,
Gopal





-Original Message-
From: [email protected]
[mailto:[email protected]] On Behalf Of
Steve Holden
Sent: Thursday, January 07, 2010 1:20 PM
To: [email protected]
Subject: Re: How to reduce the memory size of python

Mishra Gopal-QBX634 wrote:
> Hi,
>  
> When i write following pyhon program and execute it in linux machine,
>  
> if __name__=='__main__':
> while True:
>   pass
>  
> When i check the VmRSS size, it shows 2956 KB in size.
>  
> Is there any way to reduce the memory size taken by python.
>  
> I am working in flash memory devices.
>  
> Any suggession is highly helpfull to me.
>  
It would not be easy to reduce the size of the standard interpreter, but
there are various implementations for embedded devices. You may get some
help from

  http://wiki.python.org/moin/Tiny%20Python

and

  http://wiki.python.org/moin/Tiny%20Python

A company called Synapse has a working cut-down Python implementation
that they embed in their 802.15 wireless mesh devices, which IIRC
occupies less than 128K.

There is a lot in the standard interpreter that you won't need - many
embedded systems don't need floating point arithmetic, for example.

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010  http://us.pycon.org/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS:http://holdenweb.eventbrite.com/

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


Re: buffer interface problem

2010-01-07 Thread Chris Rebert
On Thu, Jan 7, 2010 at 12:19 AM, Andrew Gillanders
 wrote:
> I have run into a problem running a Python script that is part of the
> TerraGear suite for building scenery for FlightGear. I am using Mac OS X
> 10.4, running Python (version 3.0.1) in a Unix terminal.
>
> The purpose of the script is to walk a directory tree, unzipping files, and
> passing the contents to an executable C program. The problem occurs here:
>
>    gzin = GzipFile(fname, 'rb')
>    data = gzin.readline()
>    min_x,min_y = map(atoi,data.split()[:2])
>
> The input file, when uncompressed, is an ASCII file with a line with two
> numbers, then a line of four numbers, then many long lines of numbers. I can
> see what the last is trying to do: split the string into two words, convert
> them to integers, and assign them to min_x and min_y.
>
> At the third line, I get the message "expected an object with the buffer
> interface". Which object is it referring to?

The elements of the list produced by `data.split()[:2]`, which are
either Unicode strings or bytestrings, neither of which are buffers.

> Have some functions been
> changed to pass buffer objects instead of strings? How can I fix the source
> code to make it run?

The error is being raised by the atoi() function (in the future,
please post the full Traceback, not just the final error message).
What module/library does your atoi() function come from (look for an
`import` statement mentioning it)?
The only functions by that name in the Python standard library both
operate on strings, not buffers, and thus can't be the same one your
code is using.

In any case, replacing `atoi` with `int` in your code will likely
solve the problem. The built-in int() function* can convert strings to
integers.

Cheers,
Chris
--
http://blog.rebertia.com

*Not really a function, but close enough for newbie explanatory purposes.
-- 
http://mail.python.org/mailman/listinfo/python-list


How to execute a script from another script and other script does not do busy wait.

2010-01-07 Thread Rajat
I want to run a python script( aka script2) from another python script
(aka script1). While script1 executes script2 it waits for script2 to
complete and in doing so it also does some other useful work.(does not
do a busy wait).

My intention is to update a third party through script1 that script2
is going to take longer.

Please suggest how should I go about implementing it.

I'm currently executing it as:

import main from script2
ret_code  = main()
return ret_code

which surely is not going to achieve me what I intend.


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


RE: How to execute a script from another script and other script does notdo busy wait.

2010-01-07 Thread VYAS ASHISH M-NTB837
 
Use threads

Regards,
Ashish Vyas

-Original Message-
From: [email protected]
[mailto:[email protected]] On Behalf Of
Rajat
Sent: Thursday, January 07, 2010 2:42 PM
To: [email protected]
Subject: How to execute a script from another script and other script
does notdo busy wait.

I want to run a python script( aka script2) from another python script
(aka script1). While script1 executes script2 it waits for script2 to
complete and in doing so it also does some other useful work.(does not
do a busy wait).

My intention is to update a third party through script1 that script2 is
going to take longer.

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


Re: Astronomy--Programs to Compute Siderial Time?

2010-01-07 Thread John Machin
On Jan 7, 2:40 pm, "W. eWatson"  wrote:
> John Machin wrote:

>
> > What you have been reading is the "Internal maintenance
> > specification" (large font, near the top of the page) for the module.
> > The xml file is the source of the docs, not meant to be user-legible.
>
> What is it used for?

The maintainer of the module processes the xml file with some script
or other to create the user-legible docs.

> Do I need it?

No.

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


Re: Python books, literature etc

2010-01-07 Thread Stuart Murray-Smith
2010/1/6 J :
> A good point was brought up to me privately, and I agree completely,
> that the OP should re-state the request with a bit more specifics...
>
> Since the OP says he is at least familiar with Python, does he need
> info on beginner level books that are general purpose, or is he
> interested in resources that are more specific (e.g. geared toward web
> programming, mathematical analysis, data modeling, etc)
>
> My suggestions were meant just as an example of what I use in the
> course of learning something at the basic and intermediate level, once
> it goes beyond that, it's useful to know WHAT you intend to do so you
> can find the right resources to go in that direction.
>
> Just want to make sure I am not misunderstood or anything :-)
>
> For what it's worth, I also tend to collect technical books for some
> reason... My wife is just barely tolerant of my bookshelf full of
> things on various computer topics, astronomy, photography, radio and
> antenna theory and so forth ;-)  I just let her keep her shoe
> collection, and we have a quid pro quo.

Thanks J for your reply, much appreciated :)

Oops, vague OP, my bad.

Agreed, Google turns up myriad of topical books, and ESR's guide to
smart questions [1] helps set the pace of list culture. I subscribe to
various list servers, on one of them we gracefully accept that a
question like my OP is looking for opinion on a matter, which I now
understand would be a list's sub-culture?

Anyways, to rephrase, could someone kindly mention any of their
preferred Python books, websites, tutorials etc to help me get to an
intermediate/advanced level? Something that would help me add
functionality to Ubiquity, say.

Have a great day!

Stu@

[1] http://catb.org/~esr/faqs/smart-questions.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: File transfer with python

2010-01-07 Thread Shawn Milochik
Have a look at Paramiko. It lets you do secure transfers easily (scp/sftp)

http://www.lag.net/paramiko/

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


Re: Printing plain text with exact positioning on Windows

2010-01-07 Thread Nobody
On Tue, 05 Jan 2010 11:40:25 -0800, KvS wrote:

>> "Hardcopy" document formats such as PostScript and PDF use positions
>> relative to the edges of the page, not the margins.
> 
> Right. Still, Acrobat Reader by default scales the contents to fit on
> a page and creates some margins by doing so, no? So if my text is
> close to the left and right edges, as I want, it will get scaled and
> extra margins will occur. Avoiding this still requires me to be able
> to turn off this scaling in the printing preferences somehow
> programmatically, so it doesn't seem to make the problem easier?

If the document is the same size as the physical page, it will be
transferred directly without any scaling or offset. The document will not
be scaled to fit the printable area; if the document contains any marks
which lie within the printer's margins, those marks won't appear on the
printed page.

PostScript and PDF documents don't have "margins". There might be an area
around the edge of the page which doesn't contain any marks, but that's
irrelevant; the area is still part of the page.

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


suds problem

2010-01-07 Thread Fencer
Hello, I just started using suds to use web services. First I tried suds 
with a very simple web service I had written and was running myself. 
That worked fine. Then I tried to use the web services provided by KEGG:

http://soap.genome.jp/KEGG.wsdl
But I get a SAXParseException due to a supposed mis-matched tag when I 
try to consume that wsdl with suds. I checked the wsdl in oxygene and it 
claims it's valid. What is the problem here? My test program is below 
and it's very simple:

from suds.client import Client

url = 'http://soap.genome.jp/KEGG.wsdl'

client = Client(url)

print client

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


Re: Python books, literature etc

2010-01-07 Thread Gabriel Genellina
Stuart Murray-Smith  wrote in 
news:[email protected]: 

> Anyways, to rephrase, could someone kindly mention any of their 
> preferred Python books, websites, tutorials etc to help me get 
> to an intermediate/advanced level? Something that would help me 
> add functionality to Ubiquity, say. 

Have a look at the Getting Started section of the wiki: 

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

specially the PythonBooks section
 
-- 
Gabriel Genellina 

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


Re: Python books, literature etc

2010-01-07 Thread Gabriel Genellina
Stuart Murray-Smith  wrote in 
news:[email protected]: 

> Anyways, to rephrase, could someone kindly mention any of their 
> preferred Python books, websites, tutorials etc to help me get 
> to an intermediate/advanced level? Something that would help me 
> add functionality to Ubiquity, say. 

Have a look at the Getting Started section of the wiki: 

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

specially the PythonBooks section
 
-- 
Gabriel Genellina 

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


Call a DLL function with argument type as unsigned char *

2010-01-07 Thread Bhavik
Hello,

I am a newbie to the python language, and I need to call a DLL
function from the python program.
The DLL function has following prototype:

unsigned int DLLFunction(unsigned char *, unsigned int);

Now, I need to declare an array of 6 bytes in the python, and pass
that array as the first argument to the DLL function. I tried some
methods mentioned on the Internet, but it gives error like "can not
convert argument 1".

Can someone tell me how to do this?

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


Re: Call a DLL function with argument type as unsigned char *

2010-01-07 Thread Bhavik
Just to clarify, I am using Python 2.5.1

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


Re: creating tar file and streaming it over HTTP?

2010-01-07 Thread Gabriel Genellina
En Wed, 06 Jan 2010 13:39:08 -0300, pbienst   
escribió:



The problem seems to be that the
receiving end (wsgi server) does not see the end of the data:

socket = environ["wsgi.input"]
while True:
sys.stderr.write("before")
chunk = socket.read(4096)
sys.stderr.write("after")
if not chunk:
sys.stderr.write("done")
break
sys.stderr.write(chunk)

There is data from the tar file being printed, but in the end it hangs
in the 'before' statement, and never gets to 'done'. I tried flushing
the fileobj created by makefile(), but that doesn't seem to help.
There is also no environ["CONTENT_LENGTH"] that I can use to detect
the end of the stream.

I'm probably missing something basic here...


Either send a "Connection: Close" header (and close the connection at the  
end), or a "Transfer-Encoding: chunked" header (see  
http://en.wikipedia.org/wiki/Chunked_transfer_encoding )


--
Gabriel Genellina

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


Re: How to execute a script from another script and other script does notdo busy wait.

2010-01-07 Thread Rajat
On Jan 7, 2:21 pm, "VYAS ASHISH M-NTB837" 
wrote:
> Use threads
>
> Regards,
> Ashish Vyas
>
>
>
> -Original Message-
> From: [email protected]
>
> [mailto:[email protected]] On Behalf Of
> Rajat
> Sent: Thursday, January 07, 2010 2:42 PM
> To: [email protected]
> Subject: How to execute a script from another script and other script
> does notdo busy wait.
>
> I want to run a python script( aka script2) from another python script
> (aka script1). While script1 executes script2 it waits for script2 to
> complete and in doing so it also does some other useful work.(does not
> do a busy wait).
>
> My intention is to update a third party through script1 that script2 is
> going to take longer.- Hide quoted text -
>
> - Show quoted text -

Thanks Ashish.

I've single CPU machine. I've a feeling that the thread created, which
would run script2, would eat up all of the CPU if I do not use sleep()
in script2.

That way, script1 would still be waiting for script2 to finish. Thus,
my program is no way different from the sample program I posted
earlier.

Is there any other way out?
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: How to execute a script from another script and other script doesnotdo busy wait.

2010-01-07 Thread VYAS ASHISH M-NTB837
Did you try?

Thanks Ashish.

I've single CPU machine. I've a feeling that the thread created, which
would run script2, would eat up all of the CPU if I do not use sleep()
in script2.

That way, script1 would still be waiting for script2 to finish. Thus, my
program is no way different from the sample program I posted earlier.

Is there any other way out?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Exception as the primary error handling mechanism?

2010-01-07 Thread Ben Finney
Steve Holden  writes:

> Brilliant. It takes a real whole human being to make an admission like
> that (or even to bother to question their own behavior sufficiently to
> bother re-reading the thread). I think a lot more of you for the
> admission.

Seconded.

-- 
 \   “bash awk grep perl sed, df du, du-du du-du, vi troff su fsck |
  `\ rm * halt LART LART LART!” —The Swedish BOFH, |
_o__)alt.sysadmin.recovery |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Exception as the primary error handling mechanism?

2010-01-07 Thread Roel Schroeven
Bruno Desthuilliers schreef:
> Phlip a écrit :
>> On Jan 5, 8:49 pm, Steven D'Aprano
>>  wrote:
>>
 (A related question - why can't I just go 'if record = method():  use
 (record)'. Why extra lines just to trap and assign the variable before
 using it?)
>>> Because that idiom is responsible for probably the most common error in C
>>> of all, at least one of the most common errors. Thank goodness Python
>>> forbids such a dangerous construct.
>> switching = for == is the "most common error in C"?
>>
>> I can't tell if you are joking.
> 
> It's at least a _very_ common error in all languages that allow this 
> construct.

Maybe it is, maybe it's not. All I know is my own experience; in all the
years I've been doing C and C++ (1998 - now) I've made that mistake only
twice. And in both cases I found the mistake very rapidly.


> In C, it's common enough to gave birth to the "BestPractice" 
> you described, ie swapping operand orders in equality test to have the 
> compiler detect the problem - at least when one of the operand is a 
> function call expression or constant (it obviously won't 'work' when 
> both operands are variables).

I've never liked that practice, for the following reasons:
- As you say, it doesn't work when both operands are variables. In my
experience, in many cases both operands are variables.
- I tend to think that not following that practice trains me to be
careful in all cases, whereas I'm afraid that following the practice
will make me careless, which is dangerous in all the cases where the
practice won't protect me.

> Anyway: in Python, assignment is not an expression, and this isn't going 
> to change anytime soon.

To be fully clear: I'm not advocating to change the current behavior in
Python, I'm just stating my experience in other languages.

-- 
The saddest aspect of life right now is that science gathers knowledge
faster than society gathers wisdom.
  -- Isaac Asimov

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


Re: How to reduce the memory size of python

2010-01-07 Thread Steve Holden
Mishra Gopal-QBX634 wrote:
>  
> Hi,
> 
> I use twisted framework too to handle the xmlrpc request. It takes
> around 3-4MB of memory while importing itself.
> Is there any python coding standard I should follow to save the memory.
> 
> Like import logging takes 1MB of memory.
> We only use on function getLogger by 'from logging import getLogger'
> 
> But it still take the same 1 MB memory.
> 
> Instead of loading whole logging module only load the getLogger
> function.
> 
> I there any way to save the memory with taking care of small things in
> code..
> 
No. You are seeing the size of the interpreter.

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010  http://us.pycon.org/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS:http://holdenweb.eventbrite.com/

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


Re: Call a DLL function with argument type as unsigned char *

2010-01-07 Thread Steve Holden
Bhavik wrote:
> Just to clarify, I am using Python 2.5.1
> 
Take a look at the ctypes module, which allows you to do such things (at
the risk of segmentation faults and the like if you get your calls wrong).

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010  http://us.pycon.org/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS:http://holdenweb.eventbrite.com/

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


Re: embedded python on mac - linking problem

2010-01-07 Thread Krzysztof Kobus
Hi,

I have been able to solve the problem finally:

Initially I was trying (wrongly) to link distutils-made module with my 
application and that has failed. Solution was to (instead of linking the 
module) compile the source files making up the module and link corresponding 
objects as any other sources of the application.

Thank you for your help,

KK


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


Re: buffer interface problem

2010-01-07 Thread Andrew Gillanders
Thanks Chris. The atoi function was coming from the locale library  
(from locale import atoi). I changed it to int and now it works.


The next hurdle is this:
gzin = GzipFile(fname, 'rb')

data = gzin.readline()
#min_x,min_y = map(atoi,data.split()[:2])
min_x,min_y = map(int,data.split()[:2])

data = gzin.readline()
#span_x,step_x,span_y,step_y = map(atoi,data.split()[:4])
span_x,step_x,span_y,step_y = map(int,data.split()[:4])

data = gzin.read().split('\n')

The last line is a problem, giving me this message: Type str doesn't  
support the buffer API (I am guessing a conflict between split and  
read?)


Sorry, I am new to Python, so how do I get a Traceback?

Thanks
Andrew


On 07/01/2010, at 7:13 PM, Chris Rebert wrote:


On Thu, Jan 7, 2010 at 12:19 AM, Andrew Gillanders
 wrote:

I have run into a problem running a Python script that is part of the
TerraGear suite for building scenery for FlightGear. I am using  
Mac OS X

10.4, running Python (version 3.0.1) in a Unix terminal.

The purpose of the script is to walk a directory tree, unzipping  
files, and
passing the contents to an executable C program. The problem  
occurs here:


   gzin = GzipFile(fname, 'rb')
   data = gzin.readline()
   min_x,min_y = map(atoi,data.split()[:2])

The input file, when uncompressed, is an ASCII file with a line  
with two
numbers, then a line of four numbers, then many long lines of  
numbers. I can
see what the last is trying to do: split the string into two  
words, convert

them to integers, and assign them to min_x and min_y.

At the third line, I get the message "expected an object with the  
buffer

interface". Which object is it referring to?


The elements of the list produced by `data.split()[:2]`, which are
either Unicode strings or bytestrings, neither of which are buffers.


Have some functions been
changed to pass buffer objects instead of strings? How can I fix  
the source

code to make it run?


The error is being raised by the atoi() function (in the future,
please post the full Traceback, not just the final error message).
What module/library does your atoi() function come from (look for an
`import` statement mentioning it)?
The only functions by that name in the Python standard library both
operate on strings, not buffers, and thus can't be the same one your
code is using.

In any case, replacing `atoi` with `int` in your code will likely
solve the problem. The built-in int() function* can convert strings to
integers.

Cheers,
Chris
--
http://blog.rebertia.com

*Not really a function, but close enough for newbie explanatory  
purposes.


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


Re: buffer interface problem

2010-01-07 Thread Chris Rebert
On Thu, Jan 7, 2010 at 4:47 AM, Andrew Gillanders
 wrote:
> On 07/01/2010, at 7:13 PM, Chris Rebert wrote:
>> On Thu, Jan 7, 2010 at 12:19 AM, Andrew Gillanders
>>  wrote:
>>>
>>> I have run into a problem running a Python script that is part of the
>>> TerraGear suite for building scenery for FlightGear. I am using Mac OS X
>>> 10.4, running Python (version 3.0.1) in a Unix terminal.
>>>
>>> The purpose of the script is to walk a directory tree, unzipping files,
>>> and
>>> passing the contents to an executable C program. The problem occurs here:
>>>
>>>   gzin = GzipFile(fname, 'rb')
>>>   data = gzin.readline()
>>>   min_x,min_y = map(atoi,data.split()[:2])
>>>
>>> The input file, when uncompressed, is an ASCII file with a line with two
>>> numbers, then a line of four numbers, then many long lines of numbers. I
>>> can
>>> see what the last is trying to do: split the string into two words,
>>> convert
>>> them to integers, and assign them to min_x and min_y.
>>>
>>> At the third line, I get the message "expected an object with the buffer
>>> interface". Which object is it referring to?
>>
>> The elements of the list produced by `data.split()[:2]`, which are
>> either Unicode strings or bytestrings, neither of which are buffers.
>>
>>> Have some functions been
>>> changed to pass buffer objects instead of strings? How can I fix the
>>> source
>>> code to make it run?
>>
>> The error is being raised by the atoi() function (in the future,
>> please post the full Traceback, not just the final error message).
>> What module/library does your atoi() function come from (look for an
>> `import` statement mentioning it)?
>> The only functions by that name in the Python standard library both
>> operate on strings, not buffers, and thus can't be the same one your
>> code is using.
>>
>> In any case, replacing `atoi` with `int` in your code will likely
>> solve the problem. The built-in int() function* can convert strings to
>> integers.

> Thanks Chris. The atoi function was coming from the locale library (from
> locale import atoi). I changed it to int and now it works.

Hm, that's odd since it was one of the 2 functions in the std lib
which the docs say operates on strings...

> The next hurdle is this:
>gzin = GzipFile(fname, 'rb')
>
>data = gzin.readline()
> #min_x,min_y = map(atoi,data.split()[:2])
>min_x,min_y = map(int,data.split()[:2])
>
>data = gzin.readline()
> #span_x,step_x,span_y,step_y = map(atoi,data.split()[:4])
>span_x,step_x,span_y,step_y = map(int,data.split()[:4])
>
>data = gzin.read().split('\n')
>
> The last line is a problem, giving me this message: Type str doesn't support
> the buffer API (I am guessing a conflict between split and read?)

Ah, looking at the 3.0 docs on buffers, I'd surmise gzin.read()
returns bytes (http://docs.python.org/3.1/library/functions.html#bytes)
rather than a string.
You'll want to decode the bytes into characters first, and then you
can operate on the resulting string normally.
Try:

data = gzin.read().decode('ascii').split('\n')

> Sorry, I am new to Python, so how do I get a Traceback?

You should get one by default. Are you running the script in some
environment other than the command line?

Here's what a traceback looks like:

Traceback (most recent call last):
  File "foo", line 161, in 
main()
  File "foo.py", line 157, in main
bot.run()
  File "foo.py", line 68, in bar
self.baz("Enter number: ")
  File "foo.py", line 112, in baz
choice = int(raw_input(prompt))-1
ValueError: invalid literal for int() with base 10: 'y'

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pass multidimensional array (matrix) to c function using ctypes

2010-01-07 Thread Daniel Platz
Thanks a lot. This solves my problem and I understand now much better
what is going on.

Best regards,

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


Re: Where's a DOM builder that uses the Builder Pattern to ... build DOMs?

2010-01-07 Thread Stefan Behnel

Phlip, 05.01.2010 18:00:

On Jan 5, 12:16 am, Stefan Behnel  wrote:


Note that there are tons of ways to generate HTML with Python.


Forgot to note - I'm generating schematic XML, and I'm trying to find
a way better than the Django template I started with!


Well, then note that there are tons of ways to generate XML with Python, 
including the one I pointed you to.


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


Re: Python books, literature etc

2010-01-07 Thread Stuart Murray-Smith
> Have a look at the Getting Started section of the wiki:
>
> http://wiki.python.org/moin/
>
> specially the PythonBooks section

Perfect! Exactly what I'm looking for :)

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


Re: Exception as the primary error handling mechanism?

2010-01-07 Thread Lie Ryan

On 1/7/2010 10:43 PM, Roel Schroeven wrote:

- I tend to think that not following that practice trains me to be
careful in all cases, whereas I'm afraid that following the practice
will make me careless, which is dangerous in all the cases where the
practice won't protect me.



That's a sign of a gotcha... a well-designed language makes you think 
about your problem at hand and less about the language's syntax.

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


Re: Exception as the primary error handling mechanism?

2010-01-07 Thread Dave McCormick



Lie Ryan wrote:
That's a sign of a gotcha... a well-designed language makes you think 
about your problem at hand and less about the language's syntax.

Not until you learn the language that is.
From a Python newbee ;-)

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


Re: GUI for multiplatform multimedia project

2010-01-07 Thread CM
On Jan 6, 4:53 pm,  wrote:
> Hi everyone,
>
> I posted that question on a python-forum, but got answer, so I ask here.
>
> I'm working on an artistic project and I'm looking for the best
> cross-platform GUI solution. The problem is that it's gonna be a tool that
> will have to be double-click installable/runnable and pre-installation of
> any libraries for end-users is very much like an evil. It really has to be
> double-click tool
>
> My first thought was PyQt, because it's a real framework with a lot of
> stuff inside (including Phonon) and I know some cross-platform media
> software written in C++ QT (like VLC). But on the other hand I've heard
> that it's not that easy to make it "double-clicky" multi-platform. Is that
> true?
>
> Another thing that matters for me is ease of integration with libraries
> like OpenCV.
>
> I will be VERY thankful for any help. I'm SO tired googling the problem
> (it's like weeks now!!)
>
> Best from Poland,
> trzewiczek

I don't know this for sure, but I would be surprised if any of the
widget
toolkits gave you much more trouble than any other when making your
app
into a bundled executable.

I have made wxPython apps into a Windows .exe file easily using
GUI2Exe,
which is an excellent GUI interface (written in wxPython by Andrea
Gavana)
to a number of the executable bundlers:  py2exe, PyInstaller, py2app,
cx_Freeze, bbFreeze.  Some of these are for Windows, some for Mac,
some
for Linux.

wxPython apparently works with OpenCV:
http://opencv.willowgarage.com/wiki/wxpython

While you're Googling you might want to be aware of any legal
concerns with py2exe and distributing dll files (if there are
any).






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


Recommended "new" way for config files

2010-01-07 Thread Peter

Hi
There seems to be several strategies to enhance the old ini-style config 
files with real python code, for example:


1) the documentation tool sphinx uses a python file conf.py that is 
exefile(d) , but execfile is suppressed in Python 3

2) there is a module cfgparse on sourceforge that supports a hybrid style
3) modern tools like ipython seems to favor a new style based on python 
code config files but also support a hybrid style mixing .ini files and 
python code files.
4) I could use __import__ to import modules based on some command line 
options



Is there a strategy that should be prefered for new projects ?

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


Re: How to execute a script from another script and other script does not do busy wait.

2010-01-07 Thread Jorgen Grahn
On Thu, 2010-01-07, Rajat wrote:
> I want to run a python script( aka script2) from another python script
> (aka script1). While script1 executes script2 it waits for script2 to
> complete and in doing so it also does some other useful work.(does not
> do a busy wait).
>
> My intention is to update a third party through script1 that script2
> is going to take longer.

I do not understand that sentence.
What are you trying to do, more exactly?  The best solution can be
threads, os.popen, os.system or something different -- depending on
the details of what you want to do.

> Please suggest how should I go about implementing it.
>
> I'm currently executing it as:
>
> import main from script2
> ret_code  = main()
> return ret_code
>
> which surely is not going to achieve me what I intend.
>
>
> Thanks,
> Rajat.

/Jorgen

-- 
  // Jorgen GrahnO  o   .
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Recommended "new" way for config files

2010-01-07 Thread Jean-Michel Pichavant

Peter wrote:

Hi
There seems to be several strategies to enhance the old ini-style 
config files with real python code, for example:


1) the documentation tool sphinx uses a python file conf.py that is 
exefile(d) , but execfile is suppressed in Python 3

2) there is a module cfgparse on sourceforge that supports a hybrid style
3) modern tools like ipython seems to favor a new style based on 
python code config files but also support a hybrid style mixing .ini 
files and python code files.
4) I could use __import__ to import modules based on some command line 
options



Is there a strategy that should be prefered for new projects ?

thanks
peter
I would add the standard module ConfigParser 
http://docs.python.org/library/configparser.html to your list.
I don't know exactly what you intend to do with point 4/, but I would 
exclude it if any other point may fit. Imports can become tricky when 
used out of the common way. Anyway, hacking the import statement for 
managing configuration files does not sound very appropriate.


The .ini file is the simpliest solution, at least from the user point of 
view, no need to learn any python syntax.
However, speeking for myself, I am using python coded configuration 
files, but: we all worship python in the team and thus are familiar with 
it.


JM


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


Re: Recommended "new" way for config files

2010-01-07 Thread Lie Ryan

On 1/8/2010 3:10 AM, Peter wrote:

Is there a strategy that should be prefered for new projects ?


The answer is, it depends.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Do I have to use threads?

2010-01-07 Thread Jorgen Grahn
On Thu, 2010-01-07, Marco Salden wrote:
> On Jan 6, 5:36 am, Philip Semanchuk  wrote:
>> On Jan 5, 2010, at 11:26 PM, aditya shukla wrote:
>>
>> > Hello people,
>>
>> > I have 5 directories corresponding 5  different urls .I want to  
>> > download
>> > images from those urls and place them in the respective  
>> > directories.I have
>> > to extract the contents and download them simultaneously.I can  
>> > extract the
>> > contents and do then one by one. My questions is for doing it  
>> > simultaneously
>> > do I have to use threads?
>>
>> No. You could spawn 5 copies of wget (or curl or a Python program that  
>> you've written). Whether or not that will perform better or be easier  
>> to code, debug and maintain depends on the other aspects of your  
>> program(s).
>>
>> bye
>> Philip
>
> Yep, the more easier and straightforward the approach, the better:
> threads are always (programmers')-error-prone by nature.
> But my question would be: does it REALLY need to be simultaneously:
> the CPU/OS only has more overhead doing this in parallel with
> processess. Measuring sequential processing and then trying to
> optimize (e.g. for user response or whatever) would be my prefered way
> to go. Less=More.

Normally when you do HTTP in parallell over several TCP sockets, it
has nothing to do with CPU overhead. You just don't want every GET to
be delayed just because the server(s) are lazy responding to the first
few ones; or you might want to read the text of a web page and the CSS
before a few huge pictures have been downloaded.

His "I have to [do them] simultaneously" makes me want to ask "Why?".

If he's expecting *many* pictures, I doubt that the parallel download
will buy him much.  Reusing the same TCP socket for all of them is
more likely to help, especially if the pictures aren't tiny. One
long-lived TCP connection is much more efficient than dozens of
short-lived ones.

Personally, I'd popen() wget and let it do the job for me.

/Jorgen

-- 
  // Jorgen GrahnO  o   .
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Recommended "new" way for config files

2010-01-07 Thread Robert Kern

On 2010-01-07 10:10 AM, Peter wrote:

Hi
There seems to be several strategies to enhance the old ini-style config
files with real python code, for example:

1) the documentation tool sphinx uses a python file conf.py that is
exefile(d) , but execfile is suppressed in Python 3


Only because it is redundant, not because it is a discouraged approach. You can 
still read the file and exec() the resulting string.


--
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: Where's a DOM builder that uses the Builder Pattern to ... build DOMs?

2010-01-07 Thread Phlip
On Jan 7, 5:36 am, Stefan Behnel  wrote:

> Well, then note that there are tons of ways to generate XML with Python,
> including the one I pointed you to.

from lxml.html import builder as E
xml = E.foo()

All I want is "", but I get "AttributeError: 'module' object has
no attribute 'foo'".

A peek at dir(E) shows it only has HTML tags, all hard coded.

So how to get it to generate any random XML tag my clients think of?

I will write this myself with __getattr__ etc, if I can't find it,
because the permissive & expressive builder pattern I'm after would be
very ... permissive & expressive.

All I want is a library that reads my mind!!! Is that too much to
ask??? (Unless if the library insists on throwing a NullMind
exception, on principle...)

--
  Phlip
  http://twitter.com/Pen_Bird
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python books, literature etc

2010-01-07 Thread Jorgen Grahn
On Thu, 2010-01-07, Stuart Murray-Smith wrote:
...
> [...] ESR's guide to
> smart questions [1] helps set the pace of list culture.

It's good, if you can ignore the "These People Are Very Important
Hacker Gods, Not Mere Mortals" subtext.

...
> Anyways, to rephrase, could someone kindly mention any of their
> preferred Python books, websites, tutorials etc to help me get to an
> intermediate/advanced level? Something that would help me add
> functionality to Ubiquity, say.

I may be alone in this, but Alex Martelli's book ("Python in a
nutshell"?) on Python 2.2 and a bit of 2.3, plus the official
documentation, plus this group, is all I think I need.
But I had a lot of Unix, C, C++ and Perl experience to help me.

/Jorgen

-- 
  // Jorgen GrahnO  o   .
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Do I have to use threads?

2010-01-07 Thread MRAB

Jorgen Grahn wrote:

On Thu, 2010-01-07, Marco Salden wrote:

On Jan 6, 5:36 am, Philip Semanchuk  wrote:

On Jan 5, 2010, at 11:26 PM, aditya shukla wrote:


Hello people,
I have 5 directories corresponding 5  different urls .I want to  
download
images from those urls and place them in the respective  
directories.I have
to extract the contents and download them simultaneously.I can  
extract the
contents and do then one by one. My questions is for doing it  
simultaneously

do I have to use threads?
No. You could spawn 5 copies of wget (or curl or a Python program that  
you've written). Whether or not that will perform better or be easier  
to code, debug and maintain depends on the other aspects of your  
program(s).


bye
Philip

Yep, the more easier and straightforward the approach, the better:
threads are always (programmers')-error-prone by nature.
But my question would be: does it REALLY need to be simultaneously:
the CPU/OS only has more overhead doing this in parallel with
processess. Measuring sequential processing and then trying to
optimize (e.g. for user response or whatever) would be my prefered way
to go. Less=More.


Normally when you do HTTP in parallell over several TCP sockets, it
has nothing to do with CPU overhead. You just don't want every GET to
be delayed just because the server(s) are lazy responding to the first
few ones; or you might want to read the text of a web page and the CSS
before a few huge pictures have been downloaded.

His "I have to [do them] simultaneously" makes me want to ask "Why?".

If he's expecting *many* pictures, I doubt that the parallel download
will buy him much.  Reusing the same TCP socket for all of them is
more likely to help, especially if the pictures aren't tiny. One
long-lived TCP connection is much more efficient than dozens of
short-lived ones.

Personally, I'd popen() wget and let it do the job for me.


From my own experience:

I wanted to download a number of webpages.

I noticed that there was a significant delay before it would reply, and
an especially long delay for one of them, so I used a number of threads,
each one reading a URL from a queue, performing the download, and then
reading the next URL, until there were none left (actually, until it
read the sentinel None, which it put back for the other threads).

The result?

Shorter total download time because it could be downloading one webpage
while waiting for another to reply.

(Of course, I had to make sure that I didn't have too many threads,
because that might've put too many demands on the website, not a nice
thing to do!)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Do I have to use threads?

2010-01-07 Thread Philip Semanchuk


On Jan 7, 2010, at 11:32 AM, Jorgen Grahn wrote:


On Thu, 2010-01-07, Marco Salden wrote:

On Jan 6, 5:36 am, Philip Semanchuk  wrote:

On Jan 5, 2010, at 11:26 PM, aditya shukla wrote:


Hello people,



I have 5 directories corresponding 5  different urls .I want to
download
images from those urls and place them in the respective
directories.I have
to extract the contents and download them simultaneously.I can
extract the
contents and do then one by one. My questions is for doing it
simultaneously
do I have to use threads?


No. You could spawn 5 copies of wget (or curl or a Python program  
that
you've written). Whether or not that will perform better or be  
easier

to code, debug and maintain depends on the other aspects of your
program(s).

bye
Philip


Yep, the more easier and straightforward the approach, the better:
threads are always (programmers')-error-prone by nature.
But my question would be: does it REALLY need to be simultaneously:
the CPU/OS only has more overhead doing this in parallel with
processess. Measuring sequential processing and then trying to
optimize (e.g. for user response or whatever) would be my prefered  
way

to go. Less=More.


Normally when you do HTTP in parallell over several TCP sockets, it
has nothing to do with CPU overhead. You just don't want every GET to
be delayed just because the server(s) are lazy responding to the first
few ones; or you might want to read the text of a web page and the CSS
before a few huge pictures have been downloaded.

His "I have to [do them] simultaneously" makes me want to ask "Why?".


Exactly what I was thinking. He's surely doing something more  
complicated than his post suggests, and without that detail it's  
impossible to say whether threads, processes, asynch or voodoo is the  
best approach.



bye
P


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


ANN: Pymazon 0.1.0 released!

2010-01-07 Thread S. Chris Colbert
Hello, 

I'm happy to announce the first non-beta release of Pymazon: a python 
implemented downloader for the Amazon mp3 store. 

Improvements from the beta:
- Running download status indicator
- Various fixes for Windows
- Some code cleanup

Pymazon was created to be a simple and easy alternative for the Linux version 
of the Amazon downloader, and alleviate the pain of getting it to work with 
64bit Linux. 

You can read about Pymazon at http://pymazon.googlecode.com

You can download from googlecode or the cheeseshop:

$ pip install pymazon

or

$ easy_install pymazon

It also works on Windows. 

Dependencies:
PyCrypto (it's in the ubuntu repos and the cheeseshop)
PyQt4 >= 4.5 (optional, only needed for GUI)

GPLv3 License

Cheers!

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


Re: Recommended "new" way for config files

2010-01-07 Thread Peter

Thanks for your answer, let me be more precise:

I would add the standard module ConfigParser 
http://docs.python.org/library/configparser.html to your list.
of course, that was the implicit starting point of my request, when 
talking about .ini files.
I don't know exactly what you intend to do with point 4/, 
It would allow me to select different conf.py files with command line 
switches, like for example a  -c  option.
but I would exclude it if any other point may fit. Imports can become 
tricky when used out of the common way. Anyway, hacking the import 
statement for managing configuration files does not sound very 
appropriate.



Would this be considered a hack ?

#!/usr/bin/env python

import sys

# parse command line options here

if option='standard':
  const = __import__('consts')
else:
  const = __import__('alternative_consts')

The .ini file is the simpliest solution, at least from the user point 
of view, no need to learn any python syntax.
I am speaking from the point of view of a python programmer, and I find 
the .ini restrictions not necessarily simple, for example when dealing 
with structured data (I suppose it is trivial to specify a dictionnary 
or a list for the purpose of my request) For example, configuration 
files for the logging module get unwieldy when you specify several 
loggers , handlers, formatters etc, because you have to break down 
structured data ( objects ) to name,value pairs.
However, speeking for myself, I am using python coded configuration 
files, but: we all worship python in the team and thus are familiar 
with it.



so do I.

JM


So what is the "worshipped" approach, when you need more than name=value 
pairs ?


Peter




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


Re: Recommended "new" way for config files

2010-01-07 Thread Chris Rebert
On Thu, Jan 7, 2010 at 10:19 AM, Peter  wrote:

>> The .ini file is the simpliest solution, at least from the user point of
>> view, no need to learn any python syntax.
>
> I am speaking from the point of view of a python programmer, and I find the
> .ini restrictions not necessarily simple, for example when dealing with
> structured data (I suppose it is trivial to specify a dictionnary or a list
> for the purpose of my request) For example, configuration files for the
> logging module get unwieldy when you specify several loggers , handlers,
> formatters etc, because you have to break down structured data ( objects )
> to name,value pairs.

> So what is the "worshipped" approach, when you need more than name=value
> pairs ?

JSON is one option: http://docs.python.org/library/json.html

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Dictionary used to build a Triple Store

2010-01-07 Thread Lee

Definitely a newbie question, so please bear with  me.

I'm reading "Programming the Semantic Web" by Segaran, Evans, and Tayor.

It's about the Semantic Web BUT it uses python to build a "toy" triple 
store claimed to have good performance in the "tens of thousands" of 
triples.


Just in case anybody doesnt know what an RDF triple is (not that it 
matters for my question) think of it as an ordered 3 tuple representing 
a Subject, a Predicate, and an Object eg: (John, loves, Mary) (Mary, 
has-a, lamb) {theSky, has-color,blue}


To build the triple store entirely in Python, the authors recommend
using the Python hash. Three hashes actually (I get that. You
want to have a hash with the major index being the Subject in one hash,
the Predicate in another hash, or the Object for the third hash)

He creates a class SimpleGraph which initializes itself by setting the
three hashes names _spo, _pos, and _osp thus

class SimpleGraph;
  def __init__(self);
 self._spo={};
 self._pos=();
 self._osp={};

So far so good. I get the convention with the double underbars for the 
initializer but


Q1: Not the main question but while I'm hereI'm a little fuzzy on 
the convention about the use of the single underbar in the definition of 
the hashes. Id the idea to "underbar" all objects and methods that 
belong to the class? Why do that?


But now the good stuff:

Our authors define the hashes thus: (showing only one of the three 
hashes because they're all the same idea)


self._pos = {predicate:{object:set( [subject] ) }}

Q2: Wha? Two surprises ...
   1) Why not {predicate:{object:subject}} i.e. 
pos[predicate][object]=subjectwhy the set( [object] ) construct? 
putting the object into a list and turning the list into a set to be the 
"value" part of a name:value pair. Why not just use the naked subject 
for the value?


   2) Why not something like pos[predicate][object][subject] = 1 
.or any constant. The idea being to create the set of three indexes. 
If the triple exists in the hash, its "in" your tripple store. If not, 
then there's no such triple.



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


Re: How to reduce the memory size of python

2010-01-07 Thread Terry Reedy

On 1/7/2010 3:34 AM, Mishra Gopal-QBX634 wrote:





Like import logging takes 1MB of memory.
We only use on function getLogger by 'from logging import getLogger'

But it still take the same 1 MB memory.

Instead of loading whole logging module only load the getLogger
function.


from x import y

causes creation of module x and binding of the module to sys.modules'x]. 
It then binds name 'y' in the current namespace to the corresponding 
object in x. Functions in general need a reference to the module 
namespace to resolve module-level variables.


To save anything, you must cut the function out of the module and verify 
that it works in isolation. But I presume 'getLogger' refers to other 
stuff in the logging module and would not work in isolation.


Terry Jan Reedy

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


Re: Python books, literature etc

2010-01-07 Thread Peter



Anyways, to rephrase, could someone kindly mention any of their
preferred Python books, websites, tutorials etc to help me get to an
intermediate/advanced level? Something that would help me add
functionality to Ubiquity, say.
 

I may be alone in this, but Alex Martelli's book ("Python in a
nutshell"?) on Python 2.2 and a bit of 2.3, plus the official
documentation, plus this group, is all I think I need.
But I had a lot of Unix, C, C++ and Perl experience to help me.

/Jorgen

   
I find Alex Martellis "Python Cookbook" excellent/invaluable and ( and 
also his Nutshell book mentioned above ) and depending on your 
application domain, I liked:


1) Hans Petter Langtangen: Python Scripting for Computational Science
A truly excellent book, not only with respect to Python Scripting , but 
also on how to avoid paying  license fees by using opensource tools as 
an engineer ( plotting, graphing, gui dev etc ). Very good , pratical 
introduction to Python with careful and non-trivial examples and exercises.


2) There is a book at Apress on using Python and matplotlib ( amongst 
other ) "Beginning Python Visualization" which is not as comprehensive 
as reference 1) but useful , especially for beginners who wants to 
visualize data from an engineers background


3) "Programming for the semantic web" Oreilly is a very pratical and 
interesting guide to things like OWL, triplestore, logic, reasoning, 
data mining and it is amongst the very few books on these topics I have 
seen that has working code examples


4) "Natural language priocessing with Python " Oreilly is also a 
pratical book with lots of working code if you are interested in data 
mining, text searching and natural language tasks. It is based on a 
rather large opensource library for natural language processing ( sorry 
forgot the exact name,but easy to find on the net)


All these book make you feel warm and confortable if you have ever tried 
to do these things in Perl, C++ or Java


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


Re: Dictionary used to build a Triple Store

2010-01-07 Thread S. Chris Colbert
> Definitely a newbie question, so please bear with  me.
> 
> I'm reading "Programming the Semantic Web" by Segaran, Evans, and Tayor.
> 
> It's about the Semantic Web BUT it uses python to build a "toy" triple
> store claimed to have good performance in the "tens of thousands" of
> triples.
> 
> Just in case anybody doesnt know what an RDF triple is (not that it
> matters for my question) think of it as an ordered 3 tuple representing
> a Subject, a Predicate, and an Object eg: (John, loves, Mary) (Mary,
> has-a, lamb) {theSky, has-color,blue}
> 
> To build the triple store entirely in Python, the authors recommend
> using the Python hash. Three hashes actually (I get that. You
> want to have a hash with the major index being the Subject in one hash,
> the Predicate in another hash, or the Object for the third hash)
> 
> He creates a class SimpleGraph which initializes itself by setting the
> three hashes names _spo, _pos, and _osp thus
> 
> class SimpleGraph;
>def __init__(self);
>   self._spo={};
>   self._pos=();
>   self._osp={};
> 
> So far so good. I get the convention with the double underbars for the
> initializer but
> 
> Q1: Not the main question but while I'm hereI'm a little fuzzy on
> the convention about the use of the single underbar in the definition of
> the hashes. Id the idea to "underbar" all objects and methods that
> belong to the class? Why do that?
> 
> But now the good stuff:
> 
> Our authors define the hashes thus: (showing only one of the three
> hashes because they're all the same idea)
> 
> self._pos = {predicate:{object:set( [subject] ) }}
> 
> Q2: Wha? Two surprises ...
> 1) Why not {predicate:{object:subject}} i.e.
> pos[predicate][object]=subjectwhy the set( [object] ) construct?
> putting the object into a list and turning the list into a set to be the
> "value" part of a name:value pair. Why not just use the naked subject
> for the value?
> 
because the argument has to be iterable.

In [1]: set(1)
---
TypeError Traceback (most recent call last)

/home/brucewayne/ in ()

TypeError: 'int' object is not iterable

> 2) Why not something like pos[predicate][object][subject] = 1
> .or any constant. The idea being to create the set of three indexes.
> If the triple exists in the hash, its "in" your tripple store. If not,
> then there's no such triple.
> 
I can't really answer that, I imagine there is a better way to code what is 
trying to be accomplished. But I'm no Steven D'Aprano and I'm already a few 
beers in ;)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dictionary used to build a Triple Store

2010-01-07 Thread Steve Holden
Lee wrote:
> Definitely a newbie question, so please bear with  me.
> 
> I'm reading "Programming the Semantic Web" by Segaran, Evans, and Tayor.
> 
> It's about the Semantic Web BUT it uses python to build a "toy" triple
> store claimed to have good performance in the "tens of thousands" of
> triples.
> 
> Just in case anybody doesnt know what an RDF triple is (not that it
> matters for my question) think of it as an ordered 3 tuple representing
> a Subject, a Predicate, and an Object eg: (John, loves, Mary) (Mary,
> has-a, lamb) {theSky, has-color,blue}
> 
> To build the triple store entirely in Python, the authors recommend
> using the Python hash. Three hashes actually (I get that. You
> want to have a hash with the major index being the Subject in one hash,
> the Predicate in another hash, or the Object for the third hash)
> 
> He creates a class SimpleGraph which initializes itself by setting the
> three hashes names _spo, _pos, and _osp thus
> 
> class SimpleGraph;
>   def __init__(self);
>  self._spo={};
>  self._pos=();
>  self._osp={};
> 
> So far so good. I get the convention with the double underbars for the
> initializer but
> 
> Q1: Not the main question but while I'm hereI'm a little fuzzy on
> the convention about the use of the single underbar in the definition of
> the hashes. Id the idea to "underbar" all objects and methods that
> belong to the class? Why do that?
> 
> But now the good stuff:
> 
> Our authors define the hashes thus: (showing only one of the three
> hashes because they're all the same idea)
> 
> self._pos = {predicate:{object:set( [subject] ) }}
> 
> Q2: Wha? Two surprises ...
>1) Why not {predicate:{object:subject}} i.e.
> pos[predicate][object]=subjectwhy the set( [object] ) construct?
> putting the object into a list and turning the list into a set to be the
> "value" part of a name:value pair. Why not just use the naked subject
> for the value?
> 
Because for a given predicate there can be many objects, and you need to
be able to look up the subjects associated with the same object and
predicate. (I am assuming this is initialization code: to add another
subject with the same object to the predicate you would use

self._pos[predicate][object].add(subject)

>2) Why not something like pos[predicate][object][subject] = 1 .or
> any constant. The idea being to create the set of three indexes. If the
> triple exists in the hash, its "in" your tripple store. If not, then
> there's no such triple.
> 
Because it's less efficient. Since there will only ever be one unique
occurrence of each (predicate, object, subject) triple using a dict
would be unnecessarily wasteful. Containment checks for sets are just as
fast as for dicts, asn you don't need to store all those references to 1.

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010  http://us.pycon.org/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS:http://holdenweb.eventbrite.com/

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


Re: Exception as the primary error handling mechanism?

2010-01-07 Thread Roel Schroeven
Lie Ryan schreef:
> On 1/7/2010 10:43 PM, Roel Schroeven wrote:
>> - I tend to think that not following that practice trains me to be
>> careful in all cases, whereas I'm afraid that following the practice
>> will make me careless, which is dangerous in all the cases where the
>> practice won't protect me.
>>
> 
> That's a sign of a gotcha... a well-designed language makes you think 
> about your problem at hand and less about the language's syntax.

It's not a big deal to me, but you're right, it's a gotcha. I don't
think there's a single language without gotcha's; one of the things I
like about Python is that is has many less than the other languages I know.

-- 
The saddest aspect of life right now is that science gathers knowledge
faster than society gathers wisdom.
  -- Isaac Asimov

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


PyQt QThreadPool error

2010-01-07 Thread h0uk
Hello.

I have the following code:

#workers = {}
QtCore.QThreadPool.globalInstance().setExpiryTimeout
(30)
QtCore.QThreadPool.globalInstance().setMaxThreadCount(1)
for i in range(1, int(userscnt) + 1):
work = wk.Worker(i)
# connect signals
work.mmShowCaptcha.connect(self.show_captcha_dlg)
work.log.connect(self.handle_log)
self.captcha_answer.connect(work.mmCaptchaAnswer)
work.setAutoDelete(True)
QtCore.QThreadPool.globalInstance().start(work)



On last line of code ( QtCore.QThreadPool.globalInstance().start
(work) ) i get an error:

SystemError: error return without exception set

What is wrong in my code??? Any advise???

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


Re: How to execute a script from another script and other script does not do busy wait.

2010-01-07 Thread [email protected]
On Jan 7, 9:18 am, Jorgen Grahn  wrote:
> On Thu, 2010-01-07, Rajat wrote:
> > I want to run a python script( aka script2) from another python script
> > (aka script1). While script1 executes script2 it waits for script2 to
> > complete and in doing so it also does some other useful work.(does not
> > do a busy wait).
>
> > My intention is to update a third party through script1 that script2
> > is going to take longer.
>
> I do not understand that sentence.
> What are you trying to do, more exactly?  The best solution can be
> threads, os.popen, os.system or something different -- depending on
> the details of what you want to do.
>
> > Please suggest how should I go about implementing it.
>
> > I'm currently executing it as:
>
> > import main from script2
> > ret_code  = main()
> > return ret_code
>
> > which surely is not going to achieve me what I intend.
>
> > Thanks,
> > Rajat.
>
> /Jorgen
>
> --
>   // Jorgen Grahn  \X/     snipabacken.se>   O  o   .

I personally use subprocess. Once you launch via subprocess you can
wait or not.

p = subprocess.Popen(...)
p.wait() #or not.

See subprocess docs.

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


Re: GUI for multiplatform multimedia project

2010-01-07 Thread Diez B. Roggisch

[email protected] schrieb:

Hi everyone,

I posted that question on a python-forum, but got answer, so I ask here. 


I'm working on an artistic project and I'm looking for the best
cross-platform GUI solution. The problem is that it's gonna be a tool that
will have to be double-click installable/runnable and pre-installation of
any libraries for end-users is very much like an evil. It really has to be
double-click tool 


My first thought was PyQt, because it's a real framework with a lot of
stuff inside (including Phonon) and I know some cross-platform media
software written in C++ QT (like VLC). But on the other hand I've heard
that it's not that easy to make it "double-clicky" multi-platform. Is that
true? 


I don't know exactly what you mean with that, but I doubt it's easier 
with anything else.


Another option might be pygame + simple OpenGL though, if you are 
planning on heavy use of Canvas-like things, that might be good enough & 
less heavyweight.




Another thing that matters for me is ease of integration with libraries
like OpenCV. 


That has little todo with the toolkit. You need some 
image-converting-code, I've written such for Cocoa, to convert OpenCV's 
RGBA to OSX ARGB. But the last resort would be to save the images from 
OpenCV and read them using Qt (or whatever toolkit you use in th eend.


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


Re: PyQt QThreadPool error

2010-01-07 Thread Diez B. Roggisch

h0uk schrieb:

Hello.

I have the following code:

#workers = {}
QtCore.QThreadPool.globalInstance().setExpiryTimeout
(30)
QtCore.QThreadPool.globalInstance().setMaxThreadCount(1)
for i in range(1, int(userscnt) + 1):
work = wk.Worker(i)
# connect signals
work.mmShowCaptcha.connect(self.show_captcha_dlg)
work.log.connect(self.handle_log)
self.captcha_answer.connect(work.mmCaptchaAnswer)
work.setAutoDelete(True)
QtCore.QThreadPool.globalInstance().start(work)



On last line of code ( QtCore.QThreadPool.globalInstance().start
(work) ) i get an error:

SystemError: error return without exception set

What is wrong in my code??? Any advise???



The error is on C-level. AFAIK it occurs when a Python-C-function 
returns "NULL" without setting an exception.


It's hard to say where it really occurs. I'd use a debug-build of PyQt, 
and possibly Python, and then investigate using gdb.


Alternatively, what happens when you do some "dummy"-work that doesn't 
use signals and no other libraries?


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


Re: Recommended "new" way for config files

2010-01-07 Thread Peter




   

So what is the "worshipped" approach, when you need more than name=value
pairs ?
 

JSON is one option: http://docs.python.org/library/json.html


   
Thanks, didn't think about that, although most of the apps I know don't 
seem to use this approach for improved conf file handling ( ipython, 
pylons, etc ).


To me , the ipython way ( hybrid: ipy_user_conf.py and *.ini files ) 
seems to be the most comprehensive way amongst the larger apps I know 
of, since it let you have a python coded file for what ever you might 
want to do during initialization and have additional .ini files, 
,possibily several in different locations, for simpler options in 
name,value format.


Has anybody experiences with other tools that use this approach ?

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


Re: How to execute a script from another script and other script does notdo busy wait.

2010-01-07 Thread Jan Kaliszewski

Rajat  wrote:


I've single CPU machine. I've a feeling that the thread created, which
would run script2, would eat up all of the CPU if I do not use sleep()
in script2.



That way, script1 would still be waiting for script2 to finish.


Single CPU is not a problem for threads (in fact it's even better).
It'll work. Try it.

Another possibility is to run script2 in a separate process (e.g. using
subprocess module).

Cheers,
*j
--
http://mail.python.org/mailman/listinfo/python-list


Re: File transfer with python

2010-01-07 Thread Jan Kaliszewski

Valentin de Pablo Fouce  wrote:


On 6 ene, 22:42, "Jan Kaliszewski"  wrote:

Valentin de Pablo Fouce  wrote:

> Ok, I am trying to do a very quick application (is "home based" so is
> not a big deal...). My intention is to transfer files from one
> computer to another.
> My intention is to be able to transfer files from one computer to
> another in this environment.

> Looking (and surfing) at internet the only suggestion given is to use
> low level sockets for this file transfer. Is there another way to do
> it, is there any top level library that helps you to do that?

Python standard library offers tools for HTTP communication (rather  
easy to use) --



Your solution looks quite nice...but one question, just by looking to
it I think I will need to create an HTTP server on a "file" server PC,
isn't it?


Yes, in case of such communication protocols like HTTP always one side  
have a role of a serwer, and the other -- of a client. However, please  
note that files can be both downloaded *from* serwer and uploaded *to*  
serwer. You can also run *each* side both as a client *and* as a serwer  
(though it's rather unnecessary...).


Cheers,
*j
--
http://mail.python.org/mailman/listinfo/python-list


Re: Regex help needed!

2010-01-07 Thread Aahz
In article <19de1d6e-5ba9-42b5-9221-ed7246e39...@u36g2000prn.googlegroups.com>,
Oltmans   wrote:
>
>I've written this regex that's kind of working
>re.findall("\w+\s*\W+amazon_(\d+)",str)
>
>but I was just wondering that there might be a better RegEx to do that
>same thing. Can you kindly suggest a better/improved Regex. Thank you
>in advance.

'Some people, when confronted with a problem, think "I know, I'll use
regular expressions."  Now they have two problems.'
--Jamie Zawinski

Take the advice other people gave you and use BeautifulSoup.
-- 
Aahz ([email protected])   <*> http://www.pythoncraft.com/

"If you think it's expensive to hire a professional to do the job, wait
until you hire an amateur."  --Red Adair
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PyQt QThreadPool error

2010-01-07 Thread h0uk
On 8 янв, 01:02, "Diez B. Roggisch"  wrote:
> h0uk schrieb:
>
>
>
> > Hello.
>
> > I have the following code:
>
> >             #workers = {}
> >             QtCore.QThreadPool.globalInstance().setExpiryTimeout
> > (30)
> >             QtCore.QThreadPool.globalInstance().setMaxThreadCount(1)
> >             for i in range(1, int(userscnt) + 1):
> >                 work = wk.Worker(i)
> >                 # connect signals
> >                 work.mmShowCaptcha.connect(self.show_captcha_dlg)
> >                 work.log.connect(self.handle_log)
> >                 self.captcha_answer.connect(work.mmCaptchaAnswer)
> >                 work.setAutoDelete(True)
> >                 QtCore.QThreadPool.globalInstance().start(work)
>
> > On last line of code ( QtCore.QThreadPool.globalInstance().start
> > (work) ) i get an error:
>
> > SystemError: error return without exception set
>
> > What is wrong in my code??? Any advise???
>
> The error is on C-level. AFAIK it occurs when a Python-C-function
> returns "NULL" without setting an exception.
>
> It's hard to say where it really occurs. I'd use a debug-build of PyQt,
> and possibly Python, and then investigate using gdb.
>
> Alternatively, what happens when you do some "dummy"-work that doesn't
> use signals and no other libraries?
>
> Diez

About some "dummy" code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import os
import time

from PyQt4 import QtCore, QtGui

class Job(QtCore.QRunnable):
def __init__(self, name):
QtCore.QRunnable.__init__(self)
self._name = name

def run(self):
time.sleep(10)
print self._name

def autoDelete(self):
return self._auto

def setAutoDelete(self, auto):
self._auto = auto



if __name__ == "__main__":

app = QtGui.QApplication(sys.argv)

QtCore.QThreadPool.globalInstance().setMaxThreadCount(1)

j = Job("Job-1")
j.setAutoDelete(True)
QtCore.QThreadPool.globalInstance().start(j)


Even this code not work. On the last line of code
( QtCore.QThreadPool.globalInstance().start(j) ) i get the error:

>> An unhandled win32 exception occured in python.exe

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


Re: PyQt QThreadPool error

2010-01-07 Thread Diez B. Roggisch

h0uk schrieb:

On 8 янв, 01:02, "Diez B. Roggisch"  wrote:

h0uk schrieb:




Hello.
I have the following code:
#workers = {}
QtCore.QThreadPool.globalInstance().setExpiryTimeout
(30)
QtCore.QThreadPool.globalInstance().setMaxThreadCount(1)
for i in range(1, int(userscnt) + 1):
work = wk.Worker(i)
# connect signals
work.mmShowCaptcha.connect(self.show_captcha_dlg)
work.log.connect(self.handle_log)
self.captcha_answer.connect(work.mmCaptchaAnswer)
work.setAutoDelete(True)
QtCore.QThreadPool.globalInstance().start(work)
On last line of code ( QtCore.QThreadPool.globalInstance().start
(work) ) i get an error:
SystemError: error return without exception set
What is wrong in my code??? Any advise???

The error is on C-level. AFAIK it occurs when a Python-C-function
returns "NULL" without setting an exception.

It's hard to say where it really occurs. I'd use a debug-build of PyQt,
and possibly Python, and then investigate using gdb.

Alternatively, what happens when you do some "dummy"-work that doesn't
use signals and no other libraries?

Diez


About some "dummy" code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import os
import time

from PyQt4 import QtCore, QtGui

class Job(QtCore.QRunnable):
def __init__(self, name):
QtCore.QRunnable.__init__(self)
self._name = name

def run(self):
time.sleep(10)
print self._name

def autoDelete(self):
return self._auto

def setAutoDelete(self, auto):
self._auto = auto



if __name__ == "__main__":

app = QtGui.QApplication(sys.argv)

QtCore.QThreadPool.globalInstance().setMaxThreadCount(1)

j = Job("Job-1")
j.setAutoDelete(True)
QtCore.QThreadPool.globalInstance().start(j)


Even this code not work. On the last line of code
( QtCore.QThreadPool.globalInstance().start(j) ) i get the error:


An unhandled win32 exception occured in python.exe


Hm. I suggest you take this to the PyQt mailinglist. Phil Thompson is 
very responsive.


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


Re: PyQt QThreadPool error

2010-01-07 Thread h0uk
On 8 янв, 02:25, "Diez B. Roggisch"  wrote:
> h0uk schrieb:
>
>
>
> > On 8 янв, 01:02, "Diez B. Roggisch"  wrote:
> >> h0uk schrieb:
>
> >>> Hello.
> >>> I have the following code:
> >>>             #workers = {}
> >>>             QtCore.QThreadPool.globalInstance().setExpiryTimeout
> >>> (30)
> >>>             QtCore.QThreadPool.globalInstance().setMaxThreadCount(1)
> >>>             for i in range(1, int(userscnt) + 1):
> >>>                 work = wk.Worker(i)
> >>>                 # connect signals
> >>>                 work.mmShowCaptcha.connect(self.show_captcha_dlg)
> >>>                 work.log.connect(self.handle_log)
> >>>                 self.captcha_answer.connect(work.mmCaptchaAnswer)
> >>>                 work.setAutoDelete(True)
> >>>                 QtCore.QThreadPool.globalInstance().start(work)
> >>> On last line of code ( QtCore.QThreadPool.globalInstance().start
> >>> (work) ) i get an error:
> >>> SystemError: error return without exception set
> >>> What is wrong in my code??? Any advise???
> >> The error is on C-level. AFAIK it occurs when a Python-C-function
> >> returns "NULL" without setting an exception.
>
> >> It's hard to say where it really occurs. I'd use a debug-build of PyQt,
> >> and possibly Python, and then investigate using gdb.
>
> >> Alternatively, what happens when you do some "dummy"-work that doesn't
> >> use signals and no other libraries?
>
> >> Diez
>
> > About some "dummy" code:
>
> > #!/usr/bin/env python
> > # -*- coding: utf-8 -*-
>
> > import sys
> > import os
> > import time
>
> > from PyQt4 import QtCore, QtGui
>
> > class Job(QtCore.QRunnable):
> >    def __init__(self, name):
> >            QtCore.QRunnable.__init__(self)
> >            self._name = name
>
> >    def run(self):
> >            time.sleep(10)
> >            print self._name
>
> >    def autoDelete(self):
> >            return self._auto
>
> >    def setAutoDelete(self, auto):
> >            self._auto = auto
>
> > if __name__ == "__main__":
>
> >    app = QtGui.QApplication(sys.argv)
>
> >    QtCore.QThreadPool.globalInstance().setMaxThreadCount(1)
>
> >    j = Job("Job-1")
> >    j.setAutoDelete(True)
> >    QtCore.QThreadPool.globalInstance().start(j)
>
> > Even this code not work. On the last line of code
> > ( QtCore.QThreadPool.globalInstance().start(j) ) i get the error:
>
> >>> An unhandled win32 exception occured in python.exe
>
> Hm. I suggest you take this to the PyQt mailinglist. Phil Thompson is
> very responsive.
>
> Diez

Thanks you, Diez. Thanks for your time.
I already wrote to PyQt mailing list, but yet no get answer.

I will be waiting and try to write Phil Thompson.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PyQt QThreadPool error

2010-01-07 Thread Phil Thompson
On Thu, 7 Jan 2010 13:03:24 -0800 (PST), h0uk 
wrote:
> On 8 янв, 01:02, "Diez B. Roggisch"  wrote:
>> h0uk schrieb:
>>
>>
>>
>> > Hello.
>>
>> > I have the following code:
>>
>> >             #workers = {}
>> >             QtCore.QThreadPool.globalInstance().setExpiryTimeout
>> > (30)
>> >            
>> > QtCore.QThreadPool.globalInstance().setMaxThreadCount(1)
>> >             for i in range(1, int(userscnt) + 1):
>> >                 work = wk.Worker(i)
>> >                 # connect signals
>> >                
>> > work.mmShowCaptcha.connect(self.show_captcha_dlg)
>> >                 work.log.connect(self.handle_log)
>> >                
>> > self.captcha_answer.connect(work.mmCaptchaAnswer)
>> >                 work.setAutoDelete(True)
>> >                
QtCore.QThreadPool.globalInstance().start(work)
>>
>> > On last line of code ( QtCore.QThreadPool.globalInstance().start
>> > (work) ) i get an error:
>>
>> > SystemError: error return without exception set
>>
>> > What is wrong in my code??? Any advise???
>>
>> The error is on C-level. AFAIK it occurs when a Python-C-function
>> returns "NULL" without setting an exception.
>>
>> It's hard to say where it really occurs. I'd use a debug-build of PyQt,
>> and possibly Python, and then investigate using gdb.
>>
>> Alternatively, what happens when you do some "dummy"-work that doesn't
>> use signals and no other libraries?
>>
>> Diez
> 
> About some "dummy" code:
> 
> #!/usr/bin/env python
> # -*- coding: utf-8 -*-
> 
> import sys
> import os
> import time
> 
> from PyQt4 import QtCore, QtGui
> 
> class Job(QtCore.QRunnable):
>   def __init__(self, name):
>   QtCore.QRunnable.__init__(self)
>   self._name = name
> 
>   def run(self):
>   time.sleep(10)
>   print self._name
> 
>   def autoDelete(self):
>   return self._auto
> 
>   def setAutoDelete(self, auto):
>   self._auto = auto
> 
> 
> 
> if __name__ == "__main__":
> 
>   app = QtGui.QApplication(sys.argv)
> 
>   QtCore.QThreadPool.globalInstance().setMaxThreadCount(1)
> 
>   j = Job("Job-1")
>   j.setAutoDelete(True)
>   QtCore.QThreadPool.globalInstance().start(j)
> 
> 
> Even this code not work. On the last line of code
> ( QtCore.QThreadPool.globalInstance().start(j) ) i get the error:
> 
>>> An unhandled win32 exception occured in python.exe

You aren't letting the thread run before exiting the program. Try adding...

app.exec_()

...after you call start().

Also, I'm not sure what you are trying to achieve with your implementations
of autoDelete() and setAutoDelete().

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


Re: a huge shared read-only data in parallel accesses -- How? multithreading? multiprocessing?

2010-01-07 Thread Klauss
On Dec 31 2009, 6:36 pm, garyrob  wrote:
> One thing I'm not clear on regarding Klauss' patch. He says it's
> applicable where the data is primarily non-numeric. In trying to
> understand why that would be the case, I'm thinking that the increased
> per-object memory overhead for reference-counting would outweigh the
> space gains from the shared memory.
>
> Klauss's test code stores a large number of dictionaries which each
> contain just 3 items. The stored items are strings, but short ones...
> it looks like they take up less space than double floats(?).
>
> So my understanding is that the point is that the overhead for the
> dictionaries is big enough that the patch is very helpful even though
> the stored items are small. And that the patch would be less and less
> effective as the number of items stored in each dictionary became
> greater and greater, until eventually the patch might do more use more
> space for reference counting than it saved by shared memory.

Not really.
The real difference is that numbers (ints and floats) are allocated
out of small contiguous pools. So even if a great percentage of those
objects would remain read-only, there's probably holes in those pools
left by the irregular access pattern during initialization, and those
holes would be written to eventually as the pool gets used.

In essence, those pools aren't read-only for other reasons than
reference counting.

Dictionaries, tuples and lists (and many other types) don't exhibit
that behavior.
-- 
http://mail.python.org/mailman/listinfo/python-list


Threading change, 2.5.4 -> 2.6.1

2010-01-07 Thread Gib Bogle
The code below runs with Python 2.5.4, but gives the following error messages 
with Python 2.6.1.  What needs to be done to make it work?  Thanks.


C:\Summer09\Tutorials>python url_queue.pyw
Traceback (most recent call last):
File "url_queue.pyw", line 3, in 
import threading
File "C:\Summer09\Tutorials\threading.py", line 9, in 
class ProcessingThread(threading.Thread, QtCore.QObject):
AttributeError: 'module' object has no attribute 'Thread'


url_queue.py


#!/usr/bin/env python
import Queue
import threading
import urllib2
import time

hosts = ["http://yahoo.com";, "http://google.com";, "http://amazon.com";, 
"http://ibm.com";, "http://apple.com";]


queue = Queue.Queue()

class ThreadUrl(threading.Thread):
#"""Threaded Url Grab"""
  def __init__(self, queue,i):
threading.Thread.__init__(self)
self.queue = queue
self.num = i
print "Thread: ",self.num

  def run(self):
while True:
  #grabs host from queue
  host = self.queue.get()
  print "num, host: ",self.num,host
  #grabs urls of hosts and prints first 1024 bytes of page
  url = urllib2.urlopen(host)
  print url.read(1024)

  #signals to queue job is done
  self.queue.task_done()

start = time.time()
def main():

  #spawn a pool of threads, and pass them queue instance
  for i in range(5):
t = ThreadUrl(queue,i)
t.setDaemon(True)
t.start()

  #populate queue with data
  for host in hosts:
queue.put(host)

 #wait on the queue until everything has been processed
queue.join()

main()
print "Elapsed Time: %s" % (time.time() - start)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Regex help needed!

2010-01-07 Thread Rolando Espinoza La Fuente
# http://gist.github.com/271661

import lxml.html
import re

src = """
lksjdfls  kdjff lsdfs  sdjfls sdfsdwelcome
hello, my age is 86 years old and I was born in 1945. Do you know
that
PI is roughly 3.1443534534534534534 """

regex = re.compile('amazon_(\d+)')

doc = lxml.html.document_fromstring(src)

for div in doc.xpath('//div[starts-with(@id, "amazon_")]'):
match = regex.match(div.get('id'))
if match:
print match.groups()[0]



On Thu, Jan 7, 2010 at 4:42 PM, Aahz  wrote:
> In article 
> <19de1d6e-5ba9-42b5-9221-ed7246e39...@u36g2000prn.googlegroups.com>,
> Oltmans   wrote:
>>
>>I've written this regex that's kind of working
>>re.findall("\w+\s*\W+amazon_(\d+)",str)
>>
>>but I was just wondering that there might be a better RegEx to do that
>>same thing. Can you kindly suggest a better/improved Regex. Thank you
>>in advance.
>
> 'Some people, when confronted with a problem, think "I know, I'll use
> regular expressions."  Now they have two problems.'
> --Jamie Zawinski
>
> Take the advice other people gave you and use BeautifulSoup.
> --
> Aahz ([email protected])           <*>         http://www.pythoncraft.com/
>
> "If you think it's expensive to hire a professional to do the job, wait
> until you hire an amateur."  --Red Adair
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Rolando Espinoza La fuente
www.rolandoespinoza.info
-- 
http://mail.python.org/mailman/listinfo/python-list


One function calling another defined in the same file being exec'd

2010-01-07 Thread Mitchell L Model

[Python 3.1]

I thought I thoroughly understood eval, exec, globals, and locals, but I
encountered something bewildering today. I have some short files I  
want to
exec. (Users of my application write them, and the application gives  
them a
command that opens a file dialog box and execs the chosen file. Users  
are

expected to be able to write simple Python scripts, including function
definitions. Neither security nor errors are relevant for the purposes  
of this

discussion, though I do deal with them in my actual code.)

Here is a short piece of code to exec a file and report its result.  
(The file

being exec'd must assign 'result'.)

def dofile(filename):
ldict = {'result': None}
with open(filename) as file:
exec(file.read(), globals(), ldict)
print('Result for {}: {}'.format(filename, ldict['result']))

First I call dofile() on a file containing the following:


def fn(arg):
return sum(range(arg))

result = fn(5)


The results are as expected.

Next I call dofile() on a slightly more complex file, in which one  
function

calls  another function defined earlier in the same file.


def fn1(val):
return sum(range(val))

def fn2(arg):
return fn1(arg)

result = fn2(5)


This produces a surprise:

NameError: global name 'fn1' is not defined

[1] How is it that fn2 can be called from the top-level of the script  
but fn1

cannot be called from fn2?

[2] Is this correct behavior or is there something wrong with Python  
here?


[3] How should I write a file to be exec'd that defines several  
functions that

call each other, as in the trivial fn1-fn2 example above?



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


Re: Threading change, 2.5.4 -> 2.6.1

2010-01-07 Thread anon

Gib Bogle wrote:
The code below runs with Python 2.5.4, but gives the following error 
messages with Python 2.6.1.  What needs to be done to make it work?  
Thanks.


C:\Summer09\Tutorials>python url_queue.pyw
Traceback (most recent call last):
File "url_queue.pyw", line 3, in 
import threading
File "C:\Summer09\Tutorials\threading.py", line 9, in 
class ProcessingThread(threading.Thread, QtCore.QObject):
AttributeError: 'module' object has no attribute 'Thread'



Nothing to do with the version of python

You have a file called threading.py in C:\Summer09\Tutorials (the 
same folder as url_queue.pyw). The line 'import threading' is 
finding this module before python's threading module.


Rename this file and you should be fine.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Dictionary used to build a Triple Store

2010-01-07 Thread Lee

Lee wrote:

Definitely a newbie question, so please bear with  me.

I'm reading "Programming the Semantic Web" by Segaran, Evans, and Tayor.

It's about the Semantic Web BUT it uses python to build a "toy" triple 
store claimed to have good performance in the "tens of thousands" of 
triples.


Just in case anybody doesnt know what an RDF triple is (not that it 
matters for my question) think of it as an ordered 3 tuple representing 
a Subject, a Predicate, and an Object eg: (John, loves, Mary) (Mary, 
has-a, lamb) {theSky, has-color,blue}


To build the triple store entirely in Python, the authors recommend
using the Python hash. Three hashes actually (I get that. You
want to have a hash with the major index being the Subject in one hash,
the Predicate in another hash, or the Object for the third hash)

He creates a class SimpleGraph which initializes itself by setting the
three hashes names _spo, _pos, and _osp thus

class SimpleGraph;
  def __init__(self);
 self._spo={};
 self._pos=();
 self._osp={};

So far so good. I get the convention with the double underbars for the 
initializer but


Q1: Not the main question but while I'm hereI'm a little fuzzy on 
the convention about the use of the single underbar in the definition of 
the hashes. Id the idea to "underbar" all objects and methods that 
belong to the class? Why do that?


But now the good stuff:

Our authors define the hashes thus: (showing only one of the three 
hashes because they're all the same idea)


self._pos = {predicate:{object:set( [subject] ) }}

Q2: Wha? Two surprises ...
   1) Why not {predicate:{object:subject}} i.e. 
pos[predicate][object]=subjectwhy the set( [object] ) construct? 
putting the object into a list and turning the list into a set to be the 
"value" part of a name:value pair. Why not just use the naked subject 
for the value?


   2) Why not something like pos[predicate][object][subject] = 1 .or 
any constant. The idea being to create the set of three indexes. If the 
triple exists in the hash, its "in" your tripple store. If not, then 
there's no such triple.




OK, Thanx. That cleares things up.

I had forgotton that once you have a "Cell" in a 2D matrix to represent 
the first two components of the 3 tuple, you then want multiple values 
IN the cell for the possibly multi valued  third component.

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


Re: Threading change, 2.5.4 -> 2.6.1

2010-01-07 Thread MRAB

Gib Bogle wrote:
The code below runs with Python 2.5.4, but gives the following error 
messages with Python 2.6.1.  What needs to be done to make it work?  
Thanks.


C:\Summer09\Tutorials>python url_queue.pyw
Traceback (most recent call last):
File "url_queue.pyw", line 3, in 
import threading
File "C:\Summer09\Tutorials\threading.py", line 9, in 
class ProcessingThread(threading.Thread, QtCore.QObject):
AttributeError: 'module' object has no attribute 'Thread'


[snip]
url_queue.pyw is trying to import Python's "threading" module, but it's
finding the threading.py script in your "Tutorials" folder first.

Renaming your tutorial script to something other than "threading.py".
--
http://mail.python.org/mailman/listinfo/python-list


Re: PyQt QThreadPool error

2010-01-07 Thread h0uk
On 8 янв, 03:02, Phil Thompson  wrote:
> On Thu, 7 Jan 2010 13:03:24 -0800 (PST), h0uk 
> wrote:
>
> > On 8 янв, 01:02, "Diez B. Roggisch"  wrote:
> >> h0uk schrieb:
>
> >> > Hello.
>
> >> > I have the following code:
>
> >> >             #workers = {}
> >> >             QtCore.QThreadPool.globalInstance().setExpiryTimeout
> >> > (30)
> >> >            
> >> > QtCore.QThreadPool.globalInstance().setMaxThreadCount(1)
> >> >             for i in range(1, int(userscnt) + 1):
> >> >                 work = wk.Worker(i)
> >> >                 # connect signals
> >> >                
> >> > work.mmShowCaptcha.connect(self.show_captcha_dlg)
> >> >                 work.log.connect(self.handle_log)
> >> >                
> >> > self.captcha_answer.connect(work.mmCaptchaAnswer)
> >> >                 work.setAutoDelete(True)
> >> >                
>
> QtCore.QThreadPool.globalInstance().start(work)
>
>
>
>
>
> >> > On last line of code ( QtCore.QThreadPool.globalInstance().start
> >> > (work) ) i get an error:
>
> >> > SystemError: error return without exception set
>
> >> > What is wrong in my code??? Any advise???
>
> >> The error is on C-level. AFAIK it occurs when a Python-C-function
> >> returns "NULL" without setting an exception.
>
> >> It's hard to say where it really occurs. I'd use a debug-build of PyQt,
> >> and possibly Python, and then investigate using gdb.
>
> >> Alternatively, what happens when you do some "dummy"-work that doesn't
> >> use signals and no other libraries?
>
> >> Diez
>
> > About some "dummy" code:
>
> > #!/usr/bin/env python
> > # -*- coding: utf-8 -*-
>
> > import sys
> > import os
> > import time
>
> > from PyQt4 import QtCore, QtGui
>
> > class Job(QtCore.QRunnable):
> >    def __init__(self, name):
> >            QtCore.QRunnable.__init__(self)
> >            self._name = name
>
> >    def run(self):
> >            time.sleep(10)
> >            print self._name
>
> >    def autoDelete(self):
> >            return self._auto
>
> >    def setAutoDelete(self, auto):
> >            self._auto = auto
>
> > if __name__ == "__main__":
>
> >    app = QtGui.QApplication(sys.argv)
>
> >    QtCore.QThreadPool.globalInstance().setMaxThreadCount(1)
>
> >    j = Job("Job-1")
> >    j.setAutoDelete(True)
> >    QtCore.QThreadPool.globalInstance().start(j)
>
> > Even this code not work. On the last line of code
> > ( QtCore.QThreadPool.globalInstance().start(j) ) i get the error:
>
> >>> An unhandled win32 exception occured in python.exe
>
> You aren't letting the thread run before exiting the program. Try adding...
>
>     app.exec_()
>
> ...after you call start().
>
> Also, I'm not sure what you are trying to achieve with your implementations
> of autoDelete() and setAutoDelete().
>
> Phil

Phil you right about app.exec_(). But situation is sligthly different.

I want to have more than one Job. I add these Jobs into QThreadPool
trough cycle. And I also want these Jobs to run  sequentially.

The following code illustrate what I mean:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import os
import time

from PyQt4 import QtCore, QtGui

class Job(QtCore.QRunnable):
def __init__(self, name):
QtCore.QRunnable.__init__(self)
self._name = name

def run(self):
time.sleep(3)
print self._name


if __name__ == "__main__":

app = QtGui.QApplication(sys.argv)

QtCore.QThreadPool.globalInstance().setMaxThreadCount(1)

for i in range(5):
j = Job("Job-" + str(i))
j.setAutoDelete(True)
QtCore.QThreadPool.globalInstance().start(j, i)
app.exec_()

After 5 cycle I get the same error:  An unhandled win32 exception
occured in python.exe.

How I can do it?? To run my Jobs sequentially???

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


Accessing python from a network share in windows 7

2010-01-07 Thread aj
I access python from a network share. This works fine on XP but on
windows 7 it throws the following error:

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.
>>> import random
Traceback (most recent call last):
  File "", line 1, in 
  File "t:\win32\python-2.6.1\lib\random.py", line 871, in 
_inst = Random()
  File "t:\win32\python-2.6.1\lib\random.py", line 96, in __init__
self.seed(x)
  File "t:\win32\python-2.6.1\lib\random.py", line 110, in seed
a = long(_hexlify(_urandom(16)), 16)
WindowsError: [Error 127] The specified procedure could not be found

Is there some security policy that I need to enable/disable to use
python from a network on windows 7?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: One function calling another defined in the same file being exec'd

2010-01-07 Thread Mitchell L Model
I forgot to offer one answer for question [3] in what I just posted: I  
can define all the secondary functions inside one main one and just  
call the main one. That provides a separate local scope within the  
main function, with the secondary functions defined inside it when  
(each time) the main function is called. Not too bad, but will freak  
out my users and it doesn't seem as if it should be necessary to  
resort to this.

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


Re: One function calling another defined in the same file being exec'd

2010-01-07 Thread anon

Rather than exec the files, why not import them?

I can get both your examples to work using the 'imp' module.
http://docs.python.org/3.1/library/imp.html#module-imp

I used python 2.6.4. Note that 3.1 also has 'importlib' module.


import imp

# the name of the python file written by a user
name = 'test1'

fp, pathname, description = imp.find_module(name)
test1 = imp.load_module(name, fp, pathname, description)

print test1.result

# remember to close file (see docs)
fp.close()
--
http://mail.python.org/mailman/listinfo/python-list


Re: Accessing python from a network share in windows 7

2010-01-07 Thread MRAB

aj wrote:

I access python from a network share. This works fine on XP but on
windows 7 it throws the following error:

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.

import random

Traceback (most recent call last):
  File "", line 1, in 
  File "t:\win32\python-2.6.1\lib\random.py", line 871, in 
_inst = Random()
  File "t:\win32\python-2.6.1\lib\random.py", line 96, in __init__
self.seed(x)
  File "t:\win32\python-2.6.1\lib\random.py", line 110, in seed
a = long(_hexlify(_urandom(16)), 16)
WindowsError: [Error 127] The specified procedure could not be found

Is there some security policy that I need to enable/disable to use
python from a network on windows 7?


Is it a problem with the share or with Windows 7? Does it work with
Windows 7 when running a local copy of Python?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Accessing python from a network share in windows 7

2010-01-07 Thread aj
On Jan 7, 3:51 pm, MRAB  wrote:
> aj wrote:
> > I access python from a network share. This works fine on XP but on
> > windows 7 it throws the following error:
>
> > 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.
>  import random
> > Traceback (most recent call last):
> >   File "", line 1, in 
> >   File "t:\win32\python-2.6.1\lib\random.py", line 871, in 
> >     _inst = Random()
> >   File "t:\win32\python-2.6.1\lib\random.py", line 96, in __init__
> >     self.seed(x)
> >   File "t:\win32\python-2.6.1\lib\random.py", line 110, in seed
> >     a = long(_hexlify(_urandom(16)), 16)
> > WindowsError: [Error 127] The specified procedure could not be found
>
> > Is there some security policy that I need to enable/disable to use
> > python from a network on windows 7?
>
> Is it a problem with the share or with Windows 7? Does it work with
> Windows 7 when running a local copy of Python?

It works without any issue on win7 if I copy python to my local drive.
Also, accessing python from the same network share works fine on win
XP. So I am suspecting some security policy of win7 that is causing
problem while accessing it over a network share.
-- 
http://mail.python.org/mailman/listinfo/python-list


PIL show() not working for 2nd pic

2010-01-07 Thread suresh.amritapuri
Hi

I am using PIL for image processing in ubuntu 9.04. When i give two
im.show() commands for two different images, the second image is not
displayed (eye of gnome is the display program). It says no such file
or directory. Any ideas?

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


Re: Threading change, 2.5.4 -> 2.6.1

2010-01-07 Thread Gib Bogle

MRAB wrote:

Gib Bogle wrote:
The code below runs with Python 2.5.4, but gives the following error 
messages with Python 2.6.1.  What needs to be done to make it work?  
Thanks.


C:\Summer09\Tutorials>python url_queue.pyw
Traceback (most recent call last):
File "url_queue.pyw", line 3, in 
import threading
File "C:\Summer09\Tutorials\threading.py", line 9, in 
class ProcessingThread(threading.Thread, QtCore.QObject):
AttributeError: 'module' object has no attribute 'Thread'


[snip]
url_queue.pyw is trying to import Python's "threading" module, but it's
finding the threading.py script in your "Tutorials" folder first.

Renaming your tutorial script to something other than "threading.py".


Thanks very much to you both.  It's actually not in my folder, it is 
(presumably) in my student's folder (I'm the one using 2.5.4, she has 2.6.1 and 
thought this could be the problem.)

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


How do I access what's in this module?

2010-01-07 Thread Fencer

Hello, look at this lxml documentation page:
http://codespeak.net/lxml/api/index.html

How do I access the functions and variables listed?

I tried from lxml.etree import ElementTree and the import itself seems 
to pass without complaint by the python interpreter but I can't seem to 
access anything in ElementTree, not the functions or variables. What is 
the proper way to import that module?


For example:
>>> from lxml.etree import ElementTree
>>> ElementTree.dump(None)
Traceback (most recent call last):
  File "", line 1, in 

Also, can I access those items that begin with an underscore if I get 
the import sorted?


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


Re: Where's a DOM builder that uses the Builder Pattern to ... build DOMs?

2010-01-07 Thread Stephen Hansen
On Thu, Jan 7, 2010 at 8:44 AM, Phlip  wrote:

> On Jan 7, 5:36 am, Stefan Behnel  wrote:
>
> > Well, then note that there are tons of ways to generate XML with Python,
> > including the one I pointed you to.
>
> from lxml.html import builder as E
>xml = E.foo()
>
> All I want is "", but I get "AttributeError: 'module' object has
> no attribute 'foo'".
>
> A peek at dir(E) shows it only has HTML tags, all hard coded.
>
> So how to get it to generate any random XML tag my clients think of?
>

If you want to generate random XML, don't use the HTML sub-module of lxml.
Its a specific sub-set of functionality for HTML only documents.

>>> from lxml.builder import ElementMaker
>>> from lxml import etree
>>> E = ElementMaker()
>>> html = E.html(
... E.form(
... E.input(type="text", name="email"),
... E.input(type="text", name="blah")
... )
... )
>>> etree.tostring(html)
''

HTH,

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


Re: One function calling another defined in the same file being exec'd

2010-01-07 Thread Steven D'Aprano
On Thu, 07 Jan 2010 17:47:13 -0500, Mitchell L Model wrote:

> Next I call dofile() on a slightly more complex file, in which one
> function calls  another function defined earlier in the same file.
> 
> 
> def fn1(val):
>  return sum(range(val))
> 
> def fn2(arg):
>  return fn1(arg)
> 
> result = fn2(5)
> 
> 
> This produces a surprise:
> 
>  NameError: global name 'fn1' is not defined
> 
> [1] How is it that fn2 can be called from the top-level of the script
> but fn1 cannot be called from fn2?


This might help you to see what's going on. Define your own cut-down 
version of the global namespace, and a local namespace, and a string to 
execute:


myglobals = {'__builtins__': None, 'globals': globals, 'locals': locals, 
'print': print}
mylocals = {'result': None}
s = """def f():
print("Globals inside f:", globals())
print("Locals inside f:", locals())

print("Globals at the top level:", globals())
print("Locals at the top level:", locals())
f()
"""

exec(s, myglobals, mylocals)



And this is what you should see:


Globals at the top level: {'__builtins__': None, 'print': , 'globals': , 'locals': }
Locals at the top level: {'result': None, 'f': }
Globals inside f: {'__builtins__': None, 'print': , 'globals': , 'locals': }
Locals inside f: {}


Does that clarify what's going on?


> [2] Is this correct behavior or is there something wrong with Python
> here?

This certainly surprised me too. I don't know if it is correct or not, 
but it goes back to at least Python 2.5.



> [3] How should I write a file to be exec'd that defines several
> functions that call each other, as in the trivial fn1-fn2 example above?

My preference would be to say, don't use exec, just import the module. 
Put responsibility on the user to ensure that they set a global "result", 
and then just do this:

mod = __import__('user_supplied_file_name')
result = mod.result


But if that's unworkable for you, then try simulating the namespace setup 
at the top level of a module. The thing to remember is that in the top 
level of a module:

>>> globals() is locals()
True

so let's simulate that:


myglobals = {'result': None}  # You probably also want __builtins__
s = """def f():
return g() + 1

def g():
return 2

result = f()
"""
exec(s, myglobals, myglobals)
myglobals['result']


This works for me.


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


Re: How do I access what's in this module?

2010-01-07 Thread John Machin
On Jan 8, 12:21 pm, Fencer 
wrote:
> Hello, look at this lxml documentation 
> page:http://codespeak.net/lxml/api/index.html

That's for getting details about an object once you know what object
you need to use to do what. In the meantime, consider reading the
tutorial and executing some of the examples:
http://codespeak.net/lxml/tutorial.html

> How do I access the functions and variables listed?
>
> I tried from lxml.etree import ElementTree and the import itself seems
> to pass without complaint by the python interpreter but I can't seem to
> access anything in ElementTree, not the functions or variables. What is
> the proper way to import that module?
>
> For example:
>  >>> from lxml.etree import ElementTree
>  >>> ElementTree.dump(None)
> Traceback (most recent call last):
>    File "", line 1, in 

lxml.etree is a module. ElementTree is effectively a class. The error
message that you omitted to show us might have given you a clue.

To save keystrokes you may like to try
from lxml import etree as ET
and thereafter refer to the module as "ET"

| >>> from lxml import etree as ET
| >>> type(ET)
| 
| >>> type(ET.ElementTree)
| 
| >>> help(ET.ElementTree)
| Help on built-in function ElementTree in module lxml.etree:
|
| ElementTree(...)
| ElementTree(element=None, file=None, parser=None)
|
| ElementTree wrapper class.

> Also, can I access those items that begin with an underscore if I get
> the import sorted?

Using pommy slang like "sorted" in an IT context has the potential to
confuse your transatlantic correspondents :-)

Can access? Yes. Should access? The usual Python convention is that an
object whose name begins with an underscore should be accessed only
via a documented interface (or, at your own risk, if you think you
know what you are doing).

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


Ask how to use HTMLParser

2010-01-07 Thread Water Lin

I am a new guy to use Python, but I want to parse a html page now. I
tried to use HTMLParse. Here is my sample code:
--
from HTMLParser import HTMLParser
from urllib2 import urlopen

class MyParser(HTMLParser):
title = ""
is_title = ""
def __init__(self, url):
HTMLParser.__init__(self)
req = urlopen(url)
self.feed(req.read())

def handle_starttag(self, tag, attrs):
if tag == 'div' and attrs[0][1] == 'articleTitle':
print "Found link => %s" % attrs[0][1]
self.is_title = 1

def handle_data(self, data):
if self.is_title:
print "here"
self.title = data
print self.title
self.is_title = 0
---

For the tag
---
open article title
---

I use my code to parse it. I can locate the div tag but I don't know how
to get the text for the tag which is "open article title" in my example.

How can I get the html content? What's wrong in my handle_data function?

Thanks

Water Lin

-- 
Water Lin's notes and pencils: http://en.waterlin.org
Email: [email protected]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I access what's in this module?

2010-01-07 Thread Fencer

On 2010-01-08 04:40, John Machin wrote:



For example:
  >>>  from lxml.etree import ElementTree
  >>>  ElementTree.dump(None)
Traceback (most recent call last):
File "", line 1, in


lxml.etree is a module. ElementTree is effectively a class. The error
message that you omitted to show us might have given you a clue.


But I did show the error message? It's just above what you just wrote. I 
try to include all relevant information in my posts.




Using pommy slang like "sorted" in an IT context has the potential to
confuse your transatlantic correspondents :-)


Ah, of course! :-)



Can access? Yes. Should access? The usual Python convention is that an
object whose name begins with an underscore should be accessed only
via a documented interface (or, at your own risk, if you think you
know what you are doing).


It turns out I no longer want to access anything in there but I thank 
you for your information nontheless.




HTH,
John


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


Re: How do I access what's in this module?

2010-01-07 Thread John Machin
On Jan 8, 2:45 pm, Fencer 
wrote:
> On 2010-01-08 04:40, John Machin wrote:
>
>
>
> >> For example:
> >>   >>>  from lxml.etree import ElementTree
> >>   >>>  ElementTree.dump(None)
> >> Traceback (most recent call last):
> >>     File "", line 1, in
>
> > lxml.etree is a module. ElementTree is effectively a class. The error
> > message that you omitted to show us might have given you a clue.
>
> But I did show the error message? It's just above what you just wrote. I
> try to include all relevant information in my posts.


Traceback (most recent call last):
   File "", line 1, in 

Also, can I access those items ...


Error message should appear after line starting with "File". Above
excerpt taken from google groups; identical to what shows in
http://news.gmane.org/gmane.comp.python.general ... what are you
looking at?

With Windows XP and Python 2.5.4 I get:

Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'builtin_function_or_method' object has no attribute
'dump'

> It turns out I no longer want to access anything in there but I thank
> you for your information nontheless.

You're welcome -- the advice on _methods is portable :-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Ask how to use HTMLParser

2010-01-07 Thread h0uk
On 8 янв, 08:44, Water Lin  wrote:
> I am a new guy to use Python, but I want to parse a html page now. I
> tried to use HTMLParse. Here is my sample code:
> --
> from HTMLParser import HTMLParser
> from urllib2 import urlopen
>
> class MyParser(HTMLParser):
>     title = ""
>     is_title = ""
>     def __init__(self, url):
>         HTMLParser.__init__(self)
>         req = urlopen(url)
>         self.feed(req.read())
>
>     def handle_starttag(self, tag, attrs):
>         if tag == 'div' and attrs[0][1] == 'articleTitle':
>             print "Found link => %s" % attrs[0][1]
>             self.is_title = 1
>
>     def handle_data(self, data):
>         if self.is_title:
>             print "here"
>             self.title = data
>             print self.title
>             self.is_title = 0
> ---
>
> For the tag
> ---
> open article title
> ---
>
> I use my code to parse it. I can locate the div tag but I don't know how
> to get the text for the tag which is "open article title" in my example.
>
> How can I get the html content? What's wrong in my handle_data function?
>
> Thanks
>
> Water Lin
>
> --
> Water Lin's notes and pencils:http://en.waterlin.org
> Email: [email protected]

Hi.

Have you get errors or anything else??? What is wrong??

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


Re: Ask how to use HTMLParser

2010-01-07 Thread h0uk
On 8 янв, 08:44, Water Lin  wrote:
> I am a new guy to use Python, but I want to parse a html page now. I
> tried to use HTMLParse. Here is my sample code:
> --
> from HTMLParser import HTMLParser
> from urllib2 import urlopen
>
> class MyParser(HTMLParser):
>     title = ""
>     is_title = ""
>     def __init__(self, url):
>         HTMLParser.__init__(self)
>         req = urlopen(url)
>         self.feed(req.read())
>
>     def handle_starttag(self, tag, attrs):
>         if tag == 'div' and attrs[0][1] == 'articleTitle':
>             print "Found link => %s" % attrs[0][1]
>             self.is_title = 1
>
>     def handle_data(self, data):
>         if self.is_title:
>             print "here"
>             self.title = data
>             print self.title
>             self.is_title = 0
> ---
>
> For the tag
> ---
> open article title
> ---
>
> I use my code to parse it. I can locate the div tag but I don't know how
> to get the text for the tag which is "open article title" in my example.
>
> How can I get the html content? What's wrong in my handle_data function?
>
> Thanks
>
> Water Lin
>
> --
> Water Lin's notes and pencils:http://en.waterlin.org
> Email: [email protected]

I want to say your code works well
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Need help to pass self.count to other classes.

2010-01-07 Thread Steven D'Aprano
On Wed, 06 Jan 2010 08:56:23 -0500, Steve Holden wrote:

> This is untested code (some days I don't seem to write any other kind
> ...) but it should give you the flavor:
> 
> class kbInterface(object):
> def __init__(self):
> self.zxc = 0
> def prompt1(self):
> self.count += 1
> return "[%d]> "
> def prompt2(self):
> l = len(str(self.count))+1
> return "%s " % "."*l
> def dhook(self, value):
> print "[%d out]" % self.count
> def ehook(self, type, value, trace):
> print "[%d err]\n" % value
> 
> kbi = kbInterface()
> sys.ps1 = kbi.prompt1
> sys.ps2 = kbi.prompt2
> sys.displayhook = kbi.dhook
> sys.excepthook = kbi.ehook


Unfortunately this won't do what you expect, because sys.ps1 and ps2 
should be either strings, or objects with a __str__ method. They aren't 
called to generate the prompt.

(After fixing the typo with self.count vs self.zxc)


>>> kbi = kbInterface()
>>> sys.ps1 = kbi.prompt1
>print "Hello"
Hello
>



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


Clarifications on compiling for Windows

2010-01-07 Thread peteshinners
My presentation for Pycon is coming together, but I need to make sure
my information about compiling Python and Python extensions for
Windows is correct. I'm really only experienced with this on the Linux
side of things.

First of all, is the Windows FAQ fairly up to date? Should people be
referring to section 6 if they are going to build an application with
an embedded Python interpreter?
http://www.python.org/doc/faq/windows/#how-can-i-embed-python-into-a-windows-application

If I understand correctly, compiled extensions for Python on Windows
should match the compiler that was used to build the interpreter
itself? Is there a list somewhere that shows which version of msvc was
used to compile the recent Python binaries?

Thank you for feedback. I definitely want to make sure I have this
correct before telling anybody else?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Ask how to use HTMLParser

2010-01-07 Thread Water Lin
h0uk  writes:

> On 8 янв, 08:44, Water Lin  wrote:
>> I am a new guy to use Python, but I want to parse a html page now. I
>> tried to use HTMLParse. Here is my sample code:
>> --
>> from HTMLParser import HTMLParser
>> from urllib2 import urlopen
>>
>> class MyParser(HTMLParser):
>>     title = ""
>>     is_title = ""
>>     def __init__(self, url):
>>         HTMLParser.__init__(self)
>>         req = urlopen(url)
>>         self.feed(req.read())
>>
>>     def handle_starttag(self, tag, attrs):
>>         if tag == 'div' and attrs[0][1] == 'articleTitle':
>>             print "Found link => %s" % attrs[0][1]
>>             self.is_title = 1
>>
>>     def handle_data(self, data):
>>         if self.is_title:
>>             print "here"
>>             self.title = data
>>             print self.title
>>             self.is_title = 0
>> ---
>>
>> For the tag
>> ---
>> open article title
>> ---
>>
>> I use my code to parse it. I can locate the div tag but I don't know how
>> to get the text for the tag which is "open article title" in my example.
>>
>> How can I get the html content? What's wrong in my handle_data function?
>>
>> Thanks
>>
>> Water Lin
>>
>> --
>> Water Lin's notes and pencils:http://en.waterlin.org
>> Email: [email protected]
>
> I want to say your code works well

But in handle_data I can't print self.title. I don't why I can't set the
self.title in handle_data.

Thanks

Water Lin

-- 
Water Lin's notes and pencils: http://en.waterlin.org
Email: [email protected]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Clarifications on compiling for Windows

2010-01-07 Thread Mensanator
On Jan 8, 12:19 am, peteshinners  wrote:
> My presentation for Pycon is coming together, but I need to make sure
> my information about compiling Python and Python extensions for
> Windows is correct. I'm really only experienced with this on the Linux
> side of things.
>
> First of all, is the Windows FAQ fairly up to date? Should people be
> referring to section 6 if they are going to build an application with
> an embedded Python 
> interpreter?http://www.python.org/doc/faq/windows/#how-can-i-embed-python-into-a-...
>
> If I understand correctly, compiled extensions for Python on Windows
> should match the compiler that was used to build the interpreter
> itself? Is there a list somewhere that shows which version of msvc was
> used to compile the recent Python binaries?
>
> Thank you for feedback. I definitely want to make sure I have this
> correct before telling anybody else?

You aren't going to try it?
-- 
http://mail.python.org/mailman/listinfo/python-list