Python Forum
How to modify __init__ of built-in module directly from the script?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to modify __init__ of built-in module directly from the script?
#1
Hi,

I am using a f5-common-python library which I installed with pip.

There is a 'asm' directory which has this content:

Output:
~/.local/lib/python3.6/site-packages/f5/bigip/tm/asm$ attack_types.py file_transfer.py __init__.py policies policy_templates.py __pycache__ signature_sets.py signatures.py signature_statuses.py signature_systems.py signature_update.py tasks.py
The __init__.py looks like this:

from f5.bigip.resource import OrganizingCollection
from f5.bigip.tm.asm.attack_types import Attack_Types_s
from f5.bigip.tm.asm.file_transfer import File_Transfer
from f5.bigip.tm.asm.policies import Policies_s
from f5.bigip.tm.asm.policy_templates import Policy_Templates_s
from f5.bigip.tm.asm.signature_sets import Signature_Sets_s
from f5.bigip.tm.asm.signature_statuses import Signature_Statuses_s
from f5.bigip.tm.asm.signature_systems import Signature_Systems_s
from f5.bigip.tm.asm.signature_update import Signature_Update
from f5.bigip.tm.asm.signatures import Signatures_s
from f5.bigip.tm.asm.tasks import Tasks

class Asm(OrganizingCollection):
    def __init__(self, tm):
        super(Asm, self).__init__(tm)
        self._meta_data['allowed_lazy_attributes'] = [
            Attack_Types_s,
            File_Transfer,
            Policies_s,
            Policy_Templates_s,
            Signature_Sets_s,
            Signature_Statuses_s,
            Signature_Systems_s,
            Signature_Update,
            Signatures_s,
            Tasks,
        ]
I want to add functionality to this library by adding a new file called violations.py into the 'asm' directory.
To be able to work with violations, I need to update the __init__.py by importing Violations_s and adding Violations_s to self._meta_data['allowed_lazy_attributes'], so the __init__.py then looks like this:

from f5.bigip.resource import OrganizingCollection
from f5.bigip.tm.asm.attack_types import Attack_Types_s
from f5.bigip.tm.asm.file_transfer import File_Transfer
from f5.bigip.tm.asm.policies import Policies_s
from f5.bigip.tm.asm.policy_templates import Policy_Templates_s
from f5.bigip.tm.asm.signature_sets import Signature_Sets_s
from f5.bigip.tm.asm.signature_statuses import Signature_Statuses_s
from f5.bigip.tm.asm.signature_systems import Signature_Systems_s
from f5.bigip.tm.asm.signature_update import Signature_Update
from f5.bigip.tm.asm.signatures import Signatures_s
from f5.bigip.tm.asm.tasks import Tasks
from f5.bigip.tm.asm.violations import Violations_s

class Asm(OrganizingCollection):
    def __init__(self, tm):
        super(Asm, self).__init__(tm)
        self._meta_data['allowed_lazy_attributes'] = [
            Attack_Types_s,
            File_Transfer,
            Policies_s,
            Policy_Templates_s,
            Signature_Sets_s,
            Signature_Statuses_s,
            Signature_Systems_s,
            Signature_Update,
            Signatures_s,
            Tasks,
			Violations_s,
        ]
Is it somehow possible in python to make these changes directly in my own script, so that I don't need to touch and change the default distribution files under ~/.local/lib/python3.6/site-packages/ ?

Can I write the Violations_s definition in my own script and then also update self._meta_data['allowed_lazy_attributes'] in the __init__.py file directly from my script?

Thanks.
Martin
Reply
#2
That is called inheritance. You can define your own class that inherits from Asm, that override whatever methods you like
check our tutorial https://python-forum.io/Thread-Classes-C...nheritance

and one more https://www.digitalocean.com/community/t...n-python-3

In any case don't mess with the installed package
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Hi,
Thanks for a quick reply, however it still does not explain my question.

I understand inheritance, at least in the standard form, where child inherits from a parent class, etc.
This is all clear to me.

