From 4e4a925f0b63de1a45905447be043ef30b86adcb Mon Sep 17 00:00:00 2001 From: Dominic Kempf Date: Thu, 28 Feb 2019 04:41:28 +0100 Subject: [PATCH 1/3] Cache hash value of ImmutableRecord Due to the data structure to being intended immutable, the hash value cannot change during object lifetime. This commit caches the value upon first evaluation. --- pytools/__init__.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pytools/__init__.py b/pytools/__init__.py index 57e3378..c096e99 100644 --- a/pytools/__init__.py +++ b/pytools/__init__.py @@ -315,12 +315,18 @@ class Record(RecordWithoutPickling): class ImmutableRecordWithoutPickling(RecordWithoutPickling): "Hashable record. Does not explicitly enforce immutability." + def __init__(self, *args, **kwargs): + RecordWithoutPickling.__init__(self, *args, **kwargs) + self._cached_hash = None def __hash__(self): - return hash( + if self._cached_hash is None: + self._cached_hash = hash( (type(self),) + tuple(getattr(self, field) for field in self.__class__.fields)) + return self._cached_hash + class ImmutableRecord(ImmutableRecordWithoutPickling, Record): pass -- GitLab From 9cca2d7475e1fb4b411bf5063f3ea5c491fa48e1 Mon Sep 17 00:00:00 2001 From: Dominic Kempf Date: Thu, 28 Feb 2019 16:43:17 +0100 Subject: [PATCH 2/3] [pylint] fix else-after-raise errors from pylint --- pytools/log.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/pytools/log.py b/pytools/log.py index 8d579bd..104cc82 100644 --- a/pytools/log.py +++ b/pytools/log.py @@ -404,7 +404,8 @@ class LogManager(object): # we've opened an existing database if mode == "w": raise RuntimeError("Log database '%s' already exists" % filename) - elif mode == "wu": + + if mode == "wu": # try again with a new suffix continue @@ -446,9 +447,9 @@ class LogManager(object): if self.old_showwarning is None: raise RuntimeError( "Warnings capture was disabled, but never enabled") - else: - warnings.showwarning = self.old_showwarning - self.old_showwarning = None + + warnings.showwarning = self.old_showwarning + self.old_showwarning = None def _load(self): if self.mpi_comm and self.mpi_comm.rank != self.head_rank: @@ -859,8 +860,8 @@ class LogManager(object): if self.is_parallel: raise ValueError( "must specify explicit aggregator for '%s'" % name) - else: - agg_func = lambda lst: lst[0] + + agg_func = lambda lst: lst[0] elif isinstance(dep, Lookup): assert isinstance(dep.aggregate, Variable) name = dep.aggregate.name -- GitLab From d11b457ea04ed0ce5248162cc7e49214c0f7880e Mon Sep 17 00:00:00 2001 From: Dominic Kempf Date: Thu, 28 Feb 2019 16:56:57 +0100 Subject: [PATCH 3/3] More pylint warnings in lex.py --- pytools/lex.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pytools/lex.py b/pytools/lex.py index 2ad9cee..ff9d8a8 100644 --- a/pytools/lex.py +++ b/pytools/lex.py @@ -148,8 +148,8 @@ class LexIterator(object): def raise_parse_error(self, msg): if self.is_at_end(): raise ParseError(msg, self.raw_string, None) - else: - raise ParseError(msg, self.raw_string, self.lexed[self.index]) + + raise ParseError(msg, self.raw_string, self.lexed[self.index]) def expected(self, what_expected): if self.is_at_end(): -- GitLab