Skip to content

Commit

Permalink
Generated gh-pages for commit d963bf7
Browse files Browse the repository at this point in the history
Author: Giulio Ungaretti <[email protected]>

    feat: add JSON encoding of unexpected types (#402)
  • Loading branch information
Documentation Bot committed Nov 22, 2016
1 parent 9634560 commit 1c7391a
Show file tree
Hide file tree
Showing 18 changed files with 103 additions and 42 deletions.
44 changes: 40 additions & 4 deletions _modules/qcodes/tests/test_helpers.html
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,22 @@
<div itemprop="articleBody">

<h1>Source code for qcodes.tests.test_helpers</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">unittest</span> <span class="kn">import</span> <span class="n">TestCase</span>
<span></span><span class="kn">import</span> <span class="nn">asyncio</span>
<span class="kn">import</span> <span class="nn">json</span>
<span class="kn">import</span> <span class="nn">time</span>

<span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">OrderedDict</span>
<span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">datetime</span>
<span class="kn">import</span> <span class="nn">asyncio</span>
<span class="kn">import</span> <span class="nn">json</span>
<span class="kn">from</span> <span class="nn">unittest</span> <span class="kn">import</span> <span class="n">TestCase</span>


<span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>

<span class="kn">from</span> <span class="nn">qcodes.utils.helpers</span> <span class="kn">import</span> <span class="p">(</span><span class="n">is_sequence</span><span class="p">,</span> <span class="n">permissive_range</span><span class="p">,</span> <span class="n">wait_secs</span><span class="p">,</span>
<span class="n">make_unique</span><span class="p">,</span> <span class="n">DelegateAttributes</span><span class="p">,</span>
<span class="n">LogCapture</span><span class="p">,</span> <span class="n">strip_attrs</span><span class="p">,</span> <span class="n">full_class</span><span class="p">,</span>
<span class="n">named_repr</span><span class="p">,</span> <span class="n">make_sweep</span><span class="p">,</span> <span class="n">is_sequence_of</span><span class="p">,</span>
<span class="n">compare_dictionaries</span><span class="p">)</span>
<span class="n">compare_dictionaries</span><span class="p">,</span> <span class="n">NumpyJSONEncoder</span><span class="p">)</span>
<span class="kn">from</span> <span class="nn">qcodes.utils.deferred_operations</span> <span class="kn">import</span> <span class="n">is_function</span>


Expand Down Expand Up @@ -686,6 +690,38 @@ <h1>Source code for qcodes.tests.test_helpers</h1><div class="highlight"><pre>
<span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">subTest</span><span class="p">(</span><span class="n">args</span><span class="o">=</span><span class="n">args</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">assertFalse</span><span class="p">(</span><span class="n">is_sequence_of</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">))</span></div></div>

<span class="c1"># tests related to JSON encoding</span>
<div class="viewcode-block" id="TestJSONencoder"><a class="viewcode-back" href="../../../auto/qcodes.tests.html#qcodes.tests.test_helpers.TestJSONencoder">[docs]</a><span class="k">class</span> <span class="nc">TestJSONencoder</span><span class="p">(</span><span class="n">TestCase</span><span class="p">):</span>

<div class="viewcode-block" id="TestJSONencoder.testNumpyJSONEncoder"><a class="viewcode-back" href="../../../auto/qcodes.tests.html#qcodes.tests.test_helpers.TestJSONencoder.testNumpyJSONEncoder">[docs]</a> <span class="k">def</span> <span class="nf">testNumpyJSONEncoder</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">e</span> <span class="o">=</span> <span class="n">NumpyJSONEncoder</span><span class="p">()</span>

<span class="c1"># test basic python types</span>
<span class="n">od</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="p">()</span>
<span class="n">od</span><span class="p">[</span><span class="s1">&#39;a&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>
<span class="n">od</span><span class="p">[</span><span class="s1">&#39;b&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>
<span class="n">testinput</span><span class="o">=</span><span class="p">[</span><span class="mi">10</span><span class="p">,</span> <span class="nb">float</span><span class="p">(</span><span class="mf">10.</span><span class="p">),</span> <span class="s1">&#39;hello&#39;</span><span class="p">,</span> <span class="n">od</span><span class="p">]</span>
<span class="n">testoutput</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;10&#39;</span><span class="p">,</span> <span class="s1">&#39;10.0&#39;</span><span class="p">,</span> <span class="s1">&#39;&quot;hello&quot;&#39;</span><span class="p">,</span> <span class="s1">&#39;{&quot;a&quot;: 0, &quot;b&quot;: 1}&#39;</span><span class="p">]</span>
<span class="c1"># int</span>
<span class="k">for</span> <span class="n">d</span><span class="p">,</span> <span class="n">r</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">testinput</span><span class="p">,</span> <span class="n">testoutput</span><span class="p">):</span>
<span class="n">v</span><span class="o">=</span><span class="n">e</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
<span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">d</span><span class="p">)</span> <span class="o">==</span> <span class="nb">dict</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">assertDictEqual</span><span class="p">(</span><span class="n">v</span><span class="p">,</span> <span class="n">r</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="n">v</span><span class="p">,</span> <span class="n">r</span><span class="p">)</span>


<span class="c1"># test numpy array</span>
<span class="n">x</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">])</span>
<span class="n">v</span><span class="o">=</span><span class="n">e</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="n">v</span><span class="p">,</span> <span class="s1">&#39;[1, 0, 0]&#39;</span><span class="p">)</span>

<span class="c1"># test class</span>
<span class="k">class</span> <span class="nc">dummy</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="k">pass</span>
<span class="c1"># test that does not raise, do not care about</span>
<span class="c1"># return value</span>
<span class="n">e</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="n">dummy</span><span class="p">())</span></div></div>

<div class="viewcode-block" id="TestCompareDictionaries"><a class="viewcode-back" href="../../../auto/qcodes.tests.html#qcodes.tests.test_helpers.TestCompareDictionaries">[docs]</a><span class="k">class</span> <span class="nc">TestCompareDictionaries</span><span class="p">(</span><span class="n">TestCase</span><span class="p">):</span>
<div class="viewcode-block" id="TestCompareDictionaries.test_same"><a class="viewcode-back" href="../../../auto/qcodes.tests.html#qcodes.tests.test_helpers.TestCompareDictionaries.test_same">[docs]</a> <span class="k">def</span> <span class="nf">test_same</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
Expand Down
8 changes: 7 additions & 1 deletion _modules/qcodes/utils/helpers.html
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ <h1>Source code for qcodes.utils.helpers</h1><div class="highlight"><pre>
<span class="sd">&quot;&quot;&quot;Return numpy types as standard types.&quot;&quot;&quot;</span>
<span class="c1"># http://stackoverflow.com/questions/27050108/convert-numpy-type-to-python</span>
<span class="c1"># http://stackoverflow.com/questions/9452775/converting-numpy-dtypes-to-native-python-types/11389998#11389998</span>

<div class="viewcode-block" id="NumpyJSONEncoder.default"><a class="viewcode-back" href="../../../auto/qcodes.utils.html#qcodes.utils.helpers.NumpyJSONEncoder.default">[docs]</a> <span class="k">def</span> <span class="nf">default</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">integer</span><span class="p">):</span>
<span class="k">return</span> <span class="nb">int</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>
Expand All @@ -169,7 +170,12 @@ <h1>Source code for qcodes.utils.helpers</h1><div class="highlight"><pre>
<span class="s1">&#39;im&#39;</span><span class="p">:</span> <span class="nb">float</span><span class="p">(</span><span class="n">obj</span><span class="o">.</span><span class="n">imag</span><span class="p">)</span>
<span class="p">}</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">NumpyJSONEncoder</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">default</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span></div></div>
<span class="k">try</span><span class="p">:</span>
<span class="n">s</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">NumpyJSONEncoder</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">default</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>
<span class="k">except</span> <span class="ne">TypeError</span><span class="p">:</span>
<span class="c1"># we cannot convert the object to JSON, just take a string</span>
<span class="n">s</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>
<span class="k">return</span> <span class="n">s</span></div></div>


<div class="viewcode-block" id="tprint"><a class="viewcode-back" href="../../../auto/qcodes.utils.html#qcodes.utils.helpers.tprint">[docs]</a><span class="k">def</span> <span class="nf">tprint</span><span class="p">(</span><span class="n">string</span><span class="p">,</span> <span class="n">dt</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">tag</span><span class="o">=</span><span class="s1">&#39;default&#39;</span><span class="p">):</span>
Expand Down
2 changes: 1 addition & 1 deletion _modules/qcodes/utils/validators.html
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ <h1>Source code for qcodes.utils.validators</h1><div class="highlight"><pre>
<span class="k">if</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">shape</span><span class="p">(</span><span class="n">value</span><span class="p">)</span> <span class="o">!=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_shape</span><span class="p">):</span>
<span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
<span class="s1">&#39;{} does not have expected shape {}; {}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
<span class="nb">repr</span><span class="p">(</span><span class="n">value</span><span class="p">),</span> <span class="bp">self</span><span class="o">.</span><span class="n">_shape</span><span class="p">,</span> <span class="n">context</span><span class="p">))</span>
<span class="nb">repr</span><span class="p">(</span><span class="n">value</span><span class="p">),</span> <span class="bp">self</span><span class="o">.</span><span class="n">_shape</span><span class="p">,</span> <span class="n">context</span><span class="p">))</span>

