<commit_before>import re
import six

from cassandra.util import OrderedDict
from cassandra.cqlengine import CQLEngineException
from cassandra.cqlengine import columns
from cassandra.cqlengine import connection
from cassandra.cqlengine import models

class UserTypeException(CQLEngineException):
    pass

class UserTypeDefinitionException(UserTypeException):
    pass

class BaseUserType(object):
    """
    The base type class; don't inherit from this, inherit from UserType, defined below
    """
    __type_name__ = None

    _fields = None
    _db_map = None

    def __init__(self, **values):
        self._values = {}
        if self._db_map:
            values = dict((self._db_map.get(k, k), v) for k, v in values.items())

        for name, field in self._fields.items():
            value = values.get(name, None)
            if value is not None or isinstance(field, columns.BaseContainerColumn):
                value = field.to_python(value)
            value_mngr = field.value_manager(self, field, value)
            value_mngr.explicit = name in values
            self._values[name] = value_mngr

    def __eq__(self, other):
        if self.__class__ != other.__class__:
            return False

        keys = set(self._fields.keys())
        other_keys = set(other._fields.keys())
        if keys != other_keys:
            return False

        for key in other_keys:
            if getattr(self, key, None) != getattr(other, key, None):
                return False

        return True

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

    def __str__(self):
        return "{{{0}}}".format(', '.join("'{0}': {1}".format(k, getattr(self, k)) for k, v in six.iteritems(self._values)))

    def has_changed_fields(self):
        return any(v.changed for v in self._values.values())

    def reset_changed_fields(self):
        for v in self._values.values():
            v.reset_previous_value()

    def __iter__(self):
        for field in self._fields.keys():
            yield field

    def __getattr__(self, attr):
        # provides the mapping from db_field to fields
        try:
            return getattr(self, self._db_map[attr])
        except KeyError:
            raise AttributeError(attr)

    def __getitem__(self, key):
        if not isinstance(key, six.string_types):
            raise TypeError
        if key not in self._fields.keys():
            raise KeyError
        return getattr(self, key)

    def __setitem__(self, key, val):
        if not isinstance(key, six.string_types):
            raise TypeError
        if key not in self._fields.keys():
            raise KeyError
        return setattr(self, key, val)

    def __len__(self):
        try:
            return self._len
        except:
            self._len = len(self._fields.keys())
            return self._len

    def keys(self):
        """ Returns a list of column IDs. """
        return [k for k in self]

    def values(self):
        """ Returns list of column values. """
        return [self[k] for k in self]

    def items(self):
        """ Returns a list of column ID/value tuples. """
        return [(k, self[k]) for k in self]

    @classmethod
    def register_for_keyspace(cls, keyspace):
        connection.register_udt(keyspace, cls.type_name(), cls)

    @classmethod
    def type_name(cls):
        """
        Returns the type name if it's been defined
        otherwise, it creates it from the class name
        """
        if cls.__type_name__:
            type_name = cls.__type_name__.lower()
        else:
            camelcase = re.compile(r'([a-z])([A-Z])')
            ccase = lambda s: camelcase.sub(lambda v: '{0}_{1}'.format(v.group(1), v.group(2)), s)

            type_name = ccase(cls.__name__)
            # trim to less than 48 characters or cassandra will complain
            type_name = type_name[-48:]
            type_name = type_name.lower()
            type_name = re.sub(r'^_+', '', type_name)
            cls.__type_name__ = type_name

        return type_name

    def validate(self):
        """
        Cleans and validates the field values
        """
        for name, field in self._fields.items():
            v = getattr(self, name)
            if v is None and not self._values[name].explicit and field.has_default:
                v = field.get_default()
            val = field.validate(v)
            setattr(self, name, val)

class UserTypeMetaClass(type):

    def __new__(cls, name, bases, attrs):
        field_dict = OrderedDict()

        field_defs = [(k, v) for k, v in attrs.items() if isinstance(v, columns.Column)]
        field_defs = sorted(field_defs, key=lambda x: x[1].position)

        def _transform_column(field_name, field_obj):
            field_dict[field_name] = field_obj
            field_obj.set_column_name(field_name)
            attrs[field_name] = models.ColumnDescriptor(field_obj)

        # transform field definitions
        for k, v in field_defs:
            # don't allow a field with the same name as a built-in attribute or method
            if k in BaseUserType.__dict__:
                raise UserTypeDefinitionException("field '{0}' conflicts with built-in attribute/method".format(k))
            _transform_column(k, v)

        attrs['_fields'] = field_dict

        db_map = {}
        for field_name, field in field_dict.items():
            db_field = field.db_field_name
            if db_field != field_name:
                if db_field in field_dict:
                    raise UserTypeDefinitionException("db_field '{0}' for field '{1}' conflicts with another attribute name".format(db_field, field_name))
                db_map[db_field] = field_name
        attrs['_db_map'] = db_map

        klass = super(UserTypeMetaClass, cls).__new__(cls, name, bases, attrs)

        return klass

@six.add_metaclass(UserTypeMetaClass)
class UserType(BaseUserType):
    """
    This class is used to model User Defined Types. To define a type, declare a class inheriting from this,
    and assign field types as class attributes:

    .. code-block:: python

        # connect with default keyspace ...

        from cassandra.cqlengine.columns import Text, Integer
        from cassandra.cqlengine.usertype import UserType

        class address(UserType):
            street = Text()
            zipcode = Integer()

        from cassandra.cqlengine import management
        management.sync_type(address)

    Please see :ref:`user_types` for a complete example and discussion.
    """

    __type_name__ = None
    """
    *Optional.* Sets the name of the CQL type for this type.

    If not specified, the type name will be the name of the class, with it's module name as it's prefix.
    """
<commit_msg>cqle: assign UDT default on init<commit_after>import re
import six

from cassandra.util import OrderedDict
from cassandra.cqlengine import CQLEngineException
from cassandra.cqlengine import columns
from cassandra.cqlengine import connection
from cassandra.cqlengine import models

class UserTypeException(CQLEngineException):
    pass

class UserTypeDefinitionException(UserTypeException):
    pass

class BaseUserType(object):
    """
    The base type class; don't inherit from this, inherit from UserType, defined below
    """
    __type_name__ = None

    _fields = None
    _db_map = None

    def __init__(self, **values):
        self._values = {}
        if self._db_map:
            values = dict((self._db_map.get(k, k), v) for k, v in values.items())

        for name, field in self._fields.items():
            field_default = field.get_default() if field.has_default else None
            value = values.get(name, field_default)
            if value is not None or isinstance(field, columns.BaseContainerColumn):
                value = field.to_python(value)
            value_mngr = field.value_manager(self, field, value)
            value_mngr.explicit = name in values
            self._values[name] = value_mngr

    def __eq__(self, other):
        if self.__class__ != other.__class__:
            return False

        keys = set(self._fields.keys())
        other_keys = set(other._fields.keys())
        if keys != other_keys:
            return False

        for key in other_keys:
            if getattr(self, key, None) != getattr(other, key, None):
                return False

        return True

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

    def __str__(self):
        return "{{{0}}}".format(', '.join("'{0}': {1}".format(k, getattr(self, k)) for k, v in six.iteritems(self._values)))

    def has_changed_fields(self):
        return any(v.changed for v in self._values.values())

    def reset_changed_fields(self):
        for v in self._values.values():
            v.reset_previous_value()

    def __iter__(self):
        for field in self._fields.keys():
            yield field

    def __getattr__(self, attr):
        # provides the mapping from db_field to fields
        try:
            return getattr(self, self._db_map[attr])
        except KeyError:
            raise AttributeError(attr)

    def __getitem__(self, key):
        if not isinstance(key, six.string_types):
            raise TypeError
        if key not in self._fields.keys():
            raise KeyError
        return getattr(self, key)

    def __setitem__(self, key, val):
        if not isinstance(key, six.string_types):
            raise TypeError
        if key not in self._fields.keys():
            raise KeyError
        return setattr(self, key, val)

    def __len__(self):
        try:
            return self._len
        except:
            self._len = len(self._fields.keys())
            return self._len

    def keys(self):
        """ Returns a list of column IDs. """
        return [k for k in self]

    def values(self):
        """ Returns list of column values. """
        return [self[k] for k in self]

    def items(self):
        """ Returns a list of column ID/value tuples. """
        return [(k, self[k]) for k in self]

    @classmethod
    def register_for_keyspace(cls, keyspace):
        connection.register_udt(keyspace, cls.type_name(), cls)

    @classmethod
    def type_name(cls):
        """
        Returns the type name if it's been defined
        otherwise, it creates it from the class name
        """
        if cls.__type_name__:
            type_name = cls.__type_name__.lower()
        else:
            camelcase = re.compile(r'([a-z])([A-Z])')
            ccase = lambda s: camelcase.sub(lambda v: '{0}_{1}'.format(v.group(1), v.group(2)), s)

            type_name = ccase(cls.__name__)
            # trim to less than 48 characters or cassandra will complain
            type_name = type_name[-48:]
            type_name = type_name.lower()
            type_name = re.sub(r'^_+', '', type_name)
            cls.__type_name__ = type_name

        return type_name

    def validate(self):
        """
        Cleans and validates the field values
        """
        for name, field in self._fields.items():
            v = getattr(self, name)
            if v is None and not self._values[name].explicit and field.has_default:
                v = field.get_default()
            val = field.validate(v)
            setattr(self, name, val)

