[QGIS-Developer] free variable 'self' referenced before assignment in enclosing scope
Bjørn Burr Nyberg
Bjorn.Nyberg at uib.no
Fri Feb 1 01:18:21 PST 2019
Hi Nyall,
Thank you for your time! I really appreciate it and will definitely implement those suggestions as well.
I am using QGIS 3.4 however the bug seems to be resolved with the your suggestions and if I save the script to the processing toolbox rather than run it from the editor.
Kind regards,
Bjorn
-----Original Message-----
From: Nyall Dawson <nyall.dawson at gmail.com>
Sent: Friday, February 01, 2019 12:21 AM
To: Bjørn Burr Nyberg <Bjorn.Nyberg at uib.no>
Cc: qgis-developer at lists.osgeo.org
Subject: Re: [QGIS-Developer] free variable 'self' referenced before assignment in enclosing scope
On Thu, 31 Jan 2019 at 02:46, Bjørn Burr Nyberg <Bjorn.Nyberg at uib.no> wrote:
>
> Dear QGIS community,
>
> In the spirit of open-source I am trying to convert a set of scripts that ive designed in ArcMap to QGIS 3.x. I hope that this will enable a broader community of individuals to use the workflows ive designed. The original scripts can be found at https://github.com/BjornNyberg/NetworkGT/tree/master/Scripts
Great!
> My problem is that in my first attempt to convert the scripts, QGIS either crashes or gives the following error.
>
> 2019-01-30T16:51:27 WARNING Traceback (most recent call last):
> File "C:/PROGRA~1/QGIS3~1.4/apps/qgis/./python/plugins\processing\gui\AlgorithmDialog.py", line 238, in on_complete
> self.feedback.pushInfo(self.tr('Execution completed in {0:0.2f} seconds').format(time.time() - start_time))
> NameError: free variable 'self' referenced before assignment in enclosing scope
>
This was a bug fixed already - are you on QGIS 3.4?
Anyone, the problem stems from this line:
templines = st.run('native:splitwithlines',parameters)
This should be:
templines =
st.run('native:splitwithlines',parameters,context=context,
feedback=feedback)
I.e. you need to pass the child algorithm the context and feedback
objects given to your algorithm, to allow that child algorithm to push
feedback messages and access existing map layers.
Two other suggestions:
except Exception as e:
feedback.pushInfo(QCoreApplication.translate('Sample_Area','%s'%(e)))
Here this would be better as:
except Exception as e:
feedback.reportError(QCoreApplication.translate('Sample_Area','%s'%(e)))
(reportError instead of pushInfo)
And
for part in geom: #Check for multipart polyline
parts.append(QgsGeometry.fromPolyline(part)) #intersected geometry
This should be
for part in geom.asGeometryCollection(): #Check for multipart polyline
parts.append(QgsGeometry.fromPolyline(part)) #intersected geometry
(or, nicer for 3.4):
for part in geom.parts(): #Check for multipart polyline
parts.append(QgsGeometry.fromPolyline(part)) #intersected geometry
Hope that helps!
Nyall
> I have established that this error occurs at the very end of the script when calling ' return {self.Branches:dest_id,self.Nodes:dest_id2}'. I do not believe that I have explicitly defined self anywhere in the processAlgorithm function itself. The algorithms themselves work as intended and the shapefiles are created before the program crashes.
>
> I am not a programmer by trait so I'm not entirely sure how to problem solve this error and any help would be most appreciated. I can also provide a test dataset if that is helpful.
>
> Cheers,
> Bjorn
> -----------------------------
> Postdoctoral Fellow
> Department of Earth Science, University of Bergen
> PO Box 7803, 5020, Bergen, Norway
> +47 485 024 08
>
>
> _______________________________________________
> QGIS-Developer mailing list
> QGIS-Developer at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
More information about the QGIS-Developer
mailing list