Skip to content

Instantly share code, notes, and snippets.

View zmwangx's full-sized avatar
🐍
Pythonista learning new technologies

Zhiming Wang zmwangx

🐍
Pythonista learning new technologies
View GitHub Profile
@zmwangx
zmwangx / literalize_regex.sh
Created May 28, 2014 08:01
Regex: escape all meta characters in a string. Keywords: escape, literal, meta, regex.
# this is experimental, assuming the metacharacters are ^[.${*(\+)|?
gsed -e 's/[\^\[\.\$\{\*\(\\\+\)\|\?]/\\&/g'
@zmwangx
zmwangx / unix_system_mail.md
Created May 29, 2014 06:27
A little tutorial (more accurately, several basic tips) on UNIX system mail. This is a clone of the post http://kevinsayhiiiiiiiii.tumblr.com/post/87179246018/05-28-2014-getting-started-with-unix-system-mail. (That's my blog, btw.)

BSD periodic can no longer satisfy my needs, so I switched to Vixie cron last week, and also got started with system mail. (Of course periodic can also mail results, but I never took advantage of that.) I'll list the things I've learned about mail thus far.

Warning: Here are things I've learnt on OS X 10.9. YMMV on other systems.

  1. It's easy to let cron email output to you. Add to your crontab

     MAILTO=torvalds,torvalds@klaava.helsinki.fi
    

    As in the above example, you can either specify a local user, e.g., torvalds, or an actual email address, torvalds@klaava.helsinki.fi. Do separate email addresses with a comma. Do not add an extra space. See man 5 crontab, though you might not find the tip above.

@zmwangx
zmwangx / rule30.py
Last active January 5, 2021 14:26
Stephen Wolfram Rule 30 cellular automaton emulation in python, with the simplest initial state of exactly one filled cell. (UPDATE: I've published a much more efficient and refined package with builtin visualization: https://github.com/zmwangx/rule30)
import sys
MAX_TIME = int(sys.argv[1])
HALF_SIZE = MAX_TIME
indices = range(-HALF_SIZE, HALF_SIZE+1)
# initial condition
cells = {i: '0' for i in indices}
cells[0] = '1'
# padding on both ends
@zmwangx
zmwangx / emacs-pb.el
Last active August 29, 2015 14:02
Emacs: make kill ring and system pasteboard function more reasonably in window systems.
;;; Tested on:
;;; 1. GNU Emacs 24.3.1 (x86_64-apple-darwin13.0.0)
;;; of 2013-12-22 on tennine-slave.macports.org
;;; (MacPorts emacs@24.3_1)
;;;
;;; 2. GNU Emacs 24.3.1 (x86_64-apple-darwin, NS apple-appkit-1038.36)
;;; of 2013-03-12 on bob.porkrind.org
;;; (Emacs For Mac OS X)
(defun isolate-kill-ring()
@zmwangx
zmwangx / uuniq
Created July 1, 2014 18:16
Shell script: unsorted uniq via awk (uniq without sorting first).
#!/bin/bash
# remove duplicates without pre-sorting
# `uuniq' stands for unsorted uniq
awk '!x[$0]++'
@zmwangx
zmwangx / html-entity-decode
Created July 1, 2014 19:25
Shell script: decode html entities via php.
#!/bin/bash
# The ENT_QUOTES flag converts both double and single quotes.
# See http://www.php.net/manual/en/function.html-entity-decode.php.
php -R 'echo html_entity_decode($argn, ENT_QUOTES)."\n";'
@zmwangx
zmwangx / test-newline.sh
Created July 4, 2014 16:37
Bash: test for newline in a string.
#!/bin/bash
# Use pattern matching: http://goo.gl/q6qNco.
# The newline in the pattern string needs to be quoted.
# See ANSI-C quoting: http://goo.gl/I3HZTR
string=$(echo -e 'hello\nworld')
if [[ ${string} == *$'\n'* ]]; then
echo 'newline detected' >&2
else
@zmwangx
zmwangx / ports.txt
Last active August 29, 2015 14:04
MacPorts: my list of installed ports. Auto resolved deps are not included.
bison
bzr
coreutils
dos2unix
emacs
fdupes
ffmpeg +gpl2+nonfree
gawk
gcc48
getopt
@zmwangx
zmwangx / hide-trash.md
Last active October 2, 2017 07:29
Toward a minimalistic Dock experience — hiding the Trash icon.

How to hide the trash icon on OS X Dock (OS X 10.9)

1. Make the trash icon hideable

Add the following to /System/Library/CoreServices/Dock.app/Contents/Resources/DockMenus.plist under the trash array:

<dict>
    <key>command</key>
    <integer>1004</integer>

name

@zmwangx
zmwangx / Postfix: relay to authenticated SMTP.md
Created August 3, 2014 18:28
Postfix: relay to authenticated SMTP.
  1. Taking Gmail as an example, put the following into /etc/postfix/main.cf.

     relayhost = [smtp.gmail.com]:587
     smtp_sasl_auth_enable = yes
     smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
     smtp_sasl_security_options = noanonymous
     smtp_sasl_mechanism_filter = plain
     smtp_tls_CAfile = /etc/pki/tls/certs/ca-bundle.crt
     smtp_use_tls = yes
    

smtp_tls_security_level = encrypt