Skip to content

Instantly share code, notes, and snippets.

View mkaranasou's full-sized avatar
🏠
Working from home

Maria Karanasou mkaranasou

🏠
Working from home
View GitHub Profile
@gsakkis
gsakkis / render_query.py
Created January 19, 2013 11:25
Generate an SQL expression string with bound parameters rendered inline for the given SQLAlchemy statement.
from datetime import datetime, date
from sqlalchemy.orm.query import Query
def render_query(statement, bind=None):
"""
Generate an SQL expression string with bound parameters rendered inline
for the given SQLAlchemy statement.
WARNING: This method of escaping is insecure, incomplete, and for debugging
purposes only. Executing SQL statements with inline-rendered user values is
@albarrentine
albarrentine / gist:2577076
Created May 2, 2012 14:44
numpy.fromiter using a custom generator and data type
import numpy
# Imagine these come from a db cursor or something
coordinates = [(1,2,3), (4,5,6), (7,8,9)]
def my_gen(some_tuple):
for x, y, z in some_tuple:
yield x, y, z
a = numpy.fromiter(my_gen(coordinates), dtype=[('x', 'l'), ('y', 'l'), ('z', 'l')])
"""
#!/usr/bin/perl
use strict;
use warnings;
use HTTP::Response;
my $response = HTTP::Response->new(
200, 'OK', [ 'Content-Type' => 'multipart/form-data' ]
);
@pklaus
pklaus / print_contacts_from_address-book.py
Created June 16, 2011 18:24
How to access the Mac OS X Address Book from Python: <http://www.programmish.com/?p=26>
import objc
import AddressBook as ab
import pprint as pp
def pythonize(objc_obj):
if isinstance(objc_obj, objc.pyobjc_unicode):
return unicode(objc_obj)
elif isinstance(objc_obj, ab.NSDate):
return objc_obj.description()
@uogbuji
uogbuji / gruber_urlintext.py
Created November 18, 2010 18:28
John Gruber's regex to find URLs in plain text, converted to Python/Unicode
#See: http://daringfireball.net/2010/07/improved_regex_for_matching_urls
import re, urllib
GRUBER_URLINTEXT_PAT = re.compile(ur'(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?\xab\xbb\u201c\u201d\u2018\u2019]))')
for line in urllib.urlopen("http://daringfireball.net/misc/2010/07/url-matching-regex-test-data.text"):
print [ mgroups[0] for mgroups in GRUBER_URLINTEXT_PAT.findall(line) ]
@Eckankar
Eckankar / MarkovWord.py
Created April 8, 2010 15:56 — forked from agiliq/gist:131679
Generate random words based on markov chains rather than random sentences.
#!/usr/bin/env python
import random
class Markov:
def __init__(self, file, size):
self.size = size
self.starts = []
self.cache = {}
self.file_to_words(file)
self.parse_words()