snipt

Ctrl+h for KB shortcuts

VimL

Auto-incrementing pattern match

let g:I=1
%g/^- id: \d\+$/ s/\d\+/\=g:I/|let g:I=g:I+1
https://snipt.net/embed/e7c3d2a3be4f428a198dbf003a98641c/
https://snipt.net/raw/e7c3d2a3be4f428a198dbf003a98641c/
e7c3d2a3be4f428a198dbf003a98641c
vim
VimL
2
2016-09-22T20:21:10
True
False
False
Mar 30, 2013 at 08:47 PM
/api/public/snipt/58583/
auto-incrementing-pattern-match
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="k">let</span> <span class="k">g</span>:I<span class="p">=</span><span class="m">1</span> </span><span id="L-2"><a name="L-2"></a>%<span class="k">g</span><span class="sr">/^- id: \d\+$/</span> s<span class="sr">/\d\+/</span>\<span class="p">=</span><span class="k">g</span>:I/<span class="p">|</span><span class="k">let</span> <span class="k">g</span>:I<span class="p">=</span><span class="k">g</span>:I<span class="p">+</span><span class="m">1</span> </span></pre></div> </td></tr></table>
pattern, vim

Python

Singleton trigger

1
2
3
4
5
6
7
8
class Foo(int):
    shared_state = {'current': 0}
    def __new__(cls):
        return int.__new__(cls, cls.shared_state['current'])              
    
    def __init__(self):
        self.shared_state['current'] = abs(self.shared_state['current'] - 1)
        self.__dict__ = self.shared_state
https://snipt.net/embed/b72d490b985e11918daabfecc468c83a/
https://snipt.net/raw/b72d490b985e11918daabfecc468c83a/
b72d490b985e11918daabfecc468c83a
python
Python
8
2016-09-22T22:43:53
True
False
False
/api/public/snipt/31485/
singleton-trigger
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a> <a href="#L-8">8</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="k">class</span> <span class="nc">Foo</span><span class="p">(</span><span class="nb">int</span><span class="p">):</span> </span><span id="L-2"><a name="L-2"></a> <span class="n">shared_state</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;current&#39;</span><span class="p">:</span> <span class="mi">0</span><span class="p">}</span> </span><span id="L-3"><a name="L-3"></a> <span class="k">def</span> <span class="nf">__new__</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span> </span><span id="L-4"><a name="L-4"></a> <span class="k">return</span> <span class="nb">int</span><span class="o">.</span><span class="n">__new__</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="n">cls</span><span class="o">.</span><span class="n">shared_state</span><span class="p">[</span><span class="s">&#39;current&#39;</span><span class="p">])</span> </span><span id="L-5"><a name="L-5"></a> </span><span id="L-6"><a name="L-6"></a> <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> </span><span id="L-7"><a name="L-7"></a> <span class="bp">self</span><span class="o">.</span><span class="n">shared_state</span><span class="p">[</span><span class="s">&#39;current&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">abs</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">shared_state</span><span class="p">[</span><span class="s">&#39;current&#39;</span><span class="p">]</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> </span><span id="L-8"><a name="L-8"></a> <span class="bp">self</span><span class="o">.</span><span class="n">__dict__</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">shared_state</span> </span></pre></div> </td></tr></table>
borg, pattern, python, trigger
Copyrighted, illegal, or inappropriate content? Email support@snipt.net.