Skip to content

Instantly share code, notes, and snippets.

#!/bin/bash
#######################################################################
# This is a helper script that keeps snapraid parity info in sync with
# your data and optionally verifies the parity info. Here's how it works:
# 1) Shuts down configured services
# 2) Calls diff to figure out if the parity info is out of sync.
# 3) If parity info is out of sync, AND the number of deleted or changed files exceed
# X (each configurable), it triggers an alert email and stops. (In case of
# accidental deletions, you have the opportunity to recover them from
@tadone
tadone / snippets.py
Created April 3, 2018 07:44
[YAML] Work with YAML files #python
import ruamel.yaml as yaml
with open("example.yaml") as stream:
try:
print(yaml.load(stream))
except yaml.YAMLError as exc:
print(exc)
@tadone
tadone / snippets.sh
Last active March 13, 2018 10:06
[tar --exclude] Exclude some folder
# Exclude folder from tar
$ tar --exclude='./folder' --exclude='./upload/folder2' -zcvf /backup/filename.tgz .
# Make sure to put --exclude before the source and destination items.
@tadone
tadone / virtenv.py
Created January 2, 2018 13:01
[Virtual Envirionment] Create Python3 virtual environment
# Creation of virtualenv:
python3 -m venv <desired-path>
# Activate the virtualenv:
source <desired-path>/bin/activate
# Deactivate the virtualenv:
deactivate
# Install packages with pip3
pip install Cython==0.27.3
pip install kivy
@tadone
tadone / snippets.py
Created November 22, 2017 15:01
[Read Lines from file] Strip new line character
# Open file
with open(streams_file, 'r') as f:
# Read lines and strip '\n' new line character
streams = [x.strip('\n') for x in f.readlines()] # List of streams
@tadone
tadone / snippets.py
Created November 7, 2017 08:52
[Reverse String] Reverse given string using string slicing
# This is extended slice syntax. It works by doing [begin:end:step] - by leaving begin and end off and specifying a step of -1, it reverses a string.
>>> 'hello world'[::-1]
'dlrow olleh'
@tadone
tadone / snippets.py
Last active November 7, 2017 12:38
[Return Multiple Values from Functions]
# To return multiple values from a function, simply return a tuple. For example:
>>> def myfun():
... return 1, 2, 3
...
>>> a, b, c = myfun()
>>> a
1
>>> b
2
@tadone
tadone / snippets.py
Created November 6, 2017 15:27
[Dictionary Return Values] #python
# It allows you to provide a default value if the key is missing:
dictionary.get("bogus", default_value)
# returns default_value (whatever you choose it to be), whereas
dictionary["bogus"]
# would raise a KeyError.
# If omitted, default_value is None, such that
dictionary.get("bogus") # <-- No default specified -- defaults to None
@tadone
tadone / snippets.py
Created November 2, 2017 14:00
[Exit Script] #python
# To exit a script you can use:
import sys
sys.exit()
# You can also provide an exit status value, usually an integer.
import sys
sys.exit(0)
# Exits with zero, which is generally interpreted as success. Non-zero codes are usually treated as errors. The default is to exit with zero.
import sys
@tadone
tadone / snippets.py
Created November 2, 2017 13:57
[Archive (Zip) directory] Zip a directory #python
#!/usr/bin/env python
import os
import zipfile
def zipdir(path, ziph):
# ziph is zipfile handle
for root, dirs, files in os.walk(path):
for file in files:
ziph.write(os.path.join(root, file))