Skip to content

Instantly share code, notes, and snippets.

View sethrh's full-sized avatar

Seth Hill sethrh

  • Portland, OR
View GitHub Profile
@sethrh
sethrh / ros_race_condition.py
Created February 7, 2023 04:48
Program to test for a race condition in rostopic handlers
# 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
@sethrh
sethrh / time tricks.py
Created November 23, 2022 20:23
Get the current time with timezone-aware datetime objects. These are two utility functions to get the current time in a timezone-aware datetime in UTC or the local timezone. Python 3.9+ has finally started to get smarter about timezones. BUT, it's still a fairly arcane process to get the current time with a timezone.
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.
@sethrh
sethrh / better_splitext.py
Last active March 22, 2021 17:44
better_splitext.py
# SRH 17-Mar-2021
"""
Improved os.path.splitext which properly deals with files containing multiple
dots in extensions.
"""
import os
def better_splitext(name):
@sethrh
sethrh / txaio-test.py
Created April 2, 2020 18:53
txaio.aio logging bug example
'''
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
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
@sethrh
sethrh / backup.sh
Created July 13, 2017 20:48
Rdiff-backup shell script.
#!/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"
@sethrh
sethrh / wsgi.py
Created October 17, 2016 20:51
UWSGI + Django, reload on code change
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
@sethrh
sethrh / multifilter.py
Last active September 20, 2016 23:13
Function to simultaneously partition a list into multiple sub-lists based on used-defined criteria. Kind of like a list comprehension with multiple return values.
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],
@sethrh
sethrh / sync_call.py
Created May 29, 2014 00:28
Trick to synchronously call a coroutine from a non-asyncio-aware legacy code. The calling thread can treat the coroutine as a regular (synchronous) function call.
#!/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
@sethrh
sethrh / Condition Decorators
Created May 28, 2014 17:53
Decorator / Context manager to wait and notify on conditions
from functools import wraps
from threading import Condition
"""
Usage:
cond = Condition()
Acquire the condition, wait for it, then do something:
@WaitOn(cond)