Re: python segfault

2012-03-27 Thread Michael Poeltl
hi,

* Dave Angel  [2012-03-28 04:38]:
> On 03/27/2012 06:27 PM, Michael Poeltl wrote:
> >hi,
> >
> >can anybody tell why this 'little stupid *thing* of code' let's 
> >python-3.2.2, 2.6.X or python 2.7.2 segfault?
> >
> >>>def get_steps2(pos=0, steps=0):
> >... if steps == 0:
> >... pos = random.randint(-1,1)
> >... if pos == 0:
> >... return steps
> >... steps += 2
> >... pos += random.randint(-1,1)
> >... return get_steps2(pos,steps)
> >...
> >
> >0
> >2
> >8
> >0
> >Segmentation fault
> >?>
> >
> >funny, isn't it?
> >I was able to reproduce this segfault on various machines (32bit 64bit), 
> >ubuntu, slackware, debian
> >python.X segfaults on all of them
> >
> >thx
> >Michael
> 
> Others have explained why you can't just raise the recursion limit
> to arbitrarily large values, and why there's no particular bound on
> the possible recursion size.  But the real question is why you don't
> do the completely trivial conversion to a non-recursive equivalent.
> 
> All you need do is add a while True:  to the beginning of the
> function, and remove the return statement.
yeah - of course 'while True' was the first, most obvious best way... ;-)
but I was asked if there was a way without 'while True'
and so I started the 'recursive function'

and quick quick; RuntimeError-Exception -> not thinking much -> just adding
two zeros to the default limit (quick and dirty) -> segfault ==> subject: 
python segfault ;-)

and that was my first time that I received a segfault and not an Exception

NOW it's quite clear ;-)

thank you!
Michael
> 
> 
> 
> -- 
> 
> DaveA
> 

-- 
Michael Poeltl
Computational Materials Physics  voice: +43-1-4277-51409
Univ. Wien, Sensengasse 8/12 fax:   +43-1-4277-9514 (or 9513) 
A-1090 Wien, AUSTRIA   cmp.mpi.univie.ac.at 
---
ubuntu-11.10 | vim-7.3 | python-3.2.2 | mutt-1.5.21 | elinks-0.12
---
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python segfault

2012-03-27 Thread Dave Angel

On 03/27/2012 06:27 PM, Michael Poeltl wrote:

hi,

can anybody tell why this 'little stupid *thing* of code' let's python-3.2.2, 
2.6.X or python 2.7.2 segfault?


def get_steps2(pos=0, steps=0):

... if steps == 0:
... pos = random.randint(-1,1)
... if pos == 0:
... return steps
... steps += 2
... pos += random.randint(-1,1)
... return get_steps2(pos,steps)
...

0
2
8
0
Segmentation fault
?>

funny, isn't it?
I was able to reproduce this segfault on various machines (32bit 64bit), 
ubuntu, slackware, debian
python.X segfaults on all of them

thx
Michael


Others have explained why you can't just raise the recursion limit to 
arbitrarily large values, and why there's no particular bound on the 
possible recursion size.  But the real question is why you don't do the 
completely trivial conversion to a non-recursive equivalent.


All you need do is add a while True:  to the beginning of the function, 
and remove the return statement.




--

DaveA

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


Generating a pkg config file with distutils

2012-03-27 Thread Tycho Andersen
Hi all,

I'm distributing a package which for various legacy reasons needs to
generate a pkgconfig file from a template (adding version numbers,
prefixes, etc.) and install the file in the right place
($PREFIX/lib/pkgconfig/foo.pc in most cases).

Currently, I have a rather nasty hack to implement all this, but
presumably there's a better way to do it. If I could even get the
installation part (e.g. using the right MANIFEST.in incantations),
that would be wonderful. Reading the MANIFEST.in docs [1], it's not
obvious that you can control the install locations of these files
(i.e., .pc files must be installed to the above location to be
correctly detected by other packages).

Is what I want to do possible, or should I continue using my nasty
hack?

TIA!

Tycho

[1]: http://docs.python.org/distutils/sourcedist.html#commands
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: RE: Advise of programming one of my first programs

2012-03-27 Thread Devin Jeanpierre
On Tue, Mar 27, 2012 at 5:59 PM, Evan Driscoll  wrote:
>> The use of eval is dangerous if you are not *completely* sure what is
>> being passed in. Try using pickle instead:
>> http://docs.python.org/release/2.5.2/lib/pickle-example.html
>
>
> Um, at least by my understanding, the use of Pickle is also dangerous if you
> are not completely sure what is being passed in:

Oh goodness yes. pickle is exactly as unsafe as eval is. Try running this code:

from pickle import loads
loads("c__builtin__\neval\n(c__builtin__\nraw_input\n(S'py>'\ntRtR.")

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


Re: Advise of programming one of my first programs

2012-03-27 Thread Tim Chase

On 03/27/12 16:53, Anatoli Hristov wrote:

On Tue, Mar 27, 2012 at 5:53 PM, Tim Chase wrote:

On 03/27/12 10:32, Prasad, Ramit wrote:

fileread = open('myfile.txt','r')
tbook = eval(fileread.read())
fileread.close()



The use of eval is dangerous if you are not *completely* sure what is
being passed in. Try using pickle instead:


Or, depending on the use, you might use ast.literal_eval()


