top level of PyhonModule

   4630   8   0
User Avatar
Member
58 posts
Joined: Sept. 2009
Offline
Here is a simple script in my OTL Operator Type Properties:
=====================================
import inspect
def runFunction():
print inspect.getmodule(runFunction)

runFunction()
=====================================
when I run this scripts inside Operator Type Properties, it returns “None”.
However, that is not what i expect. I thought I would get at least a “hou” or “PyhonModule” module.

It returns me “__main__” if I run this script in a regular python shell outside houdini. It is the way I want.

Does there anyone know the reason why houdini has a “None” top level and is there any way to cheat houdini, to created a fake temp top level module inside my OTL Operator Type Properties?
User Avatar
Member
7710 posts
Joined: July 2005
Offline
Why do you need a top level module?
User Avatar
Member
58 posts
Joined: Sept. 2009
Offline
Wrap a function form other external module code.

the code would be setarrt(module, function_name, wrapper)

so the error message is: “None” object has no attribute function_name
because there is a __main__ top level module when I run the wrap module in a regular Python shell, it runs without error.
User Avatar
Member
7710 posts
Joined: July 2005
Offline
Then you can check for None, no?
User Avatar
Member
58 posts
Joined: Sept. 2009
Offline
no, sadly the error message shows up.
“AttributeError: ‘None’ object has no attribute ''functionName”
seems “None” module has no attribute.
User Avatar
Staff
270 posts
Joined: July 2005
Offline
I'm not sure I fully understand what you want to do. Do you want to make a function in a different module appear as though it's defined in the code inside type properties?

If so, one way accessing the current module's dictionary is via globals():
globals() = other_function

Of course, if you're already running this from outside a function/method, you could just write:
function_name = other_function
or
from other_module import other_function
User Avatar
Member
58 posts
Joined: Sept. 2009
Offline

from functools import wraps
import inspect
def foo(func):

print “wrap”

@wraps(func)
def with_logging(*args, **kwargs):
print func.__name__ + “ wrapped”
return func(*args, **kwargs)

print “wrapII”
print inspect.getmodule(func), func.__name__
setattr(inspect.getmodule(func), func.__name__, with_logging)
return with_logging


def toWrap(x):
print x + x * x




foo(toWrap)
toWrap(2)

so, run this code outside houdini is okay, but error raised inside type properties.

this will make my question more clear
User Avatar
Staff
270 posts
Joined: July 2005
Offline
Ah. One way to do it is to access the globals dict where the function is defined using the __globals__ attribute of the function object:


from functools import wraps
import inspect
def foo(func):

print “wrap”

@wraps(func)
def with_logging(*args, **kwargs):
print func.__name__ + “ wrapped”
return func(*args, **kwargs)

print “wrapII”
if inspect.getmodule(func) is None:
func.__globals__ = with_logging
else:
setattr(inspect.getmodule(func), func.__name__, with_logging)
return with_logging


def toWrap(x):
print x + x * x




foo(toWrap)
toWrap(2)
User Avatar
Member
58 posts
Joined: Sept. 2009
Offline
Thank you a lot, lucas!
This does help!!
  • Quick Links