<commit_before># -*- coding: utf-8 -*-
#
# datetime_utils documentation build configuration file, created by
# sphinx-quickstart on Sat Apr 30 21:57:29 2016.
#
# This file is execfile()d with the current directory set to its
# containing dir.
#
# Note that not all possible configuration values are present in this
# autogenerated file.
#
# All configuration values have a default; values that are commented out
# serve to show the default.

import sys
import os

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#sys.path.insert(0, os.path.abspath('.'))

# -- General configuration ------------------------------------------------

# If your documentation needs a minimal Sphinx version, state it here.
#needs_sphinx = '1.0'

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
    'sphinx.ext.autodoc',
    'sphinx.ext.intersphinx',
]

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']

# The suffix(es) of source filenames.
# You can specify multiple suffix as a list of string:
# source_suffix = ['.rst', '.md']
source_suffix = '.rst'

# The encoding of source files.
#source_encoding = 'utf-8-sig'

# The master toctree document.
master_doc = 'toc'

# General information about the project.
project = u'datetime_utils'
copyright = u'2016, Ann Paul'
author = u'Ann Paul'

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.

# The short X.Y version.
version = u'0.1'
# The full version, including alpha/beta/rc tags.
release = u'0.1'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#
# This is also used if you do content translation via gettext catalogs.
# Usually you set "language" from the command line for these cases.
language = None

# There are two options for replacing |today|: either, you set today to some
# non-false value, then it is used:
#today = ''
# Else, today_fmt is used as the format for a strftime call.
#today_fmt = '%B %d, %Y'

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This patterns also effect to html_static_path and html_extra_path
exclude_patterns = []

# The reST default role (used for this markup: `text`) to use for all
# documents.
#default_role = None

# If true, '()' will be appended to :func: etc. cross-reference text.
#add_function_parentheses = True

# If true, the current module name will be prepended to all description
# unit titles (such as .. function::).
#add_module_names = True

# If true, sectionauthor and moduleauthor directives will be shown in the
# output. They are ignored by default.
#show_authors = False

# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx'

# A list of ignored prefixes for module index sorting.
#modindex_common_prefix = []

# If true, keep warnings as "system message" paragraphs in the built documents.
#keep_warnings = False

# If true, `todo` and `todoList` produce output, else they produce nothing.
todo_include_todos = False

# -- Options for HTML output ----------------------------------------------

# The theme to use for HTML and HTML Help pages.  See the documentation for
# a list of builtin themes.
html_theme = 'default'
on_rtd = os.environ.get('READTHEDOCS', None) == 'True'
if not on_rtd:  # only import and set the theme if we're building docs locally
    import sphinx_rtd_theme
    html_theme = 'sphinx_rtd_theme'
    html_theme_path = [sphinx_rtd_theme.get_html_theme_path()]

# Theme options are theme-specific and customize the look and feel of a theme
# further.  For a list of options available for each theme, see the
# documentation.
#html_theme_options = {}

# Add any paths that contain custom themes here, relative to this directory.
#html_theme_path = []

# The name for this set of Sphinx documents.
# "<project> v<release> documentation" by default.
#html_title = u'datetime_utils v0.1'

# A shorter title for the navigation bar.  Default is the same as html_title.
#html_short_title = None

# The name of an image file (relative to this directory) to place at the top
# of the sidebar.
#html_logo = None

# The name of an image file (relative to this directory) to use as a favicon of
# the docs.  This file should be a Windows icon file (.ico) being 16x16 or 32x32
# pixels large.
#html_favicon = None

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_build']

# Add any extra paths that contain custom files (such as robots.txt or
# .htaccess) here, relative to this directory. These files are copied
# directly to the root of the documentation.
#html_extra_path = []

# If not None, a 'Last updated on:' timestamp is inserted at every page
# bottom, using the given strftime format.
# The empty string is equivalent to '%b %d, %Y'.
#html_last_updated_fmt = None

# If true, SmartyPants will be used to convert quotes and dashes to
# typographically correct entities.
#html_use_smartypants = True

# Custom sidebar templates, maps document names to template names.
#html_sidebars = {}

# Additional templates that should be rendered to pages, maps page names to
# template names.
#html_additional_pages = {}

# If false, no module index is generated.
#html_domain_indices = True

# If false, no index is generated.
#html_use_index = True

# If true, the index is split into individual pages for each letter.
#html_split_index = False

# If true, links to the reST sources are added to the pages.
#html_show_sourcelink = True

# If true, "Created using Sphinx" is shown in the HTML footer. Default is True.
#html_show_sphinx = True

# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True.
#html_show_copyright = True

# If true, an OpenSearch description file will be output, and all pages will
# contain a <link> tag referring to it.  The value of this option must be the
# base URL from which the finished HTML is served.
#html_use_opensearch = ''

# This is the file name suffix for HTML files (e.g. ".xhtml").
#html_file_suffix = None

# Language to be used for generating the HTML full-text search index.
# Sphinx supports the following languages:
#   'da', 'de', 'en', 'es', 'fi', 'fr', 'hu', 'it', 'ja'
#   'nl', 'no', 'pt', 'ro', 'ru', 'sv', 'tr', 'zh'
#html_search_language = 'en'

# A dictionary with options for the search language support, empty by default.
# 'ja' uses this config value.
# 'zh' user can custom change `jieba` dictionary path.
#html_search_options = {'type': 'default'}

# The name of a javascript file (relative to the configuration directory) that
# implements a search results scorer. If empty, the default will be used.
#html_search_scorer = 'scorer.js'

# Output file base name for HTML help builder.
htmlhelp_basename = 'datetime_utilsdoc'

# -- Options for LaTeX output ---------------------------------------------

latex_elements = {
# The paper size ('letterpaper' or 'a4paper').
#'papersize': 'letterpaper',

# The font size ('10pt', '11pt' or '12pt').
#'pointsize': '10pt',

# Additional stuff for the LaTeX preamble.
#'preamble': '',

# Latex figure (float) alignment
#'figure_align': 'htbp',
}

# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title,
#  author, documentclass [howto, manual, or own class]).
latex_documents = [
    (master_doc, 'datetime_utils.tex', u'datetime\\_utils Documentation',
     u'Ann Paul', 'manual'),
]

# The name of an image file (relative to this directory) to place at the top of
# the title page.
#latex_logo = None

# For "manual" documents, if this is true, then toplevel headings are parts,
# not chapters.
#latex_use_parts = False

# If true, show page references after internal links.
#latex_show_pagerefs = False

# If true, show URL addresses after external links.
#latex_show_urls = False

# Documents to append as an appendix to all manuals.
#latex_appendices = []

# If false, no module index is generated.
#latex_domain_indices = True

# -- Options for manual page output ---------------------------------------

# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
    (master_doc, 'datetime_utils', u'datetime_utils Documentation',
     [author], 1)
]

# If true, show URL addresses after external links.
#man_show_urls = False

# -- Options for Texinfo output -------------------------------------------

# Grouping the document tree into Texinfo files. List of tuples
# (source start file, target name, title, author,
#  dir menu entry, description, category)
texinfo_documents = [
    (master_doc, 'datetime_utils', u'datetime_utils Documentation',
     author, 'datetime_utils', 'One line description of project.',
     'Miscellaneous'),
]

# Documents to append as an appendix to all manuals.
#texinfo_appendices = []

# If false, no module index is generated.
#texinfo_domain_indices = True

# How to display URL addresses: 'footnote', 'no', or 'inline'.
#texinfo_show_urls = 'footnote'

# If true, do not generate a @detailmenu in the "Top" node's menu.
#texinfo_no_detailmenu = False
<commit_msg>Another try<commit_after># -*- coding: utf-8 -*-
#
# datetime_utils documentation build configuration file, created by
# sphinx-quickstart on Sat Apr 30 21:57:29 2016.
#
# This file is execfile()d with the current directory set to its
# containing dir.
#
# Note that not all possible configuration values are present in this
# autogenerated file.
#
# All configuration values have a default; values that are commented out
# serve to show the default.

import sys
import os

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#sys.path.insert(0, os.path.abspath('.'))

# -- General configuration ------------------------------------------------

# If your documentation needs a minimal Sphinx version, state it here.
#needs_sphinx = '1.0'

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
    'sphinx.ext.autodoc',
    'sphinx.ext.intersphinx',
]

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']

# The suffix(es) of source filenames.
# You can specify multiple suffix as a list of string:
# source_suffix = ['.rst', '.md']
source_suffix = '.rst'

# The encoding of source files.
#source_encoding = 'utf-8-sig'

# The master toctree document.
master_doc = 'toc'

# General information about the project.
project = u'datetime_utils'
copyright = u'2016, Ann Paul'
author = u'Ann Paul'

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.

# The short X.Y version.
version = u'0.1'
# The full version, including alpha/beta/rc tags.
release = u'0.1'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#
# This is also used if you do content translation via gettext catalogs.
# Usually you set "language" from the command line for these cases.
language = None

# There are two options for replacing |today|: either, you set today to some
# non-false value, then it is used:
#today = ''
# Else, today_fmt is used as the format for a strftime call.
#today_fmt = '%B %d, %Y'

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This patterns also effect to html_static_path and html_extra_path
exclude_patterns = ['_build']

# The reST default role (used for this markup: `text`) to use for all
# documents.
#default_role = None

# If true, '()' will be appended to :func: etc. cross-reference text.
#add_function_parentheses = True

# If true, the current module name will be prepended to all description
# unit titles (such as .. function::).
#add_module_names = True

# If true, sectionauthor and moduleauthor directives will be shown in the
# output. They are ignored by default.
#show_authors = False

# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx'

# A list of ignored prefixes for module index sorting.
#modindex_common_prefix = []

# If true, keep warnings as "system message" paragraphs in the built documents.
#keep_warnings = False

# If true, `todo` and `todoList` produce output, else they produce nothing.
todo_include_todos = False

# -- Options for HTML output ----------------------------------------------

# The theme to use for HTML and HTML Help pages.  See the documentation for
# a list of builtin themes.
html_theme = 'default'
on_rtd = os.environ.get('READTHEDOCS', None) == 'True'
if not on_rtd:  # only import and set the theme if we're building docs locally
    import sphinx_rtd_theme
    html_theme = 'sphinx_rtd_theme'
    html_theme_path = [sphinx_rtd_theme.get_html_theme_path()]

# Theme options are theme-specific and customize the look and feel of a theme
# further.  For a list of options available for each theme, see the
# documentation.
#html_theme_options = {}

# Add any paths that contain custom themes here, relative to this directory.
#html_theme_path = []

# The name for this set of Sphinx documents.
# "<project> v<release> documentation" by default.
#html_title = u'datetime_utils v0.1'

# A shorter title for the navigation bar.  Default is the same as html_title.
#html_short_title = None

# The name of an image file (relative to this directory) to place at the top
# of the sidebar.
#html_logo = None

# The name of an image file (relative to this directory) to use as a favicon of
# the docs.  This file should be a Windows icon file (.ico) being 16x16 or 32x32
# pixels large.
#html_favicon = None

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_sources']

# Add any extra paths that contain custom files (such as robots.txt or
# .htaccess) here, relative to this directory. These files are copied
# directly to the root of the documentation.
#html_extra_path = []

# If not None, a 'Last updated on:' timestamp is inserted at every page
# bottom, using the given strftime format.
# The empty string is equivalent to '%b %d, %Y'.
#html_last_updated_fmt = None

# If true, SmartyPants will be used to convert quotes and dashes to
# typographically correct entities.
#html_use_smartypants = True

# Custom sidebar templates, maps document names to template names.
#html_sidebars = {}

# Additional templates that should be rendered to pages, maps page names to
# template names.
#html_additional_pages = {}

# If false, no module index is generated.
#html_domain_indices = True

# If false, no index is generated.
#html_use_index = True

# If true, the index is split into individual pages for each letter.
#html_split_index = False

# If true, links to the reST sources are added to the pages.
#html_show_sourcelink = True

# If true, "Created using Sphinx" is shown in the HTML footer. Default is True.
#html_show_sphinx = True

# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True.
#html_show_copyright = True

# If true, an OpenSearch description file will be output, and all pages will
# contain a <link> tag referring to it.  The value of this option must be the
# base URL from which the finished HTML is served.
#html_use_opensearch = ''

# This is the file name suffix for HTML files (e.g. ".xhtml").
#html_file_suffix = None

# Language to be used for generating the HTML full-text search index.
# Sphinx supports the following languages:
#   'da', 'de', 'en', 'es', 'fi', 'fr', 'hu', 'it', 'ja'
#   'nl', 'no', 'pt', 'ro', 'ru', 'sv', 'tr', 'zh'
#html_search_language = 'en'

# A dictionary with options for the search language support, empty by default.
# 'ja' uses this config value.
# 'zh' user can custom change `jieba` dictionary path.
#html_search_options = {'type': 'default'}

# The name of a javascript file (relative to the configuration directory) that
# implements a search results scorer. If empty, the default will be used.
#html_search_scorer = 'scorer.js'

# Output file base name for HTML help builder.
htmlhelp_basename = 'datetime_utilsdoc'

# -- Options for LaTeX output ---------------------------------------------

latex_elements = {
# The paper size ('letterpaper' or 'a4paper').
#'papersize': 'letterpaper',

# The font size ('10pt', '11pt' or '12pt').
#'pointsize': '10pt',

# Additional stuff for the LaTeX preamble.
#'preamble': '',

# Latex figure (float) alignment
#'figure_align': 'htbp',
}

# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title,
#  author, documentclass [howto, manual, or own class]).
latex_documents = [
    (master_doc, 'datetime_utils.tex', u'datetime\\_utils Documentation',
     u'Ann Paul', 'manual'),
]

# The name of an image file (relative to this directory) to place at the top of
# the title page.
#latex_logo = None

# For "manual" documents, if this is true, then toplevel headings are parts,
# not chapters.
#latex_use_parts = False

# If true, show page references after internal links.
#latex_show_pagerefs = False

# If true, show URL addresses after external links.
#latex_show_urls = False

# Documents to append as an appendix to all manuals.
#latex_appendices = []

# If false, no module index is generated.
#latex_domain_indices = True

# -- Options for manual page output ---------------------------------------

# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
    (master_doc, 'datetime_utils', u'datetime_utils Documentation',
     [author], 1)
]

# If true, show URL addresses after external links.
#man_show_urls = False

# -- Options for Texinfo output -------------------------------------------

# Grouping the document tree into Texinfo files. List of tuples
# (source start file, target name, title, author,
#  dir menu entry, description, category)
texinfo_documents = [
    (master_doc, 'datetime_utils', u'datetime_utils Documentation',
     author, 'datetime_utils', 'One line description of project.',
     'Miscellaneous'),
]

# Documents to append as an appendix to all manuals.
#texinfo_appendices = []

# If false, no module index is generated.
#texinfo_domain_indices = True

# How to display URL addresses: 'footnote', 'no', or 'inline'.
#texinfo_show_urls = 'footnote'

# If true, do not generate a @detailmenu in the "Top" node's menu.
#texinfo_no_detailmenu = False
<|endoftext|>


<commit_before># fmt: off
__title__ = "spacy-nightly"
__version__ = "3.0.0a34"
__download_url__ = "https://github.com/explosion/spacy-models/releases/download"
__compatibility__ = "https://raw.githubusercontent.com/explosion/spacy-models/master/compatibility.json"
__projects__ = "https://github.com/explosion/projects"
__projects_branch__ = "v3"
<commit_msg>Set version to v3.0.0a35<commit_after># fmt: off
__title__ = "spacy-nightly"
__version__ = "3.0.0a35"
__download_url__ = "https://github.com/explosion/spacy-models/releases/download"
__compatibility__ = "https://raw.githubusercontent.com/explosion/spacy-models/master/compatibility.json"
__projects__ = "https://github.com/explosion/projects"
__projects_branch__ = "v3"
<|endoftext|>


<commit_before>#!/usr/bin/env python
'''
Inspired by the post "Populating a sparse list with random 1's" on
StackOverflow:

    http://stackoverflow.com/q/17522753/78845

A "sparse list" is a list where most (say, more than 95% of) values will be
None (or some other default) and for reasons of memory efficiency you don't
wish to store these. cf. Sparse array:

    http://en.wikipedia.org/wiki/Sparse_array
'''

try:
    xrange
except NameError:
    # On Python 3, range() is equivalent to Python 2's xrange()
    xrange = range

from six import iteritems, itervalues
from six.moves import zip_longest

class SparseList(object):
    '''
    This implementation has a similar interface to Python's built-in list but
    stores the data in a dictionary to conserve memory.
    '''

    def __init__(self, arg, default_value=None):
        self.default = default_value
        self.elements = {}
        if isinstance(arg, int):
            self.size = int(arg)
        elif isinstance(arg, dict):
            self.__initialise_from_dict(arg)
        else:
            self.__initialise_from_iterable(arg)

    def __len__(self):
        return self.size

    def __setitem__(self, index, value):
        try:
            if index.start:
                self.size = max(self.size, index.start + len(value))
            s = slice(index.start, index.stop, index.step).indices(self.size)
            for v, i in enumerate(xrange(*s)):
                self.__setitem__(i, value[v])
        except AttributeError:
            self.elements[index] = value
            self.size = max(index + 1, self.size)

    def __getitem__(self, index):
        try:
            s = slice(index.start, index.stop, index.step).indices(self.size)
            return [self[i] for i in xrange(*s)]
        except AttributeError:
            i = slice(index).indices(self.size)[1]
            return self.elements.get(i, self.default)

    def __setslice__(self, start, stop, vals):
        '''
        __setslice__ is deprecated, but kept here for backwards compatibility
        '''
        return self.__setitem__(slice(start, stop), vals)

    def __delitem__(self, item):
        if isinstance(item, slice):
            deletion_indices = xrange(*item.indices(self.size))
        elif item >= 0:
            deletion_indices = (item, )
        else:
            deletion_indices = (self.size + item,)

        original_indices = sorted(self.elements.keys())
        original_indices_cursor = 0
        deleted_count = 0
        new_elements = {}
        for key_offset, i in enumerate(deletion_indices):
            while (original_indices_cursor < len(original_indices) and
                original_indices[original_indices_cursor] < i):
                index = original_indices[original_indices_cursor]
                new_elements[index - key_offset] = self.elements[index]
                original_indices_cursor += 1
            if (original_indices_cursor < len(original_indices) and
                original_indices[original_indices_cursor] == i):
                original_indices_cursor += 1

        for remaining_index in original_indices[original_indices_cursor:]:
            new_elements[remaining_index - len(deletion_indices)] = self.elements[remaining_index]

        self.elements = new_elements
        self.size = self.size - len(deletion_indices)

    def __delslice__(self, start, stop):
        '''
        __delslice__ is deprecated, but kept here for backwards compatibility
        '''
        return self.__delitem__(slice(start, stop))

    def __iter__(self):
        for index in xrange(self.size):
            yield self[index]

    def __contains__(self, index):
        return index in itervalues(self.elements)

    def __repr__(self):
        return '[{}]'.format(', '.join([str(e) for e in self]))

    def __add__(self, other):
        result = self[:]
        return result.__iadd__(other)

    def __iadd__(self, other):
        for element in other:
            self.append(element)
        return self

    def append(self, element):
        '''
        append element, increasing size by exactly one
        '''
        self.elements[self.size] = element
        self.size += 1

    push = append

    def __initialise_from_dict(self, arg):
        def __convert_and_size(key):
            try:
                key = int(key)
            except ValueError:
                raise ValueError('Invalid key: {}'.format(key))
            self.size = max(key + 1, self.size)
            return key
        self.size = 0
        self.elements = {__convert_and_size(k): v for k, v in iteritems(arg)}

    def __initialise_from_iterable(self, arg):
        self.elements = {k: v for k, v in enumerate(arg)}
        self.size = len(self.elements)

    def __eq__(self, other):
        return all(a == b for a, b in zip_longest(self, other))

    def __ne__(self, other):
        return not self.__eq__(other)

    def __lt__(self, other):
        return any(a < b for a, b in zip_longest(self, other))

    def __ge__(self, other):
        return not self.__lt__(other)

    def __mul__(self, multiplier):
        result = []
        for _ in xrange(multiplier):
            result += self[:]
        return result

    def count(self, value):
        '''
        return number of occurrences of value
        '''
        return sum(v == value for v in itervalues(self.elements)) + (
            self.size - len(self.elements) if value == self.default else 0
        )

    def extend(self, iterable):
        '''
        extend sparse_list by appending elements from the iterable
        '''
        self.__iadd__(iterable)

    def index(self, value):
        '''
        return first index of value.
        Raises ValueError if the value is not present.
        '''

        if value == self.default:
            for k, v in enumerate(self):
                if v == value:
                    return k
            raise ValueError('{} not in SparseList'.format(value))
        for k, v in iteritems(self.elements):
            if v == value:
                return k
        raise ValueError('{} not in SparseList'.format(value))

    def pop(self):
        '''
        remove and return item at end of SparseList
        Raises IndexError if list is empty.
        '''
        if self.size < 1:
            raise IndexError('pop from empty SparseList')
        value = self[-1]
        del self[-1]
        return value

    def remove(self, value):
        '''
        remove first occurrence of value.
        Raises ValueError if the value is not present.
        '''
        if value == self.default:
            return
        for k, v in iteritems(self.elements):
            if v == value:
                del self.elements[k]
                return
        raise ValueError('{} not in SparseList'.format(value))
