Skip to content

Instantly share code, notes, and snippets.

View metatoaster's full-sized avatar

Tommy Yu metatoaster

  • Auckland Bioengineering Institute, University of Auckland
  • Auckland, New Zealand
View GitHub Profile
@metatoaster
metatoaster / mkpypi.release.sh
Created September 7, 2016 07:43
Sick of forgetting to clean dir before running bdist_wheel/sdist
#!/bin/sh
PYSRC=/usr/bin/python3.4
TMPROOT=/tmp
P=$(readlink -f "$1")
REPO=${P}/.git
if [ ! -d $REPO ]; then
echo "Need a repo"
exit
# In Python 2, no way to distinguish which got the -v flag?
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from argparse import ArgumentParser
>>> parser = ArgumentParser()
>>> pa = parser.add_argument('-v', action='count', default=0)
>>> commands = parser.add_subparsers(dest='command', metavar='<command>')
>>> cmd1 = commands.add_parser('cmd1')
@metatoaster
metatoaster / test_stdio.py
Last active September 19, 2021 13:39
Simple Python unittest for mocking/stubbing sys.stdin and friends using native libs.
# -*- coding: utf-8 -*-
# To use, at the directory you saved this:
# python -m unittest test_stdio
import sys
import io
import unittest
def stub_stdin(testcase_inst, inputs):
return chain(
[(
'/'.join(module_frags + mod_path.split(sep))[:-len(JS_EXT)],
module_base_path,
mod_path,
) for mod_path in (
relpath(path, module_base_path)
for path in glob(join(module_base_path, '*' + fext))
)]
for module_base_path in module_base_paths
@metatoaster
metatoaster / test_exportimport.py
Last active November 16, 2015 03:25
Test can be added to plone.app.registry to show schema validation on broken plone.supermodel
Failure in test test_import_list_choice_field_vocabulary_usage (plone.app.registry.tests.test_exportimport.TestImport)
Traceback (most recent call last):
...
File ".../plone.app.registry/plone/app/registry/tests/test_exportimport.py", line 1073, in test_import_list_choice_field_vocabulary_usage
self.registry['test.registry.field'] = [u'One', u'Three']
...
AssertionError: WrongContainedType not raised
@metatoaster
metatoaster / python_closure.md
Last active January 19, 2023 23:37
Python closure fun

This is most certainly a bad idea, but what you want to do can be done in some extent, and this is going to take a lot of time to explain. First off, rather than thinking of decorators as a syntax sugar, think of them as what they really are: a function (that is a closure) with a function that exist inside it. Now this is out of the way, supposed we have a function:

def operation(a, b):
    print('doing operation')
    return a + b

Simply it will do this

>>> hi = operation('hello', 'world')

doing operation

@metatoaster
metatoaster / mod1.py
Created October 9, 2015 02:34
Python closures are funny
def decorator(arg1, arg2_=None):
def d(f):
arg2 = arg2_
if arg2 is None:
arg2 = 'default arg2'
print('arg1 = %s' % arg1)
print('arg2 = %s' % arg2)
@metatoaster
metatoaster / gist:322b84289f2be660f6f2
Created November 27, 2014 11:02
What the fuck is this shit?
top's Config File (Linux processes with windows)
Id:i, Mode_altscr=0, Mode_irixps=1, Delay_time=1.500, Curwin=0
Def fieldscur=¥&K¨³´»½@·º¹56ÄFÅ')*+,-./0128<>?ABCGHIJLMNOPQRSTUVWXYZ[\]^_`abcdefghij
winflags=192564, sortindx=18, maxtasks=0, graph_cpus=0, graph_mems=0
summclr=1, msgsclr=1, headclr=3, taskclr=1
Job fieldscur=¥¦¹·º(³´Ä»½@<§Å)*+,-./012568>?ABCFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghij
winflags=163124, sortindx=0, maxtasks=0, graph_cpus=2, graph_mems=0
summclr=6, msgsclr=6, headclr=7, taskclr=6
Mem fieldscur=¥º»<½¾¿ÀÁMBNÃD34·Å&'()*+,-./0125689FGHIJKLOPQRSTUVWXYZ[\]^_`abcdefghij
winflags=163124, sortindx=21, maxtasks=0, graph_cpus=2, graph_mems=0
@metatoaster
metatoaster / bootstrap.py
Last active August 29, 2015 14:05
CellML API buildout with cgrspy
##############################################################################
#
# Copyright (c) 2006 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
import inspect
import functools
def arg2attr(clsins_init):
@functools.wraps(clsins_init)
def initattrs(self, *a, **kw):
# build default dict from default args, in reverse order due to
# the definition syntax is designed.