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 / 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,
@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 / 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)
# To be case-insensitive, and to eliminate a potentially large else-if chain:
m.lower().endswith(('.png', '.jpg', '.jpeg'))
@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)
@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 / 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)