diff options
author | Arthur Zamarin <arthurzam@gentoo.org> | 2022-09-26 19:00:17 +0300 |
---|---|---|
committer | Arthur Zamarin <arthurzam@gentoo.org> | 2022-09-26 19:00:17 +0300 |
commit | e855d3e5f8b0a355ccace76bdc18c43e5ed62bf9 (patch) | |
tree | b08c4e55d8df3d2fa5da91a841400f9faf239aad | |
parent | move `DemandLoadTargets` to tests (diff) | |
download | snakeoil-e855d3e5f8b0a355ccace76bdc18c43e5ed62bf9.tar.gz snakeoil-e855d3e5f8b0a355ccace76bdc18c43e5ed62bf9.tar.bz2 snakeoil-e855d3e5f8b0a355ccace76bdc18c43e5ed62bf9.zip |
refactor to use f-strings
Signed-off-by: Arthur Zamarin <arthurzam@gentoo.org>
-rw-r--r-- | src/snakeoil/caching.py | 3 | ||||
-rw-r--r-- | src/snakeoil/compatibility.py | 2 | ||||
-rw-r--r-- | src/snakeoil/data_source.py | 12 | ||||
-rw-r--r-- | src/snakeoil/errors.py | 4 | ||||
-rw-r--r-- | src/snakeoil/fileutils.py | 9 | ||||
-rw-r--r-- | src/snakeoil/formatters.py | 6 | ||||
-rw-r--r-- | src/snakeoil/klass.py | 2 | ||||
-rw-r--r-- | src/snakeoil/mappings.py | 2 | ||||
-rw-r--r-- | src/snakeoil/modules.py | 19 | ||||
-rw-r--r-- | src/snakeoil/stringio.py | 2 |
10 files changed, 25 insertions, 36 deletions
diff --git a/src/snakeoil/caching.py b/src/snakeoil/caching.py index 55e49c87..3ed75d23 100644 --- a/src/snakeoil/caching.py +++ b/src/snakeoil/caching.py @@ -106,8 +106,7 @@ class WeakInstMeta(type): instance = cls.__inst_dict__.get(key) except (NotImplementedError, TypeError) as t: warnings.warn( - "caching keys for %s, got %s for a=%s, kw=%s" % ( - cls, t, a, kw)) + f"caching keys for {cls}, got {t} for a={a}, kw={kw}") del t key = instance = None diff --git a/src/snakeoil/compatibility.py b/src/snakeoil/compatibility.py index bf531719..c9a8a545 100644 --- a/src/snakeoil/compatibility.py +++ b/src/snakeoil/compatibility.py @@ -4,8 +4,6 @@ Compatibility functionality stubs __all__ = ("cmp", "sorted_cmp", "sort_cmp") -import sys - def sorted_key_from_cmp(cmp_func, key_func=None): class _key_proxy: diff --git a/src/snakeoil/data_source.py b/src/snakeoil/data_source.py index 8b9cf72b..1faa5400 100644 --- a/src/snakeoil/data_source.py +++ b/src/snakeoil/data_source.py @@ -258,7 +258,7 @@ class bz2_source(base): 'bzip2', fileutils.readfile_bytes(self.path)).decode() if writable: if not self.mutable: - raise TypeError("data source %s is not mutable" % (self,)) + raise TypeError(f"data source {self} is not mutable") return text_wr_StringIO(self._set_data, data) return text_ro_StringIO(data) @@ -267,7 +267,7 @@ class bz2_source(base): 'bzip2', fileutils.readfile_bytes(self.path)) if writable: if not self.mutable: - raise TypeError("data source %s is not mutable" % (self,)) + raise TypeError(f"data source {self} is not mutable") return bytes_wr_StringIO(self._set_data, data) return bytes_ro_StringIO(data) @@ -317,7 +317,7 @@ class data_source(base): def text_fileobj(self, writable=False): if writable: if not self.mutable: - raise TypeError("data source %s is not mutable" % (self,)) + raise TypeError(f"data source {self} is not mutable") return text_wr_StringIO(self._reset_data, self._convert_data('text')) return text_ro_StringIO(self._convert_data('text')) @@ -334,7 +334,7 @@ class data_source(base): def bytes_fileobj(self, writable=False): if writable: if not self.mutable: - raise TypeError("data source %s is not mutable" % (self,)) + raise TypeError(f"data source {self} is not mutable") return bytes_wr_StringIO(self._reset_data, self._convert_data('bytes')) return bytes_ro_StringIO(self._convert_data('bytes')) @@ -402,13 +402,13 @@ class invokable_data_source(data_source): @klass.steal_docs(data_source) def text_fileobj(self, writable=False): if writable: - raise TypeError("data source %s data is immutable" % (self,)) + raise TypeError(f"data source {self} data is immutable") return self.data(True) @klass.steal_docs(data_source) def bytes_fileobj(self, writable=False): if writable: - raise TypeError("data source %s data is immutable" % (self,)) + raise TypeError(f"data source {self} data is immutable") return self.data(False) @classmethod diff --git a/src/snakeoil/errors.py b/src/snakeoil/errors.py index e9fb9c64..9b7b5411 100644 --- a/src/snakeoil/errors.py +++ b/src/snakeoil/errors.py @@ -34,10 +34,10 @@ def dump_error(raw_exc, msg=None, handle=sys.stderr, tb=None): if raw_exc is not None: for exc in walk_exception_chain(raw_exc): exc_strings.extend( - '%s%s' % (prefix, x.strip()) + prefix + x.strip() for x in (x for x in str(exc).split("\n") if x)) if exc_strings: if msg and tb: - handle.write("\n%s:\n" % raw_exc.__class__.__name__) + handle.write(f"\n{raw_exc.__class__.__name__}:\n") handle.write("\n".join(exc_strings)) handle.write("\n") diff --git a/src/snakeoil/fileutils.py b/src/snakeoil/fileutils.py index cb604802..ed3a0342 100644 --- a/src/snakeoil/fileutils.py +++ b/src/snakeoil/fileutils.py @@ -2,11 +2,6 @@ file related operations, mainly reading """ -__all__ = ("AtomicWriteFile", 'touch') -types = [""] + list("_%s" % x for x in ("ascii", "utf8")) -__all__ += tuple("readfile%s" % x for x in types) + tuple("readlines%s" % x for x in types) -del types - import mmap import os from functools import partial @@ -62,7 +57,7 @@ class AtomicWriteFile_mixin: Upon invocation of the close method, this class will use :py:func:`os.rename` to atomically replace the destination. - Similar to file protocol behaviour, except that close *must* + Similar to file protocol behavior, except that close *must* be called for the changes to be made live, If along the way it's decided that these changes should be discarded, @@ -90,7 +85,7 @@ class AtomicWriteFile_mixin: fp = os.path.realpath(fp) self._original_fp = fp self._temp_fp = os.path.join( - os.path.dirname(fp), ".update.%s" % os.path.basename(fp)) + os.path.dirname(fp), ".update." + os.path.basename(fp)) old_umask = None if perms: # give it just write perms diff --git a/src/snakeoil/formatters.py b/src/snakeoil/formatters.py index 20175b3b..397667fa 100644 --- a/src/snakeoil/formatters.py +++ b/src/snakeoil/formatters.py @@ -379,8 +379,7 @@ else: formatter.stream.write(res) def __setattr__(self, key, val): - raise AttributeError("%s instances are immutable" % - (self.__class__.__name__,)) + raise AttributeError(f"{self.__class__.__name__} instances are immutable") class TerminfoCode: """Encapsulates specific terminfo entry commands, reset for example. @@ -397,8 +396,7 @@ else: object.__setattr__(self, 'value', value) def __setattr__(self, key, value): - raise AttributeError("%s instances are immutable" % - (self.__class__.__name__,)) + raise AttributeError(f"{self.__class__.__name__} instances are immutable") class TerminfoMode(TerminfoCode): diff --git a/src/snakeoil/klass.py b/src/snakeoil/klass.py index 649704ac..0e592588 100644 --- a/src/snakeoil/klass.py +++ b/src/snakeoil/klass.py @@ -618,7 +618,7 @@ def patch(target, external_decorator=None): try: module = getattr(module, comp) except AttributeError: - import_path += ".%s" % comp + import_path += f".{comp}" module = import_module(import_path) return module diff --git a/src/snakeoil/mappings.py b/src/snakeoil/mappings.py index 3eef3a9a..c3498978 100644 --- a/src/snakeoil/mappings.py +++ b/src/snakeoil/mappings.py @@ -747,7 +747,7 @@ class _SlottedDict(DictMixin): this eliminates the allocation of a dict for the instance thus avoiding the wasted memory common to dictionary overallocation- for small mappings that waste is roughly 75%, for 100 item mappings it's roughly 95%, and for 1000 - items it's roughly 84%. Point is, it's sizable, consistantly so. + items it's roughly 84%. Point is, it's sizable, consistently so. The constraint of this is that the resultant mapping has a locked set of keys- you cannot add a key that wasn't allowed up front. diff --git a/src/snakeoil/modules.py b/src/snakeoil/modules.py index ab890f5e..740ea245 100644 --- a/src/snakeoil/modules.py +++ b/src/snakeoil/modules.py @@ -15,8 +15,7 @@ class FailedImport(ImportError): Raised when a requested target cannot be imported """ def __init__(self, trg, e): - ImportError.__init__( - self, "Failed importing target '%s': '%s'" % (trg, e)) + super().__init__(self, f"Failed importing target '{trg}': '{e}'") self.trg, self.e = trg, e @@ -35,8 +34,8 @@ def load_module(name): return import_module(name) except IGNORED_EXCEPTIONS: raise - except Exception as e: - raise FailedImport(name, e) from e + except Exception as exc: + raise FailedImport(name, exc) from exc def load_attribute(name): @@ -54,13 +53,13 @@ def load_attribute(name): raise FailedImport(name, "it isn't an attribute, it's a module") try: return getattr(import_module(chunks[0]), chunks[1]) - except (AttributeError, ImportError) as e: + except (AttributeError, ImportError) as exc: # try to show actual import error if it exists try: import_module(name) - except ImportError as e: - raise FailedImport(name, e) from e - raise FailedImport(name, e) from e + except ImportError as exc: + raise FailedImport(name, exc) from exc + raise FailedImport(name, exc) from exc def load_any(name): @@ -78,5 +77,5 @@ def load_any(name): return import_module(name) except ImportError: return load_attribute(name) - except Exception as e: - raise FailedImport(name, e) from e + except Exception as exc: + raise FailedImport(name, exc) from exc diff --git a/src/snakeoil/stringio.py b/src/snakeoil/stringio.py index c9dca204..c17db923 100644 --- a/src/snakeoil/stringio.py +++ b/src/snakeoil/stringio.py @@ -10,7 +10,7 @@ if at all possible for performance reasons. Thus we have readonly and writable classes; the separation has clear performance benefits. Note that while this functionality is based on StringIO and friends, there is some -differences in behaviour from stdlib- stdlib's ``cStringIO.StringIO`` is +differences in behavior from stdlib- stdlib's ``cStringIO.StringIO`` is pseudo-writable; it has a truncate method (which works). This is suboptimal since the majority of consumers treat it as write only, thus in these classes we specifically raise a TypeError if you try to truncate a readonly instance. Further, instead of |