<commit_msg>Reduce complexity of __delitem__<commit_after>#!/usr/bin/env python
'''
Inspired by the post "Populating a sparse list with random 1's" on
StackOverflow:

    http://stackoverflow.com/q/17522753/78845

A "sparse list" is a list where most (say, more than 95% of) values will be
None (or some other default) and for reasons of memory efficiency you don't
wish to store these. cf. Sparse array:

    http://en.wikipedia.org/wiki/Sparse_array
'''

try:
    xrange
except NameError:
    # On Python 3, range() is equivalent to Python 2's xrange()
    xrange = range

from six import iteritems, itervalues
from six.moves import zip_longest

class SparseList(object):
    '''
    This implementation has a similar interface to Python's built-in list but
    stores the data in a dictionary to conserve memory.
    '''

    def __init__(self, arg, default_value=None):
        self.default = default_value
        self.elements = {}
        if isinstance(arg, int):
            self.size = int(arg)
        elif isinstance(arg, dict):
            self.__initialise_from_dict(arg)
        else:
            self.__initialise_from_iterable(arg)

    def __len__(self):
        return self.size

    def __setitem__(self, index, value):
        try:
            if index.start:
                self.size = max(self.size, index.start + len(value))
            s = slice(index.start, index.stop, index.step).indices(self.size)
            for v, i in enumerate(xrange(*s)):
                self.__setitem__(i, value[v])
        except AttributeError:
            self.elements[index] = value
            self.size = max(index + 1, self.size)

    def __getitem__(self, index):
        try:
            s = slice(index.start, index.stop, index.step).indices(self.size)
            return [self[i] for i in xrange(*s)]
        except AttributeError:
            i = slice(index).indices(self.size)[1]
            return self.elements.get(i, self.default)

    def __setslice__(self, start, stop, vals):
        '''
        __setslice__ is deprecated, but kept here for backwards compatibility
        '''
        return self.__setitem__(slice(start, stop), vals)

    def __delitem__(self, item):
        if isinstance(item, slice):
            indices = reversed(xrange(*item.indices(self.size)))
        elif item < 0:
            indices = ((self.size + item), )
        else:
            indices = (item, )

        for i in indices:
            for k in self.elements.keys():
                if k == i:
                    del self.elements[k]
                elif k > i:
                    self.elements[k - 1] = self.elements[k]
                    del self.elements[k]
            self.size -= 1

    def __delslice__(self, start, stop):
        '''
        __delslice__ is deprecated, but kept here for backwards compatibility
        '''
        return self.__delitem__(slice(start, stop))

    def __iter__(self):
        for index in xrange(self.size):
            yield self[index]

    def __contains__(self, index):
        return index in itervalues(self.elements)

    def __repr__(self):
        return '[{}]'.format(', '.join([str(e) for e in self]))

    def __add__(self, other):
        result = self[:]
        return result.__iadd__(other)

    def __iadd__(self, other):
        for element in other:
            self.append(element)
        return self

    def append(self, element):
        '''
        append element, increasing size by exactly one
        '''
        self.elements[self.size] = element
        self.size += 1

    push = append

    def __initialise_from_dict(self, arg):
        def __convert_and_size(key):
            try:
                key = int(key)
            except ValueError:
                raise ValueError('Invalid key: {}'.format(key))
            self.size = max(key + 1, self.size)
            return key
        self.size = 0
        self.elements = {__convert_and_size(k): v for k, v in iteritems(arg)}

    def __initialise_from_iterable(self, arg):
        self.elements = {k: v for k, v in enumerate(arg)}
        self.size = len(self.elements)

    def __eq__(self, other):
        return all(a == b for a, b in zip_longest(self, other))

    def __ne__(self, other):
        return not self.__eq__(other)

    def __lt__(self, other):
        return any(a < b for a, b in zip_longest(self, other))

    def __ge__(self, other):
        return not self.__lt__(other)

    def __mul__(self, multiplier):
        result = []
        for _ in xrange(multiplier):
            result += self[:]
        return result

    def count(self, value):
        '''
        return number of occurrences of value
        '''
        return sum(v == value for v in itervalues(self.elements)) + (
            self.size - len(self.elements) if value == self.default else 0
        )

    def extend(self, iterable):
        '''
        extend sparse_list by appending elements from the iterable
        '''
        self.__iadd__(iterable)

    def index(self, value):
        '''
        return first index of value.
        Raises ValueError if the value is not present.
        '''

        if value == self.default:
            for k, v in enumerate(self):
                if v == value:
                    return k
            raise ValueError('{} not in SparseList'.format(value))
        for k, v in iteritems(self.elements):
            if v == value:
                return k
        raise ValueError('{} not in SparseList'.format(value))

    def pop(self):
        '''
        remove and return item at end of SparseList
        Raises IndexError if list is empty.
        '''
        if self.size < 1:
            raise IndexError('pop from empty SparseList')
        value = self[-1]
        del self[-1]
        return value

    def remove(self, value):
        '''
        remove first occurrence of value.
        Raises ValueError if the value is not present.
        '''
        if value == self.default:
            return
        for k, v in iteritems(self.elements):
            if v == value:
                del self.elements[k]
                return
        raise ValueError('{} not in SparseList'.format(value))
<|endoftext|>


<commit_before>#!/usr/bin/python
#  Licensed to the Apache Software Foundation (ASF) under one
#  or more contributor license agreements.  See the NOTICE file
#  distributed with this work for additional information
#  regarding copyright ownership.  The ASF licenses this file
#  to you under the Apache License, Version 2.0 (the
#  "License"); you may not use this file except in compliance
#  with the License.  You may obtain a copy of the License at
#
#  http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing,
#  software distributed under the License is distributed on an
#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
#  KIND, either express or implied.  See the License for the
#  specific language governing permissions and limitations
#  under the License.

__author__ = 'Shamal Faily'

import os
import sys
sys.path.insert(0,'/home/nathan/cairis/cairis')
os.environ['CAIRIS_CFG'] = '/home/nathan/cairis.cnf'
from cairis.daemon import create_app
from cairis.daemon.CairisHTTPError import CairisHTTPError

application = create_app()

if __name__ == '__main__':
  try:
    application.run()
  except CairisHTTPError, e:
    print 'Fatal CAIRIS error: ' + str(e)
    sys.exit(-1)
<commit_msg>Update cairis.wsgi<commit_after>#!/usr/bin/python
#  Licensed to the Apache Software Foundation (ASF) under one
#  or more contributor license agreements.  See the NOTICE file
#  distributed with this work for additional information
#  regarding copyright ownership.  The ASF licenses this file
#  to you under the Apache License, Version 2.0 (the
#  "License"); you may not use this file except in compliance
#  with the License.  You may obtain a copy of the License at
#
#  http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing,
#  software distributed under the License is distributed on an
#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
#  KIND, either express or implied.  See the License for the
#  specific language governing permissions and limitations
#  under the License.

__author__ = 'Shamal Faily'

import os
import sys
sys.path.insert(0,'/home/nathan/cairis/')
os.environ['CAIRIS_CFG'] = '/home/nathan/cairis.cnf'
from cairis.daemon import create_app
from cairis.daemon.CairisHTTPError import CairisHTTPError

application = create_app()

if __name__ == '__main__':
  try:
    application.run()
  except CairisHTTPError, e:
    print 'Fatal CAIRIS error: ' + str(e)
    sys.exit(-1)
<|endoftext|>


<commit_before>import matplotlib.pyplot as plt
import matplotlib.ticker
import numpy as np
import wave
import sys

# Attempt to load progressbar2 library. If it is not available, declare
# a stub ProgressBar class that does nothing.
try:
    from progressbar import ProgressBar
except:
    class ProgressBar (object):
        def __init__(*args, **kwargs):
            pass
        def update(self, x):
            pass
        def finish(self):
            pass

