Skip to content

Instantly share code, notes, and snippets.

View hrchu's full-sized avatar
:octocat:
Have an Octotastic day!

petertc hrchu

:octocat:
Have an Octotastic day!
View GitHub Profile
@hrchu
hrchu / gen.py
Last active July 5, 2021 12:58
Python generator simple usage
def gen():
a = range(5)
for x in a:
yield x
x = gen() # instanlize
x.__next__() # get next
Out[36]: 0
next(x) # alternative way
@hrchu
hrchu / blink.py
Last active July 15, 2020 12:00
Blink LED on Raspberry Pi
18.04
apt install python3-pip
16.04
sudo apt-get install vim htop nmon ngrep iftop tmux dnsmasq curl ifstat iperf httpie python-virtualenv build-essential
@hrchu
hrchu / eviocgmodule.c
Last active July 7, 2020 03:01
Get input event device name via ioctl by CPython extension module
#include <Python.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mtio.h>
#include <stdio.h>
#include <stdlib.h>
#include <linux/input.h>
char* getDeviceName(char* device, char* name, size_t len)
@hrchu
hrchu / README.md
Created March 20, 2020 08:19
Daily report from systemd service/timer via email

TRY IT

  1. sudo apt-get install heirloom-mail
  2. config your email address and MTA server in send-mail.sh
  3. sudo cp send-mail.sh /opt/send-mail.sh
  4. sudo cp dummy.* /etc/systemd/system/
  5. execute sudo systemctl start dummy.service to confirm it works.
  6. sudo systemctl start dummy.timer
  7. Check the schedule via systemctl list-timers
@hrchu
hrchu / how-to.md
Last active January 24, 2020 12:47 — forked from reywood/how-to.md
How to get a stack trace from a stuck/hanging python script

How to get a stack trace for each thread in a running python script

Sometimes a python script will simply hang forever with no indication of where things went wrong. Perhaps it's polling a service that will never return a value that allows the program to move forward. Here's a way to see where the program is currently stuck.

Install gdb and pyrasite

Install gdb.

# Redhat, CentOS, etc
@hrchu
hrchu / test.py
Created November 25, 2019 06:08
Why map/filter is weaker than list comprehension or for loop iterating?
alist = [[1,1],[1,1]]
assert([1, 1] == [x for x, y in alist]) # OK
for x, y in alist: # OK
assert x == 1
assert([1, 1] == list(map(lambda x, y: x, alist))) # TypeError: <lambda>() missing 1 required positional argument: 'y'
@hrchu
hrchu / yubikey4-ssh-macos.md
Last active October 26, 2019 07:25 — forked from ixdy/yubikey4-ssh-macos.md
Setting up ssh public key authentication on macOS using a YubiKey 4

Setting up ssh public key authentication on macOS using a YubiKey 4

I largely followed Florin's blog post, but have a few notes to add regarding issues I encountered:

Basic setup notes

  1. I used a YubiKey 4, while the blog describes using a YubiKey NEO. I'm sure a YubiKey 5 would also work. I'm also running macOS 10.13.6.
  2. I installed GPGTools as recommended. However, as I'll note later, it seems that gpg-agent only automatically starts when gpg is used; for ssh, you'll need to ensure it's running.
  3. Before generating your keys, decide what key size you want to use. If you run the list command inside gpg --edit-card, look for the Key attributes line to see what is currently selected. On my YubiKey 4, it defaulted to 2048 bits for all keys:
Key attributes ...: rsa2048 rsa2048 rsa2048
@hrchu
hrchu / install_phantomjs.sh
Created May 17, 2019 05:03
install phantomjs on ubuntu 16.04.4
# install phantomjs on ubuntu 16.04.4
wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2
tar xvjf phantomjs-2.1.1-linux-x86_64.tar.bz2
sudo cp phantomjs-2.1.1-linux-x86_64/bin/phantomjs /usr/bin/
phantomjs --version
sudo cp phantomjs-2.1.1-linux-x86_64/bin/phantomjs /usr/local/bin/
@hrchu
hrchu / docker_ubuntu_install.sh
Created April 30, 2019 03:41
Install docker and docker-compose on ubuntu 16.04 in one shot.
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
sudo curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose