Skip to content

Instantly share code, notes, and snippets.

Avatar

melissamarima

View GitHub Profile
View Betfair scrape try error
>>> 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__
View Betfair scrape try code
# 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"
View Without fat arrows.js
//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 Dec 21, 2015
Understanding fat arrows
View with fat arrows.js
// 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 Nov 2, 2015 — forked from robotlolita/promises.md
Promises: replacing dependency on time, by dependency on your data.
View promises.md

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);
View 3. Promises way.js
// 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 Oct 18, 2015
Trying to understand JS concurrency and promises
View freendoe.irc chat record
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 Oct 18, 2015
Trying to understand JS concurrency
View Sorella's JS
function sum(x, cc) {
setTimeout(function() {
cc(x.l + x.r);
}, 100);
}
var a = {
l: 1,
r: 3
},
View 2. Async function.js
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
View 1. synchronous way.js
// 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);