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 / 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 / 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 / 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 / cookiecutter_alias_setup.sh
Created May 15, 2017 12:28
cookiecutter commands: setup new basic projects
alias new-springboot-app='cookiecutter https://github.com/m-x-k/cookiecutter-spring-boot'
alias new-python-app='cookiecutter https://github.com/m-x-k/cookiecutter-python-flask'
alias new-react-app='cookiecutter https://github.com/m-x-k/cookiecutter-react'
alias new-java-app='cookiecutter https://github.com/m-x-k/cookiecutter-java'
alias new-elm-app='cookiecutter https://github.com/m-x-k/cookiecutter-elm'
alias new-scala-app='cookiecutter https://github.com/m-x-k/cookiecutter-scala'
alias new-golang-app='cookiecutter https://github.com/m-x-k/cookiecutter-golang'
alias new-play-app='cookiecutter https://github.com/m-x-k/cookiecutter-play'
@m-x-k
m-x-k / merge_two_dicts.py
Created May 27, 2017 10:19
Python (2 and 3) how to merge two dicts
x = {'a': 1, 'b': 2}
y = {'b': 3, 'c': 4}
def merge_two_dicts_python3():
z = {**x, **y}
print(z) # {'c': 4, 'a': 1, 'b': 3}
def merge_two_dicts_python2():
z = dict(x, **y)
print(z) # {'a': 1, 'c': 4, 'b': 3}
@m-x-k
m-x-k / test_requests.py
Created May 28, 2017 15:08
Python Unittest mock example: requests library
from unittest.mock import patch, Mock
from requests import Response
import requests
import unittest
def health_check(endpoint):
return requests.get(endpoint).status_code
class TestHealthCheck(unittest.TestCase):
@m-x-k
m-x-k / after_this_request_flask_app_example.py
Created June 13, 2017 20:30
Python flask after_this_request example on matching endpoint
from flask import Flask, g, request
from flask import render_template, redirect
app = Flask(__name__)
def after_this_request(f):
if not hasattr(g, 'after_request_callbacks'):
g.after_request_callbacks = []
g.after_request_callbacks.append(f)
return f
@m-x-k
m-x-k / version.py
Last active April 8, 2018 02:13
Python version support
import os
from pathlib import Path
current_dir = os.path.dirname(__file__)
version_file_full_path = os.path.join(current_dir, "VERSION.txt")
"""
Create and obtain version information based on environment variables:
* APP_MAJOR_VERSION
@m-x-k
m-x-k / active_directory_users.py
Created July 18, 2017 12:12
Python ldap3 active directory add and search for users
import ssl
from flask import json
from ldap3 import Server, \
Connection, \
SUBTREE, \
ALL_ATTRIBUTES, \
Tls, MODIFY_REPLACE
OBJECT_CLASS = ['top', 'person', 'organizationalPerson', 'user']
@m-x-k
m-x-k / recursiveFileInfo.go
Created July 29, 2017 15:27
Output file size and check if a duplicate exists for all files in a specific directory
package main
import (
"os"
"fmt"
"log"
"crypto/sha512"
"io/ioutil"
"path/filepath"
)