Apr-01-2020, 12:43 PM
Hi
I recently decided to learn Python, based on the Python documentation https://docs.python.org/3. I take the opportunity of this learning to code a script, of which the aim is to create a translation table to collate ancient greek.
The issue concerns the definition of what is a block of code, and its scope as regards the definition of variables.
Besides those definitions, it appears a very strange handling of a line of code, which both produces the correct output and raises an exception.
Hereunder is an extract from this script.
Arbiel
I recently decided to learn Python, based on the Python documentation https://docs.python.org/3. I take the opportunity of this learning to code a script, of which the aim is to create a translation table to collate ancient greek.
The issue concerns the definition of what is a block of code, and its scope as regards the definition of variables.
Besides those definitions, it appears a very strange handling of a line of code, which both produces the correct output and raises an exception.
Hereunder is an extract from this script.
#!/usr/bin/ python3 # -*- coding: utf-8 -*- def lireUnicode (bloc): symbol=None titre=None unicd=None if symbol==None: print('symbol is None') from html.parser import HTMLParser import requests class MyHTMLParser(HTMLParser): ################# traitement des tags de début (<tag …) def handle_starttag(self, tag, attrs): global detone nonlocal deb_car if tag == 'html': deb_car=False elif tag == 'section' and 'symbols-block__grid' in attrs[0]: deb_car=True elif tag == 'div' and deb_car: #'symbols-grid__item u0000 symbol-copy' in attrs[0] : print(attrs) for x in attrs: if x[0]== "data-symbol": symbol=x[1] elif x[0]=="title": titre=x[1] elif x[0]=="onclick": unicd='U+' + x[1].split("/")[2] else: pass # for x in attrs[1:4]: # print (x[0].replace("data-","") + " = '" + x[1] + "'") # for x in attrs[1:4]: # exec(x[0].replace("data-","") + " = '" + x[1] + "'") print(symbol) print(unicd) print(titre) else: pass ######################### traitement des tags de fin (/> ou </tag> def handle_endtag(self, tag): nonlocal deb_car if tag == 'section': deb_car = False try: html=requests.get(bloc, timeout=2 ) except requests.exceptions.ConnectionError: print("La connexion au site de l'Unicode n'a pas pu être établie") return False except requests.exceptions.Timeout: print("Le serveur de l'Unicode ne répond pas") return False else: pass deb_car=False parser=MyHTMLParser(convert_charrefs=True) parser.feed(html.text) parser.close()To show up the issue, I ran
>>> import sys >>> sys.path[0]='/home/remi/Documents/programmation/python' >>> import exemple >>> exemple.lireUnicode("https://unicode-table.com/fr/blocks/greek-extended/") symbol is None [('class', 'symbols-grid__item u1c00 symbol-copy'), ('data-symbol', 'ἀ'), ('title', 'Lettre minuscule grecque alpha esprit doux'), ('onclick', "location.href='/fr/1F00/'")] ἀ U+1F00 Lettre minuscule grecque alpha esprit doux [('class', 'symbols-grid__symbol')] Traceback (most recent call last):Here is the whole error message
Error:Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/remi/Documents/programmation/python/exemple.py", line 58, in lireUnicode
parser.feed(html.text)
File "/usr/lib/python3.8/html/parser.py", line 111, in feed
self.goahead(0)
File "/usr/lib/python3.8/html/parser.py", line 171, in goahead
k = self.parse_starttag(i)
File "/usr/lib/python3.8/html/parser.py", line 345, in parse_starttag
self.handle_starttag(tag, attrs)
File "/home/remi/Documents/programmation/python/exemple.py", line 35, in handle_starttag
print(symbol)
UnboundLocalError: local variable 'symbol' referenced before assignment
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 "<stdin>", line 1, in <module>
File "/home/remi/Documents/programmation/python/exemple.py", line 58, in lireUnicode
parser.feed(html.text)
File "/usr/lib/python3.8/html/parser.py", line 111, in feed
self.goahead(0)
File "/usr/lib/python3.8/html/parser.py", line 171, in goahead
k = self.parse_starttag(i)
File "/usr/lib/python3.8/html/parser.py", line 345, in parse_starttag
self.handle_starttag(tag, attrs)
File "/home/remi/Documents/programmation/python/exemple.py", line 35, in handle_starttag
print(symbol)
UnboundLocalError: local variable 'symbol' referenced before assignment
>>>
What do you think of all of that.Arbiel