py-gdalogr-cookbook icon indicating copy to clipboard operation
py-gdalogr-cookbook copied to clipboard

Print functions compatible with python 2 or 3

Open ValerieAnne563 opened this issue 4 years ago • 0 comments

Still an essential python reference, the cookbook snippets would be easier to test and run if the code was compatible with python 2 or 3. attn: @pcjericks @jericks

Changes include

Replace print **args with print(**args) so that snippets in the cookbook will work for python 2 or 3.

Does not include:

  • Checks for iterator safety (range, iteritems, etc.) Willing to add these in a follow-up change

  • references to ```print >> sys.stderr **args`` in vector_layers.rst. Options include:

  1. leave it as-is (python 2 only)
  2. print("fatal error", file=sys.stderr) (python 3 only)
  3. from __future__ import print_function ; print("fatal error", file=sys.stderr) (works for 2 or 3 but requires import in the cookbook
$ git grep stderr
vector_layers.rst:            print >> sys.stderr, '[ ERROR ]: layer name = "%s" could not be found in database "%s"' % ( lyr_name, databaseName )
vector_layers.rst:            print >> sys.stderr, '[ ERROR ]: you must pass at least one argument -- the layer name argument'
vector_layers.rst:            print >> sys.stderr, '[ ERROR ]: layer name = "%s" could not be found in database "%s"' % ( lyr_name, databaseName )
vector_layers.rst:            print >> sys.stderr, '[ ERROR ]: you must pass at least one argument -- the layer name argument'
vector_layers.rst:            print >> sys.stderr, '[ ERROR ]: layer name = "%s" could not be found in database "%s"' % ( lyr_name, databaseName )
vector_layers.rst:            print >> sys.stderr, '[ ERROR ]: you must pass at least one argument -- the layer name argument'

ValerieAnne563 avatar Nov 09 '20 16:11 ValerieAnne563