Skip to content

Commit

Permalink
make test_performance work with pre-3.6; add JOSS badge
Browse files Browse the repository at this point in the history
  • Loading branch information
jdidion committed Jun 18, 2017
1 parent 16da830 commit 83e26af
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/b2c0baa52b604e39a09ed108ac2f53ee)](https://www.codacy.com/app/jdidion/xphyle?utm_source=github.com&utm_medium=referral&utm_content=jdidion/xphyle&utm_campaign=Badge_Grade)
[![Documentation Status](https://readthedocs.org/projects/xphyle/badge/?version=latest)](http://xphyle.readthedocs.io/en/latest/?badge=latest)
[![DOI](https://zenodo.org/badge/71260678.svg)](https://zenodo.org/badge/latestdoi/71260678)
[![JOSS](http://joss.theoj.org/papers/10.21105/joss.00255/status.svg)](http://joss.theoj.org/papers/10.21105/joss.00255)

# xphyle: extraordinarily simple file handling

Expand Down
24 changes: 23 additions & 1 deletion tests/test_performance.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
"""Self-contained performance tests.
"""
from bisect import bisect
from contextlib import contextmanager
import gzip
from itertools import accumulate
from random import random, randint
import time
from xphyle.utils import read_lines
from xphyle.paths import TempDir
Expand All @@ -24,6 +27,26 @@ def __exit__(self, exception_type, exception_value, traceback):
duration=self.duration,
**self.msg_args))

def choices(population, weights=None, *, cum_weights=None, k=1):
"""Return a k sized list of population elements chosen with replacement.
If the relative weights or cumulative weights are not specified,
the selections are made with equal probability.
This function is borrowed from the python 3.6 'random' package.
"""
if cum_weights is None:
if weights is None:
_int = int
total = len(population)
return [population[_int(random() * total)] for i in range(k)]
cum_weights = list(accumulate(weights))
elif weights is not None:
raise TypeError('Cannot specify both weights and cumulative weights')
if len(cum_weights) != len(population):
raise ValueError('The number of weights does not match the population')
total = cum_weights[-1]
return [population[bisect(cum_weights, random() * total)] for i in range(k)]

def perftest(name, text_generator, num_iter=10):
# generate a big text
msg = """
Expand Down Expand Up @@ -62,7 +85,6 @@ def test_lorem_ipsum():

@pytest.mark.perf
def test_fastq():
from random import randint, choices
def generate_fastq(seqlen=100):
num_records = randint(100000, 500000)
qualspace = list(chr(i + 33) for i in range(60))
Expand Down

0 comments on commit 83e26af

Please sign in to comment.