Thanks, but I`m still far from for dose details I thing:)


[reordered to make the commenting inline instead of top-posted]

To make it safer, just change eval(fileread.read()) to 
ast.literal_eval(fileread.read()) and insert "import ast" at the 
top of your script.


-tkc



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


Re: python segfault

2012-03-27 Thread Christian Heimes
Am 28.03.2012 00:27, schrieb Michael Poeltl:
> hi,
> 
> can anybody tell why this 'little stupid *thing* of code' let's python-3.2.2, 
> 2.6.X or python 2.7.2 segfault?

The code segfaults because you have increased the recursion limit. The
amount of recursions is limited by the stack size. A C program with a
usually stack size can have about 4000 recursions. Python takes at least
two stack levels for each recursion.

The documentation
http://docs.python.org/library/sys.html#sys.setrecursionlimit contains a
warning, too.

Christian

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


Re: python segfault

2012-03-27 Thread Chris Kaynor
On Tue, Mar 27, 2012 at 3:27 PM, Michael Poeltl
 wrote:
> hi,
>
> can anybody tell why this 'little stupid *thing* of code' let's python-3.2.2, 
> 2.6.X or python 2.7.2 segfault?
>
>>> def get_steps2(pos=0, steps=0):
> ...     if steps == 0:
> ...         pos = random.randint(-1,1)
> ...     if pos == 0:
> ...         return steps
> ...     steps += 2
> ...     pos += random.randint(-1,1)
> ...     return get_steps2(pos,steps)
> ...
 import random, sys
 sys.setrecursionlimit(10)

If you remove this setrecursionlimit, it will throw an exception. The
issue is that your code is overflowing the C stack by trying to make
too many calls. The Python recursion limit prevents this by turning
such cases into Python exceptions prior to the stack overflow.

As your recursion is based on a random number generator, all you need
is a sequence of random numbers that is unbalanced between -1 and 1
results for 1000-3000 calls (the exact number will vary from os,
compiler, maybe machine, etc), which is not too unlikely to occur.
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Advise of programming one of my first programs

2012-03-27 Thread Prasad, Ramit
> def load_book():
> load_book = open('c:/Python27/Toli/myfile.txt', 'r')
> load_book = eval(load_book.read())
> return load_book
> def write_book(tbook):
> write_book = open('c:/Python27/Toli/myfile.txt', 'w')
> write_book.write(str(tbook))
> 
> def details(choice):
> sb = tbook[choice]
> print 'Nickname: ', choice, ' is selected\n'
> print 'First name:\t', sb[0], '\n'
> print 'Last name:\t', sb[1], '\n'
> print 'Country:\t', sb[2], '\n'
> print 'City:\t\t', sb[3], '\n'
> print 'Phone number:\t',sb[4], '\n'
> print 'Memos:\n'
> print sb[5]
> print '\n\n(E)dit\n\n'
> print '(B)ack to phonebook list\n\n'
> dmenu(choice)
> 
> def edit(choice):
> sb = tbook[choice]
> fn = raw_input('New name for ' + sb[0] + ' : ')
> sb[0] = fn
> ln = raw_input('New name for ' + sb[1] + ' : ')
> sb[1] = ln
> write_book(tbook)
> ##filewrite = open('myfile.txt','w')
> ##filewrite.write(str(tbook))
> ##filewrite.close()
> ##raw_input('\n\n\nPress  to return')
> details(choice)
> 
> def get_menu_choice():
>   choice = raw_input('input: ')
>   return choice
> 
> 
> 
> def listpb():
> global tbook
> tbook = load_book()
> print '_' *45, ' Phonebook ', '_' *45,'\n\n\n'
> print 'Nick\t\tF.Name\t\tL.Name\t\tCity\t\t\tRegion\t\tTel'
> print '_' * 105,'\n','\t' * 13
> for val in tbook.keys():
> print val, '\t\t', tbook[val][0], '\t', tbook[val][1], '\t',
> tbook[val][2], '\t\t', tbook[val][3], '\t\t', tbook[val][4],'\t\t\n'
> print '_'*105,'\n\n'
> mmenu()
> 
> def mmenu():
> while True:
> choice = get_menu_choice()
> if choice in tbook:
> details(choice)
> elif choice not in tbook:
> print choice + 'Not in the book.'
> mmenu()
> elif choice =='Q' or choice =='q':
> break
> else:
> print 'Selection {0} not understood.'.format(choice) ## This is
> something that I don`t understand yet
> 
> 
> def dmenu(choice):
> while True:
> choicem = get_menu_choice()
> if choicem == 'e' or choicem == 'E':
>   edit(choice)
> elif choicem == 'd' or choicem == 'D':
>   book = get_book_to_edit()
>   details( tbook, book )
> elif choicem =='Q' or choicem == 'q':
>   break # end loop to exit program
> else:
>   print 'Selection {0} not understood.'.format( choicem )
> 
> listpb()

