Skip to content

Instantly share code, notes, and snippets.

@aantonw
aantonw / api.py
Created August 27, 2017 03:41 — forked from alanhamlett/api.py
Serialize SQLAlchemy Model to dictionary (for JSON output) and update Model from dictionary attributes.
import uuid
import wtforms_json
from sqlalchemy import not_
from sqlalchemy.dialects.postgresql import UUID
from wtforms import Form
from wtforms.fields import FormField, FieldList
from wtforms.validators import Length
from flask import current_app as app
from flask import request, json, jsonify, abort
@aantonw
aantonw / add ssh key one line.sh
Created August 7, 2016 23:50
one line command to add ssh keys to a remote server
cat ~/.ssh/id_rsa.pub | ssh user@hostname 'cat >> .ssh/authorized_keys'
@aantonw
aantonw / README.md
Created July 29, 2016 16:14
Circular imports in Python 2 and Python 3: when are they fatal? When do they work?

When are Python circular imports fatal?

In your Python package, you have:

  • an __init__.py that designates this as a Python package
  • a module_a.py, containing a function action_a() that references an attribute (like a function or variable) in module_b.py, and
  • a module_b.py, containing a function action_b() that references an attribute (like a function or variable) in module_a.py.

This situation can introduce a circular import error: module_a attempts to import module_b, but can't, because module_b needs to import module_a, which is in the process of being interpreted.

But, sometimes Python is magic, and code that looks like it should cause this circular import error works just fine!

@aantonw
aantonw / _service.md
Created July 27, 2016 03:02 — forked from naholyr/_service.md
Sample /etc/init.d script

Sample service script for debianoids

Look at LSB init scripts for more information.

Usage

Copy to /etc/init.d:

# replace "$YOUR_SERVICE_NAME" with your service's name (whenever it's not enough obvious)
@aantonw
aantonw / uwsgi_nginx_on_centos6.7.txt
Created April 29, 2016 17:17 — forked from ckandoth/uwsgi_nginx_on_centos6.7.txt
Set up nginx and uwsgi emperor on a CentOS 6.7 box
# GOAL: On a CentOS 6.7 minimal install, set up nginx as a reverse proxy to uWSGI Emperor with python 2.7.11 as a plugin to run Flask apps
# ::NOTE:: All instructions below are run as a sudoer, not as root. Talk to your sysadmins if you're not a sudoer.
# Install bare essentials:
sudo yum install -y epel-release
sudo yum groupinstall 'Development Tools'
sudo yum install -y vim openssl unzip nginx openssl-devel zlib-devel sqlite-devel bzip2-devel libffi-devel lapack-devel blas-devel libpng-devel freetype-devel
# Set SELINUX=disabled in the file below, and reboot, or this tutorial will get unnecessarily complicated:
@aantonw
aantonw / lsof_listen_port.sh
Created December 30, 2015 03:03
OS X lsof get listen ports
sudo lsof -iTCP -sTCP:LISTEN -P -n #tcp
sudo lsof -iUDP -P -n | egrep -v '(127|::1)' #udp
@aantonw
aantonw / ES_order_by_ids.json
Last active December 26, 2015 08:34
Elasticsearch order by ids, ES 1.7≤
{
"query": {
"function_score":{
"filter": {
"ids": {
"values": ["2344", "345", "13", "16", "17", "1938", "3546", "333"]
}
},
"script_score": {
"script": "return -ids.indexOf(_fields['_id'].value);",
@aantonw
aantonw / spintax.php
Created November 14, 2015 01:14 — forked from irazasyed/spintax.php
PHP: Text Spinner Class - Nested spinning supported.
<?PHP
/**
* Spintax - A helper class to process Spintax strings.
* @name Spintax
* @author Jason Davis - https://www.codedevelopr.com/
* Tutorial: https://www.codedevelopr.com/articles/php-spintax-class/
*/
class Spintax
{
public function process($text)
@aantonw
aantonw / flaskplotlib.py
Last active November 3, 2015 00:28 — forked from wilsaj/flaskplotlib.py
Example of rendering a matplotlib image directly to Flask view
from flask import Flask, make_response
app = Flask(__name__)
@app.route("/simple.png")
def simple():
import datetime
import StringIO
import random
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
@aantonw
aantonw / auto_increment_capacity.sql
Last active August 29, 2015 14:26 — forked from gplessis/auto_increment_capacity.sql
Detect AUTO_INCREMENT capacity in MySQL
SELECT table_schema,
table_name,
data_type,
( CASE data_type
WHEN 'tinyint' THEN 255
WHEN 'smallint' THEN 65535
WHEN 'mediumint' THEN 16777215
WHEN 'int' THEN 4294967295
WHEN 'bigint' THEN 18446744073709551615
end >> IF(Locate('unsigned', column_type) > 0, 0, 1) ) AS MAX_VALUE,