<commit_before>from optparse import OptionParser, OptionGroup, Values

from .attacks import all_attacks
from .utils import read_config

def parse_options():
    parser = OptionParser(usage='usage: %prog [options] url...')

    default_options = OptionGroup(parser, "Default", "")
    default_options.add_option('--verbose', '-v', default=None, dest="verbose",
                               action="store_true",
                               help="Print the current targets, etc.")
    default_options.add_option('--very-verbose', '--vv', default=False,
                               dest="very_verbose", action="store_true",
                               help="Print every")
    default_options.add_option('--dont-filter', default=False, dest="do_print",
                               action="store_true",
                               help="Write output directly to the command"
                               "line, don't filter it.")
    default_options.add_option('--vulnerabilities-only', default=False,
                               dest="vuln_only", action="store_true",
                               help="Print only vulnerabilities "
                                    "(i.e. no warnings)")
    default_options.add_option('--abort-early', '-a', default=False,
                               dest="abort_early", action="store_true",
                               help="Exit on first found vulnerability.")
    default_options.add_option('--import-cookies', default=None,
                               dest="import_cookies",
                               help="Given a file, it will import it."
                               "(Hint: Useful to avoid Captchas...)")
    parser.add_option_group(default_options)

    crawling_options = OptionGroup(parser, "Crawling",
                                   "This section provides information"
                                   "about the different crawling options.")
    crawling_options.add_option('--no-crawl', action='store_true',
                                dest='no_crawl',
                                help="DO NOT search for links on the target")
    crawling_options.add_option('--whitelist', default=[],
                                dest="whitelist",
                                help="Hosts which are allowed to be crawled.")
    crawling_options.add_option('--blacklist', default=[], dest="blacklist",
                                action="append",
                                help="Specify sites which shouldn't be"
                                "visited or attacked. (Hint: logout)")

    parser.add_option_group(crawling_options)
    authentification_options = OptionGroup(parser, "Authentification",
                                           "Authentification to a specific"
                                           " post site.")
    authentification_options.add_option('--auth',  default=None,
                                        dest="auth_url",
                                        help="Post target for "
                                        "authentification")
    authentification_options.add_option('--auth-data',  dest='auth_data',
                                        action='append', type='str',
                                        default=[],
                                        help="A post parameter in the "
                                        "form of targetname=targetvalue")
    authentification_options.add_option('--form-page', dest='form_page',
                                        default=None,
                                        help="The site of the form you want "
                                        "to use to sign in")
    authentification_options.add_option('--form-id', dest='form_id',
                                        default=None,
                                        help="The id of the form you want "
                                        "to use to sign in.")
    authentification_options.add_option('--form-data', dest='form_data',
                                        action='append', type='str',
                                        default=[],
                                        help="A field you want to set "
                                        "manually.")
    parser.add_option_group(authentification_options)

    configuration_options = OptionGroup(parser, "Configuration",
                                        "You are also able to write your"
                                        " specified parameters in a file"
                                        " for easier usage.")
    configuration_options.add_option('--config', '-c', metavar='FILE',
                                     dest="read_config",
                                     help="Read the parameters from FILE")
    configuration_options.add_option('--write-config', metavar='FILE',
                                     dest="write_config",
                                     help="Insted of running the options,"
                                     ' write them to the specified file ("-" '
                                     'for standard output).')
    parser.add_option_group(configuration_options)

    # Options for scanning for specific vulnerabilities.
    attack_options = OptionGroup(parser, "Attacks",
                                 "If you specify own or several of the "
                                 "options _only_ this/these will be run. "
                                 "If you don't specify any, all will be "
                                 "run.")
    for attack in all_attacks():
        attack_options.add_option('--' + attack.__name__, dest=attack.__name__,
                                  action="store_true", default=False)
        attack_options.add_option('--except-' + attack.__name__,
                                  dest=attack.__name__ + "_except",
                                  action="store_true", default=False)
    parser.add_option_group(attack_options)

    # Get default values
    options, arguments = parser.parse_args([])

    # Parse command line
    cli_options = Values()
    _, cli_arguments = parser.parse_args(values=cli_options)

    # Update default values with configuration file
    config_fn = cli_options.__dict__.get('read_config')
    if config_fn is not None:
        read_options, read_arguments = read_config(config_fn, parser)
        options.__dict__.update(read_options)
        arguments += read_arguments

    # Update actual CLI options
    options.__dict__.update(cli_options.__dict__)
    arguments += cli_arguments

    if not arguments and not options.write_config:
        parser.error(u'Need at least one target')

    return (options, arguments)
<commit_msg>Remove unused option<commit_after>from optparse import OptionParser, OptionGroup, Values

from .attacks import all_attacks
from .utils import read_config

def parse_options():
    parser = OptionParser(usage='usage: %prog [options] url...')

    default_options = OptionGroup(parser, "Default", "")
    default_options.add_option('--verbose', '-v', default=None, dest="verbose",
                               action="store_true",
                               help="Print the current targets, etc.")
    default_options.add_option('--dont-filter', default=False, dest="do_print",
                               action="store_true",
                               help="Write output directly to the command"
                               "line, don't filter it.")
    default_options.add_option('--vulnerabilities-only', default=False,
                               dest="vuln_only", action="store_true",
                               help="Print only vulnerabilities "
                                    "(i.e. no warnings)")
    default_options.add_option('--abort-early', '-a', default=False,
                               dest="abort_early", action="store_true",
                               help="Exit on first found vulnerability.")
    default_options.add_option('--import-cookies', default=None,
                               dest="import_cookies",
                               help="Given a file, it will import it."
                               "(Hint: Useful to avoid Captchas...)")
    parser.add_option_group(default_options)

    crawling_options = OptionGroup(parser, "Crawling",
                                   "This section provides information"
                                   "about the different crawling options.")
    crawling_options.add_option('--no-crawl', action='store_true',
                                dest='no_crawl',
                                help="DO NOT search for links on the target")
    crawling_options.add_option('--whitelist', default=[],
                                dest="whitelist",
                                help="Hosts which are allowed to be crawled.")
    crawling_options.add_option('--blacklist', default=[], dest="blacklist",
                                action="append",
                                help="Specify sites which shouldn't be"
                                "visited or attacked. (Hint: logout)")

    parser.add_option_group(crawling_options)
    authentification_options = OptionGroup(parser, "Authentification",
                                           "Authentification to a specific"
                                           " post site.")
    authentification_options.add_option('--auth',  default=None,
                                        dest="auth_url",
                                        help="Post target for "
                                        "authentification")
    authentification_options.add_option('--auth-data',  dest='auth_data',
                                        action='append', type='str',
                                        default=[],
                                        help="A post parameter in the "
                                        "form of targetname=targetvalue")
    authentification_options.add_option('--form-page', dest='form_page',
                                        default=None,
                                        help="The site of the form you want "
                                        "to use to sign in")
    authentification_options.add_option('--form-id', dest='form_id',
                                        default=None,
                                        help="The id of the form you want "
                                        "to use to sign in.")
    authentification_options.add_option('--form-data', dest='form_data',
                                        action='append', type='str',
                                        default=[],
                                        help="A field you want to set "
                                        "manually.")
    parser.add_option_group(authentification_options)

    configuration_options = OptionGroup(parser, "Configuration",
                                        "You are also able to write your"
                                        " specified parameters in a file"
                                        " for easier usage.")
    configuration_options.add_option('--config', '-c', metavar='FILE',
                                     dest="read_config",
                                     help="Read the parameters from FILE")
    configuration_options.add_option('--write-config', metavar='FILE',
                                     dest="write_config",
                                     help="Insted of running the options,"
                                     ' write them to the specified file ("-" '
                                     'for standard output).')
    parser.add_option_group(configuration_options)

    # Options for scanning for specific vulnerabilities.
    attack_options = OptionGroup(parser, "Attacks",
                                 "If you specify own or several of the "
                                 "options _only_ this/these will be run. "
                                 "If you don't specify any, all will be "
                                 "run.")
    for attack in all_attacks():
        attack_options.add_option('--' + attack.__name__, dest=attack.__name__,
                                  action="store_true", default=False)
        attack_options.add_option('--except-' + attack.__name__,
                                  dest=attack.__name__ + "_except",
                                  action="store_true", default=False)
    parser.add_option_group(attack_options)

    # Get default values
    options, arguments = parser.parse_args([])

    # Parse command line
    cli_options = Values()
    _, cli_arguments = parser.parse_args(values=cli_options)

    # Update default values with configuration file
    config_fn = cli_options.__dict__.get('read_config')
    if config_fn is not None:
        read_options, read_arguments = read_config(config_fn, parser)
        options.__dict__.update(read_options)
        arguments += read_arguments

    # Update actual CLI options
    options.__dict__.update(cli_options.__dict__)
    arguments += cli_arguments

    if not arguments and not options.write_config:
        parser.error(u'Need at least one target')

    return (options, arguments)
<|endoftext|>


<commit_before># -*- encoding: utf-8 -*-
from supriya import SynthDefBuilder
from supriya import ugentools

