Re: Why doesn't Python include non-blocking keyboard input function?

2016-10-26 Thread Steven D'Aprano
On Thursday 27 October 2016 12:12, BartC wrote:

> I don't
> understand the argument that a language shouldn't have a basic keyboard
> API because some computers it could run on might not have a keyboards.

That's not the argument. The argument is that Python has a basic keyboard API: 
raw_input (in Python 2) or input (in 3).

This supports 97% of keyboard-based interaction, namely blocking line-based 
text input. Ctrl-C (KeyboardInterrupt) can be co-opted to support maybe another 
one or two percent.

The question is, what are the use-cases for the sorts of key APIs you are 
asking for? Who needs them? Why should it be a language feature? 

Those are not rhetoricial questions.

Python falls neatly into the same niche of languages as (for example) Ruby, 
Javascript, Lua, Swift, Tcl, and (not quite as well) bash, Java, Julia. Which 
of these languages offer non-blocking keyboard input as a standard part of the 
language?


Python has no "peek" and "poke" memory access commands either. It's not 1972 
and programming has moved on. The utility of something like this feature is 
very low, the amount of effort needed for a cross-platform solution is very 
high.



-- 
Steven
git gets easier once you get the basic idea that branches are homeomorphic 
endofunctors mapping submanifolds of a Hilbert space.

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


Re: Why doesn't Python include non-blocking keyboard input function?

2016-10-26 Thread Marko Rauhamaa
Grant Edwards :
> I've offered a few times to extend the Linux pty driver to support the
> same set of ioctl calls that a tty does (so that it could be used in
> place of a tty generally), but I've never gotten any response.

Ah, Linux kernel politics are Byzantine. It's virtually impossible to
get a hearing at the linux-kernel main mailing list. Did you try one of
the targeted mailing lists on http://vger.kernel.org/vger-lists.html>?


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


Re: Why doesn't Python include non-blocking keyboard input function?

2016-10-26 Thread Marko Rauhamaa
Terry Reedy :

> On 10/26/2016 8:33 AM, Marko Rauhamaa wrote:
>> Maybe there should be some way to get the raw events from the PTY.
>
> PTY?  Must be Linux-specific.  Most beginners are not on Linux.

