Re: Bug in Python

2012-04-17 Thread Terry Reedy

On 4/18/2012 12:47 AM, Hans Mulder wrote:

On 18/04/12 03:08:08, Kiuhnm wrote:

print(1)
print(2)
print(3)

with open('test') as f:
 data = f.read()
with open('test') as f:
 data = f.read()


How much of that is needed to trigger the problem?
All three prints? Any of them?


I get the same result with Pythin 3.3.0a0 on MacOS X 10.6:

93>  ./python.exe -m pdb /tmp/script.py

/tmp/script.py(1)()

->  print(1)
(Pdb) j 7
Assertion failed: (blockstack_top>  0), function frame_setlineno, file
Objects/frameobject.c, line 208.
Abort trap

The assertion that fails is in this code fragment:

 case POP_BLOCK:
 assert(blockstack_top>  0);

This looks like a genuine bug in the CPythin interpreter to me.


Looks like something to report on the tracker bugs.python.org
If either of you do that, please include details above.

--
Terry Jan Reedy

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


Re: Bug in Python

2012-04-17 Thread John O'Hagan
On Wed, 18 Apr 2012 03:08:08 +0200
Kiuhnm  wrote:

> I'm using Python 3.2.2, 64 bit on Windows 7.
> 
> Consider this code:
> --->
> print(1)
> print(2)
> print(3)
> 
> with open('test') as f:
>  data = f.read()
> with open('test') as f:
>  data = f.read()
> <---
> If I debug this code with
>python -m pdb script.py
> and I issue the command
>j 7
> Python crashes.

I get this with 2.7.2 and 3.2.3 on Debian.

I did some experiments:

The "script" file can contain as little as

with 1 as f:
pass
pass

and the segfault will occur if 'j 3' is issued. It seems jumping anywhere within
the file beyond the context manager will trigger it. 

With 2.7 the segfault happens consistently. With 3.2, it's complicated: it
occurs every nth time 'j 3' is issued where n < 5 or thereabouts. However, if it
doesn't happen when the command is issued, it happens when 'exit' is called.

Hopefully some of this is a clue.

Regards,

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


Re: Bug in Python

2012-04-17 Thread Hans Mulder
On 18/04/12 03:08:08, Kiuhnm wrote:
> print(1)
> print(2)
> print(3)
> 
> with open('test') as f:
> data = f.read()
> with open('test') as f:
> data = f.read()

I get the same result with Pythin 3.3.0a0 on MacOS X 10.6:

93> ./python.exe -m pdb /tmp/script.py
> /tmp/script.py(1)()
-> print(1)
(Pdb) j 7
Assertion failed: (blockstack_top > 0), function frame_setlineno, file
Objects/frameobject.c, line 208.
Abort trap

The assertion that fails is in this code fragment:

case POP_BLOCK:
assert(blockstack_top > 0);


This looks like a genuine bug in the CPythin interpreter to me.

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


Re: Framework for a beginner

2012-04-17 Thread Bryan
Roy Smith wrote:
> Bryan wrote:
> > Django has emphasized backwards compatibility with the
> > down-side that, last I heard, there was no plan to move to Python 3.
>
> Hardly.  Seehttps://www.djangoproject.com/weblog/2012/mar/13/py3k/

Ah, I'm behind the times again. Thanks, that's good news.

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


Bug in Python

2012-04-17 Thread Kiuhnm

I'm using Python 3.2.2, 64 bit on Windows 7.

Consider this code:
--->
print(1)
print(2)
print(3)

with open('test') as f:
data = f.read()
with open('test') as f:
data = f.read()
<---
If I debug this code with
  python -m pdb script.py
and I issue the command
  j 7
Python crashes.

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


Re: How to determine if IO redirection is occurring with the output from a Python program?

2012-04-17 Thread Chris Angelico
On Wed, Apr 18, 2012 at 12:59 AM, Edward d'Auvergne
 wrote:
