Category: Python & Frameworks

  • Coloured Diff With Python

    TL; DR: my simple Python code to output a result of text comparison in git-diff style Here’s the code snippet in Python. 🙂

  • An Admin-Only Python Decorator for Telegram Bots

    TL; DR: Here’s a Python Decorator I wrote for Telegram Bots so certain commands can only be used by group admins.

  • SAUS: Such A URL Shortener

    TL; DR: It’s a simple but effective URL shortener I wrote in Python + Django, I consider it stable now. Also added QR code generator for mobile applications. Source code is in Github. 🙂

  • Caching in Python with Redis + @Decorator

    TL;DR: Here’s a simple Python decorator I wrote which can cache function results with Redis. Prerequisites The Python Decorator What’s a decorator in Python? Here’s a very easy-to-understand explanation just for the question. Below is my code of the decorator with notes: Done 🙂

  • Cheat Sheet: From SQL to NoSQL

    Recently I was fascinated by MongoDB’s flexibility to change data structure on the fly, in contrast there will need a ALT TABLE SQL statement for any schema change in a SQL database. Here I make a list of typical operations and how they are done in both SQL and NoSQL(in my case, MongoDB) flavours. Create…

  • How to Easily Enable Webhook Mode for Python Telegram Bots

    I created a Telegram bot(@wordsquad_bot) using python-telegram-bot framework. There are 2 modes to run the bot: Since my bot has got almost all features and run stably for a while, I decided to setup webhook for it. I came across a few articles and did a bit of troubleshooting myself so here’s probably the easiest…

  • Internal Service in Kubernetes Cluster

    In Kubernetes(K8s) cluster, 1 or more containers form a pod and every container in the pod can access other container’s port just like apps in the same local host. For example: – pod1 – nginx1 – gunicorn1, port:8000 – pod2 – nginx2 – gunicorn2, port:8000 So nginx1 can access gunicorn1’s port using localhost:8000 and nginx2…

  • A Simple Python IO Trick

    I thought using Python’s gzip is quite straight forward, however the IO performance almost doubled with an extra BufferedWriter: import io import gzip def export_to_json_gz(schema): json_file = “test.json.gz” with io.BufferedWriter( gzip.open(temp_dir + json_file, ‘wb’) ) as gzfile: for row in stream_table_data(schema):       ujson.dump(row, gzfile, ensure_ascii=False)       gzfile.write(‘\n’) Now the bottle neck…