Re: GUI, Python2.7- how to build a loop with a button + textbox

2018-09-12 Thread Thomas Jollans

On 12/09/18 04:51, alon.naj...@gmail.com wrote:

hi,

on python 2.7

Please use Python 3.

  how do I build a loop with a button + textbox?
There are many GUI libraries. Tkinter is part of the standard library 
and appears to be well-documented. 
https://docs.python.org/3/library/tkinter.html


A good (and normally less ugly) alternative is Qt, using either PyQt5 or 
PySide2 for Python bindings.




for example:

I want the user to enter is name and then press "ok" button, I want his name to 
be printed 5 times.


thanks! :) you people are amazing it's almost better then stackoverflow :D


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


Re: Enum with nested classes or with types as members

2018-09-12 Thread Ben Finney
Ethan Furman  writes:

> I'm asking because in doing some work on Enum it became apparent to me
> that having nested classes was not a smooth, satisfying experience,
> and I'm considering treating them the same way as methods (they will
> no longer be converted into members).

For reference (and to hopefully better inform this discussion) the topic,
of Enum subclasses with nested class attributes, was raised recently
https://mail.python.org/pipermail/python-list/2018-June/735128.html>
by Ethan, and several conflicting positions were aired back then.

> So if you use that functionality, tell me now!  :)

Thanks for keeping this going, I hope a consensus can emerge.

-- 
 \   “If we listen only to those who are like us, we will squander |
  `\   the great opportunity before us: To live together peacefully in |
_o__)a world of unresolved differences.” —David Weinberger |
Ben Finney

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


Re: Sending file to the user gives UnicodeEncodeError in Flask framework

2018-09-12 Thread Alister via Python-list
On Wed, 12 Sep 2018 06:57:36 -0700, Νίκος Βέργος wrote:

> I want to send the user a file when he clicks the appropriate button and
> I'm suing the following.
> 
>   # Prepare selected file for download...
>   send_file( '/home/nikos/wsgi/static/files/' + filename )
> 
> But no matter what file the usser selects iam always receiving this
> response.
> 
> 
> [Wed Sep 12 14:10:48.450211 2018] [wsgi:error] [pid 5172] [remote
> 46.103.174.201:14089]   File "/home/nikos/wsgi/downloads.py", line
> 182, in file [Wed Sep 12 14:10:48.450214 2018] [wsgi:error] [pid
> 5172] [remote 46.103.174.201:14089] send_file(
> '/home/nikos/wsgi/static/files/' + filename )
> [Wed Sep 12 14:10:48.450219 2018] [wsgi:error] [pid 5172] [remote
> 46.103.174.201:14089]   File
> "/usr/lib/python3.6/site-packages/flask/helpers.py", line 592, in
> send_file [Wed Sep 12 14:10:48.450221 2018] [wsgi:error] [pid 5172]
> [remote 46.103.174.201:14089] file = open(filename, 'rb')
> [Wed Sep 12 14:10:48.450237 2018] [wsgi:error] [pid 5172] [remote
> 46.103.174.201:14089] UnicodeEncodeError: 'ascii' codec can't encode
> characters in position 30-39: ordinal not in range(128)
> 
> 
> How will I be able to send the selected file to the user?

try providing enough code for people to reproduce the problem & you may 
get some assistance although i suspect many posters may believe you to be 
a previous poster (also called Nicos) who was a disaster looking for 
somewhere to happen, if that is not you then I would also suggest you try 
reading https://www.biostars.org/p/75548/



-- 
"Free markets select for winning solutions."
-- Eric S. Raymond
-- 
https://mail.python.org/mailman/listinfo/python-list


getfqdn passes a hostname to gethostbyaddr instead of an ip address

2018-09-12 Thread Florian Bergmann
Hello,

While I was debugging some salt issues I dug into the python code and found a
piece of code in the `socket.py` module that surprised my a bit:

In the `getfqdn` function the `gethostbyaddr` name function is being called with
a `hostname` instead of an `ipaddress`:

```python
def getfqdn(name=''):
"""Get fully qualified domain name from name.
An empty argument is interpreted as meaning the local host.
First the hostname returned by gethostbyaddr() is checked, then
possibly existing aliases. In case no FQDN is available, hostname
from gethostname() is returned.
"""
name = name.strip()
if not name or name == '0.0.0.0':
name = gethostname() # (1)
try:
hostname, aliases, ipaddrs = gethostbyaddr(name) # (2)
except error:
pass
else:
aliases.insert(0, hostname)
for name in aliases:
if '.' in name:
break
else:
name = hostname
return name
```

1. `gethostname()`:

The documentation states:

```
Return a string containing the hostname of the machine where the Python 
interpreter is currently executing.

