Skip to content

Instantly share code, notes, and snippets.

View m-x-k's full-sized avatar

Martin Kelly m-x-k

View GitHub Profile
@m-x-k
m-x-k / wordExists.py
Created April 16, 2017 10:06
Python quick check if a word exists
import string
def is_word(match):
for word in open('/usr/share/dict/words', 'r'):
if str(word.strip().lower()) == str(match.lower()):
return True
return False
@m-x-k
m-x-k / updateAcrossDatabases.js
Created April 5, 2017 14:35
Mongo update across databases
// Add employee.badge_number to matching person in another database collection
db.getSiblingDB('databaseA').getCollection('employees').find({}).forEach(function(employee1) {
db.getSiblingDB('databaseB').getCollection('people').update(
{"_id": employee1._id},
{
"$set": {
"badge_number": employee1.badge_number
}
}
)
@m-x-k
m-x-k / webscrape.py
Created April 4, 2017 19:48
Web scrape example using python and BeautifulSoup
import fire
import requests
from bs4 import BeautifulSoup
# Web scrape example using python and BeautifulSoup
# Setup: pip install requests beautifulsoup4 fire
# Run: python webscrape.py scrape
class WebScrape(object):
url = "https://www.themuse.com/advice/43-simple-habits-thatll-improve-your-life-even-if-you-just-pick-one"
@m-x-k
m-x-k / JSRValidationTest.java
Created February 27, 2017 16:11
JUnit validate JSR-303 with assertion
import org.hibernate.validator.constraints.NotEmpty;
import org.junit.BeforeClass;
import org.junit.Test;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.util.Set;
@m-x-k
m-x-k / StringTrimExample.js
Created February 12, 2017 21:22
Javascript augment String prototype to include trim function
Function.prototype.method = function (name, func) {
this.prototype[name] = func;
return this;
};
String.method('trim', function ( ) {
return this.replace(/^\s+|\s+$/g, '');
});
document.writeln('"' + " neat ".trim( ) + '"');
@m-x-k
m-x-k / GetValueAsInteger.js
Created February 12, 2017 21:19
Javascript augmenting types example
Function.prototype.method = function (name, func) {
this.prototype[name] = func;
return this;
};
Number.method('integer', function ( ) {
return Math[this < 0 ? 'ceil' : 'floor'](this);
});
document.writeln((-10 / 3).integer( )); // −3
@m-x-k
m-x-k / BubbleSort.java
Last active July 26, 2018 13:33
Java 8 bubble sort integer array example using predicates for acending and descending options
import java.util.Arrays;
import java.util.Random;
import java.util.function.Predicate;
public class BubbleSort {
private static Random random = new Random();
private Integer[] sort(Integer[] array, Predicate<Integer> sortType) {
int count = 0;
@m-x-k
m-x-k / TestRandomStringGenerator.java
Created February 3, 2017 15:30
java random string list generator example with streams
import java.util.List;
import java.util.Random;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class TestRandomStringGenerator {
private static Supplier<String> randomDischargeId = () -> RandomStringUtils.randomConsonant(8);
public static void main(String[] args) {
@m-x-k
m-x-k / rx_interval_observerables.py
Created January 23, 2017 21:49
Reactive (Rx) python example with intervals
from rx import Observable, Observer
Observable.interval(1000) \
.map(lambda i: "{0} Mississippi".format(i))\
.subscribe(lambda s: print(s))
input("Press any key to quit")
@m-x-k
m-x-k / rx_operators.py
Created January 21, 2017 20:36
reactive observable operators example
from rx import Observable, Observer
letters = Observable.from_(['Alpha', 'Beta', 'Gamma', 'Delta', 'Epsilon']) \
.map(lambda s: len(s)) \
.filter(lambda i: i >= 5) \
.subscribe(lambda i: print(i))