Skip to content

Instantly share code, notes, and snippets.

@jn0
jn0 / tzsex.py
Last active May 23, 2023 07:52
Sex with times and zones
# first off, there is *no* *good* builtin TZ handling in Python.
# No. Nothing. At all.
# you have to `pip install pytz pytzdata tzlocal` to survive...
# maybe.
# he-he.
import datetime
import tzlocal
right_now_and_here = datetime.datetime.now().replace(tzinfo=tzlocal.get_localzone())
@jn0
jn0 / ssh.py
Created August 9, 2017 15:44
Custom paramiko-based python SSH client class to asynchronously run time-limited non-interactive commands and return exit status along with stdout and stderr.
#!/usr/bin/python
# -*- coding: UTF-8 -*-
'''
Custom SSH client.
1. It somehow _supports_ async execution.
2. It provides exit status.
3. It jams stdin (ssh -nT).
Run like something "python ssh.py box user=bs command='echo aaa; echo bbb >&2; sleep 40; exit 23'" to test
@jn0
jn0 / Makefile
Created August 9, 2017 16:11
Makefile to compile a Python module into an .so binary.
.SUFFIXES: # Delete the default suffixes
.SUFFIXES: .c .o .py .so
.py.c:
cython -2 $<
.c.o:
gcc -pthread -Wno-unused-result -DNDEBUG -g -fwrapv -O3 \
-Wall -Wstrict-prototypes -Wstrict-aliasing \
-fPIC -I/usr/include/python2.7/ -c $< -o $@
@jn0
jn0 / README.md
Last active August 16, 2017 14:35
how to build a binary version of a Python package and make it pure binary

How to build a binary version of a pure Python package and make it pure binary

The package

Let we have a package that looks this way:

  • mext/ package directory
    • __init__.py package "constructor" module
    • m1.py package member module #1
  • m2.py package member module #2
@jn0
jn0 / only.py
Created September 1, 2017 08:05
Host-wide exclusive lock without any file use (instead of flock -x $0 $0 "$@")
#!/usr/bin/python
# a tool to allow only one instance of the command to be ran at once
# host-wide exclusive lock
# no lock- or pid- files, never-used-port is "locked" instead
# written after http://timkay.com/solo/ idea
THE_POINT_TO_LOCK = ("127.1.2.3", 9000)
import sys
import os
@jn0
jn0 / i18n.py
Last active October 19, 2017 08:17
Sample i18n Python module for a project
#!/usr/bin/python
'''
i18n.py
Use `from my_project.i18n import _` in source files to be localized.
Inspired by https://www.mattlayman.com/2015/i18n.html
'''
APP_NAME = 'cluster_sim'
@jn0
jn0 / HashableDict.py
Last active November 8, 2017 08:15
Sometimes you want a better `dict` in Python...
import __builtin__
class HashableDict(__builtin__.dict):
'''and with .attr capability'''
def __init__(self, *av, **kw):
return __builtin__.dict.__init__(self, *av, **kw)
@staticmethod
def dict__hash(d):
@jn0
jn0 / handyArgumentParser.py
Created November 8, 2017 08:20
`argparse.ArgumentParser` is great, but sometimes you want a more handy one
import argparse
class AP(argparse.ArgumentParser):
def add(self, *av, **kw):
if 'default' not in kw:
if kw.get('action') == 'count':
kw['default'] = 0
self.add_argument(*av, **kw)
return self
#end class AP
@jn0
jn0 / GitConfigParser.py
Created November 8, 2017 08:26
Wanna parse ini-style ./.git/config and failed? Well...
import os
try:
import ConfigParser
except ImportError: # python3?
import configparser as ConfigParser
dot_git_config = os.path.join('.git', 'config')
class GitConfigParser(ConfigParser.RawConfigParser):
def _read(self, fp, filename=None):
@jn0
jn0 / fixsvg.md
Last active November 24, 2017 10:13
Generated SVG optimizer

Fix SVG Utility

The fixsvg.py is intended to flatten an SVG generated by a converter (in my case - from Acme CAD Converter, which "Converts DWG, DXF, DWF to SVG", ran on an AutoCAD drawing).

My savings was 65%: 2MB of converted SVG versus 700KB of "fixed" one.

Converters often leave a lot of extraneous stuff in the SVG pathes like forced Move before any Line as well as single path splet over several tags.