class UserTypeMetaClass(type):

    def __new__(cls, name, bases, attrs):
        field_dict = OrderedDict()

        field_defs = [(k, v) for k, v in attrs.items() if isinstance(v, columns.Column)]
        field_defs = sorted(field_defs, key=lambda x: x[1].position)

        def _transform_column(field_name, field_obj):
            field_dict[field_name] = field_obj
            field_obj.set_column_name(field_name)
            attrs[field_name] = models.ColumnDescriptor(field_obj)

        # transform field definitions
        for k, v in field_defs:
            # don't allow a field with the same name as a built-in attribute or method
            if k in BaseUserType.__dict__:
                raise UserTypeDefinitionException("field '{0}' conflicts with built-in attribute/method".format(k))
            _transform_column(k, v)

        attrs['_fields'] = field_dict

        db_map = {}
        for field_name, field in field_dict.items():
            db_field = field.db_field_name
            if db_field != field_name:
                if db_field in field_dict:
                    raise UserTypeDefinitionException("db_field '{0}' for field '{1}' conflicts with another attribute name".format(db_field, field_name))
                db_map[db_field] = field_name
        attrs['_db_map'] = db_map

        klass = super(UserTypeMetaClass, cls).__new__(cls, name, bases, attrs)

        return klass

@six.add_metaclass(UserTypeMetaClass)
class UserType(BaseUserType):
    """
    This class is used to model User Defined Types. To define a type, declare a class inheriting from this,
    and assign field types as class attributes:

    .. code-block:: python

        # connect with default keyspace ...

        from cassandra.cqlengine.columns import Text, Integer
        from cassandra.cqlengine.usertype import UserType

        class address(UserType):
            street = Text()
            zipcode = Integer()

        from cassandra.cqlengine import management
        management.sync_type(address)

    Please see :ref:`user_types` for a complete example and discussion.
    """

    __type_name__ = None
    """
    *Optional.* Sets the name of the CQL type for this type.

    If not specified, the type name will be the name of the class, with it's module name as it's prefix.
    """
<|endoftext|>


<commit_before>#!/usr/bin/env python
"""Perform a molecular optimization using a set of smiles and CP2K."""
import argparse
import logging
import os
from collections import OrderedDict

import numpy as np
import pandas as pd
import pkg_resources
from noodles import gather
from scm.plams import Molecule, finish, from_smiles, init, run_crs_adf

from nac.workflows.templates import generate_kinds
from qmflows import Settings, cp2k, run, templates

# Starting logger
logger = logging.getLogger(__name__)

# Solvent to compute properties
solvents = OrderedDict({"toluene": "cc1ccccc1", "o-xylene": "cc1c(c)cccc1",
                        "octadecene": "C=CCCCCCCCCCCCCCCCC"})

def main(file_path: str):
    """Run script."""
    set_logger()
    # Read input smiles
    df_smiles = pd.read_csv(file_path)
    # Path results
    path_results = "results.csv"
    # Read the database file o create new db
    df_results = pd.read_csv(path_results) if os.path.exists(
        path_results) else pd.DataFrame(columns=solvents.keys())
    # HDF5 to store the array
    # Compute the properties
    for smile in df_smiles["smiles"]:
        if smile in df_results["smiles"]:
            logger.info(f"properties of {smile} are already store!")
        else:
            results = compute_properties(smile)
            store_results_in_df(results, path_results)

if __name__ == "__main__":
    """Parse the command line arguments and call the modeller class."""
    parser = argparse.ArgumentParser(
        description="smiles_calculator.py -i smiles.csv")
    # configure logger
    parser.add_argument('-i', '--input', required=True,
                        help="Input file with the smiles")
    args = parser.parse_args()
    print(args)
    main(args.input)

def store_results_in_df(results: tuple, path_results: str):
    """Store the computed properties in the results df."""
    pass

def compute_properties(smile: str) -> np.array:
    """Compute properties for the given smile and solvent."""
    # Create the CP2K job
    job_cp2k = create_job_cp2k(smile, {smile})

    # Run the cp2k job
    energy, optimized_geometry = run(
        gather(job_cp2k.energy, job_cp2k.geometry), folder="/tmp/smiles")

    # Create the ADF JOB
    crs_dict = create_job_adf(smile, optimized_geometry)

    # extract results
    delta_g = pd.Series({name: try_to_readkf("delta_g")
                         for name, results in crs_dict.items()})
    gammas = pd.Series({name: try_to_readkf("gamma")
                        for name, results in crs_dict.items()})

    # Add name to the series
    delta_g.name = "delta_g"
    gammas.name = "gammas"

    return delta_g, gammas

def create_job_cp2k(smile: str, job_name: str) -> object:
    """Create a CP2K job object."""
    # Set path for basis set
    path_basis = pkg_resources.resource_filename("nac", "basis/BASIS_MOLOPT")
    path_potential = pkg_resources.resource_filename(
        "nac", "basis/GTH_POTENTIALS")

    # Settings specifics
    s = Settings()
    s.basis = "DZVP-MOLOPT-SR-GTH"
    s.potential = "GTH-PBE"
    s.cell_parameters = None
    s.specific.cp2k.force_eval.dft.basis_set_file_name = path_basis
    s.specific.cp2k.force_eval.dft.potential_file_name = path_potential

    # Molecular geometry
    system = from_smiles(smile)

    # Generate kinds for the atom types
    elements = [x.symbol for x in system.atoms]
    kinds = generate_kinds(elements, s.basis, s.potential)

    # Update the setting with the kinds
    s = s + kinds

    return cp2k(templates.geometry.overlay(s), system, job_name=job_name)

def create_job_adf(smile: str, optimized_geometry: Molecule, workdir: str = "tmp/smiles") -> object:
    """Create a Single Point Calculation with ADF on geometries optimized with CP2k."""
    settings_adf = Settings()
    settings_adf.input.basis.type = 'TZ2P'
    settings_adf.input.xc.gga = 'PBE'
    settings_adf.input.scf.converge = '1.0e-06'

    settings_crs = Settings()
    settings_crs.input.temperature = 298.15
    settings_crs.input.property._h = 'activitycoef'

    settings_adf.runscript.pre = "export SCM_TMPDIR=$PWD"
    settings_crs.runscript.pre = "export SCM_TMPDIR=$PWD"

    # ADF optimizations
    solvents_geometries = [from_smiles(smile) for smile in solvents.values()]

    init(workdir)
    crs_dict = run_crs_adf(settings_adf, settings_crs,
                           solvents_geometries, optimized_geometry)
    finish()

    return crs_dict

def try_to_readkf(results: object, property_name: str):
    """Try to read the output from a KF binary file."""
    try:
        return results.readkf("ACTIVITYCOEF", property_name)[1]
    except KeyError:
        return None

def set_logger():
    """Set logging default behaviour."""
    file_log = 'output.log'
    logging.basicConfig(filename=file_log, level=logging.DEBUG,
                        format='%(asctime)s---%(levelname)s\n%(message)s\n',
                        datefmt='[%I:%M:%S]')
<commit_msg>script to optimize smiles has been created<commit_after>#!/usr/bin/env python
"""Perform a molecular optimization using a set of smiles and CP2K."""
import argparse
import logging
import os
from collections import OrderedDict, namedtuple

import numpy as np
import pandas as pd
import pkg_resources
from rdkit import Chem
from rdkit.Chem import AllChem
from scm.plams import (Molecule, finish, from_rdmol, from_smiles, init,
                       run_crs_adf)

from nac.workflows.templates import generate_kinds
from qmflows import Settings, cp2k, run, templates

# Starting logger
logger = logging.getLogger(__name__)

# Solvent to compute properties
solvents = OrderedDict({"toluene": "CC1=CC=CC=C1", "o-xylene": "CC1=CC=CC=C1C",
                        "octadecene": "CCCCCCCCCCCCCCCCC=C"})

Result = namedtuple("Result", ['gammas', 'delta_g'])

def set_logger():
    """Set logging default behaviour."""
    file_log = 'output.log'
    logging.basicConfig(filename=file_log, level=logging.DEBUG,
                        format='%(asctime)s---%(levelname)s\n%(message)s\n',
                        datefmt='[%I:%M:%S]')

def create_multiindex_df() -> pd.DataFrame:
    """Create a multiindex DataFrame to store the compute properties for each smile/solvent."""
    idx = pd.MultiIndex.from_tuples([], names=["smile", "property"])

    return pd.DataFrame(columns=solvents.keys(), index=idx)

def store_results_in_df(smile: str, results: namedtuple, df_results: pd.DataFrame, path_results: str):
    """Store the computed properties in the results DataFrame."""
    cols = df_results.columns
    df_results.loc[(smile, "gammas"), cols] = results.gammas
    df_results.loc[(smile, "delta_g"), cols] = results.delta_g
    df_results.to_csv(path_results)

def compute_properties(smile: str) -> np.array:
    """Compute properties for the given smile and solvent."""
    # Create the CP2K job
    job_cp2k = create_job_cp2k(smile, smile)

    # Run the cp2k job
    optimized_geometry = run(job_cp2k.geometry, folder="/tmp/cp2k_job")

    # Create the ADF JOB
    crs_dict = create_job_adf(smile, optimized_geometry)

    # extract results
    delta_g = pd.Series({name: try_to_readkf("delta_g")
                         for name, results in crs_dict.items()})
    gammas = pd.Series({name: try_to_readkf("gamma")
                        for name, results in crs_dict.items()})

    return Result(gammas, delta_g)

def create_job_cp2k(smile: str, job_name: str) -> object:
    """Create a CP2K job object."""
    # Set path for basis set
    path_basis = pkg_resources.resource_filename("nac", "basis/BASIS_MOLOPT")
    path_potential = pkg_resources.resource_filename(
        "nac", "basis/GTH_POTENTIALS")

    # Settings specifics
    s = Settings()
    s.basis = "DZVP-MOLOPT-SR-GTH"
    s.potential = "GTH-PBE"
    s.cell_parameters = 5
    s.specific.cp2k.force_eval.dft.basis_set_file_name = path_basis
    s.specific.cp2k.force_eval.dft.potential_file_name = path_potential

    # functional
    s.specific.cp2k.force_eval.dft.xc["xc_functional"] = {}

    # Molecular geometry
    system = try_to_optimize(smile)

    # Generate kinds for the atom types
    elements = [x.symbol for x in system.atoms]
    kinds = generate_kinds(elements, s.basis, s.potential)

    # Update the setting with the kinds
    sett = templates.geometry.overlay(s)
    sett.specific = sett.specific + kinds

    return cp2k(sett, system, job_name="cp2k_opt")

def try_to_optimize(smile: str) -> Molecule:
    """Try to optimize the molecule with a force field."""
    try:
        # Try to optimize with RDKIT
        mol = Chem.MolFromSmiles(smile)
        AllChem.EmbedMolecule(mol)
        AllChem.MMFFOptimizeMolecule(mol)
        mol = from_rdmol(mol)
    except:
        mol = from_smiles(smile)

    return mol

def create_job_adf(smile: str, optimized_geometry: Molecule, workdir: str = "/tmp/adf") -> object:
    """Create a Single Point Calculation with ADF on geometries optimized with CP2k."""
    settings_adf = Settings()
    settings_adf.input.basis.type = 'TZ2P'
    settings_adf.input.xc.gga = 'PBE'
    settings_adf.input.scf.converge = '1.0e-06'

    settings_crs = Settings()
    settings_crs.input.temperature = 298.15
    settings_crs.input.property._h = 'activitycoef'

    settings_adf.runscript.pre = "export SCM_TMPDIR=$PWD"
    settings_crs.runscript.pre = "export SCM_TMPDIR=$PWD"

    # ADF optimizations
    solvents_geometries = [from_smiles(smile) for smile in solvents.values()]

    if not os.path.exists(workdir):
        os.makedirs(workdir)

    init(workdir)
    crs_dict = run_crs_adf(settings_adf, settings_crs,
                           solvents_geometries, optimized_geometry)
    finish()

    return crs_dict

def try_to_readkf(results: object, property_name: str):
    """Try to read the output from a KF binary file."""
    try:
        return results.readkf("ACTIVITYCOEF", property_name)[1]
    except KeyError:
        return None

def main(file_path: str):
    """Run script."""
    set_logger()
    # Read input smiles
    df_smiles = pd.read_csv(file_path)
    # Path results
    path_results = "results.csv"
    # Read the database file o create new db
    df_results = pd.read_csv(path_results) if os.path.exists(
        path_results) else create_multiindex_df()

    # Compute the properties
    for smile in df_smiles["smiles"]:
        if smile in df_results.index:
            logger.info(f"properties of {smile} are already store!")
        else:
            results = compute_properties(smile)
            store_results_in_df(smile, results, df_results, path_results)

if __name__ == "__main__":
    """Parse the command line arguments and call the modeller class."""
    parser = argparse.ArgumentParser(
        description="smiles_calculator.py -i smiles.csv")
    # configure logger
    parser.add_argument('-i', '--input', required=True,
                        help="Input file with the smiles")
    args = parser.parse_args()

    main(args.input)
<|endoftext|>


<commit_before>import datetime

import cassiopeia.riotapi
import cassiopeia.dto.matchlistapi
import cassiopeia.core.requests
import cassiopeia.type.core.common
import cassiopeia.type.core.matchlist

def get_match_list(summoner, begin_index=-1, begin_time=0, end_time=0, champions=None, ranked_queues=None, seasons=None):
    """Gets a summoner's match history

    summoner        Summoner                     the summoner to get match history for
    begin_index     int                          the game index to start from (default -1)
    begin_time      int | datetime               the begin time to use for fetching games (default 0)
    end_time        int | datetime               the end time to use for fetching games (default 0)
    champions       Champion | list<Champion>    the champion(s) to limit the results to (default None)
    ranked_queue    Queue | list<Queue>          the ranked queue(s) to limit the results to (default None)
    seasons         Season | list<Season>        the season(s) to limit the results to (default None)

    return          list<MatchSummary>           the summoner's match history
    """
    if(ranked_queues and isinstance(ranked_queues, list)):
        for queue in ranked_queues:
            if queue not in cassiopeia.type.core.common.ranked_queues:
                raise ValueError("{queue} is not a ranked queue".format(queue=queue))
    elif(ranked_queues):
        if ranked_queues not in cassiopeia.type.core.common.ranked_queues:
            raise ValueError("{queue} is not a ranked queue".format(queue=ranked_queues))

    # Convert core types to API-ready types
    if(isinstance(begin_time, datetime.datetime)):
        epoch = datetime.datetime.utcfromtimestamp(0)
        delta = begin_time - epoch
        begin_time = int(delta.total_seconds() * 1000)
    if(isinstance(end_time, datetime.datetime)):
        epoch = datetime.datetime.utcfromtimestamp(0)
        delta = end_time - epoch
        end_time = int(delta.total_seconds() * 1000)

    champion_ids = [champion.id for champion in champions] if isinstance(champions, list) else champions.id if champions else None
    queues = [queue.value for queue in ranked_queues] if isinstance(ranked_queues, list) else ranked_queues.value if ranked_queues else None
    seasons = [season.value for season in seasons] if isinstance(seasons, list) else seasons.value if seasons else None

    history = cassiopeia.dto.matchlistapi.get_match_list(summoner.id, begin_index, begin_time, end_time, champion_ids, queues, seasons)

    # Load required data if loading policy is eager
    if(cassiopeia.core.requests.load_policy is cassiopeia.type.core.common.LoadPolicy.eager):
        cassiopeia.riotapi.get_champions() if history.champion_ids else None

    return [cassiopeia.type.core.matchlist.MatchReference(ref) for ref in history.matches]<commit_msg>matchlist doc fix<commit_after>import datetime

import cassiopeia.riotapi
import cassiopeia.dto.matchlistapi
import cassiopeia.core.requests
import cassiopeia.type.core.common
import cassiopeia.type.core.matchlist

def get_match_list(summoner, begin_index=-1, begin_time=0, end_time=0, champions=None, ranked_queues=None, seasons=None):
    """Gets a summoner's match history

    summoner        Summoner                     the summoner to get match history for
    begin_index     int                          the game index to start from (default -1)
    begin_time      int | datetime               the begin time to use for fetching games (default 0)
    end_time        int | datetime               the end time to use for fetching games (default 0)
    champions       Champion | list<Champion>    the champion(s) to limit the results to (default None)
    ranked_queue    Queue | list<Queue>          the ranked queue(s) to limit the results to (default None)
    seasons         Season | list<Season>        the season(s) to limit the results to (default None)

    return          list<MatchReference>         the summoner's match history
    """
    if(ranked_queues and isinstance(ranked_queues, list)):
        for queue in ranked_queues:
            if queue not in cassiopeia.type.core.common.ranked_queues:
                raise ValueError("{queue} is not a ranked queue".format(queue=queue))
    elif(ranked_queues):
        if ranked_queues not in cassiopeia.type.core.common.ranked_queues:
            raise ValueError("{queue} is not a ranked queue".format(queue=ranked_queues))

    # Convert core types to API-ready types
    if(isinstance(begin_time, datetime.datetime)):
        epoch = datetime.datetime.utcfromtimestamp(0)
        delta = begin_time - epoch
        begin_time = int(delta.total_seconds() * 1000)
    if(isinstance(end_time, datetime.datetime)):
        epoch = datetime.datetime.utcfromtimestamp(0)
        delta = end_time - epoch
        end_time = int(delta.total_seconds() * 1000)

    champion_ids = [champion.id for champion in champions] if isinstance(champions, list) else champions.id if champions else None
    queues = [queue.value for queue in ranked_queues] if isinstance(ranked_queues, list) else ranked_queues.value if ranked_queues else None
    seasons = [season.value for season in seasons] if isinstance(seasons, list) else seasons.value if seasons else None

    history = cassiopeia.dto.matchlistapi.get_match_list(summoner.id, begin_index, begin_time, end_time, champion_ids, queues, seasons)

    # Load required data if loading policy is eager
    if(cassiopeia.core.requests.load_policy is cassiopeia.type.core.common.LoadPolicy.eager):
        cassiopeia.riotapi.get_champions() if history.champion_ids else None

    return [cassiopeia.type.core.matchlist.MatchReference(ref) for ref in history.matches]<|endoftext|>


<commit_before>import sys, py, pytest

class TestGeneralUsage:
    def test_config_error(self, testdir):
        testdir.makeconftest("""
            def pytest_configure(config):
                import pytest
                raise pytest.UsageError("hello")
        """)
        result = testdir.runpytest(testdir.tmpdir)
        assert result.ret != 0
        result.stderr.fnmatch_lines([
            '*ERROR: hello'
        ])

    def test_file_not_found(self, testdir):
        result = testdir.runpytest("asd")
        assert result.ret != 0
        result.stderr.fnmatch_lines(["ERROR: file not found*asd"])

    def test_config_preparse_plugin_option(self, testdir):
        testdir.makepyfile(pytest_xyz="""
            def pytest_addoption(parser):
                parser.addoption("--xyz", dest="xyz", action="store")
        """)
        testdir.makepyfile(test_one="""
            import py
            def test_option(pytestconfig):
                assert pytestconfig.option.xyz == "123"
        """)
        result = testdir.runpytest("-p", "xyz", "--xyz=123")
        assert result.ret == 0
        result.stdout.fnmatch_lines([
            '*1 passed*',
        ])

    def test_basetemp(self, testdir):
        mytemp = testdir.tmpdir.mkdir("mytemp")
        p = testdir.makepyfile("""
            import py
            def test_1(pytestconfig):
                pytestconfig.getbasetemp().ensure("hello")
        """)
        result = testdir.runpytest(p, '--basetemp=%s' %mytemp)
        assert result.ret == 0
        assert mytemp.join('hello').check()

    def test_assertion_magic(self, testdir):
        p = testdir.makepyfile("""
            def test_this():
                x = 0
                assert x
        """)
        result = testdir.runpytest(p)
        result.stdout.fnmatch_lines([
            ">       assert x",
            "E       assert 0",
        ])
        assert result.ret == 1

    def test_nested_import_error(self, testdir):
        p = testdir.makepyfile("""
                import import_fails
                def test_this():
                    assert import_fails.a == 1
        """)
        testdir.makepyfile(import_fails="import does_not_work")
        result = testdir.runpytest(p)
        result.stdout.fnmatch_lines([
            #XXX on jython this fails:  ">   import import_fails",
            "E   ImportError: No module named does_not_work",
        ])
        assert result.ret == 1

    def test_not_collectable_arguments(self, testdir):
        p1 = testdir.makepyfile("")
        p2 = testdir.makefile(".pyc", "123")
        result = testdir.runpytest(p1, p2)
        assert result.ret
        result.stderr.fnmatch_lines([
            "*ERROR: not found:*%s" %(p2.basename,)
        ])

    def test_early_skip(self, testdir):
        testdir.mkdir("xyz")
        testdir.makeconftest("""
            import py
            def pytest_collect_directory():
                py.test.skip("early")
        """)
        result = testdir.runpytest()
        assert result.ret == 0
        result.stdout.fnmatch_lines([
            "*1 skip*"
        ])

    def test_issue88_initial_file_multinodes(self, testdir):
        testdir.makeconftest("""
            import py
            class MyFile(py.test.collect.File):
                def collect(self):
                    return
            def pytest_collect_file(path, parent):
                return MyFile(path, parent)
        """)
        p = testdir.makepyfile("def test_hello(): pass")
        result = testdir.runpytest(p, "--collectonly")
        result.stdout.fnmatch_lines([
            "*MyFile*test_issue88*",
            "*Module*test_issue88*",
        ])

    def test_issue93_initialnode_importing_capturing(self, testdir):
        testdir.makeconftest("""
            import sys
            print ("should not be seen")
            sys.stderr.write("stder42\\n")
        """)
        result = testdir.runpytest()
        assert result.ret == 0
        assert "should not be seen" not in result.stdout.str()
        assert "stderr42" not in result.stderr.str()

    def test_conftest_printing_shows_if_error(self, testdir):
        testdir.makeconftest("""
            print ("should be seen")
            assert 0
        """)
        result = testdir.runpytest()
        assert result.ret != 0
        assert "should be seen" in result.stdout.str()

    @py.test.mark.skipif("not hasattr(os, 'symlink')")
    def test_chdir(self, testdir):
        testdir.tmpdir.join("py").mksymlinkto(py._pydir)
        p = testdir.tmpdir.join("main.py")
        p.write(py.code.Source("""
            import sys, os
            sys.path.insert(0, '')
            import py
            print (py.__file__)
            print (py.__path__)
            os.chdir(os.path.dirname(os.getcwd()))
            print (py.log)
        """))
        result = testdir.runpython(p, prepend=False)
        assert not result.ret

    def test_issue109_sibling_conftests_not_loaded(self, testdir):
        sub1 = testdir.tmpdir.mkdir("sub1")
        sub2 = testdir.tmpdir.mkdir("sub2")
        sub1.join("conftest.py").write("assert 0")
        result = testdir.runpytest(sub2)
        assert result.ret == 0
        sub2.ensure("__init__.py")
        p = sub2.ensure("test_hello.py")
        result = testdir.runpytest(p)
        assert result.ret == 0
        result = testdir.runpytest(sub1)
        assert result.ret != 0

    def test_directory_skipped(self, testdir):
        testdir.makeconftest("""
            import py
            def pytest_ignore_collect():
                py.test.skip("intentional")
        """)
        testdir.makepyfile("def test_hello(): pass")
        result = testdir.runpytest()
        assert result.ret == 0
        result.stdout.fnmatch_lines([
            "*1 skipped*"
        ])

    def test_multiple_items_per_collector_byid(self, testdir):
        c = testdir.makeconftest("""
            import py
            class MyItem(py.test.collect.Item):
                def runtest(self):
                    pass
            class MyCollector(py.test.collect.File):
                def collect(self):
                    return [MyItem(name="xyz", parent=self)]
            def pytest_collect_file(path, parent):
                if path.basename.startswith("conftest"):
                    return MyCollector(path, parent)
        """)
        result = testdir.runpytest(c.basename+"::"+"xyz")
        assert result.ret == 0
        result.stdout.fnmatch_lines([
            "*1 pass*",
        ])

    def test_skip_on_generated_funcarg_id(self, testdir):
        testdir.makeconftest("""
            import py
            def pytest_generate_tests(metafunc):
                metafunc.addcall({'x': 3}, id='hello-123')
            def pytest_runtest_setup(item):
                print (item.keywords)
                if 'hello-123' in item.keywords:
                    py.test.skip("hello")
                assert 0
        """)
        p = testdir.makepyfile("""def test_func(x): pass""")
        res = testdir.runpytest(p)
        assert res.ret == 0
        res.stdout.fnmatch_lines(["*1 skipped*"])

    def test_direct_addressing_selects(self, testdir):
        p = testdir.makepyfile("""
            def pytest_generate_tests(metafunc):
                metafunc.addcall({'i': 1}, id="1")
                metafunc.addcall({'i': 2}, id="2")
            def test_func(i):
                pass
        """)
        res = testdir.runpytest(p.basename + "::" + "test_func[1]")
        assert res.ret == 0
        res.stdout.fnmatch_lines(["*1 passed*"])

    def test_direct_addressing_notfound(self, testdir):
        p = testdir.makepyfile("""
            def test_func():
                pass
        """)
        res = testdir.runpytest(p.basename + "::" + "test_notfound")
        assert res.ret
        res.stderr.fnmatch_lines(["*ERROR*not found*"])

class TestInvocationVariants:
    def test_earlyinit(self, testdir):
        p = testdir.makepyfile("""
            import py
            assert hasattr(py.test, 'mark')
        """)
        result = testdir.runpython(p)
        assert result.ret == 0

    def test_pydoc(self, testdir):
        result = testdir.runpython_c("import py;help(py.test)")
        assert result.ret == 0
        s = result.stdout.str()
        assert 'MarkGenerator' in s

    def test_double_pytestcmdline(self, testdir):
        p = testdir.makepyfile(run="""
            import py
            py.test.cmdline.main()
            py.test.cmdline.main()
        """)
        testdir.makepyfile("""
            def test_hello():
                pass
        """)
        result = testdir.runpython(p)
        result.stdout.fnmatch_lines([
            "*1 passed*",
            "*1 passed*",
        ])

    @py.test.mark.skipif("sys.version_info < (2,5)")
    def test_python_minus_m_invocation_ok(self, testdir):
        p1 = testdir.makepyfile("def test_hello(): pass")
        res = testdir.run(py.std.sys.executable, "-m", "py.test", str(p1))
        assert res.ret == 0

    @py.test.mark.skipif("sys.version_info < (2,5)")
    def test_python_minus_m_invocation_fail(self, testdir):
        p1 = testdir.makepyfile("def test_fail(): 0/0")
        res = testdir.run(py.std.sys.executable, "-m", "py.test", str(p1))
        assert res.ret == 1

    def test_python_pytest_main(self, testdir):
        p1 = testdir.makepyfile("def test_pass(): pass")
        res = testdir.run(py.std.sys.executable, "-m", "pytest.main", str(p1))
        assert res.ret == 0
        res.stdout.fnmatch_lines(["*1 passed*"])

    @py.test.mark.skipif("sys.version_info < (2,7)")
    def test_python_pytest_package(self, testdir):
        p1 = testdir.makepyfile("def test_pass(): pass")
        res = testdir.run(py.std.sys.executable, "-m", "pytest", str(p1))
        assert res.ret == 0
        res.stdout.fnmatch_lines(["*1 passed*"])

    def test_equivalence_pytest_pytest(self):
        assert pytest.main == py.test.cmdline.main

    def test_invoke_with_string(self, capsys):
        retcode = pytest.main("-h")
        assert not retcode
        out, err = capsys.readouterr()
        assert "--help" in out
        pytest.raises(ValueError, lambda: pytest.main(retcode))

    def test_invoke_with_path(self, testdir, capsys):
        retcode = testdir.pytestmain(testdir.tmpdir)
        assert not retcode
        out, err = capsys.readouterr()

    def test_invoke_plugin_api(self, capsys):
        class MyPlugin:
            def pytest_addoption(self, parser):
                parser.addoption("--myopt")

        pytest.main(["-h"], plugins=[MyPlugin()])
        out, err = capsys.readouterr()
        assert "--myopt" in out

    def test_cmdline_python_package(self, testdir, monkeypatch):
        monkeypatch.delenv('PYTHONDONTWRITEBYTECODE')
        path = testdir.mkpydir("tpkg")
        path.join("test_hello.py").write("def test_hello(): pass")
        path.join("test_world.py").write("def test_world(): pass")
        result = testdir.runpytest("--pyargs", "tpkg")
        assert result.ret == 0
        result.stdout.fnmatch_lines([
            "*2 passed*"
        ])
        result = testdir.runpytest("--pyargs", "tpkg.test_hello")
        assert result.ret == 0
        result.stdout.fnmatch_lines([
            "*1 passed*"
        ])

    def test_cmdline_python_package_not_exists(self, testdir):
        result = testdir.runpytest("--pyargs", "tpkgwhatv")
        assert result.ret
        result.stderr.fnmatch_lines([
            "ERROR*file*or*package*not*found*",
        ])

    def test_noclass_discovery_if_not_testcase(self, testdir):
        testpath = testdir.makepyfile("""
            import unittest
            import py
            class TestHello(object):
                def test_hello(self):
                    assert self.attr

            class RealTest(TestHello, unittest.TestCase):
                attr = 42
        """)
        reprec = testdir.inline_run(testpath)
        reprec.assertoutcome(passed=1)
<commit_msg>PYTHONDONTWRITEBYTECODE might not be set<commit_after>import sys, py, pytest

class TestGeneralUsage:
    def test_config_error(self, testdir):
        testdir.makeconftest("""
            def pytest_configure(config):
                import pytest
                raise pytest.UsageError("hello")
        """)
        result = testdir.runpytest(testdir.tmpdir)
        assert result.ret != 0
        result.stderr.fnmatch_lines([
            '*ERROR: hello'
        ])

    def test_file_not_found(self, testdir):
        result = testdir.runpytest("asd")
        assert result.ret != 0
        result.stderr.fnmatch_lines(["ERROR: file not found*asd"])

    def test_config_preparse_plugin_option(self, testdir):
        testdir.makepyfile(pytest_xyz="""
            def pytest_addoption(parser):
                parser.addoption("--xyz", dest="xyz", action="store")
        """)
        testdir.makepyfile(test_one="""
            import py
            def test_option(pytestconfig):
                assert pytestconfig.option.xyz == "123"
        """)
        result = testdir.runpytest("-p", "xyz", "--xyz=123")
        assert result.ret == 0
        result.stdout.fnmatch_lines([
            '*1 passed*',
        ])

    def test_basetemp(self, testdir):
        mytemp = testdir.tmpdir.mkdir("mytemp")
        p = testdir.makepyfile("""
            import py
            def test_1(pytestconfig):
                pytestconfig.getbasetemp().ensure("hello")
        """)
        result = testdir.runpytest(p, '--basetemp=%s' %mytemp)
        assert result.ret == 0
        assert mytemp.join('hello').check()

    def test_assertion_magic(self, testdir):
        p = testdir.makepyfile("""
            def test_this():
                x = 0
                assert x
        """)
        result = testdir.runpytest(p)
        result.stdout.fnmatch_lines([
            ">       assert x",
            "E       assert 0",
        ])
        assert result.ret == 1

    def test_nested_import_error(self, testdir):
        p = testdir.makepyfile("""
                import import_fails
                def test_this():
                    assert import_fails.a == 1
        """)
        testdir.makepyfile(import_fails="import does_not_work")
        result = testdir.runpytest(p)
        result.stdout.fnmatch_lines([
            #XXX on jython this fails:  ">   import import_fails",
            "E   ImportError: No module named does_not_work",
        ])
        assert result.ret == 1

    def test_not_collectable_arguments(self, testdir):
        p1 = testdir.makepyfile("")
        p2 = testdir.makefile(".pyc", "123")
        result = testdir.runpytest(p1, p2)
        assert result.ret
        result.stderr.fnmatch_lines([
            "*ERROR: not found:*%s" %(p2.basename,)
        ])

    def test_early_skip(self, testdir):
        testdir.mkdir("xyz")
        testdir.makeconftest("""
            import py
            def pytest_collect_directory():
                py.test.skip("early")
        """)
        result = testdir.runpytest()
        assert result.ret == 0
        result.stdout.fnmatch_lines([
            "*1 skip*"
        ])

    def test_issue88_initial_file_multinodes(self, testdir):
        testdir.makeconftest("""
            import py
            class MyFile(py.test.collect.File):
                def collect(self):
                    return
            def pytest_collect_file(path, parent):
                return MyFile(path, parent)
        """)
        p = testdir.makepyfile("def test_hello(): pass")
        result = testdir.runpytest(p, "--collectonly")
        result.stdout.fnmatch_lines([
            "*MyFile*test_issue88*",
            "*Module*test_issue88*",
        ])

    def test_issue93_initialnode_importing_capturing(self, testdir):
        testdir.makeconftest("""
            import sys
            print ("should not be seen")
            sys.stderr.write("stder42\\n")
        """)
        result = testdir.runpytest()
        assert result.ret == 0
        assert "should not be seen" not in result.stdout.str()
        assert "stderr42" not in result.stderr.str()

    def test_conftest_printing_shows_if_error(self, testdir):
        testdir.makeconftest("""
            print ("should be seen")
            assert 0
        """)
        result = testdir.runpytest()
        assert result.ret != 0
        assert "should be seen" in result.stdout.str()

    @py.test.mark.skipif("not hasattr(os, 'symlink')")
    def test_chdir(self, testdir):
        testdir.tmpdir.join("py").mksymlinkto(py._pydir)
        p = testdir.tmpdir.join("main.py")
        p.write(py.code.Source("""
            import sys, os
            sys.path.insert(0, '')
            import py
            print (py.__file__)
            print (py.__path__)
            os.chdir(os.path.dirname(os.getcwd()))
            print (py.log)
        """))
        result = testdir.runpython(p, prepend=False)
        assert not result.ret

    def test_issue109_sibling_conftests_not_loaded(self, testdir):
        sub1 = testdir.tmpdir.mkdir("sub1")
        sub2 = testdir.tmpdir.mkdir("sub2")
        sub1.join("conftest.py").write("assert 0")
        result = testdir.runpytest(sub2)
        assert result.ret == 0
        sub2.ensure("__init__.py")
        p = sub2.ensure("test_hello.py")
        result = testdir.runpytest(p)
        assert result.ret == 0
        result = testdir.runpytest(sub1)
        assert result.ret != 0

    def test_directory_skipped(self, testdir):
        testdir.makeconftest("""
            import py
            def pytest_ignore_collect():
                py.test.skip("intentional")
        """)
        testdir.makepyfile("def test_hello(): pass")
        result = testdir.runpytest()
        assert result.ret == 0
        result.stdout.fnmatch_lines([
            "*1 skipped*"
        ])

    def test_multiple_items_per_collector_byid(self, testdir):
        c = testdir.makeconftest("""
            import py
            class MyItem(py.test.collect.Item):
                def runtest(self):
                    pass
            class MyCollector(py.test.collect.File):
                def collect(self):
                    return [MyItem(name="xyz", parent=self)]
            def pytest_collect_file(path, parent):
                if path.basename.startswith("conftest"):
                    return MyCollector(path, parent)
        """)
        result = testdir.runpytest(c.basename+"::"+"xyz")
        assert result.ret == 0
        result.stdout.fnmatch_lines([
            "*1 pass*",
        ])

    def test_skip_on_generated_funcarg_id(self, testdir):
        testdir.makeconftest("""
            import py
            def pytest_generate_tests(metafunc):
                metafunc.addcall({'x': 3}, id='hello-123')
            def pytest_runtest_setup(item):
                print (item.keywords)
                if 'hello-123' in item.keywords:
                    py.test.skip("hello")
                assert 0
        """)
        p = testdir.makepyfile("""def test_func(x): pass""")
        res = testdir.runpytest(p)
        assert res.ret == 0
        res.stdout.fnmatch_lines(["*1 skipped*"])

    def test_direct_addressing_selects(self, testdir):
        p = testdir.makepyfile("""
            def pytest_generate_tests(metafunc):
                metafunc.addcall({'i': 1}, id="1")
                metafunc.addcall({'i': 2}, id="2")
            def test_func(i):
                pass
        """)
        res = testdir.runpytest(p.basename + "::" + "test_func[1]")
        assert res.ret == 0
        res.stdout.fnmatch_lines(["*1 passed*"])

    def test_direct_addressing_notfound(self, testdir):
        p = testdir.makepyfile("""
            def test_func():
                pass
        """)
        res = testdir.runpytest(p.basename + "::" + "test_notfound")
        assert res.ret
        res.stderr.fnmatch_lines(["*ERROR*not found*"])

