@@ -111,7 +111,7 @@ def make_percept_sentence(self, percept, t):
111
111
return Expr ("Percept" )(percept , t )
112
112
113
113
def make_action_query (self , t ):
114
- return expr ("ShouldDo(action, %d)" % t )
114
+ return expr ("ShouldDo(action, {})" . format ( t ) )
115
115
116
116
def make_action_sentence (self , action , t ):
117
117
return Expr ("Did" )(action [expr ('action' )], t )
@@ -185,11 +185,11 @@ def __repr__(self):
185
185
if not self .args : # Constant or proposition with arity 0
186
186
return str (self .op )
187
187
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 )))
189
189
elif len (self .args ) == 1 : # Prefix operator
190
190
return self .op + repr (self .args [0 ])
191
191
else : # Infix operator
192
- return '(%s)' % ( ' ' + self .op + ' ' ).join (map (repr , self .args ))
192
+ return '({})' . format (( ' ' + self .op + ' ' ).join (map (repr , self .args ) ))
193
193
194
194
def __eq__ (self , other ):
195
195
"""x and y are equal iff their ops and args are equal."""
@@ -986,7 +986,7 @@ def standardize_variables(sentence, dic=None):
986
986
if sentence in dic :
987
987
return dic [sentence ]
988
988
else :
989
- v = Expr ('v_%d' % next (standardize_variables .counter ))
989
+ v = Expr ('v_{}' . format ( next (standardize_variables .counter ) ))
990
990
dic [sentence ] = v
991
991
return v
992
992
else :
@@ -1020,7 +1020,7 @@ def tell(self, sentence):
1020
1020
if is_definite_clause (sentence ):
1021
1021
self .clauses .append (sentence )
1022
1022
else :
1023
- raise Exception ("Not a definite clause: %s" % sentence )
1023
+ raise Exception ("Not a definite clause: {}" . format ( sentence ) )
1024
1024
1025
1025
def ask_generator (self , query ):
1026
1026
return fol_bc_ask (self , query )
@@ -1037,7 +1037,7 @@ def test_ask(query, kb=None):
1037
1037
vars = variables (q )
1038
1038
answers = fol_bc_ask (kb or test_kb , q )
1039
1039
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 )
1041
1041
for a in answers ], key = repr )
1042
1042
1043
1043
test_kb = FolKB (
@@ -1146,7 +1146,7 @@ def diff(y, x):
1146
1146
elif op == 'log' :
1147
1147
return diff (u , x ) / u
1148
1148
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 ))
1150
1150
1151
1151
1152
1152
def simp (x ):
@@ -1222,52 +1222,9 @@ def d(y, x):
1222
1222
# to compensate for the random order in the standard representation
1223
1223
1224
1224
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
-
1264
1225
1265
- def ppdict (d ):
1266
- print (pretty_dict (d ))
1267
1226
1268
1227
1269
- def ppset (s ):
1270
- print (pretty_set (s ))
1271
1228
1272
1229
# ________________________________________________________________________
1273
1230
0 commit comments