Python Library Caching
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. <!DOCTYPE html>
  2. <html class="writer-html4" lang="en" >
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Welcome to cachings’s documentation! &mdash; caching documentation</title>
  7. <link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
  8. <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
  9. <!--[if lt IE 9]>
  10. <script src="_static/js/html5shiv.min.js"></script>
  11. <![endif]-->
  12. <script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
  13. <script type="text/javascript" src="_static/jquery.js"></script>
  14. <script type="text/javascript" src="_static/underscore.js"></script>
  15. <script type="text/javascript" src="_static/doctools.js"></script>
  16. <script type="text/javascript" src="_static/language_data.js"></script>
  17. <script type="text/javascript" src="_static/js/theme.js"></script>
  18. <link rel="index" title="Index" href="genindex.html" />
  19. <link rel="search" title="Search" href="search.html" />
  20. </head>
  21. <body class="wy-body-for-nav">
  22. <div class="wy-grid-for-nav">
  23. <nav data-toggle="wy-nav-shift" class="wy-nav-side">
  24. <div class="wy-side-scroll">
  25. <div class="wy-side-nav-search" >
  26. <a href="#" class="icon icon-home" alt="Documentation Home"> caching
  27. </a>
  28. <div role="search">
  29. <form id="rtd-search-form" class="wy-form" action="search.html" method="get">
  30. <input type="text" name="q" placeholder="Search docs" />
  31. <input type="hidden" name="check_keywords" value="yes" />
  32. <input type="hidden" name="area" value="default" />
  33. </form>
  34. </div>
  35. </div>
  36. <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
  37. <!-- Local TOC -->
  38. <div class="local-toc"><ul>
  39. <li><a class="reference internal" href="#">Welcome to cachings’s documentation!</a><ul>
  40. <li><a class="reference internal" href="#caching-caching-module">caching (Caching Module)</a></li>
  41. </ul>
  42. </li>
  43. <li><a class="reference internal" href="#indices-and-tables">Indices and tables</a></li>
  44. </ul>
  45. </div>
  46. </div>
  47. </div>
  48. </nav>
  49. <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
  50. <nav class="wy-nav-top" aria-label="top navigation">
  51. <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
  52. <a href="#">caching</a>
  53. </nav>
  54. <div class="wy-nav-content">
  55. <div class="rst-content">
  56. <div role="navigation" aria-label="breadcrumbs navigation">
  57. <ul class="wy-breadcrumbs">
  58. <li><a href="#" class="icon icon-home"></a> &raquo;</li>
  59. <li>Welcome to cachings’s documentation!</li>
  60. <li class="wy-breadcrumbs-aside">
  61. <a href="_sources/index.rst.txt" rel="nofollow"> View page source</a>
  62. </li>
  63. </ul>
  64. <hr/>
  65. </div>
  66. <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
  67. <div itemprop="articleBody">
  68. <div class="section" id="module-caching">
  69. <span id="welcome-to-cachings-s-documentation"></span><h1>Welcome to cachings’s documentation!<a class="headerlink" href="#module-caching" title="Permalink to this headline">¶</a></h1>
  70. <div class="section" id="caching-caching-module">
  71. <h2>caching (Caching Module)<a class="headerlink" href="#caching-caching-module" title="Permalink to this headline">¶</a></h2>
  72. <p><strong>Author:</strong></p>
  73. <ul class="simple">
  74. <li>Dirk Alders &lt;<a class="reference external" href="mailto:sudo-dirk&#37;&#52;&#48;mount-mockery&#46;de">sudo-dirk<span>&#64;</span>mount-mockery<span>&#46;</span>de</a>&gt;</li>
  75. </ul>
  76. <p><strong>Description:</strong></p>
  77. <blockquote>
  78. <div>This Module supports functions and classes for caching e.g. properties of other instances.</div></blockquote>
  79. <p><strong>Submodules:</strong></p>
  80. <ul class="simple">
  81. <li><a class="reference internal" href="#caching.property_cache_json" title="caching.property_cache_json"><code class="xref py py-class docutils literal notranslate"><span class="pre">caching.property_cache_json</span></code></a></li>
  82. <li><a class="reference internal" href="#caching.property_cache_pickle" title="caching.property_cache_pickle"><code class="xref py py-class docutils literal notranslate"><span class="pre">caching.property_cache_pickle</span></code></a></li>
  83. </ul>
  84. <p><strong>Unittest:</strong></p>
  85. <blockquote>
  86. <div>See also the <a class="reference download internal" download="" href="_downloads/f5f51665bfc67c10ccc039770b738067/unittest.pdf"><code class="xref download docutils literal notranslate"><span class="pre">unittest</span></code></a> documentation.</div></blockquote>
  87. <dl class="class">
  88. <dt id="caching.property_cache_json">
  89. <em class="property">class </em><code class="descclassname">caching.</code><code class="descname">property_cache_json</code><span class="sig-paren">(</span><em>source_instance</em>, <em>cache_filename</em>, <em>load_all_on_init=False</em>, <em>callback_on_data_storage=None</em><span class="sig-paren">)</span><a class="headerlink" href="#caching.property_cache_json" title="Permalink to this definition">¶</a></dt>
  90. <dd><p>Class to cache properties, which take longer on initialising than reading a file in json format. See also parent <a class="reference internal" href="#caching.property_cache_pickle" title="caching.property_cache_pickle"><code class="xref py py-class docutils literal notranslate"><span class="pre">property_cache_pickle</span></code></a></p>
  91. <table class="docutils field-list" frame="void" rules="none">
  92. <col class="field-name" />
  93. <col class="field-body" />
  94. <tbody valign="top">
  95. <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
  96. <li><strong>source_instance</strong> (<em>instance</em>) – The source instance holding the data</li>
  97. <li><strong>cache_filename</strong> (<em>str</em>) – File name, where the properties are stored as cache</li>
  98. <li><strong>load_all_on_init</strong> – Optionally init behaviour control parameter. True will load all available properties from source on init, False not.</li>
  99. </ul>
  100. </td>
  101. </tr>
  102. </tbody>
  103. </table>
  104. <div class="admonition warning">
  105. <p class="first admonition-title">Warning</p>
  106. <ul class="last simple">
  107. <li>This class uses json. You should <strong>only</strong> use keys of type string!</li>
  108. <li>Unicode types are transfered to strings</li>
  109. </ul>
  110. </div>
  111. <div class="admonition note">
  112. <p class="first admonition-title">Note</p>
  113. <p>source_instance needs to have at least the following methods: uid(), keys(), data_version(), get()</p>
  114. <ul class="last simple">
  115. <li>uid(): returns the unique id of the source.</li>
  116. <li>keys(): returns a list of all available keys.</li>
  117. <li>data_version(): returns a version number of the current data (it should be increased, if the get method of the source instance returns improved values or the data structure had been changed).</li>
  118. <li>get(key, default): returns the property for a key. If key does not exists, default will be returned.</li>
  119. </ul>
  120. </div>
  121. <p>Reasons for updating the complete data set:</p>
  122. <ul class="simple">
  123. <li>UID of source_instance has changed (in comparison to the cached value).</li>
  124. <li>data_version is increased</li>
  125. </ul>
  126. <p><strong>Example:</strong></p>
  127. <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="ch">#!/usr/bin/env python</span>
  128. <span class="c1"># -*- coding: UTF-8 -*-</span>
  129. <span class="kn">import</span> <span class="nn">sys</span>
  130. <span class="kn">import</span> <span class="nn">time</span>
  131. <span class="n">sys</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;../..&#39;</span><span class="p">)</span>
  132. <span class="kn">import</span> <span class="nn">caching</span>
  133. <span class="kn">import</span> <span class="nn">report</span>
  134. <span class="n">report</span><span class="o">.</span><span class="n">stdoutLoggingConfigure</span><span class="p">(</span><span class="n">log_name_lvl</span><span class="o">=</span><span class="p">[(</span><span class="s1">&#39;root&#39;</span><span class="p">,</span> <span class="s1">&#39;DEBUG&#39;</span><span class="p">),</span> <span class="p">])</span>
  135. <span class="k">class</span> <span class="nc">test_slow_data</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
  136. <span class="n">_ONE</span> <span class="o">=</span> <span class="s1">&#39;1&#39;</span>
  137. <span class="n">_TWO</span> <span class="o">=</span> <span class="s1">&#39;2&#39;</span>
  138. <span class="n">_THREE</span> <span class="o">=</span> <span class="s1">&#39;_property_cache_data_version_&#39;</span>
  139. <span class="n">_FOUR</span> <span class="o">=</span> <span class="s1">&#39;_property_cache_uid_&#39;</span>
  140. <span class="n">_FIVE</span> <span class="o">=</span> <span class="s1">&#39;__property_cache_uid_&#39;</span>
  141. <span class="n">KEYS</span> <span class="o">=</span> <span class="p">[</span><span class="n">_ONE</span><span class="p">,</span> <span class="n">_TWO</span><span class="p">,</span> <span class="n">_THREE</span><span class="p">,</span> <span class="n">_FOUR</span><span class="p">,</span> <span class="n">_FIVE</span><span class="p">]</span>
  142. <span class="n">VERS</span> <span class="o">=</span> <span class="mf">0.1</span>
  143. <span class="k">def</span> <span class="nf">data_version</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  144. <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">VERS</span>
  145. <span class="k">def</span> <span class="nf">one</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  146. <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_ONE</span><span class="p">)</span>
  147. <span class="k">def</span> <span class="nf">two</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  148. <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_TWO</span><span class="p">)</span>
  149. <span class="k">def</span> <span class="nf">three</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  150. <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_THREE</span><span class="p">)</span>
  151. <span class="k">def</span> <span class="nf">four</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  152. <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_FOUR</span><span class="p">)</span>
  153. <span class="k">def</span> <span class="nf">five</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  154. <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_FIVE</span><span class="p">)</span>
  155. <span class="k">def</span> <span class="nf">get</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
  156. <span class="k">def</span> <span class="nf">print_n_sleep</span><span class="p">(</span><span class="n">k</span><span class="p">):</span>
  157. <span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;slow get executed for </span><span class="si">%s</span><span class="se">\n</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">k</span><span class="p">)</span>
  158. <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
  159. <span class="k">if</span> <span class="n">key</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">_ONE</span><span class="p">:</span>
  160. <span class="n">print_n_sleep</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
  161. <span class="k">return</span> <span class="s1">&#39;one&#39;</span>
  162. <span class="k">if</span> <span class="n">key</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">_TWO</span><span class="p">:</span>
  163. <span class="n">print_n_sleep</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
  164. <span class="k">return</span> <span class="s1">&#39;two&#39;</span>
  165. <span class="k">if</span> <span class="n">key</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">_THREE</span><span class="p">:</span>
  166. <span class="n">print_n_sleep</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
  167. <span class="k">return</span> <span class="s1">&#39;three&#39;</span>
  168. <span class="k">if</span> <span class="n">key</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">_FOUR</span><span class="p">:</span>
  169. <span class="n">print_n_sleep</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
  170. <span class="k">return</span> <span class="s1">&#39;four&#39;</span>
  171. <span class="k">if</span> <span class="n">key</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">_FIVE</span><span class="p">:</span>
  172. <span class="n">print_n_sleep</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
  173. <span class="k">return</span> <span class="s1">&#39;five&#39;</span>
  174. <span class="k">return</span> <span class="n">default</span>
  175. <span class="k">def</span> <span class="nf">keys</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  176. <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">KEYS</span>
  177. <span class="k">def</span> <span class="nf">uid</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  178. <span class="k">return</span> <span class="kc">None</span>
  179. <span class="k">class</span> <span class="nc">tsd_cache_json</span><span class="p">(</span><span class="n">test_slow_data</span><span class="p">):</span>
  180. <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
  181. <span class="n">test_slow_data</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
  182. <span class="bp">self</span><span class="o">.</span><span class="n">_cached_data</span> <span class="o">=</span> <span class="n">caching</span><span class="o">.</span><span class="n">property_cache_json</span><span class="p">(</span><span class="n">test_slow_data</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">),</span> <span class="s1">&#39;cache.json&#39;</span><span class="p">,</span> <span class="n">load_all_on_init</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
  183. <span class="k">def</span> <span class="nf">two</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  184. <span class="k">return</span> <span class="n">test_slow_data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_TWO</span><span class="p">)</span>
  185. <span class="k">def</span> <span class="nf">get</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
  186. <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cached_data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">default</span><span class="p">)</span>
  187. <span class="n">data</span> <span class="o">=</span> <span class="n">tsd_cache_json</span><span class="p">()</span>
  188. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Testing property_cache (json):</span><span class="se">\\</span><span class="s1">n--------------------------------&#39;</span><span class="p">)</span>
  189. <span class="nb">print</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">one</span><span class="p">())</span>
  190. <span class="nb">print</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">two</span><span class="p">())</span>
  191. <span class="nb">print</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">three</span><span class="p">())</span>
  192. <span class="nb">print</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">four</span><span class="p">())</span>
  193. <span class="nb">print</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">five</span><span class="p">())</span>
  194. </pre></div>
  195. </div>
  196. <p>Will result on the first execution to the following output (with a long execution time):</p>
  197. <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">Testing</span> <span class="n">property_cache</span> <span class="p">(</span><span class="n">json</span><span class="p">):</span>\<span class="n">n</span><span class="o">--------------------------------</span>
  198. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">14</span> <span class="mi">00</span><span class="p">:</span><span class="mi">50</span><span class="p">:</span><span class="mi">45</span><span class="p">,</span><span class="mi">282</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">JsonCache</span><span class="p">:</span> <span class="n">Cache</span> <span class="n">file</span> <span class="n">does</span> <span class="ow">not</span> <span class="n">exists</span> <span class="p">(</span><span class="n">yet</span><span class="p">)</span><span class="o">.</span>
  199. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">14</span> <span class="mi">00</span><span class="p">:</span><span class="mi">50</span><span class="p">:</span><span class="mi">45</span><span class="p">,</span><span class="mi">283</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">INFO</span> <span class="o">-</span> <span class="n">JsonCache</span><span class="p">:</span> <span class="n">cache</span><span class="o">-</span><span class="n">file</span> <span class="n">stored</span> <span class="p">(</span><span class="n">cache</span><span class="o">.</span><span class="n">json</span><span class="p">)</span>
  200. <span class="n">slow</span> <span class="n">get</span> <span class="n">executed</span> <span class="k">for</span> <span class="mi">1</span>
  201. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">14</span> <span class="mi">00</span><span class="p">:</span><span class="mi">50</span><span class="p">:</span><span class="mi">48</span><span class="p">,</span><span class="mi">285</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">JsonCache</span><span class="p">:</span> <span class="n">Loading</span> <span class="nb">property</span> <span class="k">for</span> <span class="s1">&#39;1&#39;</span> <span class="kn">from</span> <span class="nn">source</span> <span class="n">instance</span> <span class="p">(</span><span class="s1">&#39;one&#39;</span><span class="p">)</span>
  202. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">14</span> <span class="mi">00</span><span class="p">:</span><span class="mi">50</span><span class="p">:</span><span class="mi">48</span><span class="p">,</span><span class="mi">286</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">INFO</span> <span class="o">-</span> <span class="n">JsonCache</span><span class="p">:</span> <span class="n">cache</span><span class="o">-</span><span class="n">file</span> <span class="n">stored</span> <span class="p">(</span><span class="n">cache</span><span class="o">.</span><span class="n">json</span><span class="p">)</span>
  203. <span class="n">one</span>
  204. <span class="n">slow</span> <span class="n">get</span> <span class="n">executed</span> <span class="k">for</span> <span class="mi">2</span>
  205. <span class="n">two</span>
  206. <span class="n">slow</span> <span class="n">get</span> <span class="n">executed</span> <span class="k">for</span> <span class="n">_property_cache_data_version_</span>
  207. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">14</span> <span class="mi">00</span><span class="p">:</span><span class="mi">50</span><span class="p">:</span><span class="mi">54</span><span class="p">,</span><span class="mi">292</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">JsonCache</span><span class="p">:</span> <span class="n">Loading</span> <span class="nb">property</span> <span class="k">for</span> <span class="s1">&#39;_property_cache_data_version_&#39;</span> <span class="kn">from</span> <span class="nn">source</span> <span class="n">instance</span> <span class="p">(</span><span class="s1">&#39;three&#39;</span><span class="p">)</span>
  208. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">14</span> <span class="mi">00</span><span class="p">:</span><span class="mi">50</span><span class="p">:</span><span class="mi">54</span><span class="p">,</span><span class="mi">293</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">INFO</span> <span class="o">-</span> <span class="n">JsonCache</span><span class="p">:</span> <span class="n">cache</span><span class="o">-</span><span class="n">file</span> <span class="n">stored</span> <span class="p">(</span><span class="n">cache</span><span class="o">.</span><span class="n">json</span><span class="p">)</span>
  209. <span class="n">three</span>
  210. <span class="n">slow</span> <span class="n">get</span> <span class="n">executed</span> <span class="k">for</span> <span class="n">_property_cache_uid_</span>
  211. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">14</span> <span class="mi">00</span><span class="p">:</span><span class="mi">50</span><span class="p">:</span><span class="mi">57</span><span class="p">,</span><span class="mi">297</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">JsonCache</span><span class="p">:</span> <span class="n">Loading</span> <span class="nb">property</span> <span class="k">for</span> <span class="s1">&#39;_property_cache_uid_&#39;</span> <span class="kn">from</span> <span class="nn">source</span> <span class="n">instance</span> <span class="p">(</span><span class="s1">&#39;four&#39;</span><span class="p">)</span>
  212. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">14</span> <span class="mi">00</span><span class="p">:</span><span class="mi">50</span><span class="p">:</span><span class="mi">57</span><span class="p">,</span><span class="mi">297</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">INFO</span> <span class="o">-</span> <span class="n">JsonCache</span><span class="p">:</span> <span class="n">cache</span><span class="o">-</span><span class="n">file</span> <span class="n">stored</span> <span class="p">(</span><span class="n">cache</span><span class="o">.</span><span class="n">json</span><span class="p">)</span>
  213. <span class="n">four</span>
  214. <span class="n">slow</span> <span class="n">get</span> <span class="n">executed</span> <span class="k">for</span> <span class="n">__property_cache_uid_</span>
  215. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">14</span> <span class="mi">00</span><span class="p">:</span><span class="mi">51</span><span class="p">:</span><span class="mi">00</span><span class="p">,</span><span class="mi">301</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">JsonCache</span><span class="p">:</span> <span class="n">Loading</span> <span class="nb">property</span> <span class="k">for</span> <span class="s1">&#39;__property_cache_uid_&#39;</span> <span class="kn">from</span> <span class="nn">source</span> <span class="n">instance</span> <span class="p">(</span><span class="s1">&#39;five&#39;</span><span class="p">)</span>
  216. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">14</span> <span class="mi">00</span><span class="p">:</span><span class="mi">51</span><span class="p">:</span><span class="mi">00</span><span class="p">,</span><span class="mi">301</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">INFO</span> <span class="o">-</span> <span class="n">JsonCache</span><span class="p">:</span> <span class="n">cache</span><span class="o">-</span><span class="n">file</span> <span class="n">stored</span> <span class="p">(</span><span class="n">cache</span><span class="o">.</span><span class="n">json</span><span class="p">)</span>
  217. <span class="n">five</span>
  218. </pre></div>
  219. </div>
  220. <p>With every following execution (slow for getting “two” which is not cached - see implementation):</p>
  221. <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">Testing</span> <span class="n">property_cache</span> <span class="p">(</span><span class="n">json</span><span class="p">):</span>\<span class="n">n</span><span class="o">--------------------------------</span>
  222. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">14</span> <span class="mi">00</span><span class="p">:</span><span class="mi">51</span><span class="p">:</span><span class="mi">00</span><span class="p">,</span><span class="mi">380</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">INFO</span> <span class="o">-</span> <span class="n">JsonCache</span><span class="p">:</span> <span class="n">Loading</span> <span class="n">properties</span> <span class="kn">from</span> <span class="nn">cache</span> <span class="p">(</span><span class="n">cache</span><span class="o">.</span><span class="n">json</span><span class="p">)</span>
  223. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">14</span> <span class="mi">00</span><span class="p">:</span><span class="mi">51</span><span class="p">:</span><span class="mi">00</span><span class="p">,</span><span class="mi">380</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">JsonCache</span><span class="p">:</span> <span class="n">Providing</span> <span class="nb">property</span> <span class="k">for</span> <span class="s1">&#39;1&#39;</span> <span class="kn">from</span> <span class="nn">cache</span> <span class="p">(</span><span class="s1">&#39;one&#39;</span><span class="p">)</span>
  224. <span class="n">one</span>
  225. <span class="n">slow</span> <span class="n">get</span> <span class="n">executed</span> <span class="k">for</span> <span class="mi">2</span>
  226. <span class="n">two</span>
  227. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">14</span> <span class="mi">00</span><span class="p">:</span><span class="mi">51</span><span class="p">:</span><span class="mi">03</span><span class="p">,</span><span class="mi">381</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">JsonCache</span><span class="p">:</span> <span class="n">Providing</span> <span class="nb">property</span> <span class="k">for</span> <span class="s1">&#39;_property_cache_data_version_&#39;</span> <span class="kn">from</span> <span class="nn">cache</span> <span class="p">(</span><span class="s1">&#39;three&#39;</span><span class="p">)</span>
  228. <span class="n">three</span>
  229. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">14</span> <span class="mi">00</span><span class="p">:</span><span class="mi">51</span><span class="p">:</span><span class="mi">03</span><span class="p">,</span><span class="mi">381</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">JsonCache</span><span class="p">:</span> <span class="n">Providing</span> <span class="nb">property</span> <span class="k">for</span> <span class="s1">&#39;_property_cache_uid_&#39;</span> <span class="kn">from</span> <span class="nn">cache</span> <span class="p">(</span><span class="s1">&#39;four&#39;</span><span class="p">)</span>
  230. <span class="n">four</span>
  231. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">14</span> <span class="mi">00</span><span class="p">:</span><span class="mi">51</span><span class="p">:</span><span class="mi">03</span><span class="p">,</span><span class="mi">381</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">JsonCache</span><span class="p">:</span> <span class="n">Providing</span> <span class="nb">property</span> <span class="k">for</span> <span class="s1">&#39;__property_cache_uid_&#39;</span> <span class="kn">from</span> <span class="nn">cache</span> <span class="p">(</span><span class="s1">&#39;five&#39;</span><span class="p">)</span>
  232. <span class="n">five</span>
  233. </pre></div>
  234. </div>
  235. </dd></dl>
  236. <dl class="class">
  237. <dt id="caching.property_cache_pickle">
  238. <em class="property">class </em><code class="descclassname">caching.</code><code class="descname">property_cache_pickle</code><span class="sig-paren">(</span><em>source_instance</em>, <em>cache_filename</em>, <em>load_all_on_init=False</em>, <em>callback_on_data_storage=None</em><span class="sig-paren">)</span><a class="headerlink" href="#caching.property_cache_pickle" title="Permalink to this definition">¶</a></dt>
  239. <dd><p>Class to cache properties, which take longer on initialising than reading a file in pickle format.</p>
  240. <table class="docutils field-list" frame="void" rules="none">
  241. <col class="field-name" />
  242. <col class="field-body" />
  243. <tbody valign="top">
  244. <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
  245. <li><strong>source_instance</strong> (<em>instance</em>) – The source instance holding the data</li>
  246. <li><strong>cache_filename</strong> (<em>str</em>) – File name, where the properties are stored as cache</li>
  247. <li><strong>load_all_on_init</strong> – Optionally init behaviour control parameter. True will load all available properties from source on init, False not.</li>
  248. </ul>
  249. </td>
  250. </tr>
  251. </tbody>
  252. </table>
  253. <div class="admonition note">
  254. <p class="first admonition-title">Note</p>
  255. <p>source_instance needs to have at least the following methods: uid(), keys(), data_version(), get()</p>
  256. <ul class="last simple">
  257. <li>uid(): returns the unique id of the source.</li>
  258. <li>keys(): returns a list of all available keys.</li>
  259. <li>data_version(): returns a version number of the current data (it should be increased, if the get method of the source instance returns improved values or the data structure had been changed).</li>
  260. <li>get(key, default): returns the property for a key. If key does not exists, default will be returned.</li>
  261. </ul>
  262. </div>
  263. <p>Reasons for updating the complete data set:</p>
  264. <ul class="simple">
  265. <li>UID of source_instance has changed (in comparison to the cached value).</li>
  266. <li>data_version is increased</li>
  267. </ul>
  268. <p><strong>Example:</strong></p>
  269. <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="ch">#!/usr/bin/env python</span>
  270. <span class="c1"># -*- coding: UTF-8 -*-</span>
  271. <span class="kn">import</span> <span class="nn">sys</span>
  272. <span class="kn">import</span> <span class="nn">time</span>
  273. <span class="n">sys</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;../..&#39;</span><span class="p">)</span>
  274. <span class="kn">import</span> <span class="nn">caching</span>
  275. <span class="kn">import</span> <span class="nn">report</span>
  276. <span class="n">report</span><span class="o">.</span><span class="n">stdoutLoggingConfigure</span><span class="p">(</span><span class="n">log_name_lvl</span><span class="o">=</span><span class="p">[(</span><span class="s1">&#39;root&#39;</span><span class="p">,</span> <span class="s1">&#39;DEBUG&#39;</span><span class="p">),</span> <span class="p">])</span>
  277. <span class="k">class</span> <span class="nc">test_slow_data</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
  278. <span class="n">_ONE</span> <span class="o">=</span> <span class="s1">&#39;1&#39;</span>
  279. <span class="n">_TWO</span> <span class="o">=</span> <span class="s1">&#39;2&#39;</span>
  280. <span class="n">_THREE</span> <span class="o">=</span> <span class="s1">&#39;_property_cache_data_version_&#39;</span>
  281. <span class="n">_FOUR</span> <span class="o">=</span> <span class="s1">&#39;_property_cache_uid_&#39;</span>
  282. <span class="n">_FIVE</span> <span class="o">=</span> <span class="s1">&#39;__property_cache_uid_&#39;</span>
  283. <span class="n">KEYS</span> <span class="o">=</span> <span class="p">[</span><span class="n">_ONE</span><span class="p">,</span> <span class="n">_TWO</span><span class="p">,</span> <span class="n">_THREE</span><span class="p">,</span> <span class="n">_FOUR</span><span class="p">,</span> <span class="n">_FIVE</span><span class="p">]</span>
  284. <span class="n">VERS</span> <span class="o">=</span> <span class="mf">0.1</span>
  285. <span class="k">def</span> <span class="nf">data_version</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  286. <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">VERS</span>
  287. <span class="k">def</span> <span class="nf">one</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  288. <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_ONE</span><span class="p">)</span>
  289. <span class="k">def</span> <span class="nf">two</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  290. <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_TWO</span><span class="p">)</span>
  291. <span class="k">def</span> <span class="nf">three</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  292. <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_THREE</span><span class="p">)</span>
  293. <span class="k">def</span> <span class="nf">four</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  294. <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_FOUR</span><span class="p">)</span>
  295. <span class="k">def</span> <span class="nf">five</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  296. <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_FIVE</span><span class="p">)</span>
  297. <span class="k">def</span> <span class="nf">get</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
  298. <span class="k">def</span> <span class="nf">print_n_sleep</span><span class="p">(</span><span class="n">k</span><span class="p">):</span>
  299. <span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;slow get executed for </span><span class="si">%s</span><span class="se">\n</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">k</span><span class="p">)</span>
  300. <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
  301. <span class="k">if</span> <span class="n">key</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">_ONE</span><span class="p">:</span>
  302. <span class="n">print_n_sleep</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
  303. <span class="k">return</span> <span class="s1">&#39;one&#39;</span>
  304. <span class="k">if</span> <span class="n">key</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">_TWO</span><span class="p">:</span>
  305. <span class="n">print_n_sleep</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
  306. <span class="k">return</span> <span class="s1">&#39;two&#39;</span>
  307. <span class="k">if</span> <span class="n">key</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">_THREE</span><span class="p">:</span>
  308. <span class="n">print_n_sleep</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
  309. <span class="k">return</span> <span class="s1">&#39;three&#39;</span>
  310. <span class="k">if</span> <span class="n">key</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">_FOUR</span><span class="p">:</span>
  311. <span class="n">print_n_sleep</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
  312. <span class="k">return</span> <span class="s1">&#39;four&#39;</span>
  313. <span class="k">if</span> <span class="n">key</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">_FIVE</span><span class="p">:</span>
  314. <span class="n">print_n_sleep</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
  315. <span class="k">return</span> <span class="s1">&#39;five&#39;</span>
  316. <span class="k">return</span> <span class="n">default</span>
  317. <span class="k">def</span> <span class="nf">keys</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  318. <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">KEYS</span>
  319. <span class="k">def</span> <span class="nf">uid</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  320. <span class="k">return</span> <span class="kc">None</span>
  321. <span class="k">class</span> <span class="nc">tsd_cache_pickle</span><span class="p">(</span><span class="n">test_slow_data</span><span class="p">):</span>
  322. <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
  323. <span class="n">test_slow_data</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
  324. <span class="bp">self</span><span class="o">.</span><span class="n">_cached_data</span> <span class="o">=</span> <span class="n">caching</span><span class="o">.</span><span class="n">property_cache_pickle</span><span class="p">(</span><span class="n">test_slow_data</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">),</span> <span class="s1">&#39;cache.pickle&#39;</span><span class="p">,</span> <span class="n">load_all_on_init</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
  325. <span class="k">def</span> <span class="nf">two</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  326. <span class="k">return</span> <span class="n">test_slow_data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_TWO</span><span class="p">)</span>
  327. <span class="k">def</span> <span class="nf">get</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
  328. <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cached_data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">default</span><span class="p">)</span>
  329. <span class="n">data</span> <span class="o">=</span> <span class="n">tsd_cache_pickle</span><span class="p">()</span>
  330. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Testing property_cache (pickle):</span><span class="se">\\</span><span class="s1">n--------------------------------&#39;</span><span class="p">)</span>
  331. <span class="nb">print</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">one</span><span class="p">())</span>
  332. <span class="nb">print</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">two</span><span class="p">())</span>
  333. <span class="nb">print</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">three</span><span class="p">())</span>
  334. <span class="nb">print</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">four</span><span class="p">())</span>
  335. <span class="nb">print</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">five</span><span class="p">())</span>
  336. </pre></div>
  337. </div>
  338. <p>Will result on the first execution to the following output (with a long execution time):</p>
  339. <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">Testing</span> <span class="n">property_cache</span> <span class="p">(</span><span class="n">pickle</span><span class="p">):</span>\<span class="n">n</span><span class="o">--------------------------------</span>
  340. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">14</span> <span class="mi">00</span><span class="p">:</span><span class="mi">50</span><span class="p">:</span><span class="mi">27</span><span class="p">,</span><span class="mi">103</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">PickCache</span><span class="p">:</span> <span class="n">Cache</span> <span class="n">file</span> <span class="n">does</span> <span class="ow">not</span> <span class="n">exists</span> <span class="p">(</span><span class="n">yet</span><span class="p">)</span><span class="o">.</span>
  341. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">14</span> <span class="mi">00</span><span class="p">:</span><span class="mi">50</span><span class="p">:</span><span class="mi">27</span><span class="p">,</span><span class="mi">103</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">INFO</span> <span class="o">-</span> <span class="n">PickCache</span><span class="p">:</span> <span class="n">cache</span><span class="o">-</span><span class="n">file</span> <span class="n">stored</span> <span class="p">(</span><span class="n">cache</span><span class="o">.</span><span class="n">pickle</span><span class="p">)</span>
  342. <span class="n">slow</span> <span class="n">get</span> <span class="n">executed</span> <span class="k">for</span> <span class="mi">1</span>
  343. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">14</span> <span class="mi">00</span><span class="p">:</span><span class="mi">50</span><span class="p">:</span><span class="mi">30</span><span class="p">,</span><span class="mi">107</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">PickCache</span><span class="p">:</span> <span class="n">Loading</span> <span class="nb">property</span> <span class="k">for</span> <span class="s1">&#39;1&#39;</span> <span class="kn">from</span> <span class="nn">source</span> <span class="n">instance</span> <span class="p">(</span><span class="s1">&#39;one&#39;</span><span class="p">)</span>
  344. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">14</span> <span class="mi">00</span><span class="p">:</span><span class="mi">50</span><span class="p">:</span><span class="mi">30</span><span class="p">,</span><span class="mi">107</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">INFO</span> <span class="o">-</span> <span class="n">PickCache</span><span class="p">:</span> <span class="n">cache</span><span class="o">-</span><span class="n">file</span> <span class="n">stored</span> <span class="p">(</span><span class="n">cache</span><span class="o">.</span><span class="n">pickle</span><span class="p">)</span>
  345. <span class="n">one</span>
  346. <span class="n">slow</span> <span class="n">get</span> <span class="n">executed</span> <span class="k">for</span> <span class="mi">2</span>
  347. <span class="n">two</span>
  348. <span class="n">slow</span> <span class="n">get</span> <span class="n">executed</span> <span class="k">for</span> <span class="n">_property_cache_data_version_</span>
  349. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">14</span> <span class="mi">00</span><span class="p">:</span><span class="mi">50</span><span class="p">:</span><span class="mi">36</span><span class="p">,</span><span class="mi">112</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">PickCache</span><span class="p">:</span> <span class="n">Loading</span> <span class="nb">property</span> <span class="k">for</span> <span class="s1">&#39;_property_cache_data_version_&#39;</span> <span class="kn">from</span> <span class="nn">source</span> <span class="n">instance</span> <span class="p">(</span><span class="s1">&#39;three&#39;</span><span class="p">)</span>
  350. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">14</span> <span class="mi">00</span><span class="p">:</span><span class="mi">50</span><span class="p">:</span><span class="mi">36</span><span class="p">,</span><span class="mi">113</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">INFO</span> <span class="o">-</span> <span class="n">PickCache</span><span class="p">:</span> <span class="n">cache</span><span class="o">-</span><span class="n">file</span> <span class="n">stored</span> <span class="p">(</span><span class="n">cache</span><span class="o">.</span><span class="n">pickle</span><span class="p">)</span>
  351. <span class="n">three</span>
  352. <span class="n">slow</span> <span class="n">get</span> <span class="n">executed</span> <span class="k">for</span> <span class="n">_property_cache_uid_</span>
  353. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">14</span> <span class="mi">00</span><span class="p">:</span><span class="mi">50</span><span class="p">:</span><span class="mi">39</span><span class="p">,</span><span class="mi">116</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">PickCache</span><span class="p">:</span> <span class="n">Loading</span> <span class="nb">property</span> <span class="k">for</span> <span class="s1">&#39;_property_cache_uid_&#39;</span> <span class="kn">from</span> <span class="nn">source</span> <span class="n">instance</span> <span class="p">(</span><span class="s1">&#39;four&#39;</span><span class="p">)</span>
  354. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">14</span> <span class="mi">00</span><span class="p">:</span><span class="mi">50</span><span class="p">:</span><span class="mi">39</span><span class="p">,</span><span class="mi">117</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">INFO</span> <span class="o">-</span> <span class="n">PickCache</span><span class="p">:</span> <span class="n">cache</span><span class="o">-</span><span class="n">file</span> <span class="n">stored</span> <span class="p">(</span><span class="n">cache</span><span class="o">.</span><span class="n">pickle</span><span class="p">)</span>
  355. <span class="n">four</span>
  356. <span class="n">slow</span> <span class="n">get</span> <span class="n">executed</span> <span class="k">for</span> <span class="n">__property_cache_uid_</span>
  357. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">14</span> <span class="mi">00</span><span class="p">:</span><span class="mi">50</span><span class="p">:</span><span class="mi">42</span><span class="p">,</span><span class="mi">120</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">PickCache</span><span class="p">:</span> <span class="n">Loading</span> <span class="nb">property</span> <span class="k">for</span> <span class="s1">&#39;__property_cache_uid_&#39;</span> <span class="kn">from</span> <span class="nn">source</span> <span class="n">instance</span> <span class="p">(</span><span class="s1">&#39;five&#39;</span><span class="p">)</span>
  358. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">14</span> <span class="mi">00</span><span class="p">:</span><span class="mi">50</span><span class="p">:</span><span class="mi">42</span><span class="p">,</span><span class="mi">121</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">INFO</span> <span class="o">-</span> <span class="n">PickCache</span><span class="p">:</span> <span class="n">cache</span><span class="o">-</span><span class="n">file</span> <span class="n">stored</span> <span class="p">(</span><span class="n">cache</span><span class="o">.</span><span class="n">pickle</span><span class="p">)</span>
  359. <span class="n">five</span>
  360. </pre></div>
  361. </div>
  362. <p>With every following execution (slow for getting “two” which is not cached - see implementation):</p>
  363. <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">Testing</span> <span class="n">property_cache</span> <span class="p">(</span><span class="n">pickle</span><span class="p">):</span>\<span class="n">n</span><span class="o">--------------------------------</span>
  364. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">14</span> <span class="mi">00</span><span class="p">:</span><span class="mi">50</span><span class="p">:</span><span class="mi">42</span><span class="p">,</span><span class="mi">200</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">INFO</span> <span class="o">-</span> <span class="n">PickCache</span><span class="p">:</span> <span class="n">Loading</span> <span class="n">properties</span> <span class="kn">from</span> <span class="nn">cache</span> <span class="p">(</span><span class="n">cache</span><span class="o">.</span><span class="n">pickle</span><span class="p">)</span>
  365. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">14</span> <span class="mi">00</span><span class="p">:</span><span class="mi">50</span><span class="p">:</span><span class="mi">42</span><span class="p">,</span><span class="mi">200</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">PickCache</span><span class="p">:</span> <span class="n">Providing</span> <span class="nb">property</span> <span class="k">for</span> <span class="s1">&#39;1&#39;</span> <span class="kn">from</span> <span class="nn">cache</span> <span class="p">(</span><span class="s1">&#39;one&#39;</span><span class="p">)</span>
  366. <span class="n">one</span>
  367. <span class="n">slow</span> <span class="n">get</span> <span class="n">executed</span> <span class="k">for</span> <span class="mi">2</span>
  368. <span class="n">two</span>
  369. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">14</span> <span class="mi">00</span><span class="p">:</span><span class="mi">50</span><span class="p">:</span><span class="mi">45</span><span class="p">,</span><span class="mi">201</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">PickCache</span><span class="p">:</span> <span class="n">Providing</span> <span class="nb">property</span> <span class="k">for</span> <span class="s1">&#39;_property_cache_data_version_&#39;</span> <span class="kn">from</span> <span class="nn">cache</span> <span class="p">(</span><span class="s1">&#39;three&#39;</span><span class="p">)</span>
  370. <span class="n">three</span>
  371. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">14</span> <span class="mi">00</span><span class="p">:</span><span class="mi">50</span><span class="p">:</span><span class="mi">45</span><span class="p">,</span><span class="mi">201</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">PickCache</span><span class="p">:</span> <span class="n">Providing</span> <span class="nb">property</span> <span class="k">for</span> <span class="s1">&#39;_property_cache_uid_&#39;</span> <span class="kn">from</span> <span class="nn">cache</span> <span class="p">(</span><span class="s1">&#39;four&#39;</span><span class="p">)</span>
  372. <span class="n">four</span>
  373. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">14</span> <span class="mi">00</span><span class="p">:</span><span class="mi">50</span><span class="p">:</span><span class="mi">45</span><span class="p">,</span><span class="mi">201</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">PickCache</span><span class="p">:</span> <span class="n">Providing</span> <span class="nb">property</span> <span class="k">for</span> <span class="s1">&#39;__property_cache_uid_&#39;</span> <span class="kn">from</span> <span class="nn">cache</span> <span class="p">(</span><span class="s1">&#39;five&#39;</span><span class="p">)</span>
  374. <span class="n">five</span>
  375. </pre></div>
  376. </div>
  377. <dl class="method">
  378. <dt id="caching.property_cache_pickle.get">
  379. <code class="descname">get</code><span class="sig-paren">(</span><em>key</em>, <em>default=None</em><span class="sig-paren">)</span><a class="headerlink" href="#caching.property_cache_pickle.get" title="Permalink to this definition">¶</a></dt>
  380. <dd><p>Method to get the cached property. If key does not exists in cache, the property will be loaded from source_instance and stored in cache (file).</p>
  381. <table class="docutils field-list" frame="void" rules="none">
  382. <col class="field-name" />
  383. <col class="field-body" />
  384. <tbody valign="top">
  385. <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
  386. <li><strong>key</strong> – key for value to get.</li>
  387. <li><strong>default</strong> – value to be returned, if key does not exists.</li>
  388. </ul>
  389. </td>
  390. </tr>
  391. <tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last">value for a given key or default value.</p>
  392. </td>
  393. </tr>
  394. </tbody>
  395. </table>
  396. </dd></dl>
  397. <dl class="method">
  398. <dt id="caching.property_cache_pickle.keys">
  399. <code class="descname">keys</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#caching.property_cache_pickle.keys" title="Permalink to this definition">¶</a></dt>
  400. <dd><p>Method to get the available keys (from <code class="xref py py-data docutils literal notranslate"><span class="pre">source_instance</span></code>).</p>
  401. </dd></dl>
  402. </dd></dl>
  403. </div>
  404. <div class="toctree-wrapper compound">
  405. </div>
  406. </div>
  407. <div class="section" id="indices-and-tables">
  408. <h1>Indices and tables<a class="headerlink" href="#indices-and-tables" title="Permalink to this headline">¶</a></h1>
  409. <ul class="simple">
  410. <li><a class="reference internal" href="genindex.html"><span class="std std-ref">Index</span></a></li>
  411. <li><a class="reference internal" href="py-modindex.html"><span class="std std-ref">Module Index</span></a></li>
  412. <li><a class="reference internal" href="search.html"><span class="std std-ref">Search Page</span></a></li>
  413. </ul>
  414. </div>
  415. </div>
  416. </div>
  417. <footer>
  418. <hr/>
  419. <div role="contentinfo">
  420. <p>
  421. &copy; Copyright 2021, Dirk Alders
  422. </p>
  423. </div>
  424. Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a
  425. <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a>
  426. provided by <a href="https://readthedocs.org">Read the Docs</a>.
  427. </footer>
  428. </div>
  429. </div>
  430. </section>
  431. </div>
  432. <script type="text/javascript">
  433. jQuery(function () {
  434. SphinxRtdTheme.Navigation.enable(true);
  435. });
  436. </script>
  437. </body>
  438. </html>