<commit_before># Lint as: python2, python3
# Copyright 2019 Google LLC. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""TFX artifact type definition."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import builtins
import enum
import importlib
import json
from typing import Any, Dict, Optional, Text

from google.protobuf import json_format
from ml_metadata.proto import metadata_store_pb2
from tfx.utils import json_utils

class ArtifactState(object):
  """Enumeration of possible Artifact states."""

  # Indicates that there is a pending execution producing the artifact.
  PENDING = 'pending'
  # Indicates that the artifact ready to be consumed.
  PUBLISHED = 'published'
  # Indicates that the no data at the artifact uri, though the artifact is not
  # marked as deleted.
  MISSING = 'missing'
  # Indicates that the artifact should be garbage collected.
  MARKED_FOR_DELETION = 'MARKED_FOR_DELETION'
  # Indicates that the artifact has been garbage collected.
  DELETED = 'deleted'

# Default split of examples data.
DEFAULT_EXAMPLE_SPLITS = ['train', 'eval']

class PropertyType(enum.Enum):
  INT = 1
  STRING = 2

class Property(object):
  """Property specified for an Artifact."""
  _ALLOWED_MLMD_TYPES = {
      PropertyType.INT: metadata_store_pb2.INT,
      PropertyType.STRING: metadata_store_pb2.STRING,
  }

  def __init__(self, type):  # pylint: disable=redefined-builtin
    if type not in Property._ALLOWED_MLMD_TYPES:
      raise ValueError('Property type must be one of %s.' %
                       list(Property._ALLOWED_MLMD_TYPES.keys()))
    self.type = type

  def mlmd_type(self):
    return Property._ALLOWED_MLMD_TYPES[self.type]

class Artifact(json_utils.Jsonable):
  """TFX artifact used for orchestration.

  This is used for type-checking and inter-component communication. Currently,
  it wraps a tuple of (ml_metadata.proto.Artifact,
  ml_metadata.proto.ArtifactType) with additional property accessors for
  internal state.

  A user may create a subclass of Artifact and override the TYPE_NAME property
  with the type for this artifact subclass. Users of the subclass may then omit
  the "type_name" field when construction the object.

  A user may specify artifact type-specific properties for an Artifact subclass
  by overriding the PROPERTIES dictionary, as detailed below.

  Note: the behavior of this class is experimental, without backwards
  compatibility guarantees, and may change in upcoming releases.
  """

  # String artifact type name used to identify the type in ML Metadata
  # database. Must be overridden by subclass.
  #
  # Example usage:
  #
  # TYPE_NAME = 'MyTypeName'
  TYPE_NAME = None

  # Optional dictionary of property name strings as keys and `Property`
  # objects as values, used to specify the artifact type's properties.
  # Subsequently, this artifact property may be accessed as Python attributes
  # of the artifact object.
  #
  # Example usage:
  #
  # PROPERTIES = {
  #   'span': Property(type=PropertyType.INT),
  #   # Comma separated of splits for an artifact. Empty string means artifact
  #   # has no split.
  #   'split_names': Property(type=PropertyType.STRING),
  # }
  #
  # Subsequently, these properties can be stored and accessed as
  # `myartifact.span` and `myartifact.split_name`, respectively.
  PROPERTIES = None

  # Initialization flag to support setattr / getattr behavior.
  _initialized = False

  def __init__(
      self,
      mlmd_artifact_type: Optional[metadata_store_pb2.ArtifactType] = None):
    """Construct an instance of Artifact.

    Used by TFX internal implementation: create an empty Artifact with
    type_name and optional split info specified. The remaining info will be
    filled in during compiling and running time. The Artifact should be
    transparent to end users and should not be initiated directly by pipeline
    users.

    Args:
      mlmd_artifact_type: Proto message defining the underlying ArtifactType.
        Optional and intended for internal use.
    """
    if self.__class__ == Artifact:
      if not mlmd_artifact_type:
        raise ValueError(
            'The "mlmd_artifact_type" argument must be passed to specify a '
            'type for this Artifact.')
      if not isinstance(mlmd_artifact_type, metadata_store_pb2.ArtifactType):
        raise ValueError(
            'The "mlmd_artifact_type" argument must be an instance of the '
            'proto message ml_metadata.proto.metadata_store_pb2.ArtifactType.')
    else:
      if mlmd_artifact_type:
        raise ValueError(
            'The "mlmd_artifact_type" argument must not be passed for '
            'Artifact subclass %s.' % self.__class__)
      type_name = self.__class__.TYPE_NAME
      if not (type_name and isinstance(type_name, (str, Text))):
        raise ValueError(
            ('The Artifact subclass %s must override the TYPE_NAME attribute '
             'with a string type name identifier (got %r instead).') %
            (self.__class__, type_name))
      mlmd_artifact_type = self._construct_artifact_type(type_name)

    # MLMD artifact type proto object.
    self._artifact_type = mlmd_artifact_type
    # Underlying MLMD artifact proto object.
    self._artifact = metadata_store_pb2.Artifact()
    # Initialization flag to prevent recursive getattr / setattr errors.
    self._initialized = True

  def _construct_artifact_type(self, type_name):
    artifact_type = metadata_store_pb2.ArtifactType()
    artifact_type.name = type_name
    if self.__class__.PROPERTIES:
      # Perform validation on PROPERTIES dictionary.
      if not isinstance(self.__class__.PROPERTIES, dict):
        raise ValueError(
            'Artifact subclass %s.PROPERTIES is not a dictionary.' %
            self.__class__)
      for key, value in self.__class__.PROPERTIES.items():
        if not (isinstance(key, (Text, bytes)) and isinstance(value, Property)):
          raise ValueError(
              ('Artifact subclass %s.PROPERTIES dictionary must have keys of '
               'type string and values of type artifact.Property.') %
              self.__class__)

      # Populate ML Metadata artifact properties dictionary.
      for key, value in self.__class__.PROPERTIES.items():
        artifact_type.properties[key] = value.mlmd_type()
    return artifact_type

  def __getattr__(self, name: Text) -> Any:
    """Custom __getattr__ to allow access to artifact properties."""
    if name == '_artifact_type':
      # Prevent infinite recursion when used with copy.deepcopy().
      raise AttributeError()
    if name not in self._artifact_type.properties:
      raise AttributeError('Artifact has no property %r.' % name)
    property_mlmd_type = self._artifact_type.properties[name]
    if property_mlmd_type == metadata_store_pb2.STRING:
      return self._artifact.properties[name].string_value
    elif property_mlmd_type == metadata_store_pb2.INT:
      return self._artifact.properties[name].int_value
    else:
      raise Exception('Unknown MLMD type %r for property %r.' %
                      (property_mlmd_type, name))

  def __setattr__(self, name: Text, value: Any):
    """Custom __setattr__ to allow access to artifact properties."""
    if not self._initialized:
      object.__setattr__(self, name, value)
      return
    if name not in self._artifact_type.properties:
      if name in Artifact.__dict__ or name in self.__dict__:
        # Use any provided getter / setter if available.
        object.__setattr__(self, name, value)
        return
      # In the case where we do not handle this via an explicit getter /
      # setter, we assume that the user implied an artifact attribute store,
      # and we raise an exception since such an attribute was not explicitly
      # defined in the Artifact PROPERTIES dictionary.
      raise AttributeError('Cannot set unknown property %r on artifact %r.' %
                           (name, self))
    property_mlmd_type = self._artifact_type.properties[name]
    if property_mlmd_type == metadata_store_pb2.STRING:
      if not isinstance(value, (Text, bytes)):
        raise Exception(
            'Expected string value for property %r; got %r instead.' %
            (name, value))
      self._artifact.properties[name].string_value = value
    elif property_mlmd_type == metadata_store_pb2.INT:
      if not isinstance(value, int):
        raise Exception(
            'Expected integer value for property %r; got %r instead.' %
            (name, value))
      self._artifact.properties[name].int_value = value
    else:
      raise Exception('Unknown MLMD type %r for property %r.' %
                      (property_mlmd_type, name))

  def set_mlmd_artifact(self, artifact: metadata_store_pb2.Artifact):
    """Replace the MLMD artifact object on this artifact."""
    self._artifact = artifact

  def set_mlmd_artifact_type(self,
                             artifact_type: metadata_store_pb2.ArtifactType):
    """Set entire ArtifactType in this object."""
    self._artifact_type = artifact_type
    self._artifact.type_id = artifact_type.id

  def __repr__(self):
    return 'Artifact(type_name: {}, uri: {}, id: {})'.format(
        self._artifact_type.name, self.uri, str(self.id))

  def to_json_dict(self) -> Dict[Text, Any]:
    return {
        'artifact':
            json.loads(
                json_format.MessageToJson(
                    message=self._artifact, preserving_proto_field_name=True)),
        'artifact_type':
            json.loads(
                json_format.MessageToJson(
                    message=self._artifact_type,
                    preserving_proto_field_name=True)),
        '__artifact_class_module__':
            self.__class__.__module__,
        '__artifact_class_name__':
            self.__class__.__name__,
    }

  @classmethod
  def from_json_dict(cls, dict_data: Dict[Text, Any]) -> Any:
    print('dict_data', dict_data)
    module_name = dict_data['__artifact_class_module__']
    class_name = dict_data['__artifact_class_name__']
    artifact_cls = getattr(importlib.import_module(module_name), class_name)
    artifact = metadata_store_pb2.Artifact()
    json_format.Parse(json.dumps(dict_data['artifact']), artifact)
    artifact_type = metadata_store_pb2.ArtifactType()
    json_format.Parse(json.dumps(dict_data['artifact_type']), artifact_type)
    result = artifact_cls()
    result.set_mlmd_artifact_type(artifact_type)
    result.set_mlmd_artifact(artifact)
    return result

  # Read-only properties.
  @property
  def type(self):
    return self.__class__

  @property
  def type_name(self):
    return self._artifact_type.name

  @property
  def artifact_type(self):
    return self._artifact_type

  @property
  def mlmd_artifact(self):
    return self._artifact

  # Settable properties for all artifact types.
  @property
  def uri(self) -> Text:
    """Artifact URI."""
    return self._artifact.uri

  @uri.setter
  def uri(self, uri: Text):
    """Setter for artifact URI."""
    self._artifact.uri = uri

  @property
  def id(self) -> int:
    """Id of underlying artifact."""
    return self._artifact.id

  @id.setter
  def id(self, artifact_id: int):
    """Set id of underlying artifact."""
    self._artifact.id = artifact_id

  @property
  def type_id(self) -> int:
    """Id of underlying artifact type."""
    return self._artifact.type_id

  @type_id.setter
  def type_id(self, type_id: int):
    """Set id of underlying artifact type."""
    self._artifact.type_id = type_id

  # System-managed properties for all artifact types. Will be deprecated soon
  # in favor of a unified getter / setter interface and MLMD context.
  #
  # TODO(b/135056715): Rely on MLMD context for pipeline grouping for
  # artifacts once it's ready.
  #
  # The following system properties are used:
  #   - name: The name of the artifact, used to differentiate same type of
  #       artifact produced by the same component (in a subsequent change, this
  #       information will move to the associated ML Metadata Event object).
  #   - state: The state of an artifact; can be one of PENDING, PUBLISHED,
  #       MISSING, DELETING, DELETED (in a subsequent change, this information
  #       will move to a top-level ML Metadata Artifact attribute).
  #   - pipeline_name: The name of the pipeline that produces the artifact (in
  #       a subsequent change, this information will move to an associated ML
  #       Metadata Context attribute).
  #   - producer_component: The name of the component that produces the
  #       artifact (in a subsequent change, this information will move to the
  #       associated ML Metadata Event object).
  def _get_system_property(self, key: Text) -> Text:
    if (key in self._artifact_type.properties and
        key in self._artifact.properties):
      # Legacy artifact types which have explicitly defined system properties.
      return self._artifact.properties[key].string_value
    return self._artifact.custom_properties[key].string_value

  def _set_system_property(self, key: Text, value: Text):
    if (key in self._artifact_type.properties and
        key in self._artifact.properties):
      # Clear non-custom property in legacy artifact types.
      del self._artifact.properties[key]
    self._artifact.custom_properties[key].string_value = value

  @property
  def name(self) -> Text:
    """Name of the underlying artifact."""
    return self._get_system_property('name')

  @name.setter
  def name(self, name: Text):
    """Set name of the underlying artifact."""
    self._set_system_property('name', name)

  @property
  def state(self) -> Text:
    """State of the underlying artifact."""
    return self._get_system_property('state')

  @state.setter
  def state(self, state: Text):
    """Set state of the underlying artifact."""
    self._set_system_property('state', state)

  @property
  def pipeline_name(self) -> Text:
    """Name of the pipeline that produce the artifact."""
    return self._get_system_property('pipeline_name')

  @pipeline_name.setter
  def pipeline_name(self, pipeline_name: Text):
    """Set name of the pipeline that produce the artifact."""
    self._set_system_property('pipeline_name', pipeline_name)

  @property
  def producer_component(self) -> Text:
    """Producer component of the artifact."""
    return self._get_system_property('producer_component')

  @producer_component.setter
  def producer_component(self, producer_component: Text):
    """Set producer component of the artifact."""
    self._set_system_property('producer_component', producer_component)

  # Custom property accessors.
  def set_string_custom_property(self, key: Text, value: Text):
    """Set a custom property of string type."""
    self._artifact.custom_properties[key].string_value = value

  def set_int_custom_property(self, key: Text, value: int):
    """Set a custom property of int type."""
    self._artifact.custom_properties[key].int_value = builtins.int(value)

  def get_string_custom_property(self, key: Text) -> Text:
    """Get a custom property of string type."""
    return self._artifact.custom_properties[key].string_value

  def get_int_custom_property(self, key: Text) -> int:
    """Get a custom property of int type."""
    return self._artifact.custom_properties[key].int_value
