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 / MouseDelta.py
Created October 7, 2016 17:39
Gets mouse delta as (x, y) tuple of coordinates.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from pymouse import PyMouse, PyMouseEvent
from threading import Thread
import numpy
class DetectMouseClick(PyMouseEvent):
def __init__(self):
PyMouseEvent.__init__(self)
@pykong
pykong / MultipleSubstitution.py
Last active October 10, 2016 21:37
Substitute multiple patterns in string with those in a provided dictionary.
import re
def multisub(dict, text):
# Create a regular expression from the dictionary keys
regex = re.compile("(%s)" % "|".join(map(re.escape, dict.keys())))
# For each match, look-up corresponding value in dictionary
return regex.sub(lambda mo: dict[mo.string[mo.start():mo.end()]], text)
@pykong
pykong / Paste.py
Created October 15, 2016 13:40
Example of how to paste text with pykeyboard.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from pykeyboard import PyKeyboard
k = PyKeyboard()
def paste():
# Don't do this:
# k.press_key(k.control_key)
@pykong
pykong / EasyThreading.py
Last active June 18, 2017 13:58
Very simple interface for multi-threaded execution of functions in python.
import threading
from queue import Queue
class EasyThreading(object):
"""
To Process the functions in parallel
"""
@pykong
pykong / EasyMultiProcessing.py
Created May 12, 2017 11:35
Very easy interface for multi-processed execution of functions in python.
import multiprocessing
class EasyMultiProcessing(object):
"""
To Process the functions in parallel
"""
def __init__(self,

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:

@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
@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 / 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 / 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