Silly, fun and useful things to do with Resolver One
  • HOME
  • About
  • Articles
  • Fun
  • Snippets
  • Disclaimer
  • Useful Links

Site by Voidspace

Email Michael

Download Resolver One

RSS Feed RSS Feed

Resolver Hacks contains information and code for doing fun and useful things with Resolver One. This site is created and maintained by Michael Foord, not by Resolver Systems. Please read the disclaimer before using any of the code on this site.

Site Built with rest2web

Creative Commons License

IronPython Wiki

IronPython in Action IronPython in Action

The Main Module

Hello World from inside a module

Strictly speaking this is a hack, but occasionally it can be useful.

In Python the top-level script runs with the name __main__. That means that inside other modules you can get access to the namespace of the top-level script with import __main__.

In Resolver One, spreadsheets are also executed with the name __main__. Unfortunately though, probably as an artifact of the way that each spreadsheet is executed inside its own Python Engine, this import returns us a .NET 'module' object rather than a Python one. This means that you access names inside the spreadsheet namespace by indexing the Globals collection rather than through attributes on the module (as you would with a normal Python module).

The following snippet shows this in action, using a conditional hasattr so that it works with both .NET and Python modules:

def sayhello():
    import __main__
    if hasattr(__main__, 'Globals'):
        workbook = __main__.Globals['workbook']
    else:
        workbook = __main__.workbook

    for col in range(10):
        for row in range(10):
            workbook['Sheet1'][col + 1, row + 1] = 'Hello World'
    print 'Hello World'

If you put this code into a Python file, and import and call sayHello from a Resolver One spreadsheet, then it will populate 'Sheet1' with a 10x10 block of cells saying 'Hello World'. This is the helloworld module that comes with Resolverforge.


Hosted by Webfaction

Return to Top

Last edited Sat Feb 09 00:59:49 2008.

Copyright ©2007 Michael Foord. All rights reserved. Design by Elemental Works. Logo by FuchsiaShock. Valid XHTML & CSS.

This work is licensed under a Creative Commons License.