Generates a random string of specified length. Args: length (int, optional): The length of the random string. Defaults to 10. digits (bool, optional): Whether to include digits in the string. Defaults to True. numeric_only (bool, optional): Whether to generate a num
(length=10, digits=True, numeric_only=False)
| 804 | |
| 805 | |
| 806 | def rand_string(length=10, digits=True, numeric_only=False): |
| 807 | """ |
| 808 | Generates a random string of specified length. |
| 809 | |
| 810 | Args: |
| 811 | length (int, optional): The length of the random string. Defaults to 10. |
| 812 | digits (bool, optional): Whether to include digits in the string. Defaults to True. |
| 813 | numeric_only (bool, optional): Whether to generate a numeric-only string. Defaults to False. |
| 814 | |
| 815 | Returns: |
| 816 | str: A random string of the specified length. |
| 817 | |
| 818 | Examples: |
| 819 | >>> rand_string() |
| 820 | 'c4hp4i9jzx' |
| 821 | >>> rand_string(20) |
| 822 | 'ap4rsdtg5iw7ey7y3oa5' |
| 823 | >>> rand_string(30, digits=False) |
| 824 | 'xdmyxtglqfzqktngkesyulwbfrihva' |
| 825 | >>> rand_string(15, numeric_only=True) |
| 826 | '934857349857395' |
| 827 | """ |
| 828 | if numeric_only: |
| 829 | pool = string.digits |
| 830 | elif digits: |
| 831 | pool = string.ascii_lowercase + string.digits |
| 832 | else: |
| 833 | pool = string.ascii_lowercase |
| 834 | |
| 835 | return "".join(random.choice(pool) for _ in range(length)) |
| 836 | |
| 837 | |
| 838 | def truncate_string(s: str, n: int) -> str: |
no outgoing calls
no test coverage detected
searching dependent graphs…