def _make_synthdef(channel_count=2):
    with SynthDefBuilder(
        frequency_1=200,
        frequency_2=2000,
        frequency_3=5000,
        band_1_clamp_time=0.01,
        band_1_postgain=0,
        band_1_pregain=0,
        band_1_relax_time=0.1,
        band_1_slope_above=0.5,
        band_1_slope_below=1.0,
        band_1_threshold=0.9,
        band_2_clamp_time=0.01,
        band_2_postgain=0,
        band_2_pregain=0,
        band_2_relax_time=0.1,
        band_2_slope_above=0.5,
        band_2_slope_below=1.0,
        band_2_threshold=0.9,
        band_3_clamp_time=0.01,
        band_3_postgain=0,
        band_3_pregain=0,
        band_3_relax_time=0.1,
        band_3_slope_above=0.5,
        band_3_slope_below=1.0,
        band_3_threshold=0.9,
        band_4_clamp_time=0.01,
        band_4_postgain=0,
        band_4_pregain=0,
        band_4_relax_time=0.1,
        band_4_slope_above=0.5,
        band_4_slope_below=1.0,
        band_4_threshold=0.9,
        in_=0,
        out=0,
        ) as builder:
        source = ugentools.In.ar(
            bus=builder['in_'],
            channel_count=channel_count,
            )
        band_1 = ugentools.LPF.ar(
            frequency=builder['frequency_1'],
            source=source,
            )
        band_4 = ugentools.HPF.ar(
            frequency=builder['frequency_3'],
            source=source,
            )
        center = source - band_1 - band_4
        band_2 = ugentools.LPF.ar(
            frequency=builder['frequency_2'],
            source=center,
            )
        band_3 = ugentools.HPF.ar(
            frequency=builder['frequency_2'],
            source=center,
            )
        band_1 = ugentools.CompanderD.ar(
            clamp_time=builder['band_1_clamp_time'],
            relax_time=builder['band_1_relax_time'],
            slope_above=builder['band_1_slope_above'],
            slope_below=builder['band_1_slope_below'],
            source=band_1 * builder['band_1_pregain'].db_to_amplitude(),
            threshold=builder['band_1_threshold'].db_to_amplitude(),
            )
        band_2 = ugentools.CompanderD.ar(
            clamp_time=builder['band_2_clamp_time'],
            relax_time=builder['band_2_relax_time'],
            slope_above=builder['band_2_slope_above'],
            slope_below=builder['band_2_slope_below'],
            source=band_2 * builder['band_2_pregain'].db_to_amplitude(),
            threshold=builder['band_2_threshold'].db_to_amplitude(),
            )
        band_3 = ugentools.CompanderD.ar(
            clamp_time=builder['band_3_clamp_time'],
            relax_time=builder['band_3_relax_time'],
            slope_above=builder['band_3_slope_above'],
            slope_below=builder['band_3_slope_below'],
            source=band_3 * builder['band_3_pregain'].db_to_amplitude(),
            threshold=builder['band_3_threshold'].db_to_amplitude(),
            )
        band_4 = ugentools.CompanderD.ar(
            clamp_time=builder['band_4_clamp_time'],
            relax_time=builder['band_4_relax_time'],
            slope_above=builder['band_4_slope_above'],
            slope_below=builder['band_4_slope_below'],
            source=band_4 * builder['band_4_pregain'].db_to_amplitude(),
            threshold=builder['band_4_threshold'].db_to_amplitude(),
            )
        band_1 *= builder['band_1_postgain'].db_to_amplitude()
        band_2 *= builder['band_2_postgain'].db_to_amplitude()
        band_3 *= builder['band_3_postgain'].db_to_amplitude()
        band_4 *= builder['band_4_postgain'].db_to_amplitude()
        source = ugentools.Sum4.new(
            input_one=band_1,
            input_two=band_2,
            input_three=band_3,
            input_four=band_4,
            )
        source = ugentools.Limiter.ar(source=source)
        ugentools.ReplaceOut.ar(bus=builder['out'], source=source)
    return builder.build()

multiband_compressor = _make_synthdef(channel_count=2)

__all__ = (
    'multiband_compressor',
    )
<commit_msg>Added global pregain and postgain to compressor.<commit_after># -*- encoding: utf-8 -*-
from supriya import SynthDefBuilder
from supriya import ugentools

def _make_synthdef(channel_count=2):
    with SynthDefBuilder(
        frequency_1=200,
        frequency_2=2000,
        frequency_3=5000,
        band_1_clamp_time=0.01,
        band_1_postgain=0,
        band_1_pregain=0,
        band_1_relax_time=0.1,
        band_1_slope_above=0.5,
        band_1_slope_below=1.0,
        band_1_threshold=0.9,
        band_2_clamp_time=0.01,
        band_2_postgain=0,
        band_2_pregain=0,
        band_2_relax_time=0.1,
        band_2_slope_above=0.5,
        band_2_slope_below=1.0,
        band_2_threshold=0.9,
        band_3_clamp_time=0.01,
        band_3_postgain=0,
        band_3_pregain=0,
        band_3_relax_time=0.1,
        band_3_slope_above=0.5,
        band_3_slope_below=1.0,
        band_3_threshold=0.9,
        band_4_clamp_time=0.01,
        band_4_postgain=0,
        band_4_pregain=0,
        band_4_relax_time=0.1,
        band_4_slope_above=0.5,
        band_4_slope_below=1.0,
        band_4_threshold=0.9,
        pregain=0,
        postgain=0,
        in_=0,
        out=0,
        ) as builder:
        source = ugentools.In.ar(
            bus=builder['in_'],
            channel_count=channel_count,
            )
        source *= builder['pregain'].db_to_amplitude()
        band_1 = ugentools.LPF.ar(
            frequency=builder['frequency_1'],
            source=source,
            )
        band_4 = ugentools.HPF.ar(
            frequency=builder['frequency_3'],
            source=source,
            )
        center = source - band_1 - band_4
        band_2 = ugentools.LPF.ar(
            frequency=builder['frequency_2'],
            source=center,
            )
        band_3 = ugentools.HPF.ar(
            frequency=builder['frequency_2'],
            source=center,
            )
        band_1 = ugentools.CompanderD.ar(
            clamp_time=builder['band_1_clamp_time'],
            relax_time=builder['band_1_relax_time'],
            slope_above=builder['band_1_slope_above'],
            slope_below=builder['band_1_slope_below'],
            source=band_1 * builder['band_1_pregain'].db_to_amplitude(),
            threshold=builder['band_1_threshold'].db_to_amplitude(),
            )
        band_2 = ugentools.CompanderD.ar(
            clamp_time=builder['band_2_clamp_time'],
            relax_time=builder['band_2_relax_time'],
            slope_above=builder['band_2_slope_above'],
            slope_below=builder['band_2_slope_below'],
            source=band_2 * builder['band_2_pregain'].db_to_amplitude(),
            threshold=builder['band_2_threshold'].db_to_amplitude(),
            )
        band_3 = ugentools.CompanderD.ar(
            clamp_time=builder['band_3_clamp_time'],
            relax_time=builder['band_3_relax_time'],
            slope_above=builder['band_3_slope_above'],
            slope_below=builder['band_3_slope_below'],
            source=band_3 * builder['band_3_pregain'].db_to_amplitude(),
            threshold=builder['band_3_threshold'].db_to_amplitude(),
            )
        band_4 = ugentools.CompanderD.ar(
            clamp_time=builder['band_4_clamp_time'],
            relax_time=builder['band_4_relax_time'],
            slope_above=builder['band_4_slope_above'],
            slope_below=builder['band_4_slope_below'],
            source=band_4 * builder['band_4_pregain'].db_to_amplitude(),
            threshold=builder['band_4_threshold'].db_to_amplitude(),
            )
        band_1 *= builder['band_1_postgain'].db_to_amplitude()
        band_2 *= builder['band_2_postgain'].db_to_amplitude()
        band_3 *= builder['band_3_postgain'].db_to_amplitude()
        band_4 *= builder['band_4_postgain'].db_to_amplitude()
        source = ugentools.Sum4.new(
            input_one=band_1,
            input_two=band_2,
            input_three=band_3,
            input_four=band_4,
            )
        source *= builder['postgain'].db_to_amplitude()
        source = ugentools.Limiter.ar(source=source)
        ugentools.ReplaceOut.ar(bus=builder['out'], source=source)
    return builder.build()

multiband_compressor = _make_synthdef(channel_count=2)

__all__ = (
    'multiband_compressor',
    )
<|endoftext|>


<commit_before>
import discogs_client

user_agent = "MXPO/1.0"

token = "DfVagowbDawoqOLydLgUTbQqjEUUlYJXTptoUUvl"

d = discogs_client.Client(user_agent, user_token=token)

results = d.search("Gorillaz", type="release")

print results[0].labels[0].sublabels<commit_msg>removed discog db example<commit_after><|endoftext|>


<commit_before>#
# Copyright 2011-2013 Blender Foundation
#
# Licensed 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.
#

# <pep8 compliant>

def _is_using_buggy_driver():
    import bgl
    # We need to be conservative here because in multi-GPU systems display card
    # might be quite old, but others one might be just good.
    #
    # So We shouldn't disable possible good dedicated cards just because display
    # card seems weak. And instead we only blacklist configurations which are
    # proven to cause problems.
    if bgl.glGetString(bgl.GL_VENDOR) == "ATI Technologies Inc.":
        import re
        version = bgl.glGetString(bgl.GL_VERSION)
        if version.endswith("Compatibility Profile Context"):
            # Old HD 4xxx and 5xxx series drivers did not have driver version
            # in the version string, but thsoe cards do not quite work and
            # cusing crashes.
            return True
        regex = re.compile(".*Compatibility Profile Context ([0-9]+(\.[0-9]+)+)$")
        if not regex.match(version):
            # Skip cards like FireGL
            return False
        version = regex.sub("\\1", version).split('.')
        return int(version[0]) == 8
    return False

def _workaround_buggy_drivers():
    if _is_using_buggy_driver():
        import _cycles
        if hasattr(_cycles, "opencl_disable"):
            print("Cycles: OpenGL driver known to be buggy, disabling OpenCL platform.")
            _cycles.opencl_disable()

def init():
    import bpy
    import _cycles
    import os.path

    # Workaroud posibly buggy legacy drivers which crashes on the OpenCL
    # device enumeration.
    #
    # This checks are not really correct because they might still fail
    # in the case of multiple GPUs. However, currently buggy drivers
    # are really old and likely to be used in single GPU systems only
    # anyway.
    #
    # Can't do it in the background mode, so we hope OpenCL is no enabled
    # in the user preferences.
    if not bpy.app.background:
        _workaround_buggy_drivers()

    path = os.path.dirname(__file__)
    user_path = os.path.dirname(os.path.abspath(bpy.utils.user_resource('CONFIG', '')))

    _cycles.init(path, user_path, bpy.app.background)

def create(engine, data, scene, region=None, v3d=None, rv3d=None, preview_osl=False):
    import bpy
    import _cycles

    data = data.as_pointer()
    userpref = bpy.context.user_preferences.as_pointer()
    scene = scene.as_pointer()
    if region:
        region = region.as_pointer()
    if v3d:
        v3d = v3d.as_pointer()
    if rv3d:
        rv3d = rv3d.as_pointer()

    engine.session = _cycles.create(engine.as_pointer(), userpref, data, scene, region, v3d, rv3d, preview_osl)

