I have this code that somehow treats config like a global variable even though it's a local variable. My colleague just noticed this because they were turning the script into a Lambda function and AWS kept giving them a NameError:
Error Message:
name 'config' is not defined
If I run the following in my terminal, it works just fine. The question is, is it supposed to error out since config isn't defined in the get_fields_for_issuetype function, or am I missing something? Why is it working just fine?
The only difference between the Lambda code and this one is that the Lambda code has a constants.py where ProjectConfig and project_configs are defined, then are imported into the main code.
This is the code that runs fine in my terminal:
import requests
from collections import namedtuple
BASE_URL = "https://example.com"
ProjectConfig = namedtuple("ProjectConfig", "projects required_fields tab_name")
project_configs = [
ProjectConfig(
projects=sorted(
[
"AGNT",
"APS",
"CLOD",
]
),
required_fields=[
"Field 1",
"Field 2",
],
tab_name="Group_1_Fields",
),
ProjectConfig(
projects=sorted(
[
"ABAGNT",
"ABAPS",
"ABCLOD",
]
),
required_fields=[
"Field 3",
"Field 4",
],
tab_name="Group_2_Fields",
),
]
def auth():
email = "email@email.com"
pwd = "***"
credentials = (email, pwd)
session = requests.Session()
session.auth = credentials
return session
def get_fields_for_issuetype(project_key, session):
url = (BASE_URL + f"/something/something")
response = session.get(url)
data = response.json()
project = data["projects"][0]
proj_key = project["key"]
name = project["name"] + f" ({proj_key}) - Issue Types"
issuetypes = project["issuetypes"]
for issuetype in issuetypes:
issuetype_name = issuetype["name"]
required_fields = config.required_fields.copy()
def main(config):
session = auth()
for project in config.projects:
table = get_fields_for_issuetype(project, session)
if __name__ == "__main__":
for config in project_configs:
main(config)
This is the code the doesn't run when turned into a Lambda function:
app.py
import requests
from constants import API_TOKEN, BASE_URL, USER_NAME, project_configs
def auth():
email = USERNAME
pwd = API_TOKEN
credentials = (email, pwd)
session = requests.Session()
session.auth = credentials
return session
def get_fields_for_issuetype(project_key, session):
url = (BASE_URL + f"/something/something")
response = session.get(url)
data = response.json()
project = data["projects"][0]
proj_key = project["key"]
name = project["name"] + f" ({proj_key}) - Issue Types"
issuetypes = project["issuetypes"]
for issuetype in issuetypes:
issuetype_name = issuetype["name"]
required_fields = config.required_fields.copy()
def main(config):
session = auth()
for project in config.projects:
table = get_fields_for_issuetype(project, session)
def lambda_handler(event, context):
for config in project_configs:
main(config)
constants.py:
from collections import namedtuple
BASE_URL = "https://example.com"
ProjectConfig = namedtuple("ProjectConfig", "projects required_fields tab_name")
project_configs = [
ProjectConfig(
projects=sorted(
[
"AGNT",
"APS",
"CLOD",
]
),
required_fields=[
"Field 1",
"Field 2",
],
tab_name="Group_1_Fields",
),
ProjectConfig(
projects=sorted(
[
"ABAGNT",
"ABAPS",
"ABCLOD",
]
),
required_fields=[
"Field 3",
"Field 4",
],
tab_name="Group_2_Fields",
),
]