<span class="c1"># Only check if max is not inf as it can be expensive for large arrays</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_max_value</span> <span class="o">!=</span> <span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="s2">&quot;inf&quot;</span><span class="p">)):</span>
Expand Down
2 changes: 1 addition & 1 deletion api/generated/qcodes.MockInstrument.html
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ <h1>qcodes.MockInstrument<a class="headerlink" href="#qcodes-mockinstrument" tit
<li><strong>name</strong> (<a class="reference external" href="http://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.5)"><em>str</em></a>) &#8211; The name of this instrument.</li>
<li><strong>delay</strong> (<em>number</em>) &#8211; Time (in seconds) to wait after any operation
to simulate communication delay. Default 0.</li>
<li><strong>model</strong> (<a class="reference internal" href="qcodes.MockModel.html#qcodes.MockModel" title="qcodes.MockModel"><em>MockModel</em></a>) &#8211; A model to connect to. Subclasses MUST accept
<li><strong>model</strong> (<a class="reference internal" href="../../auto/qcodes.instrument.html#qcodes.instrument.mock.MockModel" title="qcodes.instrument.mock.MockModel"><em>MockModel</em></a>) &#8211; A model to connect to. Subclasses MUST accept
<code class="docutils literal"><span class="pre">model</span></code> as a constructor kwarg ONLY, even though it is required.
See notes in <code class="docutils literal"><span class="pre">Instrument</span></code> docstring.
The model should have one or two methods related directly to this
Expand Down
2 changes: 1 addition & 1 deletion api/generated/qcodes.SweepFixedValues.html
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ <h1>qcodes.SweepFixedValues<a class="headerlink" href="#qcodes-sweepfixedvalues"
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>parameter</strong> (<a class="reference internal" href="../../auto/qcodes.instrument.html#qcodes.instrument.parameter.Parameter" title="qcodes.instrument.parameter.Parameter"><em>Parameter</em></a>) &#8211; the target of the sweep, an object with set and
<li><strong>parameter</strong> (<a class="reference internal" href="qcodes.Parameter.html#qcodes.Parameter" title="qcodes.Parameter"><em>Parameter</em></a>) &#8211; the target of the sweep, an object with set and
optionally validate methods</li>
<li><strong>keys</strong> (<em>Optional[Any]</em>) &#8211; one or a sequence of items, each of which can be:
- a single parameter value
Expand Down
2 changes: 1 addition & 1 deletion api/generated/qcodes.SweepValues.html
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ <h1>qcodes.SweepValues<a class="headerlink" href="#qcodes-sweepvalues" title="Pe
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>parameter</strong> (<a class="reference internal" href="../../auto/qcodes.instrument.html#qcodes.instrument.parameter.Parameter" title="qcodes.instrument.parameter.Parameter"><em>Parameter</em></a>) &#8211; the target of the sweep, an object with
<li><strong>parameter</strong> (<a class="reference internal" href="qcodes.Parameter.html#qcodes.Parameter" title="qcodes.Parameter"><em>Parameter</em></a>) &#8211; the target of the sweep, an object with
set, and optionally validate methods</li>
<li><strong>**kwargs</strong> &#8211; Passed on to Metadatable parent</li>
</ul>
Expand Down
Loading

0 comments on commit 1c7391a

Please sign in to comment.