A PTY is an emulated console (https://en.wikipedia.org/wiki/Pseudoterminal>). I don't know Windows but
I would guess cmd.exe does something similar there.

Also, I have no statistics on most beginning programmers operating
systems.

>> But what would be wrong in a GUI PTY API? No windowing,
>
> 'No windowing'? Python normally runs in a text widget in a window,
> programmed to emulate a dumb terminal.

Must be Windows-specific.

> I never touched an Apple II and only briefly a Commodore 64, so either
> never knew or have forgotten the Basic commands they had.  But it should
> be possible to emulate at least the text screen of either with
> tkinter.

Everything is possible for an individual application (like Python). The
TTY/PTY interface enhancement I was entertaining would be a new API
provided by the OS to all programs.


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


Re: Why doesn't Python include non-blocking keyboard input function?

2016-10-26 Thread Grant Edwards
On 2016-10-27, Paul Rubin  wrote:
> Terry Reedy  writes:
>
>> Today, ethernet-connected *nix servers have no keyboard, mouse, or
>> even a directly connected terminal.
>
> Usually you ssh into them and connect to a pty which supports the
> same ioctls that a real terminal would.

On all the Unixes/Linuxs I know of, a pty supports a _subset_ of those
that a real tty supports.  The fact that some of them are missing has
annoyed me for decades because it prevents you from implementing a
serial port in user-space[1]. I've offered a few times to extend the
Linux pty driver to support the same set of ioctl calls that a tty
does (so that it could be used in place of a tty generally), but I've
never gotten any response.

> I use screen editors over ssh all the time, not to mention filters
> like "more" where you press the space bar to scroll to the next
> page.  It's sad that there's no easy way to do that in Python.

Yep, among the ioctl calls that ptys do support are those that allow
you to do raw and non-blocking input.

[1] The fact that Windows allows you to implement a serial port in
userspace and Linux doesn't just adds insult to injury.

--
Grant


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


Re: Why doesn't Python include non-blocking keyboard input function?

2016-10-26 Thread Paul Rubin
Terry Reedy  writes:
> Today, ethernet-connected *nix servers have no
> keyboard, mouse, or even a directly connected terminal.

Usually you ssh into them and connect to a pty which supports the same
ioctls that a real terminal would.  I use screen editors over ssh all
the time, not to mention filters like "more" where you press the space
bar to scroll to the next page.  It's sad that there's no easy way to do
that in Python.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why doesn't Python include non-blocking keyboard input function?

2016-10-26 Thread eryk sun
On Wed, Oct 26, 2016 at 11:39 AM, Dennis Lee Bieber
 wrote:
> Curses  tends to not be available on Windows as M$ hasn't implemented a
> compatible console driver.

The PDCurses library supports the Windows console. Christoph Gohlke
distributes a curses extension module based on it:

http://www.lfd.uci.edu/~gohlke/pythonlibs/#curses
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why doesn't Python include non-blocking keyboard input function?

2016-10-26 Thread Terry Reedy

On 10/26/2016 11:00 AM, BartC wrote:

On 26/10/2016 13:33, Marko Rauhamaa wrote:



Say you want to implement a simple, character-based shooting game where
the two guns are operated by the [Shift] keys. Unfortunately, the Unix
terminal API doesn't make that possible. You need to get the keyboard
events from some other API. In practice, your only choice is X11/Wayland
(on Linux).


This is trivial with tkinter and practical if one uses a tk Text.  See 
below for the problem with using tkinter and console.



That sort of thing is possible to build by directly calling OS-specific
functions in a similar manner to Steven D'Aprano's way of implementing
getch().

But it's something everyone would have to code themselves.

(I just tried it using my 'getchx' function where it ought to have
worked. Unfortunately MS' interface to key events doesn't seem to
distinguish between left and right shift keys. But it was doable with
left/right ctrl keys.

That's a blocking function it it means having to wait for input. But a
version that just tests for status shouldn't be hard.)


In my answer to Marko, I posted code which worked as far as I tested it. 
 Here I add a line to make the tk window invisible.  I also replace 
rshift with a function that gets input from the user.


import tkinter as tk
root = tk.Tk()
root.withdraw()  # make tk window invisible
def lshift(event): print('shift-l')
def rshift(event):
s = input('type something: ')
print('received', s)
root.bind('', lshift)
root.bind('', rshift)
root.mainloop()

For input, the problem is input focus.  When the tk window is created, 
the OS gives it input focus.  Leaving itself invisible does not negate 
that.  But to respond to input in a different window, the user must, as 
least on Windows, click on the input window.  I do not know of any way 
for tkinter to give focus back to a parent window that is either not a 
tk window or is not in the same process.


After input is received, or indeed after focus is moved by clicking on 
any other window, there is the problem of moving focus back to the tk 
window.  If it is invisible, it cannot be clicked on.  And without a 
binding to stop mainloop when the focus leaves, there is no way to stop 
it without killing the console, or with IDLE, restarting Shell.


I conclude that if one uses tkinter to captures and process some 
keyboard events, one should do so for all.  Python's input should be 
replaced by a tkinter simulation.  I can think of a couple of ways this 
might be implemented.


--
Terry Jan Reedy

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


Re: Why doesn't Python include non-blocking keyboard input function?

2016-10-26 Thread BartC

On 27/10/2016 00:30, Terry Reedy wrote:

On 10/26/2016 7:18 AM, BartC wrote:


Can tkinter do it without creating a distracting pop-up window at the
same time?


Yes.  I already showed how on this thread.  Of course, for some text
appications, one would be better off with a Text widget than with the
system-specific console.

Bart, you appear to have been fortunate enough to be spoiled by learning
programming on microcomputers, where the terminal and computer are
combined into one unit, so that the computer, and potentially the
programmer, have access to user input actions.


Actually, I first used machines such as pdp10 and pdp11. Those mostly 
used serial terminals, a mystery to me, and they still are. It seems 
Unix is keen to propagate the mystery.


  However, Python was not

developed on, and in not limited to use on, such machines.  Today,
ethernet-connected *nix servers have no keyboard, mouse, or even a
directly connected terminal.


So how does your tkinter example work in such a server? As I don't 
understand the argument that a language shouldn't have a basic keyboard 
API because some computers it could run on might not have a keyboards.


Anyway vast numbers of /consumer/ machines /do/ have displays and 
keyboards. Look at any computer in an office: it has a display and 
keyboard. Look at any laptop: display and keyboard. Even a tablet can 
have an on-screen keyboard or have a real one plugged in.



Over 20 years ago, tk was written in tcl and C to give tcl programmers
access to X Windows graphics terminals, including user input actions. It
has since been ported to MS Windows and Apple OSX and adopted by other
languages, including Python, to give the same access.


OK. I was writing graphics applications before a lot of those things 
existed. They needed to use a display and they needed user input, and I 
had to make it work. I couldn't ask my customers to wait a decade or so 
until someone invented an API before they could fully use their keyboards!


(I've looked at X Windows; bloody hell, it makes Win32/GDI look like 
child's play. It's quite frustrating see things that used to be so 
simple to do become next to impossible.)


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


Re: Why doesn't Python include non-blocking keyboard input function?

2016-10-26 Thread Terry Reedy

On 10/26/2016 8:33 AM, Marko Rauhamaa wrote:

BartC :



Say you want to implement a simple, character-based shooting game where
the two guns are operated by the [Shift] keys. Unfortunately, the Unix
terminal API doesn't make that possible. You need to get the keyboard
events from some other API. In practice, your only choice is X11/Wayland
(on Linux).


Or tk(inter), which uses X11 on *nix and other stuff on OSX and Windows. 
 I verified that it gives access to the Shift keys as keys in 
themselves, rather than just as modifiers.


import tkinter as tk
root = tk.Tk()
def lshift(event): print('shift-l')
def rshift(event): print('shift-r')
root.bind('', lshift)
root.bind('', rshift)
root.mainloop()

does what I hoped, with autorepeat when the key was held down.


It's more building a mountain of complexity around something that
ought to be straightforward.


Maybe there should be some way to get the raw events from the PTY.


PTY?  Must be Linux-specific.  Most beginners are not on Linux.


However, next you'd start wanting the mouse events and pixel-level color
controls. It starts to look like a GUI application.

But what would be wrong in a GUI PTY API? No windowing,


'No windowing'?  Python normally runs in a text widget in a window, 
programmed to emulate a dumb terminal.  The request for non-blocking 
access to user actions is a request for something smarter.



just a regular
character display where you could draw pictures and interpret the inputs
directly à la Apple II or Commodore 64.


I never touched an Apple II and only briefly a Commodore 64, so either 
never knew or have forgotten the Basic commands they had.  But it should 
be possible to emulate at least the text screen of either with tkinter. 
(Graphics modes with pixel peek and poke might also be possible with a 
Canvas, but I won't claim that without knowing more.)  It should then be 
possible to translate old games into Python.


Maybe something like this has been done?


It would make teaching programming much more fun, too.


The logo-in-Python turtle module is already used for this.

What would be doable and possibly useful would be a textscreen module 
with a Screen class that does as much boilerplate for people as 
possible.  For my proof-of-concept above, something like


from textscreen import Screen
def lshift(event): print('shift-l')
def rshift(event): print('shift-r')
Screen().go

should be enough.  The go method should import tkinter, create root, add 
a Text, scan globals() for functions with key names (and names of mouse 
actions), bind any that are found, and start mainloop.


This sort of thing would be even nicer if and when tcl/tk gains support 
for the full unicode character set so that all the non-BMP emoji are 
potentially available.  (A font supporting such would also be needed.)


--
Terry Jan Reedy


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


Re: Why doesn't Python include non-blocking keyboard input function?

2016-10-26 Thread Terry Reedy

On 10/26/2016 7:18 AM, BartC wrote:


Can tkinter do it without creating a distracting pop-up window at the
same time?


Yes.  I already showed how on this thread.  Of course, for some text 
appications, one would be better off with a Text widget than with the 
system-specific console.


Bart, you appear to have been fortunate enough to be spoiled by learning 
programming on microcomputers, where the terminal and computer are 
combined into one unit, so that the computer, and potentially the 
programmer, have access to user input actions.  However, Python was not 
developed on, and in not limited to use on, such machines.  Today, 
ethernet-connected *nix servers have no keyboard, mouse, or even a 
directly connected terminal.


When Python was developed, standard C did not have keyboard and mouse 
functions.  (I don't know about the most recent standards.)  Keyboard 
functions on microcomputer C ports were non-standard OS-specific 
extensions.  On Unix, X windows was and is optional.


Over 20 years ago, tk was written in tcl and C to give tcl programmers 
access to X Windows graphics terminals, including user input actions. 
It has since been ported to MS Windows and Apple OSX and adopted by 
other languages, including Python, to give the same access.


--
Terry Jan Reedy

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


Re: multiprocess passing arguments double asterisks

2016-10-26 Thread MRAB

On 2016-10-26 21:44, pic8...@gmail.com wrote:

On Monday, October 24, 2016 at 12:39:47 PM UTC-5, Thomas Nyberg wrote:

On 10/24/2016 12:45 PM, pic8...@gmail.com wrote:
> Thanks for the reply.
>
> The code snippet given by Peter is not very clear
>
> I would like to multiprocess a function which is written in python of the 
form bar(**kwargs) which returns a value.  This example does not return anything
>
> Would you please use my example for the map function?
>
> I appreciate your help,
>
I'm honestly not totally sure what you want to do. However, say you want
to do the following (btw this is basically what Dennis said i nhis last
email, but maybe I can help clarify):

kwargs = {'param1': val1, 'param2': val2})

Then you'd like to have the following two operations performed in
separate processes:

bar(param1=val1)
bar(param2=val2)

In that case, I guess I would do something like the following. First
define bar_wrapper as follows (*I haven't tested any code here!):

def bar_wrapper(pair):
key, val = pair
return bar(**{key: val})

Then I would probably do something like

map(bar_wrapper, kwargs.items())

I.e. basically what I'm doing is taking the key-val pairs and producing
a list of them (as tuples). This is something that you can apply map
too, but not with the original function. So then the wrapper function
converts the tuple back to what you want originally.

Hopefully I'm understanding correctly and hopefully this helps.

Cheers,
Thomas


Thomas,

I have strings & numpy.ndarray as arguments.  The wrapper function works great 
for strings.

Here's what I'm trying to do...
**

I have a serial python fn of the form

def bar(**kwargs):
  a=kwargs.get("name")
  print a
  self.a1d=ma.asanyarray(kwargs.get('a1d'), dtype=float)
  (more calculations--takes a long time to compute
  returns an object)

I am trying to run this function in parallel.

Here's my Main program

import numpy as np
import numpy.ma as ma
from delegate import parallelize
from hashlib import sha1
a1d=np.zeros((32))
b1d=np.zeros((32))
p=open("/tmp/pdata","rb")
pdata=np.load(p)
for i in range(0,10):
  a1d=pdata['t1d']
  b1d=pdata['gz1d']
  print a1d,b1d
  kwargs={'name':'special','a':a1d,'b':b1d}
  val=parallelize(bar,kwargs)
***
Error:
line 30, in 
val=parallelize(bar,kwargs)
   delegate.py", line 311, in process
item, items = items[0], items[1:]
KeyError: 0
**
Error: (with the wrapper function)
val=parallelize(bar_wrapper,kwargs.items())
TypeError: unhashable type: 'numpy.ndarray'
***

How do I pass the string & numpy.ndarray to the function bar(**kwargs)?

Thank you for suggestions & help,


a1d=np.zeros((32))
b1d=np.zeros((32))
p=open("/tmp/pdata","rb")
pdata=np.load(p)
for i in range(0,100):
  a1d=pdata['t1d']
  b1d=pdata['gz1d']
  print a1d,b1d
  kwargs={'name':'special','a':a1d,'b':b1d}
  val=parallelize(bar,kwargs)

'parallelize' expects a function and a list of items. It calls the 
function with each item in the list in parallel, passing the item as the 
argument of the function.


That _single_ argument item can be a tuple or a dict.

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


thread local storage

2016-10-26 Thread Joseph L. Casale
Looks like the shipped implementation  doesn't give access to all the discrete 
copies
of tls for us in a case where a context manager needs to perform any cleanup. 
Does
something exists where I can leverage this feature or do I need to roll my own?

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


Re: Reversing \N{...} notation?

2016-10-26 Thread Peter Otten
Tim Chase wrote:

> On 2016-10-25 20:14, Peter Otten wrote:
>> Tim Chase wrote:
>> > I like the clarity of using the "\N{...}" notation when creating
>> > string literals involving Unicode chars.
>> > 
>> > Is there a built-in way to get such strings back from Python?
>> 
>> >>> 'mañana'.encode("ascii", "namereplace").decode()
>> 'ma\\N{LATIN SMALL LETTER N WITH TILDE}ana'
> 
> Wonderful!  Thanks for knowing about that corner of Python and
> sharing it.
> 
>> (requires Python 3.5)
> 
> Sorry it wasn't back-ported, but glad it's there now.  Thanks!

My original plan was to write a custom error handler, and I found 
namereplace when I tried to understand the required function signature. 
Completing the initial effort:

$ cat codecs_mynamereplace.py
# -*- coding: utf-8 -*-
import codecs
import unicodedata

try:
codecs.namereplace_errors
except AttributeError:
print("using mynamereplace")
def mynamereplace(exc):
return u"".join(
"\\N{%s}" % unicodedata.name(c)
for c in exc.object[exc.start:exc.end]
), exc.end
codecs.register_error("namereplace", mynamereplace)


print(u"mañana".encode("ascii", "namereplace").decode())
$ python3.5 codecs_mynamereplace.py
ma\N{LATIN SMALL LETTER N WITH TILDE}ana
$ python3.4 codecs_mynamereplace.py
using mynamereplace
ma\N{LATIN SMALL LETTER N WITH TILDE}ana
$ python2.7 codecs_mynamereplace.py
using mynamereplace
ma\N{LATIN SMALL LETTER N WITH TILDE}ana


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


Re: multiprocess passing arguments double asterisks

2016-10-26 Thread pic8690
On Monday, October 24, 2016 at 12:39:47 PM UTC-5, Thomas Nyberg wrote:
> On 10/24/2016 12:45 PM, pic8...@gmail.com wrote:
> > Thanks for the reply.
> >
> > The code snippet given by Peter is not very clear
> >
> > I would like to multiprocess a function which is written in python of the 
> > form bar(**kwargs) which returns a value.  This example does not return 
> > anything
> >
> > Would you please use my example for the map function?
> >
> > I appreciate your help,
> >
> I'm honestly not totally sure what you want to do. However, say you want 
> to do the following (btw this is basically what Dennis said i nhis last 
> email, but maybe I can help clarify):
> 
>   kwargs = {'param1': val1, 'param2': val2})
> 
> Then you'd like to have the following two operations performed in 
> separate processes:
> 
>   bar(param1=val1)
>   bar(param2=val2)
> 
> In that case, I guess I would do something like the following. First 
> define bar_wrapper as follows (*I haven't tested any code here!):
> 
> def bar_wrapper(pair):
>   key, val = pair
>   return bar(**{key: val})
> 
> Then I would probably do something like
> 
> map(bar_wrapper, kwargs.items())
> 
> I.e. basically what I'm doing is taking the key-val pairs and producing 
> a list of them (as tuples). This is something that you can apply map 
> too, but not with the original function. So then the wrapper function 
> converts the tuple back to what you want originally.
> 
> Hopefully I'm understanding correctly and hopefully this helps.
> 
> Cheers,
> Thomas

Thomas,

I have strings & numpy.ndarray as arguments.  The wrapper function works great 
for strings.

Here's what I'm trying to do...
**

I have a serial python fn of the form

def bar(**kwargs):
  a=kwargs.get("name")
  print a
  self.a1d=ma.asanyarray(kwargs.get('a1d'), dtype=float)
  (more calculations--takes a long time to compute
  returns an object)

I am trying to run this function in parallel.

Here's my Main program

import numpy as np
import numpy.ma as ma
from delegate import parallelize
from hashlib import sha1
a1d=np.zeros((32))
b1d=np.zeros((32))
p=open("/tmp/pdata","rb")
pdata=np.load(p)
for i in range(0,10):
  a1d=pdata['t1d']
  b1d=pdata['gz1d']
  print a1d,b1d
  kwargs={'name':'special','a':a1d,'b':b1d}
  val=parallelize(bar,kwargs)
***
Error:
line 30, in 
val=parallelize(bar,kwargs)
   delegate.py", line 311, in process
item, items = items[0], items[1:]
KeyError: 0
**
Error: (with the wrapper function)
val=parallelize(bar_wrapper,kwargs.items())
TypeError: unhashable type: 'numpy.ndarray'
***

How do I pass the string & numpy.ndarray to the function bar(**kwargs)?

Thank you for suggestions & help, 






a1d=np.zeros((32))
b1d=np.zeros((32))
p=open("/tmp/pdata","rb")
pdata=np.load(p)
for i in range(0,100):
  a1d=pdata['t1d']
  b1d=pdata['gz1d']
  print a1d,b1d
  kwargs={'name':'special','a':a1d,'b':b1d}
  val=parallelize(bar,kwargs)

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


Re: Reversing \N{...} notation?

2016-10-26 Thread Tim Chase
On 2016-10-25 20:14, Peter Otten wrote:
> Tim Chase wrote:
> > I like the clarity of using the "\N{...}" notation when creating
> > string literals involving Unicode chars.
> > 
> > Is there a built-in way to get such strings back from Python?
> 
> >>> 'mañana'.encode("ascii", "namereplace").decode()
> 'ma\\N{LATIN SMALL LETTER N WITH TILDE}ana'

Wonderful!  Thanks for knowing about that corner of Python and
sharing it.

> (requires Python 3.5)

Sorry it wasn't back-ported, but glad it's there now.  Thanks!

-tkc


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


Re: Why doesn't Python include non-blocking keyboard input function?

2016-10-26 Thread BartC

On 26/10/2016 13:33, Marko Rauhamaa wrote:

BartC :


On 26/10/2016 05:44, Marko Rauhamaa wrote:
(I've implemented 'keyboards' both on-screen, and on the surface of
digitising tablets (also with a hacked Casio calculator pcb when I
couldn't afford a real one). With all of those I was mainly interested
in key events, not the details.)


Say you want to implement a simple, character-based shooting game where
the two guns are operated by the [Shift] keys. Unfortunately, the Unix
terminal API doesn't make that possible. You need to get the keyboard
events from some other API. In practice, your only choice is X11/Wayland
(on Linux).


That sort of thing is possible to build by directly calling OS-specific 
functions in a similar manner to Steven D'Aprano's way of implementing 
getch().


But it's something everyone would have to code themselves.

(I just tried it using my 'getchx' function where it ought to have 
worked. Unfortunately MS' interface to key events doesn't seem to 
distinguish between left and right shift keys. But it was doable with 
left/right ctrl keys.


That's a blocking function it it means having to wait for input. But a 
version that just tests for status shouldn't be hard.)


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


Re: How to use two threads (GUI and backend)

2016-10-26 Thread Marko Rauhamaa
Chris Angelico :
> Python-the-language doesn't permit those kinds of rewrites.

[Citation needed]

Is there something here, perhaps?

   https://docs.python.org/3/library/concurrency.html>


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


Re: How to use two threads (GUI and backend)

2016-10-26 Thread Chris Angelico
On Thu, Oct 27, 2016 at 1:42 AM, Marko Rauhamaa  wrote:
> Chris Angelico :
>> And since Python doesn't rewrite the code, you don't have a problem.
>
> Do you mean Python or CPython?
>
> And how do you know?

Both, and I know because Python-the-language doesn't permit those
kinds of rewrites. PyPy does do a whole lot of optimization, but it
very carefully ensures that semantics are retained with reference to
object identities and execution order and so on.

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


Re: How to use two threads (GUI and backend)

2016-10-26 Thread Marko Rauhamaa
Chris Angelico :
> And since Python doesn't rewrite the code, you don't have a problem.

Do you mean Python or CPython?

And how do you know?


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


Re: Qtimer and extra argument

2016-10-26 Thread luca72 via Python-list
thanks


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


Re: How to use two threads (GUI and backend)

2016-10-26 Thread Chris Angelico
On Thu, Oct 27, 2016 at 1:21 AM, Marko Rauhamaa  wrote:
> Analogous code in C or Java would not be guaranteed to finish if func1()
> and func2() were in different execution contexts. In fact, it would be
> almost guaranteed to hang.
>
> That is because the compiler can see that "active" cannot change within
> T1.run() and would rewrite the code...

And since Python doesn't rewrite the code, you don't have a problem.
Optimization has costs.

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


Re: How to use two threads (GUI and backend)

2016-10-26 Thread Marko Rauhamaa
Chris Angelico :

> On Thu, Oct 27, 2016 at 12:37 AM, Marko Rauhamaa  wrote:
>> I don't know what "Global state is shared across all threads" means
>> in this context. It sounds like something that would be true for,
>> say, Java and C as well. However, those languages don't promise to
>> propagate improperly synchronized changes between threads.
>>
>> Now I would like to ask for some documentation.
>
> Here you have two functions and a global:
>
> active = True
>
> def func1():
> while active:
> # do work
> time.sleep(1)
> func2()
>
> def func2():
> global active
> if random.random() < 0.1:
> active = False

The thread version:


   active = True

   class T1(threading.Thread):
   def run(self):
   while active:
   # do work
   time.sleep(1)

   class T2(threading.Thread):
   def run(self):
   global active
   if random.random() < 0.1:
   active = False

   t1, t2 = T1(), T2()
   t1.start()
   t2.start()
   t1.join()
   t2.join


Analogous code in C or Java would not be guaranteed to finish if func1()
and func2() were in different execution contexts. In fact, it would be
almost guaranteed to hang.

That is because the compiler can see that "active" cannot change within
T1.run() and would rewrite the code as:


   class T1(threading.Thread):
   def run(self):
   if active:
   while True:
   # do work
   time.sleep(1)


Similarly, setting a flag in a signal handler might not be noticed by
the main program if it were written in C. (You need to mark the variable
as volatile.)

> I'm sure you understand that these functions share the global state of
> the 'active' flag. One changes it, the other sees the change. So far,
> nothing controversial or difficult.
>
> It's exactly the same with threads.

Extraordinary claims require extraordinary evidence.

   http://stackoverflow.com/questions/3549833/python-threading-memo
   ry-model-and-visibility>

Now, Jython developers guarantee the volatility of all memory access.
They even state this as normative for Python:

   The fundamental thing to know about Python, and what we have
   implemented in Jython, is that setting any attribute in Python is a
   volatile write; and getting any attribute is a volatile read.

   [...]

   This means that safe publication is pretty much trivial in Python,
   when compared to Java. Safe publication means the thread safe
   association of an object with a name. [...] this is always a
   memory-fenced operation in Python

   http://www.jython.org/jythonbook/en/1.0/Concurrency.html>


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


Re: How to use two threads (GUI and backend)

2016-10-26 Thread jmp

On 10/26/2016 02:45 PM, pozz wrote:

Il 26/10/2016 13:16, jmp ha scritto:

[...]
I suggest you write a GUI that make synchronouscalls to a remote
application, if possible. If the remote app is in python, you have
access to remote protocols already written for you, Pyro is one of them,
you can skip the low level communication part.


I'm not sure Pyro (or similar alternatives) helps in my case.

The real problem is that retrieving status from remote device is a slow
operation.  If the GUI thread blocks waiting for the answer, the GUI
blocks and the user complains.

 From Pyro documentation:
---
Normal method calls always block until the response is returned. This
can be any normal return value, None, or an error in the form of a
raised exception. The client code execution is suspended until the
method call has finished and produced its result.
---

So, even with Pyro, I need to have another thread that manages Pyro
communication (instead of serial communication)... additional problems.



Also from the Pyro doc:

You can execute a remote method call and tell Pyro: “hey, I don’t need 
the results right now. Go ahead and compute them, I’ll come back later 
once I need them”. The call will be processed in the background and you 
can collect the results at a later time.


[...]

It is possible to define one or more callables (the “call chain”) that 
should be invoked automatically by Pyro as soon as the result value 
becomes available.


jm

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


Re: lxml and xpath(?)

2016-10-26 Thread Peter Otten
Doug OLeary wrote:

> Hey;
> 
> Reasonably new to python and incredibly new to xml much less trying to
> parse it. I need to identify cluster nodes from a series of weblogic xml
> configuration files. I've figured out how to get 75% of them; now, I'm
> going after the edge case and I'm unsure how to proceed.
> 
> Weblogic xml config files start with namespace definitions then a number
> of child elements some of which have children of their own.
> 
> The element that I'm interested in is  which will usually have a
> subelement called  containing the hostname that I'm
> looking for.
> 
> Following the paradigm of "we love standards, we got lots of them", this
> model doesn't work everywhere. Where it doesn't work, I need to look for a
> subelement of  called . That element contains an alias
> which is expanded in a different root child, at the same level as
> .
> 
> So, picture worth a 1000 words:
> 
> 
> < [[ heinous namespace xml snipped ]] >
>[[text]]
>...
>
>   EDIServices_MS1
>   ...
>   EDIServices_MC1
>   ...
>
>
>   EDIServices_MS2
>   ...
>   EDIServices_MC2
>   ...
>
>
>  EDIServices_MC1
>  
>EDIServices_MC1
>SSL
>host001
>7001
>  
>
>
>  EDIServices_MC2
>  
>EDIServices_MC2
>host002
>7001
>  
>
> 
> 
> So, running it on 'normal' config, I get:
> 
> $ ./lxml configs/EntsvcSoa_Domain_config.xml
> EntsvcSoa_CS=> host003.myco.com
> EntsvcSoa_CS   => host004.myco.com
> 
> Running it against the abi-normal config, I'm currently getting:
> 
> $ ./lxml configs/EDIServices_Domain_config.xml
> EDIServices_CS => EDIServices_MC1
> EDIServices_CS => EDIServices_MC2
> 
> Using the examples above, I would like to translate EDIServices_MC1 and
> EDIServices_MC2 to host001 and host002 respectively.
> 
> The primary loop is:
> 
> for server in root.findall('ns:server', namespaces):
>   cs = server.find('ns:cluster', namespaces)
>   if cs is None:
> continue
>   # cluster_name = server.find('ns:cluster', namespaces).text
>   cluster_name = cs.text
>   listen_address = server.find('ns:listen-address', namespaces)
>   server_name = listen_address.text
>   if server_name is None:
> machine = server.find('ns:machine', namespaces)
> if machine is None:
>   continue
> else:
>   server_name = machine.text
> 
>   print("%-15s => %s" % (cluster_name, server_name))
> 
> (it's taken me days to write 12 lines of code... good thing I don't do
> this for a living :) )

You tend to get more efficient when you read the tutorial before you start 
writing code. Hard-won advice that I still not always follow myself ;)

> 
> Rephrased, I need to find the  under the  child
> who's name matches the name under the corresponding  child. From
> some of the examples on the web, I believe xpath might help but I've not
> been able to get even the simple examples working. Go figure, I just
> figured out what a namespace is...
> 
> Any hints/tips/suggestions greatly appreciated especially with complete
> noob tutorials for xpath.

Use your favourite search engine. One advantage of XPath is that it's not 
limited to Python.

I did not completely follow your question, so the example below is my 
interpretation of what you are asking for. It may still help you get 
started...

$ cat lxml_translate_host.py
from lxml import etree

s = """\


   text
   
  EDIServices_MS1
  EDIServices_MC1
   
   
  EDIServices_MS2
  EDIServices_MC2
   
   
 EDIServices_MC1
 
   EDIServices_MC1
   SSL
   host001
   7001
 
   
   
 EDIServices_MC2
 
   EDIServices_MC2
   host002
   7001
 
   

""".encode()

root = etree.fromstring(s)
for server in root.xpath("./server"):
servername = server.xpath("./name/text()")[0]
print("server", servername)
if not servername.isidentifier():
raise ValueError("Kind regards to Bobby Tables' Mom")
machine = server.xpath("./machine/text()")[0]
print("machine", machine)
path = ("../machine[name='{}']/node-manager/"
"listen-address/text()").format(machine)
host = server.xpath(path)[0]
print("host", host)
print()
$ python3 lxml_translate_host.py 
server EDIServices_MS1
machine EDIServices_MC1
host host001

server EDIServices_MS2
machine EDIServices_MC2
host host002

$


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


Re: How to use two threads (GUI and backend)

2016-10-26 Thread Chris Angelico
On Thu, Oct 27, 2016 at 12:37 AM, Marko Rauhamaa  wrote:
> Chris Angelico :
>
>> On Wed, Oct 26, 2016 at 11:58 PM, Marko Rauhamaa  wrote:
>>> I can't think of a valid program that could take advantage of this
>>> primitive guarantee of Python's. For example, there is no "volatile"
>>> in Python so you can't coordinate Python threads safely without
>>> proper synchronization. If you set a variable in one thread and read
>>> it in another thread, the latter might never see the change.
>>
>> Incorrect. If you set something in one thread and read it in another,
>> it WILL see it, just as it would with any other way of running two
>> functions. (Obviously function locals won't be seen, because they
>> never will.) Global state is shared across all threads.
>
> I don't know what "Global state is shared across all threads" means in
> this context. It sounds like something that would be true for, say, Java
> and C as well. However, those languages don't promise to propagate
> improperly synchronized changes between threads.
>
> Now I would like to ask for some documentation.

Here you have two functions and a global:


active = True

def func1():
while active:
# do work
time.sleep(1)
func2()

def func2():
global active
if random.random() < 0.1:
active = False


I'm sure you understand that these functions share the global state of
the 'active' flag. One changes it, the other sees the change. So far,
nothing controversial or difficult.

It's exactly the same with threads. If you remove the func2() call
from func1 and have it operate on a separate thread, and then call
func2 from the main thread (or another secondary thread), func1 will
notice the change the very next time it gets to the top of the loop.
The two functions are executing in the same process, the same module,
the same everything except their call stack (ie locals).

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


Re: How to use two threads (GUI and backend)

2016-10-26 Thread Marko Rauhamaa
Chris Angelico :

> On Wed, Oct 26, 2016 at 11:58 PM, Marko Rauhamaa  wrote:
>> I can't think of a valid program that could take advantage of this
>> primitive guarantee of Python's. For example, there is no "volatile"
>> in Python so you can't coordinate Python threads safely without
>> proper synchronization. If you set a variable in one thread and read
>> it in another thread, the latter might never see the change.
>
> Incorrect. If you set something in one thread and read it in another,
> it WILL see it, just as it would with any other way of running two
> functions. (Obviously function locals won't be seen, because they
> never will.) Global state is shared across all threads.

I don't know what "Global state is shared across all threads" means in
this context. It sounds like something that would be true for, say, Java
and C as well. However, those languages don't promise to propagate
improperly synchronized changes between threads.

Now I would like to ask for some documentation.


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


Re: How to use two threads (GUI and backend)

2016-10-26 Thread Chris Angelico
On Thu, Oct 27, 2016 at 12:02 AM, Marko Rauhamaa  wrote:
> pozz :
>
>> The real problem is that retrieving status from remote device is a
>> slow operation. If the GUI thread blocks waiting for the answer, the
>> GUI blocks and the user complains.
>
> Correct. Obnoxious, blocking APIs abound.
>
> However, I have usually used processes (instead of threads) to
> encapsulate blocking APIs. Processes have neater resource isolation and
> a better-behaving life cycle. For example, you can actually kill a
> process while you can't kill a thread.

Why is there so much FUD against threads? Processes involve a lot more
overhead, and are completely unnecessary for this task. Threads will
work just fine.

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


Re: Qtimer and extra argument

2016-10-26 Thread Phil Thompson
On 26 Oct 2016, at 1:29 pm, luca72 via Python-list  
wrote:
> 
> I get () missing 1 required positional argument: 's'

Sorry, it should have been...

lambda: self.metto_testo(testo)

Phil

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


Re: How to use two threads (GUI and backend)

2016-10-26 Thread Marko Rauhamaa
pozz :

> The real problem is that retrieving status from remote device is a
> slow operation. If the GUI thread blocks waiting for the answer, the
> GUI blocks and the user complains.

Correct. Obnoxious, blocking APIs abound.

However, I have usually used processes (instead of threads) to
encapsulate blocking APIs. Processes have neater resource isolation and
a better-behaving life cycle. For example, you can actually kill a
process while you can't kill a thread.


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


Re: How to use two threads (GUI and backend)

2016-10-26 Thread Chris Angelico
On Wed, Oct 26, 2016 at 11:58 PM, Marko Rauhamaa  wrote:
> In practice, this coherency has been implemented in CPython with a
> global lock (GIL). CPython programs are effectively single-threaded.
> They only let go of the lock when they are performing a system call.
>
> I can't think of a valid program that could take advantage of this
> primitive guarantee of Python's. For example, there is no "volatile" in
> Python so you can't coordinate Python threads safely without proper
> synchronization. If you set a variable in one thread and read it in
> another thread, the latter might never see the change.

Incorrect. If you set something in one thread and read it in another,
it WILL see it, just as it would with any other way of running two
functions. (Obviously function locals won't be seen, because they
never will.) Global state is shared across all threads.

pozz, a classic worker model like you're suggesting will be fine IMO. Go for it.

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


Re: How to use two threads (GUI and backend)

2016-10-26 Thread Marko Rauhamaa
pozz :

> Il 26/10/2016 13:27, Antoon Pardon ha scritto:
>> Op 26-10-16 om 12:22 schreef pozz:
>>> Is it safe to access this variable from two different threads?
>>> Should I implement a safer and more complex mechanism? If yes, what
>>> mechanism?
>>
>> Accessing from multiple thread shouldn't be a problem. As long as you
>> only change it in one thread.
>
> I don't want to doubt what you have written, but... are you
> definitevely sure? I tried to search for some authoritative
> documentation about this topic, but I couldn't find any.

I didn't check but I would guess you are right. That kind of
authoritative statement is not made explicitly. Java, on the other hand,
has been documented nicely:

  https://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jl
  s-17.4.5>

> If the main loop is updating the variable from 0x01020304 to
> 0xA1A2A3A4 and the change happens on a byte basis, the ISR could
> access a completely wrong value, for example 0x0102A3A4.
>
> So the main question here is: does python *specification/standard*
> guarantees atomic operations? If yes, what are they?

Python guarantees that even a pathological Python application program
that only employs ordinary, safe operations cannot crash Python. It
follows (de facto) that "pointers" must be protected against race
conditions and other artifacts. Similarly, you can't render lists, dicts
and other complex data structures incoherent with any ordinary means
regardless of race conditions.

Since Python's integers are (really or conceptually) objects behind
pointers, any Python implementation would be considered out of
compliance if it didn't guarantee either 0x01020304 or 0xa1a2a3a4 in
your example.

In practice, this coherency has been implemented in CPython with a
global lock (GIL). CPython programs are effectively single-threaded.
They only let go of the lock when they are performing a system call.

I can't think of a valid program that could take advantage of this
primitive guarantee of Python's. For example, there is no "volatile" in
Python so you can't coordinate Python threads safely without proper
synchronization. If you set a variable in one thread and read it in
another thread, the latter might never see the change.


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


Re: How to use two threads (GUI and backend)

2016-10-26 Thread pozz

Il 26/10/2016 13:16, jmp ha scritto:

[...]
I suggest you write a GUI that make synchronouscalls to a remote
application, if possible. If the remote app is in python, you have
access to remote protocols already written for you, Pyro is one of them,
you can skip the low level communication part.


I'm not sure Pyro (or similar alternatives) helps in my case.

The real problem is that retrieving status from remote device is a slow 
operation.  If the GUI thread blocks waiting for the answer, the GUI 
blocks and the user complains.


From Pyro documentation:
---
Normal method calls always block until the response is returned. This 
can be any normal return value, None, or an error in the form of a 
raised exception. The client code execution is suspended until the 
method call has finished and produced its result.

---

So, even with Pyro, I need to have another thread that manages Pyro 
communication (instead of serial communication)... additional problems.


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


Re: How to use two threads (GUI and backend)

2016-10-26 Thread pozz

Il 26/10/2016 13:27, Antoon Pardon ha scritto:

Op 26-10-16 om 12:22 schreef pozz:

Il 26/10/2016 09:13, pozz ha scritto:

[...]
When the user press Start button (the pressed handler is in the GUI
class):

  self.comm_active = True
  threading.Thread(target=self.comm_thread).start()

The backend thread is:

  def comm_thread(self):
while self.comm_active:
  self.device.get_status()
  GLib.idle_add(self.polling_received)
  time.sleep(1)
self.m.close()
[...]


Now I have some concerns even in using self.comm_active.  It is a boolean 
variable
accessed by the GUI thread (inside Start/Stop buttons handler) and backend 
thread
(in the "while self.comm_active" instruction).



Is it safe to access this variable from two different threads?
Should I implement a safer and more complex mechanism?  If yes, what mechanism?


Accessing from multiple thread shouldn't be a problem. As long as you only 
change
it in one thread.


I don't want to doubt what you have written, but... are you definitevely 
sure?  I tried to search for some authoritative documentation about this 
topic, but I couldn't find any.


I have many years of experiece in embedded firmware written in C for 
small microcontrollers, so I know the problems that could occur when a 
variable is read in one ISR (interrupt service routine) and written in 
the main loop (or viceversa).
ISR and main loop can be considered two threads. If the variable is 
32-bits and the microcontroller can't write atomically (without any 
interruption) a 32-bit variable, bad things could occur.


If the main loop is updating the variable from 0x01020304 to 0xA1A2A3A4 
and the change happens on a byte basis, the ISR could access a 
completely wrong value, for example 0x0102A3A4.


So the main question here is: does python *specification/standard* 
guarantees atomic operations? If yes, what are they?


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


Re: Why doesn't Python include non-blocking keyboard input function?

2016-10-26 Thread Marko Rauhamaa
BartC :

> On 26/10/2016 05:44, Marko Rauhamaa wrote:
> (I've implemented 'keyboards' both on-screen, and on the surface of
> digitising tablets (also with a hacked Casio calculator pcb when I
> couldn't afford a real one). With all of those I was mainly interested
> in key events, not the details.)

Say you want to implement a simple, character-based shooting game where
the two guns are operated by the [Shift] keys. Unfortunately, the Unix
terminal API doesn't make that possible. You need to get the keyboard
events from some other API. In practice, your only choice is X11/Wayland
(on Linux).

> It's more building a mountain of complexity around something that
> ought to be straightforward.

Maybe there should be some way to get the raw events from the PTY.
However, next you'd start wanting the mouse events and pixel-level color
controls. It starts to look like a GUI application.

But what would be wrong in a GUI PTY API? No windowing, just a regular
character display where you could draw pictures and interpret the inputs
directly à la Apple II or Commodore 64.

It would make teaching programming much more fun, too.


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


Re: UDP decode

2016-10-26 Thread Julian Madoz
Thanks for response! 
I don't know what can appear because the values changes 30 times per second. I 
only know the format.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Qtimer and extra argument

2016-10-26 Thread luca72 via Python-list
I get () missing 1 required positional argument: 's'
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why doesn't Python include non-blocking keyboard input function?

2016-10-26 Thread BartC

On 26/10/2016 05:44, Marko Rauhamaa wrote:

BartC :



Some people want to work at low level, without needing to drag in a GUI,
and want to do something as simple as finding out if a button has been
pressed on a standard peripheral that nearly every computer has. It
can't be that hard!


I don't consider that to be very low level.


I think working in text mode (character-based display) and a key at a 
time is enough to be called low-level.


Lower than that would be directly working with the hardware. But it gets 
more specific as keyboards work in different ways. That's not so useful 
or interesting.


(I've implemented 'keyboards' both on-screen, and on the surface of 
digitising tablets (also with a hacked Casio calculator pcb when I 
couldn't afford a real one). With all of those I was mainly interested 
in key events, not the details.)


 If you want to get to the

low level, open

   /dev/input/by-id/*-event-kbd

See:

   http://stackoverflow.com/questions/3662368/dev-input-keyboard-format


That's not what I'd call low-level. It's more building a mountain of 
complexity around something that ought to be straightforward. Apparently 
when you need to dig very deeply to get through to the fundamentals, 
that's now called 'low-level'!


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


Re: How to use two threads (GUI and backend)

2016-10-26 Thread Antoon Pardon
Op 26-10-16 om 12:22 schreef pozz:
> Il 26/10/2016 09:13, pozz ha scritto:
> > [...]
>> When the user press Start button (the pressed handler is in the GUI
>> class):
>>
>>   self.comm_active = True
>>   threading.Thread(target=self.comm_thread).start()
>>
>> The backend thread is:
>>
>>   def comm_thread(self):
>> while self.comm_active:
>>   self.device.get_status()
>>   GLib.idle_add(self.polling_received)
>>   time.sleep(1)
>> self.m.close()
> > [...]
>
> Now I have some concerns even in using self.comm_active.  It is a boolean 
> variable
> accessed by the GUI thread (inside Start/Stop buttons handler) and backend 
> thread
> (in the "while self.comm_active" instruction).

> Is it safe to access this variable from two different threads?
> Should I implement a safer and more complex mechanism?  If yes, what 
> mechanism?

Accessing from multiple thread shouldn't be a problem. As long as you only 
change
it in one thread.


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


Re: Why doesn't Python include non-blocking keyboard input function?

2016-10-26 Thread BartC

On 26/10/2016 02:02, Steve D'Aprano wrote:

On Tue, 25 Oct 2016 09:02 pm, BartC wrote:


raw_input('Press the Enter key to continue... ')


Which doesn't work on Python 3. So even here, making it easy by using
line-input, it's not so straightforward.


Really, Bart? You're stymied by the change of raw_input() to input() in
Python 3? A programmer of your many years experience and skill can't work
out how to conditionally change "raw_input" to "input" according to the
version of the interpreter running. I don't think so.


I could probably figure it out. But how about a beginner?

(When I post Python code I try and make sure it works on both, or 
specify which version.)



But why the need to have to use someone else's massive great wheel? Both
Curses and wxPython are completely over the top IMO for something so
fundamental.


*shrug* Then find a more lightweight solution. Tkinter? Something even
lighter?


Can tkinter do it without creating a distracting pop-up window at the 
same time? If not then that's too intrusive.


Curses? If I try 'import curses' (on Windows), I get 'No module named 
'_curses'. Same with ncurses or Curses. Now instead of getting on with 
it I have to go chasing some add-on. And if I want to share a bit of 
code with someone else, then /they/ have to do the same!


My point is that this simple stuff just be included in a language.


Maybe there is no such lightweight solution? Then that tells you that nobody
else needed this enough to build a solution.


I find this a lot with stuff that originates on Unix or Linux.

(Do you know when I create executables that run on Linux, I have to try 
and cram all the support files within the executable itself - because it 
appears to be impossible to discover the path the executable was started 
from. With a view to using the same or adjoining path for associated 
files. When I ask about this, Oh it's never been needed!


Same with basic keyboard stuff that any microcomputer from the 80s could 
do in an instant.)



I don't know. I've never needed this enough to care to investigate. Like I
said, it *seems* like the sort of obvious functionality every programmer
should need all the time, but in 15 years I've never, not once, actually
needs a non-blocking way to check for a keyboard event in a situation where
I wasn't using something like curses or a GUI framework.


By non-blocking you mean checking if a key has been pressed rather than 
waiting for it to be pressed? I use the latter ALL THE TIME when 
debugging. The former less often, because if I wanted to use it to abort 
huge amounts of output, I just abort the program (with Ctrl Break).


Nevertheless, it is still used sometimes, and it's there when I need it 
(NOT Python):


repeat
print "A"
until testkey()


So, why has it all become so difficult?


Build line oriented input? Why would I do that, when the OS does it?


Sometimes you want your own line-input functions because they need to be 
customised to do special things. Assign special meanings to certain key 
events for example.


You make it sound completely crazy like building your own OS. But I've 
done this dozens of times (and yes including when there /was/ no OS), it 
really isn't a big deal. Except when a language strives to make it so.


> I don't know. I've never needed this enough to care to investigate.

Try this little task. In the late 70s I was fascinated by the ability of 
a teletype to do this: it would ask a Yes/No question, perhaps:


   .CONFIRM (YES/NO):

But then, instead of typing YES or NO, if either Y or N 
was pressed, it would auto-complete it, printing 'ES' or 'O' and doing a 
Return for you ("." is the prompt; you have to imagine the clatter):


   .CONFIRM (YES/NO): YES
   .

How hard is it to do this nearly 40 years On a machines a million times 
more powerful? And in Python (although I'd imagine any difficulties it 
has originate outside the language and probably affect others).



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


Re: How to use two threads (GUI and backend)

2016-10-26 Thread jmp

On 10/26/2016 12:22 PM, pozz wrote:

Il 26/10/2016 09:13, pozz ha scritto:
 > [...]

When the user press Start button (the pressed handler is in the GUI
class):

  self.comm_active = True
  threading.Thread(target=self.comm_thread).start()

The backend thread is:

  def comm_thread(self):
while self.comm_active:
  self.device.get_status()
  GLib.idle_add(self.polling_received)
  time.sleep(1)
self.m.close()

 > [...]


Now I have some concerns even in using self.comm_active.  It is a
boolean variable accessed by the GUI thread (inside Start/Stop buttons
handler) and backend thread (in the "while self.comm_active" instruction).

Is it safe to access this variable from two different threads?  Should I
implement a safer and more complex mechanism?  If yes, what mechanism?




from http://nedbatchelder.com/blog/201204/two_problems.html

Some people, when confronted with a problem, think, "I know, I'll use 
threads," and then two they hav erpoblesms.



I suggest you write a GUI that make synchronous	calls to a remote 
application, if possible. If the remote app is in python, you have 
access to remote protocols already written for you, Pyro is one of them, 
you can skip the low level communication part.


jm

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


[RELEASE] ‘python-daemon’ version 2.1.2 released

2016-10-26 Thread Ben Finney
Howdy all,

I am pleased to announce the release of version 2.1.2 of the
‘python-daemon’ library.

The current release is always available at
https://pypi.python.org/pypi/python-daemon/>.


Significant changes since the previous version
==

Additions:

* Add a README document for the code base.

Changes:

* Migrate code project hosting to Pagure.
  Record the change of homepage URL in PyPI metadata.

* Raise a warning that the ‘runner’ module is pending deprecation.
  This has been an unofficial example module from the beginning, and
  it will be removed in a future version.

Bug Fixes:

* Ensure custom types are part of the Python type hierarchy.

* Avoid a circular dependency for the version string at install time.
  Thanks to Maarten van Gompel for the reproducible test case.


What is the ‘python-daemon’ library?


‘python-daemon’ is a Python library to implement a well-behaved Unix
daemon process.

-- 
 \   “The best is the enemy of the good.” —Voltaire, _Dictionnaire |
  `\Philosophique_ |
_o__)  |
Ben Finney 


signature.asc
Description: PGP signature
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to use two threads (GUI and backend)

2016-10-26 Thread pozz

Il 26/10/2016 09:13, pozz ha scritto:
> [...]

When the user press Start button (the pressed handler is in the GUI class):

  self.comm_active = True
  threading.Thread(target=self.comm_thread).start()

The backend thread is:

  def comm_thread(self):
while self.comm_active:
  self.device.get_status()
  GLib.idle_add(self.polling_received)
  time.sleep(1)
self.m.close()

> [...]


Now I have some concerns even in using self.comm_active.  It is a 
boolean variable accessed by the GUI thread (inside Start/Stop buttons 
handler) and backend thread (in the "while self.comm_active" instruction).


Is it safe to access this variable from two different threads?  Should I 
implement a safer and more complex mechanism?  If yes, what mechanism?


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


Re: Qtimer and extra argument

2016-10-26 Thread Phil Thompson
On 26 Oct 2016, at 10:36 am, luca72 via Python-list  
wrote:
> 
> Hello i hope that yo can reply to this question also if the argument is pyqt
> 
> i have a simple test:
> def start_timer(self):
>self.timer = QTimer()
>testo = 'pressed'
>self.timer.singleShot(1000, self.metto_testo)
> 
> def test(self, testo):
>self.lineEdit.setText(testo)
> 
> 
> How i can pass the variable testo to the def test?
> If i use :
> self.timer.singleShot(1000, self.metto_testo(testo)
> i get error

Use a lambda...

self.timer.singleShot(1000, lambda s: self.metto_testo(testo))

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


Re: ImportError: No module named 'simstring'

2016-10-26 Thread Ben Finney
Daiyue Weng  writes:

> Hi, I am trying to install simstring on Windows 10 through pip/conda, but
> couldn't find the package.

Pip installs packages listed on the Python Package Index (PyPI)
https://pypi.python.org/>, do any of the search results there match
what you want?

-- 
 \ “Pinky, are you pondering what I'm pondering?” “I think so, |
  `\  Brain, but can the Gummi Worms really live in peace with the |
_o__)  Marshmallow Chicks?” —_Pinky and The Brain_ |
Ben Finney

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


Qtimer and extra argument

2016-10-26 Thread luca72 via Python-list
Hello i hope that yo can reply to this question also if the argument is pyqt

i have a simple test:
def start_timer(self):
self.timer = QTimer()
testo = 'pressed'
self.timer.singleShot(1000, self.metto_testo)

def test(self, testo):
self.lineEdit.setText(testo)


How i can pass the variable testo to the def test?
If i use :
self.timer.singleShot(1000, self.metto_testo(testo)
i get error

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


ImportError: No module named 'simstring'

2016-10-26 Thread Daiyue Weng
Hi, I am trying to install simstring on Windows 10 through pip/conda, but
couldn't find the package.

I am using Pycharm + Anaconda, so I am wondering is it possible to install
simstring on Windows, and how.

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


Re: How to use two threads (GUI and backend)

2016-10-26 Thread pozz

Il 26/10/2016 09:13, pozz ha scritto:
> [...]

What is the best approach to use in my scenario (GUI and backend
communication)?


I just found this[1] page, where the thread approach is explained with 
the following code:


---
  import threading
  import time

  from gi.repository import GLib, Gtk, GObject


  def app_main():
win = Gtk.Window(default_height=50, default_width=300)
win.connect("delete-event", Gtk.main_quit)

progress = Gtk.ProgressBar(show_text=True)
win.add(progress)

def update_progess(i):
  progress.pulse()
  progress.set_text(str(i))
  return False

def example_target():
  for i in range(50):
GLib.idle_add(update_progess, i)
time.sleep(0.2)

win.show_all()

thread = threading.Thread(target=example_target)
thread.daemon = True
thread.start()


  if __name__ == "__main__":
# Calling GObject.threads_init() is not needed for PyGObject 3.10.2+
GObject.threads_init()

app_main()
---

This is similar to my approach, with a main difference: the callback 
update_progress() added to the GLib idle loop (so executed in the main 
GUI thread) receives all the data as arguments (the value i to write as 
text in the progress widget).


In my case, I have many many properties of the remote device. So my 
first idea is to get directly the value by accessing variables changed 
during backend thread... I think this is wrong.






[1] https://wiki.gnome.org/Projects/PyGObject/Threading
--
https://mail.python.org/mailman/listinfo/python-list


How to use two threads (GUI and backend)

2016-10-26 Thread pozz
I'm designing a GUI application in Python (with pyGObject, so GTK).  The 
application communicates with a remote device (connected through RS232, 
but it could be on Internet) to retrieve its status and set/get its 
configuration.


When the user press "Start" button, the application starts sending "GET 
STATUS" requests to the remote device, waiting its response.  When the 
response arrives, the GUI widgets are refreshed with the new status. The 
"GET STATUS" requests are send at a regular frequency (polling mode).


I thought two split the application in two threads: the GUI main thread 
that manages graphical widgets and user interaction; the backend thread 
that manages low-level communication.


When the user press Start button (the pressed handler is in the GUI class):

  self.comm_active = True
  threading.Thread(target=self.comm_thread).start()

The backend thread is:

  def comm_thread(self):
while self.comm_active:
  self.device.get_status()
  GLib.idle_add(self.polling_received)
  time.sleep(1)
self.m.close()

self.device.get_status() is blocking. It is executed in backend thread, 
so the GUI isn't blocked. self.polling_received() function will be 
executed in main thread (thanks to GLib.idle_add), because it will 
change widgets properties.


Now the get_stats() of self.comm object:

  def get_status(self):



self.property1 = 
self.property2 = 
return

And self.polling_received() of GUI class:

  def polling_received(self):
txtEntry1.set_text(self.comm.property1)
txtEntry2.set_text(self.comm.property2)



I didn't fully tested this, but it seems it works well. However I have 
some concerns, mainly for thread syncronizations.


self.polling_received() is executed in GUI thread and reads properties 
(self.comm.property1, ...) that are changed during parsing of responses 
in self.comm.get_status() function that is executed in the backend thread.
So the two threads use the same variables/objects without 
synchronization. Is this a problem?


What is the best approach to use in my scenario (GUI and backend 
communication)?

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