Alembic set autogenerate model

Alembic create revision autogenerate with comment: $ alembic -c dev.ini revision –autogenerate -m “adding some tables” Check upgrade script: $ cat alembic/versions/123xxx_xxx.py Alembic upgrade metadata with version $ alembic -c dev.ini upgrade 123xxx Alembic upgrade metadata with latest version $ alembic -c dev.ini upgrade head  

Create AWS codebuild project with Terraform

Summary AWS Codebuild is fully managed build service that compiles source code, run tests, and produces software packages that are ready to reploy. To make it easier, we can create it’s infrastructure using terraform. Setup directory structure Before we begin, we can create our own directory structure for the infrastructure. Why this is important? because … Read more

Python migration with Alembic

$ pip install alembic $ alembic init –template generic alembic edit alembic.ini sqlalchemy.url = mysql://root:@localhost/database_name $ alembic current $ alembic revision -m “Init” $ alembic upgrade head INFO [alembic.migration] Context impl MySQLImpl. INFO [alembic.migration] Will assume non-transactional DDL. INFO [alembic.migration] Running upgrade None -> 174f01a0ar12, Init

Setup python app in centos from scratch (centos 6.9+uwsgi+nginx+flask+mysql)

Initial setup $ sudo yum update $ sudo yum install epel-release $ sudo yum groupinstall “Development tools” $ sudo yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel telnet htop $ sudo yum install python-devel python-virtualenv $ sudo yum install mysql-connector-python mysql-devel mysql-server Install Python Download and install Python : https://www.python.org/ ./configure && make && make altinstall Install … Read more

Setup uWSGI and Emperor on Centos 6.xx

uWSGI is driving me crazy when install it, every single time. I’m using Centos 6.9 to setup my python app. This one is for setup uWSGI and make sure you already installed Python2.7 on your machine: $ wget https://bootstrap.pypa.io/get-pip.py $ which python2.7 $ sudo /usr/local/bin/python2.7 get-pip.py $ which pip2.7 $ sudo /usr/local/bin/pip2.7 install uWSGI $ … Read more

Post twitter using python script

$ pip install –upgrade pip $ pip install tweepy import tweepy def get_api(cfg): auth = tweepy.OAuthHandler(cfg[‘consumer_key’], cfg[‘consumer_secret’]) auth.set_access_token(cfg[‘access_token’], cfg[‘access_token_secret’]) return tweepy.API(auth) def main(): cfg = { “consumer_key” : “xxxxx”, “consumer_secret” : “xxxxx”, “access_token” : “xxxxx”, “access_token_secret” : “xxxxx” } api = get_api(cfg) tweet = “my status here” status = api.update_status(status=tweet) if __name__ == “__main__”: main()

Create watermark and resize images with Python

After holiday, I have many pictures that needs to be uploaded and all of them should have watermark. Watermark in image is one thing that I could’ve done it with photoshop when I was in school. But I’m not gonna waste my time to do that one by one instead I’m doing with this simple … Read more

Make paging output from your python script

pydoc is a python library to make your console output paginates for easier reading. to use it follow the simple example below: import pydoc text = “…paginate me…” pager = pydoc.ttypager(text) print pager you will see the output is gonna be truncated at the beginning when you start to see like when you open up … Read more

Django table is marked as crashed and should be repaired

My django app is showing 500 because one of the tables in database marked crashed and should be repaired. InternalError at / (145, “Table ‘./dbname/django_session’ is marked as crashed and should be repaired”) I thought it was just an usual error from mysql and needed to restart but it wasn’t. Solutions:  Login to mysql, select your … Read more

Get json results from mysql with python

This is just simple trick to retrieve data from mysql rows and turn into json. #!/usr/bin/env python import MySQLdb as mdb import sys con = mdb.connect(‘localhost’, ‘root’, ‘pass123’, ‘mydb1′, charset=’utf8’) cur = con.cursor(mdb.cursors.DictCursor) def get_post(): list = [] gw = {} cur.execute(“select * from tbl1_core”) rows = cur.fetchall() for i in rows: tes = [] # … Read more