Tips and Techniques

From EventScripts Community Encyclopedia


Tutorial: Tips and Techniques

Requires: ES 2.0+
Difficulty: Easy
Author: *XYZ*SaYnt et al.
Contributors: ~
Length: ~

Tutorial Overview

Table of Contents

Contents

Description

Tips and Techniques all Python Eventscripts programmers should know.

Tutorial Content

Debugging techniques

Logging all exceptions in your scripts to console

When 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.

Optimization: psyco

One 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()

Messaging techniques

To 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)

Miscellania

Setting version info and public variable

Somewhere 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")


Testing for Mani / EST

def hasMani():
    return es.exists("variable", "mani_admin_plugin_version")
 
def hasEST():
    return es.exists("variable", "est_version")

Conclusion

Ellipsis...

Thanks for reading

blog comments powered by Disqus