Why is there no documentation for keras.engine? - python

I am trying to use https://github.com/matterport/Mask_RCNN, but get the error
AttributeError: module 'keras.engine' has no attribute 'Layer'.
I'm not the only one who's had this problem - here are some suggestions for how to solve it.
Now, trying to better understand the problem, I have searched for documentation of the keras.engine module, but have found nothing. In the official Keras API reference, there seems to be no mentioning of anything called 'engine'. Why is this the case? How is one supposed to use a module for which there is no documentation?

TLDR
No one** is supposed to use the keras.engine module because it is not part of the public API.
Explanation
In most projects (e.g. Keras) the assumption is that you shouldn't rely on features that are not documented because they can change at any time. I think that's exactly what happened here. As Dr. Snooppy pointed out in the comments, Matterport shouldn't have called keras.engine.Layer for precisely this reason.
Finding docs and source
Keras is open source and keras.engine is full of docstrings, so if you really want to get to the source of something it's not that hard:
Find the site-packages folder with python -m site
Navigate to .../site-packages/tensorflow/keras/python/engine
Check out the source files. Pay particular attention to the class keywords and to class and method docstrings (These are delimited by """).
In my Tensorflow/Keras version 2.6.0, a file search yields that class Layer is now in a different place than the two mentioned in the linked answer: .../site-packages/tensorflow/keras/python/engine/base_layer.py, illustrating the point that anything that's not part of the public API may and does change all the time.
** Obvious exception: the engineers who write code for the Keras library.

Related

Not able to understand the implementation of the os module in cpython

So I wanted to check out some implementations of standard libraries. I started with the os library with the code being here on github.
I took one method for example os.listdir() and I have absolutely no idea how it is implemented even after looking at the code ( pardon this noob ). I have following questions:
os.__all__ do not list this method but I think it is definitely a method as print(type(os.listdir)) listed <class 'builtin_function_or_method'> and I searched on google to find all the builtin functions which I found on this doc page and this is not one of them.
There is not such exclusive function named listdir defined in the module. In the code, from my limited understanding, the function is taken from globals() and put into a support_fd set. How this method is being called I do not understand.
I think the main problem I have is how that module is designed and I was not able to find any resources online to explain in simpler terms hence I am asking here for pointers.
EDIT: For those who are asking, I tried the following code in onlinegdb
import os
if "listdir" in os.__all__:
print("Yes")
print(os.listdir())
The result is only main.py, it should also print Yes, maybe the platform onlinegdb is the problem but it clearly shows the output of listdir as main.py.
After having discussion in the comments I see now that this is more of a online python version problem and not an issue with python or the module itself.

FastText version before most recent change

I was going through old FastText code, and started to realize it doesn't work anymore and expects different parameters. When looking at the dcoumentation , it appears the documentation has been partially updated.
Which it can be seen size and iter are not in the class definition shown in the docs despite being in the parameters. I was wondering if anyone knew exact version where this change has occured as it appears I've accidentally updated it to something newer.
Most changes occurred in gensim-4.0.0. There are a series of notes on the changes & how to adapt your code in the project wiki page, "Migrating from Gensim 3.x to 4":
https://github.com/RaRe-Technologies/gensim/wiki/Migrating-from-Gensim-3.x-to-4
In most cases small changes to the method & variable names older code is using will restore full functionality.
There have been significant fixes and optimizations to the FastText implementation, especially in the realm of reducing memory usage, so you probably don't want to stay on any older version (like gensim-3.8.3) except as a temporary quickie workaround.

How to find method for handling object file in python package trading-calander

I have installed package from https://pypi.org/project/trading-calendars/
When i run get_calendar('XNYS') it produces and object file. How can find out what method does this object have for viewing data? The reason i am asking is because in this case in the documentation shows no examples on its usage.
You must read the documentation. If there is none, the module is ... well ... crap. In such case do print(obj.__dict__) to see all the properties of the object, and if anything is of use to you. Worst come to worst, read the source code of the module on Github.
Here is the documentation as mentioned in trading_calendars#107 (found by going to Issues and searching "documentation", for future reference).

Extract documentation link from Python help() function

Python has a built-in function called help() which returns the docstrings on methods or functions, per the turtle documentation. For example, say I was to type this:
>>> help('os')
I get greeted with
Help on module os:
NAME
os - OS routines for NT or Posix depending on what system we're on.
MODULE REFERENCE
https://docs.python.org/3.8/library/os
The following documentation is automatically generated from the Python
source files. It may be incomplete, incorrect or include features that
are considered implementation detail and may vary between Python
implementations. When in doubt, consult the module reference at the
location listed above.
. . .
and so forth. It is quite simple to find the module reference by just looking at this, but say I was collecting the references to 100 different modules. It would take quite some time and would be very repetitive work.
How can I parse through each help() function for the link to the module's documentation? It would involve finding a similar value such as https:// or .org or .com.
I'd argue that you actually don't need to do any parsing, since as far as I know, all the standard library Python modules have documentation accessible at the URL https://docs.python.org/<version>/library/<modulename>. It would be far more efficient to construct the URL according to that pattern compared to parsing the help text.
That being said, if you really do want to parse the help text, the re.search function should be useful. You can write a regular expression to match the URL of a Python standard library documentation page and presumably the first match should be the result you want.

Do I need to do type checking when preparing library for open source?

I have I small module that I use inside one of my projects. Now I decided to place it on github so now I am writing some docstrings and cleaning the code.
I have a composition of 2 classes so the initialization looks like this:
foo = Class_1()
bar = Class_2(param1=foo)
I know that the first argument to the Class_2 has to be an instance of Class_1 or the code won't work. But it may be clear only for me as I wrote code of Class_2, but when using module as API it may be unclear for a user that param1 has to be an instance of Class_1. If someone will use bar = Class_2(param1='foo'). The trackback will be bad and it will be impossible to understand what happened. So the question is: do I need to check in my __init__ that isinstance(param1, Class_1) and if no raise an excaption with an appropriate message, or writing good documentation is enough?
This is very opinion-based (not great for StackOverflow in particular) - but in my opinion, you should do both.
On the one hand, using isinstance() and exception-handling are both good defensive-coding practices.
On the other hand, inline documentation is nice. Per the Python developer guide:
The markup used for the Python documentation is reStructuredText, developed by the docutils project, amended by custom directives and using a toolset named Sphinx to post-process the HTML output.
Some IDEs, such as JetBrains PyCharm, are configured to automatically pick up well-formed reST docstrings and perform automated type-checking based on those conventions (which I've found to be really useful). See also: PEP 257 and What is the standard Python docstring format? for details.

Categories