> Hi,
>
> Thanks for the incredibly quick responses!  The os.isatty() call (and
> sys.std*.isatty() calls as suggested by Chris Angelico at
> http://mail.python.org/pipermail/python-list/2012-April/1291048.html)
> work perfectly for this job!  I might have to do some testing later on
> Windows though to see what happens when the ansi escape characters are
> not supported.

isatty() is supported on Windows (the underlying C API is different,
but the beauty of a high-level language is that you no longer need to
care), but the standard Windows console doesn't support ANSI
sequences. I think there is a way to enable them, but I don't recall
it off hand. However, if you can pipe your output through a socket
connection, a MUD client can be your console. There are plenty around;
RosMud [1] is a quite light-weight one that I wrote some years ago,
and use constantly. Going for a MUD connection may feel a little
weird, but it's pretty easy, and it gives you instant cross-platform
networking capabilities.

ChrisA
[1] http://www.kepl.com.au/esstu/rosmud.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Working with Cursors

2012-04-17 Thread nn
On Apr 17, 2:11 pm, timlash  wrote:
> Searched the web and this forum without satisfaction.  Using Python 2.7 and 
> pyODBC on Windows XP I can get the code below to run and generate two cursors 
> from two different databases without problems.  Ideally, I'd then like to 
> join these result cursors thusly:
>
> SELECT a.state, sum(b.Sales) FROM cust_curs a INNER JOIN fin_curs b ON 
> a.Cust_id = b.Cust_id GROUP BY a.state
>
> Is there a way to join cursors using SQL statements in python or pyODBC?  
> Would I need to store these cursors in a common DB (SQLite3?) to accomplish 
> this?  Is there a pure python data handling approach that would generate this 
> summary from these two cursors?
>
> Thanks for your consideration.
>
> Working code:
>
> import pyodbc
>
> #
> # DB2 Financial Data Cursor
> #
> cnxn = pyodbc.connect('DSN=DB2_Fin;UID=;PWD=')
> fin_curs = cnxn.cursor()
>
> fin_curs.execute("""SELECT Cust_id, sum(Sales) as Sales
>                         FROM Finance.Sales_Tbl
>                         GROUP BY Cust_id""")
>
> #
> # Oracle Customer Data Cursor
> #
> cnxn = pyodbc.connect('DSN=Ora_Cust;UID=;PWD=')
> cust_curs = cnxn.cursor()
>
> cust_curs.execute("""SELECT Distinct Cust_id, gender, address, state
>                         FROM Customers.Cust_Data""")

If any of the two cursors fits in memory you could store it in a dict
and then look the extra data up as you traverse the second one. E.g.

>sales = {}
>for fin_curs_row in fin_curs:
>fin_cust_id,sales = fin_curs_row
>sales[fin_cust_id] = sales
>for cust_curs_row in cust_curs:
>cust_cust_id = cust_curs_row[0]
>print cust_curs_row, sales[cust_cust_id]

If you can't make it fit in memory, one way would be to order both
cursors by customer_id and pair them up as they come along:

>fin_curs_row = fin_curs.next()
>cust_curs_row = cust_curs.next()
>While True:
>fin_cust_id,sales = fin_curs_row
>cust_cust_id = cust_curs_row[0]
>try:
>if fin_cust_id == cust_cust_id:
>print cust_curs_row, sales
>fin_curs_row = fin_curs.next()
>elif fin_cust_id > cust_cust_id:
>cust_curs_row = cust_curs.next()
>else:
>fin_curs_row = fin_curs.next()
>except StopIteration:
>break


In the end if speed is not an issue, just loading everthing in SQLite
and doing the join there makes it so simple that anybody should be
able to maintain the code, so that is also a good choice.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Working with Cursors

2012-04-17 Thread nn
On Apr 17, 2:11 pm, timlash  wrote:
> Searched the web and this forum without satisfaction.  Using Python 2.7 and 
> pyODBC on Windows XP I can get the code below to run and generate two cursors 
> from two different databases without problems.  Ideally, I'd then like to 
> join these result cursors thusly:
>
> SELECT a.state, sum(b.Sales) FROM cust_curs a INNER JOIN fin_curs b ON 
> a.Cust_id = b.Cust_id GROUP BY a.state
>
> Is there a way to join cursors using SQL statements in python or pyODBC?  
> Would I need to store these cursors in a common DB (SQLite3?) to accomplish 
> this?  Is there a pure python data handling approach that would generate this 
> summary from these two cursors?
>
> Thanks for your consideration.
>
> Working code:
>
> import pyodbc
>
> #
> # DB2 Financial Data Cursor
> #
> cnxn = pyodbc.connect('DSN=DB2_Fin;UID=;PWD=')
> fin_curs = cnxn.cursor()
>
> fin_curs.execute("""SELECT Cust_id, sum(Sales) as Sales
>                         FROM Finance.Sales_Tbl
>                         GROUP BY Cust_id""")
>
> #
> # Oracle Customer Data Cursor
> #
> cnxn = pyodbc.connect('DSN=Ora_Cust;UID=;PWD=')
> cust_curs = cnxn.cursor()
>
> cust_curs.execute("""SELECT Distinct Cust_id, gender, address, state
>                         FROM Customers.Cust_Data""")

If any of the two cursors fits in memory you could store it in a dict
and then look the extra data up as you traverse the second one. E.g.

>sales = {}
>for fin_curs_row in fin_curs:
>fin_cust_id,sales = fin_curs_row
>sales[fin_cust_id] = sales
>for cust_curs_row in cust_curs:
>cust_cust_id = cust_curs_row[0]
>print cust_curs_row, sales[cust_cust_id]

If you can't make it fit in memory, one way would be to order both
cursors by customer_id and pair them up as they come along:

>fin_curs_row = fin_curs.next()
>cust_curs_row = cust_curs.next()
>While True:
>fin_cust_id,sales = fin_curs_row
>cust_cust_id = cust_curs_row[0]
>try:
>if fin_cust_id == cust_cust_id:
>print cust_curs_row, sales
>fin_curs_row = fin_curs.next()
>elif fin_cust_id > cust_cust_id:
>cust_curs_row = cust_curs.next()
>else:
>fin_curs_row = fin_curs.next()
>except StopIteration:
>break


In the end if speed is not an issue, just loading everthing in SQLite
and doing the join there makes it so simple that anybody should be
able to maintain the code, so that is also a good choice.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to count the total number of strings (in the list) used in Python?

2012-04-17 Thread nn
On Apr 16, 3:00 pm, Chinesekidz  wrote:
> Hello!
>
> I would like to know how to write the program to count the total
> number of strings (in the list) used in Python..
>
> for example:
>
> list:['1','2','3','4']
>
> for l in range(4):
>      num=input("list:"+list[l]+"(between 1 and 4):")
>      if num.isdigit:
>         tnum=tnum+int(num)
>
> print("Total number of list used:",???)
>
> I want the output to be:
>
> list 1 (between 1 and 4): 2
> list 2 (between 1 and 4): 5
> list 3 (between 1 and 4): 6
> list 4 (between 1 and 4):5
> list 1 (between 1 and 4):4
>
> Total number of list used: 5
>
> in the case above, it is to show the how many times of strings from
> the list has been used...
>
> I would like to know how to write the code for this...
>
> Thanks in advance

I didn't understand the question. If you don't get any further answers
on the list it is because nobody else understood the question either.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Working with Cursors

2012-04-17 Thread Ian Kelly
On Tue, Apr 17, 2012 at 12:11 PM, timlash  wrote:
> Searched the web and this forum without satisfaction.  Using Python 2.7 and 
> pyODBC on Windows XP I can get the code below to run and generate two cursors 
> from two different databases without problems.  Ideally, I'd then like to 
> join these result cursors thusly:
>
> SELECT a.state, sum(b.Sales) FROM cust_curs a INNER JOIN fin_curs b ON 
> a.Cust_id = b.Cust_id GROUP BY a.state
>
> Is there a way to join cursors using SQL statements in python or pyODBC?

Not using SQL.  Oracle has the ability to do cross-database queries,
but only to other Oracle databases.  If you want to join that data,
you'll need to do it in Python.


>  Would I need to store these cursors in a common DB (SQLite3?) to accomplish 
> this?

That's an option, although it seems like overkill to me.

>  Is there a pure python data handling approach that would generate this 
> summary from these two cursors?

Create a dict keyed on the cust_id.  Something like this should do
what you want:

dict1 = {row[0]: row for row in cursor1}
dict2 = {row[0]: row for row in cursor2}
join = {}
for key in dict1.viewkeys() & dict2.viewkeys():
join[key] = (dict1[key], dict2[key])

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


Re: Framework for a beginner

2012-04-17 Thread Gerd Niemetz
Am Mittwoch, 11. April 2012 22:11:37 UTC+2 schrieb biof...@gmail.com:
> I am new to python and only have read the Byte of Python ebook, but want to 
> move to the web. I am tired of being a CMS tweaker and after I tried python, 
> ruby and php, the python language makes more sense (if that makes any "sense" 
> for the real programmers). I heard a lot of good things about Django, 
> Pyramid, etc, but I dont want to pick the most used or the one with the most 
> magic. Instead I was thinking about one that could "teach" me python along 
> the way. My plan is to rebuild my portfolio using python and a framework and 
> also benefit my python learning along the way.
> 
> Thanks in advance.

Hi!

Take a look at http://www.web2py.com, a powerful and easy to learn python 
framework, and the community at 
https://groups.google.com/forum/?fromgroups#!forum/web2py is also very helpful

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


Working with Cursors

2012-04-17 Thread timlash
Searched the web and this forum without satisfaction.  Using Python 2.7 and 
pyODBC on Windows XP I can get the code below to run and generate two cursors 
from two different databases without problems.  Ideally, I'd then like to join 
these result cursors thusly:

SELECT a.state, sum(b.Sales) FROM cust_curs a INNER JOIN fin_curs b ON 
a.Cust_id = b.Cust_id GROUP BY a.state

Is there a way to join cursors using SQL statements in python or pyODBC?  Would 
I need to store these cursors in a common DB (SQLite3?) to accomplish this?  Is 
there a pure python data handling approach that would generate this summary 
from these two cursors?

Thanks for your consideration.


Working code:

import pyodbc

#
# DB2 Financial Data Cursor
#
cnxn = pyodbc.connect('DSN=DB2_Fin;UID=;PWD=')
fin_curs = cnxn.cursor()

fin_curs.execute("""SELECT Cust_id, sum(Sales) as Sales 
FROM Finance.Sales_Tbl 
GROUP BY Cust_id""")


#
# Oracle Customer Data Cursor
#
cnxn = pyodbc.connect('DSN=Ora_Cust;UID=;PWD=')
cust_curs = cnxn.cursor()

cust_curs.execute("""SELECT Distinct Cust_id, gender, address, state  
FROM Customers.Cust_Data""")


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


Re: Framework for a beginner

2012-04-17 Thread Gerd Niemetz
Am Mittwoch, 11. April 2012 22:11:37 UTC+2 schrieb biof...@gmail.com:
> I am new to python and only have read the Byte of Python ebook, but want to 
> move to the web. I am tired of being a CMS tweaker and after I tried python, 
> ruby and php, the python language makes more sense (if that makes any "sense" 
> for the real programmers). I heard a lot of good things about Django, 
> Pyramid, etc, but I dont want to pick the most used or the one with the most 
> magic. Instead I was thinking about one that could "teach" me python along 
> the way. My plan is to rebuild my portfolio using python and a framework and 
> also benefit my python learning along the way.
> 
> Thanks in advance.

Hi!

Take a look at http://www.web2by.com, a powerful and easy to learn python 
framework, and the community at 
https://groups.google.com/forum/?fromgroups&hl=de#!forum/web2py is also very 
helpful

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


A better way to read Image from a server and start download / load

2012-04-17 Thread vijay swaminathan
Hi Expert,

I'm a newbie to python and still on the process of learning on the go ...

I have a webserver which has list of images to load on a Device Under Test
(DUT) ...

*Requirement is:*

if the image is already present on the server, proceed with loading the
image onto the DUT.

if the image is not present on the server , then proceed with the download
of the image and then upgrade the DUT.

I have written the following code but I'm quite not happy with the way I
have written this, because I have a feeling that it could have been done
better using some other method/s

Please suggest the areas where i could have done better and the techniques
to do so..

Appreciate your time in reading this email and for your valuable
suggestions.


import urllib2

url = 'http://localhost/test'
filename = 'Image60.txt'   # image to Verify

def Image_Upgrade():
print 'proceeding with Image upgrade !!!'

def Image_Download():
print 'Proceeding with Image Download !!!'

resp = urllib2.urlopen(url)
flag = False
list_of_files = []
for contents in resp.readlines():
if 'Image' in contents:
c=(((contents.split('href='))[-1]).split('>')[0]).strip('"')  # The
content output would have html tags. so removing the tags to pick only
image name
if c != filename:
list_of_files.append(c)
else:
Image_Upgrade()
flag = True
if flag==False:
Image_Download()



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


Re: How to determine if IO redirection is occurring with the output from a Python program?

2012-04-17 Thread Edward d'Auvergne
Hi,

Thanks for the incredibly quick responses!  The os.isatty() call (and
sys.std*.isatty() calls as suggested by Chris Angelico at
http://mail.python.org/pipermail/python-list/2012-April/1291048.html)
work perfectly for this job!  I might have to do some testing later on
Windows though to see what happens when the ansi escape characters are
not supported.

Cheers,

Edward



On 17 April 2012 16:29, pat  wrote:
> Check os.isatty(fd). It will return True if fd is a terminal-like device.
>
> On Tue, Apr 17, 2012 at 5:21 PM, Edward d'Auvergne 
> wrote:
>>
>> Hi,
>>
>> I was wondering if anyone knows of how to detect when IO redirection
>> of any form is happening within a Python program?  I would like to
>> emulate the behaviour of the GNU tools (for example the Unix commands
>> 'ls' or 'grep') whereby ascii escape sequences are printed if the
>> output is solely to the terminal, and in all other cases (redirection
>> to file via '>', pipes via '|', or more complex redirections) the
>> ascii escape characters are suppressed.  Any ideas would be
>> appreciated!
>>
>> Cheers,
>>
>> Edward
>>
>>
>> --
>> Edward d'Auvergne, PhD
>> Lead developer of the projects relax, minfx, and bmrblib
>> http://www.nmr-relax.com
>> http://gna.org/projects/minfx
>> http://gna.org/projects/bmrblib
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>
>

--
Edward d'Auvergne, PhD
Lead developer of the projects relax, minfx, and bmrblib
http://www.nmr-relax.com
http://gna.org/projects/minfx
http://gna.org/projects/bmrblib
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: File traversing

2012-04-17 Thread Nibin V M
thanks for a super fast reply Chris :)



On Tue, Apr 17, 2012 at 8:06 PM, Chris Angelico  wrote:

> On Wed, Apr 18, 2012 at 12:31 AM, Nibin V M  wrote:
> > # python test.py
> >   File "test.py", line 1
> > with open('/etc/trueuserowners') as res_own_file:
> > ^
> > IndentationError: unexpected indent
>
> Make sure your first code line is flush left. Since indentation
> indicates block structure (and not scoping, as it does in C-like
> languages), Python refuses to allow it anywhere other than inside
> something that can make use of it. If you're copying and pasting a
> huge lot of code from somewhere and it's all indented a minimum of a
> couple of spaces, the simplest solution may be to add a line at the
> top saying "if True:", thus making your whole program into a massive
> block if, which Python will happily accept.
>
> ChrisA
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Regards

Nibin.

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


Re: File traversing

2012-04-17 Thread Chris Angelico
On Wed, Apr 18, 2012 at 12:31 AM, Nibin V M  wrote:
> # python test.py
>   File "test.py", line 1
>     with open('/etc/trueuserowners') as res_own_file:
>     ^
> IndentationError: unexpected indent

Make sure your first code line is flush left. Since indentation
indicates block structure (and not scoping, as it does in C-like
languages), Python refuses to allow it anywhere other than inside
something that can make use of it. If you're copying and pasting a
huge lot of code from somewhere and it's all indented a minimum of a
couple of spaces, the simplest solution may be to add a line at the
top saying "if True:", thus making your whole program into a massive
block if, which Python will happily accept.

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


Re: How to determine if IO redirection is occurring with the output from a Python program?

2012-04-17 Thread Chris Angelico
On Wed, Apr 18, 2012 at 12:21 AM, Edward d'Auvergne
 wrote:
> I was wondering if anyone knows of how to detect when IO redirection
> of any form is happening within a Python program?  I would like to
> emulate the behaviour of the GNU tools (for example the Unix commands
> 'ls' or 'grep') whereby ascii escape sequences are printed if the
> output is solely to the terminal, and in all other cases (redirection
> to file via '>', pipes via '|', or more complex redirections) the
> ascii escape characters are suppressed.  Any ideas would be
> appreciated!

What you want is the "is-a-TTY" query, which is available in Python as
a method on the file-like object:

import sys
if sys.stdout.isatty(): # True if console, False if redirected
# do your fancy escape character stuff

Tip: Like the GNU tools, make this only a default. For instance, both
ls and grep have an option --color=WHEN where WHEN is either "never",
"always", or "auto". If you choose auto (which you can make the
default), the tools then check if stdout is a TTY.

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


Re: File traversing

2012-04-17 Thread Nibin V M
# python test.py
  File "test.py", line 1
with open('/etc/trueuserowners') as res_own_file:
^
IndentationError: unexpected indent
 [~]# cat test.py
 with open('/etc/trueuserowners') as res_own_file:
all_res = set(line.strip().replace(' ', '').split(':')[1] for line in
res_own_file if ':' in line)

 print all_res

am I missing something? :)

