Skip to content

Instantly share code, notes, and snippets.

@wrenoud
wrenoud / Select Line.py
Created November 9, 2012 20:45
Select Line
import editor
text = editor.get_text()
selection = editor.get_line_selection()
selected_text = text[selection[0]:selection[1]]
editor.set_selection(selection[0], selection[0] + len(selected_text) - 1)
@wrenoud
wrenoud / Delete Line.py
Created November 9, 2012 20:50
Delete Line
import editor
selection = editor.get_line_selection()
editor.replace_text(selection[0], selection[1], '')
@wrenoud
wrenoud / DropboxSync.py
Created November 10, 2012 02:46
DropboxSync
import os
import sys
import pickle
import console
# I moved 'dropboxlogin' into a sub folder so it doesn't clutter my main folder
sys.path += [os.path.join(os.path.dirname(os.path.abspath(__file__)), 'lib')]
import dropboxlogin # this code can be found here https://gist.github.com/4034526
STATE_FILE = '.dropbox_state'
@wrenoud
wrenoud / file_browser.py
Created November 10, 2012 03:26
Pythonista file browser using scene module. Latest change adds delete functionality.
import os
import sys
sys.path += [os.path.join(
os.path.dirname(
os.path.abspath(__file__)), 'lib')]
# Pythonista Modules
import console
from scene import *
@wrenoud
wrenoud / debug.js
Last active December 22, 2015 17:59
Used to print debug message in node.js that include filename and line number.
// Modified from code found in the following stackoverflow answer:
// http://stackoverflow.com/questions/14172455/get-name-and-line-of-calling-function-in-node-js
Object.defineProperty(global, '__stack', {
get: function(){
var orig = Error.prepareStackTrace;
Error.prepareStackTrace = function(_, stack){ return stack; };
var err = new Error;
Error.captureStackTrace(err, arguments.callee);
var stack = err.stack;
@wrenoud
wrenoud / countTo10.py
Last active December 25, 2015 18:59
Pointless recursion.
def countTo10_recursively(count = 0):
if(count <= 10):
countTo10_recursively(count + 1)
# which is the same as
def countTo10_theObviousWay():
for count in xrange(10):
pass
# lets do some tests
@wrenoud
wrenoud / enum_test.py
Created December 6, 2013 22:40
I was curious if there was any efficiency difference between using enumerate() and counting for myself. It appears that enumerate() might be slightly faster.
def enum_the_old_way():
i = 0
for j in myrange:
i += 1
def enum_the_new_way():
for i,j in enumerate(myrange):
pass
myrange = range(10000)
@wrenoud
wrenoud / hexdump.py
Last active August 29, 2015 14:01
Prints a formatted hexdump of a binary string
#!/usr/bin/python
"""CLI USAGE: hexdump [-w n] [-ws b] <filename>
-w --words the number of words to display on a line
-ws --word-size the number of bytes to display in a word
"""
import __future__
from StringIO import StringIO
import math
@wrenoud
wrenoud / datagram_reader.py
Last active August 29, 2015 14:02
Buffered datagram reader
from collections import deque
import struct
import sys
import io
class datagram_reader:
"""Buffered datagram reader
Accepts any stream that supports read() method
"""
@wrenoud
wrenoud / timehandler.py
Last active August 29, 2015 14:04
Convert different time formats to datetimes or timestamps (comes with tests)
import calendar
from datetime import datetime, timedelta, tzinfo
from math import floor
def seconds(day=0.0,hour=0.0,minute=0.0,second=0.0,microsecond=0.0):
return ((day*24.0 + hour)*60.0 + minute)*60.0 + second + microsecond/1000000.0
def minutes(day=0.0,hour=0.0,minute=0.0,second=0.0,microsecond=0.0):
return (day*24.0 + hour)*60.0 + minute + (second + microsecond/1000000.0)/60.0