mirror of
https://github.com/kennethreitz/dive-into-python3.git
synced 2026-06-05 23:10:17 +00:00
validation typos
This commit is contained in:
+3
-3
@@ -120,13 +120,13 @@ def approximate_size(size, a_kilobyte_is_1024_bytes=True):
|
||||
Returns: string
|
||||
|
||||
<a> """ <span>③</span></a>
|
||||
if size < 0:
|
||||
if size < 0:
|
||||
<a> raise ValueError('number must be non-negative') <span>④</span></a>
|
||||
|
||||
multiple = 1024 if a_kilobyte_is_1024_bytes else 1000
|
||||
for suffix in SUFFIXES[multiple]:
|
||||
size /= multiple
|
||||
if size < multiple:
|
||||
if size < multiple:
|
||||
<a> return "{0:.1f} {1}".format(size, suffix) <span>⑤</span></a>
|
||||
|
||||
raise ValueError('number too large')</code></pre>
|
||||
@@ -203,7 +203,7 @@ def approximate_size(size, a_kilobyte_is_1024_bytes=True):
|
||||
|
||||
<p>But wait! There's more! Let's take another look at that strange line of code from <code>humansize.py</code>:
|
||||
|
||||
<pre><code>if size < multiple:
|
||||
<pre><code>if size < multiple:
|
||||
return "{0:.1f} {1}".format(size, suffix)</code></pre>
|
||||
|
||||
<p><code>{1}</code> is replaced with the second argument passed to the <code>format()</code> method, which is <var>suffix</var>. But what is <code>{0:.1f}</code>? It's two things: <code>{0}</code>, which you recognize, and <code>:.1f</code>, which you don't. The second half (including and after the colon) defines the <i>format specifier</i>, which further refines how the replaced variable should be formatted.
|
||||
|
||||
Reference in New Issue
Block a user