<commit_msg>Remove accidental debug statement in types/artifact.py.<commit_after># Lint as: python2, python3
# Copyright 2019 Google LLC. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""TFX artifact type definition."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import builtins
import enum
import importlib
import json
from typing import Any, Dict, Optional, Text

from google.protobuf import json_format
from ml_metadata.proto import metadata_store_pb2
from tfx.utils import json_utils

class ArtifactState(object):
  """Enumeration of possible Artifact states."""

  # Indicates that there is a pending execution producing the artifact.
  PENDING = 'pending'
  # Indicates that the artifact ready to be consumed.
  PUBLISHED = 'published'
  # Indicates that the no data at the artifact uri, though the artifact is not
  # marked as deleted.
  MISSING = 'missing'
  # Indicates that the artifact should be garbage collected.
  MARKED_FOR_DELETION = 'MARKED_FOR_DELETION'
  # Indicates that the artifact has been garbage collected.
  DELETED = 'deleted'

# Default split of examples data.
DEFAULT_EXAMPLE_SPLITS = ['train', 'eval']

class PropertyType(enum.Enum):
  INT = 1
  STRING = 2

class Property(object):
  """Property specified for an Artifact."""
  _ALLOWED_MLMD_TYPES = {
      PropertyType.INT: metadata_store_pb2.INT,
      PropertyType.STRING: metadata_store_pb2.STRING,
  }

  def __init__(self, type):  # pylint: disable=redefined-builtin
    if type not in Property._ALLOWED_MLMD_TYPES:
      raise ValueError('Property type must be one of %s.' %
                       list(Property._ALLOWED_MLMD_TYPES.keys()))
    self.type = type

  def mlmd_type(self):
    return Property._ALLOWED_MLMD_TYPES[self.type]

