Re: cross platform alternative for signal.SIGALRM?

2015-11-11 Thread Chris Angelico
On Thu, Nov 12, 2015 at 5:43 PM, Christian Gollwitzer  wrote:
> My understanding of async is that it creates an event loop. In which case
> the loop has no chance to run within a block of code that computes anything,
> is that correct?

This is correct. At its simplest, asynchronous code is an abstraction
over the select() call, which basically says "Hey system, tell me when
(a) I can read from here, (b) I can write to here, or (c) I've been
waiting this long". The most common use is sockets; a web server has
its main listening socket (it becomes readable when someone connects),
any clients that haven't finished sending their requests yet (they
become readable when more data arrives), any clients that you're still
sending to (they become writeable when there's room in their output
buffers), and maybe some sort of periodic checks ("every hour, do
maintenance"). Whenever you finish a bit of processing (reading from a
client, sending to a client, whatever), you return to the "event
loop", which in this case would be select().

An async library makes all this look a lot cleaner in your code, but
ultimately, it's not preemptive. You still have to make sure the
processing doesn't take too long.

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


Re: cross platform alternative for signal.SIGALRM?

2015-11-11 Thread Ulli Horlacher
Marko Rauhamaa  wrote:

> I'm thinking the only portable way is to run a watchdog process with
> subprocess or multiprocessing.

How can a subprocess interrupt a function in another process?

For example: waiting for user input with a timeout.

raw_input("Hit ENTER to continue or wait 10 s")



-- 
Ullrich Horlacher  Server und Virtualisierung
Rechenzentrum IZUS/TIK E-Mail: horlac...@tik.uni-stuttgart.de
Universitaet Stuttgart Tel:++49-711-68565868
Allmandring 30aFax:++49-711-682357
70550 Stuttgart (Germany)  WWW:http://www.tik.uni-stuttgart.de/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: cross platform alternative for signal.SIGALRM?

2015-11-11 Thread Ulli Horlacher
Terry Reedy  wrote:
> On 11/11/2015 11:16 AM, Ulli Horlacher wrote:
> > I am rewriting a Perl program into Python (2.7).
> 
> I recommend using 3.4+ if you possibly can.

It is not possible.
The main target platform offers only python 2.7

-- 
Ullrich Horlacher  Server und Virtualisierung
Rechenzentrum IZUS/TIK E-Mail: horlac...@tik.uni-stuttgart.de
Universitaet Stuttgart Tel:++49-711-68565868
Allmandring 30aFax:++49-711-682357
70550 Stuttgart (Germany)  WWW:http://www.tik.uni-stuttgart.de/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: cross platform alternative for signal.SIGALRM?

2015-11-11 Thread Christian Gollwitzer

Am 12.11.15 um 07:14 schrieb Marko Rauhamaa:

Terry Reedy :


The cross-platform 3.4 asyncio module has some functions with
timeouts.


Even that doesn't forcefully interrupt an obnoxious blocking function
call like

time.sleep(1)


A blocking call - granted. But what happens in a blocking loop, i.e.

for i in range(10):
pass

?

My understanding of async is that it creates an event loop. In which 
case the loop has no chance to run within a block of code that computes 
anything, is that correct? Or does it hook into the interpreter and is 
able to interrupt the program between bytecodes?



I'm thinking the only portable way is to run a watchdog process with
subprocess or multiprocessing.


What about a thread which calls exit() after the timeout? Does that 
forcefully kill the whole process?


Christian
--
https://mail.python.org/mailman/listinfo/python-list


Re: new to python, help please !!

2015-11-11 Thread Marko Rauhamaa
Steven D'Aprano :

> On Thursday 12 November 2015 04:48, Quivis wrote:
>
>> On Wed, 11 Nov 2015 08:34:30 -0800, Anas Belemlih wrote:
>> 
>>> md5
>> 
>> If those are md5 values stored inside files, wouldn't it be easier to
>> just hash them?
>> 
>> import hashlib
>> 
>> m1 = hashlib.sha224(open('f1').read()).hexdigest()
>> m2 = hashlib.sha224(open('f2').read()).hexdigest()
>
> I presume that the purpose of the exercise is to learn basic Python
> skills like looping.

And if you really wanted to compare two files that are known to contain
MD5 checksums, the simplest way is:

   with open('f1.md5') as f1, open('f2.md5') as f2:
   if f1.read() == f2.read():
   ...
   else:
   ...


Marko
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: cross platform alternative for signal.SIGALRM?

2015-11-11 Thread Marko Rauhamaa
Terry Reedy :

> The cross-platform 3.4 asyncio module has some functions with
> timeouts.

Even that doesn't forcefully interrupt an obnoxious blocking function
call like

   time.sleep(1)

The original question claimed signal.alarm() would do the trick in
Linux. However, even that cannot be relied on as "man alarm" states:

   sleep(3) may be implemented using SIGALRM; mixing calls to alarm()
   and sleep(3) is a bad idea.

I'm thinking the only portable way is to run a watchdog process with
subprocess or multiprocessing.


Marko
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: cross platform alternative for signal.SIGALRM?

2015-11-11 Thread Cameron Simpson

On 11Nov2015 16:16, Ulli Horlacher  wrote:

I am rewriting a Perl program into Python (2.7).
It must run on Linux and Windows.
With Linux I have no problems, but Windows... :-(

The current show stopper is signal.SIGALRM which is not available on
Windows:

 File "fexit.py", line 674, in formdata_post
   signal.signal(signal.SIGALRM,timeout_handler)
 AttributeError: 'module' object has no attribute 'SIGALRM'

 https://docs.python.org/2/library/signal.html

 signal.alarm(time) (...) Availability: Unix.

Perl for Windows has had SIGALRM support (or some kind of emulation).

Ok, I have to redesign this part of my code:

 def timeout_handler(sig,frame):
   raise ValueError("timeout!")

 signal.signal(signal.SIGALRM,timeout_handler)

 while True:
   chunk = fileo.read(bs)
   sock.sendall(chunk)
   (...)

What is the best practise for a cross platform timeout handler?


I suggest you look at the socket.settimeout function. Avoid SIGALRM altogether.  
Then (untested):


 import socket
 ...
 socket.settimeout(timeout_in_seconds)
 ...
 while True:
   ...
   chunk = fileo.read(bs)
   try:
 sock.sendall(chunk)
   except socket.timeout as e:
 ... complain about timeout, reciting "e" in the message ...

Cheers,
Cameron Simpson 

I think you're confusing "recognizing" and "understanding" with "caring".
The net is cruel, sometimes, but always fair.
   - Rick Gordon 
--
https://mail.python.org/mailman/listinfo/python-list


Re: Python.exe is not a valid Win32 application error message

2015-11-11 Thread Steve Hayes
On Wed, 11 Nov 2015 08:39:21 -0500, Dennis Lee Bieber
 wrote:

>On Tue, 10 Nov 2015 00:34:23 + (UTC), "M. Kamisato via Python-list"
> declaimed the following:
>
>>I am running python on Windows XP SP3 and download version 3.5xx.  I got the 
>>above error message and could not run the program.
>>I have downloaded Python version 2.7xx and it runs fine.
>>Is there any way I can get version 3.5xx to run on my computer?
>>Mel KamisatoBuena Park, CA
>
>   Install Windows Vista, 7, 8.1, or 10.
>
>   Windows XP is not a supported OS for Python 3.5+

Or revert to an earlier version of Python that does work. 


-- 
Steve Hayes from Tshwane, South Africa
Web:  http://www.khanya.org.za/stevesig.htm
Blog: http://khanya.wordpress.com
E-mail - see web page, or parse: shayes at dunelm full stop org full stop uk
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python.exe is not a valid Win32 application error message

2015-11-11 Thread Steve Hayes
On Thu, 12 Nov 2015 00:13:29 +1100, Chris Angelico 
wrote:

>On Tue, Nov 10, 2015 at 11:34 AM, M. Kamisato via Python-list
> wrote:
>> I am running python on Windows XP SP3 and download version 3.5xx.  I got the 
>> above error message and could not run the program.
>> I have downloaded Python version 2.7xx and it runs fine.
>> Is there any way I can get version 3.5xx to run on my computer?
>> Mel KamisatoBuena Park, CA
>
>You can't get 3.5 to run on XP, no; your options are:
>
>1) Install Python 3.4, which does support XP
>2) Upgrade to a newer version of Windows (anything from Vista onward
>will run 3.5; to save having to do this in future, jump straight to 7
>or 10)
>3) Make the jump to Linux or FreeBSD or some other OS.

That is useful to know. 

I get messages (from Glary Utilities) that some of my programs
(including Python) need to be updated, but when I've downloaded and
updated them, the update hasn't worked. 


-- 
Steve Hayes from Tshwane, South Africa
Web:  http://www.khanya.org.za/stevesig.htm
Blog: http://khanya.wordpress.com
E-mail - see web page, or parse: shayes at dunelm full stop org full stop uk
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to get 'od' run?

2015-11-11 Thread Michael Torrie
On 11/11/2015 08:21 PM, Michael Torrie wrote:
> On 11/11/2015 08:04 PM, fl wrote:
>> Hi,
>>
>> I am learning python. I see a previous post has such code:
>>
>>
>>
>>
>>
>>>>> data = '"binääridataa"\n'.encode('utf-8') 
>>>>> f = open('roska.txt', 'wb') 
>>>>> f.write(data) 
>>17 
>>>>> f.close() 
>>
>> The .encode methods produced a bytestring, which Python likes to display 
>> as ASCII characters where it can and in hexadecimal where it cannot: 
>>
>>>>> data 
>>b'"bin\xc3\xa4\xc3\xa4ridataa"\n' 
>>
>> An "octal dump" in characters (where ASCII, otherwise apparently octal) 
>> and the corresponding hexadecimal shows that it is, indeed, these bytes 
>> that ended up in the file: 
>>
>> $ od -t cx1 roska.txt 
>  ^^^
> This is most likely a bash prompt. Therefore "od" is a program on your
> computer.  Nothing to do with Python at all.
> 
> To get Python to display \x## hex codes for non-ascii characters in a
> byte stream, you can print out the repr() of the byte string.  For example:
> 
> print (repr(my_unicode_string.encode('utf-8')))

Also there are numerous recipes for doing standard hex dumps out there.
 For example,

http://code.activestate.com/recipes/142812-hex-dumper/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to get 'od' run?

2015-11-11 Thread Michael Torrie
On 11/11/2015 08:04 PM, fl wrote:
> Hi,
> 
> I am learning python. I see a previous post has such code:
> 
> 
> 
> 
> 
>>>> data = '"binääridataa"\n'.encode('utf-8') 
>>>> f = open('roska.txt', 'wb') 
>>>> f.write(data) 
>17 
>>>> f.close() 
> 
> The .encode methods produced a bytestring, which Python likes to display 
> as ASCII characters where it can and in hexadecimal where it cannot: 
> 
>>>> data 
>b'"bin\xc3\xa4\xc3\xa4ridataa"\n' 
> 
> An "octal dump" in characters (where ASCII, otherwise apparently octal) 
> and the corresponding hexadecimal shows that it is, indeed, these bytes 
> that ended up in the file: 
> 
> $ od -t cx1 roska.txt 
 ^^^
This is most likely a bash prompt. Therefore "od" is a program on your
computer.  Nothing to do with Python at all.

To get Python to display \x## hex codes for non-ascii characters in a
byte stream, you can print out the repr() of the byte string.  For example:

print (repr(my_unicode_string.encode('utf-8')))


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


How to get 'od' run?

2015-11-11 Thread fl
Hi,

I am learning python. I see a previous post has such code:





   >>> data = '"binääridataa"\n'.encode('utf-8') 
   >>> f = open('roska.txt', 'wb') 
   >>> f.write(data) 
   17 
   >>> f.close() 

