Python Forum
Override a library function that is not in __all__
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Override a library function that is not in __all__
#1
Greetings Community,

I would like to override a function of the library of networkx that is not included in __all__.

More specifically:

import networkx ​as nx

def geometric_edges(G, radius, p):
   ​return []

...
nx.generators.geometric.geometric_edges = geometric_edges
return nx.random_geometric_graph(number, radius, pos=pos)
I also tried:

import networkx as nx
from unittest.mock import patch as patch

def geometric_edges(G, radius, p):
   ​return []

...
patch("nx.generators.geometric.geometric_edges", geometric_edges)
return nx.random_geometric_graph(number, radius, pos=pos)
Both attempts did fail so the original function will still be executed instead of the replacement returning an empty list which should lead to an unconnected graph.

Could you possibly explain to me which ways I have to achieve my goal and how they are executed correctly?

Thank you very much in advance.

Kind regards,
Weird
Reply
#2
you need to inherit class you wish to override.
There are many good examples, here's one: https://www.geeksforgeeks.org/method-ove...in-python/
Reply
#3
(Aug-11-2021, 03:24 PM)Larz60+ Wrote: you need to inherit class you wish to override.
There are many good examples, here's one: https://www.geeksforgeeks.org/method-ove...in-python/

Sorry, I forgot to add a git reference: https://github.com/networkx/networkx/blo...ometric.py

The module is not a class but a collection of functions. Therefore my two approaches above that didn't work. I hope the git reference helps you to help me Shy

Also "override" might not be the correct technical term I assume it pretty much describes my intention.

@Edit: I guess the lack of further comments signals a significant misunderstanding on my side - could somebody help me to notify it?
Reply
#4
I don't know whether it is okay to push your thread after some days. So I'll try and in case it is not, I will know for future interactions in this forum.
Reply
#5
see: https://dabeaz-course.github.io/practica...tance.html
you need to create an instance of nx before you can redefine one of it's methods.

see especially section labeled 'Redefining an existing method'
Reply
#6
Hi,
didn't knew that it is possible in python to instantiate modules.

Kind regards,
Weird
Reply
#7
I would like to add a question regarding this topic:

Is there a way to globally override the function - somehow export the instantiated module for all modules of my project?

Or somehow create a new module that takes networkx overrides this method and allows me to import it as networky?
Reply
#8
create a class, and then import into other modules.

in list below, I should have pointed you there before, but forgot about these:
more on classes:
class basics
class intermediate inheritance
class advanced operator overloading
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Calling a function (which accesses a library) from another file mouse9095 4 821 Jun-07-2023, 08:55 PM
Last Post: deanhystad
  python update binary object (override delivered Object properties) pierre38 4 1,772 May-19-2022, 07:52 AM
Last Post: pierre38
  dict class override: how access parent values? Andrey 1 1,639 Mar-06-2022, 10:49 PM
Last Post: deanhystad
  Due to MDLable ID does not have get attribute how to override on this jayyu 0 2,832 Dec-20-2021, 10:38 AM
Last Post: jayyu
  How to use a function from third party library? rrowhe4d 2 1,864 Aug-31-2021, 04:30 PM
Last Post: Larz60+
  python library not defined in user defined function johnEmScott 2 3,869 May-30-2020, 04:14 AM
Last Post: DT2000
  Merge dicts without override chisox721 4 3,244 Jul-20-2019, 01:45 AM
Last Post: chisox721
  Could I override a fully implemented method zatlas1 2 2,413 Jun-06-2019, 02:20 AM
Last Post: zatlas1
  How can we override decorator? bhojendra 2 9,378 May-12-2019, 11:15 PM
Last Post: ichabod801
  Trouble on importing function from astropy library samsonite 4 3,711 Mar-21-2019, 12:18 PM
Last Post: samsonite

Forum Jump:

User Panel Messages

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