<commit_before># Copyright (c) 2009-2021 The Regents of the University of Michigan
# This file is part of the HOOMD-blue project, released under the BSD 3-Clause
# License.

"""Implement MD Integrator."""

import itertools

from hoomd.md import _md
from hoomd.data.parameterdicts import ParameterDict
from hoomd.data.typeconverter import OnlyFrom, OnlyTypes
from hoomd.integrate import BaseIntegrator
from hoomd.data import syncedlist
from hoomd.md.methods import Method
from hoomd.md.force import Force
from hoomd.md.constrain import Constraint, Rigid

def _preprocess_aniso(value):
    if value is True:
        return "true"
    elif value is False:
        return "false"
    else:
        return value

def _set_synced_list(old_list, new_list):
    old_list.clear()
    old_list.extend(new_list)

class _DynamicIntegrator(BaseIntegrator):

    def __init__(self, forces, constraints, methods, rigid):
        forces = [] if forces is None else forces
        constraints = [] if constraints is None else constraints
        methods = [] if methods is None else methods
        self._forces = syncedlist.SyncedList(
            Force, syncedlist._PartialGetAttr('_cpp_obj'), iterable=forces)

        self._constraints = syncedlist.SyncedList(
            OnlyTypes(Constraint, disallow_types=(Rigid,)),
            syncedlist._PartialGetAttr('_cpp_obj'),
            iterable=constraints)

        self._methods = syncedlist.SyncedList(
            Method, syncedlist._PartialGetAttr('_cpp_obj'), iterable=methods)

        param_dict = ParameterDict(rigid=OnlyTypes(Rigid, allow_none=True))
        if rigid is not None and rigid._added:
            raise ValueError("Rigid object can only belong to one integrator.")
        param_dict["rigid"] = rigid
        self._param_dict.update(param_dict)

    def _attach(self):
        self.forces._sync(self._simulation, self._cpp_obj.forces)
        self.constraints._sync(self._simulation, self._cpp_obj.constraints)
        self.methods._sync(self._simulation, self._cpp_obj.methods)
        super()._attach()
        if self.rigid is not None:
            self.rigid._attach()
            self._cpp_obj.rigid = self.rigid._cpp_obj

    def _add(self, simulation):
        super()._add(simulation)
        if self.rigid is not None:
            self.rigid._add(simulation)

    @property
    def forces(self):
        return self._forces

    @forces.setter
    def forces(self, value):
        _set_synced_list(self._forces, value)

    @property
    def constraints(self):
        return self._constraints

    @constraints.setter
    def constraints(self, value):
        _set_synced_list(self._constraints, value)

    @property
    def methods(self):
        return self._methods

    @methods.setter
    def methods(self, value):
        _set_synced_list(self._methods, value)

    @property
    def _children(self):
        children = list(self.forces)
        children.extend(self.constraints)
        children.extend(self.methods)

        for child in itertools.chain(self.forces, self.constraints,
                                     self.methods):
            children.extend(child._children)

        return children

    def _getattr_param(self, attr):
        if attr == "rigid":
            return self._param_dict["rigid"]
        return super()._getattr_param(attr)

    def _setattr_param(self, attr, value):
        if attr == "rigid":
            self._set_rigid(value)
            return
        super()._setattr_param(attr, value)

    def _set_rigid(self, value):
        """Handles the adding and detaching of potential Rigid objects."""
        # this generally only happens when attaching and we can ignore it since
        # we attach the rigid body in _attach.
        if value is self.rigid:
            return

        old_rigid = self.rigid
        self._param_dict["rigid"] = value

        if self.rigid is not None and self.rigid._added:
            raise ValueError("Cannot add Rigid object to multiple integrators.")

        if old_rigid is not None and self._attached:
            old_rigid._detach()

        if self._added:
            if old_rigid is not None:
                old_rigid._remove()
            self.rigid._add(self._simulation)

        if self._attached:
            self.rigid._attach()
            self._cpp_obj.rigid = value._cpp_obj

class Integrator(_DynamicIntegrator):
    """Enables a variety of standard integration methods.

    Args:
        dt (float): Integrator time step size :math:`[\\mathrm{time}]`.

        methods (Sequence[hoomd.md.methods.Method]): Sequence of integration
            methods. Each integration method can be applied to only a specific
            subset of particles. The intersection of the subsets must be null.
            The default value of ``None`` initializes an empty list.

        forces (Sequence[hoomd.md.force.Force]): Sequence of forces applied to
            the particles in the system. All the forces are summed together.
            The default value of ``None`` initializes an empty list.

        aniso (str or bool): Whether to integrate rotational degrees of freedom
            (bool), default 'auto' (autodetect if there is anisotropic factor
            from any defined active or constraint forces).

        constraints (Sequence[hoomd.md.constrain.Constraint]): Sequence of
            constraint forces applied to the particles in the system.
            The default value of ``None`` initializes an empty list. Rigid body
            objects (i.e. `hoomd.md.constrain.Rigid`) are not allowed in the
            list.

        rigid (hoomd.md.constrain.Rigid): A rigid bodies object defining the
            rigid bodies in the simulation.

    The following classes can be used as elements in `methods`

    - `hoomd.md.methods.Brownian`
    - `hoomd.md.methods.Langevin`
    - `hoomd.md.methods.NVE`
    - `hoomd.md.methods.NVT`
    - `hoomd.md.methods.NPT`

    The classes of following modules can be used as elements in `forces`

    - `hoomd.md.angle`
    - `hoomd.md.bond`
    - `hoomd.md.charge`
    - `hoomd.md.dihedral`
    - `hoomd.md.external.field`
    - `hoomd.md.force`
    - `hoomd.md.improper`
    - `hoomd.md.pair`
    - `hoomd.md.wall`
    - `hoomd.md.special_pair`

    The classes of the following module can be used as elements in `constraints`

    - `hoomd.md.constrain`

    Examples::

        nlist = hoomd.md.nlist.Cell()
        lj = hoomd.md.pair.LJ(nlist=nlist)
        lj.params.default = dict(epsilon=1.0, sigma=1.0)
        lj.r_cut[('A', 'A')] = 2**(1/6)
        nve = hoomd.md.methods.NVE(filter=hoomd.filter.All())
        integrator = hoomd.md.Integrator(dt=0.001, methods=[nve], forces=[lj])
        sim.operations.integrator = integrator

    Attributes:
        dt (float): Integrator time step size :math:`[\\mathrm{time}]`.

        methods (list[hoomd.md.methods.Method]): List of integration methods.
            Each integration method can be applied to only a specific subset of
            particles.

        forces (list[hoomd.md.force.Force]): List of forces applied to
            the particles in the system. All the forces are summed together.

        aniso (str): Whether rotational degrees of freedom are integrated.

        constraints (list[hoomd.md.constrain.Constraint]): List of
            constraint forces applied to the particles in the system.

        rigid (hoomd.md.constrain.Rigid): The rigid body definition for the
            simulation associated with the integrator.
    """

    def __init__(self,
                 dt,
                 aniso='auto',
                 forces=None,
                 constraints=None,
                 methods=None,
                 rigid=None):

        super().__init__(forces, constraints, methods, rigid)

        self._param_dict.update(
            ParameterDict(dt=float(dt),
                          aniso=OnlyFrom(['true', 'false', 'auto'],
                                         preprocess=_preprocess_aniso),
                          _defaults={"aniso": "auto"}))
        if aniso is not None:
            self.aniso = aniso

    def _attach(self):
        # initialize the reflected c++ class
        self._cpp_obj = _md.IntegratorTwoStep(
            self._simulation.state._cpp_sys_def, self.dt)
        # Call attach from DynamicIntegrator which attaches forces,
        # constraint_forces, and methods, and calls super()._attach() itself.
        super()._attach()
<commit_msg>Fix hoomd.md.Integrator detaching.<commit_after># Copyright (c) 2009-2021 The Regents of the University of Michigan
# This file is part of the HOOMD-blue project, released under the BSD 3-Clause
# License.

"""Implement MD Integrator."""

import itertools

from hoomd.md import _md
from hoomd.data.parameterdicts import ParameterDict
from hoomd.data.typeconverter import OnlyFrom, OnlyTypes
from hoomd.integrate import BaseIntegrator
from hoomd.data import syncedlist
from hoomd.md.methods import Method
from hoomd.md.force import Force
from hoomd.md.constrain import Constraint, Rigid

def _preprocess_aniso(value):
    if value is True:
        return "true"
    elif value is False:
        return "false"
    else:
        return value

def _set_synced_list(old_list, new_list):
    old_list.clear()
    old_list.extend(new_list)

class _DynamicIntegrator(BaseIntegrator):

    def __init__(self, forces, constraints, methods, rigid):
        forces = [] if forces is None else forces
        constraints = [] if constraints is None else constraints
        methods = [] if methods is None else methods
        self._forces = syncedlist.SyncedList(
            Force, syncedlist._PartialGetAttr('_cpp_obj'), iterable=forces)

        self._constraints = syncedlist.SyncedList(
            OnlyTypes(Constraint, disallow_types=(Rigid,)),
            syncedlist._PartialGetAttr('_cpp_obj'),
            iterable=constraints)

        self._methods = syncedlist.SyncedList(
            Method, syncedlist._PartialGetAttr('_cpp_obj'), iterable=methods)

        param_dict = ParameterDict(rigid=OnlyTypes(Rigid, allow_none=True))
        if rigid is not None and rigid._added:
            raise ValueError("Rigid object can only belong to one integrator.")
        param_dict["rigid"] = rigid
        self._param_dict.update(param_dict)

    def _attach(self):
        self.forces._sync(self._simulation, self._cpp_obj.forces)
        self.constraints._sync(self._simulation, self._cpp_obj.constraints)
        self.methods._sync(self._simulation, self._cpp_obj.methods)
        super()._attach()
        if self.rigid is not None:
            self.rigid._attach()
            self._cpp_obj.rigid = self.rigid._cpp_obj

    def _detach(self):
        self._forces._unsync()
        self._methods._unsync()
        self._constraints._unsync()
        if self.rigid is not None:
            self.rigid._detach()

    def _add(self, simulation):
        super()._add(simulation)
        if self.rigid is not None:
            self.rigid._add(simulation)

    @property
    def forces(self):
        return self._forces

    @forces.setter
    def forces(self, value):
        _set_synced_list(self._forces, value)

    @property
    def constraints(self):
        return self._constraints

    @constraints.setter
    def constraints(self, value):
        _set_synced_list(self._constraints, value)

    @property
    def methods(self):
        return self._methods

    @methods.setter
    def methods(self, value):
        _set_synced_list(self._methods, value)

    @property
    def _children(self):
        children = list(self.forces)
        children.extend(self.constraints)
        children.extend(self.methods)

        for child in itertools.chain(self.forces, self.constraints,
                                     self.methods):
            children.extend(child._children)

        return children

    def _getattr_param(self, attr):
        if attr == "rigid":
            return self._param_dict["rigid"]
        return super()._getattr_param(attr)

    def _setattr_param(self, attr, value):
        if attr == "rigid":
            self._set_rigid(value)
            return
        super()._setattr_param(attr, value)

    def _set_rigid(self, value):
        """Handles the adding and detaching of potential Rigid objects."""
        # this generally only happens when attaching and we can ignore it since
        # we attach the rigid body in _attach.
        if value is self.rigid:
            return

        old_rigid = self.rigid
        self._param_dict["rigid"] = value

        if self.rigid is not None and self.rigid._added:
            raise ValueError("Cannot add Rigid object to multiple integrators.")

        if old_rigid is not None and self._attached:
            old_rigid._detach()

        if self._added:
            if old_rigid is not None:
                old_rigid._remove()
            self.rigid._add(self._simulation)

        if self._attached:
            self.rigid._attach()
            self._cpp_obj.rigid = value._cpp_obj