def main(input_file=None, window_size="1024"):

    # Process command-line args.
    if input_file is None:
        sys.stderr.write("usage: python %s <input_file.wav> [window_size=1024]\n" % sys.argv[0])
        return 1
    window_size = int(window_size)

    # Open wave file and load frame rate, number of channels, sample width, and number of frames.
    w = wave.open(input_file, 'r')
    sample_freq = w.getframerate()
    chans = w.getnchannels()
    width = w.getsampwidth()
    frame = w.getnframes()

    # Use an 8-bit integer for single-byte samples, 16-bit integer for 2-byte samples.
    dtype = np.int8 if width == 1 else np.int16

    # Hann window function coefficients.
    hann = 0.5 - 0.5 * np.cos(2.0 * np.pi * (np.arange(window_size)) / window_size)

    # Hann window must have 4x overlap for good results.
    overlap = 4

    # Y will hold the DFT of each window. We use acc and bar for displaying progress.
    Y = []
    acc = 0
    bar = ProgressBar(max_value=frame*overlap)

    # X will hold residual window content for overlapping.
    X = []

    # Process each window of audio.
    while True:

        # If this is the first read, load an entire window, otherwise just 1/overlap of a window.
        read_size = window_size if len(X) == 0 else window_size / overlap

        # Load raw audio data into a numpy array.
        x = w.readframes(read_size)
        x = np.fromstring(x, dtype=dtype)

        # If the window read was short, end.
        if len(x) != read_size * chans:
            break

        # Reshape our array into an N*c matrix in order to separate the channels.
        x = np.reshape(x, (read_size, chans))

        # Average the channels if the audio is stereo.
        if chans > 1:
            x = (x[:,0] + x[:,1]) / 2
        else:
            x = x[:,0]

        # Append these frames to the window.
        X.extend(x)

        # Perform the FFT.
        y = np.fft.fft(X * hann)

        # Only data up to window_size/2 is useful; the rest is past the Nyquist cutoff.
        y = y[:window_size/2]

        # Normalize the data by converting to dB.
        y = (np.log10(np.power(np.absolute(y), 2).clip(1)) * 10)

        # Add this DFT frame to the output, update the progress bar, and truncate the window.
        Y.append(y)
        acc += window_size
        bar.update(acc)
        X = X[window_size / overlap:]

    # Inform progress bar that the computation is complete.
    bar.finish()

    # Time domain: We have len(Y) windows, so convert to seconds by multiplying
    # by window size, dividing by sample rate, and dividing by the overlap rate.
    t = np.arange(0, len(Y), dtype=np.float) * window_size / sample_freq / overlap

    # Frequency domain: There are window_size/2 frequencies represented, and we scale
    # by dividing by window size and multiplying by sample frequency.
    f = np.arange(0, window_size / 2, dtype=np.float) * sample_freq / window_size

    # By default, numpy will arrange the matrix by rows, so we need to transpose it.
    Y = np.array(Y).transpose()

    # Scale dB by maximum value.
    m = np.amax(Y)
    Y = Y - m

    # Plot the spectrogram.
    ax = plt.subplot(111)
    plt.pcolormesh(t, f, Y)

    # Use log scale above 100 Hz, linear below.
    plt.yscale('symlog', linthreshy=100, linscaley=0.25)
    ax.yaxis.set_major_formatter(matplotlib.ticker.ScalarFormatter())

    # Set x/y limits by using the maximums from the time/frequency arrays.
    plt.xlim(0, t[-1])
    plt.ylim(0, f[-1])

    # Set axis labels.
    plt.xlabel("Time (s)")
    plt.ylabel("Frequency (Hz)")

    # Show legend and set label.
    cbar = plt.colorbar()
    cbar.set_label("Intensity (dB)")

    # Display spectrogram.
    plt.show()

    return 0

if __name__ == "__main__":
    sys.exit(main(*sys.argv[1:]))
<commit_msg>display error if sample width is not 8 or 16 bit<commit_after>import matplotlib.pyplot as plt
import matplotlib.ticker
import numpy as np
import wave
import sys

# Attempt to load progressbar2 library. If it is not available, declare
# a stub ProgressBar class that does nothing.
try:
    from progressbar import ProgressBar
except:
    class ProgressBar (object):
        def __init__(*args, **kwargs):
            pass
        def update(self, x):
            pass
        def finish(self):
            pass

def main(input_file=None, window_size="1024"):

    # Process command-line args.
    if input_file is None:
        sys.stderr.write("usage: python %s <input_file.wav> [window_size=1024]\n" % sys.argv[0])
        return 1
    window_size = int(window_size)

    # Open wave file and load frame rate, number of channels, sample width, and number of frames.
    w = wave.open(input_file, 'r')
    sample_freq = w.getframerate()
    chans = w.getnchannels()
    width = w.getsampwidth()
    frame = w.getnframes()

    # Use an 8-bit integer for single-byte samples, 16-bit integer for 2-byte samples.
    if width == 1:
        dtype = np.int8
    elif width == 2:
        dtype = np.int16
    else:
        sys.stderr.write("error: only 8-bit and 16-bit signed samples are supported\n")
        return 1

    # Hann window function coefficients.
    hann = 0.5 - 0.5 * np.cos(2.0 * np.pi * (np.arange(window_size)) / window_size)

    # Hann window must have 4x overlap for good results.
    overlap = 4

    # Y will hold the DFT of each window. We use acc and bar for displaying progress.
    Y = []
    acc = 0
    bar = ProgressBar(max_value=frame*overlap)

    # X will hold residual window content for overlapping.
    X = []

    # Process each window of audio.
    while True:

        # If this is the first read, load an entire window, otherwise just 1/overlap of a window.
        read_size = window_size if len(X) == 0 else window_size / overlap

        # Load raw audio data into a numpy array.
        x = w.readframes(read_size)
        x = np.fromstring(x, dtype=dtype)

        # If the window read was short, end.
        if len(x) != read_size * chans:
            break

        # Reshape our array into an N*c matrix in order to separate the channels.
        x = np.reshape(x, (read_size, chans))

        # Average the channels if the audio is stereo.
        if chans > 1:
            x = (x[:,0] + x[:,1]) / 2
        else:
            x = x[:,0]

        # Append these frames to the window.
        X.extend(x)

        # Perform the FFT.
        y = np.fft.fft(X * hann)

        # Only data up to window_size/2 is useful; the rest is past the Nyquist cutoff.
        y = y[:window_size/2]

        # Normalize the data by converting to dB.
        y = (np.log10(np.power(np.absolute(y), 2).clip(1)) * 10)

        # Add this DFT frame to the output, update the progress bar, and truncate the window.
        Y.append(y)
        acc += window_size
        bar.update(acc)
        X = X[window_size / overlap:]

    # Inform progress bar that the computation is complete.
    bar.finish()

    # Time domain: We have len(Y) windows, so convert to seconds by multiplying
    # by window size, dividing by sample rate, and dividing by the overlap rate.
    t = np.arange(0, len(Y), dtype=np.float) * window_size / sample_freq / overlap

    # Frequency domain: There are window_size/2 frequencies represented, and we scale
    # by dividing by window size and multiplying by sample frequency.
    f = np.arange(0, window_size / 2, dtype=np.float) * sample_freq / window_size

    # By default, numpy will arrange the matrix by rows, so we need to transpose it.
    Y = np.array(Y).transpose()

    # Scale dB by maximum value.
    m = np.amax(Y)
    Y = Y - m

    # Plot the spectrogram.
    ax = plt.subplot(111)
    plt.pcolormesh(t, f, Y)

    # Use log scale above 100 Hz, linear below.
    plt.yscale('symlog', linthreshy=100, linscaley=0.25)
    ax.yaxis.set_major_formatter(matplotlib.ticker.ScalarFormatter())

    # Set x/y limits by using the maximums from the time/frequency arrays.
    plt.xlim(0, t[-1])
    plt.ylim(0, f[-1])

    # Set axis labels.
    plt.xlabel("Time (s)")
    plt.ylabel("Frequency (Hz)")

    # Show legend and set label.
    cbar = plt.colorbar()
    cbar.set_label("Intensity (dB)")

    # Display spectrogram.
    plt.show()

    return 0

if __name__ == "__main__":
    sys.exit(main(*sys.argv[1:]))
<|endoftext|>


<commit_before># seleniumbase package
__version__ = "2.4.17"
<commit_msg>Version 2.4.18<commit_after># seleniumbase package
__version__ = "2.4.18"
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
#
# pyroTorrent documentation build configuration file, created by
# sphinx-quickstart on Sun Jan 23 16:32:39 2011.
#
# This file is execfile()d with the current directory set to its containing dir.
#
# Note that not all possible configuration values are present in this
# autogenerated file.
#
# All configuration values have a default; values that are commented out
# serve to show the default.

import sys, os

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#sys.path.insert(0, os.path.abspath('.'))
# XXX:  Change this to model path later.
sys.path.insert(0, os.path.abspath('../model'))
sys.path.insert(0, os.path.abspath('../'))
sys.path.insert(0, os.path.abspath('../lib'))

# -- General configuration -----------------------------------------------------

# If your documentation needs a minimal Sphinx version, state it here.
#needs_sphinx = '1.0'

# Add any Sphinx extension module names here, as strings. They can be extensions
# coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
extensions = ['sphinx.ext.autodoc', 'sphinx.ext.viewcode']

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']

# The suffix of source filenames.
source_suffix = '.rst'

# The encoding of source files.
#source_encoding = 'utf-8-sig'

# The master toctree document.
master_doc = 'index'

# General information about the project.
project = u'pyroTorrent'
copyright = u'2011, Merlijn Wajer, Bas Weelinck'

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
#
# The short X.Y version.
version = '0.01'
# The full version, including alpha/beta/rc tags.
release = '0.01'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#language = None

# There are two options for replacing |today|: either, you set today to some
# non-false value, then it is used:
#today = ''
# Else, today_fmt is used as the format for a strftime call.
#today_fmt = '%B %d, %Y'

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
exclude_patterns = ['_build']

# The reST default role (used for this markup: `text`) to use for all documents.
#default_role = None

# If true, '()' will be appended to :func: etc. cross-reference text.
#add_function_parentheses = True

# If true, the current module name will be prepended to all description
# unit titles (such as .. function::).
#add_module_names = True

# If true, sectionauthor and moduleauthor directives will be shown in the
# output. They are ignored by default.
#show_authors = False

# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx'

# A list of ignored prefixes for module index sorting.
#modindex_common_prefix = []

# -- Options for HTML output ---------------------------------------------------

# The theme to use for HTML and HTML Help pages.  See the documentation for
# a list of builtin themes.
html_theme = 'default'

# Theme options are theme-specific and customize the look and feel of a theme
# further.  For a list of options available for each theme, see the
# documentation.
#html_theme_options = {}

# Add any paths that contain custom themes here, relative to this directory.
#html_theme_path = []

# The name for this set of Sphinx documents.  If None, it defaults to
# "<project> v<release> documentation".
#html_title = None

# A shorter title for the navigation bar.  Default is the same as html_title.
#html_short_title = None

# The name of an image file (relative to this directory) to place at the top
# of the sidebar.
#html_logo = None

# The name of an image file (within the static path) to use as favicon of the
# docs.  This file should be a Windows icon file (.ico) being 16x16 or 32x32
# pixels large.
#html_favicon = None

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']

