#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""Generates random sequences confirming certain restrictions"""


import sys
import random
from Bio.Seq import Seq
from Bio.Alphabet import IUPAC

DEF_ALPHABET = IUPAC.unambiguous_rna
MIN_LEN = 6


class RandSeqException(Exception):
    """A simple custom Exception class for randseq.py"""
    def __init__(self, m):
        message = "[randseq] {}".format(m)
        super(RandSeqException, self).__init__(message)


def get_rand_seq(length, alphabet=None):
    """Returns a random sequence of given length and alphabet"""
    if not (isinstance(length, int) and length >= MIN_LEN):
        raise RandSeqException(
            'get_rand_seq 1 arg has to be an int >= {}'.format(MIN_LEN))
    if alphabet is None:
        alphabet = DEF_ALPHABET
    if not isinstance(alphabet, IUPAC.Alphabet.Alphabet):
        raise RandSeqException(
            'get_rand_seq alphabet arg has to be of IUPAC.Alphabet.Alphabet')
    letters = alphabet.letters
    seq = ''.join([random.choice(letters) for x in range(length)])
    return Seq(seq, alphabet)


def main():
    """Main body of code"""
    print(get_rand_seq(20))


if __name__ == '__main__':
    sys.exit(main())
