Skip to content

Instantly share code, notes, and snippets.

@Racum
Racum / gist:3688400
Created September 10, 2012 01:54
NSMutableURLRequest and NSURLConnectionDelegate
- (void)go {
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://httpbin.org/headers"]];
[request setHTTPShouldHandleCookies:NO];
[request setHTTPMethod:@"GET"];
[request setValue:@"Test" forHTTPHeaderField:@"User-Agent"];
[request setTimeoutInterval:10];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection) {
@Racum
Racum / detect_country_from_list.py
Created June 6, 2013 17:59
Using pygeoip to choose a country from a list if matches.
# $ pip install pygeoip
# $ pip install requests
# $ wget -N http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
# $ gunzip GeoIP.dat.gz
import pygeoip
import requests # Just for hiting httplib.
# Example or supported countries:
COUNTRIES = ('br', 'mx', 'cl', 'ar')
# Download this example file first:
# http://podcast.cbc.ca/mp3/podcasts/spark_20130623_97794.mp3
import cStringIO as StringIO
from PIL import Image
import eyed3
audiofile = eyed3.load("spark_20130623_97794.mp3")
if len(audiofile.tag.images) > 0:

Google Speech to Text API

Audio

Format:

  • Format: FLAC
  • Sample Rate: 16000Hz
  • Chanels: 1 (mono)
  • Depth: 16bit
# Handshake: (resolve the addressbook_path in order to cache it)
# Step 1: get [principal_path] from [server_root]:
$ curl -X PROPFIND --user [user]:[password] -H "Depth: 1" -d "<?xml version=\"1.0\" encoding=\"UTF-8\"?><A:propfind xmlns:A=\"DAV:\"><A:prop><A:current-user-principal/></A:prop></A:propfind>" [server_root] | xpath "/d:multistatus/d:response[1]/d:propstat/d:prop/d:current-user-principal/d:href/text()"
# Step 2: get [addressbook_list_path] from [principal_path]:
$ curl -X PROPFIND --user [user]:[password] -H "Depth: 1" -d "<?xml version=\"1.0\" encoding=\"UTF-8\"?><A:propfind xmlns:A=\"DAV:\"><A:prop><B:addressbook-home-set xmlns:B=\"urn:ietf:params:xml:ns:carddav\"/></A:prop></A:propfind>" [server_root][principal_path] | xpath "/d:multistatus/d:response[1]/d:propstat/d:prop/card:addressbook-home-set/d:href/text()"
# Step 3: get [addressbook_path] from [addressbook_list_path]:
$ curl -X PROPFIND --user [user]:[password] -H "Depth: 1" -d "<?xml version=\"1.0\" encoding=\"UTF-8\"?><A:propfind xml

AngularJS - Templates

Loading all templates from a single file

index.html

<!DOCTYPE html>
<html ng-app="myApp">
<head>
...
@Racum
Racum / guaranteed_order_json.py
Created August 8, 2014 15:05
Guaranteed-order JSON dump in Python using collections.OrderedDict.
import json
from collections import OrderedDict
def serialize_with(collection_type):
complex_object = collection_type()
complex_object['z'] = collection_type()
complex_object['z']['c'] = 30
complex_object['z']['b'] = 20
complex_object['z']['a'] = 10

Keybase proof

I hereby claim:

  • I am racum on github.
  • I am racum (https://keybase.io/racum) on keybase.
  • I have a public key whose fingerprint is 54B1 C033 AD52 6A75 E453 5995 206E E57B 9B64 69D3

To claim this, I am signing this object:

@Racum
Racum / test_thread.py
Created April 18, 2016 16:58
Test Python's threading module
import threading
import time
import random
class MyThread(threading.Thread):
def __init__(self, num):
self.num = num
threading.Thread.__init__(self)
@Racum
Racum / api_status_lifecycle.md
Created August 16, 2016 13:53
API Status and Lifecycle

API Status and Lifecycle

Endpoint

Request

GET [service_api_root]/status

No authentication or custom accept header required.