def free(engine):
    if hasattr(engine, "session"):
        if engine.session:
            import _cycles
            _cycles.free(engine.session)
        del engine.session

def render(engine):
    import _cycles
    if hasattr(engine, "session"):
        _cycles.render(engine.session)

def bake(engine, obj, pass_type, object_id, pixel_array, num_pixels, depth, result):
    import _cycles
    session = getattr(engine, "session", None)
    if session is not None:
        _cycles.bake(engine.session, obj.as_pointer(), pass_type, object_id, pixel_array.as_pointer(), num_pixels, depth, result.as_pointer())

def reset(engine, data, scene):
    import _cycles
    data = data.as_pointer()
    scene = scene.as_pointer()
    _cycles.reset(engine.session, data, scene)

def update(engine, data, scene):
    import _cycles
    _cycles.sync(engine.session)

def draw(engine, region, v3d, rv3d):
    import _cycles
    v3d = v3d.as_pointer()
    rv3d = rv3d.as_pointer()

    # draw render image
    _cycles.draw(engine.session, v3d, rv3d)

def available_devices():
    import _cycles
    return _cycles.available_devices()

def with_osl():
    import _cycles
    return _cycles.with_osl

def with_network():
    import _cycles
    return _cycles.with_network

def system_info():
    import _cycles
    return _cycles.system_info()
<commit_msg>Cleanup: pep8<commit_after>#
# Copyright 2011-2013 Blender Foundation
#
# Licensed 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.
#

# <pep8 compliant>

def _is_using_buggy_driver():
    import bgl
    # We need to be conservative here because in multi-GPU systems display card
    # might be quite old, but others one might be just good.
    #
    # So We shouldn't disable possible good dedicated cards just because display
    # card seems weak. And instead we only blacklist configurations which are
    # proven to cause problems.
    if bgl.glGetString(bgl.GL_VENDOR) == "ATI Technologies Inc.":
        import re
        version = bgl.glGetString(bgl.GL_VERSION)
        if version.endswith("Compatibility Profile Context"):
            # Old HD 4xxx and 5xxx series drivers did not have driver version
            # in the version string, but thsoe cards do not quite work and
            # cusing crashes.
            return True
        regex = re.compile(".*Compatibility Profile Context ([0-9]+(\.[0-9]+)+)$")
        if not regex.match(version):
            # Skip cards like FireGL
            return False
        version = regex.sub("\\1", version).split('.')
        return int(version[0]) == 8
    return False

def _workaround_buggy_drivers():
    if _is_using_buggy_driver():
        import _cycles
        if hasattr(_cycles, "opencl_disable"):
            print("Cycles: OpenGL driver known to be buggy, disabling OpenCL platform.")
            _cycles.opencl_disable()

def init():
    import bpy
    import _cycles
    import os.path

    # Workaroud posibly buggy legacy drivers which crashes on the OpenCL
    # device enumeration.
    #
    # This checks are not really correct because they might still fail
    # in the case of multiple GPUs. However, currently buggy drivers
    # are really old and likely to be used in single GPU systems only
    # anyway.
    #
    # Can't do it in the background mode, so we hope OpenCL is no enabled
    # in the user preferences.
    if not bpy.app.background:
        _workaround_buggy_drivers()

    path = os.path.dirname(__file__)
    user_path = os.path.dirname(os.path.abspath(bpy.utils.user_resource('CONFIG', '')))

    _cycles.init(path, user_path, bpy.app.background)

def create(engine, data, scene, region=None, v3d=None, rv3d=None, preview_osl=False):
    import bpy
    import _cycles

    data = data.as_pointer()
    userpref = bpy.context.user_preferences.as_pointer()
    scene = scene.as_pointer()
    if region:
        region = region.as_pointer()
    if v3d:
        v3d = v3d.as_pointer()
    if rv3d:
        rv3d = rv3d.as_pointer()

    engine.session = _cycles.create(engine.as_pointer(), userpref, data, scene, region, v3d, rv3d, preview_osl)

def free(engine):
    if hasattr(engine, "session"):
        if engine.session:
            import _cycles
            _cycles.free(engine.session)
        del engine.session

def render(engine):
    import _cycles
    if hasattr(engine, "session"):
        _cycles.render(engine.session)

def bake(engine, obj, pass_type, object_id, pixel_array, num_pixels, depth, result):
    import _cycles
    session = getattr(engine, "session", None)
    if session is not None:
        _cycles.bake(engine.session, obj.as_pointer(), pass_type, object_id, pixel_array.as_pointer(), num_pixels, depth, result.as_pointer())

def reset(engine, data, scene):
    import _cycles
    data = data.as_pointer()
    scene = scene.as_pointer()
    _cycles.reset(engine.session, data, scene)

def update(engine, data, scene):
    import _cycles
    _cycles.sync(engine.session)

def draw(engine, region, v3d, rv3d):
    import _cycles
    v3d = v3d.as_pointer()
    rv3d = rv3d.as_pointer()

    # draw render image
    _cycles.draw(engine.session, v3d, rv3d)

def available_devices():
    import _cycles
    return _cycles.available_devices()

def with_osl():
    import _cycles
    return _cycles.with_osl

def with_network():
    import _cycles
    return _cycles.with_network

def system_info():
    import _cycles
    return _cycles.system_info()
<|endoftext|>


<commit_before>from pathlib import Path
from typing import List, Text
import re

import pytest

DOCS_BASE_DIR = Path("docs/")
MDX_DOCS_FILES = list((DOCS_BASE_DIR / "docs").glob("**/*.mdx"))
CODE_BLOCK_BOUNDARY = "```"

def get_heading_level(heading: Text) -> int:
    return len(heading.split(" ", 1)[0])

def get_heading(heading: Text) -> Text:
    return heading.split(" ", 1)[1]

@pytest.mark.parametrize("mdx_file_path", MDX_DOCS_FILES)
def test_docs_header_hierarchy(mdx_file_path: Path):
    with mdx_file_path.open("r") as handle:
        mdx_content = handle.read()

    headings: List[Tuple[int, Text, Text]] = []
    inside_code_block = False
    for idx, line in enumerate(mdx_content.split("\n")):
        if not inside_code_block and line.startswith(CODE_BLOCK_BOUNDARY):
            inside_code_block = True
        elif inside_code_block and line.startswith(CODE_BLOCK_BOUNDARY):
            inside_code_block = False

        if not inside_code_block and line.startswith("#"):
            headings.append((idx + 1, get_heading(line), get_heading_level(line)))

    errors: List[Text] = []
    prev_level = 1
    for line_number, title, level in headings:
        if level == 1:
            errors.append(
                f"\n  - Title '# {title}' at line {line_number} "
                f"cannot be level 1. Use '## {title}' instead."
            )

        if level > prev_level + 1:
            errors.append(
                f"\n  - Title '{'#' * level} {title}' at line {line_number} "
                f"is skipping a level. Use '{'#' * (prev_level + 1)} {title}' instead."
            )
            # that way we continue to have errors
            prev_level = prev_level + 1
        else:
            prev_level = level

    if errors:
        raise AssertionError(f"({mdx_file_path}):{''.join(errors)}")
<commit_msg>Add missing import Tuple<commit_after>from pathlib import Path
from typing import List, Text, Tuple
import re

import pytest

DOCS_BASE_DIR = Path("docs/")
MDX_DOCS_FILES = list((DOCS_BASE_DIR / "docs").glob("**/*.mdx"))
CODE_BLOCK_BOUNDARY = "```"

def get_heading_level(heading: Text) -> int:
    return len(heading.split(" ", 1)[0])

def get_heading(heading: Text) -> Text:
    return heading.split(" ", 1)[1]

@pytest.mark.parametrize("mdx_file_path", MDX_DOCS_FILES)
def test_docs_header_hierarchy(mdx_file_path: Path):
    with mdx_file_path.open("r") as handle:
        mdx_content = handle.read()

    headings: List[Tuple[int, Text, Text]] = []
    inside_code_block = False
    for idx, line in enumerate(mdx_content.split("\n")):
        if not inside_code_block and line.startswith(CODE_BLOCK_BOUNDARY):
            inside_code_block = True
        elif inside_code_block and line.startswith(CODE_BLOCK_BOUNDARY):
            inside_code_block = False

        if not inside_code_block and line.startswith("#"):
            headings.append((idx + 1, get_heading(line), get_heading_level(line)))

    errors: List[Text] = []
    prev_level = 1
    for line_number, title, level in headings:
        if level == 1:
            errors.append(
                f"\n  - Title '# {title}' at line {line_number} "
                f"cannot be level 1. Use '## {title}' instead."
            )

        if level > prev_level + 1:
            errors.append(
                f"\n  - Title '{'#' * level} {title}' at line {line_number} "
                f"is skipping a level. Use '{'#' * (prev_level + 1)} {title}' instead."
            )
            # that way we continue to have errors
            prev_level = prev_level + 1
        else:
            prev_level = level

    if errors:
        raise AssertionError(f"({mdx_file_path}):{''.join(errors)}")
<|endoftext|>


<commit_before>import datetime
import logging

from colander import iso8601
from colander.iso8601 import parse_date

from ichnaea.db import CellMeasure
from ichnaea.db import Measure
from ichnaea.db import RADIO_TYPE
from ichnaea.db import Score
from ichnaea.db import User
from ichnaea.db import WifiMeasure
from ichnaea.decimaljson import dumps
from ichnaea.decimaljson import to_precise_int

logger = logging.getLogger('ichnaea')

def handle_token_nickname(token, nickname, session):
    userid = None
    if not (24 <= len(token) <= 36):
        # doesn't look like it's a uuid
        token = ""
        nickname = ""
    elif (3 <= len(nickname) <= 128):
        # automatically create user objects and update nickname
        if isinstance(nickname, str):
            nickname = nickname.decode('utf-8', 'ignore')
        rows = session.query(User).filter(User.token == token)
        old = rows.first()
        if old:
            # update nickname
            old.nickname = nickname
            userid = old.id
        else:
            user = User()
            user.token = token
            user.nickname = nickname
            session.add(user)
            session.flush()
            userid = user.id
    return (userid, token, nickname)

def handle_score(userid, points, session):
    # TODO use select for update to lock the row
    rows = session.query(Score).filter(Score.userid == userid)
    old = rows.first()
    if old:
        # update score
        old.value = old.value + points
    else:
        score = Score()
        score.userid = userid
        score.value = points
        session.add(score)
    return points

