diff --git a/gwsumm/plot/core.py b/gwsumm/plot/core.py index bf377d3d..b7480782 100644 --- a/gwsumm/plot/core.py +++ b/gwsumm/plot/core.py @@ -21,7 +21,6 @@ for GWSumm """ -import hashlib import os.path import re import warnings @@ -338,8 +337,7 @@ def pid(self): filts = "".join(map(str, [ getattr(c, 'filter', getattr(c, 'frequency_response', '')) for c in self.channels])) - h = (chans + filts).encode('utf-8') - self._pid = hashlib.md5(h).hexdigest()[:6] + self._pid = putils.hash(chans + filts) return self.pid @pid.setter diff --git a/gwsumm/plot/range.py b/gwsumm/plot/range.py index 3e61ba26..374d1cd8 100644 --- a/gwsumm/plot/range.py +++ b/gwsumm/plot/range.py @@ -22,7 +22,6 @@ from __future__ import division import re -import hashlib from math import pi from six import string_types @@ -35,6 +34,7 @@ from gwpy.timeseries import TimeSeries from .registry import (get_plot, register_plot) +from .utils import hash from ..data import (get_range_channel, get_range, get_timeseries) from ..segments import get_segments from ..channels import split as split_channels @@ -146,8 +146,7 @@ def pid(self): try: return self._pid except: - chans = "".join(map(str, self.channels+self.flags)) - self._pid = hashlib.md5(chans).hexdigest()[:6] + self._pid = hash("".join(map(str, self.channels+self.flags))) return self.pid @pid.setter diff --git a/gwsumm/plot/segments.py b/gwsumm/plot/segments.py index 79343109..f1f547f6 100644 --- a/gwsumm/plot/segments.py +++ b/gwsumm/plot/segments.py @@ -21,7 +21,6 @@ from __future__ import division -import hashlib import bisect from itertools import (cycle, combinations) from numbers import Number @@ -61,7 +60,7 @@ from .core import (BarPlot, PiePlot, format_label) from .registry import (get_plot, register_plot) from .mixins import SegmentLabelSvgMixin -from .utils import usetex_tex +from .utils import (hash, usetex_tex) __author__ = 'Duncan Macleod ' @@ -191,8 +190,7 @@ def pid(self): try: return self._pid except AttributeError: - self._pid = hashlib.md5( - "".join(map(str, self.flags))).hexdigest()[:6] + self._pid = hash("".join(map(str, self.flags))) return self.pid @pid.setter @@ -406,11 +404,10 @@ def pid(self): try: return self._pid except: - chans = "".join(map(str, self.channels)) - self._pid = hashlib.md5(chans).hexdigest()[:6] + basis = "".join(map(str, self.channels)) if self.pargs.get('bits', None): - self._pid = hashlib.md5( - self._pid + str(self.pargs['bits'])).hexdigest()[:6] + basis += str(self.pargs['bits']) + self._pid = hash(basis) return self.pid def _parse_labels(self, defaults=[]): @@ -838,10 +835,10 @@ def pid(self): except: chans = "".join(map(str, self.channels)) masks = "".join(map(str, self.get_bitmask_channels())) - self._pid = hashlib.md5(chans+masks).hexdigest()[:6] + basis = chans + masks if self.pargs.get('bits', None): - self._pid = hashlib.md5( - self._pid + str(self.pargs['bits'])).hexdigest()[:6] + basis += str(self.pargs["bits"]) + self._pid = hash(basis) return self.pid def draw(self): diff --git a/gwsumm/plot/triggers.py b/gwsumm/plot/triggers.py index a7466395..32041fec 100644 --- a/gwsumm/plot/triggers.py +++ b/gwsumm/plot/triggers.py @@ -21,7 +21,6 @@ from __future__ import division -import hashlib import re from collections import OrderedDict from itertools import cycle @@ -42,7 +41,7 @@ from ..data import (get_channel, get_timeseries, add_timeseries) from ..triggers import (get_triggers, get_time_column) from .registry import (get_plot, register_plot) -from .utils import (get_column_string, usetex_tex) +from .utils import (get_column_string, hash, usetex_tex) __author__ = 'Duncan Macleod ' @@ -80,8 +79,8 @@ def pid(self): getattr(c, 'filter', getattr(c, 'frequency_response', '')) for c in self.channels])) if self.filterstr: - filts = "".join([filts, self.filterstr]) - self._pid = hashlib.md5(chans+filts).hexdigest()[:6] + filts += self.filterstr + self._pid = hash(chans + filts) return self.pid diff --git a/gwsumm/plot/utils.py b/gwsumm/plot/utils.py index 597efc07..af20b280 100644 --- a/gwsumm/plot/utils.py +++ b/gwsumm/plot/utils.py @@ -19,6 +19,7 @@ """Utilies for GWSumm plotting """ +import hashlib import itertools import re @@ -151,3 +152,28 @@ def usetex_tex(text): if rcParams['text.usetex']: return label_to_latex(text) return text + + +def hash(string, num=6): + """Generate an N-character hash string based using string to initialise + + Parameters + ---------- + string : `str` + the initialisation string + + num : `int`, optional + the length of the hash to produce + + Returns + ------- + hash : `str` + the new hash + + Examples + -------- + >>> from gwsumm.plot.utils import hash + >>> print(hash("I love gravitational waves")) + 80c897 + """ + return hashlib.md5(string.encode("utf-8")).hexdigest()[:num]