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

#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
Keywords: | CPU: All
Platform: All |
-------------------------+-------------------------
If you agree, I would like to add two new functions to grass.script python
library, that I could not find in existing lib and that I would like to
use in other module(s):

1. A function that parses a GRASS shell command (e.g. from history) into a
dictionary that can be used in pygrass modules:
https://trac.osgeo.org/grass/browser/sandbox/sbl/t.rast.aggregate.update/t.rast.aggregate.update.py#L67

2: A very basic function, that generates a random name for GRASS maps that
are SQL and GRASS compliant:
https://trac.osgeo.org/grass/browser/sandbox/sbl/t.rast.aggregate.update/t.rast.aggregate.update.py#L116

Docstrings would have to be improved, probably, and I am of course open to
changing names or any other suggestions...
If someone can point me to existing functions that do this which I
overlooked, I am happy to use those instead...

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3615&gt;
GRASS GIS <https://grass.osgeo.org>

#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&gt;
GRASS GIS <https://grass.osgeo.org>

#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):

Since I did not hear any objections, I will commit this function to trunk
preferably before 7.6 branch is cut. So I can use it in some addons that I
have in progress...

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3615#comment:2&gt;
GRASS GIS <https://grass.osgeo.org>

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

* status: new => closed
* resolution: => fixed

Comment:

In [changeset:"73205" 73205]:
{{{
#!CommitTicketReference repository="" revision="73205"
add tempname() function to grass.script.core, fix #3615
}}}

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3615#comment:3&gt;
GRASS GIS <https://grass.osgeo.org>