[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:
 so that one is fine.

 The only remaining thing would then be to add a script.core.tempname()
 function somewhere here I suppose:

 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
     :rtype: str


     >>> tempname(12)

     if lowercase:
         chars = string.ascii_lowercase + string.digits
         chars = string.ascii_uppercase + string.ascii_lowercase +
     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