Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Chaining scripts
#1
Hello all,For datalogging purposes i have concoted several scripts.
One reads a MCP3008 adc, another reads several 1-wire sensors, one gets the date and time and one check for a contactlock being on or off. Finally a script writes the gathered data to a CSV file.

For clarity it seems appropriate to keep the scripts separated although they have some variables in common.
Is this a good thought? If so can i write some sort over supervising script to govern them and pass on common variables.

What would be the best way of doing this.

Your comment would be very much appreciated and please bear in mind that i'm rather new to Python.

regards Steffen
Reply
#2
Yes, you can have a common module that contains the data shared by other parts of your app (or provides access to such data). Other modules import the common module. For example
# common.py

class SharedState:
    def __init__(self):
        self.foo = 14

state = SharedState()



# module sensors.py

from common import state

def bar():
    print(state.foo + 1)


# module mcp3008.py

from common import state
qux = state.foo ** 2
print('etc')
Reply
#3
Hi Gribouillis,
Tks for your reply. I tried to avoid Class as i still can't get to grips with the concept. It seems i have to master it, so i'll have a go at it.
The foo, bar and cux are fake names to be replaced by reall ones if i understand the search results form google correctly?
Greetz Steffen
Reply
#4
(Jan-16-2018, 09:25 AM)Steffenwolt Wrote: The foo, bar and cux are fake names to be replaced by reall ones
Yes, they are example names, or more technically metasytactic variables. Python also uses ham, eggs, spam for these.

The common module may contain as many variables as needed.

Don't hesitate to use classes. A class instance such as state in my example is nothing but a place where you can store data by using the dot syntax, such as
state.bar = 'hello'
state.mylist = ['a', 'b', 'c']
print(state.bar, state.mylist)
etc.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020