Skip to content

Instantly share code, notes, and snippets.

View elliotchance's full-sized avatar
🤓
Building awesome stuff with V

Elliot Chance elliotchance

🤓
Building awesome stuff with V
View GitHub Profile
@elliotchance
elliotchance / waitForSelector.js
Created December 18, 2015 05:17
Nightwatch: Wait for a selector (CSS) that launches a pseudo-debugger.
/**
* Wait for a selector (CSS) that launches a pseudo-debugger.
*
* This builds on top of `waitForElementPresent()` that comes with Nightwatch
* and works as a drop in replacement. The major difference is that if the
* selector does not appear in the specified time it will act like a breakpoint
* where the state is paused and a Continue button appears to proceed manually.
*
* @param selector The CSS entity to wait for - e.g. '#something'
* @param [timeout] The maximum time we should wait. If this isn't provided 10
next_xid = 1
active_xids = set()
records = []
def new_transaction():
global next_xid
next_xid += 1
active_xids.add(next_xid)
return Transaction(next_xid)
@elliotchance
elliotchance / server.go
Created January 10, 2016 15:46
A simple server in Go
package main
import (
"bufio"
"fmt"
"net"
)
```
This is helper method
@elliotchance
elliotchance / server.go
Last active May 30, 2020 18:40
A simple server in Go
package main
import (
"bufio"
"fmt"
"net"
)
func check(err error, message string) {
if err != nil {
@elliotchance
elliotchance / brainfuck.py
Created January 19, 2016 11:52
Write Your Own Brainfuck Compiler
import sys
import ply.yacc as yacc
import ply.lex as lex
tokens = (
'INCREMENT',
'DECREMENT',
'SHIFT_LEFT',
'SHIFT_RIGHT',
'OUTPUT',
@elliotchance
elliotchance / example.go
Last active October 22, 2024 12:46
Capturing grouping for regex function replace in Go
func main() {
str := "abc foo:bar def baz:qux ghi"
re := regexp.MustCompile("([a-z]+):([a-z]+)")
result := ReplaceAllStringSubmatchFunc(re, str, func(groups []string) string {
return groups[1] + "." + groups[2]
})
fmt.Printf("'%s'\n", result)
}
@elliotchance
elliotchance / roman.py
Created February 13, 2016 05:25
Convert roman numerals to a number
import unittest
import re
class RomanToNumberConverter:
# Validate that the input may even be processed.
def validate(self, roman):
if type(roman) is not str:
raise ValueError("You must provide a string.")
if roman == '':
raise ValueError("An empty string was provided.")
@elliotchance
elliotchance / mymod.js
Last active February 17, 2016 02:34
Pythonic style modules in JavaScript
// 1. Modules are always lowercase, one word.
// 2. They can be nested in any level of directories, but the name of the module
// always reflects the file name (not including the path).
// Private
let counter = 0;
function addition(a, b) {
return a + b;
}
@elliotchance
elliotchance / mapreduce.py
Created February 27, 2016 23:00
Simple MapReduce example
import re
def mapper(emit, text):
words = re.split('[^\w]', text.lower())
for word in words:
if word:
emit(word, 1)
def reducer(key, values):
@elliotchance
elliotchance / bowling.py
Created May 3, 2016 11:39
Kata: The Bowling Game
import pytest
class Game:
def __init__(self):
self.frames = [[]]
self.allow_bonus_roll = False
@property
def last_frame(self):
return self.frames[-1]