> This was difficult, now I feel more confused it works, but I`m sure its not
> the way you wanted :)

You are correct it is not. :) You code is overly complex making it harder
to understand. Try and reduce the problem to the least number of tasks you need.
>From the Zen of Python, "Simple is better than complex." It is a good 
>programming 
mentality. 

1. function that returns the loaded book
def load_book():
load_book = open('c:/Python27/Toli/myfile.txt', 'r')
load_book = eval(load_book.read())
return load_book! 

2. A system to navigate your program.
def mmenu():
# load tbook here
while True:
choicem = get_menu_choice()
if choicem == 'e' or choicem == 'E':
  book = get_book_name()
  edit( tbook, book )
elif choicem == 'd' or choicem == 'D':
  book = get_book_name()
  details( tbook, book )
elif choicem =='Q' or choicem == 'q':
  break # end loop to exit program
else:
  print 'Selection {0} not understood.'.format( choicem )I have 
given you more functions

3. function to write an edited book
def write_book(tbook):
write_book = open('c:/Python27/Toli/myfile.txt', 'w')
write_book.write(str(tbook)) 
# I think repr would be more accurate than str here.

4. Function to print the entire book
def listpb( tbook ):
print '_' *45, ' Phonebook ', '_' *45,'\n\n\n'
print 'Nick\t\tF.Name\t\tL.Name\t\tCity\t\t\tRegion\t\tTel'
print '_' * 105,'\n','\t' * 13
for val in tbook.keys():
print val, '\t\t', tbook[val][0], '\t', tbook[val][1], '\t', 
tbook[val][2], '\t\t', tbook[val][3], '\t\t', tbook[val][4],'\t\t\n'
print '_'*105,'\n\n'

5. Get input from user
def get_menu_choice():
choice = raw_input('input: ')
return choice

6. A function to get book name from user
def get_book_name(tbook):
# write this and do not use global

6. A function to print an entry from the book
def details( tbook, choice ):
# write this, no menu interaction allowed

7. A function to edit an entry from the book
def edit( tbook, choice ):
# write this, no menu interaction allowed 
# you can ask the user what you need to change the values

I do not think you need any other functions. Now you just need to finsh all the 
functions
and put it all together.


Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Curre

python segfault

2012-03-27 Thread Michael Poeltl
hi,

can anybody tell why this 'little stupid *thing* of code' let's python-3.2.2, 
2.6.X or python 2.7.2 segfault?

>> def get_steps2(pos=0, steps=0):
... if steps == 0:
... pos = random.randint(-1,1)
... if pos == 0:
... return steps
... steps += 2
... pos += random.randint(-1,1)
... return get_steps2(pos,steps)
...
>>> import random, sys
>>> sys.setrecursionlimit(10)
>>> for i in range(200):
... print ( get_steps2() )
...
4
0
8
0
0
0
2
2
166
2
0
0
16
4
2
16
0
0
10
70
152
50
58
0
6
0
0
0
2
8
0
Segmentation fault
?>

funny, isn't it?
I was able to reproduce this segfault on various machines (32bit 64bit), 
ubuntu, slackware, debian
python.X segfaults on all of them

thx
Michael
-- 
Michael Poeltl
Computational Materials Physics  voice: +43-1-4277-51409
Univ. Wien, Sensengasse 8/12 fax:   +43-1-4277-9514 (or 9513) 
A-1090 Wien, AUSTRIA   cmp.mpi.univie.ac.at 
---
ubuntu-11.10 | vim-7.3 | python-3.2.2 | mutt-1.5.21 | elinks-0.12
---
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: RE: Advise of programming one of my first programs

2012-03-27 Thread Evan Driscoll

On 01/-10/-28163 01:59 PM, Prasad, Ramit wrote:

### CODE #
fileread = open('myfile.txt','r')
tbook = eval(fileread.read())
fileread.close()


The use of eval is dangerous if you are not *completely* sure what is
being passed in. Try using pickle instead:
http://docs.python.org/release/2.5.2/lib/pickle-example.html


Um, at least by my understanding, the use of Pickle is also dangerous if 
you are not completely sure what is being passed in:


  Warning: The pickle module is not intended to be secure
  against erroneous or maliciously constructed data. Never
  unpickle data received from an untrusted or unauthenticated
  source.
- http://docs.python.org/library/pickle.html


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


Re: Advise of programming one of my first programs

2012-03-27 Thread Anatoli Hristov
Thanks, but I`m still far from for dose details I thing:)

Regards

Anatoli

On Tue, Mar 27, 2012 at 5:53 PM, Tim Chase wrote:

> On 03/27/12 10:32, Prasad, Ramit wrote:
>
>> fileread = open('myfile.txt','r')
>>> tbook = eval(fileread.read())
>>> fileread.close()
>>>
>>
>> The use of eval is dangerous if you are not *completely* sure what is
>> being passed in. Try using pickle instead:
>> http://docs.python.org/**release/2.5.2/lib/pickle-**example.html
>>
>
> Or, depending on the use, you might use ast.literal_eval()
>
> A cursory glance at the OP's code suggests that this may simply be a dict
> of values-to-lists of purely literals, so literal_eval() should do the job.
>
> -tkc
>
>
> --
> http://mail.python.org/**mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Advise of programming one of my first programs

2012-03-27 Thread Anatoli Hristov
>
> This was difficult, now I feel more confused it works, but I`m sure its
> not the way you wanted :)



> The use of eval is dangerous if you are not *completely* sure what is
>> being passed in. Try using pickle instead:
>> http://docs.python.org/release/2.5.2/lib/pickle-example.html
>
>  I`m sure about that I will use pickle nex time


> Maybe I can help you more by giving you a somewhere to build from. Try
> building the rest of the program given the new version of listpb below.

That was hard :)