On Tue, Apr 17, 2012 at 4:44 PM, Nibin V M  wrote:

> wow...thanks Karl :)
>
> On Tue, Apr 17, 2012 at 2:20 AM, Karl Knechtel  wrote:
>
>>
>>
>> On Sun, Apr 15, 2012 at 5:51 AM, Chris Angelico  wrote:
>>
>>>
>>> (You may also want to consider using the 'with' statement to guarantee
>>> a timely closing of the file. Outside the scope of this mail though.)
>>>
>>> I think this list is just to collect unique entries, yes? If so, a set
>>> may be more to your liking. Check out:
>>> http://docs.python.org/py3k/library/stdtypes.html#set
>>>
>>>  
>>>
>>
>> In cases like this I like to just show the final code after making all
>> the changes, and let the student ask questions :)
>>
>> with open('/bah') as res_own_file:
>> all_res = set(line.strip().replace(' ', '').split(':')[1] for line in
>> res_own_file if ':' in line)
>>
>> --
>> ~Zahlman {:>
>>
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
>>
>
>
> --
> Regards
>
> Nibin.
>
> http://TechsWare.in
>
>


-- 
Regards

Nibin.

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


Re: How to determine if IO redirection is occurring with the output from a Python program?

2012-04-17 Thread pat
Check os.isatty(fd). It will return True if fd is a terminal-like device.

On Tue, Apr 17, 2012 at 5:21 PM, Edward d'Auvergne wrote:

> Hi,
>
> I was wondering if anyone knows of how to detect when IO redirection
> of any form is happening within a Python program?  I would like to
> emulate the behaviour of the GNU tools (for example the Unix commands
> 'ls' or 'grep') whereby ascii escape sequences are printed if the
> output is solely to the terminal, and in all other cases (redirection
> to file via '>', pipes via '|', or more complex redirections) the
> ascii escape characters are suppressed.  Any ideas would be
> appreciated!
>
> Cheers,
>
> Edward
>
>
> --
> Edward d'Auvergne, PhD
> Lead developer of the projects relax, minfx, and bmrblib
> http://www.nmr-relax.com
> http://gna.org/projects/minfx
> http://gna.org/projects/bmrblib
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PrintPreviewDialog problem

2012-04-17 Thread Chris Angelico
On Tue, Apr 17, 2012 at 4:09 PM, alhad  wrote:
> but, i am getting AttributeError for "paintRequested"

Before executing that line, find out what 'preview' contains. My guess
is that you have an error return such as None, which will of course
not have a paintRequested attribute. If it's not obvious to you as
soon as you see what preview is, follow Vincent's advice and post a
full error traceback.

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


How to determine if IO redirection is occurring with the output from a Python program?

2012-04-17 Thread Edward d'Auvergne
Hi,

I was wondering if anyone knows of how to detect when IO redirection
of any form is happening within a Python program?  I would like to
emulate the behaviour of the GNU tools (for example the Unix commands
'ls' or 'grep') whereby ascii escape sequences are printed if the
output is solely to the terminal, and in all other cases (redirection
to file via '>', pipes via '|', or more complex redirections) the
ascii escape characters are suppressed.  Any ideas would be
appreciated!

Cheers,

Edward


--
Edward d'Auvergne, PhD
Lead developer of the projects relax, minfx, and bmrblib
http://www.nmr-relax.com
http://gna.org/projects/minfx
http://gna.org/projects/bmrblib
-- 
http://mail.python.org/mailman/listinfo/python-list


Simple TLS NPN negotiation not working on socket server

2012-04-17 Thread Alek Storm
Hi all,

Support for the TLS NPN extension was apparently just added in Python 3.3
(see http://bugs.python.org/issue14204), but I can't get the following
simple TCP server using it to work with a browser:

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('',8000))
sock.listen(5)
while True:
conn, address = sock.accept()
context = ssl.SSLContext(ssl.PROTOCOL_SSLv3)
context.load_cert_chain(certfile=ssl_options['certfile'],
keyfile=ssl_options['keyfile'])
context.set_npn_protocols(['spdy/2', 'http/1.1'])

conn = context.wrap_socket(conn, server_side=True)
print(conn.selected_npn_protocol()) # prints `None`, instead of either
protocol name
conn.close()

Connecting with either Firefox 11 or Chrome (which both support NPN) causes
this to print None, rather than a protocol name. What's going on? This is
such a basic use case that I doubt it's a library bug, but I have no idea
how to debug a TLS connection. I'm using Python 3.3.0a2 and OpenSSL 1.0.1 (
ssl.HAS_NPN is True).

