Tips and Techniques
|
[edit] Tutorial: Tips and TechniquesRequires: ES 2.0+ [edit] Tutorial Overview[edit] Table of Contents
[edit] DescriptionTips and Techniques all Python Eventscripts programmers should know. [edit] Tutorial Content[edit] Debugging techniques[edit] Logging all exceptions in your scripts to consoleWhen first writing a script, is it extremely useful to have all exceptions logged, so that you can tell when errors occur. The following procedure enables this behavior. First, make sure you have imported the following at the top of your python module: import sys import traceback Then, add this to your module somewhere (usually at the bottom): def except_hook(type, value, tb): error_list = traceback.format_exception(type, value, tb) for message in error_list: es.log("[Exception] " + message.strip()) sys.excepthook = except_hook With this addition, each time a Python exception occurs, you will see a full traceback of where it occurred in the server console. [edit] Optimization: psycoOne quick and dirty optimization is simply to use the psyco module, and you see a speed benefit for free. Simply add the following to the top of your module: import psyco psyco.full() [edit] Messaging techniquesTo you use a log of console messages in your addon, you may want to use prefixes. This would be a hassle adding [Your mod here] in front of every console message, so just use a simple helper function like so: def log(msg): es.log("[MyMod] %s" % msg) [edit] Miscellania[edit] Setting version info and public variableSomewhere near the top of your python file, you can add info = es.AddonInfo() info.name = "CC Team Balancer" info.version = "1.1-alpha" info.author = "*XYZ*SaYnt" info.url = "http://addons.eventscripts.com/addons/view/ccbalance" info.basename = "ccbalance" info.description = "Advanced team balancer" and in your load function: def load(): # your stuff here es.set("ccb_version",info.version) es.makepublic("ccb_version")
[edit] Testing for Mani / ESTdef hasMani(): return es.exists("variable", "mani_admin_plugin_version") def hasEST(): return es.exists("variable", "est_version") [edit] ConclusionEllipsis... [edit] Thanks for reading |
|
