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
@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()
@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) ]
@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()
#!/usr/bin/perl
use strict;
use warnings;
use HTTP::Response;
my $response = HTTP::Response->new(
200, 'OK', [ 'Content-Type' => 'multipart/form-data' ]
);
@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')])
"""
@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
@yanofsky
yanofsky / LICENSE
Last active June 5, 2024 21:51
A script to download all of a user's tweets into a csv
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
@michiexile
michiexile / gap.py
Created May 23, 2013 10:59
A Python implementation of the Gap Statistic from Tibshirani, Walther, Hastie to determine the inherent number of clusters in a dataset with k-means clustering.
# gap.py
# (c) 2013 Mikael Vejdemo-Johansson
# BSD License
#
# SciPy function to compute the gap statistic for evaluating k-means clustering.
# Gap statistic defined in
# Tibshirani, Walther, Hastie:
# Estimating the number of clusters in a data set via the gap statistic
# J. R. Statist. Soc. B (2001) 63, Part 2, pp 411-423
@davesque
davesque / patcher_test_case.py
Created August 30, 2013 20:09
setUp/tearDown patcher pattern for python unittest and python mock
import unittest
from mock import call, patch
class PatcherTestCase(unittest.TestCase):
def setUp(self):
self.patcher = patch('some_module.some_object')
self.mock_object = self.patcher.start()
@sloria
sloria / bobp-python.md
Last active May 31, 2024 07:02
A "Best of the Best Practices" (BOBP) guide to developing in Python.

The Best of the Best Practices (BOBP) Guide for Python

A "Best of the Best Practices" (BOBP) guide to developing in Python.

In General

Values

  • "Build tools for others that you want to be built for you." - Kenneth Reitz
  • "Simplicity is alway better than functionality." - Pieter Hintjens