Re: [Tutor] Fwd: Accessing variables from other modules

2018-09-05 Thread Alan Gauld via Tutor
On 05/09/18 18:12, Chip Wachob wrote:

> In your examples name1 and name2 could be anything that is contained
> in that module.. a variable, function, class, etc..  correct?

Correct. They are just names.

Again a difference between Python and C.
In C a name is a label associated with a memory address
which in turn is associated with a specified data type.

In Python a name is just a name, a key in a dictionary.
It's value is any kind of object and can change throughout
the program's lifetime. A string now, an integer later,
then a list and maybe even a class or instance. Of course,
it's probably not a good idea to change a variables type
too often but it is possible.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fwd: Accessing variables from other modules

2018-09-05 Thread Chip Wachob
This helps tremendously!

One last question.

In your examples name1 and name2 could be anything that is contained
in that module.. a variable, function, class, etc..  correct?





On Wed, Sep 5, 2018 at 12:58 PM, Alan Gauld via Tutor  wrote:
> On 05/09/18 15:06, Chip Wachob wrote:
>
>> Okay, I think I'm starting to get a handle on the visibility of
>> things.  As you said, much different than C.
>
> Yes. The significant thing is to remember that in
> Python you are importing names. In C you include
> the contents of the file.
>
> #include 
>
> Lets you access everything in the stdio.h file
> as if it were part of your own file.
>
> import sys
>
> Lets you see the sys module but not whats inside it.
> To access whats inside you must use the name as
> a prefix.
>
> The Pytho import idiom
>
> from sys import *
>
> is similar in effect to the C style include
> (although by an entirely different mechanism)
> but is considered bad practice (for the same
> reasons C++ has its scope operator(::))
>
>> Even through the import Adafruit_GPIO as GPIO line exists in the
>> AdafruitInit.py file, which is imported by the import AdafruitInit
>> line in main.py,
>
> The critical conceptual error here is that the
> file is not imported(*), only the name. Importing
> in Python is all about visibility control
>
> (*)In practice although the module is not
> imported into your file it is executed, so any
> new variables, classes and functions are created,
> but their names are not visible in your code
> except via the module name.
>
> Let me revisit the different import styles
> in more detail. Remember we are discussing visibility
> of names not code.
>
>
> ##
> import modulename
>
> This makes modulename visible to the importing module.
> Nothing inside modulename is visible. To access the
> contents you must use modulename as a prefix.
>
> x = modulename.name1
>
> ###
> import modulename as alias
>
> Exactly the same except that you can refer to
> modulename using the (usually shorter) alias.
>
> x = alias.name1
>
> There are a few community standard aliases such as
>
> import numpy as np
> import tkinter as tk
>
> But they are purely conventions, you can use
> any alias you like. For those who enjoy typing
> they could even do
>
> import os as operating_system
>
> And access the functions as
>
> operating_system.listdir('.')
>
> instead of
>
> os.listdir('.')
>
> if they really wanted to...
>
> 
> from module import name1, name2
>
> This imports specific names from within module.
>
> You can now access name1 and name2 directly:
>
> x - name1  # accesses module.name1
>
> But it does NOT import module itself,
> only name1, name2, etc. If you try
>
> x = module.name3
>
> You will get an error about module (not name3!)
> not being recognised.
>
>
> 
> from module import name1 as n1
>
> Same as above but use the alias n1 instead of
> the longer name1.
>
> x = n1   # like x = module.name1
>
> This is very like you doing the following:
>
> from module import name1
> n1 = name1
>
> 
> from module import *
>
> This makes all the names defined in module visible
> within the importing module. Again it does not make
> module itself visible, only the names inside.
>
> This is considered bad practice because if you have
> multiple modules containing the same name (things
> like open() and write() are common then only the
> last name imported will be visible and that can
> lead to unexpected errors.
>
>
> HTH
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fwd: Accessing variables from other modules

2018-09-05 Thread Alan Gauld via Tutor
On 05/09/18 15:06, Chip Wachob wrote:

> Okay, I think I'm starting to get a handle on the visibility of
> things.  As you said, much different than C.

Yes. The significant thing is to remember that in
Python you are importing names. In C you include
the contents of the file.

#include 

Lets you access everything in the stdio.h file
as if it were part of your own file.

import sys

Lets you see the sys module but not whats inside it.
To access whats inside you must use the name as
a prefix.

The Pytho import idiom

from sys import *

is similar in effect to the C style include
(although by an entirely different mechanism)
but is considered bad practice (for the same
reasons C++ has its scope operator(::))

> Even through the import Adafruit_GPIO as GPIO line exists in the
> AdafruitInit.py file, which is imported by the import AdafruitInit
> line in main.py, 

The critical conceptual error here is that the
file is not imported(*), only the name. Importing
in Python is all about visibility control

(*)In practice although the module is not
imported into your file it is executed, so any
new variables, classes and functions are created,
but their names are not visible in your code
except via the module name.

Let me revisit the different import styles
in more detail. Remember we are discussing visibility
of names not code.


##
import modulename

This makes modulename visible to the importing module.
Nothing inside modulename is visible. To access the
contents you must use modulename as a prefix.

x = modulename.name1

###
import modulename as alias

Exactly the same except that you can refer to
modulename using the (usually shorter) alias.

x = alias.name1

There are a few community standard aliases such as

import numpy as np
import tkinter as tk

But they are purely conventions, you can use
any alias you like. For those who enjoy typing
they could even do

import os as operating_system

And access the functions as

operating_system.listdir('.')

instead of

os.listdir('.')

if they really wanted to...


from module import name1, name2

This imports specific names from within module.

You can now access name1 and name2 directly:

x - name1  # accesses module.name1

But it does NOT import module itself,
only name1, name2, etc. If you try

x = module.name3

You will get an error about module (not name3!)
not being recognised.



from module import name1 as n1

Same as above but use the alias n1 instead of
the longer name1.

x = n1   # like x = module.name1

This is very like you doing the following:

from module import name1
n1 = name1


from module import *

This makes all the names defined in module visible
within the importing module. Again it does not make
module itself visible, only the names inside.

This is considered bad practice because if you have
multiple modules containing the same name (things
like open() and write() are common then only the
last name imported will be visible and that can
lead to unexpected errors.


HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fwd: Accessing variables from other modules

2018-09-05 Thread Chip Wachob
Thank you!

Okay, I think I'm starting to get a handle on the visibility of
things.  As you said, much different than C.

Just to confirm that I'm understanding correctly:

Even through the import Adafruit_GPIO as GPIO line exists in the
AdafruitInit.py file, which is imported by the import AdafruitInit
line in main.py, main.py does not automatically inherit the
Adafruit_GPIO.  Which is why you indicate that I need to also import
it in main.py





On Wed, Sep 5, 2018 at 9:42 AM, Alan Gauld  wrote:
> On 05/09/18 14:05, Chip Wachob wrote:
>> # module AdafruitInit.py
>> # from the Adafruit tutorials..
>> import Adafruit_GPIO.FT232H as FT232H
>> import Adafruit_GPIO as GPIO
>>
>> FT232H.use_FT232H()
>>
>> ft232h = FT232H.FT232H()
>>
>> # config settings for the SPI 'machine'
>> spi = FT232.SPI(ft232h, 4, 2, 2, FT232H.MSBFIRST)
>>
>
> That last line created a new variable inside the AdafruitInit module.
> It is not visible anywhere else.
>
>> # module RSI.py
>> import AdafruitInit
>>
>> def write(byte):
>>spi.write(byte)
>
> You use spi but it is not visible. It is inside AdafriuitInit
> so you need to prefix the name (I'd suggest importing
> as an alias!)
>
>AdafruitInit.spi.write(byte)
>
>> # toggle the latch signal
>>ft232h.output(5, GPIO.LOW)
>>ft232h.output(5, GPIO.HIGH)
>
> And the same here. ft232h is a variable inside AdafriotInit,
> so you must prefix it.
>
> BUT GPIO is defined in some other module which
> you will also need to import that
>
> import Adafruit_GPIO as GPIO
>
> adafruitInit.ft232h.output(5, GPIO.LOW)
>
>
> Any name that you try to use must be
> - defined in this module or
> - imported or
> - prefixed with the name of a module that you imported.
>
> If you do not define it in this module you must import
> or prefix.
>
>
>> ---module separator 
>>
>> # module main.py
>> import RSI
>> import AdafruitInit
>>
>> ft232h.setup(5, GPIO.OUT)
>>
>> write(0xAA)
>
> Again you need to use the prefixes (and import GPIO)
>
> AdafruitInit.ft232h.setup(...)
> RSI.write(...)
>
>> The actual error message for the ft232h... line is:
>>
>> NameError: global name 'ft232h' is not defined
>
> Please send the full error text, they contain a lot of useful
> information. In this case the summary line is enough but in
> future we really need the full text.
>
> The name error is because your main module cannpt see
> ft232h defined anywhere - it is inside the other module.
>
> So, my write line should have read?
>> RSI.write(0xAA)
>>
>> Does this mean that I need to call the ft232h like this?
>>
>> AdafruitInit.ft232h.setup(5, GPIO.OUT)
>
> Exactly. And import GPIO too.
>
>> Earlier in the thread you mentioned that there are several ways to
>> import.  Obviously there's small differences between the methods.  If
>> you know of a good resource I can use to get a better understanding /
>
> Since you know C already you should probably just read the official
> tutorial.
> It covers all the different styles.
>
> The only guidance i'll add is to avoid the
>
> from foo import *
>
> style since it easily leads to hard to spot name collisions.
> Only use it for experiments in the >>> prompt.
>
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fwd: Accessing variables from other modules

2018-09-05 Thread Alan Gauld via Tutor
On 05/09/18 14:05, Chip Wachob wrote:
> # module AdafruitInit.py
> # from the Adafruit tutorials..
> import Adafruit_GPIO.FT232H as FT232H
> import Adafruit_GPIO as GPIO
>
> FT232H.use_FT232H()
>
> ft232h = FT232H.FT232H()
>
> # config settings for the SPI 'machine'
> spi = FT232.SPI(ft232h, 4, 2, 2, FT232H.MSBFIRST)
>

That last line created a new variable inside the AdafruitInit module.
It is not visible anywhere else.

> # module RSI.py
> import AdafruitInit
>
> def write(byte):
>spi.write(byte)

You use spi but it is not visible. It is inside AdafriuitInit
so you need to prefix the name (I'd suggest importing
as an alias!)

   AdafruitInit.spi.write(byte)

> # toggle the latch signal
>ft232h.output(5, GPIO.LOW)
>ft232h.output(5, GPIO.HIGH)

And the same here. ft232h is a variable inside AdafriotInit,
so you must prefix it.

BUT GPIO is defined in some other module which
you will also need to import that

import Adafruit_GPIO as GPIO

adafruitInit.ft232h.output(5, GPIO.LOW)


Any name that you try to use must be
- defined in this module or
- imported or
- prefixed with the name of a module that you imported.

If you do not define it in this module you must import
or prefix.


> ---module separator 
>
> # module main.py
> import RSI
> import AdafruitInit
>
> ft232h.setup(5, GPIO.OUT)
>
> write(0xAA)

Again you need to use the prefixes (and import GPIO)

AdafruitInit.ft232h.setup(...)
RSI.write(...)

> The actual error message for the ft232h... line is:
>
> NameError: global name 'ft232h' is not defined

Please send the full error text, they contain a lot of useful
information. In this case the summary line is enough but in
future we really need the full text.

The name error is because your main module cannpt see
ft232h defined anywhere - it is inside the other module.

So, my write line should have read?
> RSI.write(0xAA)
>
> Does this mean that I need to call the ft232h like this?
>
> AdafruitInit.ft232h.setup(5, GPIO.OUT)

Exactly. And import GPIO too.

> Earlier in the thread you mentioned that there are several ways to
> import.  Obviously there's small differences between the methods.  If
> you know of a good resource I can use to get a better understanding /

Since you know C already you should probably just read the official
tutorial.
It covers all the different styles.

The only guidance i'll add is to avoid the

from foo import *

style since it easily leads to hard to spot name collisions.
Only use it for experiments in the >>> prompt.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fwd: Accessing variables from other modules

2018-09-05 Thread Chip Wachob
Alan,

Once again, thank you for the feedback and comments.

Revised code snips: Sorry they were not complete.  Several typos while
trying to create the SSCCE version.

# module AdafruitInit.py
# from the Adafruit tutorials..
import Adafruit_GPIO.FT232H as FT232H
import Adafruit_GPIO as GPIO

FT232H.use_FT232H()

ft232h = FT232H.FT232H()

# config settings for the SPI 'machine'
spi = FT232.SPI(ft232h, 4, 2, 2, FT232H.MSBFIRST)

---module separator 

# module RSI.py
import AdafruitInit

def write(byte):
   spi.write(byte)

# toggle the latch signal
   ft232h.output(5, GPIO.LOW)
   ft232h.output(5, GPIO.HIGH)

---module separator 

# module main.py
import RSI
import AdafruitInit

ft232h.setup(5, GPIO.OUT)

write(0xAA)




The actual error message for the ft232h... line is:

NameError: global name 'ft232h' is not defined


So, my write line should have read?

RSI.write(0xAA)

Does this mean that I need to call the ft232h like this?

AdafruitInit.ft232h.setup(5, GPIO.OUT)

And, this is why it is not visible?

I feel like I _thought_ I understood the import statements, but I
guess I didn't and that's what is getting me into trouble.

Earlier in the thread you mentioned that there are several ways to
import.  Obviously there's small differences between the methods.  If
you know of a good resource I can use to get a better understanding /
contrast of the different import types I'll read over it and make sure
I'm using the right one for my situation.

And, at this point, I'm pretty clear on the fact that the script that
runs leaves no trace behind when I exit the call to the interpreter
from the command line.

I appreciate your patience with my questions and foibles.




On Wed, Sep 5, 2018 at 4:53 AM, Alan Gauld via Tutor  wrote:
> On 05/09/18 04:12, Chip Wachob wrote:
>
>> # module RSI.py
>> def write(byte):
>>spi.write(byte)
>
> You don't have any import statements here.
> You need to import spi to use it.
>
>> # toggle the latch signal
>>ft232h.output(5, GPIO.LOW)
>>ft232h.output(5, GPIO.HIGH)
>
> And the same for ft232h
>
>>
>> # module main.py
>> import RSI
>> import AdafruitInit.py
>
> Note you do NOT use the .py extension when
> importing, just the base name.
>
>> ft232.setup(5, GPIO.OUT)
>
> But again you have not imported ft232
> Also I note that you use ft232 here but ft232h elsewhere.
> Is that correct?
>
> You must import any external names that
> you intend to use.
>
>> write(0xAA)
>
> And here you need to prefix with RSI:
>
> import RSI
>
> 
>
> RSI.write()
>
> A Python import is very different to a C include.
> In C you actually include the source text in your
> file so everything inside the file becomes visible.
> In a Python import you add names to a dictionary.
> In this case the only name added is RSI. The code
> inside the RSI module is effectively invisible to
> your main.py, only the name of the module is seen.
> So you must prefix the RSI contents before you use it.
>
>
>> "write()" tells me that
>>
>> "global name 'ft232h' is not defined"
>
> Please always post full error texts, never summarize.
>
>> Regarding permissions.  I'm not sure about the need for sudo, but that
>> was used in the example.
>
> I suspect it's needed because you are accessing
> privileged IO ports. It would not normally be
> needed to run a Python script.
>
>> I had been working most of the day on Friday and the scripts were
>> running fine.
>
> That is the real mystery since the above code
> should not have worked.
>
>> Does some of the FTDI (AdafruitInit.py) remain resident
>
> No, it will be deleted.
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fwd: Accessing variables from other modules

2018-09-05 Thread Alan Gauld via Tutor
On 05/09/18 04:12, Chip Wachob wrote:

> # module RSI.py
> def write(byte):
>spi.write(byte)

You don't have any import statements here.
You need to import spi to use it.

> # toggle the latch signal
>ft232h.output(5, GPIO.LOW)
>ft232h.output(5, GPIO.HIGH)

And the same for ft232h

> 
> # module main.py
> import RSI
> import AdafruitInit.py

Note you do NOT use the .py extension when
importing, just the base name.

> ft232.setup(5, GPIO.OUT)

But again you have not imported ft232
Also I note that you use ft232 here but ft232h elsewhere.
Is that correct?

You must import any external names that
you intend to use.

> write(0xAA)

And here you need to prefix with RSI:

import RSI



RSI.write()

A Python import is very different to a C include.
In C you actually include the source text in your
file so everything inside the file becomes visible.
In a Python import you add names to a dictionary.
In this case the only name added is RSI. The code
inside the RSI module is effectively invisible to
your main.py, only the name of the module is seen.
So you must prefix the RSI contents before you use it.


> "write()" tells me that
> 
> "global name 'ft232h' is not defined"

Please always post full error texts, never summarize.

> Regarding permissions.  I'm not sure about the need for sudo, but that
> was used in the example.  

I suspect it's needed because you are accessing
privileged IO ports. It would not normally be
needed to run a Python script.

> I had been working most of the day on Friday and the scripts were
> running fine.  

That is the real mystery since the above code
should not have worked.

> Does some of the FTDI (AdafruitInit.py) remain resident

No, it will be deleted.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Accessing variables from other modules

2018-09-05 Thread Chip Wachob
Alan,

Thanks for your comments.

I just forwarded a copy of a message to the list that I inadvertently
sent as a reply only.

So in that message there's information about the code.

Thank you for confirming that once the interpreter is finished running
my script that all the data is gone.

The board was cycled with the PC since it's powered off the USB port.

I believe that when the script closes, the Adafruit driver is removed
and the Ubuntu driver is reinstated (at least that's what the
documentation says).  So, my presumption would be that there would be
no TSR action going on.  The part on the board is 'dumb' and requires
configuration to run in whatever mode is desired.

I realize that the day of the week doesn't have anything to do with
it, but it sure was feeling like it when I wrote.  The only idea I
could come up with was that maybe something was still running in the
background and was 'helping' my code run properly, and today, that
code was not running.  Friday was a day filled with a _lot_ of trial
and error as I attempted to split this code into manageable chunks..
so anything could have happened.  But, I never ran anything in the
interpreter itself.  The script was always run from the command line
as noted in the forwarded message.







On Tue, Sep 4, 2018 at 7:41 PM, Alan Gauld via Tutor  wrote:
> On 04/09/18 16:10, Chip Wachob wrote:
>
>> (like I would do in C).  I then used the import statement to 'include'
>> them into the main.py file.
>
> OK a basically good idea but how did you use the import statement?
> There are many forms:
>
> import foo
> from foo import name1, name2,...
> from foo import *
> import foo as f
> from foo import name1 as n1
>
>
> Now to access name1 (which could be a function,
> class or global variable within foo) you could
> do any of (depending on your import):
>
> x = foo.name1
> x = name1
> x = name1
> x = f.name1
> x = n1
>
> Does your import statememt and access method
> match the above?
>
> The other thing to remember is that local variables
> (defined inside functions) are not visible outside
> the function.
>
>> I ran into a problem in that the code which was moved to the module
>> could no longer 'see' the ft232h variable (object?).  After many
>> attempts, I figured out that the best solution seemed to be to put the
>> ft232h setup code into yet another file.  I then imported that file
>> into both my main.py and foo.py files.  And, that seemed to work.. on
>> Friday.
>
> That may be a good idea, but it shouldn't really
> have been necessary. And it should work any day
> of the week :-)
>
>> This morning, I came back to continue working on the code, and now the
>> ft232h variable can no longer be 'seen' by my modules...
>
> We need to see some actual code to figure that one out.
>
>> When I was running the code on Friday it was being run from a command
>> line each time.  So, I'm assuming that the dictionary disappears and
>> that there is a new 'fresh' start each time I execute.  Is this
>> correct?
>
> Everything disappears at the end of the interpreter session,
> so yes it starts afresh every time. (Unlike if you run your
> modules from inside the interpreter in which case values
> stick around.)
>
>> main.py - calls various functions from other *.py files (foo.py, etc).
>> It also creates a console-based menu selection which determines which
>> of the functions to call.
>>
>> foo.py (and others) - contain the actual functions for the different
>> work that I want to get done.
>>
>> spi.py - contains the setup and initialization code for the Adafruit
>> board and configures it to function as a SPI peripheral via USB.
>
> It is a good idea to keep the hardware specifics in one
> place but for the purpose of this discussion it shouldn't
> make any difference it's just another module.
>
>> - Was I mislead by the fact that there was a power cycle on the
>> machine and it has now forgotten something that was staying resident
>> when I tested the code on Friday?  Or, does each 'run' of the script
>> start fresh?
>
> Each run should start afresh.
> (The only caveat is that if there is some process still
> running on your hardware(I don't know anything about the
> board in question) it may retain memory and pass it back
> to the init code in spi.py on startup. Does the board get
> power cycled between runs?)
>
>> - What approach do I need to use to be able to initialize the
>> interface in spi.py and have it be something that is accessible to all
>> the other modules in my project?
>
> It depends a lot on your actual code. Without it
> we are just guessing.
>
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor

[Tutor] Fwd: Accessing variables from other modules

2018-09-05 Thread Chip Wachob
My apologies.  I hit reply and not reply to all.

Alan,

I think I answered many of your questions in this message to Steven.




-- Forwarded message --
From: Chip Wachob 
Date: Tue, Sep 4, 2018 at 1:48 PM
Subject: Re: [Tutor] Accessing variables from other modules
To: Steven D'Aprano 


Steven,

Thank you.

Responding to your comments in order:

# module AdafruitInit.py
# from the Adafruit tutorials..
import Adafruit_GPIO.FT232H as FT232H
import Adafruit_GPIO as GPIO

FT232H.use_FT232H()

ft232h = FT232H.FT232H()

# config settings for the SPI 'machine'
spi = FT232.SPI(ft232h, 4, 2, 2, FT232H.MSBFIRST)



# module RSI.py
def write(byte):
   spi.write(byte)

# toggle the latch signal
   ft232h.output(5, GPIO.LOW)
   ft232h.output(5, GPIO.HIGH)



# module main.py
import RSI
import AdafruitInit.py

ft232.setup(5, GPIO.OUT)

write(0xAA)


"write()" tells me that

"global name 'ft232h' is not defined"


Regarding permissions.  I'm not sure about the need for sudo, but that
was used in the example.  The script will fail to run without it.  I
can't say for certain that I understand why sudo is required.  It may
have something to do with disabling the "built in" FTDI driver and
enabling the Adafruit version.  I had hoped to just get things
'working' before worrying about trying to get rid of the sudo
requirement.

I had been working most of the day on Friday and the scripts were
running fine.  I was running the scripts from the command line as
noted above (and related to your question re: sudo).  I powered off
the machine Friday night, and started it back up this morning.

Does some of the FTDI (AdafruitInit.py) remain resident even though
I've exited out of the script.  My menu has an entry that will permit
breaking the while() loop and ending the script.



...



On Tue, Sep 4, 2018 at 1:19 PM, Steven D'Aprano  wrote:
> Hi Chip, and welcome!
>
> On Tue, Sep 04, 2018 at 11:10:36AM -0400, Chip Wachob wrote:
>
>> I'll refrain from posting a bunch of code, but here is the 5000 foot view:
>
> Not posting a mountain of code is a great idea, but from 5000 ft away
> we can't see what is going on.
>
> Try posting *a little bit of code*. This is written for Java programmers
> but the principle is the same for Python:
>
> http://sscce.org/
>
> If I had to make a *wild guess* as to what is going on, it would be that
> you have a couple of modules like this:
>
> # Module spam.py
> x = 1  # global variable
> def foo():
> print(x)
>
>
> # Module eggs.py
> import spam
> from spam import x
> foo()  # prints 1, as you expect
> x = 999
> foo()  # still prints 1, instead of 999
>
>
> Is that what is going on? On something different?
>
>
> A few more comments:
>
>> execution looks like this:
>>
>> $ sudo python main.py
>
> Do you really need this to be run with root permissions?
>
>> So, the important questions are:
>>
>> - Was I mislead by the fact that there was a power cycle on the
>> machine and it has now forgotten something that was staying resident
>> when I tested the code on Friday?  Or, does each 'run' of the script
>> start fresh?
>
> Um, yes no maybe?
>
> Was there a power cycle? How were you running the scripts?
>
>
>> - What approach do I need to use to be able to initialize the
>> interface in spi.py and have it be something that is accessible to all
>> the other modules in my project?
>
>
>
>
>>
>> Thank you in advance for your time.
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> https://mail.python.org/mailman/listinfo/tutor
>>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor