From 20f590b7263b540eb5e7a116e03360066c58de4d Mon Sep 17 00:00:00 2001 From: Devdutt Shenoi Date: Wed, 9 Dec 2020 05:05:54 +0530 Subject: [PATCH] [python] code quality improvements (#1745) * Add DeepSource config * Simplify if statement * Refactor use of dict() with empty literal * Use set comprehension syntax * Refactor chained comparison * Rewrite instances of list literal within set() as set literals * Add test_slow to class definition --- .deepsource.toml | 11 +++++++++++ python/fbprophet/forecaster.py | 2 +- python/fbprophet/hdays.py | 2 +- python/fbprophet/models.py | 6 +++--- python/scripts/generate_holidays_file.py | 6 +++--- python/setup.py | 1 + 6 files changed, 20 insertions(+), 8 deletions(-) create mode 100644 .deepsource.toml diff --git a/.deepsource.toml b/.deepsource.toml new file mode 100644 index 0000000..629c63c --- /dev/null +++ b/.deepsource.toml @@ -0,0 +1,11 @@ +version = 1 + +test_patterns = ["python/fbprophet/tests/**"] +exclude_patterns = ["R/**", "notebooks/**", "docs/**", "examples/**"] + +[[analyzers]] +name = "python" +enabled = true + + [analyzers.meta] + runtime_version = "3.x.x" \ No newline at end of file diff --git a/python/fbprophet/forecaster.py b/python/fbprophet/forecaster.py index 8766657..1f5c12e 100644 --- a/python/fbprophet/forecaster.py +++ b/python/fbprophet/forecaster.py @@ -353,7 +353,7 @@ class Prophet(object): if n_vals < 2: standardize = False if standardize == 'auto': - if set(df[name].unique()) == set([1, 0]): + if set(df[name].unique()) == {1, 0}: standardize = False # Don't standardize binary variables. else: standardize = True diff --git a/python/fbprophet/hdays.py b/python/fbprophet/hdays.py index c6bf49c..9c6355c 100644 --- a/python/fbprophet/hdays.py +++ b/python/fbprophet/hdays.py @@ -531,7 +531,7 @@ class Malaysia(HolidayBase): if monthcal[i][5].month == 6: saturdays.append(monthcal[i][5]) self[saturdays[0]] = name - elif (year >= 2017) and (year <= 2021): + elif 2017 <= year <= 2021: c = Calendar(firstweekday=MONDAY) monthcal = c.monthdatescalendar(year, 7) diff --git a/python/fbprophet/models.py b/python/fbprophet/models.py index 5776469..a4ac0a6 100644 --- a/python/fbprophet/models.py +++ b/python/fbprophet/models.py @@ -175,7 +175,7 @@ class CmdStanPyBackend(IStanBackend): start = 0 end = 0 - two_dims = True if len(data.shape) > 1 else False + two_dims = len(data.shape) > 1 for cname in column_names: parsed = cname.split(".") @@ -235,7 +235,7 @@ class PyStanBackend(IStanBackend): ) args.update(kwargs) self.stan_fit = self.model.sampling(**args) - out = dict() + out = {} for par in self.stan_fit.model_pars: out[par] = self.stan_fit[par] # Shape vector parameters @@ -265,7 +265,7 @@ class PyStanBackend(IStanBackend): else: raise e - params = dict() + params = {} for par in self.stan_fit.keys(): params[par] = self.stan_fit[par].reshape((1, -1)) diff --git a/python/scripts/generate_holidays_file.py b/python/scripts/generate_holidays_file.py index 078649a..8382b2f 100644 --- a/python/scripts/generate_holidays_file.py +++ b/python/scripts/generate_holidays_file.py @@ -46,12 +46,12 @@ def generate_holidays_file(): all_holidays = [] # class names in holiday packages which are not countries # Also cut out countries with utf-8 holidays that don't parse to ascii - class_to_exclude = set(['rd', 'BY', 'BG', 'JP', 'RS', 'UA', 'KR']) + class_to_exclude = {'rd', 'BY', 'BG', 'JP', 'RS', 'UA', 'KR'} class_list2 = inspect.getmembers(hdays_part2, inspect.isclass) - country_set = set([name for name in list(zip(*class_list2))[0] if len(name) == 2]) + country_set = {name for name in list(zip(*class_list2))[0] if len(name) == 2} class_list1 = inspect.getmembers(hdays_part1, inspect.isclass) - country_set1 = set([name for name in list(zip(*class_list1))[0] if len(name) == 2]) + country_set1 = {name for name in list(zip(*class_list1))[0] if len(name) == 2} country_set.update(country_set1) country_set -= class_to_exclude diff --git a/python/setup.py b/python/setup.py index be14cd2..87fb11d 100644 --- a/python/setup.py +++ b/python/setup.py @@ -70,6 +70,7 @@ class TestCommand(test_command): ('test-runner=', 'r', "Test runner to use"), ('test-slow', 'w', "Test slow suites (default off)"), ] + test_slow = None def initialize_options(self): super(TestCommand, self).initialize_options()