Skip to content

Instantly share code, notes, and snippets.

View congma's full-sized avatar

Cong Ma congma

View GitHub Profile
diff --git a/checkimpcont.py b/checkimpcont.py
index 3ca2d3f..46066b2 100755
--- a/checkimpcont.py
+++ b/checkimpcont.py
@@ -140,7 +140,7 @@ ST_CHECK = State("check")
ST_INIT.add_rule(IS_STR, ST_SEEK_NL, push_stack)
ST_INIT.set_default(ST_INIT)
-ST_SEEK_NL.add_rule(IS_STR, ST_SEEK_NL, compose(pop_stack, push_stack))
+ST_SEEK_NL.add_rule(IS_STR, ST_SEEK_NL, compose(use_stack, push_stack))
@congma
congma / List comprehension vs. filter.ipynb
Last active October 4, 2015 11:35
Exploring Python's list comprehension and "filter" built-in. Compare their performances.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@congma
congma / cpulist.py
Last active August 29, 2015 14:08
Dump the CPUs' physical/core/processor IDs as an ASCII-drawing of a tree.
#!/usr/bin/env python
"""Dump CPU affinity information as a tree."""
import sys
import collections
import re
import json
@congma
congma / gist:24f00de67021fdb94bc5
Created June 15, 2014 05:46
ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn
from rlyeh import cthulhu
cthulhu.fhtagn()
@congma
congma / human_enum.py
Last active December 14, 2015 18:59
Return a "human-readable" string for a list of strings that enumerates the items.
def human_enum(list_):
cleantxtlist = []
for item in list_:
word = str(item).strip()
if word:
cleantxtlist.append(word)
head = cleantxtlist[:-1]
tail = cleantxtlist[-1:]
hs = ", ".join(head)
return " and ".join([hs] + tail if hs else tail)
@congma
congma / gist:5097259
Created March 6, 2013 06:48
Quick and dirty one-liner Bash script for testing function coverage in a Python script. Given a Python script PYTHONFILE, we first grep out all first-level function names, then grep again for each function name to see how many times the name is called. The aggregated list is then sorted. Of course this is not waterproof, and should not be intend…
for f in `pcregrep -o '(?<=^def )(.+)(?=\s*\(.*\):)' PYTHONFILE`; do echo -n "$f: "; pcregrep -c `printf "%s%s%s" '(?<!def)(\s+\b' $f ')(?=\()'` PYTHONFILE; done | sort -n -t: -k2
@congma
congma / gist:5082048
Created March 4, 2013 12:49
Tip on Unix domain sockets' file permission under Linux.

Protip: If you ever want to control the permission of the Unix domain socket file generated after bind(), call fchmod() before bind()ing. This is blatant Linuxism anyway, and file permission on UDS file has no effect on BSD systems.

@congma
congma / scraper.py
Created April 19, 2012 09:51
Read two lines from file, filter by some criterion, and splash to output.
#!/usr/bin/python
import sys
import itertools
for linepair in itertools.izip_longest(*[sys.stdin]*2):
if int(linepair[0].split()[1]) >= 200:
for line in linepair:
sys.stdout.write(line)