Sourcery Starbot ⭐ refactored MAli82/py2sas#1
Conversation
| usageString = "Usage: python3 %s <infile> <outfile> [out_var_name]" % sys.argv[0] | ||
| usageString = f"Usage: python3 {sys.argv[0]} <infile> <outfile> [out_var_name]" | ||
|
|
||
| # Basic checks | ||
| if len(sys.argv) < 3 or len(sys.argv) > 4: | ||
| print("Wrong parameters! %s" % usageString) | ||
| print(f"Wrong parameters! {usageString}") | ||
| sys.exit(1) | ||
|
|
||
| # Load input and output file names | ||
| in_file = sys.argv[1] | ||
| out_file = sys.argv[2] | ||
|
|
||
| out_var_name = "P_TARGET" | ||
| if len(sys.argv) == 4: | ||
| out_var_name = sys.argv[3] | ||
|
|
||
| out_var_name = sys.argv[3] if len(sys.argv) == 4 else "P_TARGET" |
There was a problem hiding this comment.
Lines 5-19 refactored with the following changes:
- Replace interpolated string formatting with f-string [×2] (
replace-interpolation-with-fstring) - Move setting of default value for variable into
elsebranch (introduce-default-else) - Replace if statement with if expression (
assign-if-exp)
| comp_types = ["xgboost.sklearn.XGBModel", "lightgbm.LGBMModel", "lightgbm.basic.Booster", "sklearn.ensemble.RandomForestClassifier", "GBM.pmml file"] | ||
|
|
||
| parser = None | ||
| if xgboost and isinstance(model, xgboost.sklearn.XGBModel): | ||
| if model.booster not in ['gbtree', 'dart']: | ||
| raise RuntimeError("Model is xgboost. Unsupported booster type: %s. Supported types are: %s" % (model.booster, ', '.join(comp_types))) | ||
| comp_types = ["xgboost.sklearn.XGBModel", "lightgbm.LGBMModel", "lightgbm.basic.Booster", "sklearn.ensemble.RandomForestClassifier", "GBM.pmml file"] | ||
|
|
||
| raise RuntimeError( | ||
| f"Model is xgboost. Unsupported booster type: {model.booster}. Supported types are: {', '.join(comp_types)}" | ||
| ) |
There was a problem hiding this comment.
Function _check_type refactored with the following changes:
- Move assignments closer to their usage (
move-assign) - Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring)
| output = unicodedata.normalize('NFKD', input).encode('ASCII', 'ignore').decode() | ||
| else: | ||
| # On Python < 3.0.0 | ||
| if type(input) == str: | ||
| input = unicode(input, 'ISO-8859-1') | ||
| output = unicodedata.normalize('NFKD', input).encode('ASCII', 'ignore') | ||
|
|
||
| return output | ||
| return unicodedata.normalize('NFKD', input).encode('ASCII', 'ignore').decode() | ||
| # On Python < 3.0.0 | ||
| if type(input) == str: | ||
| input = unicode(input, 'ISO-8859-1') | ||
| return unicodedata.normalize('NFKD', input).encode('ASCII', 'ignore') |
There was a problem hiding this comment.
Function TreeParser._remove_diacritic refactored with the following changes:
- Lift return into if (
lift-return-into-if) - Remove unnecessary else after guard condition (
remove-unnecessary-else)
| if node is not None: | ||
| self._node = node | ||
| else: | ||
| self._node = self._root | ||
|
|
||
| self._node = node if node is not None else self._root |
There was a problem hiding this comment.
Function TreeParser.parse_node refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp) - Replace call to format with f-string [×5] (
use-fstring-for-formatting) - Use f-string instead of string concatenation [×3] (
use-fstring-for-concatenation)
|
|
||
| def _aggregate(self, booster_count): | ||
| return "treeValue = sum({});\n".format(', '.join(["treeValue%d" % i for i in range(booster_count)])) | ||
| return f"""treeValue = sum({', '.join(["treeValue%d" % i for i in range(booster_count)])});\n""" |
There was a problem hiding this comment.
Function EnsembleParser._aggregate refactored with the following changes:
- Replace call to format with f-string (
use-fstring-for-formatting)
| return "treeValue = sum({}) / {};\n".format(', '.join(["treeValue%d" % i for i in range(booster_count)]), booster_count) | ||
| return f"""treeValue = sum({', '.join(["treeValue%d" % i for i in range(booster_count)])}) / {booster_count};\n""" |
There was a problem hiding this comment.
Function ForestParser._aggregate refactored with the following changes:
- Replace call to format with f-string (
use-fstring-for-formatting)
|
|
||
| self._dump = booster.dump_model() | ||
| if self._dump['objective'] != 'binary sigmoid:1': | ||
| raise Exception("Unfortunately only binary sigmoid objective function is supported right now. Your objective is %s. Please, open an issue at https://gitlab.sas.com/from-russia-with-love/lgb2sas." % self.dump['objective']) | ||
| raise Exception( | ||
| f"Unfortunately only binary sigmoid objective function is supported right now. Your objective is {self.dump['objective']}. Please, open an issue at https://gitlab.sas.com/from-russia-with-love/lgb2sas." | ||
| ) |
There was a problem hiding this comment.
Function LightgbmParser.__init__ refactored with the following changes:
- Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring)
| raise RuntimeError("Lxml is not installed. Lxml is needed to parse xml files.") | ||
| raise RuntimeError("Lxml is not installed. Lxml is needed to parse xml files.") | ||
| objectify.deannotate(tree_root, cleanup_namespaces=True) | ||
|
|
||
| self._forest = tree_root.find('MiningModel/Segmentation')[0].find('MiningModel') | ||
|
|
||
| rescaleConstant = self._forest.find('Targets/Target').get('rescaleConstant') | ||
| self.out_transform = "1 / (1 + exp(-{}))".format("({0} + " + "{})".format(rescaleConstant)) | ||
| self.out_transform = "1 / (1 + exp(-{}))".format( | ||
| "({0} + " + f"{rescaleConstant})" | ||
| ) |
There was a problem hiding this comment.
Function PmmlParser.__init__ refactored with the following changes:
- Replace call to format with f-string (
use-fstring-for-formatting)
|
|
||
| self._dump = booster.get_dump(dump_format='json') | ||
| self._objective = objective | ||
| self._features = booster.feature_names | ||
|
|
||
| if objective == 'binary:logistic': | ||
| self.out_transform = "1 / (1 + exp(-{0}))" | ||
| elif objective == 'reg:linear': | ||
| pass | ||
| else: | ||
| raise Exception("Unsupported objective: %s. Supported objectives are: binary:logistic and reg:linear." % objective) | ||
| elif objective != 'reg:linear': | ||
| raise Exception( | ||
| f"Unsupported objective: {objective}. Supported objectives are: binary:logistic and reg:linear." | ||
| ) | ||
|
|
There was a problem hiding this comment.
Function XgbParser.__init__ refactored with the following changes:
- Remove empty elif clause (
remove-pass-elif) - Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring)
Thanks for starring sourcery-ai/sourcery ✨ 🌟 ✨
Here's your pull request refactoring your most popular Python repo.
If you want Sourcery to refactor all your Python repos and incoming pull requests install our bot.
Review changes via command line
To manually merge these changes, make sure you're on the
masterbranch, then run: