User:Legoktm/toolforge library

From Wikitech
Jump to: navigation, search

toolforge is a simple Python 3 library to help with some common tasks that Toolforge users might encounter. Source code is published on Github. Patches welcome.


pip install toolforge.


An example how to connect to a replicating database without needing to remember all the various options.

import toolforge
conn = toolforge.connect('enwiki')  # You can also use "enwiki_p"
# conn is a pymysql.connection object.
with conn.cursor() as cur:
    cur.execute(query)  # Or something....
Set the default requests user-agent to one that complies with the Wikimedia User-Agent policy:
import requests
import toolforge

# Sets user-agent to:
# mycooltool (; python-requests/2.13.0

Make flask tools require HTTPS:

from flask import Flask
import toolforge

app = Flask(__name__)