Skip to content

Instantly share code, notes, and snippets.

@lonetwin
lonetwin / Git dot files management
Last active Oct 8, 2022
A simple way to manage dotfiles with git without silly symlinks and special tools. Just use negative matches in your .gitignore !
View Git dot files management
I like to manage dotfiles without having to mess with silly symlinks or having
to install/configure specific dotfile managament tools. So here's what I did:
$ cd ~
$ git init .
$ echo '*' > .gitignore # ignore all files by default
$ echo '!.bashrc' >> .gitignore # ...and then tell git what files not to *not* ignore
$ # ...add other files you may want to track to *not* ignore
$ git add .bashrc # now actually add the files to git
$ git add .gitignore # add the .gitignore to git
@lonetwin
lonetwin / locked_open.py
Last active Aug 9, 2022
Simple python file locking context manager example on linux using python stdlib's `fcntl.flock`
View locked_open.py
import logging
import fcntl
from contextlib import contextmanager
@contextmanager
def locked_open(filename, mode='r'):
"""locked_open(filename, mode='r') -> <open file object>
Context manager that on entry opens the path `filename`, using `mode`
(default: `r`), and applies an advisory write lock on the file which
@lonetwin
lonetwin / text_transforms.py
Created Aug 17, 2021
Transform a piece of text by appling an ordered list of string transformations to input text
View text_transforms.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
from itertools import chain
from functools import reduce, partial
def apply_substitutions(text, substitutions):
"""Apply an ordered list of string transformations to input text
@lonetwin
lonetwin / print_table.py
Last active May 7, 2021
print out ascii tables in python using data in the form: [ ('column 0 title', 'column 1 title' ..), ('row 0, column 0 data', 'row 0, column 1 data' ...) ...]
View print_table.py
# I needed to print out ascii tables from data in the form:
# [ ('column 0 title', 'column 1 title' ..),
# ('row 0, column 0 data', 'row 0, column 1 data' ...) ...]
#
# and surprisingly it got complicated because of variable lengths of the data.
# I googled for 'standard' ways of doing this and I found suggestions like:
# http://stackoverflow.com/questions/5909873/python-pretty-printing-ascii-tables
# ...which were a bit dated and hard to read or full scale modules like:
#
# http://pypi.python.org/pypi/texttable/
@lonetwin
lonetwin / pip_meta_path_finder.py
Created May 16, 2016
Auto-install missing python modules
View pip_meta_path_finder.py
import sys
import pip
from importlib import import_module
from importlib.abc import MetaPathFinder
class PipMetaPathFinder(MetaPathFinder):
"""A importlib.abc.MetaPathFinder to auto-install missing modules using pip
"""
def find_spec(fullname, path, target=None):
@lonetwin
lonetwin / custom_sftp.py
Last active Oct 4, 2020
Replacing openssh's external sftp server with a custom sftp server based on paramiko [DEPRECATED -- please see comment]
View custom_sftp.py
#!/usr/bin/env python
# A more detailed explaination will come as a blog post soon, but in brief,
# here is the problem that led to this:
#
# For various reasons we have a requirement to implement an 'sftp' like
# abstraction/interface for a service that we provide. Basically we need to
# present objects in our application as a filesystem accessible over sftp.
#
# The way we have decided to go about this is to replace the 'standard' openssh
# sftp subsystem command entry in /etc/ssh/sshd_config on our servers with our
@lonetwin
lonetwin / tracer.py
Created Apr 14, 2018
tracer.py - Quick and dirty python 'strace'
View tracer.py
#!/usr/bin/python
""" tracer.py: Quick and dirty python 'strace'
"""
import os
import os.path
import sys
import linecache
from functools import wraps
@lonetwin
lonetwin / pythonrc.py
Last active Feb 11, 2020
lonetwin's pimped-up pythonrc - Now at https://github.com/lonetwin/pythonrc
View pythonrc.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# The MIT License (MIT)
#
# Copyright (c) 2015 Steven Fernandez
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
@lonetwin
lonetwin / simple_test.py
Created Sep 6, 2017
Difference in parameterized tests with nose and nose2
View simple_test.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# With nose, this test will (incorrectly) pass
# With nose2, this test will (correctly) fail
import unittest
class TestSomething(unittest.TestCase):
def validate(self, x, y):
@lonetwin
lonetwin / random_functions.sh
Last active Apr 26, 2016
Collection of bashrc functions (and their bash completions) I find useful
View random_functions.sh
# execute an interactive python shell on a remote machine,
# over ssh, after coping my local pythonrc to that machine
# (my pythonrc available here: https://gist.github.com/lonetwin/5902720)
function rpython {
DEST='/tmp/.pythonrc.py'
scp -q $PYTHONSTARTUP $1:$DEST
ssh -t $1 -- "PYTHONSTARTUP=$DEST python"
ssh $1 "rm $DEST"
}
# - setup completion for rpython