1

I want to know if there is a way where I can change a value in a python script and that will in return change all the corresponding values in other scripts as a bulk change?

I'll explain myself. I have a python script that simply types in an email address in a text box (let's say we saved this script as portal.py):

EMAIL = "Test@email.com"

... loads of code in between

email = driver.find_element_by_id("ctl00_MainContent_addressView_emailAddTextBox").send_keys(EMAIL)
confirm_email = driver.find_element_by_id("ctl00_MainContent_addressView_confirmEmailTextBox").send_keys(EMAIL)

Now what is going to happen is that other users are going to use portal.py saved in their local machines. Every person has their own unique email address. So they will need to manually change their email address in the script. But what happens if we have 50 scripts that require email addresses? Then this will be time consuming.

So what I'm hoping is we have a python script which sole purpose is to change email addresses in each script in bulk, or better yet, contains the email address and all the other scripts simply calls on that script to use that email address stored in that script.

Is this possible and how can this be implemented?

BruceyBandit
  • 3,978
  • 19
  • 72
  • 144
  • You could have a file that contains only an email address. Then change each script to read that file rather than setting the email address explicitly. – zondo Feb 17 '16 at 01:07

2 Answers2

2

One of the options would be to have the email passed as a command line argument. There is a built-in and quite convenient module to parse arguments from a command line - argparse.

Example:

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('email', help='Email to be used in testing')

args = parser.parse_args()
EMAIL = args.email

Then, you can run your script as:

python your_script.py --email="test@domain.com"

You may also have a special configuration file. If we would follow what other frameworks, like Django or Scrapy, have - we would call it settings.py. At the end of the settings.py we can have an import:

try:
    from local_settings import *
except ImportError:
    pass

that would import every setting from a local_settings.py file if it exists. Usualy local_settings.py is added to the repository ignored files (.gitignore in case of git). This basically allows to redefine the default settings configured in settings.py locally. Every user of your script may create local local_settings.py file that would contain it's own EMAIL setting. In your script you would just import EMAIL from settings:

import settings

EMAIL = settings.EMAIL

See also:

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • much better idea than actually changing the email :P – Joran Beasley Feb 17 '16 at 01:12
  • @alecxe, to make sure I understand this correctly, once an email is entered in cmd, will that setting last for all time so that as long as every script contains that code you mentioned in your first snippet, it will always refer to the email set in the cmd? – BruceyBandit Feb 17 '16 at 01:29
  • I never understand myself lol. Cool thanks again. Great answer :) – BruceyBandit Feb 17 '16 at 01:45
2

If you really need to change it in the script and cannot simply use the commandline args as the other answer suggests

with open("portal.py","rb") as f:
    data = f.read()

with open("portal.py","wb") as f:
    f.write(re.sub('EMAIL = ".*"','EMAIL = "my_new_email@blah.com"',data))

#replace current script with portal.py
os.execv("python",("portal.py",)+sys.argv[1:])

but I much more strongly recommend editing portal.py to use the commandline args

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179