Redefining name 'config' from outer scope (line 11)
11config = util.get_config()
12
13
14def get_driveclient(config):15 """Return an authorized apiclient"""
16 credentials = ServiceAccountCredentials.from_p12_keyfile(
17 config["service_account"],
Redefining name 'config' from outer scope (line 10)
10config = util.get_config()
11
12
13def get_sites_client(config, site="sustainablecorn"):14 """Return an authorized sites client"""
15
16 token = gdata.gauth.OAuth2Token(
Redefining name 'sites_client' from outer scope (line 48)
39 refresh_token=config["googleauth"]["refresh_token"],
40 )
41
42 sites_client = sclient.SitesClient(site=site)43 token.authorize(sites_client)
44 return sites_client
45
Redefining name 'token' from outer scope (line 14)
31def get_sites_client(config, site="sustainablecorn"):
32 """Return an authorized sites client"""
33
34 token = gdata.gauth.OAuth2Token(35 client_id=config["appauth"]["client_id"],
36 client_secret=config["appauth"]["app_secret"],
37 user_agent="daryl.testing",
Redefining name 'config' from outer scope (line 12)
28 drive_users.append(email.lower())
29
30
31def get_sites_client(config, site="sustainablecorn"):32 """Return an authorized sites client"""
33
34 token = gdata.gauth.OAuth2Token(
Redefining name 'varname' from outer scope (line 71)
24
25def delete_entries(current, siteid):
26 for key in current:
27 (plotid, varname) = key.split("|") 28 print("harvest_agronomic REMOVE %s %s %s" % (siteid, plotid, varname))
29 pcursor.execute(
30 """DELETE from agronomic_data where uniqueid = %s and
Redefining name 'plotid' from outer scope (line 73)
24
25def delete_entries(current, siteid):
26 for key in current:
27 (plotid, varname) = key.split("|") 28 print("harvest_agronomic REMOVE %s %s %s" % (siteid, plotid, varname))
29 pcursor.execute(
30 """DELETE from agronomic_data where uniqueid = %s and
Redefining name 'key' from outer scope (line 51)
23
24
25def delete_entries(current, siteid):
26 for key in current: 27 (plotid, varname) = key.split("|")
28 print("harvest_agronomic REMOVE %s %s %s" % (siteid, plotid, varname))
29 pcursor.execute(
Redefining name 'siteid' from outer scope (line 42)
22drive_client = util.get_driveclient(config)
23
24
25def delete_entries(current, siteid): 26 for key in current:
27 (plotid, varname) = key.split("|")
28 print("harvest_agronomic REMOVE %s %s %s" % (siteid, plotid, varname))
Redefining name 'current' from outer scope (line 44)
22drive_client = util.get_driveclient(config)
23
24
25def delete_entries(current, siteid): 26 for key in current:
27 (plotid, varname) = key.split("|")
28 print("harvest_agronomic REMOVE %s %s %s" % (siteid, plotid, varname))
Redefining name 'c2' from outer scope (line 61)
35 d = dict()
36 for rowdata in griddata["rowData"]:
37 c1 = rowdata["values"][0].get("formattedValue", "n/a")
38 c2 = rowdata["values"][1].get("formattedValue", "n/a")39 d[c1.strip()] = c2.strip()
40 return d
41
Redefining name 'c1' from outer scope (line 61)
34 griddata = sheet["data"][0]
35 d = dict()
36 for rowdata in griddata["rowData"]:
37 c1 = rowdata["values"][0].get("formattedValue", "n/a")38 c2 = rowdata["values"][1].get("formattedValue", "n/a")
39 d[c1.strip()] = c2.strip()
40 return d
Redefining name 'griddata' from outer scope (line 58)
31 )
32 j = util.exponential_backoff(f.execute)
33 sheet = j["sheets"][0]
34 griddata = sheet["data"][0]35 d = dict()
36 for rowdata in griddata["rowData"]:
37 c1 = rowdata["values"][0].get("formattedValue", "n/a")
Redefining name 'sheet' from outer scope (line 55)
30 includeGridData=True,
31 )
32 j = util.exponential_backoff(f.execute)
33 sheet = j["sheets"][0]34 griddata = sheet["data"][0]
35 d = dict()
36 for rowdata in griddata["rowData"]:
Redefining name 'j' from outer scope (line 54)
29 spreadsheetId="1PKK-vWuOryYFOSYSgt4TosrjIDX_F-opHOvrEo5q-i4",
30 includeGridData=True,
31 )
32 j = util.exponential_backoff(f.execute)33 sheet = j["sheets"][0]
34 griddata = sheet["data"][0]
35 d = dict()
Redefining name 'f' from outer scope (line 51)
25
26
27def build_xref():
28 f = sheets.spreadsheets().get(29 spreadsheetId="1PKK-vWuOryYFOSYSgt4TosrjIDX_F-opHOvrEo5q-i4",
30 includeGridData=True,
31 )
Description
The local variable name hides the variable defined in the outer scope, making it inaccessible and might confuse.
Bad practice
filename = 'myfile.txt'
def read_file(filename): # This shadows the global `filename`
with open(filename) as file:
return file.readlines()
Preferred:
FILENAME = 'myfile.txt' # renamed global to UPPER_CASE as convention
def read_file(filename):
with open(filename) as file:
return file.readlines()
Bad practice
Another usual suspect of this is when you use the same parameter name inside a function as the global variable you are using. For example:
def run_app(app):
# This `app` shadows the global app...
app.run()
if __name__ == '__main__':
app = MyApp() # This is a global variable!
run_app(app)
Preferred:
To avoid this re-defining of a global, consider not defining app
as a global, but inside a main()
function instead:
def run_app(app):
# There is no longer a global `app` variable.
app.run()
def main():
app = MyApp()
run_app(app)
if __name__ == '__main__':
main()