Skip to content

Instantly share code, notes, and snippets.

View Lucretiel's full-sized avatar

Nathan West Lucretiel

View GitHub Profile
//Global Data example
function load_problem(tokens) {
global_data = get_global_data(tokens)
return global_data.num_cases, function(tokens) {
case_specific_data = get_case_data(tokens)
return function() {
// Do a bunch of math with global_data and case_specific_data
return solution
}
}
def emit(list_of_elements):
for elem in list_of_elements:
if elem.thing1 > 0:
yield elem.thing1
if elem.thing2 < 0:
yield elem.thing2
if elem.thing3 != 0:
yield elem.thing3
class SyncClientThread:
def __init__(self):
...
self.stop = False
self.nostop_depth = 0
def Stop(self):
self.stop = True
def IsStopped(self):
return self.nostop_depth == 0 and self.stop
# Try appending ?ts=2 to the url of this page.
# Then try ?ts=4
# Then ?ts=8
def my_member_function(self, argument):
for value in range(10):
# Notice how, as the indents shift, all the arguments
# to this function stay vertically aligned.
self.my_other_func(argument,
value,
@Lucretiel
Lucretiel / async.js
Last active October 11, 2017 04:50
Sample code showing the use of async functions in Javascript
// Create a promise that is resolved after `timeout` ms
const promiseTimer = timeout => new Promise(resolve => setTimeout(() => resolve(null), timeout))
async function counter(max) {
for(let i = 0; i < max; i++) {
console.log("Counting:", i)
// This is the important part. "await" allows the function to appear to block;
// other asynchronous work can be done here. This is basically the equivelent
// of a .then on promiseTimer
def get_rows(selected_table):
for row in selected_table.css('tr'):
cells = row.css('td').extract()
yield tuple(cells)
def find_bge_table(response):
for table_element in response.css('table'):
table = list(get_rows(table_element))
if any(row[0] == "BGE" for row in table):
@Lucretiel
Lucretiel / spread_fixtures.py
Created June 22, 2018 03:58
Call a pytest test once for each value in a fixture list
import itertools
import functools
def keyed_product(**iterables):
'''
Same as itertools.product, but returns key-value dicts. Basically, given
a dict of key => list, return every possible combination of all the
values in the lists, and their associated keys.
@Lucretiel
Lucretiel / spread_fixtures.py
Created June 22, 2018 03:58
Call a pytest test once for each value in a fixture list
import itertools
import functools
def keyed_product(**iterables):
'''
Same as itertools.product, but returns key-value dicts. Basically, given
a dict of key => list, return every possible combination of all the
values in the lists, and their associated keys.
@Lucretiel
Lucretiel / spread_fixtures.py
Last active June 22, 2018 04:01
Call a pytest test once for each value in a fixture list
import itertools
import functools
def keyed_product(**iterables):
'''
Same as itertools.product, but returns key-value dicts. Basically, given
a dict of key => list, return every possible combination of all the
values in the lists, and their associated keys.
@Lucretiel
Lucretiel / advent_template.c
Last active December 5, 2018 16:49
A template for advent of code problems. Write your solution in the `solve` function; the template handles all the i/o
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
/**
* Write a message to stderr, followed by a newline, then exit the program.
* This helper function is provided because C doesn't have throwable exceptions
* or any similarly ergonomic error handling.
*
* Arguments to this function are similar to `fprintf`; see online documentation