How to split python script in parts and import parts in a loop?

Firstly, sorry for my silly headline :) And here is my problem .. Actually this is not a problem. Everything works, but I want to have a better structure ...

I have a python script with a looped loop every second. There are a lot of IFs in the loop. Is it possible to put each IF in a separate file and then include it in a loop? Thus, every time the loop is โ€œloopedโ€, all IFs will also be transmitted.

My script has too many conditions, and all of them are generally different from the others, so I want to have some kind of folder with modules - mod_wheather.py, mod_sport.py, mod_horoscope.py, etc.

Thanks in advance. I hope I wrote everything thatโ€™s clear ...

EDIT: Here is a structural example of what I have now:

while True: if condition=='news': #do something if condition=='sport': #so something else time.sleep(1) 

Well, if I had something like this:

 while True: import mod_news import mod_sport time.sleep(1) 

And these IFs from the first example will be separated in the files mod_news.py, mod_sport.py ...

+4
source share
6 answers

You might be wondering how to work with your own modules in general. create one file called "weather.py" and include the corresponding if statements in it:

 """ weather.py - conditions to check """ def check_all(*args, **kwargs): """ check all conditions """ if check_temperature(kwargs['temperature']): ... your code ... def check_temperature(temp): -- perhaps some code including temp or whatever ... return temp > 40 

same for sport.py, horoscope.py etc.

then your main script will look like this:

 import time, weather, sport, horoscope kwargs = {'temperature':30} condition = 'weather' while True: if condition == 'weather': weather.check_all(**kwargs) elif condition == 'sport': sport.check_all() elif condition == 'horoscope': horoscope.check_all() time.sleep(1) 

edit: edited according to the resolution in your question. Please note that I suggest importing all modules only once, at the beginning of the script and using its functions. This is better than code execution by import. But if you insist, you can use reload(weather) , which actually does a reboot, including code execution. But I can't stress too much that using the functions of external modules is the best way to go!

+6
source

Put them in functions in separate files, and then Import them:

 """thing1.py A function to demonstrate """ def do_things(some_var): print("Doing things with %s" % (some_var)) 

``

 """thing2.py Demonstrates the same thing with a condition """ def do_things(some_var): if len(some_var) < 10: print("%s is < 10 characters long" % (some_var)) else: print("too long") 

``

 """main_program.py""" import thing1, thing2 myvar = "cats" thing1.do_things(myvar) thing2.do_things(myvar) 
+3
source

Perhaps you only need to call functions in your loop; and have these functions in other modules that you import as needed.

 while true: if condition: from module_a import f f() if condition2 from module_b import g g() 

Although the above is legitimate Python, and therefore answers your question, you should practice writing all the imports at the top of your file.

+1
source

I believe you are looking for some kind of PHP-like include() or C prepocessor #include . You will have a file, for example included.py below:

 a = 2 print "ok" 

and another file that has the following code:

 for i in values: import included 

and you want the result to be equivalent

 for i in values: a = 2 print "ok" 

Is this what you are looking for? If so ... no, that is not possible. After Python imports the module, the module code is executed, and after importing the same mode, the already imported instance of the module is retrieved. Module code is not executed every time it is imported.

I can come up with some crazy ways to do this (let's say file.read() + eval() or calling reload() in an imported module.), But that would be a bad idea. I bet we can think of a better solution to your real problem :)

+1
source

You can import the necessary modules if necessary, for example:

 if condition: import weather ... do something 

However, I'm not sure if this is what you really want.

0
source

I have a python script with a looped loop every second. There are many IFs in the loop.

Then you have to optimize the tests you have done repeatedly. Suppose your code has 50 IFs blocks, and that at the end of the for loop of the Nth condition is True: this means that the other conditions of N-1 must be checked before Nth is tested, and the corresponding code will be executed .

It would be preferable:

 # to_include.py def func_weather(*args,**kwargs): # code return "I'm the weather" def func_horoscope(*args,**kwargs): # code return "Give me your birth'date" def func_gastronomy(*args,**kwargs): # code return 'Miam crunch' def func_sports(*args,**kwargs): # code return 'golf, swimming and canoeing in the resort station' didi = {'weather':func_weather, 'horoscope':func_horoscope, 'gastronomy':func_gastronomy, 'sports':func_sports} 

and main module:

 # use_to_include.py import to_include x = 'sports' y = to_include.didi[x]() # instead of # if x =='weather' : y = func_weather() # elif x=='horoscope' : y = func_horoscope() # elif x=='gastronomy': y = func_gastronomy() # elif x=='sports' : y = func_sports() print y 

result

 golf, swimming and canoeing in the resort station 
0
source

Source: https://habr.com/ru/post/1369316/


All Articles