You will need to read the chapter on Function Arguments (chapter 18?).
>
My book is translated in Bulgarian and it is chapter 13 should be the same
in the original too.

Here is the code now:

def load_book():

load_book = open('c:/Python27/Toli/myfile.txt', 'r')
load_book = eval(load_book.read())
return load_book
def write_book(tbook):
write_book = open('c:/Python27/Toli/myfile.txt', 'w')
write_book.write(str(tbook))

def details(choice):
sb = tbook[choice]
print 'Nickname: ', choice, ' is selected\n'
print 'First name:\t', sb[0], '\n'
print 'Last name:\t', sb[1], '\n'
print 'Country:\t', sb[2], '\n'
print 'City:\t\t', sb[3], '\n'
print 'Phone number:\t',sb[4], '\n'
print 'Memos:\n'
print sb[5]
print '\n\n(E)dit\n\n'
print '(B)ack to phonebook list\n\n'
dmenu(choice)

def edit(choice):
sb = tbook[choice]
fn = raw_input('New name for ' + sb[0] + ' : ')
sb[0] = fn
ln = raw_input('New name for ' + sb[1] + ' : ')
sb[1] = ln
write_book(tbook)
##filewrite = open('myfile.txt','w')
##filewrite.write(str(tbook))
##filewrite.close()
##raw_input('\n\n\nPress  to return')
details(choice)

def get_menu_choice():
choice = raw_input('input: ')
return choice



def listpb():
global tbook
tbook = load_book()
print '_' *45, ' Phonebook ', '_' *45,'\n\n\n'
print 'Nick\t\tF.Name\t\tL.Name\t\tCity\t\t\tRegion\t\tTel'
print '_' * 105,'\n','\t' * 13
for val in tbook.keys():
print val, '\t\t', tbook[val][0], '\t', tbook[val][1], '\t',
tbook[val][2], '\t\t', tbook[val][3], '\t\t', tbook[val][4],'\t\t\n'
print '_'*105,'\n\n'
mmenu()

def mmenu():
while True:
choice = get_menu_choice()
if choice in tbook:
details(choice)
elif choice not in tbook:
print choice + 'Not in the book.'
mmenu()
elif choice =='Q' or choice =='q':
break
else:
print 'Selection {0} not understood.'.format(choice) ## This is
something that I don`t understand yet


def dmenu(choice):
while True:
choicem = get_menu_choice()
if choicem == 'e' or choicem == 'E':
  edit(choice)
elif choicem == 'd' or choicem == 'D':
  book = get_book_to_edit()
  details( tbook, book )
elif choicem =='Q' or choicem == 'q':
  break # end loop to exit program
else:
  print 'Selection {0} not understood.'.format( choicem )

listpb()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Slow termination of process

2012-03-27 Thread Chris Angelico
On Wed, Mar 28, 2012 at 1:52 AM, Roland Hedberg  wrote:
> So, I went for the low-hanging fruit and defined my own TCPServer class
>
> class MyTCPServer(SocketServer.TCPServer):
>    def __init__(self, server_address, RequestHandlerClass):
>        self.allow_reuse_address = True
>        SocketServer.TCPServer.__init__(self, server_address,
>                                        RequestHandlerClass)
>
> and this solved my problem!
>
> So, thanks again Chris!

Awesome! I wonder - and someone who's used the facility may know
better than I - is there a reason not to make these sorts of things
into keyword-only arguments? Or, more generally, is there a reason for
them to be class variables rather than instance?

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


Re: OAuth 2.0 implementation

2012-03-27 Thread Demian Brecht
On Tuesday, 27 March 2012 07:18:26 UTC-7, Roy Smith  wrote:
> In article 
> <7909491.0.1332826232743.JavaMail.geo-discussion-forums@pbim5>,
>  Demian Brecht  wrote:
> 
> > OAuth 2.0 is still in draft status (draft 25 is the current one I believe) 
> > and yes, unfortunately every single server available at this point have 
> > varying degrees of separation from the actual spec. It's not a 
> > pseudo-standard, it's just not observed to the letter. Google is the 
> > closest 
> > and Facebook seems to be the farthest away (Stack Exchange is in close 
> > second 
> > due to building theirs to work like Facebook's).
> 
> In practice, OAuth is all about getting your site to work with Facebook.  
> That is all most web sites care about today because that's where the 
> money is.  The fact that other sites also use OAuth is of mostly 
> academic interest at this point.
> 
> The next player on the list is Twitter, and they're not even up to using 
> their own incompatible version of OAuth 2.0.  They're still using OAuth 
> 1.0 (although, I understand, they're marching towards 2.0).

Sure, with the initial surge of the Facebook platform, I'm sure there are many 
more applications that only work with Facebook. However, after the initial gold 
rush, I'm sure there will be more developers who see the potential power of 
service aggregation (and not just for feeds ;)). I know I'm one of them.

Of course, a lot of these thoughts are around niche markets, but isn't that 
where the money is? Untapped, niche markets? That's a completely different 
discussion though and would obviously be quite the thread derailment.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Advise of programming one of my first programs

2012-03-27 Thread Tim Chase

On 03/27/12 10:32, Prasad, Ramit wrote:

fileread = open('myfile.txt','r')
tbook = eval(fileread.read())
fileread.close()


The use of eval is dangerous if you are not *completely* sure what is
being passed in. Try using pickle instead:
http://docs.python.org/release/2.5.2/lib/pickle-example.html


Or, depending on the use, you might use ast.literal_eval()

A cursory glance at the OP's code suggests that this may simply 
be a dict of values-to-lists of purely literals, so 
literal_eval() should do the job.


-tkc


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


RE: Advise of programming one of my first programs

2012-03-27 Thread Prasad, Ramit
It is considered polite to post your reply either after the quoted text
or interspersed as I have done below.

> By the way I dont know why your mail was in my junk I just saw it.

Probably because I only reply back to the list and let the list 
forward to you.

> And here is my last code I did for the phonebook:
> Thanks
> 
> ### CODE #
> fileread = open('myfile.txt','r')
> tbook = eval(fileread.read())
> fileread.close()

The use of eval is dangerous if you are not *completely* sure what is 
being passed in. Try using pickle instead: 
http://docs.python.org/release/2.5.2/lib/pickle-example.html 


> Thank you Ramit for your advice`s. I`m reading a book ( Learning Python,
> Second Edition ) by Mark Lutz and David Ascher and now I just finished the
> Basic Function lesson :) I will keep in mind what you have advised me, but
> will implement it later when I have more experience with the book, because
> I don`t understand exactly what you mean by doing all dose changes :)

