Skip to content

Instantly share code, notes, and snippets.

@pawl
pawl / dms_monit
Last active Aug 29, 2015
Dallas Makerspace Monit Configuration
View dms_monit
set mailserver localhost
# don't alert when pid changed or when monit starts
set alert network@dallasmakerspace.org but not on { pid, instance }
check process apache with pidfile /run/apache2.pid
start program = "/etc/init.d/apache2 start" with timeout 60 seconds
stop program = "/etc/init.d/apache2 stop"
if cpu > 60% for 2 cycles then alert
if cpu > 80% for 5 cycles then restart
@pawl
pawl / copy_table.py
Last active Oct 26, 2020
Copy Schema Of A Remote Database Table To A New Local Table With SQLalchemy
View copy_table.py
from sqlalchemy import create_engine, Table, Column, Integer, Unicode, MetaData, String, Text, update, and_, select, func, types
# create engine, reflect existing columns, and create table object for oldTable
srcEngine = create_engine('mysql+mysqldb://username:password@111.111.111.111/database') # change this for your source database
srcEngine._metadata = MetaData(bind=srcEngine)
srcEngine._metadata.reflect(srcEngine) # get columns from existing table
srcTable = Table('oldTable', srcEngine._metadata)
# create engine and table object for newTable
destEngine = create_engine('mysql+mysqldb://username:password@localhost/database') # change this for your destination database
@pawl
pawl / rfid.py
Created Apr 2, 2014
Parallax USB RFID reader Python Code
View rfid.py
#! /usr/bin/python
import serial
import time
ser = serial.Serial('/dev/ttyUSB0', 2400, timeout=1) # replace '/dev/ttyUSB0' with your port
while True:
response = ser.read(12)
if response <> "":
print "raw: " + str(response)
@pawl
pawl / sybase.py
Created Apr 29, 2014
Copy Sybase IQ Table Schema to MySQL Using SQLalchemy and SQL Anywhere ODBC
View sybase.py
import sqlalchemy, sqlanydb
from sqlalchemy import create_engine, Table, Column, Integer, Unicode, MetaData, String, Text, update, and_, select, func, types
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
def connect():
return sqlanydb.connect(DSN='<your DNS>', userid='<username>', password='<password>')
srcEngine = sqlalchemy.create_engine('sqlalchemy_sqlany://', creator=connect, echo=True) # pip install sqlalchemy-sqlany
@pawl
pawl / flask_admin_edit_id.py
Created Jun 2, 2014
Get ID of edited item within WTforms validator - Flask-Admin
View flask_admin_edit_id.py
def start_must_not_conflict(form, field):
print request.args.get('id') # get ID
if Event.query.filter(db.and_(Event.location == form.location.data, Event.start.between(form.start.data, form.end.data), Event.id != request.args.get('id'))).first(): # exclude ID from query
raise wtforms.validators.ValidationError('Start time conflicts with another request for the same time.')
form_args = dict(
start=dict(validators=[start_must_not_conflict])
)
@pawl
pawl / google_calendar_api.py
Created Jun 2, 2014
Escape Newlines In Google Calendar API - Python
View google_calendar_api.py
# how you need to format the body of the request
requestbody = """{
"description": %s,
"summary": %s
}
""" % (json.dumps(description), json.dumps(summary)) # use json.dumps to escape string for the request body
@pawl
pawl / flotpie.js
Last active Aug 29, 2015
Change Percent To Numbers and Keep Color - Flot Pie Chart
View flotpie.js
options = {
series: {
pie: {
innerRadius: 0.5,
show: true,
label: {
show: true,
formatter: function (label, series) {
console.log(series);
return '<div style="font-size:8pt;text-align:center;padding:2px; color: ' + series.color +';">' + label + '<br/>' + series.data[0][1] + '</div>';
@pawl
pawl / bindparameters.py
Created Aug 6, 2014
Use List In Bind Parameters - SQLAlchemy
View bindparameters.py
my_list = ['peach', 'grape', 'apple']
query_parameters = {}
counter = 1
for list_item in my_list:
query_parameters["list_item" + str(counter)] = list_item
counter += 1
where_clause = 'fruits IN(:' + ",:".join(query_parameters.keys()) + ')' # create clause to be inserted into query
query_text = db.text("""
SELECT
@pawl
pawl / nest_loop.py
Last active Aug 29, 2015
Slow Nested For Loop Example
View nest_loop.py
#data
old_list = [
{'name': "matthew", 'test_result1': "A", 'test_result2': "A"},
{'name': "steve", 'test_result1': "B", 'test_result2': "D"},
{'name': "dave", 'test_result1': "C", 'test_result2': "F"}
]
new_list = [
{'name': "steve", 'test_result1': "A", 'test_result2': "F"},
{'name': "matthew", 'test_result1': "B", 'test_result2': "F"},
@pawl
pawl / get_parameter_flask_change.py
Created Sep 10, 2014
Change Single GET Parameter - Flask
View get_parameter_flask_change.py
from application import app
from flask import request, url_for, redirect
@app.route('/')
def index():
modified_args = dict(request.args)
modified_args['key'] = 'modified_value'
return redirect(url_for('my_view', **modified_args))