AnsweredAssumed Answered

How to pass arguments to function in module?

Question asked by LANDVEST on May 29, 2019
Latest reply on Jun 4, 2019 by LANDVEST

I know there's a ton of other material here and @ SE regarding this subject, but conceptually i'm just stuck. In an effort to improve the modularity of my code, I'm trying to get better at importing existing functions into scripts. I'm stuck on successfully passing arguments captured within the script to the imported module. Ideally I just want to use the script and pass it a handful of client identifiers (arguments) and have those cascade down into several modules called up in  the script. Like: functiontest2('identifier')

 

some crude code:

functiontest2.py

from HarvestTable import HarvestTableReport
import sys
HarvestThing = sys.argv[1]

HarvestTableReport(HarvestThing)

HarvestTable.py

import numpy as np
import pandas as pd
import arcpy
   
def HarvestTableReport(HarvestID):
    table = "\\\\fileshare\SDE_CONNECTIONS\\LV_NEXUS.sde\\LV_NEXUS.DATAOWNER.NORTHEAST\\clientGDB.featureclass"
    HUID = HarvestID
    whereClause = """ "LV_HARVEST_UNIT_ID" = '{0}' """.format(HUID)
    tableArray = arcpy.da.TableToNumPyArray(table, ['SUPER_TYPE','STRATA', 'OS_TYPE', 'STAND_NUMB', 'SILV_PRES', 'ACRES'], where_clause = whereClause)
    df = round(pd.DataFrame(tableArray),1)
    report = df.groupby(['SUPER_TYPE']).apply(lambda sub_df:  sub_df.pivot_table(index=['STRATA', 'OS_TYPE', 'STAND_NUMB', 'SILV_PRES'], values=['ACRES'],aggfunc=np.sum, margins=True,margins_name= 'TOTAL'))
    # this is creating a new row at level(1) called grand total
    # set it equal to the sum of ACRES where level(1) != 'Total' so you are not counting the  calculated totals in the total sum
    report.loc[('', 'GRAND TOTAL','','',''), :] = report[report.index.get_level_values(1) != 'TOTAL'].sum()
    print(whereClause)
    return np.round(report,1)
   
if __name__ == '__main__':
    HarvestTableReport()

Outcomes