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

Skip to content

Commit 1356ab9

Browse files
sampadsaha5reachtarunhere
authored andcommitted
Modern String Formatting in Code (#292)
* Modern string formatting in csp.py. * Modern string formatting in games.py. * Modern string formatting in learning.py. * Modern string formatting in nlp.py. * Modern string formatting in probability.py. * Modern string formatting in search.py. * Replaced {0\!r} by {} if %s. * Corrected a typo.
1 parent 82d78c6 commit 1356ab9

File tree

6 files changed

+17
-17
lines changed

6 files changed

+17
-17
lines changed

csp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ def __init__(self, value): self.value = value
344344

345345
def __getitem__(self, key): return self.value
346346

347-
def __repr__(self): return '{Any: %r}' % self.value
347+
def __repr__(self): return '{{Any: {0!r}}}'.format(self.value)
348348

349349

350350
def different_values_constraint(A, a, B, b):

games.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ def display(self, state):
203203
print(state)
204204

205205
def __repr__(self):
206-
return '<%s>' % self.__class__.__name__
206+
return '<{}>'.format(self.__class__.__name__)
207207

208208

209209
class Fig52Game(Game):

learning.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ def check_example(self, example):
139139
if self.values:
140140
for a in self.attrs:
141141
if example[a] not in self.values[a]:
142-
raise ValueError('Bad value %s for attribute %s in %s' %
143-
(example[a], self.attrnames[a], example))
142+
raise ValueError('Bad value {} for attribute {} in {}'
143+
.format(example[a], self.attrnames[a], example))
144144

145145
def attrnum(self, attr):
146146
"Returns the number used for attr, which can be a name, or -n .. n-1."
@@ -157,7 +157,7 @@ def sanitize(self, example):
157157
for i, attr_i in enumerate(example)]
158158

159159
def __repr__(self):
160-
return '<DataSet(%s): %d examples, %d attributes>' % (
160+
return '<DataSet({}): {:d} examples, {:d} attributes>'.format(
161161
self.name, len(self.examples), len(self.attrs))
162162

163163
# ______________________________________________________________________________
@@ -317,8 +317,8 @@ def display(self, indent=0):
317317
subtree.display(indent + 1)
318318

319319
def __repr__(self):
320-
return ('DecisionFork(%r, %r, %r)'
321-
% (self.attr, self.attrname, self.branches))
320+
return ('DecisionFork({0!r}, {1!r}, {2!r})'
321+
.format(self.attr, self.attrname, self.branches))
322322

323323

324324
class DecisionLeaf:
@@ -771,9 +771,9 @@ def test(predict, dataset, examples=None, verbose=0):
771771
if output == desired:
772772
right += 1
773773
if verbose >= 2:
774-
print(' OK: got %s for %s' % (desired, example))
774+
print(' OK: got {} for {}'.format(desired, example))
775775
elif verbose:
776-
print('WRONG: got %s, expected %s for %s' % (
776+
print('WRONG: got {}, expected {} for {}'.format(
777777
output, desired, example))
778778
return 1 - (right / len(examples))
779779

nlp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def isa(self, word, cat):
5252
return cat in self.categories[word]
5353

5454
def __repr__(self):
55-
return '<Grammar %s>' % self.name
55+
return '<Grammar {}>'.format(self.name)
5656

5757
E0 = Grammar('E0',
5858
Rules( # Grammar for E_0 [Figure 22.4]
@@ -158,7 +158,7 @@ def add_edge(self, edge):
158158
if edge not in self.chart[end]:
159159
self.chart[end].append(edge)
160160
if self.trace:
161-
print('Chart: added %s' % (edge,))
161+
print('Chart: added {}'.format(edge))
162162
if not expects:
163163
self.extender(edge)
164164
else:

probability.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def show_approx(self, numfmt='%.3g'):
8080
for (v, p) in sorted(self.prob.items())])
8181

8282
def __repr__(self):
83-
return "P(%s)" % self.varname
83+
return "P({})".format(self.varname)
8484

8585

8686
class JointProbDist(ProbDist):
@@ -117,7 +117,7 @@ def values(self, var):
117117
return self.vals[var]
118118

119119
def __repr__(self):
120-
return "P(%s)" % self.variables
120+
return "P({})".format(self.variables)
121121

122122

123123
def event_values(event, variables):
@@ -192,14 +192,14 @@ def variable_node(self, var):
192192
for n in self.nodes:
193193
if n.variable == var:
194194
return n
195-
raise Exception("No such variable: %s" % var)
195+
raise Exception("No such variable: {}".format(var))
196196

197197
def variable_values(self, var):
198198
"Return the domain of var."
199199
return [True, False]
200200

201201
def __repr__(self):
202-
return 'BayesNet(%r)' % self.nodes
202+
return 'BayesNet({0!r})'.format(self.nodes)
203203

204204

205205
class BayesNode:

search.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def __init__(self, state, parent=None, action=None, path_cost=0):
9696
self.depth = parent.depth + 1
9797

9898
def __repr__(self):
99-
return "<Node %s>" % (self.state,)
99+
return "<Node {}>".format(self.state)
100100

101101
def __lt__(self, node):
102102
return self.state < node.state
@@ -1133,7 +1133,7 @@ def __getattr__(self, attr):
11331133
return getattr(self.problem, attr)
11341134

11351135
def __repr__(self):
1136-
return '<%4d/%4d/%4d/%s>' % (self.succs, self.goal_tests,
1136+
return '<{:4d}/{:4d}/{:4d}/{}>'.format(self.succs, self.goal_tests,
11371137
self.states, str(self.found)[:4])
11381138

11391139

0 commit comments

Comments
 (0)