This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def merge_dicts(d1, d2): | |
"""Safely merges the contents of the dictionary d2 into dictionary d1. | |
Unlike dict.update(), this is a non-destructive operation: a KeyError is | |
raised if one of d2's keys already exists in d1. | |
""" | |
# Based on timing tests, it's faster to scan for conflicts upfront and | |
# then perform a bulk update. This presumes that, generally speaking, | |
# the destination dictionary d1 will be larger (in item count) than d2. | |
for key in d2.iterkeys(): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ClassAttribute(object): | |
"""Class attributes can only be accessed via class type objects. | |
Attempting to access the attribute via an instance of the class will | |
result in an AttributeError. | |
""" | |
__slots__ = ('attr',) | |
def __init__(self, attr): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/NSArray.h> | |
@interface NSArray (Predicates) | |
- (NSArray *)arrayOfObjectsPassingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate; | |
- (NSArray *)arrayOfObjectsWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NSString * NSStringFromIndexSet(NSIndexSet *indexSet) { | |
NSMutableString *string = nil; | |
NSRange range = NSMakeRange([indexSet firstIndex], 1); | |
while (range.location != NSNotFound) { | |
NSUInteger nextIndex = [indexSet indexGreaterThanIndex:range.location]; | |
while (nextIndex == range.location + range.length) { | |
range.length++; | |
nextIndex = [indexSet indexGreaterThanIndex:nextIndex]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface NSCountedSet (NSDictionaryExtensions) | |
- (id)initWithDictionary:(NSDictionary *)dict; | |
- (void)addObjectsFromDictionary:(NSDictionary *)dict; | |
- (NSDictionary *)dictionary; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ProtocolBufferMixin(object): | |
"""Protocol Buffer support for RequestHandler objects.""" | |
MIMETYPE = 'application/x-protobuf' | |
def read_protobuf(self, message_type, data): | |
"""Attempts to parse a protocol buffer message from the given data.""" | |
# Create the message object and attempt to parse the data into it. | |
try: | |
message = message_type() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def shutdown(graceful=True): | |
"""Shut down the application. | |
If a graceful stop is requested, waits for all of the IO loop's | |
handlers to finish before shutting down the rest of the process. | |
We impose a 10 second timeout. | |
""" | |
ioloop = tornado.ioloop.IOLoop.instance() | |
def final_stop(): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (NSArray *)arrayByApplyingBlock:(id (^)(id obj))block { | |
NSParameterAssert(block != nil); | |
NSMutableArray *result = [NSMutableArray arrayWithCapacity:self.count]; | |
for (id obj in self) { | |
[result addObject:block(obj)]; | |
} | |
return result; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (NSString *)substringMatchingPattern:(NSString *)pattern { | |
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil]; | |
if (regex) { | |
NSAssert1(regex.numberOfCaptureGroups == 1, @"Pattern must include exactly one capture group: %@", pattern); | |
NSTextCheckingResult *match = [regex firstMatchInString:self options:0 range:NSMakeRange(0, self.length)]; | |
if (match && match.numberOfRanges == 2) { | |
return [self substringWithRange:[match rangeAtIndex:1]]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
for i in `find . -name "*.png" -o -name "*.jpg"`; do | |
file=`basename -s .jpg "$i" | xargs basename -s .png | xargs basename -s @2x` | |
result=`ack -i "$file"` | |
if [ -z "$result" ]; then | |
echo "$i" | |
fi | |
done |
OlderNewer