extensions_framework: lots of docs and code formatting to be more pep8-like
This commit is contained in:
@@ -24,7 +24,7 @@
|
||||
#
|
||||
# ***** END GPL LICENCE BLOCK *****
|
||||
#
|
||||
'''
|
||||
"""
|
||||
Pure logic and validation class.
|
||||
|
||||
By using a Subject object, and a dict of described logic tests, it
|
||||
@@ -45,10 +45,11 @@ class Subject(object):
|
||||
|
||||
Tests are described thus:
|
||||
|
||||
Use the special list types Logic_AND and Logic_OR to describe combinations
|
||||
of values and other members. Use Logic_Operator for numerical comparison.
|
||||
Use the special list types Logic_AND and Logic_OR to describe
|
||||
combinations of values and other members. Use Logic_Operator for
|
||||
numerical comparison.
|
||||
|
||||
# With regards to Subject, each of these evaluate to True:
|
||||
With regards to Subject, each of these evaluate to True:
|
||||
TESTA = {
|
||||
'a': 0,
|
||||
'c': Logic_OR([ 'foo', 'bar' ]),
|
||||
@@ -58,7 +59,7 @@ TESTA = {
|
||||
'g': Logic_OR([ 'baz', Logic_AND([{'b': 1}, {'f': 8}]) ])
|
||||
}
|
||||
|
||||
# With regards to Subject, each of these evaluate to False:
|
||||
With regards to Subject, each of these evaluate to False:
|
||||
TESTB = {
|
||||
'a': 'foo',
|
||||
'c': Logic_OR([ 'bar', 'baz' ]),
|
||||
@@ -68,17 +69,17 @@ TESTB = {
|
||||
'g': Logic_OR([ 'baz', Logic_AND([{'b':0}, {'f': 8}]) ])
|
||||
}
|
||||
|
||||
# With regards to Subject, this test is invalid
|
||||
With regards to Subject, this test is invalid
|
||||
TESTC = {
|
||||
'n': 0
|
||||
}
|
||||
|
||||
# Tests are executed thus:
|
||||
Tests are executed thus:
|
||||
S = Subject()
|
||||
L = Logician(S)
|
||||
L.execute(TESTA)
|
||||
|
||||
'''
|
||||
"""
|
||||
|
||||
class Logic_AND(list):
|
||||
pass
|
||||
@@ -88,31 +89,28 @@ class Logic_Operator(dict):
|
||||
pass
|
||||
|
||||
class Logician(object):
|
||||
'''
|
||||
Given a subject and a dict that describes tests to perform on its members,
|
||||
this class will evaluate True or False results for each member/test pair.
|
||||
|
||||
See the examples below for test syntax.
|
||||
'''
|
||||
"""Given a subject and a dict that describes tests to perform on
|
||||
its members, this class will evaluate True or False results for
|
||||
each member/test pair. See the examples below for test syntax.
|
||||
|
||||
"""
|
||||
|
||||
subject = None
|
||||
def __init__(self, subject):
|
||||
self.subject = subject
|
||||
|
||||
def get_member(self, member_name):
|
||||
'''
|
||||
Get a member value from the subject object.
|
||||
Raise exception is subject is None or member not found.
|
||||
'''
|
||||
"""Get a member value from the subject object. Raise exception
|
||||
if subject is None or member not found.
|
||||
|
||||
"""
|
||||
if self.subject is None:
|
||||
raise Exception('Cannot run tests on a subject which is None')
|
||||
|
||||
return getattr(self.subject, member_name)
|
||||
|
||||
def test_logic(self, member, logic, operator='eq'):
|
||||
'''
|
||||
Find the type of test to run on member, and perform that test
|
||||
'''
|
||||
"""Find the type of test to run on member, and perform that test"""
|
||||
|
||||
if type(logic) is dict:
|
||||
return self.test_dict(member, logic)
|
||||
@@ -123,17 +121,21 @@ class Logician(object):
|
||||
elif type(logic) is Logic_Operator:
|
||||
return self.test_operator(member, logic)
|
||||
else:
|
||||
# compare the value, I think using Logic_Operator() here allows completeness in test_operator(),
|
||||
# but I can't put my finger on why for the minute
|
||||
return self.test_operator(member, Logic_Operator({operator: logic}))
|
||||
# compare the value, I think using Logic_Operator() here
|
||||
# allows completeness in test_operator(), but I can't put
|
||||
# my finger on why for the minute
|
||||
return self.test_operator(member,
|
||||
Logic_Operator({operator: logic}))
|
||||
|
||||
def test_operator(self, member, value):
|
||||
'''
|
||||
execute the operators contained within value and expect that ALL operators are True
|
||||
'''
|
||||
"""Execute the operators contained within value and expect that
|
||||
ALL operators are True
|
||||
|
||||
# something in this method is incomplete, what if operand is a dict, Logic_AND, Logic_OR or another Logic_Operator ?
|
||||
# do those constructs even make any sense ?
|
||||
"""
|
||||
|
||||
# something in this method is incomplete, what if operand is
|
||||
# a dict, Logic_AND, Logic_OR or another Logic_Operator ?
|
||||
# Do those constructs even make any sense ?
|
||||
|
||||
result = True
|
||||
for operator, operand in value.items():
|
||||
@@ -161,9 +163,10 @@ class Logician(object):
|
||||
return result
|
||||
|
||||
def test_or(self, member, logic):
|
||||
'''
|
||||
member is a value, logic is a set of values, ANY of which can be True
|
||||
'''
|
||||
"""Member is a value, logic is a set of values, ANY of which
|
||||
can be True
|
||||
|
||||
"""
|
||||
result = False
|
||||
for test in logic:
|
||||
result |= self.test_logic(member, test)
|
||||
@@ -171,9 +174,10 @@ class Logician(object):
|
||||
return result
|
||||
|
||||
def test_and(self, member, logic):
|
||||
'''
|
||||
member is a value, logic is a list of values, ALL of which must be True
|
||||
'''
|
||||
"""Member is a value, logic is a list of values, ALL of which
|
||||
must be True
|
||||
|
||||
"""
|
||||
result = True
|
||||
for test in logic:
|
||||
result &= self.test_logic(member, test)
|
||||
@@ -181,9 +185,10 @@ class Logician(object):
|
||||
return result
|
||||
|
||||
def test_dict(self, member, logic):
|
||||
'''
|
||||
member is a value, logic is a dict of other members to compare to. All other member tests must be True
|
||||
'''
|
||||
"""Member is a value, logic is a dict of other members to
|
||||
compare to. All other member tests must be True
|
||||
|
||||
"""
|
||||
result = True
|
||||
for other_member, test in logic.items():
|
||||
result &= self.test_logic(self.get_member(other_member), test)
|
||||
@@ -191,11 +196,11 @@ class Logician(object):
|
||||
return result
|
||||
|
||||
def execute(self, test):
|
||||
'''
|
||||
subject is an object,
|
||||
test is a dict of {member: test} pairs to perform on subject's members.
|
||||
each key in test is a member of subject.
|
||||
'''
|
||||
"""Subject is an object, test is a dict of {member: test} pairs
|
||||
to perform on subject's members. Wach key in test is a member
|
||||
of subject.
|
||||
|
||||
"""
|
||||
|
||||
for member_name, logic in test.items():
|
||||
result = self.test_logic(self.get_member(member_name), logic)
|
||||
|
||||
Reference in New Issue
Block a user