class TestInvocationVariants:
    def test_earlyinit(self, testdir):
        p = testdir.makepyfile("""
            import py
            assert hasattr(py.test, 'mark')
        """)
        result = testdir.runpython(p)
        assert result.ret == 0

    def test_pydoc(self, testdir):
        result = testdir.runpython_c("import py;help(py.test)")
        assert result.ret == 0
        s = result.stdout.str()
        assert 'MarkGenerator' in s

    def test_double_pytestcmdline(self, testdir):
        p = testdir.makepyfile(run="""
            import py
            py.test.cmdline.main()
            py.test.cmdline.main()
        """)
        testdir.makepyfile("""
            def test_hello():
                pass
        """)
        result = testdir.runpython(p)
        result.stdout.fnmatch_lines([
            "*1 passed*",
            "*1 passed*",
        ])

    @py.test.mark.skipif("sys.version_info < (2,5)")
    def test_python_minus_m_invocation_ok(self, testdir):
        p1 = testdir.makepyfile("def test_hello(): pass")
        res = testdir.run(py.std.sys.executable, "-m", "py.test", str(p1))
        assert res.ret == 0

    @py.test.mark.skipif("sys.version_info < (2,5)")
    def test_python_minus_m_invocation_fail(self, testdir):
        p1 = testdir.makepyfile("def test_fail(): 0/0")
        res = testdir.run(py.std.sys.executable, "-m", "py.test", str(p1))
        assert res.ret == 1

    def test_python_pytest_main(self, testdir):
        p1 = testdir.makepyfile("def test_pass(): pass")
        res = testdir.run(py.std.sys.executable, "-m", "pytest.main", str(p1))
        assert res.ret == 0
        res.stdout.fnmatch_lines(["*1 passed*"])

    @py.test.mark.skipif("sys.version_info < (2,7)")
    def test_python_pytest_package(self, testdir):
        p1 = testdir.makepyfile("def test_pass(): pass")
        res = testdir.run(py.std.sys.executable, "-m", "pytest", str(p1))
        assert res.ret == 0
        res.stdout.fnmatch_lines(["*1 passed*"])

    def test_equivalence_pytest_pytest(self):
        assert pytest.main == py.test.cmdline.main

    def test_invoke_with_string(self, capsys):
        retcode = pytest.main("-h")
        assert not retcode
        out, err = capsys.readouterr()
        assert "--help" in out
        pytest.raises(ValueError, lambda: pytest.main(retcode))

    def test_invoke_with_path(self, testdir, capsys):
        retcode = testdir.pytestmain(testdir.tmpdir)
        assert not retcode
        out, err = capsys.readouterr()

    def test_invoke_plugin_api(self, capsys):
        class MyPlugin:
            def pytest_addoption(self, parser):
                parser.addoption("--myopt")

        pytest.main(["-h"], plugins=[MyPlugin()])
        out, err = capsys.readouterr()
        assert "--myopt" in out

    def test_cmdline_python_package(self, testdir, monkeypatch):
        monkeypatch.delenv('PYTHONDONTWRITEBYTECODE', False)
        path = testdir.mkpydir("tpkg")
        path.join("test_hello.py").write("def test_hello(): pass")
        path.join("test_world.py").write("def test_world(): pass")
        result = testdir.runpytest("--pyargs", "tpkg")
        assert result.ret == 0
        result.stdout.fnmatch_lines([
            "*2 passed*"
        ])
        result = testdir.runpytest("--pyargs", "tpkg.test_hello")
        assert result.ret == 0
        result.stdout.fnmatch_lines([
            "*1 passed*"
        ])

    def test_cmdline_python_package_not_exists(self, testdir):
        result = testdir.runpytest("--pyargs", "tpkgwhatv")
        assert result.ret
        result.stderr.fnmatch_lines([
            "ERROR*file*or*package*not*found*",
        ])

    def test_noclass_discovery_if_not_testcase(self, testdir):
        testpath = testdir.makepyfile("""
            import unittest
            import py
            class TestHello(object):
                def test_hello(self):
                    assert self.attr

            class RealTest(TestHello, unittest.TestCase):
                attr = 42
        """)
        reprec = testdir.inline_run(testpath)
        reprec.assertoutcome(passed=1)
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
import redis
from hashring import HashRing
import functools

_findhash = re.compile('.*\{(.*)\}.*', re.I)

class RedisShardAPI(object):

    def __init__(self,servers):
        VERSION = tuple(map(int, redis.__version__.split('.')))
        self.nodes = []
        self.connections = {}
        if VERSION < (2,4,0):
            self.pool = redis.ConnectionPool()
        else:
            self.pool = None
        if isinstance(servers,list):
            for server in servers:
                conn = redis.Redis(host=server['host'], port=server['port'], db=server['db'],connection_pool=self.pool)
                name = server['name']
                if name in self.connections:
                    raise ValueError("server's name config must be unique")
                self.connections[name] = conn
                self.nodes.append(name)
        elif isinstance(servers,dict):
            for server_name,server in servers.items():
                conn = redis.Redis(host=server['host'], port=server['port'], db=server['db'],connection_pool=self.pool)
                name = server_name
                if name in self.connections:
                    raise ValueError("server's name config must be unique")
                self.connections[name] = conn
                self.nodes.append(name)
        else:
            raise ValueError("server's config must be list or dict")
        self.ring = HashRing(self.nodes)

    def get_server_name(self, key):
        g = _findhash.match(key)
        if g != None and len(g.groups()) > 0:
            key = g.groups()[0]
        name = self.ring.get_node(key)
        return name

    def get_server(self,key):
        name = self.get_server_name(key)
        return self.connections[name]

    def __wrap(self, method, *args, **kwargs):
        try:
            key = args[0]
            assert isinstance(key, basestring)
        except:
            raise ValueError("method '%s' requires a key param as the first argument" % method)
        server = self.get_server(key)
        f = getattr(server, method)
        return f(*args, **kwargs)

    def __getattr__(self, method):
        if method in [
            "get", "set", "getset",
            "setnx", "setex",
            "incr", "decr", "exists",
            "delete", "get_type", "rename",
            "expire", "ttl", "push",
            "llen", "lrange", "ltrim","lpush","lpop",
            "lindex", "pop", "lset",
            "lrem", "sadd", "srem",
            "sismember", "smembers",
            "zadd", "zrem", "zincr","zrank",
            "zrange", "zrevrange", "zrangebyscore","zremrangebyrank",
            "zremrangebyscore", "zcard", "zscore",
            "hget", "hset", "hdel", "hincrby", "hlen",
            "hkeys", "hvals", "hgetall", "hexists", "hmget", "hmset",
            "publish","brpop","blpop","lpush","rpush",
            ]:
            return functools.partial(self.__wrap, method)
        else:
            raise NotImplementedError("method '%s' cannot be sharded" % method)

    #########################################
    ###  some methods implement as needed ###
    ########################################

    def keys(self,key):
        _keys = []
        for node in self.nodes:
            server = self.connections[node]
            _keys.extend(server.keys(key))
        return _keys

    def tag_keys(self,key):
        server = self.get_server(key)
        return server.keys(key)
