Python (36)

Getting SQL query string from Django orm query

I often want to see the actual SQL generated by the orm query. This is important especially if you are running some DML queries like update, select etc and want to ensure that generated sql is exactly what you had in mind. If the query results in a queryset, its really straight forward. goto ./ […]

Temporary Files in Python

Let me start with an actual use case scenario. As a backend developer, I need to process the user uploaded file data all the time. Here temporary files shine. The best part about these is that they make cleanup easier. If you make a real file, you need to use some OS level utility to […]

Removing non-ascii characters from text in Python

I was handling some text scraped using Scrapy and the text had non-ascii unicode charcters like \u003e. If I did this, it didn’t work: html_text = response.text.encode(‘ascii’, errors=’ignore’).decode() Here response.text is the string that contains unicode text (scrapy returns strings encoded in unicode). The html_text still had non ascii unicode characters like \u003e This worked: […]

Working with Python lxml parser for creating XML elements

lxml parser can be a bit confusing because of the sheer range of options it offers. Here are a few cookbook style examples. XML Generation Target code: <root_element xmlns=”” some_more_params=”12345-678-ABC” yet_more_params=”POKEMON-SUCKS”> <element> <element_data1 type=”sometype”><![CDATA[SomeVal123]]></element_data1> <element_data2>12345</element_data2> <element_data3 some_attr=”some_attr”>More random data</element_data3> </element> <another_element> <element_data1 type=”sometype”><![CDATA[SomeVal1234]]></element_data1> <element_data2>12345</element_data2> <element_data3 some_attr=”some_attr”>More random data</element_data3> </another_element> </root_element> Ok, Here is the code […]

Steps for adding ImageField in Django, Django Rest Framework

1. pip install pillow 2. modify the task app model and add ImageField to handle images 3. modify serializer for image and file field 4. create a folder in base project directory to store MEDIA data (i.e.: images, uploaded files) 5. Add MEDIA related settings (MEDIA_ROOT & MEDIA_URL) ins file. 6. Modify in […]

Flask globals – request context – application context – session

I have come to Flask from Django and in this post I am writing down my thoughts as I get more comfortable with Flask globals. Request and Application Context from flask import request, current_app, g request context In that simple line, we have imported three global objects. One is the request object and other two […]

Secret Key generation for Django and Flask

Both Django and Flask rely on SECRET_KEY to generate things like session IDs, cookies etc. Here is a safe way to generate them. Note that this relies on the secrets module introduced in Python 3.6 and onwards. From the Python docs: The secrets module is used for generating cryptographically strong random numbers suitable for managing […]

Flask + SQLAlchemy tips

First and foremost: this : This is the old Flask website built in flask. Some patterns are outdated. But still pretty nice to gloss over with a cup of tea and some buiscuits. 🙂 scoped_session(sessionmaker()) or plain sessionmaker() ? Well, the recommended pattern is to use scoped_session as it is considered thread safe. […]

Install psycopg2 on Ubuntu 18.04+

I was trying to install psycopg2 database adapter for PostgreSQL on Ubuntu 18.04. And was always getting an error when doing “pipenv install psycopg2”, complaining about “Python.h” missing. After much head-scratching, I found the issue. Multiple Python distributions confuse psycopg2 installation. Its like this. psycopg2 has two main dependencies libpq-dev python3-dev. On Ubuntu, you install […]

Configure Pycharm Community Edition to run Flask

Setup Environment Step 1: pipenv install –python 3.8 Step 2: pipenv shell Step 3: pipenv install flask flask-sqlalchemy flask-marshmallow marshmallow-sqlalchemy flask-migrate psycopg2 flask-sqlalchemy is the sql-alchemy with flask bindings. flask-marshmallow is like the Serializer in Django Rest Framework, marshmallow-sqlalchemy is common binding between marshmallow and sqlalchemy (for things like ModelSerializer in Django Rest Framework).flask-migrate is […]