Acquire.ObjectStore._encoding module

bytes_to_string(b)[source]

Return the passed binary bytes safely encoded to a base64 utf-8 string

Parameters:b (bytes) – binary bytes to encode
Returns:UTF-8 encoded string object
Return type:str
string_to_bytes(s)[source]

Return the passed base64 utf-8 encoded binary data back converted from a string back to bytes. Note that this can only convert strings that were encoded using bytes_to_string - you cannot use this to convert arbitrary strings to bytes

Parameters:s (str) – base64 byte object to decode
Returns:bytes object
Return type:bytes
string_to_encoded(s)[source]

Return the passed unicode string encoded to a safely encoded base64 utf-8 string

Parameters:s (str) – String to encode
Returns:Unicode string s encoded to UTF-8
Return type:str
encoded_to_string(b)[source]

Return the passed encoded base64 utf-8 string converted back into a unicode string

Parameters:b (bytes) – base64 utf-8 byte string
Returns:UTF-8 string converted from byte string
Return type:str
url_to_encoded(url)[source]

Return an encoding of the passed url that is safe to use as a name, filename or key in an object store

Parameters:url (str) – URL to encode
Returns:base64 encoded bytes object
Return type:bytes
encoded_to_url(e)[source]

Decode the passed encoded data back to the URL. This will only produce valid output for inputs created using url_to_encoded

Parameters:e (bytes) – base64 encoded byte object
Returns:bytes object as URL
Return type:bytes
decimal_to_string(d)[source]

Return the passed decimal number encoded as a string that can be safely serialised via JSON

Parameters:d (Decimal) – Decimal to convert to string
Returns:String version of Decimal
Return type:str
string_to_decimal(s, default=0)[source]

Return the decimal that had been encoded via ‘decimal_to_string’. This string must have been created via ‘decimal_to_string’

Parameters:s (str) – String to convert to Decimal
Returns:Decimal version of string
Return type:Decimal
datetime_to_string(d)[source]

Return the passed datetime encoded to a string. This will be a standard iso-formatted time in the UTC timezone (converting to UTC if the passed datetime is for another timezone)

Parameters:d (datetime) – Datetime to convert to string
Returns:Datetime as a string
Return type:str
string_to_datetime(s)[source]

Return the datetime that had been encoded to the passed string via datetime_to_string. This string must have been created via ‘datetime_to_string’

Parameters:s (str) – String to convert
Returns:Datetime version of string
Return type:datetime
date_to_string(d)[source]

Return the date that has been encoded to a string. This will write the date as a standard iso-formatted date. IF a datetime is passed then this will be in the UTC timezone (converting to UTC if the passed datetime is for another timezone)

Parameters:d (datetime) – Datetie to convert
Returns:Datetime in ISO format
Return type:str
string_to_date(s)[source]

Return a date from the string that has been encoded using ‘date_to_string’. This is only guaranteed to work for strings that were created using that function

Parameters:s (str) – String from date_to_string function
Returns:Datetime created from string
Return type:datetime
time_to_string(t)[source]

Return the time that has been encoded to a string. This will write the time as a standard iso-formatted time. If a datetime is passed then this will be in the UTC timezone (converting to UTC if the passed datetime is for another timezone)

Parameters:t (time) – Time to convert, can be datetime
Returns:String of passed time converted to UTC ISO format
Return type:str
string_to_time(s)[source]

Return a time from the string that was encoded by ‘time_to_string’. This will only be guaranteed to produce valid output for strings produced using that function

Parameters:s (str) – String to convert to time
Returns:Time object created from string
Return type:datetime.time
get_datetime_now()[source]

Return the current time in the UTC timezone. This creates an object that will be properly stored using datetime_to_string and string_to_datetime

Returns:Current datetime
Return type:datetime
datetime_to_datetime(d)[source]

Return the passed datetime as a datetime that is clean and usable by Acquire. This will move the datetime to UTC, adding the timezone if this is missing

Parameters:d (datetime) – datetime to convert to UTC
Returns:UTC datetime useable by Acquire
Return type:datetime
string_to_safestring(s)[source]

Return a safe (base64) encoded version of ‘s’. This string has no special characters or spaces, thereby making it safe for use, e.g. as a filename or to save in a database

Parameters:s (str) – String to encode to base
Returns:base64 encoded string
Return type:str
safestring_to_string(s)[source]

Return the original string encoded by string_to_safestring

string_to_list(s, C)[source]

Convert the string encoded using list_to_string back to a list of objects of type C. Note that all objects must have the same type

Parameters:
  • s (str) – String to convert
  • C (type) – Type to create
Returns:

List of C objects

Return type:

list

list_to_string(l)[source]

Return the passed list of items converted to a json string. All items should have the same type

Parameters:l (list) – List to convert to JSON
Returns:JSON string
Return type:str
string_to_dict(s, C)[source]

Convert the string encoded using dict_to_string back to a dict of objects of type C. Note that all objects must have the same type

Parameters:
  • s (str) – String to convert
  • C (type) – Type to create
Returns:

Dictionary created from string

Return type:

dict

dict_to_string(d)[source]

Return the passed dict of items converted to a json string. All items should have the same type

Parameters:d (dict) – Dictionary to convert
Returns:JSON version of dict
Return type:str
string_to_filepath(path)[source]

This function cleans the passed path so that doesn’t contain redundant slashes or ‘..’ etc., so that all backslashes are forwards slashes, and that the trailing slash is removed

Parameters:path (str) – Path to clean
Returns:Cleaned path
Return type:str
string_to_filepath_parts(path)[source]

Break the passed path into a list of the individual parts, e.g. /home/user/test/../something/./new.txt will return

[‘home’, ‘user’, ‘something’, ‘new.txt’]

Parameters:path (str) – Path to split
Returns:List of sections of path
Return type:list
get_datetime_future(weeks=0, days=0, hours=0, minutes=0, seconds=0, timedelta=None)[source]

Return the datetime that is the supplied time in the future. This will raise an exception if the time is not in the future!

Parameters:
  • weeks (int, default=0) – Number of weeks in future
  • days (int, default=0) – Number of days in future
  • hours (int, default=0) – Number of hours in future
  • minutes (int, default=0) – Number of minutes in future
  • seconds (int, default=0) – Number of seconds in future
  • timedelta (datetime.timedelta, default=0) – Timedelta from now
get_datetime_now_to_string()[source]

Convenience function that returns the result of get_datetime_now as a string converted via datetime_to_string

Returns:Current datetime as string
Return type:str
date_and_time_to_datetime(date, time=datetime.time(0, 0))[source]

Return the passed date and time as a UTC datetime. By default the time is midnight (first second of the day)

Parameters:
  • date (datetime) – Date (may be any timezone)
  • time (default=_datetime.time(0)) – Time of day, default midnight
Returns:

UTC datetime

Return type:

datetime

date_and_hour_to_datetime(date, hour)[source]

Return the passed date and hour as a UTC datetime. By default the time is hour:00:00 (first second of the hour)

create_uuid(short_uid=False, include_date=None)[source]

Return a newly created random uuid. This is highly likely to be globally unique. If ‘short_uid’ is True, then a shorter, potentially less unique UID will be generated. If ‘include_date’ is passed, then the passed date will be encoded into the UID

Returns:Random UUID
Return type:str
create_uid(short_uid=False, include_date=None)[source]

Synonym for create_uuid