Skip to content

Instantly share code, notes, and snippets.

@absent1706
absent1706 / sqlalchemy-truncate_db.py
Last active April 5, 2023 03:50
Sqlalchemy: Truncate all tables
def truncate_db(engine):
# delete all table data (but keep tables)
# we do cleanup before test 'cause if previous test errored,
# DB can contain dust
meta = MetaData(bind=engine, reflect=True)
con = engine.connect()
trans = con.begin()
con.execute('SET FOREIGN_KEY_CHECKS = 0;')
for table in meta.sorted_tables:
con.execute(table.delete())
@absent1706
absent1706 / activate.bat
Last active March 2, 2017 11:25
knowstory-env
@echo off
set "ROOT_DIR=E:\code\knowstory"
set "VIRTUAL_ENV=%ROOT_DIR%\env"
if defined _OLD_VIRTUAL_PROMPT (
set "PROMPT=%_OLD_VIRTUAL_PROMPT%"
) else (#!E:\Python\python.exe
import re
import sys
@absent1706
absent1706 / config.php
Created January 11, 2017 13:27
psysh/config.php
<?php
/*
PsySh config file (see http://psysh.org/#configure)
!!!
Copy this file to ~\.config\psysh\config.php, where ~ is your home dir
for example (Windows): C:\Users\MY_USER\.config\psysh\config.php
Result dump will look like http://www.qopy.me/D6R2fYEQSw6wjV7NDaPMxw
*/
@absent1706
absent1706 / example.html
Last active July 24, 2021 12:10
Bootstrap-collapse rotate icon. https://jsfiddle.net/1L3xy40k/
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<style>
[data-toggle="collapse"][aria-expanded="true"] > .js-rotate-if-collapsed
{
-webkit-transform: rotate(180deg);
@absent1706
absent1706 / js-form-hacks.js
Last active February 1, 2017 14:37
JS form hacks: reset button, clean empty fields
/*
* Handler for reset button that cleans all form fields and submits form
* See http://www.javascript-coder.com/javascript-form/javascript-reset-form.phtml
*/
$('.js-reset-closest-form').click(function() {
var form = $(this).closest('form')[0];
/* it's important to take form.elements, not just $(form).find(input)
* because inputs can be OUTSIDE <form> tag, i.g.:
* <form id="search-form"> ... </form>
* ...
@absent1706
absent1706 / sqlalchemy_debug_utils.py
Last active March 27, 2017 14:32
SQLAlchemy debug utils
def to_sql(query, dialect_name=None):
from sqlalchemy import dialects
import operator
if dialect_name:
dialect_module = getattr(dialects, dialect_name)
if not dialect_module:
raise KeyError('Wrong dialect {}'.format(dialect_name))
dialect = dialect_module.dialect()
else:
@absent1706
absent1706 / index.html
Last active February 19, 2017 23:55
Oauth2
<!--
See https://developers.google.com/api-client-library/javascript/samples/samples#authorizing-and-making-authorized-requests
for description and setup instructions
Key setup steps:
1. get client ID and API KEY http://www.qopy.me/0yILY0U8TD6kZ0ilhhoIAA
2. run HTTP server in folder where index.html lays.
I.g. PHP server:
php -S php -S 0.0.0.0:5000
or this server (just EXE) http://www.zachsaw.com/?pg=quickphp_php_tester_debugger
@absent1706
absent1706 / index.html
Last active February 20, 2017 00:03
'SignIn with Google' button from https://developers.google.com/identity/sign-in/web/ with ready-to-go credentials
<!--
See https://developers.google.com/identity/sign-in/web/
for description and setup instructions
Key setup steps:
1. get client ID and API KEY http://www.qopy.me/0yILY0U8TD6kZ0ilhhoIAA
2. run HTTP server in folder where index.html lays.
I.g. PHP server:
php -S php -S 0.0.0.0:5000
or this server (just EXE) http://www.zachsaw.com/?pg=quickphp_php_tester_debugger
from oauth2client import client, crypt
# (Receive token by HTTPS POST)
token = 'eyJhbGciOiJSUzI1NiIsImtpZCI6ImQyYzQ4YmFkMzAwMjBhY2U4MzBlZDkzYmQ4MWMzMjhkYzY4NTFhOTMifQ.eyJpc3MiOiJodHRwczovL2FjY291bnRzLmdvb2dsZS5jb20iLCJpYXQiOjE0ODc0MTc5ODEsImV4cCI6MTQ4NzQyMTU4MSwiYXRfaGFzaCI6IkIyYkJWU2d1M0tkOHdxT1p5NnN4Z3ciLCJhdWQiOiI1ODE3ODY2NTg3MDgtZWxmbGFua2VycXVvMWE2dnNja2FiYmhuMjVoY2xsYTAuYXBwcy5nb29nbGV1c2VyY29udGVudC5jb20iLCJzdWIiOiIxMDcwMzQzNjY2NTEwMjUwMzAwMDAiLCJlbWFpbF92ZXJpZmllZCI6dHJ1ZSwiYXpwIjoiNTgxNzg2NjU4NzA4LWVsZmxhbmtlcnF1bzFhNnZzY2thYmJobjI1aGNsbGEwLmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwiZW1haWwiOiJsaXR2aW5lbmtvMTcwNkBnbWFpbC5jb20iLCJuYW1lIjoi0JDQu9C10LrRgdCw0L3QtNGAINCb0LjRgtCy0LjQvdC10L3QutC-IiwicGljdHVyZSI6Imh0dHBzOi8vbGg0Lmdvb2dsZXVzZXJjb250ZW50LmNvbS8tME9hYkNoaHZMU1kvQUFBQUFBQUFBQUkvQUFBQUFBQUFBLU0vUkFhaHlTaUE2aGsvczk2LWMvcGhvdG8uanBnIiwiZ2l2ZW5fbmFtZSI6ItCQ0LvQtdC60YHQsNC90LTRgCIsImZhbWlseV9uYW1lIjoi0JvQuNGC0LLQuNC90LXQvdC60L4iLCJsb2NhbGUiOiJydS1VQSJ9.LrL5ji_j-oX9Lh78TsoyJXzSQfUaQ3elxg8GOrsHwURxMAY7lbo
@absent1706
absent1706 / test.py
Created March 13, 2017 19:58
run all python tests (unittest discover) programmatically
import os
import unittest
loader = unittest.TestLoader()
tests = loader.discover(os.path.join(os.path.dirname(__file__),
'sqlalchemy_mixins'))
testRunner = unittest.runner.TextTestRunner()
testRunner.run(tests)