Skip to content

Instantly share code, notes, and snippets.

var request = require('request');
var fs = require('fs');
var moment = require('moment');
var htmlToText = require('html-to-text');
var SerialPort = require('serialport').SerialPort,
serialPort = new SerialPort('/dev/ttyAMA0', {
baudrate: 19200
}),
@josephmosby
josephmosby / gist:2de6cc44d30147ee5c89
Created July 12, 2015 22:55
kinds of sorting for exact matches
sorted(queryset.filter(id__in=collection_list), key=lambda s: s.headline.lower().startswith(kwargs['query']))
# this way will do a sort of the queryset based on whether it starts with the search term
sorted(self.model.objects.filter(id__in=collection_list), key=lambda s: kwargs['query'] in s.headline.lower(), reverse=True)
# this way will do a sort of the queryset based on whether it's an exact match
@josephmosby
josephmosby / format_date.js
Created April 28, 2015 15:14
JS Format an ISO8601 date as Django date string representation
var format_date = function(date) {
var month_names = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"];
js_date = new Date(date);
formatted_date = month_names[js_date.getMonth()] + ' ' + js_date.getDate() + ', ' + js_date.getFullYear()
+ ', ' + js_date.getUTCHours() + ':' + js_date.getUTCMinutes();
if(js_date.getHours() < 12) {
formatted_date += " a.m.";
} else {
@josephmosby
josephmosby / finding_variables.md
Last active August 29, 2015 14:20
Finding mysterious Django variables in templates

In a Django template file...

{% if last_updated %}Last Updated: {{ last_updated }}{% endif %}
{% endblock %}

Wait, where is last updated set?

$ grep -R "last_updated" .        	// where "." is the Django project directory

stuff
@josephmosby
josephmosby / mongo-django.py
Created March 25, 2015 15:03
Architecture for declaring a global MongoDB MongoClient in Django
#project/project/__init__.py
import project.settings
import pymongo
THE_MONGO_CLIENT = pymongo.MongoClient(settings.MONGO_HOST, settings.MONGO_PORT)
#project/project/settings.py
INSTALLED_APPS += 'utils'
MONGO_HOST = 'localhost' #default
@josephmosby
josephmosby / indexAll.js
Created March 20, 2015 19:43
Index everything in a MongoDB collection
db.system.js.save(
{
_id: "indexAll",
value: function() {
colls = db.getCollectionNames();
for (var i = 0; i < colls.length; i++) {
if (colls[i] != "system.indexes") {
db[colls[i]].createIndex( {"$**":"text"}, {name: "TextIndex"});
}
}
@josephmosby
josephmosby / ocheck.js
Created February 23, 2015 20:17
Low-grade check for equality between two JavaScript objects
Object.prototype.equals = function(object) {
if (Object.keys(this).length != Object.keys(object).length) {
return false;
}
for (var i = 0; i < Object.keys(this).length; i++) {
if (this[Object.keys(this)[i]] != object[Object.keys(object)[i]]) {
return false;
}
}
@josephmosby
josephmosby / embed.html
Created December 16, 2014 14:35
An embed for a friendly holiday e-card
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge"/>
</head>
<body>
<iframe width="800" height="600" src="EMBED CODE HURR" frameborder="0" allowfullscreen></iframe>
</body>
</html>
@josephmosby
josephmosby / working_video.html
Created December 10, 2014 18:51
A snippet of code to launch a hosted MP4 video that will auto play in all browsers, including IE8.
<!DOCTYPE html>
<html>
<head>
<!-- use Modernizr and Video.js for cross browser compatibility -->
<meta http-equiv="X-UA-Compatible" content="IE=Edge"/>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/modernizr/2.8.3/modernizr.min.js"></script>
<link href="//vjs.zencdn.net/4.11/video-js.css" rel="stylesheet">
</head>
<body>
<!-- for Video.js, the ID and class attributes are needed.
@josephmosby
josephmosby / tpls.php
Last active August 29, 2015 14:10
TPLS
/* FOR LE TEMPLATE.PHP
/* THIS CODE CALLS A CUSTOM PAGE.TPL FILE IF YOU HAVE A SPECIFIC CONTENT TYPE
function plstwo_preprocess_page(&$vars) {
if (isset($vars['node']->type)) {
$vars['theme_hook_suggestions'][] = 'page__' . $vars['node']->type;
}
}
/* --------------- */