You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
2.2 KiB
78 lines
2.2 KiB
# hello_world package
|
|
# This file is being exectued upon 'import hello_world'
|
|
#
|
|
# Very comprehensive import doc: https://chrisyeh96.github.io/2017/08/08/definitive-guide-python-imports.html
|
|
#
|
|
# __all__ could be used to limit the symbols exported when using from <pkg> import *
|
|
|
|
|
|
# Import all symbols EXCEPT the ones beginning with underscore into the current module (hello_world)
|
|
from _hello_world import *
|
|
# TODO: inter-pkg ref to make sure which _hello_world in sys.path gets loaded
|
|
# like: ._hello_world
|
|
|
|
# import the module
|
|
# so you can use all the names from the native module, including the ones beginning with underscore)
|
|
import _hello_world
|
|
|
|
from .utils import log_call
|
|
|
|
# Init sequence of the module on 'import hello_world'
|
|
# 1. init_before_main_module() (native)
|
|
# 2. hello_world.init() (python) (this function)
|
|
# 3. _init_after_main_module() (native)
|
|
@log_call
|
|
def init():
|
|
# do some python init here
|
|
_hello_world._init_after_main_module()
|
|
|
|
|
|
# Calls (wraps) a C++ function
|
|
# there is a c++ function _wrapped_cxx_function() which
|
|
# wont be imported into this module because of the underscore
|
|
# so you can wrap it in python and hide it
|
|
# in the wrapped you can augment/change the behaviour
|
|
@log_call
|
|
def wrapped_cxx_function():
|
|
return not _hello_world._wrapped_cxx_function()
|
|
|
|
|
|
# A pure python function
|
|
# we can freely combine python and c++ code
|
|
@log_call
|
|
def pure_python_func(number, string):
|
|
msg = "a string from python made from params n:{}, s:{}".format(number, string)
|
|
return msg
|
|
|
|
# Example callback
|
|
# This function is as a callback target for a
|
|
# function call from C++.
|
|
# use hello_world.trigger_callback()
|
|
@log_call
|
|
def callback_func(number, string):
|
|
print("Default implementation of callback")
|
|
msg = "default string from params n:{}, s:{}".format(number, string)
|
|
return msg
|
|
|
|
# Hybrid C++/Python Class
|
|
# Inherit and extend/overwrite the native class
|
|
# All public methods of the class can only b
|
|
class HybridClass(_hello_world._HybridClass):
|
|
@log_call
|
|
def get_msg(self):
|
|
ret = super().get_msg()
|
|
ret = "<PY_WRAPPED>" + ret + "<\PY_WRAPPED>"
|
|
return ret
|
|
|
|
|
|
# Executed when run as script
|
|
@log_call
|
|
def main():
|
|
print("I am being run as a script")
|
|
|
|
|
|
# MAIN
|
|
if __name__ == "__main__":
|
|
main()
|
|
else:
|
|
init() |