[Qgis-developer] (First?) API break

Pirmin Kalberer pi_ml at sourcepole.com
Tue Dec 13 07:34:54 EST 2011


Hi all,

Am Sonntag, 11. Dezember 2011, um 18.12:08 schrieb Anne Ghisla:
> On Sun, 11 Dec 2011 15:26:55 +0100
> 
> Andreas Neumann <a.neumann at carto.net> wrote:
> > in javascript there is try and catch for such problems. Ore one could
> > test if an object/method/property is present. There are a lot of
> > browser differencies - so in a web environment it is quite normal to
> > test for capabilities.
> > 
> > How can one check if an object/method is present in Python?
> 
> Hello Andreas, all,
> 
> the check can be done during import:
> 
> """
> try:
>     from qgis.core import newFunctionName as function
> except:
>     from qgis.core import oldFunctionName as function
> """
> 
> so that you can use "function" anywhere in the code without further
> checks.
> To keep the import code short, only the previous version of the API is
> tested. If a plugin developer wants to extend support to even older
> versions, I would recommend to create a separate "LTS" version.
> What do you think?


I support Annes approach.
It's appropriate to use the Duck typing style in Python and not the C++ style 
(if API_VERSION >= 2.0): http://en.wikipedia.org/wiki/Duck_typing#In_Python
If a method is called only once, one could also write
if hasattr(composerView, "addComposerShape"):
...

I've started a section on Marco's Wiki page for this:
http://hub.qgis.org/wiki/quantum-gis/API_changes_for_version_20#Best-
practises-for-Plugin-developers

Pirmin

> > 
> > On 12/11/2011 02:10 PM, Paolo Cavallini wrote:
> > > Il 11/12/2011 10:57, Ziegler Stefan ha scritto:
> > >> Hi
> > >> 
> > >> What is the best practice to deal with such API breaks (regarding
> > >> python plugins)?
> > > 
> > > Can we have a conditional fix (if api_version=2.0)? This way we
> > > should not maintain two copies of the same plugins, much better if
> > > someone works with qgis stable but also tests master.
> > > All the best.


-- 
Pirmin Kalberer
Sourcepole  -  Linux & Open Source Solutions
http://www.sourcepole.com


More information about the Qgis-developer mailing list