However, the mentioned library looks to work with inheritance in much more complicated way, to me unclear.

First I must connect to the F5 device:

from f5.bigip import ManagementRoot
mgmt = ManagementRoot(IP, credentials, etc)

Then I would like to get a collection of objects under following hierarchy:
info = mgmt.tm.asm.violations_s.get_collection

My issue is that, to get the collection of violations, I basically must use the 'mgmt' object which holds the opened session to the F5 device.
If I would create my own class which would inherit from 'asm' class how would I attached this class to 'mgmt'?
Reply
#4
(Jul-30-2018, 12:13 PM)sonicblind Wrote: If I would create my own class which would inherit from 'asm' class how would I attached this class to 'mgmt'?

You say you understand inheritance, yet, you want to modify the third-party package source?
If you inherit properly from Asm, your class will have all the functionality of the parent class, i.e. all the methods, properties, etc of Asm class. Of course without the one you override. I don't know what mgmt is or how Asm class is using it.
You show us how you want to change the __init__ method of the original code and I advise you not to mess with the original source, but inherit from it (of course you will need to properly use super() to initialize the parent - in your case Asm). If you override just the __init__ method (like you show in your original post) you will have all the functionality of Asm.
Also, assuming you write the violations policies and you will import it differently
How do you expect to be able to do this import
from f5.bigip.tm.asm.violations import Violations_s
when it does not exists?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
OK, I had a look at the source of the package you are using and it looks like I may misunderstood/overlooked your original question. I also saw you did it with changing the source code and it worked.

Anyway, just for sake of those who may look for answers in the future, I think this should work too (i.e. without need to mess with the code of the installed package):

from f5.bigip import ManagementRoot
from violantions import Violations_s # here you need to make sure the import is correct and works


mgmt = ManagementRoot(IP, credentials, etc)
mgmt.tm.asm._meta_data['allowed_lazy_attributes'].append(Violations_s)
info = mgmt.tm.asm.violations_s.get_collection
If you decide to test it, please let me know what the result is
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
For sure it was not my original intention to mess with third-party package source, but what else can you do if you miss functionality?
It was exactly my original question, how do I do this directly in my own script, so that I don't need to touch the original code.
And because the inheritance in this case is a bit complicated (at least to me), I wasn't sure how to achieve it.

Your last post answered my question.
The line:

mgmt.tm.asm._meta_data['allowed_lazy_attributes'].append(Violations_s)
did the trick!

I have just tested it and it works perfectly.

Those new classes I simply declared directly in my script, so even there is no need for the import statement.
This way I can keep all the code in just one file, while I don't touch the original source code.

Thanks a lot for the tip and help ;-)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Import XML file directly into Excel spreadsheet demdej 0 833 Jan-24-2023, 02:48 PM
Last Post: demdej
  How to modify python script to append data on file using sql server 2019? ahmedbarbary 1 1,211 Aug-03-2022, 06:03 AM
Last Post: Pedroski55
  Make console show after script was built with Pyinstaller --NOCONSOLE? H84Gabor 0 1,206 May-05-2022, 12:32 PM
Last Post: H84Gabor
  No module named '_cffi_backend' error with executable not with python script stephanh 2 5,632 Nov-25-2021, 06:52 AM
Last Post: stephanh
  using WinRt module in python script akhilgnair 0 2,097 Apr-23-2021, 05:38 AM
Last Post: akhilgnair
  How to load log.txt directly into python codes? sparkt 6 2,949 Aug-21-2020, 03:51 PM
Last Post: sparkt
  Get input directly as a number? Pedroski55 4 2,177 May-05-2020, 04:29 PM
Last Post: deanhystad
  TypeError indexing a range of elements directly on the list JFerreira 2 2,198 Mar-30-2020, 04:22 PM
Last Post: bowlofred
  modify script. MuhammadTalha 15 4,629 Nov-23-2019, 04:43 PM
Last Post: Gribouillis
  Is there any way to convert a python script (with Tkinter module), to a .exe (Windows moste 3 3,982 May-12-2019, 12:02 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020