Skip to content

Instantly share code, notes, and snippets.

View nguyenkims's full-sized avatar
🎯
Focusing

Son Nguyen Kim nguyenkims

🎯
Focusing
View GitHub Profile
@nguyenkims
nguyenkims / test_iso.py
Last active February 23, 2022 19:10
This snippet shows how to isolate a pytest test using pytest fixture.
"""
This snippet shows how to isolate a pytest test using pytest fixture.
It requires flask, sqlalchemy, pytest, pytest-order to be installed:
> pip install flask sqlalchemy pytest pytest-order
You can run the script by
> pytest -s test_iso.py
@nguyenkims
nguyenkims / postfix.py
Last active July 15, 2019 08:09
Example on how to create an external Postfix script in python
#!/usr/bin/env python3
"""
An example on how to create a external Postfix script in python. By the end of this small tutorial,
"postmap -q 200 tcp:127.0.0.1:10444" should return "success".
This is not much 😅 but serves as an example on how to create Python script for Postfix.
I haven't found any snippet on this topic and ended up creating a toy script.
This is based on http://dozzie.jarowit.net/blog-archive/2013/11/13/postfix-sender-rewriting-scheme/
@nguyenkims
nguyenkims / log.py
Last active February 13, 2024 07:59
Basic example on how setup a Python logger
import logging
import sys
from logging.handlers import TimedRotatingFileHandler
FORMATTER = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
LOG_FILE = "my_app.log"
def get_console_handler():
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setFormatter(FORMATTER)
@nguyenkims
nguyenkims / sync-sequel-pro-favorite.md
Created October 10, 2017 09:53
Sync sequel pro favorites between macs

The sequel pro favorites can be shared between Macs using cloud storage service like Dropbox, Google Drive, OneDrive, etc.

The trick is to create a file that will be synced using these cloud services and sequel pro favorites file is just a symlink to this file.

The below script is used for OneDrive but it can also work for Dropbox and other cloud storages.

  1. Copy the current sequel pro favorites file

cp ~/Library/Application\ Support/Sequel\ Pro/Data/Favorites.plist ~/OneDrive/SequelPro.plist

@nguyenkims
nguyenkims / app.py
Created July 11, 2017 08:56
An example on how to reload Flask app in runtime
"""
This is an example on how to reload Flask app in runtime
It can be useful for the use case where you want to enable/disable blueprints/routes dynamically.
To run the app:
> pip install flask & python app.py
Then test it via curl
@nguyenkims
nguyenkims / WebViewActivity.java
Created April 27, 2016 15:11
webview example that handles facebook comments
package ovh.meo.mypackage;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
@nguyenkims
nguyenkims / generate_365_days.sql
Created April 4, 2016 10:24
Fill up table "dates" with all the days from 1/1/2016 till 12/31/2016
DROP PROCEDURE IF EXISTS fillupdate;
DELIMITER //
CREATE PROCEDURE fillupdate()
BEGIN
DECLARE nbdate INT DEFAULT 0;
DECLARE dt DATETIME DEFAULT 0;
SET dt = '2016-01-01';
class Animal(db.Model):
__abstract__ = True
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100))
class Cat(Animal):
__tablename__ = "cat"
meo = db.Column(db.String(10))
class Dog(Animal):
"""
To debug the program, send the SIGUSR2 to the PID. You can find the PID by using 'ps aux | grep python'
kill -SIGUSR2 {PID}
"""
import signal
import pdb
def open_debug(sig, frame):
print 'sig', sig
pdb.set_trace()
@nguyenkims
nguyenkims / admin.py
Last active December 2, 2015 09:40
flask admin authentication
import flask_admin as admin
from flask import request, make_response, redirect, g
from flask.ext.admin import BaseView
from flask.ext.admin.contrib.sqla import ModelView
from flask_admin import Admin, expose
class MyModelView(ModelView):
"""Only admin can see"""
column_filters = ['id']