[GRASS-SVN] r73205 - grass/trunk/lib/python/script

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Aug 29 02:44:33 PDT 2018


Author: sbl
Date: 2018-08-29 02:44:33 -0700 (Wed, 29 Aug 2018)
New Revision: 73205

Modified:
   grass/trunk/lib/python/script/core.py
Log:
add tempname() function to grass.script.core, fix #3615

Modified: grass/trunk/lib/python/script/core.py
===================================================================
--- grass/trunk/lib/python/script/core.py	2018-08-28 19:51:06 UTC (rev 73204)
+++ grass/trunk/lib/python/script/core.py	2018-08-29 09:44:33 UTC (rev 73205)
@@ -25,6 +25,8 @@
 import subprocess
 import shutil
 import codecs
+import string
+import random
 import types as python_types
 
 from .utils import KeyValue, parse_key_val, basename, encode
@@ -822,6 +824,30 @@
     return tmp
 
 
+def tempname(length, lowercase=False):
+    """Generate a GRASS and SQL compliant random name starting with tmp_
+    followed by a random part of length "length"
+
+    :param int length: length of the random part of the 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)
+    'tmp_MxMa1kAS13s9'
+    """
+
+    chars = string.ascii_lowercase + string.digits
+    if not lowercase:
+        chars += string.ascii_uppercase
+    random_part = ''.join(random.choice(chars) for _ in range(length))
+    randomname = 'tmp_' + random_part
+
+    return randomname
+
+
 def _compare_projection(dic):
     """Check if projection has some possibility of duplicate names like
     Universal Transverse Mercator and Universe Transverse Mercator and



More information about the grass-commit mailing list