tmpbit
Posts: 10
Joined: Sun May 21, 2017 1:10 pm
Location: Bristol

passing a variable (data) to a function/class

Sun May 21, 2017 8:58 pm

Hi.
It might be a silly question, but when I thought, that my Python skills are good enough I encountered a problem.
I started validating my skills writing a program. It got bigger and bigger so I decided to split it in a few files. I use threading so I'm passing some args to the functions which are in separate files now. Initially I had problems with making variables global, but eventually I could make some of them global. I do need global variables to change their values from different parts of program (those separate files/modules). The exact structure that makes me troubles is like this:

Code: Select all

#main file with variable <path> which I want to pass over to module2 (separate file with <thread_func> function)
another_thread = threading.Thread(target=module2.func, args=(path))
timelapse_thread.start()

#module2 file that is structured as below
def func():
    def another_func():
        '''some code'''
    class MyClass():
        def do_something():
            '''some code'''
            another_func()

    '''some code'''
    something_that_uses_the_class(MyClass)
    #some code
    return
I need the variable path in another_func that is called from the MyClass (which in turn is called from main code of module2, but the variable path originally comes from the main code of the main file. How to do it? I tried a few things, but the only thing (that I think should work and seems easy to do) I haven't tried yet is to modify the class (which isn't mine) to take another argument. What do you think about it? How do you pass over 'data' between modules/parts of a program?
Thanks!

Davies
Posts: 150
Joined: Sat Apr 04, 2015 4:24 pm

Re: passing a variable (data) to a function/class

Mon May 22, 2017 5:33 pm

if your using threading and want multiple elements to share a global variable within one (.py/.pyc) file, this may be of use to you...

Code: Select all

import threading
import time

variable = 0
print "variable = ", variable


def var_change1():
    global variable
    variable = 1
    print "variable = ", variable


def var_change2():
    global variable
    variable = 2
    print "variable = ", variable


def var_change3():
    global variable
    variable = 3
    print "variable = ", variable

time.sleep(1)

t = threading.Thread(target=var_change1)
t.daemon = True
t.start()

time.sleep(1)

t1 = threading.Thread(target=var_change2)
t1.daemon = True
t1.start()

time.sleep(1)

t2 = threading.Thread(target=var_change3)
t2.daemon = True
t2.start()
variable is changed 0-3 each within its own thread but at global level
if you want that variable to be saved, a memory as such.. look into .pickle for python

if your wanting to pass a variable between multiple files pickle can work but i wouldnt say its ideal for use between multiple .py files running at the same time unless you had a way to ensure they wouldnt try to create a save at the same time. if you use a single file with threading you can use a global variable to ensure 2 things can never attempt to save data at the same time.

if you have one file acquiring the data and writing it to pickle, then another file reading the pickle and acting on the data without creating a save, that wouldnt be an issue with pickle.

take a look here... theres an example of pickle further down the page along with some other info...
https://stackoverflow.com/questions/182 ... -in-python

also if your writing your code with notepad it could be worth looking at JetBrains Pycharm as their software is python specific and points out errors within your code. get the community edition, its free.

tmpbit
Posts: 10
Joined: Sun May 21, 2017 1:10 pm
Location: Bristol

Re: passing a variable (data) to a function/class

Mon May 22, 2017 7:15 pm

Thanks for the reply. I'm reading the suggestions in the link provided. If I find the best solution I post it here.
I downloaded Pycharm some time ago, but I didn't installed it yet. I like using simple/lightweight IDE/Editors (like Atom). Maybe it's time to try something 'more appropriate' :)
Thanks!

Davies
Posts: 150
Joined: Sat Apr 04, 2015 4:24 pm

Re: passing a variable (data) to a function/class

Mon May 22, 2017 8:01 pm

thinking about it, pickle would probably be best, if your wanting both files to modify the variable you can just use two pickles, that way both scripts wont try to write the same file.

one script could use...

Code: Select all

import pickle
import time


def open_file():
    global var_1, var_2, var_3
    with open('sub.pickle') as fr:
        var_1, var_2, var_3 = pickle.load(fr)


def save_file():
    with open('main.pickle', 'w') as fs:
        pickle.dump([var_1, var_2, var_3], fs)

var_1 = "000"
var_2 = "000"
var_3 = "000"

save_file()

while 1:
    try:
        open_file()
    except:
        print "run other script"
        
    time.sleep(1)
    print var_1
    time.sleep(1)
    print var_2
    time.sleep(1)
    print var_3
    time.sleep(1)
    save_file()
and the other would use something like..

Code: Select all

import pickle
import time

with open('main.pickle') as fr:
    var_1, var_2, var_3 = pickle.load(fr)


def open_file():
    global  var_1, var_2, var_3
    with open('main.pickle') as ff:
        var_1, var_2, var_3 = pickle.load(ff)


def save_file():
    with open('sub.pickle', 'w') as fs:
        pickle.dump([var_1, var_2, var_3], fs)

while 1:
    open_file()
    if var_1 == "000":
        var_1 = "111"
        var_2 = "222"
        var_3 = "333"
        save_file()
    else:
        var_1 = "000"
        var_2 = "000"
        var_3 = "000"
        save_file()
    time.sleep(5)
in this example both scripts share and modify the same variable but it is handled between two pickle files.

tmpbit
Posts: 10
Joined: Sun May 21, 2017 1:10 pm
Location: Bristol

Re: passing a variable (data) to a function/class

Wed May 24, 2017 7:52 pm

It could work, but... I don't want to save it to disk, because I need exchange variables (including a dictionary) fast. If I won't come up with a good (for me ;)) solution I can eventually include all my modules in the main program and make the variables global.
Anyway thanks for your help.
Is pickle faster then json?

tmpbit
Posts: 10
Joined: Sun May 21, 2017 1:10 pm
Location: Bristol

Re: passing a variable (data) to a function/class

Wed May 24, 2017 9:09 pm

I have changed my program a bit. I changed some functions into classes and passing a dictionary between them. It's funny as I wanted to change only a variable that carried a boolean, but it didn't work. It's OK with dictionary and all seems to be good enough for me :)


Return to “Python”