Python Forum
Calls to Attributes of a Class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calls to Attributes of a Class
#1
how does a direct call to openpyxl.load_workbook work?

According to OpenPyXL documentation, load_workbook is a submodule of a the subpackage reader i.e:
openpyxl.reader.excel.load_workbook

However a direct call to openpyxl.load_workbook works fine while using other submodules of openpyxl need the full callout. See below:

Initially in my code I had:
import openpyxl
try:
    openpyxl.load_workbook(filename)
except openpyxl.InvalidFileException as ife:
    print(ife)
which gave me the following error:
Error:
except openpyxl.InvalidFileException as ife: AttributeError: module 'openpyxl' has no attribute 'InvalidFileException'
however, when I changed line 4 to:
except openpyxl.utils.exceptions.InvalidFileException as ife:
exception handling work without an error.

Why full Callout required on one and not the other submodule? I could not find anything in the Documentation.
Reply
#2
it depends what names are available on top level

>>> import openpyxl
>>> dir(openpyxl)
['DEFUSEDXML', 'LXML', 'NUMPY', 'Workbook', '__author__', '__author_email__', '__builtins__', '__cached__', '__doc__', '__file__', 
'__license__', '__loader__', '__maintainer_email__', '__name__', '__package__', '__path__', '__spec__', '__url__', 
'__version__', '_constants', 'cell', 'chart', 'chartsheet', 'comments', 'compat', 
'constants', 'descriptors', 'drawing', 'formatting', 'formula', 'load_workbook', 
'open', 'packaging', 'pivot', 'reader', 'styles', 'utils', 'workbook', 'worksheet', 'writer', 'xml']
load_workbook is made available/is exposed on top level

Here is the __init__.py source code
https://foss.heptapod.net/openpyxl/openp..._init__.py

In other words - it's a decision of the developer how to organise their code and what names to be exposed top level.
SKarimi likes this post
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
Also, note that open is alias for load_workbook
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
#4
Thanks buran. Exactly what I was looking for. Thumbs Up
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem With Database Calls and Load Timbo03 1 156 Nov-21-2021, 10:48 AM
Last Post: Timbo03
  creating a list of dictionaries from API calls AndrewEnglsh101 5 1,238 Apr-03-2020, 02:21 PM
Last Post: AndrewEnglsh101
  SQL Alchemy dynamic class - declarative_base losing attributes mrdominikku 4 1,819 Jan-10-2020, 06:46 PM
Last Post: mrdominikku
  Need help with a function that calls other functions. skurrtboi 4 1,048 Sep-30-2019, 09:28 PM
Last Post: stullis
  how to add class instance attributes from list 999masks 2 1,360 Jul-22-2019, 07:59 AM
Last Post: 999masks
  How to intercept python method calls? DineshBhat 0 1,349 Jul-15-2019, 03:46 AM
Last Post: DineshBhat
  How to split a string containing function calls? Metalman488 4 1,659 Oct-27-2018, 06:50 PM
Last Post: Metalman488
  Testing function calls jenselme 1 1,503 Jul-25-2018, 10:33 AM
Last Post: Larz60+
  function state between calls Skaperen 5 3,298 Feb-08-2018, 02:20 AM
Last Post: Skaperen
  Is it possible to loop through class attributes via string? 04chiak 3 8,022 Feb-04-2018, 09:29 PM
Last Post: 04chiak

Forum Jump:

User Panel Messages

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