Does the protocol matter? PROTOCOL_SSLv23 gives the same result, but
PROTOCOL_TLSv1 makes it die with SSL3_GET_CLIENT_HELLO:wrong version
number(strange, because both browsers ostensibly support TLS).

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


Re: system call that is killed after n seconds if not finished

2012-04-17 Thread Adam Skutt
On Apr 16, 11:34 am, Alain Ketterlin 
wrote:
> Jaroslav Dobrek  writes:
> > I would like to execute shell commands, but only if their execution
> > time is not longer than n seconds. Like so:
>
> > monitor(os.system("do_something"), 5)
>
> > I.e. the command do_somthing should be executed by the operating
> > system. If the call has not finished after 5 seconds, the process
> > should be killed.
>
> > How could this be done?
>
> My system (linux) has a timeout command that does just this.

Alas, while timeout(1) is in GNU coreutils it's a rather recent
addition and so you cannot rely on it being on every Linux machine.
Also, there are multiple versions of timeout, from different
applications, that do the same thing with different syntax.  However,
if it is available on the systems you care about, I would use it.
Pay close attention to its caveats.

> Otherwise,
> you may use subprocess.Popen to spawn the process, sleep for 5 seconds
> in the parent, then use Popen.poll() to check whether the process has
> finished, and finally Popen.kill() if it has not.
>