If you want to know the current machine’s IP address, you may want to use 
gethostbyname(gethostname()).
```

2. `gethostbyaddr()`:

Also from the documentation:

```
Return a triple (hostname, aliaslist, ipaddrlist) where hostname is the primary 
host name responding to the *given ip_address* (...)
```

As the documentation states it expects an `ip_address` and not a hostname,
but it is given a `hostname` instead.

I used the following two snippets to check the different behaviors:

```
python -c 'import socket; 
print(socket.gethostbyaddr(socket.gethostbyname(socket.gethostname('
```

```
python -c 'import socket; print(socket.gethostbyaddr(socket.gethostname()))'
```

Now on *most* of my machines the results are exactly the same, but on some it
differs (which I actually attribute to strange `/etc/hosts` configurations).

On the other hand I feel given the documentation, passing the `ip_address` would
be the right thing to do, so I am wondering if I am missing something very
obvious here (especially given that the code seems to be unchanged for 18 
years).

Best regards,

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


Re: Running a second wsgi script from within the first wsgi script

2018-09-12 Thread tienrua
Actualy only one wsgi script can run(mean apache handler redirector), your 
server got  "superhost.gr" name but this outer namespace(on apache.conf virtual 
server *:80), you need call local name for internal accessing. Replace  
"http://superhost.gr/clientele"; to ["http://127.0.0.1/clientele";, or 
"http://localhost/clientele";]. Request mode is important local>>global, 
global>>local, local>>local , i can't imagine which method you are using. I 
hope helpfull, kala imera!

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


Re: getfqdn passes a hostname to gethostbyaddr instead of an ip address

2018-09-12 Thread Rhodri James

On 12/09/18 15:29, Florian Bergmann wrote:

Hello,

While I was debugging some salt issues I dug into the python code and found a
piece of code in the `socket.py` module that surprised my a bit:

In the `getfqdn` function the `gethostbyaddr` name function is being called with
a `hostname` instead of an `ipaddress`:


[snip]


2. `gethostbyaddr()`:

Also from the documentation:

```
Return a triple (hostname, aliaslist, ipaddrlist) where hostname is the primary 
host name responding to the *given ip_address* (...)
```

As the documentation states it expects an `ip_address` and not a hostname,
but it is given a `hostname` instead.


I believe the online documentation is wrong.  The help text certainly 
differs:


Help on built-in function gethostbyaddr in module _socket:

gethostbyaddr(...)
gethostbyaddr(host) -> (name, aliaslist, addresslist)

Return the true host name, a list of aliases, and a list of IP 
addresses,
for a host.  The host argument is a string giving a host name or IP 
number.



--
Rhodri James *-* Kynesim Ltd
--
https://mail.python.org/mailman/listinfo/python-list


Trying to use threading.local()

2018-09-12 Thread Steven D'Aprano
I'm originally posted this on the Python-Ideas list, but this is probably 
more appropriate.


import time
from threading import Thread, local

def func():
pass

def attach(value):
func.__params__ = local()
func.__params__.value = value


def worker(i):
print("called from thread %s" % i)
attach(i)
assert func.__params__.value == i
time.sleep(3)
value = func.__params__.value
if value != i:
print("mismatch", i, value)

for i in range(5):
t = Thread(target=worker, args=(i,))
t.start()

print()





When I run that, each of the threads print their "called from ..."
message, the assertions all pass, then a couple of seconds later they
consistently all raise exceptions:

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/threading.py", line 914, in
_bootstrap_inner
self.run()
  File "/usr/local/lib/python3.5/threading.py", line 862, in run
self._target(*self._args, **self._kwargs)
  File "", line 5, in worker
AttributeError: '_thread._local' object has no attribute 'value'



What am I doing wrong?



-- 
Steven D'Aprano
"Ever since I learned about confirmation bias, I've been seeing
it everywhere." -- Jon Ronson

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


Re: getfqdn passes a hostname to gethostbyaddr instead of an ip address

2018-09-12 Thread Thomas Jollans

On 12/09/18 16:29, Florian Bergmann wrote:

On the other hand I feel given the documentation, passing the `ip_address` would
be the right thing to do, so I am wondering if I am missing something very
obvious here (especially given that the code seems to be unchanged for 18 
years).
Whatever the docs say, turning the hostname into an IP address and 
working with that would be incorrect.


Say we have a server, 'fred.weasley.example.com', which is also known as 
'www.example.com'. Its reverse DNS pointer is 
'fred.weasley.example.com'. Now, if we have 'example.com' on our DNS 
search path, the FQDN of 'www' is 'www.example.com', while the FQDN 
derived from the IP would be 'fred.weasley.example.com'.


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


Re: GUI, Python2.7- how to build a loop with a button + textbox

2018-09-12 Thread Peter Pearson
On Tue, 11 Sep 2018 19:51:01 -0700 (PDT), alon.naj...@gmail.com wrote:
> hi,
>
> on python 2.7 how do I build a loop with a button + textbox?
>
> for example:
>
> I want the user to enter is name and then press "ok" button, I want
> his name to be printed 5 times.

Tested on Python 3.5.3:

import browsergui as bg

def button_clicked():
for i in range(5):
print(text_field.value)
   
text_field = bg.TextField()
bg.GUI(text_field, bg.Button("OK", callback=button_clicked)).run(quiet=True)

If you don't have browsergui, "pip install browsergui".

-- 
To email me, substitute nowhere->runbox, invalid->com.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Trying to use threading.local()

2018-09-12 Thread MRAB

On 2018-09-12 17:16, Steven D'Aprano wrote:

I'm originally posted this on the Python-Ideas list, but this is probably
more appropriate.


import time
from threading import Thread, local

def func():
 pass

def attach(value):
 func.__params__ = local()
 func.__params__.value = value


def worker(i):
 print("called from thread %s" % i)
 attach(i)
 assert func.__params__.value == i
 time.sleep(3)
 value = func.__params__.value
 if value != i:
 print("mismatch", i, value)

for i in range(5):
 t = Thread(target=worker, args=(i,))
 t.start()

print()





When I run that, each of the threads print their "called from ..."
message, the assertions all pass, then a couple of seconds later they
consistently all raise exceptions:

Exception in thread Thread-1:
Traceback (most recent call last):
   File "/usr/local/lib/python3.5/threading.py", line 914, in
_bootstrap_inner
 self.run()
   File "/usr/local/lib/python3.5/threading.py", line 862, in run
 self._target(*self._args, **self._kwargs)
   File "", line 5, in worker
AttributeError: '_thread._local' object has no attribute 'value'



What am I doing wrong?


I've changed the code to:

import time
from threading import Thread, local

def func():
pass

def attach(value):
func.__params__ = local()
func.__params__.value = value

def worker(i):
print("called from thread %s" % i)
attach(i)
print('thread %s before sleep => %s' % (i, func.__params__))
assert func.__params__.value == i
time.sleep(1)
print('thread %s after sleep => %s' % (i, func.__params__))
time.sleep(1)
print('thread %s finally => %s' % (i, func.__params__))
print('stored value for thread %s is %s' % (i, 
getattr(func.__params__, 'value', 'MISSING')))


for i in range(2):
t = Thread(target=worker, args=(i,))
t.start()

print()



My output is:

called from thread 0
thread 0 before sleep => <_thread._local object at 0x014936FB8C50>
called from thread 1
thread 1 before sleep => <_thread._local object at 0x014936FB8CA8>

thread 1 after sleep => <_thread._local object at 0x014936FB8CA8>
thread 0 after sleep => <_thread._local object at 0x014936FB8CA8>
thread 0 finally => <_thread._local object at 0x014936FB8CA8>
stored value for thread 0 is MISSING
thread 1 finally => <_thread._local object at 0x014936FB8CA8>
stored value for thread 1 is 1


Note that thread 1 is overwriting the reference to the thread-local 
storage of thread 0.


It looks like what's happening is that thread 0 ends up trying to read 
the storage for thread 1, but as it's for a different thread, the 
contents aren't visible to it.

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


Re: Trying to use threading.local()

2018-09-12 Thread Peter Otten
Steven D'Aprano wrote:

> I'm originally posted this on the Python-Ideas list, but this is probably
> more appropriate.
> 
> 
> import time
> from threading import Thread, local
> 
> def func():
> pass
> 
> def attach(value):
  # no new local() here
> func.__params__.value = value
> 
> def worker(i):
> print("called from thread %s" % i)
> attach(i)
> assert func.__params__.value == i
> time.sleep(3)
> value = func.__params__.value
> if value != i:
> print("mismatch", i, value)

  func.__params__ = local()
> 
> for i in range(5):
> t = Thread(target=worker, args=(i,))
> t.start()
> 
> print()
> 
> 
> 
> 
> 
> When I run that, each of the threads print their "called from ..."
> message, the assertions all pass, then a couple of seconds later they
> consistently all raise exceptions:
> 
> Exception in thread Thread-1:
> Traceback (most recent call last):
>   File "/usr/local/lib/python3.5/threading.py", line 914, in
> _bootstrap_inner
> self.run()
>   File "/usr/local/lib/python3.5/threading.py", line 862, in run
> self._target(*self._args, **self._kwargs)
>   File "", line 5, in worker
> AttributeError: '_thread._local' object has no attribute 'value'
> 
> 
> 
> What am I doing wrong?

As I understand it you need one local() instance that is shared by all 
workers. Every thead will then see thread-specific values. Once you 
overwrite func.__params__ with a new local() in the next worker the data 
from the previously initialised worker is lost.



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


Re: Sending file to the user gives UnicodeEncodeError in Flask framework

2018-09-12 Thread dieter
> On Wed, 12 Sep 2018 06:57:36 -0700, Νίκος Βέργος wrote:
>> I want to send the user a file when he clicks the appropriate button and
>> I'm suing the following.
>> 
>>  # Prepare selected file for download...
>>  send_file( '/home/nikos/wsgi/static/files/' + filename )
>> 
>> But no matter what file the usser selects iam always receiving this
>> response.
>> 
>> 
>> [Wed Sep 12 14:10:48.450211 2018] [wsgi:error] [pid 5172] [remote
>> 46.103.174.201:14089]   File "/home/nikos/wsgi/downloads.py", line
>> 182, in file [Wed Sep 12 14:10:48.450214 2018] [wsgi:error] [pid
>> 5172] [remote 46.103.174.201:14089] send_file(
>> '/home/nikos/wsgi/static/files/' + filename )
>> [Wed Sep 12 14:10:48.450219 2018] [wsgi:error] [pid 5172] [remote
>> 46.103.174.201:14089]   File
>> "/usr/lib/python3.6/site-packages/flask/helpers.py", line 592, in
>> send_file [Wed Sep 12 14:10:48.450221 2018] [wsgi:error] [pid 5172]
>> [remote 46.103.174.201:14089] file = open(filename, 'rb')
>> [Wed Sep 12 14:10:48.450237 2018] [wsgi:error] [pid 5172] [remote
>> 46.103.174.201:14089] UnicodeEncodeError: 'ascii' codec can't encode
>> characters in position 30-39: ordinal not in range(128)

The "UnicodeEncodeError" indicates that Python tries to convert
(= "encode") a unicode string into bytes and the corresponding encoding
is unable to do so.

It is quite natural, that the "send_file" wants to transfer bytes.
It is not natural that in between it has to handle unicode.
The "open(filename, 'rb')" should open the file in binary (= bytes) mode;
subsequent reads should return bytes.

But, potentially, the "open(filename, 'rb')", itself, raises
the "UnicodeEncodeError". Is it possible that your "filename"
contains special characters? In this case, you may need to
encode (the complete!) filename yourself with an encoding
appropriate for your file system (likely "utf-8") before you
pass it to "send_file".


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