# If not '', a 'Last updated on:' timestamp is inserted at every page bottom,
# using the given strftime format.
#html_last_updated_fmt = '%b %d, %Y'

# If true, SmartyPants will be used to convert quotes and dashes to
# typographically correct entities.
#html_use_smartypants = True

# Custom sidebar templates, maps document names to template names.
#html_sidebars = {}

# Additional templates that should be rendered to pages, maps page names to
# template names.
#html_additional_pages = {}

# If false, no module index is generated.
#html_domain_indices = True

# If false, no index is generated.
#html_use_index = True

# If true, the index is split into individual pages for each letter.
#html_split_index = False

# If true, links to the reST sources are added to the pages.
#html_show_sourcelink = True

# If true, "Created using Sphinx" is shown in the HTML footer. Default is True.
#html_show_sphinx = True

# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True.
#html_show_copyright = True

# If true, an OpenSearch description file will be output, and all pages will
# contain a <link> tag referring to it.  The value of this option must be the
# base URL from which the finished HTML is served.
#html_use_opensearch = ''

# This is the file name suffix for HTML files (e.g. ".xhtml").
#html_file_suffix = None

# Output file base name for HTML help builder.
htmlhelp_basename = 'pyroTorrentdoc'

# -- Options for LaTeX output --------------------------------------------------

# The paper size ('letter' or 'a4').
#latex_paper_size = 'letter'

# The font size ('10pt', '11pt' or '12pt').
#latex_font_size = '10pt'

# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title, author, documentclass [howto/manual]).
latex_documents = [
  ('index', 'pyroTorrent.tex', u'pyroTorrent Documentation',
   u'Merlijn Wajer, Bas Weelinck', 'manual'),
]

# The name of an image file (relative to this directory) to place at the top of
# the title page.
#latex_logo = None

# For "manual" documents, if this is true, then toplevel headings are parts,
# not chapters.
#latex_use_parts = False

# If true, show page references after internal links.
#latex_show_pagerefs = False

# If true, show URL addresses after external links.
#latex_show_urls = False

# Additional stuff for the LaTeX preamble.
#latex_preamble = ''

# Documents to append as an appendix to all manuals.
#latex_appendices = []

# If false, no module index is generated.
#latex_domain_indices = True

# -- Options for manual page output --------------------------------------------

# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
    ('index', 'pyrotorrent', u'pyroTorrent Documentation',
     [u'Merlijn Wajer, Bas Weelinck'], 1)
]
<commit_msg>Fixed man page file name.<commit_after># -*- coding: utf-8 -*-
#
# pyroTorrent documentation build configuration file, created by
# sphinx-quickstart on Sun Jan 23 16:32:39 2011.
#
# This file is execfile()d with the current directory set to its containing dir.
#
# Note that not all possible configuration values are present in this
# autogenerated file.
#
# All configuration values have a default; values that are commented out
# serve to show the default.

import sys, os

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#sys.path.insert(0, os.path.abspath('.'))
# XXX:  Change this to model path later.
sys.path.insert(0, os.path.abspath('../model'))
sys.path.insert(0, os.path.abspath('../'))
sys.path.insert(0, os.path.abspath('../lib'))

# -- General configuration -----------------------------------------------------

# If your documentation needs a minimal Sphinx version, state it here.
#needs_sphinx = '1.0'

# Add any Sphinx extension module names here, as strings. They can be extensions
# coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
extensions = ['sphinx.ext.autodoc', 'sphinx.ext.viewcode']

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']

# The suffix of source filenames.
source_suffix = '.rst'

# The encoding of source files.
#source_encoding = 'utf-8-sig'

# The master toctree document.
master_doc = 'index'

# General information about the project.
project = u'pyroTorrent'
copyright = u'2011, Merlijn Wajer, Bas Weelinck'

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
#
# The short X.Y version.
version = '0.01'
# The full version, including alpha/beta/rc tags.
release = '0.01'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#language = None

# There are two options for replacing |today|: either, you set today to some
# non-false value, then it is used:
#today = ''
# Else, today_fmt is used as the format for a strftime call.
#today_fmt = '%B %d, %Y'

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
exclude_patterns = ['_build']

# The reST default role (used for this markup: `text`) to use for all documents.
#default_role = None

# If true, '()' will be appended to :func: etc. cross-reference text.
#add_function_parentheses = True

# If true, the current module name will be prepended to all description
# unit titles (such as .. function::).
#add_module_names = True

# If true, sectionauthor and moduleauthor directives will be shown in the
# output. They are ignored by default.
#show_authors = False

# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx'

# A list of ignored prefixes for module index sorting.
#modindex_common_prefix = []

# -- Options for HTML output ---------------------------------------------------

# The theme to use for HTML and HTML Help pages.  See the documentation for
# a list of builtin themes.
html_theme = 'default'

# Theme options are theme-specific and customize the look and feel of a theme
# further.  For a list of options available for each theme, see the
# documentation.
#html_theme_options = {}

# Add any paths that contain custom themes here, relative to this directory.
#html_theme_path = []

# The name for this set of Sphinx documents.  If None, it defaults to
# "<project> v<release> documentation".
#html_title = None

# A shorter title for the navigation bar.  Default is the same as html_title.
#html_short_title = None

# The name of an image file (relative to this directory) to place at the top
# of the sidebar.
#html_logo = None

# The name of an image file (within the static path) to use as favicon of the
# docs.  This file should be a Windows icon file (.ico) being 16x16 or 32x32
# pixels large.
#html_favicon = None

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']

# If not '', a 'Last updated on:' timestamp is inserted at every page bottom,
# using the given strftime format.
#html_last_updated_fmt = '%b %d, %Y'

# If true, SmartyPants will be used to convert quotes and dashes to
# typographically correct entities.
#html_use_smartypants = True

# Custom sidebar templates, maps document names to template names.
#html_sidebars = {}

# Additional templates that should be rendered to pages, maps page names to
# template names.
#html_additional_pages = {}

# If false, no module index is generated.
#html_domain_indices = True

# If false, no index is generated.
#html_use_index = True

# If true, the index is split into individual pages for each letter.
#html_split_index = False

# If true, links to the reST sources are added to the pages.
#html_show_sourcelink = True

# If true, "Created using Sphinx" is shown in the HTML footer. Default is True.
#html_show_sphinx = True

# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True.
#html_show_copyright = True

# If true, an OpenSearch description file will be output, and all pages will
# contain a <link> tag referring to it.  The value of this option must be the
# base URL from which the finished HTML is served.
#html_use_opensearch = ''

# This is the file name suffix for HTML files (e.g. ".xhtml").
#html_file_suffix = None

# Output file base name for HTML help builder.
htmlhelp_basename = 'pyroTorrentdoc'

# -- Options for LaTeX output --------------------------------------------------

# The paper size ('letter' or 'a4').
#latex_paper_size = 'letter'

# The font size ('10pt', '11pt' or '12pt').
#latex_font_size = '10pt'

# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title, author, documentclass [howto/manual]).
latex_documents = [
  ('index', 'pyroTorrent.tex', u'pyroTorrent Documentation',
   u'Merlijn Wajer, Bas Weelinck', 'manual'),
]

# The name of an image file (relative to this directory) to place at the top of
# the title page.
#latex_logo = None

# For "manual" documents, if this is true, then toplevel headings are parts,
# not chapters.
#latex_use_parts = False

# If true, show page references after internal links.
#latex_show_pagerefs = False

# If true, show URL addresses after external links.
#latex_show_urls = False

# Additional stuff for the LaTeX preamble.
#latex_preamble = ''

# Documents to append as an appendix to all manuals.
#latex_appendices = []

# If false, no module index is generated.
#latex_domain_indices = True

# -- Options for manual page output --------------------------------------------

# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
    ('index', 'pyroTorrent', u'pyroTorrent Documentation',
     [u'Merlijn Wajer, Bas Weelinck'], 1)
]
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
#
#  This file is part of Sequana software
#
#  Copyright (c) 2016 - Sequana Development Team
#
#  File author(s):
#      Thomas Cokelaer <thomas.cokelaer@pasteur.fr>
#      Dimitri Desvillechabrol <dimitri.desvillechabrol@pasteur.fr>,
#          <d.desvillechabrol@gmail.com>
#
#  Distributed under the terms of the 3-clause BSD license.
#  The full license is in the LICENSE file, distributed with this software.
#
#  website: https://github.com/sequana/sequana
#  documentation: http://sequana.readthedocs.io
#
##############################################################################
"""Standalone dedicated to taxonomic content (kraken)"""
import os
import shutil
import glob
import sys
import argparse

from easydev import DevTools
from sequana import logger
from sequana.modules_report.kraken import KrakenModule

logger.name = __name__

class Options(argparse.ArgumentParser):
    def  __init__(self, prog="sequana_taxonomy"):
        usage = """Welcome to SEQUANA - Taxonomy standalone

        This standalone takes as input one or two (paired end) FastQ files.
        Using Kraken, Krona and some codecs from Sequana, it tries to identify
        the taxon/organism that match each reads found in the input files.

        Kraken requires an input database. We provide 3 databases but you can
        also use Sequana to help you building a customised one.

        We provide a DB called toydb. It contains only a few
        measles viruses. Its size is only 32Mb and should be used for testing
        and examples only.

        Another database is the so-called minikraken provided by Kraken's
        authors. It is about 4Gb and contains viruses and bacteria only.

        A third database is built with Sequana and is about 8Gb. It is
        stored on Synapse website and you will need an account (synapse.org).
        It contains about 22,000 species: viruses, bacteria, homo sapiens, fungi,

        Each DB can be downloaded using:

            sequana_taxonomy --download toydb

        Then, you need to use this kind of command:

            sequana_taxonomy --file1 R1.fastq --file2 R2.fastq
                --databases /home/user/.config/sequana/kraken_toydb
                --show-html --thread 4

AUTHORS: Thomas Cokelaer
Documentation: http://sequana.readthedocs.io
Issues: http://github.com/sequana/sequana
        """
        description = """DESCRIPTION:
        """
        super(Options, self).__init__(usage=usage, prog=prog,
                description=description)

        # options to fill the config file
        self.add_argument("--file1", dest="file1", type=str,
            help="""R1 fastq file (zipped) """)
        self.add_argument("--file2", dest="file2", type=str,
            help="""R2 fastq file (zipped) """)
        self.add_argument("--databases", dest="databases", type=str,
            nargs="+",
            help="""Path to a valid Kraken database(s). If you do not have any, use
                --download option. You may use several, in which case, an
                iterative taxonomy is performed as explained in online sequana
                documentation (see HierarchicalKRaken""")
        self.add_argument("--output-directory", dest="directory", type=str,
            help="""name of the output directory""", default="taxonomy")
        self.add_argument("--keep-temp-files", default=False,
            action="store_true", dest="keep_temp_files",
            help="keep temporary files (hierarchical case with several databases")
        self.add_argument("--thread", dest="thread", type=int,
            help="""number of threads to use (default 4)""", default=4)
        self.add_argument("--show-html", dest="html",
            action="store_true",
            help="""Results are stored in report/ directory and results are
                not shown by default""")
        self.add_argument("--download", dest="download", type=str,
            default=None, choices=["sequana_db1", "toydb", "minikraken"],
            help="""download an official sequana DB. The sequana_db1 is stored
                in a dedicated Synapse page (www.synapse.org). minikraken
                is downloaded from the kraken's author page, and toydb from
                sequana github.""")
        self.add_argument("--unclassified-out",
            help="save unclassified sequences to filename")
        self.add_argument("--classified-out",
            help="save unclassified sequences to filename")
        self.add_argument('--level', dest="level",
            default="INFO", choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"])

def main(args=None):

    if args is None:
        args = sys.argv[:]

    user_options = Options(prog="sequana")

    # If --help or no options provided, show the help
    if len(args) == 1:
        user_options.parse_args(["prog", "--help"])
    else:
       options = user_options.parse_args(args[1:])

    logger.level = options.level

    # We put the import here to make the --help faster
    from sequana import KrakenPipeline
    from sequana.kraken import KrakenHierarchical
    devtools = DevTools()

    if options.download:
        from sequana import KrakenDownload
        kd = KrakenDownload()
        kd.download(options.download)
        sys.exit()

    fastq = []
    if options.file1:
        devtools.check_exists(options.file1)
        fastq.append(options.file1)
    if options.file2:
        devtools.check_exists(options.file2)
        fastq.append(options.file2)

    from sequana import sequana_config_path as scfg
    if options.databases is None:
        logger.critical("You must provide a database")
        sys.exit(1)

    databases = []
    for database in options.databases:
        if database == "toydb":
            database = "kraken_toydb"
        elif database == "minikraken":
            database = "minikraken_20141208"

        if os.path.exists(scfg + os.sep + database): # in Sequana path
            databases.append(scfg + os.sep + database)
        elif os.path.exists(database): # local database
            databases.append(database)
        else:
            msg = "Invalid database name (%s). Neither found locally "
            msg += "or in the sequana path %s; Use the --download option"
            raise ValueError(msg % (database, scfg))

    output_directory = options.directory + os.sep + "kraken"
    devtools.mkdirs(output_directory)

    # if there is only one database, use the pipeline else KrakenHierarchical
    _pathto = lambda x: "{}/kraken/{}".format(options.directory, x) if x else x
    if len(databases) == 1:
        logger.info("Using 1 database")
        k = KrakenPipeline(fastq, databases[0], threads=options.thread,
            output_directory=output_directory)

        k.run(output_filename_classified=_pathto(options.classified_out),
              output_filename_unclassified=_pathto(options.unclassified_out))
    else:
        logger.info("Using %s databases" % len(databases))
        k = KrakenHierarchical(fastq, databases,
            threads=options.thread,
            output_directory=output_directory + os.sep,
            force=True,
            keep_temp_files=options.keep_temp_files,
            output_filename_unclassified=_pathto(options.unclassified_out))
        k.run(output_prefix="kraken")

    # This statements sets the directory where HTML will be saved
    from sequana.utils import config
    config.output_dir = options.directory

    # output_directory first argument: the directory where to find the data
    # output_filename is relative to the config.output_dir defined above
    kk = KrakenModule(output_directory, output_filename="summary.html")

    logger.info("Open ./%s/summary.html" % options.directory)
    logger.info("or ./%s/kraken/kraken.html" % options.directory)

    if options.html is True:
        ss.onweb()

if __name__ == "__main__":
   import sys
   main(sys.argv)

<commit_msg>add --update-taxonomy option<commit_after># -*- coding: utf-8 -*-
#
#  This file is part of Sequana software
#
#  Copyright (c) 2016 - Sequana Development Team
#
#  File author(s):
#      Thomas Cokelaer <thomas.cokelaer@pasteur.fr>
#      Dimitri Desvillechabrol <dimitri.desvillechabrol@pasteur.fr>,
#          <d.desvillechabrol@gmail.com>
#
#  Distributed under the terms of the 3-clause BSD license.
#  The full license is in the LICENSE file, distributed with this software.
#
#  website: https://github.com/sequana/sequana
#  documentation: http://sequana.readthedocs.io
#
##############################################################################
"""Standalone dedicated to taxonomic content (kraken)"""
import os
import shutil
import glob
import sys
import argparse

from easydev import DevTools
from sequana import logger
from sequana.modules_report.kraken import KrakenModule

logger.name = __name__

class Options(argparse.ArgumentParser):
    def  __init__(self, prog="sequana_taxonomy"):
        usage = """Welcome to SEQUANA - Taxonomy standalone

        This standalone takes as input one or two (paired end) FastQ files.
        Using Kraken, Krona and some codecs from Sequana, it tries to identify
        the taxon/organism that match each reads found in the input files.

        Kraken requires an input database. We provide 3 databases but you can
        also use Sequana to help you building a customised one.

        We provide a DB called toydb. It contains only a few
        measles viruses. Its size is only 32Mb and should be used for testing
        and examples only.

        Another database is the so-called minikraken provided by Kraken's
        authors. It is about 4Gb and contains viruses and bacteria only.

        A third database is built with Sequana and is about 8Gb. It is
        stored on Synapse website and you will need an account (synapse.org).
        It contains about 22,000 species: viruses, bacteria, homo sapiens, fungi,

        Each DB can be downloaded using:

            sequana_taxonomy --download toydb

        Then, you need to use this kind of command:

            sequana_taxonomy --file1 R1.fastq --file2 R2.fastq
                --databases /home/user/.config/sequana/kraken_toydb
                --show-html --thread 4

AUTHORS: Thomas Cokelaer
Documentation: http://sequana.readthedocs.io
Issues: http://github.com/sequana/sequana
        """
        description = """DESCRIPTION:
        """
        super(Options, self).__init__(usage=usage, prog=prog,
                description=description)

        # options to fill the config file
        self.add_argument("--file1", dest="file1", type=str,
            help="""R1 fastq file (zipped) """)
        self.add_argument("--file2", dest="file2", type=str,
            help="""R2 fastq file (zipped) """)
        self.add_argument("--databases", dest="databases", type=str,
            nargs="+",
            help="""Path to a valid Kraken database(s). If you do not have any, use
                --download option. You may use several, in which case, an
                iterative taxonomy is performed as explained in online sequana
                documentation (see HierarchicalKRaken""")
        self.add_argument("--output-directory", dest="directory", type=str,
            help="""name of the output directory""", default="taxonomy")
        self.add_argument("--keep-temp-files", default=False,
            action="store_true", dest="keep_temp_files",
            help="keep temporary files (hierarchical case with several databases")
        self.add_argument("--thread", dest="thread", type=int,
            help="""number of threads to use (default 4)""", default=4)
        self.add_argument("--show-html", dest="html",
            action="store_true",
            help="""Results are stored in report/ directory and results are
                not shown by default""")
        self.add_argument("--download", dest="download", type=str,
            default=None, choices=["sequana_db1", "toydb", "minikraken"],
            help="""download an official sequana DB. The sequana_db1 is stored
                in a dedicated Synapse page (www.synapse.org). minikraken
                is downloaded from the kraken's author page, and toydb from
                sequana github.""")
        self.add_argument("--unclassified-out",
            help="save unclassified sequences to filename")
        self.add_argument("--classified-out",
            help="save unclassified sequences to filename")
        self.add_argument("--confidence", type=float, default=0,
            help="confidence (kraken2 DB only)")
        self.add_argument("--update-taxonomy", action="store_true",
            default="False",
            help="Update the local NCBI taxonomy database to the last version")
        self.add_argument('--level', dest="level",
            default="INFO", choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"])

def main(args=None):

    if args is None:
        args = sys.argv[:]

    user_options = Options(prog="sequana")

    # If --help or no options provided, show the help
    if len(args) == 1:
        user_options.parse_args(["prog", "--help"])
    else:
       options = user_options.parse_args(args[1:])

    logger.level = options.level

    if options.update_taxonomy is True:
        from sequana.taxonomy import Taxonomy
        tax = Taxonomy()
        from sequana import sequana_config_path as cfg
        logger.info("Will overwrite the local database taxonomy.dat in {}".format(cfg))
        tax.download_taxonomic_file(overwrite=True)
        sys.exit(0)

    # We put the import here to make the --help faster
    from sequana import KrakenPipeline
    from sequana.kraken import KrakenSequential
    devtools = DevTools()

    if options.download:
        from sequana import KrakenDownload
        kd = KrakenDownload()
        kd.download(options.download)
        sys.exit()

    fastq = []
    if options.file1:
        devtools.check_exists(options.file1)
        fastq.append(options.file1)
    if options.file2:
        devtools.check_exists(options.file2)
        fastq.append(options.file2)

    from sequana import sequana_config_path as scfg
    if options.databases is None:
        logger.critical("You must provide a database")
        sys.exit(1)

    databases = []
    for database in options.databases:
        if database == "toydb":
            database = "kraken_toydb"
        elif database == "minikraken":
            database = "minikraken_20141208"

        if os.path.exists(scfg + os.sep + database): # in Sequana path
            databases.append(scfg + os.sep + database)
        elif os.path.exists(database): # local database
            databases.append(database)
        else:
            msg = "Invalid database name (%s). Neither found locally "
            msg += "or in the sequana path %s; Use the --download option"
            raise ValueError(msg % (database, scfg))

    output_directory = options.directory + os.sep + "kraken"
    devtools.mkdirs(output_directory)

    # if there is only one database, use the pipeline else KrakenHierarchical
    _pathto = lambda x: "{}/kraken/{}".format(options.directory, x) if x else x
    if len(databases) == 1:
        logger.info("Using 1 database")
        k = KrakenPipeline(fastq, databases[0], threads=options.thread,
            output_directory=output_directory, confidence=options.confidence)

        k.run(output_filename_classified=_pathto(options.classified_out),
              output_filename_unclassified=_pathto(options.unclassified_out))
    else:
        logger.info("Using %s databases" % len(databases))
        k = KrakenSequential(fastq, databases,
            threads=options.thread,
            output_directory=output_directory + os.sep,
            force=True,
            keep_temp_files=options.keep_temp_files,
            output_filename_unclassified=_pathto(options.unclassified_out),
            confidence=options.confidence)
        k.run(output_prefix="kraken")

    # This statements sets the directory where HTML will be saved
    from sequana.utils import config
    config.output_dir = options.directory

    # output_directory first argument: the directory where to find the data
    # output_filename is relative to the config.output_dir defined above
    kk = KrakenModule(output_directory, output_filename="summary.html")

    logger.info("Open ./%s/summary.html" % options.directory)
    logger.info("or ./%s/kraken/kraken.html" % options.directory)

    if options.html is True:
        ss.onweb()

if __name__ == "__main__":
   import sys
   main(sys.argv)

<|endoftext|>


<commit_before>import sqlite3, time, random

conn = sqlite3.connect("/Users/jameseggers/Library/Application Support/iPhone Simulator/7.0.3-64/Applications/882B36EA-19F0-4056-97D1-8EFDB42CC59E/Documents/miyo.db")

c = conn.cursor()

current_timestamp = int(round(time.time() * 1000))

weeks_of_data = 4
counter = 0
lifetime_points = 10

while counter < weeks_of_data:

	current_timestamp -= counter * 604800000

	num_inserts = random.randint(2,20)
	inserts_counter = 0

	while inserts_counter < num_inserts:
		lifetime_points += 50
		c.execute("INSERT into data VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", (50, 1, random.getrandbits(1), random.getrandbits(1), random.getrandbits(1), random.getrandbits(1), random.getrandbits(1), random.getrandbits(1), random.getrandbits(1), current_timestamp+random.randint(2,5000), lifetime_points))

		inserts_counter += 1

	conn.commit()

	counter += 1
<commit_msg>fixed merge conflict<commit_after>import sqlite3, time, random

class sampleDataMaker:

	def __init__(self):

		if len(sys.argv) != 2:
   			exit()

		print "Opening Database at path: " + sys.argv[1]
		self.conn = sqlite3.connect(sys.argv[1])

		self.c = self.conn.cursor()

		self.current_timestamp = int(round(time.time() * 1000))

	def giveBronzeAchievementsToAll(self):

		counter = 0
		lifetime_points = 10

		while counter < 7:

			lifetime_points += 10
			self.c.execute("INSERT INTO data VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", (50, 1, 1, 1, 1, 1, 1, 1, 1, self.current_timestamp+random.randint(2,5000), lifetime_points) )
			counter += 1
		self.conn.commit()

	def addRandomData(self):
		self.weeks_of_data = 4
		counter = 0
		lifetime_points = 10

		while counter < weeks_of_data:

			current_timestamp -= counter * 604800000

			num_inserts = random.randint(2,20)
			inserts_counter = 0

			if counter == 1:
				eat = 1

			elif counter == 2:
				eat = 0
			elif counter == 3:
				eat = 1
			else:
				eat = 0

			while inserts_counter < 7:
				lifetime_points += 50
				self.c.execute("INSERT INTO data VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", (50, eat, eat, random.getrandbits(1), random.getrandbits(1), random.getrandbits(1), random.getrandbits(1), random.getrandbits(1), random.getrandbits(1), self.current_timestamp+random.randint(2,5000), lifetime_points) )

				inserts_counter += 1

			self.conn.commit()

			counter += 1

o = sampleDataMaker()
o.giveBronzeAchievementsToAll()

<|endoftext|>


<commit_before>import inspect
import unittest

from cinspect import getsource

# Imports for testing
import gc
import audioop

class TestGetSource(unittest.TestCase):

    def test_should_get_source_for_builtin_functions(self):
        # Given
        for function in self._get_builtin_functions():
            # When/Then
            self.assertPyMethodDef(getsource(function))

    def test_should_get_source_for_builtin_methods(self):
        # Given
        for function in self._get_builtin_methods():
            # When/Then
            self.assertPyMethodDef(getsource(function))

    def test_should_get_source_for_method_descriptors(self):
        # Given
        for function in self._get_method_descriptors():
            # When/Then
            self.assertPyMethodDef(getsource(function))

    def test_should_get_source_for_compiled_method(self):
        # Given
        function = gc.collect

        # When/Then
        self.assertPyMethodDef(getsource(function))

    def test_should_get_source_for_module(self):
        # Given
        module = gc

        # When
        source = getsource(module)

        # Then
        self.assertGreaterEqual(len(source.splitlines()), 1)
        self.assertIn('Reference Cycle Garbage Collection', source)

    def test_should_get_source_for_module_with_longname(self):
        # Given
        module = audioop

        # When
        source = getsource(module)

        # Then
        self.assertGreaterEqual(len(source.splitlines()), 1)
        self.assertIn('peak values', source)

    #### Assertions ###########################################################

    def assertPyMethodDef(self, source):
        source_lines = source.splitlines()
        self.assertGreaterEqual(source_lines, 1)
        self.assertNotIn('\nstatic PyObject', '\n'.join(source_lines[2:]))

    #### Private protocol #####################################################

    def _get_builtin_functions(self):
        builtin_functions = []
        ns = {}
        exec 'from __builtin__ import *' in ns

        return [obj for (name, obj) in ns.iteritems() if inspect.isbuiltin(obj)]

    def _get_builtin_methods(self):
        return [

            getattr(kls(), method_name)

            for kls in (list, set, dict)
            for method_name in dir(kls())

            # fixme: fix code to work for [].__add__
            if not method_name.startswith('_')
        ]

    def _get_method_descriptors(self):
        return [

            getattr(kls, method_name)

            for kls in (list, set, dict)
            for method_name in dir(kls())

            # fixme: fix code to work for dict.view*
            if not method_name.startswith(('_', 'view'))
        ]

if __name__ == '__main__':
    unittest.main()
<commit_msg>Run test for view* named method descriptors also.<commit_after>import inspect
import unittest

from cinspect import getsource

# Imports for testing
import gc
import audioop

class TestGetSource(unittest.TestCase):

    def test_should_get_source_for_builtin_functions(self):
        # Given
        for function in self._get_builtin_functions():
            # When/Then
            self.assertPyMethodDef(getsource(function))

    def test_should_get_source_for_builtin_methods(self):
        # Given
        for function in self._get_builtin_methods():
            # When/Then
            self.assertPyMethodDef(getsource(function))

    def test_should_get_source_for_method_descriptors(self):
        # Given
        for function in self._get_method_descriptors():
            # When/Then
            self.assertPyMethodDef(getsource(function))

    def test_should_get_source_for_compiled_method(self):
        # Given
        function = gc.collect

        # When/Then
        self.assertPyMethodDef(getsource(function))

    def test_should_get_source_for_module(self):
        # Given
        module = gc

        # When
        source = getsource(module)

        # Then
        self.assertGreaterEqual(len(source.splitlines()), 1)
        self.assertIn('Reference Cycle Garbage Collection', source)

    def test_should_get_source_for_module_with_longname(self):
        # Given
        module = audioop

        # When
        source = getsource(module)

        # Then
        self.assertGreaterEqual(len(source.splitlines()), 1)
        self.assertIn('peak values', source)

    #### Assertions ###########################################################

    def assertPyMethodDef(self, source):
        source_lines = source.splitlines()
        self.assertGreaterEqual(source_lines, 1)
        self.assertNotIn('\nstatic PyObject', '\n'.join(source_lines[2:]))

    #### Private protocol #####################################################

    def _get_builtin_functions(self):
        builtin_functions = []
        ns = {}
        exec 'from __builtin__ import *' in ns

        return [obj for (name, obj) in ns.iteritems() if inspect.isbuiltin(obj)]

    def _get_builtin_methods(self):
        return [

            getattr(kls(), method_name)

            for kls in (list, set, dict)
            for method_name in dir(kls())

            # fixme: fix code to work for [].__add__
            if not method_name.startswith('_')
        ]

    def _get_method_descriptors(self):
        return [

            getattr(kls, method_name)

            for kls in (list, set, dict)
            for method_name in dir(kls())

            # fixme: fix code to work for [].__add__
            if not method_name.startswith('_')
        ]

if __name__ == '__main__':
    unittest.main()
<|endoftext|>


<commit_before>"""
Tests for the unmatcher module.
"""
import re

import unmatcher

import pytest

DEFAULT_TESTS_COUNT = 32
DEFAULT_CHARACTER_SETS = ('ascii_letters', 'digits')

str_arg = lambda name: {name: str, 'str_attrs': DEFAULT_CHARACTER_SETS}
str_args = lambda *names: dict([(name, str) for name in names] +
                               [('str_attrs', DEFAULT_CHARACTER_SETS)])

@pytest.mark.randomize(ncalls=DEFAULT_TESTS_COUNT, **str_arg('expr'))
def test_literal(expr):
    assert expr == unmatcher.reverse(expr)

@pytest.mark.randomize(_=int, ncalls=DEFAULT_TESTS_COUNT)
@pytest.mark.parametrize('flags', (0, re.DOTALL))
def test_any(_, flags):
    dot_re = re.compile('.', flags)
    reversed_dot = unmatcher.reverse(dot_re)
    assert bool(dot_re.match(reversed_dot))

@pytest.mark.randomize(ncalls=DEFAULT_TESTS_COUNT, **str_args('left', 'right'))
def test_branch_double(left, right):
    branch_re = re.compile('|'.join(map(re.escape, (left, right))))
    reversed_branch = unmatcher.reverse(branch_re)
    assert reversed_branch in (left, right)

@pytest.mark.randomize(ncalls=DEFAULT_TESTS_COUNT,
                       **str_args('first', 'second', 'third'))
def test_branch_triple(first, second, third):
    branch_re = re.compile('|'.join(map(re.escape, (first, second, third))))
    reversed_branch = unmatcher.reverse(branch_re)
    assert reversed_branch in (first, second, third)

# TODO: eliminate repetition in above tests by devising a more general variant
#       that would also verify a more diverse set of test cases

@pytest.mark.randomize(ncalls=DEFAULT_TESTS_COUNT,
                       chars=str, str_attrs=('ascii_letters',))
def test_charset_specified(chars):
    if not chars:
        return
    charset_re = re.compile('[%s]' % chars)
    reversed_charset = unmatcher.reverse(charset_re)
    assert reversed_charset in chars

@pytest.mark.randomize(ncalls=DEFAULT_TESTS_COUNT,
                       minchar=str, maxchar=str,
                       fixed_length=1, str_attrs=('ascii_letters',))
def test_charset_range(minchar, maxchar):
    minchar, maxchar = min(minchar, maxchar), max(minchar, maxchar)
    charset_re = re.compile('[%s-%s]' % (minchar, maxchar))
    reversed_charset = unmatcher.reverse(charset_re)
    assert minchar <= reversed_charset <= maxchar

@pytest.mark.randomize(ncalls=DEFAULT_TESTS_COUNT,
                       class_=str, choices='wds')
def test_charset_class(class_):
    charset_re = re.compile(r'\%s' % class_)
    reversed_charset = unmatcher.reverse(charset_re)
    assert bool(charset_re.match(reversed_charset))

@pytest.mark.parametrize('symbol', ('+', '*'))
@pytest.mark.randomize(ncalls=DEFAULT_TESTS_COUNT,
                       fixed_length=1, **str_arg('char'))
def test_repeat_symbols(symbol, char):
    repeat_re = re.compile('%s%s' % (re.escape(char), symbol))
    reversed_repeat = unmatcher.reverse(repeat_re)
    assert bool(repeat_re.match(reversed_repeat))
    if len(reversed_repeat) == 0:
        assert symbol == '*', "empty string for repeater other than `*'"
    else:
        for chunk in chunks(reversed_repeat, len(char)):
            assert char == chunk

# NOTE: total number of test cases here is ncalls * (max_num - min_num) ** 2
#       so be careful if not setting the parameters too high
@pytest.mark.randomize(ncalls=4,
                       lower_bound=int, upper_bound=int, min_num=0, max_num=4,
                       fixed_length=1, **str_arg('char'))
def test_repeat_range(lower_bound, upper_bound, char):
    if lower_bound == upper_bound:
        # test exact "range": foo{3}
        repeat_re = re.compile('%s{%d}' % (re.escape(char), lower_bound))
    elif lower_bound < upper_bound:
        # test closed range: foo{2,4}
        repeat_re = re.compile('%s{%d,%d}' % (re.escape(char),
                                              lower_bound, upper_bound))
    elif lower_bound > upper_bound:
        # test "infinite" range: foo{4,}
        lower_bound = min((lower_bound, upper_bound))
        repeat_re = re.compile('%s{%d,}' % (re.escape(char), lower_bound))
    else:
        pytest.fail()  # FSM help you if that actually happens

    reversed_repeat = unmatcher.reverse(repeat_re)
    assert bool(repeat_re.match(reversed_repeat))
    for chunk in chunks(reversed_repeat, len(char)):
        assert char == chunk

# TODO: test non-capture groups: (?:foo)bar
# TODO: test capture groups without backreferences: (foo)bar, (?P<huh>foo)bar
# TODO: test capture groups with backreferences: (foo)bar\1, (?P<huh>foo)bar(?P=huh)

# Utility functions

def chunks(seq, n):
    """Split a ``seq``\ uence into chunks of length ``n``."""
    return (seq[i:i+n] for i in xrange(0, len(seq), n)) if len(seq) > 0 else ()
<commit_msg>Test for capture groups w/o backreferences<commit_after>"""
Tests for the unmatcher module.
"""
import re

import unmatcher

import pytest

DEFAULT_TESTS_COUNT = 32
DEFAULT_CHARACTER_SETS = ('ascii_letters', 'digits')

str_arg = lambda name: {name: str, 'str_attrs': DEFAULT_CHARACTER_SETS}
str_args = lambda *names: dict([(name, str) for name in names] +
                               [('str_attrs', DEFAULT_CHARACTER_SETS)])

@pytest.mark.randomize(ncalls=DEFAULT_TESTS_COUNT, **str_arg('expr'))
def test_literal(expr):
    assert expr == unmatcher.reverse(expr)

@pytest.mark.randomize(_=int, ncalls=DEFAULT_TESTS_COUNT)
@pytest.mark.parametrize('flags', (0, re.DOTALL))
def test_any(_, flags):
    dot_re = re.compile('.', flags)
    reversed_dot = unmatcher.reverse(dot_re)
    assert bool(dot_re.match(reversed_dot))

@pytest.mark.randomize(ncalls=DEFAULT_TESTS_COUNT, **str_args('left', 'right'))
def test_branch(left, right):
    branch_re = re.compile('|'.join(map(re.escape, (left, right))))
    reversed_branch = unmatcher.reverse(branch_re)
    assert reversed_branch in (left, right)

@pytest.mark.randomize(ncalls=DEFAULT_TESTS_COUNT,
                       chars=str, str_attrs=('ascii_letters',))
def test_charset_specified(chars):
    if not chars:
        return
    charset_re = re.compile('[%s]' % chars)
    reversed_charset = unmatcher.reverse(charset_re)
    assert reversed_charset in chars

@pytest.mark.randomize(ncalls=DEFAULT_TESTS_COUNT,
                       minchar=str, maxchar=str,
                       fixed_length=1, str_attrs=('ascii_letters',))
def test_charset_range(minchar, maxchar):
    minchar, maxchar = min(minchar, maxchar), max(minchar, maxchar)
    charset_re = re.compile('[%s-%s]' % (minchar, maxchar))
    reversed_charset = unmatcher.reverse(charset_re)
    assert minchar <= reversed_charset <= maxchar

@pytest.mark.randomize(ncalls=DEFAULT_TESTS_COUNT,
                       class_=str, choices='wds')
def test_charset_class(class_):
    charset_re = re.compile(r'\%s' % class_)
    reversed_charset = unmatcher.reverse(charset_re)
    assert bool(charset_re.match(reversed_charset))

@pytest.mark.parametrize('symbol', ('+', '*'))
@pytest.mark.randomize(ncalls=DEFAULT_TESTS_COUNT,
                       fixed_length=1, **str_arg('char'))
def test_repeat_symbols(symbol, char):
    repeat_re = re.compile('%s%s' % (re.escape(char), symbol))
    reversed_repeat = unmatcher.reverse(repeat_re)
    assert bool(repeat_re.match(reversed_repeat))
    if len(reversed_repeat) == 0:
        assert symbol == '*', "empty string for repeater other than `*'"
    else:
        for chunk in chunks(reversed_repeat, len(char)):
            assert char == chunk

# NOTE: total number of test cases here is ncalls * (max_num - min_num) ** 2
#       so be careful if not setting the parameters too high
@pytest.mark.randomize(ncalls=4,
                       lower_bound=int, upper_bound=int, min_num=0, max_num=4,
                       fixed_length=1, **str_arg('char'))
def test_repeat_range(lower_bound, upper_bound, char):
    if lower_bound == upper_bound:
        # test exact "range": foo{3}
        repeat_re = re.compile('%s{%d}' % (re.escape(char), lower_bound))
    elif lower_bound < upper_bound:
        # test closed range: foo{2,4}
        repeat_re = re.compile('%s{%d,%d}' % (re.escape(char),
                                              lower_bound, upper_bound))
    elif lower_bound > upper_bound:
        # test "infinite" range: foo{4,}
        lower_bound = min((lower_bound, upper_bound))
        repeat_re = re.compile('%s{%d,}' % (re.escape(char), lower_bound))
    else:
        pytest.fail()  # FSM help you if that actually happens

    reversed_repeat = unmatcher.reverse(repeat_re)
    assert bool(repeat_re.match(reversed_repeat))
    for chunk in chunks(reversed_repeat, len(char)):
        assert char == chunk

@pytest.mark.randomize(ncalls=DEFAULT_TESTS_COUNT,
                       **str_args('ingroup', 'outgroup'))
def test_noncapture_group(ingroup, outgroup):
    the_re = re.compile('(?:%s)%s' % (ingroup, outgroup))
    reversed_re = unmatcher.reverse(the_re)
    match = the_re.match(reversed_re)
    assert bool(match)
    assert len(match.groups()) == 0

@pytest.mark.randomize(ncalls=DEFAULT_TESTS_COUNT,
                       **str_args('ingroup', 'outgroup'))
def test_group_sans_backrefs(ingroup, outgroup):
    the_re = re.compile('(%s)%s' % (ingroup, outgroup))
    reversed_re = unmatcher.reverse(the_re)
    match = the_re.match(reversed_re)
    assert bool(match)
    assert match.groups() == (ingroup,)

@pytest.mark.randomize(ncalls=4,
                       **str_args('groupname', 'ingroup', 'outgroup'))
def test_named_group_sans_backrefs(groupname, ingroup, outgroup):
    groupname = 'a' + groupname  # cannot start with digit
    the_re = re.compile('(?P<%s>%s)%s' % (groupname, ingroup, outgroup))
    reversed_re = unmatcher.reverse(the_re)
    match = the_re.match(reversed_re)
    assert bool(match)
    assert match.groupdict() == {groupname: ingroup}

# TODO: test capture groups with backreferences: (foo)bar\1, (?P<huh>foo)bar(?P=huh)

# Utility functions

def chunks(seq, n):
    """Split a ``seq``\ uence into chunks of length ``n``."""
    return (seq[i:i+n] for i in xrange(0, len(seq), n)) if len(seq) > 0 else ()
<|endoftext|>