From e56edf6725affcabcdb7b17d6a44ca1ca8fcbb62 Mon Sep 17 00:00:00 2001 From: Dan Bornside Date: Tue, 16 Apr 2013 17:48:11 -0400 Subject: [PATCH 1/5] Squash obvious pep8 stuff --- python/Rx.py | 631 ++++++++++++++++++++++++++++----------------------- 1 file changed, 353 insertions(+), 278 deletions(-) diff --git a/python/Rx.py b/python/Rx.py index 4c98574..5464d10 100644 --- a/python/Rx.py +++ b/python/Rx.py @@ -1,401 +1,476 @@ - +import collections import re -import types -core_types = [ ] class Error(Exception): - pass + pass + class Util(object): - @staticmethod - def make_range_check(opt): - range = { } - for entry in opt.keys(): - if entry not in ('min', 'max', 'min-ex', 'max-ex'): - raise ValueError("illegal argument to make_range_check") + @staticmethod + def make_range_check(opt): + range = {} + for entry in opt.keys(): + if entry not in ('min', 'max', 'min-ex', 'max-ex'): + raise ValueError("illegal argument to make_range_check") - range[entry] = opt[entry] + range[entry] = opt[entry] - def check_range(value): - if range.get('min' ) != None and value < range['min' ]: return False - if range.get('min-ex') != None and value <= range['min-ex']: return False - if range.get('max-ex') != None and value >= range['max-ex']: return False - if range.get('max' ) != None and value > range['max' ]: return False - return True + def check_range(value): + if range.get('min') is None and value < range['min']: + return False + if range.get('min-ex') is None and value <= range['min-ex']: + return False + if range.get('max-ex') is None and value >= range['max-ex']: + return False + if range.get('max') is None and value > range['max']: + return False + return True + + return check_range - return check_range class Factory(object): - def __init__(self, opt={}): - self.prefix_registry = { - '': 'tag:codesimply.com,2008:rx/core/', - '.meta': 'tag:codesimply.com,2008:rx/meta/', - } + def __init__(self, opt={}): + self.prefix_registry = { + '': 'tag:codesimply.com,2008:rx/core/', + '.meta': 'tag:codesimply.com,2008:rx/meta/', + } + + self.type_registry = {} + if opt.get("register_core_types", False): + for t in core_types: + self.register_type(t) - self.type_registry = {} - if opt.get("register_core_types", False): - for t in core_types: self.register_type(t) + @staticmethod + def _default_prefixes(): + pass - @staticmethod - def _default_prefixes(): pass + def expand_uri(self, type_name): + if re.match('^\w+:', type_name): + return type_name - def expand_uri(self, type_name): - if re.match('^\w+:', type_name): return type_name + m = re.match('^/([-._a-z0-9]*)/([-._a-z0-9]+)$', type_name) - m = re.match('^/([-._a-z0-9]*)/([-._a-z0-9]+)$', type_name) + if not m: + raise ValueError("couldn't understand type name '%s'" % type_name) - if not m: - raise ValueError("couldn't understand type name '%s'" % type_name) + if not self.prefix_registry.get(m.group(1)): + raise ValueError( + "unknown prefix '%s' in type name '%s'" % (m.group(1), type_name) + ) - if not self.prefix_registry.get(m.group(1)): - raise ValueError( - "unknown prefix '%s' in type name '%s'" % (m.group(1), type_name) - ) + return '%s%s' % (self.prefix_registry[m.group(1)], m.group(2)) - return '%s%s' % (self.prefix_registry[ m.group(1) ], m.group(2)) + def add_prefix(self, name, base): + if self.prefix_registry.get(name, None): + raise Error("the prefix '%s' is already registered" % name) - def add_prefix(self, name, base): - if self.prefix_registry.get(name, None): - raise Error("the prefix '%s' is already registered" % name) + self.prefix_registry[name] = base - self.prefix_registry[name] = base; + def register_type(self, t): + t_uri = t.uri() - def register_type(self, t): - t_uri = t.uri() + if self.type_registry.get(t_uri, None): + raise ValueError("type already registered for %s" % t_uri) - if self.type_registry.get(t_uri, None): - raise ValueError("type already registered for %s" % t_uri) + self.type_registry[t_uri] = t - self.type_registry[t_uri] = t + def learn_type(self, uri, schema): + if self.type_registry.get(uri, None): + raise Error("tried to learn type for already-registered uri %s" % uri) - def learn_type(self, uri, schema): - if self.type_registry.get(uri, None): - raise Error("tried to learn type for already-registered uri %s" % uri) + # make sure schema is valid + # should this be in a try/except? + self.make_schema(schema) - # make sure schema is valid - # should this be in a try/except? - self.make_schema(schema) + self.type_registry[uri] = {"schema": schema} - self.type_registry[uri] = { "schema": schema } + def make_schema(self, schema): + if type(schema) in (str, unicode): + schema = {"type": schema} - def make_schema(self, schema): - if type(schema) in (str, unicode): - schema = { "type": schema } + if not type(schema) is dict: + raise Error('invalid schema argument to make_schema') - if not type(schema) is dict: - raise Error('invalid schema argument to make_schema') + uri = self.expand_uri(schema["type"]) - uri = self.expand_uri(schema["type"]) + if not self.type_registry.get(uri): + raise Error("unknown type %s" % uri) - if not self.type_registry.get(uri): raise Error("unknown type %s" % uri) + type_class = self.type_registry[uri] - type_class = self.type_registry[ uri ] + if type(type_class) is dict: + if not set(schema.keys()).issubset(set(['type'])): + raise Error('composed type does not take check arguments') + return self.make_schema(type_class["schema"]) + else: + return type_class(schema, self) - if type(type_class) is dict: - if not set(schema.keys()).issubset(set(['type'])): - raise Error('composed type does not take check arguments'); - return self.make_schema(type_class["schema"]) - else: - return type_class(schema, self) class _CoreType(object): - @classmethod - def uri(self): - return 'tag:codesimply.com,2008:rx/core/' + self.subname() + @classmethod + def uri(self): + return 'tag:codesimply.com,2008:rx/core/' + self.subname() - def __init__(self, schema, rx): - if not set(schema.keys()).issubset(set(['type'])): - raise Error('unknown parameter for //%s' % self.subname()) + def __init__(self, schema, rx): + if not set(schema.keys()).issubset(set(['type'])): + raise Error('unknown parameter for //%s' % self.subname()) + + def check(self, value): + return False - def check(self, value): return False class AllType(_CoreType): - @staticmethod - def subname(): return 'all' + @staticmethod + def subname(): + return 'all' + + def __init__(self, schema, rx): + if not set(schema.keys()).issubset(set(('type', 'of'))): + raise Error('unknown parameter for //all') + + if not(schema.get('of') and len(schema.get('of'))): + raise Error('no alternatives given in //all of') - def __init__(self, schema, rx): - if not set(schema.keys()).issubset(set(('type', 'of'))): - raise Error('unknown parameter for //all') - - if not(schema.get('of') and len(schema.get('of'))): - raise Error('no alternatives given in //all of') + self.alts = [rx.make_schema(s) for s in schema['of']] - self.alts = [ rx.make_schema(s) for s in schema['of'] ] + def check(self, value): + for schema in self.alts: + if (not schema.check(value)): + return False + return True - def check(self, value): - for schema in self.alts: - if (not schema.check(value)): return False - return True class AnyType(_CoreType): - @staticmethod - def subname(): return 'any' + @staticmethod + def subname(): + return 'any' - def __init__(self, schema, rx): - self.alts = None + def __init__(self, schema, rx): + self.alts = None - if not set(schema.keys()).issubset(set(('type', 'of'))): - raise Error('unknown parameter for //any') - - if schema.get('of') != None: - if not schema['of']: raise Error('no alternatives given in //any of') - self.alts = [ rx.make_schema(alt) for alt in schema['of'] ] + if not set(schema.keys()).issubset(set(('type', 'of'))): + raise Error('unknown parameter for //any') - def check(self, value): - if self.alts is None: return True + if schema.get('of') is not None: + if not schema['of']: + raise Error('no alternatives given in //any of') + self.alts = [rx.make_schema(alt) for alt in schema['of']] - for alt in self.alts: - if alt.check(value): return True + def check(self, value): + if self.alts is None: + return True + + for alt in self.alts: + if alt.check(value): + return True + + return False - return False class ArrType(_CoreType): - @staticmethod - def subname(): return 'arr' + @staticmethod + def subname(): + return 'arr' + + def __init__(self, schema, rx): + self.length = None - def __init__(self, schema, rx): - self.length = None + if not set(schema.keys()).issubset(set(('type', 'contents', 'length'))): + raise Error('unknown parameter for //arr') - if not set(schema.keys()).issubset(set(('type', 'contents', 'length'))): - raise Error('unknown parameter for //arr') + if not schema.get('contents'): + raise Error('no contents provided for //arr') - if not schema.get('contents'): - raise Error('no contents provided for //arr') + self.content_schema = rx.make_schema(schema['contents']) - self.content_schema = rx.make_schema(schema['contents']) + if schema.get('length'): + self.length = Util.make_range_check(schema["length"]) - if schema.get('length'): - self.length = Util.make_range_check( schema["length"] ) + def check(self, value): + if not(type(value) in [type([]), type(())]): + return False + if self.length and not self.length(len(value)): + return False - def check(self, value): - if not(type(value) in [ type([]), type(()) ]): return False - if self.length and not self.length(len(value)): return False + for item in value: + if not self.content_schema.check(item): + return False - for item in value: - if not self.content_schema.check(item): return False + return True - return True; class BoolType(_CoreType): - @staticmethod - def subname(): return 'bool' + @staticmethod + def subname(): + return 'bool' + + def check(self, value): + if value is True or value is False: + return True + return False - def check(self, value): - if value is True or value is False: return True - return False class DefType(_CoreType): - @staticmethod - def subname(): return 'def' + @staticmethod + def subname(): + return 'def' + + def check(self, value): + return not(value is None) - def check(self, value): return not(value is None) class FailType(_CoreType): - @staticmethod - def subname(): return 'fail' + @staticmethod + def subname(): + return 'fail' + + def check(self, value): + return False - def check(self, value): return False class IntType(_CoreType): - @staticmethod - def subname(): return 'int' - - def __init__(self, schema, rx): - if not set(schema.keys()).issubset(set(('type', 'range', 'value'))): - raise Error('unknown parameter for //int') - - self.value = None - if schema.has_key('value'): - if not type(schema['value']) in (float, int, long): - raise Error('invalid value parameter for //int') - if schema['value'] % 1 != 0: - raise Error('invalid value parameter for //int') - self.value = schema['value'] - - self.range = None - if schema.has_key('range'): - self.range = Util.make_range_check( schema["range"] ) - - def check(self, value): - if not(type(value) in (float, int, long)): return False - if value % 1 != 0: return False - if self.range and not self.range(value): return False - if (not self.value is None) and value != self.value: return False - return True + @staticmethod + def subname(): + return 'int' + + def __init__(self, schema, rx): + if not set(schema.keys()).issubset(set(('type', 'range', 'value'))): + raise Error('unknown parameter for //int') + + self.value = None + if 'value' in schema: + if not type(schema['value']) in (float, int, long): + raise Error('invalid value parameter for //int') + if schema['value'] % 1 != 0: + raise Error('invalid value parameter for //int') + self.value = schema['value'] + + self.range = None + if 'range' in schema: + self.range = Util.make_range_check(schema["range"]) + + def check(self, value): + if not(type(value) in (float, int, long)): + return False + if value % 1 != 0: + return False + if self.range and not self.range(value): + return False + if (not self.value is None) and value != self.value: + return False + return True + class MapType(_CoreType): - @staticmethod - def subname(): return 'map' + @staticmethod + def subname(): + return 'map' - def __init__(self, schema, rx): - self.allowed = set() + def __init__(self, schema, rx): + self.allowed = set() - if not set(schema.keys()).issubset(set(('type', 'values'))): - raise Error('unknown parameter for //map') + if not set(schema.keys()).issubset(set(('type', 'values'))): + raise Error('unknown parameter for //map') - if not schema.get('values'): - raise Error('no values given for //map') + if not schema.get('values'): + raise Error('no values given for //map') - self.value_schema = rx.make_schema(schema['values']) + self.value_schema = rx.make_schema(schema['values']) - def check(self, value): - if not(type(value) is type({})): return False + def check(self, value): + if not isinstance(value, collections.Mapping): + #if not(type(value) is type({})): + return False - for v in value.values(): - if not self.value_schema.check(v): return False + for v in value.values(): + if not self.value_schema.check(v): + return False + + return True - return True; class NilType(_CoreType): - @staticmethod - def subname(): return 'nil' + @staticmethod + def subname(): + return 'nil' + + def check(self, value): + return value is None - def check(self, value): return value is None class NumType(_CoreType): - @staticmethod - def subname(): return 'num' + @staticmethod + def subname(): + return 'num' - def __init__(self, schema, rx): - if not set(schema.keys()).issubset(set(('type', 'range', 'value'))): - raise Error('unknown parameter for //num') + def __init__(self, schema, rx): + if not set(schema.keys()).issubset(set(('type', 'range', 'value'))): + raise Error('unknown parameter for //num') - self.value = None - if schema.has_key('value'): - if not type(schema['value']) in (float, int, long): - raise Error('invalid value parameter for //num') - self.value = schema['value'] + self.value = None + if 'value' in schema: + if not type(schema['value']) in (float, int, long): + raise Error('invalid value parameter for //num') + self.value = schema['value'] - self.range = None + self.range = None - if schema.get('range'): - self.range = Util.make_range_check( schema["range"] ) + if schema.get('range'): + self.range = Util.make_range_check(schema["range"]) - def check(self, value): - if not(type(value) in (float, int, long)): return False - if self.range and not self.range(value): return False - if (not self.value is None) and value != self.value: return False - return True + def check(self, value): + if not(type(value) in (float, int, long)): + return False + if self.range and not self.range(value): + return False + if (not self.value is None) and value != self.value: + return False + return True -class OneType(_CoreType): - @staticmethod - def subname(): return 'one' - def check(self, value): - if type(value) in (int, float, long, bool, str, unicode): return True +class OneType(_CoreType): + @staticmethod + def subname(): + return 'one' - return False + def check(self, value): + if type(value) in (int, float, long, bool, str, unicode): + return True -class RecType(_CoreType): - @staticmethod - def subname(): return 'rec' + return False - def __init__(self, schema, rx): - if not set(schema.keys()).issubset(set(('type', 'rest', 'required', 'optional'))): - raise Error('unknown parameter for //rec') - self.known = set() - self.rest_schema = None - if schema.get('rest'): self.rest_schema = rx.make_schema(schema['rest']) +class RecType(_CoreType): + @staticmethod + def subname(): + return 'rec' - for which in ('required', 'optional'): - self.__setattr__(which, { }) - for field in schema.get(which, {}).keys(): - if field in self.known: - raise Error('%s appears in both required and optional' % field) + def __init__(self, schema, rx): + if not set(schema.keys()).issubset(set(('type', 'rest', 'required', 'optional'))): + raise Error('unknown parameter for //rec') - self.known.add(field) + self.known = set() + self.rest_schema = None + if schema.get('rest'): + self.rest_schema = rx.make_schema(schema['rest']) - self.__getattribute__(which)[field] = rx.make_schema( - schema[which][field] - ) + for which in ('required', 'optional'): + self.__setattr__(which, {}) + for field in schema.get(which, {}).keys(): + if field in self.known: + raise Error('%s appears in both required and optional' % field) - def check(self, value): - if not(type(value) is type({})): return False + self.known.add(field) - unknown = [ ] - for field in value.keys(): - if not field in self.known: unknown.append(field) + self.__getattribute__(which)[field] = rx.make_schema( + schema[which][field] + ) - if len(unknown) and not self.rest_schema: return False + def check(self, value): + if not isinstance(value, collections.Mapping): + return False - for field in self.required.keys(): - if not value.has_key(field): return False - if not self.required[field].check( value[field] ): return False + unknown = [] + for field in value.keys(): + if not field in self.known: + unknown.append(field) - for field in self.optional.keys(): - if not value.has_key(field): continue - if not self.optional[field].check( value[field] ): return False + if len(unknown) and not self.rest_schema: + return False - if len(unknown): - rest = { } - for field in unknown: rest[field] = value[field] - if not self.rest_schema.check(rest): return False + for field in self.required.keys(): + if field not in value: + return False + if not self.required[field].check(value[field]): + return False - return True; + for field in self.optional.keys(): + if field not in value: + continue + if not self.optional[field].check(value[field]): + return False -class SeqType(_CoreType): - @staticmethod - def subname(): return 'seq' + if len(unknown): + rest = {} + for field in unknown: + rest[field] = value[field] + if not self.rest_schema.check(rest): + return False - def __init__(self, schema, rx): - if not set(schema.keys()).issubset(set(('type', 'contents', 'tail'))): - raise Error('unknown parameter for //seq') + return True - if not schema.get('contents'): - raise Error('no contents provided for //seq') - self.content_schema = [ rx.make_schema(s) for s in schema["contents"] ] +class SeqType(_CoreType): + @staticmethod + def subname(): + return 'seq' - self.tail_schema = None - if (schema.get('tail')): - self.tail_schema = rx.make_schema(schema['tail']) + def __init__(self, schema, rx): + if not set(schema.keys()).issubset(set(('type', 'contents', 'tail'))): + raise Error('unknown parameter for //seq') - def check(self, value): - if not(type(value) in [ type([]), type(()) ]): return False + if not schema.get('contents'): + raise Error('no contents provided for //seq') - if len(value) < len(self.content_schema): - return False + self.content_schema = [rx.make_schema(s) for s in schema["contents"]] - for i in range(0, len(self.content_schema)): - if not self.content_schema[i].check(value[i]): - return False + self.tail_schema = None + if (schema.get('tail')): + self.tail_schema = rx.make_schema(schema['tail']) - if len(value) > len(self.content_schema): - if not self.tail_schema: return False + def check(self, value): + if not(type(value) in [type([]), type(())]): + return False - if not self.tail_schema.check(value[ len(self.content_schema) : ]): - return False + if len(value) < len(self.content_schema): + return False - return True; + for i in range(0, len(self.content_schema)): + if not self.content_schema[i].check(value[i]): + return False -class StrType(_CoreType): - @staticmethod - def subname(): return 'str' + if len(value) > len(self.content_schema): + if not self.tail_schema: + return False - def __init__(self, schema, rx): - if not set(schema.keys()).issubset(set(('type', 'value', 'length'))): - raise Error('unknown parameter for //str') + if not self.tail_schema.check(value[len(self.content_schema):]): + return False - self.value = None - if schema.has_key('value'): - if not type(schema['value']) in (str, unicode): - raise Error('invalid value parameter for //str') - self.value = schema['value'] + return True - self.length = None - if schema.has_key('length'): - self.length = Util.make_range_check( schema["length"] ) - def check(self, value): - if not type(value) in (str, unicode): return False - if (not self.value is None) and value != self.value: return False - if self.length and not self.length(len(value)): return False - return True +class StrType(_CoreType): + @staticmethod + def subname(): + return 'str' + + def __init__(self, schema, rx): + if not set(schema.keys()).issubset(set(('type', 'value', 'length'))): + raise Error('unknown parameter for //str') + + self.value = None + if 'value' in schema: + if not type(schema['value']) in (str, unicode): + raise Error('invalid value parameter for //str') + self.value = schema['value'] + + self.length = None + if 'length' in schema: + self.length = Util.make_range_check(schema["length"]) + + def check(self, value): + if not type(value) in (str, unicode): + return False + if (not self.value is None) and value != self.value: + return False + if self.length and not self.length(len(value)): + return False + return True core_types = [ - AllType, AnyType, ArrType, BoolType, DefType, - FailType, IntType, MapType, NilType, NumType, - OneType, RecType, SeqType, StrType + AllType, AnyType, ArrType, BoolType, DefType, + FailType, IntType, MapType, NilType, NumType, + OneType, RecType, SeqType, StrType ] From d853e3bff43715a607365ae382529a5f983b3386 Mon Sep 17 00:00:00 2001 From: Dan Bornside Date: Tue, 16 Apr 2013 19:05:10 -0400 Subject: [PATCH 2/5] use isinstance() for all type checks. Python types inherit from each other; bool's are numbers and strings are sequences; so they are special cased. --- python/Rx.py | 135 +++++++++++++++++++-------------------------------- 1 file changed, 49 insertions(+), 86 deletions(-) diff --git a/python/Rx.py b/python/Rx.py index 5464d10..ffa7dda 100644 --- a/python/Rx.py +++ b/python/Rx.py @@ -1,3 +1,4 @@ +import numbers import collections import re @@ -6,34 +7,32 @@ class Error(Exception): pass -class Util(object): - @staticmethod - def make_range_check(opt): - range = {} - for entry in opt.keys(): - if entry not in ('min', 'max', 'min-ex', 'max-ex'): - raise ValueError("illegal argument to make_range_check") +def make_range_check(opt): + range = {} + for entry in opt.keys(): + if entry not in ('min', 'max', 'min-ex', 'max-ex'): + raise ValueError("illegal argument to make_range_check") - range[entry] = opt[entry] + range[entry] = opt[entry] - def check_range(value): - if range.get('min') is None and value < range['min']: - return False - if range.get('min-ex') is None and value <= range['min-ex']: - return False - if range.get('max-ex') is None and value >= range['max-ex']: - return False - if range.get('max') is None and value > range['max']: - return False - return True + def check_range(value): + if range.get('min') is not None and value < range['min']: + return False + if range.get('min-ex') is not None and value <= range['min-ex']: + return False + if range.get('max-ex') is not None and value >= range['max-ex']: + return False + if range.get('max') is not None and value > range['max']: + return False + return True - return check_range + return check_range class Factory(object): def __init__(self, opt={}): self.prefix_registry = { - '': 'tag:codesimply.com,2008:rx/core/', + '': 'tag:codesimply.com,2008:rx/core/', '.meta': 'tag:codesimply.com,2008:rx/meta/', } @@ -42,10 +41,6 @@ def __init__(self, opt={}): for t in core_types: self.register_type(t) - @staticmethod - def _default_prefixes(): - pass - def expand_uri(self, type_name): if re.match('^\w+:', type_name): return type_name @@ -87,10 +82,10 @@ def learn_type(self, uri, schema): self.type_registry[uri] = {"schema": schema} def make_schema(self, schema): - if type(schema) in (str, unicode): + if isinstance(schema, (str, unicode)): schema = {"type": schema} - if not type(schema) is dict: + if not isinstance(schema, collections.Mapping): raise Error('invalid schema argument to make_schema') uri = self.expand_uri(schema["type"]) @@ -100,7 +95,7 @@ def make_schema(self, schema): type_class = self.type_registry[uri] - if type(type_class) is dict: + if isinstance(type_class, collections.Mapping): if not set(schema.keys()).issubset(set(['type'])): raise Error('composed type does not take check arguments') return self.make_schema(type_class["schema"]) @@ -111,20 +106,18 @@ def make_schema(self, schema): class _CoreType(object): @classmethod def uri(self): - return 'tag:codesimply.com,2008:rx/core/' + self.subname() + return 'tag:codesimply.com,2008:rx/core/' + self.subname def __init__(self, schema, rx): if not set(schema.keys()).issubset(set(['type'])): - raise Error('unknown parameter for //%s' % self.subname()) + raise Error('unknown parameter for //%s' % self.subname) def check(self, value): return False class AllType(_CoreType): - @staticmethod - def subname(): - return 'all' + subname = 'all' def __init__(self, schema, rx): if not set(schema.keys()).issubset(set(('type', 'of'))): @@ -143,9 +136,7 @@ def check(self, value): class AnyType(_CoreType): - @staticmethod - def subname(): - return 'any' + subname = 'any' def __init__(self, schema, rx): self.alts = None @@ -170,9 +161,7 @@ def check(self, value): class ArrType(_CoreType): - @staticmethod - def subname(): - return 'arr' + subname = 'arr' def __init__(self, schema, rx): self.length = None @@ -186,10 +175,10 @@ def __init__(self, schema, rx): self.content_schema = rx.make_schema(schema['contents']) if schema.get('length'): - self.length = Util.make_range_check(schema["length"]) + self.length = make_range_check(schema["length"]) def check(self, value): - if not(type(value) in [type([]), type(())]): + if not isinstance(value, collections.Sequence) or isinstance(value, basestring): return False if self.length and not self.length(len(value)): return False @@ -202,9 +191,7 @@ def check(self, value): class BoolType(_CoreType): - @staticmethod - def subname(): - return 'bool' + subname = 'bool' def check(self, value): if value is True or value is False: @@ -213,27 +200,21 @@ def check(self, value): class DefType(_CoreType): - @staticmethod - def subname(): - return 'def' + subname = 'def' def check(self, value): return not(value is None) class FailType(_CoreType): - @staticmethod - def subname(): - return 'fail' + subname = 'fail' def check(self, value): return False class IntType(_CoreType): - @staticmethod - def subname(): - return 'int' + subname = 'int' def __init__(self, schema, rx): if not set(schema.keys()).issubset(set(('type', 'range', 'value'))): @@ -241,7 +222,7 @@ def __init__(self, schema, rx): self.value = None if 'value' in schema: - if not type(schema['value']) in (float, int, long): + if not isinstance(schema['value'], numbers.Number): raise Error('invalid value parameter for //int') if schema['value'] % 1 != 0: raise Error('invalid value parameter for //int') @@ -249,10 +230,10 @@ def __init__(self, schema, rx): self.range = None if 'range' in schema: - self.range = Util.make_range_check(schema["range"]) + self.range = make_range_check(schema["range"]) def check(self, value): - if not(type(value) in (float, int, long)): + if not isinstance(value, numbers.Number) or isinstance(value, bool): return False if value % 1 != 0: return False @@ -264,9 +245,7 @@ def check(self, value): class MapType(_CoreType): - @staticmethod - def subname(): - return 'map' + subname = 'map' def __init__(self, schema, rx): self.allowed = set() @@ -281,7 +260,6 @@ def __init__(self, schema, rx): def check(self, value): if not isinstance(value, collections.Mapping): - #if not(type(value) is type({})): return False for v in value.values(): @@ -292,18 +270,14 @@ def check(self, value): class NilType(_CoreType): - @staticmethod - def subname(): - return 'nil' + subname = 'nil' def check(self, value): return value is None class NumType(_CoreType): - @staticmethod - def subname(): - return 'num' + subname = 'num' def __init__(self, schema, rx): if not set(schema.keys()).issubset(set(('type', 'range', 'value'))): @@ -318,10 +292,10 @@ def __init__(self, schema, rx): self.range = None if schema.get('range'): - self.range = Util.make_range_check(schema["range"]) + self.range = make_range_check(schema["range"]) def check(self, value): - if not(type(value) in (float, int, long)): + if not isinstance(value, numbers.Number) or isinstance(value, bool): return False if self.range and not self.range(value): return False @@ -331,21 +305,14 @@ def check(self, value): class OneType(_CoreType): - @staticmethod - def subname(): - return 'one' + subname = 'one' def check(self, value): - if type(value) in (int, float, long, bool, str, unicode): - return True - - return False + return isinstance(value, (numbers.Number, basestring)) class RecType(_CoreType): - @staticmethod - def subname(): - return 'rec' + subname = 'rec' def __init__(self, schema, rx): if not set(schema.keys()).issubset(set(('type', 'rest', 'required', 'optional'))): @@ -403,9 +370,7 @@ def check(self, value): class SeqType(_CoreType): - @staticmethod - def subname(): - return 'seq' + subname = 'seq' def __init__(self, schema, rx): if not set(schema.keys()).issubset(set(('type', 'contents', 'tail'))): @@ -421,7 +386,7 @@ def __init__(self, schema, rx): self.tail_schema = rx.make_schema(schema['tail']) def check(self, value): - if not(type(value) in [type([]), type(())]): + if not isinstance(value, collections.Sequence) or isinstance(value, basestring): return False if len(value) < len(self.content_schema): @@ -442,9 +407,7 @@ def check(self, value): class StrType(_CoreType): - @staticmethod - def subname(): - return 'str' + subname = 'str' def __init__(self, schema, rx): if not set(schema.keys()).issubset(set(('type', 'value', 'length'))): @@ -458,10 +421,10 @@ def __init__(self, schema, rx): self.length = None if 'length' in schema: - self.length = Util.make_range_check(schema["length"]) + self.length = make_range_check(schema["length"]) def check(self, value): - if not type(value) in (str, unicode): + if not isinstance(value, basestring): return False if (not self.value is None) and value != self.value: return False From 47e80f51478f00aa96795035b2fcf0aef2d50215 Mon Sep 17 00:00:00 2001 From: Paul Curry Date: Tue, 16 Apr 2013 19:13:20 -0400 Subject: [PATCH 3/5] Formatted tests to be PEP8 compliant --- python/rx-test.py | 164 +++++++++++++++++++++++++--------------------- 1 file changed, 88 insertions(+), 76 deletions(-) diff --git a/python/rx-test.py b/python/rx-test.py index c4b2dd2..a75f981 100644 --- a/python/rx-test.py +++ b/python/rx-test.py @@ -1,118 +1,130 @@ -from TAP.Simple import * -import Rx + +import collections import json import re +import Rx + +from TAP.Simple import isa_ok +from TAP.Simple import ok +from TAP.Simple import plan + plan(None) -rx = Rx.Factory({ "register_core_types": True }); +rx = Rx.Factory({"register_core_types": True}) isa_ok(rx, Rx.Factory) index = json.loads(file('spec/index.json').read()) -test_data = {} +test_data = {} test_schemata = {} + def normalize(entries, test_data): - if entries == '*': - entries = { "*": None } + if entries == '*': + entries = {"*": None} + + if isinstance(entries, collections.Sequence): + new_entries = {} + for n in entries: + new_entries[n] = None + entries = new_entries - if type(entries) is type([]): - new_entries = { } - for n in entries: new_entries[n] = None - entries = new_entries + if len(entries) == 1 and '*' in entries: + value = entries["*"] + entries = {} + for k in test_data.keys(): + entries[k] = value - if len(entries) == 1 and entries.has_key('*'): - value = entries["*"] - entries = { } - for k in test_data.keys(): entries[k] = value + return entries - return entries for filename in index: - if filename == 'spec/index.json': continue - payload = json.loads(file(filename).read()) + if filename == 'spec/index.json': + continue + payload = json.loads(file(filename).read()) - parts = filename.split('/') - parts.pop(0) + parts = filename.split('/') + parts.pop(0) - leaf_name = '/'.join(parts[1:]) - leaf_name = re.sub('\.json$', '', leaf_name) + leaf_name = '/'.join(parts[1:]) + leaf_name = re.sub('\.json$', '', leaf_name) - filetype = parts.pop(0) + filetype = parts.pop(0) - if filetype == 'schemata': - test_schemata[ leaf_name ] = payload - elif filetype == 'data': - test_data[ leaf_name ] = {} + if filetype == 'schemata': + test_schemata[leaf_name] = payload + elif filetype == 'data': + test_data[leaf_name] = {} - if type(payload) is type([]): - for data_str in payload: - boxed_data = json.loads("[ %s ]" % data_str) - test_data[ leaf_name ][ data_str ] = boxed_data[0] + if isinstance(payload, collections.Sequence): + for data_str in payload: + boxed_data = json.loads("[ %s ]" % data_str) + test_data[leaf_name][data_str] = boxed_data[0] + else: + for entry in payload.keys(): + boxed_data = json.loads("[ %s ]" % payload[entry]) + test_data[leaf_name][entry] = boxed_data[0] else: - for entry in payload.keys(): - boxed_data = json.loads("[ %s ]" % payload[entry]) - test_data[ leaf_name ][ entry ] = boxed_data[0] - else: - raise StandardError("weird file in data dir: %s" % filename) + raise StandardError("weird file in data dir: %s" % filename) schema_names = test_schemata.keys() schema_names.sort() for schema_name in schema_names: - rx = Rx.Factory({ "register_core_types": True }); + rx = Rx.Factory({"register_core_types": True}) - schema_test_spec = test_schemata[ schema_name ] + schema_test_spec = test_schemata[schema_name] - if schema_test_spec.get("composedtype", False): - try: - rx.learn_type(schema_test_spec['composedtype']['uri'], - schema_test_spec['composedtype']['schema']) - except Rx.Error, e: - if schema_test_spec['composedtype'].get("invalid", False): - ok(1, "BAD COMPOSED TYPE: schemata %s" % schema_name) - continue - else: - raise + if schema_test_spec.get("composedtype", False): + try: + rx.learn_type(schema_test_spec['composedtype']['uri'], + schema_test_spec['composedtype']['schema']) + except Rx.Error, e: + if schema_test_spec['composedtype'].get("invalid", False): + ok(1, "BAD COMPOSED TYPE: schemata %s" % schema_name) + continue + else: + raise - if schema_test_spec['composedtype'].get("invalid", False): - ok(0, "BAD COMPOSED TYPE: schemata %s" % schema_name) + if schema_test_spec['composedtype'].get("invalid", False): + ok(0, "BAD COMPOSED TYPE: schemata %s" % schema_name) - if schema_test_spec['composedtype'].get("prefix", False): - rx.add_prefix(schema_test_spec['composedtype']['prefix'][0], - schema_test_spec['composedtype']['prefix'][1]) + if schema_test_spec['composedtype'].get("prefix", False): + rx.add_prefix(schema_test_spec['composedtype']['prefix'][0], + schema_test_spec['composedtype']['prefix'][1]) - try: - schema = rx.make_schema(schema_test_spec["schema"]) - except Rx.Error, e: - if schema_test_spec.get("invalid", False): - ok(1, "BAD SCHEMA: schemata %s" % schema_name) - continue - else: - raise + try: + schema = rx.make_schema(schema_test_spec["schema"]) + except Rx.Error, e: + if schema_test_spec.get("invalid", False): + ok(1, "BAD SCHEMA: schemata %s" % schema_name) + continue + else: + raise - if schema_test_spec.get("invalid", False): - ok(0, "BAD SCHEMA: schemata %s" % schema_name) - continue + if schema_test_spec.get("invalid", False): + ok(0, "BAD SCHEMA: schemata %s" % schema_name) + continue - if not schema: raise StandardError("got no schema obj for valid input") + if not schema: + raise StandardError("got no schema obj for valid input") - for pf in [ 'pass', 'fail' ]: - for source in schema_test_spec.get(pf, []): - to_test = schema_test_spec[pf][ source ] + for pf in ('pass', 'fail'): + for source in schema_test_spec.get(pf, []): + to_test = schema_test_spec[pf][source] - to_test = normalize(to_test, test_data[ source ]) - # if to_test == '*': to_test = test_data[ source ].keys() + to_test = normalize(to_test, test_data[source]) + # if to_test == '*': to_test = test_data[ source ].keys() - for entry in to_test: - result = schema.check( test_data[source][entry] ) + for entry in to_test: + result = schema.check(test_data[source][entry]) - desc = "%s/%s against %s" % (source, entry, schema_name) + desc = "%s/%s against %s" % (source, entry, schema_name) - if pf == 'pass': - ok(result, "VALID : %s" % desc) - else: - ok(not(result), "INVALID: %s" % desc) + if pf == 'pass': + ok(result, "VALID : %s" % desc) + else: + ok(not(result), "INVALID: %s" % desc) From 1858e37256e3dcc36c45a8fa7d26a538a849c9d3 Mon Sep 17 00:00:00 2001 From: Paul Curry Date: Tue, 16 Apr 2013 19:21:01 -0400 Subject: [PATCH 4/5] Added setup.py to make this installable --- python/setup.py | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 python/setup.py diff --git a/python/setup.py b/python/setup.py new file mode 100644 index 0000000..ca6b67a --- /dev/null +++ b/python/setup.py @@ -0,0 +1,8 @@ + +from distutils.core import setup + +setup( + name = "Rx", + version = "0.200003", + py_modules = ["Rx"], +) From 2b71358df736abe4ded83a76c4bad8b21f4a0e87 Mon Sep 17 00:00:00 2001 From: Paul Curry Date: Sun, 28 Apr 2013 23:50:46 -0400 Subject: [PATCH 5/5] More long line breakups --- python/Rx.py | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/python/Rx.py b/python/Rx.py index ffa7dda..00ece33 100644 --- a/python/Rx.py +++ b/python/Rx.py @@ -1,5 +1,5 @@ -import numbers import collections +import numbers import re @@ -52,7 +52,8 @@ def expand_uri(self, type_name): if not self.prefix_registry.get(m.group(1)): raise ValueError( - "unknown prefix '%s' in type name '%s'" % (m.group(1), type_name) + "unknown prefix '%s' in type name '%s'" % (m.group(1), + type_name) ) return '%s%s' % (self.prefix_registry[m.group(1)], m.group(2)) @@ -73,7 +74,8 @@ def register_type(self, t): def learn_type(self, uri, schema): if self.type_registry.get(uri, None): - raise Error("tried to learn type for already-registered uri %s" % uri) + raise Error( + "tried to learn type for already-registered uri %s" % uri) # make sure schema is valid # should this be in a try/except? @@ -166,7 +168,9 @@ class ArrType(_CoreType): def __init__(self, schema, rx): self.length = None - if not set(schema.keys()).issubset(set(('type', 'contents', 'length'))): + if not set(schema.keys()).issubset(set(('type', + 'contents', + 'length'))): raise Error('unknown parameter for //arr') if not schema.get('contents'): @@ -178,7 +182,8 @@ def __init__(self, schema, rx): self.length = make_range_check(schema["length"]) def check(self, value): - if not isinstance(value, collections.Sequence) or isinstance(value, basestring): + if (not isinstance(value, collections.Sequence) + or isinstance(value, basestring)): return False if self.length and not self.length(len(value)): return False @@ -315,7 +320,10 @@ class RecType(_CoreType): subname = 'rec' def __init__(self, schema, rx): - if not set(schema.keys()).issubset(set(('type', 'rest', 'required', 'optional'))): + if not set(schema.keys()).issubset(set(('type', + 'rest', + 'required', + 'optional'))): raise Error('unknown parameter for //rec') self.known = set() @@ -327,7 +335,8 @@ def __init__(self, schema, rx): self.__setattr__(which, {}) for field in schema.get(which, {}).keys(): if field in self.known: - raise Error('%s appears in both required and optional' % field) + raise Error( + '%s appears in both required and optional' % field) self.known.add(field) @@ -386,7 +395,8 @@ def __init__(self, schema, rx): self.tail_schema = rx.make_schema(schema['tail']) def check(self, value): - if not isinstance(value, collections.Sequence) or isinstance(value, basestring): + if (not isinstance(value, collections.Sequence) + or isinstance(value, basestring)): return False if len(value) < len(self.content_schema): @@ -433,7 +443,7 @@ def check(self, value): return True core_types = [ - AllType, AnyType, ArrType, BoolType, DefType, - FailType, IntType, MapType, NilType, NumType, - OneType, RecType, SeqType, StrType + AllType, AnyType, ArrType, BoolType, DefType, + FailType, IntType, MapType, NilType, NumType, + OneType, RecType, SeqType, StrType ]