Skip to content

Instantly share code, notes, and snippets.

liyanage / enumerate-lines.m
Created Nov 13, 2019
Enumerate lines in a large file with memory mapping through NSData
View enumerate-lines.m
#import <Foundation/Foundation.h>
typedef void(^LineEnumerator)(NSData *lineData, BOOL *stop);
void enumerateLinesInFileAtURL(NSURL *url, LineEnumerator lineEnumerator) {
NSError *error = nil;
NSData *data = [NSData dataWithContentsOfURL:url options:NSDataReadingMappedAlways error:&error];
if (!data) {
NSLog(@"Unable to open: %@", error);
View fusion-360-zoom-trackpad-delay.d
sudo dtrace -q -p $(pgrep 'Autodesk Fusion 360') -s fusion-360-zoom-trackpad-delay.d
eventcounter = 0;
rendercounter = 0;
eventtime = 0;
max_delay = 300;

Keybase proof

I hereby claim:

  • I am liyanage on github.
  • I am liyanage ( on keybase.
  • I have a public key ASCNOzfp-gxK8wRWWMH40CjVTLzpxH_k2FMjehyCaXg46go

To claim this, I am signing this object:

liyanage / gist:6f6379d1f7bffb2bbe1c
Created Oct 17, 2014
MicroPython firmware update via DFU on OS X
View gist:6f6379d1f7bffb2bbe1c

brew install dfu-util

Download new firmware from

Connect pins DFU and 3.3v, press reset button

dfu-util -l should list entries with "Found DFU: "

dfu-util --alt 0 --device 0483:df11 -D ~/Desktop/pybv10-2014-10-17-v1.3.3-86-g37ada23.dfu


Retry helper

def with_retry(retry_limit=3)
  exception = nil
  retry_limit.times do |i|
      return yield()
    rescue Exception => ex
      print "Attempt #{i + 1} of #{retry_limit} failed: #{ex}\n"
def merge(left, right):
nl = len(left)
nr = len(right)
merged = []
il = 0
ir = 0
while il < nl and ir < nr:
l = left[il]
r = right[ir]
liyanage / gist:4627093
Created Jan 24, 2013
Bookmarklet to clean up clutter on video viewing web pages.
View gist:4627093
liyanage /
Created Dec 14, 2012
Copy files or directories
import shutil
import os
def copy_item(source_path, destination_parent):
if os.path.isdir(source_path):
destination_path = os.path.join(destination_parent, os.path.basename(source_path))
shutil.copytree(source_path, destination_path, ignore=shutil.ignore_patterns('.DS_Store', '*.pyc'))
shutil.copy(source_path, destination_parent)
liyanage /
Created Dec 1, 2012
__repr__ implementation handling non-ascii characters.
class Foo:
def __unicode__(self):
return u'xx \u2192 xx'
def __repr__(self):
return unicode(self).encode('ascii', 'backslashreplace')
foo = Foo()
liyanage /
Created Nov 18, 2012
Python email sending example
#!/usr/bin/env python
import email.mime.text
import email.header
import smtplib
email.Charset.add_charset('utf-8', email.Charset.QP, email.Charset.QP, 'utf-8')
me = u'Foo Bar <>'
you = me