mercredi 19 octobre 2022

How to know if a customized random string function returns a string that matches a particular regex on Python 3?

Context:

One of the great features that the Binance API has is to allow its users to set a custom order id (a.k.a. newClientOrderId) for every trade they place on the Binance Futures market, this feature becomes useful if someone wants to keep a track of the trades that were placed following a particular trading strategy.

According to its Offical Docs, the parameter to be sent in the POST request to place the new order must be newClientOrderId and its value has to match this regex criteria: ^[\.A-Z\:/a-z0-9_-]{1,36}$

Problem:

I wrote a simple random string function on Python3 that returns a random string based on 2 parameters which are a string and the length of the desired string to combine, here it is:

import random, string
def randstr(s, length):

    all_chars = string.ascii_lowercase
    result = s
    for i in range(length):
        result += random.choice(all_chars)
    return(result)

Tests

randstr('test1',5)
Out[14]: 'test1uljgp'

randstr('test1',2)
Out[15]: 'test1mo'

randstr('test1',5)
Out[16]: 'test1pbgjw'

How can I know if the output of my custom randstr function matches this regex criteria: ^[\.A-Z\:/a-z0-9_-]{1,36}$? By knowing this, I will ensure that future developments related to this API will keep working nonstop.




Aucun commentaire:

Enregistrer un commentaire