The .encode methods produced a bytestring, which Python likes to display 
as ASCII characters where it can and in hexadecimal where it cannot: 

   >>> data 
   b'"bin\xc3\xa4\xc3\xa4ridataa"\n' 

An "octal dump" in characters (where ASCII, otherwise apparently octal) 
and the corresponding hexadecimal shows that it is, indeed, these bytes 
that ended up in the file: 

$ od -t cx1 roska.txt 


When I run the above line with python 2.7, it does not recognize 'od'.

Is it from a package? Or some internal function?

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


Re: new to python, help please !!

2015-11-11 Thread Steven D'Aprano
On Thursday 12 November 2015 04:48, Quivis wrote:

> On Wed, 11 Nov 2015 08:34:30 -0800, Anas Belemlih wrote:
> 
>> md5
> 
> If those are md5 values stored inside files, wouldn't it be easier to
> just hash them?
> 
> import hashlib
> 
> m1 = hashlib.sha224(open('f1').read()).hexdigest()
> m2 = hashlib.sha224(open('f2').read()).hexdigest()

I presume that the purpose of the exercise is to learn basic Python skills 
like looping.

Also, using sha224 when all you want is a simple "different"/"equal" is 
horribly inefficient. Sha224 needs to read the entire file, every single 
byte, *and* perform a bunch of expensive cryptographic operations. Consider 
reading two five GB files, the first starting with byte \x30 and the second 
starting with byte \x60. The two bytes are different, so we know the files 
differ, but sha224 still needs to do a massive amount of work.



-- 
Steve

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


Re: More tkinter Madness

2015-11-11 Thread Chris Angelico
On Thu, Nov 12, 2015 at 12:52 PM, Tim Daneliuk
 wrote:
> I am the author of twander (https://www.tundraware.com/Software/twander).
> This code has run flawlessly for years on FreeBSD, Linux, MacOS and
> Windows.  Some months ago, I put it on a couple of VPS servers (FreeBSD
> and Linux) and BOOM, it doesn't run.  I asked around here and got some
> suggestions and then did some homework.
>
> Traceback (most recent call last):
>   File "/usr/lib64/python2.6/runpy.py", line 122, in _run_module_as_main

It's running here under Python 2.6. Sorry if this is a question you've
already been asked, but were you successfully running under 2.6
elsewhere? Might be a versioning issue.

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


Re: More tkinter Madness

2015-11-11 Thread Paul Rubin
Tim Daneliuk  writes:
> Some months ago, I put it on a couple of VPS servers (FreeBSD
> and Linux) and BOOM, it doesn't run.  I asked around here and got some
> suggestions and then did some homework.

I'd expect a VPS server to have no display--is it an X client forward to
your workstation?  Are all the proper X libraries installed?  Note that
X port forwarding over the internet is generally unusable even if your
internet connection is pretty fast.  It's kind of ok over a LAN.
-- 
https://mail.python.org/mailman/listinfo/python-list


More tkinter Madness

2015-11-11 Thread Tim Daneliuk
I am the author of twander (https://www.tundraware.com/Software/twander).
This code has run flawlessly for years on FreeBSD, Linux, MacOS and
Windows.  Some months ago, I put it on a couple of VPS servers (FreeBSD
and Linux) and BOOM, it doesn't run.  I asked around here and got some
suggestions and then did some homework.

I see the error being thrown by using the trace module, but it's not 
terribly meaningful to me.  Any ideas of what this means - again,
I emphasize this is only happening on VPS hosts:

 --- modulename: Tkinter, funcname: _cnfmerge
Tkinter.py(76): if type(cnfs) is DictionaryType:
Tkinter.py(77): return cnfs
Tkinter.py(1046): res = ()
Tkinter.py(1047): for k, v in cnf.items():
Tkinter.py(1048): if v is not None:
Tkinter.py(1049): if k[-1] == '_': k = k[:-1]
Tkinter.py(1050): if callable(v):
Tkinter.py(1052): elif isinstance(v, (tuple, list)):
Tkinter.py(1064): res = res + ('-'+k, v)
Tkinter.py(1047): for k, v in cnf.items():
Tkinter.py(1048): if v is not None:
Tkinter.py(1049): if k[-1] == '_': k = k[:-1]
Tkinter.py(1050): if callable(v):
Tkinter.py(1052): elif isinstance(v, (tuple, list)):
Tkinter.py(1064): res = res + ('-'+k, v)
Tkinter.py(1047): for k, v in cnf.items():
Tkinter.py(1048): if v is not None:
Tkinter.py(1049): if k[-1] == '_': k = k[:-1]
Tkinter.py(1050): if callable(v):
Tkinter.py(1052): elif isinstance(v, (tuple, list)):
Tkinter.py(1064): res = res + ('-'+k, v)
Tkinter.py(1047): for k, v in cnf.items():
Tkinter.py(1065): return res
Traceback (most recent call last):
  File "/usr/lib64/python2.6/runpy.py", line 122, in _run_module_as_main
"__main__", fname, loader, pkg_name)
  File "/usr/lib64/python2.6/runpy.py", line 34, in _run_code
exec code in run_globals
  File "/usr/lib64/python2.6/trace.py", line 823, in 
main()
  File "/usr/lib64/python2.6/trace.py", line 811, in main
t.runctx(code, globs, globs)
  File "/usr/lib64/python2.6/trace.py", line 512, in runctx
exec cmd in globals, locals
  File "/local/TundraWare/bin/twander.py", line 5464, in 
UI = twanderUI(UIroot)
  File "/local/TundraWare/bin/twander.py", line 2152, in __init__
self.CmdBtn = Menubutton(self.mBar, text=COMMANDMENU, underline=0, 
state=DISABLED)
  File "/usr/lib64/python2.6/lib-tk/Tkinter.py", line 2710, in __init__
Widget.__init__(self, master, 'menubutton', cnf, kw)
  File "/usr/lib64/python2.6/lib-tk/Tkinter.py", line 1932, in __init__
(widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: cross platform alternative for signal.SIGALRM?

2015-11-11 Thread Terry Reedy

On 11/11/2015 11:16 AM, Ulli Horlacher wrote:

I am rewriting a Perl program into Python (2.7).


I recommend using 3.4+ if you possibly can.


It must run on Linux and Windows.
With Linux I have no problems, but Windows... :-(

The current show stopper is signal.SIGALRM which is not available on
Windows:



Perl for Windows has had SIGALRM support (or some kind of emulation).

Ok, I have to redesign this part of my code:

   def timeout_handler(sig,frame):
 raise ValueError("timeout!")

   signal.signal(signal.SIGALRM,timeout_handler)

   while True:
 chunk = fileo.read(bs)
 sock.sendall(chunk)
 (...)

What is the best practise for a cross platform timeout handler?


The cross-platform 3.4 asyncio module has some functions with timeouts.
(3.5 has new 'async' syntac which supposedly makes it easier to use.  I 
have not looked at this yet.)


For instance: coroutine asyncio.wait(futures, *, loop=None, 
timeout=None, return_when=ALL_COMPLETED)
Wait for the Futures and coroutine objects given by the sequence 
futures to complete. Coroutines will be wrapped in Tasks. Returns two 
sets of Future: (done, pending).

...
Usage:
  done, pending = yield from asyncio.wait(fs)

I believe the backport on pypi.python.org, called tulip, works on 2.7.

In the example above, the read/send would be a task.  Wait on the task, 
and when it returns, cancel the task if in pending.



--
Terry Jan Reedy

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


Re: cross platform alternative for signal.SIGALRM?

2015-11-11 Thread Ulli Horlacher
Marko Rauhamaa  wrote:

> Correct. The timer callback function (hello) would be called in a
> separate thread. An exception raised in one thread cannot be caught in
> the main thread. In general, there is no way for a thread to interrupt a
> sibling thread that is in a blocking function call.

Then threading.Timer is not a solution for my problem.

-- 
Ullrich Horlacher  Server und Virtualisierung
Rechenzentrum IZUS/TIK E-Mail: horlac...@tik.uni-stuttgart.de
Universitaet Stuttgart Tel:++49-711-68565868
Allmandring 30aFax:++49-711-682357
70550 Stuttgart (Germany)  WWW:http://www.tik.uni-stuttgart.de/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: using binary in python

2015-11-11 Thread Christian Gollwitzer

Am 10.11.15 um 22:29 schrieb kent nyberg:

On Mon, Nov 09, 2015 at 10:20:25PM -0800, Larry Hudson via Python-list wrote:

Your questions are somewhat difficult to answer because you misunderstand
binary.  The key is that EVERYTHING in a computer is binary.  There are NO
EXCEPTIONS, it's all binary ALL the time.  The difference comes about in how
this binary data is displayed and manipulated.  I want to emphasize, ALL the
DATA is binary.



Thanks alot for taking the time.
I get it now. I sort of, but not fully, misunderstood the conecpt of binary 
files.
The thing I was after; and the thing Im playing with now after a more 
succesfull time with google,
is writing more specific things to a file than just strings.
English is not my native language so please forgive me, but
I wanted to write specifc 16bit codes, and read them. And later play with 
bitwise operations on them. Sort of.
It might not make sense at all, but hey..  it doesnt have to


I think I understand what you want. Look at the struct module:

https://docs.python.org/2/library/struct.html

You can write/read binary data from files with standard means. Using 
struct, you can interpret or format integer values into a specific 
binary format. That would allow to create a reader or writer for a given 
binary format in Python.


Christian


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


Re: Using subprocess to capture a progress line

2015-11-11 Thread Tim Johnson
* Chris Warrick  [15 07:54]:
> On 11 November 2015 at 17:16, Tim Johnson  wrote:
> >> (2) [don’t do it] do you need to intercept the lines? If you don’t set
> >> stderr= and stdout=, things will print just fine.
> >   Got to try that before using the module, just for edification.
> 
> At which point your initial code sample will become:
> ###
> p = subprocess.Popen(list(args))
> ###
> 
  Yeah, 'list is redundant.
  Progress is now showing, but I forgot to say that I've lost the
  original intent, and that was to examine each line so that I could
  pull out the title.

  No matter. I'm on the way to make the youtube_dl module working.
  cheers

-- 
Tim 
http://www.akwebsoft.com, http://www.tj49.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: cross platform alternative for signal.SIGALRM?

2015-11-11 Thread Marko Rauhamaa
Ulli Horlacher :

> Hmmm... not so simple for me. My test code:
>
> from time import *
> import threading
> import sys
>
> def hello(): 
> raise ValueError("hello!!!")
>
> t = threading.Timer(3.0,hello)
> t.start()
> try:
>   print "start"
>   sleep(5)
>   print "end"
> except ValueError as e:
>   print e.args[0]
>   sys.exit(1)

Correct. The timer callback function (hello) would be called in a
separate thread. An exception raised in one thread cannot be caught in
the main thread. In general, there is no way for a thread to interrupt a
sibling thread that is in a blocking function call.


Marko
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python.exe is not a valid Win32 application error message

2015-11-11 Thread Michael Torrie
On 11/11/2015 10:21 AM, Quivis wrote:
> On Tue, 10 Nov 2015 00:34:23 +, M. Kamisato wrote:
> 
>> I am running python on Windows XP SP3 and download version 3.5xx.  I got
>> the above error message and could not run the program.
>> I have downloaded Python version 2.7xx and it runs fine.
>> Is there any way I can get version 3.5xx to run on my computer?
>> Mel KamisatoBuena Park, CA
> 
> 
> Did you DL the 64-bit version...?

In this case, it would not matter; Windows XP is simply too old to run
Python 3.5, but the web page neglects to mention that, and the installer
does not yet warn the user, though it will in the next minor version
release.

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


Re: new to python, help please !!

2015-11-11 Thread Ben Finney
Anas Belemlih  writes:

> i am  a beginning programmer,  i am trying to write a simple code to
> compare two character sets in 2 seperate files. ( 2 hash value files
> basically)

Welcome, and congratulations on arriving at Python for your programming!

As a beginning programmer, you will benefit from joining the ‘tutor’
forum https://mail.python.org/mailman/listinfo/tutor>, which is
much better suited to collaborative teaching of newcomers.

-- 
 \ “As scarce as truth is, the supply has always been in excess of |
  `\   the demand.” —Josh Billings |
_o__)  |
Ben Finney

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


Re: cross platform alternative for signal.SIGALRM?

2015-11-11 Thread Ulli Horlacher
Marko Rauhamaa  wrote:
> Ulli Horlacher :
> 
> > What is the best practise for a cross platform timeout handler?
> 
> Here's the simplest answer:
> 
>https://docs.python.org/3/library/threading.html#threading.Timer
> 
> (Also available in Python 2.)

Hmmm... not so simple for me. My test code:

from time import *
import threading
import sys

def hello(): 
raise ValueError("hello!!!")

t = threading.Timer(3.0,hello)
t.start()
try:
  print "start"
  sleep(5)
  print "end"
except ValueError as e:
  print e.args[0]
  sys.exit(1)


gives:


start
Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 551, in __bootstrap_inner
self.run()
  File "/usr/lib/python2.7/threading.py", line 759, in run
self.function(*self.args, **self.kwargs)
  File "x.py", line 7, in hello
def hello(): raise ValueError("hello!!!")
ValueError: hello!!!

end



-- 
Ullrich Horlacher  Server und Virtualisierung
Rechenzentrum IZUS/TIK E-Mail: horlac...@tik.uni-stuttgart.de
Universitaet Stuttgart Tel:++49-711-68565868
Allmandring 30aFax:++49-711-682357
70550 Stuttgart (Germany)  WWW:http://www.tik.uni-stuttgart.de/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: new to python, help please !!

2015-11-11 Thread Tim Chase
On 2015-11-11 08:34, Anas Belemlih wrote:
> i am  a beginning programmer,  i am trying to write a simple code
> to compare two character sets in 2 seperate files. ( 2 hash value
> files basically) idea is: open both files, measure the length of
> the  loop on.
> 
> if the length doesn't match, ==  files do not  match
> 
> if length matchs, loop  while comparing each character from each
> file if they match. please tell me what i am doing wrong ?  i am
> using python 2.7
> 
> **
> hash1= open ("file1.md5", "r")
> line1 =hash1.read()
> hash2 = open("file2.md5","r")
> line2= hash2.read()
> 
> number1 = len(line1)
> number2 = len(line2)
> 
> #**
> i=0
> s1=line1[i]
> s2=line2[i]
> count = 0
> 
> if number1 != number2:
>   print " hash table not the same size"
> else:
> while count < number1:
>   if s1 == s2:
>   print " character", line1[i]," matchs"
>   i=i+1
>   count=count+1
>   else
>   print "Hash values corrupt"

Well, the immediate answer is that you don't update s1 or s2 inside
your loop.  Also, the indent on "count=count+1" is wrong.  Finally,
if the hashes don't match, you don't break out of your while loop.
That said, the pythonesque way of writing this would likely look
something much more like

  with open("file1.md5") as a, open("file2.md5") as b:
for s1, s2 in zip(a, b):
  if s1 != s2:
print("Files differ")

You can compare the strings to get the actual offset if you want, or
check the lengths if you really want a more verbatim translation of
your code:

  with open("file1.md5") as a, open("file2.md5") as b:
for s1, s2 in zip(a, b):
  if len(s1) != len(s2):
print("not the same size")
  else:
for i, (c1, c2) in enumerate(zip(s1, s2)):
  if c1 == c2:
print(" character %s matches" %  c1)
  else:
print(" %r and %r differ at position %i" % (s1, s2, i))

-tkc



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


Re: new to python, help please !!

2015-11-11 Thread John Gordon
In <93aef8e5-3d6f-41f4-a625-cd3c20076...@googlegroups.com> Anas Belemlih 
 writes:

> i=0
> s1=line1[i]
> s2=line2[i]
> count = 0

> if number1 != number2:
>   print " hash table not the same size"
> else:
> while count < number1:
>   if s1 == s2:
>   print " character", line1[i]," matchs"
>   i=i+1
>   count=count+1
>   else
>   print "Hash values corrupt"

It looks like you're expecting s1 and s2 to automatically update their
values when i gets incremented, but it doesn't work like that.  When you
increment i, you also have to reassign s1 and s2.

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

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


Re: Using subprocess to capture a progress line

2015-11-11 Thread Chris Warrick
On 11 November 2015 at 17:16, Tim Johnson  wrote:
>> (2) [don’t do it] do you need to intercept the lines? If you don’t set
>> stderr= and stdout=, things will print just fine.
>   Got to try that before using the module, just for edification.

At which point your initial code sample will become:
###
p = subprocess.Popen(list(args))
###

(is list(args) really necessary? Wouldn’t plain Popen(args) just work?)

-- 
Chris Warrick 
PGP: 5EAAEA16
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: cross platform alternative for signal.SIGALRM?

2015-11-11 Thread Marko Rauhamaa
Ulli Horlacher :

> What is the best practise for a cross platform timeout handler?

Here's the simplest answer:

   https://docs.python.org/3/library/threading.html#threading.Timer

(Also available in Python 2.)


Marko
-- 
https://mail.python.org/mailman/listinfo/python-list


new to python, help please !!

2015-11-11 Thread Anas Belemlih
i am  a beginning programmer,  i am trying to write a simple code to compare 
two character sets in 2 seperate files. ( 2 hash value files basically)
idea is:
 open both files, measure the length of the  loop on.

if the length doesn't match, ==  files do not  match

if length matchs, loop  while comparing each character from each file if they 
match. 
 please tell me what i am doing wrong ?  i am using python 2.7

**
hash1= open ("file1.md5", "r")
line1 =hash1.read()
hash2 = open("file2.md5","r")
line2= hash2.read()

number1 = len(line1)
number2 = len(line2)

#**
i=0
s1=line1[i]
s2=line2[i]
count = 0

if number1 != number2:
print " hash table not the same size"
else:
while count < number1:
if s1 == s2:
print " character", line1[i]," matchs"
i=i+1
count=count+1
else
print "Hash values corrupt"
-- 
https://mail.python.org/mailman/listinfo/python-list


cross platform alternative for signal.SIGALRM?

2015-11-11 Thread Ulli Horlacher
I am rewriting a Perl program into Python (2.7).
It must run on Linux and Windows.
With Linux I have no problems, but Windows... :-(

The current show stopper is signal.SIGALRM which is not available on
Windows:

  File "fexit.py", line 674, in formdata_post
signal.signal(signal.SIGALRM,timeout_handler)
  AttributeError: 'module' object has no attribute 'SIGALRM'


  https://docs.python.org/2/library/signal.html

  signal.alarm(time) (...) Availability: Unix.

Perl for Windows has had SIGALRM support (or some kind of emulation).

Ok, I have to redesign this part of my code:

  def timeout_handler(sig,frame): 
raise ValueError("timeout!")

  signal.signal(signal.SIGALRM,timeout_handler)

  while True:
chunk = fileo.read(bs)
sock.sendall(chunk)
(...)


What is the best practise for a cross platform timeout handler?




-- 
Ullrich Horlacher  Server und Virtualisierung
Rechenzentrum IZUS/TIK E-Mail: horlac...@tik.uni-stuttgart.de
Universitaet Stuttgart Tel:++49-711-68565868
Allmandring 30aFax:++49-711-682357
70550 Stuttgart (Germany)  WWW:http://www.tik.uni-stuttgart.de/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Using subprocess to capture a progress line

2015-11-11 Thread Tim Johnson
* Chris Warrick  [15 00:55]:
> On 10 November 2015 at 23:47, Tim Johnson  wrote:
> > Using python 2.7.6 on ubuntu 14.04
<..> 
> There is no \n character at the end — which means that
> p.stdout.readline() cannot return. In fact, if you printed repr() of
> the line you read, you would get this:
> 
> b'\r[download]  54.9% of 2.73MiB at 26.73KiB/s ETA 00:47\r[download]
> 55.0% of 2.73MiB at 79.33KiB/s ETA 00:15\r…snip…\r[download] 100% of
> 2.73MiB in 00:01\n'
> 
> The download line is implemented using \r, which is the carriage
> return character (return to the first character), and then by
> overwriting characters that were already printed.
> 
> The solution? There are numerous. I’ll help you by obscuring the worst one.
> 
> (1) [recommended] figure out how to make youtube_dl work as a library,
> read its main file to figure out the problem. Don’t mess with
> subprocess.
  Was my first goal, had some problems, but I have solved them in
  part by finding the good documentation of the developers.

  I.E., the subprocess method _is_ going away and I will be using
  the youtube_dl module.

> (2) [don’t do it] do you need to intercept the lines? If you don’t set
> stderr= and stdout=, things will print just fine.
  Got to try that before using the module, just for edification.

> (3) [DON’T DO IT] .ernq() punenpgre ol punenpgre naq znxr n zrff.
> 
> PS. Thank you for setting a sensible Reply-To header on your messages.
> Which is something the list should be doing.
  LOL! Glad to help :)
  Thanks for the reply and the further education. 
  Cheers
-- 
Tim 
http://www.akwebsoft.com, http://www.tj49.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python.exe is not a valid Win32 application error message

2015-11-11 Thread Michael Torrie
On 11/11/2015 06:13 AM, Chris Angelico wrote:
> On Tue, Nov 10, 2015 at 11:34 AM, M. Kamisato via Python-list
>  wrote:
>> I am running python on Windows XP SP3 and download version 3.5xx.  I got the 
>> above error message and could not run the program.
>> I have downloaded Python version 2.7xx and it runs fine.
>> Is there any way I can get version 3.5xx to run on my computer?
>> Mel KamisatoBuena Park, CA
> 
> You can't get 3.5 to run on XP, no; your options are:
> 
> 1) Install Python 3.4, which does support XP
> 2) Upgrade to a newer version of Windows (anything from Vista onward
> will run 3.5; to save having to do this in future, jump straight to 7
> or 10)
> 3) Make the jump to Linux or FreeBSD or some other OS.
> 
> When 3.5.1 is released (currently scheduled for early December),
> you'll get a clear and simple error when you try to install, rather
> than strange issues about it not being a valid program. That won't
> change the platform support, but at least it won't be as confusing.

The Python website is looking very professional and polished these days.
 But we still don't have a simple message on the download page telling
people Windows XP is no longer supported.  Given the number of people
posting lately (several each week), I would think even with the limited
time and resources of the python.org webmaster a little warning message
would be in order.  Even when the installer is fixed, it would still be
nice to have basic OS requirements listed before the download is ever
clicked.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python.exe is not a valid Win32 application error message

2015-11-11 Thread Chris Angelico
On Tue, Nov 10, 2015 at 11:34 AM, M. Kamisato via Python-list
 wrote:
> I am running python on Windows XP SP3 and download version 3.5xx.  I got the 
> above error message and could not run the program.
> I have downloaded Python version 2.7xx and it runs fine.
> Is there any way I can get version 3.5xx to run on my computer?
> Mel KamisatoBuena Park, CA

You can't get 3.5 to run on XP, no; your options are:

1) Install Python 3.4, which does support XP
2) Upgrade to a newer version of Windows (anything from Vista onward
will run 3.5; to save having to do this in future, jump straight to 7
or 10)
3) Make the jump to Linux or FreeBSD or some other OS.

When 3.5.1 is released (currently scheduled for early December),
you'll get a clear and simple error when you try to install, rather
than strange issues about it not being a valid program. That won't
change the platform support, but at least it won't be as confusing.

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


Re: Guide in Deskop Application Development in Python for newbies

2015-11-11 Thread Leonard Andrew Mesiera
Thank you sir @Chris Warrick for your great suggestion, even though I
really got overwhelmed by the things that I need to study to get this
project done. I'm really new to programming  so I havent heard or even
tried DJANGO, but on your suggestion, if thats what I need to get my
project done, that would I do. It would really take alot of time for me to
finish this project, but thank you man, I really appreciate your help

On Wed, Nov 11, 2015 at 8:38 PM, Leonard Andrew Mesiera <
leonardmesi...@gmail.com> wrote:

> Thank you sir @Chris Warrick for your great suggestion, even though I
> really got overwhelmed by the things that I need to study to get this
> project done. I'm really new to programming  so I havent heard or even
> tried DJANGO, but on your suggestion, if thats what I need to get my
> project done, that would I do. It would really take alot of time for me to
> finish this project, but thank you man, I really appreciate your help
>
>
> On Sun, Nov 8, 2015 at 5:22 PM, Chris Warrick  wrote:
>
>> On 7 November 2015 at 15:44,   wrote:
>> > How do you start building a desktop application in python? I mean where
>> do I start? Besides installing python on your windows what else do I need,
>> and any suggestion on how do I accomplish this project.
>> >
>> > Right now I really want to finish this beauty pageant judging system
>> which requires to have a client and a server, client would be for the
>> judges and a server that computes the scores from all the categories, (i do
>> hope you get I want mean by that project). I just finished reading
>> Headfirst Python and I really loving this language, so any help from all
>> the great programmers here would be so great.
>> > --
>> > https://mail.python.org/mailman/listinfo/python-list
>>
>> This project requires two very different components, or one monolithic
>> server.
>>
>> The first one is the server. It basically needs to talk to clients
>> (via HTTP) and to a database. This is a trivial app to write in your
>> favorite web framework, eg. Django [0]. Come up with a good database
>> structure (read the excellent tutorial and documentation, should get
>> you there), write some models. But you can’t write your views just
>> yet. Because the views you write depend strictly on the client.
>>
>> For the client, you basically have two choices:
>> (a) write a web application in Django;
>> (b) use a GUI framework and make a standalone desktop application.
>>
>> If you choose option (a), you need to learn HTML/CSS and write the
>> views for your Django application (or use a ready-made front-end
>> framework, eg. Bootstrap [1]). This is the simplest choice, and it
>> takes a lot of work away from you. Your users will use their favorite
>> web browser to access the voting system, log in, and make their votes,
>> and there is no special setup for them (apart from giving them
>> credentials to access your app). Your Django views will use the
>> built-in Django templating, forms, and is relatively simple to do
>> (might even be doable in a weekend).
>>
>> Route (b) is much more complicated. To follow this route, you need to
>> pick a GUI framework. There are also multiple options, I personally
>> recommend PySide, but you could also try wxWidgets, pygobject or kivy.
>> The web app side of things will require serializing data to JSON and
>> writing a RESTful API, but there are ready-made solutions for many web
>> frameworks [2].
>> But most of those come with a catch: they usually make you produce
>> ugly code, because they are wrappers around ugly C++ APIs. And then
>> you need to write code to talk to your HTTP server. You can’t use the
>> beautiful requests library, because it will block — so there’s more
>> work ahead, unless you want your app to be unresponsive every time you
>> talk to the server. For example, in Qt, you would need to use Qt
>> networking capabilities (which work asynchronously within the event
>> loop), or some other implementation that you can use asynchronously
>> (eg. Twisted, but then you lock yourself to Python 2, which is bad, or
>> threading, which has its limitations…)
>> And then you need to distribute your app to your users. Which is
>> already hard, because you need to coordinate Python, your GUI
>> framework, and your app. Are your users on Windows, Linux, or OS X? If
>> you have at least one person on a platform, you will need some sort of
>> testing environment…
>>
>> And no matter which route you choose, you can’t do much without a
>> Linux server, so there’s more learning to do.
>>
>> Sadly, developing big things is hard and requires a lot of knowledge —
>> especially if you’re a one-man-band.
>> Here’s a short list of skills you need, with a subjectively suggested
>> implementation and ease of implementation:
>>
>> * understanding of the HTTP protocol (*)
>> * web application development (Django *)
>> * database schema writing (planning out the structure + Django ORM **)
>> * app server setup (uWSGI + nginx + Linux ***)
>> * da

Python.exe is not a valid Win32 application error message

2015-11-11 Thread M. Kamisato via Python-list
I am running python on Windows XP SP3 and download version 3.5xx.  I got the 
above error message and could not run the program.
I have downloaded Python version 2.7xx and it runs fine.
Is there any way I can get version 3.5xx to run on my computer?
Mel KamisatoBuena Park, CA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Guide in Deskop Application Development in Python for newbies

2015-11-11 Thread Leonard Andrew Mesiera
Thank you sir @Chris Warrick for your great suggestion, even though I
really got overwhelmed by the things that I need to study to get this
project done. I'm really new to programming  so I havent heard or even
tried DJANGO, but on your suggestion, if thats what I need to get my
project done, that would I do. It would really take alot of time for me to
finish this project, but thank you man, I really appreciate your help

On Sun, Nov 8, 2015 at 5:22 PM, Chris Warrick  wrote:

> On 7 November 2015 at 15:44,   wrote:
> > How do you start building a desktop application in python? I mean where
> do I start? Besides installing python on your windows what else do I need,
> and any suggestion on how do I accomplish this project.
> >
> > Right now I really want to finish this beauty pageant judging system
> which requires to have a client and a server, client would be for the
> judges and a server that computes the scores from all the categories, (i do
> hope you get I want mean by that project). I just finished reading
> Headfirst Python and I really loving this language, so any help from all
> the great programmers here would be so great.
> > --
> > https://mail.python.org/mailman/listinfo/python-list
>
> This project requires two very different components, or one monolithic
> server.
>
> The first one is the server. It basically needs to talk to clients
> (via HTTP) and to a database. This is a trivial app to write in your
> favorite web framework, eg. Django [0]. Come up with a good database
> structure (read the excellent tutorial and documentation, should get
> you there), write some models. But you can’t write your views just
> yet. Because the views you write depend strictly on the client.
>
> For the client, you basically have two choices:
> (a) write a web application in Django;
> (b) use a GUI framework and make a standalone desktop application.
>
> If you choose option (a), you need to learn HTML/CSS and write the
> views for your Django application (or use a ready-made front-end
> framework, eg. Bootstrap [1]). This is the simplest choice, and it
> takes a lot of work away from you. Your users will use their favorite
> web browser to access the voting system, log in, and make their votes,
> and there is no special setup for them (apart from giving them
> credentials to access your app). Your Django views will use the
> built-in Django templating, forms, and is relatively simple to do
> (might even be doable in a weekend).
>
> Route (b) is much more complicated. To follow this route, you need to
> pick a GUI framework. There are also multiple options, I personally
> recommend PySide, but you could also try wxWidgets, pygobject or kivy.
> The web app side of things will require serializing data to JSON and
> writing a RESTful API, but there are ready-made solutions for many web
> frameworks [2].
> But most of those come with a catch: they usually make you produce
> ugly code, because they are wrappers around ugly C++ APIs. And then
> you need to write code to talk to your HTTP server. You can’t use the
> beautiful requests library, because it will block — so there’s more
> work ahead, unless you want your app to be unresponsive every time you
> talk to the server. For example, in Qt, you would need to use Qt
> networking capabilities (which work asynchronously within the event
> loop), or some other implementation that you can use asynchronously
> (eg. Twisted, but then you lock yourself to Python 2, which is bad, or
> threading, which has its limitations…)
> And then you need to distribute your app to your users. Which is
> already hard, because you need to coordinate Python, your GUI
> framework, and your app. Are your users on Windows, Linux, or OS X? If
> you have at least one person on a platform, you will need some sort of
> testing environment…
>
> And no matter which route you choose, you can’t do much without a
> Linux server, so there’s more learning to do.
>
> Sadly, developing big things is hard and requires a lot of knowledge —
> especially if you’re a one-man-band.
> Here’s a short list of skills you need, with a subjectively suggested
> implementation and ease of implementation:
>
> * understanding of the HTTP protocol (*)
> * web application development (Django *)
> * database schema writing (planning out the structure + Django ORM **)
> * app server setup (uWSGI + nginx + Linux ***)
> * database setup (PostgreSQL *** or something simpler[3])
> * Route A:
>   * HTML/CSS skills; a front-end framework (Bootstrap **)
> * Route B:
>   * RESTful APIs (Django REST Framework ***/* if you use OAuth)
>   * GUI framework (PyQt )
>   * talking to your server from within the framework (/*)
>
> [0]: https://www.djangoproject.com/
> [1]: http://getbootstrap.com/
> [2]: http://www.django-rest-framework.org/
> [3]: If this is going to be VERY small, you could go with a sqlite
> database, which requires zero setup, but which is not suited for
> anything more serious.
>
> Other learning ma

Re: Question about math.pi is mutable

2015-11-11 Thread Steven D'Aprano
On Wed, 11 Nov 2015 07:30 pm, Marko Rauhamaa wrote:

> Steven D'Aprano :
> 
>> Since compile, eval and exec are Python built-ins, if it doesn't
>> include a byte-code compiler, it isn't Python. It's just a subset of
>> Python.
> 
> compile() can be implemented trivially, or in any other manner. It
> simply needs to return a "code object." I suspect even a string might
> work as a code object.

Sure. That's just quality of implementation. In principle, a Python
interpreter might even operate without any byte-code at all, parsing each
line of code before executing it.

Nevertheless, whatever quality of implementation compile/eval/exec offer,
they *must* be available at runtime, otherwise the language is just a
subset of Python.



-- 
Steven

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


Re: Using subprocess to capture a progress line

2015-11-11 Thread Chris Warrick
On 10 November 2015 at 23:47, Tim Johnson  wrote:
> Using python 2.7.6 on ubuntu 14.04
> The application in question is run with bash and gnome-terminal :
>
> I've written a command-line "wrapper" for youtube-dl, executing
> youtube-dl as a subprocess.
>
> --
> youtube-dl reports download progress on one line. I.E. the line is
> overwritten numerous times with no carriage return until the
> downloading is finished.
> --
>
> The following code runs the youtube-dl command and reports each line
> as output by youtube-dl
> ###
> p = subprocess.Popen(list(args), stderr=subprocess.STDOUT,
>  stdout=subprocess.PIPE)
> while True:
> line = p.stdout.readline()
> if not line:
> break
> tmp = line.strip()
> print tmp
> ###
>
> However this method not does show the download progress _until_ the
> download is complete.

There is no \n character at the end — which means that
p.stdout.readline() cannot return. In fact, if you printed repr() of
the line you read, you would get this:

b'\r[download]  54.9% of 2.73MiB at 26.73KiB/s ETA 00:47\r[download]
55.0% of 2.73MiB at 79.33KiB/s ETA 00:15\r…snip…\r[download] 100% of
2.73MiB in 00:01\n'

The download line is implemented using \r, which is the carriage
return character (return to the first character), and then by
overwriting characters that were already printed.

The solution? There are numerous. I’ll help you by obscuring the worst one.

(1) [recommended] figure out how to make youtube_dl work as a library,
read its main file to figure out the problem. Don’t mess with
subprocess.
(2) [don’t do it] do you need to intercept the lines? If you don’t set
stderr= and stdout=, things will print just fine.
(3) [DON’T DO IT] .ernq() punenpgre ol punenpgre naq znxr n zrff.

PS. Thank you for setting a sensible Reply-To header on your messages.
Which is something the list should be doing.

-- 
Chris Warrick 
PGP: 5EAAEA16
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Question about math.pi is mutable

2015-11-11 Thread Marko Rauhamaa
Steven D'Aprano :

> Since compile, eval and exec are Python built-ins, if it doesn't
> include a byte-code compiler, it isn't Python. It's just a subset of
> Python.

compile() can be implemented trivially, or in any other manner. It
simply needs to return a "code object." I suspect even a string might
work as a code object.


Marko
-- 
https://mail.python.org/mailman/listinfo/python-list