# Path: crumbs/utils/optional_modules.py
# MSG = 'A python package to run this executable is required,'
# BIO = 'biopython'
# BIO_BGZF = 'biopython with Bgzf support'
#     NCBIXML = create_fake_class(MSG + BIO)
#     NCBIWWW = create_fake_class(MSG + BIO)
# def create_fake_class(msg):
#         def __init__(self, *args, **kwargs):
# def create_fake_funct(msg):
#     def FakeRequiredfunct(*args, **kwargs):
#     class FakePythonRequiredClass(object):
# 
# Path: crumbs/exceptions.py
# class SampleSizeError(Exception):
#     'The asked sample size is bigger than the population'
#     pass


import random
import sqlite3
import cPickle as pickle
from itertools import izip_longest, islice, tee, izip
from tempfile import NamedTemporaryFile
from collections import namedtuple
from crumbs.utils.optional_modules import merge_sorted
from crumbs.exceptions import SampleSizeError
    # See also:
    # http://stackoverflow.com/questions/12128948/python-random-lines-from-
    # subfolders/

    if in_disk:
        sample_ = _ListLikeDb()
    else:
        sample_ = []
    too_big_sample = True
    for index, elem in enumerate(iterator):
        if len(sample_) < sample_size:
            sample_.append(elem)
        else:
            too_big_sample = False
            if random.randint(0, index) < sample_size:
                sample_[random.randint(0, sample_size - 1)] = elem
    if too_big_sample:
        raise SampleSizeError('Sample larger than population')
    return iter(sample_)


def sample_low_mem(iterator, iter_length, sample_size):
    'It makes a sample from the given iterator'
    # This implementation will use less memory when the number of sampled items
    # is quite high.
    # It requires to know the number of items beforehand

    if sample_size <= 0:
        raise SampleSizeError('No items to sample')
    elif sample_size > iter_length:

# Path: crumbs/utils/optional_modules.py
# MSG = 'A python package to run this executable is required,'
# BIO = 'biopython'
# BIO_BGZF = 'biopython with Bgzf support'
#     NCBIXML = create_fake_class(MSG + BIO)
#     NCBIWWW = create_fake_class(MSG + BIO)
# def create_fake_class(msg):
#         def __init__(self, *args, **kwargs):
# def create_fake_funct(msg):
#     def FakeRequiredfunct(*args, **kwargs):
#     class FakePythonRequiredClass(object):
# 
# Path: crumbs/exceptions.py
# class SampleSizeError(Exception):
#     'The asked sample size is bigger than the population'
#     pass


# Path: crumbs/iterutils.py
import random
import sqlite3
import cPickle as pickle
from itertools import izip_longest, islice, tee, izip
from tempfile import NamedTemporaryFile
from collections import namedtuple
from crumbs.utils.optional_modules import merge_sorted
from crumbs.exceptions import SampleSizeError

    # See also:
    # http://stackoverflow.com/questions/12128948/python-random-lines-from-
    # subfolders/

    if in_disk:
        sample_ = _ListLikeDb()
    else:
        sample_ = []
    too_big_sample = True
    for index, elem in enumerate(iterator):
        if len(sample_) < sample_size:
            sample_.append(elem)
        else:
            too_big_sample = False
            if random.randint(0, index) < sample_size:
                sample_[random.randint(0, sample_size - 1)] = elem
    if too_big_sample:
        raise SampleSizeError('Sample larger than population')
    return iter(sample_)


def sample_low_mem(iterator, iter_length, sample_size):
    'It makes a sample from the given iterator'
    # This implementation will use less memory when the number of sampled items
    # is quite high.
    # It requires to know the number of items beforehand

    if sample_size <= 0:
        raise SampleSizeError('No items to sample')
    elif sample_size > iter_length:

        raise SampleSizeError('Sample larger than population')