Module areixio.utils.utility
Functions
def almost_equal(a, b)
def authentication(bypass: bool = False, debug: bool = False)
def ceil_to(value: float, target: float) ‑> float
-
Similar to math.ceil function, but to target float number.
def create_report_folder(base: str = None) ‑> str
-
Create folder with name same as your strategy script
Args
base
:str
, optional- The base path. Defaults to None.
Returns
srt
- The abosulte path of the folder
def find_nearest_datetime(dt_list: List[datetime.datetime], target_dt: datetime.datetime)
-
Given a list of datetime objects (dt_list) and a target datetime object (target_dt), this function finds the datetime object from the list that is closest in time to the target datetime object and returns it.
def floor_to(value: float, target: float) ‑> float
-
Similar to math.floor function, but to target float number.
def get_auth_path()
def get_digits(value: float) ‑> int
-
Get number of digits after decimal point.
def get_user_info()
def int_to_string(number: int, alphabet: List[str], padding: Optional[int] = None) ‑> str
-
Convert a number to a string, using the given alphabet.
The output has the most significant digit first.
def is_integer_num(n)
-
Check if the float is integer number
def prec_round(value: float, target: float) ‑> float
-
Round to specified precision
def resample_agg_gen(columns: list)
def round_to(value: float, target: float) ‑> float
-
Round price to price tick value.
def set_user_info(username: str = None, password: str = None)
def string_to_int(string: str, alphabet: List[str]) ‑> int
-
Convert a string to a number, using the given alphabet.
The input is assumed to have the most significant digit first.
Classes
class ShortUUID (alphabet: Optional[str] = None)
-
Instantiate a ShortUUID object.
>>> shortuuid = ShortUUID() You can then generate a short UUID:
>>> shortuuid.uuid() 'vytxeTZskVKR7C7WgdSP3d' If you prefer a version 5 UUID, you can pass a name (DNS or URL) to the call and it will be used as a namespace (uuid.NAMESPACE_DNS or uuid.NAMESPACE_URL) for the resulting UUID:
>>> shortuuid.uuid(name="example.com") 'exu3DTbj2ncsn9tLdLWspw'
>>> shortuuid.uuid(name="<http://example.com>") 'shortuuid.uuid(name="<http://example.com>")' You can also generate a cryptographically secure random string (using os.urandom() internally) with:
>>> shortuuid.ShortUUID().random(length=22) 'RaF56o2r58hTKT7AYS9doj' To see the alphabet that is being used to generate new UUIDs:
>>> shortuuid.get_alphabet() '23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' If you want to use your own alphabet to generate UUIDs, use set_alphabet():
>>> shortuuid.set_alphabet("aaaaabcdefgh1230123") >>> shortuuid.uuid() '0agee20aa1hehebcagddhedddc0d2chhab3b' The default alphabet matches the regex [2-9A-HJ-NP-Za-km-z]{22}.
shortuuid will automatically sort and remove duplicates from your alphabet to ensure consistency:
>>> shortuuid.get_alphabet() '0123abcdefgh' If the default 22 digits are too long for you, you can get shorter IDs by just truncating the string to the desired length. The IDs won't be universally unique any longer, but the probability of a collision will still be very low.
To serialize existing UUIDs, use encode() and decode():
>>> import uuid >>> u = uuid.uuid4() >>> u UUID('6ca4f0f8-2508-4bac-b8f1-5d1e3da2247a')
>>> s = shortuuid.encode(u) >>> s 'MLpZDiEXM4VsUryR9oE8uc'
>>> shortuuid.decode(s) == u True
>>> short = s[:7] >>> short 'MLpZDiE'
>>> h = shortuuid.decode(short) UUID('00000000-0000-0000-0000-009a5b27f8b9')
>>> shortuuid.decode(shortuuid.encode(h)) == h True Class-based usage If you need to have various alphabets per-thread, you can use the ShortUUID class, like so:
>>> su = shortuuid.ShortUUID(alphabet="01345678") >>> su.uuid() '034636353306816784480643806546503818874456'
>>> su.get_alphabet() '01345678'
>>> su.set_alphabet("21345687654123456") >>> su.get_alphabet() '12345678'
Methods
def decode(self, string: str, legacy: bool = False) ‑> uuid.UUID
-
Decode a string according to the current alphabet into a UUID.
Raises ValueError when encountering illegal characters or a too-long string.
If string too short, fills leftmost (MSB) bits with 0.
Pass
legacy=True
if your UUID was encoded with a ShortUUID version prior to 1.0.0. def encode(self, uuid: uuid.UUID, pad_length: Optional[int] = None) ‑> str
-
Encode a UUID into a string (LSB first) according to the alphabet.
If leftmost (MSB) bits are 0, the string might be shorter.
def encoded_length(self, num_bytes: int = 16) ‑> int
-
Return the string length of the shortened UUID.
def get_alphabet(self) ‑> str
-
Return the current alphabet used for new UUIDs.
def random(self, length: Optional[int] = None) ‑> str
-
Generate and return a cryptographically secure short random string of
length
. def set_alphabet(self, alphabet: str) ‑> None
-
Set the alphabet to be used for new UUIDs.
def uuid(self, name: Optional[str] = None, pad_length: Optional[int] = None) ‑> str
-
Generate and return a UUID.
If the name parameter is provided, set the namespace to the provided name and generate a UUID.