diff --git a/iota/types.py b/iota/types.py index 70372f9..26b25ed 100644 --- a/iota/types.py +++ b/iota/types.py @@ -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. @@ -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