1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
| import matplotlib
def value_compliance_check(name, value):
"""
Return a boolean for the compliance of "value" to the specified rule "name"
"""
def ymin(value):
""" ymin have to be an integer"""
try:
value = int(value)
return True
except:
print(f'Sorry, {value} is not an integer.')
return False
def ymax(value):
""" ymax have to be an integer"""
try:
value = int(value)
return True
except:
print(f'Sorry, {value} is not an integer.')
return False
def color(value):
""" color have to be a Matplotlib compatible value"""
available_colors = []
colors = dict(matplotlib.colors.BASE_COLORS, **matplotlib.colors.CSS4_COLORS)
for color_name, color_value in colors.items():
available_colors.append(color_name)
available_colors.append(color_value)
if value in available_colors:
return True
else:
print(f'Sorry, {value} is not an available color.')
return False
def marker(value):
""" marker have to be a Matplotlib compatible value"""
marker_list = ['.', ',', 'o', 'v', '^', '<', '>',
'1', '2', '3', '4', 's', 'p', '*', 'h', 'H', '+',
'x', 'D', 'd', '|', '_']
if value in marker_list:
return True
else:
print(f'Sorry, {value} is not an available marker.')
return False
def line_style(value):
""" line_style have to be a Matplotlib compatible value"""
line_style_list = ['-', '--', '-.', ':']
if value in line_style_list:
return True
else:
print(f'Sorry, {value} is not an available line style.')
return False
def line_size(value):
""" line_size have to be a float"""
try:
value = float(value)
return True
except:
print(f'Sorry, {value} is not a float.')
return False
def marker_size(value):
""" marker_size have to be a float"""
try:
value = float(value)
return True
except:
print(f'Sorry, {value} is not a float.')
return False
def graph_number(value):
""" graph_number have to be an integer"""
try:
value = int(value)
return True
except:
print(f'Sorry, {value} is not an integer.')
return False
rules_dictionnary = {
'ymin':ymin,
'ymax':ymax,
'color':color,
'marker':marker,
'line_style':line_style,
'line_size':line_size,
'marker_size':marker_size,
'graph_number':graph_number
}
return rules_dictionnary.get(name)(value)
# --- testing ---
print(value_compliance_check('ymin', '10'))
print(value_compliance_check('ymin', '10f'))
print(value_compliance_check('ymax', '10'))
print(value_compliance_check('ymax', '-10'))
print(value_compliance_check('ymax', '10f'))
print(value_compliance_check('color', 'r'))
print(value_compliance_check('color', 'red'))
print(value_compliance_check('color', '#EE82EE'))
print(value_compliance_check('color', (1,1,1)))
print(value_compliance_check('color', '100'))
print(value_compliance_check('line_style', '-'))
print(value_compliance_check('line_style', '-.'))
print(value_compliance_check('line_style', '...'))
print(value_compliance_check('marker', 'o'))
print(value_compliance_check('marker', 'v'))
print(value_compliance_check('marker', '|'))
print(value_compliance_check('marker', 'X'))
print(value_compliance_check('line_size', '1.5'))
print(value_compliance_check('line_size', '1,5'))
print(value_compliance_check('marker_size', '1.2'))
print(value_compliance_check('marker_size', '1,2'))
print(value_compliance_check('graph_number', '-1')) |
Partager