My generate_report
function has 2 parameters. Within the generate_report
function I have another function named generate_sub_report
. I want to use the parameters report_type
and date_range
within generate_sub_report
.
def generate_report(report_type, date_range):
with open('file.csv') as f:
# do some stuff
def generate_sub_report():
report_map = {'Budget': ['a', 'b', 'c'], 'Finance': ['d', 'e', 'f']}
get_list_from_dict = report_map.get(report_type)
if date_range == 'Summary':
columns = ...
How do I use the function parameters report_type
and date_range
within generate_sub_report
too? What I'm trying to say is, how can I make generate_sub_report
inherit the parameters from generate_report
?
Edited/Updated Question
Run the code below.
It throws:
UnboundLocalError: local variable 'date_range' referenced before assignment
If I change def get_keyword_report_request(report_type, date_range):
with the thought that maybe I have to pass the parameters through the nested function, it then throws:
TypeError: get_keyword_report_request() missing 2 required positional arguments: 'report_type' and 'date_range'
def generate_report(report_type, date_range):
def get_keyword_report_request():
columns_map = {
"Account": ["AccountName", "AccountId", "Impressions", "Clicks", "Spend",
"Conversions"],
"Campaign": ["AccountName", "AccountId", "CampaignName", "Status", "Impressions",
"Clicks", "Spend", "Conversions"],
"Keyword": ["AccountName", "AccountId", "CampaignName", "CampaignStatus",
"AdGroupName", "AdGroupStatus", "Keyword", "KeywordStatus",
"BidMatchType", "CurrentMaxCpc", "Impressions", "Clicks", "Spend",
"AveragePosition", "Conversions"],
"Ad group": ["AccountName", "AccountId", "CampaignName", "AdGroupName", "Status",
"Impressions", "Clicks", "Spend", "AveragePosition", "Conversions"],
"Search Query": ["AccountName", "AccountId", "CampaignName", "CampaignStatus",
"AdGroupName", "SearchQuery", "Keyword", "BidMatchType",
"DeliveredMatchType", "Impressions", "Clicks", "Spend",
"AveragePosition", "Conversions"],
"Ad": ["AccountName", "AccountId", "CampaignName", "AdGroupName", "Status",
"AdTitle", "AdDescription", "DisplayUrl", "DestinationUrl", "AdStatus",
"Impressions", "Clicks", "Spend", "AveragePosition", "Conversions"]
}
columns = columns_map.get(report_type)
if isinstance(date_range, list):
# do this
print('wha')
elif isinstance(date_range, str):
date_range = date_range
print(date_range)
return(date_range, report_type)
get_keyword_report_request()
generate_report('Keyword', 'Summary')
My initial question still remains: How can I use the top-level function's parameters in a nested/inner function within the larger function? This is probably super-basic and I'm probably an idiot, thumbs down, that's fine. I clearly don't comprehend something very fundamental. Sorry for the lack of clarity initially.