Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2508647

Browse files
committed
Update logic.py - remove pretty; % => .format
1 parent 8406e04 commit 2508647

File tree

1 file changed

+7
-50
lines changed

1 file changed

+7
-50
lines changed

logic.py

Lines changed: 7 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def make_percept_sentence(self, percept, t):
111111
return Expr("Percept")(percept, t)
112112

113113
def make_action_query(self, t):
114-
return expr("ShouldDo(action, %d)" % t)
114+
return expr("ShouldDo(action, {})".format(t))
115115

116116
def make_action_sentence(self, action, t):
117117
return Expr("Did")(action[expr('action')], t)
@@ -185,11 +185,11 @@ def __repr__(self):
185185
if not self.args: # Constant or proposition with arity 0
186186
return str(self.op)
187187
elif is_symbol(self.op): # Functional or propositional operator
188-
return '%s(%s)' % (self.op, ', '.join(map(repr, self.args)))
188+
return '{}({})'.format(self.op, ', '.join(map(repr, self.args)))
189189
elif len(self.args) == 1: # Prefix operator
190190
return self.op + repr(self.args[0])
191191
else: # Infix operator
192-
return '(%s)' % (' '+self.op+' ').join(map(repr, self.args))
192+
return '({})'.format((' '+self.op+' ').join(map(repr, self.args)))
193193

194194
def __eq__(self, other):
195195
"""x and y are equal iff their ops and args are equal."""
@@ -986,7 +986,7 @@ def standardize_variables(sentence, dic=None):
986986
if sentence in dic:
987987
return dic[sentence]
988988
else:
989-
v = Expr('v_%d' % next(standardize_variables.counter))
989+
v = Expr('v_{}'.format(next(standardize_variables.counter)))
990990
dic[sentence] = v
991991
return v
992992
else:
@@ -1020,7 +1020,7 @@ def tell(self, sentence):
10201020
if is_definite_clause(sentence):
10211021
self.clauses.append(sentence)
10221022
else:
1023-
raise Exception("Not a definite clause: %s" % sentence)
1023+
raise Exception("Not a definite clause: {}".format(sentence))
10241024

10251025
def ask_generator(self, query):
10261026
return fol_bc_ask(self, query)
@@ -1037,7 +1037,7 @@ def test_ask(query, kb=None):
10371037
vars = variables(q)
10381038
answers = fol_bc_ask(kb or test_kb, q)
10391039
return sorted(
1040-
[pretty(dict((x, v) for x, v in list(a.items()) if x in vars))
1040+
[dict((x, v) for x, v in list(a.items()) if x in vars)
10411041
for a in answers], key=repr)
10421042

10431043
test_kb = FolKB(
@@ -1146,7 +1146,7 @@ def diff(y, x):
11461146
elif op == 'log':
11471147
return diff(u, x) / u
11481148
else:
1149-
raise ValueError("Unknown op: %s in diff(%s, %s)" % (op, y, x))
1149+
raise ValueError("Unknown op: {} in diff({}, {})".format(op, y, x))
11501150

11511151

11521152
def simp(x):
@@ -1222,52 +1222,9 @@ def d(y, x):
12221222
# to compensate for the random order in the standard representation
12231223

12241224

1225-
def pretty(x):
1226-
t = type(x)
1227-
if t is dict:
1228-
return pretty_dict(x)
1229-
elif t is set:
1230-
return pretty_set(x)
1231-
else:
1232-
return repr(x)
1233-
1234-
1235-
def pretty_dict(d):
1236-
"""Return dictionary d's repr but with the items sorted.
1237-
>>> pretty_dict({'m': 'M', 'a': 'A', 'r': 'R', 'k': 'K'})
1238-
"{'a': 'A', 'k': 'K', 'm': 'M', 'r': 'R'}"
1239-
>>> pretty_dict({z: C, y: B, x: A})
1240-
'{x: A, y: B, z: C}'
1241-
"""
1242-
return '{%s}' % ', '.join('%r: %r' % (k, v)
1243-
for k, v in sorted(list(d.items()), key=repr))
1244-
1245-
1246-
def pretty_set(s):
1247-
"""Return set s's repr but with the items sorted.
1248-
>>> pretty_set(set(['A', 'Q', 'F', 'K', 'Y', 'B']))
1249-
"set(['A', 'B', 'F', 'K', 'Q', 'Y'])"
1250-
>>> pretty_set(set([z, y, x]))
1251-
'set([x, y, z])'
1252-
"""
1253-
return 'set(%r)' % sorted(s, key=repr)
1254-
1255-
1256-
def pp(x):
1257-
print(pretty(x))
1258-
1259-
1260-
def ppsubst(s):
1261-
"""Pretty-print substitution s"""
1262-
ppdict(s)
1263-
12641225

1265-
def ppdict(d):
1266-
print(pretty_dict(d))
12671226

12681227

1269-
def ppset(s):
1270-
print(pretty_set(s))
12711228

12721229
# ________________________________________________________________________
12731230

0 commit comments

Comments
 (0)