That's true subject to two conditions:
1. The subprocess doesn't spawn child processes of its own or you
don't care about killing them (doubtful).
2. The application isn't running on a command-line, where it could be
suspended; or you don't care about timeliness if the application is
suspended.

The first item can be solved by manually performing fork()/exec() and
using setpgid() to create a process group.  Terminating the process
group on timeout will terminate the subprocess and all of its
children.

The second one is much more tricky to solve.  Basically, you have to
supply enough job control functionality (like a shell) to handle the
terminal signals and ensure your signaling process stays alive.  The
GNU coreutils version of timeout basically immunizes itself from the
signals, which works but also creates problems if it is run in the
background.  If you need to do this, I'd advise cheating, as I believe
the general solution is pretty ugly and doesn't exist in ready form.
I certainly could not find it.

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


Re: logging.config.fileConfig FileHandler configure to write to APP_DATA

2012-04-17 Thread Jean-Michel Pichavant

Jeffrey Britton wrote:

I figured out what I was after.

logfn = os.path.join(os.environ['APPDATA'], directory, filename)
ensure_path(logfn)
logging.config.fileConfig("logging.conf", defaults={'logfn': logfn})

In the config file use:
[handler_fileHandler]
class=FileHandler
level=DEBUG
formatter=simpleFormatter
args=(r'%(logfn)s', 'w')



  

