Skip to content

Instantly share code, notes, and snippets.

@nvgoldin
nvgoldin / local_vars.sh
Created April 18, 2017 07:08
bash local variables and assignment in the same line masking the return code [SC2155]
> cat local.sh
#!/bin/bash -x
function test1() {
echo "test1"
local kk=$(wtf)
echo $?
}
function test2() {
@nvgoldin
nvgoldin / inspect_local.sh
Created April 23, 2017 08:48
inspect whether a variable declared with 'local' is local
#!/bin/bash -ex
a="global"
b="global"
c="global"
d="global"
function koko() {
local a b c d
a="xxx"
b="zzz"
c="ddd"
@nvgoldin
nvgoldin / transient.py
Created April 23, 2017 16:09
Running 'transient' systemd unit files with dbus
from __future__ import print_function
import uuid
import functools
import hashlib
# TO-DO: switch to pydbus, currently same code
# with pydbus gives errors.
from dbus import SessionBus, SystemBus, Interface
def rand_name():
import pytest
import textwrap
import tempfile
from lago import sdk
def generate_config():
conf = """
domains:
@nvgoldin
nvgoldin / Dockerfile-python:2.7.14-alpine-h5py
Last active January 3, 2021 15:31
Installing tables, h5py in Alpine Docker(python:2.7.14-alpine)
FROM python:2.7.14-alpine
RUN apk add --no-cache \
--allow-untrusted \
--repository \
http://dl-3.alpinelinux.org/alpine/edge/testing \
hdf5 \
hdf5-dev && \
apk add --no-cache \
build-base
RUN pip install --no-cache-dir --no-binary :all: tables h5py
@nvgoldin
nvgoldin / __del__.py
Last active April 25, 2020 10:44
example of __del__ drawback: order of clearing globals not promised
In [2]: # %load risk_of__del__globals_cleanup.py
...: import gc
...:
...: class GlobalCounter:
...: value = 0
...:
...:
...: class SomeKlass:
...: def __del__(self):
...: print('in SomeKlass __del__')
@nvgoldin
nvgoldin / prefer_unittest_mock.md
Created April 20, 2020 06:57
Why you should prefer to use `spec` in unittest.mock

So back to spec and why its important: Basically when you create a MagicMock, you have an object which is not bounded to any interface. This makes testing easy, but on the other hand, your code does use interfaces and they do change over time.

Here is a quick example:

Lets say I'm using a class called HashAPI which provides hashing services and exposes the following interface:

class HashAPI:
    def create_hash(self, key):
        return hash(key)
FROM python:3.9-slim-buster
WORKDIR /tor-scraper
RUN mkdir /tor-scraper/nltk_data
ENV NLTK_DATA=/tor-scraper/nltk_data
COPY nltk_data/ /tor-scraper/nltk_data/
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
#CMD [ "python3", "-c", 'nltk.corpus.stopwords.words(\"english\")' ]