snipt

Ctrl+h for KB shortcuts

Python 3

allin() and anyin() as alternatives to `in` comparator

1
2
3
4
5
6
def allin(what, where):
    return all(x in where for x in what)


def anyin(what, where):
    return any(x in where for x in what)
https://snippets.siftie.com/embed/2189542c9ab35c1fbe8618a1f2eb0dc7/
/raw/2189542c9ab35c1fbe8618a1f2eb0dc7/
2189542c9ab35c1fbe8618a1f2eb0dc7
python3
Python 3
6
2019-04-09T11:16:04
True
False
False
Oct 16, 2012 at 10:31 AM
/api/public/snipt/39631/
allin-and-anyin-as-alternatives-to-in-comparator
<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></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="k">def</span> <span class="nf">allin</span><span class="p">(</span><span class="n">what</span><span class="p">,</span> <span class="n">where</span><span class="p">):</span> </span><span id="L-2"><a name="L-2"></a> <span class="k">return</span> <span class="nb">all</span><span class="p">(</span><span class="n">x</span> <span class="ow">in</span> <span class="n">where</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">what</span><span class="p">)</span> </span><span id="L-3"><a name="L-3"></a> </span><span id="L-4"><a name="L-4"></a> </span><span id="L-5"><a name="L-5"></a><span class="k">def</span> <span class="nf">anyin</span><span class="p">(</span><span class="n">what</span><span class="p">,</span> <span class="n">where</span><span class="p">):</span> </span><span id="L-6"><a name="L-6"></a> <span class="k">return</span> <span class="nb">any</span><span class="p">(</span><span class="n">x</span> <span class="ow">in</span> <span class="n">where</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">what</span><span class="p">)</span> </span></pre></div> </td></tr></table>
function, python, util

Python 3

invert string

1
2
3
s = 'string to be inverted'
s = s[::-1]
print s
https://snippets.siftie.com/embed/6ccedd596ea47b9733431ac023149cac/
/raw/6ccedd596ea47b9733431ac023149cac/
6ccedd596ea47b9733431ac023149cac
python3
Python 3
4
2019-04-12T10:24:38
True
False
False
/api/public/snipt/24925/
invert-string
<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></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="n">s</span> <span class="o">=</span> <span class="s1">&#39;string to be inverted&#39;</span> </span><span id="L-2"><a name="L-2"></a><span class="n">s</span> <span class="o">=</span> <span class="n">s</span><span class="p">[::</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> </span><span id="L-3"><a name="L-3"></a><span class="nb">print</span> <span class="n">s</span> </span></pre></div> </td></tr></table>
python, string, util

JavaScript

Some useful regex...

1
2
3
4
5
// Validate URL
/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \?=.-]*)*\/?$/

// Unclosed image tags
<img([^>]+)(\s*[^\/])>
https://snippets.siftie.com/embed/ca7e8273e3a493f5e26a3426e18c92da/
/raw/ca7e8273e3a493f5e26a3426e18c92da/
ca7e8273e3a493f5e26a3426e18c92da
js
JavaScript
7
2019-04-13T23:19:37
True
False
False
/api/public/snipt/13407/
some-useful-regex
<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></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="c1">// Validate URL</span> </span><span id="L-2"><a name="L-2"></a><span class="sr">/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \?=.-]*)*\/?$/</span> </span><span id="L-3"><a name="L-3"></a> </span><span id="L-4"><a name="L-4"></a><span class="c1">// Unclosed image tags</span> </span><span id="L-5"><a name="L-5"></a><span class="o">&lt;</span><span class="nx">img</span><span class="p">([</span><span class="o">^&gt;</span><span class="p">]</span><span class="o">+</span><span class="p">)(</span><span class="err">\</span><span class="nx">s</span><span class="o">*</span><span class="p">[</span><span class="o">^</span><span class="err">\</span><span class="o">/</span><span class="p">])</span><span class="o">&gt;</span> </span></pre></div> </td></tr></table>
regex, util

Bash

Find python site-packages directory

python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"
https://snippets.siftie.com/embed/108c576cc0b38c9ebb44d405ce0d3bb5/
/raw/108c576cc0b38c9ebb44d405ce0d3bb5/
108c576cc0b38c9ebb44d405ce0d3bb5
bash
Bash
1
2019-04-02T11:32:03
True
False
False
/api/public/snipt/13406/
find-python-site-packages-direcotory
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a>python -c <span class="s2">&quot;from distutils.sysconfig import get_python_lib; print get_python_lib()&quot;</span> </span></pre></div> </td></tr></table>
python, util