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

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

Follow Michael on:

The Techie Blog

Twitter

Del.icio.us

Shared Feeds

Import with Reload

When you are creating functions and classes to be used inside your spreadsheets, it often makes sense to develop these in Python files and import them into your spreadsheet.

Note

The mechanism used in this article is no longer the best way of forcing re-imports of your modules (although in theory it could be useful if you want to force a reload with every calculation anyway!).

If you cause a recalculation by pressing SHIFT-F9, Resolver One will clear out imported modules and then re-import them when it recalculates.

So long as you have saved the current spreadsheet, you can import Python modules that are saved in the same directory as your spreadsheet. The problem is, when Python imports modules it caches them. That means that if you edit your code, the new version won't be used when the spreadsheet is recalculated. There is a reload function, but this only reloads the module. I usually use the from module import name form, and reload doesn't update names imported like this.

The following code can be used instead of an import, whilst you are working on your modules. It reloads the modules and returns the names you want every time it is executed, so your latest changes will always get picked up.

Warning

Saving your spreadsheet in a different directory will clear the previous directory form the path and remove the previously imported module.

def importWithReload(moduleName, names):
   """
   Returns specific names from a module, after reloading them.

   The moduleName argument should be a string.

   The names argument can either be a list of names (strings), or a single name.
   """

   mod = __import__(moduleName)
   components = moduleName.split('.')
   for comp in components[1:]:
       mod = getattr(mod, comp)
   mod = reload(mod)
   if isinstance(names, list):
       output = []
       for name in names:
           output.append(getattr(mod, name))
       return output
   return getattr(mod, names)

You use it like this:

from module import name1, name2

# Delete this line once you have finished
# developing your module
name1, name2 = reloadWithImport('module', ['name1', 'name2'])


Hosted by Webfaction

Return to Top

Last edited Mon Feb 2 19:33:39 2009.

Copyright ©2007-2009 Michael Foord. All rights reserved. Design by Elemental Works. Logo by FuchsiaShock. Resolver One ™ is an Excel compatible Python powered spreadsheet.

This work is licensed under a Creative Commons License.