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

Skip to content

Commit d17d6b5

Browse files
committed
Cleanup while 1: ... and file iteration.
1 parent cb13a92 commit d17d6b5

File tree

8 files changed

+67
-102
lines changed

8 files changed

+67
-102
lines changed

examples/misc/multiprocess.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def terminate(self):
2828
def poll_draw(self):
2929

3030
def call_back():
31-
while 1:
31+
while True:
3232
if not self.pipe.poll():
3333
break
3434

lib/matplotlib/afm.py

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def _sanity_check(fh):
9191
# do something else with the file.
9292
pos = fh.tell()
9393
try:
94-
line = fh.readline()
94+
line = next(fh)
9595
finally:
9696
fh.seek(pos, 0)
9797

@@ -148,10 +148,7 @@ def _parse_header(fh):
148148
}
149149

150150
d = {}
151-
while 1:
152-
line = fh.readline()
153-
if not line:
154-
break
151+
for line in fh:
155152
line = line.rstrip()
156153
if line.startswith(b'Comment'):
157154
continue
@@ -191,10 +188,7 @@ def _parse_char_metrics(fh):
191188

192189
ascii_d = {}
193190
name_d = {}
194-
while 1:
195-
line = fh.readline()
196-
if not line:
197-
break
191+
for line in fh:
198192
line = line.rstrip().decode('ascii') # Convert from byte-literal
199193
if line.startswith('EndCharMetrics'):
200194
return ascii_d, name_d
@@ -231,20 +225,17 @@ def _parse_kern_pairs(fh):
231225
232226
"""
233227

234-
line = fh.readline()
228+
line = next(fh)
235229
if not line.startswith(b'StartKernPairs'):
236230
raise RuntimeError('Bad start of kern pairs data: %s' % line)
237231

238232
d = {}
239-
while 1:
240-
line = fh.readline()
241-
if not line:
242-
break
233+
for line in fh:
243234
line = line.rstrip()
244235
if len(line) == 0:
245236
continue
246237
if line.startswith(b'EndKernPairs'):
247-
fh.readline() # EndKernData
238+
next(fh) # EndKernData
248239
return d
249240
vals = line.split()
250241
if len(vals) != 4 or vals[0] != b'KPX':
@@ -269,10 +260,7 @@ def _parse_composites(fh):
269260
270261
"""
271262
d = {}
272-
while 1:
273-
line = fh.readline()
274-
if not line:
275-
break
263+
for line in fh:
276264
line = line.rstrip()
277265
if len(line) == 0:
278266
continue
@@ -306,12 +294,9 @@ def _parse_optional(fh):
306294
}
307295

308296
d = {b'StartKernData': {}, b'StartComposites': {}}
309-
while 1:
310-
line = fh.readline()
311-
if not line:
312-
break
297+
for line in fh:
313298
line = line.rstrip()
314-
if len(line) == 0:
299+
if not line:
315300
continue
316301
key = line.split()[0]
317302

lib/matplotlib/axes/_base.py

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -392,25 +392,15 @@ def _plot_args(self, tup, kwargs):
392392
return ret
393393

394394
def _grab_next_args(self, *args, **kwargs):
395-
396-
remaining = args
397-
while 1:
398-
399-
if len(remaining) == 0:
400-
return
401-
if len(remaining) <= 3:
402-
for seg in self._plot_args(remaining, kwargs):
403-
yield seg
395+
while True:
396+
if not args:
404397
return
405-
406-
if is_string_like(remaining[2]):
407-
isplit = 3
408-
else:
409-
isplit = 2
410-
411-
for seg in self._plot_args(remaining[:isplit], kwargs):
398+
this, args = args[:2], args[2:]
399+
if args and is_string_like(args[0]):
400+
this += args[0],
401+
args = args[1:]
402+
for seg in self._plot_args(this, kwargs):
412403
yield seg
413-
remaining = remaining[isplit:]
414404

415405

416406
class _AxesBase(martist.Artist):

lib/matplotlib/backends/backend_ps.py

Lines changed: 41 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1638,55 +1638,48 @@ def pstoeps(tmpfile, bbox=None, rotated=False):
16381638
bbox_info, rotate = None, None
16391639

