Popular recipes tagged "formatting"http://code.activestate.com/recipes/tags/formatting/popular/2014-04-16T15:36:04-07: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>
Easy ansi color plus more. (Ruby)
2014-04-16T15:36:04-07:00Mike 'Fuzzy' Partinhttp://code.activestate.com/recipes/users/4179778/http://code.activestate.com/recipes/578018-easy-ansi-color-plus-more/
<p style="color: grey">
Ruby
recipe 578018
by <a href="/recipes/users/4179778/">Mike 'Fuzzy' Partin</a>
(<a href="/recipes/tags/ansi/">ansi</a>, <a href="/recipes/tags/clear/">clear</a>, <a href="/recipes/tags/console/">console</a>, <a href="/recipes/tags/cursor/">cursor</a>, <a href="/recipes/tags/formatting/">formatting</a>, <a href="/recipes/tags/movement/">movement</a>, <a href="/recipes/tags/output/">output</a>, <a href="/recipes/tags/ruby/">ruby</a>, <a href="/recipes/tags/screen/">screen</a>, <a href="/recipes/tags/string/">string</a>, <a href="/recipes/tags/underline/">underline</a>).
</p>
<p>This quick class extends the base String class to add the ability to chain escape codes onto your output. For instance: puts 'String'.bold.underline.blink.red for something truly hideous. Aside from the colors (all are supported, but I haven't put in support for background colors as of the time of this post), cursor placement (ymmv based on the term type), screen clearing, bold, underline, blink reverse, conceal are all handled as well.</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>
string Formatter that renders None values as empty strings (Python)
2010-05-14T18:35:27-07:00Antonio Cunihttp://code.activestate.com/recipes/users/4169762/http://code.activestate.com/recipes/577227-string-formatter-that-renders-none-values-as-empty/
<p style="color: grey">
Python
recipe 577227
by <a href="/recipes/users/4169762/">Antonio Cuni</a>
(<a href="/recipes/tags/formatting/">formatting</a>, <a href="/recipes/tags/none/">none</a>, <a href="/recipes/tags/string/">string</a>).
</p>
<p>This string Formatter works exactly as the default string.Formatter one (i.e., as str.format), with the exception that None values are rendered as empty strings instead of "None". Moreover, any attempt to access attributes or items of None values is rendered as an empty string as well, instead of raising an exception. <br />
E.g. fmt.format('{a}{a.foo}{a[0]}', a=None) == ''</p>
<p>This is useful e.g. when filling a template with values that are fetched from a DB, where usually None means empty.</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>