Skip to content

Instantly share code, notes, and snippets.

View melissamarima's full-sized avatar

melissamarima

View GitHub Profile
>>> from models import Greeting
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "models.py", line 4, in <module>
class Greeting(models.Model):
File "models.py", line 5, in Greeting
when = models.DateTimeField('date created', auto_now_add=True)
File "/Users/myusername/pythonPlay/littlefrog/venv/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 1187, in __init__
super(DateField, self).__init__(verbose_name, name, **kwargs)
File "/Users/myusername/pythonPlay/littlefrog/venv/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 166, in __init__
@melissamarima
melissamarima / Betfair scrape try code
Created August 14, 2016 16:29
Betfair scrape try
# scrape.py
from bs4 import BeautifulSoup
import requests
import urllib
from models import Greeting
from models import Betfair
def scrapeBetfair():
url = "https://www.betfair.com/exchange/politics/market?id=924.8325569&exp=e"
@melissamarima
melissamarima / Without fat arrows.js
Created December 21, 2015 03:53
Understanding fat arrows
//https://greenin.space/how-do-you-write-an-asynchronous-for-loop-in-javascript/
var N = 5
function asyncFunc (cb) {
console.log("in asyncFunc")
setTimeout(cb(Math.random()), 3000)
console.log("out asyncFunc")
}
@melissamarima
melissamarima / with fat arrows.js
Last active December 21, 2015 03:54
Understanding fat arrows
// https://greenin.space/how-do-you-write-an-asynchronous-for-loop-in-javascript/
var N = 5
function asyncFunc (cb) {
console.log("in asyncFunc")
setTimeout(() => cb(Math.random()), 3000)
console.log("out asyncFunc")
}
function loop (max, results, done) {
@melissamarima
melissamarima / promises.md
Created November 2, 2015 02:21 — forked from robotlolita/promises.md
Promises: replacing dependency on time, by dependency on your data.

So, promises are the big thing now in JavaScript and all that shit. Though they are a fairly old concept. In short using promises means you're giving up on your dependency on time and mutable state, and buying into dependency on your data, regardless of time, or structure, or any of those unnecessarily complicated things.

First, let's build a conceptual understanding of promises, and then I'll show how this maps to the particulars of the Promises/A+ specification. A promise is a placeholder for a value. Instead of saying what your value depends on through your program structure, i.e.: doX(a); doY(a); ... you say what the value depends on through other values. ie.: b = doX(a); c = doX(b); — here b depends on a; and c depends on b, which depends on a.

Or, a more "visual" representation of this would be:

var a = Promise();

b = a.then(doX);
// Logic MUST BE identical to the synchronous way:
// https://gist.github.com/melissamarima/2b9e8594892103dbc02c
var mongoose = require('mongoose');
var collectionASchema = new mongoose.Schema({field1: Number, field2: Number});
var collectionBSchema = new mongoose.Schema({field1: Number, field2: Number});
collectionASchema.index({field1: 1});
collectionBSchema.index({field1: -1});
mongoose.model('collectionA', collectionASchema);
mongoose.model('collectionB', collectionBSchema);
@melissamarima
melissamarima / freendoe.irc chat record
Created October 18, 2015 16:48
Trying to understand JS concurrency and promises
10-16-2015 freenode.irc #javascript
22:24] <melissamm> I still don't understand why it's so bad to force synchronous functions in the async node.js MEAN stack. Sure it waits around, but wouldn't promises also have to wait around to get resolved?
[22:25] <melissamm> (sorry my internet died for a bit, I might have not caught the answers to my previous question.)
[22:25] <oojacoboo> melissamm: the async nature of js is important
[22:25] <oojacoboo> it's the nature of how people read and interpret it, it's also one of the key benefits of the language
[22:26] <oojacoboo> if you want sync, use another language
[22:26] <melissamm> I think I'm missing something. Promises still waits around to get resolved. So that also takes long.
[22:26] <melissamm> nono I have to use Async, I'm using the MEAN stack
[22:26] <oojacoboo> melissamm: yes, and promises are optional
@melissamarima
melissamarima / Sorella's JS
Created October 18, 2015 16:47
Trying to understand JS concurrency
function sum(x, cc) {
setTimeout(function() {
cc(x.l + x.r);
}, 100);
}
var a = {
l: 1,
r: 3
},
var mongoose = require('mongoose');
var collectionASchema = new mongoose.Schema({field1: Number, field2: Number});
var collectionBSchema = new mongoose.Schema({field1: Number, field2: Number});
collectionASchema.index({field1: 1});
collectionBSchema.index({field1: -1});
mongoose.model('collectionA', collectionASchema);
mongoose.model('collectionB', collectionBSchema);
collectionBSchema.methods.addToCollectionB = function(cb){
// `this` is the newDocB
// collectionA and collectionB are arrays of Doc objects
collectionA = [];
collectionB = [];
addToCollectionB: function (newField1, newField2){
var newDocB = new Doc(newField, newField2);
// 1. If collection A is empty, push newDocB onto collection B, and stop
if (collectionA.length == 0){
collectionB.push(newDocB);