Skip to content

Instantly share code, notes, and snippets.

View pykong's full-sized avatar
🎯
Focusing

Ben Felder pykong

🎯
Focusing
View GitHub Profile
@pykong
pykong / DictDelta.py
Last active October 31, 2017 14:44
Get all added, deleted or modified keys. DictDelata implements this in a stateful fashion.
class DictDelta:
'''Returns a list of ḱeys, which are added, deleted or whose values have been altered compared to the dict passed in the previous call.'''
def __init__(self):
self.old_dict = None
def __call__(self, new_dict):
"""Returns list of changed keys."""
# explicitly check for None, prevent all keys being returned on 1st run
@pykong
pykong / lazy_memo.py
Last active October 22, 2017 11:44
Can be used to implement memoization as well.
def mt(a, memo=[]):
a += 1
print(a)
memo.append(a)
print(memo)
mt(1)
mt(2)
mt(3)
@pykong
pykong / multi_kwargs.py
Created October 17, 2017 20:39
Python 3.5+ allows passing multiple sets of keyword arguments ("kwargs") to a function within a single call, using the `"**"` syntax.
>>> def process_data(a, b, c, d):
>>> print(a, b, c, d)
>>> x = {'a': 1, 'b': 2}
>>> y = {'c': 3, 'd': 4}
>>> process_data(**x, **y)
1 2 3 4
>>> process_data(**x, c=23, d=42)
# To be case-insensitive, and to eliminate a potentially large else-if chain:
m.lower().endswith(('.png', '.jpg', '.jpeg'))
@pykong
pykong / dict2xml.py
Last active December 10, 2017 18:52
Recursively transform even nested dicts to xml. (This is my own creation do not confuse with the dedicated library: https://pypi.python.org/pypi/dicttoxml) Plus: Tuple to xml converter.
from xml.etree.ElementTree import Element, tostring
def dict_to_xml(tag, d):
'''
Turn a simple dict of key/value pairs into XML
'''
elem = Element(tag)
for key, val in d.items():
if type(val) == dict:
@pykong
pykong / all_equal.py
Last active October 22, 2017 09:00
Ordered those from "most Pythonic" to "least Pythonic" and "least efficient" to "most efficient". - the len(set()) solution is idiomatic, - but constructing a set is less efficient memory and speed-wise.
>>> lst = ['a', 'a', 'a']
>>> len(set(lst)) == 1
True
>>> all(x == lst[0] for x in lst)
True
>>> lst.count(lst[0]) == len(lst)
True
@pykong
pykong / pywatch.sh
Last active October 24, 2019 13:37
Execute python script on file change in a directory. Useful when coding to instantly get output from the modified code. Setup: 1) Install inotify: sudo apt-get install inotify-tools 2) Make script executable chmod +x pywatch.sh 3) Make script global
#!/bin/bash
echo watch activated
inotifywait -qmre modify . | while read f
do
python $1
done
@pykong
pykong / consumer.py
Created July 23, 2017 20:44
Example how to communicate between plugins of Sublime Text 3 via in-memory settings files. Put both python files into plugin directory, include the keybinding (maybe change different key combination if required.) Watch the console and trigger the command. See what happens. Also, see following thread: https://forum.sublimetext.com/t/inter-plugin-…
import sublime
import sublime_plugin
s = sublime.load_settings('var_share.sublime-settings')
def consume():
num = s.get("test_var")
print("Consumer received: ", num)
@pykong
pykong / set_env_var.sh
Last active May 9, 2023 12:06
Script to permanently set environment variables under Linux (Ubuntu 16.04 and later). Run as sudo. Usage: sudo set_env_var.sh key value
#!/bin/bash
# run under sudo
# script for permanently setting environment variables, found here:
# https://stackoverflow.com/questions/13046624/how-to-permanently-export-a-variable-in-linux
add_env_var()
{
KEY=$1
VALUE=$2
echo "export "$KEY"="$VALUE>>~/.bashrc

Erasing SSD

  1. Boot with a live USB stick.
  2. Get name of SSD by running lsblk
  3. Run this command:
sudo hdparm --user-master u --security-set-pass 1234 /dev/sda &&
sudo hdparm --user-master u --sanitize-crypto-scramble 1234 /dev/sda

Troubleshooting: