# 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 cPickle as pickle
import sqlite3
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
        else:
            duplicated = key_for_item == prev_key
        if not duplicated:
            yield item
        prev_key = key_for_item
    # An alternative implementation would be:
    # return (first(groups[1]) for groups in groupby(items, key))
    # But it is a little bit slower


def sorted_items(items, key=None, max_items_in_memory=None, tempdir=None):
    if max_items_in_memory:
        grouped_items = group_in_packets(items, max_items_in_memory)
    else:
        grouped_items = [items]

    # sort and write to disk all groups but the last one
    write_to_disk = True if max_items_in_memory else False
    sorted_groups = []
    for group in grouped_items:
        sorted_group = sorted(group, key=key)
        del group
        if write_to_disk:
            group_fhand = _pickle_items(sorted_group, tempdir=tempdir)
            sorted_groups.append(_unpickle_items(open(group_fhand.name)))
        else:
            sorted_groups.append(sorted_group)
        del sorted_group
    if len(sorted_groups) > 1:
        if key is None:

# 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 cPickle as pickle
import sqlite3
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

        else:
            duplicated = key_for_item == prev_key
        if not duplicated:
            yield item
        prev_key = key_for_item
    # An alternative implementation would be:
    # return (first(groups[1]) for groups in groupby(items, key))
    # But it is a little bit slower


def sorted_items(items, key=None, max_items_in_memory=None, tempdir=None):
    if max_items_in_memory:
        grouped_items = group_in_packets(items, max_items_in_memory)
    else:
        grouped_items = [items]

    # sort and write to disk all groups but the last one
    write_to_disk = True if max_items_in_memory else False
    sorted_groups = []
    for group in grouped_items:
        sorted_group = sorted(group, key=key)
        del group
        if write_to_disk:
            group_fhand = _pickle_items(sorted_group, tempdir=tempdir)
            sorted_groups.append(_unpickle_items(open(group_fhand.name)))
        else:
            sorted_groups.append(sorted_group)
        del sorted_group
    if len(sorted_groups) > 1:
        if key is None:

            sorted_items = merge_sorted(*sorted_groups)