Hi,

An alternative is to subclass FileHandler with a handler customized for 
your app.


class AppFileHandler(FileHandler):
   def __init__(filename):
  if not os.path.isabs(filename):
 filename = os.path.join(os.environ['APPDATA'], 'whateverdir', 
filename)

  FileHandler.__init__(self, filename, 'w')

[handler_appFileHandler]
class=AppFileHandler
level=DEBUG
formatter=simpleFormatter
args=('foo.log')


It removes the cryptic args=(r'%(logfn)s', 'w') which may puzzle users.


I didn't test the code, it could contain some errors, but you got the idea.

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


Re: Framework for a beginner

2012-04-17 Thread Roy Smith
In article 
,
 Bryan  wrote:

> Django has emphasized backwards compatibility with the
> down-side that, last I heard, there was no plan to move to Python 3.

Hardly.  See https://www.djangoproject.com/weblog/2012/mar/13/py3k/

I agree that Django is probably what the OP should be looking at, for 
most of the reasons you mention.  The ecosystem/community which has 
grown up around Django is a major plus.

I've done a couple of projects in Django, ranging from a fairly simple 
site based on django-cms, to a REST API server with a MongoDB back end.  

This later site actually used very little of Django's capabilities.  I'm 
using the url routing, middleware framework, and session management, and 
that's about it.  No templates, no ORM (at least not the one that comes 
with Django).  But even with just using those small pieces, there was 
enough value in the scaffolding I got from Django to make it a good pick.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: File traversing

