Skip to content

Commit

Permalink
type check for Integral, bool by value (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmckerns authored Sep 9, 2024
1 parent 52cb40f commit ac2a71f
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 41 deletions.
13 changes: 7 additions & 6 deletions pygrace/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -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'))
Expand Down Expand Up @@ -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'))
Expand Down
28 changes: 14 additions & 14 deletions pygrace/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
19 changes: 10 additions & 9 deletions pygrace/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions pygrace/drawing_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions pygrace/extensions/multi_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions pygrace/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
3 changes: 2 additions & 1 deletion pygrace/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#
import time
import os
from numbers import Integral

from .base import GraceObject
from .graph import Graph
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit ac2a71f

Please sign in to comment.