Wrapper around :py:class:`random.SystemRandom`. Provided for ease of use and to avoid having to initialize a SystemRandom object every time something random is desired. .. note:: The default character set when generating random variable names or strings is the a
| 8 | |
| 9 | |
| 10 | class RandomGen(object): |
| 11 | """ |
| 12 | Wrapper around :py:class:`random.SystemRandom`. |
| 13 | Provided for ease of use and to avoid |
| 14 | having to initialize a SystemRandom object |
| 15 | every time something random is desired. |
| 16 | |
| 17 | .. note:: |
| 18 | The default character set when generating random variable names |
| 19 | or strings is the alphanumeric charset, or the (almost) full ASCII |
| 20 | charset if :meth:`~RandomGen.setFullAsciiStrings` is called. |
| 21 | """ |
| 22 | randGen = random.SystemRandom() |
| 23 | _generatedVars = set() |
| 24 | _uniqueRandStrs = set() |
| 25 | |
| 26 | _randStrCharList = [c for c in string.ascii_letters + string.digits + string.punctuation] |
| 27 | _randStrCharList.remove("'") |
| 28 | _randStrCharList.remove("/") |
| 29 | |
| 30 | _reservedVars = {"auto_resume", "BASH", "BASH_ENV", "BASH_VERSINFO", "BASH_VERSION", "CDPATH", "COLUMNS", "COMP_CWORD", "COMP_LINE", "COMP_POINT", "COMPREPLY", "COMP_WORDS", "DIRSTACK", "EUID", "FCEDIT", "FIGNORE", "FUNCNAME", "GLOBIGNORE", "GROUPS", "histchars", "HISTCMD", "HISTCONTROL", "HISTFILE", "HISTFILESIZE", "HISTIGNORE", "HISTSIZE", "HOME", "HOSTFILE", "HOSTNAME", "HOSTTYPE", "IFS", "IGNOREEOF", "INPUTRC", "LANG", "LC_ALL", "LC_COLLATE", "LC_CTYPE", "LC_MESSAGES", "LC_NUMERIC", "LINENO", "LINES", "MACHTYPE", "MAIL", "MAILCHECK", "MAILPATH", "OLDPWD", "OPTARG", "OPTERR", "OPTIND", "OSTYPE", "PATH", "PIPESTATUS", "POSIXLY_CORRECT", "PPID", "PROMPT_COMMAND", "PS1", "PS2", "PS3", "PS4", "PWD", "RANDOM", "REPLY", "SECONDS", "SHELLOPTS", "SHLVL", "TIMEFORMAT", "TMOUT", "UID"} |
| 31 | _boblReservedStrsRegex = re.compile("DATA|END") |
| 32 | |
| 33 | _boblSyntaxRegex = re.compile(r":\w+:|\^ \^|\? \?|% %|\* \*|#\d+#|&\d+&|DATA|END") |
| 34 | |
| 35 | def __init__(self): |
| 36 | self.sizePref = None |
| 37 | |
| 38 | def setFullAsciiStrings(self): |
| 39 | """ |
| 40 | Set the default charset used when generating random |
| 41 | variables and strings to the (almost) full ASCII charset. |
| 42 | Only "'" and "/" are not used. |
| 43 | """ |
| 44 | RandomGen._randStrCharList = [chr(i) for i in range(1, 128)] |
| 45 | RandomGen._randStrCharList.remove("'") |
| 46 | RandomGen._randStrCharList.remove("/") |
| 47 | |
| 48 | # TODO: make this functionality local to each RandomGen instance |
| 49 | def forgetUniqueStrs(self): |
| 50 | """ |
| 51 | Clear the sets of previously generated variable names |
| 52 | and strings. Should be called when random variable |
| 53 | names/strings are needed but can have the same name as |
| 54 | previously generated variable names/strings without |
| 55 | causing conflicts. |
| 56 | """ |
| 57 | RandomGen._generatedVars.clear() |
| 58 | RandomGen._uniqueRandStrs.clear() |
| 59 | |
| 60 | def randGenNum(self, min, max): |
| 61 | """ |
| 62 | Randomly generate an integer inclusively. |
| 63 | |
| 64 | :param min: minimum integer that can be returned |
| 65 | :type min: int |
| 66 | :param max: maximum integer that can be returned |
| 67 | :type max: int |