<commit_msg>add limit blpop/brpop support<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
import redis
from hashring import HashRing
import functools

_findhash = re.compile('.*\{(.*)\}.*', re.I)

class RedisShardAPI(object):

    def __init__(self,servers):
        VERSION = tuple(map(int, redis.__version__.split('.')))
        self.nodes = []
        self.connections = {}
        if VERSION < (2,4,0):
            self.pool = redis.ConnectionPool()
        else:
            self.pool = None
        if isinstance(servers,list):
            for server in servers:
                conn = redis.Redis(host=server['host'], port=server['port'], db=server['db'],connection_pool=self.pool)
                name = server['name']
                if name in self.connections:
                    raise ValueError("server's name config must be unique")
                self.connections[name] = conn
                self.nodes.append(name)
        elif isinstance(servers,dict):
            for server_name,server in servers.items():
                conn = redis.Redis(host=server['host'], port=server['port'], db=server['db'],connection_pool=self.pool)
                name = server_name
                if name in self.connections:
                    raise ValueError("server's name config must be unique")
                self.connections[name] = conn
                self.nodes.append(name)
        else:
            raise ValueError("server's config must be list or dict")
        self.ring = HashRing(self.nodes)

    def get_server_name(self, key):
        g = _findhash.match(key)
        if g != None and len(g.groups()) > 0:
            key = g.groups()[0]
        name = self.ring.get_node(key)
        return name

    def get_server(self,key):
        name = self.get_server_name(key)
        return self.connections[name]

    def __wrap(self, method, *args, **kwargs):
        try:
            key = args[0]
            assert isinstance(key, basestring)
        except:
            raise ValueError("method '%s' requires a key param as the first argument" % method)
        server = self.get_server(key)
        f = getattr(server, method)
        return f(*args, **kwargs)

    def __getattr__(self, method):
        if method in [
            "get", "set", "getset",
            "setnx", "setex",
            "incr", "decr", "exists",
            "delete", "get_type", "rename",
            "expire", "ttl", "push",
            "llen", "lrange", "ltrim","lpush","lpop",
            "lindex", "pop", "lset",
            "lrem", "sadd", "srem",
            "sismember", "smembers",
            "zadd", "zrem", "zincr","zrank",
            "zrange", "zrevrange", "zrangebyscore","zremrangebyrank",
            "zremrangebyscore", "zcard", "zscore",
            "hget", "hset", "hdel", "hincrby", "hlen",
            "hkeys", "hvals", "hgetall", "hexists", "hmget", "hmset",
            "publish","rpush","rpop"
            ]:
            return functools.partial(self.__wrap, method)
        else:
            raise NotImplementedError("method '%s' cannot be sharded" % method)

    #########################################
    ###  some methods implement as needed ###
    ########################################

    def brpop(self,key, timeout=0):
        if not isinstance(key, basestring):
            raise NotImplementedError("The key must be single string;mutiple keys cannot be sharded")
        server = self.get_server(key)
        return server.brpop(key,timeout)

    def blpop(self,key, timeout=0):
        if not isinstance(key, basestring):
            raise NotImplementedError("The key must be single string;mutiple keys cannot be sharded")
        server = self.get_server(key)
        return server.blpop(key,timeout)

    def keys(self,key):
        _keys = []
        for node in self.nodes:
            server = self.connections[node]
            _keys.extend(server.keys(key))
        return _keys

    def tag_keys(self,key):
        server = self.get_server(key)
        return server.keys(key)
<|endoftext|>


<commit_before>#!/usr/bin/env python
#coding=utf8

from os import statvfs
from func import *
from Local.Main  import *
from time import sleep
from copy import deepcopy

def Disk_Get_Info(Dev, Proc_Diskstats):

	Column_Diskstats = [
		'm', 			# 主设备号
		'mm', 			# 磁盘设备号
		'dev', 			# 磁盘设备名
		'reads', 		# number of read I/Os processed
		'rd_mrg', 		# number of read I/Os merged with in-queue I/O
		'rd_sectors', 	# number of sectors read
		'ms_reading', 	# total wait time for read requests
		'writes', 		# number of write I/Os processed
		'wr_mrg', 		# number of write I/Os merged with in-queue I/O
		'wr_sectors', 	# number of sectors written
		'ms_writing', 	# total wait time for write requests
		'cur_ios', 		# number of I/Os currently in flight
		'ms_doing_io', 	# total time this block device has been active
		'ms_weighted'	# total wait time for all requests
		]
	Docs = 'https://www.kernel.org/doc/Documentation/block/stat.txt'
	for Devs in Proc_Diskstats:
		if Devs.split()[2] == Dev:
			return dict(zip(Column_Diskstats, Devs.split()))

def Disk_Count_Add(Dev, Proc_Diskstats_1, Proc_Diskstats_2, avg_io):
	result = {}
	A_Disk_Info  = Disk_Get_Info(Dev, Proc_Diskstats_1)
	A_Reads_KB_1 = float(A_Disk_Info['rd_sectors'])
	A_Write_KB_2 = float(A_Disk_Info['wr_sectors'])
	A_Reads_IO_1 = float(A_Disk_Info['rd_mrg'])
	A_Write_IO_2 = float(A_Disk_Info['wr_mrg'])

	B_Disk_Info  = Disk_Get_Info(Dev, Proc_Diskstats_2)
	B_Reads_KB_1 = float(B_Disk_Info['rd_sectors'])
	B_Write_KB_2 = float(B_Disk_Info['wr_sectors'])
	B_Reads_IO_1 = float(B_Disk_Info['rd_mrg'])
	B_Write_IO_2 = float(B_Disk_Info['wr_mrg'])

	result["Reads_KB"] = Bytes(B_Reads_KB_1 - A_Reads_KB_1)
	result["Write_KB"] = Bytes(B_Write_KB_2 - A_Write_KB_2)
	result["Reads_IO"] = B_Reads_IO_1 - A_Reads_IO_1
	result["Write_IO"] = B_Write_IO_2 - A_Write_IO_2

	if not avg_io.has_key(Dev):
		avg_io[Dev] = result
	else:
		avg_io[Dev]['Reads_KB'] = avg_io[Dev]['Reads_KB'] + result['Reads_KB']
		avg_io[Dev]['Write_KB'] = avg_io[Dev]['Write_KB'] + result['Write_KB']
		avg_io[Dev]['Reads_IO'] = avg_io[Dev]['Reads_IO'] + result['Reads_IO']
		avg_io[Dev]['Write_IO'] = avg_io[Dev]['Write_IO'] + result['Write_IO']

	return avg_io

def Disk_Count_IO(All_Devices, avg_io):
	Proc_Diskstats_1 = Read_Proc('diskstats')
	sleep(TDM_Sleep)
	Proc_Diskstats_2 = Read_Proc('diskstats')

	for Dev in All_Devices:
		Disk_Count_Add(Dev, Proc_Diskstats_1, Proc_Diskstats_2, avg_io)

	return avg_io

def Disk_IO(data):
	Result = {}
	avg_io = {}

	Partitions, Devices = Disk_Partitions()

	for i in Devices:
		Devs = str(disk_dev_re.findall(i)[0])
		if Devs not in Devices:
			Devices.append(Devs)

	avg_io = Disk_Count_IO(Devices, avg_io)

	all_io = {
		"Reads_IO" : 0,
		"Write_IO" : 0,
		"Reads_KB" : 0,
		"Write_KB" : 0
	}

	for i in avg_io:
		for k, v in avg_io[i].items():
			avg_io[i][k] = int(v)
			all_io[k] = all_io.get(k) + avg_io[i][k]

	data['disk_io'] = avg_io
	data['disk_io']['all'] = all_io
	return data

def Disk_Usage(data):
	All_Disk_Total, All_Disk_Free, All_Disk_Used = 0, 0, 0
	All_Inode_Total, All_Inode_Used, All_Inode_Free = 0, 0, 0
	Partitions, Devices = Disk_Partitions()
	Usage      = {}
	for Partition in Partitions:
		Disk_Stat  = statvfs(Partition)
		Disk_Free  = (Disk_Stat.f_bfree  * Disk_Stat.f_frsize) / 1024
		Disk_Total = (Disk_Stat.f_blocks * Disk_Stat.f_frsize) / 1024
		Disk_Used  = Disk_Total - Disk_Free
		Disk_Free  = (Disk_Stat.f_bavail  * Disk_Stat.f_frsize) / 1024

		Inode_Total = Disk_Stat.f_files
		Inode_Free  = Disk_Stat.f_ffree
		Inode_Used  = Inode_Total - Inode_Free

		Usage[Partition] = {
			"Total"  : Bytes(Disk_Total),
			"Free"   : Bytes(Disk_Free),
			"Used"   : Bytes(Disk_Used),
			"Inodes" : Inode_Total,
			"IUsed"  : Inode_Used,
			"IFree"  : Inode_Free
		}
		All_Disk_Total  = All_Disk_Total  + Disk_Total
		All_Disk_Free   = All_Disk_Free   + Disk_Free
		All_Disk_Used   = All_Disk_Used   + Disk_Used
		All_Inode_Total = All_Inode_Total + Inode_Total
		All_Inode_Used  = All_Inode_Used  + Inode_Used
		All_Inode_Free  = All_Inode_Free  + Inode_Free
	# All Disk Usage
	Usage['All'] = {
		"Total"  : Bytes(All_Disk_Total),
		"Free"   : Bytes(All_Disk_Free),
		"Used"   : Bytes(All_Disk_Used),
		"Inodes" : All_Inode_Total,
		"IUsed"  : All_Inode_Used,
		"IFree"  : All_Inode_Free
	}
	data['disk_us'] = Usage
	return data

def Disk_Partitions():
	Proc_Mounts = Read_Proc('mounts')
	ExcludeType = ['rootfs', 'proc', 'sysfs', 'devtmpfs', 'devpts', 'tmpfs', 'usbfs', 'binfmt_misc']
	IncludeType = ['ext2', 'ext3', 'ext4', 'vfat', 'zfs', 'gfs', 'btrfs', 'xfs']
	Partitions  = []
	Devices     = []
	for Line in Proc_Mounts:
		if Line.split()[2] in IncludeType:
			Partitions.append(Line.split()[1])
			Fileln = os.path.realpath(Line.split()[0])
			Devices.append(Fileln.split('/')[-1])

	return Partitions, Devices
<commit_msg>fix ExcludeType<commit_after>#!/usr/bin/env python
#coding=utf8

from os import statvfs
from func import *
from Local.Main  import *
from time import sleep
from copy import deepcopy

def Disk_Get_Info(Dev, Proc_Diskstats):

	Column_Diskstats = [
		'm', 			# 主设备号
		'mm', 			# 磁盘设备号
		'dev', 			# 磁盘设备名
		'reads', 		# number of read I/Os processed
		'rd_mrg', 		# number of read I/Os merged with in-queue I/O
		'rd_sectors', 	# number of sectors read
		'ms_reading', 	# total wait time for read requests
		'writes', 		# number of write I/Os processed
		'wr_mrg', 		# number of write I/Os merged with in-queue I/O
		'wr_sectors', 	# number of sectors written
		'ms_writing', 	# total wait time for write requests
		'cur_ios', 		# number of I/Os currently in flight
		'ms_doing_io', 	# total time this block device has been active
		'ms_weighted'	# total wait time for all requests
		]
	Docs = 'https://www.kernel.org/doc/Documentation/block/stat.txt'
	for Devs in Proc_Diskstats:
		if Devs.split()[2] == Dev:
			return dict(zip(Column_Diskstats, Devs.split()))
	return {}

def Disk_Count_Add(Dev, Proc_Diskstats_1, Proc_Diskstats_2, avg_io):
	result = {}
	A_Disk_Info  = Disk_Get_Info(Dev, Proc_Diskstats_1)
	A_Reads_KB_1 = float(A_Disk_Info['rd_sectors'])
	A_Write_KB_2 = float(A_Disk_Info['wr_sectors'])
	A_Reads_IO_1 = float(A_Disk_Info['rd_mrg'])
	A_Write_IO_2 = float(A_Disk_Info['wr_mrg'])

	B_Disk_Info  = Disk_Get_Info(Dev, Proc_Diskstats_2)
	B_Reads_KB_1 = float(B_Disk_Info['rd_sectors'])
	B_Write_KB_2 = float(B_Disk_Info['wr_sectors'])
	B_Reads_IO_1 = float(B_Disk_Info['rd_mrg'])
	B_Write_IO_2 = float(B_Disk_Info['wr_mrg'])

	result["Reads_KB"] = Bytes(B_Reads_KB_1 - A_Reads_KB_1)
	result["Write_KB"] = Bytes(B_Write_KB_2 - A_Write_KB_2)
	result["Reads_IO"] = B_Reads_IO_1 - A_Reads_IO_1
	result["Write_IO"] = B_Write_IO_2 - A_Write_IO_2

	if not avg_io.has_key(Dev):
		avg_io[Dev] = result
	else:
		avg_io[Dev]['Reads_KB'] = avg_io[Dev]['Reads_KB'] + result['Reads_KB']
		avg_io[Dev]['Write_KB'] = avg_io[Dev]['Write_KB'] + result['Write_KB']
		avg_io[Dev]['Reads_IO'] = avg_io[Dev]['Reads_IO'] + result['Reads_IO']
		avg_io[Dev]['Write_IO'] = avg_io[Dev]['Write_IO'] + result['Write_IO']

	return avg_io

def Disk_Count_IO(All_Devices, avg_io):
	Proc_Diskstats_1 = Read_Proc('diskstats')
	sleep(TDM_Sleep)
	Proc_Diskstats_2 = Read_Proc('diskstats')

	for Dev in All_Devices:
		Disk_Count_Add(Dev, Proc_Diskstats_1, Proc_Diskstats_2, avg_io)

	return avg_io

def Disk_IO(data):
	Result = {}
	avg_io = {}

	Partitions, Devices = Disk_Partitions()

	for i in Devices:
		Devs = str(disk_dev_re.findall(i)[0])
		if Devs not in Devices:
			Devices.append(Devs)

	avg_io = Disk_Count_IO(Devices, avg_io)

	all_io = {
		"Reads_IO" : 0,
		"Write_IO" : 0,
		"Reads_KB" : 0,
		"Write_KB" : 0
	}

	for i in avg_io:
		for k, v in avg_io[i].items():
			avg_io[i][k] = int(v)
			all_io[k] = all_io.get(k) + avg_io[i][k]

	data['disk_io'] = avg_io
	data['disk_io']['all'] = all_io
	return data

def Disk_Usage(data):
	All_Disk_Total, All_Disk_Free, All_Disk_Used = 0, 0, 0
	All_Inode_Total, All_Inode_Used, All_Inode_Free = 0, 0, 0
	Partitions, Devices = Disk_Partitions()
	Usage      = {}
	for Partition in Partitions:
		Disk_Stat  = statvfs(Partition)
		Disk_Free  = (Disk_Stat.f_bfree  * Disk_Stat.f_frsize) / 1024
		Disk_Total = (Disk_Stat.f_blocks * Disk_Stat.f_frsize) / 1024
		Disk_Used  = Disk_Total - Disk_Free
		Disk_Free  = (Disk_Stat.f_bavail  * Disk_Stat.f_frsize) / 1024

		Inode_Total = Disk_Stat.f_files
		Inode_Free  = Disk_Stat.f_ffree
		Inode_Used  = Inode_Total - Inode_Free

		Usage[Partition] = {
			"Total"  : Bytes(Disk_Total),
			"Free"   : Bytes(Disk_Free),
			"Used"   : Bytes(Disk_Used),
			"Inodes" : Inode_Total,
			"IUsed"  : Inode_Used,
			"IFree"  : Inode_Free
		}
		All_Disk_Total  = All_Disk_Total  + Disk_Total
		All_Disk_Free   = All_Disk_Free   + Disk_Free
		All_Disk_Used   = All_Disk_Used   + Disk_Used
		All_Inode_Total = All_Inode_Total + Inode_Total
		All_Inode_Used  = All_Inode_Used  + Inode_Used
		All_Inode_Free  = All_Inode_Free  + Inode_Free
	# All Disk Usage
	Usage['All'] = {
		"Total"  : Bytes(All_Disk_Total),
		"Free"   : Bytes(All_Disk_Free),
		"Used"   : Bytes(All_Disk_Used),
		"Inodes" : All_Inode_Total,
		"IUsed"  : All_Inode_Used,
		"IFree"  : All_Inode_Free
	}
	data['disk_us'] = Usage
	return data

def Disk_Partitions():
	Proc_Mounts = Read_Proc('mounts')
	ExcludeType = ['rootfs', 'proc', 'sysfs', 'devtmpfs', 'devpts', 'tmpfs', 'usbfs', 'binfmt_misc', 'dm', '/dev/mapper/']
	IncludeType = ['ext2', 'ext3', 'ext4', 'vfat', 'zfs', 'gfs', 'btrfs', 'xfs']
	Partitions  = []
	Devices     = []
	for Line in Proc_Mounts:
		if Line.split()[2] in IncludeType:
			s = False
			for exclude in ExcludeType:
				if exclude in Line.split()[0]:
					s = True
					break
			if s: break
			Partitions.append(Line.split()[1])
			Fileln = os.path.realpath(Line.split()[0])
			Devices.append(Fileln.split('/')[-1])

	return Partitions, Devices
<|endoftext|>