<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>String Utilities — Stem 1.1.0 documentation</title> <link rel="stylesheet" href="../../_static/haiku.css" type="text/css" /> <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" /> <link rel="stylesheet" href="../../_static/print.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../../', VERSION: '1.1.0', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../../_static/jquery.js"></script> <script type="text/javascript" src="../../_static/underscore.js"></script> <script type="text/javascript" src="../../_static/doctools.js"></script> <script type="text/javascript" src="../../_static/theme_extras.js"></script> <link rel="shortcut icon" href="../../_static/favicon.png"/> <link rel="top" title="Stem 1.1.0 documentation" href="../../index.html" /> <link rel="up" title="Contents" href="../../contents.html" /> <link rel="next" title="System Utilities" href="system.html" /> <link rel="prev" title="Proc Utilities" href="proc.html" /> </head> <body> <div class="header"><img class="rightlogo" src="../../_static/logo.png" alt="Logo"/><h1 class="heading"><a href="../../index.html"> <span>Stem Docs</span></a></h1> <h2 class="heading"><span>String Utilities</span></h2> </div> <div class="topnav"> <p> <ul id="navbar"> <li><a href="../../index.html">Home</a></li> <li><a href="../../tutorials.html">Tutorials</a> <ul> <li><a href="../../tutorials/the_little_relay_that_could.html">Hello World</a></li> <li><a href="../../tutorials/to_russia_with_love.html">Client Usage</a></li> <li><a href="../../tutorials/tortoise_and_the_hare.html">Event Listening</a></li> <li><a href="../../tutorials/mirror_mirror_on_the_wall.html">Tor Descriptors</a></li> <li><a href="../../tutorials/east_of_the_sun.html">Utilities</a></li> <li><a href="../../tutorials/double_double_toil_and_trouble.html">Examples</a></li> </ul> </li> <li><a href="../../api.html">API</a> <ul> <li><a href="../control.html">stem.control</a></li> <li><a href="../connection.html">stem.connection</a></li> <li><a href="../socket.html">stem.socket</a></li> <li><a href="../process.html">stem.process</a></li> <li><a href="../response.html">stem.response</a></li> <li><a href="../exit_policy.html">stem.exit_policy</a></li> <li><a href="../version.html">stem.version</a></li> <li><a href="../../api.html#descriptors">Descriptors</a></li> <li><a href="../../api.html#utilities">Utilities</a></li> </ul> </li> <li><a href="https://trac.torproject.org/projects/tor/wiki/doc/stem">Development</a> <ul> <li><a href="../../faq.html">FAQ</a></li> <li><a href="../../change_log.html">Change Log</a></li> <li><a href="https://trac.torproject.org/projects/tor/wiki/doc/stem/bugs">Bug Tracker</a></li> <li><a href="../../download.html">Download</a></li> </ul> </li> </ul> </p> </div> <div class="content"> <div class="section" id="module-stem.util.str_tools"> <span id="string-utilities"></span><h1>String Utilities<a class="headerlink" href="#module-stem.util.str_tools" title="Permalink to this headline">¶</a></h1> <p>Toolkit for various string activity.</p> <p><strong>Module Overview:</strong></p> <div class="highlight-python"><pre>get_size_label - human readable label for a number of bytes get_time_label - human readable label for a number of seconds get_time_labels - human readable labels for each time unit get_short_time_label - condensed time label output parse_short_time_label - seconds represented by a short time label</pre> </div> <dl class="function"> <dt id="stem.util.str_tools.get_size_label"> <tt class="descclassname">stem.util.str_tools.</tt><tt class="descname">get_size_label</tt><big>(</big><em>byte_count</em>, <em>decimal=0</em>, <em>is_long=False</em>, <em>is_bytes=True</em><big>)</big><a class="reference internal" href="../../_modules/stem/util/str_tools.html#get_size_label"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#stem.util.str_tools.get_size_label" title="Permalink to this definition">¶</a></dt> <dd><p>Converts a number of bytes into a human readable label in its most significant units. For instance, 7500 bytes would return “7 KB”. If the is_long option is used this expands unit labels to be the properly pluralized full word (for instance ‘Kilobytes’ rather than ‘KB’). Units go up through petabytes.</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">get_size_label</span><span class="p">(</span><span class="mi">2000000</span><span class="p">)</span> <span class="go">'1 MB'</span> <span class="gp">>>> </span><span class="n">get_size_label</span><span class="p">(</span><span class="mi">1050</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span> <span class="go">'1.02 KB'</span> <span class="gp">>>> </span><span class="n">get_size_label</span><span class="p">(</span><span class="mi">1050</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="bp">True</span><span class="p">)</span> <span class="go">'1.025 Kilobytes'</span> </pre></div> </div> <table class="docutils field-list" frame="void" rules="none"> <col class="field-name" /> <col class="field-body" /> <tbody valign="top"> <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple"> <li><strong>byte_count</strong> (<em>int</em>) – number of bytes to be converted</li> <li><strong>decimal</strong> (<em>int</em>) – number of decimal digits to be included</li> <li><strong>is_long</strong> (<em>bool</em>) – expands units label</li> <li><strong>is_bytes</strong> (<em>bool</em>) – provides units in bytes if <strong>True</strong>, bits otherwise</li> </ul> </td> </tr> <tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last"><strong>str</strong> with human readable representation of the size</p> </td> </tr> </tbody> </table> </dd></dl> <dl class="function"> <dt id="stem.util.str_tools.get_time_label"> <tt class="descclassname">stem.util.str_tools.</tt><tt class="descname">get_time_label</tt><big>(</big><em>seconds</em>, <em>decimal=0</em>, <em>is_long=False</em><big>)</big><a class="reference internal" href="../../_modules/stem/util/str_tools.html#get_time_label"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#stem.util.str_tools.get_time_label" title="Permalink to this definition">¶</a></dt> <dd><p>Converts seconds into a time label truncated to its most significant units. For instance, 7500 seconds would return “2h”. Units go up through days.</p> <p>This defaults to presenting single character labels, but if the is_long option is used this expands labels to be the full word (space included and properly pluralized). For instance, “4h” would be “4 hours” and “1m” would become “1 minute”.</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">get_time_label</span><span class="p">(</span><span class="mi">10000</span><span class="p">)</span> <span class="go">'2h'</span> <span class="gp">>>> </span><span class="n">get_time_label</span><span class="p">(</span><span class="mi">61</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="bp">True</span><span class="p">)</span> <span class="go">'1.0 minute'</span> <span class="gp">>>> </span><span class="n">get_time_label</span><span class="p">(</span><span class="mi">61</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="bp">True</span><span class="p">)</span> <span class="go">'1.01 minutes'</span> </pre></div> </div> <table class="docutils field-list" frame="void" rules="none"> <col class="field-name" /> <col class="field-body" /> <tbody valign="top"> <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple"> <li><strong>seconds</strong> (<em>int</em>) – number of seconds to be converted</li> <li><strong>decimal</strong> (<em>int</em>) – number of decimal digits to be included</li> <li><strong>is_long</strong> (<em>bool</em>) – expands units label</li> </ul> </td> </tr> <tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last"><strong>str</strong> with human readable representation of the time</p> </td> </tr> </tbody> </table> </dd></dl> <dl class="function"> <dt id="stem.util.str_tools.get_time_labels"> <tt class="descclassname">stem.util.str_tools.</tt><tt class="descname">get_time_labels</tt><big>(</big><em>seconds</em>, <em>is_long=False</em><big>)</big><a class="reference internal" href="../../_modules/stem/util/str_tools.html#get_time_labels"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#stem.util.str_tools.get_time_labels" title="Permalink to this definition">¶</a></dt> <dd><p>Provides a list of label conversions for each time unit, starting with its most significant units on down. Any counts that evaluate to zero are omitted. For example...</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">get_time_labels</span><span class="p">(</span><span class="mi">400</span><span class="p">)</span> <span class="go">['6m', '40s']</span> <span class="gp">>>> </span><span class="n">get_time_labels</span><span class="p">(</span><span class="mi">3640</span><span class="p">,</span> <span class="bp">True</span><span class="p">)</span> <span class="go">['1 hour', '40 seconds']</span> </pre></div> </div> <table class="docutils field-list" frame="void" rules="none"> <col class="field-name" /> <col class="field-body" /> <tbody valign="top"> <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple"> <li><strong>seconds</strong> (<em>int</em>) – number of seconds to be converted</li> <li><strong>is_long</strong> (<em>bool</em>) – expands units label</li> </ul> </td> </tr> <tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last"><strong>list</strong> of strings with human readable representations of the time</p> </td> </tr> </tbody> </table> </dd></dl> <dl class="function"> <dt id="stem.util.str_tools.get_short_time_label"> <tt class="descclassname">stem.util.str_tools.</tt><tt class="descname">get_short_time_label</tt><big>(</big><em>seconds</em><big>)</big><a class="reference internal" href="../../_modules/stem/util/str_tools.html#get_short_time_label"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#stem.util.str_tools.get_short_time_label" title="Permalink to this definition">¶</a></dt> <dd><p>Provides a time in the following format: [[dd-]hh:]mm:ss</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">get_short_time_label</span><span class="p">(</span><span class="mi">111</span><span class="p">)</span> <span class="go">'01:51'</span> <span class="gp">>>> </span><span class="n">get_short_time_label</span><span class="p">(</span><span class="mi">544100</span><span class="p">)</span> <span class="go">'6-07:08:20'</span> </pre></div> </div> <table class="docutils field-list" frame="void" rules="none"> <col class="field-name" /> <col class="field-body" /> <tbody valign="top"> <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>seconds</strong> (<em>int</em>) – number of seconds to be converted</td> </tr> <tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><strong>str</strong> with the short representation for the time</td> </tr> <tr class="field-odd field"><th class="field-name">Raises :</th><td class="field-body"><strong>ValueError</strong> if the input is negative</td> </tr> </tbody> </table> </dd></dl> <dl class="function"> <dt id="stem.util.str_tools.parse_short_time_label"> <tt class="descclassname">stem.util.str_tools.</tt><tt class="descname">parse_short_time_label</tt><big>(</big><em>label</em><big>)</big><a class="reference internal" href="../../_modules/stem/util/str_tools.html#parse_short_time_label"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#stem.util.str_tools.parse_short_time_label" title="Permalink to this definition">¶</a></dt> <dd><p>Provides the number of seconds corresponding to the formatting used for the cputime and etime fields of ps: [[dd-]hh:]mm:ss or mm:ss.ss</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parse_short_time_label</span><span class="p">(</span><span class="s">'01:51'</span><span class="p">)</span> <span class="go">111</span> <span class="gp">>>> </span><span class="n">parse_short_time_label</span><span class="p">(</span><span class="s">'6-07:08:20'</span><span class="p">)</span> <span class="go">544100</span> </pre></div> </div> <table class="docutils field-list" frame="void" rules="none"> <col class="field-name" /> <col class="field-body" /> <tbody valign="top"> <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>label</strong> (<em>str</em>) – time entry to be parsed</td> </tr> <tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><strong>int</strong> with the number of seconds represented by the label</td> </tr> <tr class="field-odd field"><th class="field-name">Raises :</th><td class="field-body"><strong>ValueError</strong> if input is malformed</td> </tr> </tbody> </table> </dd></dl> </div> </div> <div class="bottomnav"> </div> <div class="footer"> </div> </body> </html>