Python Forum
python update binary object (override delivered Object properties) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: python update binary object (override delivered Object properties) (/thread-37248.html)



python update binary object (override delivered Object properties) - pierre38 - May-17-2022

Hello,
I did many trials without success. I need to have flexibility in my Python code, so I use already .yaml configuration for deliverable scripts (generated executable from pyInstaller), but this is not enough.
I am trying to find out if it is possible to redefine some methods from a python wrapper script calling the executable binary script.

Below is the example:

wrap.py:
import os
# import invest or ??

def displayParam(self):
    print("overriden text")

Foo.displayParam = displayParam

os.system("/prj/mms_nvm/cad_prj/python/users/paolipie/python/py_workdev/create_deliverable/dist/invest/invest")
invest.py
import sys
from src.foo import Foo


def main(argv):
    foo = Foo()
    foo.displayParam()

# exec
if __name__ == "__main__":
    main(sys.argv[1:])
foo.py
import sys
import os
from pathlib import Path

# main
class Foo:
    def __init__(self):
        self.other = None

    def displayParam(self):
        print("default text")
When I execute the wrap.py:
> python wrap.py
Traceback (most recent call last):
File "wrap.py", line 7, in <module>
Foo.displayParam = displayParam
NameError: name 'Foo' is not defined


Can someone provide me some advice ?

thanks a lot,
Pierre


RE: python update binary object (override delivered Object properties) - ibreeden - May-18-2022

(May-17-2022, 03:30 PM)pierre38 Wrote: NameError: name 'Foo' is not defined
That is correct. You did not import foo.


RE: python update binary object (override delivered Object properties) - pierre38 - May-18-2022

Yes and how to "import" it from the delivered script ?

thanks,
Pierre


RE: python update binary object (override delivered Object properties) - bowlofred - May-18-2022

The class is defined in foo.py. So you'd import from that file whenever you need to reference it.

It appears you do that in invest.py. But in wrap.py you refer to Foo without an import. One would be necessary there as well (although the code in wrap.py doesn't seem to call the class properly. Looks like you're overwriting the method rather than calling it).


RE: python update binary object (override delivered Object properties) - pierre38 - May-19-2022

Yes my goal is to override a method. The method is into a python script which is "delivered" via PyInstaller into a binary .so suite of files:

Quote:paolipie@gnbsx26072:/<4>users/paolipie/python/py_workdev/invest_overwritte_func/> ll ../create_deliverable/dist/invest/
base_library.zip invest* _random.cpython-37m-x86_64-linux-gnu.so*
binascii.cpython-37m-x86_64-linux-gnu.so* libbz2.so.1* readline.cpython-37m-x86_64-linux-gnu.so*
_bisect.cpython-37m-x86_64-linux-gnu.so* liblzma.so.5* resource.cpython-37m-x86_64-linux-gnu.so*
_blake2.cpython-37m-x86_64-linux-gnu.so* libpython3.7m.so.1.0* select.cpython-37m-x86_64-linux-gnu.so*
_bz2.cpython-37m-x86_64-linux-gnu.so* libreadline.so.6* _sha1.cpython-37m-x86_64-linux-gnu.so*
_codecs_cn.cpython-37m-x86_64-linux-gnu.so* libtinfo.so.5* _sha256.cpython-37m-x86_64-linux-gnu.so*
_codecs_hk.cpython-37m-x86_64-linux-gnu.so* libz.so.1* _sha3.cpython-37m-x86_64-linux-gnu.so*
_codecs_iso2022.cpython-37m-x86_64-linux-gnu.so* _lzma.cpython-37m-x86_64-linux-gnu.so* _sha512.cpython-37m-x86_64-linux-gnu.so*
_codecs_jp.cpython-37m-x86_64-linux-gnu.so* math.cpython-37m-x86_64-linux-gnu.so* _socket.cpython-37m-x86_64-linux-gnu.so*
_codecs_kr.cpython-37m-x86_64-linux-gnu.so* _md5.cpython-37m-x86_64-linux-gnu.so* _ssl.cpython-37m-x86_64-linux-gnu.so*
_codecs_tw.cpython-37m-x86_64-linux-gnu.so* _multibytecodec.cpython-37m-x86_64-linux-gnu.so* _struct.cpython-37m-x86_64-linux-gnu.so*
_datetime.cpython-37m-x86_64-linux-gnu.so* _opcode.cpython-37m-x86_64-linux-gnu.so* termios.cpython-37m-x86_64-linux-gnu.so*
grp.cpython-37m-x86_64-linux-gnu.so* _pickle.cpython-37m-x86_64-linux-gnu.so* unicodedata.cpython-37m-x86_64-linux-gnu.so*
_hashlib.cpython-37m-x86_64-linux-gnu.so* _posixsubprocess.cpython-37m-x86_64-linux-gnu.so* zlib.cpython-37m-x86_64-linux-gnu.so*
_heapq.cpython-37m-x86_64-linux-gnu.so* pyexpat.cpython-37m-x86_64-linux-gnu.so*

I cannot understand well the import doc: https://docs.python.org/3/reference/import.html#importsystem
It may appear that importing .so "shared libraries as they call" is possible but I did not find how.

Quote:5.5. The Path Based Finder
As mentioned previously, Python comes with several default meta path finders. One of these, called the path based finder (PathFinder), searches an import path, which contains a list of path entries. Each path entry names a location to search for modules.

The path based finder itself doesn’t know how to import anything. Instead, it traverses the individual path entries, associating each of them with a path entry finder that knows how to handle that particular kind of path.

The default set of path entry finders implement all the semantics for finding modules on the file system, handling special file types such as Python source code (.py files), Python byte code (.pyc files) and shared libraries (e.g. .so files). When supported by the zipimport module in the standard library, the default path entry finders also handle loading all of these file types (other than shared libraries) from zipfiles.

thanks for helping,
Pierre