Skip to content

Instantly share code, notes, and snippets.

@bewt85
bewt85 / app.py
Last active October 12, 2017 16:22
A better (non-blueprinting) way of splitting a Flask-RESTful project over a number of files
from flask import Flask
from flask.ext.restful import Api
from todo import TodoList, Todo
from done import CompleteTaskList, CompleteTask
app = Flask(__name__)
api = Api(app)
api.add_resource(CompleteTaskList, '/dones/')
api.add_resource(CompleteTask, '/dones/<string:id>', endpoint='done_ep')
@bewt85
bewt85 / base.py
Last active December 19, 2015 20:09
First stab at find_and_modify for MongoEngine. Can be added to mongoengine/queryset/base.py. NB As yet untested...
def andModify(self, upsert=False, sort=None, full_response=False, **update):
"""Perform an atomic update on the fields matched by the query. Returns
a document. Essentially a wrapper for PyMongo's find_and_modify; itself
a wrapper for MongoDB's findAndModify.
:param upsert: Any existing document with that "_id" is overwritten.
:param sort: a list of (key, direction) pairs specifying the sort order
for this query.
:param full_response: return the entire response object from the server.
:param update: Django-style update keyword arguments
@bewt85
bewt85 / pseudoRandom.py
Created June 8, 2014 16:41
Creates a file which appears random but is actually deterministic
#!/usr/bin/env python
from hashlib import md5
import argparse, os
parser = argparse.ArgumentParser(description="Makes pseudo-random but deterministic files of ascii text")
parser.add_argument('output_file', help="Location of output file")
parser.add_argument('size', nargs='?', default=1024, type=int, help="Size in bytes of output file")
parser.add_argument('seed', nargs='?', default=None, help="A seed value to differentiate output files")
args = parser.parse_args()
@bewt85
bewt85 / splitGitDirs.sh
Created June 8, 2014 19:12
Script to help split a project into sub-modules
#!/bin/bash
set -ex
ROOT_DIR=$(cd $(dirname $1); pwd)/$(basename $1)
START_DIR=$(pwd)
[[ -d ${START_DIR}/splits ]] || mkdir ${START_DIR}/splits
for subProject in $(find $ROOT_DIR -mindepth 1 -maxdepth 1 -type d | grep -v git); do
@bewt85
bewt85 / pullAllSubmodules.sh
Last active August 29, 2015 14:02
Script to pull latest version of submodules
#!/bin/bash
START_DIR=$(pwd)
for dir in $(find . -name ".git" -type d); do
cd $(echo $dir | sed 's/\/.git$//') && git checkout master && git pull && cd $START_DIR;
done
git status
@bewt85
bewt85 / directory_structure.txt
Last active August 29, 2015 14:17
mocking os.path.isdir
.
├── bar
├── foo
├── scripts
│   ├── foobar.py
│   └── __init__.py
└── tests
├── foobar_test.py
└── __init__.py
@bewt85
bewt85 / Futures.py
Created March 26, 2015 01:24
A mix of scala's Future and Try in Python
import gevent
class Try(object):
def __init__(self, result):
self.result = result
class Success(Try):
def __repr__(self):
return "<Success '%s'>" % self.result
@bewt85
bewt85 / write_foo.py
Created April 28, 2015 09:39
Python dodgy file handle modes
with open('foo', 'w') as foo:
foo.write("bar\n") # writes bar
with open('foo', 'wa') as foo:
foo.write("baz\n") # writes baz, removes bar
with open('foo', 'what will this do?') as foo:
foo.write("nonsense\n") # removes bar, writes nonsense!
with open('foo', 'something else') as foo:
const { Readable } = require('stream');
const logger = require("debug");
const _ = require("lodash");
const whenHttpRequestComplete = new Promise(resolve => setTimeout(resolve, _.random(100, 1000)));
class SlowNumberSource extends Readable {
constructor(options={}) {
super(options);
this.getNext = this.buildNext(1);
@bewt85
bewt85 / README.md
Last active July 8, 2023 08:09
here-here - what did I run here

here-here

here-here is like history but it saves the time you ran a command and the directory you were in when you ran it. Running here tells you all the commands you ran in this directory even if you were on a different server.

I commonly wonder how I made a file (and maybe why). Sometimes I try looking in my history (using the history command). This makes that easier by clearing out some of the noise.

Installation