Skip to content

Instantly share code, notes, and snippets.

@akfreas
akfreas / Block Factory Pattern
Created March 25, 2014 21:05
How to create block factories in Objective-C
BOOL(^(^comparator)(NSString *, NSString *, BOOL))(NSString *, NSDictionary *) = ^BOOL(^(NSString *prefixString, NSString *typeString, BOOL hasNumberedSuffix))(NSString *, NSDictionary *) {
return ^BOOL(NSString *str, NSDictionary *dict) {
NSString *regexString = [NSString stringWithFormat:@"%@_([0-9]{2})_%@", prefixString, typeString];
NSError *err = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexString options:nil error:&err];
if ([[regex matchesInString:str options:0 range:NSMakeRange(0, [str length])] count] > 0) {
return YES;
} else {
return NO;
@akfreas
akfreas / gist:0cd345065b6d9bd780e5
Created June 18, 2014 14:59
NSURLRequest Curl Command
@interface NSURLRequest (CURL)
- (NSString *)curlCommand;
@end
@implementation NSURLRequest (CURL)
- (NSString *)curlCommand
{
NSDictionary *params = [NSJSONSerialization JSONObjectWithData:self.HTTPBody options:0 error:nil];
NSMutableString *paramString = [NSMutableString stringWithString:@"{"];
for (NSString *key in params) {
@akfreas
akfreas / NSURLRequest+cURL.m
Created September 15, 2015 10:13
NSURLRequest Create cURL Command
@interface NSURLRequest (cURL)
- (NSString *)curlCommand;
@end
@implementation NSURLRequest (cURL)
- (NSString *)curlCommand
{
NSString *(^stringFromDict)(NSDictionary *dict) = ^NSString*(NSDictionary *dict){
let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(1 * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {
println("test")
}
@akfreas
akfreas / gist:5054929
Created February 28, 2013 07:26
Grabs profile pictures of a person/place/thing from Wikipedia. This function will check for all images associated with an article's title and compare those images against the images inside the infobox (typically the box on the top right hand corner of an article), returning the WikiMedia URLs of the images inside the infobox.
def figure_wikipedia_pic(figure_name, image_size):
wiki_images_get = requests.get("http://en.wikipedia.org/w/api.php?format=json&action=query&titles=%s&prop=images" % figure_name)
wiki_json = wiki_images_get.json()
wiki_page_json = requests.get("http://en.wikipedia.org/w/api.php?format=json&action=query&titles=%s&prop=revisions&rvprop=content&rvsection=0" % figure_name).json()
wiki_page_json = str(wiki_page_json)
#!/usr/bin/python
from git import *
from argparse import ArgumentParser
import urllib
import shutil
from elementtree.ElementTree import Element, parse
import os
@akfreas
akfreas / gist:6788980
Created October 2, 2013 04:00
Plurality correctness
NSString *pluralString;
NSString *lastComponent = [person.firstName substringWithRange:NSMakeRange(firstName - 1, 1)];
if ([lastComponent isEqualToString:@"s"]) {
pluralString = @"'";
} else {
pluralString = @"'s";
}
#!/usr/bin/env python
import os
import shutil
from zipfile import ZipFile
from boto.s3.connection import S3Connection
from tempfile import mkdtemp
aws_access_key_id = os.getenv("AWS_ACCESS_KEY_ID")
@akfreas
akfreas / bashfu.sh
Created January 5, 2016 10:36
Bash-Fu
mkdir -p nathan/is_a_{alex,god}
BACKUP_DIR='_backup';
FILE_LIST=`find . -type f -not -path "*$BACKUP_DIR*"`
mkdir -pv $BACKUP_DIR;
while IFS= read -r line
do
if [[ $line == *"$1"* ]]; then
newfile=`echo $line | sed "s%$1%$2%g"`;
mkdir -pv "`dirname "$newfile"`";
echo "Copying $line to $newfile";
cp "$line" "$newfile" 2> /dev/null;