I have recently started learning more about Python Packages and Modules. I'm currently busy updating my existing modules so that that can be run as script or imported as a module into my other code. I'm not sure how to construct my input arguments within my module and pass them to the main() function within my module.
I've have written my my main() function and called it under if __name__ == '__main__' passing the input arguments. The inputs are currently hard coded to show what I'm trying to achieve. Any help in how to correctly construct my input arguments that the user will pass, which will then be passed onto the main function will be appreciated.
As mentioned I'm trying to be able to use the following as a script when used directly or imported as a module into my other code and run from there. If I import it as a module would I call the main() function when importing it? Is the following structure correct in how I have written the following? Any advice is appreciated.
<SPAN class="string token">'''
Created on March 12, 2017
Create a new ArcHydro Schema
File Geodatabase and Rasters
Folder
@author: PeterW
'''</SPAN>
<SPAN class="comment token"># import site-packages and modules</SPAN>
<SPAN class="keyword token">import</SPAN> re
<SPAN class="keyword token">from</SPAN> pathlib <SPAN class="keyword token">import</SPAN> Path
<SPAN class="keyword token">import</SPAN> arcpy
<SPAN class="comment token"># set environment settings</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>overwriteOutput <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">archydro_rasters_folder</SPAN><SPAN class="punctuation token">(</SPAN>workspace<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="string token">"""Create rasters folder directory
if it doens't already exist"""</SPAN>
model_name <SPAN class="operator token">=</SPAN> Path<SPAN class="punctuation token">(</SPAN>workspace<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>name
layers_name <SPAN class="operator token">=</SPAN> re<SPAN class="punctuation token">.</SPAN>sub<SPAN class="punctuation token">(</SPAN>r<SPAN class="string token">"\D+"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Layers"</SPAN><SPAN class="punctuation token">,</SPAN> model_name<SPAN class="punctuation token">)</SPAN>
layers_folder <SPAN class="operator token">=</SPAN> Path<SPAN class="punctuation token">(</SPAN>workspace<SPAN class="punctuation token">,</SPAN> layers_name<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> layers_folder<SPAN class="punctuation token">.</SPAN>exists<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Rasters folder: {0} exists"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>layers_name<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN>
layers_folder<SPAN class="punctuation token">.</SPAN>mkdir<SPAN class="punctuation token">(</SPAN>parents<SPAN class="operator token">=</SPAN><SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Rasters folder {0} created"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>layers_name<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">archydro_fgdb_schema</SPAN><SPAN class="punctuation token">(</SPAN>schema<SPAN class="punctuation token">,</SPAN> dem<SPAN class="punctuation token">,</SPAN> workspace<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="string token">"""Create file geodatabase using XML
schema and set coordinate system based
on input DEM if it doesn't already exist"""</SPAN>
model_name <SPAN class="operator token">=</SPAN> Path<SPAN class="punctuation token">(</SPAN>workspace<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>name
fgdb <SPAN class="operator token">=</SPAN> <SPAN class="string token">"{0}.gdb"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>model_name<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Exists<SPAN class="punctuation token">(</SPAN>str<SPAN class="punctuation token">(</SPAN>Path<SPAN class="punctuation token">(</SPAN>workspace<SPAN class="punctuation token">,</SPAN> fgdb<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"{0} file geodatabase exists"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>fgdb<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN>
new_fgdb <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>CreateFileGDB_management<SPAN class="punctuation token">(</SPAN>str<SPAN class="punctuation token">(</SPAN>workspace<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> fgdb<SPAN class="punctuation token">)</SPAN>
import_type <SPAN class="operator token">=</SPAN> <SPAN class="string token">"SCHEMA_ONLY"</SPAN>
config_keyword <SPAN class="operator token">=</SPAN> <SPAN class="string token">"DEFAULTS"</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"New {0} file geodatabase created"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>fgdb<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>ImportXMLWorkspaceDocument_management<SPAN class="punctuation token">(</SPAN>new_fgdb<SPAN class="punctuation token">,</SPAN> schema<SPAN class="punctuation token">,</SPAN>
import_type<SPAN class="punctuation token">,</SPAN>
config_keyword<SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"ArcHydro schema imported"</SPAN><SPAN class="punctuation token">)</SPAN>
projection <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Describe<SPAN class="punctuation token">(</SPAN>dem<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>spatialReference
projection_name <SPAN class="operator token">=</SPAN> projection<SPAN class="punctuation token">.</SPAN>PCSName
feature_dataset <SPAN class="operator token">=</SPAN> Path<SPAN class="punctuation token">(</SPAN>workspace<SPAN class="punctuation token">,</SPAN> fgdb<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Layers"</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>DefineProjection_management<SPAN class="punctuation token">(</SPAN>str<SPAN class="punctuation token">(</SPAN>feature_dataset<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN>
projection<SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Changed projection to {0}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>projection_name<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">main</SPAN><SPAN class="punctuation token">(</SPAN>workspace<SPAN class="punctuation token">,</SPAN> dem<SPAN class="punctuation token">,</SPAN> schema<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="string token">"""main function to create rasters folder
and file geodatabase"""</SPAN>
archydro_rasters_folder<SPAN class="punctuation token">(</SPAN>workspace<SPAN class="punctuation token">)</SPAN>
archydro_fgdb_schema<SPAN class="punctuation token">(</SPAN>schema<SPAN class="punctuation token">,</SPAN> dem<SPAN class="punctuation token">,</SPAN> workspace<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> __name__ <SPAN class="operator token">==</SPAN> <SPAN class="string token">'__main__'</SPAN><SPAN class="punctuation token">:</SPAN>
main<SPAN class="punctuation token">(</SPAN>workspace <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"E:\Projects\2016\01_Bertrand_Small_Projects\G113268\ArcHydro\Model04"</SPAN><SPAN class="punctuation token">,</SPAN>
dem <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"E:\Projects\2016\01_Bertrand_Small_Projects\G113268\ArcHydro\DEM2\raw"</SPAN><SPAN class="punctuation token">,</SPAN>
schema <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"E:\Python\Masters\Schema\ESRI_UC12\ModelBuilder\Schema\Model01.xml"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>