Harpo
Harpo

Reputation: 83

How should I organize my scripts which are mostly the same?

So I'm new to Python and I need some help on how to improve my life. I learned Python for work and need to cut my workload a little. I have three different scripts which I run around 5 copies of at the same time all the time, they read XML data and add in information etc... However, when I make a change to a script I have to change the 5 other files too, which is annoying after a while. I can't just run the same script 5 times because each file needs some different parameters which I store as variables at the start in every script (different filepaths...).

But I'm sure theres a much better way out there?

A very small example:

script1.py

xml.open('c:\file1.xls')
while True:
    do script...


script2.py

xml.open('c:\file2.xls')
while True:
    do exactley the same script...

etc...

Upvotes: 1

Views: 138

Answers (2)

chepner
chepner

Reputation: 531888

You only need one script, that takes the name of the file as an argument:

import sys

xml.open(sys.argv[1])
while True:
    do script...

Then run the script. Other variables can be passed as additional arguments, accessed via sys.argv[2], etc.

If there are many such parameters, it may be easier to save them in a configuration file, the pass the name of the configuration file as the single argument. Your script would then parse the file for all the information it needs.

For example, you might have a JSON file with contents like

{
    "filename": "c:\file1.xls",
    "some_param": 6,
    "some_other_param": True
}

and your script would look like

import json
import sys

with open(sys.argv[1]) as f:
    config = json.load(f)

xml.open(config['filename'])
while True:
    do stuff using config['some_param'] and config['some_other_param']

Upvotes: 0

Grismar
Grismar

Reputation: 31354

You'll want to learn about Python functions and modules.

A function is the solution to your problem: it bundles some functionality and allows you to call it to run it, with only minor differences passed as a parameter:

def do_something_with_my_sheet(name):
    xml.open(name)
    while True:
        do script...

Elsewhere in your script, you can just call the function:

do_something_with_my_sheet('c:\file1.xls')

Now, if you want to use the same function from multiple other scripts, you can put the function in a module and import it from both scripts. For example:

This is my_module.py:

def do_something_with_my_sheet(name):
    xml.open(name)
    while True:
        do script...

This is script1.py:

import my_module

my_module.do_something_with_my_sheet('c:\file1.xls')

And this could be script2.py (showing a different style of import):

from my_module import do_something_with_my_sheet

do_something_with_my_sheet('c:\file2.xls')

Note that the examples above assume you have everything sitting in a single folder, all the scripts in one place. You can separate stuff for easier reuse by putting your module in a package, but that's beyond the scope of this answer - look into it if you're curious.

Upvotes: 3

Related Questions