16401640
epsfile = tmpfile + '.eps'
1641-
with io.open(epsfile, 'wb') as epsh:
1641+
with io.open(epsfile, 'wb') as epsh, io.open(tmpfile, 'rb') as tmph:
16421642
write = epsh.write
1643-
with io.open(tmpfile, 'rb') as tmph:
1644-
line = tmph.readline()
1645-
# Modify the header:
1646-
while line:
1647-
if line.startswith(b'%!PS'):
1648-
write(b"%!PS-Adobe-3.0 EPSF-3.0\n")
1649-
if bbox:
1650-
write(bbox_info.encode('ascii') + b'\n')
1651-
elif line.startswith(b'%%EndComments'):
1652-
write(line)
1653-
write(b'%%BeginProlog\n')
1654-
write(b'save\n')
1655-
write(b'countdictstack\n')
1656-
write(b'mark\n')
1657-
write(b'newpath\n')
1658-
write(b'/showpage {} def\n')
1659-
write(b'/setpagedevice {pop} def\n')
1660-
write(b'%%EndProlog\n')
1661-
write(b'%%Page 1 1\n')
1662-
if rotate:
1663-
write(rotate.encode('ascii') + b'\n')
1664-
break
1665-
elif bbox and (line.startswith(b'%%Bound') \
1666-
or line.startswith(b'%%HiResBound') \
1667-
or line.startswith(b'%%DocumentMedia') \
1668-
or line.startswith(b'%%Pages')):
1669-
pass
1670-
else:
1671-
write(line)
1672-
line = tmph.readline()
1673-
# Now rewrite the rest of the file, and modify the trailer.
1674-
# This is done in a second loop such that the header of the embedded
1675-
# eps file is not modified.
1676-
line = tmph.readline()
1677-
while line:
1678-
if line.startswith(b'%%EOF'):
1679-
write(b'cleartomark\n')
1680-
write(b'countdictstack\n')
1681-
write(b'exch sub { end } repeat\n')
1682-
write(b'restore\n')
1683-
write(b'showpage\n')
1684-
write(b'%%EOF\n')
1685-
elif line.startswith(b'%%PageBoundingBox'):
1686-
pass
1687-
else:
1688-
write(line)
1689-
line = tmph.readline()
1643+
# Modify the header:
1644+
for line in tmph:
1645+
if line.startswith(b'%!PS'):
1646+
write(b"%!PS-Adobe-3.0 EPSF-3.0\n")
1647+
if bbox:
1648+
write(bbox_info.encode('ascii') + b'\n')
1649+
elif line.startswith(b'%%EndComments'):
1650+
write(line)
1651+
write(b'%%BeginProlog\n'
1652+
b'save\n'
1653+
b'countdictstack\n'
1654+
b'mark\n'
1655+
b'newpath\n'
1656+
b'/showpage {} def\n'
1657+
b'/setpagedevice {pop} def\n'
1658+
b'%%EndProlog\n'
1659+
b'%%Page 1 1\n')
1660+
if rotate:
1661+
write(rotate.encode('ascii') + b'\n')
1662+
break
1663+
elif bbox and line.startswith((b'%%Bound', b'%%HiResBound',
1664+
b'%%DocumentMedia', b'%%Pages')):
1665+
pass
1666+
else:
1667+
write(line)
1668+
# Now rewrite the rest of the file, and modify the trailer.
1669+
# This is done in a second loop such that the header of the embedded
1670+
# eps file is not modified.
1671+
for line in tmph:
1672+
if line.startswith(b'%%EOF'):
1673+
write(b'cleartomark\n'
1674+
b'countdictstack\n'
1675+
b'exch sub { end } repeat\n'
1676+
b'restore\n'
1677+
b'showpage\n'
1678+
b'%%EOF\n')
1679+
elif line.startswith(b'%%PageBoundingBox'):
1680+
pass
1681+
else:
1682+
write(line)
16901683

16911684
os.remove(tmpfile)
16921685
shutil.move(epsfile, tmpfile)

lib/matplotlib/backends/tkagg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@ def test(aggimage):
4040
blit(p, aggimage)
4141
c.create_image(aggimage.width,aggimage.height,image=p)
4242
blit(p, aggimage)
43-
while 1: r.update_idletasks()
43+
while True: r.update_idletasks()

lib/matplotlib/bezier.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def find_bezier_t_intersecting_with_closedpath(bezier_point_at_t,
113113
114114
- bezier_point_at_t : a function which returns x, y coordinates at *t*
115115
116-
- inside_closedpath : return True if the point is insed the path
116+
- inside_closedpath : return True if the point is inside the path
117117
118118
"""
119119
# inside_closedpath : function
@@ -124,17 +124,14 @@ def find_bezier_t_intersecting_with_closedpath(bezier_point_at_t,
124124
start_inside = inside_closedpath(start)
125125
end_inside = inside_closedpath(end)
126126

127-
if start_inside == end_inside:
128-
if start != end:
129-
raise NonIntersectingPathException(
130-
"the segment does not seem to intersect with the path"
131-
)
127+
if start_inside == end_inside and start != end:
128+
raise NonIntersectingPathException(
129+
"Both points are on the same side of the closed path")
132130

133-
while 1:
131+
while True:
134132

135133
# return if the distance is smaller than the tolerence
136-
if (start[0] - end[0]) ** 2 + \
137-
(start[1] - end[1]) ** 2 < tolerence ** 2:
134+
if np.hypot(start[0] - end[0], start[1] - end[1]) < tolerence:
138135
return t0, t1
139136

140137
# calculate the middle point

lib/matplotlib/dates.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1150,7 +1150,7 @@ def tick_values(self, vmin, vmax):
11501150
ymax = self.base.ge(vmax.year)
11511151

11521152
ticks = [vmin.replace(year=ymin, **self.replaced)]
1153-
while 1:
1153+
while True:
11541154
dt = ticks[-1]
11551155
if dt.year >= ymax:
11561156
return date2num(ticks)

lib/matplotlib/mlab.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2908,7 +2908,7 @@ def get_converters(reader, comments):
29082908
process_skiprows(reader)
29092909

29102910
if needheader:
2911-
while 1:
2911+
while True:
29122912
# skip past any comments and consume one line of column header
29132913
row = next(reader)
29142914
if (len(row) and comments is not None and

0 commit comments

Comments
 (0)