def handle_time(measure, utcnow, utcmin):
    try:
        measure['time'] = iso8601.parse_date(measure['time'])
    except (iso8601.ParseError, TypeError):
        if measure['time']:  # pragma: no cover
            # ignore debug log for empty values
            logger.debug('submit_time_error' + repr(measure['time']))
        measure['time'] = utcnow
    else:
        # don't accept future time values or
        # time values more than 60 days in the past
        if measure['time'] > utcnow or measure['time'] < utcmin:
            measure['time'] = utcnow
    return measure

def submit_request(request):
    session = request.database.session()

    token = request.headers.get('X-Token', '')
    nickname = request.headers.get('X-Nickname', '')
    userid, token, nickname = handle_token_nickname(token, nickname, session)

    utcnow = datetime.datetime.utcnow().replace(tzinfo=iso8601.UTC)
    utcmin = utcnow - datetime.timedelta(60)

    points = 0
    for measure in request.validated['items']:
        measure = handle_time(measure, utcnow, utcmin)
        process_measure(measure, session)
        points += 1

    if userid is not None:
        handle_score(userid, points, session)

    session.commit()

def process_cell(cells, measure, session):
    result = []
    for entry in cells:
        cell = CellMeasure()
        cell.lat = measure.lat
        cell.lon = measure.lon
        cell.time = measure.time
        cell.accuracy = measure.accuracy
        cell.altitude = measure.altitude
        cell.altitude_accuracy = measure.altitude_accuracy
        cell.radio = measure.radio
        cell.mcc = entry['mcc']
        cell.mnc = entry['mnc']
        cell.lac = entry['lac']
        cell.cid = entry['cid']
        cell.psc = entry['psc']
        cell.asu = entry['asu']
        cell.signal = entry['signal']
        cell.ta = entry['ta']
        session.add(cell)
        result.append(entry)
    return result

def process_wifi(wifis, measure, session):
    result = []
    for entry in wifis:
        # convert frequency into channel numbers and remove frequency
        freq = entry.pop('frequency', 0)
        # if no explicit channel was given, calculate
        if freq and not entry['channel']:
            if 2411 < freq < 2473:
                # 2.4 GHz band
                entry['channel'] = (freq - 2407) // 5
            elif 5169 < freq < 5826:
                # 5 GHz band
                entry['channel'] = (freq - 5000) // 5
        wifi = WifiMeasure()
        wifi.lat = measure.lat
        wifi.lon = measure.lon
        wifi.time = measure.time
        wifi.accuracy = measure.accuracy
        wifi.altitude = measure.altitude
        wifi.altitude_accuracy = measure.altitude_accuracy
        wifi.key = entry['key']
        wifi.channel = entry['channel']
        wifi.signal = entry['signal']
        session.add(wifi)
        result.append(entry)
    return result

def process_measure(data, session):
    measure = Measure()
    time = data['time']
    if isinstance(time, basestring):
        time = parse_date(time)
    measure.time = time
    measure.lat = to_precise_int(data['lat'])
    measure.lon = to_precise_int(data['lon'])
    measure.accuracy = data['accuracy']
    measure.altitude = data['altitude']
    measure.altitude_accuracy = data['altitude_accuracy']
    if data.get('cell'):
        measure.radio = RADIO_TYPE.get(data['radio'], 0)
        measure.cell = dumps(process_cell(data['cell'], measure, session))
    if data.get('wifi'):
        measure.wifi = dumps(process_wifi(data['wifi'], measure, session))
    session.add(measure)
<commit_msg>reorganize submit module and unify function naming<commit_after>import datetime
import logging

from colander import iso8601
from colander.iso8601 import parse_date

from ichnaea.db import CellMeasure
from ichnaea.db import Measure
from ichnaea.db import RADIO_TYPE
from ichnaea.db import Score
from ichnaea.db import User
from ichnaea.db import WifiMeasure
from ichnaea.decimaljson import dumps
from ichnaea.decimaljson import to_precise_int

logger = logging.getLogger('ichnaea')

def process_user(token, nickname, session):
    userid = None
    if not (24 <= len(token) <= 36):
        # doesn't look like it's a uuid
        token = ""
        nickname = ""
    elif (3 <= len(nickname) <= 128):
        # automatically create user objects and update nickname
        if isinstance(nickname, str):
            nickname = nickname.decode('utf-8', 'ignore')
        rows = session.query(User).filter(User.token == token)
        old = rows.first()
        if old:
            # update nickname
            old.nickname = nickname
            userid = old.id
        else:
            user = User()
            user.token = token
            user.nickname = nickname
            session.add(user)
            session.flush()
            userid = user.id
    return (userid, token, nickname)

def process_score(userid, points, session):
    # TODO use select for update to lock the row
    rows = session.query(Score).filter(Score.userid == userid)
    old = rows.first()
    if old:
        # update score
        old.value = old.value + points
    else:
        score = Score()
        score.userid = userid
        score.value = points
        session.add(score)
    return points

def process_time(measure, utcnow, utcmin):
    try:
        measure['time'] = iso8601.parse_date(measure['time'])
    except (iso8601.ParseError, TypeError):
        if measure['time']:  # pragma: no cover
            # ignore debug log for empty values
            logger.debug('submit_time_error' + repr(measure['time']))
        measure['time'] = utcnow
    else:
        # don't accept future time values or
        # time values more than 60 days in the past
        if measure['time'] > utcnow or measure['time'] < utcmin:
            measure['time'] = utcnow
    return measure

def process_measure(data, session):
    measure = Measure()
    time = data['time']
    if isinstance(time, basestring):
        time = parse_date(time)
    measure.time = time
    measure.lat = to_precise_int(data['lat'])
    measure.lon = to_precise_int(data['lon'])
    measure.accuracy = data['accuracy']
    measure.altitude = data['altitude']
    measure.altitude_accuracy = data['altitude_accuracy']
    if data.get('cell'):
        measure.radio = RADIO_TYPE.get(data['radio'], 0)
        measure.cell = dumps(process_cell(data['cell'], measure, session))
    if data.get('wifi'):
        measure.wifi = dumps(process_wifi(data['wifi'], measure, session))
    session.add(measure)

def process_cell(cells, measure, session):
    result = []
    for entry in cells:
        cell = CellMeasure()
        cell.lat = measure.lat
        cell.lon = measure.lon
        cell.time = measure.time
        cell.accuracy = measure.accuracy
        cell.altitude = measure.altitude
        cell.altitude_accuracy = measure.altitude_accuracy
        cell.radio = measure.radio
        cell.mcc = entry['mcc']
        cell.mnc = entry['mnc']
        cell.lac = entry['lac']
        cell.cid = entry['cid']
        cell.psc = entry['psc']
        cell.asu = entry['asu']
        cell.signal = entry['signal']
        cell.ta = entry['ta']
        session.add(cell)
        result.append(entry)
    return result

def process_wifi(wifis, measure, session):
    result = []
    for entry in wifis:
        # convert frequency into channel numbers and remove frequency
        freq = entry.pop('frequency', 0)
        # if no explicit channel was given, calculate
        if freq and not entry['channel']:
            if 2411 < freq < 2473:
                # 2.4 GHz band
                entry['channel'] = (freq - 2407) // 5
            elif 5169 < freq < 5826:
                # 5 GHz band
                entry['channel'] = (freq - 5000) // 5
        wifi = WifiMeasure()
        wifi.lat = measure.lat
        wifi.lon = measure.lon
        wifi.time = measure.time
        wifi.accuracy = measure.accuracy
        wifi.altitude = measure.altitude
        wifi.altitude_accuracy = measure.altitude_accuracy
        wifi.key = entry['key']
        wifi.channel = entry['channel']
        wifi.signal = entry['signal']
        session.add(wifi)
        result.append(entry)
    return result

def submit_request(request):
    session = request.database.session()

    token = request.headers.get('X-Token', '')
    nickname = request.headers.get('X-Nickname', '')
    userid, token, nickname = process_user(token, nickname, session)

    utcnow = datetime.datetime.utcnow().replace(tzinfo=iso8601.UTC)
    utcmin = utcnow - datetime.timedelta(60)

    points = 0
    for measure in request.validated['items']:
        measure = process_time(measure, utcnow, utcmin)
        process_measure(measure, session)
        points += 1

    if userid is not None:
        process_score(userid, points, session)

    session.commit()
<|endoftext|>


<commit_before>from __future__ import print_function

from metakernel import MetaKernel, ProcessMetaKernel, REPLWrapper, u
from IPython.display import Image, SVG
from subprocess import check_output
import os
import tempfile
from shutil import rmtree

__version__ = '0.10.0'

class OctaveKernel(ProcessMetaKernel):
    implementation = 'Octave Kernel'
    implementation_version = __version__,
    language = 'octave'
    language_version = '0.1',
    banner = "Matlab Kernel"
    language_info = {
        'mimetype': 'text/x-octave',
        'name': 'octave_kernel',
        'file_extension': '.m',
        'help_links': MetaKernel.help_links,
    }

    _setup = """
    more off;
    set(0, 'defaultfigurepaperunits', 'inches');
    set(0, 'defaultfigureunits', 'inches');
    """

    _first = True

    _banner = None

    @property
    def banner(self):
        if self._banner is None:
            banner = check_output(['octave', '--version'])
            self._banner = banner.decode('utf-8')
        return self._banner

    def makeWrapper(self):
        """Start a bash shell and return a :class:`REPLWrapper` object.
        Note that this is equivalent :function:`metakernel.pyexpect.bash`,
        but is used here as an example of how to be cross-platform.
        """
        if os.name == 'nt':
            orig_prompt = u(chr(3))
            prompt_cmd = u('disp(char(3))')
            change_prompt = None
        else:
            orig_prompt = u('octave.*>')
            prompt_cmd = None
            change_prompt = u("PS1('{0}'); PS2('{1}')")

        self._first = True

        return REPLWrapper('octave', orig_prompt, change_prompt,
                           prompt_emit_cmd=prompt_cmd)

    def do_execute_direct(self, code):
        if self._first:
            self._first = False
            self.handle_plot_settings()
            super(OctaveKernel, self).do_execute_direct(self._setup)

        resp = super(OctaveKernel, self).do_execute_direct(code)

        if self.plot_settings.get('backend', None) == 'inline':
            plot_dir = tempfile.mkdtemp()
            self._make_figs(plot_dir)
            for fname in os.listdir(plot_dir):
                filename = os.path.join(plot_dir, fname)
                try:
                    if fname.lower().endswith('.svg'):
                        im = SVG(filename)
                    else:
                        im = Image(filename)
                    self.Display(im)
                except Exception as e:
                    self.Error(e)
            rmtree(plot_dir)

        return resp

    def get_kernel_help_on(self, info, level=0, none_on_fail=False):
        obj = info.get('help_obj', '')
        if not obj or len(obj.split()) > 1:
            if none_on_fail:
                return None
            else:
                return ""
        resp = self.do_execute_direct('help %s' % obj)
        return resp

    def get_completions(self, info):
        """
        Get completions from kernel based on info dict.
        """
        cmd = 'completion_matches("%s")' % info['obj']
        resp = self.do_execute_direct(cmd)
        return resp.splitlines()

    def handle_plot_settings(self):
        """Handle the current plot settings"""
        settings = self.plot_settings
        if settings.get('format', None) is None:
            settings.clear()
        settings.setdefault('backend', 'inline')
        settings.setdefault('format', 'png')
        settings.setdefault('size', '560,420')

        cmds = []

        if settings['backend'] == 'inline':
            cmds.append("set(0, 'defaultfigurevisible', 'off');")
            cmds.append("graphics_toolkit('gnuplot');")
        else:
            cmds.append("set(0, 'defaultfigurevisible', 'on');")
            cmds.append("graphics_toolkit('%s');" % settings['backend'])

        width, height = 560, 420
        if isinstance(settings['size'], tuple):
            width, height = settings['size']
        elif settings['size']:
            try:
                width, height = settings['size'].split(',')
                width, height = int(width), int(height)
            except Exception as e:
                self.Error('Error setting plot settings: %s' % e)

        size = "set(0, 'defaultfigurepaperposition', [0 0 %s %s]);"
        cmds.append(size % (width / 150., height / 150.))

        self.do_execute_direct('\n'.join(cmds))

    def _make_figs(self, plot_dir):
            cmd = """
            figHandles = get(0, 'children');
            for fig=1:length(figHandles);
                h = figHandles(fig);
                filename = fullfile('%s', ['OctaveFig', sprintf('%%03d', fig)]);
                saveas(h, [filename, '.png']);
                close(h);
            end;
            """ % plot_dir
            super(OctaveKernel, self).do_execute_direct(cmd.replace('\n', ''))

if __name__ == '__main__':
    from IPython.kernel.zmq.kernelapp import IPKernelApp
    IPKernelApp.launch_instance(kernel_class=OctaveKernel)
<commit_msg>Use svg by default on darwin<commit_after>from __future__ import print_function

from metakernel import MetaKernel, ProcessMetaKernel, REPLWrapper, u
from IPython.display import Image, SVG
from subprocess import check_output
import os
import sys
import tempfile
from shutil import rmtree

__version__ = '0.10.0'

class OctaveKernel(ProcessMetaKernel):
    implementation = 'Octave Kernel'
    implementation_version = __version__,
    language = 'octave'
    language_version = '0.1',
    banner = "Matlab Kernel"
    language_info = {
        'mimetype': 'text/x-octave',
        'name': 'octave_kernel',
        'file_extension': '.m',
        'help_links': MetaKernel.help_links,
    }

    _setup = """
    more off;
    set(0, 'defaultfigurepaperunits', 'inches');
    set(0, 'defaultfigureunits', 'inches');
    """

    _first = True

    _banner = None

    @property
    def banner(self):
        if self._banner is None:
            banner = check_output(['octave', '--version'])
            self._banner = banner.decode('utf-8')
        return self._banner

    def makeWrapper(self):
        """Start a bash shell and return a :class:`REPLWrapper` object.
        Note that this is equivalent :function:`metakernel.pyexpect.bash`,
        but is used here as an example of how to be cross-platform.
        """
        if os.name == 'nt':
            orig_prompt = u(chr(3))
            prompt_cmd = u('disp(char(3))')
            change_prompt = None
        else:
            orig_prompt = u('octave.*>')
            prompt_cmd = None
            change_prompt = u("PS1('{0}'); PS2('{1}')")

        self._first = True

        return REPLWrapper('octave', orig_prompt, change_prompt,
                           prompt_emit_cmd=prompt_cmd)

    def do_execute_direct(self, code):
        if self._first:
            self._first = False
            if sys.platform == 'darwin':
                self.plot_settings['format'] = 'svg'
            self.handle_plot_settings()
            super(OctaveKernel, self).do_execute_direct(self._setup)

        resp = super(OctaveKernel, self).do_execute_direct(code)

        if self.plot_settings.get('backend', None) == 'inline':
            plot_dir = tempfile.mkdtemp()
            self._make_figs(plot_dir)
            for fname in os.listdir(plot_dir):
                filename = os.path.join(plot_dir, fname)
                try:
                    if fname.lower().endswith('.svg'):
                        im = SVG(filename)
                    else:
                        im = Image(filename)
                    self.Display(im)
                except Exception as e:
                    self.Error(e)
            #rmtree(plot_dir)

        return resp

    def get_kernel_help_on(self, info, level=0, none_on_fail=False):
        obj = info.get('help_obj', '')
        if not obj or len(obj.split()) > 1:
            if none_on_fail:
                return None
            else:
                return ""
        resp = self.do_execute_direct('help %s' % obj)
        return resp

    def get_completions(self, info):
        """
        Get completions from kernel based on info dict.
        """
        cmd = 'completion_matches("%s")' % info['obj']
        resp = self.do_execute_direct(cmd)
        return resp.splitlines()

    def handle_plot_settings(self):
        """Handle the current plot settings"""
        settings = self.plot_settings
        if settings.get('format', None) is None:
            settings.clear()
        settings.setdefault('backend', 'inline')
        settings.setdefault('format', 'png')
        settings.setdefault('size', '560,420')

        cmds = []

        self._plot_fmt = settings['format']

        if settings['backend'] == 'inline':
            cmds.append("set(0, 'defaultfigurevisible', 'off');")
            cmds.append("graphics_toolkit('gnuplot');")
        else:
            cmds.append("set(0, 'defaultfigurevisible', 'on');")
            cmds.append("graphics_toolkit('%s');" % settings['backend'])

        width, height = 560, 420
        if isinstance(settings['size'], tuple):
            width, height = settings['size']
        elif settings['size']:
            try:
                width, height = settings['size'].split(',')
                width, height = int(width), int(height)
            except Exception as e:
                self.Error('Error setting plot settings: %s' % e)

        size = "set(0, 'defaultfigurepaperposition', [0 0 %s %s]);"
        cmds.append(size % (width / 150., height / 150.))

        self.do_execute_direct('\n'.join(cmds))

    def _make_figs(self, plot_dir):
            cmd = """
            figHandles = get(0, 'children');
            for fig=1:length(figHandles);
                h = figHandles(fig);
                filename = fullfile('%s', ['OctaveFig', sprintf('%%03d', fig)]);
                saveas(h, [filename, '.%s']);
                close(h);
            end;
            """ % (plot_dir, self._plot_fmt)
            super(OctaveKernel, self).do_execute_direct(cmd.replace('\n', ''))

if __name__ == '__main__':
    from IPython.kernel.zmq.kernelapp import IPKernelApp
    IPKernelApp.launch_instance(kernel_class=OctaveKernel)
<|endoftext|>


<commit_before>"""Open Drug Discovery Toolkit
==============================
Universal and easy to use resource for various drug discovery tasks, ie docking, virutal screening, rescoring.

    Attributes
    ----------
    toolkit : module,
        Toolkits backend module, currenlty OpenBabel [ob] and RDKit [rdk].
        This setting is toolkit-wide, and sets given toolkit as default
"""

from numpy.random import seed as np_seed
from random import seed as python_seed
from .toolkits import ob, rdk

__version__ = '0.1.3'
toolkit = ob
__all__ = ['toolkit']

def random_seed(i):
    """
    Set global random seed for all underlying components. Use 'brute-force' approach, by setting undelying libraries' seeds.

    Parameters
    ----------
        i: int
            integer used as seed for random number generators
    """
    # python's random module
    python_seed(i)
    # numpy random module
    np_seed(i)
<commit_msg>Failback to another toolkit, except if none available<commit_after>"""Open Drug Discovery Toolkit
==============================
Universal and easy to use resource for various drug discovery tasks, ie docking, virutal screening, rescoring.

    Attributes
    ----------
    toolkit : module,
        Toolkits backend module, currenlty OpenBabel [ob] and RDKit [rdk].
        This setting is toolkit-wide, and sets given toolkit as default
"""

from numpy.random import seed as np_seed
from random import seed as python_seed
try:
    from .toolkits import ob
except ImportError:
    ob = None
try:
    from .toolkits import rdk
except ImportError:
    rdk = None
if ob:
    toolkit = ob
elif rdk:
    toolkit = rdk
else:
    raise Exception('You need at least one toolkit for ODDT.')

__version__ = '0.1.3'
__all__ = ['toolkit']

def random_seed(i):
    """
    Set global random seed for all underlying components. Use 'brute-force' approach, by setting undelying libraries' seeds.

    Parameters
    ----------
        i: int
            integer used as seed for random number generators
    """
    # python's random module
    python_seed(i)
    # numpy random module
    np_seed(i)
<|endoftext|>


<commit_before># -*- coding: UTF-8 -*-
"""
:mod:`Solver` -- ODE Solvers
============================

A collection of solvers for ODEs of various types.

.. module :: solver
.. moduleauthor :: Olivier Verdier <olivier.verdier@gmail.com>

The :class:`Scheme` class contains methods on how to perform one iteration step.
It is its responsibility to take care of the time step.

The higher level class is :class:`Solver`, which is initialized with an instance of a :class:`Scheme` class.
"""
from __future__ import division

import scipy.io # used for saving in shelves
import numpy as np
import pylab as PL

import itertools

