This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import wraps | |
from threading import Condition | |
""" | |
Usage: | |
cond = Condition() | |
Acquire the condition, wait for it, then do something: | |
@WaitOn(cond) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/Library/Frameworks/Python.framework/Versions/3.4/bin/python3 | |
""" | |
Synchronously call an asyncio task/coroutine from a non-asyncio thread. | |
usage: | |
# block the current thread until the coroutine yields or returns | |
value = sync_call(loop, coroutine, 1, 2, 3, four=4, five=5) | |
# control returns here when the coroutine is done | |
""" | |
import asyncio |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def multifilter(seq, *tests, multiple=False): | |
"""Filters items in a list into sublists corresponding to the | |
supplied test functions. | |
:returns: A list of lists of items matching conditions, in order. The last | |
item in the return value is the list of items that did not | |
match any condition. | |
[[items matching condition0], | |
[items matching condition1], |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from django.core.wsgi import get_wsgi_application | |
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "your-project.settings") | |
application = get_wsgi_application() | |
try: | |
import uwsgi | |
from uwsgidecorators import timer | |
from django.utils import autoreload |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Shell script to run rdiff-backup. | |
# Put this in your crontab to run this every hour: | |
# 0 * * * * /home/seth/root/bin/backup.sh | |
# Change these locations to suit | |
BACKUP_DIR=/opt/backup/$HOSTNAME | |
BACKUP_LOG=$HOME/backup.log | |
savelog -n -c 7 "$BACKUP_LOG" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a = 55 | |
b = 99 | |
print(a) | |
print(a & b, hex(a & b), bin(a & b)) | |
print(a | b, hex(a | b), bin(a | b)) | |
print(a ^ b, hex(a ^ b), bin(a ^ b)) | |
a = 'abcdefgh'[:2] | |
x = 0x7F7F |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
To run: | |
$ python3.7 -m venv txaio-test | |
$ . txaio-test/bin/activate | |
(txaio-test) $ pip install txaio | |
Collecting txaio | |
Downloading https://files.pythonhosted.org/packages/4f/82/0cd8d81d57e55a598cd4cef10c6e971dbcaf437e4f138dc1624cf7c1388e/txaio-20.4.1-py2.py3-none-any.whl | |
Installing collected packages: txaio | |
Successfully installed txaio-20.4.1 | |
(txaio-test) $ python txaio-test.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# SRH 17-Mar-2021 | |
""" | |
Improved os.path.splitext which properly deals with files containing multiple | |
dots in extensions. | |
""" | |
import os | |
def better_splitext(name): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
def utcnow(): | |
"""Return a timezone-aware now, in UTC. | |
""" | |
return datetime.datetime.now(datetime.timezone.utc) | |
def tznow(): | |
"""Return a timezone-aware now, in the local timezone. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Program to test for a race condition in rostopic handlers | |
# Run (note the single & to put the first pub in the background and simultaneously start the second) | |
# $ rostopic pub -1 /test_topic1 std_msgs/String "data: 'hello1'" & rostopic pub -1 /test_topic2 std_msgs/String "data: 'hello2'" | |
# Both topic handlers should fire nearly simultaenously. | |
# Each topic reads the global variable, then increments it by 1. | |
# When one thread increments the GLOBAL from 1 to 3, you have a race condition. | |
import rospy | |
from std_msgs.msg import String | |
import threading |