| |
| |
| <!DOCTYPE html> |
| <html class="writer-html5" lang="en" data-content_root="../"> |
| <head> |
| <meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1" /> |
| |
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| <title>Serialize/Deserialize (SerDe) — datasketches 0.1 documentation</title> |
| <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" /> |
| <link rel="stylesheet" type="text/css" href="../_static/css/theme.css?v=e59714d7" /> |
| |
| |
| <script src="../_static/jquery.js?v=5d32c60e"></script> |
| <script src="../_static/_sphinx_javascript_frameworks_compat.js?v=2cd50e6c"></script> |
| <script src="../_static/documentation_options.js?v=2709fde1"></script> |
| <script src="../_static/doctools.js?v=9bcbadda"></script> |
| <script src="../_static/sphinx_highlight.js?v=dc90522c"></script> |
| <script src="../_static/js/theme.js"></script> |
| <link rel="index" title="Index" href="../genindex.html" /> |
| <link rel="search" title="Search" href="../search.html" /> |
| <link rel="next" title="Jaccard Similarity" href="jaccard.html" /> |
| <link rel="prev" title="Helper Classes" href="index.html" /> |
| </head> |
| |
| <body class="wy-body-for-nav"> |
| <div class="wy-grid-for-nav"> |
| <nav data-toggle="wy-nav-shift" class="wy-nav-side"> |
| <div class="wy-side-scroll"> |
| <div class="wy-side-nav-search" > |
| |
| |
| |
| <a href="../index.html" class="icon icon-home"> |
| datasketches |
| </a> |
| <div role="search"> |
| <form id="rtd-search-form" class="wy-form" action="../search.html" method="get"> |
| <input type="text" name="q" placeholder="Search docs" aria-label="Search docs" /> |
| <input type="hidden" name="check_keywords" value="yes" /> |
| <input type="hidden" name="area" value="default" /> |
| </form> |
| </div> |
| </div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu"> |
| <ul> |
| <li class="toctree-l1"><a class="reference internal" href="../distinct_counting/index.html">Distinct Counting</a></li> |
| </ul> |
| <ul> |
| <li class="toctree-l1"><a class="reference internal" href="../quantiles/index.html">Quantiles Sketches</a></li> |
| </ul> |
| <ul> |
| <li class="toctree-l1"><a class="reference internal" href="../frequency/index.html">Frequency Sketches</a></li> |
| </ul> |
| <ul> |
| <li class="toctree-l1"><a class="reference internal" href="../vector/index.html">Vector Sketches</a></li> |
| </ul> |
| <ul> |
| <li class="toctree-l1"><a class="reference internal" href="../sampling/index.html">Random Sampling Sketches</a></li> |
| </ul> |
| <ul class="current"> |
| <li class="toctree-l1 current"><a class="reference internal" href="index.html">Helper Classes</a><ul class="current"> |
| <li class="toctree-l2 current"><a class="current reference internal" href="#">Serialize/Deserialize (SerDe)</a><ul> |
| <li class="toctree-l3"><a class="reference internal" href="#datasketches.PyObjectSerDe"><code class="docutils literal notranslate"><span class="pre">PyObjectSerDe</span></code></a><ul> |
| <li class="toctree-l4"><a class="reference internal" href="#datasketches.PyObjectSerDe.get_size"><code class="docutils literal notranslate"><span class="pre">PyObjectSerDe.get_size()</span></code></a></li> |
| <li class="toctree-l4"><a class="reference internal" href="#datasketches.PyObjectSerDe.to_bytes"><code class="docutils literal notranslate"><span class="pre">PyObjectSerDe.to_bytes()</span></code></a></li> |
| <li class="toctree-l4"><a class="reference internal" href="#datasketches.PyObjectSerDe.from_bytes"><code class="docutils literal notranslate"><span class="pre">PyObjectSerDe.from_bytes()</span></code></a></li> |
| </ul> |
| </li> |
| <li class="toctree-l3"><a class="reference internal" href="#datasketches.PyStringsSerDe"><code class="docutils literal notranslate"><span class="pre">PyStringsSerDe</span></code></a></li> |
| <li class="toctree-l3"><a class="reference internal" href="#datasketches.PyIntsSerDe"><code class="docutils literal notranslate"><span class="pre">PyIntsSerDe</span></code></a></li> |
| <li class="toctree-l3"><a class="reference internal" href="#datasketches.PyLongsSerDe"><code class="docutils literal notranslate"><span class="pre">PyLongsSerDe</span></code></a></li> |
| <li class="toctree-l3"><a class="reference internal" href="#datasketches.PyFloatsSerDe"><code class="docutils literal notranslate"><span class="pre">PyFloatsSerDe</span></code></a></li> |
| <li class="toctree-l3"><a class="reference internal" href="#datasketches.PyDoublesSerDe"><code class="docutils literal notranslate"><span class="pre">PyDoublesSerDe</span></code></a></li> |
| </ul> |
| </li> |
| <li class="toctree-l2"><a class="reference internal" href="jaccard.html">Jaccard Similarity</a></li> |
| <li class="toctree-l2"><a class="reference internal" href="tuple_policy.html">Tuple Policy</a></li> |
| <li class="toctree-l2"><a class="reference internal" href="ks_test.html">Kolmogorov-Smirnov Test</a></li> |
| <li class="toctree-l2"><a class="reference internal" href="kernel.html">Kernel Function</a></li> |
| </ul> |
| </li> |
| </ul> |
| |
| </div> |
| </div> |
| </nav> |
| |
| <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" > |
| <i data-toggle="wy-nav-top" class="fa fa-bars"></i> |
| <a href="../index.html">datasketches</a> |
| </nav> |
| |
| <div class="wy-nav-content"> |
| <div class="rst-content"> |
| <div role="navigation" aria-label="Page navigation"> |
| <ul class="wy-breadcrumbs"> |
| <li><a href="../index.html" class="icon icon-home" aria-label="Home"></a></li> |
| <li class="breadcrumb-item"><a href="index.html">Helper Classes</a></li> |
| <li class="breadcrumb-item active">Serialize/Deserialize (SerDe)</li> |
| <li class="wy-breadcrumbs-aside"> |
| <a href="../_sources/helper/serde.rst.txt" rel="nofollow"> View page source</a> |
| </li> |
| </ul> |
| <hr/> |
| </div> |
| <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article"> |
| <div itemprop="articleBody"> |
| |
| <section id="serialize-deserialize-serde"> |
| <h1>Serialize/Deserialize (SerDe)<a class="headerlink" href="#serialize-deserialize-serde" title="Link to this heading"></a></h1> |
| <p>A SerDe is a class used to serialize items sketches to a <code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code> object in binary. |
| Several example SerDes are provided as references.</p> |
| <p>The use of binary-compatible SerDes in different languages is critical for cross-language compatibility.</p> |
| <p>Each implementation must extend the <a class="reference internal" href="#datasketches.PyObjectSerDe" title="datasketches.PyObjectSerDe"><code class="xref py py-class docutils literal notranslate"><span class="pre">PyObjectSerDe</span></code></a> class and override all three of its methods.</p> |
| <dl class="py class"> |
| <dt class="sig sig-object py" id="datasketches.PyObjectSerDe"> |
| <em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">PyObjectSerDe</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#datasketches.PyObjectSerDe" title="Link to this definition"></a></dt> |
| <dd><p>An abstract base class for serde objects. All custom serdes must extend this class.</p> |
| <dl class="py method"> |
| <dt class="sig sig-object py" id="datasketches.PyObjectSerDe.get_size"> |
| <span class="sig-name descname"><span class="pre">get_size</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">self</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">item</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">→</span> <span class="sig-return-typehint"><span class="pre">int</span></span></span><a class="headerlink" href="#datasketches.PyObjectSerDe.get_size" title="Link to this definition"></a></dt> |
| <dd><p>Returns the size in bytes of an item</p> |
| <dl class="field-list simple"> |
| <dt class="field-odd">Parameters<span class="colon">:</span></dt> |
| <dd class="field-odd"><p><strong>item</strong> (<em>object</em>) – The specified object</p> |
| </dd> |
| <dt class="field-even">Returns<span class="colon">:</span></dt> |
| <dd class="field-even"><p>The size of the item in bytes</p> |
| </dd> |
| <dt class="field-odd">Return type<span class="colon">:</span></dt> |
| <dd class="field-odd"><p>int</p> |
| </dd> |
| </dl> |
| </dd></dl> |
| |
| <dl class="py method"> |
| <dt class="sig sig-object py" id="datasketches.PyObjectSerDe.to_bytes"> |
| <span class="sig-name descname"><span class="pre">to_bytes</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">self</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">item</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">→</span> <span class="sig-return-typehint"><span class="pre">bytes</span></span></span><a class="headerlink" href="#datasketches.PyObjectSerDe.to_bytes" title="Link to this definition"></a></dt> |
| <dd><p>Retuns a bytes object with a serialized version of an item</p> |
| <dl class="field-list simple"> |
| <dt class="field-odd">Parameters<span class="colon">:</span></dt> |
| <dd class="field-odd"><p><strong>item</strong> (<em>object</em>) – The specified object</p> |
| </dd> |
| <dt class="field-even">Returns<span class="colon">:</span></dt> |
| <dd class="field-even"><p>A <code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code> object with the serialized object</p> |
| </dd> |
| <dt class="field-odd">Return type<span class="colon">:</span></dt> |
| <dd class="field-odd"><p>bytes</p> |
| </dd> |
| </dl> |
| </dd></dl> |
| |
| <dl class="py method"> |
| <dt class="sig sig-object py" id="datasketches.PyObjectSerDe.from_bytes"> |
| <span class="sig-name descname"><span class="pre">from_bytes</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">self</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">data</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">bytes</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">offset</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">int</span></span></em><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">→</span> <span class="sig-return-typehint"><span class="pre">tuple</span></span></span><a class="headerlink" href="#datasketches.PyObjectSerDe.from_bytes" title="Link to this definition"></a></dt> |
| <dd><p>Reads a bytes object starting from the given offest and returns a tuple of the reconstructed object and the number of additional bytes read</p> |
| <dl class="field-list simple"> |
| <dt class="field-odd">Parameters<span class="colon">:</span></dt> |
| <dd class="field-odd"><ul class="simple"> |
| <li><p><strong>data</strong> (<em>bytes</em>) – A <code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code> object from which to deserialize</p></li> |
| <li><p><strong>offset</strong> (<em>int</em>) – The offset, in bytes, at which to start reading</p></li> |
| </ul> |
| </dd> |
| <dt class="field-even">Returns<span class="colon">:</span></dt> |
| <dd class="field-even"><p>A <code class="xref py py-class docutils literal notranslate"><span class="pre">tuple</span></code> with the reconstructed object and the number of bytes read</p> |
| </dd> |
| <dt class="field-odd">Return type<span class="colon">:</span></dt> |
| <dd class="field-odd"><p>tuple(object, int)</p> |
| </dd> |
| </dl> |
| </dd></dl> |
| |
| </dd></dl> |
| |
| <p>The provided SerDes are:</p> |
| <dl class="py class"> |
| <dt class="sig sig-object py" id="datasketches.PyStringsSerDe"> |
| <em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">PyStringsSerDe</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#datasketches.PyStringsSerDe" title="Link to this definition"></a></dt> |
| <dd><p>Bases: <a class="reference internal" href="#datasketches.PyObjectSerDe" title="_datasketches.PyObjectSerDe"><code class="xref py py-class docutils literal notranslate"><span class="pre">PyObjectSerDe</span></code></a></p> |
| <p>Implements a simple string-encoding scheme where a string is |
| written as <cite><num_bytes> <string_contents></cite>, with no null termination. |
| This format allows pre-allocating each string, at the cost of |
| additional storage. Using this format, the serialized string consumes |
| <code class="docutils literal notranslate"><span class="pre">4</span> <span class="pre">+</span> <span class="pre">len(item)</span></code> bytes.</p> |
| </dd></dl> |
| |
| <dl class="py class"> |
| <dt class="sig sig-object py" id="datasketches.PyIntsSerDe"> |
| <em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">PyIntsSerDe</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#datasketches.PyIntsSerDe" title="Link to this definition"></a></dt> |
| <dd><p>Bases: <a class="reference internal" href="#datasketches.PyObjectSerDe" title="_datasketches.PyObjectSerDe"><code class="xref py py-class docutils literal notranslate"><span class="pre">PyObjectSerDe</span></code></a></p> |
| <p>Implements an integer encoding scheme where each integer is written |
| as a 32-bit (4 byte) little-endian value.</p> |
| </dd></dl> |
| |
| <dl class="py class"> |
| <dt class="sig sig-object py" id="datasketches.PyLongsSerDe"> |
| <em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">PyLongsSerDe</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#datasketches.PyLongsSerDe" title="Link to this definition"></a></dt> |
| <dd><p>Bases: <a class="reference internal" href="#datasketches.PyObjectSerDe" title="_datasketches.PyObjectSerDe"><code class="xref py py-class docutils literal notranslate"><span class="pre">PyObjectSerDe</span></code></a></p> |
| <p>Implements an integer encoding scheme where each integer is written |
| as a 64-bit (8 byte) little-endian value.</p> |
| </dd></dl> |
| |
| <dl class="py class"> |
| <dt class="sig sig-object py" id="datasketches.PyFloatsSerDe"> |
| <em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">PyFloatsSerDe</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#datasketches.PyFloatsSerDe" title="Link to this definition"></a></dt> |
| <dd><p>Bases: <a class="reference internal" href="#datasketches.PyObjectSerDe" title="_datasketches.PyObjectSerDe"><code class="xref py py-class docutils literal notranslate"><span class="pre">PyObjectSerDe</span></code></a></p> |
| <p>Implements a floating point encoding scheme where each value is written |
| as a 32-bit floating point value.</p> |
| </dd></dl> |
| |
| <dl class="py class"> |
| <dt class="sig sig-object py" id="datasketches.PyDoublesSerDe"> |
| <em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">PyDoublesSerDe</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#datasketches.PyDoublesSerDe" title="Link to this definition"></a></dt> |
| <dd><p>Implements a floating point encoding scheme where each value is written |
| as a 64-bit floating point value.</p> |
| </dd></dl> |
| |
| </section> |
| |
| |
| </div> |
| </div> |
| <footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer"> |
| <a href="index.html" class="btn btn-neutral float-left" title="Helper Classes" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a> |
| <a href="jaccard.html" class="btn btn-neutral float-right" title="Jaccard Similarity" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a> |
| </div> |
| |
| <hr/> |
| |
| <div role="contentinfo"> |
| <p>© Copyright 2023.</p> |
| </div> |
| |
| Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a |
| <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a> |
| provided by <a href="https://readthedocs.org">Read the Docs</a>. |
| |
| |
| </footer> |
| </div> |
| </div> |
| </section> |
| </div> |
| <script> |
| jQuery(function () { |
| SphinxRtdTheme.Navigation.enable(true); |
| }); |
| </script> |
| |
| </body> |
| </html> |