Popular recipes tagged "formatting" but not "string"http://code.activestate.com/recipes/tags/formatting-string/2012-11-11T17:28:57-08:00ActiveState Code RecipesHuman readable file/memory sizes (Python)
2012-11-05T11:59:20-08:00Tony Fluryhttp://code.activestate.com/recipes/users/4184150/http://code.activestate.com/recipes/578321-human-readable-filememory-sizes/
<p style="color: grey">
Python
recipe 578321
by <a href="/recipes/users/4184150/">Tony Flury</a>
(<a href="/recipes/tags/formatting/">formatting</a>, <a href="/recipes/tags/memory/">memory</a>, <a href="/recipes/tags/size/">size</a>).
Revision 5.
</p>
<p>In writing a application to display the file sizes of set of files, I wanted to provide a human readable size rather then just displaying a byte count (which can get rather big).</p>
<p>I developed this useful short recipe that extends the format specifier mini Language to add the S presentation type - which will intelligently convert the value to be displayed into a known human readable size format - i.e. b, Kb,Mb, Gb etc. It honours the rest of the format specification language (<a href="http://docs.python.org/2/library/string.html#format-string-syntax%29" rel="nofollow">http://docs.python.org/2/library/string.html#format-string-syntax)</a></p>
<p>It uses a factor of 1024 at each stage</p>
Human readable file/memory sizes v2 (Python)
2012-11-11T17:28:57-08:00Tony Fluryhttp://code.activestate.com/recipes/users/4184150/http://code.activestate.com/recipes/578323-human-readable-filememory-sizes-v2/
<p style="color: grey">
Python
recipe 578323
by <a href="/recipes/users/4184150/">Tony Flury</a>
(<a href="/recipes/tags/formatting/">formatting</a>, <a href="/recipes/tags/memory/">memory</a>, <a href="/recipes/tags/size/">size</a>).
</p>
<p>In writing a application to display the file sizes of set of files, I wanted to provide a human readable size rather then just displaying a byte count (which can get rather big).</p>
<p>I developed this useful short recipe that extends the format specifier mini Language to add new presentation type s- which will intelligently convert the value to be displayed into a known human readable size format - i.e. b, Kb,Mb, Gb, B, KB etc. It honours the rest of the format specification language (<a href="http://docs.python.org/2/library/string.html#format-string-syntax%29" rel="nofollow">http://docs.python.org/2/library/string.html#format-string-syntax)</a></p>
<p>It uses a factor of 1024 for IEC and common formats, and factor of 1000 for SI units.</p>
format_iter: easy formatting of arbitrary iterables (Python)
2011-08-16T11:44:59-07:00Nick Coghlanhttp://code.activestate.com/recipes/users/2035254/http://code.activestate.com/recipes/577845-format_iter-easy-formatting-of-arbitrary-iterables/
<p style="color: grey">
Python
recipe 577845
by <a href="/recipes/users/2035254/">Nick Coghlan</a>
(<a href="/recipes/tags/formatting/">formatting</a>, <a href="/recipes/tags/strings/">strings</a>).
</p>
<p>The <code>format_iter</code> recipe defines a simple wrapper around <code>str.join</code> and <code>str.format</code> that makes it easy to format an arbitrary iterable with a specified format string and item separator.</p>
Banner (Python)
2011-01-11T05:16:12-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577537-banner/
<p style="color: grey">
Python
recipe 577537
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/banner/">banner</a>, <a href="/recipes/tags/demo/">demo</a>, <a href="/recipes/tags/formatting/">formatting</a>, <a href="/recipes/tags/printing/">printing</a>).
Revision 2.
</p>
<p>Easily customizable banner program.</p>
Formatting numbers with a state machine (implementation of a regex pattern) (Python)
2011-03-22T03:40:45-07:00James Millshttp://code.activestate.com/recipes/users/4167757/http://code.activestate.com/recipes/577618-formatting-numbers-with-a-state-machine-implementa/
<p style="color: grey">
Python
recipe 577618
by <a href="/recipes/users/4167757/">James Mills</a>
(<a href="/recipes/tags/formatting/">formatting</a>, <a href="/recipes/tags/regular_expressions/">regular_expressions</a>).
</p>
<p>I was once asked to explain how the following regular expression works which formats any integer with commas for every thousand (or group of 3 digits):</p>
<pre class="prettyprint"><code>(\d)(?=(\d{3})+$)
</code></pre>
<p>Example:</p>
<pre class="prettyprint"><code>>>> import re
>>> re.sub("(\d)(?=(\d{3})+$)", "\\1,", "1234")
'1,234'
</code></pre>
<p>So here is an implementation of the above regular expression (as best as I could over a lunch break) that will hopefully highlight
how a regular expression engine and finite automa work.</p>
<p>Comments and feedback welcome!</p>
<p>--JamesMills / prologic</p>
CMS page range validator. (Python)
2009-08-21T11:14:13-07:00Joseph Reaglehttp://code.activestate.com/recipes/users/4171494/http://code.activestate.com/recipes/576889-cms-page-range-validator/
<p style="color: grey">
Python
recipe 576889
by <a href="/recipes/users/4171494/">Joseph Reagle</a>
(<a href="/recipes/tags/formatting/">formatting</a>, <a href="/recipes/tags/page_ranges/">page_ranges</a>, <a href="/recipes/tags/writing/">writing</a>).
</p>
<p>Specification and validator for Chicago Manual of Style page ranges.</p>