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


Messages In This Thread
How to modify __init__ of built-in module directly from the script? - by sonicblind - Jul-26-2018, 12:56 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Import XML file directly into Excel spreadsheet demdej 0 874 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,237 Aug-03-2022, 06:03 AM
Last Post: Pedroski55
  Make console show after script was built with Pyinstaller --NOCONSOLE? H84Gabor 0 1,227 May-05-2022, 12:32 PM
Last Post: H84Gabor
  No module named '_cffi_backend' error with executable not with python script stephanh 2 5,716 Nov-25-2021, 06:52 AM
Last Post: stephanh
  using WinRt module in python script akhilgnair 0 2,123 Apr-23-2021, 05:38 AM
Last Post: akhilgnair
  How to load log.txt directly into python codes? sparkt 6 3,005 Aug-21-2020, 03:51 PM
Last Post: sparkt
  Get input directly as a number? Pedroski55 4 2,220 May-05-2020, 04:29 PM
Last Post: deanhystad
  TypeError indexing a range of elements directly on the list JFerreira 2 2,231 Mar-30-2020, 04:22 PM
Last Post: bowlofred
  modify script. MuhammadTalha 15 4,678 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 4,041 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