Skip to content

Commit

Permalink
Pylint score 10.
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotgao2 committed Mar 29, 2017
1 parent 76c1d1e commit 047e6bb
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 32 deletions.
1 change: 1 addition & 0 deletions examples/custom_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class UserSchema(Schema):


schema = UserSchema({'username': '1234567890'})
print(schema.checkers)
if schema.valid:
print(schema.validated_data)
else:
Expand Down
3 changes: 3 additions & 0 deletions xdata/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""
XData is written for validating data.
"""
5 changes: 5 additions & 0 deletions xdata/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
"""
Exceptions of xdata is defined here
"""


class XDataException(Exception):
"""Exception"""

Expand Down
43 changes: 31 additions & 12 deletions xdata/schema.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
"""
Custom Schema class should be a subclass of Schema
"""

from xdata.exceptions import CheckError
from xdata.types import DataType


class SchemaMeta(type):
"""
SchemaMeta is for init the checkers of a custom Schema
"""

def __new__(mcs, name, bases, attrs):

checkers = {}

for k, v in attrs.items():
if isinstance(v, DataType):
checkers[k] = v
v.name = k
for k, checker in attrs.items():
if isinstance(checker, DataType):
checkers[k] = checker
checker.name = k

for k in checkers:
attrs.pop(k)
Expand All @@ -21,25 +29,32 @@ def __new__(mcs, name, bases, attrs):


class Schema(metaclass=SchemaMeta):
"""
Schema is the base class of custom Schema
"""

def __init__(self, data):
self.data = data
self._validated_data = {}
self._errors = {}
self._checked = False
self.valid = True
self.validate()
self.checkers = self.checkers

def validate(self):

for k, v in self.checkers.items():
"""
Validate all data.
"""
for k, checker in self.checkers.items():

if k in self.data:
self.checkers[k].value = self.data[k]
checker.value = self.data[k]

for k, checker in self.checkers.items():
result = checker.check()
if result is None:
self._validated_data[k] = self.checkers[k].value
result = checker.valid()
if result is True:
self._validated_data[k] = checker.value
else:
self._errors[k] = result

Expand All @@ -52,7 +67,9 @@ def validate(self):

@property
def errors(self):

"""
Return errors as a dict
"""
if not self._checked:
raise CheckError('Data should be validated before visit errors')

Expand All @@ -63,7 +80,9 @@ def errors(self):

@property
def validated_data(self):

"""
Return validated_data as a dict
"""
if not self._checked:
raise CheckError('Data should be validate before visit validated_data')

Expand Down
89 changes: 69 additions & 20 deletions xdata/types.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
"""
All data types
"""
import re
from datetime import datetime


class DataType:
def __init__(self, *args, **kwargs):
"""
All data types is a subclass of DataType
"""

def __init__(self, **kwargs):
self.required = kwargs.get('required', False)
self.default = kwargs.get('default', None)
self.choices = kwargs.get('choices', None)
self.fn = kwargs.get('fn', None)
self.function = kwargs.get('fn', None)
self.value = None
self.name = None

def check(self):
"""
Check value
"""
if self.default is not None and not self.required:
self.value = self.default

Expand All @@ -21,13 +31,26 @@ def check(self):
if self.choices is not None and self.value not in self.choices:
return '{} should be in [{}]'.format(self.name, ','.join(self.choices))

if self.fn is not None and self.value is not None and not self.fn(self.value):
return '{} should be satisfied function {}'.format(self.name, self.fn)
if self.function is not None and self.value is not None and not self.function(self.value):
return '{} should be satisfied function {}'.format(self.name, self.function)

def valid(self):
"""
If value is valid return True
"""
result = self.check()
if result is None:
return True
return result


class Str(DataType):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
"""
String
"""

def __init__(self, **kwargs):
super().__init__(**kwargs)
self.max_length = kwargs.get('max_length', None)
self.min_length = kwargs.get('min_length', None)
self.length = kwargs.get('length', None)
Expand Down Expand Up @@ -55,8 +78,12 @@ def check(self):


class Int(DataType):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
"""
Int
"""

def __init__(self, **kwargs):
super().__init__(**kwargs)
self.max = kwargs.get('max', None)
self.min = kwargs.get('min', None)

Expand All @@ -76,8 +103,12 @@ def check(self):


class Bool(DataType):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
"""
Bool
"""

def __init__(self, **kwargs):
super().__init__(**kwargs)

def check(self):
super().check()
Expand All @@ -86,8 +117,12 @@ def check(self):


class Decimal(DataType):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
"""
Decimal
"""

def __init__(self, **kwargs):
super().__init__(**kwargs)
self.left = kwargs.get('left', 8)
self.right = kwargs.get('right', 2)

Expand All @@ -101,14 +136,20 @@ def check(self):
point_left, point_right = str(self.value).split('.')

if len(point_left) > self.left:
return 'length of the right of the decimal point should be less than {}'.format(self.left)
return 'length of the right of the decimal ' \
'point should be less than {}'.format(self.left)
if not len(point_right) == self.right:
return 'length of the left of the decimal point should be equal to {}'.format(self.right)
return 'length of the left of the decimal ' \
'point should be equal to {}'.format(self.right)


class DateTime(DataType):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
"""
DateTime
"""

def __init__(self, **kwargs):
super().__init__(**kwargs)
self.max_datetime = kwargs.get('max_datetime', None)
self.min_datetime = kwargs.get('min_datetime', None)

Expand All @@ -132,8 +173,12 @@ def check(self):


class Date(DataType):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
"""
Date
"""

def __init__(self, **kwargs):
super().__init__(**kwargs)
self.max_date = kwargs.get('max_date', None)
self.min_date = kwargs.get('min_date', None)

Expand All @@ -157,8 +202,12 @@ def check(self):


class Time(DataType):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
"""
Time
"""

def __init__(self, **kwargs):
super().__init__(**kwargs)
self.max_time = kwargs.get('max_time', None)
self.min_time = kwargs.get('min_time', None)

Expand Down

0 comments on commit 047e6bb

Please sign in to comment.