class Integrator(_DynamicIntegrator):
    """Enables a variety of standard integration methods.

    Args:
        dt (float): Integrator time step size :math:`[\\mathrm{time}]`.

        methods (Sequence[hoomd.md.methods.Method]): Sequence of integration
            methods. Each integration method can be applied to only a specific
            subset of particles. The intersection of the subsets must be null.
            The default value of ``None`` initializes an empty list.

        forces (Sequence[hoomd.md.force.Force]): Sequence of forces applied to
            the particles in the system. All the forces are summed together.
            The default value of ``None`` initializes an empty list.

        aniso (str or bool): Whether to integrate rotational degrees of freedom
            (bool), default 'auto' (autodetect if there is anisotropic factor
            from any defined active or constraint forces).

        constraints (Sequence[hoomd.md.constrain.Constraint]): Sequence of
            constraint forces applied to the particles in the system.
            The default value of ``None`` initializes an empty list. Rigid body
            objects (i.e. `hoomd.md.constrain.Rigid`) are not allowed in the
            list.

        rigid (hoomd.md.constrain.Rigid): A rigid bodies object defining the
            rigid bodies in the simulation.

    The following classes can be used as elements in `methods`

    - `hoomd.md.methods.Brownian`
    - `hoomd.md.methods.Langevin`
    - `hoomd.md.methods.NVE`
    - `hoomd.md.methods.NVT`
    - `hoomd.md.methods.NPT`

    The classes of following modules can be used as elements in `forces`

    - `hoomd.md.angle`
    - `hoomd.md.bond`
    - `hoomd.md.charge`
    - `hoomd.md.dihedral`
    - `hoomd.md.external.field`
    - `hoomd.md.force`
    - `hoomd.md.improper`
    - `hoomd.md.pair`
    - `hoomd.md.wall`
    - `hoomd.md.special_pair`

    The classes of the following module can be used as elements in `constraints`

    - `hoomd.md.constrain`

    Examples::

        nlist = hoomd.md.nlist.Cell()
        lj = hoomd.md.pair.LJ(nlist=nlist)
        lj.params.default = dict(epsilon=1.0, sigma=1.0)
        lj.r_cut[('A', 'A')] = 2**(1/6)
        nve = hoomd.md.methods.NVE(filter=hoomd.filter.All())
        integrator = hoomd.md.Integrator(dt=0.001, methods=[nve], forces=[lj])
        sim.operations.integrator = integrator

    Attributes:
        dt (float): Integrator time step size :math:`[\\mathrm{time}]`.

        methods (list[hoomd.md.methods.Method]): List of integration methods.
            Each integration method can be applied to only a specific subset of
            particles.

        forces (list[hoomd.md.force.Force]): List of forces applied to
            the particles in the system. All the forces are summed together.

        aniso (str): Whether rotational degrees of freedom are integrated.

        constraints (list[hoomd.md.constrain.Constraint]): List of
            constraint forces applied to the particles in the system.

        rigid (hoomd.md.constrain.Rigid): The rigid body definition for the
            simulation associated with the integrator.
    """

    def __init__(self,
                 dt,
                 aniso='auto',
                 forces=None,
                 constraints=None,
                 methods=None,
                 rigid=None):

        super().__init__(forces, constraints, methods, rigid)

        self._param_dict.update(
            ParameterDict(dt=float(dt),
                          aniso=OnlyFrom(['true', 'false', 'auto'],
                                         preprocess=_preprocess_aniso),
                          _defaults={"aniso": "auto"}))
        if aniso is not None:
            self.aniso = aniso

    def _attach(self):
        # initialize the reflected c++ class
        self._cpp_obj = _md.IntegratorTwoStep(
            self._simulation.state._cpp_sys_def, self.dt)
        # Call attach from DynamicIntegrator which attaches forces,
        # constraint_forces, and methods, and calls super()._attach() itself.
        super()._attach()
<|endoftext|>


<commit_before>
try:
    import redis
except ImportError:
    pass
else:
    from .types import *
    from .client import *

__version__ = "0.2.0"
<commit_msg>Version bump to 0.2.1<commit_after>
try:
    import redis
except ImportError:
    pass
else:
    from .types import *
    from .client import *

__version__ = "0.2.1"
<|endoftext|>


<commit_before>"""ben-sh - Execute a campaign

Usage:
  ben-sh [-v | -vv] [-r TAG] [-n HOST] [-o OUTDIR] [-l LOGFILE]
         [-g] CAMPAIGN_FILE
  ben-sh (-h | --help)
  ben-sh --version

Options:
  -n HOST                 Specify node name. Default is localhost
  -o --output-dir=OUTDIR  Specify output directory, overwriting campaign file
                          output_dir directive
  -l --log=LOGFILE        Specify an option logfile to write to
  -r --srun=TAG           Go into srun mode and run on one tag, which is used
                          when ben-sh is called as a dependent process inside
                          a SLURM job.
  -h --help               Show this screen
  -g                      Generate a default YAML campaign file
  --version               Show version
  -v -vv                  Increase program verbosity
"""

import os.path as osp

from hpcbench.campaign import Generator
from hpcbench.driver import CampaignDriver
from . import cli_common

def main(argv=None):
    """ben-sh entry point"""
    arguments = cli_common(__doc__, argv=argv)
    campaign_file = arguments['CAMPAIGN_FILE']
    if arguments['-g']:
        if osp.exists(campaign_file):
            raise Exception('Campaign file already exists')
        with open(campaign_file, 'w') as ostr:
            Generator().write(ostr)
    else:
        node = arguments.get('-n')
        output_dir = arguments.get('--output-dir')
        srun_tag = arguments.get('--srun')
        driver = CampaignDriver(campaign_file,
                                node=node,
                                output_dir=output_dir,
                                srun=srun_tag)
        driver()
        if argv is not None:
            return driver
<commit_msg>ben-sh prints campaign output path to standard output<commit_after>"""ben-sh - Execute a campaign

Usage:
  ben-sh [-v | -vv] [-r TAG] [-n HOST] [-o OUTDIR] [-l LOGFILE]
         [-g] CAMPAIGN_FILE
  ben-sh (-h | --help)
  ben-sh --version

Options:
  -n HOST                 Specify node name. Default is localhost
  -o --output-dir=OUTDIR  Specify output directory, overwriting campaign file
                          output_dir directive
  -l --log=LOGFILE        Specify an option logfile to write to
  -r --srun=TAG           Go into srun mode and run on one tag, which is used
                          when ben-sh is called as a dependent process inside
                          a SLURM job.
  -h --help               Show this screen
  -g                      Generate a default YAML campaign file
  --version               Show version
  -v -vv                  Increase program verbosity
"""
from __future__ import print_function
import os.path as osp

from hpcbench.campaign import Generator
from hpcbench.driver import CampaignDriver
from . import cli_common

def main(argv=None):
    """ben-sh entry point"""
    arguments = cli_common(__doc__, argv=argv)
    campaign_file = arguments['CAMPAIGN_FILE']
    if arguments['-g']:
        if osp.exists(campaign_file):
            raise Exception('Campaign file already exists')
        with open(campaign_file, 'w') as ostr:
            Generator().write(ostr)
    else:
        node = arguments.get('-n')
        output_dir = arguments.get('--output-dir')
        srun_tag = arguments.get('--srun')
        driver = CampaignDriver(campaign_file,
                                node=node,
                                output_dir=output_dir,
                                srun=srun_tag)
        driver()
        if argv is not None:
            return driver
        print(osp.abspath(driver.campaign_path))
<|endoftext|>


<commit_before>#    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.
#
"""Utilities and helper functions."""

import os
import sys

from cinderclient import client as cinder
from os_brick.initiator import connector
from oslo_log import log as logging
from oslo_utils import encodeutils
from oslo_utils import netutils
import prettytable
import six

LOG = logging.getLogger(__name__)

def get_initiator():
    """Get the initiator connector dict."""
    # Get the intiator side connector properties
    my_ip = netutils.get_my_ipv4()
    initiator = connector.get_connector_properties('sudo', my_ip, True, False)
    LOG.debug("initiator = %s", initiator)
    return initiator

def build_cinder(args):
    """Build the cinder client object."""
    (os_username, os_password, os_tenant_name,
     os_auth_url, os_tenant_id) = (
        args.os_username, args.os_password, args.os_tenant_name,
        args.os_auth_url, args.os_tenant_id)

    # force this to version 2.0 of Cinder API
    api_version = 2

    c = cinder.Client(api_version,
                      os_username, os_password,
                      os_tenant_name,
                      os_auth_url,
                      tenant_id=os_tenant_id)
    return c

def env(*vars, **kwargs):
    """This returns the first environment variable set.

    if none are non-empty, defaults to '' or keyword arg default

    """
    for v in vars:
        value = os.environ.get(v, None)
        if value:
            return value
    return kwargs.get('default', '')

def _print(pt, order):
    if sys.version_info >= (3, 0):
        print(pt.get_string(sortby=order))
    else:
        print(encodeutils.safe_encode(pt.get_string(sortby=order)))

def print_list(objs, fields, exclude_unavailable=False, formatters=None,
               sortby_index=0):
    '''Prints a list of objects.

    @param objs: Objects to print
    @param fields: Fields on each object to be printed
    @param exclude_unavailable: Boolean to decide if unavailable fields are
                                removed
    @param formatters: Custom field formatters
    @param sortby_index: Results sorted against the key in the fields list at
                         this index; if None then the object order is not
                         altered
    '''
    formatters = formatters or {}
    mixed_case_fields = ['serverId']
    removed_fields = []
    rows = []

    for o in objs:
        row = []
        for field in fields:
            if field in removed_fields:
                continue
            if field in formatters:
                row.append(formatters[field](o))
            else:
                if field in mixed_case_fields:
                    field_name = field.replace(' ', '_')
                else:
                    field_name = field.lower().replace(' ', '_')
                if type(o) == dict and field in o:
                    data = o[field]
                else:
                    if not hasattr(o, field_name) and exclude_unavailable:
                        removed_fields.append(field)
                        continue
                    else:
                        data = getattr(o, field_name, '')
                if data is None:
                    data = '-'
                if isinstance(data, six.string_types) and "\r" in data:
                    data = data.replace("\r", " ")
                row.append(data)
        rows.append(row)

    for f in removed_fields:
        fields.remove(f)

    pt = prettytable.PrettyTable((f for f in fields), caching=False)
    pt.aligns = ['l' for f in fields]
    for row in rows:
        pt.add_row(row)

    if sortby_index is None:
        order_by = None
    else:
        order_by = fields[sortby_index]
    _print(pt, order_by)

def print_dict(d, property="Property"):
    """Print out a dict."""
    pt = prettytable.PrettyTable([property, 'Value'], caching=False)
    pt.aligns = ['l', 'l']
    for r in six.iteritems(d):
        r = list(r)
        if isinstance(r[1], six.string_types) and "\r" in r[1]:
            r[1] = r[1].replace("\r", " ")
        pt.add_row(r)
    _print(pt, property)
<commit_msg>simple fix in output for a list<commit_after>#    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.
#
"""Utilities and helper functions."""

import os
import sys

from cinderclient import client as cinder
from os_brick.initiator import connector
from oslo_log import log as logging
from oslo_utils import encodeutils
from oslo_utils import netutils
import prettytable
import six

LOG = logging.getLogger(__name__)

def get_initiator():
    """Get the initiator connector dict."""
    # Get the intiator side connector properties
    my_ip = netutils.get_my_ipv4()
    initiator = connector.get_connector_properties('sudo', my_ip, True, False)
    LOG.debug("initiator = %s", initiator)
    return initiator

def build_cinder(args):
    """Build the cinder client object."""
    (os_username, os_password, os_tenant_name,
     os_auth_url, os_tenant_id) = (
        args.os_username, args.os_password, args.os_tenant_name,
        args.os_auth_url, args.os_tenant_id)

    # force this to version 2.0 of Cinder API
    api_version = 2

    c = cinder.Client(api_version,
                      os_username, os_password,
                      os_tenant_name,
                      os_auth_url,
                      tenant_id=os_tenant_id)
    return c

def env(*vars, **kwargs):
    """This returns the first environment variable set.

    if none are non-empty, defaults to '' or keyword arg default

    """
    for v in vars:
        value = os.environ.get(v, None)
        if value:
            return value
    return kwargs.get('default', '')

def _print(pt, order):
    if sys.version_info >= (3, 0):
        print(pt.get_string(sortby=order))
    else:
        print(encodeutils.safe_encode(pt.get_string(sortby=order)))

def print_list(objs, fields, exclude_unavailable=False, formatters=None,
               sortby_index=0):
    '''Prints a list of objects.

    @param objs: Objects to print
    @param fields: Fields on each object to be printed
    @param exclude_unavailable: Boolean to decide if unavailable fields are
                                removed
    @param formatters: Custom field formatters
    @param sortby_index: Results sorted against the key in the fields list at
                         this index; if None then the object order is not
                         altered
    '''
    formatters = formatters or {}
    mixed_case_fields = ['serverId']
    removed_fields = []
    rows = []

    for o in objs:
        row = []
        for field in fields:
            if field in removed_fields:
                continue
            if field in formatters:
                row.append(formatters[field](o))
            else:
                if field in mixed_case_fields:
                    field_name = field.replace(' ', '_')
                else:
                    field_name = field.lower().replace(' ', '_')
                if type(o) == dict and field in o:
                    data = o[field]
                else:
                    if not hasattr(o, field_name) and exclude_unavailable:
                        removed_fields.append(field)
                        continue
                    else:
                        data = getattr(o, field_name, '')
                if data is None:
                    data = '-'
                if isinstance(data, six.string_types) and "\r" in data:
                    data = data.replace("\r", " ")
                row.append(data)
        rows.append(row)

    for f in removed_fields:
        fields.remove(f)

    pt = prettytable.PrettyTable((f for f in fields), caching=False)
    pt.aligns = ['l' for f in fields]
    for row in rows:
        pt.add_row(row)

    if sortby_index is None:
        order_by = None
    else:
        order_by = fields[sortby_index]
    _print(pt, order_by)

def print_dict(d, property="Property"):
    """Print out a dict."""
    pt = prettytable.PrettyTable([property, 'Value'], caching=False)
    pt.aligns = ['l', 'l']
    for r in six.iteritems(d):
        r = list(r)
        if isinstance(r[1], list):
            r[1] = '\n'.join(r[1])
        if isinstance(r[1], six.string_types) and "\r" in r[1]:
            r[1] = r[1].replace("\r", " ")
        pt.add_row(r)
    _print(pt, property)
<|endoftext|>


<commit_before>#!/usr/bin/env python
#
# Copyright (c)  2013 Hewlett-Packard Development Company, L.P.
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software  and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
#
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR  OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

"""This library provides a Python interface to the HP SDN
Controller API"""

__version__ = '0.3.0'

from hpsdnclient.api import ApiBase
from hpsdnclient.core import CoreMixin
from hpsdnclient.net import NetMixin
from hpsdnclient.of import OfMixin

class Api(CoreMixin, OfMixin, NetMixin, ApiBase):
    """ The container class for the HP SDN Controller Api """
    def __init__(self, controller, auth):
        super(Api, self).__init__(controller, auth)
<commit_msg>version bump<commit_after>#!/usr/bin/env python
#
# Copyright (c)  2013 Hewlett-Packard Development Company, L.P.
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software  and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
#
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR  OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

"""This library provides a Python interface to the HP SDN
Controller API"""

__version__ = '0.3.1'

from hpsdnclient.api import ApiBase
from hpsdnclient.core import CoreMixin
from hpsdnclient.net import NetMixin
from hpsdnclient.of import OfMixin

class Api(CoreMixin, OfMixin, NetMixin, ApiBase):
    """ The container class for the HP SDN Controller Api """
    def __init__(self, controller, auth):
        super(Api, self).__init__(controller, auth)
<|endoftext|>


<commit_before>#! python
# -*- coding: UTF-8 -*-

# $Id$

"Date time utilities not available in stock python"

import datetime
import time

#TODO: replace this with dateutil.parser.parse()
class Parser(object):
	"""Datetime parser: parses a date-time string using multiple possible formats.
	>>> p = Parser(('%H%M', '%H:%M'))
	>>> tuple(p.parse('1319'))
	(1900, 1, 1, 13, 19, 0, 0, 1, -1)
	>>> dateParser = Parser(('%m/%d/%Y', '%Y-%m-%d', '%d-%b-%Y'))
	>>> tuple(dateParser.parse('2003-12-20'))
	(2003, 12, 20, 0, 0, 0, 5, 354, -1)
	>>> tuple(dateParser.parse('16-Dec-1994'))
	(1994, 12, 16, 0, 0, 0, 4, 350, -1)
	>>> tuple(dateParser.parse('5/19/2003'))
	(2003, 5, 19, 0, 0, 0, 0, 139, -1)
	>>> dtParser = Parser(('%Y-%m-%d %H:%M:%S', '%a %b %d %H:%M:%S %Y'))
	>>> tuple(dtParser.parse('2003-12-20 19:13:26'))
	(2003, 12, 20, 19, 13, 26, 5, 354, -1)
	>>> tuple(dtParser.parse('Tue Jan 20 16:19:33 2004'))
	(2004, 1, 20, 16, 19, 33, 1, 20, -1)

	Be forewarned:
	>>> Parser(('%H%M', '%H%M%S')).parse('732')
	Traceback (most recent call last):
		...
	ValueError: More than one format string matched target 732.
	"""

	# default to some common formats
	formats = ('%m/%d/%Y', '%m/%d/%y', '%Y-%m-%d', '%d-%b-%Y', '%d-%b-%y')
	def __init__(self, formats = None):
		if formats:
			self.formats = formats

	def parse(self, target):
		self.target = target
		results = filter(None, map(self._parse, self.formats))
		del self.target
		if not results:
			raise ValueError, "No format strings matched the target %s." % target
		if not len(results) == 1:
			raise ValueError, "More than one format string matched target %s." % target
		return results[0]

	def _parse(self, format):
		try:
			result = time.strptime(self.target, format)
		except ValueError:
			result = False
		return result

# some useful constants
# mean vernal equinox year expressed in oscillations of atomic cesium at the year 2000
# see http://webexhibits.org/calendars/timeline.html for more info
osc_per_year = 290091329207984000
osc_per_second = 9192631770
seconds_per_year = 31556940
seconds_per_minute = 60
minutes_per_hour = 60
hours_per_day = 24
seconds_per_hour = seconds_per_minute * minutes_per_hour
seconds_per_day = seconds_per_hour * hours_per_day
days_per_year = float(seconds_per_year) / seconds_per_day
six_months = datetime.timedelta(days=days_per_year/2)
thirty_days = datetime.timedelta(days=30)

def strftime(fmt, t):
	"""A class to replace the strftime in datetime package or time module.
	Identical to strftime behavior in those modules except supports any
	year.
	Also supports datetime.datetime times.
	Also supports milliseconds using %s
	Also supports microseconds using %u"""
	if isinstance(t, (time.struct_time, tuple)):
		t = datetime.datetime(*t[:6])
	assert isinstance(t, (datetime.datetime, datetime.time, datetime.date))
	try:
		year = t.year
		if year < 1900: t = t.replace(year = 1900)
	except AttributeError:
		year = 1900
	subs = (
		('%Y', '%04d' % year),
		('%y', '%02d' % (year % 100)),
		('%s', '%03d' % (t.microsecond / 1000)),
		('%u', '%03d' % (t.microsecond % 1000))
		)
	doSub = lambda s, sub: s.replace(*sub)
	doSubs = lambda s: reduce(doSub, subs, s)
	fmt = '%%'.join(map(doSubs, fmt.split('%%')))
	return t.strftime(fmt)

def strptime(s, fmt, tzinfo = None):
	"""A function to replace strptime in the time module.  Should behave identically
	to the strptime function except it returns a datetime.datetime object instead of
	a time.struct_time object.
	Also takes an optional tzinfo parameter which is a time zone info object."""
	res = time.strptime(s, fmt)
	return datetime.datetime(tzinfo = tzinfo, *res[:6])

def ConstructDatetime(*args, **kargs):
	"""Construct a datetime.datetime from a number of different time
	types found in python and pythonwin"""
	if len(args) == 1:
		arg = args[0]
		method = __GetDTConstructor__(type(arg).__module__, type(arg).__name__)
		result = method(arg)
		try:
			result = result.replace(tzinfo = kargs.pop('tzinfo'))
		except KeyError:
			pass
		if kargs:
			raise TypeError, "%s is an invalid keyword argument for this function." % kargs.keys()[0]
	else:
		result = datetime.datetime(*args, **kargs)
	return result

def __GetDTConstructor__(moduleName, name):
	try:
		return eval('__dt_from_%(moduleName)s_%(name)s__' % vars())
	except NameError:
		raise TypeError, "No way to construct datetime.datetime from %s.%s" % (moduleName, name)

def __dt_from_datetime_datetime__(source):
	dtattrs = ('year', 'month', 'day', 'hour', 'minute', 'second', 'microsecond', 'tzinfo')
	attrs = map(lambda a: getattr(source, a), dtattrs)
	return datetime.datetime(*attrs)

def __dt_from___builtin___time__(pyt):
	"Construct a datetime.datetime from a pythonwin time"
	fmtString = '%Y-%m-%d %H:%M:%S'
	result = strptime(pyt.Format(fmtString), fmtString)
	# get milliseconds and microseconds.  The only way to do this is
	#  to use the __float__ attribute of the time, which is in days.
	microseconds_per_day = seconds_per_day * 1000000
	microseconds = float(pyt) * microseconds_per_day
	microsecond = int(microseconds % 1000000)
	result = result.replace(microsecond = microsecond)
	return result

def __dt_from_timestamp__(timestamp):
	return datetime.datetime.utcfromtimestamp(timestamp)
__dt_from___builtin___float__ = __dt_from_timestamp__
__dt_from___builtin___long__ = __dt_from_timestamp__
__dt_from___builtin___int__ = __dt_from_timestamp__

def __dt_from_time_struct_time__(s):
	return datetime.datetime(*s[:6])

def DatetimeMod(dt, period, start = None):
	"""Find the time which is the specified date/time truncated to the time delta
	relative to the start date/time.
	By default, the start time is midnight of the same day as the specified date/time.
	>>> DatetimeMod(datetime.datetime(2004, 1, 2, 3), datetime.timedelta(days = 1.5), start = datetime.datetime(2004, 1, 1))
	datetime.datetime(2004, 1, 1, 0, 0)
	>>> DatetimeMod(datetime.datetime(2004, 1, 2, 13), datetime.timedelta(days = 1.5), start = datetime.datetime(2004, 1, 1))
	datetime.datetime(2004, 1, 2, 12, 0)
	>>> DatetimeMod(datetime.datetime(2004, 1, 2, 13), datetime.timedelta(days = 7), start = datetime.datetime(2004, 1, 1))
	datetime.datetime(2004, 1, 1, 0, 0)
	>>> DatetimeMod(datetime.datetime(2004, 1, 10, 13), datetime.timedelta(days = 7), start = datetime.datetime(2004, 1, 1))
	datetime.datetime(2004, 1, 8, 0, 0)
"""
	if start is None:
		# use midnight of the same day
		start = datetime.datetime.combine(dt.date(), datetime.time())
	# calculate the difference between the specified time and the start date.
	delta = dt - start
	# now aggregate the delta and the period into microseconds
	# I use microseconds because that's the highest precision of these time pieces.  Also,
	#  using microseconds ensures perfect precision (no floating point errors).
	GetTimeDeltaMicroseconds = lambda td: (td.days * seconds_per_day + td.seconds) * 1000000 + td.microseconds
	delta, period = map(GetTimeDeltaMicroseconds , (delta, period))
	offset = datetime.timedelta(microseconds = delta % period)
	# the result is the original specified time minus the offset
	result = dt - offset
	return result

def DatetimeRound(dt, period, start = None):
	"""Find the nearest even period for the specified date/time.
	>>> DatetimeRound(datetime.datetime(2004, 11, 13, 8, 11, 13), datetime.timedelta(hours = 1))
	datetime.datetime(2004, 11, 13, 8, 0)
	>>> DatetimeRound(datetime.datetime(2004, 11, 13, 8, 31, 13), datetime.timedelta(hours = 1))
	datetime.datetime(2004, 11, 13, 9, 0)
	>>> DatetimeRound(datetime.datetime(2004, 11, 13, 8, 30), datetime.timedelta(hours = 1))
	datetime.datetime(2004, 11, 13, 9, 0)
	"""
	result = DatetimeMod(dt, period, start)
	if abs(dt - result) >= period / 2:
		result += period
	return result

# This function takes a Julian day and infers a year by choosing the
#  nearest year to that date.
def GetNearestYearForDay(day):
	now = time.gmtime()
	result = now.tm_year
	# if the day is far greater than today, it must be from last year
	if day - now.tm_yday > 365/2:
		result -= 1
	# if the day is far less than today, it must be for next year.
	if now.tm_yday - day > 365/2:
		result += 1
	return result

def gregorian_date(year, julian_day):
	"""
	Gregorian Date is defined as a year and a julian day (1-based
	index into the days of the year).
	>>> gregorian_date(2007, 15)
	datetime.date(2007, 1, 15)
	"""
	result = datetime.date(year, 1, 1)
	result += datetime.timedelta(days = julian_day - 1)
	return result

def get_period_seconds(period):
	"""
	return the number of seconds in the specified period
	"""
	if isinstance(period, basestring):
		try:
			result = eval('seconds_per_%s' % period.lower())
		except NameError:
			raise ValueError, "period not in (minute, hour, day, year)"
	elif isinstance(period, (int, long)):
		result = period
	elif isinstance(period, datetime.timedelta):
		result = period.days * get_period_seconds('day') + period.seconds
	else:
		raise TypeError, 'period must be a string or integer'
	return result

def get_date_format_string(period):
	"""
	for a given period (e.g. 'month', 'day', or some numeric interval
	such as 3600 (in secs)), return the format string that can be
	used with strftime to format that time to specify the times
	across that interval, but no more detailed.
	so,
	get_date_format_string('month') == '%Y-%m'
	get_date_format_string(3600) == get_date_format_string('hour') == '%Y-%m-%d %H'
	get_date_format_string(None) -> raise TypeError
	get_date_format_string('garbage') -> raise ValueError
	"""
	# handle the special case of 'month' which doesn't have
	#  a static interval in seconds
	if isinstance(period, basestring) and string.lower(period) == 'month':
		result = '%Y-%m'
	else:
		file_period_secs = get_period_seconds(period)
		format_pieces = ('%Y', '-%m-%d', ' %H', '-%M', '-%S')
		intervals = (
			seconds_per_year,
			seconds_per_day,
			seconds_per_hour,
			seconds_per_minute,
			1, # seconds_per_second
			)
		mods = map(lambda interval: file_period_secs % interval, intervals)
		format_pieces = format_pieces[: mods.index(0) + 1]
		result = ''.join(format_pieces)
	return result

def divide_timedelta_float(td, divisor):
	# td is comprised of days, seconds, microseconds
	dsm = [getattr(td, attr) for attr in ('days', 'seconds', 'microseconds')]
	dsm = map(lambda elem: elem/divisor, dsm)
	return timedelta(*dsm)

# for backward compatibility
getPeriodSeconds = get_period_seconds
getDateFormatString = get_date_format_string
GregorianDate = gregorian_date<commit_msg>Began work on Python 3.0 compatibility<commit_after>#! python
# -*- coding: UTF-8 -*-

# $Id$

"Date time utilities not available in stock python"

import datetime
import time

#TODO: replace this with dateutil.parser.parse()
class Parser(object):
	"""Datetime parser: parses a date-time string using multiple possible formats.
	>>> p = Parser(('%H%M', '%H:%M'))
	>>> tuple(p.parse('1319'))
	(1900, 1, 1, 13, 19, 0, 0, 1, -1)
	>>> dateParser = Parser(('%m/%d/%Y', '%Y-%m-%d', '%d-%b-%Y'))
	>>> tuple(dateParser.parse('2003-12-20'))
	(2003, 12, 20, 0, 0, 0, 5, 354, -1)
	>>> tuple(dateParser.parse('16-Dec-1994'))
	(1994, 12, 16, 0, 0, 0, 4, 350, -1)
	>>> tuple(dateParser.parse('5/19/2003'))
	(2003, 5, 19, 0, 0, 0, 0, 139, -1)
	>>> dtParser = Parser(('%Y-%m-%d %H:%M:%S', '%a %b %d %H:%M:%S %Y'))
	>>> tuple(dtParser.parse('2003-12-20 19:13:26'))
	(2003, 12, 20, 19, 13, 26, 5, 354, -1)
	>>> tuple(dtParser.parse('Tue Jan 20 16:19:33 2004'))
	(2004, 1, 20, 16, 19, 33, 1, 20, -1)

	Be forewarned:
	>>> Parser(('%H%M', '%H%M%S')).parse('732')
	Traceback (most recent call last):
		...
	ValueError: More than one format string matched target 732.
	"""

	# default to some common formats
	formats = ('%m/%d/%Y', '%m/%d/%y', '%Y-%m-%d', '%d-%b-%Y', '%d-%b-%y')
	def __init__(self, formats = None):
		if formats:
			self.formats = formats

	def parse(self, target):
		self.target = target
		results = filter(None, map(self._parse, self.formats))
		del self.target
		if not results:
			raise ValueError("No format strings matched the target %s." % target)
		if not len(results) == 1:
			raise ValueError("More than one format string matched target %s." % target)
		return results[0]

	def _parse(self, format):
		try:
			result = time.strptime(self.target, format)
		except ValueError:
			result = False
		return result

# some useful constants
# mean vernal equinox year expressed in oscillations of atomic cesium at the year 2000
# see http://webexhibits.org/calendars/timeline.html for more info
osc_per_year = 290091329207984000
osc_per_second = 9192631770
seconds_per_year = 31556940
seconds_per_minute = 60
minutes_per_hour = 60
hours_per_day = 24
seconds_per_hour = seconds_per_minute * minutes_per_hour
seconds_per_day = seconds_per_hour * hours_per_day
days_per_year = float(seconds_per_year) / seconds_per_day
six_months = datetime.timedelta(days=days_per_year/2)
thirty_days = datetime.timedelta(days=30)

def strftime(fmt, t):
	"""A class to replace the strftime in datetime package or time module.
	Identical to strftime behavior in those modules except supports any
	year.
	Also supports datetime.datetime times.
	Also supports milliseconds using %s
	Also supports microseconds using %u"""
	if isinstance(t, (time.struct_time, tuple)):
		t = datetime.datetime(*t[:6])
	assert isinstance(t, (datetime.datetime, datetime.time, datetime.date))
	try:
		year = t.year
		if year < 1900: t = t.replace(year = 1900)
	except AttributeError:
		year = 1900
	subs = (
		('%Y', '%04d' % year),
		('%y', '%02d' % (year % 100)),
		('%s', '%03d' % (t.microsecond / 1000)),
		('%u', '%03d' % (t.microsecond % 1000))
		)
	doSub = lambda s, sub: s.replace(*sub)
	doSubs = lambda s: reduce(doSub, subs, s)
	fmt = '%%'.join(map(doSubs, fmt.split('%%')))
	return t.strftime(fmt)

def strptime(s, fmt, tzinfo = None):
	"""A function to replace strptime in the time module.  Should behave identically
	to the strptime function except it returns a datetime.datetime object instead of
	a time.struct_time object.
	Also takes an optional tzinfo parameter which is a time zone info object."""
	res = time.strptime(s, fmt)
	return datetime.datetime(tzinfo = tzinfo, *res[:6])

def ConstructDatetime(*args, **kargs):
	"""Construct a datetime.datetime from a number of different time
	types found in python and pythonwin"""
	if len(args) == 1:
		arg = args[0]
		method = __GetDTConstructor__(type(arg).__module__, type(arg).__name__)
		result = method(arg)
		try:
			result = result.replace(tzinfo = kargs.pop('tzinfo'))
		except KeyError:
			pass
		if kargs:
			raise TypeError("%s is an invalid keyword argument for this function." % kargs.keys()[0])
	else:
		result = datetime.datetime(*args, **kargs)
	return result

def __GetDTConstructor__(moduleName, name):
	try:
		return eval('__dt_from_%(moduleName)s_%(name)s__' % vars())
	except NameError:
		raise TypeError("No way to construct datetime.datetime from %s.%s" % (moduleName, name))

def __dt_from_datetime_datetime__(source):
	dtattrs = ('year', 'month', 'day', 'hour', 'minute', 'second', 'microsecond', 'tzinfo')
	attrs = map(lambda a: getattr(source, a), dtattrs)
	return datetime.datetime(*attrs)

def __dt_from___builtin___time__(pyt):
	"Construct a datetime.datetime from a pythonwin time"
	fmtString = '%Y-%m-%d %H:%M:%S'
	result = strptime(pyt.Format(fmtString), fmtString)
	# get milliseconds and microseconds.  The only way to do this is
	#  to use the __float__ attribute of the time, which is in days.
	microseconds_per_day = seconds_per_day * 1000000
	microseconds = float(pyt) * microseconds_per_day
	microsecond = int(microseconds % 1000000)
	result = result.replace(microsecond = microsecond)
	return result

def __dt_from_timestamp__(timestamp):
	return datetime.datetime.utcfromtimestamp(timestamp)
__dt_from___builtin___float__ = __dt_from_timestamp__
__dt_from___builtin___long__ = __dt_from_timestamp__
__dt_from___builtin___int__ = __dt_from_timestamp__

def __dt_from_time_struct_time__(s):
	return datetime.datetime(*s[:6])

def DatetimeMod(dt, period, start = None):
	"""Find the time which is the specified date/time truncated to the time delta
	relative to the start date/time.
	By default, the start time is midnight of the same day as the specified date/time.
	>>> DatetimeMod(datetime.datetime(2004, 1, 2, 3), datetime.timedelta(days = 1.5), start = datetime.datetime(2004, 1, 1))
	datetime.datetime(2004, 1, 1, 0, 0)
	>>> DatetimeMod(datetime.datetime(2004, 1, 2, 13), datetime.timedelta(days = 1.5), start = datetime.datetime(2004, 1, 1))
	datetime.datetime(2004, 1, 2, 12, 0)
	>>> DatetimeMod(datetime.datetime(2004, 1, 2, 13), datetime.timedelta(days = 7), start = datetime.datetime(2004, 1, 1))
	datetime.datetime(2004, 1, 1, 0, 0)
	>>> DatetimeMod(datetime.datetime(2004, 1, 10, 13), datetime.timedelta(days = 7), start = datetime.datetime(2004, 1, 1))
	datetime.datetime(2004, 1, 8, 0, 0)
"""
	if start is None:
		# use midnight of the same day
		start = datetime.datetime.combine(dt.date(), datetime.time())
	# calculate the difference between the specified time and the start date.
	delta = dt - start
	# now aggregate the delta and the period into microseconds
	# I use microseconds because that's the highest precision of these time pieces.  Also,
	#  using microseconds ensures perfect precision (no floating point errors).
	GetTimeDeltaMicroseconds = lambda td: (td.days * seconds_per_day + td.seconds) * 1000000 + td.microseconds
	delta, period = map(GetTimeDeltaMicroseconds , (delta, period))
	offset = datetime.timedelta(microseconds = delta % period)
	# the result is the original specified time minus the offset
	result = dt - offset
	return result

def DatetimeRound(dt, period, start = None):
	"""Find the nearest even period for the specified date/time.
	>>> DatetimeRound(datetime.datetime(2004, 11, 13, 8, 11, 13), datetime.timedelta(hours = 1))
	datetime.datetime(2004, 11, 13, 8, 0)
	>>> DatetimeRound(datetime.datetime(2004, 11, 13, 8, 31, 13), datetime.timedelta(hours = 1))
	datetime.datetime(2004, 11, 13, 9, 0)
	>>> DatetimeRound(datetime.datetime(2004, 11, 13, 8, 30), datetime.timedelta(hours = 1))
	datetime.datetime(2004, 11, 13, 9, 0)
	"""
	result = DatetimeMod(dt, period, start)
	if abs(dt - result) >= period / 2:
		result += period
	return result

# This function takes a Julian day and infers a year by choosing the
#  nearest year to that date.
def GetNearestYearForDay(day):
	now = time.gmtime()
	result = now.tm_year
	# if the day is far greater than today, it must be from last year
	if day - now.tm_yday > 365/2:
		result -= 1
	# if the day is far less than today, it must be for next year.
	if now.tm_yday - day > 365/2:
		result += 1
	return result

def gregorian_date(year, julian_day):
	"""
	Gregorian Date is defined as a year and a julian day (1-based
	index into the days of the year).
	>>> gregorian_date(2007, 15)
	datetime.date(2007, 1, 15)
	"""
	result = datetime.date(year, 1, 1)
	result += datetime.timedelta(days = julian_day - 1)
	return result

def get_period_seconds(period):
	"""
	return the number of seconds in the specified period
	"""
	if isinstance(period, basestring):
		try:
			result = eval('seconds_per_%s' % period.lower())
		except NameError:
			raise ValueError("period not in (minute, hour, day, year)")
	elif isinstance(period, (int, long)):
		result = period
	elif isinstance(period, datetime.timedelta):
		result = period.days * get_period_seconds('day') + period.seconds
	else:
		raise TypeError('period must be a string or integer')
	return result

def get_date_format_string(period):
	"""
	for a given period (e.g. 'month', 'day', or some numeric interval
	such as 3600 (in secs)), return the format string that can be
	used with strftime to format that time to specify the times
	across that interval, but no more detailed.
	so,
	get_date_format_string('month') == '%Y-%m'
	get_date_format_string(3600) == get_date_format_string('hour') == '%Y-%m-%d %H'
	get_date_format_string(None) -> raise TypeError
	get_date_format_string('garbage') -> raise ValueError
	"""
	# handle the special case of 'month' which doesn't have
	#  a static interval in seconds
	if isinstance(period, basestring) and string.lower(period) == 'month':
		result = '%Y-%m'
	else:
		file_period_secs = get_period_seconds(period)
		format_pieces = ('%Y', '-%m-%d', ' %H', '-%M', '-%S')
		intervals = (
			seconds_per_year,
			seconds_per_day,
			seconds_per_hour,
			seconds_per_minute,
			1, # seconds_per_second
			)
		mods = map(lambda interval: file_period_secs % interval, intervals)
		format_pieces = format_pieces[: mods.index(0) + 1]
		result = ''.join(format_pieces)
	return result

def divide_timedelta_float(td, divisor):
	# td is comprised of days, seconds, microseconds
	dsm = [getattr(td, attr) for attr in ('days', 'seconds', 'microseconds')]
	dsm = map(lambda elem: elem/divisor, dsm)
	return timedelta(*dsm)

# for backward compatibility
getPeriodSeconds = get_period_seconds
getDateFormatString = get_date_format_string
GregorianDate = gregorian_date<|endoftext|>


<commit_before>from __future__ import division # for proper float division

import os
import sys
import math
import time
import types
import functools
import random
import numpy as np
from director import ik
from director import ikconstraints
from director import ikconstraintencoder
import drc as lcmdrc
import json
from director.utime import getUtime
from director import lcmUtils
from director import robotstate

class PlannerPublisher(object):

  def __init__(self, ikPlanner, affordanceMan,robotModel):
    self.ikPlanner = ikPlanner
    self.affordanceManager = affordanceMan
    self.poses={}
    self.robotModel=robotModel
    self.jointLimits={}
    for jointName in robotstate.getDrakePoseJointNames():
       self.jointLimits[jointName]= [self.robotModel.model.getJointLimits(jointName)[0],self.robotModel.model.getJointLimits(jointName)[1]]

  def updateJointLimits(self, limitData):
    for jointName, epsilon, jointPosition in limitData:
        if epsilon < 0:
            self.jointLimits[jointName][0]=jointPosition
        else:
            self.jointLimits[jointName][1]=jointPosition
    print limitData
    print self.jointLimits

  def setupMessage(self, constraints, endPoseName="", nominalPoseName="", seedPoseName="", additionalTimeSamples=None):
    poses = ikconstraintencoder.getPlanPoses(constraints, self.ikPlanner)
    poses.update(self.poses)
    msg = lcmdrc.exotica_planner_request_t()
    msg.utime = getUtime()
    msg.poses = json.dumps(poses)
    msg.constraints = ikconstraintencoder.encodeConstraints(constraints)
    msg.seed_pose = seedPoseName
    msg.nominal_pose = nominalPoseName
    msg.end_pose = endPoseName
    msg.joint_names = json.dumps(list(self.ikPlanner.jointController.jointNames))
    msg.affordances = self.processAffordances()
    opt=ikplanner.getIkOptions()._properties
    if additionalTimeSamples:
      opt.update({'timeSamples':additionalTimeSamples})
    opt['jointLimits']=self.jointLimits
    msg.options = json.dumps(opt)
    return msg

  def processIK(self, constraints, endPoseName="", nominalPoseName="", seedPoseName="", additionalTimeSamples=None):
    listener = self.ikPlanner.getManipIKListener()
    msg = self.setupMessage(constraints, endPoseName, nominalPoseName, seedPoseName, additionalTimeSamples)
    lcmUtils.publish('IK_REQUEST', msg)
    ikplan = listener.waitForResponse(timeout=12000)
    listener.finish()

    endPose = [0] * self.ikPlanner.jointController.numberOfJoints
    if ikplan.num_states>0:
      endPose[len(endPose)-len(ikplan.plan[ikplan.num_states-1].joint_position):] = ikplan.plan[ikplan.num_states-1].joint_position
      info=ikplan.plan_info[ikplan.num_states-1]
    else:
      info = -1
    self.ikPlanner.ikServer.infoFunc(info)
    return endPose, info

  def processTraj(self, constraints, endPoseName="", nominalPoseName="", seedPoseName="", additionalTimeSamples=None):
    # Temporary fix / HACK / TODO (should be done in exotica_json)
    largestTspan = [0, 0]
    for constraintIndex, _ in enumerate(constraints):
      # Get tspan extend to normalise time-span
      if np.isfinite(constraints[constraintIndex].tspan[0]) and np.isfinite(constraints[constraintIndex].tspan[1]):
        largestTspan[0] = constraints[constraintIndex].tspan[0] if (constraints[constraintIndex].tspan[0] < largestTspan[0]) else largestTspan[0]
        largestTspan[1] = constraints[constraintIndex].tspan[1] if (constraints[constraintIndex].tspan[1] > largestTspan[1]) else largestTspan[1]

    # Temporary fix / HACK/ TODO to normalise time spans
    for constraintIndex, _ in enumerate(constraints):
      if np.isfinite(constraints[constraintIndex].tspan[0]) and np.isfinite(constraints[constraintIndex].tspan[1]):
        if largestTspan[1] != 0:
          constraints[constraintIndex].tspan[0] = constraints[constraintIndex].tspan[0] / largestTspan[1]
          constraints[constraintIndex].tspan[1] = constraints[constraintIndex].tspan[1] / largestTspan[1]

    listener = self.ikPlanner.getManipPlanListener()
    msg = self.setupMessage(constraints, endPoseName, nominalPoseName, seedPoseName, additionalTimeSamples)
    lcmUtils.publish('PLANNER_REQUEST', msg)
    lastManipPlan = listener.waitForResponse(timeout=20000)
    listener.finish()

    self.ikPlanner.ikServer.infoFunc(lastManipPlan.plan_info[0])
    return lastManipPlan, lastManipPlan.plan_info[0]

  def processAddPose(self, pose, poseName):
    self.poses[poseName]=list(pose);

  def processAffordances(self):
      affs = self.affordanceManager.getCollisionAffordances()
      s='['
      first=True
      for aff in affs:
        des=aff.getDescription()
        classname=des['classname'];
        if first:
          s+='{'
        else:
          s+='\n,{'
        first=False
        s+='"classname":"'+classname+'"'
        s+=',"name":"'+des['Name']+'"'
        s+=',"uuid":"'+des['uuid']+'"'
        s+=',"pose": {"position":{"__ndarray__":'+repr(des['pose'][0].tolist())+'},"quaternion":{"__ndarray__":'+repr(des['pose'][1].tolist())+'}}'
        if self.affordanceManager.affordanceUpdater is not None: # attached collision object / frameSync
          if des['Name'] in self.affordanceManager.affordanceUpdater.attachedAffordances:
            s+=',"attachedTo":"'+self.affordanceManager.affordanceUpdater.attachedAffordances[des['Name']]+'"'
          else: # it's not attached
            s+=',"attachedTo":"__world__"' # __world__ means it's a fixed collision object (sometimes called world or map - we use __world__ here)
        else: # no affordanceUpdater - so no attached collision objects either
          s+=',"attachedTo":"__world__"'
        if classname=='MeshAffordanceItem':
          s+=',"filename":"'+aff.getMeshManager().getFilesystemFilename(des['Filename'])+'"'
        if classname=='SphereAffordanceItem':
          s+=',"radius":'+repr(des['Radius'])
        if classname=='CylinderAffordanceItem' or classname=='CapsuleAffordanceItem':
          s+=',"radius":'+repr(des['Radius'])
          s+=',"length":'+repr(des['Length'])
        if classname=='BoxAffordanceItem':
          s+=',"dimensions":'+repr(des['Dimensions'])
        if classname=='CapsuleRingAffordanceItem':
          s+=',"radius":'+repr(des['Radius'])
          s+=',"tube_radius":'+repr(des['Tube Radius'])
          s+=',"segments":'+repr(des['Segments'])
        s+='}'
      s=s+']'
      return s

import ikplanner
<commit_msg>Remove print in PlannerPublisher.updateJointLimits()<commit_after>from __future__ import division # for proper float division

import os
import sys
import math
import time
import types
import functools
import random
import numpy as np
from director import ik
from director import ikconstraints
from director import ikconstraintencoder
import drc as lcmdrc
import json
from director.utime import getUtime
from director import lcmUtils
from director import robotstate

class PlannerPublisher(object):

  def __init__(self, ikPlanner, affordanceMan,robotModel):
    self.ikPlanner = ikPlanner
    self.affordanceManager = affordanceMan
    self.poses={}
    self.robotModel=robotModel
    self.jointLimits={}
    for jointName in robotstate.getDrakePoseJointNames():
       self.jointLimits[jointName]= [self.robotModel.model.getJointLimits(jointName)[0],self.robotModel.model.getJointLimits(jointName)[1]]

  def updateJointLimits(self, limitData):
    for jointName, epsilon, jointPosition in limitData:
        if epsilon < 0:
            self.jointLimits[jointName][0]=jointPosition
        else:
            self.jointLimits[jointName][1]=jointPosition

  def setupMessage(self, constraints, endPoseName="", nominalPoseName="", seedPoseName="", additionalTimeSamples=None):
    poses = ikconstraintencoder.getPlanPoses(constraints, self.ikPlanner)
    poses.update(self.poses)
    msg = lcmdrc.exotica_planner_request_t()
    msg.utime = getUtime()
    msg.poses = json.dumps(poses)
    msg.constraints = ikconstraintencoder.encodeConstraints(constraints)
    msg.seed_pose = seedPoseName
    msg.nominal_pose = nominalPoseName
    msg.end_pose = endPoseName
    msg.joint_names = json.dumps(list(self.ikPlanner.jointController.jointNames))
    msg.affordances = self.processAffordances()
    opt=ikplanner.getIkOptions()._properties
    if additionalTimeSamples:
      opt.update({'timeSamples':additionalTimeSamples})
    opt['jointLimits']=self.jointLimits
    msg.options = json.dumps(opt)
    return msg

  def processIK(self, constraints, endPoseName="", nominalPoseName="", seedPoseName="", additionalTimeSamples=None):
    listener = self.ikPlanner.getManipIKListener()
    msg = self.setupMessage(constraints, endPoseName, nominalPoseName, seedPoseName, additionalTimeSamples)
    lcmUtils.publish('IK_REQUEST', msg)
    ikplan = listener.waitForResponse(timeout=12000)
    listener.finish()

    endPose = [0] * self.ikPlanner.jointController.numberOfJoints
    if ikplan.num_states>0:
      endPose[len(endPose)-len(ikplan.plan[ikplan.num_states-1].joint_position):] = ikplan.plan[ikplan.num_states-1].joint_position
      info=ikplan.plan_info[ikplan.num_states-1]
    else:
      info = -1
    self.ikPlanner.ikServer.infoFunc(info)
    return endPose, info

  def processTraj(self, constraints, endPoseName="", nominalPoseName="", seedPoseName="", additionalTimeSamples=None):
    # Temporary fix / HACK / TODO (should be done in exotica_json)
    largestTspan = [0, 0]
    for constraintIndex, _ in enumerate(constraints):
      # Get tspan extend to normalise time-span
      if np.isfinite(constraints[constraintIndex].tspan[0]) and np.isfinite(constraints[constraintIndex].tspan[1]):
        largestTspan[0] = constraints[constraintIndex].tspan[0] if (constraints[constraintIndex].tspan[0] < largestTspan[0]) else largestTspan[0]
        largestTspan[1] = constraints[constraintIndex].tspan[1] if (constraints[constraintIndex].tspan[1] > largestTspan[1]) else largestTspan[1]

    # Temporary fix / HACK/ TODO to normalise time spans
    for constraintIndex, _ in enumerate(constraints):
      if np.isfinite(constraints[constraintIndex].tspan[0]) and np.isfinite(constraints[constraintIndex].tspan[1]):
        if largestTspan[1] != 0:
          constraints[constraintIndex].tspan[0] = constraints[constraintIndex].tspan[0] / largestTspan[1]
          constraints[constraintIndex].tspan[1] = constraints[constraintIndex].tspan[1] / largestTspan[1]

    listener = self.ikPlanner.getManipPlanListener()
    msg = self.setupMessage(constraints, endPoseName, nominalPoseName, seedPoseName, additionalTimeSamples)
    lcmUtils.publish('PLANNER_REQUEST', msg)
    lastManipPlan = listener.waitForResponse(timeout=20000)
    listener.finish()

    self.ikPlanner.ikServer.infoFunc(lastManipPlan.plan_info[0])
    return lastManipPlan, lastManipPlan.plan_info[0]

  def processAddPose(self, pose, poseName):
    self.poses[poseName]=list(pose);

  def processAffordances(self):
      affs = self.affordanceManager.getCollisionAffordances()
      s='['
      first=True
      for aff in affs:
        des=aff.getDescription()
        classname=des['classname'];
        if first:
          s+='{'
        else:
          s+='\n,{'
        first=False
        s+='"classname":"'+classname+'"'
        s+=',"name":"'+des['Name']+'"'
        s+=',"uuid":"'+des['uuid']+'"'
        s+=',"pose": {"position":{"__ndarray__":'+repr(des['pose'][0].tolist())+'},"quaternion":{"__ndarray__":'+repr(des['pose'][1].tolist())+'}}'
        if self.affordanceManager.affordanceUpdater is not None: # attached collision object / frameSync
          if des['Name'] in self.affordanceManager.affordanceUpdater.attachedAffordances:
            s+=',"attachedTo":"'+self.affordanceManager.affordanceUpdater.attachedAffordances[des['Name']]+'"'
          else: # it's not attached
            s+=',"attachedTo":"__world__"' # __world__ means it's a fixed collision object (sometimes called world or map - we use __world__ here)
        else: # no affordanceUpdater - so no attached collision objects either
          s+=',"attachedTo":"__world__"'
        if classname=='MeshAffordanceItem':
          s+=',"filename":"'+aff.getMeshManager().getFilesystemFilename(des['Filename'])+'"'
        if classname=='SphereAffordanceItem':
          s+=',"radius":'+repr(des['Radius'])
        if classname=='CylinderAffordanceItem' or classname=='CapsuleAffordanceItem':
          s+=',"radius":'+repr(des['Radius'])
          s+=',"length":'+repr(des['Length'])
        if classname=='BoxAffordanceItem':
          s+=',"dimensions":'+repr(des['Dimensions'])
        if classname=='CapsuleRingAffordanceItem':
          s+=',"radius":'+repr(des['Radius'])
          s+=',"tube_radius":'+repr(des['Tube Radius'])
          s+=',"segments":'+repr(des['Segments'])
        s+='}'
      s=s+']'
      return s

import ikplanner
<|endoftext|>


<commit_before>from qgl2.qgl2 import concur, qbit, qgl2decl, sequence, pulse, qgl2main
from qgl2.qgl1 import Qubit, X90, Y90

@qgl2decl
def func_c(q: qbit):

	for i in range(0,3):
 		func_b(q)

@qgl2decl
def func_b(q: qbit):

	for i in range(0,3):
 		func_a(q)

@qgl2decl
def func_a(q: qbit) -> pulse:

	for i in range(0,3):
 		X90(q)

@qgl2main
def main():

    q = Qubit("1")
    func_c(q)

<commit_msg>Example test file for nested for loops. 4/25/16 code base supports one next, but not two.<commit_after>from qgl2.qgl2 import concur, qbit, qgl2decl, sequence, pulse, qgl2main
from qgl2.qgl1 import QubitFactory, X90, Y90

@qgl2decl
def func_c(q: qbit):

	for i in range(0,3):
 		func_b(q)

@qgl2decl
def func_b(q: qbit):

	for i in range(0,3):
 		func_a(q)

@qgl2decl
def func_a(q: qbit) -> pulse:

	for i in range(0,3):
 		X90(q)

@qgl2main
def main():

    q = Qubit("1")
    func_c(q)

<|endoftext|>


<commit_before>from pymisp import MISPAttribute, MISPEvent, MISPObject
import json
import requests

misperrors = {'error': 'Error'}
mispattributes = {'input': ['domain', 'hostname', 'ip-src', 'ip-dst', 'md5', 'sha256', 'url'],
                  'output': ['url', 'filename', 'md5', 'sha256'],
                  'format': 'misp_standard'}
moduleinfo = {'version': '0.1', 'author': 'Christian Studer',
              'description': 'Query of the URLhaus API to get additional information about some attributes.',
              'module-type': ['expansion', 'hover']}
moduleconfig = []

file_keys = ('filename', 'response_size', 'response_md5', 'response_sha256')
file_relations = ('filename', 'size-in-bytes', 'md5', 'sha256')
vt_keys = ('result', 'link')
vt_types = ('text', 'link')
vt_relations = ('detection-ratio', 'permalink')

class URLhaus():
    def __init__(self):
        super(URLhaus, self).__init__()
        self.misp_event = MISPEvent()

    @staticmethod
    def _create_vt_object(virustotal):
        vt_object = MISPObject('virustotal-report')
        for key, vt_type, relation in zip(vt_keys, vt_types, vt_relations):
            vt_object.add_attribute(relation, **{'type': vt_type, 'value': virustotal[key]})
        return vt_object

    def get_result(self):
        event = json.loads(self.misp_event.to_json())
        results = {key: event[key] for key in ('Attribute', 'Object') if (key in event and event[key])}
        return {'results': results}

class HostQuery(URLhaus):
    def __init__(self, attribute):
        super(HostQuery, self).__init__()
        self.attribute = MISPAttribute()
        self.attribute.from_dict(**attribute)
        self.url = 'https://urlhaus-api.abuse.ch/v1/host/'

    def query_api(self):
        response = requests.post(self.url, data={'host': self.attribute.value}).json()
        if 'urls' in response and response['urls']:
            for url in response['urls']:
                self.misp_event.add_attribute(type='url', value=url['url'])

class PayloadQuery(URLhaus):
    def __init__(self, attribute):
        super(PayloadQuery, self).__init__()
        self.attribute = MISPAttribute()
        self.attribute.from_dict(**attribute)
        self.url = 'https://urlhaus-api.abuse.ch/v1/payload/'

    def query_api(self):
        hash_type = self.attribute.type
        file_object = MISPObject('file')
        if hasattr(self.attribute, 'object_id') and hasattr(self.attribute, 'event_id') and self.attribute.event_id != '0':
            file_object.id = self.attribute.object_id
        response = requests.post(self.url, data={'{}_hash'.format(hash_type): self.attribute.value}).json()
        other_hash_type = 'md5' if hash_type == 'sha256' else 'sha256'
        for key, relation in zip(('{}_hash'.format(other_hash_type), 'file_size'), (other_hash_type, 'size-in-bytes')):
            if response[key]:
                file_object.add_attribute(relation, **{'type': relation, 'value': response[key]})
        if response['virustotal']:
            vt_object = self._create_vt_object(response['virustotal'])
            file_object.add_reference(vt_object.uuid, 'analyzed-with')
            self.misp_event.add_object(**vt_object)
        _filename_ = 'filename'
        for url in response['urls']:
            attribute = MISPAttribute()
            attribute.from_dict(type='url', value=url['url'])
            self.misp_event.add_attribute(**attribute)
            file_object.add_reference(attribute.uuid, 'retrieved-from')
            if url[_filename_]:
                file_object.add_attribute(_filename_, **{'type': _filename_, 'value': url[_filename_]})
        if any((file_object.attributes, file_object.references)):
            self.misp_event.add_object(**file_object)

class UrlQuery(URLhaus):
    def __init__(self, attribute):
        super(UrlQuery, self).__init__()
        self.attribute = MISPAttribute()
        self.attribute.from_dict(**attribute)
        self.url = 'https://urlhaus-api.abuse.ch/v1/url/'

    @staticmethod
    def _create_file_object(payload):
        file_object = MISPObject('file')
        for key, relation in zip(file_keys, file_relations):
            if payload[key]:
                file_object.add_attribute(relation, **{'type': relation, 'value': payload[key]})
        return file_object

    def query_api(self):
        response = requests.post(self.url, data={'url': self.attribute.value}).json()
        if 'payloads' in response and response['payloads']:
            for payload in response['payloads']:
                file_object = self._create_file_object(payload)
                if payload['virustotal']:
                    vt_object = self._create_vt_object(payload['virustotal'])
                    file_object.add_reference(vt_object.uuid, 'analyzed-with')
                    self.misp_event.add_object(**vt_object)
                if any((file_object.attributes, file_object.references)):
                    self.misp_event.add_object(**file_object)

_misp_type_mapping = {'url': UrlQuery, 'md5': PayloadQuery, 'sha256': PayloadQuery,
                      'domain': HostQuery, 'hostname': HostQuery,
                      'ip-src': HostQuery, 'ip-dst': HostQuery}

def handler(q=False):
    if q is False:
        return False
    request = json.loads(q)
    attribute = request['attribute']
    urlhaus_parser = _misp_type_mapping[attribute['type']](attribute)
    urlhaus_parser.query_api()
    return urlhaus_parser.get_result()

def introspection():
    return mispattributes

def version():
    moduleinfo['config'] = moduleconfig
    return moduleinfo
<commit_msg>fix: Catching errors in the reponse of the query to URLhaus<commit_after>from pymisp import MISPAttribute, MISPEvent, MISPObject
import json
import requests

misperrors = {'error': 'Error'}
mispattributes = {'input': ['domain', 'hostname', 'ip-src', 'ip-dst', 'md5', 'sha256', 'url'],
                  'output': ['url', 'filename', 'md5', 'sha256'],
                  'format': 'misp_standard'}
moduleinfo = {'version': '0.1', 'author': 'Christian Studer',
              'description': 'Query of the URLhaus API to get additional information about some attributes.',
              'module-type': ['expansion', 'hover']}
moduleconfig = []

file_keys = ('filename', 'response_size', 'response_md5', 'response_sha256')
file_relations = ('filename', 'size-in-bytes', 'md5', 'sha256')
vt_keys = ('result', 'link')
vt_types = ('text', 'link')
vt_relations = ('detection-ratio', 'permalink')

class URLhaus():
    def __init__(self):
        super(URLhaus, self).__init__()
        self.misp_event = MISPEvent()

    @staticmethod
    def _create_vt_object(virustotal):
        vt_object = MISPObject('virustotal-report')
        for key, vt_type, relation in zip(vt_keys, vt_types, vt_relations):
            vt_object.add_attribute(relation, **{'type': vt_type, 'value': virustotal[key]})
        return vt_object

    def get_result(self):
        event = json.loads(self.misp_event.to_json())
        results = {key: event[key] for key in ('Attribute', 'Object') if (key in event and event[key])}
        return {'results': results}

    def parse_error(self, query_status):
        if query_status == 'no_results':
            return {'error': f'No results found on URLhaus for this {self.attribute.type} attribute'}
        return {'error': f'Error encountered during the query of URLhaus: {query_status}'}

class HostQuery(URLhaus):
    def __init__(self, attribute):
        super(HostQuery, self).__init__()
        self.attribute = MISPAttribute()
        self.attribute.from_dict(**attribute)
        self.url = 'https://urlhaus-api.abuse.ch/v1/host/'

    def query_api(self):
        response = requests.post(self.url, data={'host': self.attribute.value}).json()
        if response['query_status'] != 'ok':
            return self.parse_error(response['query_status'])
        if 'urls' in response and response['urls']:
            for url in response['urls']:
                self.misp_event.add_attribute(type='url', value=url['url'])
        return self.get_result()

class PayloadQuery(URLhaus):
    def __init__(self, attribute):
        super(PayloadQuery, self).__init__()
        self.attribute = MISPAttribute()
        self.attribute.from_dict(**attribute)
        self.url = 'https://urlhaus-api.abuse.ch/v1/payload/'

    def query_api(self):
        hash_type = self.attribute.type
        file_object = MISPObject('file')
        if hasattr(self.attribute, 'object_id') and hasattr(self.attribute, 'event_id') and self.attribute.event_id != '0':
            file_object.id = self.attribute.object_id
        response = requests.post(self.url, data={'{}_hash'.format(hash_type): self.attribute.value}).json()
        if response['query_status'] != 'ok':
            return self.parse_error(response['query_status'])
        other_hash_type = 'md5' if hash_type == 'sha256' else 'sha256'
        for key, relation in zip(('{}_hash'.format(other_hash_type), 'file_size'), (other_hash_type, 'size-in-bytes')):
            if response[key]:
                file_object.add_attribute(relation, **{'type': relation, 'value': response[key]})
        if response['virustotal']:
            vt_object = self._create_vt_object(response['virustotal'])
            file_object.add_reference(vt_object.uuid, 'analyzed-with')
            self.misp_event.add_object(**vt_object)
        _filename_ = 'filename'
        for url in response['urls']:
            attribute = MISPAttribute()
            attribute.from_dict(type='url', value=url['url'])
            self.misp_event.add_attribute(**attribute)
            file_object.add_reference(attribute.uuid, 'retrieved-from')
            if url[_filename_]:
                file_object.add_attribute(_filename_, **{'type': _filename_, 'value': url[_filename_]})
        if any((file_object.attributes, file_object.references)):
            self.misp_event.add_object(**file_object)
        return self.get_result()

class UrlQuery(URLhaus):
    def __init__(self, attribute):
        super(UrlQuery, self).__init__()
        self.attribute = MISPAttribute()
        self.attribute.from_dict(**attribute)
        self.url = 'https://urlhaus-api.abuse.ch/v1/url/'

    @staticmethod
    def _create_file_object(payload):
        file_object = MISPObject('file')
        for key, relation in zip(file_keys, file_relations):
            if payload[key]:
                file_object.add_attribute(relation, **{'type': relation, 'value': payload[key]})
        return file_object

    def query_api(self):
        response = requests.post(self.url, data={'url': self.attribute.value}).json()
        if response['query_status'] != 'ok':
            return self.parse_error(response['query_status'])
        if 'payloads' in response and response['payloads']:
            for payload in response['payloads']:
                file_object = self._create_file_object(payload)
                if payload['virustotal']:
                    vt_object = self._create_vt_object(payload['virustotal'])
                    file_object.add_reference(vt_object.uuid, 'analyzed-with')
                    self.misp_event.add_object(**vt_object)
                if any((file_object.attributes, file_object.references)):
                    self.misp_event.add_object(**file_object)
        return self.get_result()

_misp_type_mapping = {'url': UrlQuery, 'md5': PayloadQuery, 'sha256': PayloadQuery,
                      'domain': HostQuery, 'hostname': HostQuery,
                      'ip-src': HostQuery, 'ip-dst': HostQuery}

def handler(q=False):
    if q is False:
        return False
    request = json.loads(q)
    attribute = request['attribute']
    urlhaus_parser = _misp_type_mapping[attribute['type']](attribute)
    return urlhaus_parser.query_api()

def introspection():
    return mispattributes

def version():
    moduleinfo['config'] = moduleconfig
    return moduleinfo
<|endoftext|>


<commit_before>import random
import torch
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F

#don't ask how I came up with these numbers

SIZE_H1 = 500
SIZE_H2 = 1000
SIZE_H3 = 600

class model(torch.nn.Module):
	"""Defines custom model
	Inherits from torch.nn.Module
	"""
	def __init__(self, dim_input, dim_output):

		super(model, self).__init__()
		self._dim_input = dim_input
		self._dim_output = dim_output

		'''Initialize nnet layers'''
		self._l1 = torch.nn.Linear(self._dim_input, SIZE_H1)
		self._l2 = torch.nn.Linear(SIZE_H1, SIZE_H2)
		self._l3 = torch.nn.Linear(SIZE_H2, SIZE_H3)
		self._l4 = torch.nn.Linear( SIZE_H3, self._dim_output)

	def forward(self,x):
		self._l1_out = F.relu(self._l1(x))
		self._l2_out = F.relu(self._l2(self._l1_out))
		self._l3_out = nn.BatchNorm1d(SIZE_H3)(self._l3(self._l2_out))
		self._out = F.tanh(self._l4(self._l3_out))

		return self._out

<commit_msg>Update mountain_cart_actor.py<commit_after>import random
import torch
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F
import numpy as np

#don't ask how I came up with these numbers

SIZE_H1 = 500
SIZE_H2 = 1000
SIZE_H3 = 600

class model(torch.nn.Module):
	"""Defines custom model
	Inherits from torch.nn.Module
	"""
	def __init__(self, dim_input, dim_output):

		super(model, self).__init__()
		self._dim_input = dim_input
		self._dim_output = dim_output

		'''Initialize nnet layers'''
		self._l1 = torch.nn.Linear(self._dim_input, SIZE_H1)
		self._l2 = torch.nn.Linear(SIZE_H1, SIZE_H2)
		self._l3 = torch.nn.Linear(SIZE_H2, SIZE_H3)
		self._l4 = torch.nn.Linear( SIZE_H3, self._dim_output)

	def forward(self,s_t, r_t, aux_array): #TODO: Add aux task support, experiment with inputting previous action as well
		x = Variable(torch.FloatTensor(np.concatenate((s_t,r_t), axis = 1)))
		self._l1_out = F.relu(self._l1(x))
		self._l2_out = F.relu(self._l2(self._l1_out))
		self._l3_out = nn.BatchNorm1d(SIZE_H3)(self._l3(self._l2_out))
		self._out = F.tanh(self._l4(self._l3_out))

		return self._out

<|endoftext|>


<commit_before>from django.db import models

class Person(models.Model):
    SHIRT_SIZES = (
        ('S', 'Small'),
        ('M', 'Medium'),
        ('L', 'Large'),
    )
    first_name = models.CharField(max_length=254)
    last_name = models.CharField(max_length=254)
    shirt_size = models.CharField(max_length=1, choices=SHIRT_SIZES)

class Musician(models.Model):
    id_musician = models.AutoField(primary_key=True)
    first_name = models.CharField(max_length=254)
    last_name = models.CharField(max_length=254)
    instrument = models.CharField(max_length=100)

class Album(models.Model):
    artist = models.ForeignKey(Musician)
    name = models.CharField(max_length=254)
    release_date = models.DateField()
    num_stars = models.IntegerField()

class Group(models.Model):
    name = models.CharField(max_length=128)
    members = models.ManyToManyField(Person, through='Membership')

    def __unicode__(self):
        return self.name

class Membership(models.Model):
    person = models.ForeignKey(Person)
    group = models.ForeignKey(Group)
    date_joined = models.DateField()
    invite_reason = models.CharField(max_length=64)

class Topping(models.Model):
    type = models.CharField(max_length=254)

class Pizza(models.Model):
    toppings = models.ManyToManyField(Topping)

## One to One field
class Place(models.Model):
    name = models.CharField(max_length=50)
    address = models.CharField(max_length=80)

    def __unicode__(self):              # __unicode__ on Python 2
        return "%s the place" % self.name

class Restaurant(models.Model):
    place = models.OneToOneField(Place, primary_key=True)
    serves_hot_dogs = models.BooleanField(default=False)
    serves_pizza = models.BooleanField(default=False)

    def __unicode__(self):              # __unicode__ on Python 2
        return "%s the restaurant" % self.place.name

class Waiter(models.Model):
    restaurant = models.ForeignKey(Restaurant)
    name = models.CharField(max_length=50)

    def __unicode__(self):              # __unicode__ on Python 2
        return "%s the waiter at %s" % (self.name, self.restaurant)

<commit_msg>Adding Meta fields<commit_after>from django.db import models

class Person(models.Model):
    SHIRT_SIZES = (
        ('S', 'Small'),
        ('M', 'Medium'),
        ('L', 'Large'),
    )
    first_name = models.CharField(max_length=254)
    last_name = models.CharField(max_length=254)
    shirt_size = models.CharField(max_length=1, choices=SHIRT_SIZES)

class Musician(models.Model):
    id_musician = models.AutoField(primary_key=True)
    first_name = models.CharField(max_length=254)
    last_name = models.CharField(max_length=254)
    instrument = models.CharField(max_length=100)

class Album(models.Model):
    artist = models.ForeignKey(Musician)
    name = models.CharField(max_length=254)
    release_date = models.DateField()
    num_stars = models.IntegerField()
    class Meta:
        ordering = ["release_date"]
        verbose_name_plural = "albums"

class Group(models.Model):
    name = models.CharField(max_length=128)
    members = models.ManyToManyField(Person, through='Membership')

    def __unicode__(self):
        return self.name

class Membership(models.Model):
    person = models.ForeignKey(Person)
    group = models.ForeignKey(Group)
    date_joined = models.DateField()
    invite_reason = models.CharField(max_length=64)

class Topping(models.Model):
    type = models.CharField(max_length=254)

class Pizza(models.Model):
    toppings = models.ManyToManyField(Topping)

## One to One field
class Place(models.Model):
    name = models.CharField(max_length=50)
    address = models.CharField(max_length=80)

    def __unicode__(self):              # __unicode__ on Python 2
        return "%s the place" % self.name

class Restaurant(models.Model):
    place = models.OneToOneField(Place, primary_key=True)
    serves_hot_dogs = models.BooleanField(default=False)
    serves_pizza = models.BooleanField(default=False)

    def __unicode__(self):              # __unicode__ on Python 2
        return "%s the restaurant" % self.place.name

class Waiter(models.Model):
    restaurant = models.ForeignKey(Restaurant)
    name = models.CharField(max_length=50)

    def __unicode__(self):              # __unicode__ on Python 2
        return "%s the waiter at %s" % (self.name, self.restaurant)

<|endoftext|>


<commit_before>"""Dependencies of Java Tink."""

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive", "http_file")

TINK_MAVEN_ARTIFACTS = [
    "com.google.protobuf:protobuf-javalite:3.19.3",
    "com.amazonaws:aws-java-sdk-core:1.12.182",
    "com.amazonaws:aws-java-sdk-kms:1.12.182",
    "androidx.annotation:annotation:1.3.0",
    "com.google.auto:auto-common:1.2.1",
    "com.google.auto.service:auto-service:1.0.1",
    "com.google.auto.service:auto-service-annotations:1.0.1",
    "com.google.api-client:google-api-client:1.33.2",
    "com.google.apis:google-api-services-cloudkms:v1-rev108-1.25.0",
    "com.google.auth:google-auth-library-oauth2-http:1.5.3",
    "com.google.code.findbugs:jsr305:3.0.1",
    "com.google.code.gson:gson:2.8.9",
    "com.google.errorprone:error_prone_annotations:2.10.0",
    "com.google.http-client:google-http-client:1.39.0",
    "com.google.http-client:google-http-client-gson:1.39.0",
    "com.google.oauth-client:google-oauth-client:1.30.1",
    "com.google.truth:truth:0.44",
    "joda-time:joda-time:2.10.3",
    "junit:junit:4.13",
    "org.conscrypt:conscrypt-openjdk-uber:2.4.0",
    "org.mockito:mockito-core:2.23.0",
    "org.ow2.asm:asm:7.0",
    "org.ow2.asm:asm-commons:7.0",
    "org.pantsbuild:jarjar:1.7.2",
]

def tink_java_deps():
    """Loads dependencies of Java Tink."""

    # Google PKI certs for connecting to GCP KMS
    if not native.existing_rule("google_root_pem"):
        http_file(
            name = "google_root_pem",
            executable = 0,
            urls = ["https://pki.goog/roots.pem"],
            sha256 = "9c9b9685ad319b9747c3fe69b46a61c11a0efabdfa09ca6a8b0c3da421036d27",
        )

    # Basic rules we need to add to bazel.
    if not native.existing_rule("bazel_skylib"):
        # Release from 2021-09-27.
        http_archive(
            name = "bazel_skylib",
            urls = [
                "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.1.1/bazel-skylib-1.1.1.tar.gz",
                "https://github.com/bazelbuild/bazel-skylib/releases/download/1.1.1/bazel-skylib-1.1.1.tar.gz",
            ],
            sha256 = "c6966ec828da198c5d9adbaa94c05e3a1c7f21bd012a0b29ba8ddbccb2c93b0d",
        )

    # -------------------------------------------------------------------------
    # Protobuf.
    # -------------------------------------------------------------------------
    # proto_library, cc_proto_library and java_proto_library rules implicitly
    # depend respectively on:
    #   * @com_google_protobuf//:proto
    #   * @com_google_protobuf//:cc_toolchain
    #   * @com_google_protobuf//:java_toolchain
    # This statement defines the @com_google_protobuf repo.
    if not native.existing_rule("com_google_protobuf"):
        # Release from 2021-06-08.
        http_archive(
            name = "com_google_protobuf",
            strip_prefix = "protobuf-3.19.3",
            urls = ["https://github.com/protocolbuffers/protobuf/archive/v3.19.3.zip"],
            sha256 = "6b6bf5cd8d0cca442745c4c3c9f527c83ad6ef35a405f64db5215889ac779b42",
        )

    # -------------------------------------------------------------------------
    # Remote Build Execution (RBE).
    # -------------------------------------------------------------------------
    if not native.existing_rule("bazel_toolchains"):
        # Latest bazel_toolchains package on 2021-10-13.
        http_archive(
            name = "bazel_toolchains",
            sha256 = "179ec02f809e86abf56356d8898c8bd74069f1bd7c56044050c2cd3d79d0e024",
            strip_prefix = "bazel-toolchains-4.1.0",
            urls = [
                "https://mirror.bazel.build/github.com/bazelbuild/bazel-toolchains/releases/download/4.1.0/bazel-toolchains-4.1.0.tar.gz",
                "https://github.com/bazelbuild/bazel-toolchains/releases/download/4.1.0/bazel-toolchains-4.1.0.tar.gz",
            ],
        )

    # -------------------------------------------------------------------------
    # Transitive Maven artifact resolution and publishing rules for Bazel.
    # -------------------------------------------------------------------------
    if not native.existing_rule("rules_jvm_external"):
        # Release from 2021-11-24
        http_archive(
            name = "rules_jvm_external",
            strip_prefix = "rules_jvm_external-4.2",
            sha256 = "cd1a77b7b02e8e008439ca76fd34f5b07aecb8c752961f9640dea15e9e5ba1ca",
            url = "https://github.com/bazelbuild/rules_jvm_external/archive/4.2.zip",
        )

    # -------------------------------------------------------------------------
    # Android rules for Bazel.
    # -------------------------------------------------------------------------
    if not native.existing_rule("build_bazel_rules_android"):
        # Last release from 2018-08-07.
        http_archive(
            name = "build_bazel_rules_android",
            urls = ["https://github.com/bazelbuild/rules_android/archive/refs/tags/v0.1.1.zip"],
            sha256 = "cd06d15dd8bb59926e4d65f9003bfc20f9da4b2519985c27e190cddc8b7a7806",
            strip_prefix = "rules_android-0.1.1",
        )

    # -------------------------------------------------------------------------
    # Wycheproof.
    # -------------------------------------------------------------------------
    if not native.existing_rule("wycheproof"):
        # Commit from 2019-12-17
        http_archive(
            name = "wycheproof",
            strip_prefix = "wycheproof-d8ed1ba95ac4c551db67f410c06131c3bc00a97c",
            url = "https://github.com/google/wycheproof/archive/d8ed1ba95ac4c551db67f410c06131c3bc00a97c.zip",
            sha256 = "eb1d558071acf1aa6d677d7f1cabec2328d1cf8381496c17185bd92b52ce7545",
        )
<commit_msg>Update Conscrypt version to 2.5.2.<commit_after>"""Dependencies of Java Tink."""

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive", "http_file")

TINK_MAVEN_ARTIFACTS = [
    "com.google.protobuf:protobuf-javalite:3.19.3",
    "com.amazonaws:aws-java-sdk-core:1.12.182",
    "com.amazonaws:aws-java-sdk-kms:1.12.182",
    "androidx.annotation:annotation:1.3.0",
    "com.google.auto:auto-common:1.2.1",
    "com.google.auto.service:auto-service:1.0.1",
    "com.google.auto.service:auto-service-annotations:1.0.1",
    "com.google.api-client:google-api-client:1.33.2",
    "com.google.apis:google-api-services-cloudkms:v1-rev108-1.25.0",
    "com.google.auth:google-auth-library-oauth2-http:1.5.3",
    "com.google.code.findbugs:jsr305:3.0.1",
    "com.google.code.gson:gson:2.8.9",
    "com.google.errorprone:error_prone_annotations:2.10.0",
    "com.google.http-client:google-http-client:1.39.0",
    "com.google.http-client:google-http-client-gson:1.39.0",
    "com.google.oauth-client:google-oauth-client:1.30.1",
    "com.google.truth:truth:0.44",
    "joda-time:joda-time:2.10.3",
    "junit:junit:4.13",
    "org.conscrypt:conscrypt-openjdk-uber:2.5.2",
    "org.mockito:mockito-core:2.23.0",
    "org.ow2.asm:asm:7.0",
    "org.ow2.asm:asm-commons:7.0",
    "org.pantsbuild:jarjar:1.7.2",
]

def tink_java_deps():
    """Loads dependencies of Java Tink."""

    # Google PKI certs for connecting to GCP KMS
    if not native.existing_rule("google_root_pem"):
        http_file(
            name = "google_root_pem",
            executable = 0,
            urls = ["https://pki.goog/roots.pem"],
            sha256 = "9c9b9685ad319b9747c3fe69b46a61c11a0efabdfa09ca6a8b0c3da421036d27",
        )

    # Basic rules we need to add to bazel.
    if not native.existing_rule("bazel_skylib"):
        # Release from 2021-09-27.
        http_archive(
            name = "bazel_skylib",
            urls = [
                "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.1.1/bazel-skylib-1.1.1.tar.gz",
                "https://github.com/bazelbuild/bazel-skylib/releases/download/1.1.1/bazel-skylib-1.1.1.tar.gz",
            ],
            sha256 = "c6966ec828da198c5d9adbaa94c05e3a1c7f21bd012a0b29ba8ddbccb2c93b0d",
        )

    # -------------------------------------------------------------------------
    # Protobuf.
    # -------------------------------------------------------------------------
    # proto_library, cc_proto_library and java_proto_library rules implicitly
    # depend respectively on:
    #   * @com_google_protobuf//:proto
    #   * @com_google_protobuf//:cc_toolchain
    #   * @com_google_protobuf//:java_toolchain
    # This statement defines the @com_google_protobuf repo.
    if not native.existing_rule("com_google_protobuf"):
        # Release from 2021-06-08.
        http_archive(
            name = "com_google_protobuf",
            strip_prefix = "protobuf-3.19.3",
            urls = ["https://github.com/protocolbuffers/protobuf/archive/v3.19.3.zip"],
            sha256 = "6b6bf5cd8d0cca442745c4c3c9f527c83ad6ef35a405f64db5215889ac779b42",
        )

    # -------------------------------------------------------------------------
    # Remote Build Execution (RBE).
    # -------------------------------------------------------------------------
    if not native.existing_rule("bazel_toolchains"):
        # Latest bazel_toolchains package on 2021-10-13.
        http_archive(
            name = "bazel_toolchains",
            sha256 = "179ec02f809e86abf56356d8898c8bd74069f1bd7c56044050c2cd3d79d0e024",
            strip_prefix = "bazel-toolchains-4.1.0",
            urls = [
                "https://mirror.bazel.build/github.com/bazelbuild/bazel-toolchains/releases/download/4.1.0/bazel-toolchains-4.1.0.tar.gz",
                "https://github.com/bazelbuild/bazel-toolchains/releases/download/4.1.0/bazel-toolchains-4.1.0.tar.gz",
            ],
        )

    # -------------------------------------------------------------------------
    # Transitive Maven artifact resolution and publishing rules for Bazel.
    # -------------------------------------------------------------------------
    if not native.existing_rule("rules_jvm_external"):
        # Release from 2021-11-24
        http_archive(
            name = "rules_jvm_external",
            strip_prefix = "rules_jvm_external-4.2",
            sha256 = "cd1a77b7b02e8e008439ca76fd34f5b07aecb8c752961f9640dea15e9e5ba1ca",
            url = "https://github.com/bazelbuild/rules_jvm_external/archive/4.2.zip",
        )

    # -------------------------------------------------------------------------
    # Android rules for Bazel.
    # -------------------------------------------------------------------------
    if not native.existing_rule("build_bazel_rules_android"):
        # Last release from 2018-08-07.
        http_archive(
            name = "build_bazel_rules_android",
            urls = ["https://github.com/bazelbuild/rules_android/archive/refs/tags/v0.1.1.zip"],
            sha256 = "cd06d15dd8bb59926e4d65f9003bfc20f9da4b2519985c27e190cddc8b7a7806",
            strip_prefix = "rules_android-0.1.1",
        )

    # -------------------------------------------------------------------------
    # Wycheproof.
    # -------------------------------------------------------------------------
    if not native.existing_rule("wycheproof"):
        # Commit from 2019-12-17
        http_archive(
            name = "wycheproof",
            strip_prefix = "wycheproof-d8ed1ba95ac4c551db67f410c06131c3bc00a97c",
            url = "https://github.com/google/wycheproof/archive/d8ed1ba95ac4c551db67f410c06131c3bc00a97c.zip",
            sha256 = "eb1d558071acf1aa6d677d7f1cabec2328d1cf8381496c17185bd92b52ce7545",
        )
<|endoftext|>