[GRASS-SVN] r74437 - grass/branches/releasebranch_7_4/lib/python/script

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Apr 29 14:21:28 PDT 2019


Author: sbl
Date: 2019-04-29 14:21:28 -0700 (Mon, 29 Apr 2019)
New Revision: 74437

Modified:
   grass/branches/releasebranch_7_4/lib/python/script/core.py
Log:
backport tempname function, see #3691

Modified: grass/branches/releasebranch_7_4/lib/python/script/core.py
===================================================================
--- grass/branches/releasebranch_7_4/lib/python/script/core.py	2019-04-29 18:54:43 UTC (rev 74436)
+++ grass/branches/releasebranch_7_4/lib/python/script/core.py	2019-04-29 21:21:28 UTC (rev 74437)
@@ -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