Python Forum
Trouble with "from…import…as"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trouble with "from…import…as"
#1
Hi

Trying to understand how to use "from…import…as"

I recorded in "test.py" this extract from a script I'm presently coding

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from html.parser import HTMLParser
from unicodedata import name as ucd_nom
from unicodedata import lookup as ucd_car
#import xml.etree.ElementTree
from xml.etree.ElementTree import ElementTree as arbre
from xml.etree.ElementTree import Element as xml

def le_fichier_xml():
	"""Table de correspondance."""
	return '/home/remi/Documents/programmation/python/grec_vocab/alphabet.xml'
le_fichier_xml()
larbre=arbre()
alphabet=larbre.parse(le_fichier_xml())
for elem in list(alphabet):
	print(elem.tag, elem.attrib)
No problem when I run it
remi@remi-Vostro-3550:~$ python '/home/remi/Documents/programmation/python/test.py' 
div {'id': 'simples'}
div {'id': 'combinées'}
remi@remi-Vostro-3550:~$ 
If I skip the line 15:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from html.parser import HTMLParser
from unicodedata import name as ucd_nom
from unicodedata import lookup as ucd_car
#import xml.etree.ElementTree
from xml.etree.ElementTree import ElementTree as arbre
from xml.etree.ElementTree import Element as xml

def le_fichier_xml():
	"""Table de correspondance."""
	return '/home/remi/Documents/programmation/python/grec_vocab/alphabet.xml'
le_fichier_xml()
#larbre=arbre()
#alphabet=larbre.parse(le_fichier_xml())
alphabet=arbre.parse(le_fichier_xml())
for elem in list(alphabet):
	print(elem.tag, elem.attrib)
it does not run any longer
remi@remi-Vostro-3550:~$ python '/home/remi/Documents/programmation/python/test.py' 
Traceback (most recent call last):
  File "/home/remi/Documents/programmation/python/test.py", line 17, in <module>
    alphabet=arbre.parse(le_fichier_xml())
TypeError: parse() missing 1 required positional argument: 'source'
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
    from apport.fileutils import likely_packaged, get_recent_crashes
  File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
    from apport.report import Report
  File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module>
    import apport.fileutils
  File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module>
    from apport.packaging_impl import impl as packaging
  File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 24, in <module>
    import apt
  File "/usr/lib/python3/dist-packages/apt/__init__.py", line 23, in <module>
    import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'

Original exception was:
Traceback (most recent call last):
  File "/home/remi/Documents/programmation/python/test.py", line 17, in <module>
    alphabet=arbre.parse(le_fichier_xml())
TypeError: parse() missing 1 required positional argument: 'source'
remi@remi-Vostro-3550:~$ 
with an error
Error:
TypeError: parse() missing 1 required positional argument: 'source'
not quite clear, as the python documentation reads
Quote:parse(source, parser=None)

Loads an external XML section into this element tree. source is a file name or file object. parser is an optional parser instance. If not given, the standard XMLParser parser is used. Returns the section root element.

and I however passed a positional parameter.

So, I'm quite confused

Arbiel
Reply
#2
The argument you passed in was assigned to a different parameter. When a method is called, such as ElementTree.parse(), there is an additional parameter often not mentioned in the documentation:

class ElementTree(self, source, parser=None):
If you've already learned how to write classes and methods, that should look familiar. The self parameter is intended to be filled by the instantiated object itself (hence the naming convention). When line 15 is omitted, no object is instantiated - there is no self. So, the interpreter fills in the parameters starting from left to right. The argument passed in becomes "self" and there's nothing left for source.
Reply
#3
Hi stullis

This is very clear.

In fact, I did really know what type of object results from «import». I am quite illiterate in terms of object oriented programming (and in lot of other domains also, even if I'm still older than what the Beatles sung). I'm learning by trying, and searching through the documentation.

I modified line 17 with :
alphabet=arbre.parse(arbre,le_fichier_xml())
and it works.

It is however, I suppose, better to instantiate the object first and to use it later on.

Thank you.

Arbiel
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Having trouble with a circula import SuchUmami 2 710 Jul-05-2023, 06:28 PM
Last Post: deanhystad
  Import from .py trouble michael1789 4 2,325 Nov-27-2019, 11:23 PM
Last Post: michael1789

Forum Jump:

User Panel Messages

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