Skip to content

Instantly share code, notes, and snippets.

@ratpik
ratpik / get_days_in_week.py
Last active December 18, 2015 14:39
Get days in the same week as given date in python
from datetime import timedelta
#Input date is a datetime instance
"""
>>> get_days_in_week(datetime(2013,06,17))
[datetime.datetime(2013, 6, 17, 0, 0), datetime.datetime(2013, 6, 18, 0, 0), datetime.datetime(2013, 6, 19, 0, 0), datetime.datetime(2013, 6, 20, 0, 0), datetime.datet ime(2013, 6, 21, 0, 0), datetime.datetime(2013, 6, 22, 0, 0), datetime.datetime(2013, 6, 23, 0, 0)]
>>> get_days_in_week(datetime(2013,06,17))
@ratpik
ratpik / s3upload.py
Last active December 18, 2015 10:29
Utility function that helps upload files to S3 using python and boto
#s3_object_key is the file path relative to the your S3 URL where you want to store the file
Eg.
s3_object_key = '%s/%s' %(settings.USER_PROFILE_PICTURE_DIR, filename)
#file is the file that you have create locally either in ephermal storage (Like the Heroku Dyno) or permanent storage like your local file system
#Write to local storage
Eg.
@ratpik
ratpik / HTTP Post Android to Django Tastypie
Created March 24, 2013 17:36
This describes a way to submit HTTP POST in a way the REST interface provided by Tastypie to Django understands it for JSON data
//Creating the data to be sent, note the escaped quotes that are required
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("\A\"", "\"/api/v1/a/1/\""));
nameValuePairs.add(new BasicNameValuePair("\"B\"", "\"/api/v1/b/1/\""));
nameValuePairs.add(new BasicNameValuePair("\"C\"", "\"Hello from Android\""));
@ratpik
ratpik / SnapbirdTweetExtractor
Created October 13, 2012 19:38
Jquery to get tweets and published date of SnapBird
$('.tweet').each(function(index, element){
var tweet = $(element).find('.entry-content').html().replace(new RegExp('<strong></strong><strong></strong>', 'gm'),' ').replace(new RegExp('<(/){0,1}strong>', 'gm'), '').replace(new RegExp('"', 'gm'),'');
var date = $(element).find('.published').attr('title');
console.log(tweet+' <my-deliminator> ' +date);
});
@ratpik
ratpik / Crunchbase
Created July 7, 2012 21:40
Crunchbase Api retrienval
To get all entities of one kind:
http://api.crunchbase.com/v/1/<plural-namespace>
Namespace can be:
companies
people
financial-organizations
products
@ratpik
ratpik / rank.py
Created June 26, 2012 11:43
Ranking system
def calculate_score(votes, item_hour_age, gravity=1.8):
return (votes - 1) / pow((item_hour_age+2), gravity)