|
@@ -0,0 +1,596 @@
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+<!DOCTYPE html>
|
|
4
|
+<html class="writer-html4" lang="en" >
|
|
5
|
+<head>
|
|
6
|
+ <meta charset="utf-8">
|
|
7
|
+
|
|
8
|
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
9
|
+
|
|
10
|
+ <title>Welcome to cachings’s documentation! — caching documentation</title>
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+ <link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
|
|
15
|
+ <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+ <!--[if lt IE 9]>
|
|
24
|
+ <script src="_static/js/html5shiv.min.js"></script>
|
|
25
|
+ <![endif]-->
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+ <script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
|
|
29
|
+ <script type="text/javascript" src="_static/jquery.js"></script>
|
|
30
|
+ <script type="text/javascript" src="_static/underscore.js"></script>
|
|
31
|
+ <script type="text/javascript" src="_static/doctools.js"></script>
|
|
32
|
+ <script type="text/javascript" src="_static/language_data.js"></script>
|
|
33
|
+
|
|
34
|
+ <script type="text/javascript" src="_static/js/theme.js"></script>
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+ <link rel="index" title="Index" href="genindex.html" />
|
|
38
|
+ <link rel="search" title="Search" href="search.html" />
|
|
39
|
+</head>
|
|
40
|
+
|
|
41
|
+<body class="wy-body-for-nav">
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+ <div class="wy-grid-for-nav">
|
|
45
|
+
|
|
46
|
+ <nav data-toggle="wy-nav-shift" class="wy-nav-side">
|
|
47
|
+ <div class="wy-side-scroll">
|
|
48
|
+ <div class="wy-side-nav-search" >
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+ <a href="#" class="icon icon-home" alt="Documentation Home"> caching
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+ </a>
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+<div role="search">
|
|
65
|
+ <form id="rtd-search-form" class="wy-form" action="search.html" method="get">
|
|
66
|
+ <input type="text" name="q" placeholder="Search docs" />
|
|
67
|
+ <input type="hidden" name="check_keywords" value="yes" />
|
|
68
|
+ <input type="hidden" name="area" value="default" />
|
|
69
|
+ </form>
|
|
70
|
+</div>
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+ </div>
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+ <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+ <!-- Local TOC -->
|
|
84
|
+ <div class="local-toc"><ul>
|
|
85
|
+<li><a class="reference internal" href="#">Welcome to cachings’s documentation!</a><ul>
|
|
86
|
+<li><a class="reference internal" href="#caching-caching-module">caching (Caching Module)</a></li>
|
|
87
|
+</ul>
|
|
88
|
+</li>
|
|
89
|
+<li><a class="reference internal" href="#indices-and-tables">Indices and tables</a></li>
|
|
90
|
+</ul>
|
|
91
|
+</div>
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+ </div>
|
|
95
|
+
|
|
96
|
+ </div>
|
|
97
|
+ </nav>
|
|
98
|
+
|
|
99
|
+ <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+ <nav class="wy-nav-top" aria-label="top navigation">
|
|
103
|
+
|
|
104
|
+ <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
|
|
105
|
+ <a href="#">caching</a>
|
|
106
|
+
|
|
107
|
+ </nav>
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+ <div class="wy-nav-content">
|
|
111
|
+
|
|
112
|
+ <div class="rst-content">
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+<div role="navigation" aria-label="breadcrumbs navigation">
|
|
131
|
+
|
|
132
|
+ <ul class="wy-breadcrumbs">
|
|
133
|
+
|
|
134
|
+ <li><a href="#" class="icon icon-home"></a> »</li>
|
|
135
|
+
|
|
136
|
+ <li>Welcome to cachings’s documentation!</li>
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+ <li class="wy-breadcrumbs-aside">
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+ <a href="_sources/index.rst.txt" rel="nofollow"> View page source</a>
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+ </li>
|
|
146
|
+
|
|
147
|
+ </ul>
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+ <hr/>
|
|
151
|
+</div>
|
|
152
|
+ <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
|
|
153
|
+ <div itemprop="articleBody">
|
|
154
|
+
|
|
155
|
+ <div class="section" id="module-caching">
|
|
156
|
+<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>
|
|
157
|
+<div class="section" id="caching-caching-module">
|
|
158
|
+<h2>caching (Caching Module)<a class="headerlink" href="#caching-caching-module" title="Permalink to this headline">¶</a></h2>
|
|
159
|
+<p><strong>Author:</strong></p>
|
|
160
|
+<ul class="simple">
|
|
161
|
+<li>Dirk Alders <<a class="reference external" href="mailto:sudo-dirk%40mount-mockery.de">sudo-dirk<span>@</span>mount-mockery<span>.</span>de</a>></li>
|
|
162
|
+</ul>
|
|
163
|
+<p><strong>Description:</strong></p>
|
|
164
|
+<blockquote>
|
|
165
|
+<div>This Module supports functions and classes for caching e.g. properties of other instances.</div></blockquote>
|
|
166
|
+<p><strong>Submodules:</strong></p>
|
|
167
|
+<ul class="simple">
|
|
168
|
+<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>
|
|
169
|
+<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>
|
|
170
|
+</ul>
|
|
171
|
+<p><strong>Unittest:</strong></p>
|
|
172
|
+<blockquote>
|
|
173
|
+<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>
|
|
174
|
+<dl class="class">
|
|
175
|
+<dt id="caching.property_cache_json">
|
|
176
|
+<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>
|
|
177
|
+<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>
|
|
178
|
+<table class="docutils field-list" frame="void" rules="none">
|
|
179
|
+<col class="field-name" />
|
|
180
|
+<col class="field-body" />
|
|
181
|
+<tbody valign="top">
|
|
182
|
+<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
|
|
183
|
+<li><strong>source_instance</strong> (<em>instance</em>) – The source instance holding the data</li>
|
|
184
|
+<li><strong>cache_filename</strong> (<em>str</em>) – File name, where the properties are stored as cache</li>
|
|
185
|
+<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>
|
|
186
|
+</ul>
|
|
187
|
+</td>
|
|
188
|
+</tr>
|
|
189
|
+</tbody>
|
|
190
|
+</table>
|
|
191
|
+<div class="admonition warning">
|
|
192
|
+<p class="first admonition-title">Warning</p>
|
|
193
|
+<ul class="last simple">
|
|
194
|
+<li>This class uses json. You should <strong>only</strong> use keys of type string!</li>
|
|
195
|
+<li>Unicode types are transfered to strings</li>
|
|
196
|
+</ul>
|
|
197
|
+</div>
|
|
198
|
+<div class="admonition note">
|
|
199
|
+<p class="first admonition-title">Note</p>
|
|
200
|
+<p>source_instance needs to have at least the following methods: uid(), keys(), data_version(), get()</p>
|
|
201
|
+<ul class="last simple">
|
|
202
|
+<li>uid(): returns the unique id of the source.</li>
|
|
203
|
+<li>keys(): returns a list of all available keys.</li>
|
|
204
|
+<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>
|
|
205
|
+<li>get(key, default): returns the property for a key. If key does not exists, default will be returned.</li>
|
|
206
|
+</ul>
|
|
207
|
+</div>
|
|
208
|
+<p>Reasons for updating the complete data set:</p>
|
|
209
|
+<ul class="simple">
|
|
210
|
+<li>UID of source_instance has changed (in comparison to the cached value).</li>
|
|
211
|
+<li>data_version is increased</li>
|
|
212
|
+</ul>
|
|
213
|
+<p><strong>Example:</strong></p>
|
|
214
|
+<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="ch">#!/usr/bin/env python</span>
|
|
215
|
+<span class="c1"># -*- coding: UTF-8 -*-</span>
|
|
216
|
+
|
|
217
|
+<span class="kn">import</span> <span class="nn">sys</span>
|
|
218
|
+<span class="kn">import</span> <span class="nn">time</span>
|
|
219
|
+<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">'../..'</span><span class="p">)</span>
|
|
220
|
+
|
|
221
|
+<span class="kn">import</span> <span class="nn">caching</span>
|
|
222
|
+<span class="kn">import</span> <span class="nn">report</span>
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+<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">'root'</span><span class="p">,</span> <span class="s1">'DEBUG'</span><span class="p">),</span> <span class="p">])</span>
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+<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>
|
|
229
|
+ <span class="n">_ONE</span> <span class="o">=</span> <span class="s1">'1'</span>
|
|
230
|
+ <span class="n">_TWO</span> <span class="o">=</span> <span class="s1">'2'</span>
|
|
231
|
+ <span class="n">_THREE</span> <span class="o">=</span> <span class="s1">'_property_cache_data_version_'</span>
|
|
232
|
+ <span class="n">_FOUR</span> <span class="o">=</span> <span class="s1">'_property_cache_uid_'</span>
|
|
233
|
+ <span class="n">_FIVE</span> <span class="o">=</span> <span class="s1">'__property_cache_uid_'</span>
|
|
234
|
+ <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>
|
|
235
|
+ <span class="n">VERS</span> <span class="o">=</span> <span class="mf">0.1</span>
|
|
236
|
+
|
|
237
|
+ <span class="k">def</span> <span class="nf">data_version</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|
238
|
+ <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">VERS</span>
|
|
239
|
+
|
|
240
|
+ <span class="k">def</span> <span class="nf">one</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|
241
|
+ <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>
|
|
242
|
+
|
|
243
|
+ <span class="k">def</span> <span class="nf">two</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|
244
|
+ <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>
|
|
245
|
+
|
|
246
|
+ <span class="k">def</span> <span class="nf">three</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|
247
|
+ <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>
|
|
248
|
+
|
|
249
|
+ <span class="k">def</span> <span class="nf">four</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|
250
|
+ <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>
|
|
251
|
+
|
|
252
|
+ <span class="k">def</span> <span class="nf">five</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|
253
|
+ <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>
|
|
254
|
+
|
|
255
|
+ <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>
|
|
256
|
+ <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>
|
|
257
|
+ <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">'slow get executed for </span><span class="si">%s</span><span class="se">\n</span><span class="s1">'</span> <span class="o">%</span> <span class="n">k</span><span class="p">)</span>
|
|
258
|
+ <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>
|
|
259
|
+ <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>
|
|
260
|
+ <span class="n">print_n_sleep</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
|
|
261
|
+ <span class="k">return</span> <span class="s1">'one'</span>
|
|
262
|
+ <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>
|
|
263
|
+ <span class="n">print_n_sleep</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
|
|
264
|
+ <span class="k">return</span> <span class="s1">'two'</span>
|
|
265
|
+ <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>
|
|
266
|
+ <span class="n">print_n_sleep</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
|
|
267
|
+ <span class="k">return</span> <span class="s1">'three'</span>
|
|
268
|
+ <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>
|
|
269
|
+ <span class="n">print_n_sleep</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
|
|
270
|
+ <span class="k">return</span> <span class="s1">'four'</span>
|
|
271
|
+ <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>
|
|
272
|
+ <span class="n">print_n_sleep</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
|
|
273
|
+ <span class="k">return</span> <span class="s1">'five'</span>
|
|
274
|
+ <span class="k">return</span> <span class="n">default</span>
|
|
275
|
+
|
|
276
|
+ <span class="k">def</span> <span class="nf">keys</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|
277
|
+ <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">KEYS</span>
|
|
278
|
+
|
|
279
|
+ <span class="k">def</span> <span class="nf">uid</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|
280
|
+ <span class="k">return</span> <span class="kc">None</span>
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+<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>
|
|
284
|
+ <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>
|
|
285
|
+ <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>
|
|
286
|
+ <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">'cache.json'</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>
|
|
287
|
+
|
|
288
|
+ <span class="k">def</span> <span class="nf">two</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|
289
|
+ <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>
|
|
290
|
+
|
|
291
|
+ <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>
|
|
292
|
+ <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>
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+<span class="n">data</span> <span class="o">=</span> <span class="n">tsd_cache_json</span><span class="p">()</span>
|
|
296
|
+<span class="nb">print</span><span class="p">(</span><span class="s1">'Testing property_cache (json):</span><span class="se">\\</span><span class="s1">n--------------------------------'</span><span class="p">)</span>
|
|
297
|
+<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>
|
|
298
|
+<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>
|
|
299
|
+<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>
|
|
300
|
+<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>
|
|
301
|
+<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>
|
|
302
|
+</pre></div>
|
|
303
|
+</div>
|
|
304
|
+<p>Will result on the first execution to the following output (with a long execution time):</p>
|
|
305
|
+<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>
|
|
306
|
+<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">26</span><span class="p">,</span><span class="mi">892</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>
|
|
307
|
+<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">26</span><span class="p">,</span><span class="mi">893</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>
|
|
308
|
+<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>
|
|
309
|
+<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">29</span><span class="p">,</span><span class="mi">896</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">'1'</span> <span class="kn">from</span> <span class="nn">source</span> <span class="n">instance</span> <span class="p">(</span><span class="s1">'one'</span><span class="p">)</span>
|
|
310
|
+<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">29</span><span class="p">,</span><span class="mi">897</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>
|
|
311
|
+<span class="n">one</span>
|
|
312
|
+<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>
|
|
313
|
+<span class="n">two</span>
|
|
314
|
+<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>
|
|
315
|
+<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">35</span><span class="p">,</span><span class="mi">904</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">'_property_cache_data_version_'</span> <span class="kn">from</span> <span class="nn">source</span> <span class="n">instance</span> <span class="p">(</span><span class="s1">'three'</span><span class="p">)</span>
|
|
316
|
+<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">35</span><span class="p">,</span><span class="mi">904</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>
|
|
317
|
+<span class="n">three</span>
|
|
318
|
+<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>
|
|
319
|
+<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">38</span><span class="p">,</span><span class="mi">905</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">'_property_cache_uid_'</span> <span class="kn">from</span> <span class="nn">source</span> <span class="n">instance</span> <span class="p">(</span><span class="s1">'four'</span><span class="p">)</span>
|
|
320
|
+<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">38</span><span class="p">,</span><span class="mi">906</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>
|
|
321
|
+<span class="n">four</span>
|
|
322
|
+<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>
|
|
323
|
+<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">41</span><span class="p">,</span><span class="mi">908</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">'__property_cache_uid_'</span> <span class="kn">from</span> <span class="nn">source</span> <span class="n">instance</span> <span class="p">(</span><span class="s1">'five'</span><span class="p">)</span>
|
|
324
|
+<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">41</span><span class="p">,</span><span class="mi">910</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>
|
|
325
|
+<span class="n">five</span>
|
|
326
|
+</pre></div>
|
|
327
|
+</div>
|
|
328
|
+<p>With every following execution (slow for getting “two” which is not cached - see implementation):</p>
|
|
329
|
+<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>
|
|
330
|
+<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">42</span><span class="p">,</span><span class="mi">014</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>
|
|
331
|
+<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">42</span><span class="p">,</span><span class="mi">015</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">'1'</span> <span class="kn">from</span> <span class="nn">cache</span> <span class="p">(</span><span class="s1">'one'</span><span class="p">)</span>
|
|
332
|
+<span class="n">one</span>
|
|
333
|
+<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>
|
|
334
|
+<span class="n">two</span>
|
|
335
|
+<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">45</span><span class="p">,</span><span class="mi">016</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">'_property_cache_data_version_'</span> <span class="kn">from</span> <span class="nn">cache</span> <span class="p">(</span><span class="s1">'three'</span><span class="p">)</span>
|
|
336
|
+<span class="n">three</span>
|
|
337
|
+<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">45</span><span class="p">,</span><span class="mi">016</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">'_property_cache_uid_'</span> <span class="kn">from</span> <span class="nn">cache</span> <span class="p">(</span><span class="s1">'four'</span><span class="p">)</span>
|
|
338
|
+<span class="n">four</span>
|
|
339
|
+<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">45</span><span class="p">,</span><span class="mi">017</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">'__property_cache_uid_'</span> <span class="kn">from</span> <span class="nn">cache</span> <span class="p">(</span><span class="s1">'five'</span><span class="p">)</span>
|
|
340
|
+<span class="n">five</span>
|
|
341
|
+</pre></div>
|
|
342
|
+</div>
|
|
343
|
+</dd></dl>
|
|
344
|
+
|
|
345
|
+<dl class="class">
|
|
346
|
+<dt id="caching.property_cache_pickle">
|
|
347
|
+<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>
|
|
348
|
+<dd><p>Class to cache properties, which take longer on initialising than reading a file in pickle format.</p>
|
|
349
|
+<table class="docutils field-list" frame="void" rules="none">
|
|
350
|
+<col class="field-name" />
|
|
351
|
+<col class="field-body" />
|
|
352
|
+<tbody valign="top">
|
|
353
|
+<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
|
|
354
|
+<li><strong>source_instance</strong> (<em>instance</em>) – The source instance holding the data</li>
|
|
355
|
+<li><strong>cache_filename</strong> (<em>str</em>) – File name, where the properties are stored as cache</li>
|
|
356
|
+<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>
|
|
357
|
+</ul>
|
|
358
|
+</td>
|
|
359
|
+</tr>
|
|
360
|
+</tbody>
|
|
361
|
+</table>
|
|
362
|
+<div class="admonition note">
|
|
363
|
+<p class="first admonition-title">Note</p>
|
|
364
|
+<p>source_instance needs to have at least the following methods: uid(), keys(), data_version(), get()</p>
|
|
365
|
+<ul class="last simple">
|
|
366
|
+<li>uid(): returns the unique id of the source.</li>
|
|
367
|
+<li>keys(): returns a list of all available keys.</li>
|
|
368
|
+<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>
|
|
369
|
+<li>get(key, default): returns the property for a key. If key does not exists, default will be returned.</li>
|
|
370
|
+</ul>
|
|
371
|
+</div>
|
|
372
|
+<p>Reasons for updating the complete data set:</p>
|
|
373
|
+<ul class="simple">
|
|
374
|
+<li>UID of source_instance has changed (in comparison to the cached value).</li>
|
|
375
|
+<li>data_version is increased</li>
|
|
376
|
+</ul>
|
|
377
|
+<p><strong>Example:</strong></p>
|
|
378
|
+<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="ch">#!/usr/bin/env python</span>
|
|
379
|
+<span class="c1"># -*- coding: UTF-8 -*-</span>
|
|
380
|
+
|
|
381
|
+<span class="kn">import</span> <span class="nn">sys</span>
|
|
382
|
+<span class="kn">import</span> <span class="nn">time</span>
|
|
383
|
+<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">'../..'</span><span class="p">)</span>
|
|
384
|
+
|
|
385
|
+<span class="kn">import</span> <span class="nn">caching</span>
|
|
386
|
+<span class="kn">import</span> <span class="nn">report</span>
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+<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">'root'</span><span class="p">,</span> <span class="s1">'DEBUG'</span><span class="p">),</span> <span class="p">])</span>
|
|
390
|
+
|
|
391
|
+
|
|
392
|
+<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>
|
|
393
|
+ <span class="n">_ONE</span> <span class="o">=</span> <span class="s1">'1'</span>
|
|
394
|
+ <span class="n">_TWO</span> <span class="o">=</span> <span class="s1">'2'</span>
|
|
395
|
+ <span class="n">_THREE</span> <span class="o">=</span> <span class="s1">'_property_cache_data_version_'</span>
|
|
396
|
+ <span class="n">_FOUR</span> <span class="o">=</span> <span class="s1">'_property_cache_uid_'</span>
|
|
397
|
+ <span class="n">_FIVE</span> <span class="o">=</span> <span class="s1">'__property_cache_uid_'</span>
|
|
398
|
+ <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>
|
|
399
|
+ <span class="n">VERS</span> <span class="o">=</span> <span class="mf">0.1</span>
|
|
400
|
+
|
|
401
|
+ <span class="k">def</span> <span class="nf">data_version</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|
402
|
+ <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">VERS</span>
|
|
403
|
+
|
|
404
|
+ <span class="k">def</span> <span class="nf">one</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|
405
|
+ <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>
|
|
406
|
+
|
|
407
|
+ <span class="k">def</span> <span class="nf">two</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|
408
|
+ <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>
|
|
409
|
+
|
|
410
|
+ <span class="k">def</span> <span class="nf">three</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|
411
|
+ <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>
|
|
412
|
+
|
|
413
|
+ <span class="k">def</span> <span class="nf">four</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|
414
|
+ <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>
|
|
415
|
+
|
|
416
|
+ <span class="k">def</span> <span class="nf">five</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|
417
|
+ <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>
|
|
418
|
+
|
|
419
|
+ <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>
|
|
420
|
+ <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>
|
|
421
|
+ <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">'slow get executed for </span><span class="si">%s</span><span class="se">\n</span><span class="s1">'</span> <span class="o">%</span> <span class="n">k</span><span class="p">)</span>
|
|
422
|
+ <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>
|
|
423
|
+ <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>
|
|
424
|
+ <span class="n">print_n_sleep</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
|
|
425
|
+ <span class="k">return</span> <span class="s1">'one'</span>
|
|
426
|
+ <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>
|
|
427
|
+ <span class="n">print_n_sleep</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
|
|
428
|
+ <span class="k">return</span> <span class="s1">'two'</span>
|
|
429
|
+ <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>
|
|
430
|
+ <span class="n">print_n_sleep</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
|
|
431
|
+ <span class="k">return</span> <span class="s1">'three'</span>
|
|
432
|
+ <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>
|
|
433
|
+ <span class="n">print_n_sleep</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
|
|
434
|
+ <span class="k">return</span> <span class="s1">'four'</span>
|
|
435
|
+ <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>
|
|
436
|
+ <span class="n">print_n_sleep</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
|
|
437
|
+ <span class="k">return</span> <span class="s1">'five'</span>
|
|
438
|
+ <span class="k">return</span> <span class="n">default</span>
|
|
439
|
+
|
|
440
|
+ <span class="k">def</span> <span class="nf">keys</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|
441
|
+ <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">KEYS</span>
|
|
442
|
+
|
|
443
|
+ <span class="k">def</span> <span class="nf">uid</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|
444
|
+ <span class="k">return</span> <span class="kc">None</span>
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+<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>
|
|
448
|
+ <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>
|
|
449
|
+ <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>
|
|
450
|
+ <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">'cache.pickle'</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>
|
|
451
|
+
|
|
452
|
+ <span class="k">def</span> <span class="nf">two</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|
453
|
+ <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>
|
|
454
|
+
|
|
455
|
+ <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>
|
|
456
|
+ <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>
|
|
457
|
+
|
|
458
|
+
|
|
459
|
+<span class="n">data</span> <span class="o">=</span> <span class="n">tsd_cache_pickle</span><span class="p">()</span>
|
|
460
|
+<span class="nb">print</span><span class="p">(</span><span class="s1">'Testing property_cache (pickle):</span><span class="se">\\</span><span class="s1">n--------------------------------'</span><span class="p">)</span>
|
|
461
|
+<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>
|
|
462
|
+<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>
|
|
463
|
+<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>
|
|
464
|
+<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>
|
|
465
|
+<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>
|
|
466
|
+</pre></div>
|
|
467
|
+</div>
|
|
468
|
+<p>Will result on the first execution to the following output (with a long execution time):</p>
|
|
469
|
+<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>
|
|
470
|
+<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">08</span><span class="p">,</span><span class="mi">749</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>
|
|
471
|
+<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">08</span><span class="p">,</span><span class="mi">749</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>
|
|
472
|
+<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>
|
|
473
|
+<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">11</span><span class="p">,</span><span class="mi">752</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">'1'</span> <span class="kn">from</span> <span class="nn">source</span> <span class="n">instance</span> <span class="p">(</span><span class="s1">'one'</span><span class="p">)</span>
|
|
474
|
+<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">11</span><span class="p">,</span><span class="mi">753</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>
|
|
475
|
+<span class="n">one</span>
|
|
476
|
+<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>
|
|
477
|
+<span class="n">two</span>
|
|
478
|
+<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>
|
|
479
|
+<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">17</span><span class="p">,</span><span class="mi">760</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">'_property_cache_data_version_'</span> <span class="kn">from</span> <span class="nn">source</span> <span class="n">instance</span> <span class="p">(</span><span class="s1">'three'</span><span class="p">)</span>
|
|
480
|
+<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">17</span><span class="p">,</span><span class="mi">760</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>
|
|
481
|
+<span class="n">three</span>
|
|
482
|
+<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>
|
|
483
|
+<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">20</span><span class="p">,</span><span class="mi">764</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">'_property_cache_uid_'</span> <span class="kn">from</span> <span class="nn">source</span> <span class="n">instance</span> <span class="p">(</span><span class="s1">'four'</span><span class="p">)</span>
|
|
484
|
+<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">20</span><span class="p">,</span><span class="mi">764</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>
|
|
485
|
+<span class="n">four</span>
|
|
486
|
+<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>
|
|
487
|
+<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">23</span><span class="p">,</span><span class="mi">768</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">'__property_cache_uid_'</span> <span class="kn">from</span> <span class="nn">source</span> <span class="n">instance</span> <span class="p">(</span><span class="s1">'five'</span><span class="p">)</span>
|
|
488
|
+<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">23</span><span class="p">,</span><span class="mi">768</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>
|
|
489
|
+<span class="n">five</span>
|
|
490
|
+</pre></div>
|
|
491
|
+</div>
|
|
492
|
+<p>With every following execution (slow for getting “two” which is not cached - see implementation):</p>
|
|
493
|
+<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>
|
|
494
|
+<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">23</span><span class="p">,</span><span class="mi">828</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>
|
|
495
|
+<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">23</span><span class="p">,</span><span class="mi">828</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">'1'</span> <span class="kn">from</span> <span class="nn">cache</span> <span class="p">(</span><span class="s1">'one'</span><span class="p">)</span>
|
|
496
|
+<span class="n">one</span>
|
|
497
|
+<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>
|
|
498
|
+<span class="n">two</span>
|
|
499
|
+<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">26</span><span class="p">,</span><span class="mi">828</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">'_property_cache_data_version_'</span> <span class="kn">from</span> <span class="nn">cache</span> <span class="p">(</span><span class="s1">'three'</span><span class="p">)</span>
|
|
500
|
+<span class="n">three</span>
|
|
501
|
+<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">26</span><span class="p">,</span><span class="mi">829</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">'_property_cache_uid_'</span> <span class="kn">from</span> <span class="nn">cache</span> <span class="p">(</span><span class="s1">'four'</span><span class="p">)</span>
|
|
502
|
+<span class="n">four</span>
|
|
503
|
+<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">26</span><span class="p">,</span><span class="mi">829</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">'__property_cache_uid_'</span> <span class="kn">from</span> <span class="nn">cache</span> <span class="p">(</span><span class="s1">'five'</span><span class="p">)</span>
|
|
504
|
+<span class="n">five</span>
|
|
505
|
+</pre></div>
|
|
506
|
+</div>
|
|
507
|
+<dl class="method">
|
|
508
|
+<dt id="caching.property_cache_pickle.get">
|
|
509
|
+<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>
|
|
510
|
+<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>
|
|
511
|
+<table class="docutils field-list" frame="void" rules="none">
|
|
512
|
+<col class="field-name" />
|
|
513
|
+<col class="field-body" />
|
|
514
|
+<tbody valign="top">
|
|
515
|
+<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
|
|
516
|
+<li><strong>key</strong> – key for value to get.</li>
|
|
517
|
+<li><strong>default</strong> – value to be returned, if key does not exists.</li>
|
|
518
|
+</ul>
|
|
519
|
+</td>
|
|
520
|
+</tr>
|
|
521
|
+<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>
|
|
522
|
+</td>
|
|
523
|
+</tr>
|
|
524
|
+</tbody>
|
|
525
|
+</table>
|
|
526
|
+</dd></dl>
|
|
527
|
+
|
|
528
|
+<dl class="method">
|
|
529
|
+<dt id="caching.property_cache_pickle.keys">
|
|
530
|
+<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>
|
|
531
|
+<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>
|
|
532
|
+</dd></dl>
|
|
533
|
+
|
|
534
|
+</dd></dl>
|
|
535
|
+
|
|
536
|
+</div>
|
|
537
|
+<div class="toctree-wrapper compound">
|
|
538
|
+</div>
|
|
539
|
+</div>
|
|
540
|
+<div class="section" id="indices-and-tables">
|
|
541
|
+<h1>Indices and tables<a class="headerlink" href="#indices-and-tables" title="Permalink to this headline">¶</a></h1>
|
|
542
|
+<ul class="simple">
|
|
543
|
+<li><a class="reference internal" href="genindex.html"><span class="std std-ref">Index</span></a></li>
|
|
544
|
+<li><a class="reference internal" href="py-modindex.html"><span class="std std-ref">Module Index</span></a></li>
|
|
545
|
+<li><a class="reference internal" href="search.html"><span class="std std-ref">Search Page</span></a></li>
|
|
546
|
+</ul>
|
|
547
|
+</div>
|
|
548
|
+
|
|
549
|
+
|
|
550
|
+ </div>
|
|
551
|
+
|
|
552
|
+ </div>
|
|
553
|
+ <footer>
|
|
554
|
+
|
|
555
|
+
|
|
556
|
+ <hr/>
|
|
557
|
+
|
|
558
|
+ <div role="contentinfo">
|
|
559
|
+ <p>
|
|
560
|
+
|
|
561
|
+ © Copyright 2021, Dirk Alders
|
|
562
|
+
|
|
563
|
+ </p>
|
|
564
|
+ </div>
|
|
565
|
+
|
|
566
|
+
|
|
567
|
+
|
|
568
|
+ Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a
|
|
569
|
+
|
|
570
|
+ <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a>
|
|
571
|
+
|
|
572
|
+ provided by <a href="https://readthedocs.org">Read the Docs</a>.
|
|
573
|
+
|
|
574
|
+</footer>
|
|
575
|
+
|
|
576
|
+ </div>
|
|
577
|
+ </div>
|
|
578
|
+
|
|
579
|
+ </section>
|
|
580
|
+
|
|
581
|
+ </div>
|
|
582
|
+
|
|
583
|
+
|
|
584
|
+ <script type="text/javascript">
|
|
585
|
+ jQuery(function () {
|
|
586
|
+ SphinxRtdTheme.Navigation.enable(true);
|
|
587
|
+ });
|
|
588
|
+ </script>
|
|
589
|
+
|
|
590
|
+
|
|
591
|
+
|
|
592
|
+
|
|
593
|
+
|
|
594
|
+
|
|
595
|
+</body>
|
|
596
|
+</html>
|