[GRASS-dev] [GRASS GIS] #3615: Two new functions for grass.script python lib

GRASS GIS trac at osgeo.org
Tue Aug 14 01:13:16 PDT 2018


#3615: Two new functions for grass.script python lib
--------------------------+-------------------------
  Reporter:  sbl          |      Owner:  grass-dev@…
      Type:  enhancement  |     Status:  new
  Priority:  normal       |  Milestone:  7.6.0
 Component:  Python       |    Version:  svn-trunk
Resolution:               |   Keywords:
       CPU:  All          |   Platform:  All
--------------------------+-------------------------

Comment (by sbl):

 OK, I found:
 https://grass.osgeo.org/grass74/manuals/libpython/script.html#script.task.cmdstring_to_tuple
 so that one is fine.

 The only remaining thing would then be to add a script.core.tempname()
 function somewhere here I suppose:
 https://grass.osgeo.org/grass75/manuals/libpython/script.html#script.core.tempdir

 My function below would require additional imports though, hope that would
 be OK...
 Generated names are SQL and GRASS mp name compliant.

 {{{
 import string
 import random

 def tempname(length, lowercase=False):
     """Generate a random name of length "length" starting with a letter

     :param int length: length of the random name to generate
     :param bool lowercase: use only lowercase characters to generate name
     :returns: String with a random name of length "length" starting with a
 letter
     :rtype: str

     :Example:

     >>> tempname(12)
     'MxMa1kAS13s9'
     """

     if lowercase:
         chars = string.ascii_lowercase + string.digits
     else:
         chars = string.ascii_uppercase + string.ascii_lowercase +
 string.digits
     randomname = '1'
     while randomname[0].isdigit():
         randomname = ''.join(random.choice(chars) for _ in range(length))

     return randomname

 }}}

-- 
Ticket URL: <https://trac.osgeo.org/grass/ticket/3615#comment:1>
GRASS GIS <https://grass.osgeo.org>



More information about the grass-dev mailing list