stanford.green¶
Functions of general use to the Stanford community.
- stanford.green.random_uid(prefix: str = '', length: int | None = None) str¶
Create a random Stanford-compliant uid.
- Parameters:
prefix (str) – a string prefix (defaults to the empty string)
length (int or None) – desired length of the resulting uid (optional)
- Returns:
a random string between three and eight characters long that is a valid Stanford uid unless
lengthis provided in which case the returned string will have lengthlength. Ifprefixis supplied then the returned string will start withprefixAND the length of returned string will be eight.- Raises:
Exception – if
prefixis eight or more characters there is not enough space to put the rest of the randomly generated characters and in this case an Exception will be raised.
Algorithm
1. If neither
prefixnorlengthare given then a string of length between 3 and 8 is returned.2. If
lengthis given and is smaller then 3 or larger than 8, an exception is raised.3. If
lengthis given and is between 3 and 8 (inclusive), then a string of length oflengthis returned.4. If
prefixis given but notlength, then a string of length 8 prefixed byprefixis returned.5. If
prefixandlengthare both given, then a string of lengthlength+ the length ofprefixis returned.Examples:
random_uid() --> dherkk random_uid() --> ioervion random_uid('test') --> testcwq random_uid('abcdefgh') --> Exception (prefix is too long) random_uid(length=5) --> azxlg random_uid(prefix='abc', length=4) --> abclsah
- stanford.green.utc_datetime_secs_from_now(secs: int) datetime¶
Get the datetime.datetime object corresponding to
secsseconds from now.- Returns:
a
datetime.datetimeobject in the UTC timezone corresponding to the current time plussecsseconds.- Return type:
datetime.datetime