Introducing the symboldict module

Updated Gribouillis 4 Tallied Votes 515 Views Share

I uploaded a module named symboldict to the python package index (pypi). This snippet shows how to use it to create a dictionary of symbols from various modules. This dictionary can be used as a common language shared by several modules. It can be used to load python libraries in a lazy way. It also brings the power of python dictionaries to manipulate collections of functions.

"""This script demonstrates how to use module symboldict"""
# python 2 and 3

from symboldict import symbol, SymbolDict

# This symboldict instance refers symbolically to functions
# living in various modules. It defines a glossary of functions
# used in this script an potentially shared by other modules.

sy = SymbolDict(
    print_exc = symbol.traceback.print_exc,
    isdir = symbol.os.path.isdir,
    mkdtemp = symbol.tempfile.mkdtemp,
    pjoin = symbol.os.path.join,
    rmtree = symbol.shutil.rmtree,
    sleep = symbol.time.sleep,
    Tempdir = symbol.tempfile.TemporaryDirectory,
)

# The following code uses the symboldict to access external functions.
# The corresponding modules are imported lazily when they are needed

def function():
    root = sy.mkdtemp()
    try:
        spamfn = sy.pjoin(root, "spam.txt")
        try:
            x = 1/0
        except Exception as exc:
            with open(spamfn, 'w') as spam:  
                sy.print_exc(file=spam)
            print("Hello!!!")
            with open(spamfn) as spam:
                print(spam.read())
    finally:
        sy.rmtree(root)
    return root

if __name__ == '__main__':
    root = function()
    print('please wait ...')
    sy.sleep(0.5)
    if sy.isdir(root):
        print('Directory {} still exists'.format(root))
    else:
        print('Directory {} has been removed'.format(root))
Gribouillis 1,391 Programming Explorer Team Colleague

Upgrade today to version 0.3.0! It is an optimized version with respect to the efficiency of attribute access. To upgrade, use

pip install --upgrade symboldict

2015 jan 18

Edit: I hope some members of this forum will use symboldict in their programs and give some feedback in this thread!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.