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
length
is provided in which case the returned string will have lengthlength
. Ifprefix
is supplied then the returned string will start withprefix
AND the length of returned string will be eight.- Raises:
Exception – if
prefix
is 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
prefix
norlength
are given then a string of length between 3 and 8 is returned.2. If
length
is given and is smaller then 3 or larger than 8, an exception is raised.3. If
length
is given and is between 3 and 8 (inclusive), then a string of length oflength
is returned.4. If
prefix
is given but notlength
, then a string of length 8 prefixed byprefix
is returned.5. If
prefix
andlength
are both given, then a string of lengthlength
+ the length ofprefix
is 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
secs
seconds from now.- Returns:
a
datetime.datetime
object in the UTC timezone corresponding to the current time plussecs
seconds.- Return type:
datetime.datetime