Skip to content

Instantly share code, notes, and snippets.

@liyanage
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);
return;
View fusion-360-zoom-trackpad-delay.d
/*
sudo dtrace -q -p $(pgrep 'Autodesk Fusion 360') -s fusion-360-zoom-trackpad-delay.d
*/
BEGIN
{
eventcounter = 0;
rendercounter = 0;
eventtime = 0;
max_delay = 300;
View keybase.md

Keybase proof

I hereby claim:

  • I am liyanage on github.
  • I am liyanage (https://keybase.io/liyanage) on keybase.
  • I have a public key ASCNOzfp-gxK8wRWWMH40CjVTLzpxH_k2FMjehyCaXg46go

To claim this, I am signing this object:

@liyanage
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 http://micropython.org/download/

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

View ruby-cheat-sheet.md

Retry helper

def with_retry(retry_limit=3)
  exception = nil
  retry_limit.times do |i|
    begin
      return yield()
    rescue Exception => ex
      print "Attempt #{i + 1} of #{retry_limit} failed: #{ex}\n"
View mergesort.py
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
liyanage / gist:4627093
Created Jan 24, 2013
Bookmarklet to clean up clutter on infoq.com video viewing web pages.
View gist:4627093
javascript:$('#rightbar,%20.vendor-content-box,%20#relatedContent,%20#problemsVideo,%20#relatedResearchWidget,%20#header,%20#footer,%20#textlinks').remove()
@liyanage
liyanage / copy_items.py
Created Dec 14, 2012
Copy files or directories
View copy_items.py
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'))
else:
shutil.copy(source_path, destination_parent)
@liyanage
liyanage / repr-fallback.py
Created Dec 1, 2012
__repr__ implementation handling non-ascii characters.
View repr-fallback.py
class Foo:
def __unicode__(self):
return u'xx \u2192 xx'
def __repr__(self):
return unicode(self).encode('ascii', 'backslashreplace')
foo = Foo()
@liyanage
liyanage / email-test.py
Created Nov 18, 2012
Python email sending example
View email-test.py
#!/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 <test@example.com>'
you = me