class Artifact(json_utils.Jsonable):
  """TFX artifact used for orchestration.

  This is used for type-checking and inter-component communication. Currently,
  it wraps a tuple of (ml_metadata.proto.Artifact,
  ml_metadata.proto.ArtifactType) with additional property accessors for
  internal state.

  A user may create a subclass of Artifact and override the TYPE_NAME property
  with the type for this artifact subclass. Users of the subclass may then omit
  the "type_name" field when construction the object.

  A user may specify artifact type-specific properties for an Artifact subclass
  by overriding the PROPERTIES dictionary, as detailed below.

  Note: the behavior of this class is experimental, without backwards
  compatibility guarantees, and may change in upcoming releases.
  """

  # String artifact type name used to identify the type in ML Metadata
  # database. Must be overridden by subclass.
  #
  # Example usage:
  #
  # TYPE_NAME = 'MyTypeName'
  TYPE_NAME = None

  # Optional dictionary of property name strings as keys and `Property`
  # objects as values, used to specify the artifact type's properties.
  # Subsequently, this artifact property may be accessed as Python attributes
  # of the artifact object.
  #
  # Example usage:
  #
  # PROPERTIES = {
  #   'span': Property(type=PropertyType.INT),
  #   # Comma separated of splits for an artifact. Empty string means artifact
  #   # has no split.
  #   'split_names': Property(type=PropertyType.STRING),
  # }
  #
  # Subsequently, these properties can be stored and accessed as
  # `myartifact.span` and `myartifact.split_name`, respectively.
  PROPERTIES = None

  # Initialization flag to support setattr / getattr behavior.
  _initialized = False

  def __init__(
      self,
      mlmd_artifact_type: Optional[metadata_store_pb2.ArtifactType] = None):
    """Construct an instance of Artifact.

    Used by TFX internal implementation: create an empty Artifact with
    type_name and optional split info specified. The remaining info will be
    filled in during compiling and running time. The Artifact should be
    transparent to end users and should not be initiated directly by pipeline
    users.

    Args:
      mlmd_artifact_type: Proto message defining the underlying ArtifactType.
        Optional and intended for internal use.
    """
    if self.__class__ == Artifact:
      if not mlmd_artifact_type:
        raise ValueError(
            'The "mlmd_artifact_type" argument must be passed to specify a '
            'type for this Artifact.')
      if not isinstance(mlmd_artifact_type, metadata_store_pb2.ArtifactType):
        raise ValueError(
            'The "mlmd_artifact_type" argument must be an instance of the '
            'proto message ml_metadata.proto.metadata_store_pb2.ArtifactType.')
    else:
      if mlmd_artifact_type:
        raise ValueError(
            'The "mlmd_artifact_type" argument must not be passed for '
            'Artifact subclass %s.' % self.__class__)
      type_name = self.__class__.TYPE_NAME
      if not (type_name and isinstance(type_name, (str, Text))):
        raise ValueError(
            ('The Artifact subclass %s must override the TYPE_NAME attribute '
             'with a string type name identifier (got %r instead).') %
            (self.__class__, type_name))
      mlmd_artifact_type = self._construct_artifact_type(type_name)

    # MLMD artifact type proto object.
    self._artifact_type = mlmd_artifact_type
    # Underlying MLMD artifact proto object.
    self._artifact = metadata_store_pb2.Artifact()
    # Initialization flag to prevent recursive getattr / setattr errors.
    self._initialized = True

  def _construct_artifact_type(self, type_name):
    artifact_type = metadata_store_pb2.ArtifactType()
    artifact_type.name = type_name
    if self.__class__.PROPERTIES:
      # Perform validation on PROPERTIES dictionary.
      if not isinstance(self.__class__.PROPERTIES, dict):
        raise ValueError(
            'Artifact subclass %s.PROPERTIES is not a dictionary.' %
            self.__class__)
      for key, value in self.__class__.PROPERTIES.items():
        if not (isinstance(key, (Text, bytes)) and isinstance(value, Property)):
          raise ValueError(
              ('Artifact subclass %s.PROPERTIES dictionary must have keys of '
               'type string and values of type artifact.Property.') %
              self.__class__)

      # Populate ML Metadata artifact properties dictionary.
      for key, value in self.__class__.PROPERTIES.items():
        artifact_type.properties[key] = value.mlmd_type()
    return artifact_type

  def __getattr__(self, name: Text) -> Any:
    """Custom __getattr__ to allow access to artifact properties."""
    if name == '_artifact_type':
      # Prevent infinite recursion when used with copy.deepcopy().
      raise AttributeError()
    if name not in self._artifact_type.properties:
      raise AttributeError('Artifact has no property %r.' % name)
    property_mlmd_type = self._artifact_type.properties[name]
    if property_mlmd_type == metadata_store_pb2.STRING:
      return self._artifact.properties[name].string_value
    elif property_mlmd_type == metadata_store_pb2.INT:
      return self._artifact.properties[name].int_value
    else:
      raise Exception('Unknown MLMD type %r for property %r.' %
                      (property_mlmd_type, name))

  def __setattr__(self, name: Text, value: Any):
    """Custom __setattr__ to allow access to artifact properties."""
    if not self._initialized:
      object.__setattr__(self, name, value)
      return
    if name not in self._artifact_type.properties:
      if name in Artifact.__dict__ or name in self.__dict__:
        # Use any provided getter / setter if available.
        object.__setattr__(self, name, value)
        return
      # In the case where we do not handle this via an explicit getter /
      # setter, we assume that the user implied an artifact attribute store,
      # and we raise an exception since such an attribute was not explicitly
      # defined in the Artifact PROPERTIES dictionary.
      raise AttributeError('Cannot set unknown property %r on artifact %r.' %
                           (name, self))
    property_mlmd_type = self._artifact_type.properties[name]
    if property_mlmd_type == metadata_store_pb2.STRING:
      if not isinstance(value, (Text, bytes)):
        raise Exception(
            'Expected string value for property %r; got %r instead.' %
            (name, value))
      self._artifact.properties[name].string_value = value
    elif property_mlmd_type == metadata_store_pb2.INT:
      if not isinstance(value, int):
        raise Exception(
            'Expected integer value for property %r; got %r instead.' %
            (name, value))
      self._artifact.properties[name].int_value = value
    else:
      raise Exception('Unknown MLMD type %r for property %r.' %
                      (property_mlmd_type, name))

  def set_mlmd_artifact(self, artifact: metadata_store_pb2.Artifact):
    """Replace the MLMD artifact object on this artifact."""
    self._artifact = artifact

  def set_mlmd_artifact_type(self,
                             artifact_type: metadata_store_pb2.ArtifactType):
    """Set entire ArtifactType in this object."""
    self._artifact_type = artifact_type
    self._artifact.type_id = artifact_type.id

  def __repr__(self):
    return 'Artifact(type_name: {}, uri: {}, id: {})'.format(
        self._artifact_type.name, self.uri, str(self.id))

  def to_json_dict(self) -> Dict[Text, Any]:
    return {
        'artifact':
            json.loads(
                json_format.MessageToJson(
                    message=self._artifact, preserving_proto_field_name=True)),
        'artifact_type':
            json.loads(
                json_format.MessageToJson(
                    message=self._artifact_type,
                    preserving_proto_field_name=True)),
        '__artifact_class_module__':
            self.__class__.__module__,
        '__artifact_class_name__':
            self.__class__.__name__,
    }

  @classmethod
  def from_json_dict(cls, dict_data: Dict[Text, Any]) -> Any:
    module_name = dict_data['__artifact_class_module__']
    class_name = dict_data['__artifact_class_name__']
    artifact_cls = getattr(importlib.import_module(module_name), class_name)
    artifact = metadata_store_pb2.Artifact()
    json_format.Parse(json.dumps(dict_data['artifact']), artifact)
    artifact_type = metadata_store_pb2.ArtifactType()
    json_format.Parse(json.dumps(dict_data['artifact_type']), artifact_type)
    result = artifact_cls()
    result.set_mlmd_artifact_type(artifact_type)
    result.set_mlmd_artifact(artifact)
    return result

  # Read-only properties.
  @property
  def type(self):
    return self.__class__

  @property
  def type_name(self):
    return self._artifact_type.name

  @property
  def artifact_type(self):
    return self._artifact_type

  @property
  def mlmd_artifact(self):
    return self._artifact

  # Settable properties for all artifact types.
  @property
  def uri(self) -> Text:
    """Artifact URI."""
    return self._artifact.uri

  @uri.setter
  def uri(self, uri: Text):
    """Setter for artifact URI."""
    self._artifact.uri = uri

  @property
  def id(self) -> int:
    """Id of underlying artifact."""
    return self._artifact.id

  @id.setter
  def id(self, artifact_id: int):
    """Set id of underlying artifact."""
    self._artifact.id = artifact_id

  @property
  def type_id(self) -> int:
    """Id of underlying artifact type."""
    return self._artifact.type_id

  @type_id.setter
  def type_id(self, type_id: int):
    """Set id of underlying artifact type."""
    self._artifact.type_id = type_id

  # System-managed properties for all artifact types. Will be deprecated soon
  # in favor of a unified getter / setter interface and MLMD context.
  #
  # TODO(b/135056715): Rely on MLMD context for pipeline grouping for
  # artifacts once it's ready.
  #
  # The following system properties are used:
  #   - name: The name of the artifact, used to differentiate same type of
  #       artifact produced by the same component (in a subsequent change, this
  #       information will move to the associated ML Metadata Event object).
  #   - state: The state of an artifact; can be one of PENDING, PUBLISHED,
  #       MISSING, DELETING, DELETED (in a subsequent change, this information
  #       will move to a top-level ML Metadata Artifact attribute).
  #   - pipeline_name: The name of the pipeline that produces the artifact (in
  #       a subsequent change, this information will move to an associated ML
  #       Metadata Context attribute).
  #   - producer_component: The name of the component that produces the
  #       artifact (in a subsequent change, this information will move to the
  #       associated ML Metadata Event object).
  def _get_system_property(self, key: Text) -> Text:
    if (key in self._artifact_type.properties and
        key in self._artifact.properties):
      # Legacy artifact types which have explicitly defined system properties.
      return self._artifact.properties[key].string_value
    return self._artifact.custom_properties[key].string_value

  def _set_system_property(self, key: Text, value: Text):
    if (key in self._artifact_type.properties and
        key in self._artifact.properties):
      # Clear non-custom property in legacy artifact types.
      del self._artifact.properties[key]
    self._artifact.custom_properties[key].string_value = value

  @property
  def name(self) -> Text:
    """Name of the underlying artifact."""
    return self._get_system_property('name')

  @name.setter
  def name(self, name: Text):
    """Set name of the underlying artifact."""
    self._set_system_property('name', name)

  @property
  def state(self) -> Text:
    """State of the underlying artifact."""
    return self._get_system_property('state')

  @state.setter
  def state(self, state: Text):
    """Set state of the underlying artifact."""
    self._set_system_property('state', state)

  @property
  def pipeline_name(self) -> Text:
    """Name of the pipeline that produce the artifact."""
    return self._get_system_property('pipeline_name')

  @pipeline_name.setter
  def pipeline_name(self, pipeline_name: Text):
    """Set name of the pipeline that produce the artifact."""
    self._set_system_property('pipeline_name', pipeline_name)

  @property
  def producer_component(self) -> Text:
    """Producer component of the artifact."""
    return self._get_system_property('producer_component')

  @producer_component.setter
  def producer_component(self, producer_component: Text):
    """Set producer component of the artifact."""
    self._set_system_property('producer_component', producer_component)

  # Custom property accessors.
  def set_string_custom_property(self, key: Text, value: Text):
    """Set a custom property of string type."""
    self._artifact.custom_properties[key].string_value = value

  def set_int_custom_property(self, key: Text, value: int):
    """Set a custom property of int type."""
    self._artifact.custom_properties[key].int_value = builtins.int(value)

  def get_string_custom_property(self, key: Text) -> Text:
    """Get a custom property of string type."""
    return self._artifact.custom_properties[key].string_value

  def get_int_custom_property(self, key: Text) -> int:
    """Get a custom property of int type."""
    return self._artifact.custom_properties[key].int_value
<|endoftext|>


<commit_before>from django.contrib import admin
from django.db import transaction
from django.contrib.admin.options import csrf_protect_m
from sorl.thumbnail import get_thumbnail
from imager_images.filters import PhotoSizeFilter
from imager_images.forms import *
from imager_images.models import Album, Photo

class PhotoAdmin(admin.ModelAdmin):
    def get_form(self, request, obj=None, **kwargs):
        if not obj:
            self.form = NewPhotoForm
        else:
            self.form = EditPhotoForm
        return super(PhotoAdmin, self).get_form(request, obj, **kwargs)

    def get_fields(self, request, obj=None):
        if obj:
            return ('user',
                    'image',
                    'albums',
                    'title',
                    'description',
                    'image_thumbnail',
                    'date_published',
                    'published',
                    'date_uploaded',
                    'date_modified',
                    'size',
                    )
        else:
            return ('user',
                    'image',
                    'title',
                    'description',
                    'date_published',
                    'published',
                    )

    def get_readonly_fields(self, request, obj=None):
        if obj:
            return ('user',
                    'image_thumbnail',
                    'date_uploaded',
                    'date_modified',
                    'size'
                    )
        else:
            return ()

    def image_thumbnail(self, obj):
        if obj.image:
            thumb = get_thumbnail(
                obj.image, "50x50", crop='center', quality=99)
            return '' % (thumb.url)
        else:
            return 'No Image'

    list_display = ('image',
                    'title',
                    'user_linked',
                    'description',
                    'date_uploaded',
                    'date_modified',
                    'date_published',
                    'size'
                    )

    list_filter = ('user',
                   'albums',
                   PhotoSizeFilter,
                   )

    search_fields = ('user__username',
                     'user__first_name',
                     'user__last_name',
                     'user__email',
                     'albums__title',
                     'albums__description',
                     'title',
                     'description'
                     )

    def user_linked(self, obj):
        return '%s' % (
            '/admin/auth/user/', obj.user.pk, obj.user)
    user_linked.allow_tags = True
    user_linked.short_description = 'User'

    def size(self, obj):
            if obj.image.size <= 1024:
                return "{:0.1f} B".format(obj.file_size)
            if obj.image.size <= 1024.0**2:
                return "{:0.1f} KB".format(obj.file_size/1024.0)
            if obj.image.size <= 1024.0**3:
                return "{:0.1f} MB".format(obj.file_size/(1024.0**2))
            if obj.image.size <= 1024.0**4:
                return "{:0.1f} GB".format(obj.file_size/(1024.0**3))
            return "0 MB"

class PhotoInline(admin.TabularInline):
    model = Photo.albums.through
    # form = PhotoAlbumForm
    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        field = super(PhotoInline, self).formfield_for_foreignkey(
            db_field, request, **kwargs)
        field.queryset = field.queryset.filter(user=request._obj_.user)
        return field

class AlbumAdmin(admin.ModelAdmin):
    def get_form(self, request, obj=None, **kwargs):
        request._obj_ = obj
        if not obj:
            self.form = NewAlbumForm
        else:
            self.form = EditAlbumForm
        return super(AlbumAdmin, self).get_form(request, obj, **kwargs)

    def get_fields(self, request, obj=None):
        if obj:
            return ('user',
                    'title',
                    'description',
                    'cover',
                    'date_published',
                    'published',
                    'date_uploaded',
                    'date_modified',
                    )
        else:
            return ('user',
                    'title',
                    'description',
                    'date_published',
                    'published',
                    )

    def get_readonly_fields(self, request, obj=None):
        if obj:
            return ('user',
                    'date_uploaded',
                    'date_modified',
                    )
        else:
            return ()

    def user_linked(self, obj):
        return '%s' % (
            '/admin/auth/user/', obj.user.pk, obj.user)

    user_linked.allow_tags = True
    user_linked.short_description = 'User'

    @csrf_protect_m
    @transaction.atomic
    def changeform_view(
        self, request, object_id=None, form_url='', extra_context=None
    ):
        if not object_id:
            try:
                self.inlines.remove(PhotoInline)
            except ValueError:
                pass
        else:
            if PhotoInline not in self.inlines:
                self.inlines.append(PhotoInline)

        return super(AlbumAdmin, self).changeform_view(
            request, object_id, form_url, extra_context
            )

    list_display = ('title',
                    'description',
                    'user_linked',
                    'date_uploaded',
                    'date_modified',
                    'date_published'
                    )

    list_filter = ('user',)
    search_fields = ('user__username',
                     'user__first_name',
                     'user__last_name',
                     'user__email',
                     'title',
                     'description',
                     )

    readonly_fields = ('user',
                       'date_uploaded',
                       'date_modified'
                       )

    inlines = [PhotoInline, ]

admin.site.register(Album, AlbumAdmin)
admin.site.register(Photo, PhotoAdmin)
<commit_msg>add published status to admin list view for photos and albums.<commit_after>from django.contrib import admin
from django.db import transaction
from django.contrib.admin.options import csrf_protect_m
from sorl.thumbnail import get_thumbnail
from imager_images.filters import PhotoSizeFilter
from imager_images.forms import *
from imager_images.models import Album, Photo

class PhotoAdmin(admin.ModelAdmin):
    def get_form(self, request, obj=None, **kwargs):
        if not obj:
            self.form = NewPhotoForm
        else:
            self.form = EditPhotoForm
        return super(PhotoAdmin, self).get_form(request, obj, **kwargs)

    def get_fields(self, request, obj=None):
        if obj:
            return ('user',
                    'image',
                    'albums',
                    'title',
                    'description',
                    'image_thumbnail',
                    'date_published',
                    'published',
                    'date_uploaded',
                    'date_modified',
                    'size',
                    )
        else:
            return ('user',
                    'image',
                    'title',
                    'description',
                    'date_published',
                    'published',
                    )

    def get_readonly_fields(self, request, obj=None):
        if obj:
            return ('user',
                    'image_thumbnail',
                    'date_uploaded',
                    'date_modified',
                    'size'
                    )
        else:
            return ()

    def image_thumbnail(self, obj):
        if obj.image:
            thumb = get_thumbnail(
                obj.image, "50x50", crop='center', quality=99)
            return '' % (thumb.url)
        else:
            return 'No Image'

    list_display = ('image',
                    'title',
                    'description',
                    'user_linked',
                    'published',
                    'date_uploaded',
                    'date_modified',
                    'date_published',
                    'size'
                    )

    list_filter = ('user',
                   'albums',
                   PhotoSizeFilter,
                   )

    search_fields = ('user__username',
                     'user__first_name',
                     'user__last_name',
                     'user__email',
                     'albums__title',
                     'albums__description',
                     'title',
                     'description'
                     )

    def user_linked(self, obj):
        return '%s' % (
            '/admin/auth/user/', obj.user.pk, obj.user)
    user_linked.allow_tags = True
    user_linked.short_description = 'User'

    def size(self, obj):
            if obj.image.size <= 1024:
                return "{:0.1f} B".format(obj.file_size)
            if obj.image.size <= 1024.0**2:
                return "{:0.1f} KB".format(obj.file_size/1024.0)
            if obj.image.size <= 1024.0**3:
                return "{:0.1f} MB".format(obj.file_size/(1024.0**2))
            if obj.image.size <= 1024.0**4:
                return "{:0.1f} GB".format(obj.file_size/(1024.0**3))
            return "0 MB"

class PhotoInline(admin.TabularInline):
    model = Photo.albums.through
    # form = PhotoAlbumForm
    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        field = super(PhotoInline, self).formfield_for_foreignkey(
            db_field, request, **kwargs)
        field.queryset = field.queryset.filter(user=request._obj_.user)
        return field

class AlbumAdmin(admin.ModelAdmin):
    def get_form(self, request, obj=None, **kwargs):
        request._obj_ = obj
        if not obj:
            self.form = NewAlbumForm
        else:
            self.form = EditAlbumForm
        return super(AlbumAdmin, self).get_form(request, obj, **kwargs)

    def get_fields(self, request, obj=None):
        if obj:
            return ('user',
                    'title',
                    'description',
                    'cover',
                    'date_published',
                    'published',
                    'date_uploaded',
                    'date_modified',
                    )
        else:
            return ('user',
                    'title',
                    'description',
                    'date_published',
                    'published',
                    )

    def get_readonly_fields(self, request, obj=None):
        if obj:
            return ('user',
                    'date_uploaded',
                    'date_modified',
                    )
        else:
            return ()

    def user_linked(self, obj):
        return '%s' % (
            '/admin/auth/user/', obj.user.pk, obj.user)

    user_linked.allow_tags = True
    user_linked.short_description = 'User'

    @csrf_protect_m
    @transaction.atomic
    def changeform_view(
        self, request, object_id=None, form_url='', extra_context=None
    ):
        if not object_id:
            try:
                self.inlines.remove(PhotoInline)
            except ValueError:
                pass
        else:
            if PhotoInline not in self.inlines:
                self.inlines.append(PhotoInline)

        return super(AlbumAdmin, self).changeform_view(
            request, object_id, form_url, extra_context
            )

    list_display = ('title',
                    'description',
                    'user_linked',
                    'published',
                    'date_uploaded',
                    'date_modified',
                    'date_published'
                    )

    list_filter = ('user',)
    search_fields = ('user__username',
                     'user__first_name',
                     'user__last_name',
                     'user__email',
                     'title',
                     'description',
                     )

    readonly_fields = ('user',
                       'date_uploaded',
                       'date_modified'
                       )

    inlines = [PhotoInline, ]

admin.site.register(Album, AlbumAdmin)
admin.site.register(Photo, PhotoAdmin)
<|endoftext|>


<commit_before>from __future__ import print_function

import numpy as np

# Import sile objects
from ..sile import add_sile, Sile_fh_open, sile_raise_write
from .sile import *

__all__ = ['kpSileSiesta']

class kpSileSiesta(SileSiesta):
    """ k-point Siesta file object """

    @Sile_fh_open
    def read_data(self, sc=None):
        """ Returns K-points from the file (note that these are in reciprocal units)

        Parameters
        ----------
        sc : SuperCellChild
           if supplied the returned k-points will be in reduced coordinates

        Returns
        -------
        k : k-points
        w : weights for k-points
        """
        nk = int(self.readline())

        k = np.empty([nk, 3], np.float64)
        w = np.empty([nk], np.float64)
        for ik in range(nk):
            l = self.readline().split()
            k[ik, :] = float(l[1]), float(l[2]), float(l[3])
            w[ik] = float(l[4])

        if sc is None:
            return k, w
        return np.dot(k, sc.cell.T / (2 * np.pi)), w

    @Sile_fh_open
    def write_data(self, bz, fmt='.9e'):
        """ Writes K-points to file

        Parameters
        ----------
        bz : BrillouinZone
           object contain all weights and k-points
        """
        sile_raise_write(self)

        nk = len(bz)
        self._write('{}\n'.format(nk))
        _fmt = ('{:d}' + (' {:' + fmt + '}') * 4) + '\n'

        for i, k in enumerate(bz):
            self._write(_fmt.format(i + 1, k[0], k[1], k[2], bz.weight[i]))

add_sile('KP', kpSileSiesta, case=False, gzip=True)
<commit_msg>bug: fixed units when reading *.KP files from siesta<commit_after>from __future__ import print_function

import numpy as np

# Import sile objects
from ..sile import add_sile, Sile_fh_open, sile_raise_write
from .sile import *

from sisl.unit.siesta import unit_convert

__all__ = ['kpSileSiesta']

Bohr2Ang = unit_convert('Bohr', 'Ang')

class kpSileSiesta(SileSiesta):
    """ k-point Siesta file object """

    @Sile_fh_open
    def read_data(self, sc=None):
        """ Returns K-points from the file (note that these are in reciprocal units)

        Parameters
        ----------
        sc : SuperCellChild, optional
           if supplied the returned k-points will be in reduced coordinates

        Returns
        -------
        k : k-points
        w : weights for k-points
        """
        nk = int(self.readline())

        k = np.empty([nk, 3], np.float64)
        w = np.empty([nk], np.float64)
        for ik in range(nk):
            l = self.readline().split()
            k[ik, :] = float(l[1]), float(l[2]), float(l[3])
            w[ik] = float(l[4])

        # Correct units to 1/Ang
        k /= Bohr2Ang

        if sc is None:
            return k, w
        return np.dot(k, sc.cell.T / (2 * np.pi)), w

    @Sile_fh_open
    def read_brillouinzone(self, sc):
        """ Returns K-points from the file (note that these are in reciprocal units)

        Parameters
        ----------
        sc : SuperCellChild
           required supercell for the BrillouinZone object

        Returns
        -------
        bz : BrillouinZone
        """
        k, w = self.read_data(sc)
        from sisl.physics.brillouinzone import BrillouinZone

        bz = BrillouinZone(sc)
        bz._k = k
        bz._w = w
        return bz

    @Sile_fh_open
    def write_data(self, bz, fmt='.9e'):
        """ Writes K-points to file

        Parameters
        ----------
        bz : BrillouinZone
           object contain all weights and k-points
        """
        sile_raise_write(self)

        nk = len(bz)
        self._write('{}\n'.format(nk))
        _fmt = ('{:d}' + (' {:' + fmt + '}') * 4) + '\n'

        for i, k in enumerate(bz):
            self._write(_fmt.format(i + 1, k[0], k[1], k[2], bz.weight[i]))

add_sile('KP', kpSileSiesta, case=False, gzip=True)
<|endoftext|>


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

'''This file contains recurrent network structures.'''

import climate
import numpy as np
import numpy.random as rng
import theano
import theano.tensor as TT

#from theano.tensor.shared_randomstreams import RandomStreams
from theano.sandbox.rng_mrg import MRG_RandomStreams as RandomStreams

from . import feedforward as ff

logging = climate.get_logger(__name__)

class Network(ff.Network):
    '''A fully connected recurrent network with one input and one output layer.

    Parameters
    ----------
    layers : sequence of int
        A sequence of integers specifying the number of units at each layer. As
        an example, layers=(10, 20, 3) has one "input" layer with 10 units, one
        "hidden" layer with 20 units, and one "output" layer with 3 units. That
        is, inputs should be of length 10, and outputs will be of length 3.

    recurrent_layers : sequence of int, optional
        A sequence of integers specifying the indices of recurrent layers in the
        network. Non-recurrent network layers receive input only from the
        preceding layers for a given input, while recurrent layers also receive
        input from the output of the recurrent layer from the previous time
        step. The index values in this sequence must be greater than 0; that is,
        the input of a network cannot be recurrent. Defaults to
        [len(layers) // 2 - 1], i.e., the "middle" layer of the network is the
        only recurrent layer.

    hidden_activation : str, optional
        The name of an activation function to use on hidden network units.
        Defaults to 'sigmoid'.

    output_activation : str, optional
        The name of an activation function to use on output units. Defaults to
        'linear'.

    rng : theano.RandomStreams, optional
        Use a specific Theano random number generator. A new one will be created
        if this is None.

    input_noise : float, optional
        Standard deviation of desired noise to inject into input.

    hidden_noise : float, optional
        Standard deviation of desired noise to inject into hidden unit
        activation output.

    input_dropouts : float, optional
        Proportion of input units to randomly set to 0.

    hidden_dropouts : float, optional
        Proportion of hidden unit activations to randomly set to 0.

    recurrent_error_start : int, optional
        Compute error metrics starting at this time step. (Defaults to 3.)
    '''

    @property
    def error_start(self):
        return self.kwargs.get('recurrent_error_start', 3)

    def setup_vars(self):
        # the first dimension indexes time, the second indexes the elements of
        # each minibatch, and the third indexes the variables in a given frame.
        self.x = TT.tensor3('x')

    def setup_layers(self, **kwargs):
        '''
        '''
        count = 0

        noise = kwargs.get('input_noise', 0)
        dropout = kwargs.get('input_dropouts', 0)
        x = self._add_noise(self.x, noise, dropout)

        noise = kwargs.get('hidden_noise', 0)
        dropout = kwargs.get('hidden_dropouts', 0)
        layers = kwargs.get('layers')
        recurrent = set(kwargs.get('recurrent_layers', [len(layers) // 2 - 1]))
        for i, (nin, nout) in enumerate(zip(layers[:-1], layers[1:])):
            if i in recurrent:
                x, n = self.add_recurrent_layer(x, nin, nout, i)
                count += n
            else:
                count += (nin + 1) * nout
                x = self.add_feedforward_layer(
                    x, nin, nout, i, noise=noise, dropout=dropout)

        self.y = self.hiddens.pop()
        logging.info('%d total network parameters', count)

    def add_recurrent_layer(self, x, nin, nout, label=None):
        '''Add a new recurrent layer to the network.

        Parameters
        ----------
        input : theano variable
            The theano variable that represents the inputs to this layer.
        nin : int
            The number of input units to this layer.
        nout : out
            The number of output units from this layer.
        label : any, optional
            The name of this layer, used for logging and as the theano variable
            name suffix. Defaults to the index of this layer in the network.

        Returns
        -------
        output : theano variable
            The theano variable that represents the outputs from this layer.
        count : int
            The number of learnable parameters in this layer.
        '''
        b_h, _ = self.create_bias(nout, 'h_{}'.format(label))
        W_xh, _ = self.create_weights(nin, nout, 'xh_{}'.format(label))
        W_hh, _ = self.create_weights(nout, nout, 'hh_{}'.format(label))

        def fn(x_t, h_tm1, W_xh, W_hh, b_h):
            return self._hidden_func(TT.dot(x_t, W_xh) + TT.dot(h_tm1, W_hh) + b_h)

        batch_size = self.kwargs.get('batch_size', 64)
        h, updates = theano.scan(
            name='f_{}'.format(label),
            fn=fn,
            sequences=x,
            outputs_info=[TT.zeros((batch_size, nout), dtype=ff.FLOAT)],
            non_sequences=[W_xh, W_hh, b_h])

        self.updates.update(updates)
        self.hiddens.append(h)
        self.weights.extend([W_xh, W_hh])
        self.biases.append(b_h)

        return h, nout * (1 + nin + nout)

    def lstm_recurrence(self, input_size, cell_size):
        count = 0
        W_xi, b_i, n = self.create_layer(input_size, cell_size, 'xi')
        count += n
        W_hi, W_ci, n = self.create_layer(cell_size, cell_size, 'hi')
        W_ci.name = 'W_ci'
        count += n

        W_xf, b_f, n = self.create_layer(input_size, cell_size, 'xf')
        count += n
        W_hf, W_cf, n = self.create_layer(cell_size, cell_size, 'hf')
        W_cf.name = 'W_cf'
        count += n

        W_xo, b_o, n = self.create_layer(input_size, cell_size, 'xo')
        count += n
        W_ho, W_co, n = self.create_layer(cell_size, cell_size, 'ho')
        W_co.name = 'W_co'
        count += n

        W_xc, b_c, n = self.create_layer(input_size, cell_size, 'xc')
        count += n
        W_hc, _, n = self.create_layer(cell_size, cell_size, 'hc')
        count += n - cell_size

        def recurrence(x_t, h_tm1, c_tm1):
            i_t = TT.nnet.sigmoid(TT.dot(x_t, W_xi) +
                                  TT.dot(h_tm1, W_hi) +
                                  TT.dot(c_tm1, TT.diag(W_ci)) + b_i)
            f_t = TT.nnet.sigmoid(TT.dot(x_t, W_xf) +
                                  TT.dot(h_tm1, W_hf) +
                                  TT.dot(c_tm1, TT.diag(W_cf)) + b_f)
            c_t = f_t * c_tm1 + i_t * TT.tanh(TT.dot(x_t, W_xc) +
                                              TT.dot(h_tm1, W_hc) + b_c)
            o_t = TT.nnet.sigmoid(TT.dot(x_t, W_xo) +
                                  TT.dot(h_tm1, W_ho) +
                                  TT.dot(c_t, TT.diag(W_co)) + b_o)
            h_t = o_t * TT.tanh(c_t)
            return h_t, c_t

        return recurrence, count

class Autoencoder(Network):
    '''An autoencoder attempts to reproduce its input.'''

    @property
    def cost(self):
        err = self.y - self.x
        return TT.mean((err * err).sum(axis=2)[self.error_start:])

class Predictor(Autoencoder):
    '''A predictor network attempts to predict its next time step.
    '''

    @property
    def cost(self):
        # we want the network to predict the next time step. y is the prediction
        # (output of the network), so we want y[0] to match x[1], y[1] to match
        # x[2], and so forth.
        err = self.x[1:] - self.generate_prediction(self.y)[:-1]
        return TT.mean((err * err).sum(axis=2)[self.error_start:])

    def generate_prediction(self, y):
        '''Given outputs from each time step, map them to subsequent inputs.

        This defaults to the identity transform, i.e., the output from one time
        step is treated as the input to the next time step with no
        transformation. Override this method in a subclass to provide, e.g.,
        predictions based on random samples, lookups in a dictionary, etc.

        Parameters
        ----------
        y : theano variable
            A symbolic variable representing the "raw" output of the recurrent
            predictor.

        Returns
        -------
        y : theano variable
            A symbolic variable representing the inputs for the next time step.
        '''
        return y

class Regressor(Network):
    '''A regressor attempts to produce a target output.'''

    def setup_vars(self):
        self.x = TT.tensor3('x')
        self.k = TT.tensor3('k')

    @property
    def inputs(self):
        return [self.x, self.k]

    @property
    def cost(self):
        err = self.y - self.k
        return TT.mean((err * err).sum(axis=2)[self.error_start:])

class Classifier(Network):
    '''A classifier attempts to match a 1-hot target output.'''

    def __init__(self, **kwargs):
        kwargs['output_activation'] = 'softmax'
        super(Classifier, self).__init__(**kwargs)

    def setup_vars(self):
        super(Classifier, self).setup_vars()

        # for a classifier, k specifies the correct labels for a given input.
        self.k = TT.ivector('k')

    @property
    def inputs(self):
        return [self.x, self.k]

    @property
    def cost(self):
        return -TT.mean(TT.log(self.y)[TT.arange(self.k.shape[0]), self.k])

    @property
    def accuracy(self):
        '''Compute the percent correct classifications.'''
        return 100 * TT.mean(TT.eq(TT.argmax(self.y, axis=1), self.k))

    @property
    def monitors(self):
        yield 'acc', self.accuracy
        for i, h in enumerate(self.hiddens):
            yield 'h{}<0.1'.format(i+1), 100 * (abs(h) < 0.1).mean()
            yield 'h{}<0.9'.format(i+1), 100 * (abs(h) < 0.9).mean()

    def classify(self, x):
        return self.predict(x).argmax(axis=1)
<commit_msg>Refactor LSTM as a mixin.<commit_after># Copyright (c) 2012 Leif Johnson <leif@leifjohnson.net>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

'''This file contains recurrent network structures.'''

import climate
import numpy as np
import numpy.random as rng
import theano
import theano.tensor as TT

#from theano.tensor.shared_randomstreams import RandomStreams
from theano.sandbox.rng_mrg import MRG_RandomStreams as RandomStreams

from . import feedforward as ff

logging = climate.get_logger(__name__)

class Network(ff.Network):
    '''A fully connected recurrent network with one input and one output layer.

    Parameters
    ----------
    layers : sequence of int
        A sequence of integers specifying the number of units at each layer. As
        an example, layers=(10, 20, 3) has one "input" layer with 10 units, one
        "hidden" layer with 20 units, and one "output" layer with 3 units. That
        is, inputs should be of length 10, and outputs will be of length 3.

    recurrent_layers : sequence of int, optional
        A sequence of integers specifying the indices of recurrent layers in the
        network. Non-recurrent network layers receive input only from the
        preceding layers for a given input, while recurrent layers also receive
        input from the output of the recurrent layer from the previous time
        step. The index values in this sequence must be greater than 0; that is,
        the input of a network cannot be recurrent. Defaults to
        [len(layers) // 2 - 1], i.e., the "middle" layer of the network is the
        only recurrent layer.

    hidden_activation : str, optional
        The name of an activation function to use on hidden network units.
        Defaults to 'sigmoid'.

    output_activation : str, optional
        The name of an activation function to use on output units. Defaults to
        'linear'.

    rng : theano.RandomStreams, optional
        Use a specific Theano random number generator. A new one will be created
        if this is None.

    input_noise : float, optional
        Standard deviation of desired noise to inject into input.

    hidden_noise : float, optional
        Standard deviation of desired noise to inject into hidden unit
        activation output.

    input_dropouts : float, optional
        Proportion of input units to randomly set to 0.

    hidden_dropouts : float, optional
        Proportion of hidden unit activations to randomly set to 0.

    recurrent_error_start : int, optional
        Compute error metrics starting at this time step. (Defaults to 3.)
    '''

    @property
    def error_start(self):
        return self.kwargs.get('recurrent_error_start', 3)

    def setup_vars(self):
        # the first dimension indexes time, the second indexes the elements of
        # each minibatch, and the third indexes the variables in a given frame.
        self.x = TT.tensor3('x')

    def setup_layers(self, **kwargs):
        '''
        '''
        count = 0

        noise = kwargs.get('input_noise', 0)
        dropout = kwargs.get('input_dropouts', 0)
        x = self._add_noise(self.x, noise, dropout)

        noise = kwargs.get('hidden_noise', 0)
        dropout = kwargs.get('hidden_dropouts', 0)
        layers = kwargs.get('layers')
        recurrent = set(kwargs.get('recurrent_layers', [len(layers) // 2 - 1]))
        for i, (nin, nout) in enumerate(zip(layers[:-1], layers[1:])):
            if i in recurrent:
                x, n = self.add_recurrent_layer(x, nin, nout, i)
                count += n
            else:
                count += (nin + 1) * nout
                x = self.add_feedforward_layer(
                    x, nin, nout, i, noise=noise, dropout=dropout)

        self.y = self.hiddens.pop()
        logging.info('%d total network parameters', count)

    def add_recurrent_layer(self, x, nin, nout, label=None):
        '''Add a new recurrent layer to the network.

        Parameters
        ----------
        input : theano variable
            The theano variable that represents the inputs to this layer.
        nin : int
            The number of input units to this layer.
        nout : out
            The number of output units from this layer.
        label : any, optional
            The name of this layer, used for logging and as the theano variable
            name suffix. Defaults to the index of this layer in the network.

        Returns
        -------
        output : theano variable
            The theano variable that represents the outputs from this layer.
        count : int
            The number of learnable parameters in this layer.
        '''
        b_h, _ = self.create_bias(nout, 'h_{}'.format(label))
        W_xh, _ = self.create_weights(nin, nout, 'xh_{}'.format(label))
        W_hh, _ = self.create_weights(nout, nout, 'hh_{}'.format(label))

        def fn(x_t, h_tm1, W_xh, W_hh, b_h):
            return self._hidden_func(TT.dot(x_t, W_xh) + TT.dot(h_tm1, W_hh) + b_h)

        batch_size = self.kwargs.get('batch_size', 64)
        h, updates = theano.scan(
            name='f_{}'.format(label),
            fn=fn,
            sequences=x,
            outputs_info=[TT.zeros((batch_size, nout), dtype=ff.FLOAT)],
            non_sequences=[W_xh, W_hh, b_h])

        self.updates.update(updates)
        self.hiddens.append(h)
        self.weights.extend([W_xh, W_hh])
        self.biases.append(b_h)

        return h, nout * (1 + nin + nout)

class LSTM(Network):
    '''
    '''

    def add_recurrent_layer(self, x, nin, nout, label=None):
        '''Add a new recurrent layer to the network.

        Parameters
        ----------
        input : theano variable
            The theano variable that represents the inputs to this layer.
        nin : int
            The number of input units to this layer.
        nout : out
            The number of output units from this layer.
        label : any, optional
            The name of this layer, used for logging and as the theano variable
            name suffix. Defaults to the index of this layer in the network.

        Returns
        -------
        output : theano variable
            The theano variable that represents the outputs from this layer.
        count : int
            The number of learnable parameters in this layer.
        '''
        b_i, _ = self.create_bias(nout, 'input')
        b_f, _ = self.create_bias(nout, 'forget')
        b_o, _ = self.create_bias(nout, 'output')
        b_c, _ = self.create_bias(nout, 'cell')

        # these weight matrices are always diagonal.
        W_ci, _ = self.create_bias(nout, 'ci')
        W_cf, _ = self.create_bias(nout, 'cf')
        W_co, _ = self.create_bias(nout, 'co')

        W_xi, _ = self.create_weights(nin, nout, 'xi')
        W_xf, _ = self.create_weights(nin, nout, 'xf')
        W_xo, _ = self.create_weights(nin, nout, 'xo')
        W_xc, _ = self.create_weights(nin, nout, 'xc')

        W_hi, _ = self.create_weights(nout, nout, 'hi')
        W_hf, _ = self.create_weights(nout, nout, 'hf')
        W_ho, _ = self.create_weights(nout, nout, 'ho')
        W_hc, _ = self.create_weights(nout, nout, 'hc')

        def fn(x_t, h_tm1, c_tm1, W_ci, W_cf, W_co, W_xi, W_xf, W_xo, W_xc, W_hi, W_hf, W_ho, W_hc, b_i, b_f, b_o, b_c):
            i_t = TT.nnet.sigmoid(TT.dot(x_t, W_xi) +
                                  TT.dot(h_tm1, W_hi) +
                                  TT.dot(c_tm1, TT.diag(W_ci)) + b_i)
            f_t = TT.nnet.sigmoid(TT.dot(x_t, W_xf) +
                                  TT.dot(h_tm1, W_hf) +
                                  TT.dot(c_tm1, TT.diag(W_cf)) + b_f)
            c_t = f_t * c_tm1 + i_t * TT.tanh(TT.dot(x_t, W_xc) +
                                              TT.dot(h_tm1, W_hc) + b_c)
            o_t = TT.nnet.sigmoid(TT.dot(x_t, W_xo) +
                                  TT.dot(h_tm1, W_ho) +
                                  TT.dot(c_t, TT.diag(W_co)) + b_o)
            h_t = o_t * TT.tanh(c_t)
            return h_t, c_t

        W = [W_ci, W_cf, W_co, W_xi, W_xf, W_xo, W_xc, W_hi, W_hf, W_ho, W_hc]
        B = [b_i, b_f, b_o, b_c]

        batch_size = self.kwargs.get('batch_size', 64)
        (h, _), updates = theano.scan(
            name='f_{}'.format(label), fn=fn, non_sequences=W + B, sequences=x,
            outputs_info=[TT.zeros((batch_size, nout), dtype=ff.FLOAT),
                          TT.zeros((batch_size, nout), dtype=ff.FLOAT)])

        self.updates.update(updates)
        self.hiddens.append(h)
        self.weights.extend(W)
        self.biases.extend(B)

        return h, nout * (7 + 4 * nout + 4 * nin)

class Autoencoder(Network):
    '''An autoencoder attempts to reproduce its input.'''

    @property
    def cost(self):
        err = self.y - self.x
        return TT.mean((err * err).sum(axis=2)[self.error_start:])

class Predictor(Autoencoder):
    '''A predictor network attempts to predict its next time step.
    '''

    @property
    def cost(self):
        # we want the network to predict the next time step. y is the prediction
        # (output of the network), so we want y[0] to match x[1], y[1] to match
        # x[2], and so forth.
        err = self.x[1:] - self.generate_prediction(self.y)[:-1]
        return TT.mean((err * err).sum(axis=2)[self.error_start:])

    def generate_prediction(self, y):
        '''Given outputs from each time step, map them to subsequent inputs.

        This defaults to the identity transform, i.e., the output from one time
        step is treated as the input to the next time step with no
        transformation. Override this method in a subclass to provide, e.g.,
        predictions based on random samples, lookups in a dictionary, etc.

        Parameters
        ----------
        y : theano variable
            A symbolic variable representing the "raw" output of the recurrent
            predictor.

        Returns
        -------
        y : theano variable
            A symbolic variable representing the inputs for the next time step.
        '''
        return y

class Regressor(Network):
    '''A regressor attempts to produce a target output.'''

    def setup_vars(self):
        self.x = TT.tensor3('x')
        self.k = TT.tensor3('k')

    @property
    def inputs(self):
        return [self.x, self.k]

    @property
    def cost(self):
        err = self.y - self.k
        return TT.mean((err * err).sum(axis=2)[self.error_start:])

class Classifier(Network):
    '''A classifier attempts to match a 1-hot target output.'''

    def __init__(self, **kwargs):
        kwargs['output_activation'] = 'softmax'
        super(Classifier, self).__init__(**kwargs)

    def setup_vars(self):
        super(Classifier, self).setup_vars()

        # for a classifier, k specifies the correct labels for a given input.
        self.k = TT.ivector('k')

    @property
    def inputs(self):
        return [self.x, self.k]

    @property
    def cost(self):
        return -TT.mean(TT.log(self.y)[TT.arange(self.k.shape[0]), self.k])

    @property
    def accuracy(self):
        '''Compute the percent correct classifications.'''
        return 100 * TT.mean(TT.eq(TT.argmax(self.y, axis=1), self.k))

    @property
    def monitors(self):
        yield 'acc', self.accuracy
        for i, h in enumerate(self.hiddens):
            yield 'h{}<0.1'.format(i+1), 100 * (abs(h) < 0.1).mean()
            yield 'h{}<0.9'.format(i+1), 100 * (abs(h) < 0.9).mean()

    def classify(self, x):
        return self.predict(x).argmax(axis=1)
<|endoftext|>


<commit_before>import re
import logging

from praw.errors import Forbidden

from images_of import AcceptFlag

LOG = logging.getLogger(__name__)

class Match:
    def __init__(self, reason, detail):
        self.reason = reason
        self.detail = detail

class Subreddit:
    def __init__(self, name, search, feeds=[], ignore=None, ignore_case=None, whitelist=[],
                 blacklist=[], wiki_blacklist=False, feed_limit=None, **kwargs):
        """
        Create new Subreddit object. This object is responsible for
        checking if posts meet sub-specific criteria for whether or
        not a post belongs.

        :param name: Subreddit name
        :param feeds: Atom feeds of content to be posted
        :param search: terms or regexes to be searched
        :param ignore: terms or regexes to ignore
        :param whitelist: list of subreddits to always accept
        :param blacklist: list of subreddits to never accept
        :param wiki_blacklist: subreddit has a blacklist on it's wiki
        """

        LOG.debug('Setting up /r/{}'.format(name))

        def make_regex(x, flags=0):
            if x is None:
                return None
            if isinstance(x, str):
                x = [x]

            terms = '(\\b{}\\b)'.format('\\b|\\b'.join(x))
            return re.compile(terms, flags)

        self.name = name
        self.feeds = feeds
        self.search_re = make_regex(search, re.IGNORECASE)
        self.ignore_re = make_regex(ignore, re.IGNORECASE)
        self.ignore_case_re = make_regex(ignore_case)
        self.whitelist = [sub.lower() for sub in whitelist]
        blacklist_pats = [sub.lower() for sub in blacklist]
        self.blacklist_res = [re.compile(pat) for pat in blacklist_pats]
        self.wiki_blacklist = wiki_blacklist
        self.feed_limit = feed_limit

        if kwargs:
            bad_keys = list(kwargs.keys())
            LOG.warning('Unrecognized subreddit settings: {}'.format(bad_keys))

    def load_wiki_blacklist(self, r):
        """
        Load blacklist from subreddit wiki.

        The subreddit may know that it needs to fetch pages externally,
        but is only a container for settings, and cannot fetch the
        contents of a wiki page by itself. Provided with a Reddit instance,
        it can go out and update it's blacklist.

        The wiki page 'subredditblacklist' should be a list of users,
        one per line, including the /u/ portion of the username.

        :param r: A praw.Reddit object.
        """
        if not self.wiki_blacklist or hasattr(self, 'wiki_blacklist_loaded'):
            return

        try:
            LOG.info('Loading wiki blacklist for /r/{}'.format(self.name))
            content = r.get_wiki_page(self.name, 'subredditblacklist').content_md
            subs = set(sub.strip().lower()[3:] for sub in content.splitlines() if sub)
            wiki_blacklist = subs.union(self.blacklist)
        except Forbidden:
            LOG.warning('Forbidden from reading blacklist on /r/{}'.format(self.name))
            wiki_blacklist = set()

        blacklist_pats = sorted(wiki_blacklist.union(self.blacklist))
        self.blacklist_res = [re.compile(pat) for pat in blacklist_pats]
        self.wiki_blacklist_loaded = True

    def check(self, post, flag=AcceptFlag.OK):
        """See if post is appropriate for this subreddit"""

        if flag is AcceptFlag.BAD:
            return

        title = post.title
        post_sub = post.subreddit.display_name.lower()

        if post_sub in self.whitelist:
            return Match('whitelist', post_sub)
        if flag is AcceptFlag.OK_IF_WHITELISTED:
            return
        if any(bl_sub.fullmatch(post_sub) for bl_sub in self.blacklist):
            return

        if self.ignore_case_re and self.ignore_case_re.search(title):
            return
        if self.ignore_re and self.ignore_re.search(title):
            return

        match = self.search_re.search(title)
        if match:
            return Match('match', match.group())

        return

<commit_msg>missed variable rename<commit_after>import re
import logging

from praw.errors import Forbidden

from images_of import AcceptFlag

LOG = logging.getLogger(__name__)

class Match:
    def __init__(self, reason, detail):
        self.reason = reason
        self.detail = detail

class Subreddit:
    def __init__(self, name, search, feeds=[], ignore=None, ignore_case=None, whitelist=[],
                 blacklist=[], wiki_blacklist=False, feed_limit=None, **kwargs):
        """
        Create new Subreddit object. This object is responsible for
        checking if posts meet sub-specific criteria for whether or
        not a post belongs.

        :param name: Subreddit name
        :param feeds: Atom feeds of content to be posted
        :param search: terms or regexes to be searched
        :param ignore: terms or regexes to ignore
        :param whitelist: list of subreddits to always accept
        :param blacklist: list of subreddits to never accept
        :param wiki_blacklist: subreddit has a blacklist on it's wiki
        """

        LOG.debug('Setting up /r/{}'.format(name))

        def make_regex(x, flags=0):
            if x is None:
                return None
            if isinstance(x, str):
                x = [x]

            terms = '(\\b{}\\b)'.format('\\b|\\b'.join(x))
            return re.compile(terms, flags)

        self.name = name
        self.feeds = feeds
        self.search_re = make_regex(search, re.IGNORECASE)
        self.ignore_re = make_regex(ignore, re.IGNORECASE)
        self.ignore_case_re = make_regex(ignore_case)
        self.whitelist = [sub.lower() for sub in whitelist]
        blacklist_pats = [sub.lower() for sub in blacklist]
        self.blacklist_res = [re.compile(pat) for pat in blacklist_pats]
        self.wiki_blacklist = wiki_blacklist
        self.feed_limit = feed_limit

        if kwargs:
            bad_keys = list(kwargs.keys())
            LOG.warning('Unrecognized subreddit settings: {}'.format(bad_keys))

    def load_wiki_blacklist(self, r):
        """
        Load blacklist from subreddit wiki.

        The subreddit may know that it needs to fetch pages externally,
        but is only a container for settings, and cannot fetch the
        contents of a wiki page by itself. Provided with a Reddit instance,
        it can go out and update it's blacklist.

        The wiki page 'subredditblacklist' should be a list of users,
        one per line, including the /u/ portion of the username.

        :param r: A praw.Reddit object.
        """
        if not self.wiki_blacklist or hasattr(self, 'wiki_blacklist_loaded'):
            return

        try:
            LOG.info('Loading wiki blacklist for /r/{}'.format(self.name))
            content = r.get_wiki_page(self.name, 'subredditblacklist').content_md
            subs = set(sub.strip().lower()[3:] for sub in content.splitlines() if sub)
            wiki_blacklist = subs.union(self.blacklist)
        except Forbidden:
            LOG.warning('Forbidden from reading blacklist on /r/{}'.format(self.name))
            wiki_blacklist = set()

        blacklist_pats = sorted(wiki_blacklist.union(self.blacklist))
        self.blacklist_res = [re.compile(pat) for pat in blacklist_pats]
        self.wiki_blacklist_loaded = True

    def check(self, post, flag=AcceptFlag.OK):
        """See if post is appropriate for this subreddit"""

        if flag is AcceptFlag.BAD:
            return

        title = post.title
        post_sub = post.subreddit.display_name.lower()

        if post_sub in self.whitelist:
            return Match('whitelist', post_sub)
        if flag is AcceptFlag.OK_IF_WHITELISTED:
            return
        if any(bl_sub.fullmatch(post_sub) for bl_sub in self.blacklist_res):
            return

        if self.ignore_case_re and self.ignore_case_re.search(title):
            return
        if self.ignore_re and self.ignore_re.search(title):
            return

        match = self.search_re.search(title)
        if match:
            return Match('match', match.group())

        return

<|endoftext|>


<commit_before># Copyright 2019 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

bazel_version = "87e3017b8761c6769fbaa1f1370fed6df82fc9cc"
bazel_sha256 = "3ff26941dff37b87707b571ce0316775e38f66802f2bdaafb567f6eba7bcdaf4"
<commit_msg>Update Bazel to latest version<commit_after># Copyright 2019 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

bazel_version = "e2a3b0f7905becc041ff960ac191b1d37a8ced22"
bazel_sha256 = "3ff75a1a60b77b3ba354bf8182bec07cca0e018419613b53925d1d20e88659ec"
<|endoftext|>


<commit_before>#!/usr/bin/python3
import random

def run(message):
    args = message['text'].slpit()[1:]
    if len(args) == 0:
        return str(random.randint(1, 6))
    elif len(args) == 1 and args[0].isdigit():
        return str(random.randint(1, int(args[0])))
    return 'Incorrect syntax, see /help for help"
<commit_msg>Fixed syntax error in roll<commit_after>#!/usr/bin/python3
import random

def run(message):
    args = message['text'].slpit()[1:]
    if len(args) == 0:
        return str(random.randint(1, 6))
    elif len(args) == 1 and args[0].isdigit():
        return str(random.randint(1, int(args[0])))
    return 'Incorrect syntax, see /help for help'
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.conf import settings
from django.views.generic import RedirectView

import autocomplete_light
autocomplete_light.autodiscover()

from apps.core import views

if 'django.contrib.admin' in settings.INSTALLED_APPS:
    admin.autodiscover()

urlpatterns = patterns('',
    url('^$',  RedirectView.as_view(url='/main/')),
    url(r'^main/$', views.main, name='main'),
    url(r'^mo/', include('apps.mo.urls')),
    url(r'^obj/', include('apps.build.urls')),
    url(r'^cmp/', include('apps.cmp.urls')),
    url(r'^payment/', include('apps.payment.urls')),
    url(r'^user/', include('apps.user.urls')),
    url(r'^imgfile/', include('apps.imgfile.urls')),
    url(r'^media/(?P<name>.+)$', 'apps.core.views.download_file'),
    url(r"^login/$", "django.contrib.auth.views.login",
        {"template_name": "login.html"},
        name="auth-login"),
    url(r"^logout/$", "django.contrib.auth.views.logout_then_login",
        {"login_url": "/login"}, name="logout"),
    url(r'^autocomplete/', include('autocomplete_light.urls')),

)

if 'django.contrib.admin' in settings.INSTALLED_APPS and settings.DEBUG:
    urlpatterns += patterns(
        '',
        url(r'^admin/', include(admin.site.urls)),
    )
<commit_msg>fix for url<commit_after># -*- coding: utf-8 -*-
from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.conf import settings
from django.views.generic import RedirectView

import autocomplete_light
autocomplete_light.autodiscover()

from apps.core import views

if 'django.contrib.admin' in settings.INSTALLED_APPS:
    admin.autodiscover()

urlpatterns = patterns('',
    url('^$',  RedirectView.as_view(url='/main/')),
    url(r'^main/$', views.main, name='main'),
    url(r'^mo/', include('apps.mo.urls')),
    url(r'^obj/', include('apps.build.urls')),
    url(r'^cmp/', include('apps.cmp.urls')),
    url(r'^core/', include('apps.core.urls')),
    url(r'^payment/', include('apps.payment.urls')),
    url(r'^user/', include('apps.user.urls')),
    url(r'^imgfile/', include('apps.imgfile.urls')),
    url(r'^media/(?P<name>.+)$', 'apps.core.views.download_file'),
    url(r"^login/$", "django.contrib.auth.views.login",
        {"template_name": "login.html"},
        name="auth-login"),
    url(r"^logout/$", "django.contrib.auth.views.logout_then_login",
        {"login_url": "/login"}, name="logout"),
    url(r'^autocomplete/', include('autocomplete_light.urls')),

)

if 'django.contrib.admin' in settings.INSTALLED_APPS and settings.DEBUG:
    urlpatterns += patterns(
        '',
        url(r'^admin/', include(admin.site.urls)),
    )
<|endoftext|>


<commit_before>"""Bundles common functions for maya.

Befor you use maya with our pipeline, call mayainit at least once. Usually the launcher does that for you.
"""
import os

import maya.standalone
import maya.cmds as cmds

from jukeboxcore import main
import jukeboxmaya
from jukeboxmaya.constants import MAYA_PLUGIN_PATH, BUILTIN_PLUGIN_PATH
from jukeboxmaya.plugins import MayaPluginManager
from jukeboxmaya.menu import MenuManager

def load_mayaplugins():
    """Loads the maya plugins (not jukebox plugins) of the pipeline

    :returns: None
    :rtype: None
    :raises: None
    """
    mpp = os.environ.get('MAYA_PLUG_IN_PATH')
    if mpp is not None:
        ';'.join([mpp, MAYA_PLUGIN_PATH])
    else:
        mpp = MAYA_PLUGIN_PATH

    # to simply load all plugins inside our plugin path, we override pluginpath temporarly
    os.environ['MAYA_PLUG_IN_PATH'] = MAYA_PLUGIN_PATH
    cmds.loadPlugin(allPlugins=True)
    # then we set the MAYA_PLUG_IN_PATH to the correct value
    # NOTE: this ignores the order of paths in MAYA_PLUG_IN_PATH completely
    os.environ['MAYA_PLUG_IN_PATH'] = mpp

def init():
    """Initialize the pipeline in maya so everything works

    Init environment and load plugins.
    This also creates the initial Jukebox Menu entry.

    :returns: None
    :rtype: None
    :raises: None
    """
    main.init_environment()
    pluginpath = os.pathsep.join((os.environ.get('JUKEBOX_PLUGIN_PATHS', ''), BUILTIN_PLUGIN_PATH))
    os.environ['JUKEBOX_PLUGIN_PATHS'] = pluginpath
    try:
        maya.standalone.initialize()
        jukeboxmaya.STANDALONE_INITIALIZED = True
    except RuntimeError as e:
        jukeboxmaya.STANDALONE_INITIALIZED = False
        if str(e) == "maya.standalone may only be used from an external Python interpreter":
            mm = MenuManager.get()
            mm.create_menu("Jukebox", tearOff=True)
    # load plugins
    pmanager = MayaPluginManager.get()
    pmanager.load_plugins()
    load_mayaplugins()
<commit_msg>Fix paths to path<commit_after>"""Bundles common functions for maya.

Befor you use maya with our pipeline, call mayainit at least once. Usually the launcher does that for you.
"""
import os

import maya.standalone
import maya.cmds as cmds

from jukeboxcore import main
import jukeboxmaya
from jukeboxmaya.constants import MAYA_PLUGIN_PATH, BUILTIN_PLUGIN_PATH
from jukeboxmaya.plugins import MayaPluginManager
from jukeboxmaya.menu import MenuManager

def load_mayaplugins():
    """Loads the maya plugins (not jukebox plugins) of the pipeline

    :returns: None
    :rtype: None
    :raises: None
    """
    mpp = os.environ.get('MAYA_PLUG_IN_PATH')
    if mpp is not None:
        ';'.join([mpp, MAYA_PLUGIN_PATH])
    else:
        mpp = MAYA_PLUGIN_PATH

    # to simply load all plugins inside our plugin path, we override pluginpath temporarly
    os.environ['MAYA_PLUG_IN_PATH'] = MAYA_PLUGIN_PATH
    cmds.loadPlugin(allPlugins=True)
    # then we set the MAYA_PLUG_IN_PATH to the correct value
    # NOTE: this ignores the order of paths in MAYA_PLUG_IN_PATH completely
    os.environ['MAYA_PLUG_IN_PATH'] = mpp

def init():
    """Initialize the pipeline in maya so everything works

    Init environment and load plugins.
    This also creates the initial Jukebox Menu entry.

    :returns: None
    :rtype: None
    :raises: None
    """
    main.init_environment()
    pluginpath = os.pathsep.join((os.environ.get('JUKEBOX_PLUGIN_PATH', ''), BUILTIN_PLUGIN_PATH))
    os.environ['JUKEBOX_PLUGIN_PATH'] = pluginpath
    try:
        maya.standalone.initialize()
        jukeboxmaya.STANDALONE_INITIALIZED = True
    except RuntimeError as e:
        jukeboxmaya.STANDALONE_INITIALIZED = False
        if str(e) == "maya.standalone may only be used from an external Python interpreter":
            mm = MenuManager.get()
            mm.create_menu("Jukebox", tearOff=True)
    # load plugins
    pmanager = MayaPluginManager.get()
    pmanager.load_plugins()
    load_mayaplugins()
<|endoftext|>


<commit_before>from django.db import models
from month import forms
from month import widgets
from month import Month
import datetime

class MonthField(models.DateField):
    description = "A specific month of a specific year."
    widget = widgets.MonthSelectorWidget
    def to_python(self, value):
        if isinstance(value, Month):
            month = value
        elif isinstance(value, datetime.date):
            month = Month.from_date(value)
        elif isinstance(value, basestring):
            month = Month.from_string(value)
        else:
            month = None
        return month

    def get_prep_value(self, value):
        month = self.to_python(value)
        if month is not None:
            return month.first_day()
        return None

    def from_db_value(self, value, expression, connection, context):
        return self.to_python(value)

    def clean(self, value, instance):
        return self.to_python(value)

    def formfield(self, **kwargs):
        #defaults = {'widget': self.widget}
        #defaults.update(kwargs)
        #return forms.MonthField(**defaults)

        #The widget is allready being specified somewhere by models.DateField...
        kwargs['widget'] = self.widget
        return forms.MonthField(**kwargs)
<commit_msg>Posible fix para python3<commit_after>from django.db import models
from month import forms
from month import widgets
from month import Month
import datetime

# Patch python3 basestring
# Credits @leingang
# https://github.com/oxplot/fysom/issues/1

try:
    unicode = unicode
except NameError:
    # 'unicode' is undefined, must be Python 3
    str = str
    unicode = str
    bytes = bytes
    basestring = (str,bytes)
else:
    # 'unicode' exists, must be Python 2
    str = str
    unicode = unicode
    bytes = str
    basestring = basestring

class MonthField(models.DateField):
    description = "A specific month of a specific year."
    widget = widgets.MonthSelectorWidget
    def to_python(self, value):
        if isinstance(value, Month):
            month = value
        elif isinstance(value, datetime.date):
            month = Month.from_date(value)
        elif isinstance(value, basestring):
            month = Month.from_string(value)
        else:
            month = None
        return month

    def get_prep_value(self, value):
        month = self.to_python(value)
        if month is not None:
            return month.first_day()
        return None

    def from_db_value(self, value, expression, connection, context):
        return self.to_python(value)

    def clean(self, value, instance):
        return self.to_python(value)

    def formfield(self, **kwargs):
        #defaults = {'widget': self.widget}
        #defaults.update(kwargs)
        #return forms.MonthField(**defaults)

        #The widget is allready being specified somewhere by models.DateField...
        kwargs['widget'] = self.widget
        return forms.MonthField(**kwargs)
<|endoftext|>