Skip to content

Instantly share code, notes, and snippets.

View justinmklam's full-sized avatar

Justin Lam justinmklam

View GitHub Profile
@justinmklam
justinmklam / linux-email-ssmtp-configuration.md
Created August 26, 2020 22:11
Set up email from a linux server
def exception_handler(func):
def inner_function(*args, **kwargs):
try:
func(*args, **kwargs)
except TypeError:
print(f"{func.__name__} only takes numbers as the argument")
return inner_function
@exception_handler
#!/usr/bin/python3
class ReadLine:
def __init__(self, s):
self.buf = bytearray()
self.s = s
def readline(self):
i = self.buf.find(b"\n")
if i >= 0:
r = self.buf[:i+1]
@justinmklam
justinmklam / include-data-file-python-package.md
Last active June 4, 2020 20:22
Include data files in python package

Add MANIFEST.in, which includes:

include path/to/yourfile.csv

And then in setup() inside setup.py, include:

include_package_data=True
@justinmklam
justinmklam / monorepo.sh
Created May 26, 2020 23:32
Script to combine repositories while preserving history. From http://choly.ca/post/git-merge-to-monorepo/
#!/bin/bash
# This script takes a remote repository and merges it into
# the current one as a subdirectory
set -e
if [ -z "$1" ]
then
echo "Usage:"
@justinmklam
justinmklam / ssh-reverse-tunneling.sh
Last active May 19, 2020 18:56
Easily expose localhost to the public via http://serveo.net/
# Expose localhost at port 5000 to a random subdomain on serveo.net
ssh -R 80:localhost:5000 serveo.net
# Request a specific subdomain on serveo.net (ie. my-custom-app.serveo.net)
ssh -R my-custom-app:80:localhost:5000 serveo.net
# SSH session will close if no connections are made, so autossh will automatically reconnect and keep it persistent
sudo apt install autossh
autossh -M 0 -R my-custom-app:80:localhost:5000 serveo.net
@justinmklam
justinmklam / linux-fix-apt-update.md
Created May 9, 2020 17:37
Fix release not found in repository when running sudo apt update

If running update on a no longer supported Ubuntu version (ie. 19.10):

justin@pop-os:~$ sudo apt update
Ign:1 http://dl.google.com/linux/chrome/deb stable InRelease
Hit:2 http://apt.pop-os.org/proprietary disco InRelease                                                           
Get:3 http://repository.spotify.com stable InRelease [3,316 B]                                                    
Hit:4 http://dl.google.com/linux/chrome/deb stable Release                                                        
Hit:5 http://packages.microsoft.com/repos/vscode stable InRelease                                                 
Ign:6 http://archive.ubuntu.com/ubuntu disco InRelease                                                            
@justinmklam
justinmklam / docker-cross-compile-arm.md
Last active September 18, 2020 16:44
Instructions to cross compile applications for target arm architecture.

The steps below provide a way to cross compile on your local development machine. Docker and QEMU to emulate the armhf/arm32v7 architecture.

Tested on:

  • Ubuntu 18.04 LTS

Prerequisites

Install dependencies on your local development machine:

@justinmklam
justinmklam / screen.sh
Last active April 24, 2020 20:10
Cheat sheet for launching detached screens (ie. via crontab)
# Starts a detached screen and logs to a datestamped log file. '%' characters only need to be escaped if running this command via cron
screen -L -Logfile ~/my/log/path/$(date +\%Y-\%m-\%d_\%H-\%M-\%S).log -dm <command>
# Execute multiple commands in a detached screen session
screen -dm /bin/bash -c "command1 | command 2"
@justinmklam
justinmklam / emailer.py
Last active April 6, 2020 05:57
Send emails from Python (using a gmail account over SMTP). Requires enable access for less secure apps here: https://myaccount.google.com/lesssecureapps
"""
Emailer
==========
Sends automated emails. Can send as HTML or plain text.
Example Usage:
.. code-block:: python