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

Skip to content

Modern String Formatting in Code #292

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion csp.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ def __init__(self, value): self.value = value

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

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


def different_values_constraint(A, a, B, b):
Expand Down
2 changes: 1 addition & 1 deletion games.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def display(self, state):
print(state)

def __repr__(self):
return '<%s>' % self.__class__.__name__
return '<{}>'.format(self.__class__.__name__)


class Fig52Game(Game):
Expand Down
14 changes: 7 additions & 7 deletions learning.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ def check_example(self, example):
if self.values:
for a in self.attrs:
if example[a] not in self.values[a]:
raise ValueError('Bad value %s for attribute %s in %s' %
(example[a], self.attrnames[a], example))
raise ValueError('Bad value {} for attribute {} in {}'
.format(example[a], self.attrnames[a], example))

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

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

# ______________________________________________________________________________
Expand Down Expand Up @@ -317,8 +317,8 @@ def display(self, indent=0):
subtree.display(indent + 1)

def __repr__(self):
return ('DecisionFork(%r, %r, %r)'
% (self.attr, self.attrname, self.branches))
return ('DecisionFork({0!r}, {1!r}, {2!r})'
.format(self.attr, self.attrname, self.branches))


class DecisionLeaf:
Expand Down Expand Up @@ -772,9 +772,9 @@ def test(predict, dataset, examples=None, verbose=0):
if output == desired:
right += 1
if verbose >= 2:
print(' OK: got %s for %s' % (desired, example))
print(' OK: got {} for {}'.format(desired, example))
elif verbose:
print('WRONG: got %s, expected %s for %s' % (
print('WRONG: got {}, expected {} for {}'.format(
output, desired, example))
return 1 - (right / len(examples))

Expand Down
4 changes: 2 additions & 2 deletions nlp.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def isa(self, word, cat):
return cat in self.categories[word]

def __repr__(self):
return '<Grammar %s>' % self.name
return '<Grammar {}>'.format(self.name)

E0 = Grammar('E0',
Rules( # Grammar for E_0 [Figure 22.4]
Expand Down Expand Up @@ -158,7 +158,7 @@ def add_edge(self, edge):
if edge not in self.chart[end]:
self.chart[end].append(edge)
if self.trace:
print('Chart: added %s' % (edge,))
print('Chart: added {}'.format(edge))
if not expects:
self.extender(edge)
else:
Expand Down
8 changes: 4 additions & 4 deletions probability.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def show_approx(self, numfmt='%.3g'):
for (v, p) in sorted(self.prob.items())])

def __repr__(self):
return "P(%s)" % self.varname
return "P({})".format(self.varname)


class JointProbDist(ProbDist):
Expand Down Expand Up @@ -117,7 +117,7 @@ def values(self, var):
return self.vals[var]

def __repr__(self):
return "P(%s)" % self.variables
return "P({})".format(self.variables)


def event_values(event, variables):
Expand Down Expand Up @@ -192,14 +192,14 @@ def variable_node(self, var):
for n in self.nodes:
if n.variable == var:
return n
raise Exception("No such variable: %s" % var)
raise Exception("No such variable: {}".format(var))

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

def __repr__(self):
return 'BayesNet(%r)' % self.nodes
return 'BayesNet({0!r})'.format(self.nodes)


class BayesNode:
Expand Down
4 changes: 2 additions & 2 deletions search.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def __init__(self, state, parent=None, action=None, path_cost=0):
self.depth = parent.depth + 1

def __repr__(self):
return "<Node %s>" % (self.state,)
return "<Node {}>".format(self.state)

def __lt__(self, node):
return self.state < node.state
Expand Down Expand Up @@ -1132,7 +1132,7 @@ def __getattr__(self, attr):
return getattr(self.problem, attr)

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


Expand Down