Re: Instance vs Class variable oddity

2019-05-18 Thread Chris Angelico
On Sat, May 18, 2019 at 1:51 PM  wrote:
>
> Correct me if I am wrong, please.
>
> I always think that the LEGB rule (e.g. the namespace to look up for) was 
> applied at compile-time, only the binding was resolved "dynamically" at 
> run-time. For example:
>
> def foo():
> print(x)
>
> foo() will cause a NameError. But after
>
> x = 5
>
> foo() will run correctly.

This is correct; however, this function will fail with UnboundLocalError:

x = 1
def foo():
print(x)
x = 2

Function locals ARE locked in at compile time.

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


Re: Instance vs Class variable oddity

2019-05-18 Thread jfong
Chris Angelico於 2019年5月18日星期六 UTC+8下午3時09分37秒寫道:
> On Sat, May 18, 2019 at 1:51 PM  wrote:
> >
> > Correct me if I am wrong, please.
> >
> > I always think that the LEGB rule (e.g. the namespace to look up for) was 
> > applied at compile-time, only the binding was resolved "dynamically" at 
> > run-time. For example:
> >
> > def foo():
> > print(x)
> >
> > foo() will cause a NameError. But after
> >
> > x = 5
> >
> > foo() will run correctly.
> 
> This is correct; however, this function will fail with UnboundLocalError:
> 
> x = 1
> def foo():
> print(x)
> x = 2
> 
> Function locals ARE locked in at compile time.
> 
> ChrisA

Thank you. This example proves that the LEGB rule was applied at compile-time, 
and it fails to resolve the binding at run-time.

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


Re: Import module from a different subdirectory

2019-05-18 Thread Rich Shepard

On Sat, 18 May 2019, dieter wrote:


Test this by looking at "sys.path" instead:

import sys
sys.path

It is "sys.path" which actually controls the import machinery.


Dieter,

Thank you. I missed this when researching PYTHONPATH. Here's what I get:


sys.path

['', '/home/rshepard/development/bustrac', '/usr/lib/python37.zip',
'/usr/lib/python3.7', '/usr/lib/python3.7/lib-dynload',
'/usr/lib/python3.7/site-packages']

All directories are present, and the one for the current project is the
first one searched.


"sys.path" is typically a sequence of folders. Python's import machinery
will look in those folders for modules/paackages for its (absolute)
imports. Thus, if you use "import " or "from  import ...", then
one of those folders should contain a module or package named "".


I understand this. Within ~/development/bustrac as the CWD when I invoke the
test application with the first line:

from classes import model as m

I still see this result:

$ python3 test_act_de.py 
Traceback (most recent call last):

  File "test_act_de.py", line 1, in 
from classes import model as m
ModuleNotFoundError: No module named 'classes'

'classes' is a package (a subdirectory) containing an empty __init__.py, not
a module. 'classes' conains a single module, model.py.

Since the project's root directory is seen by sys.path I don't know why the
import statement keeps failing.

Thanks,

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


Re: Import module from a different subdirectory

2019-05-18 Thread Peter J. Holzer
On 2019-05-18 05:45:23 -0700, Rich Shepard wrote:
> On Sat, 18 May 2019, dieter wrote:
> > > > sys.path
> ['', '/home/rshepard/development/bustrac', '/usr/lib/python37.zip',
> '/usr/lib/python3.7', '/usr/lib/python3.7/lib-dynload',
> '/usr/lib/python3.7/site-packages']
> 
> All directories are present, and the one for the current project is the
> first one searched.
[...]
> I understand this. Within ~/development/bustrac as the CWD when I invoke the
> test application with the first line:
> 
> from classes import model as m
> 
> I still see this result:
> 
> $ python3 test_act_de.py Traceback (most recent call last):
>   File "test_act_de.py", line 1, in 
> from classes import model as m
> ModuleNotFoundError: No module named 'classes'
> 
> 'classes' is a package (a subdirectory) containing an empty __init__.py, not
> a module. 'classes' conains a single module, model.py.

This works for me:

hrunkner:~ 18:14 :-) 1141% cd tmp 
hrunkner:~/tmp 18:14 :-) 1142% mkdir bustrac  
hrunkner:~/tmp 18:14 :-) 1143% cd bustrac   
hrunkner:~/tmp/bustrac 18:14 :-) 1144% mkdir classes 
hrunkner:~/tmp/bustrac 18:14 :-) 1145% touch classes/__init__.py
hrunkner:~/tmp/bustrac 18:14 :-) 1146% touch classes/model.py   
hrunkner:~/tmp/bustrac 18:14 :-) 1147% python3
Python 3.5.3 (default, Sep 27 2018, 17:25:39) 
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from classes import model
>>> 

"" is in sys.path, so "classes" and classes.model are found.

Now lets go to a different subdirectory:

hrunkner:~/tmp/bustrac 18:15 :-) 1148% mkdir scripts
hrunkner:~/tmp/bustrac 18:15 :-) 1149% cd scripts 
hrunkner:~/tmp/bustrac/scripts 18:15 :-) 1150% python3  
Python 3.5.3 (default, Sep 27 2018, 17:25:39) 
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from classes import model
Traceback (most recent call last):
  File "", line 1, in 
ImportError: No module named 'classes'
>>> 

This doesn't work, since there is no classes/model.py in "", only in "..".

But if I add a PYTHONPATH, it works again:

hrunkner:~/tmp/bustrac/scripts 18:15 :-) 1151% export 
PYTHONPATH=~/tmp/bustrac
hrunkner:~/tmp/bustrac/scripts 18:15 :-) 1152% python3  
  
Python 3.5.3 (default, Sep 27 2018, 17:25:39) 
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from classes import model
>>> 


-- 
   _  | Peter J. Holzer| we build much bigger, better disasters now
|_|_) || because we have much more sophisticated
| |   | h...@hjp.at | management tools.
__/   | http://www.hjp.at/ | -- Ross Anderson 


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


Re: Import module from a different subdirectory

2019-05-18 Thread Rich Shepard

On Sat, 18 May 2019, Peter J. Holzer wrote:


"" is in sys.path, so "classes" and classes.model are found.

Now lets go to a different subdirectory:
This doesn't work, since there is no classes/model.py in "", only in "..".

But if I add a PYTHONPATH, it works again:


Peter,

The project layout, briefly, is:

~/development/business-tracker/
classes/
gui/

All subdirectories contain a __init__.py file to identify them as packages.
'classes/' contains model.py; 'gui/' contains test_act_de.py.

The project root directory is present in both PYTHONPATH and sys.path:

$ echo $PYTHONPATH
/home/rshepard/development/bustrac/

$ python3
Python 3.7.3 (default, Mar 26 2019, 06:40:28) 
[GCC 5.5.0] on linux

Type "help", "copyright", "credits" or "license" for more information.

import sys
sys.path

['', '/home/rshepard/development/bustrac', '/usr/lib/python37.zip', 
'/usr/lib/python3.7', '/usr/lib/python3.7/lib-dynload', 
'/usr/lib/python3.7/site-packages']

When the gui/ subdirectory is the current working directory and I try to run
text_act_de.py I get the error that model.py is not found in the classes
subdirectory. I don't see how my directory structure, file locations, and
paths differ from your examples.

Regards,

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


Re: Import module from a different subdirectory

2019-05-18 Thread Piet van Oostrum
Rich Shepard  writes:

>
> $ python3
> Python 3.7.3 (default, Mar 26 2019, 06:40:28) [GCC 5.5.0] on linux
> Type "help", "copyright", "credits" or "license" for more information.
 import sys
 sys.path
> ['', '/home/rshepard/development/bustrac', '/usr/lib/python37.zip',
> '/usr/lib/python3.7', '/usr/lib/python3.7/lib-dynload',
> '/usr/lib/python3.7/site-packages']
>
> When the gui/ subdirectory is the current working directory and I try to run
> text_act_de.py I get the error that model.py is not found in the classes
> subdirectory. I don't see how my directory structure, file locations, and
> paths differ from your examples.

In a previous message the error was that 'classes' wasn't found, not that 
model.py wasn't found in classes, i.e. it was the same error as Peter got.

So to get this working you must make sure 'classes' is inside a directory that 
is in sys.path, for example by adding:

sys.path.insert(0, '..')
-- 
Piet van Oostrum 
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Import module from a different subdirectory

2019-05-18 Thread Peter J. Holzer
On 2019-05-18 09:43:34 -0700, Rich Shepard wrote:
> The project layout, briefly, is:
> 
> ~/development/business-tracker/

>   classes/
>   gui/
> 
> All subdirectories contain a __init__.py file to identify them as packages.
> 'classes/' contains model.py; 'gui/' contains test_act_de.py.
> 
> The project root directory is present in both PYTHONPATH and sys.path:
> 
> $ echo $PYTHONPATH
> /home/rshepard/development/bustrac/
 ^^^

This won't help much if your directory named "business-tracker" (see
above).

hp

-- 
   _  | Peter J. Holzer| we build much bigger, better disasters now
|_|_) || because we have much more sophisticated
| |   | h...@hjp.at | management tools.
__/   | http://www.hjp.at/ | -- Ross Anderson 


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


Re: Import module from a different subdirectory

2019-05-18 Thread Rich Shepard

On Sun, 19 May 2019, Peter J. Holzer wrote:


This won't help much if your directory named "business-tracker" (see
above).


Peter, et al.:

Yep. User error. The directory is actually 'business_tracker' and I used the
application name, 'bustrac', instead when I set PYTHONPATH. Discovered this
a bit ago when I looked over the messages in the thread and realized that I
had a major brain cramp when setting up PYTHONPATH.

Having corrected this, the problem has gone away.

My apologies to all who patiently tried to get me to see what I kept
missing.

My thanks to all,

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


Re: Checking network input processing by Python for a multi-threaded server

2019-05-18 Thread Markus Elfring
> socketserver threading model is that the main server loops waiting for
> connection requests, when it receives a request it creates a handler thread,

This data processing style can be generally fine as long as you would like
to work without a thread (or process) pool.


> and then it completely forgets about the thread

I have taken another look at the implementation of the corresponding methods.
https://github.com/python/cpython/blob/3.7/Lib/socketserver.py#L656

I get an other impression from the statements “self._threads.append(t)” 
(process_request)
and “thread.join()” (server_close).


> -- the thread is independent and completes the handling of the request.

Will a corresponding return value bet set?


> If you need to ensure everything has finished before starting the next server 
> instance,

I am still looking for the support of software constraints in this direction.


> you will have to write the extensions to socketserver.

Will the development situation evolve any more here?

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