示例#1
0
def init():
    global logger
    logger = logging.getLogger(__name__)
    global _parser_lock
    _parser_lock = modules.mp().Lock()
    global _grammar
    _grammar = make_grammar()
示例#2
0
def server_control(config, daemon_cmd):
    start_cmd = "start"
    stop_cmd = "stop"
    restart_cmd = "restart"

    wbconfig = config["wordbase"]

    pidfile = wbconfig.get("pidfile", "/var/run/" + PROGRAM_NAME + ".pid")
    wbdaemon = WBDaemon(PROGRAM_NAME, pidfile, start_server)
    control_func = None

    if daemon_cmd in (None, start_cmd, restart_cmd):
        random.seed()

        smconfig = config["srvmon"]
        util.srvmon.configure(smconfig)

        modules.init(config)

        dconfig = config["dict"]
        match.configure(dconfig)
        core.configure(dconfig)

        wbdaemon.run_args = (wbconfig, modules.mp())

        if daemon_cmd == start_cmd:
            control_func = wbdaemon.start
        elif daemon_cmd == restart_cmd:
            control_func = wbdaemon.restart
        elif daemon_cmd is None:
            control_func = wbdaemon.run
        else:
            assert False, "unhandled command"

        log_init = True
    elif daemon_cmd == stop_cmd:
        control_func = wbdaemon.stop
        log_init = False
    else:
        print('command "{}" not recognized'.format(daemon_cmd), file=sys.stderr)
        print_help_hint()
        sys.exit(2)

    if log_init:
        logger.debug("initialized")

    control_func()
示例#3
0
from modules import make_pizza as mp
mp(16, 'pepperoni')
mp(12, 'mushrooms', 'green peppers', 'extra cheese')
示例#4
0
import modules

#Functions can be used from modules
modules.make_pizza(16, 'cheese', 'pepperoni')

from modules import make_pizza

#They can be referenced via their namespace or directly if specifically imported.
make_pizza(16, 'cheese', 'sausage')

import modules as m
from modules import make_pizza as mp

#Modules and functions from modules can be assigned an alias
m.make_pizza(16, 'cheese', 'meatballs')
mp(16, 'cheese', 'olives')

from modules import *

#All functions can be imported from a module into the script's namespace
make_pizza(16, 'cheese', 'sausage', 'pepperoni')


#Call Stack Example
def a():
    print('a() starts')
    b()
    d()
    print('a() returns')

示例#5
0
#                           What are function aliases?

'''
Sometimes, you're going to import a function that has a name that is either too long, or conflicts
with a function name in your current file.

To avoid this you can give your function an alias to refer to easier when using dot notation.

For the following example we will import make_pizza() from modules.py and give it the alias
mp so we don't have to continually type out make_pizza.

This also allows us to get away with not using the dot notation as you'll notice
'''
from modules import make_pizza as mp

mp(5, "pepperoni")
#   results:    Making a 5-inch pizza with the following toppings