Skip to content

Make args from argparse.ArgumentParser().parse_args() explicit #32

@atteggiani

Description

@atteggiani

Instead of using args.<destination> as variables, make those variables explicit and call them directly.
Example:

args = parser.parse_args()
print(args)
# Convert the date/time to a formatted string
t = args.start.strftime("%Y%m%dT%H%MZ")
print(args.mask, args.file, t)
# If necessary replace ERA5 land/surface fields with higher-resolution options
if "era5land" in args.type:
replace_landsurface_with_ERA5land_IC.swap_land_era5land(args.mask, args.file, t)
shutil.move(args.file.as_posix(), args.file.as_posix().replace('.tmp', ''))

can be changed to:

- args = parser.parse_args()
+ mask, replacement_file, start, type = parser.parse_args() 
print(args)

# Convert the date/time to a formatted string
- t = args.start.strftime("%Y%m%dT%H%MZ")
+ t = start.strftime("%Y%m%dT%H%MZ")
- print(args.mask, args.file, t)
+ print(mask, replacement_file, t)

# If necessary replace ERA5 land/surface fields with higher-resolution options
- if "era5land" in args.type:
+ if "era5land" in type:
-     replace_landsurface_with_ERA5land_IC.swap_land_era5land(args.mask, args.file, t)
+     replace_landsurface_with_ERA5land_IC.swap_land_era5land(mask, replacement_file, t)
-     shutil.move(args.file.as_posix(), args.file.as_posix().replace('.tmp', ''))
+     shutil.move(replacement_file.as_posix(), replacement_file.as_posix().replace('.tmp', ''))

This simplifies the code and avoids accessing attribute multiple times (anytime args.<destination> gets called is an attribute access).

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

Status

Todo ⏳

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions