Skip to content

Instantly share code, notes, and snippets.

@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)
@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 / 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 / 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 / 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 / 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 / patch_tastypie_imagefield.py
Last active December 21, 2015 12:49
A simple way to workaround using HTTP PATCH with Django tastypie on models that contain an Image/File Field
'''
I faced a problem when sending a HTTP PATCH to django models containing an ImageField.
The same problem was discussed on the django-tastypie mailing list - https://groups.google.com/forum/?fromgroups#!topic/django-tastypie/cxrI6Cl1z4s
When PATCHing a resource, tasypie dehydrates the original object, merges the changed fields, and then simulates a PUT. This presents a problem for FileField and ImageField as dehydrating these fields produces a URL to the file, rather than the path relative to MEDIA_ROOT stored in the database. Therefore, after a PATCH to a model with FileFields, those fields will contain full URLs rather than relatives paths.
For example, consider a set up in which media for a site is served from http://media.example.com/. When PATCHing a resource containing a file field, a reference to "/directory/file.jpg" will be dehydrated to "http://media.example.com/directory/file.jpg" which is the value that will be written back to the database. - Nikolas Stevenson-Molnar,
@ratpik
ratpik / django_ajax_login.py
Created August 23, 2013 11:10
A simple way to log-in a user via an ajax request
from annoying.decorators import ajax_request
from django.contrib.auth import authenticate, login
from django.views.decorators.http import require_http_methods
@ajax_request
@require_http_methods(["POST"])
def ajax_login(request):
username = request.POST['username']
password = request.POST['password']
#Directory sizes sorted in reverse order
du -ch --exclude=.git --max-depth=1|sort -hr
@ratpik
ratpik / visualize_django_models
Created November 25, 2013 10:48
A way to visualize the schema of models in Django and take periodic snapshots to document schema changes over time. Uses the `django-extensions` app and `graphviz`
python manage.py graph_models <app > <app>.dot
dot <app>.dot -Tpng -o <app>+`date`.png