Skip to content
This repository has been archived by the owner on Jan 13, 2023. It is now read-only.

TryteString.random() use cls.LEN as its default length, when available #167

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion iota/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class TryteString(JsonSerializable):
IMPORTANT: A TryteString does not represent a numeric value!
"""
@classmethod
def random(cls, length):
def random(cls, length=None):
# type: (int) -> TryteString
"""
Generates a random sequence of trytes.
Expand All @@ -60,6 +60,16 @@ def random(cls, length):
alphabet = list(itervalues(AsciiTrytesCodec.alphabet))
generator = SystemRandom()

try:
if length is None:
length = cls.LEN

if length <= 0:
raise TypeError("length parameter needs to be greater than zero")
except AttributeError: # class has no LEN attribute
if length is None:
raise TypeError("{class_name} does not define a length property".format(class_name=cls.__name__))

# :py:meth:`SystemRandom.choices` wasn't added until Python 3.6;
# for compatibility, we will continue to use ``choice`` in a loop.
# https://docs.python.org/3/library/random.html#random.choices
Expand Down