Skip to content

Instantly share code, notes, and snippets.

@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.

#!/bin/sh
#
# Adam Sharp
# Aug 21, 2013
#
# Usage: Add it to your PATH and `git remove-submodule path/to/submodule`.
#
# Does the inverse of `git submodule add`:
# 1) `deinit` the submodule
# 2) Remove the submodule from the index and working directory
import ctypes as c
import string
import logging; log = logging.getLogger(__name__)
# ...
def dump(obj, logger=log.info):
# assert isinstance(obj, c.C_TYPE)
B = []
C = []
offset = 0
@jn0
jn0 / fs.py
Last active September 12, 2018 13:08
Handy wrapper to [sys/proc] filesystem
#!/usr/bin/python
import os
import errno
AUTO_EXPAND_USER = True
def RootFS(root='/'): return FS(root)
def SysFS(root='/sys'): return FS(root)
def ProcFS(root='/proc'): return FS(root)