User:Legoktm/wmflib

From Wikitech
Jump to: navigation, search

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

Installation

pip install "wmflabs>=3".

Examples

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

import wmflabs
conn = wmflabs.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 wmflabs

wmflabs.set_user_agent('mycooltool')
# Sets user-agent to:
# mycooltool (https://tools.wmflabs.org/mycooltool; tools.mycooltool@tools.wmflabs.org) python-requests/2.13.0
requests.get('...')