From 58429c2ad68f96e1911aa12c6ad0057f4ce59e76 Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Mon, 2 Nov 2020 11:33:31 +0000 Subject: [PATCH 1/3] refactor the rest --- pandas/_config/config.py | 10 +++------- pandas/_config/localization.py | 3 +-- pandas/_testing.py | 18 ++++++++--------- pandas/_version.py | 36 ++++++++++++++-------------------- pandas/compat/__init__.py | 2 +- pandas/conftest.py | 3 +-- 6 files changed, 30 insertions(+), 42 deletions(-) diff --git a/pandas/_config/config.py b/pandas/_config/config.py index 0b802f2cc9e69..850394baba240 100644 --- a/pandas/_config/config.py +++ b/pandas/_config/config.py @@ -392,7 +392,7 @@ class option_context(ContextDecorator): """ def __init__(self, *args): - if not (len(args) % 2 == 0 and len(args) >= 2): + if len(args) % 2 != 0 or len(args) < 2: raise ValueError( "Need to invoke as option_context(pat, val, [(pat, val), ...])." ) @@ -648,7 +648,7 @@ def _build_option_description(k: str) -> str: s += f"\n [default: {o.defval}] [currently: {_get_option(k, True)}]" if d: - rkey = d.rkey if d.rkey else "" + rkey = d.rkey or "" s += "\n (Deprecated" s += f", use `{rkey}` instead." s += ")" @@ -827,13 +827,9 @@ def is_nonnegative_int(value: Optional[int]) -> None: ValueError When the value is not None or is a negative integer """ - if value is None: + if isinstance(value, int) and value >= 0 or value is None: return - elif isinstance(value, int): - if value >= 0: - return - msg = "Value must be a nonnegative integer or None" raise ValueError(msg) diff --git a/pandas/_config/localization.py b/pandas/_config/localization.py index 3933c8f3d519c..bc76aca93da2a 100644 --- a/pandas/_config/localization.py +++ b/pandas/_config/localization.py @@ -99,8 +99,7 @@ def _valid_locales(locales, normalize): def _default_locale_getter(): - raw_locales = subprocess.check_output(["locale -a"], shell=True) - return raw_locales + return subprocess.check_output(["locale -a"], shell=True) def get_locales(prefix=None, normalize=True, locale_getter=_default_locale_getter): diff --git a/pandas/_testing.py b/pandas/_testing.py index a4fdb390abf42..80dc7ce153226 100644 --- a/pandas/_testing.py +++ b/pandas/_testing.py @@ -729,8 +729,7 @@ def _get_ilevel_values(index, level): unique = index.levels[level] level_codes = index.codes[level] filled = take_1d(unique._values, level_codes, fill_value=unique._na_value) - values = unique._shallow_copy(filled, name=index.names[level]) - return values + return unique._shallow_copy(filled, name=index.names[level]) if check_less_precise is not no_default: warnings.warn( @@ -1871,8 +1870,7 @@ def makeTimedeltaIndex(k=10, freq="D", name=None, **kwargs): def makePeriodIndex(k=10, name=None, **kwargs): dt = datetime(2000, 1, 1) - dr = pd.period_range(start=dt, periods=k, freq="B", name=name, **kwargs) - return dr + return pd.period_range(start=dt, periods=k, freq="B", name=name, **kwargs) def makeMultiIndex(k=10, names=None, **kwargs): @@ -2511,9 +2509,12 @@ def network( @wraps(t) def wrapper(*args, **kwargs): - if check_before_test and not raise_on_error: - if not can_connect(url, error_classes): - skip() + if ( + check_before_test + and not raise_on_error + and not can_connect(url, error_classes) + ): + skip() try: return t(*args, **kwargs) except Exception as err: @@ -2928,8 +2929,7 @@ def convert_rows_list_to_csv_str(rows_list: List[str]): Expected output of to_csv() in current OS. """ sep = os.linesep - expected = sep.join(rows_list) + sep - return expected + return sep.join(rows_list) + sep def external_error_raised(expected_exception: Type[Exception]) -> ContextManager: diff --git a/pandas/_version.py b/pandas/_version.py index b3fa8530d09eb..5882247b63d73 100644 --- a/pandas/_version.py +++ b/pandas/_version.py @@ -22,8 +22,7 @@ def get_keywords(): # get_keywords(). git_refnames = "$Format:%d$" git_full = "$Format:%H$" - keywords = {"refnames": git_refnames, "full": git_full} - return keywords + return {"refnames": git_refnames, "full": git_full} class VersioneerConfig: @@ -121,17 +120,16 @@ def git_get_keywords(versionfile_abs): # _version.py. keywords = {} try: - f = open(versionfile_abs) - for line in f.readlines(): - if line.strip().startswith("git_refnames ="): - mo = re.search(r'=\s*"(.*)"', line) - if mo: - keywords["refnames"] = mo.group(1) - if line.strip().startswith("git_full ="): - mo = re.search(r'=\s*"(.*)"', line) - if mo: - keywords["full"] = mo.group(1) - f.close() + with open(versionfile_abs) as f: + for line in f.readlines(): + if line.strip().startswith("git_refnames ="): + mo = re.search(r'=\s*"(.*)"', line) + if mo: + keywords["refnames"] = mo.group(1) + if line.strip().startswith("git_full ="): + mo = re.search(r'=\s*"(.*)"', line) + if mo: + keywords["full"] = mo.group(1) except OSError: pass return keywords @@ -286,13 +284,11 @@ def render_pep440(pieces): if pieces["distance"] or pieces["dirty"]: rendered += plus_or_dot(pieces) rendered += f"{pieces['distance']:d}.g{pieces['short']}" - if pieces["dirty"]: - rendered += ".dirty" else: # exception #1 rendered = f"0+untagged.{pieces['distance']:d}.g{pieces['short']}" - if pieces["dirty"]: - rendered += ".dirty" + if pieces["dirty"]: + rendered += ".dirty" return rendered @@ -348,13 +344,11 @@ def render_pep440_old(pieces): rendered = pieces["closest-tag"] if pieces["distance"] or pieces["dirty"]: rendered += f".post{pieces['distance']:d}" - if pieces["dirty"]: - rendered += ".dev0" else: # exception #1 rendered = f"0.post{pieces['distance']:d}" - if pieces["dirty"]: - rendered += ".dev0" + if pieces["dirty"]: + rendered += ".dev0" return rendered diff --git a/pandas/compat/__init__.py b/pandas/compat/__init__.py index 57e378758cc78..2ac9b9e2c875c 100644 --- a/pandas/compat/__init__.py +++ b/pandas/compat/__init__.py @@ -50,7 +50,7 @@ def is_platform_windows() -> bool: bool True if the running platform is windows. """ - return sys.platform == "win32" or sys.platform == "cygwin" + return sys.platform in ["win32", "cygwin"] def is_platform_linux() -> bool: diff --git a/pandas/conftest.py b/pandas/conftest.py index 515d20e8c5781..207be8a86bb8b 100644 --- a/pandas/conftest.py +++ b/pandas/conftest.py @@ -386,13 +386,12 @@ def _create_multiindex(): major_codes = np.array([0, 0, 1, 2, 3, 3]) minor_codes = np.array([0, 1, 0, 1, 0, 1]) index_names = ["first", "second"] - mi = MultiIndex( + return MultiIndex( levels=[major_axis, minor_axis], codes=[major_codes, minor_codes], names=index_names, verify_integrity=False, ) - return mi def _create_mi_with_dt64tz_level(): From a4d04ed832be236790a20830434f1daa0fef3f37 Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Mon, 2 Nov 2020 14:51:45 +0000 Subject: [PATCH 2/3] revert one change --- pandas/_config/config.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pandas/_config/config.py b/pandas/_config/config.py index 850394baba240..512b638fc4877 100644 --- a/pandas/_config/config.py +++ b/pandas/_config/config.py @@ -827,9 +827,13 @@ def is_nonnegative_int(value: Optional[int]) -> None: ValueError When the value is not None or is a negative integer """ - if isinstance(value, int) and value >= 0 or value is None: + if value is None: return + elif isinstance(value, int): + if value >= 0: + return + msg = "Value must be a nonnegative integer or None" raise ValueError(msg) From ff934afda24e29f4043e07f5a30d5445a1b84d79 Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Mon, 2 Nov 2020 14:53:09 +0000 Subject: [PATCH 3/3] f -> fd --- pandas/_version.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/_version.py b/pandas/_version.py index 5882247b63d73..d2df063ff3acf 100644 --- a/pandas/_version.py +++ b/pandas/_version.py @@ -120,8 +120,8 @@ def git_get_keywords(versionfile_abs): # _version.py. keywords = {} try: - with open(versionfile_abs) as f: - for line in f.readlines(): + with open(versionfile_abs) as fd: + for line in fd.readlines(): if line.strip().startswith("git_refnames ="): mo = re.search(r'=\s*"(.*)"', line) if mo: