<commit_before># Copyright (c) 2010-2012, GEM Foundation.
#
# OpenQuake is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# OpenQuake is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with OpenQuake.  If not, see <http://www.gnu.org/licenses/>.

"""Custom Django field and formfield types (for models and forms."""

import re
try:
    import simplejson as json
except ImportError:
    import json
try:
    import cPickle as pickle
except ImportError:
    import pickle

from django.contrib.gis import forms
from django.contrib.gis.db import models as djm

#: regex for splitting string lists on whitespace and/or commas
ARRAY_RE = re.compile('[\s,]+')

# Disable pylint for 'Too many public methods'
# pylint: disable=R0904

class FloatArrayFormField(forms.Field):
    """Form field for properly handling float arrays/lists."""

    def clean(self, value):
        """Try to coerce either a string list of values (separated by
        whitespace and/or commas or a list/tuple of values to a list of
        floats. If unsuccessful, raise a
        :exc:`django.forms.ValidationError`
        """
        if isinstance(value, (tuple, list)):
            try:
                value = [float(x) for x in value]
            except (TypeError, ValueError):
                raise forms.ValidationError(
                    'Could not coerce sequence values to `float` values'
                )
        elif isinstance(value, str):
            # it could be a string list, like this: "1, 2,3 , 4 5"
            # try to convert it to a an actual list of floats
            if len(value) == 0:
                # It's an empty string list
                value = []
            else:
                try:
                    value = [float(x) for x in ARRAY_RE.split(value)]
                except ValueError:
                    raise forms.ValidationError(
                        'Could not coerce `str` to a list of `float` values'
                    )
        else:
            raise forms.ValidationError(
                'Could not convert value to `list` of `float` values: %s'
                % value
            )
        return value

class PickleFormField(forms.Field):
    """Form field for Python objects which are pickle and saved to the
    database."""

    def clean(self, value):
        """We assume that the Python value specified for this field is exactly
        what we want to pickle and save to the database.

        The value will not modified.
        """
        return value

class FloatArrayField(djm.Field):
    """This field models a postgres `float` array."""

    def db_type(self, connection):
        return 'float[]'

    def get_prep_value(self, value):
        if value is None:
            return None

        # Normally, the value passed in here will be a list.
        # It could also be a string list, each separated by comma/whitespace.
        if isinstance(value, str):
            if len(value) == 0:
                # It's an empty string list
                value = []
            else:
                # try to coerce the string to a list of floats
                value = [float(x) for x in ARRAY_RE.split(value)]
                # If there's an exception here, just let it be raised.
        return "{" + ', '.join(str(v) for v in value) + "}"

    def formfield(self, **kwargs):
        """Specify a custom form field type so forms know how to handle fields
        of this type.
        """
        defaults = {'form_class': FloatArrayFormField}
        defaults.update(kwargs)
        return super(FloatArrayField, self).formfield(**defaults)

class CharArrayField(djm.Field):
    """This field models a postgres `varchar` array."""

    def db_type(self, _connection):
        return 'varchar[]'

    def get_prep_value(self, value):
        """Return data in a format that has been prepared for use as a
        parameter in a query.

        :param value: sequence of string values to be saved in a varchar[]
            field
        :type value: list or tuple

        >>> caf = CharArrayField()
        >>> caf.get_prep_value(['foo', 'bar', 'baz123'])
        '{"foo", "bar", "baz123"}'
        """
        if value is not None:
            return '{' + ', '.join('"%s"' % str(v) for v in value) + '}'
        else:
            return None

class PickleField(djm.Field):
    """Field for transparent pickling and unpickling of python objects."""

    __metaclass__ = djm.SubfieldBase

    SUPPORTED_BACKENDS = set((
        'django.contrib.gis.db.backends.postgis',
        'django.db.backends.postgresql_psycopg2'
    ))

    def db_type(self, connection):
        """Return "bytea" as postgres' column type."""
        assert connection.settings_dict['ENGINE'] in self.SUPPORTED_BACKENDS
        return 'bytea'

    def to_python(self, value):
        """Unpickle the value."""
        if isinstance(value, (buffer, str, bytearray)) and value:
            import nose; nose.tools.set_trace()
            return pickle.loads(str(value))
        else:
            return value

    def get_prep_value(self, value):
        """Pickle the value."""
        return bytearray(pickle.dumps(value, pickle.HIGHEST_PROTOCOL))

    def formfield(self, **kwargs):
        """Specify a custom form field type so forms don't treat this as a
        default type (such as a string). Any Python object is valid for this
        field type.
        """
        defaults = {'form_class': PickleFormField}
        defaults.update(kwargs)
        return super(PickleField, self).formfield(**defaults)

class DictField(PickleField):
    """Field for storing Python `dict` objects (or a JSON text representation.
    """

    def to_python(self, value):
        """The value of a DictField can obviously be a `dict`. The value can
        also be specified as a JSON string. If it is, convert it to a `dict`.
        """
        if isinstance(value, str):
            try:
                value = json.loads(value)
            except ValueError:
                # This string is not JSON.
                value = super(DictField, self).to_python(value)
        else:
            value = super(DictField, self).to_python(value)

        return value

class OqNullBooleanField(djm.NullBooleanField):
    """
    A `NullBooleanField` that can convert meaningful strings to boolean
    values (in the case of config file parameters).
    """

    def to_python(self, value):
        """
        If ``value`` is a `str`, try to extract some boolean value from it.
        """
        if isinstance(value, str):
            if value.lower() in ('t', 'true', 'y', 'yes'):
                value = True
            elif value.lower() in ('f', 'false', 'n', 'no'):
                value = False
        # otherwise, it will just get cast to a bool, which could produce some
        # strange results
        value = super(OqNullBooleanField, self).to_python(value)
        return value
<commit_msg>Removed a breakpoint; oops.<commit_after># Copyright (c) 2010-2012, GEM Foundation.
#
# OpenQuake is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# OpenQuake is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with OpenQuake.  If not, see <http://www.gnu.org/licenses/>.

"""Custom Django field and formfield types (for models and forms."""

import re
try:
    import simplejson as json
except ImportError:
    import json
try:
    import cPickle as pickle
except ImportError:
    import pickle

from django.contrib.gis import forms
from django.contrib.gis.db import models as djm

#: regex for splitting string lists on whitespace and/or commas
ARRAY_RE = re.compile('[\s,]+')

# Disable pylint for 'Too many public methods'
# pylint: disable=R0904

class FloatArrayFormField(forms.Field):
    """Form field for properly handling float arrays/lists."""

    def clean(self, value):
        """Try to coerce either a string list of values (separated by
        whitespace and/or commas or a list/tuple of values to a list of
        floats. If unsuccessful, raise a
        :exc:`django.forms.ValidationError`
        """
        if isinstance(value, (tuple, list)):
            try:
                value = [float(x) for x in value]
            except (TypeError, ValueError):
                raise forms.ValidationError(
                    'Could not coerce sequence values to `float` values'
                )
        elif isinstance(value, str):
            # it could be a string list, like this: "1, 2,3 , 4 5"
            # try to convert it to a an actual list of floats
            if len(value) == 0:
                # It's an empty string list
                value = []
            else:
                try:
                    value = [float(x) for x in ARRAY_RE.split(value)]
                except ValueError:
                    raise forms.ValidationError(
                        'Could not coerce `str` to a list of `float` values'
                    )
        else:
            raise forms.ValidationError(
                'Could not convert value to `list` of `float` values: %s'
                % value
            )
        return value

class PickleFormField(forms.Field):
    """Form field for Python objects which are pickle and saved to the
    database."""

    def clean(self, value):
        """We assume that the Python value specified for this field is exactly
        what we want to pickle and save to the database.

        The value will not modified.
        """
        return value

class FloatArrayField(djm.Field):
    """This field models a postgres `float` array."""

    def db_type(self, connection):
        return 'float[]'

    def get_prep_value(self, value):
        if value is None:
            return None

        # Normally, the value passed in here will be a list.
        # It could also be a string list, each separated by comma/whitespace.
        if isinstance(value, str):
            if len(value) == 0:
                # It's an empty string list
                value = []
            else:
                # try to coerce the string to a list of floats
                value = [float(x) for x in ARRAY_RE.split(value)]
                # If there's an exception here, just let it be raised.
        return "{" + ', '.join(str(v) for v in value) + "}"

    def formfield(self, **kwargs):
        """Specify a custom form field type so forms know how to handle fields
        of this type.
        """
        defaults = {'form_class': FloatArrayFormField}
        defaults.update(kwargs)
        return super(FloatArrayField, self).formfield(**defaults)

class CharArrayField(djm.Field):
    """This field models a postgres `varchar` array."""

    def db_type(self, _connection):
        return 'varchar[]'

    def get_prep_value(self, value):
        """Return data in a format that has been prepared for use as a
        parameter in a query.

        :param value: sequence of string values to be saved in a varchar[]
            field
        :type value: list or tuple

        >>> caf = CharArrayField()
        >>> caf.get_prep_value(['foo', 'bar', 'baz123'])
        '{"foo", "bar", "baz123"}'
        """
        if value is not None:
            return '{' + ', '.join('"%s"' % str(v) for v in value) + '}'
        else:
            return None

class PickleField(djm.Field):
    """Field for transparent pickling and unpickling of python objects."""

    __metaclass__ = djm.SubfieldBase

    SUPPORTED_BACKENDS = set((
        'django.contrib.gis.db.backends.postgis',
        'django.db.backends.postgresql_psycopg2'
    ))

    def db_type(self, connection):
        """Return "bytea" as postgres' column type."""
        assert connection.settings_dict['ENGINE'] in self.SUPPORTED_BACKENDS
        return 'bytea'

    def to_python(self, value):
        """Unpickle the value."""
        if isinstance(value, (buffer, str, bytearray)) and value:
            return pickle.loads(str(value))
        else:
            return value

    def get_prep_value(self, value):
        """Pickle the value."""
        return bytearray(pickle.dumps(value, pickle.HIGHEST_PROTOCOL))

    def formfield(self, **kwargs):
        """Specify a custom form field type so forms don't treat this as a
        default type (such as a string). Any Python object is valid for this
        field type.
        """
        defaults = {'form_class': PickleFormField}
        defaults.update(kwargs)
        return super(PickleField, self).formfield(**defaults)

class DictField(PickleField):
    """Field for storing Python `dict` objects (or a JSON text representation.
    """

    def to_python(self, value):
        """The value of a DictField can obviously be a `dict`. The value can
        also be specified as a JSON string. If it is, convert it to a `dict`.
        """
        if isinstance(value, str):
            try:
                value = json.loads(value)
            except ValueError:
                # This string is not JSON.
                value = super(DictField, self).to_python(value)
        else:
            value = super(DictField, self).to_python(value)

        return value

class OqNullBooleanField(djm.NullBooleanField):
    """
    A `NullBooleanField` that can convert meaningful strings to boolean
    values (in the case of config file parameters).
    """

    def to_python(self, value):
        """
        If ``value`` is a `str`, try to extract some boolean value from it.
        """
        if isinstance(value, str):
            if value.lower() in ('t', 'true', 'y', 'yes'):
                value = True
            elif value.lower() in ('f', 'false', 'n', 'no'):
                value = False
        # otherwise, it will just get cast to a bool, which could produce some
        # strange results
        value = super(OqNullBooleanField, self).to_python(value)
        return value
<|endoftext|>


<commit_before># Copyright 2015 Google LLC
#
# 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.

"""Create / interact with Google Cloud Resource Manager connections."""

from google.cloud import _http

from google.cloud.resource_manager import __version__

_CLIENT_INFO = _http.CLIENT_INFO_TEMPLATE.format(__version__)

class Connection(_http.JSONConnection):
    """A connection to Google Cloud Resource Manager via the JSON REST API.

    :type client: :class:`~google.cloud.resource_manager.client.Client`
    :param client: The client that owns the current connection.
    """

    API_BASE_URL = 'https://cloudresourcemanager.googleapis.com'
    """The base of the API call URL."""

    API_VERSION = 'v1'
    """The version of the API, used in building the API call's URL."""

    API_URL_TEMPLATE = '{api_base_url}/{api_version}{path}'
    """A template for the URL of a particular API call."""

    _EXTRA_HEADERS = {
        _http.CLIENT_INFO_HEADER: _CLIENT_INFO,
    }
<commit_msg>Revert #6041. 'v1' API is not forward compatible. (#6047)<commit_after># Copyright 2015 Google LLC
#
# 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.

"""Create / interact with Google Cloud Resource Manager connections."""

from google.cloud import _http

from google.cloud.resource_manager import __version__

_CLIENT_INFO = _http.CLIENT_INFO_TEMPLATE.format(__version__)

class Connection(_http.JSONConnection):
    """A connection to Google Cloud Resource Manager via the JSON REST API.

    :type client: :class:`~google.cloud.resource_manager.client.Client`
    :param client: The client that owns the current connection.
    """

    API_BASE_URL = 'https://cloudresourcemanager.googleapis.com'
    """The base of the API call URL."""

    API_VERSION = 'v1beta1'
    """The version of the API, used in building the API call's URL."""

    API_URL_TEMPLATE = '{api_base_url}/{api_version}{path}'
    """A template for the URL of a particular API call."""

    _EXTRA_HEADERS = {
        _http.CLIENT_INFO_HEADER: _CLIENT_INFO,
    }
<|endoftext|>


<commit_before>from django.db import models

# Create your models here.
class ContactRequest(models.Model):
  PURPOSE_CHOICES = (
    ('', 'Where should we direct your message?'),
    ('FinTech Companies', (
      (1, 'Come to campus'),
      (2, 'Share ideas with students'),
      (3, 'Sponsor an independent study project'),
      (4, 'Recruit talent'),
      (5, 'Distribute job postings'),
      (6, 'Become a Wharton FinTech Sponsor'),
    )),
    ('Other',(
      (7, 'Author a blog post'),
      (8, 'Press Inquiries'),
      (9, 'Promote an event to Wharton FinTech members'),
      (10, 'Share information with Wharton FinTech members'),
    )),
    ('Feedback',(
      (11, 'General comments'),
      (12, 'Report an issue with the website'),
    )),
  )

  TO_EMAILS = {
     1: ['Sasha Dobrolioubov <adobro@wharton.upenn.edu>'],
     2: ['Sasha Dobrolioubov <adobro@wharton.upenn.edu>'],
     3: ['Sasha Dobrolioubov <adobro@wharton.upenn.edu>'],
     4: ['Richard Bhamb <bhamb@wharton.upenn.edu>'],
     5: ['Richard Bhamb <bhamb@wharton.upenn.edu>'],
     6: ['Daniel McAuley <dmcauley@wharton.upenn.edu>', 'Steve Weiner <weins@wharton.upenn.edu>'],
     7: ['Sasha Dobrolioubov <adobro@wharton.upenn.edu>'],
     8: ['Cameron Peake <cpeake@wharton.upenn.edu>'],
     9: ['Cameron Peake <cpeake@wharton.upenn.edu>'],
    10: ['Cameron Peake <cpeake@wharton.upenn.edu>'],
    11: ['Daniel McAuley <dmcauley@wharton.upenn.edu>', 'Steve Weiner <weins@wharton.upenn.edu>'],
    12: ['Brendan McManus <bmcmanus@wharton.upenn.edu>'],
  }
  CC_EMAILS = {
     1: ['Daniel McAuley <dmcauley@wharton.upenn.edu>', 'Steve Weiner <weins@wharton.upenn.edu>'],
     2: ['Daniel McAuley <dmcauley@wharton.upenn.edu>', 'Steve Weiner <weins@wharton.upenn.edu>'],
     3: ['Daniel McAuley <dmcauley@wharton.upenn.edu>', 'Steve Weiner <weins@wharton.upenn.edu>'],
     4: ['Daniel McAuley <dmcauley@wharton.upenn.edu>', 'Steve Weiner <weins@wharton.upenn.edu>'],
     5: ['Daniel McAuley <dmcauley@wharton.upenn.edu>', 'Steve Weiner <weins@wharton.upenn.edu>'],
     6: [],
     7: ['Daniel McAuley <dmcauley@wharton.upenn.edu>', 'Steve Weiner <weins@wharton.upenn.edu>'],
     8: ['Daniel McAuley <dmcauley@wharton.upenn.edu>', 'Steve Weiner <weins@wharton.upenn.edu>'],
     9: ['Daniel McAuley <dmcauley@wharton.upenn.edu>', 'Steve Weiner <weins@wharton.upenn.edu>'],
    10: ['Daniel McAuley <dmcauley@wharton.upenn.edu>', 'Steve Weiner <weins@wharton.upenn.edu>'],
    11: [],
    12: ['Daniel McAuley <dmcauley@wharton.upenn.edu>', 'Steve Weiner <weins@wharton.upenn.edu>'],
  }

  name = models.CharField(max_length=128)
  email = models.EmailField(max_length=128)
  purpose = models.SmallIntegerField(choices=PURPOSE_CHOICES)
  message = models.TextField()
  url = models.URLField(max_length=254, blank=True)
  ip_address = models.GenericIPAddressField(null=True, blank=True)
  created = models.DateTimeField(auto_now_add=True)
  edited = models.DateTimeField(auto_now=True)

  class Meta:
    ordering = ['-created']
<commit_msg>new directors added to email list<commit_after>from django.db import models

# Create your models here.
class ContactRequest(models.Model):
  PURPOSE_CHOICES = (
    ('', 'Where should we direct your message?'),
    ('FinTech Companies', (
      (1, 'Come to campus'),
      (2, 'Share ideas with students'),
      (3, 'Sponsor an independent study project'),
      (4, 'Recruit talent'),
      (5, 'Distribute job postings'),
      (6, 'Become a Wharton FinTech Sponsor'),
    )),
    ('Other',(
      (7, 'Author a blog post'),
      (8, 'Press Inquiries'),
      (9, 'Promote an event to Wharton FinTech members'),
      (10, 'Share information with Wharton FinTech members'),
    )),
    ('Feedback',(
      (11, 'General comments'),
      (12, 'Report an issue with the website'),
    )),
  )

  TO_EMAILS = {
     1: ['Sasha Dobrolioubov <adobro@wharton.upenn.edu>', 'Neha Goel <nehagoel@wharton.upenn.edu>'],
     2: ['Sasha Dobrolioubov <adobro@wharton.upenn.edu>', 'Neha Goel <nehagoel@wharton.upenn.edu>'],
     3: ['Sasha Dobrolioubov <adobro@wharton.upenn.edu>', 'Neha Goel <nehagoel@wharton.upenn.edu>'],
     4: ['Richard Bhamb <bhamb@wharton.upenn.edu>'],
     5: ['Richard Bhamb <bhamb@wharton.upenn.edu>'],
     6: ['Daniel McAuley <dmcauley@wharton.upenn.edu>', 'Steve Weiner <weins@wharton.upenn.edu>'],
     7: ['Sasha Dobrolioubov <adobro@wharton.upenn.edu>', 'Matthew Applegate <apmatt@wharton.upenn.edu>'],
     8: ['Cameron Peake <cpeake@wharton.upenn.edu>'],
     9: ['Cameron Peake <cpeake@wharton.upenn.edu>'],
    10: ['Cameron Peake <cpeake@wharton.upenn.edu>'],
    11: ['Daniel McAuley <dmcauley@wharton.upenn.edu>', 'Steve Weiner <weins@wharton.upenn.edu>'],
    12: ['Brendan McManus <bmcmanus@wharton.upenn.edu>'],
  }
  CC_EMAILS = {
     1: ['Daniel McAuley <dmcauley@wharton.upenn.edu>', 'Steve Weiner <weins@wharton.upenn.edu>'],
     2: ['Daniel McAuley <dmcauley@wharton.upenn.edu>', 'Steve Weiner <weins@wharton.upenn.edu>'],
     3: ['Daniel McAuley <dmcauley@wharton.upenn.edu>', 'Steve Weiner <weins@wharton.upenn.edu>'],
     4: ['Daniel McAuley <dmcauley@wharton.upenn.edu>', 'Steve Weiner <weins@wharton.upenn.edu>'],
     5: ['Daniel McAuley <dmcauley@wharton.upenn.edu>', 'Steve Weiner <weins@wharton.upenn.edu>'],
     6: [],
     7: ['Daniel McAuley <dmcauley@wharton.upenn.edu>', 'Steve Weiner <weins@wharton.upenn.edu>'],
     8: ['Daniel McAuley <dmcauley@wharton.upenn.edu>', 'Steve Weiner <weins@wharton.upenn.edu>'],
     9: ['Daniel McAuley <dmcauley@wharton.upenn.edu>', 'Steve Weiner <weins@wharton.upenn.edu>'],
    10: ['Daniel McAuley <dmcauley@wharton.upenn.edu>', 'Steve Weiner <weins@wharton.upenn.edu>'],
    11: [],
    12: ['Daniel McAuley <dmcauley@wharton.upenn.edu>', 'Steve Weiner <weins@wharton.upenn.edu>'],
  }

  name = models.CharField(max_length=128)
  email = models.EmailField(max_length=128)
  purpose = models.SmallIntegerField(choices=PURPOSE_CHOICES)
  message = models.TextField()
  url = models.URLField(max_length=254, blank=True)
  ip_address = models.GenericIPAddressField(null=True, blank=True)
  created = models.DateTimeField(auto_now_add=True)
  edited = models.DateTimeField(auto_now=True)

  class Meta:
    ordering = ['-created']
<|endoftext|>


<commit_before><commit_msg>One more commit to get the spotlight comments working<commit_after><|endoftext|>


<commit_before><commit_msg>Changes for issue SGS-179<commit_after><|endoftext|>


<commit_before>#!/usr/bin/env python3

from roboy import Roboy
from sys import stdin

from std_msgs.msg import String

import ast

class GuessingGame:
    def __init__(self):
        import os
        import shlex

        assert 'command' in os.environ
        self.roboy = Roboy(shlex.split(os.environ['command']))
        self.roboy.start()

    def ask(self, sentence):
        print('Asking: {0}'.format(sentence))
        ## see something
        rospy.wait_for_service('image_request')
        fetch_image = rospy.ServiceProxy("image_request", String)
        try:
            res = fetch_image()
            self.roboy.write(sentence, ast.literal_eval(res))
        except rospy.ServiceException as exc:
            rospy.loginfo("Service did not process request: " + str(exc))

    def read(self):
        return "\n".join([action['speak'] for action in self.roboy.read()])

    def run(self):
        print(self.read())
        for line in stdin:
            line = line.strip("\n")
            print(line)
            self.ask(line)
            print(self.read())

## @brief Initialization function of the node.
#
if __name__ == "__main__":
    rospy.init_node('guessing_game')

    rospy.loginfo('going to start roboy guessing game')

    game = GuessingGame()
    game.run()

    rospy.spin()<commit_msg>status<commit_after>#!/usr/bin/env python3

from roboy import Roboy
from sys import stdin

from std_msgs.msg import String

import ast
import rospy

class GuessingGame:
    def __init__(self):
        import os
        import shlex

        assert 'command' in os.environ
        self.roboy = Roboy(shlex.split(os.environ['command']))
        self.roboy.start()

    def ask(self, sentence):
        print('Asking: {0}'.format(sentence))
        ## see something
        rospy.wait_for_service('image_request')
        fetch_image = rospy.ServiceProxy("image_request", String)
        try:
            res = fetch_image()
            self.roboy.write(sentence, ast.literal_eval(res))
        except rospy.ServiceException as exc:
            rospy.loginfo("Service did not process request: " + str(exc))

    def read(self):
        return "\n".join([action['speak'] for action in self.roboy.read()])

    def run(self):
        print(self.read())
        for line in stdin:
            line = line.strip("\n")
            print(line)
            self.ask(line)
            print(self.read())

## @brief Initialization function of the node.
#
if __name__ == "__main__":
    rospy.init_node('guessing_game')

    rospy.loginfo('going to start roboy guessing game')

    game = GuessingGame()
    game.run()

    rospy.spin()<|endoftext|>


<commit_before>from flask import current_app, request, Blueprint
from flask.ext.discoverer import advertise
from .models import db, User
import requests
from datetime import datetime
from dateutil import parser
from sqlalchemy import exc, and_
from sqlalchemy.orm import load_only
import json

bp = Blueprint('orcid', __name__)

@advertise(scopes=[], rate_limit = [100, 3600*24])
@bp.route('/exchangeOAuthCode', methods=['GET'])
def get_access_token():
    '''Exchange 'code' for 'access_token' data'''
    payload = dict(request.args)
    if 'code' not in payload:
        raise Exception('Parameter code is missing')
    headers = {'Accept': 'application/json'}
    data = {
      'client_id': current_app.config['ORCID_CLIENT_ID'],
      'client_secret': current_app.config['ORCID_CLIENT_SECRET'],
      'code': payload['code'][0],
      'grant_type': 'authorization_code'
    }
    #print current_app.config['ORCID_OAUTH_ENDPOINT'], data, headers
    r = requests.post(current_app.config['ORCID_OAUTH_ENDPOINT'], data=data, headers=headers)

    # update/create user account
    data = r.json()
    if 'orcid' in data:
        u = db.session.query(User).filter_by(orcid_id=data['orcid']).first()
        if not u:
            u = User(orcid_id=data['orcid'], created=datetime.utcnow())
        u.updated = datetime.utcnow()
        u.access_token = data['access_token']
        # save the user
        db.session.begin_nested()
        try:
            db.session.add(u)
            db.session.commit()
        except exc.IntegrityError as e:
            db.session.rollback()
        # per PEP-0249 a transaction is always in progress
        db.session.commit()

    return r.text, r.status_code

@advertise(scopes=[], rate_limit = [1000, 3600*24])
@bp.route('/<orcid_id>/orcid-profile', methods=['GET', 'POST'])
def orcid_profile(orcid_id):
    '''Get/Set /[orcid-id]/orcid-profile - all communication exclusively in JSON'''
    payload, headers = check_request(request)
    if request.method == 'GET':
        r = requests.get(current_app.config['ORCID_API_ENDPOINT'] + '/' + orcid_id + '/orcid-profile',
                         headers=headers)
    else:
        r = requests.post(current_app.config['ORCID_API_ENDPOINT'] + '/' + orcid_id + '/orcid-profile',
                         json=payload, headers=headers)

    # save the profile data (just in case the user revokes access_token, we can still get the update
    # from our local data); however - normally the updater should grab the latest data from orcid
    update_profile(orcid_id, r.text)

    return r.text, r.status_code

@advertise(scopes=[], rate_limit = [1000, 3600*24])
@bp.route('/<orcid_id>/orcid-works', methods=['GET', 'POST', 'PUT'])
def orcid_works(orcid_id):
    '''Get/Set /[orcid-id]/orcid-works - all communication exclusively in JSON'''

    payload, headers = check_request(request)

    if request.method == 'GET':
        r = requests.get(current_app.config['ORCID_API_ENDPOINT'] + '/' + orcid_id + '/orcid-works',
                      headers=headers)
    elif request.method == 'PUT':
        r = requests.put(current_app.config['ORCID_API_ENDPOINT'] + '/' + orcid_id + '/orcid-works',
                      json=payload, headers=headers)
        update_profile(orcid_id)
    elif request.method == 'POST':
        r = requests.post(current_app.config['ORCID_API_ENDPOINT'] + '/' + orcid_id + '/orcid-works',
                      json=payload, headers=headers)
        update_profile(orcid_id)

    return r.text, r.status_code

@advertise(scopes=['ads-consumer:orcid'], rate_limit = [1000, 3600*24])
@bp.route('/export/<iso_datestring>', methods=['GET'])
def export(iso_datestring):
    '''Get the latest changes (as recorded in the ORCID)
    The optional argument latest_point is RFC3339, ie. '2008-09-03T20:56:35.450686Z'
    '''

    latest_point = parser.parse(iso_datestring) # RFC 3339 format

    payload = dict(request.args)
    allowed_fields = ['orcid_id', 'created', 'updated', 'profile']
    fields = payload.get('fields', allowed_fields)
    fields_to_load = list(set(fields) & set(allowed_fields))

    if len(fields_to_load) == 0:
        return json.dumps({'error': 'Wrong input values for fields: %s' % payload.get('fields')}), 404

    # poorman's version of paging, but it works because the time resolution is in
    # microseconds
    output = []
    recs = db.session.query(User).filter(User.updated >= latest_point) \
        .order_by(User.updated.asc()) \
        .limit(current_app.config.get('MAX_PROFILES_RETURNED', 10)) \
        .options(load_only(*fields_to_load)) \
        .all()

    for r in recs:
        o = {}
        for k in fields_to_load:
            v = getattr(r, k)
            if k == 'profile':
                try:
                    v = json.loads(unicode(v))
                except:
                    v = None
            if hasattr(v, 'isoformat'):
                v = v.isoformat()
            o[k] = v
        output.append(o)

    return json.dumps(output), 200

@advertise(scopes=['ads-consumer:orcid'], rate_limit = [1000, 3600*24])
@bp.route('/get-profile/<orcid_id>', methods=['GET'])
def get_profile(orcid_id):
    '''Fetches the latest orcid-profile'''

    u = db.session.query(User).filter_by(orcid_id=orcid_id).first()
    if not u:
        return json.dumps({'error': 'We do not have a record for: %s' % user_id}), 404

    if not u.access_token:
        return json.dumps({'error': 'We do not have access_token for: %s' % user_id}), 404

    out = u.toJSON()

    payload = dict(request.args)
    if payload.get('reload', False):
        h = {
             'Accept': 'application/json',
             'Authorization': 'Bearer %s' % u.access_token,
             'Content-Type': 'application/json'
             }

        r = requests.get(current_app.config['ORCID_API_ENDPOINT'] + '/' + orcid_id + '/orcid-profile',
                         headers=h)
        if r.status_code == 200:
            # update our record (but avoid setting the updated date)
            j = r.json()
            orcid_profile = json.dumps(j)
            db.session.begin_nested()
            try:
                db.session.add(u)
                db.session.commit()
                out['profile'] = j
            except exc.IntegrityError as e:
                db.session.rollback()
            # per PEP-0249 a transaction is always in progress
            db.session.commit()
        else:
            raise Exception('Orcid API returned err code (refreshing profile)')

    return json.dumps(out), 200

@advertise(scopes=[], rate_limit = [100, 3600*24])
@bp.route('/preferences/<orcid_id>', methods=['GET', 'POST'])
def preferences(orcid_id):
    '''Allows you to store/retrieve JSON data on the server side.
    It is always associated with the ORCID access token so there
    is no need for scope access
    '''

    # get the query data
    try:
        payload, headers = check_request(request)
    except Exception as e:
        return json.dumps({'msg': e.message or e.description}), 400

    access_token = headers['Authorization'][7:] # remove the 'Bearer:' thing

    if request.method == 'GET':
        u = db.session.query(User).filter(and_(User.orcid_id==orcid_id, User.access_token==access_token)).first()
        if not u:
            return '{}', 404 # not found
        return u.info or '{}', 200
    elif request.method == 'POST':
        d = json.dumps(payload)
        if len(d) > current_app.config.get('MAX_ALLOWED_JSON_SIZE', 1000):
            return json.dumps({'msg': 'You have exceeded the allowed storage limit, no data was saved'}), 400
        u = db.session.query(User).filter(and_(User.orcid_id==orcid_id, User.access_token==access_token)).first()
        if not u:
            return json.dumps({'error': 'We do not have a record for: %s' % orcid_id}), 404

        u.info = d

        db.session.begin_nested()
        try:
            db.session.merge(u)
            db.session.commit()
        except exc.IntegrityError:
            db.session.rollback()
            return json.dumps({'msg': 'We have hit a db error! The world is crumbling all around... (eh, btw, your data was not saved)'}), 500

        # per PEP-0249 a transaction is always in progress
        db.session.commit()
        return d, 200

def update_profile(orcid_id, data=None):
    """Inserts data into the user record and updates the 'updated'
    column with the most recent timestamp"""

    u = db.session.query(User).filter_by(orcid_id=orcid_id).first()
    if u:
        u.updated = datetime.utcnow()
        if data:
            u.profile = data
        # save the user
        db.session.begin_nested()
        try:
            db.session.add(u)
            db.session.commit()
        except exc.IntegrityError as e:
            db.session.rollback()
        # per PEP-0249 a transaction is always in progress
        db.session.commit()

def check_request(request):

    headers = dict(request.headers)
    if 'Orcid-Authorization' not in headers:
        raise Exception('Header Orcid-Authorization is missing')
    h = {
         'Accept': 'application/json',
         'Authorization': headers['Orcid-Authorization'],
         'Content-Type': 'application/json'
         }
    # transfer headers from the original
    #for x in ['Content-Type']:
    #    if x in headers:
    #        h[x] = headers[x]

    if 'Content-Type' in headers \
        and 'application/json' in headers['Content-Type'] \
        and request.method in ('POST', 'PUT'):
        payload = request.json
    else:
        payload = dict(request.args)
        payload.update(dict(request.form))

    return (payload, h)
<commit_msg>Update stored ORCID profile only if the response was 200; otherwise we risk saving garbage<commit_after>from flask import current_app, request, Blueprint
from flask.ext.discoverer import advertise
from .models import db, User
import requests
from datetime import datetime
from dateutil import parser
from sqlalchemy import exc, and_
from sqlalchemy.orm import load_only
import json

bp = Blueprint('orcid', __name__)

@advertise(scopes=[], rate_limit = [100, 3600*24])
@bp.route('/exchangeOAuthCode', methods=['GET'])
def get_access_token():
    '''Exchange 'code' for 'access_token' data'''
    payload = dict(request.args)
    if 'code' not in payload:
        raise Exception('Parameter code is missing')
    headers = {'Accept': 'application/json'}
    data = {
      'client_id': current_app.config['ORCID_CLIENT_ID'],
      'client_secret': current_app.config['ORCID_CLIENT_SECRET'],
      'code': payload['code'][0],
      'grant_type': 'authorization_code'
    }
    #print current_app.config['ORCID_OAUTH_ENDPOINT'], data, headers
    r = requests.post(current_app.config['ORCID_OAUTH_ENDPOINT'], data=data, headers=headers)

    # update/create user account
    data = r.json()
    if 'orcid' in data:
        u = db.session.query(User).filter_by(orcid_id=data['orcid']).first()
        if not u:
            u = User(orcid_id=data['orcid'], created=datetime.utcnow())
        u.updated = datetime.utcnow()
        u.access_token = data['access_token']
        # save the user
        db.session.begin_nested()
        try:
            db.session.add(u)
            db.session.commit()
        except exc.IntegrityError as e:
            db.session.rollback()
        # per PEP-0249 a transaction is always in progress
        db.session.commit()

    return r.text, r.status_code

@advertise(scopes=[], rate_limit = [1000, 3600*24])
@bp.route('/<orcid_id>/orcid-profile', methods=['GET', 'POST'])
def orcid_profile(orcid_id):
    '''Get/Set /[orcid-id]/orcid-profile - all communication exclusively in JSON'''
    payload, headers = check_request(request)
    if request.method == 'GET':
        r = requests.get(current_app.config['ORCID_API_ENDPOINT'] + '/' + orcid_id + '/orcid-profile',
                         headers=headers)
    else:
        r = requests.post(current_app.config['ORCID_API_ENDPOINT'] + '/' + orcid_id + '/orcid-profile',
                         json=payload, headers=headers)

    # save the profile data (just in case the user revokes access_token, we can still get the update
    # from our local data); however - normally the updater should grab the latest data from orcid
    if r.status_code == 200:
        update_profile(orcid_id, r.text)

    return r.text, r.status_code

@advertise(scopes=[], rate_limit = [1000, 3600*24])
@bp.route('/<orcid_id>/orcid-works', methods=['GET', 'POST', 'PUT'])
def orcid_works(orcid_id):
    '''Get/Set /[orcid-id]/orcid-works - all communication exclusively in JSON'''

    payload, headers = check_request(request)

    if request.method == 'GET':
        r = requests.get(current_app.config['ORCID_API_ENDPOINT'] + '/' + orcid_id + '/orcid-works',
                      headers=headers)
    elif request.method == 'PUT':
        r = requests.put(current_app.config['ORCID_API_ENDPOINT'] + '/' + orcid_id + '/orcid-works',
                      json=payload, headers=headers)
        update_profile(orcid_id)
    elif request.method == 'POST':
        r = requests.post(current_app.config['ORCID_API_ENDPOINT'] + '/' + orcid_id + '/orcid-works',
                      json=payload, headers=headers)
        update_profile(orcid_id)

    return r.text, r.status_code

@advertise(scopes=['ads-consumer:orcid'], rate_limit = [1000, 3600*24])
@bp.route('/export/<iso_datestring>', methods=['GET'])
def export(iso_datestring):
    '''Get the latest changes (as recorded in the ORCID)
    The optional argument latest_point is RFC3339, ie. '2008-09-03T20:56:35.450686Z'
    '''

    latest_point = parser.parse(iso_datestring) # RFC 3339 format

    payload = dict(request.args)
    allowed_fields = ['orcid_id', 'created', 'updated', 'profile']
    fields = payload.get('fields', allowed_fields)
    fields_to_load = list(set(fields) & set(allowed_fields))

    if len(fields_to_load) == 0:
        return json.dumps({'error': 'Wrong input values for fields: %s' % payload.get('fields')}), 404

    # poorman's version of paging, but it works because the time resolution is in
    # microseconds
    output = []
    recs = db.session.query(User).filter(User.updated >= latest_point) \
        .order_by(User.updated.asc()) \
        .limit(current_app.config.get('MAX_PROFILES_RETURNED', 10)) \
        .options(load_only(*fields_to_load)) \
        .all()

    for r in recs:
        o = {}
        for k in fields_to_load:
            v = getattr(r, k)
            if k == 'profile':
                try:
                    v = json.loads(unicode(v))
                except:
                    v = None
            if hasattr(v, 'isoformat'):
                v = v.isoformat()
            o[k] = v
        output.append(o)

    return json.dumps(output), 200

@advertise(scopes=['ads-consumer:orcid'], rate_limit = [1000, 3600*24])
@bp.route('/get-profile/<orcid_id>', methods=['GET'])
def get_profile(orcid_id):
    '''Fetches the latest orcid-profile'''

    u = db.session.query(User).filter_by(orcid_id=orcid_id).first()
    if not u:
        return json.dumps({'error': 'We do not have a record for: %s' % user_id}), 404

    if not u.access_token:
        return json.dumps({'error': 'We do not have access_token for: %s' % user_id}), 404

    out = u.toJSON()

    payload = dict(request.args)
    if payload.get('reload', False):
        h = {
             'Accept': 'application/json',
             'Authorization': 'Bearer %s' % u.access_token,
             'Content-Type': 'application/json'
             }

        r = requests.get(current_app.config['ORCID_API_ENDPOINT'] + '/' + orcid_id + '/orcid-profile',
                         headers=h)
        if r.status_code == 200:
            # update our record (but avoid setting the updated date)
            j = r.json()
            orcid_profile = json.dumps(j)
            db.session.begin_nested()
            try:
                db.session.add(u)
                db.session.commit()
                out['profile'] = j
            except exc.IntegrityError as e:
                db.session.rollback()
            # per PEP-0249 a transaction is always in progress
            db.session.commit()
        else:
            raise Exception('Orcid API returned err code (refreshing profile)')

    return json.dumps(out), 200

@advertise(scopes=[], rate_limit = [100, 3600*24])
@bp.route('/preferences/<orcid_id>', methods=['GET', 'POST'])
def preferences(orcid_id):
    '''Allows you to store/retrieve JSON data on the server side.
    It is always associated with the ORCID access token so there
    is no need for scope access
    '''

    # get the query data
    try:
        payload, headers = check_request(request)
    except Exception as e:
        return json.dumps({'msg': e.message or e.description}), 400

    access_token = headers['Authorization'][7:] # remove the 'Bearer:' thing

    if request.method == 'GET':
        u = db.session.query(User).filter(and_(User.orcid_id==orcid_id, User.access_token==access_token)).first()
        if not u:
            return '{}', 404 # not found
        return u.info or '{}', 200
    elif request.method == 'POST':
        d = json.dumps(payload)
        if len(d) > current_app.config.get('MAX_ALLOWED_JSON_SIZE', 1000):
            return json.dumps({'msg': 'You have exceeded the allowed storage limit, no data was saved'}), 400
        u = db.session.query(User).filter(and_(User.orcid_id==orcid_id, User.access_token==access_token)).first()
        if not u:
            return json.dumps({'error': 'We do not have a record for: %s' % orcid_id}), 404

        u.info = d

        db.session.begin_nested()
        try:
            db.session.merge(u)
            db.session.commit()
        except exc.IntegrityError:
            db.session.rollback()
            return json.dumps({'msg': 'We have hit a db error! The world is crumbling all around... (eh, btw, your data was not saved)'}), 500

        # per PEP-0249 a transaction is always in progress
        db.session.commit()
        return d, 200

def update_profile(orcid_id, data=None):
    """Inserts data into the user record and updates the 'updated'
    column with the most recent timestamp"""

    u = db.session.query(User).filter_by(orcid_id=orcid_id).first()
    if u:
        u.updated = datetime.utcnow()
        if data:
            u.profile = data
        # save the user
        db.session.begin_nested()
        try:
            db.session.add(u)
            db.session.commit()
        except exc.IntegrityError as e:
            db.session.rollback()
        # per PEP-0249 a transaction is always in progress
        db.session.commit()

def check_request(request):

    headers = dict(request.headers)
    if 'Orcid-Authorization' not in headers:
        raise Exception('Header Orcid-Authorization is missing')
    h = {
         'Accept': 'application/json',
         'Authorization': headers['Orcid-Authorization'],
         'Content-Type': 'application/json'
         }
    # transfer headers from the original
    #for x in ['Content-Type']:
    #    if x in headers:
    #        h[x] = headers[x]

    if 'Content-Type' in headers \
        and 'application/json' in headers['Content-Type'] \
        and request.method in ('POST', 'PUT'):
        payload = request.json
    else:
        payload = dict(request.args)
        payload.update(dict(request.form))

    return (payload, h)
<|endoftext|>


<commit_before><commit_msg>Fix fly2dplot<commit_after><|endoftext|>


<commit_before>from django.db import models, transaction

class OrderableQueryset(models.QuerySet):
    """
    Adds additional functionality to `Orderable.objects` and querysets.

    Provides access to the next and previous ordered object within the queryset.

    As a related manager this will provide the filtering automatically.
    """
    def before(self, orderable):
        return self.filter(sort_order__lt=orderable.sort_order).last()

    def after(self, orderable):
        return self.filter(sort_order__gt=orderable.sort_order).first()

    def set_orders(self, object_pks, *args, **kwargs):
        """
        Perform a mass update of sort_orders across the full queryset.
        Accepts a list, object_pks, of the intended order for the objects.

        Works as follows:
        - Compile a list of all sort orders in the queryset. Leave out anything that
          isn't in the object_pks list - this deals with pagination and any
          inconsistencies.
        - Get the maximum among all model object sort orders. Update the queryset to add
          it to all the existing sort order values. This lifts them 'out of the way' of
          unique_together clashes when setting the intended sort orders.
        - Set the sort order on each object. Use only sort_order values that the objects
          had before calling this method, so they get rearranged in place.
        Performs O(n) queries.
        """
        max_value = self.model.objects.count()
        objects_to_sort = self.filter(pk__in=object_pks)

        # Call list() on the values right away, so they don't get affected by the
        # update() later (since values_list() is lazy).
        orders = list(objects_to_sort.values_list('sort_order', flat=True))

        with transaction.atomic():
            objects_to_sort.update(sort_order=models.F('sort_order') + max_value)
            for i, pk in enumerate(object_pks):
                self.filter(pk=pk).update(sort_order=orders[i])

        # Return the operated-on queryset for convenience.
        return self
<commit_msg>[ci skip] Return the limited queryset.<commit_after>from django.db import models, transaction

class OrderableQueryset(models.QuerySet):
    """
    Adds additional functionality to `Orderable.objects` and querysets.

    Provides access to the next and previous ordered object within the queryset.

    As a related manager this will provide the filtering automatically.
    """
    def before(self, orderable):
        return self.filter(sort_order__lt=orderable.sort_order).last()

    def after(self, orderable):
        return self.filter(sort_order__gt=orderable.sort_order).first()

    def set_orders(self, object_pks, *args, **kwargs):
        """
        Perform a mass update of sort_orders across the full queryset.
        Accepts a list, object_pks, of the intended order for the objects.

        Works as follows:
        - Compile a list of all sort orders in the queryset. Leave out anything that
          isn't in the object_pks list - this deals with pagination and any
          inconsistencies.
        - Get the maximum among all model object sort orders. Update the queryset to add
          it to all the existing sort order values. This lifts them 'out of the way' of
          unique_together clashes when setting the intended sort orders.
        - Set the sort order on each object. Use only sort_order values that the objects
          had before calling this method, so they get rearranged in place.
        Performs O(n) queries.
        """
        max_value = self.model.objects.count()
        objects_to_sort = self.filter(pk__in=object_pks)

        # Call list() on the values right away, so they don't get affected by the
        # update() later (since values_list() is lazy).
        orders = list(objects_to_sort.values_list('sort_order', flat=True))

        with transaction.atomic():
            objects_to_sort.update(sort_order=models.F('sort_order') + max_value)
            for i, pk in enumerate(object_pks):
                self.filter(pk=pk).update(sort_order=orders[i])

        # Return the operated-on queryset for convenience.
        return objects_to_sort
<|endoftext|>


<commit_before><commit_msg>Use Future.succeed in run_in_terminal.<commit_after><|endoftext|>


<commit_before>from django.contrib import admin

from organizations.models import (Organization, OrganizationUser,
        OrganizationOwner)

class OwnerInline(admin.StackedInline):
    model = OrganizationOwner

class OrganizationAdmin(admin.ModelAdmin):
    inlines = [OwnerInline]
    list_display = ['name', 'is_active']

class OrganizationUserAdmin(admin.ModelAdmin):
    list_filter = ['organization']
    list_display = ['user', 'is_admin']

class OrganizationOwnerAdmin(admin.ModelAdmin):
    list_filter = ['organization']

admin.site.register(Organization, OrganizationAdmin)
admin.site.register(OrganizationUser, OrganizationUserAdmin)
admin.site.register(OrganizationOwner, OrganizationOwnerAdmin)
<commit_msg>Prepopulate organization slug based on name.<commit_after>from django.contrib import admin

from organizations.models import (Organization, OrganizationUser,
        OrganizationOwner)

class OwnerInline(admin.StackedInline):
    model = OrganizationOwner

class OrganizationAdmin(admin.ModelAdmin):
    inlines = [OwnerInline]
    list_display = ['name', 'is_active']
    prepopulated_fields = {"slug": ("name",)}

class OrganizationUserAdmin(admin.ModelAdmin):
    list_filter = ['organization']
    list_display = ['user', 'is_admin']

class OrganizationOwnerAdmin(admin.ModelAdmin):
    list_filter = ['organization']

admin.site.register(Organization, OrganizationAdmin)
admin.site.register(OrganizationUser, OrganizationUserAdmin)
admin.site.register(OrganizationOwner, OrganizationOwnerAdmin)
<|endoftext|>


<commit_before>from django import forms
from django.contrib.sites.models import get_current_site
from django.utils.translation import ugettext_lazy as _

from organizations.models import Organization, OrganizationUser, get_user_model
from organizations.utils import create_organization
from organizations.backends import invitation_backend, notification_backend

class OrganizationForm(forms.ModelForm):
    """Form class for updating Organizations"""
    owner = forms.ModelChoiceField(OrganizationUser.objects.all())

    def __init__(self, request, *args, **kwargs):
        self.request = request
        super(OrganizationForm, self).__init__(*args, **kwargs)
        self.fields['owner'].queryset = self.instance.organization_users.filter(
                is_admin=True, user__is_active=True)
        self.fields['owner'].initial = self.instance.owner.organization_user

    class Meta:
        model = Organization
        exclude = ('users', 'is_active')

    def save(self, commit=True):
        if self.instance.owner.organization_user != self.cleaned_data['owner']:
            self.instance.owner.organization_user = self.cleaned_data['owner']
            self.instance.owner.save()
        return super(OrganizationForm, self).save(commit=commit)

    def clean_owner(self):
        owner = self.cleaned_data['owner']
        if owner != self.instance.owner.organization_user:
            if self.request.user != self.instance.owner.organization_user.user:
                raise forms.ValidationError(_("Only the organization owner can change ownerhip"))
        return owner

class OrganizationUserForm(forms.ModelForm):
    """Form class for updating OrganizationUsers"""

    class Meta:
        model = OrganizationUser
        exclude = ('organization', 'user')

    def clean_is_admin(self):
        is_admin = self.cleaned_data['is_admin']
        if self.instance.organization.owner.organization_user == self.instance and not is_admin:
            raise forms.ValidationError(_("The organization owner must be an admin"))
        return is_admin

class OrganizationUserAddForm(forms.ModelForm):
    """Form class for adding OrganizationUsers to an existing Organization"""
    email = forms.EmailField(max_length=75)

    def __init__(self, request, organization, *args, **kwargs):
        self.request = request
        self.organization = organization
        super(OrganizationUserAddForm, self).__init__(*args, **kwargs)

    class Meta:
        model = OrganizationUser
        exclude = ('user', 'organization')

    def save(self, *args, **kwargs):
        """
        The save method should create a new OrganizationUser linking the User
        matching the provided email address. If not matching User is found it
        should kick off the registration process. It needs to create a User in
        order to link it to the Organization.
        """
        try:
            user = get_user_model().objects.get(email__iexact=self.cleaned_data['email'])
        except get_user_model().MultipleObjectsReturned:
            raise forms.ValidationError(_("This email address has been used multiple times."))
        except get_user_model().DoesNotExist:
            user = invitation_backend().invite_by_email(
                    self.cleaned_data['email'],
                    **{'site': get_current_site(self.request),
                        'organization': self.organization,
                        'sender': self.request.user})
        else:
            notification_backend().notify_by_email(self.cleaned_data['email'],
                    **{'domain': get_current_site(self.request),
                        'organization': self.organization,
                        'sender': self.request.user})

        return OrganizationUser.objects.create(user=user,
                organization=self.organization,
                is_admin=self.cleaned_data['is_admin'])

    def clean_email(self):
        email = self.cleaned_data['email']
        if self.organization.users.filter(email=email):
            raise forms.ValidationError(_("There is already an organization member with this email address!"))
        return email

class OrganizationAddForm(forms.ModelForm):
    """
    Form class for creating a new organization, complete with new owner, including a
    User instance, OrganizationUser instance, and OrganizationOwner instance.
    """
    email = forms.EmailField(max_length=30,
            help_text=_("The email address for the account owner"))

    def __init__(self, request, *args, **kwargs):
        self.request = request
        super(OrganizationAddForm, self).__init__(*args, **kwargs)

    class Meta:
        model = Organization
        exclude = ('users', 'is_active')

    def save(self, **kwargs):
        """
        Create the organization, then get the user, then make the owner.
        """
        is_active = True
        try:
            user = get_user_model().objects.get(email=self.cleaned_data['email'])
        except get_user_model().DoesNotExist:
            user = invitation_backend().invite_by_email(
                    self.cleaned_data['email'],
                    **{'domain': get_current_site(self.request),
                        'organization': self.cleaned_data['name'],
                        'sender': self.request.user, 'created': True})
            is_active = False
        return create_organization(user, self.cleaned_data['name'],
                self.cleaned_data['slug'], is_active=is_active)

class SignUpForm(forms.Form):
    """
    From class for signing up a new user and new account.
    """
    name = forms.CharField(max_length=50,
            help_text=_("The name of the organization"))
    slug = forms.SlugField(max_length=50,
            help_text=_("The name in all lowercase, suitable for URL identification"))
    email = forms.EmailField()
<commit_msg>Fix invitation emails so that they display the system name and correct URL (including domain) (kanban card https://aptivate.kanbantool.com/boards/5986-econsensus#tasks-1194319). Issue was that template for invitation email contains references to 'domain' not 'site' - bug introduced at https://github.com/aptivate/django-organizations/commit/31f8af7fc943443973377a439b52dd9b3e930ea1 and then fixed for existing users but not brand new users at https://github.com/aptivate/django-organizations/commit/b24077c1a0f6fc0cf1c599817cdf699456c2a891.<commit_after>from django import forms
from django.contrib.sites.models import get_current_site
from django.utils.translation import ugettext_lazy as _

from organizations.models import Organization, OrganizationUser, get_user_model
from organizations.utils import create_organization
from organizations.backends import invitation_backend, notification_backend

class OrganizationForm(forms.ModelForm):
    """Form class for updating Organizations"""
    owner = forms.ModelChoiceField(OrganizationUser.objects.all())

    def __init__(self, request, *args, **kwargs):
        self.request = request
        super(OrganizationForm, self).__init__(*args, **kwargs)
        self.fields['owner'].queryset = self.instance.organization_users.filter(
                is_admin=True, user__is_active=True)
        self.fields['owner'].initial = self.instance.owner.organization_user

    class Meta:
        model = Organization
        exclude = ('users', 'is_active')

    def save(self, commit=True):
        if self.instance.owner.organization_user != self.cleaned_data['owner']:
            self.instance.owner.organization_user = self.cleaned_data['owner']
            self.instance.owner.save()
        return super(OrganizationForm, self).save(commit=commit)

    def clean_owner(self):
        owner = self.cleaned_data['owner']
        if owner != self.instance.owner.organization_user:
            if self.request.user != self.instance.owner.organization_user.user:
                raise forms.ValidationError(_("Only the organization owner can change ownerhip"))
        return owner

class OrganizationUserForm(forms.ModelForm):
    """Form class for updating OrganizationUsers"""

    class Meta:
        model = OrganizationUser
        exclude = ('organization', 'user')

    def clean_is_admin(self):
        is_admin = self.cleaned_data['is_admin']
        if self.instance.organization.owner.organization_user == self.instance and not is_admin:
            raise forms.ValidationError(_("The organization owner must be an admin"))
        return is_admin

class OrganizationUserAddForm(forms.ModelForm):
    """Form class for adding OrganizationUsers to an existing Organization"""
    email = forms.EmailField(max_length=75)

    def __init__(self, request, organization, *args, **kwargs):
        self.request = request
        self.organization = organization
        super(OrganizationUserAddForm, self).__init__(*args, **kwargs)

    class Meta:
        model = OrganizationUser
        exclude = ('user', 'organization')

    def save(self, *args, **kwargs):
        """
        The save method should create a new OrganizationUser linking the User
        matching the provided email address. If not matching User is found it
        should kick off the registration process. It needs to create a User in
        order to link it to the Organization.
        """
        try:
            user = get_user_model().objects.get(email__iexact=self.cleaned_data['email'])
        except get_user_model().MultipleObjectsReturned:
            raise forms.ValidationError(_("This email address has been used multiple times."))
        except get_user_model().DoesNotExist:
            user = invitation_backend().invite_by_email(
                    self.cleaned_data['email'],
                    **{'domain': get_current_site(self.request),
                        'organization': self.organization,
                        'sender': self.request.user})
        else:
            notification_backend().notify_by_email(self.cleaned_data['email'],
                    **{'domain': get_current_site(self.request),
                        'organization': self.organization,
                        'sender': self.request.user})

        return OrganizationUser.objects.create(user=user,
                organization=self.organization,
                is_admin=self.cleaned_data['is_admin'])

    def clean_email(self):
        email = self.cleaned_data['email']
        if self.organization.users.filter(email=email):
            raise forms.ValidationError(_("There is already an organization member with this email address!"))
        return email

class OrganizationAddForm(forms.ModelForm):
    """
    Form class for creating a new organization, complete with new owner, including a
    User instance, OrganizationUser instance, and OrganizationOwner instance.
    """
    email = forms.EmailField(max_length=30,
            help_text=_("The email address for the account owner"))

    def __init__(self, request, *args, **kwargs):
        self.request = request
        super(OrganizationAddForm, self).__init__(*args, **kwargs)

    class Meta:
        model = Organization
        exclude = ('users', 'is_active')

    def save(self, **kwargs):
        """
        Create the organization, then get the user, then make the owner.
        """
        is_active = True
        try:
            user = get_user_model().objects.get(email=self.cleaned_data['email'])
        except get_user_model().DoesNotExist:
            user = invitation_backend().invite_by_email(
                    self.cleaned_data['email'],
                    **{'domain': get_current_site(self.request),
                        'organization': self.cleaned_data['name'],
                        'sender': self.request.user, 'created': True})
            is_active = False
        return create_organization(user, self.cleaned_data['name'],
                self.cleaned_data['slug'], is_active=is_active)

class SignUpForm(forms.Form):
    """
    From class for signing up a new user and new account.
    """
    name = forms.CharField(max_length=50,
            help_text=_("The name of the organization"))
    slug = forms.SlugField(max_length=50,
            help_text=_("The name in all lowercase, suitable for URL identification"))
    email = forms.EmailField()
<|endoftext|>


<commit_before>from __future__ import print_function, absolute_import, division
"""searchspace.py

This module contains code for specifying the hyperparameter search space.
The search space is specified as a product of bounded intervals. Each dimension
can be either an integer, floating-point or enumeration.

The base measure on the space, e.g. for random sampling (`rvs()`) is a product
of uniform distribution on the bounded intervals. This can, however, be
modified for floating-point dimensions using the `warp` keyword argument in
`add_float`
"""
from collections import namedtuple, Iterable

import numpy as np
from sklearn.utils import check_random_state
try:
    from hyperopt import hp, pyll
except ImportError:
    from .utils import mock_module
    hp = mock_module('hyperopt')
    pyll = mock_module('hyperopt')

class SearchSpace(object):
    def __init__(self):
        self.variables = {}

    @property
    def n_dims(self):
        return len(self.variables)

    def add_jump(self, name, min, max, num, warp=None, var_type=float):
        """ An integer/float-valued enumerable with `num` items, bounded
        between [`min`, `max`]. Note that the right endpoint of the interval
        includes `max`. This is a wrapper around the add_enum. `jump` can be
        a float or int.
        """
        if not isinstance(var_type, type):
            if var_type == 'int':
                var_type = int
            elif var_type == 'float':
                var_type = float
            else:
                raise ValueError('var_type (%s) is not supported. use '
                                 '"int" or "float",' % (var_type))

        min, max = map(var_type, (min, max))
        num = int(num)

        if not warp:
            choices = np.linspace(min, max, num=num, dtype=var_type)
        elif (min >= 0) and warp == 'log':
            choices = np.logspace(np.log10(min), np.log10(max), num=num,
                                  dtype=var_type)
        elif (min <= 0)and warp == 'log':
            raise ValueError('variable %s: log-warping requires min > 0')
        else:
            raise ValueError('variable %s: warp=%s is not supported. use '
                             'None or "log",' % (name, warp))

        self.variables[name] = EnumVariable(name, list(choices))

    def add_int(self, name, min, max, warp=None):
        """An integer-valued dimension bounded between `min` <= x <= `max`.
        Note that the right endpoint of the interval includes `max`.

        When `warp` is None, the base measure associated with this dimension
        is a categorical distribution with each weight on each of the integers
        in [min, max]. With `warp == 'log'`, the base measure is a uniform
        distribution on the log of the variable, with bounds at `log(min)` and
        `log(max)`. This is appropriate for variables that are "naturally" in
        log-space. Other `warp` functions are not supported (yet), but may be
        at a later time. Please note that this functionality is not supported
        for `hyperopt_tpe`.
        """
        min, max = map(int, (min, max))
        if max < min:
            raise ValueError('variable %s: max < min error' % name)
        if warp not in (None, 'log'):
            raise ValueError('variable %s: warp=%s is not supported. use '
                             'None or "log",' % (name, warp))
        if min <= 0 and warp == 'log':
            raise ValueError('variable %s: log-warping requires min > 0')

        self.variables[name] = IntVariable(name, min, max, warp)

    def add_float(self, name, min, max, warp=None):
        """A floating point-valued dimension bounded `min` <= x < `max`

        When `warp` is None, the base measure associated with this dimension
        is a uniform distribution on [min, max). With `warp == 'log'`, the
        base measure is a uniform distribution on the log of the variable,
        with bounds at `log(min)` and `log(max)`. This is appropriate for
        variables that are "naturally" in log-space. Other `warp` functions
        are not supported (yet), but may be at a later time.
        """
        min, max = map(float, (min, max))
        if not min < max:
            raise ValueError('variable %s: min >= max error' % name)
        if warp not in (None, 'log'):
            raise ValueError('variable %s: warp=%s is not supported. use '
                             'None or "log",' % (name, warp))
        if min <= 0 and warp == 'log':
            raise ValueError('variable %s: log-warping requires min > 0')

        self.variables[name] = FloatVariable(name, min, max, warp)

    def add_enum(self, name, choices):
        """An enumeration-valued dimension.

        The base measure associated with this dimension is a categorical
        distribution with equal weight on each element in `choices`.
        """
        if not isinstance(choices, Iterable):
            raise ValueError('variable %s: choices must be iterable' % name)
        self.variables[name] = EnumVariable(name, choices)

    def __getitem__(self, name):
        return self.variables[name]

    def __iter__(self):
        return iter(self.variables.values())

    def rvs(self, seed=None):
        random = check_random_state(seed)
        return dict((param.name, param.rvs(random)) for param in self)

    def to_hyperopt(self):
        return dict((v.name, v.to_hyperopt()) for v in self)

    def point_to_gp(self, point_dict):
        return [var.point_to_gp(point_dict[var.name]) for var in self]

    def __repr__(self):
        lines = (['Hyperparameter search space:'] +
                 ['  ' + repr(var) for var in self])
        return '\n'.join(lines)

class IntVariable(namedtuple('IntVariable', ('name', 'min', 'max', 'warp'))):
    # this pattern is a simple memory-efficient way to add some methods to
    # a namedtuple, demonstrated in sklearn.
    # https://github.com/scikit-learn/scikit-learn/blob/a38372998b560d184a195bbd10a16c8f20119aa8/sklearn/grid_search.py#L259-L278

    __slots__ = ()

    def __repr__(self):
        return '{0:<25s}\t(int)   {1:8d} <= x <= {2:d}'.format(
            self.name, self.min, self.max)

    def rvs(self, random):
        # extra +1 here because of the _inclusive_ endpoint
        if self.warp is None:
            return random.randint(self.min, self.max+1)
        elif self.warp == 'log':
            return int(np.exp(random.uniform(np.log(self.min), np.log(self.max+1))))
        raise ValueError('unknown warp: %s' % self.warp)

    def to_hyperopt(self):
        if self.warp is None:
            return pyll.scope.int(hp.uniform(self.name, self.min, self.max+1))
        raise ValueError('warped integers are not supported for hyperopt')

    def domain_to_gp(self):
        return {'min': 0.0, 'max': 1.0}

    def point_to_gp(self, value):
        if self.warp is None:
            return (value - self.min) / (self.max - self.min)
        elif self.warp == 'log':
            rng = np.log(self.max) - np.log(self.min)
            return int((np.log(value) - np.log(self.min)) / rng)

        raise ValueError('unknown warp: %s' % self.warp)

    def point_from_gp(self, gpvalue):
        if self.warp is None:
            return int(self.min + (gpvalue * (self.max - self.min)))
        elif self.warp == 'log':
            rng = np.log(self.max) - np.log(self.min)
            outvalue = np.exp(np.log(self.min) + gpvalue * rng)
            return np.clip(outvalue, self.min, self.max).astype(int)
        raise ValueError('unknown warp: %s' % self.warp)

class FloatVariable(namedtuple('FloatVariable',
                               ('name', 'min', 'max', 'warp'))):
    __slots__ = ()

    def __repr__(self):
        return '{0:<25s}\t(float) {1:8f} <= x <  {2:f}'.format(
            self.name, self.min, self.max)

    def rvs(self, random):
        if self.warp is None:
            return random.uniform(self.min, self.max)
        elif self.warp == 'log':
            return np.exp(random.uniform(np.log(self.min), np.log(self.max)))
        raise ValueError('unknown warp: %s' % self.warp)

    def to_hyperopt(self):
        if self.warp is None:
            return hp.uniform(self.name, self.min, self.max)
        elif self.warp == 'log':
            return hp.loguniform(self.name, np.log(self.min), np.log(self.max))
        raise ValueError('unknown warp: %s' % self.warp)

    def domain_to_gp(self):
        return {'min': 0.0, 'max': 1.0}

    def point_to_gp(self, value):
        if self.warp is None:
            return (value - self.min) / (self.max - self.min)
        elif self.warp == 'log':
            rng = np.log(self.max) - np.log(self.min)
            return (np.log(value) - np.log(self.min)) / rng

        raise ValueError('unknown warp: %s' % self.warp)

    def point_from_gp(self, gpvalue):
        if self.warp is None:
            outvalue = self.min + (gpvalue * (self.max - self.min))
        elif self.warp == 'log':
            rng = np.log(self.max) - np.log(self.min)
            outvalue = np.exp(np.log(self.min) + gpvalue * rng)
        else:
            raise ValueError('unknown warp: %s' % self.warp)

        return np.clip(outvalue, self.min, self.max)

class EnumVariable(namedtuple('EnumVariable', ('name', 'choices'))):
    __slots__ = ()

    def __repr__(self):
        c = [str(e) for e in self.choices]
        return '{0:<25s}\t(enum)    choices = ({1:s})'.format(
            self.name, ', '.join(c))

    def rvs(self, random):
        return self.choices[random.randint(len(self.choices))]

    def to_hyperopt(self):
        return hp.choice(self.name, self.choices)

    def domain_to_gp(self):
        return {'min': 0.0, 'max': 1.0}

    def point_to_gp(self, value):
        try:
            index = next(i for i, c in enumerate(self.choices) if c == value)
        except StopIteration:
            raise ValueError('%s not in %s' % (value, self.choices))
        return float(index) / max(len(self.choices) - 1, 1)

    def point_from_gp(self, gpvalue):
        return self.choices[int(np.round(gpvalue * max(len(self.choices) - 1, 1)))]
<commit_msg>Jump variables now cast to nearest Python equivalent<commit_after>from __future__ import print_function, absolute_import, division
"""searchspace.py

This module contains code for specifying the hyperparameter search space.
The search space is specified as a product of bounded intervals. Each dimension
can be either an integer, floating-point or enumeration.

The base measure on the space, e.g. for random sampling (`rvs()`) is a product
of uniform distribution on the bounded intervals. This can, however, be
modified for floating-point dimensions using the `warp` keyword argument in
`add_float`
"""
from collections import namedtuple, Iterable

import numpy as np
from sklearn.utils import check_random_state
try:
    from hyperopt import hp, pyll
except ImportError:
    from .utils import mock_module
    hp = mock_module('hyperopt')
    pyll = mock_module('hyperopt')

class SearchSpace(object):
    def __init__(self):
        self.variables = {}

    @property
    def n_dims(self):
        return len(self.variables)

    def add_jump(self, name, min, max, num, warp=None, var_type=float):
        """ An integer/float-valued enumerable with `num` items, bounded
        between [`min`, `max`]. Note that the right endpoint of the interval
        includes `max`. This is a wrapper around the add_enum. `jump` can be
        a float or int.
        """
        if not isinstance(var_type, type):
            if var_type == 'int':
                var_type = int
            elif var_type == 'float':
                var_type = float
            else:
                raise ValueError('var_type (%s) is not supported. use '
                                 '"int" or "float",' % (var_type))

        min, max = map(var_type, (min, max))
        num = int(num)

        if not warp:
            choices = np.linspace(min, max, num=num, dtype=var_type)
        elif (min >= 0) and warp == 'log':
            choices = np.logspace(np.log10(min), np.log10(max), num=num,
                                  dtype=var_type)
        elif (min <= 0)and warp == 'log':
            raise ValueError('variable %s: log-warping requires min > 0')
        else:
            raise ValueError('variable %s: warp=%s is not supported. use '
                             'None or "log",' % (name, warp))

        self.variables[name] = EnumVariable(name, choices.tolist())

    def add_int(self, name, min, max, warp=None):
        """An integer-valued dimension bounded between `min` <= x <= `max`.
        Note that the right endpoint of the interval includes `max`.

        When `warp` is None, the base measure associated with this dimension
        is a categorical distribution with each weight on each of the integers
        in [min, max]. With `warp == 'log'`, the base measure is a uniform
        distribution on the log of the variable, with bounds at `log(min)` and
        `log(max)`. This is appropriate for variables that are "naturally" in
        log-space. Other `warp` functions are not supported (yet), but may be
        at a later time. Please note that this functionality is not supported
        for `hyperopt_tpe`.
        """
        min, max = map(int, (min, max))
        if max < min:
            raise ValueError('variable %s: max < min error' % name)
        if warp not in (None, 'log'):
            raise ValueError('variable %s: warp=%s is not supported. use '
                             'None or "log",' % (name, warp))
        if min <= 0 and warp == 'log':
            raise ValueError('variable %s: log-warping requires min > 0')

        self.variables[name] = IntVariable(name, min, max, warp)

    def add_float(self, name, min, max, warp=None):
        """A floating point-valued dimension bounded `min` <= x < `max`

        When `warp` is None, the base measure associated with this dimension
        is a uniform distribution on [min, max). With `warp == 'log'`, the
        base measure is a uniform distribution on the log of the variable,
        with bounds at `log(min)` and `log(max)`. This is appropriate for
        variables that are "naturally" in log-space. Other `warp` functions
        are not supported (yet), but may be at a later time.
        """
        min, max = map(float, (min, max))
        if not min < max:
            raise ValueError('variable %s: min >= max error' % name)
        if warp not in (None, 'log'):
            raise ValueError('variable %s: warp=%s is not supported. use '
                             'None or "log",' % (name, warp))
        if min <= 0 and warp == 'log':
            raise ValueError('variable %s: log-warping requires min > 0')

        self.variables[name] = FloatVariable(name, min, max, warp)

    def add_enum(self, name, choices):
        """An enumeration-valued dimension.

        The base measure associated with this dimension is a categorical
        distribution with equal weight on each element in `choices`.
        """
        if not isinstance(choices, Iterable):
            raise ValueError('variable %s: choices must be iterable' % name)
        self.variables[name] = EnumVariable(name, choices)

    def __getitem__(self, name):
        return self.variables[name]

    def __iter__(self):
        return iter(self.variables.values())

    def rvs(self, seed=None):
        random = check_random_state(seed)
        return dict((param.name, param.rvs(random)) for param in self)

    def to_hyperopt(self):
        return dict((v.name, v.to_hyperopt()) for v in self)

    def point_to_gp(self, point_dict):
        return [var.point_to_gp(point_dict[var.name]) for var in self]

    def __repr__(self):
        lines = (['Hyperparameter search space:'] +
                 ['  ' + repr(var) for var in self])
        return '\n'.join(lines)

class IntVariable(namedtuple('IntVariable', ('name', 'min', 'max', 'warp'))):
    # this pattern is a simple memory-efficient way to add some methods to
    # a namedtuple, demonstrated in sklearn.
    # https://github.com/scikit-learn/scikit-learn/blob/a38372998b560d184a195bbd10a16c8f20119aa8/sklearn/grid_search.py#L259-L278

    __slots__ = ()

    def __repr__(self):
        return '{0:<25s}\t(int)   {1:8d} <= x <= {2:d}'.format(
            self.name, self.min, self.max)

    def rvs(self, random):
        # extra +1 here because of the _inclusive_ endpoint
        if self.warp is None:
            return random.randint(self.min, self.max+1)
        elif self.warp == 'log':
            return int(np.exp(random.uniform(np.log(self.min), np.log(self.max+1))))
        raise ValueError('unknown warp: %s' % self.warp)

    def to_hyperopt(self):
        if self.warp is None:
            return pyll.scope.int(hp.uniform(self.name, self.min, self.max+1))
        raise ValueError('warped integers are not supported for hyperopt')

    def domain_to_gp(self):
        return {'min': 0.0, 'max': 1.0}

    def point_to_gp(self, value):
        if self.warp is None:
            return (value - self.min) / (self.max - self.min)
        elif self.warp == 'log':
            rng = np.log(self.max) - np.log(self.min)
            return int((np.log(value) - np.log(self.min)) / rng)

        raise ValueError('unknown warp: %s' % self.warp)

    def point_from_gp(self, gpvalue):
        if self.warp is None:
            return int(self.min + (gpvalue * (self.max - self.min)))
        elif self.warp == 'log':
            rng = np.log(self.max) - np.log(self.min)
            outvalue = np.exp(np.log(self.min) + gpvalue * rng)
            return np.clip(outvalue, self.min, self.max).astype(int)
        raise ValueError('unknown warp: %s' % self.warp)

class FloatVariable(namedtuple('FloatVariable',
                               ('name', 'min', 'max', 'warp'))):
    __slots__ = ()

    def __repr__(self):
        return '{0:<25s}\t(float) {1:8f} <= x <  {2:f}'.format(
            self.name, self.min, self.max)

    def rvs(self, random):
        if self.warp is None:
            return random.uniform(self.min, self.max)
        elif self.warp == 'log':
            return np.exp(random.uniform(np.log(self.min), np.log(self.max)))
        raise ValueError('unknown warp: %s' % self.warp)

    def to_hyperopt(self):
        if self.warp is None:
            return hp.uniform(self.name, self.min, self.max)
        elif self.warp == 'log':
            return hp.loguniform(self.name, np.log(self.min), np.log(self.max))
        raise ValueError('unknown warp: %s' % self.warp)

    def domain_to_gp(self):
        return {'min': 0.0, 'max': 1.0}

    def point_to_gp(self, value):
        if self.warp is None:
            return (value - self.min) / (self.max - self.min)
        elif self.warp == 'log':
            rng = np.log(self.max) - np.log(self.min)
            return (np.log(value) - np.log(self.min)) / rng

        raise ValueError('unknown warp: %s' % self.warp)

    def point_from_gp(self, gpvalue):
        if self.warp is None:
            outvalue = self.min + (gpvalue * (self.max - self.min))
        elif self.warp == 'log':
            rng = np.log(self.max) - np.log(self.min)
            outvalue = np.exp(np.log(self.min) + gpvalue * rng)
        else:
            raise ValueError('unknown warp: %s' % self.warp)

        return np.clip(outvalue, self.min, self.max)

class EnumVariable(namedtuple('EnumVariable', ('name', 'choices'))):
    __slots__ = ()

    def __repr__(self):
        c = [str(e) for e in self.choices]
        return '{0:<25s}\t(enum)    choices = ({1:s})'.format(
            self.name, ', '.join(c))

    def rvs(self, random):
        return self.choices[random.randint(len(self.choices))]

    def to_hyperopt(self):
        return hp.choice(self.name, self.choices)

    def domain_to_gp(self):
        return {'min': 0.0, 'max': 1.0}

    def point_to_gp(self, value):
        try:
            index = next(i for i, c in enumerate(self.choices) if c == value)
        except StopIteration:
            raise ValueError('%s not in %s' % (value, self.choices))
        return float(index) / max(len(self.choices) - 1, 1)

    def point_from_gp(self, gpvalue):
        return self.choices[int(np.round(gpvalue * max(len(self.choices) - 1, 1)))]
<|endoftext|>


<commit_before>"""
Reads in a yaml file representing a set of tests
>>> with open("ostester/tests/test-compare.yaml", 'r') as f:
...     yml = parse(f)
"""

import collections.abc
from abc import ABCMeta
from io import StringIO
import re

import yaml

import ast

def parse(file):
    return yaml.safe_load(file)

class ABCYAMLMeta(ABCMeta, type(yaml.YAMLObject)): pass

class Zeroed(collections.abc.Sequence, yaml.YAMLObject, metaclass=ABCYAMLMeta):
    """
    Represents a zeroed region of memory in C
    >>> yaml.safe_load("!zeroed 5")
    Zeroed(5)
    >>> yaml.dump(Zeroed(3))
    "!zeroed '3'\\n"
    >>> list(Zeroed(7))
    [0, 0, 0, 0, 0, 0, 0]
    >>> Zeroed(3)[-3]
    0
    >>> Zeroed(3)[-2]
    0
    >>> Zeroed(4)[1:3]
    [0, 0]
    """

    yaml_loader = yaml.SafeLoader
    yaml_tag='!zeroed'

    def __init__(self, len):
        self.len = len

    @classmethod
    def from_yaml(cls, loader, node):
        return Zeroed(int(node.value))

    @classmethod
    def to_yaml(cls, dumper, data):
        return dumper.represent_scalar(Zeroed.yaml_tag, str(data.len))

    def __getitem__(self, key):
        if isinstance(key, slice):
            return [0 for key in range(*key.indices(self.len))]
        elif key > self.len-1 or key < -self.len:
            raise IndexError('Zeroed index out of range')
        return 0

    def __len__(self):
        return self.len

    def __repr__(self):
        return 'Zeroed({})'.format(repr(self.len))

class Pointer(yaml.YAMLObject):
    """
    Represents a pointer into an array.
    >>> yaml.safe_load('!ptr value')
    Pointer('value')
    >>> yaml.safe_load('!ptr array+3')
    Pointer('array', offset=3)
    >>> yaml.dump(Pointer("value"))
    "!ptr 'value'\\n"
    >>> yaml.dump(Pointer("array", 2))
    "!ptr 'array+2'\\n"
    """
    yaml_loader = yaml.SafeLoader
    yaml_tag = '!ptr'

    def __init__(self, data, offset=0):
        self.data = data
        self.offset = int(offset)

    @classmethod
    def from_yaml(cls, loader, node):
        args = map(str.strip, node.value.split('+'))
        return Pointer(*args)

    @classmethod
    def to_yaml(cls, dumper, data):
        if not data.offset:
            format_str = '{}'
        else:
            format_str = '{}+{}'
        return dumper.represent_scalar(
            Pointer.yaml_tag, format_str.format(data.data, data.offset))

    def __repr__(self):
        if not self.offset:
            format_str = 'Pointer({})'
        else:
            format_str = 'Pointer({}, offset={})'
        return format_str.format(repr(self.data), self.offset)

class Signature(yaml.YAMLObject):
    """Represents the signature of the function under test.
    >>> yaml.safe_load('int -> int')
    Signature(inputs=[int], output=int)
    >>> yaml.safe_load('int, char* -> char*')
    Signature(inputs=[int, char*], output=char*)
    >>> yaml.dump(Signature(inputs=['int'], output='int'))
    "!signature 'int -> int'\\n"
    >>> yaml.dump(Signature(inputs=['int', 'char*'], output='char*'))
    "!signature 'int, char* -> char*'\\n"
    """
    yaml_loader = yaml.SafeLoader
    yaml_tag='!signature'
    yaml_resolver = re.compile(r'.+->.+')

    def __init__(self, *, inputs, output):
        self.inputs = inputs
        self.output = output

    @classmethod
    def from_yaml(cls, loader, node):
        inputs, output = node.value.split('->')
        return Signature(
            inputs=[t.strip() for t in inputs.split(',')],
            output=output.strip(),
        )

    @classmethod
    def to_yaml(cls, dumper, data):
        return dumper.represent_scalar(
            Signature.yaml_tag,
            '{} -> {}'.format(', '.join(data.inputs), data.output)
        )

    def __repr__(self):
        return 'Signature(inputs={}, output={})'.format(
            repr(self.inputs),
            repr(self.output),
        )

yaml.SafeLoader.add_implicit_resolver('!signature', Signature.yaml_resolver, None)

class CType:
    """
    Represents en expression type in C.
    >>> t = CType("int*[3]")
    >>> t
    int*[3]
    >>> t.declare("values")
    'int *values[3]'
    """
    type_spec = re.compile(r'(\w+)\s*(\**)\s*((\[\d+\])*)')

    def __init__(self, type_decl):
        self.base_type, self.stars, self.arrays, _ = self.type_spec.match(
            type_decl).groups()

    def declare(self, name):
        return '{} {}{}{}'.format(
            self.base_type,
            self.stars,
            name,
            self.arrays,
        )

    def __repr__(self):
        return '{}{}{}'.format(
            self.base_type,
            self.stars,
            self.arrays,
        )

def transform(yaml):
    pass

<commit_msg>Made Signature use CTypes<commit_after>"""
Reads in a yaml file representing a set of tests
>>> with open("ostester/tests/test-compare.yaml", 'r') as f:
...     yml = parse(f)
"""

import collections.abc
from abc import ABCMeta
from io import StringIO
import re

import yaml

import ast

def parse(file):
    return yaml.safe_load(file)

class ABCYAMLMeta(ABCMeta, type(yaml.YAMLObject)): pass

class Zeroed(collections.abc.Sequence, yaml.YAMLObject, metaclass=ABCYAMLMeta):
    """
    Represents a zeroed region of memory in C
    >>> yaml.safe_load("!zeroed 5")
    Zeroed(5)
    >>> yaml.dump(Zeroed(3))
    "!zeroed '3'\\n"
    >>> list(Zeroed(7))
    [0, 0, 0, 0, 0, 0, 0]
    >>> Zeroed(3)[-3]
    0
    >>> Zeroed(3)[-2]
    0
    >>> Zeroed(4)[1:3]
    [0, 0]
    """

    yaml_loader = yaml.SafeLoader
    yaml_tag='!zeroed'

    def __init__(self, len):
        self.len = len

    @classmethod
    def from_yaml(cls, loader, node):
        return Zeroed(int(node.value))

    @classmethod
    def to_yaml(cls, dumper, data):
        return dumper.represent_scalar(Zeroed.yaml_tag, str(data.len))

    def __getitem__(self, key):
        if isinstance(key, slice):
            return [0 for key in range(*key.indices(self.len))]
        elif key > self.len-1 or key < -self.len:
            raise IndexError('Zeroed index out of range')
        return 0

    def __len__(self):
        return self.len

    def __repr__(self):
        return 'Zeroed({})'.format(repr(self.len))

class Pointer(yaml.YAMLObject):
    """
    Represents a pointer into an array.
    >>> yaml.safe_load('!ptr value')
    Pointer('value')
    >>> yaml.safe_load('!ptr array+3')
    Pointer('array', offset=3)
    >>> yaml.dump(Pointer("value"))
    "!ptr 'value'\\n"
    >>> yaml.dump(Pointer("array", 2))
    "!ptr 'array+2'\\n"
    """
    yaml_loader = yaml.SafeLoader
    yaml_tag = '!ptr'

    def __init__(self, data, offset=0):
        self.data = data
        self.offset = int(offset)

    @classmethod
    def from_yaml(cls, loader, node):
        args = map(str.strip, node.value.split('+'))
        return Pointer(*args)

    @classmethod
    def to_yaml(cls, dumper, data):
        if not data.offset:
            format_str = '{}'
        else:
            format_str = '{}+{}'
        return dumper.represent_scalar(
            Pointer.yaml_tag, format_str.format(data.data, data.offset))

    def __repr__(self):
        if not self.offset:
            format_str = 'Pointer({})'
        else:
            format_str = 'Pointer({}, offset={})'
        return format_str.format(repr(self.data), self.offset)

class Signature(yaml.YAMLObject):
    """Represents the signature of the function under test.
    >>> yaml.safe_load('int -> int')
    Signature(inputs=[int], output=int)
    >>> yaml.safe_load('int, char* -> char*')
    Signature(inputs=[int, char*], output=char*)
    >>> yaml.dump(Signature(inputs=['int'], output='int'))
    "!signature 'int -> int'\\n"
    >>> yaml.dump(Signature(inputs=['int', 'char*'], output='char*'))
    "!signature 'int, char* -> char*'\\n"
    """
    yaml_loader = yaml.SafeLoader
    yaml_tag='!signature'
    yaml_resolver = re.compile(r'.+->.+')

    def __init__(self, *, inputs, output):
        self.inputs = inputs
        self.output = output

    @classmethod
    def from_yaml(cls, loader, node):
        inputs, output = node.value.split('->')
        return Signature(
            inputs=[CType(t.strip()) for t in inputs.split(',')],
            output=CType(output.strip()),
        )

    @classmethod
    def to_yaml(cls, dumper, data):
        return dumper.represent_scalar(
            Signature.yaml_tag,
            '{} -> {}'.format(', '.join(data.inputs), data.output)
        )

    def __repr__(self):
        return 'Signature(inputs={}, output={})'.format(
            repr(self.inputs),
            repr(self.output),
        )

yaml.SafeLoader.add_implicit_resolver('!signature', Signature.yaml_resolver, None)

class CType:
    """
    Represents en expression type in C.
    >>> t = CType("int*[3]")
    >>> t
    int*[3]
    >>> t.declare("values")
    'int *values[3]'
    """
    type_spec = re.compile(r'(\w+)\s*(\**)\s*((\[\d+\])*)')

    def __init__(self, type_decl):
        self.base_type, self.stars, self.arrays, _ = self.type_spec.match(
            type_decl).groups()

    def declare(self, name):
        return '{} {}{}{}'.format(
            self.base_type,
            self.stars,
            name,
            self.arrays,
        )

    def __repr__(self):
        return '{}{}{}'.format(
            self.base_type,
            self.stars,
            self.arrays,
        )

def transform(yaml):
    pass

<|endoftext|>


<commit_before>import os
import signal
import subprocess
import time
import logging
log = logging.getLogger(__file__)

def run_java(cmd_line, fork=False, big_xmx="-Xmx4096m", small_xmx="-Xmx1536m", java_cmd="java", shell=False, pid_file="pid.txt"):
    ''' run java ... if we get an exception, try to run again with lower heap size
    '''
    try:
        if big_xmx is None:
            big_xmx = "-Xmx4096m"
        java_cmd = "{} {} {}".format(java_cmd, big_xmx, cmd_line)
        run_cmd(java_cmd, fork, shell, pid_file)
    except Exception, e:
        # try again with smaller java heap memory request
        # NOTE: 'fork' won't get you to see an exception here (because you fork the exception into another process)
        log.info(e)
        java_cmd = "{} {} {}".format(java_cmd, small_xmx, cmd_line)
        run_cmd(java_cmd, fork, shell, pid_file)

def run_cmd(cmd_line, fork=False, shell=False, pid_file="pid.txt"):
    ''' run_cmd("sleep 200") will block for 200 seconds
        run_cmd("sleep 200", True) will background

        NOTE: some other options for piping output...
          devnull = open(os.devnull, 'wb')
          subprocess.Popen(['nohup', 'sleep', '100'], stdout=devnull, stderr=devnull)
    '''
    log.info(cmd_line)
    kill_old_pid(pid_file)
    if fork:
        process = subprocess.Popen(cmd_line, shell=shell)
    else:
        process = subprocess.call(cmd_line, shell=shell)

    # Write PID file
    pid = None
    if process and process.pid:
        pid = process.pid
        write_pid_file(pid_file, pid)
    return pid

def kill_old_pid(pid_file):
    ''' read pid file and then kill process
    '''
    try:
        pf = open(pid_file, 'r')
        pid = pf.read().strip()
        if pid and len(pid) > 1:
            kill(pid)
            time.sleep(5)
    except Exception, e:
        log.debug(e)

def write_pid_file(pid_file, pid):
    ''' write a pid file
    '''
    pf = open(pid_file, 'w')
    pf.write(str(pid))
    pf.flush()
    pf.close()

def kill(pid):
    ''' kill a process
    '''
    #import pdb; pdb.set_trace()
    try:
        os.kill(int(pid), signal.SIGKILL)
    except Exception, e:
        log.debug(e)
        try:
            win_kill = "taskkill /pid {} /f".format(pid)
            log.debug("WINDOWS? will try to kill via: {}".format(win_kill))
            os.system(win_kill)
        except Exception, e:
            log.debug(e)
<commit_msg>more pid stuff ... shell checking<commit_after>import os
import signal
import subprocess
import time
import logging
log = logging.getLogger(__file__)

def run_java(cmd_line, fork=False, big_xmx="-Xmx4096m", small_xmx="-Xmx1536m", java_cmd="java", shell=None, pid_file="pid.txt"):
    ''' run java ... if we get an exception, try to run again with lower heap size
        NOTE: shell is None here by default, so we can detect whether java needs a shell to operate (ala maps servers)
    '''
    if shell is None:
        shell = does_java_need_a_shell(java_cmd)
    try:
        if big_xmx is None:
            big_xmx = "-Xmx4096m"
        cmd_line = "{} {} {}".format(java_cmd, big_xmx, cmd_line)
        run_cmd(cmd_line, fork, shell, pid_file)
    except Exception, e:
        # try again with smaller java heap memory request
        # NOTE: 'fork' won't get you to see an exception here (because you fork the exception into another process)
        log.info(e)
        cmd_line = "{} {} {}".format(java_cmd, small_xmx, cmd_line)
        run_cmd(cmd_line, fork, shell, pid_file)

def does_java_need_a_shell(java_cmd):
    ''' does java cmd need a shell to run properly? ...
        if we get thrown an exception, assume that we do need a shell to execute java
    '''
    ret_val = False
    try:
        subprocess.Popen(java_cmd, shell=False)
    except:
        ret_val = True
    return ret_val

def run_cmd(cmd_line, fork=False, shell=False, pid_file="pid.txt"):
    ''' run_cmd("sleep 200") will block for 200 seconds
        run_cmd("sleep 200", True) will background

        NOTE: some other options for piping output...
          devnull = open(os.devnull, 'wb')
          subprocess.Popen(['nohup', 'sleep', '100'], stdout=devnull, stderr=devnull)
    '''
    log.info(cmd_line)
    kill_old_pid(pid_file)
    if fork:
        process = subprocess.Popen(cmd_line, shell=shell)
    else:
        process = subprocess.call(cmd_line, shell=shell)

    # Write PID file
    pid = None
    if process and process.pid:
        pid = process.pid
        write_pid_file(pid_file, pid)
    return pid

def kill_old_pid(pid_file):
    ''' read pid file and then kill process
    '''
    try:
        pf = open(pid_file, 'r')
        pid = pf.read().strip()
        if pid and len(pid) > 1:
            kill(pid)
            time.sleep(5)
    except Exception, e:
        log.debug(e)

def write_pid_file(pid_file, pid):
    ''' write a pid file
    '''
    pf = open(pid_file, 'w')
    pf.write(str(pid))
    pf.flush()
    pf.close()

def kill(pid):
    ''' kill a process
    '''
    #import pdb; pdb.set_trace()
    try:
        os.kill(int(pid), signal.SIGKILL)
    except Exception, e:
        log.debug(e)
        try:
            win_kill = "taskkill /pid {} /f".format(pid)
            log.debug("WINDOWS? will try to kill via: {}".format(win_kill))
            os.system(win_kill)
        except Exception, e:
            log.debug(e)
<|endoftext|>