2012-04-17 Thread Nibin V M
wow...thanks Karl :)

On Tue, Apr 17, 2012 at 2:20 AM, Karl Knechtel  wrote:

>
>
> On Sun, Apr 15, 2012 at 5:51 AM, Chris Angelico  wrote:
>
>>
>> (You may also want to consider using the 'with' statement to guarantee
>> a timely closing of the file. Outside the scope of this mail though.)
>>
>> I think this list is just to collect unique entries, yes? If so, a set
>> may be more to your liking. Check out:
>> http://docs.python.org/py3k/library/stdtypes.html#set
>>
>>  
>>
>
> In cases like this I like to just show the final code after making all the
> changes, and let the student ask questions :)
>
> with open('/bah') as res_own_file:
> all_res = set(line.strip().replace(' ', '').split(':')[1] for line in
> res_own_file if ':' in line)
>
> --
> ~Zahlman {:>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>


-- 
Regards

Nibin.

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


Re: pygame: output to file?

2012-04-17 Thread Temia Eszteri
Oh, forgot to mention some things. Also poorly phrased other things.
Let me try that again.

>* ImageMagick (http://www.imagemagick.org/) as Ian suggested - the
>ctypes binding I found for this was too immature to use, and the other
>option will require compiling,

The 'other option' I meant here was a different Python binding,
PythonMagick. In comparison, the ctypes-based binding is called
PythonMagickWand.

Failing that, both this and GraphicsMagick can be manipulated through
shell libraries, but I haven't done anything like that yet so I'd be
going in blind and hoping for the best. (I make no claim to be an
expert or that I ever was or will be one - I'm just starting out and
helping where I can)

>* or a module I found on Google Code that greatly extends from PIL's
>meager GIF-handling abilities, images2gif
>(http://code.google.com/p/visvis/source/browse/vvmovie/images2gif.py).

While you said you preferred animated GIFs in your opening message,
the visvis project at Google code (behind images2gif) also has
images2avi, which could possibly be used with conversion software down
the line to output to a different container and codec if you wanted to
go with the video stream.

Once again, let me know what you think. As for the other
comp.lang.python folks, please tell me if I'm overlooking something
that'll throw a spanner in the works.

~Temia
--
When on earth, do as the earthlings do.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Framework for a beginner

2012-04-17 Thread Arnaud Delobelle
On 17 April 2012 09:54, Bryan  wrote:
> Django has emphasized backwards compatibility with the
> down-side that, last I heard, there was no plan to move to Python 3.

Not quite:

https://www.djangoproject.com/weblog/2012/mar/13/py3k/

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


Re: Framework for a beginner

2012-04-17 Thread Bryan
biofob wrote:
> I am new to python and only have read the Byte of Python ebook, but want to 
> move to the web. I am tired of being a CMS tweaker and after I tried python, 
> ruby and php, the python language makes more sense (if that makes any "sense" 
> for the real programmers). I heard a lot of good things about Django, 
> Pyramid, etc, but I dont want to pick the most used or the one with the most 
> magic. Instead I was thinking about one that could "teach" me python along 
> the way. My plan is to rebuild my portfolio using python and a framework and 
> also benefit my python learning along the way.

Warning: I'm a mere dabbler in web frameworks.

If by rebuilding your portfolio you mean to position yourself for a
job, then popularity counts a lot. As measured by job openings, Django
is king. It's a fine framework and reasonably Pythonic. The Django
community is large and sophisticated and helpful They have there own
app download system, which I haven't used but is supposed to work
really well. Django has emphasized backwards compatibility with the
down-side that, last I heard, there was no plan to move to Python 3.

Your disinterest in "magic" plays against Web2Py. Were the goal to go
from squat to professional quality web site quickly and easily, Web2Py
would be it.

Web2Py does a load of complex and interesting stuff behind the scenes.
Among web frameworks, it rocks like none other at teaching and using
web best-practices, at the cost of hiding or skirting core Python
practices. Professor Massimo DiPierro fist built Web2Py for his own
particular problem: teaching web programming in one semester. Without
it, his students spent so much time on the tools that his course could
not cover the essential topics.

Turbo-Gears has a lot going for it, largely by adoption. It demands
more elbow-grease than Django or Web2Py, but the extra effort has
benefits beyond the framework. In particular, Turbo-Gears has adopted
SQLAlchemy, which is significantly more demanding and vastly more
powerful than the purpose-built automatic table-generators of Django
and Web2Py.

Then there are the less than full-stack frameworks and libraries. But
this post is probably too long already.

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


Re: escaping

2012-04-17 Thread Serhiy Storchaka

This was the original intention, i.e. 'format' preferred over '%' but
even the core developers have backtracked on this issue. If I could
remember the thread it was on I'd give you a reference, but I haven't
the faintest idea as to which mailing list it was on. I'm certain that
other subscribers will remember and point us all in the correct direction.


http://comments.gmane.org/gmane.comp.python.devel/121299
http://comments.gmane.org/gmane.comp.python.devel/129939

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


Re: PrintPreviewDialog problem

2012-04-17 Thread Vincent Vande Vyvre
Le 17/04/12 08:09, alhad a écrit :
> Hi Guys,
>
> I am using Python 2.5 & Qt4.4.1 on windows xp machine. I am trying to use
> printpreviewdialog for my application.
>
> printer = QtGui.QPrinter(QtGui.QPrinter.HighResolution)
> preview = QtGui.QPrintPreviewDialog(printer,self)
> preview.paintRequested.connect(self.printPreview)
> preview.exec_()
>
> but, i am getting AttributeError for "paintRequested"
>
> What could be the possible solution for this?
>
> Thanks in advance
>
> --
> View this message in context: 
> http://python.6.n6.nabble.com/PrintPreviewDialog-problem-tp4889139p4889139.html
> Sent from the Python - python-list mailing list archive at Nabble.com.
Works fine for me.

Python 2.6.5 on Linux
Qt 4.6.2
PyQt 4.7.2

Please, paste the exact error.

-- 
Vincent V.V.
Oqapy  . Qarte+7
 . PaQager 

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


Re: pygame: output to file?

2012-04-17 Thread Temia Eszteri
Okay, superpollo. I'm looking at a few possible ways to do this (the
Zen of Python hates me, even though I'm dutch-blooded!), and I'd like
to ask a couple more questions before I dive into writing up a
solution.

First, are you simply trying to capture the framebuffer of a
Pygame-made game in realtime, or are you using Pygame to explicitly
render and build a movie or GIF animation sequence? I need to know
what kind of overhead is permissible, and in the former case I know
there won't be a lot of allowance due to how slow Pygame's drawing
operations can get when they stack up, especially at high resolutions.

Secondly, how much are you willing to install to have a means to do
this? Because from the looks of things, this'll have to either be
tackled with:

* ImageMagick (http://www.imagemagick.org/) as Ian suggested - the
ctypes binding I found for this was too immature to use, and the other
option will require compiling,
* A branch someone on EsperNet pointed me to called GraphicsMagick
(http://www.graphicsmagick.org/) - haven't looked into this much so I
can't comment on it yet,
* or a module I found on Google Code that greatly extends from PIL's
meager GIF-handling abilities, images2gif
(http://code.google.com/p/visvis/source/browse/vvmovie/images2gif.py).

Let me know what you think of the options, and I'm sorry this has
gotten a little too complex. I was hoping animated GIF sequencing in
Python would be a simple matter too!

~Temia
--
When on earth, do as the earthlings do.
-- 
http://mail.python.org/mailman/listinfo/python-list