Skip to content

Instantly share code, notes, and snippets.

@bbelyeu
Last active October 30, 2015 18:01
Show Gist options
  • Save bbelyeu/7e64ea0f2e74d6a3958b to your computer and use it in GitHub Desktop.
Save bbelyeu/7e64ea0f2e74d6a3958b to your computer and use it in GitHub Desktop.
class StringProp(BaseDescriptor):
required = False
def __init__(self, default=None, required=False):
self.default = default
self.data = WeakKeyDictionary()
self.required = bool(required)
def __set__(self, instance, val):
if self.required and not val:
raise ValueError("Invalid value for required property: {0}".format(val))
if not isinstance(val, basestring):
raise ValueError("Invalid string property: {0}".format(val))
self.data[instance] = val
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment