<commit_before>import warnings

from xml.etree import ElementTree
from couchdbkit.exceptions import ResourceNotFound, ResourceConflict
from corehq.apps.fixtures.exceptions import FixtureException
from corehq.apps.users.models import CommCareUser
from couchdbkit.ext.django.schema import Document, DocumentSchema, DictProperty, StringProperty, StringListProperty, SchemaListProperty, IntegerProperty, BooleanProperty
from corehq.apps.groups.models import Group
from dimagi.utils.couch.bulk import CouchTransaction
from dimagi.utils.couch.database import get_db

class FixtureTypeCheckError(Exception):
    pass

class FixtureVersionError(Exception):
    pass

class FixtureTypeField(DocumentSchema):
    field_name = StringProperty()
    properties = StringListProperty()

class FixtureDataType(Document):
    domain = StringProperty()
    is_global = BooleanProperty(default=False)
    tag = StringProperty()
    name = StringProperty()
    fields = SchemaListProperty(FixtureTypeField)

    @classmethod
    def wrap(cls, obj):
        if obj["fields"] and isinstance(obj['fields'][0], str):
            # print "old fields, do something here"
            obj['fields'] = [{'field_name': f, 'properties': []} for f in obj['fields']]
        return super(FixtureDataType, cls).wrap(obj)

    # support for old fields
    @property
    def old_fields(self):
        warnings.warn("old_fields should only be used in to migrate old code")
        old_fields = []
        for fixt_field in self.fields:
            old_fields.append(fixt_field.field_name)
        return old_fields

    @classmethod
    def total_by_domain(cls, domain):
        num_fixtures = FixtureDataType.get_db().view(
            'fixtures/data_types_by_domain',
            reduce=True,
            key=domain,
        ).first()
        return num_fixtures['value'] if num_fixtures is not None else 0

    @classmethod
    def by_domain(cls, domain):
        return cls.view('fixtures/data_types_by_domain', key=domain, reduce=False, include_docs=True)

    @classmethod
    def by_domain_tag(cls, domain, tag):
        return cls.view('fixtures/data_types_by_domain_tag', key=[domain, tag], reduce=False, include_docs=True)

    def recursive_delete(self, transaction):
        item_ids = []
        for item in FixtureDataItem.by_data_type(self.domain, self.get_id):
            transaction.delete(item)
            item_ids.append(item.get_id)
        transaction.delete_all(FixtureOwnership.for_all_item_ids(item_ids, self.domain))
        transaction.delete(self)

class FixtureItemField(DocumentSchema):
    """
        "field_value": "Delhi_IN_HIN",
        "properties": {"lang": "hin"}
    """
    field_value = StringProperty()
    properties = DictProperty()

FieldWithAttributes = SchemaListProperty(FixtureItemField)

class FieldList(DocumentSchema):
    """
        List of fields for different combinations of properties
    """
    field_list = SchemaListProperty(FixtureItemField)

class FixtureDataItem(Document):
    """
    Previously following
        domain = StringProperty()
        data_type_id = StringProperty()
        fields = DictProperty()
        sort_key = IntegerProperty()

    Example old Item:
        domain = "hq-domain"
        data_type_id = <id of state FixtureDataType>
        fields = {
            "country": "India",
            "state_name": "Delhi",
            "state_id": "DEL"
        }

    Example new Item with attributes:
        domain = "hq-domain"
        data_type_id = <id of state FixtureDataType>
        fields = {
            "country": {"field_list": [
                {"field_value": "India", "properties": {}},
            ]},
            "state_name": {"field_list": [
                {"field_value": "Delhi_IN_ENG", "properties": {"lang": "eng"}},
                {"field_value": "Delhi_IN_HIN", "properties": {"lang": "hin"}},
            ]},
            "state_id": {"field_list": [
                {"field_value": "DEL", "properties": {}}
            ]}
        }
    If one of field's 'properties' is an empty 'dict', the field has no attributes
    """
    domain = StringProperty()
    data_type_id = StringProperty()
    fields = DictProperty(FieldList)
    sort_key = IntegerProperty()

    @classmethod
    def wrap(cls, obj):
        if not obj["fields"]:
            return super(FixtureDataItem, cls).wrap(obj)

        is_of_new_type = False
        fields_dict = {}
        for field in obj['fields']:
            if type(obj['fields'][field]) != str:
                is_of_new_type = True
                break
            fields_dict[field] = {
                "field_list": [
                    {
                        'field_value': obj['fields'][field],
                        'properties': {}
                    }
                ]
            }
        if not is_of_new_type:
            obj['fields'] = fields_dict
        return super(FixtureDataItem, cls).wrap(obj)

    @property
    def old_fields(self):
        fields = {}
        for field in self.fields:
            # if the field has properties, a unique field_val can't be generated for FixtureItem
            if len(self.fields[field].field_list) > 1:
                raise FixtureVersionError("This method is not supported for fields with properties."
                                          " field '%s' has properties" % field)
            fields[field] = self.fields[field].field_list[0].field_value
        return fields

    @property
    def data_type(self):
        if not hasattr(self, '_data_type'):
            self._data_type = FixtureDataType.get(self.data_type_id)
        return self._data_type

    def add_owner(self, owner, owner_type, transaction=None):
        assert(owner.domain == self.domain)
        with transaction or CouchTransaction() as transaction:
            o = FixtureOwnership(domain=self.domain, owner_type=owner_type, owner_id=owner.get_id, data_item_id=self.get_id)
            transaction.save(o)
        return o

    def remove_owner(self, owner, owner_type):
        for ownership in FixtureOwnership.view('fixtures/ownership',
            key=[self.domain, 'by data_item and ' + owner_type, self.get_id, owner.get_id],
            reduce=False,
            include_docs=True
        ):
            try:
                ownership.delete()
            except ResourceNotFound:
                # looks like it was already deleted
                pass
            except ResourceConflict:
                raise FixtureException((
                    "couldn't remove ownership {owner_id} for item {fixture_id} of type "
                    "{data_type_id} in domain {domain}. It was updated elsewhere"
                ).format(
                    owner_id=ownership._id,
                    fixture_id=self._id,
                    data_type_id=self.data_type_id,
                    domain=self.domain
                ))

    def add_user(self, user, transaction=None):
        return self.add_owner(user, 'user', transaction=transaction)

    def remove_user(self, user):
        return self.remove_owner(user, 'user')

    def add_group(self, group, transaction=None):
        return self.add_owner(group, 'group', transaction=transaction)

    def remove_group(self, group):
        return self.remove_owner(group, 'group')

    def type_check(self):
        fields = set(self.fields.keys())
        for field in self.data_type.fields:
            if field.field_name in fields:
                fields.remove(field)
            else:
                raise FixtureTypeCheckError("field %s not in fixture data %s" % (field.field_name, self.get_id))
        if fields:
            raise FixtureTypeCheckError("fields %s from fixture data %s not in fixture data type" % (', '.join(fields), self.get_id))

    def to_xml(self):
        xData = ElementTree.Element(self.data_type.tag)
        for field in self.data_type.fields:
            if not self.fields.has_key(field.field_name):
                xField = ElementTree.SubElement(xData, field.field_name)
                xField.text = ""
                continue
            for field_with_attr in self.fields[field.field_name].field_list:
                xField = ElementTree.SubElement(xData, field.field_name)
                xField.text = field_with_attr.field_value
                for attribute in field_with_attr.properties:
                    xField.attrib[attribute] = field_with_attr.properties[attribute]
        return xData

    def get_groups(self, wrap=True):
        group_ids = set(
            get_db().view('fixtures/ownership',
                key=[self.domain, 'group by data_item', self.get_id],
                reduce=False,
                wrapper=lambda r: r['value']
            )
        )
        if wrap:
            return set(Group.view('_all_docs', keys=list(group_ids), include_docs=True))
        else:
            return group_ids

    def get_users(self, wrap=True, include_groups=False):
        user_ids = set(
            get_db().view('fixtures/ownership',
                key=[self.domain, 'user by data_item', self.get_id],
                reduce=False,
                wrapper=lambda r: r['value']
            )
        )
        if include_groups:
            group_ids = self.get_groups(wrap=False)
        else:
            group_ids = set()
        users_in_groups = [group.get_users(only_commcare=True) for group in Group.view('_all_docs',
            keys=list(group_ids),
            include_docs=True
        )]
        if wrap:
            return set(CommCareUser.view('_all_docs', keys=list(user_ids), include_docs=True)).union(*users_in_groups)
        else:
            return user_ids | set([user.get_id for user in users_in_groups])

    def get_all_users(self, wrap=True):
        return self.get_users(wrap=wrap, include_groups=True)

    @classmethod
    def by_user(cls, user, wrap=True, domain=None):
        group_ids = Group.by_user(user, wrap=False)

        if isinstance(user, dict):
            user_id = user.get('user_id')
            user_domain = domain
        else:
            user_id = user.user_id
            user_domain = user.domain

        fixture_ids = set(
            FixtureOwnership.get_db().view('fixtures/ownership',
                keys=[[user_domain, 'data_item by user', user_id]] + [[user_domain, 'data_item by group', group_id] for group_id in group_ids],
                reduce=False,
                wrapper=lambda r: r['value'],
            )
        )
        if wrap:
            results = cls.get_db().view('_all_docs', keys=list(fixture_ids), include_docs=True)

            # sort the results into those corresponding to real documents
            # and those corresponding to deleted or non-existent documents
            docs = []
            deleted_fixture_ids = set()

            for result in results:
                if result.get('doc'):
                    docs.append(cls.wrap(result['doc']))
                elif result.get('error'):
                    assert result['error'] == 'not_found'
                    deleted_fixture_ids.add(result['key'])
                else:
                    assert result['value']['deleted'] is True
                    deleted_fixture_ids.add(result['id'])

            # fetch and delete ownership documents pointing
            # to deleted or non-existent fixture documents
            # this cleanup is necessary since we used to not do this
            bad_ownerships = FixtureOwnership.for_all_item_ids(deleted_fixture_ids, user_domain)
            FixtureOwnership.get_db().bulk_delete(bad_ownerships)

            return docs
        else:
            return fixture_ids

    @classmethod
    def by_group(cls, group, wrap=True):
        fixture_ids = get_db().view('fixtures/ownership',
            key=[group.domain, 'data_item by group', group.get_id],
            reduce=False,
            wrapper=lambda r: r['value'],
        ).all()

        return cls.view('_all_docs', keys=list(fixture_ids), include_docs=True) if wrap else fixture_ids

    @classmethod
    def by_data_type(cls, domain, data_type):
        data_type_id = _id_from_doc(data_type)
        return cls.view('fixtures/data_items_by_domain_type', key=[domain, data_type_id], reduce=False, include_docs=True)

    @classmethod
    def by_domain(cls, domain):
        return cls.view('fixtures/data_items_by_domain_type', startkey=[domain], endkey=[domain, {}], reduce=False, include_docs=True)

    @classmethod
    def by_field_value(cls, domain, data_type, field_name, field_value):
        data_type_id = _id_from_doc(data_type)
        return cls.view('fixtures/data_items_by_field_value', key=[domain, data_type_id, field_name, field_value],
                        reduce=False, include_docs=True)

    @classmethod
    def get_item_list(cls, domain, tag):
        data_type = FixtureDataType.by_domain_tag(domain, tag).one()
        return cls.by_data_type(domain, data_type).all()

    @classmethod
    def get_indexed_items(cls, domain, tag, index_field):
        """
        Looks up an item list and converts to mapping from `index_field`
        to a dict of all fields for that item.

            fixtures = FixtureDataItem.get_indexed_items('my_domain',
                'item_list_tag', 'index_field')
            result = fixtures['index_val']['result_field']
        """
        fixtures = cls.get_item_list(domain, tag)
        return dict((f.old_fields[index_field], f.old_fields) for f in fixtures)

    def delete_ownerships(self, transaction):
        ownerships = FixtureOwnership.by_item_id(self.get_id, self.domain)
        transaction.delete_all(ownerships)

    def recursive_delete(self, transaction):
        self.delete_ownerships(transaction)
        transaction.delete(self)

def _id_from_doc(doc_or_doc_id):
    if isinstance(doc_or_doc_id, basestring):
        doc_id = doc_or_doc_id
    else:
        doc_id = doc_or_doc_id.get_id if doc_or_doc_id else None
    return doc_id

class FixtureOwnership(Document):
    domain = StringProperty()
    data_item_id = StringProperty()
    owner_id = StringProperty()
    owner_type = StringProperty(choices=['user', 'group'])

    @classmethod
    def by_item_id(cls, item_id, domain):
        ownerships = cls.view('fixtures/ownership',
            key=[domain, 'by data_item', item_id],
            include_docs=True,
            reduce=False,
        ).all()

        return ownerships

    @classmethod
    def for_all_item_ids(cls, item_ids, domain):
        ownerships = FixtureOwnership.view('fixtures/ownership',
            keys=[[domain, 'by data_item', item_id] for item_id in item_ids],
            include_docs=True,
            reduce=False
        ).all()

        return ownerships
<commit_msg>renaming fields method<commit_after>import warnings

from xml.etree import ElementTree
from couchdbkit.exceptions import ResourceNotFound, ResourceConflict
from corehq.apps.fixtures.exceptions import FixtureException
from corehq.apps.users.models import CommCareUser
from couchdbkit.ext.django.schema import Document, DocumentSchema, DictProperty, StringProperty, StringListProperty, SchemaListProperty, IntegerProperty, BooleanProperty
from corehq.apps.groups.models import Group
from dimagi.utils.couch.bulk import CouchTransaction
from dimagi.utils.couch.database import get_db

class FixtureTypeCheckError(Exception):
    pass

class FixtureVersionError(Exception):
    pass

class FixtureTypeField(DocumentSchema):
    field_name = StringProperty()
    properties = StringListProperty()

class FixtureDataType(Document):
    domain = StringProperty()
    is_global = BooleanProperty(default=False)
    tag = StringProperty()
    name = StringProperty()
    fields = SchemaListProperty(FixtureTypeField)

    @classmethod
    def wrap(cls, obj):
        if obj["fields"] and isinstance(obj['fields'][0], str):
            # print "old fields, do something here"
            obj['fields'] = [{'field_name': f, 'properties': []} for f in obj['fields']]
        return super(FixtureDataType, cls).wrap(obj)

    # support for old fields
    @property
    def fields_without_attributes(self):
        fields_without_attributes = []
        for fixt_field in self.fields:
            fields_without_attributes.append(fixt_field.field_name)
        return fields_without_attributes

    @classmethod
    def total_by_domain(cls, domain):
        num_fixtures = FixtureDataType.get_db().view(
            'fixtures/data_types_by_domain',
            reduce=True,
            key=domain,
        ).first()
        return num_fixtures['value'] if num_fixtures is not None else 0

    @classmethod
    def by_domain(cls, domain):
        return cls.view('fixtures/data_types_by_domain', key=domain, reduce=False, include_docs=True)

    @classmethod
    def by_domain_tag(cls, domain, tag):
        return cls.view('fixtures/data_types_by_domain_tag', key=[domain, tag], reduce=False, include_docs=True)

    def recursive_delete(self, transaction):
        item_ids = []
        for item in FixtureDataItem.by_data_type(self.domain, self.get_id):
            transaction.delete(item)
            item_ids.append(item.get_id)
        transaction.delete_all(FixtureOwnership.for_all_item_ids(item_ids, self.domain))
        transaction.delete(self)

class FixtureItemField(DocumentSchema):
    """
        "field_value": "Delhi_IN_HIN",
        "properties": {"lang": "hin"}
    """
    field_value = StringProperty()
    properties = DictProperty()

FieldWithAttributes = SchemaListProperty(FixtureItemField)

class FieldList(DocumentSchema):
    """
        List of fields for different combinations of properties
    """
    field_list = SchemaListProperty(FixtureItemField)

class FixtureDataItem(Document):
    """
    Previously following
        domain = StringProperty()
        data_type_id = StringProperty()
        fields = DictProperty()
        sort_key = IntegerProperty()

    Example old Item:
        domain = "hq-domain"
        data_type_id = <id of state FixtureDataType>
        fields = {
            "country": "India",
            "state_name": "Delhi",
            "state_id": "DEL"
        }

    Example new Item with attributes:
        domain = "hq-domain"
        data_type_id = <id of state FixtureDataType>
        fields = {
            "country": {"field_list": [
                {"field_value": "India", "properties": {}},
            ]},
            "state_name": {"field_list": [
                {"field_value": "Delhi_IN_ENG", "properties": {"lang": "eng"}},
                {"field_value": "Delhi_IN_HIN", "properties": {"lang": "hin"}},
            ]},
            "state_id": {"field_list": [
                {"field_value": "DEL", "properties": {}}
            ]}
        }
    If one of field's 'properties' is an empty 'dict', the field has no attributes
    """
    domain = StringProperty()
    data_type_id = StringProperty()
    fields = DictProperty(FieldList)
    sort_key = IntegerProperty()

    @classmethod
    def wrap(cls, obj):
        if not obj["fields"]:
            return super(FixtureDataItem, cls).wrap(obj)

        is_of_new_type = False
        fields_dict = {}
        for field in obj['fields']:
            if type(obj['fields'][field]) != str:
                is_of_new_type = True
                break
            fields_dict[field] = {
                "field_list": [
                    {
                        'field_value': obj['fields'][field],
                        'properties': {}
                    }
                ]
            }
        if not is_of_new_type:
            obj['fields'] = fields_dict
        return super(FixtureDataItem, cls).wrap(obj)

    @property
    def fields_without_attributes(self):
        fields = {}
        for field in self.fields:
            # if the field has properties, a unique field_val can't be generated for FixtureItem
            if len(self.fields[field].field_list) > 1:
                raise FixtureVersionError("This method is not supported for fields with properties."
                                          " field '%s' has properties" % field)
            fields[field] = self.fields[field].field_list[0].field_value
        return fields

    @property
    def data_type(self):
        if not hasattr(self, '_data_type'):
            self._data_type = FixtureDataType.get(self.data_type_id)
        return self._data_type

    def add_owner(self, owner, owner_type, transaction=None):
        assert(owner.domain == self.domain)
        with transaction or CouchTransaction() as transaction:
            o = FixtureOwnership(domain=self.domain, owner_type=owner_type, owner_id=owner.get_id, data_item_id=self.get_id)
            transaction.save(o)
        return o

    def remove_owner(self, owner, owner_type):
        for ownership in FixtureOwnership.view('fixtures/ownership',
            key=[self.domain, 'by data_item and ' + owner_type, self.get_id, owner.get_id],
            reduce=False,
            include_docs=True
        ):
            try:
                ownership.delete()
            except ResourceNotFound:
                # looks like it was already deleted
                pass
            except ResourceConflict:
                raise FixtureException((
                    "couldn't remove ownership {owner_id} for item {fixture_id} of type "
                    "{data_type_id} in domain {domain}. It was updated elsewhere"
                ).format(
                    owner_id=ownership._id,
                    fixture_id=self._id,
                    data_type_id=self.data_type_id,
                    domain=self.domain
                ))

    def add_user(self, user, transaction=None):
        return self.add_owner(user, 'user', transaction=transaction)

    def remove_user(self, user):
        return self.remove_owner(user, 'user')

    def add_group(self, group, transaction=None):
        return self.add_owner(group, 'group', transaction=transaction)

    def remove_group(self, group):
        return self.remove_owner(group, 'group')

    def type_check(self):
        fields = set(self.fields.keys())
        for field in self.data_type.fields:
            if field.field_name in fields:
                fields.remove(field)
            else:
                raise FixtureTypeCheckError("field %s not in fixture data %s" % (field.field_name, self.get_id))
        if fields:
            raise FixtureTypeCheckError("fields %s from fixture data %s not in fixture data type" % (', '.join(fields), self.get_id))

    def to_xml(self):
        xData = ElementTree.Element(self.data_type.tag)
        for field in self.data_type.fields:
            if not self.fields.has_key(field.field_name):
                xField = ElementTree.SubElement(xData, field.field_name)
                xField.text = ""
                continue
            for field_with_attr in self.fields[field.field_name].field_list:
                xField = ElementTree.SubElement(xData, field.field_name)
                xField.text = field_with_attr.field_value
                for attribute in field_with_attr.properties:
                    xField.attrib[attribute] = field_with_attr.properties[attribute]
        return xData

    def get_groups(self, wrap=True):
        group_ids = set(
            get_db().view('fixtures/ownership',
                key=[self.domain, 'group by data_item', self.get_id],
                reduce=False,
                wrapper=lambda r: r['value']
            )
        )
        if wrap:
            return set(Group.view('_all_docs', keys=list(group_ids), include_docs=True))
        else:
            return group_ids

    def get_users(self, wrap=True, include_groups=False):
        user_ids = set(
            get_db().view('fixtures/ownership',
                key=[self.domain, 'user by data_item', self.get_id],
                reduce=False,
                wrapper=lambda r: r['value']
            )
        )
        if include_groups:
            group_ids = self.get_groups(wrap=False)
        else:
            group_ids = set()
        users_in_groups = [group.get_users(only_commcare=True) for group in Group.view('_all_docs',
            keys=list(group_ids),
            include_docs=True
        )]
        if wrap:
            return set(CommCareUser.view('_all_docs', keys=list(user_ids), include_docs=True)).union(*users_in_groups)
        else:
            return user_ids | set([user.get_id for user in users_in_groups])

    def get_all_users(self, wrap=True):
        return self.get_users(wrap=wrap, include_groups=True)

    @classmethod
    def by_user(cls, user, wrap=True, domain=None):
        group_ids = Group.by_user(user, wrap=False)

        if isinstance(user, dict):
            user_id = user.get('user_id')
            user_domain = domain
        else:
            user_id = user.user_id
            user_domain = user.domain

        fixture_ids = set(
            FixtureOwnership.get_db().view('fixtures/ownership',
                keys=[[user_domain, 'data_item by user', user_id]] + [[user_domain, 'data_item by group', group_id] for group_id in group_ids],
                reduce=False,
                wrapper=lambda r: r['value'],
            )
        )
        if wrap:
            results = cls.get_db().view('_all_docs', keys=list(fixture_ids), include_docs=True)

            # sort the results into those corresponding to real documents
            # and those corresponding to deleted or non-existent documents
            docs = []
            deleted_fixture_ids = set()

            for result in results:
                if result.get('doc'):
                    docs.append(cls.wrap(result['doc']))
                elif result.get('error'):
                    assert result['error'] == 'not_found'
                    deleted_fixture_ids.add(result['key'])
                else:
                    assert result['value']['deleted'] is True
                    deleted_fixture_ids.add(result['id'])

            # fetch and delete ownership documents pointing
            # to deleted or non-existent fixture documents
            # this cleanup is necessary since we used to not do this
            bad_ownerships = FixtureOwnership.for_all_item_ids(deleted_fixture_ids, user_domain)
            FixtureOwnership.get_db().bulk_delete(bad_ownerships)

            return docs
        else:
            return fixture_ids

    @classmethod
    def by_group(cls, group, wrap=True):
        fixture_ids = get_db().view('fixtures/ownership',
            key=[group.domain, 'data_item by group', group.get_id],
            reduce=False,
            wrapper=lambda r: r['value'],
        ).all()

        return cls.view('_all_docs', keys=list(fixture_ids), include_docs=True) if wrap else fixture_ids

    @classmethod
    def by_data_type(cls, domain, data_type):
        data_type_id = _id_from_doc(data_type)
        return cls.view('fixtures/data_items_by_domain_type', key=[domain, data_type_id], reduce=False, include_docs=True)

    @classmethod
    def by_domain(cls, domain):
        return cls.view('fixtures/data_items_by_domain_type', startkey=[domain], endkey=[domain, {}], reduce=False, include_docs=True)

    @classmethod
    def by_field_value(cls, domain, data_type, field_name, field_value):
        data_type_id = _id_from_doc(data_type)
        return cls.view('fixtures/data_items_by_field_value', key=[domain, data_type_id, field_name, field_value],
                        reduce=False, include_docs=True)

    @classmethod
    def get_item_list(cls, domain, tag):
        data_type = FixtureDataType.by_domain_tag(domain, tag).one()
        return cls.by_data_type(domain, data_type).all()

    @classmethod
    def get_indexed_items(cls, domain, tag, index_field):
        """
        Looks up an item list and converts to mapping from `index_field`
        to a dict of all fields for that item.

            fixtures = FixtureDataItem.get_indexed_items('my_domain',
                'item_list_tag', 'index_field')
            result = fixtures['index_val']['result_field']
        """
        fixtures = cls.get_item_list(domain, tag)
        return dict((f.fields_without_attributes[index_field], f.fields_without_attributes) for f in fixtures)

    def delete_ownerships(self, transaction):
        ownerships = FixtureOwnership.by_item_id(self.get_id, self.domain)
        transaction.delete_all(ownerships)

    def recursive_delete(self, transaction):
        self.delete_ownerships(transaction)
        transaction.delete(self)

def _id_from_doc(doc_or_doc_id):
    if isinstance(doc_or_doc_id, basestring):
        doc_id = doc_or_doc_id
    else:
        doc_id = doc_or_doc_id.get_id if doc_or_doc_id else None
    return doc_id

class FixtureOwnership(Document):
    domain = StringProperty()
    data_item_id = StringProperty()
    owner_id = StringProperty()
    owner_type = StringProperty(choices=['user', 'group'])

    @classmethod
    def by_item_id(cls, item_id, domain):
        ownerships = cls.view('fixtures/ownership',
            key=[domain, 'by data_item', item_id],
            include_docs=True,
            reduce=False,
        ).all()

        return ownerships

    @classmethod
    def for_all_item_ids(cls, item_ids, domain):
        ownerships = FixtureOwnership.view('fixtures/ownership',
            keys=[[domain, 'by data_item', item_id] for item_id in item_ids],
            include_docs=True,
            reduce=False
        ).all()

        return ownerships
<|endoftext|>


<commit_before>def meetup_day():
    pass
<commit_msg>meetup: Add default params<commit_after>def meetup_day(year, month, day_of_the_week, which):
    pass
<|endoftext|>


<commit_before>import pytest
import os

def test_been_called(expect, mocker):
    mock_called = mocker.patch('os.remove')
    os.remove('/home/log.txt')

    expect(mock_called).to.have.been_called

    with pytest.raises(AssertionError):
        expect(mock_called).to.have_not.been_called

    mock_not_called = mocker.patch('os.rmdir')

    expect(mock_not_called).to.have_not.been_called

    with pytest.raises(AssertionError):
        expect(mock_not_called).to.have.been_called

    mock_called_several_times = mocker.patch('os.rename')
    os.rename('/home/log.txt', '/home/log_new.txt')
    os.rename('/home/log.txt', '/home/log_new.txt')

    expect(mock_called_several_times).to.have.been_called

    with pytest.raises(AssertionError):
        expect(mock_called_several_times).to.have_not.been_called

def test_been_called_times(expect, mocker):
    mock_called = mocker.patch('os.remove')
    os.remove('/home/log.txt')
    os.remove('/home/log.txt')
    os.remove('/home/log.txt')

    expect(mock_called).to.have.been_called_times(3)

    with pytest.raises(AssertionError):
        expect(mock_called).to.have_not.been_called_times(3)

    mock_not_called = mocker.patch('os.rmdir')

    expect(mock_not_called).to.have.been_called_times(0)
    expect(mock_not_called).to.have_not.been_called_times(3)

    with pytest.raises(AssertionError):
        expect(mock_not_called).to.have.been_called_times(3)

def test_been_called_with(expect, mocker):
    mock_called = mocker.patch('os.remove')
    os.remove('/home/log.txt')

    expect(mock_called).to.have.been_called_with('/home/log.txt')

    with pytest.raises(AssertionError):
        expect(mock_called).to.have_not.been_called_with('/home/log.txt')

    mock_not_called = mocker.patch('os.rmdir')

    expect(mock_not_called).to.have_not.been_called_with('/home/log.txt')

    with pytest.raises(AssertionError):
        expect(mock_not_called).to.have.been_called_with('/home/log.txt')

def test_been_called_once(expect, mocker):
    mock_called = mocker.patch('os.remove')
    os.remove('/home/log.txt')

    expect(mock_called).to.have.been_called_once

    with pytest.raises(AssertionError):
        expect(mock_called).to.have_not.been_called_once

    mock_not_called = mocker.patch('os.rmdir')

    expect(mock_not_called).to.have_not.been_called_once

    with pytest.raises(AssertionError):
        expect(mock_not_called).to.have.been_called_once

    mock_called_several_times = mocker.patch('os.rename')
    os.rename('/home/log.txt', '/home/log_new.txt')
    os.rename('/home/log.txt', '/home/log_new.txt')

    expect(mock_called_several_times).to.have_not.been_called_once

    with pytest.raises(AssertionError):
        expect(mock_called_several_times).to.have.been_called_once

def test_been_called_once_with(expect, mocker):
    mock_called = mocker.patch('os.remove')
    os.remove('/home/log.txt')

    expect(mock_called).to.have.been_called_once_with('/home/log.txt')

    with pytest.raises(AssertionError):
        expect(mock_called).to.have_not.been_called_once_with('/home/log.txt')

    mock_not_called = mocker.patch('os.rmdir')

    expect(mock_not_called).to.have_not.been_called_once_with('/home/log.txt')

    with pytest.raises(AssertionError):
        expect(mock_not_called).to.have.been_called_once_with('/home/log.txt')

    mock_called_several_times = mocker.patch('os.rename')
    os.rename('/home/log.txt', '/home/log_new.txt')
    os.rename('/home/log.txt', '/home/log_new.txt')

    expect(mock_called_several_times).to.have_not.been_called_once

    with pytest.raises(AssertionError):
        expect(mock_called_several_times).to.have.been_called_once

def test_been_called_with_a_spy(expect, mocker):
    spy = mocker.spy(os.path, 'basename')
    os.path.basename('/home/log.txt')

    expect(spy).to.have.been_called
    expect(spy).to.have.been_called_once
    expect(spy).to.have.been_called_times(1)
    expect(spy).to.have.been_called_with('/home/log.txt')
    expect(spy).to.have.been_called_once_with('/home/log.txt')

    with pytest.raises(AssertionError):
        expect(spy).to.have_not.been_called

    with pytest.raises(AssertionError):
        expect(spy).to.have_not.been_called_once

    with pytest.raises(AssertionError):
        expect(spy).to.have_not.been_called_times(1)

    with pytest.raises(AssertionError):
        expect(spy).to.have_not.been_called_with('/home/log.txt')

    with pytest.raises(AssertionError):
        expect(spy).to.have_not.been_called_once_with('/home/log.txt')

def test_been_called_with_a_stub(expect, mocker):
    def foo(on_something):
        on_something()

    stub = mocker.stub('on_something_stub')
    foo(stub)

    expect(stub).to.have.been_called
    # stubs are function like spies, we do not need to test everything again

def test_been_called_with_an_incompatible_object(expect, mocker):
    def foo():
        pass

    foo()

    with pytest.raises(AssertionError):
        expect(foo).to.have.been_called

    with pytest.raises(AssertionError):
        expect(foo).to.have.been_called_once

    with pytest.raises(AssertionError):
        expect(foo).to.have.been_called_times(1)

    with pytest.raises(AssertionError):
        expect(foo).to.have.been_called_with('something')

    with pytest.raises(AssertionError):
        expect(foo).to.have.been_called_once_with('something')
<commit_msg>add mocker stop all<commit_after>import pytest
import os

def test_been_called(expect, mocker):
    mock_called = mocker.patch('os.remove')
    os.remove('/home/log.txt')

    expect(mock_called).to.have.been_called

    with pytest.raises(AssertionError):
        expect(mock_called).to.have_not.been_called

    mock_not_called = mocker.patch('os.rmdir')

    expect(mock_not_called).to.have_not.been_called

    with pytest.raises(AssertionError):
        expect(mock_not_called).to.have.been_called

    mock_called_several_times = mocker.patch('os.rename')
    os.rename('/home/log.txt', '/home/log_new.txt')
    os.rename('/home/log.txt', '/home/log_new.txt')

    expect(mock_called_several_times).to.have.been_called

    with pytest.raises(AssertionError):
        expect(mock_called_several_times).to.have_not.been_called

    mocker.stopall()

def test_been_called_times(expect, mocker):
    mock_called = mocker.patch('os.remove')
    os.remove('/home/log.txt')
    os.remove('/home/log.txt')
    os.remove('/home/log.txt')

    expect(mock_called).to.have.been_called_times(3)

    with pytest.raises(AssertionError):
        expect(mock_called).to.have_not.been_called_times(3)

    mock_not_called = mocker.patch('os.rmdir')

    expect(mock_not_called).to.have.been_called_times(0)
    expect(mock_not_called).to.have_not.been_called_times(3)

    with pytest.raises(AssertionError):
        expect(mock_not_called).to.have.been_called_times(3)

    mocker.stopall()

def test_been_called_with(expect, mocker):
    mock_called = mocker.patch('os.remove')
    os.remove('/home/log.txt')

    expect(mock_called).to.have.been_called_with('/home/log.txt')

    with pytest.raises(AssertionError):
        expect(mock_called).to.have_not.been_called_with('/home/log.txt')

    mock_not_called = mocker.patch('os.rmdir')

    expect(mock_not_called).to.have_not.been_called_with('/home/log.txt')

    with pytest.raises(AssertionError):
        expect(mock_not_called).to.have.been_called_with('/home/log.txt')

    mocker.stopall()

def test_been_called_once(expect, mocker):
    mock_called = mocker.patch('os.remove')
    os.remove('/home/log.txt')

    expect(mock_called).to.have.been_called_once

    with pytest.raises(AssertionError):
        expect(mock_called).to.have_not.been_called_once

    mock_not_called = mocker.patch('os.rmdir')

    expect(mock_not_called).to.have_not.been_called_once

    with pytest.raises(AssertionError):
        expect(mock_not_called).to.have.been_called_once

    mock_called_several_times = mocker.patch('os.rename')
    os.rename('/home/log.txt', '/home/log_new.txt')
    os.rename('/home/log.txt', '/home/log_new.txt')

    expect(mock_called_several_times).to.have_not.been_called_once

    with pytest.raises(AssertionError):
        expect(mock_called_several_times).to.have.been_called_once

    mocker.stopall()

def test_been_called_once_with(expect, mocker):
    mock_called = mocker.patch('os.remove')
    os.remove('/home/log.txt')

    expect(mock_called).to.have.been_called_once_with('/home/log.txt')

    with pytest.raises(AssertionError):
        expect(mock_called).to.have_not.been_called_once_with('/home/log.txt')

    mock_not_called = mocker.patch('os.rmdir')

    expect(mock_not_called).to.have_not.been_called_once_with('/home/log.txt')

    with pytest.raises(AssertionError):
        expect(mock_not_called).to.have.been_called_once_with('/home/log.txt')

    mock_called_several_times = mocker.patch('os.rename')
    os.rename('/home/log.txt', '/home/log_new.txt')
    os.rename('/home/log.txt', '/home/log_new.txt')

    expect(mock_called_several_times).to.have_not.been_called_once

    with pytest.raises(AssertionError):
        expect(mock_called_several_times).to.have.been_called_once

    mocker.stopall()

def test_been_called_with_a_spy(expect, mocker):
    spy = mocker.spy(os.path, 'basename')
    os.path.basename('/home/log.txt')

    expect(spy).to.have.been_called
    expect(spy).to.have.been_called_once
    expect(spy).to.have.been_called_times(1)
    expect(spy).to.have.been_called_with('/home/log.txt')
    expect(spy).to.have.been_called_once_with('/home/log.txt')

    with pytest.raises(AssertionError):
        expect(spy).to.have_not.been_called

    with pytest.raises(AssertionError):
        expect(spy).to.have_not.been_called_once

    with pytest.raises(AssertionError):
        expect(spy).to.have_not.been_called_times(1)

    with pytest.raises(AssertionError):
        expect(spy).to.have_not.been_called_with('/home/log.txt')

    with pytest.raises(AssertionError):
        expect(spy).to.have_not.been_called_once_with('/home/log.txt')

    mocker.stopall()

def test_been_called_with_a_stub(expect, mocker):
    def foo(on_something):
        on_something()

    stub = mocker.stub('on_something_stub')
    foo(stub)

    expect(stub).to.have.been_called
    # stubs are function like spies, we do not need to test everything again

    mocker.stopall()

def test_been_called_with_an_incompatible_object(expect, mocker):
    def foo():
        pass

    foo()

    with pytest.raises(AssertionError):
        expect(foo).to.have.been_called

    with pytest.raises(AssertionError):
        expect(foo).to.have.been_called_once

    with pytest.raises(AssertionError):
        expect(foo).to.have.been_called_times(1)

    with pytest.raises(AssertionError):
        expect(foo).to.have.been_called_with('something')

    with pytest.raises(AssertionError):
        expect(foo).to.have.been_called_once_with('something')
<|endoftext|>


<commit_before>import matplotlib.pyplot as plt
import numpy as np
from matplotlib import rcParams
from matplotlib.colors import ColorConverter

def paper_figure(figsize, subplots=None, **kwargs):
    """Define default values for font, fontsize and use latex"""

    def cm2inch(cm_tupl):
        """Convert cm to inches"""
        inch = 2.54
        return (cm / inch for cm in cm_tupl)

    if subplots is None:
        fig = plt.figure(figsize=cm2inch(figsize))
    else:
        fig, ax = plt.subplots(subplots[0], subplots[1],
                               figsize=cm2inch(figsize), **kwargs)

    # Parameters for IJRR
    params = {
              'font.family': 'serif',
              'font.serif': ['Times',
                             'Palatino',
                             'New Century Schoolbook',
                             'Bookman',
                             'Computer Modern Roman'],
              'font.sans-serif': ['Times',
                                  'Helvetica',
                                  'Avant Garde',
                                  'Computer Modern Sans serif'],
              'text.usetex': True,
              # Make sure mathcal doesn't use the Times style
              'text.latex.preamble':
                  r'\DeclareMathAlphabet{\mathcal}{OMS}{cmsy}{m}{n}',

              'axes.labelsize': 9,
              'axes.linewidth': .75,

              'font.size': 9,
              'legend.fontsize': 9,
              'xtick.labelsize': 8,
              'ytick.labelsize': 8,

              # 'figure.dpi': 150,
              # 'savefig.dpi': 600,
              'legend.numpoints': 1,
              }

    rcParams.update(params)

    if subplots is None:
        return fig
    else:
        return fig, ax

def format_figure(axis, cbar=None):
    axis.spines['top'].set_linewidth(0.1)
    axis.spines['top'].set_alpha(0.5)
    axis.spines['right'].set_linewidth(0.1)
    axis.spines['right'].set_alpha(0.5)
    axis.xaxis.set_ticks_position('bottom')
    axis.yaxis.set_ticks_position('left')

    axis.set_xticks(np.arange(0, 121, 30))
    yticks = np.arange(0, 71, 35)
    axis.set_yticks(yticks)
    axis.set_yticklabels(['{0}'.format(tick) for tick in yticks[::-1]])

    axis.set_xlabel(r'distance [m]')
    axis.set_ylabel(r'distance [m]', labelpad=2)
    if cbar is not None:
        cbar.set_label(r'altitude [m]')

        cbar.set_ticks(np.arange(0, 36, 10))

        for spine in cbar.ax.spines.itervalues():
            spine.set_linewidth(0.1)
        cbar.ax.yaxis.set_tick_params(color=emulate_color('k', 0.7))

    plt.tight_layout(pad=0.1)

def emulate_color(color, alpha=1, background_color=(1, 1, 1)):
    """Take an RGBA color and an RGB background, return the emulated RGB color.

    The RGBA color with transparency alpha is converted to an RGB color via
    emulation in front of the background_color.
    """
    to_rgb = ColorConverter().to_rgb
    color = to_rgb(color)
    background_color = to_rgb(background_color)
    return [(1 - alpha) * bg_col + alpha * col
            for col, bg_col in zip(color, background_color)]

def plot_paper(altitudes, S_hat, world_shape, fileName=""):
    """
    Plots for NIPS paper
    Parameters
    ----------
    altitudes: np.array
        True value of the altitudes of the map
    S_hat: np.array
        Safe and ergodic set
    world_shape: tuple
        Size of the grid world (rows, columns)
    fileName: string
        Name of the file to save the plot
    Returns
    -------

    """
    # Size of figures and colormap
    tw = cw = 13.968
    cmap = 'jet'
    alpha = 1.
    alpha_world = 0.25
    size_wb = np.array([cw / 2.2, tw / 4.])
    #size_wb = np.array([cw / 4.2, cw / 4.2])

    # Shift altitudes
    altitudes -= np.nanmin(altitudes)
    vmin, vmax = (np.nanmin(altitudes), np.nanmax(altitudes))
    origin = 'lower'

    fig = paper_figure(size_wb)

    # Copy altitudes for different alpha values
    altitudes2 = altitudes.copy()
    altitudes2[~S_hat[:, 0]] = np.nan

    axis = fig.gca()

    # Plot world
    c = axis.imshow(np.reshape(altitudes, world_shape).T, origin=origin, vmin=vmin,
                    vmax=vmax, cmap=cmap, alpha=alpha_world)

    cbar = plt.colorbar(c)
    #cbar = None

    # Plot explored area
    plt.imshow(np.reshape(altitudes2, world_shape).T, origin=origin, vmin=vmin,
               vmax=vmax, interpolation='nearest', cmap=cmap, alpha=alpha)
    format_figure(axis, cbar)
    if fileName:
        plt.savefig(fileName, transparent=False, format="pdf")
    plt.show()

def plot_dist_from_C(mu, var, beta, altitudes, world_shape):
    """
    Image plot of the distance of the true safety feature from the
    confidence interval. Distance is equal to 0 if the true r(s) lies within
    C(s), it is > 0 if r(s)>u(s) and < 0 if r(s)<l(s)

    Parameters
    ----------
    mu: np.array
        posterior mean over the altitudes in the map
    var:
        posterior mean over the altitudes in the map
    beta: float
        Scaling factor that controls the amplitude of the confidence iterval
    altitudes: np.array
        True value of the altitudes
    world_shape: tuple
        Size of the grid world (rows, columns)

    """

    # Lower and upper C on heights
    sigma = beta * np.sqrt(var)
    l = np.squeeze(mu - sigma)
    u = np.squeeze(mu + sigma)

    # Initialize
    dist_from_confidence_interval = np.zeros(altitudes.size, dtype=float)

    # Above u
    diff_u = altitudes - u
    dist_from_confidence_interval[ diff_u > 0] = diff_u[diff_u > 0]

    # Below l
    diff_l = altitudes - l
    dist_from_confidence_interval[diff_l < 0] = diff_l[diff_l < 0]

    # Define limits
    max_value = np.max(dist_from_confidence_interval)
    min_value = np.min(dist_from_confidence_interval)
    limit = np.max([max_value, np.abs(min_value)])

    # Plot
    plt.figure()
    plt.imshow(
        np.reshape(dist_from_confidence_interval, world_shape).T, origin='lower',
        interpolation='nearest', vmin=-limit, vmax=limit)
    title = "Distance from confidence interval"
    plt.title(title)
    plt.colorbar()
    plt.show()

def plot_coverage(coverage_over_t):
    """
    Plots coverage of true_S_hat_epsilon as a function of time

    """
    plt.figure()
    plt.plot(coverage_over_t)
    title = "Coverage over time"
    plt.title(title)
    plt.show()

<commit_msg>Minor change in plot for paper<commit_after>import matplotlib.pyplot as plt
import numpy as np
from matplotlib import rcParams
from matplotlib.colors import ColorConverter

def paper_figure(figsize, subplots=None, **kwargs):
    """Define default values for font, fontsize and use latex"""

    def cm2inch(cm_tupl):
        """Convert cm to inches"""
        inch = 2.54
        return (cm / inch for cm in cm_tupl)

    if subplots is None:
        fig = plt.figure(figsize=cm2inch(figsize))
    else:
        fig, ax = plt.subplots(subplots[0], subplots[1],
                               figsize=cm2inch(figsize), **kwargs)

    # Parameters for IJRR
    params = {
              'font.family': 'serif',
              'font.serif': ['Times',
                             'Palatino',
                             'New Century Schoolbook',
                             'Bookman',
                             'Computer Modern Roman'],
              'font.sans-serif': ['Times',
                                  'Helvetica',
                                  'Avant Garde',
                                  'Computer Modern Sans serif'],
              'text.usetex': True,
              # Make sure mathcal doesn't use the Times style
              'text.latex.preamble':
                  r'\DeclareMathAlphabet{\mathcal}{OMS}{cmsy}{m}{n}',

              'axes.labelsize': 9,
              'axes.linewidth': .75,

              'font.size': 9,
              'legend.fontsize': 9,
              'xtick.labelsize': 8,
              'ytick.labelsize': 8,

              # 'figure.dpi': 150,
              # 'savefig.dpi': 600,
              'legend.numpoints': 1,
              }

    rcParams.update(params)

    if subplots is None:
        return fig
    else:
        return fig, ax

def format_figure(axis, cbar=None):
    axis.spines['top'].set_linewidth(0.1)
    axis.spines['top'].set_alpha(0.5)
    axis.spines['right'].set_linewidth(0.1)
    axis.spines['right'].set_alpha(0.5)
    axis.xaxis.set_ticks_position('bottom')
    axis.yaxis.set_ticks_position('left')

    axis.set_xticks(np.arange(0, 121, 30))
    yticks = np.arange(0, 71, 35)
    axis.set_yticks(yticks)
    axis.set_yticklabels(['{0}'.format(tick) for tick in yticks[::-1]])

    axis.set_xlabel(r'distance [m]')
    axis.set_ylabel(r'distance [m]', labelpad=2)
    if cbar is not None:
        cbar.set_label(r'altitude [m]')

        cbar.set_ticks(np.arange(0, 36, 10))

        for spine in cbar.ax.spines.itervalues():
            spine.set_linewidth(0.1)
        cbar.ax.yaxis.set_tick_params(color=emulate_color('k', 0.7))

    plt.tight_layout(pad=0.1)

def emulate_color(color, alpha=1, background_color=(1, 1, 1)):
    """Take an RGBA color and an RGB background, return the emulated RGB color.

    The RGBA color with transparency alpha is converted to an RGB color via
    emulation in front of the background_color.
    """
    to_rgb = ColorConverter().to_rgb
    color = to_rgb(color)
    background_color = to_rgb(background_color)
    return [(1 - alpha) * bg_col + alpha * col
            for col, bg_col in zip(color, background_color)]

def plot_paper(altitudes, S_hat, world_shape, fileName=""):
    """
    Plots for NIPS paper
    Parameters
    ----------
    altitudes: np.array
        True value of the altitudes of the map
    S_hat: np.array
        Safe and ergodic set
    world_shape: tuple
        Size of the grid world (rows, columns)
    fileName: string
        Name of the file to save the plot. If empty string the plot is not
        saved
    Returns
    -------

    """
    # Size of figures and colormap
    tw = cw = 13.968
    cmap = 'jet'
    alpha = 1.
    alpha_world = 0.25
    size_wb = np.array([cw / 2.2, tw / 4.])
    #size_wb = np.array([cw / 4.2, cw / 4.2])

    # Shift altitudes
    altitudes -= np.nanmin(altitudes)
    vmin, vmax = (np.nanmin(altitudes), np.nanmax(altitudes))
    origin = 'lower'

    fig = paper_figure(size_wb)

    # Copy altitudes for different alpha values
    altitudes2 = altitudes.copy()
    altitudes2[~S_hat[:, 0]] = np.nan

    axis = fig.gca()

    # Plot world
    c = axis.imshow(np.reshape(altitudes, world_shape).T, origin=origin, vmin=vmin,
                    vmax=vmax, cmap=cmap, alpha=alpha_world)

    cbar = plt.colorbar(c)
    #cbar = None

    # Plot explored area
    plt.imshow(np.reshape(altitudes2, world_shape).T, origin=origin, vmin=vmin,
               vmax=vmax, interpolation='nearest', cmap=cmap, alpha=alpha)
    format_figure(axis, cbar)

    # Save figure
    if fileName:
        plt.savefig(fileName, transparent=False, format="pdf")
    plt.show()

def plot_dist_from_C(mu, var, beta, altitudes, world_shape):
    """
    Image plot of the distance of the true safety feature from the
    confidence interval. Distance is equal to 0 if the true r(s) lies within
    C(s), it is > 0 if r(s)>u(s) and < 0 if r(s)<l(s)

    Parameters
    ----------
    mu: np.array
        posterior mean over the altitudes in the map
    var:
        posterior mean over the altitudes in the map
    beta: float
        Scaling factor that controls the amplitude of the confidence iterval
    altitudes: np.array
        True value of the altitudes
    world_shape: tuple
        Size of the grid world (rows, columns)

    """

    # Lower and upper C on heights
    sigma = beta * np.sqrt(var)
    l = np.squeeze(mu - sigma)
    u = np.squeeze(mu + sigma)

    # Initialize
    dist_from_confidence_interval = np.zeros(altitudes.size, dtype=float)

    # Above u
    diff_u = altitudes - u
    dist_from_confidence_interval[ diff_u > 0] = diff_u[diff_u > 0]

    # Below l
    diff_l = altitudes - l
    dist_from_confidence_interval[diff_l < 0] = diff_l[diff_l < 0]

    # Define limits
    max_value = np.max(dist_from_confidence_interval)
    min_value = np.min(dist_from_confidence_interval)
    limit = np.max([max_value, np.abs(min_value)])

    # Plot
    plt.figure()
    plt.imshow(
        np.reshape(dist_from_confidence_interval, world_shape).T, origin='lower',
        interpolation='nearest', vmin=-limit, vmax=limit)
    title = "Distance from confidence interval"
    plt.title(title)
    plt.colorbar()
    plt.show()

def plot_coverage(coverage_over_t):
    """
    Plots coverage of true_S_hat_epsilon as a function of time

    """
    plt.figure()
    plt.plot(coverage_over_t)
    title = "Coverage over time"
    plt.title(title)
    plt.show()

<|endoftext|>


<commit_before># coding: utf-8

from __future__ import unicode_literals
import json
from django.contrib.contenttypes.generic import GenericRelation
from django.core import serializers
from django.db.models import ManyToManyField, Manager, Model
from django.db.models.query import QuerySet
from django.db.models.related import RelatedObject
from django.utils import six
from django.utils.encoding import smart_text
from libretto.api.utils.console import info, colored_diff, error
from typography.utils import replace
from ..utils import notify_send, print_info

def clean_string(string):
    return string.strip()

def get_obj_contents(obj):
    contents = {}
    for k in [f.name for f in obj._meta.fields if f is not obj._meta.pk]:
        v = getattr(obj, k)
        if v:
            contents[k] = v
    return contents

def pprintable_dict(d):
    return '(%s)' % ', '.join('%s=%s' % (k, smart_text(repr(v)))
                              for k, v in d.items())

def ask_for_choice(intro, choices, start=1, allow_empty=False, default=None):
    notify_send(intro)
    print_info(intro)

    question = 'Que choisir{} ? '.format(
        '' if default is None else ' (par défaut {})'.format(default))

    for i, obj in enumerate(choices, start=start):
        if isinstance(obj, tuple):
            obj, msg = obj
            s = '{} {}'.format(obj, info(msg))
        else:
            s = smart_text(obj)
        out = '{} {}'.format(info('{}.'.format(i)), s)
        if isinstance(obj, Model):
            out += ' ' + pprintable_dict(get_obj_contents(obj))
        print(out)

    input_func = raw_input if six.PY2 else input
    while True:
        choice = input_func(info(question).encode('utf-8'))
        if choice.isdigit():
            choice = int(choice)
            if 0 <= choice - start < len(choices):
                return choice - start
        elif allow_empty and not choice:
            return default if default is None else default - start
        print(error('Choix invalide !'))

def serialize(l):
    serializations = []
    for item in l:
        if isinstance(item, QuerySet):
            serializations.append(serializers.serialize('json', item))
        elif isinstance(item, Model):
            serializations.append(serializers.serialize('json', [item]))
        else:
            serializations.append(json.dumps(item))
    return ','.join(serializations)

OLD_NOW_OR_CREATE_CACHE = {}

def ask_for_old_new_or_create(obj, k, v, new_v):
    cache_key = serialize([obj, k, v, new_v])
    if cache_key in OLD_NOW_OR_CREATE_CACHE:
        return OLD_NOW_OR_CREATE_CACHE[cache_key]

    intro = 'Deux possibilités pour le champ {0} de {1}'.format(k, obj)
    v, new_v = colored_diff(smart_text(v), smart_text(new_v))
    choices = (
        (v, 'valeur actuelle'),
        (new_v, 'valeur importable'),
        'Créer un nouvel objet',
    )
    result = ask_for_choice(intro, choices, allow_empty=True, default=1)
    OLD_NOW_OR_CREATE_CACHE[cache_key] = result
    return result

MANY_RELATED_FIELDS = (ManyToManyField, GenericRelation)

def is_many_related_field(object_or_model, field_name):
    # FIXME: Due to a bug in Django, we can’t fetch a GenericRelation using
    #        `model._meta.get_field` since Django 1.6.
    field = object_or_model._meta.get_field_by_name(field_name)[0]
    if isinstance(field, RelatedObject):
        field = field.field
    return isinstance(field, MANY_RELATED_FIELDS)

def is_str(v):
    return isinstance(v, six.string_types)

def get_field_cmp_value(obj, k, v):
    if is_many_related_field(obj, k):
        return v.all()
    elif is_str(v):
        return smart_text(v)
    return v

def get_field_settable_value(new_v):
    if is_str(new_v):
        return replace(smart_text(new_v))
    return new_v

def get_changed_kwargs(obj, new_kwargs):
    changed_kwargs = {}

    for k, new_v in new_kwargs.items():
        old_v = getattr(obj, k)
        old_v = get_field_cmp_value(obj, k, old_v)
        new_v = get_field_settable_value(new_v)

        if old_v != new_v or not are_sequences_equal(old_v, new_v):
            changed_kwargs[k] = new_v

    return changed_kwargs

def separate_m2m_kwargs(Model, filter_kwargs):
    filter_kwargs = filter_kwargs.copy()
    m2m_kwargs = {k: v for k, v in filter_kwargs.items()
                  if is_many_related_field(Model, k.split('__')[0])}
    for k in m2m_kwargs:
        del filter_kwargs[k]
    return filter_kwargs, m2m_kwargs

def enlarged_filter(Model, filter_kwargs):
    filter_kwargs, m2m_kwargs = separate_m2m_kwargs(Model, filter_kwargs)
    qs = Model.objects.filter(**filter_kwargs)
    excluded_pk_list = []
    for k, v in m2m_kwargs.items():
        for obj in qs:
            if not are_sequences_equal(access_related(obj, k), v):
                excluded_pk_list.append(obj.pk)
    return qs.exclude(pk__in=excluded_pk_list)

ENLARGED_GET_CACHE = {}

def enlarged_get(Model, filter_kwargs):
    qs = enlarged_filter(Model, filter_kwargs)
    n = qs.count()
    if n <= 1:
        return qs.get()

    cache_key = serialize([filter_kwargs, qs])
    if cache_key in ENLARGED_GET_CACHE:
        return ENLARGED_GET_CACHE[cache_key]

    intro = '%d objets trouvés pour les arguments %s' \
            % (n, pprintable_dict(filter_kwargs))
    ENLARGED_GET_CACHE[cache_key] = result = qs[ask_for_choice(intro, qs)]
    return result

def enlarged_create(Model, filter_kwargs, commit=True):
    filter_kwargs, m2m_kwargs = separate_m2m_kwargs(Model, filter_kwargs)
    obj = Model(**filter_kwargs)
    if commit:
        obj.save()
    for k, v in m2m_kwargs.items():
        getattr(obj, k).add(*v)
    return obj

def access_related(v, lookup):
    base_k = lookup.split('__')[0]
    new_lookup = '__'.join(lookup.split('__')[1:])
    if isinstance(v, Manager):
        v = v.all()
    if is_sequence(v):
        new_v = []
        for item in v:
            item = access_related(item, lookup)
            if is_sequence(item):
                new_v.extend(item)
            else:
                new_v.append(item)
        return new_v
    v = getattr(v, base_k)
    if isinstance(v, Manager):
        v = v.all()
    if not new_lookup:
        return v
    return access_related(v, new_lookup)

def get_or_create(Model, filter_kwargs, unique_keys=(), commit=True):
    if unique_keys:
        unique_kwargs = {}
        for k in unique_keys:
            base_k = k.split('__')[0]
            lookup = '__'.join(k.split('__')[1:])
            if base_k not in filter_kwargs:
                continue
            v = filter_kwargs[base_k]
            if lookup:
                v = access_related(v, lookup)
            unique_kwargs[k] = v
    else:
        unique_kwargs = filter_kwargs
    try:
        return enlarged_get(Model, unique_kwargs), False
    except Model.DoesNotExist:
        return enlarged_create(Model, filter_kwargs, commit=commit), True

def is_sequence(v):
    return isinstance(v, (QuerySet, list, tuple))

def are_sequences_equal(seq, other_seq):
    if not (is_sequence(seq) and is_sequence(other_seq)):
        return

    seq = list(seq)
    other_seq = list(other_seq)

    if len(seq) != len(other_seq):
        return False

    for seq_item in seq:
        if seq_item not in other_seq:
            return False
        seq.remove(seq_item)
        other_seq.remove(seq_item)
    return True

# Conflict handling variables.
INTERACTIVE = 'interactive'
KEEP = 'keep'
OVERRIDE = 'override'
CREATE = 'create'
CONFLICT_HANDLINGS = (INTERACTIVE, KEEP, OVERRIDE, CREATE)

def update_or_create(Model, filter_kwargs, unique_keys=(), commit=True,
                     conflict_handling=INTERACTIVE):

    if conflict_handling not in CONFLICT_HANDLINGS:
        raise Exception('`conflict_handling` must be in CONFLICT_HANDLINGS.')

    obj, created = get_or_create(Model, filter_kwargs, unique_keys=unique_keys,
                                 commit=commit)
    if created:
        return obj

    changed_kwargs = get_changed_kwargs(obj, filter_kwargs)
    if not changed_kwargs:
        return obj

    for k, new_v in changed_kwargs.items():
        if new_v in ('', None):
            continue

        old_v = getattr(obj, k)
        old_v = get_field_cmp_value(obj, k, old_v)

        if old_v == new_v or are_sequences_equal(old_v, new_v):
            continue

        if old_v:

            choice = None
            if hasattr(old_v, 'is_more_precise_than'):
                if old_v.is_more_precise_than(new_v):
                    choice = KEEP

            if choice is None and conflict_handling == INTERACTIVE:
                choice = ask_for_old_new_or_create(obj, k, old_v, new_v)
                choice = {0: KEEP, 1: OVERRIDE, 2: CREATE}[choice]
            else:
                choice = conflict_handling

            if choice == KEEP:
                break
            elif choice == OVERRIDE:
                setattr(obj, k, new_v)
                break
            elif choice == CREATE:
                return enlarged_create(Model, filter_kwargs, commit=commit)

        else:
            if is_many_related_field(obj, k):
                getattr(obj, k).add(*new_v)
            else:
                setattr(obj, k, new_v)

    if commit:
        obj.save()
    return obj
<commit_msg>Corrige les sérialisations imbriquées.<commit_after># coding: utf-8

from __future__ import unicode_literals
from collections import OrderedDict
import json
from django.contrib.contenttypes.generic import GenericRelation
from django.core import serializers
from django.db.models import ManyToManyField, Manager, Model
from django.db.models.query import QuerySet
from django.db.models.related import RelatedObject
from django.utils import six
from django.utils.encoding import smart_text
from libretto.api.utils.console import info, colored_diff, error
from typography.utils import replace
from ..utils import notify_send, print_info

def clean_string(string):
    return string.strip()

def get_obj_contents(obj):
    contents = {}
    for k in [f.name for f in obj._meta.fields if f is not obj._meta.pk]:
        v = getattr(obj, k)
        if v:
            contents[k] = v
    return contents

def pprintable_dict(d):
    return '(%s)' % ', '.join('%s=%s' % (k, smart_text(repr(v)))
                              for k, v in d.items())

def ask_for_choice(intro, choices, start=1, allow_empty=False, default=None):
    notify_send(intro)
    print_info(intro)

    question = 'Que choisir{} ? '.format(
        '' if default is None else ' (par défaut {})'.format(default))

    for i, obj in enumerate(choices, start=start):
        if isinstance(obj, tuple):
            obj, msg = obj
            s = '{} {}'.format(obj, info(msg))
        else:
            s = smart_text(obj)
        out = '{} {}'.format(info('{}.'.format(i)), s)
        if isinstance(obj, Model):
            out += ' ' + pprintable_dict(get_obj_contents(obj))
        print(out)

    input_func = raw_input if six.PY2 else input
    while True:
        choice = input_func(info(question).encode('utf-8'))
        if choice.isdigit():
            choice = int(choice)
            if 0 <= choice - start < len(choices):
                return choice - start
        elif allow_empty and not choice:
            return default if default is None else default - start
        print(error('Choix invalide !'))

def serialize(l):
    serializations = []
    for item in l:
        if isinstance(item, QuerySet):
            serializations.append(serializers.serialize('json', item))
        elif isinstance(item, Model):
            serializations.append(serializers.serialize('json', [item]))
        elif isinstance(item, (list, tuple)):
            serializations.append(serialize(item))
        elif isinstance(item, dict):
            item = OrderedDict(item)
            serializations.append(
                '{%s}' % ', '.join('%s: %s' % (k, v) for k, v in
                                   zip(item.keys(), serialize(item.values()))))
        else:
            serializations.append(json.dumps(item))
    return ','.join(serializations)

OLD_NOW_OR_CREATE_CACHE = {}

def ask_for_old_new_or_create(obj, k, v, new_v):
    cache_key = serialize([obj, k, v, new_v])
    if cache_key in OLD_NOW_OR_CREATE_CACHE:
        return OLD_NOW_OR_CREATE_CACHE[cache_key]

    intro = 'Deux possibilités pour le champ {0} de {1}'.format(k, obj)
    v, new_v = colored_diff(smart_text(v), smart_text(new_v))
    choices = (
        (v, 'valeur actuelle'),
        (new_v, 'valeur importable'),
        'Créer un nouvel objet',
    )
    result = ask_for_choice(intro, choices, allow_empty=True, default=1)
    OLD_NOW_OR_CREATE_CACHE[cache_key] = result
    return result

MANY_RELATED_FIELDS = (ManyToManyField, GenericRelation)

def is_many_related_field(object_or_model, field_name):
    # FIXME: Due to a bug in Django, we can’t fetch a GenericRelation using
    #        `model._meta.get_field` since Django 1.6.
    field = object_or_model._meta.get_field_by_name(field_name)[0]
    if isinstance(field, RelatedObject):
        field = field.field
    return isinstance(field, MANY_RELATED_FIELDS)

def is_str(v):
    return isinstance(v, six.string_types)

def get_field_cmp_value(obj, k, v):
    if is_many_related_field(obj, k):
        return v.all()
    elif is_str(v):
        return smart_text(v)
    return v

def get_field_settable_value(new_v):
    if is_str(new_v):
        return replace(smart_text(new_v))
    return new_v

def get_changed_kwargs(obj, new_kwargs):
    changed_kwargs = {}

    for k, new_v in new_kwargs.items():
        old_v = getattr(obj, k)
        old_v = get_field_cmp_value(obj, k, old_v)
        new_v = get_field_settable_value(new_v)

        if old_v != new_v or not are_sequences_equal(old_v, new_v):
            changed_kwargs[k] = new_v

    return changed_kwargs

def separate_m2m_kwargs(Model, filter_kwargs):
    filter_kwargs = filter_kwargs.copy()
    m2m_kwargs = {k: v for k, v in filter_kwargs.items()
                  if is_many_related_field(Model, k.split('__')[0])}
    for k in m2m_kwargs:
        del filter_kwargs[k]
    return filter_kwargs, m2m_kwargs

def enlarged_filter(Model, filter_kwargs):
    filter_kwargs, m2m_kwargs = separate_m2m_kwargs(Model, filter_kwargs)
    qs = Model.objects.filter(**filter_kwargs)
    excluded_pk_list = []
    for k, v in m2m_kwargs.items():
        for obj in qs:
            if not are_sequences_equal(access_related(obj, k), v):
                excluded_pk_list.append(obj.pk)
    return qs.exclude(pk__in=excluded_pk_list)

ENLARGED_GET_CACHE = {}

def enlarged_get(Model, filter_kwargs):
    qs = enlarged_filter(Model, filter_kwargs)
    n = qs.count()
    if n <= 1:
        return qs.get()

    cache_key = serialize([filter_kwargs, qs])
    if cache_key in ENLARGED_GET_CACHE:
        return ENLARGED_GET_CACHE[cache_key]

    intro = '%d objets trouvés pour les arguments %s' \
            % (n, pprintable_dict(filter_kwargs))
    ENLARGED_GET_CACHE[cache_key] = result = qs[ask_for_choice(intro, qs)]
    return result

def enlarged_create(Model, filter_kwargs, commit=True):
    filter_kwargs, m2m_kwargs = separate_m2m_kwargs(Model, filter_kwargs)
    obj = Model(**filter_kwargs)
    if commit:
        obj.save()
    for k, v in m2m_kwargs.items():
        getattr(obj, k).add(*v)
    return obj

def access_related(v, lookup):
    base_k = lookup.split('__')[0]
    new_lookup = '__'.join(lookup.split('__')[1:])
    if isinstance(v, Manager):
        v = v.all()
    if is_sequence(v):
        new_v = []
        for item in v:
            item = access_related(item, lookup)
            if is_sequence(item):
                new_v.extend(item)
            else:
                new_v.append(item)
        return new_v
    v = getattr(v, base_k)
    if isinstance(v, Manager):
        v = v.all()
    if not new_lookup:
        return v
    return access_related(v, new_lookup)

def get_or_create(Model, filter_kwargs, unique_keys=(), commit=True):
    if unique_keys:
        unique_kwargs = {}
        for k in unique_keys:
            base_k = k.split('__')[0]
            lookup = '__'.join(k.split('__')[1:])
            if base_k not in filter_kwargs:
                continue
            v = filter_kwargs[base_k]
            if lookup:
                v = access_related(v, lookup)
            unique_kwargs[k] = v
    else:
        unique_kwargs = filter_kwargs
    try:
        return enlarged_get(Model, unique_kwargs), False
    except Model.DoesNotExist:
        return enlarged_create(Model, filter_kwargs, commit=commit), True

def is_sequence(v):
    return isinstance(v, (QuerySet, list, tuple))

def are_sequences_equal(seq, other_seq):
    if not (is_sequence(seq) and is_sequence(other_seq)):
        return

    seq = list(seq)
    other_seq = list(other_seq)

    if len(seq) != len(other_seq):
        return False

    for seq_item in seq:
        if seq_item not in other_seq:
            return False
        seq.remove(seq_item)
        other_seq.remove(seq_item)
    return True

# Conflict handling variables.
INTERACTIVE = 'interactive'
KEEP = 'keep'
OVERRIDE = 'override'
CREATE = 'create'
CONFLICT_HANDLINGS = (INTERACTIVE, KEEP, OVERRIDE, CREATE)

def update_or_create(Model, filter_kwargs, unique_keys=(), commit=True,
                     conflict_handling=INTERACTIVE):

    if conflict_handling not in CONFLICT_HANDLINGS:
        raise Exception('`conflict_handling` must be in CONFLICT_HANDLINGS.')

    obj, created = get_or_create(Model, filter_kwargs, unique_keys=unique_keys,
                                 commit=commit)
    if created:
        return obj

    changed_kwargs = get_changed_kwargs(obj, filter_kwargs)
    if not changed_kwargs:
        return obj

    for k, new_v in changed_kwargs.items():
        if new_v in ('', None):
            continue

        old_v = getattr(obj, k)
        old_v = get_field_cmp_value(obj, k, old_v)

        if old_v == new_v or are_sequences_equal(old_v, new_v):
            continue

        if old_v:

            choice = None
            if hasattr(old_v, 'is_more_precise_than'):
                if old_v.is_more_precise_than(new_v):
                    choice = KEEP

            if choice is None and conflict_handling == INTERACTIVE:
                choice = ask_for_old_new_or_create(obj, k, old_v, new_v)
                choice = {0: KEEP, 1: OVERRIDE, 2: CREATE}[choice]
            else:
                choice = conflict_handling

            if choice == KEEP:
                break
            elif choice == OVERRIDE:
                setattr(obj, k, new_v)
                break
            elif choice == CREATE:
                return enlarged_create(Model, filter_kwargs, commit=commit)

        else:
            if is_many_related_field(obj, k):
                getattr(obj, k).add(*new_v)
            else:
                setattr(obj, k, new_v)

    if commit:
        obj.save()
    return obj
<|endoftext|>


<commit_before># cell definition
# name = 'IfElse'
# libname = 'common'

tooltip = '''
IfElse
---
parameters:
    n_if_inp:  number of inputs for if_condition
    ifcond:    (comma separated) list of boolean equations
    channels:  number of channels (different signals)

example ifcond: '{i0} == 1, {i1} > {i0}'
'''

inp = 2
outp = 1

parameters = dict(n_if_inp=1, ifcond='{i0} == 1', channels=1)
properties = {} #voor netlisten

from  supsisim import const

def ports(param):
    '''return inputs, outputs and inouts'''
    n_if_inp = param['n_if_inp'] if 'n_if_inp' in param else 1
    ifcond =  param['ifcond'] if 'ifcond' in param else '{i0} == 1'
    ncond = len(ifcond.split(','))
    channels = param['channels'] if 'channels' in param else 1

    w2, y = 100, 0
    inp = []
    outp = []
    for i in range(n_if_inp):
        inp.append(('i{}'.format(i), -w2, y))
        y += const.PD
    y += const.PD # * (2 + ncond)
    for i in range(channels):
        for c in range(ncond+1):
            inp.append(('.if{}_{}'.format(i, c), -w2, y))
            if c == ncond:
                 outp.append(('.z{}'.format(i), w2, y))
            y += const.PD
        y += const.PD

    inout = []
    return inp, outp, inout

def getSymbol(param, properties, parent=None, scene=None,):
    from  supsisim import block, text
    ifcond =  param['ifcond'] if 'ifcond' in param else '{i0} == 1'
    ifcond = [i.strip() for i in ifcond.split(',')]
    ncond = len(ifcond)

    attributes = dict()
    attributes['name'] = name
    attributes['libname'] = libname
    inp, outp, _ = ports(param)
    attributes['input'] = inp
    attributes['output'] = outp
    attributes['icon'] = views['icon']
    b = block.Block(attributes,param,properties, name, libname, parent, scene)
    for p in b.ports():
        t = p.label.text()
        if t.startswith('.if'):
            c = int(t.split('_')[-1])
            tt = 'if' if c == 0 else 'else' if c == ncond else 'elif'
            if tt.endswith('if'):
                tt += ' {}'.format(ifcond[c])
            l = text.textItem(tt, anchor=4, parent=b)
            x, y = p.pos().x(), p.pos().y()
            l.setPos(x+5,y)
            l.setMutable(False)
#        elif t.startswith('i'):
#            i = int(t[1:])
#            if i == ncond - 1:
#                tt = []
#                for ix, cc in enumerate(ifcond):
#                    tt.append('{}: {}'.format(ix, cc))
#                tt.append('{}: otherwise'.format(ncond))
#                l = text.textItem('\n'.join(tt), anchor=7, parent=b)
#                x, y = p.pos().x(), p.pos().y()
#                l.setPos(x+5,y+5)
#                l.setMutable(False)

    return b

def toMyhdlInstance(instname, connectdict, param):
    # properties end up in the connectdict
    n_if_inp = param['n_if_inp'] if 'n_if_inp' in param else 1
    ifcond =  param['ifcond'] if 'ifcond' in param else '{i0} == 1'
    cond = [s.strip() for s in ifcond.split(',')]
    ncond = len(cond)
    channels = param['channels'] if 'channels' in param else 1

    inp, outp, _ = ports(param)
    dd = dict()
    for i in range(n_if_inp):
        nm = 'i{}'.format(i)
        dd[nm], _ = connectdict[nm] # store name in dict

    r =          '    @always_comb\n'
    r +=         '    def u_{inst}():\n'.format(inst=instname)
    for c in range(ncond+1):
        if c == 0:
            r += '        if {}:\n'.format(cond[c].format(**dd))
        elif c == ncond:
            r += '        else:\n'
        else:
            r += '        elif {}:\n'.format(cond[c].format(**dd))
        for i in range(channels):
            a, tpa = connectdict['.if{}_{}'.format(i, c)]
            z, tpz = connectdict['.z{}'.format(i)]
            if '{' not in z:
                r += '            {}.next = {}\n'.format(z, a)

    ex = []
    d = dict()
    for i in range(channels):
        z, tpz = connectdict['.z{}'.format(i)]
        if '{' in z: # inline expression
            for c in range(ncond+1):
                a, tpa = connectdict['.if{}_{}'.format(i, c)]
                if c == 0:
                    print ('ifelse debug 4')
                    ex.append('{} if {}'.format(a, cond[c].format(**dd)))
                    print ('ifelse debug 5')
                elif c == ncond:
                    ex.append('else {}'.format(a))
                else:
                    ex.append('{} else if {}'.format(a, cond[c].format(**dd)))
            d[z] = ' '.join(ex)
    print (d)
    if d:
        if '.next' in r:
            return dict(__expr__ = d, __main__ = r)
        else:
            return dict(__expr__ = d)
    else:
        return r

views = {}
<commit_msg>added optional clocking<commit_after># cell definition
# name = 'IfElse'
# libname = 'common'

tooltip = '''
IfElse
---
parameters:
    n_if_inp:  number of inputs for if_condition
    ifcond:    (comma separated) list of boolean equations
    channels:  number of channels (different signals)

example ifcond: '{i0} == 1, {i1} > {i0}'
'''

inp = 2
outp = 1

parameters = dict(n_if_inp=1, ifcond='{i0} == 1', channels=1, clk_edge = ('None', ['None', 'pos', 'neg']), reset=('False', ['False', 'True']))
properties = {} #voor netlisten

from  supsisim import const

def ports(param):
    '''return inputs, outputs and inouts'''
    n_if_inp = param['n_if_inp'] if 'n_if_inp' in param else 1
    ifcond =  param['ifcond'] if 'ifcond' in param else '{i0} == 1'
    ncond = len(ifcond.split(','))
    channels = param['channels'] if 'channels' in param else 1
    clk_edge = param['clk_edge'][0] if 'clk_edge' in param else 'None'
    reset = param['reset'][0] if 'reset' in param else 'False'

    w2, y = 100, 0
    inp = []
    outp = []
    for i in range(n_if_inp):
        inp.append(('i{}'.format(i), -w2, y))
        y += const.PD
    y += const.PD # * (2 + ncond)
    for i in range(channels):
        for c in range(ncond+1):
            inp.append(('.if{}_{}'.format(i, c), -w2, y))
            if c == ncond:
                 outp.append(('.z{}'.format(i), w2, y))
            y += const.PD
        y += const.PD
    y += const.PD
    if clk_edge == 'pos':
        inp.append(('ck', -w2, y))
        y += const.PD
    elif clk_edge == 'neg':
        inp.append(('ckn', -w2, y))
        y += const.PD

    if clk_edge in ['pos', 'neg'] and reset == 'True':
        inp.append(('rst', -w2, y))

    inout = []
    return inp, outp, inout

def getSymbol(param, properties, parent=None, scene=None,):
    from  supsisim import block, text
    ifcond =  param['ifcond'] if 'ifcond' in param else '{i0} == 1'
    ifcond = [i.strip() for i in ifcond.split(',')]
    ncond = len(ifcond)

    attributes = dict()
    attributes['name'] = name
    attributes['libname'] = libname
    inp, outp, _ = ports(param)
    attributes['input'] = inp
    attributes['output'] = outp
    attributes['icon'] = views['icon']
    b = block.Block(attributes,param,properties, name, libname, parent, scene)
    for p in b.ports():
        t = p.label.text()
        if t.startswith('.if'):
            c = int(t.split('_')[-1])
            tt = 'if' if c == 0 else 'else' if c == ncond else 'elif'
            if tt.endswith('if'):
                tt += ' {}'.format(ifcond[c])
            l = text.textItem(tt, anchor=4, parent=b)
            x, y = p.pos().x(), p.pos().y()
            l.setPos(x+5,y)
            l.setMutable(False)

    return b

def toMyhdlInstance(instname, connectdict, param):
    # properties end up in the connectdict
    n_if_inp = param['n_if_inp'] if 'n_if_inp' in param else 1
    ifcond =  param['ifcond'] if 'ifcond' in param else '{i0} == 1'
    cond = [s.strip() for s in ifcond.split(',')]
    ncond = len(cond)
    channels = param['channels'] if 'channels' in param else 1
    clk_edge = param['clk_edge'][0] if 'clk_edge' in param else 'None'
    reset = param['reset'][0] if 'reset' in param else 'False'

    inp, outp, _ = ports(param)
    dd = dict()
    for i in range(n_if_inp):
        nm = 'i{}'.format(i)
        dd[nm], _ = connectdict[nm] # store name in dict

    clocked = clk_edge in ['pos', 'neg']
    if clocked:
        ck, cktp = connectdict['ck'] if clk_edge == 'pos' else  connectdict['ckn']
        if reset == 'True':
            rst, rsttp = connectdict['rst']
            r =  '    @always_seq({ck}.{clk_edge}edge, reset={rst})\n'.format(ck=ck, clk_edge=clk_edge, rst=rst)
        else:
            r =  '    @always_seq({ck}.{clk_edge}edge)\n'.format(ck=ck, clk_edge=clk_edge)
    else:
        r =      '    @always_comb\n'
    r +=         '    def u_{inst}():\n'.format(inst=instname)
    for c in range(ncond+1):
        if c == 0:
            r += '        if {}:\n'.format(cond[c].format(**dd))
        elif c == ncond:
            r += '        else:\n'
        else:
            r += '        elif {}:\n'.format(cond[c].format(**dd))
        for i in range(channels):
            a, tpa = connectdict['.if{}_{}'.format(i, c)]
            z, tpz = connectdict['.z{}'.format(i)]
            if '{' not in z:
                r += '            {}.next = {}\n'.format(z, a)
            elif clocked:
                raise Exception("clocked signals cannot be 'inlined' as they are continuous assignments")

    ex = []
    d = dict()
    for i in range(channels):
        z, tpz = connectdict['.z{}'.format(i)]
        if '{' in z: # inline expression
            for c in range(ncond+1):
                a, tpa = connectdict['.if{}_{}'.format(i, c)]
#                if c == 0:
#                    ex.append('{} if {} else'.format(a, cond[c].format(**dd)))
                if c == ncond:
                    ex.append('{}'.format(a))
                else:
                    ex.append('{} if {} else'.format(a, cond[c].format(**dd)))
            d[z] = ' '.join(ex)
    if d:
        if '.next ' in r:
            return dict(__expr__ = d, __main__ = r)
        else:
            return dict(__expr__ = d)
    else:
        return r

views = {}
<|endoftext|>


<commit_before>from urllib2 import Request, urlopen

from django.utils import simplejson

from social_auth.utils import setting
from social_auth.backends import USERNAME
from social_auth.backends.google import GoogleOAuthBackend, GoogleOAuth, validate_whitelists

# Google OAuth base configuration
GOOGLE_OAUTH_SERVER = setting('GOOGLE_APPENGINE_OAUTH_SERVER', 'oauth-profile.appspot.com')
AUTHORIZATION_URL = 'https://%s/_ah/OAuthAuthorizeToken' % GOOGLE_OAUTH_SERVER
REQUEST_TOKEN_URL = 'https://%s/_ah/OAuthGetRequestToken' % GOOGLE_OAUTH_SERVER
ACCESS_TOKEN_URL = 'https://%s/_ah/OAuthGetAccessToken' % GOOGLE_OAUTH_SERVER

GOOGLE_APPENGINE_PROFILE = 'https://%s/oauth/v1/userinfo' % GOOGLE_OAUTH_SERVER

# Backends
class GoogleAppEngineOAuthBackend(GoogleOAuthBackend):
    """Google App Engine OAuth authentication backend"""
    name = 'google-appengine-oauth'

    def get_user_id(self, details, response):
        """Use google email or appengine user_id as unique id"""
        user_id = super(GoogleAppEngineOAuthBackend, self).get_user_id(details, response)
        if setting('GOOGLE_APPENGINE_OAUTH_USE_UNIQUE_USER_ID', False):
            return response['id']
        return user_id

    def get_user_details(self, response):
        """Return the information retrieved from the API endpoint"""
        email = response['email']
        return {USERNAME: response.get('nickname', email).split('@', 1)[0],
                'email': email,
                'fullname': '',
                'first_name': '',
                'last_name': ''}

# Auth classes
class GoogleAppEngineOAuth(GoogleOAuth):
    """Google App Engine OAuth authorization mechanism"""
    AUTHORIZATION_URL = AUTHORIZATION_URL
    REQUEST_TOKEN_URL = REQUEST_TOKEN_URL
    ACCESS_TOKEN_URL = ACCESS_TOKEN_URL
    SERVER_URL = GOOGLE_OAUTH_SERVER
    AUTH_BACKEND = GoogleAppEngineOAuthBackend
    SETTINGS_KEY_NAME = 'GOOGLE_APPENGINE_CONSUMER_KEY'
    SETTINGS_SECRET_NAME = 'GOOGLE_APPENGINE_CONSUMER_SECRET'

    def user_data(self, access_token, *args, **kwargs):
        """Return user data from Google API"""
        request = self.oauth_request(access_token, GOOGLE_APPENGINE_PROFILE)
        url, params = request.to_url().split('?', 1)
        return google_appengine_userinfo(url, params)

    def oauth_request(self, token, url, extra_params=None):
        """Add OAuth parameters to the request"""
        extra_params = extra_params or {}
        # Skip direct super class since scope and other parameters are not supported
        return super(GoogleOAuth, self).oauth_request(token, url, extra_params)

    @classmethod
    def get_key_and_secret(cls):
        """Return key and secret and fix anonymous settings"""
        key_and_secret = super(GoogleAppEngineOAuth, cls).get_key_and_secret()
        if key_and_secret == (None, None):
            return 'anonymous', 'anonymous'
        return key_and_secret

def google_appengine_userinfo(url, params):
    """Loads user data from OAuth Profile Google App Engine App.

    Parameters must be passed in queryset and Authorization header as described
    on Google OAuth documentation at:
    http://groups.google.com/group/oauth/browse_thread/thread/d15add9beb418ebc
    and: http://code.google.com/apis/accounts/docs/OAuth2.html#CallingAnAPI
    """
    request = Request(url + '?' + params, headers={'Authorization': params})
    try:
        return simplejson.loads(urlopen(request).read())
    except (ValueError, KeyError, IOError):
        return None

# Backend definition
BACKENDS = {
    'google-appengine-oauth': GoogleAppEngineOAuth,
}
<commit_msg>Fixed to support latest django-social-auth<commit_after>from urllib2 import Request, urlopen

from django.utils import simplejson

from social_auth.utils import setting
from social_auth.backends.google import GoogleOAuthBackend, GoogleOAuth, validate_whitelists

# Google OAuth base configuration
GOOGLE_OAUTH_SERVER = setting('GOOGLE_APPENGINE_OAUTH_SERVER', 'oauth-profile.appspot.com')
AUTHORIZATION_URL = 'https://%s/_ah/OAuthAuthorizeToken' % GOOGLE_OAUTH_SERVER
REQUEST_TOKEN_URL = 'https://%s/_ah/OAuthGetRequestToken' % GOOGLE_OAUTH_SERVER
ACCESS_TOKEN_URL = 'https://%s/_ah/OAuthGetAccessToken' % GOOGLE_OAUTH_SERVER

GOOGLE_APPENGINE_PROFILE = 'https://%s/oauth/v1/userinfo' % GOOGLE_OAUTH_SERVER

# Backends
class GoogleAppEngineOAuthBackend(GoogleOAuthBackend):
    """Google App Engine OAuth authentication backend"""
    name = 'google-appengine-oauth'

    def get_user_id(self, details, response):
        """Use google email or appengine user_id as unique id"""
        user_id = super(GoogleAppEngineOAuthBackend, self).get_user_id(details, response)
        if setting('GOOGLE_APPENGINE_OAUTH_USE_UNIQUE_USER_ID', False):
            return response['id']
        return user_id

    def get_user_details(self, response):
        """Return the information retrieved from the API endpoint"""
        email = response['email']
        return {'username': response.get('nickname', email).split('@', 1)[0],
                'email': email,
                'fullname': '',
                'first_name': '',
                'last_name': ''}

# Auth classes
class GoogleAppEngineOAuth(GoogleOAuth):
    """Google App Engine OAuth authorization mechanism"""
    AUTHORIZATION_URL = AUTHORIZATION_URL
    REQUEST_TOKEN_URL = REQUEST_TOKEN_URL
    ACCESS_TOKEN_URL = ACCESS_TOKEN_URL
    SERVER_URL = GOOGLE_OAUTH_SERVER
    AUTH_BACKEND = GoogleAppEngineOAuthBackend
    SETTINGS_KEY_NAME = 'GOOGLE_APPENGINE_CONSUMER_KEY'
    SETTINGS_SECRET_NAME = 'GOOGLE_APPENGINE_CONSUMER_SECRET'

    def user_data(self, access_token, *args, **kwargs):
        """Return user data from Google API"""
        request = self.oauth_request(access_token, GOOGLE_APPENGINE_PROFILE)
        url, params = request.to_url().split('?', 1)
        return google_appengine_userinfo(url, params)

    def oauth_request(self, token, url, extra_params=None):
        """Add OAuth parameters to the request"""
        extra_params = extra_params or {}
        # Skip direct super class since scope and other parameters are not supported
        return super(GoogleOAuth, self).oauth_request(token, url, extra_params)

    @classmethod
    def get_key_and_secret(cls):
        """Return key and secret and fix anonymous settings"""
        key_and_secret = super(GoogleAppEngineOAuth, cls).get_key_and_secret()
        if key_and_secret == (None, None):
            return 'anonymous', 'anonymous'
        return key_and_secret

def google_appengine_userinfo(url, params):
    """Loads user data from OAuth Profile Google App Engine App.

    Parameters must be passed in queryset and Authorization header as described
    on Google OAuth documentation at:
    http://groups.google.com/group/oauth/browse_thread/thread/d15add9beb418ebc
    and: http://code.google.com/apis/accounts/docs/OAuth2.html#CallingAnAPI
    """
    request = Request(url + '?' + params, headers={'Authorization': params})
    try:
        return simplejson.loads(urlopen(request).read())
    except (ValueError, KeyError, IOError):
        return None

# Backend definition
BACKENDS = {
    'google-appengine-oauth': GoogleAppEngineOAuth,
}
<|endoftext|>


<commit_before>from functools import partial, wraps

import numpy as np
from matplotlib import pyplot as plt

from core.config import Config, ALGORITHMS
from utils import generate_static_nodes
from experiments import evaluation

LABELS = dict(zip(ALGORITHMS, ['Classical MDS', 'Anchored MDS', 'MDS-RFID']))

def runexperiment(func):
	@wraps(func)
	def wrapper(**kwargs):
		plot_hanldes = []
		plot_labels = []
		for algorithm in ALGORITHMS:
			kwargs['algorithm'] = algorithm
			x_axis, errors = func(**kwargs)
			errors = np.array(errors)
			first_q, median, third_q = evaluation.first_third_quartile_and_median(errors)
			handles = getattr(evaluation, 'plot_{}'.format(func.__name__))(first_q, median, third_q, x_axis=x_axis)
			plot_hanldes.extend(handles)
			plot_labels.extend([LABELS[algorithm], 'IQR boundaries'])
		plt.legend(loc='upper left', fontsize=12, labels=plot_labels, handles=plot_hanldes)
		plt.show()
	return wrapper

@runexperiment
def rmse_vs_noise(algorithm=None, config=None, no_of_trials=10):
	errors = []
	sigmas = np.linspace(0, 4, 4)
	for sigma in sigmas:
		config.sigma = sigma
		# generate rmse from coordinates, remembering to not pass last_n_coords to function
		error = [evaluation.rmse(*generate_static_nodes(config, algorithm)[:2]) for i in range(no_of_trials)]
		errors.append(error)
	return sigmas, errors

@runexperiment
def rmse_vs_anchors(algorithm=None, config=None, no_of_trials=10):
	errors = []
	anchors = range(3, 9)
	for anc in anchors:
		config.no_of_anchors = anc
		# generate rmse from coordinates, remembering to not pass last_n_coords to function
		error = [evaluation.rmse(*generate_static_nodes(config, algorithm)[:2]) for i in range(no_of_trials)]
		errors.append(error)
	return anchors, errors<commit_msg>reduce step size of sigmas<commit_after>from functools import partial, wraps

import numpy as np
from matplotlib import pyplot as plt

from core.config import Config, ALGORITHMS
from utils import generate_static_nodes
from experiments import evaluation

LABELS = dict(zip(ALGORITHMS, ['Classical MDS', 'Anchored MDS', 'MDS-RFID']))

def runexperiment(func):
	@wraps(func)
	def wrapper(**kwargs):
		plot_hanldes = []
		plot_labels = []
		for algorithm in ALGORITHMS:
			kwargs['algorithm'] = algorithm
			x_axis, errors = func(**kwargs)
			errors = np.array(errors)
			first_q, median, third_q = evaluation.first_third_quartile_and_median(errors)
			handles = getattr(evaluation, 'plot_{}'.format(func.__name__))(first_q, median, third_q, x_axis=x_axis)
			plot_hanldes.extend(handles)
			plot_labels.extend([LABELS[algorithm], 'IQR boundaries'])
		plt.legend(loc='upper left', fontsize=12, labels=plot_labels, handles=plot_hanldes)
		plt.show()
	return wrapper

@runexperiment
def rmse_vs_noise(algorithm=None, config=None, no_of_trials=10):
	errors = []
	sigmas = np.linspace(0, 4, 40)
	for sigma in sigmas:
		config.sigma = sigma
		# generate rmse from coordinates, remembering to not pass last_n_coords to function
		error = [evaluation.rmse(*generate_static_nodes(config, algorithm)[:2]) for i in range(no_of_trials)]
		errors.append(error)
	return sigmas, errors

@runexperiment
def rmse_vs_anchors(algorithm=None, config=None, no_of_trials=10):
	errors = []
	anchors = range(3, 9)
	for anc in anchors:
		config.no_of_anchors = anc
		# generate rmse from coordinates, remembering to not pass last_n_coords to function
		error = [evaluation.rmse(*generate_static_nodes(config, algorithm)[:2]) for i in range(no_of_trials)]
		errors.append(error)
	return anchors, errors
<|endoftext|>