class Solver (object):
	"""
	General Solver class, that takes care of calling the step function and storing the intermediate results.

	:Parameters:
		system : :class:`odelab.system.System`
			Object describing the system. The requirement on that class may vary. See the documentation of the various solver subclasses. The system may also be specified later, although before any simulation of course.
	"""

	def __init__(self, system=None):
		self.system = system

	# default values for the total time
	time = 1.

	def initialize(self, u0=None, t0=0, h=None, time=None):
		"""
Initialize the solver to the initial condition :math:`u(t0) = u0`.

:param array u0: initial condition; if it is not provided, it is set to the previous initial condition.
:param scalar t0: initial time
:param scalar h: time step
:param scalar time: span of the simulation
		"""
		if u0 is not None: # initial condition provided
			if np.isscalar(u0):
				u0 = [u0]
			u0 = np.array(u0)
			raw_event0 = np.hstack([u0,t0])
			event0 = self.system.preprocess(raw_event0)
		else: # start from the previous initial conditions
			try:
				event0 = self.events[0]
			except AttributeError:
				raise self.NotInitialized("You must provide an initial condition.")
		self.events = [event0]
		if h is not None:
			self.h = h
		if time is not None:
			self.time = time

	def __len__(self):
		return len(self.events)

	def load_data(self, data):
		"""
Initialize the solver from previously saved data.
:param array data: event array, with the same format as :py:attr:`events_array`
		"""
		self.events = list(data.T)
		self.update_events_array()

	def update_events_array(self):
		self.events_array = np.array(self.events).T

	def generate(self, event):
		"""
		Generates the (t,u) values.
		"""
		u,t = event[:-1], event[-1]
		for i in itertools.count(): # infinite loop
			t, u = self.step(t, u)
			event = np.hstack([u,t])
			yield event
			self.increment_stepsize()

	max_iter = 100000
	class FinalTimeNotReached(Exception):
		"""
		Raised when the final time was not reached within the given ``max_iter`` number of iterations.
		"""

	class Unstable(Exception):
		"""
		Raised when the scheme produces NaN values.
		"""

	class NotInitialized(Exception):
		"""
		Raised when the solver is not properly initialized.
		"""

	class Runtime(Exception):
		"""
		Raised to relay an exception occurred while running the solver.
		"""

	def simulating(self):
		return self

	def __enter__(self):
		# start from the last time we stopped
		event = self.events[-1]
		generator = self.generate(event)
		return generator

	auto_save = False # whether to automatically save the session after a run; especially useful for tests

	def __exit__(self, ex_type, ex_value, traceback):
		self.update_events_array()
		if self.auto_save:
			self.save()

	catch_runtime = True # whether to catch runtime exception (not catching allows to see the traceback)

	t_tol = 1e-12 # tolerance to tell whether the final time is reached

	def run(self, time=None):
		"""
		Run the simulation for a given time.

:param scalar time: the time span for which to run; if none is given, the default ``self.time`` is used
		"""
		if time is None:
			time = self.time
		try:
			t0 = self.events[-1][-1]
		except AttributeError:
			raise self.NotInitialized("You must call the `initialize` method before you can run the solver.")
		tf = t0 + time # final time
		with self as generator:
			for i in xrange(self.max_iter):
				try:
					event = next(generator)
				except Exception as e:
					if self.catch_runtime:
						raise self.Runtime('%s raised after %d steps: %s' % (type(e).__name__,i,e.args), e, i)
					else:
						raise
				else:
					if np.any(np.isnan(event)):
						raise self.Unstable('Unstable after %d steps.' % i)

					self.events.append(event)
					if event[-1] > tf - self.t_tol:
						break
			else:
				raise self.FinalTimeNotReached("Reached maximal number of iterations: {0}".format(self.max_iter))

	def guess_name(self):
		"""
		Guess a name for this session.
		"""
		guess = "{system}_{scheme}_T{time}_N{nsteps}".format(system=type(self.system).__name__, scheme=type(self.scheme).__name__, time=self.time, nsteps=len(self.events))
		sanitized = guess.replace('.','_')
		return sanitized

	shelf_name = 'bank' # default shelf name

	def save(self, name=None):
		"""
		Save the current results in a scipy shelf.
		"""
		shelf_name = name or self.guess_name()
		print shelf_name
		scipy.io.save_as_module(self.shelf_name, {shelf_name: self})

	def get_u(self, index, process=True):
		"""
		Return u[index] after post-processing.
		"""
		event = self.events[index]
		if process:
			event = self.system.postprocess(event)
		return event

	def get_times(self):
		return self.events[-1]

	def final_time(self):
		return self.get_times()[-1]

	def initial(self, process=True):
		"""
		Convenience method to obtain the initial condition.
		"""
		return self.get_u(0, process)

	def final(self, process=True):
		"""
		Convenience method to obtain the last computed value.
		"""
		return self.get_u(-1, process)

	max_plot_res = 500 # max plot resolution

	def plot(self, components=None, plot_exact=True, error=False, save=None, time_component=None, **plot_args):
		"""
		Plot some components of the solution.

:param list components: either a given component of the solution, or a list of components to plot, or a list of strings corresponding to methods of the system.
:param boolean plot_exact: whether to plot the exact solution (if available)
:param string save: whether to save the plot in a file
:param boolean error: to plot (the log10 of) the error instead of the value itself
:param int time_component: component to use as x variable (if None, then time is used)
		"""
		# some sampling
		size = len(self.events)
		stride = np.ceil(size/self.max_plot_res)
		events = self.events_array[:,::stride]
		ats = events[-1]

		# components
		if components is None: # plot all the components by default
			components = range(self.events[0].size - 1)
		if not np.iterable(components): # makes the code work when plotting a single component
			components = [components]
		if time_component is not None:
			components.insert(0,time_component)

		if plot_exact or error:
			has_exact = hasattr(self.system, 'exact')
			if has_exact:
				exact = self.system.exact(ats, self.initial())
		compute_exact = (plot_exact or error) and has_exact
		axis = PL.gca()
		if save: # if this is meant to be saved, clear the previous plot first
			axis.cla()
		previous_line = len(axis.lines)

		time_label = 'time'

		for component_i, component in enumerate(components):
			if isinstance(component, str):
				label = component
				data = self.system.__getattribute__(component)(events)
				if compute_exact:
					exact_comp = self.system.__getattribute__(component)(np.vstack([exact, ats]))
			else:
				label = self.system.label(component)
				data = events[component]
				if compute_exact:
					exact_comp = exact[component]

			# set up plot arguments
			defaults = {'ls':'-', 'marker':','}
			defaults.update(plot_args)

			if error and has_exact:
				data = np.log10(np.abs(data - exact_comp))
			if  time_component is not None and not component_i:
				# at the first step, if time_component is not time, then replace the time vector by the desired component
				ats = data
				time_label = label
				continue
			else:
				axis.plot(ats, data, ',-', label=label, **defaults)
			if compute_exact and not error:
				current_color = axis.lines[-1].get_color() # figure out current colour
				axis.plot(ats, exact_comp, ls='-', lw=2, label='%s*' % label, color=current_color)
		axis.set_xlabel(time_label)
		axis.legend()
		if save:
			PL.savefig(save, format='pdf', **plot_args)
		else:
			PL.plot() # plot only in interactive mode
		return axis

	def plot_function(self, function, *args, **kwargs):
		"""
		Plot a given function of the state. May be useful to plot constraints or energy.

		This is now a convenience function that calls `odelab.solver.plot`.

:param string function: name of the method to call on the current system object

		:Example:
			the code ``solver.plot_function('energy')`` will call the method ``solver.system.energy`` on the current stored solution points.
		"""
		return self.plot(*args, components=[function], **kwargs)

	def plot2D(self, time_component=0, other_component=1, *args, **kwargs):
		"""
		Plot components vs another one
		"""
		self.plot(other_component, time_component=time_component, *args, **kwargs)

	quiver_res = 20
	def quiver(self):
		mins = self.aus.min(axis=0)
		maxs = self.aus.max(axis=0)
		X,Y = np.meshgrid(linspace(mins[0], maxs[0], self.quiver_res),
								linspace(mins[1], maxs[1], self.quiver_res))
		Z = np.dstack([X,Y])
		vals = self.f(0,Z.transpose(2,0,1))
		PL.quiver(X,Y,vals[0], vals[1])

class SingleStepSolver(Solver):
	def __init__(self, scheme, system=None):
		super(SingleStepSolver, self).__init__(system)
		self.scheme = scheme

	def __repr__(self):
		return '<%s: %s>' % ('Solver', str(self.scheme))

	def set_scheme(self, scheme):
		self.current_scheme = scheme
		self.current_scheme.solver = self
		self.current_scheme.initialize()

	def step_current(self, t,u):
		return self.current_scheme.step(t,u)

	def step(self, t,u):
		stage = len(self)
		if stage < self.scheme.tail_length: # not enough past values to run main scheme
			if stage == 1:
				self.set_scheme(self.single_step_scheme)
		if stage == self.scheme.tail_length: # main scheme kicks in
			self.set_scheme(self.scheme)
		return self.step_current(t,u)

	def increment_stepsize(self):
		self.current_scheme.increment_stepsize()

class MultiStepSolver(SingleStepSolver):
## 	default_single_step_scheme = HochOst4()

	def __init__(self, scheme, system, single_step_scheme=None):
		super(MultiStepSolver, self).__init__(scheme, system)
		if single_step_scheme is None:
			from odelab.scheme.exponential import HochOst4
			single_step_scheme = HochOst4() # hard coded for the moment
		self.single_step_scheme = single_step_scheme
		self.single_step_scheme.solver = self

<commit_msg>better use of getattr in Solver.plot<commit_after># -*- coding: UTF-8 -*-
"""
:mod:`Solver` -- ODE Solvers
============================

A collection of solvers for ODEs of various types.

.. module :: solver
.. moduleauthor :: Olivier Verdier <olivier.verdier@gmail.com>

The :class:`Scheme` class contains methods on how to perform one iteration step.
It is its responsibility to take care of the time step.

The higher level class is :class:`Solver`, which is initialized with an instance of a :class:`Scheme` class.
"""
from __future__ import division

import scipy.io # used for saving in shelves
import numpy as np
import pylab as PL

import itertools

