-
Notifications
You must be signed in to change notification settings - Fork 2
/
pyyyc_v15.py
163 lines (127 loc) · 5.05 KB
/
pyyyc_v15.py
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from builtins import super
from six import string_types
from six import with_metaclass
class SpecialProp(object):
""" class SpcecialProp
This class is used for assigning properties. It validates the
property, then sets it to a secret name, defined in a metaclass.
"""
secret_name = None
def __init__(self, doc):
self.doc = doc
def __get__(self, instance, owner):
if self.secret_name is None:
raise ValueError('secret_name not set!')
if not hasattr(instance, self.secret_name):
raise ValueError('{}: property not set'.format(
self.secret_name[1:]
))
return getattr(instance, self.secret_name)
def __set__(self, instance, value):
if self.secret_name is None:
raise ValueError('secret_name not set!')
value = self.confirm(value)
setattr(instance, self.secret_name, value)
def confirm(self, value):
""" This function validates value. It is overwritten by different
subclasses of SpecialProp
"""
return value
class ColorProp(SpecialProp):
def confirm(self, value):
if value == 'red':
value = [255, 0, 0]
if value == 'green':
value = [0, 255, 0]
if value == 'blue':
value = [0, 0, 255]
if not isinstance(value, (list, tuple)) or not len(value) == 3:
raise ValueError('{}: must be rgb color'.format(value))
for v in value:
if not isinstance(v, int):
raise ValueError('{}: rgb must be ints'.format(value))
if not 0 <= v < 256:
raise ValueError('{}: rgb must be 0-255'.format(value))
return value
class FloatProp(SpecialProp):
def confirm(self, value):
if not isinstance(value, float):
raise ValueError('{}: must be float'.format(value))
return value
class IntProp(SpecialProp):
def confirm(self, value):
if not isinstance(value, int):
raise ValueError('{}: must be int'.format(value))
return value
class StrProp(SpecialProp):
def confirm(self, value):
if not isinstance(value, string_types):
raise ValueError('{}: must be string'.format(value))
return value
class SecretNameMeta(type):
""" metaclass SecretNameMeta
This metaclass manipulate its classes by creating a _props
attribute that doesn't exist in the original definition and
setting the secret_name of all the SpecialProp attributes
"""
def __new__(mcs, name, bases, attrs):
_props = []
keys = [k for k in attrs]
for key in keys:
if isinstance(attrs[key], SpecialProp) and key == 'props':
raise ValueError('Cannot have a SpecialProp named \'props\'')
if isinstance(attrs[key], SpecialProp):
_props += [key]
attrs[key].secret_name = '_' + key
attrs['_props'] = _props
if '__doc__' not in attrs:
attrs['__doc__'] = ''
attrs['__doc__'] += '\n\n Properties:\n'
for prop in _props:
attrs['__doc__'] += ' {prop} - {doc}\n'.format(
prop=prop,
doc=attrs[prop].doc
)
return super().__new__(mcs, name, bases, attrs)
class WithSpecialProps(with_metaclass(SecretNameMeta, object)):
""" class WithSpecialProps
This class contains the __init__ function to set SpecialProp
properties through keyword arguments
"""
def __init__(self, **kwargs):
for key in kwargs:
if key[0] == '_':
raise KeyError('Cannot set private property: {}'.format(key))
if key not in self._props:
raise KeyError('Property is unavailable: {}'.format(key))
setattr(self, key, kwargs[key])
class PyYYCPresentation(WithSpecialProps):
""" class PyYYCPresentation
This class contains info about basic presentations at the
PyYYC meetup. It generates some really useful summary info
about the presentation.
"""
presenter = StrProp('Name of the presenter')
topic = StrProp('Brief explanation of the topic')
time_limit = FloatProp('Time limit of the presentation in minutes')
nslides = IntProp('Number of powerpoint slides')
slide_color = ColorProp('RGB color of the slides')
def summarize(self):
"""Print a short description of the presentation. Useful for
press junkets.
"""
print('Pythonista {name} talking about {topic}.'.format(
name=self.presenter,
topic=self.topic
))
def time_per_slide(self):
"""Time available for each slide"""
return self.time_limit / self.nslides
def strains_eyes(self):
"""Determines if the slides will cause eye strain"""
return(any([rgb > 200 for rgb in self.slide_color]) and
any([rgb < 50 for rgb in self.slide_color]))