Maybe I can help you more by giving you a somewhere to build from. Try
building the rest of the program given the new version of listpb below.
You will need to read the chapter on Function Arguments (chapter 18?).

 def listpb():
 tbook = load_book()
 print '_' *45, ' Phonebook ', '_' *45,'\n\n\n'
 print 'Nick\t\tF.Name\t\tL.Name\t\tCity\t\t\tRegion\t\tTel'
 print '_' * 105,'\n','\t' * 13
 for val in tbook.keys():
 print val, '\t\t', tbook[val][0], '\t', tbook[val][1], '\t',
 tbook[val][2], '\t\t', tbook[val][3], '\t\t', tbook[val][4],'\t\t\n'
 print '_'*105,'\n\n'
 while True:
  choice = get_menu_choice()
  if choice == 'e' or choice == 'E':
   book = get_book_to_edit()
   edit( tbook, book )
  elif choice == 'd' or choice == 'D':
   book = get_book_to_edit()
   details( tbook, book )
  elif choice =='Q' or choice == 'q':
   break # end loop to exit program
  else:
   print 'Selection {0} not understood.'.format( choice ) 

Ramit


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

--


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


Re: Advise of programming one of my first programs

2012-03-27 Thread Anatoli Hristov
Thank you Ramit for your advice`s. I`m reading a book ( Learning Python,
Second Edition ) by Mark Lutz and David Ascher and now I just finished the
Basic Function lesson :) I will keep in mind what you have advised me, but
will implement it later when I have more experience with the book, because
I don`t understand exactly what you mean by doing all dose changes :)
By the way I dont know why your mail was in my junk I just saw it.

And here is my last code I did for the phonebook:
Thanks

### CODE #

fileread = open('myfile.txt','r')
tbook = eval(fileread.read())
fileread.close()


## Edit selected nickname
def edit():
sb = tbook[select]
fn = raw_input('New name for ' + sb[0] + ' : ')
sb[0] = fn
ln = raw_input('New name for ' + sb[1] + ' : ')
sb[1] = ln
filewrite = open('myfile.txt','w')
filewrite.write(str(tbook))
filewrite.close()
raw_input('\n\n\nPress  to return')
details()


## Details of nickname
def details():
sb = tbook[select]
print 'Nickname: ', select, ' is selected\n'
print 'First name:\t', sb[0], '\n'
print 'Last name:\t', sb[1], '\n'
print 'Country:\t', sb[2], '\n'
print 'City:\t\t', sb[3], '\n'
print 'Phone number:\t',sb[4], '\n'
print 'Memos:\n'
print sb[5]

print '\n\n(E)dit\n\n'
print '(B)ack to phonebook list\n\n'
menu = raw_input('What you wana do? ')
if menu == 'e' or 'E':
edit()
if menu == 'b' or 'B':
listpb()


## Select nickname
def selectm():
global select
select = raw_input('Type nickname and press : ')
if select == '':
listpb()
if select in tbook:
details()
else:
listpb()

## List all contacts
def listpb():
print '_' *45, ' Phonebook ', '_' *45,'\n\n\n'

print 'Nick\t\tF.Name\t\tL.Name\t\tCity\t\t\tRegion\t\tTel'
print '_' * 105,'\n','\t' * 13
for val in tbook.keys():
print val, '\t\t', tbook[val][0], '\t', tbook[val][1], '\t',
tbook[val][2], '\t\t', tbook[val][3], '\t\t', tbook[val][4],'\t\t\n'
print '_'*105,'\n\n'
selectm()

## Call list names
listpb()





On Tue, Mar 27, 2012 at 12:40 AM, Prasad, Ramit
wrote:

>
> > Hi guys just wanted to share one of my first programs. Could you please
> > tell me, do I use a right logic ?
> > It works fine what I wanted to do, but is it writen in the right way? My
> > next step is to make it write the changes of the dictionary on the file
> :)
> >
>
> When you do get that far, you should look at the pickle library.
> It is amazing how easy it is to store data with Python.
>
> >
> > ## DB
> > tbook = {'goodie':['Christian','Van Eckel','Bruxelles','Forest','02 344
> 33
> > 33','This is a test note :)'],
> >  'osvaldo':['Osvaldo','Rios','Liege','Centrum','023758832',''],
> >  'ronaldo':['Diego','Aspanda','Brussels','Vorst','03 443 23
> > 23','']}
> >
> > ## Edit selected nickname
> > def edit():
> > sb = tbook[select]
> > fn = raw_input('New name for ' + sb[0] + ' : ')
> > sb[0] = fn
> > ln = raw_input('New name for ' + sb[1] + ' : ')
> > sb[1] = ln
> > raw_input('\n\n\nPress  to return')
> > details()
> >
> >
> > ## Details of nickname
> > def details():
> > sb = tbook[select]
> > print 'Nickname: ', select, ' is selected\n'
> > print 'First name:\t', sb[0], '\n'
> > print 'Last name:\t', sb[1], '\n'
> > print 'Country:\t', sb[2], '\n'
> > print 'City:\t\t', sb[3], '\n'
> > print 'Phone number:\t',sb[4], '\n'
> > print 'Memos:\n'
> > print sb[5]
> >
> > print '\n\n(E)dit\n\n'
> > print '(B)ack to phonebook list\n\n'
> > menu = raw_input('What you wana do? ')
> > if menu == 'e':
> > edit()
> > if menu == 'b':
> > listpb()
> >
>
> Minor nitpick, but what if the user types 'B' or 'E' like in
> your printed menu?
>
> >
> > ## Select nickname
> > def selectm():
> > global select
> > select = raw_input('Type nickname and press : ')
> > if select == '':
> > listpb()
> > if select in tbook:
> > details()
> > else:
> > listpb()
>
>
> Remove all global variables when your program starts to work.
> Instead pass them as arguments and return them from functions.
> So do 'details( select )' instead of 'details()' and then in
> details, you would do edit( select ).
> >
> > ## List all contacts
> > def listpb():
> > print '_' *45, ' Phonebook ', '_' *45,'\n\n\n'
> >
> > print 'Nick\t\tF.Name\t\tL.Name\t\tCity\t\t\tRegion\t\tTel'
> > print '_' * 105,'\n','\t' * 13
> > for val in tbook.keys():
> > print val, '\t\t', tbook[val][0], '\t', tbook[val][1], '\t',
> > tbook[val][2], '\t\t', tbook[val][3], '\t\t', tbook[val][4],'\t\t\n'
> > print '_'*105,'\n\n'
> > selectm()
> >
> > ## Call list names
> > listpb()
>
> if __name__ == "__main__

Re: Slow termination of process

2012-03-27 Thread Roland Hedberg
So, I went for the low-hanging fruit and defined my own TCPServer class

class MyTCPServer(SocketServer.TCPServer):
def __init__(self, server_address, RequestHandlerClass):
self.allow_reuse_address = True
SocketServer.TCPServer.__init__(self, server_address,
RequestHandlerClass)

and then

httpd = MyTCPServer((hostname, port), Handler)
httpd.serve_forever()

and this solved my problem!

So, thanks again Chris!

27 mar 2012 kl. 15:55 skrev Chris Angelico:

> On Wed, Mar 28, 2012 at 12:03 AM, Roland Hedberg  wrote:
>> When the main script is done it closes down the HTTP server by doing:
>> 
>>op.terminate()
>> 
>> The problem I have is that if the main script is run again almost immediate 
>> then the old HTTP server
>> process doesn't seem to have released the port yet. So setting up a new 
>> server fails.
> 
> You may wish to consider a more orderly shutdown (send the server a
> signal upon which it shuts itself down), but the simplest and most
> direct solution is to set the SO_REUSEADDR flag.
> 
> http://docs.python.org/library/socket.html?highlight=so_reuseaddr
> 
> I've not actually used the TCPServer class myself, but a cursory
> glance at the docs suggests that it's possible if you subclass it:
> 
> http://docs.python.org/library/socketserver.html#SocketServer.BaseServer.allow_reuse_address
> 
> Chris Angelico
> -- 
> http://mail.python.org/mailman/listinfo/python-list

Roland

---
With anchovies there is no common ground 
-- Nero Wolfe

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


Re: OAuth 2.0 implementation

2012-03-27 Thread Roy Smith
In article 
<7909491.0.1332826232743.JavaMail.geo-discussion-forums@pbim5>,
 Demian Brecht  wrote:

> OAuth 2.0 is still in draft status (draft 25 is the current one I believe) 
> and yes, unfortunately every single server available at this point have 
> varying degrees of separation from the actual spec. It's not a 
> pseudo-standard, it's just not observed to the letter. Google is the closest 
> and Facebook seems to be the farthest away (Stack Exchange is in close second 
> due to building theirs to work like Facebook's).

In practice, OAuth is all about getting your site to work with Facebook.  
That is all most web sites care about today because that's where the 
money is.  The fact that other sites also use OAuth is of mostly 
academic interest at this point.

The next player on the list is Twitter, and they're not even up to using 
their own incompatible version of OAuth 2.0.  They're still using OAuth 
1.0 (although, I understand, they're marching towards 2.0).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Slow termination of process

2012-03-27 Thread Chris Angelico
On Wed, Mar 28, 2012 at 12:03 AM, Roland Hedberg  wrote:
> When the main script is done it closes down the HTTP server by doing:
>
>    op.terminate()
>
> The problem I have is that if the main script is run again almost immediate 
> then the old HTTP server
> process doesn't seem to have released the port yet. So setting up a new 
> server fails.

You may wish to consider a more orderly shutdown (send the server a
signal upon which it shuts itself down), but the simplest and most
direct solution is to set the SO_REUSEADDR flag.

http://docs.python.org/library/socket.html?highlight=so_reuseaddr

I've not actually used the TCPServer class myself, but a cursory
glance at the docs suggests that it's possible if you subclass it:

http://docs.python.org/library/socketserver.html#SocketServer.BaseServer.allow_reuse_address

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


Slow termination of process

2012-03-27 Thread Roland Hedberg
Hi!

I have an application/a script that is run from another application.
The script is mostly working as a HTTP client but in some cases it also has to 
act as a HTTP server.
Basically just for serving a few files. The files are dynamically created by 
the script when needed.

To accomplish this I'm trying to use subprocess Popen
and an extremely simple web server script which basically contains:

Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer((hostname, port), Handler)
httpd.serve_forever()

In the main script I do:

op = Popen(popen_args, stdout=PIPE, stderr=PIPE)

When the main script is done it closes down the HTTP server by doing:

op.terminate()

The problem I have is that if the main script is run again almost immediate 
then the old HTTP server 
process doesn't seem to have released the port yet. So setting up a new server 
fails.

Is there anything I can do to get the port released immediately when the 
tcpserver is terminated.
Or is there another way of doing this that will work better ?

Roland

---
With anchovies there is no common ground 
-- Nero Wolfe

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


Re: OAuth 2.0 implementation

2012-03-27 Thread Roland Hedberg
And then to complicate the picture you have OpenID Connect which is an attempt 
at
bringing OpenID and OAuth2.0 together.

By the way I have an implementation of OpenID Connect here:

https://github.com/rohe/pyoidc

-- Roland

27 mar 2012 kl. 11:59 skrev Stuart Bishop:

> On Tue, Mar 27, 2012 at 10:11 AM, Ben Finney  
> wrote:
>> Demian Brecht  writes:
>> 
>>> I'm getting close to an alpha release of an OAuth 2.0 implementation
>>> (https://github.com/demianbrecht/py-sanction).
>> 
>> Thank you for doing this work.
>> 
>> As someone who uses OpenID, what can I read about why OAuth is better?
> 
> They are different, and often you need to use both.
> 
> OpenID allows web sites to authenticate someone. It is not really
> useful for anything not an interactive web site. The consuming site
> never gets your keys, it just gets confirmation from the provider that
> the user is who they claim they are and maybe some details that the
> provider chooses to provide such as an email address.
> 
> OAuth is for generating authentication keys that allow a program to
> authenticate as someone and perform operations on their behalf. You
> use OAuth to generate a key so that Foursquare can send messages via
> Twitter on your behalf, or so the Facebook client on your phone can
> access your account without storing your password. You also get
> authentication here, as you can't generate a key without being
> authenticated, but the real reason it is used instead of OpenID is so
> you can keep the key and keep using it to act as the user; you can
> keep using that key until it expires or it is revoked.
> 
> Authentication providers that don't provide a webapi just implement
> OpenID. Big sites like Google and Facebook implement both OpenID (for
> 'log in with your GMail account') and OAuth ('post this message to
> your Facebook wall').
> 
> -- 
> Stuart Bishop 
> http://www.stuartbishop.net/
> -- 
> http://mail.python.org/mailman/listinfo/python-list

Roland

---
With anchovies there is no common ground 
-- Nero Wolfe

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


Re: Tools for refactoring/obfuscation

2012-03-27 Thread Stefan Behnel
Javier, 07.03.2012 04:29:
> I am looking for an automated tool for refactoring/obfuscation.

Sadly, there really is one thing that these two have in common: they modify
code while retaining its exact functionality. Apart from that, they are
diametric opposites. Refactoring aims at making the code "better" in some
way, e.g. by making it more readable or easier to extend. Obfuscation aims
for making it worse, as in unreadable and hard to access. It's generally
not a good idea to do that. Code is there for humans to read.

Stefan

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


Re: Your Regex Brain

2012-03-27 Thread Peter J. Holzer
["Followup-To:" header set to comp.lang.perl.misc.]
On 2012-03-27 00:02, s...@netherlands.com  wrote:
> This is more like a regex brain.
>
> '
>   (?=\s) 
>   (?= (?:[^>"\']|"[^"]*"|\'[^\']*\')*? (?<=\s) width \s*=
>   (?: (?> \s* ([\'"]) \s* (?.*?) \s* \g{-2} )
> | (?> (?!\s*[\'"]) \s* (?[^\s>]*) (?=\s|>) )   
>   )
>   )
>   (?= (?:[^>"\']|"[^"]*"|\'[^\']*\')*? (?<=\s) src \s*=
>   (?: (?> \s* ([\'"]) \s* (?.*?) \s* \g{-2} )
> | (?> (?!\s*[\'"]) \s* (?[^\s>]*) (?=\s|>) )   
>   )
>   )
>   (?= (?:[^>"\']|"[^"]*"|\'[^\']*\')*? (?<=\s) height \s*=
>   (?: (?> \s* ([\'"]) \s* (?.*?) \s* \g{-2} )
> | (?> (?!\s*[\'"]) \s* (?[^\s>]*) (?=\s|>) )   
>   )
>   )
>   (?= (?:[^>"\']|"[^"]*"|\'[^\']*\')*? (?<=\s) alt \s*=
>   (?: (?> \s* ([\'"]) \s* (?.*?) \s* \g{-2} )
> | (?> (?!\s*[\'"]) \s* (?[^\s>]*) (?=\s|>) )   
>   )
>   )
>   (?> \s+ (?:".*?"|\'.*?\'|[^>]*?)+ > ) (?)
> '

"This is your brain on drugs."

SCNR,
hp


-- 
   _  | Peter J. Holzer| Deprecating human carelessness and
|_|_) | Sysadmin WSR   | ignorance has no successful track record.
| |   | h...@hjp.at | 
__/   | http://www.hjp.at/ |  -- Bill Code on a...@irtf.org
-- 
http://mail.python.org/mailman/listinfo/python-list


Socket Error : Address still in use (Conveting from python 1.5.2 to 2.7.1)

2012-03-27 Thread Wong Wah Meng-R32813
Hello there,

I am in the midst of converting my application from python 1.5.2 to python 
2.7.1 on HP-UX 11 Itanium box. 

My application server will set a listening port, accepting request from 
multiple clients. The code just works fine in the old python environment. E.g. 
when I do a lsof | grep  I got the following.

python 62602  genasm5u  IPv4 0x7350d1f00t0  TCP 
zmy02aix02:12121 (LISTEN)
python 62602  genasm6u  IPv4 0x744fb5f00t0  TCP 
zmy02aix02:12121->zmy02aix02-bkup:51867 (ESTABLISHED)
python 62602  genasm7u  IPv4 0x75b959f00t0  TCP 
zmy02aix02:12121->zmy02aix02-bkup:51869 (ESTABLISHED)
python 62602  genasm8u  IPv4 0x75a559f00t0  TCP 
zmy02aix02:12121->zmy02aix02-bkup:51873 (ESTABLISHED)

Strange things happened in python 2.7.1. Without modifying the code of how the 
socket was created and how the TCP/IP address was bound to the socket, it seems 
that every other processes that I run, which supposed to connect to the 
listening port as a client program, also appears to be holding a listening 
port. This is weird. Anyone has encountered this before especially when you 
were converting from an old python to a new python? Like you can see below 
there are 5 processes hosting the listening port of 18882.

$ lsof -i tcp | grep 18882
python  10598 r328133u  IPv4 0xe0050b73e400   0t0  TCP 
zmy02hp3.ap.freescale.net:18882 (LISTEN)
python  18181 r328133u  IPv4 0xe0050b73e400   0t0  TCP 
zmy02hp3.ap.freescale.net:18882 (LISTEN)
python  20025 r328133u  IPv4 0xe0050b73e400   0t0  TCP 
zmy02hp3.ap.freescale.net:18882 (LISTEN)
python  26295 r328133u  IPv4 0xe0050b73e400   0t0  TCP 
zmy02hp3.ap.freescale.net:18882 (LISTEN)
python  26428 r328133u  IPv4 0xe0050b73e400   0t0  TCP 
zmy02hp3.ap.freescale.net:18882 (LISTEN)

Since only one of them is the genuine process holding the port, I need to kill 
off the rest of the process if I need to restart the genuine process running 
under that port. It should not work this way. 

Here is the code of the application process that hosts the listening port. 

self.sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
self.sock.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 )
self.sock.setsockopt( socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1
self.sock.setsockopt( socket.IPPROTO_TCP, _TCP_NODELAY, 1 )
self.sock.bind( self.server_address )
  

Here is the client code that does the connection.

   self.sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
   self.sock.setsockopt( socket.IPPROTO_TCP, _TCP_NODELAY, 1 )
   self.sock.connect( self.server_address )

Regards,
Wah Meng

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


Re: OAuth 2.0 implementation

2012-03-27 Thread Stuart Bishop
On Tue, Mar 27, 2012 at 10:11 AM, Ben Finney  wrote:
> Demian Brecht  writes:
>
>> I'm getting close to an alpha release of an OAuth 2.0 implementation
>> (https://github.com/demianbrecht/py-sanction).
>
> Thank you for doing this work.
>
> As someone who uses OpenID, what can I read about why OAuth is better?

They are different, and often you need to use both.

OpenID allows web sites to authenticate someone. It is not really
useful for anything not an interactive web site. The consuming site
never gets your keys, it just gets confirmation from the provider that
the user is who they claim they are and maybe some details that the
provider chooses to provide such as an email address.

OAuth is for generating authentication keys that allow a program to
authenticate as someone and perform operations on their behalf. You
use OAuth to generate a key so that Foursquare can send messages via
Twitter on your behalf, or so the Facebook client on your phone can
access your account without storing your password. You also get
authentication here, as you can't generate a key without being
authenticated, but the real reason it is used instead of OpenID is so
you can keep the key and keep using it to act as the user; you can
keep using that key until it expires or it is revoked.

Authentication providers that don't provide a webapi just implement
OpenID. Big sites like Google and Facebook implement both OpenID (for
'log in with your GMail account') and OAuth ('post this message to
your Facebook wall').

-- 
Stuart Bishop 
http://www.stuartbishop.net/
-- 
http://mail.python.org/mailman/listinfo/python-list