class Solver (object):
	"""
	General Solver class, that takes care of calling the step function and storing the intermediate results.

	:Parameters:
		system : :class:`odelab.system.System`
			Object describing the system. The requirement on that class may vary. See the documentation of the various solver subclasses. The system may also be specified later, although before any simulation of course.
	"""

	def __init__(self, system=None):
		self.system = system

	# default values for the total time
	time = 1.

	def initialize(self, u0=None, t0=0, h=None, time=None):
		"""
Initialize the solver to the initial condition :math:`u(t0) = u0`.

:param array u0: initial condition; if it is not provided, it is set to the previous initial condition.
:param scalar t0: initial time
:param scalar h: time step
:param scalar time: span of the simulation
		"""
		if u0 is not None: # initial condition provided
			if np.isscalar(u0):
				u0 = [u0]
			u0 = np.array(u0)
			raw_event0 = np.hstack([u0,t0])
			event0 = self.system.preprocess(raw_event0)
		else: # start from the previous initial conditions
			try:
				event0 = self.events[0]
			except AttributeError:
				raise self.NotInitialized("You must provide an initial condition.")
		self.events = [event0]
		if h is not None:
			self.h = h
		if time is not None:
			self.time = time

	def __len__(self):
		return len(self.events)

	def load_data(self, data):
		"""
Initialize the solver from previously saved data.
:param array data: event array, with the same format as :py:attr:`events_array`
		"""
		self.events = list(data.T)
		self.update_events_array()

	def update_events_array(self):
		self.events_array = np.array(self.events).T

	def generate(self, event):
		"""
		Generates the (t,u) values.
		"""
		u,t = event[:-1], event[-1]
		for i in itertools.count(): # infinite loop
			t, u = self.step(t, u)
			event = np.hstack([u,t])
			yield event
			self.increment_stepsize()

	max_iter = 100000
	class FinalTimeNotReached(Exception):
		"""
		Raised when the final time was not reached within the given ``max_iter`` number of iterations.
		"""

	class Unstable(Exception):
		"""
		Raised when the scheme produces NaN values.
		"""

	class NotInitialized(Exception):
		"""
		Raised when the solver is not properly initialized.
		"""

	class Runtime(Exception):
		"""
		Raised to relay an exception occurred while running the solver.
		"""

	def simulating(self):
		return self

	def __enter__(self):
		# start from the last time we stopped
		event = self.events[-1]
		generator = self.generate(event)
		return generator

	auto_save = False # whether to automatically save the session after a run; especially useful for tests

	def __exit__(self, ex_type, ex_value, traceback):
		self.update_events_array()
		if self.auto_save:
			self.save()

	catch_runtime = True # whether to catch runtime exception (not catching allows to see the traceback)

	t_tol = 1e-12 # tolerance to tell whether the final time is reached

	def run(self, time=None):
		"""
		Run the simulation for a given time.

:param scalar time: the time span for which to run; if none is given, the default ``self.time`` is used
		"""
		if time is None:
			time = self.time
		try:
			t0 = self.events[-1][-1]
		except AttributeError:
			raise self.NotInitialized("You must call the `initialize` method before you can run the solver.")
		tf = t0 + time # final time
		with self as generator:
			for i in xrange(self.max_iter):
				try:
					event = next(generator)
				except Exception as e:
					if self.catch_runtime:
						raise self.Runtime('%s raised after %d steps: %s' % (type(e).__name__,i,e.args), e, i)
					else:
						raise
				else:
					if np.any(np.isnan(event)):
						raise self.Unstable('Unstable after %d steps.' % i)

					self.events.append(event)
					if event[-1] > tf - self.t_tol:
						break
			else:
				raise self.FinalTimeNotReached("Reached maximal number of iterations: {0}".format(self.max_iter))

	def guess_name(self):
		"""
		Guess a name for this session.
		"""
		guess = "{system}_{scheme}_T{time}_N{nsteps}".format(system=type(self.system).__name__, scheme=type(self.scheme).__name__, time=self.time, nsteps=len(self.events))
		sanitized = guess.replace('.','_')
		return sanitized

	shelf_name = 'bank' # default shelf name

	def save(self, name=None):
		"""
		Save the current results in a scipy shelf.
		"""
		shelf_name = name or self.guess_name()
		print shelf_name
		scipy.io.save_as_module(self.shelf_name, {shelf_name: self})

	def get_u(self, index, process=True):
		"""
		Return u[index] after post-processing.
		"""
		event = self.events[index]
		if process:
			event = self.system.postprocess(event)
		return event

	def get_times(self):
		return self.events[-1]

	def final_time(self):
		return self.get_times()[-1]

	def initial(self, process=True):
		"""
		Convenience method to obtain the initial condition.
		"""
		return self.get_u(0, process)

	def final(self, process=True):
		"""
		Convenience method to obtain the last computed value.
		"""
		return self.get_u(-1, process)

	max_plot_res = 500 # max plot resolution

	def plot(self, components=None, plot_exact=True, error=False, save=None, time_component=None, **plot_args):
		"""
		Plot some components of the solution.

:param list components: either a given component of the solution, or a list of components to plot, or a list of strings corresponding to methods of the system.
:param boolean plot_exact: whether to plot the exact solution (if available)
:param string save: whether to save the plot in a file
:param boolean error: to plot (the log10 of) the error instead of the value itself
:param int time_component: component to use as x variable (if None, then time is used)
		"""
		# some sampling
		size = len(self.events)
		stride = np.ceil(size/self.max_plot_res)
		events = self.events_array[:,::stride]
		ats = events[-1]

		# components
		if components is None: # plot all the components by default
			components = range(self.events[0].size - 1)
		if not np.iterable(components): # makes the code work when plotting a single component
			components = [components]
		if time_component is not None:
			components.insert(0,time_component)

		if plot_exact or error:
			sys_exact = getattr(self.system, 'exact', None)
			if sys_exact:
				exact = sys_exact(ats, self.initial())
		compute_exact = (plot_exact or error) and sys_exact
		axis = PL.gca()
		if save: # if this is meant to be saved, clear the previous plot first
			axis.cla()
		previous_line = len(axis.lines)

		time_label = 'time'

		for component_i, component in enumerate(components):
			if isinstance(component, str):
				label = component
				function = getattr(self.system, component)
				data = function(events)
				if compute_exact:
					exact_comp = function(np.vstack([exact, ats]))
			else:
				label = self.system.label(component)
				data = events[component]
				if compute_exact:
					exact_comp = exact[component]

			# set up plot arguments
			defaults = {'ls':'-', 'marker':','}
			defaults.update(plot_args)

			if error and sys_exact:
				data = np.log10(np.abs(data - exact_comp))
			if  time_component is not None and not component_i:
				# at the first step, if time_component is not time, then replace the time vector by the desired component
				ats = data
				time_label = label
				continue
			else:
				axis.plot(ats, data, ',-', label=label, **defaults)
			if compute_exact and not error:
				current_color = axis.lines[-1].get_color() # figure out current colour
				axis.plot(ats, exact_comp, ls='-', lw=2, label='%s*' % label, color=current_color)
		axis.set_xlabel(time_label)
		axis.legend()
		if save:
			PL.savefig(save, format='pdf', **plot_args)
		else:
			PL.plot() # plot only in interactive mode
		return axis

	def plot_function(self, function, *args, **kwargs):
		"""
		Plot a given function of the state. May be useful to plot constraints or energy.

		This is now a convenience function that calls `odelab.solver.plot`.

:param string function: name of the method to call on the current system object

		:Example:
			the code ``solver.plot_function('energy')`` will call the method ``solver.system.energy`` on the current stored solution points.
		"""
		return self.plot(*args, components=[function], **kwargs)

	def plot2D(self, time_component=0, other_component=1, *args, **kwargs):
		"""
		Plot components vs another one
		"""
		self.plot(other_component, time_component=time_component, *args, **kwargs)

	quiver_res = 20
	def quiver(self):
		mins = self.aus.min(axis=0)
		maxs = self.aus.max(axis=0)
		X,Y = np.meshgrid(linspace(mins[0], maxs[0], self.quiver_res),
								linspace(mins[1], maxs[1], self.quiver_res))
		Z = np.dstack([X,Y])
		vals = self.f(0,Z.transpose(2,0,1))
		PL.quiver(X,Y,vals[0], vals[1])

class SingleStepSolver(Solver):
	def __init__(self, scheme, system=None):
		super(SingleStepSolver, self).__init__(system)
		self.scheme = scheme

	def __repr__(self):
		return '<%s: %s>' % ('Solver', str(self.scheme))

	def set_scheme(self, scheme):
		self.current_scheme = scheme
		self.current_scheme.solver = self
		self.current_scheme.initialize()

	def step_current(self, t,u):
		return self.current_scheme.step(t,u)

	def step(self, t,u):
		stage = len(self)
		if stage < self.scheme.tail_length: # not enough past values to run main scheme
			if stage == 1:
				self.set_scheme(self.single_step_scheme)
		if stage == self.scheme.tail_length: # main scheme kicks in
			self.set_scheme(self.scheme)
		return self.step_current(t,u)

	def increment_stepsize(self):
		self.current_scheme.increment_stepsize()

class MultiStepSolver(SingleStepSolver):
## 	default_single_step_scheme = HochOst4()

	def __init__(self, scheme, system, single_step_scheme=None):
		super(MultiStepSolver, self).__init__(scheme, system)
		if single_step_scheme is None:
			from odelab.scheme.exponential import HochOst4
			single_step_scheme = HochOst4() # hard coded for the moment
		self.single_step_scheme = single_step_scheme
		self.single_step_scheme.solver = self

<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from odin.utils import cached_property, field_iter_items

__all__ = ('ResourceAdapter',)

class ResourceOptionsAdapter(object):
    """
    A lightweight wrapper for the *ResourceOptions* class that filters fields.
    """
    def __init__(self, options, include, exclude):
        self._wrapped = options
        self.parents = options.parents

        # Filter available fields
        include = include or [f.attname for f in options.fields]
        exclude = exclude or []
        self.fields = [f for f in options.fields if f.attname in include and f.attname not in exclude]
        self.virtual_fields = [f for f in options.virtual_fields if f.attname in include and f.attname not in exclude]

        # Work around so cached properties still work.
        self._cache = {}

    def __getattr__(self, item):
        return getattr(self._wrapped, item)

    def __repr__(self):
        return '<Options Adapter for %s>' % self.resource_name

    @cached_property
    def all_fields(self):
        """
        All fields both standard and virtual.
        """
        return self.fields + self.virtual_fields

    @cached_property
    def field_map(self):
        return {f.attname: f for f in self.fields}

    @property
    def attribute_fields(self):
        """
        List of fields where is_attribute is True.
        """
        return [f for f in self.fields if f.is_attribute]

    @property
    def element_fields(self):
        """
        List of fields where is_attribute is False.
        """
        return [f for f in self.fields if not f.is_attribute]

