Monday, February 24, 2014

We were investigating a way to make some automation for file generation and tried to use python to solve our problems. Doing so we found an interesting way to use python scripts.
Our python scripts were stored in various directories below a root directory.
It looked some thing like this:







Each of the directories contained several scripts that would generate corresponding pages for a website. This would mean that we need to parse the directory tree and then run the Scripts. That was done using the 'walk' function available as part of the 'os' Package. Here is the code for the function:

Another thing that was done in this function was that some time due to inclusion of the sub-modules the '__pycache__' is create or there are '__init__.py' files for packages that would not be executed.

Next we need the way to run the Python sctipts using the above 'runner' function. For this there are three ways to go about. This completely depends on the way in which the script is called , its dependencies and possible case of individual or ' Runner' execution. We decided to write for all the different ways.

In this snippet depending on the variable 'bDirect' the way of execution is decided.

  • The option 0 calls the module by using the internal interpreter function 'exec' after compiling the script.
    In this case the environment of the Caller eg. the Runner function would be completely inherited.
  • The option 1 executes the script in command line. This is same a going to each of the directories and then executing the files individually. This case no environment can be inherited. Each script is responsible for seting up its required environment for execution.
  • The option 2 executes a specific function called 'run()' defined in each of the script files. In this the script being run inherits only the Path and not any other entity. This call is similar to calling any function in any other module or package. However the user program now has a way to pass parameters to the scripts which may include local / global variables, generators or even functions.
Here is the complete program for this we call it the 'main.py' and is placed in the root for the folder:

Still there is one problem that remains , how to detect if the python scripts are being executed from the Runner or are being directly executed. As this would effect the environment initialization. To solve this we created a example script that could be used to test its own environment and be able to set up the environment in case its not being run from the 'Runner' script.

This Example program how to create the scripts in the directories such that they can either run using the Runner or directly. The 'config ' here is a package that contain the configuration data for each of the scripts. So relative paths can be used to correct the errors in case the files are run directly.

Hope that this tip would be helpful, let us know your comments if you like this.

Welcome to Boseji's Blog

Popular Post

Blogger templates

Welcome

Creation by Electronics Hobbyists from Bharat(India) to encourage & help people. This blog hosts the numerous innovations, discoveries and projects we provide.
Our Research Wing Adhar Labs
Contact Form | Email Address
We Support Open Source Hardware Definition v1.0
Creative Commons License
All works on this blog are licensed under a
Creative Commons Attribution-ShareAlike 3.0 Unported License.
Based on a work at forbharat.wordpress.com and m8051.blogspot.com.

Permissions beyond the scope of this license may be available at http://forbharat.wordpress.com/permissions/.
Thank you for all your support.

© A.D.H.A.R Labs Research 2008-2013 . Powered by Blogger.

Follow by Email

Followers

PCB Unit Converter

mils
mm

- Copyright © Electronics for Bharat -Robotic Notes- Powered by Blogger - Designed by Johanes Djogan -