Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions ldndc2nc/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ def is_valid_year_range(s):
_ = int(e)
except ValueError:
return False
if int(s[1]) < int(s[0]):
return False
return True
return int(s[1]) >= int(s[0])

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function RangeAction.__call__.is_valid_year_range refactored with the following changes:

  • Simplify conditional into return statement


if is_valid_year_range(s):
setattr(namespace, self.dest, range(int(s[0]), int(s[-1]) + 1))
Expand Down Expand Up @@ -82,17 +80,16 @@ class CustomFormatter(
):
def _get_help_string(self, action):
help = action.help
if "%(default)" not in action.help:
if action.default is not argparse.SUPPRESS:
defaulting_nargs = [argparse.OPTIONAL, argparse.ZERO_OR_MORE]
if action.option_strings or action.nargs in defaulting_nargs:
if type(action.default) == list:
help += " (default: %d-%d)" % (
action.default[0],
action.default[-1],
)
else:
help += " (default: %(default)s)"
if "%(default)" not in help and action.default is not argparse.SUPPRESS:
defaulting_nargs = [argparse.OPTIONAL, argparse.ZERO_OR_MORE]
if action.option_strings or action.nargs in defaulting_nargs:
if type(action.default) == list:
help += " (default: %d-%d)" % (
action.default[0],
action.default[-1],
)
else:
help += " (default: %(default)s)"
Comment on lines -85 to +92

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _get_help_string refactored with the following changes:

  • Merge nested if conditions
  • Swap positions of nested conditionals
  • Hoist repeated code outside conditional statement
  • Use previously assigned local variable

return help


Expand Down
3 changes: 1 addition & 2 deletions ldndc2nc/extra.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def _copy_default_config():

def _find_config() -> Path:
""" look for cfgFile in the default locations """
cfgFile = None

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _find_config refactored with the following changes:

  • Inline variable that is only used once

env_var = os.environ.get("LDNDC2NC_CONF", "__NOTSET__")

locations = [
Expand All @@ -42,7 +41,7 @@ def _find_config() -> Path:
if loc.is_file():
return loc

return cfgFile
return None


def _parse_config(cfgFile):
Expand Down
6 changes: 3 additions & 3 deletions ldndc2nc/ldndc2nc.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def _select_files(inpath, ldndc_file_type, limiter=""):

infiles.sort()

if len(infiles) == 0:
if not infiles:

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _select_files refactored with the following changes:

  • Simplify sequence comparison

msg = "No LandscapeDNDC input files of type <%s>\n" % ldndc_file_type
msg += "Input dir: %s\n" % inpath
if limiter != "":
Expand Down Expand Up @@ -234,8 +234,8 @@ def read_ldndc_txt(inpath, varData, years, limiter=""):
dfs.append(df)

# we don't have any dataframes, return
# TODO: the control flow here should be more obvious
if len(dfs) == 0:
# TODO: the control flow here should be more obvious
if not dfs:
Comment on lines -237 to +238

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function read_ldndc_txt refactored with the following changes:

  • Simplify sequence comparison

log.warn("No data.frame filetype %s!" % ldndc_file_type)
continue

Expand Down
8 changes: 3 additions & 5 deletions ldndc2nc/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


def identical(elements: List) -> bool:
return all([e == elements[0] for e in elements])
return all(e == elements[0] for e in elements)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function identical refactored with the following changes:

  • Replace unneeded comprehension with generator



def valid_brackets(s: str) -> bool:
Expand All @@ -19,15 +19,13 @@ def valid_brackets(s: str) -> bool:
cnt += 1
if cnt > 1:
return False
if l == "]":
elif l == "]":
cnt -= 1
if cnt == 0:
cnt_closed += 1
elif cnt < 0:
return False
if cnt_closed > 1:
return False
return True
return cnt_closed <= 1
Comment on lines -22 to +28

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function valid_brackets refactored with the following changes:

  • Simplify conditional into switch-like form
  • Simplify conditional into return statement



def variables_compatible(s: str, src: List[str]) -> bool:
Expand Down