class ResourceAdapter(object):
    """
    A lightweight wrapper that can be placed around a resource to filter out specific fields or to provide additional
    specific methods or calculated properties.

    A good use case for an adapter is an API where you wish to filter out certain fields or in a UI where you wish to
    add methods for rendering objects without extending the underlying resource that may be shared between multiple
    rendering engines or other subsystems.

    The *ResourceAdapter* can be passed to Odin codecs just like a *Resource*.

    """
    @classmethod
    def apply_to(cls, sources, include=None, exclude=None, **kwargs):
        """
        Convenience method that applies include/exclude lists to all items in
        an iterable collection of resources.

        :param sources: Source resources being wrapped.
        :param include: Fields that should be explicitly included on the adapter.
        :param exclude: Fields to explicitly exclude on the adapter.

        """
        meta_objects = {}
        for resource in sources:
            try:
                meta = meta_objects[resource._meta.resource_name]
            except KeyError:
                meta = cls._create_options_adapter(resource._meta, include, exclude)
                meta_objects[resource._meta.resource_name] = meta
            yield cls(resource, meta=meta, **kwargs)

    @classmethod
    def _create_options_adapter(cls, options, include=None, exclude=None):
        include_fields = include if include else getattr(cls, 'include_fields', None)
        exclude_fields = exclude if exclude else getattr(cls, 'exclude_fields', None)
        return ResourceOptionsAdapter(options, include_fields, exclude_fields)

    def __init__(self, source, include=None, exclude=None, meta=None):
        """
        Initialise the adapter.

        :param source: Source resource being wrapped.
        :param include: Fields that should be explicitly included on the adapter.
        :param exclude: Fields to explicitly exclude on the adapter.

        """
        self.__dict__['_source'] = source

        if not meta:
            meta = self._create_options_adapter(source._meta, include, exclude)
        self._meta = meta

    def __getattr__(self, item):
        return getattr(self._source, item)

    def __setattr__(self, name, value):
        setattr(self._source, name, value)

    def __repr__(self):
        return '<%s: %s>' % (self.__class__.__name__, self)

    def __str__(self):
        return '%s resource adapter' % self._meta.resource_name

    def to_dict(self, include_virtual=True):
        """
        Convert this resource into a dict
        """
        fields = self._meta.all_fields if include_virtual else self._meta.fields
        return dict((f.name, v) for f, v in field_iter_items(self, fields))
<commit_msg>Added a simple currying solution that allows a adapter to have include/exclude/user defined kwargs predefined<commit_after># -*- coding: utf-8 -*-
from odin.utils import cached_property, field_iter_items

__all__ = ('ResourceAdapter',)

class CurriedAdapter(object):
    """
    Curry wrapper for a Adapter to allow for pre-config of include/exclude and
    any other user defined arguments provided in kwargs.
    """
    def __init__(self, cls, **kwargs):
        self.cls = cls
        self.kwargs = kwargs.copy()

    def __call__(self, source):
        return self.cls(source, **self.kwargs)

    def apply_to(self, sources):
        return self.cls.apply_to(sources, **self.kwargs)

class ResourceOptionsAdapter(object):
    """
    A lightweight wrapper for the *ResourceOptions* class that filters fields.
    """
    def __init__(self, options, include, exclude):
        self._wrapped = options
        self.parents = options.parents

        # Filter available fields
        include = include or [f.attname for f in options.fields]
        exclude = exclude or []
        self.fields = [f for f in options.fields if f.attname in include and f.attname not in exclude]
        self.virtual_fields = [f for f in options.virtual_fields if f.attname in include and f.attname not in exclude]

        # Work around so cached properties still work.
        self._cache = {}

    def __getattr__(self, item):
        return getattr(self._wrapped, item)

    def __repr__(self):
        return '<Options Adapter for %s>' % self.resource_name

    @cached_property
    def all_fields(self):
        """
        All fields both standard and virtual.
        """
        return self.fields + self.virtual_fields

    @cached_property
    def field_map(self):
        return {f.attname: f for f in self.fields}

    @property
    def attribute_fields(self):
        """
        List of fields where is_attribute is True.
        """
        return [f for f in self.fields if f.is_attribute]

    @property
    def element_fields(self):
        """
        List of fields where is_attribute is False.
        """
        return [f for f in self.fields if not f.is_attribute]

class ResourceAdapter(object):
    """
    A lightweight wrapper that can be placed around a resource to filter out specific
    fields or to provide additional specific methods or calculated properties.

    A good use case for an adapter is an API where you wish to filter out certain fields
    or in a UI where you wish to add methods for rendering objects without extending the
    underlying resource that may be shared between multiple rendering engines or other
    subsystems.

    The *ResourceAdapter* can be passed to Odin codecs just like a *Resource*.

    """
    @classmethod
    def apply_to(cls, sources, include=None, exclude=None, **kwargs):
        """
        Convenience method that applies include/exclude lists to all items in an
        iterable collection of resources.

        :param sources: Source resources being wrapped.
        :param include: Fields that should be explicitly included on the adapter.
        :param exclude: Fields to explicitly exclude on the adapter.

        """
        meta_objects = {}
        for resource in sources:
            try:
                meta = meta_objects[resource._meta.resource_name]
            except KeyError:
                meta = cls._create_options_adapter(resource._meta, include, exclude)
                meta_objects[resource._meta.resource_name] = meta
            yield cls(resource, meta=meta, **kwargs)

    @classmethod
    def _create_options_adapter(cls, options, include=None, exclude=None):
        include_fields = include if include else getattr(cls, 'include_fields', None)
        exclude_fields = exclude if exclude else getattr(cls, 'exclude_fields', None)
        return ResourceOptionsAdapter(options, include_fields, exclude_fields)

    @classmethod
    def curry(cls, include=None, exclude=None, **kwargs):
        """
        Creates an Adapter that has include and exclude (and any other options) preset.

        :param include: Fields that should be explicitly included on the adapter.
        :param exclude: Fields to explicitly exclude on the adapter.
        :param kwargs:

        """
        return CurriedAdapter(cls, include=include, exclude=exclude, **kwargs)

    def __init__(self, source, include=None, exclude=None, meta=None):
        """
        Initialise the adapter.

        :param source: Source resource being wrapped.
        :param include: Fields that should be explicitly included on the adapter.
        :param exclude: Fields to explicitly exclude on the adapter.

        """
        self.__dict__['_source'] = source

        if not meta:
            meta = self._create_options_adapter(source._meta, include, exclude)
        self._meta = meta

    def __getattr__(self, item):
        return getattr(self._source, item)

    def __setattr__(self, name, value):
        setattr(self._source, name, value)

    def __repr__(self):
        return '<%s: %s>' % (self.__class__.__name__, self)

    def __str__(self):
        return '%s resource adapter' % self._meta.resource_name

    def to_dict(self, include_virtual=True):
        """
        Convert this resource into a dict
        """
        fields = self._meta.all_fields if include_virtual else self._meta.fields
        return dict((f.name, v) for f, v in field_iter_items(self, fields))
<|endoftext|>


<commit_before>import datetime
from django.db import models
from django.utils import simplejson
from django.utils.timezone import now
from django.utils.translation import ugettext_lazy as _

JSON = 1
XML = 2
FORMAT_CHOICES = (
    (JSON, "JSON"),
    (XML, "XML"),
)

class ProviderRule(models.Model):
    name = models.CharField(_("name"), max_length=128, null=True, blank=True)
    regex = models.CharField(_("regex"), max_length=2000)
    endpoint = models.CharField(_("endpoint"), max_length=2000)
    format = models.IntegerField(_("format"), choices=FORMAT_CHOICES)

    def __unicode__(self):
        return self.name or self.endpoint

class StoredOEmbed(models.Model):
    match = models.TextField(_("match"))
    max_width = models.IntegerField(_("max width"))
    max_height = models.IntegerField(_("max height"))
    html = models.TextField(_("html"))
    json = models.TextField(_("json"))
    date_added = models.DateTimeField(
        _("date added"), default=now)

    class Meta:
        ordering = ('-max_width',) # larger ones take precedence

    def __unicode__(self):
        return self.match

    def get_json(self, name):
        """ Convenience for JSON properties; e.g. get_json('thumbnail_url') """
        return simplejson.loads(self.json).get(name, None)
<commit_msg>Added verbose names to fix admin formatting<commit_after>import datetime
from django.db import models
from django.utils import simplejson
from django.utils.timezone import now
from django.utils.translation import ugettext_lazy as _

JSON = 1
XML = 2
FORMAT_CHOICES = (
    (JSON, "JSON"),
    (XML, "XML"),
)

class ProviderRule(models.Model):
    name = models.CharField(_("name"), max_length=128, null=True, blank=True)
    regex = models.CharField(_("regex"), max_length=2000)
    endpoint = models.CharField(_("endpoint"), max_length=2000)
    format = models.IntegerField(_("format"), choices=FORMAT_CHOICES)

    def __unicode__(self):
        return self.name or self.endpoint

class StoredOEmbed(models.Model):
    match = models.TextField(_("match"))
    max_width = models.IntegerField(_("max width"))
    max_height = models.IntegerField(_("max height"))
    html = models.TextField(_("html"))
    json = models.TextField(_("json"))
    date_added = models.DateTimeField(
        _("date added"), default=now)

    class Meta:
        ordering = ('-max_width',) # larger ones take precedence
        verbose_name = u'Stored OEmbed'
        verbose_name_plural = u'Stored OEmbeds'

    def __unicode__(self):
        return self.match

    def get_json(self, name):
        """ Convenience for JSON properties; e.g. get_json('thumbnail_url') """
        return simplejson.loads(self.json).get(name, None)
<|endoftext|>