From ac2a71f61f4a1cdcecfb9e8b9bd56248be6644a2 Mon Sep 17 00:00:00 2001 From: Mike McKerns Date: Mon, 9 Sep 2024 12:18:20 -0400 Subject: [PATCH] type check for Integral, bool by value (#33) --- pygrace/axis.py | 13 +++++++------ pygrace/base.py | 28 ++++++++++++++-------------- pygrace/dataset.py | 19 ++++++++++--------- pygrace/drawing_objects.py | 7 ++++--- pygrace/extensions/multi_plot.py | 8 ++++---- pygrace/graph.py | 9 +++++---- pygrace/project.py | 3 ++- 7 files changed, 46 insertions(+), 41 deletions(-) diff --git a/pygrace/axis.py b/pygrace/axis.py index ba693bb..54f52b3 100644 --- a/pygrace/axis.py +++ b/pygrace/axis.py @@ -10,6 +10,7 @@ from . import base import sys import math +from numbers import Integral __all__ = ['LINEAR_SCALE','LOGARITHMIC_SCALE','AxisBar','AxisLabel', \ 'Tick','TickLabel','Axis'] @@ -110,16 +111,16 @@ def __setattr__(self, key, value): # check type of AxisLabel specific attribute if key == 'major': - self._check_type((float, int), key, value) + self._check_type((float, Integral), key, value) self._check_range(key, value, 0, None, includeMin=False) elif key == 'minor_ticks': - self._check_type(int, key, value) + self._check_type(Integral, key, value) self._check_range(key, value, 0, None) elif key.endswith('grid'): self._check_type(str, key, value) self._check_membership(key, value, ('on', 'off')) elif key == 'default': - self._check_type(int, key, value) + self._check_type(Integral, key, value) elif key == 'place_rounded': self._check_type(str, key, value) self._check_membership(key, value, ('true', 'false')) @@ -255,13 +256,13 @@ def __setattr__(self, key, value): # check type of AxisLabel specific attribute if key == 'angle': - self._check_type((float, int), key, value) + self._check_type((float, Integral), key, value) self._check_range(key, value, 0, 360, includeMax=True) elif key == 'stagger': - self._check_type(int, key, value) + self._check_type(Integral, key, value) self._check_range(key, value, 0, 9) elif key == 'start' or key == 'stop': - self._check_type((float, int), key, value) + self._check_type((float, Integral), key, value) elif key == 'start_type' or key == 'stop_type': self._check_type(str, key, value) self._check_membership(key, value, ('auto', 'spec')) diff --git a/pygrace/base.py b/pygrace/base.py index 76f4518..d0a006f 100644 --- a/pygrace/base.py +++ b/pygrace/base.py @@ -8,6 +8,7 @@ # - https://github.com/uqfoundation/pygrace/blob/master/LICENSE # import sys +from numbers import Integral __all__ = ['NAMED_CHILD_TYPES','DYNAMIC_CHILD_TYPES','GraceObject','BaseSet'] @@ -192,32 +193,32 @@ def __setattr__(self, key, value): # this list of checks on the type and value is not complete (FIX) if key.endswith('linestyle'): - self._check_type((int,), key, value) + self._check_type((Integral,), key, value) self._check_range(key, value, 0, 8) elif key.endswith('linewidth'): - self._check_type((float, int), key, value) + self._check_type((float, Integral), key, value) self._check_range(key, value, 0, None) elif key.endswith('just'): - self._check_type((int,), key, value) + self._check_type((Integral,), key, value) self._check_range(key, value, 0, 15, includeMax=False) elif key.endswith('size'): - self._check_type((float, int), key, value) + self._check_type((float, Integral), key, value) self._check_range(key, value, 0, None) elif key == 'x' or key == 'y': - self._check_type((float, int), key, value) + self._check_type((float, Integral), key, value) elif key == 'xmin' or key == 'xmax' or key == 'ymin' or key == 'ymax': - self._check_type((float, int), key, value) + self._check_type((float, Integral), key, value) elif key == 'onoff': self._check_type((str,), key, value) elif key == 'hidden': self._check_type((str,), key, value) elif key == 'rot': - self._check_type((float, int), key, value) + self._check_type((float, Integral), key, value) elif key == 'length': # legend line length - self._check_type((int,), key, value) + self._check_type((Integral,), key, value) self._check_range(key, value, 0, 8, includeMax=True) elif key.endswith('pattern'): - self._check_type((int,), key, value) + self._check_type((Integral,), key, value) self._check_range(key, value, 0, 32, includeMax=False) elif key.endswith('_tup'): self._check_type(tuple, key, value) @@ -241,7 +242,7 @@ def __setattr__(self, key, value): upperTypes = [t.upper() for t in FORMAT_TYPES] self._check_membership(key, value.upper(), upperTypes) elif key == 'prec': - self._check_type(int, key, value) + self._check_type(Integral, key, value) self._check_range(key, value, 0, 9) elif key == 'append' or key == 'prepend': self._check_type(str, key, value) @@ -612,10 +613,9 @@ def __contains__(self, value): """Returns true if either integer index or string value is in.""" if isinstance(value, str): return value in self.name2item - elif isinstance(value, int): + if isinstance(value, Integral): return value in self.index2item - else: - return False + return False def __str__(self): """Returns the string representation of each item in the set (sorted @@ -634,7 +634,7 @@ def __getitem__(self, value): if value in self: if isinstance(value, str): return '"%s"' % value - elif isinstance(value, int): + if isinstance(value, Integral): return str(value) else: message = str(value) diff --git a/pygrace/dataset.py b/pygrace/dataset.py index dc87518..f19de71 100644 --- a/pygrace/dataset.py +++ b/pygrace/dataset.py @@ -8,6 +8,7 @@ # - https://github.com/uqfoundation/pygrace/blob/master/LICENSE # from .base import GraceObject +from numbers import Integral __all__ = ['SYMBOLS','Symbol','Line','Baseline','Fill','AnnotatedValue', \ 'ErrorBar','DataSet'] @@ -72,12 +73,12 @@ def __setattr__(self, key, value): # check type of Symbol specific attribute if key == 'skip': - self._check_type(int, key, value) + self._check_type(Integral, key, value) elif key == 'char': - self._check_type(int, key, value) + self._check_type(Integral, key, value) self._check_range(key, value, 0, 128, includeMax=False) elif key == 'shape': - self._check_type(int, key, value) + self._check_type(Integral, key, value) self._check_range(key, value, 0, 12, includeMax=False) GraceObject.__setattr__(self, key, value) @@ -113,7 +114,7 @@ def __setattr__(self, key, value): # check Line specific attributes if key == 'type': - self._check_type(int, key, value) + self._check_type(Integral, key, value) self._check_range(key, value, 0, 6, includeMax=False) GraceObject.__setattr__(self, key, value) @@ -140,7 +141,7 @@ def __setattr__(self, key, value): # check BaseLine specific attributes if key == 'type': - self._check_type(int, key, value) + self._check_type(Integral, key, value) self._check_range(key, value, 0, 6, includeMax=False) GraceObject.__setattr__(self, key, value) @@ -166,10 +167,10 @@ def __setattr__(self, key, value): # check Fill specific attributes if key == 'type': - self._check_type(int, key, value) + self._check_type(Integral, key, value) self._check_range(key, value, 0, 2) elif key == 'rule': - self._check_type(int, key, value) + self._check_type(Integral, key, value) self._check_range(key, value, 0, 1) GraceObject.__setattr__(self, key, value) @@ -205,7 +206,7 @@ def __setattr__(self, key, value): # check AnnotatedValue specific attributes if key == 'type': - self._check_type(int, key, value) + self._check_type(Integral, key, value) self._check_range(key, value, 0, 6, includeMax=False) GraceObject.__setattr__(self, key, value) @@ -250,7 +251,7 @@ def __setattr__(self, key, value): self._check_type(str, key, value) self._check_membership(key, value, ('on', 'off')) elif key == 'riser_clip_length': - self._check_type((float, int), key, value) + self._check_type((float, Integral), key, value) self._check_range(key, value, 0, None) GraceObject.__setattr__(self, key, value) diff --git a/pygrace/drawing_objects.py b/pygrace/drawing_objects.py index 8bd19b9..95dd26c 100644 --- a/pygrace/drawing_objects.py +++ b/pygrace/drawing_objects.py @@ -8,6 +8,7 @@ # - https://github.com/uqfoundation/pygrace/blob/master/LICENSE # from .base import GraceObject +from numbers import Integral __all__ = ['DRAWTEXT_JUSTIFICATIONS','DrawingObject','DrawBox','DrawText', \ 'DrawLine','DrawElipse','LabelledPoint','MultiLegend'] @@ -187,13 +188,13 @@ def __setattr__(self, key, value): if key == 'start' or key == 'end': self._check_type(tuple, key, value) elif key == 'arrow': - self._check_type(int, key, value) + self._check_type(Integral, key, value) self._check_range(key, value, 0, 3) elif key == 'arrow_type': - self._check_type(int, key, value) + self._check_type(Integral, key, value) self._check_range(key, value, 0, 2) elif key == 'arrow_length': - self._check_type((float, int), key, value) + self._check_type((float, Integral), key, value) self._check_range(key, value, 0, None) elif key == 'arrow_layout': self._check_type(tuple, key, value) diff --git a/pygrace/extensions/multi_plot.py b/pygrace/extensions/multi_plot.py index 183bd39..1cc7301 100644 --- a/pygrace/extensions/multi_plot.py +++ b/pygrace/extensions/multi_plot.py @@ -93,9 +93,9 @@ def multi(self, rows, cols, hoffset=(0.15,0.05), voffset=(0.05,0.15), # for backward compatibility, allow hoffset and voffset to be # floats, which is interpretted as a symmetric offset - if type(hoffset)==type(0.0): + if isinstance(hoffset, float): hoffset = (hoffset,hoffset) - if type(voffset)==type(0.0): + if isinstance(voffset, float): voffset = (voffset,voffset) self.rows = rows @@ -139,9 +139,9 @@ def automulti(self, maxrows=5, maxcols=7, # for backward compatibility, allow hoffset and voffset to be # floats, which is interpretted as a symmetric offset - if type(hoffset)==type(0.0): + if isinstance(hoffset, float): hoffset = (hoffset,hoffset) - if type(voffset)==type(0.0): + if isinstance(voffset, float): voffset = (voffset,voffset) # set attributes here diff --git a/pygrace/graph.py b/pygrace/graph.py index 951d046..04565e7 100644 --- a/pygrace/graph.py +++ b/pygrace/graph.py @@ -13,6 +13,7 @@ INDEX2LINESTYLES from .axis import Axis,LINEAR_SCALE,LOGARITHMIC_SCALE import math +from numbers import Integral __all__ = ['INDEX_ORIGIN','Subtitle','Title','View','World','Frame', \ 'Legend','Graph'] @@ -92,7 +93,7 @@ def __setattr__(self, key, value): # check type of Frame specific attribute if key == 'znorm': - self._check_type((float, int), key, value) + self._check_type((float, Integral), key, value) elif key == 'stack_world': self._check_type(tuple, key, value) @@ -125,7 +126,7 @@ def __setattr__(self, key, value): # check type of Frame specific attribute if key == 'type': - self._check_type(int, key, value) + self._check_type(Integral, key, value) self._check_range(key, value, 0, 6, includeMax=False) GraceObject.__setattr__(self, key, value) @@ -172,7 +173,7 @@ def __setattr__(self, key, value): self._check_type(str, key, value) self._check_membership(key, value, ('true', 'false')) elif key == 'hgap' or key == 'vgap': - self._check_type((float, int), key, value) + self._check_type((float, Integral), key, value) GraceObject.__setattr__(self, key, value) @@ -231,7 +232,7 @@ def __setattr__(self, key, value): self._check_type(str, key, value) self._check_membership(key, value, ('true', 'false')) elif key == 'bar_hgap': - self._check_type((float, int), key, value) + self._check_type((float, Integral), key, value) GraceObject.__setattr__(self, key, value) diff --git a/pygrace/project.py b/pygrace/project.py index 53b1843..746df09 100644 --- a/pygrace/project.py +++ b/pygrace/project.py @@ -9,6 +9,7 @@ # import time import os +from numbers import Integral from .base import GraceObject from .graph import Graph @@ -52,7 +53,7 @@ def __setattr__(self, key, value): # check Project specific attributes if key == 'width' or key == 'height': - self._check_type(int, key, value) + self._check_type(Integral, key, value) self._check_range(key, value, 0, None) elif key == 'verbose': self._check_type(bool, key, value)