Skip to content

Instantly share code, notes, and snippets.

View dutc's full-sized avatar

James Powell dutc

View GitHub Profile
# only works for kwargs
class autoinit(type):
def __call__(self, *args, **kwargs):
rv = super().__call__(*args, **kwargs)
for k,v in rv.__init__.__annotations__.items():
setattr(rv, k, eval(v, kwargs))
return rv
class Foo(metaclass=autoinit):
def __init__(self, x: 'x + y', y: 'y * 10', z):
cut -d ';' -f 2- ~/.zsh_history | \
tr '|;' '\n' | \
FS=' ' awk '{ print $1 }' | \
cut -d '/' -f -1 | \
awk '/^[a-zA-Z]/ { cmds[$0] += 1 } END { for (cmd in cmds) print cmd, cmds[cmd] }' | \
awk '{ first = tolower(substr($1,1,1)); count = int($2); if (max[first] < count) { max[first] = count; cmds[first] = $1 } } END { for (f in cmds) print f, cmds[f], max[f] }' | \
sort

Linux可執行文件ㄉ內容分析工具

NOTE: This is a working copy. This tutorial is unfinished and may contain inaccuracies.

I've written the title of this tutorial in Chinese, as I suspect that its contents may, at first glance, appear similarly incomprehensible to the audience.

However, just as I can sketch for you the following...

可執行文件 = (可 = can) + (執行 = execute) + (文件 = file) = executable (file)

@dutc
dutc / generators-free-your-mind.ipynb
Last active November 5, 2024 08:30
IPython Notebook for "Generators Will Free Your Mind"
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@dutc
dutc / answer.py
Created May 13, 2014 19:38
itertools question
from itertools import cycle, chain
xs = [1,2,3]
ys = [4,5,6]
for x,y in chain(zip(cycle([2]), xs), zip(cycle([9]), ys)):
print(x+y)
@dutc
dutc / dinky.py
Last active August 29, 2015 14:02
dinky proxy
from functools import update_wrapper
from collections import Callable
from logging import info, basicConfig, INFO
basicConfig(level=INFO)
def dec(name, obj, meth):
def wrapped(*args, **kwargs):
rv = meth(*args, **kwargs)
info('%r:0x%x:%s.%r(*%r, **%r) -> %r', obj, id(obj), name, meth, args, kwargs, rv)
@dutc
dutc / circbuf.py
Created June 21, 2014 02:15
dinky circular buffer
from collections import deque
class CircularBuffer(deque):
def __init__(self, *args, **kwargs):
self.size = kwargs.pop('size', 3)
super(CircularBuffer, self).__init__(*args, **kwargs)
def append(self, item):
while len(self) >= self.size:
self.popleft()
super(CircularBuffer, self).append(item)
def cumsum(xs):
xs = iter(xs)
rv = [next(xs)]
for x in xs:
rv.append(rv[-1] + x)
return rv
def cumsum(xs):
tot = 0
for x in xs:
@dutc
dutc / constraint.py
Last active August 29, 2015 14:04
simple module import time constraint
class BaseMeta(type):
def __new__(cls, name, bases, dict):
if 'foo' not in dict:
raise RuntimeError('class must have its own foo method!')
return type.__new__(cls, name, bases, dict)
class Base(metaclass=BaseMeta):
def foo(self): pass
class Derived(Base):
@dutc
dutc / customtypesystem.py
Created July 20, 2014 04:16
custom type system using __instancecheck__
class FoobarMeta(type):
def __instancecheck__(self, obj):
return hasattr(obj, 'foo') and hasattr(obj, 'bar')
class Foobar(metaclass=FoobarMeta):
pass
class A:
def foo(self):
pass