Category: Python & Frameworks

  • 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…

  • 学TurboGears 2顺便还得学习一下Genshi

    Genshi是Python环境下的一个比较出名的HTML/XML/…模板框架. 作为TurboGears2.1缺省的模板组件, Genshi得到了很多好评. 我也正是因为TG粘合了如此优秀的组件而决心学习TG的. TG2.1样本项目里的主模板文件master.html的完整解释: http://www.turbogears.org/2.1/docs/main/master_html.html#master-html Genshi模板基础: http://genshi.edgewall.org/wiki/Documentation/templates.html 首先, 在TG2中通过@expose decorator来为一个View(V in MVC)指定Genshi模板. 如果不需要模板, 就是: @expose() def hello(self): return “Hello world from controller.” 这样在缺省的开发环境下, 访问http://127.0.0.1:8080/hello, 将得到简单的 Hello world from controller. 当然, 这除了测试似乎没有什么实际意义. 然后是使用模板的版本: @expose(‘example.templates.index’) def new_hello(self): return dict(hello=”New text from controller + template “, page=”new_hello”) 这样, TG会寻找example项目下的example/templates/index.html作为new_hello的模板. 同时, 将两个key传递给模板: hello和page. 而在index模板内, 可以很随意的调用这两个获得的key. <h1 py:content=”hello”>old Hello, world.</h1> Now…