<commit_before>import tempfile
import shutil
import os.path
import argparse
from subprocess import check_call
from shlex import split

import lxml.etree as etree

def htmlpreview(bibfile, citekey, bibstyle):
    basename = os.path.split(bibfile)[1].split('.')[0]
    workingdir = tempfile.mkdtemp()

    previewtemplate = r"""
\documentclass[letterpaper]{article}
\usepackage{hyperref}
\pagestyle{empty}
\renewcommand{\refname}{}
\begin{document}
\nocite{%(citekey)s}
\bibliography{%(bibfile)s}
\bibliographystyle{%(bibstyle)s}
\end{document}
"""

    with open(os.path.join(workingdir, 'preview.tex'), 'w') as previewfile:
        previewfile.write(previewtemplate % {'citekey': citekey,
                                             'bibfile': basename,
                                             'bibstyle': bibstyle})

    shutil.copy(bibfile, workingdir)

    with open(os.devnull, 'w') as devnull:
        check_call(split('htlatex preview'), stdout=devnull, cwd=workingdir)
        check_call(split('bibtex preview'), stdout=devnull, cwd=workingdir)
        check_call(split('htlatex preview'), stdout=devnull, cwd=workingdir)

    with open(os.path.join(workingdir, 'preview.html'), 'r') as outfile:
        parser = etree.HTMLParser()
        tree = etree.parse(outfile, parser)

        node = tree.xpath("//p[@class='bibitem']")[0]
        node.remove(node.xpath("//span[@class='biblabel']")[0])

        html = etree.tostring(node, encoding=unicode, method='html')

    shutil.rmtree(workingdir)
    return html

def main():
    parser = argparse.ArgumentParser(description='Generate an HTML preview for a BibTeX entry.')
    parser.add_argument('file', help='BibTeX file')
    parser.add_argument('citekey', help='cite key')
    args = parser.parse_args()

    inputfile = os.path.abspath(args.file)

    print htmlpreview(inputfile, args.citekey)

if __name__ == '__main__':
    main()
<commit_msg>Fix standalone preview generation; the style argument was missing.<commit_after>import tempfile
import shutil
import os.path
import argparse
from subprocess import check_call
from shlex import split

import lxml.etree as etree

def htmlpreview(bibfile, citekey, bibstyle):
    basename = os.path.split(bibfile)[1].split('.')[0]
    workingdir = tempfile.mkdtemp()

    previewtemplate = r"""
\documentclass[letterpaper]{article}
\usepackage{hyperref}
\pagestyle{empty}
\renewcommand{\refname}{}
\begin{document}
\nocite{%(citekey)s}
\bibliography{%(bibfile)s}
\bibliographystyle{%(bibstyle)s}
\end{document}
"""

    with open(os.path.join(workingdir, 'preview.tex'), 'w') as previewfile:
        previewfile.write(previewtemplate % {'citekey': citekey,
                                             'bibfile': basename,
                                             'bibstyle': bibstyle})

    shutil.copy(bibfile, workingdir)

    with open(os.devnull, 'w') as devnull:
        check_call(split('htlatex preview'), stdout=devnull, cwd=workingdir)
        check_call(split('bibtex preview'), stdout=devnull, cwd=workingdir)
        check_call(split('htlatex preview'), stdout=devnull, cwd=workingdir)

    with open(os.path.join(workingdir, 'preview.html'), 'r') as outfile:
        parser = etree.HTMLParser()
        tree = etree.parse(outfile, parser)

        node = tree.xpath("//p[@class='bibitem']")[0]
        node.remove(node.xpath("//span[@class='biblabel']")[0])

        html = etree.tostring(node, encoding=unicode, method='html')

    shutil.rmtree(workingdir)
    return html

def main():
    parser = argparse.ArgumentParser(description='Generate an HTML preview for a BibTeX entry.')
    parser.add_argument('-s', '--style', help="BibTeX style", default='IEEEtran')
    parser.add_argument('file', help='BibTeX file')
    parser.add_argument('citekey', help='cite key')
    args = parser.parse_args()

    inputfile = os.path.abspath(args.file)

    print htmlpreview(inputfile, args.citekey, args.style)

if __name__ == '__main__':
    main()
<|endoftext|>


<commit_before>#!/usr/bin/python2.5
#
# Copyright 2008 the Melange authors.
#
# 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.

"""This module contains the Linkable base class Model."""

__authors__ = [
  '"Todd Larsen" <tlarsen@google.com>',
]

from google.appengine.ext import db

from django.utils.translation import ugettext_lazy

from soc.models import base

class Linkable(base.ModelWithFieldAttributes):
  """A base class for Model classes that are "linkable".

  Many entities in Melange are identified by a "link path" that is formed
  by two components:  a "link scope" and a "link ID".

  The link scope is a reference to another Linkable entity, but its exact
  usage varies depending on:

   * the Model type of the entity
   * the "ownership" of the entity

  This scope represents the "context" of the entity and is *not* user-
  editable (site Developers will be able to *carefully* edit the scope
  of a Linkable entity, but implementing this will be tricky).

  Appended to this "link path prefix" generated from the transitive
  closure of the link scopes is a link ID.  Unlike the rest of the link
  path, this ID, which must be unique within the scope defined by the link
  path, is *not* determined by context and *is* supplied by the user.

  For example, a Document containing containing the FAQs for the
  Apache Software Foundation participation in GSoC 2009 program sponsored
  by Google could be given a link ID by the Apache organization
  administrator of "faqs", but the rest of the link path would be
  determined by the transitive closure of the scopes of the Document:

    google/gsoc2009/asf/faqs
      ^       ^      ^   ^
      |       |      |   +---------  link ID assigned by Apache admin
      |       |      |
      |       |      +-------------  Apache org link ID (immutable)
      |       |
      |       +--------------------  GSoC 2009 program link ID (immutable)
      |
      +----------------------------  Google sponsor link ID (immutable)

  For many entities, link IDs, once specified, are immutable, since
  changing them can break bookmarked URLs.  Changing the link IDs of
  "leaf" entities (such as the Document in the example above) could
  be allowed.
  """

  id = db.StringProperty(required=True,
      verbose_name=ugettext_lazy('Link ID'))
  id.help_text = ugettext_lazy(
      '"ID" used in URLs.'
      ' Lower ASCII characters, digits, and underscores only.')

  scope = db.SelfReferenceProperty(required=False,
      collection_name='links', verbose_name=ugettext_lazy('Link Scope'))
  scope.help_text = ugettext_lazy(
      'Reference to another Linkable entity that defines the "scope" of'
      ' this Linkable entity.')
<commit_msg>Fix typo in linkable model and add some missing comments.<commit_after>#!/usr/bin/python2.5
#
# Copyright 2008 the Melange authors.
#
# 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.

"""This module contains the Linkable base class Model."""

__authors__ = [
  '"Todd Larsen" <tlarsen@google.com>',
]

from google.appengine.ext import db

from django.utils.translation import ugettext_lazy

from soc.models import base

class Linkable(base.ModelWithFieldAttributes):
  """A base class for Model classes that are "linkable".

  Many entities in Melange are identified by a "link path" that is formed
  by two components:  a "link scope" and a "link ID".

  The link scope is a reference to another Linkable entity, but its exact
  usage varies depending on:

   * the Model type of the entity
   * the "ownership" of the entity

  This scope represents the "context" of the entity and is *not* user-
  editable (site Developers will be able to *carefully* edit the scope
  of a Linkable entity, but implementing this will be tricky).

  Appended to this "link path prefix" generated from the transitive
  closure of the link scopes is a link ID.  Unlike the rest of the link
  path, this ID, which must be unique within the scope defined by the link
  path, is *not* determined by context and *is* supplied by the user.

  For example, a Document containing the FAQs for the Apache Software
  Foundation participation in GSoC 2009 program sponsored
  by Google could be given a link ID by the Apache organization
  administrator of "faqs", but the rest of the link path would be
  determined by the transitive closure of the scopes of the Document:

    google/gsoc2009/asf/faqs
      ^       ^      ^   ^
      |       |      |   +---------  link ID assigned by Apache admin
      |       |      |
      |       |      +-------------  Apache org link ID (immutable)
      |       |
      |       +--------------------  GSoC 2009 program link ID (immutable)
      |
      +----------------------------  Google sponsor link ID (immutable)

  For many entities, link IDs, once specified, are immutable, since
  changing them can break bookmarked URLs.  Changing the link IDs of
  "leaf" entities (such as the Document in the example above) could
  be allowed.
  """
  #: Required field storing "ID" used in URLS. Lower ASCII characters,
  #: digits and underscores only.
  id = db.StringProperty(required=True,
      verbose_name=ugettext_lazy('Link ID'))
  id.help_text = ugettext_lazy(
      '"ID" used in URLs.'
      ' Lower ASCII characters, digits and underscores only.')

  #: Optional Self Reference property to another Linkable entity which defines
  #: the "scope" of this Linkable entity. The back-reference in the Linkable
  #: model is a Query named 'links'.
  scope = db.SelfReferenceProperty(required=False,
      collection_name='links', verbose_name=ugettext_lazy('Link Scope'))
  scope.help_text = ugettext_lazy(
      'Reference to another Linkable entity that defines the "scope" of'
      ' this Linkable entity.')
<|endoftext|>


<commit_before>from django.core.management.base import BaseCommand, CommandError
from python_zipcodes.django_app.zipcodes.models import ZipCode
from python_zipcodes.countries import country_list
from python_zipcodes.importers import ZipCodeManager

class Command(BaseCommand):
    args = '
    help = 'Updates the zip codes'

    def handle(self, *args, **options):
        zc_manager = ZipCodeManager()
        for c in country_list:
            zc_manager.add(c)
            for zc, data in zc_manager.zipcodes(c).items():
                ZipCode.objects.get(zipcode=zc, country=c).delete()
                ZipCode.objects.create(zipcode=zc, city=data['city'], state=data['state'], country=c)
<commit_msg>typo<commit_after>from django.core.management.base import BaseCommand, CommandError
from python_zipcodes.django_app.zipcodes.models import ZipCode
from python_zipcodes.countries import country_list
from python_zipcodes.importers import ZipCodeManager

class Command(BaseCommand):
    args = ''
    help = 'Updates the zip codes'

    def handle(self, *args, **options):
        zc_manager = ZipCodeManager()
        for c in country_list:
            zc_manager.add(c)
            for zc, data in zc_manager.zipcodes(c).items():
                ZipCode.objects.get(zipcode=zc, country=c).delete()
                ZipCode.objects.create(zipcode=zc, city=data['city'], state=data['state'], country=c)
<|endoftext|>


<commit_before># Copyright 2018 Open Source Robotics Foundation, 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.

from rcl_interfaces.msg import ParameterDescriptor
from rcl_interfaces.msg import SetParametersResult
from rcl_interfaces.srv import DescribeParameters, GetParameters, GetParameterTypes
from rcl_interfaces.srv import ListParameters, SetParameters, SetParametersAtomically
from rclpy.exceptions import ParameterNotDeclaredException
from rclpy.parameter import Parameter, PARAMETER_SEPARATOR_STRING
from rclpy.qos import qos_profile_parameters
from rclpy.validate_topic_name import TOPIC_SEPARATOR_STRING

class ParameterService:

    def __init__(self, node):
        self._node = node
        nodename = node.get_name()

        describe_parameters_service_name = \
            TOPIC_SEPARATOR_STRING.join((nodename, 'describe_parameters'))
        node.create_service(
            DescribeParameters, describe_parameters_service_name,
            self._describe_parameters_callback, qos_profile=qos_profile_parameters
        )
        get_parameters_service_name = TOPIC_SEPARATOR_STRING.join((nodename, 'get_parameters'))
        node.create_service(
            GetParameters, get_parameters_service_name, self._get_parameters_callback,
            qos_profile=qos_profile_parameters
        )
        get_parameter_types_service_name = \
            TOPIC_SEPARATOR_STRING.join((nodename, 'get_parameter_types'))
        node.create_service(
            GetParameterTypes, get_parameter_types_service_name,
            self._get_parameter_types_callback, qos_profile=qos_profile_parameters
        )
        list_parameters_service_name = TOPIC_SEPARATOR_STRING.join((nodename, 'list_parameters'))
        node.create_service(
            ListParameters, list_parameters_service_name, self._list_parameters_callback,
            qos_profile=qos_profile_parameters
        )
        set_parameters_service_name = TOPIC_SEPARATOR_STRING.join((nodename, 'set_parameters'))
        node.create_service(
            SetParameters, set_parameters_service_name, self._set_parameters_callback,
            qos_profile=qos_profile_parameters
        )
        set_parameters_atomically_service_name = \
            TOPIC_SEPARATOR_STRING.join((nodename, 'set_parameters_atomically'))
        node.create_service(
            SetParametersAtomically, set_parameters_atomically_service_name,
            self._set_parameters_atomically_callback,
            qos_profile=qos_profile_parameters
        )

    def _describe_parameters_callback(self, request, response):
        for name in request.names:
            try:
                descriptor = self._node.describe_parameter(name)
            except ParameterNotDeclaredException:
                descriptor = ParameterDescriptor()
            response.descriptors.append(descriptor)
        return response

    def _get_parameters_callback(self, request, response):
        for name in request.names:
            p = self._node.get_parameter_or(name)
            response.values.append(p.get_parameter_value())
        return response

    def _get_parameter_types_callback(self, request, response):
        for name in request.names:
            response.types.append(self._node.get_parameter_or(name).type_)
        return response

    def _list_parameters_callback(self, request, response):
        names_with_prefixes = []
        for name in self._node._parameters.keys():
            if PARAMETER_SEPARATOR_STRING in name:
                names_with_prefixes.append(name)
                continue
            elif request.prefixes:
                continue
            else:
                response.result.names.append(name)
        if 1 == request.depth:
            return response

        if not request.DEPTH_RECURSIVE == request.depth:
            names_with_prefixes = filter(
                lambda name:
                    name.count(PARAMETER_SEPARATOR_STRING) < request.depth, names_with_prefixes
            )
        for name in names_with_prefixes:
            if request.prefixes:
                for prefix in request.prefixes:
                    if name.startswith(prefix + PARAMETER_SEPARATOR_STRING):
                        response.result.names.append(name)
                        full_prefix = PARAMETER_SEPARATOR_STRING.join(
                            name.split(PARAMETER_SEPARATOR_STRING)[0:-1])
                        if full_prefix not in response.result.prefixes:
                            response.result.prefixes.append(full_prefix)
                        if prefix not in response.result.prefixes:
                            response.result.prefixes.append(prefix)
            else:
                prefix = PARAMETER_SEPARATOR_STRING.join(
                    name.split(PARAMETER_SEPARATOR_STRING)[0:-1])
                if prefix not in response.result.prefixes:
                    response.result.prefixes.append(prefix)
                response.result.names.append(name)

        return response

    def _set_parameters_callback(self, request, response):
        for p in request.parameters:
            param = Parameter.from_parameter_msg(p)
            try:
                result = self._node.set_parameters_atomically([param])
            except ParameterNotDeclaredException as e:
                result = SetParametersResult(
                    successful=False,
                    reason=str(e)
                )
            response.results.append(result)
        return response

    def _set_parameters_atomically_callback(self, request, response):
        try:
            response.result = self._node.set_parameters_atomically([
                Parameter.from_parameter_msg(p) for p in request.parameters])
        except ParameterNotDeclaredException as e:
            response.result = SetParametersResult(
                    successful=False,
                    reason=str(e)
                )
        return response
<commit_msg>Get proper parameters with prefixes without dot separator (#455)<commit_after># Copyright 2018 Open Source Robotics Foundation, 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.

from rcl_interfaces.msg import ParameterDescriptor
from rcl_interfaces.msg import SetParametersResult
from rcl_interfaces.srv import DescribeParameters, GetParameters, GetParameterTypes
from rcl_interfaces.srv import ListParameters, SetParameters, SetParametersAtomically
from rclpy.exceptions import ParameterNotDeclaredException
from rclpy.parameter import Parameter, PARAMETER_SEPARATOR_STRING
from rclpy.qos import qos_profile_parameters
from rclpy.validate_topic_name import TOPIC_SEPARATOR_STRING

class ParameterService:

    def __init__(self, node):
        self._node = node
        nodename = node.get_name()

        describe_parameters_service_name = \
            TOPIC_SEPARATOR_STRING.join((nodename, 'describe_parameters'))
        node.create_service(
            DescribeParameters, describe_parameters_service_name,
            self._describe_parameters_callback, qos_profile=qos_profile_parameters
        )
        get_parameters_service_name = TOPIC_SEPARATOR_STRING.join((nodename, 'get_parameters'))
        node.create_service(
            GetParameters, get_parameters_service_name, self._get_parameters_callback,
            qos_profile=qos_profile_parameters
        )
        get_parameter_types_service_name = \
            TOPIC_SEPARATOR_STRING.join((nodename, 'get_parameter_types'))
        node.create_service(
            GetParameterTypes, get_parameter_types_service_name,
            self._get_parameter_types_callback, qos_profile=qos_profile_parameters
        )
        list_parameters_service_name = TOPIC_SEPARATOR_STRING.join((nodename, 'list_parameters'))
        node.create_service(
            ListParameters, list_parameters_service_name, self._list_parameters_callback,
            qos_profile=qos_profile_parameters
        )
        set_parameters_service_name = TOPIC_SEPARATOR_STRING.join((nodename, 'set_parameters'))
        node.create_service(
            SetParameters, set_parameters_service_name, self._set_parameters_callback,
            qos_profile=qos_profile_parameters
        )
        set_parameters_atomically_service_name = \
            TOPIC_SEPARATOR_STRING.join((nodename, 'set_parameters_atomically'))
        node.create_service(
            SetParametersAtomically, set_parameters_atomically_service_name,
            self._set_parameters_atomically_callback,
            qos_profile=qos_profile_parameters
        )

    def _describe_parameters_callback(self, request, response):
        for name in request.names:
            try:
                descriptor = self._node.describe_parameter(name)
            except ParameterNotDeclaredException:
                descriptor = ParameterDescriptor()
            response.descriptors.append(descriptor)
        return response

    def _get_parameters_callback(self, request, response):
        for name in request.names:
            p = self._node.get_parameter_or(name)
            response.values.append(p.get_parameter_value())
        return response

    def _get_parameter_types_callback(self, request, response):
        for name in request.names:
            response.types.append(self._node.get_parameter_or(name).type_)
        return response

    def _list_parameters_callback(self, request, response):
        names_with_prefixes = []
        for name in self._node._parameters.keys():
            if PARAMETER_SEPARATOR_STRING in name:
                names_with_prefixes.append(name)
                continue
            elif request.prefixes:
                for prefix in request.prefixes:
                    if name.startswith(prefix):
                        response.result.names.append(name)
                continue
            else:
                response.result.names.append(name)
        if 1 == request.depth:
            return response

        if not request.DEPTH_RECURSIVE == request.depth:
            names_with_prefixes = filter(
                lambda name:
                    name.count(PARAMETER_SEPARATOR_STRING) < request.depth, names_with_prefixes
            )
        for name in names_with_prefixes:
            if request.prefixes:
                for prefix in request.prefixes:
                    if name.startswith(prefix + PARAMETER_SEPARATOR_STRING):
                        response.result.names.append(name)
                        full_prefix = PARAMETER_SEPARATOR_STRING.join(
                            name.split(PARAMETER_SEPARATOR_STRING)[0:-1])
                        if full_prefix not in response.result.prefixes:
                            response.result.prefixes.append(full_prefix)
                        if prefix not in response.result.prefixes:
                            response.result.prefixes.append(prefix)
            else:
                prefix = PARAMETER_SEPARATOR_STRING.join(
                    name.split(PARAMETER_SEPARATOR_STRING)[0:-1])
                if prefix not in response.result.prefixes:
                    response.result.prefixes.append(prefix)
                response.result.names.append(name)

        return response

    def _set_parameters_callback(self, request, response):
        for p in request.parameters:
            param = Parameter.from_parameter_msg(p)
            try:
                result = self._node.set_parameters_atomically([param])
            except ParameterNotDeclaredException as e:
                result = SetParametersResult(
                    successful=False,
                    reason=str(e)
                )
            response.results.append(result)
        return response

    def _set_parameters_atomically_callback(self, request, response):
        try:
            response.result = self._node.set_parameters_atomically([
                Parameter.from_parameter_msg(p) for p in request.parameters])
        except ParameterNotDeclaredException as e:
            response.result = SetParametersResult(
                    successful=False,
                    reason=str(e)
                )
        return response
<|endoftext|>


<commit_before>"""
To create a new command for **Tyrant** the ``Command`` class needs to be
subclassed for your command and override the ``Command.execute`` method to
provide your command execution.
"""
from __future__ import print_function
__author__ = 'Taylor "Nekroze" Lawson'
__email__ = 'nekroze@eturnilnetwork.com'

class Command(object):
    """
    A command eases implementation of a **Tyrant** command.

    ``Command`` requires a command name to be used to call this command and a
    description that describes this commands usage.
    """
    def __init__(self, name, description):
        self.name = name
        self.description = description
        self.commands = {"help": self.help}

    def __getitem__(self, key):
        return self.commands[key]

    def __setitem__(self, key, value):
        self.commands[key] = value

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

    def __call__(self, args):
        if args and args[0] in self.commands:
            return self.commands[args[0]](args[1:])
        self.execute(args)

    def __delitem__(self, key):
        del self.commands[key]

    def add_command(self, command):
        """
        Add a subcommand to this command. Must be an instance of ``Command``.
        """
        assert isinstance(command, Command)
        self.commands[command.name] = command

    # pylint: disable=W0613
    def execute(self, args):
        """Execute the given command."""
        assert False, "{0} execution is not yet implemented.".format(self.name)
    # pylint: enable=W0613

    def help(self, args):
        """Display command usage help."""
        if args and args[0] in self.commands:
            print("{0} - {1}".format(args[0],
                                     self.commands[args[0]].description))
        else:
            print(self.name, ':')
            print(self.description + '\n')

            for key in sorted(self.commands.keys()):
                print("{0} - {1}".format(key, self.commands[key].description))
<commit_msg>Command execution now calls help by default<commit_after>"""
To create a new command for **Tyrant** the ``Command`` class needs to be
subclassed for your command and override the ``Command.execute`` method to
provide your command execution.
"""
from __future__ import print_function
__author__ = 'Taylor "Nekroze" Lawson'
__email__ = 'nekroze@eturnilnetwork.com'

class Command(object):
    """
    A command eases implementation of a **Tyrant** command.

    ``Command`` requires a command name to be used to call this command and a
    description that describes this commands usage.
    """
    def __init__(self, name, description):
        self.name = name
        self.description = description
        self.commands = {"help": self.help}

    def __getitem__(self, key):
        return self.commands[key]

    def __setitem__(self, key, value):
        self.commands[key] = value

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

    def __call__(self, args):
        if args and args[0] in self.commands:
            return self.commands[args[0]](args[1:])
        self.execute(args)

    def __delitem__(self, key):
        del self.commands[key]

    def add_command(self, command):
        """
        Add a subcommand to this command. Must be an instance of ``Command``.
        """
        assert isinstance(command, Command)
        self.commands[command.name] = command

    def execute(self, args):
        """Execute this command, by default just runs help."""
        self.help(args)

    def help(self, args):
        """Display command usage help."""
        if args and args[0] in self.commands:
            print("{0} - {1}".format(args[0],
                                     self.commands[args[0]].description))
        else:
            print(self.name, ':')
            print(self.description + '\n')

            for key in sorted(self.commands.keys()):
                print("{0} - {1}".format(key, self.commands[key].description))
<|endoftext|>


<commit_before>#!/usr/local/bin/python2.7
import sys

class Cmd_arithmetic:

    def __init__(self, code):
        self.code = code
        self.counter = -1

    def generate(self):
        self.counter += 1
        return self.code.replace('%d', str(self.counter))

class Cmd_push_pop:
    def __init__(self, code):
        self.code = code
        self.segment = 'constant'
        self.index = 0

    def generate(self):
        code = self.code
        if self.segment == 'constant':
            code = '    @' + str(self.index) + '\n' + self.code
        return code

cmd_add = Cmd_arithmetic(
    """    @SP
    M=M-1
    A=M
    D=M     // D is value at top of the stack
    @SP
    A=M-1   // Point to next highest on stack
    M=M+D   // Add that guy to D
"""
)

cmd_sub = Cmd_arithmetic(
    """    @SP
    M=M-1
    A=M
    D=M     // D is value at top of the stack
    @SP
    A=M-1   // Point to next highest on stack
    M=M-D   // Sub D from that guy
"""
)

cmd_neg = Cmd_arithmetic(
    """    @SP
    M=M-1
    A=M
    D=-M    // D is value at top of the stack
    M=D
    @SP
    M=M+1
"""
)

cmd_eq = Cmd_arithmetic(
    """    @SP
    M=M-1
    A=M
    D=M        // D is value at top of the stack
    @SP
    A=M-1      // Point to next highest on stack
    D=M-D      // Sub D from that guy
    @EQ_TRUE_%d
    D;JEQ
(EQ_FALSE_%d)   // NOT eq, set D to false (0)
    @0
    D=A
    @EQ_DONE_%d
    0;JMP
(EQ_TRUE_%d)    // eq, set D to true (-1)
    D=D-1      // D is 0, set D to true (-1)
(EQ_DONE_%d)
    @SP
    M=M-1
    A=M
    M=D
    @SP
    M=M+1
"""
)

cmd_lt = Cmd_arithmetic(
    """    @SP
    M=M-1
    A=M
    D=M        // D is value at top of the stack
    @SP
    A=M-1      // Point to next highest on stack
    D=M-D      // Sub D from that guy
    @LT_TRUE_%d
    D;JLT
(LT_FALSE_%d)   // NOT lt, set D to false (0)
    @0
    D=A
    @LT_DONE_%d
    0;JMP
(LT_TRUE_%d)    // lt, set D to true (-1)
    @0
    D=A-1      // set D to true (-1)
(LT_DONE_%d)
    @SP
    M=M-1
    A=M
    M=D
    @SP
    M=M+1
"""
)

cmd_gt = Cmd_arithmetic(
    """    @SP
    M=M-1
    A=M
    D=M        // D is value at top of the stack
    @SP
    A=M-1      // Point to next highest on stack
    D=M-D      // Sub D from that guy
    @GT_TRUE_%d
    D;JGT
(GT_FALSE_%d)   // NOT gt, set D to false (0)
    @0
    D=A
    @GT_DONE_%d
    0;JMP
(GT_TRUE_%d)    // gt, set D to true (-1)
    @0
    D=A-1      // set D to true (-1)
(GT_DONE_%d)
    @SP
    M=M-1
    A=M
    M=D
    @SP
    M=M+1
"""
)

cmd_and = Cmd_arithmetic(
    """    @SP
    M=M-1
    A=M
    D=M     // D is value at top of the stack
    @SP
    A=M-1   // Point to next highest on stack
    M=D&M   // Add D to that guy
"""
)

cmd_or = Cmd_arithmetic(
    """    @SP
    M=M-1
    A=M
    D=M     // D is value at top of the stack
    @SP
    A=M-1   // Point to next highest on stack
    M=D|M   // Add D to that guy
"""
)

cmd_not = Cmd_arithmetic(
    """    @SP
    M=M-1
    A=M
    D=!M    // D is value at top of the stack
    M=D
    @SP
    M=M+1
"""
)

cmd_push = Cmd_push_pop(
    """    D=A
    @SP
    A=M
    M=D
    @SP     // increment SP
    M=M+1
"""
)

cmd_pop = Cmd_push_pop(
    """    @LCL
    A=M
    @SP
    M=M-1   // decrement SP
    A=M
    D=M     // D holds our "precious"
    @LCL
    A=M
    M=D
"""
)

def parse(line):
    # strip comments
    no_comments = line.split('#', 1)[0]
    no_comments = no_comments.split('//', 1)[0]
    tokens = no_comments.split()
    return (tokens)

def writecode(tokens):
    if not tokens:
        return
    asm_file.write('//')
    for token in tokens:
        asm_file.write(' ' + str(token))
    asm_file.write('\n')
    if tokens[0] == 'add':
        asm_file.write(cmd_add.generate())
    if tokens[0] == 'sub':
        asm_file.write(cmd_sub.generate())
    if tokens[0] == 'neg':
        asm_file.write(cmd_neg.generate())
    if tokens[0] == 'eq':
        asm_file.write(cmd_eq.generate())
    if tokens[0] == 'lt':
        asm_file.write(cmd_lt.generate())
    if tokens[0] == 'gt':
        asm_file.write(cmd_gt.generate())
    if tokens[0] == 'and':
        asm_file.write(cmd_and.generate())
    if tokens[0] == 'or':
        asm_file.write(cmd_or.generate())
    if tokens[0] == 'not':
        asm_file.write(cmd_not.generate())
    if tokens[0] == 'push':
        cmd_push.segment = tokens[1]
        cmd_push.index = tokens[2]
        asm_file.write(cmd_push.generate())

def banner():
    asm_file.write(
"""
//
// Brian Cunnie's output for Nand to Tetris
//
""")

cmd_name = sys.argv[0].split('/')[-1]

if len(sys.argv) != 2:
    sys.exit(cmd_name + " error: pass me one arg, the name of the file to compile")

intermediate_filename = sys.argv[1]

try:
    intermediate_code = open(intermediate_filename, "r")
except:
    sys.exit(cmd_name + " error. I couldn't open " + intermediate_filename + " for reading!")

asm_filename = intermediate_filename.replace('.vm', '.asm')

if intermediate_filename == asm_filename:
    asm_filename += ".asm"

try:
    asm_file = open(asm_filename, "w")
except:
    sys.exit(cmd_name + " error. I couldn't open " + asm_filename + " for writing!")

banner()
for line in intermediate_code:
    x = parse(line)
    writecode(x)
<commit_msg>BasicTest passes — Hurrah!<commit_after>#!/usr/local/bin/python2.7
import sys

class Cmd_arithmetic:

    def __init__(self, code):
        self.code = code
        self.counter = -1

    def generate(self):
        self.counter += 1
        return self.code.replace('%d', str(self.counter))

class Cmd_push:
    def __init__(self):
        self.segment = 'constant'
        self.index = '0'
        self.indirect = 'M'

    def generate(self):
        self.code = cmd_push_asm
        if self.segment == 'local':
            self.segment = 'LCL'
        if self.segment == 'argument':
            self.segment = 'ARG'
        if self.segment == 'this':
            self.segment = 'THIS'
        if self.segment == 'that':
            self.segment = 'THAT'
        if self.segment == 'temp':
            self.indirect = 'A'
            self.segment = '5'
        if self.segment == 'constant':
            self.code = cmd_push_asm_constant
        return self.code.replace('%s', self.segment).replace('%d', self.index).replace('%i', self.indirect)

class Cmd_pop:
    def __init__(self, code):
        self.code = code
        self.segment = 'constant'
        self.index = '0'
        self.indirect = 'M'

    def generate(self):
        if self.segment == 'local':
            self.segment = 'LCL'
        if self.segment == 'argument':
            self.segment = 'ARG'
        if self.segment == 'this':
            self.segment = 'THIS'
        if self.segment == 'that':
            self.segment = 'THAT'
        if self.segment == 'temp':
            self.indirect = 'A'
            self.segment = '5'
        return self.code.replace('%s', self.segment).replace('%d', self.index).replace('%i', self.indirect)

cmd_add = Cmd_arithmetic(
    """    @SP
    M=M-1
    A=M
    D=M     // D is value at top of the stack
    @SP
    A=M-1   // Point to next highest on stack
    M=M+D   // Add that guy to D
"""
)

cmd_sub = Cmd_arithmetic(
    """    @SP
    M=M-1
    A=M
    D=M     // D is value at top of the stack
    @SP
    A=M-1   // Point to next highest on stack
    M=M-D   // Sub D from that guy
"""
)

cmd_neg = Cmd_arithmetic(
    """    @SP
    M=M-1
    A=M
    D=-M    // D is value at top of the stack
    M=D
    @SP
    M=M+1
"""
)

cmd_eq = Cmd_arithmetic(
    """    @SP
    M=M-1
    A=M
    D=M        // D is value at top of the stack
    @SP
    A=M-1      // Point to next highest on stack
    D=M-D      // Sub D from that guy
    @EQ_TRUE_%d
    D;JEQ
(EQ_FALSE_%d)   // NOT eq, set D to false (0)
    @0
    D=A
    @EQ_DONE_%d
    0;JMP
(EQ_TRUE_%d)    // eq, set D to true (-1)
    D=D-1      // D is 0, set D to true (-1)
(EQ_DONE_%d)
    @SP
    M=M-1
    A=M
    M=D
    @SP
    M=M+1
"""
)

cmd_lt = Cmd_arithmetic(
    """    @SP
    M=M-1
    A=M
    D=M        // D is value at top of the stack
    @SP
    A=M-1      // Point to next highest on stack
    D=M-D      // Sub D from that guy
    @LT_TRUE_%d
    D;JLT
(LT_FALSE_%d)   // NOT lt, set D to false (0)
    @0
    D=A
    @LT_DONE_%d
    0;JMP
(LT_TRUE_%d)    // lt, set D to true (-1)
    @0
    D=A-1      // set D to true (-1)
(LT_DONE_%d)
    @SP
    M=M-1
    A=M
    M=D
    @SP
    M=M+1
"""
)

cmd_gt = Cmd_arithmetic(
    """    @SP
    M=M-1
    A=M
    D=M        // D is value at top of the stack
    @SP
    A=M-1      // Point to next highest on stack
    D=M-D      // Sub D from that guy
    @GT_TRUE_%d
    D;JGT
(GT_FALSE_%d)   // NOT gt, set D to false (0)
    @0
    D=A
    @GT_DONE_%d
    0;JMP
(GT_TRUE_%d)    // gt, set D to true (-1)
    @0
    D=A-1      // set D to true (-1)
(GT_DONE_%d)
    @SP
    M=M-1
    A=M
    M=D
    @SP
    M=M+1
"""
)

cmd_and = Cmd_arithmetic(
    """    @SP
    M=M-1
    A=M
    D=M     // D is value at top of the stack
    @SP
    A=M-1   // Point to next highest on stack
    M=D&M   // Add D to that guy
"""
)

cmd_or = Cmd_arithmetic(
    """    @SP
    M=M-1
    A=M
    D=M     // D is value at top of the stack
    @SP
    A=M-1   // Point to next highest on stack
    M=D|M   // Add D to that guy
"""
)

cmd_not = Cmd_arithmetic(
    """    @SP
    M=M-1
    A=M
    D=!M    // D is value at top of the stack
    M=D
    @SP
    M=M+1
"""
)

cmd_push_asm = """    @%s     // segment
    D=%i
    @%d     // index
    A=D+A   // D holds the address to pull from
    D=M     // D holds the value we're gonna push onto SP
    @SP
    A=M
    M=D
    @SP     // increment SP
    M=M+1
"""

cmd_push_asm_constant = """    @%d
    D=A
    @SP
    A=M
    M=D
    @SP     // increment SP
    M=M+1
"""

cmd_pop = Cmd_pop(
    """    @%s     // segment
    D=%i
    @%d     // index
    D=D+A   // D holds the address to which to pop
    @SP
    A=M
    M=D     // top of the stack has the address to which to pop
    @SP
    A=M-1
    D=M     // D holds the value we're going to pop
    @SP
    A=M
    A=M     // climbing the indirections
    M=D     // Done!
    @SP     // decrement SP
    M=M-1
"""
)

def parse(line):
    # strip comments
    no_comments = line.split('#', 1)[0]
    no_comments = no_comments.split('//', 1)[0]
    tokens = no_comments.split()
    return tokens

def writecode(tokens):
    if not tokens:
        return
    asm_file.write('//')
    for token in tokens:
        asm_file.write(' ' + str(token))
    asm_file.write('\n')
    if tokens[0] == 'add':
        asm_file.write(cmd_add.generate())
    if tokens[0] == 'sub':
        asm_file.write(cmd_sub.generate())
    if tokens[0] == 'neg':
        asm_file.write(cmd_neg.generate())
    if tokens[0] == 'eq':
        asm_file.write(cmd_eq.generate())
    if tokens[0] == 'lt':
        asm_file.write(cmd_lt.generate())
    if tokens[0] == 'gt':
        asm_file.write(cmd_gt.generate())
    if tokens[0] == 'and':
        asm_file.write(cmd_and.generate())
    if tokens[0] == 'or':
        asm_file.write(cmd_or.generate())
    if tokens[0] == 'not':
        asm_file.write(cmd_not.generate())
    if tokens[0] == 'push':
        cmd_push = Cmd_push()
        cmd_push.segment = tokens[1]
        cmd_push.index = tokens[2]
        asm_file.write(cmd_push.generate())
    if tokens[0] == 'pop':
        cmd_pop.segment = tokens[1]
        cmd_pop.index = tokens[2]
        asm_file.write(cmd_pop.generate())

def banner():
    asm_file.write("//\n// Brian Cunnie's output for Nand to Tetris\n//\n")

cmd_name = sys.argv[0].split('/')[-1]

if len(sys.argv) != 2:
    sys.exit(cmd_name + " error: pass me one arg, the name of the file to compile")

intermediate_filename = sys.argv[1]

try:
    intermediate_code = open(intermediate_filename, "r")
except:
    sys.exit(cmd_name + " error. I couldn't open " + intermediate_filename + " for reading!")

asm_filename = intermediate_filename.replace('.vm', '.asm')

if intermediate_filename == asm_filename:
    asm_filename += ".asm"

try:
    asm_file = open(asm_filename, "w")
except:
    sys.exit(cmd_name + " error. I couldn't open " + asm_filename + " for writing!")

banner()
for line in intermediate_code:
    x = parse(line)
    writecode(x)
<|endoftext|>


<commit_before># coding=utf-8

import urllib.request
from urllib.parse import urlparse
import os
import time
import gzip
import zlib
from http.cookiejar import CookieJar

#========================================
#       网络获取
#========================================

class FetcherInfo:
    def __init__(self):
        self.ua = ('Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:52.0)'
                   ' Gecko/20100101 Firefox/52.0')
        self.referer = ''
        self.open_timeout = 60
        self.retry_count = 4
        self.retry_interval = 5

class Fetcher:
    '''web获取器'''

    def __init__(self, fetcher_info=None):
        if fetcher_info == None:
            fetcher_info = FetcherInfo()

        self.referer = fetcher_info.referer
        self.info = fetcher_info

        # ============
        #    opener
        # ============

        # no proxy
        proxy = urllib.request.ProxyHandler({})
        # cookie for redirect
        cj = urllib.request.HTTPCookieProcessor(CookieJar())
        # opener
        self.opener = urllib.request.build_opener(proxy, cj)

# 正在使用代理？
#         proxy_dict = urllib.request.getproxies()
#         if proxy_dict:
#             print('检测到正在使用代理服务器，这可能降低网络性能。')
#             print('代理服务器信息:')
#             for k, v in proxy_dict.items():
#                 print('协议: {0:<7} 代理服务器: {1}'.format(k, v))
#             print()

    def save_file(self, url, local_path):
        if os.path.exists(local_path):
            print(local_path, '文件已存在')
            return

        byte_data = fetch_url(url)
        with open(local_path, 'wb') as f:
            f.write(byte_data)

    def get_hostname(self, url):
        '''从url得到主机域名'''
        head = r'https://' if url.startswith(r'https://') else r'http://'
        parsed = urlparse(url)

        return head + parsed.netloc + r'/'

    def fetch_url(self, url):
        '''返回bytes'''
        # 重试次数
        retry = self.info.retry_count

##        # referer
##        if not self.referer or self.referer not in url:
##            self.referer = self.get_hostname(url)

        # request对象
        req = urllib.request.Request(url)
        req.add_header('Referer', self.referer)
        req.add_header('User-Agent', self.info.ua)

        # 重试用的循环
        while True:
            try:
                # r是HTTPResponse对象
                r = self.opener.open(req,
                                timeout=self.info.open_timeout
                                )
                ret_data = r.read()

                # decompress
                contentenc = r.getheader('Content-Encoding', '')
                if not contentenc:
                    return ret_data
                else:
                    contentenc = contentenc.lower()
                    if 'gzip' in contentenc:
                        ret_data = gzip.decompress(ret_data)
                    elif 'deflate' in contentenc:
                        try:
                            # first try: zlib
                            ret_data = zlib.decompress(ret_data, 15)
                        except:
                            # second try: raw deflate
                            ret_data = zlib.decompress(ret_data, -15)
                    return ret_data

            except Exception as e:
                print('! 下载时出现异常')
                print('! 可能是网址错误、服务器抽风、下载超时、解压缩失败等等')
                print('! 详细异常信息:', type(e), '\n', e, '\n')

            retry -= 1
            if retry <= 0:
                break

            print('{0}秒后重试，剩余重试次数：{1}次\n'.
                  format(self.info.retry_interval, retry)
                  )
            time.sleep(self.info.retry_interval)

        print('重试次数用完，下载失败')
        return b''
<commit_msg>safe file<commit_after># coding=utf-8

import urllib.request
from urllib.parse import urlparse
import os
import time
import gzip
import zlib
from http.cookiejar import CookieJar

#========================================
#       网络获取
#========================================

class FetcherInfo:
    def __init__(self):
        self.ua = ('Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:52.0)'
                   ' Gecko/20100101 Firefox/52.0')
        self.referer = ''
        self.open_timeout = 60
        self.retry_count = 4
        self.retry_interval = 5

class Fetcher:
    '''web获取器'''

    def __init__(self, fetcher_info=None):
        if fetcher_info == None:
            fetcher_info = FetcherInfo()

        self.referer = fetcher_info.referer
        self.info = fetcher_info

        # ============
        #    opener
        # ============

        # no proxy
        proxy = urllib.request.ProxyHandler({})
        # cookie for redirect
        cj = urllib.request.HTTPCookieProcessor(CookieJar())
        # opener
        self.opener = urllib.request.build_opener(proxy, cj)

# 正在使用代理？
#         proxy_dict = urllib.request.getproxies()
#         if proxy_dict:
#             print('检测到正在使用代理服务器，这可能降低网络性能。')
#             print('代理服务器信息:')
#             for k, v in proxy_dict.items():
#                 print('协议: {0:<7} 代理服务器: {1}'.format(k, v))
#             print()

    def save_file(self, url, local_path):
        if os.path.exists(local_path):
            print(local_path, '文件已存在')
            return

        byte_data = self.fetch_url(url)
        with open(local_path, 'wb') as f:
            f.write(byte_data)

    def get_hostname(self, url):
        '''从url得到主机域名'''
        head = r'https://' if url.startswith(r'https://') else r'http://'
        parsed = urlparse(url)

        return head + parsed.netloc + r'/'

    def fetch_url(self, url):
        '''返回bytes'''
        # 重试次数
        retry = self.info.retry_count

##        # referer
##        if not self.referer or self.referer not in url:
##            self.referer = self.get_hostname(url)

        # request对象
        req = urllib.request.Request(url)
        req.add_header('Referer', self.referer)
        req.add_header('User-Agent', self.info.ua)

        # 重试用的循环
        while True:
            try:
                # r是HTTPResponse对象
                r = self.opener.open(req,
                                timeout=self.info.open_timeout
                                )
                ret_data = r.read()

                # decompress
                contentenc = r.getheader('Content-Encoding', '')
                if not contentenc:
                    return ret_data
                else:
                    contentenc = contentenc.lower()
                    if 'gzip' in contentenc:
                        ret_data = gzip.decompress(ret_data)
                    elif 'deflate' in contentenc:
                        try:
                            # first try: zlib
                            ret_data = zlib.decompress(ret_data, 15)
                        except:
                            # second try: raw deflate
                            ret_data = zlib.decompress(ret_data, -15)
                    return ret_data

            except Exception as e:
                print('! 下载时出现异常')
                print('! 可能是网址错误、服务器抽风、下载超时、解压缩失败等等')
                print('! 详细异常信息:', type(e), '\n', e, '\n')

            retry -= 1
            if retry <= 0:
                break

            print('{0}秒后重试，剩余重试次数：{1}次\n'.
                  format(self.info.retry_interval, retry)
                  )
            time.sleep(self.info.retry_interval)

        print('重试次数用完，下载失败')
        return b''
<|endoftext|>


<commit_before># VSYNC GPIO output example.
#
# This example shows how to toggle a pin on VSYNC interrupt.

import sensor, image, time
from pyb import Pin

sensor.reset()                      # Reset and initialize the sensor.
sensor.set_pixformat(sensor.GRAYSCALE) # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QVGA)   # Set frame size to QVGA (320x240)

# This pin will be toggled on/off on VSYNC rising and falling edges.
led_pin = Pin('LED_BLUE', Pin.OUT_PP, Pin.PULL_NONE)
sensor.set_vsync_callback(lambda state, led=led_pin: led_pin.value(state))

clock = time.clock()                # Create a clock object to track the FPS.

while(True):
    clock.tick()                    # Update the FPS clock.
    img = sensor.snapshot()         # Take a picture and return the image.
    print(clock.fps())              # Note: OpenMV Cam runs about half as fast when connected
                                    # to the IDE. The FPS should increase once disconnected.
<commit_msg>scripts/examples: Fix VSYNC GPIO LED pin name.<commit_after># VSYNC GPIO output example.
#
# This example shows how to toggle a pin on VSYNC interrupt.

import sensor, image, time
from pyb import Pin

sensor.reset()                      # Reset and initialize the sensor.
sensor.set_pixformat(sensor.GRAYSCALE) # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QVGA)   # Set frame size to QVGA (320x240)

# This pin will be toggled on/off on VSYNC rising and falling edges.
led_pin = Pin('LEDB', Pin.OUT_PP, Pin.PULL_NONE)
sensor.set_vsync_callback(lambda state, led=led_pin: led_pin.value(state))

clock = time.clock()                # Create a clock object to track the FPS.

while(True):
    clock.tick()                    # Update the FPS clock.
    img = sensor.snapshot()         # Take a picture and return the image.
    print(clock.fps())              # Note: OpenMV Cam runs about half as fast when connected
                                    # to the IDE. The FPS should increase once disconnected.
<|endoftext|>


<commit_before>
from rest_framework import generics, permissions
from rest_framework.reverse import reverse

from collectionjson import services

from .models import Plugin, PluginParameter, PluginInstance, StringParameter
from .models import FloatParameter, IntParameter, BoolParameter

from .serializers import PARAMETER_SERIALIZERS
from .serializers import PluginSerializer,  PluginParameterSerializer
from .serializers import PluginInstanceSerializer
from .permissions import IsChrisOrReadOnly
from .services.manager import PluginManager

class PluginList(generics.ListAPIView):
    """
    A view for the collection of plugins.
    """
    serializer_class = PluginSerializer
    queryset = Plugin.objects.all()
    permission_classes = (permissions.IsAuthenticated, IsChrisOrReadOnly,)

    def list(self, request, *args, **kwargs):
        """
        Overriden to append document-level link relations and a collection+json
        template to the response.
        """
        response = super(PluginList, self).list(request, *args, **kwargs)
        links = {'feeds': reverse('feed-list', request=request)}
        return services.append_collection_links(request, response, links)

class PluginDetail(generics.RetrieveAPIView):
    """
    A plugin view.
    """
    serializer_class = PluginSerializer
    queryset = Plugin.objects.all()
    permission_classes = (permissions.IsAuthenticated, IsChrisOrReadOnly,)

class PluginParameterList(generics.ListAPIView):
    """
    A view for the collection of plugin parameters.
    """
    serializer_class = PluginParameterSerializer
    queryset = Plugin.objects.all()
    permission_classes = (permissions.IsAuthenticated, IsChrisOrReadOnly,)

    def list(self, request, *args, **kwargs):
        """
        Overriden to return the list of parameters for the queried plugin.
        A document-level link relation is also added to the response.
        """
        queryset = self.get_plugin_parameters_queryset()
        response = services.get_list_response(self, queryset)
        plugin = self.get_object()
        links = {'plugin': reverse('plugin-detail', request=request,
                                   kwargs={"pk": plugin.id})}
        return services.append_collection_links(request, response, links)

    def get_plugin_parameters_queryset(self):
        """
        Custom method to get the actual plugin parameters' queryset.
        """
        plugin = self.get_object()
        return self.filter_queryset(plugin.parameters.all())

class PluginParameterDetail(generics.RetrieveAPIView):
    """
    A plugin parameter view.
    """
    serializer_class = PluginParameterSerializer
    queryset = PluginParameter.objects.all()
    permission_classes = (permissions.IsAuthenticated, IsChrisOrReadOnly,)

class PluginInstanceList(generics.ListCreateAPIView):
    """
    A view for the collection of plugin instances.
    """
    serializer_class = PluginInstanceSerializer
    queryset = Plugin.objects.all()
    permission_classes = (permissions.IsAuthenticated,)

    def perform_create(self, serializer):
        """
        Overriden to associate an owner, a plugin and a previous plugin instance with
        the newly created plugin instance before first saving to the DB. All the plugin
        instace's parameters in the resquest are also properly saved to the DB. Finally
        the plugin's app is run with the provided plugin instance's parameters.
        """
        plugin = self.get_object()
        request_data = serializer.context['request'].data
        # get previous plugin instance
        previous_id = ""
        if 'previous' in request_data:
            previous_id = request_data['previous']
        previous = serializer.validate_previous(previous_id, plugin)
        # create plugin instance with corresponding owner, plugin and previous instances
        plugin_inst = serializer.save(owner=self.request.user, plugin=plugin,
                                      previous=previous)
        # collect parameters from the request and validate and save them to the DB
        parameters = plugin.parameters.all()
        parameters_dict = {}
        for parameter in parameters:
            if parameter.name in request_data:
                data = {'value': request_data[parameter.name]}
                parameter_serializer = PARAMETER_SERIALIZERS[parameter.type](data=data)
                parameter_serializer.is_valid(raise_exception=True)
                parameter_serializer.save(plugin_inst=plugin_inst, plugin_param=parameter)
                parameters_dict[parameter.name] = request_data[parameter.name]
        # run the plugin's app
        pl_manager = PluginManager()
        pl_manager.run_plugin_app(plugin_inst, parameters_dict)

    def list(self, request, *args, **kwargs):
        """
        Overriden to return the list of instances for the queried plugin.
        A collection+json template is also added to the response.
        """
        queryset = self.get_plugin_instances_queryset()
        response = services.get_list_response(self, queryset)
        plugin = self.get_object()
        links = {'plugin': reverse('plugin-detail', request=request,
                                   kwargs={"pk": plugin.id})}
        response = services.append_collection_links(request, response, links)
        param_names = self.get_plugin_parameter_names()
        template_data = {'previous': ""}
        for name in param_names:
            template_data[name] = ""
        return services.append_collection_template(response, template_data)

    def get_plugin_instances_queryset(self):
        """
        Custom method to get the actual plugin instances' queryset.
        """
        plugin = self.get_object()
        return self.filter_queryset(plugin.instances.all())

    def get_plugin_parameter_names(self):
        """
        Custom method to get the list of plugin parameter names.
        """
        plugin = self.get_object()
        params = plugin.parameters.all()
        return [param.name for param in params]

class PluginInstanceDetail(generics.RetrieveAPIView):
    """
    A plugin instance view.
    """
    serializer_class = PluginInstanceSerializer
    queryset = PluginInstance.objects.all()
    permission_classes = (permissions.IsAuthenticated, IsChrisOrReadOnly,)

class StringParameterDetail(generics.RetrieveAPIView):
    """
    A string parameter view.
    """
    serializer_class = PARAMETER_SERIALIZERS['string']
    queryset = StringParameter.objects.all()
    permission_classes = (permissions.IsAuthenticated, IsChrisOrReadOnly,)

class IntParameterDetail(generics.RetrieveAPIView):
    """
    An integer parameter view.
    """
    serializer_class = PARAMETER_SERIALIZERS['integer']
    queryset = IntParameter.objects.all()
    permission_classes = (permissions.IsAuthenticated, IsChrisOrReadOnly,)

class FloatParameterDetail(generics.RetrieveAPIView):
    """
    A float parameter view.
    """
    serializer_class = PARAMETER_SERIALIZERS['float']
    queryset = IntParameter.objects.all()
    permission_classes = (permissions.IsAuthenticated, IsChrisOrReadOnly,)

class BoolParameterDetail(generics.RetrieveAPIView):
    """
    A boolean parameter view.
    """
    serializer_class = PARAMETER_SERIALIZERS['boolean']
    queryset = BoolParameter.objects.all()
    permission_classes = (permissions.IsAuthenticated, IsChrisOrReadOnly,)
<commit_msg>WIP, comment for build.<commit_after>
from rest_framework import generics, permissions
from rest_framework.reverse import reverse

from collectionjson import services

from .models import Plugin, PluginParameter, PluginInstance, StringParameter
from .models import FloatParameter, IntParameter, BoolParameter

from .serializers import PARAMETER_SERIALIZERS
from .serializers import PluginSerializer,  PluginParameterSerializer
from .serializers import PluginInstanceSerializer
from .permissions import IsChrisOrReadOnly
from .services.manager import PluginManager

from .services import charm

import  socket
# This will need to be better addressed!
class pman_settings():
    HOST    =  [l for l in ([ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.")][:1], [[(s.connect(('8.8.8.8', 53)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]]) if l][0][0]
    PORT    = '5010'

class PluginList(generics.ListAPIView):
    """
    A view for the collection of plugins.
    """
    serializer_class = PluginSerializer
    queryset = Plugin.objects.all()
    permission_classes = (permissions.IsAuthenticated, IsChrisOrReadOnly,)

    def list(self, request, *args, **kwargs):
        """
        Overriden to append document-level link relations and a collection+json
        template to the response.
        """
        response = super(PluginList, self).list(request, *args, **kwargs)
        links = {'feeds': reverse('feed-list', request=request)}
        return services.append_collection_links(request, response, links)

class PluginDetail(generics.RetrieveAPIView):
    """
    A plugin view.
    """
    serializer_class = PluginSerializer
    queryset = Plugin.objects.all()
    permission_classes = (permissions.IsAuthenticated, IsChrisOrReadOnly,)

class PluginParameterList(generics.ListAPIView):
    """
    A view for the collection of plugin parameters.
    """
    serializer_class = PluginParameterSerializer
    queryset = Plugin.objects.all()
    permission_classes = (permissions.IsAuthenticated, IsChrisOrReadOnly,)

    def list(self, request, *args, **kwargs):
        """
        Overriden to return the list of parameters for the queried plugin.
        A document-level link relation is also added to the response.
        """
        queryset = self.get_plugin_parameters_queryset()
        response = services.get_list_response(self, queryset)
        plugin = self.get_object()
        links = {'plugin': reverse('plugin-detail', request=request,
                                   kwargs={"pk": plugin.id})}
        return services.append_collection_links(request, response, links)

    def get_plugin_parameters_queryset(self):
        """
        Custom method to get the actual plugin parameters' queryset.
        """
        plugin = self.get_object()
        return self.filter_queryset(plugin.parameters.all())

class PluginParameterDetail(generics.RetrieveAPIView):
    """
    A plugin parameter view.
    """
    serializer_class = PluginParameterSerializer
    queryset = PluginParameter.objects.all()
    permission_classes = (permissions.IsAuthenticated, IsChrisOrReadOnly,)

class PluginInstanceList(generics.ListCreateAPIView):
    """
    A view for the collection of plugin instances.
    """
    serializer_class = PluginInstanceSerializer
    queryset = Plugin.objects.all()
    permission_classes = (permissions.IsAuthenticated,)

    def perform_create(self, serializer):
        """
        Overriden to associate an owner, a plugin and a previous plugin instance with
        the newly created plugin instance before first saving to the DB. All the plugin
        instace's parameters in the resquest are also properly saved to the DB. Finally
        the plugin's app is run with the provided plugin instance's parameters.
        """
        plugin = self.get_object()
        request_data = serializer.context['request'].data
        # get previous plugin instance
        previous_id = ""
        if 'previous' in request_data:
            previous_id = request_data['previous']
        previous = serializer.validate_previous(previous_id, plugin)
        # create plugin instance with corresponding owner, plugin and previous instances
        plugin_inst = serializer.save(owner=self.request.user, plugin=plugin,
                                      previous=previous)
        # collect parameters from the request and validate and save them to the DB
        parameters = plugin.parameters.all()
        parameters_dict = {}
        for parameter in parameters:
            if parameter.name in request_data:
                data = {'value': request_data[parameter.name]}
                parameter_serializer = PARAMETER_SERIALIZERS[parameter.type](data=data)
                parameter_serializer.is_valid(raise_exception=True)
                parameter_serializer.save(plugin_inst=plugin_inst, plugin_param=parameter)
                parameters_dict[parameter.name] = request_data[parameter.name]
        # run the plugin's app
        pl_manager = PluginManager()
        pl_manager.run_plugin_app(plugin_inst, parameters_dict)

    def list(self, request, *args, **kwargs):
        """
        Overriden to return the list of instances for the queried plugin.
        A collection+json template is also added to the response.
        """
        queryset = self.get_plugin_instances_queryset()
        response = services.get_list_response(self, queryset)
        plugin = self.get_object()
        links = {'plugin': reverse('plugin-detail', request=request,
                                   kwargs={"pk": plugin.id})}
        response = services.append_collection_links(request, response, links)
        param_names = self.get_plugin_parameter_names()
        template_data = {'previous': ""}
        for name in param_names:
            template_data[name] = ""
        return services.append_collection_template(response, template_data)

    def get_plugin_instances_queryset(self):
        """
        Custom method to get the actual plugin instances' queryset.
        """
        plugin = self.get_object()
        return self.filter_queryset(plugin.instances.all())

    def get_plugin_parameter_names(self):
        """
        Custom method to get the list of plugin parameter names.
        """
        plugin = self.get_object()
        params = plugin.parameters.all()
        return [param.name for param in params]

class PluginInstanceDetail(generics.RetrieveAPIView):
    """
    A plugin instance view.
    """
    serializer_class = PluginInstanceSerializer
    queryset = PluginInstance.objects.all()
    permission_classes = (permissions.IsAuthenticated, IsChrisOrReadOnly,)

    def retrieve(self, request, *args, **kwargs):
        """
        Overwritten method -- connect to pman to determine job status.

        :param request:
        :param args:
        :param kwargs:
        :return:
        """
        instance = self.get_object()

        instance.status = 'whatever'
        instance.save()

        # chris2pman   = charm.Charm(
        #     plugin_inst = instance
        # )
        #
        # if status is finished and not yet_register_outputfiles():
        #     register_outputfile()
        #     yet_register_outputfiles(true)

        serializer = self.get_serializer(instance)
        response = super(PluginInstanceDetail, self).retrieve(request, *args, **kwargs)

        return  response

class StringParameterDetail(generics.RetrieveAPIView):
    """
    A string parameter view.
    """
    serializer_class = PARAMETER_SERIALIZERS['string']
    queryset = StringParameter.objects.all()
    permission_classes = (permissions.IsAuthenticated, IsChrisOrReadOnly,)

class IntParameterDetail(generics.RetrieveAPIView):
    """
    An integer parameter view.
    """
    serializer_class = PARAMETER_SERIALIZERS['integer']
    queryset = IntParameter.objects.all()
    permission_classes = (permissions.IsAuthenticated, IsChrisOrReadOnly,)

class FloatParameterDetail(generics.RetrieveAPIView):
    """
    A float parameter view.
    """
    serializer_class = PARAMETER_SERIALIZERS['float']
    queryset = IntParameter.objects.all()
    permission_classes = (permissions.IsAuthenticated, IsChrisOrReadOnly,)

class BoolParameterDetail(generics.RetrieveAPIView):
    """
    A boolean parameter view.
    """
    serializer_class = PARAMETER_SERIALIZERS['boolean']
    queryset = BoolParameter.objects.all()
    permission_classes = (permissions.IsAuthenticated, IsChrisOrReadOnly,)
<|endoftext|>


<commit_before>from __future__ import unicode_literals

from django.test import TestCase

from .models import AutonomousSystem

class AutonomousSystemTestCase(TestCase):
    def test_does_exist(self):
        asn = 29467

        # AS should not exist
        autonomous_system = AutonomousSystem.does_exist(asn)
        self.assertEqual(None, autonomous_system)

        # Create the AS
        values = {
            'asn': asn,
            'name': 'LuxNetwork S.A.',
        }
        new_as = AutonomousSystem(**values)
        new_as.save()

        # AS must exist
        autonomous_system = AutonomousSystem.does_exist(asn)
        self.assertEqual(asn, new_as.asn)

    def test_create_from_peeringdb(self):
        asn = 29467

        # Create the AS
        autonomous_system1 = AutonomousSystem.create_from_peeringdb(29467)
        self.assertEqual(29467, autonomous_system1.asn)

        # Must not rise error, just return the AS
        autonomous_system2 = AutonomousSystem.create_from_peeringdb(29467)
        self.assertEqual(29467, autonomous_system2.asn)
<commit_msg>Improve tests for AutonomousSystem.<commit_after>from __future__ import unicode_literals

from django.test import TestCase

from .models import AutonomousSystem

class AutonomousSystemTestCase(TestCase):
    def test_does_exist(self):
        asn = 29467

        # AS should not exist
        autonomous_system = AutonomousSystem.does_exist(asn)
        self.assertEqual(None, autonomous_system)

        # Create the AS
        new_as = AutonomousSystem.objects.create(asn=asn,
                                                 name='LuxNetwork S.A.')

        # AS must exist
        autonomous_system = AutonomousSystem.does_exist(asn)
        self.assertEqual(asn, new_as.asn)

    def test_create_from_peeringdb(self):
        asn = 29467

        # Must not exist at first
        self.assertEqual(None, AutonomousSystem.does_exist(asn))

        # Create the AS
        autonomous_system1 = AutonomousSystem.create_from_peeringdb(asn)
        self.assertEqual(asn, autonomous_system1.asn)

        # Must exist now
        self.assertEqual(asn, AutonomousSystem.does_exist(asn).asn)

        # Must not rise error, just return the AS
        autonomous_system2 = AutonomousSystem.create_from_peeringdb(asn)
        self.assertEqual(asn, autonomous_system2.asn)

        # Must exist now also
        self.assertEqual(asn, AutonomousSystem.does_exist(asn).asn)
<|endoftext|>


<commit_before>#!/usr/bin/env python
'''
from guppy import hpy
h = hpy()
h.setref()
'''

import pentai.base.rules as r_m
import pentai.base.game as g_m

import time

from pentai.ai.ai_player import *
#from pentai.db.evaluator import *
from pentai.db.ai_factory import *
from pentai.db.openings_book import *
from pentai.db.games_mgr import *
import pentai.db.zodb_dict as z_m

import pentai.ai.ab_game as abg_m

class TwoTimer:
    def __init__(self):
        self.totals = [0.0, 0.0]
        self.current = 0

    def __enter__(self):
        self.start = time.clock()
        return self

    def __exit__(self, *args):
        self.end = time.clock()
        self.totals[self.current] += self.end - self.start
        self.current = 1 - self.current

    def __repr__(self):
        tot = self.totals
        return "B: %.2fs, W: %.2fs, B/W: %.2f" % (tot[0], tot[1], tot[0]/tot[1])

class MatchResults():
    def __init__(self):
        self.results = []
        self.bw_total = [0, 0, 0]
        self.dc_total = {"Defender":0, "Contender":0}
        self.total_ratio = 0.
        self.games_played = 0

    def __repr__(self):
        return "\n".join(self.results) + "\nB/W:" + str(self.bw_total) \
                + " won: " + str(self.dc_total) \
                + " C/D avg. time: " + str(self.total_ratio / self.games_played)

    def add(self, result):
        res_str, winner_colour, winner_name, ratio = result
        self.results.append(res_str)
        self.bw_total[winner_colour] += 1
        self.dc_total[str(winner_name)] += 1
        self.total_ratio += ratio
        self.games_played += 1

class Match():
    def __init__(self):
        self.genome1 = AIGenome("Defender")
        self.genome2 = AIGenome("Contender")
        # We're not doing player lookups, so we don't need the players_mgr
        self.games_mgr = GamesMgr()
        self.openings_book = OpeningsBook()

    def set_up(self, game_length):
        aif = AIFactory()
        self.genome1.max_depth = game_length
        self.genome2.max_depth = game_length
        self.p1 = aif.create_player(self.genome1)
        self.p2 = aif.create_player(self.genome2)

    def play_one_game(self, board_size, rules_type, p1, p2):
        r = r_m.Rules(board_size, rules_type)
        self.game = self.games_mgr.create_game(r, p1, p2)
        #self.evaluator = Evaluator(self.game.current_state)

        tt = TwoTimer()

        while not self.game.finished():
            p = self.game.get_current_player()
            with tt:
                turn, prev_move, m = p.do_the_search()
                self.game.make_move(m)
                #print self.evaluator.utility()

        winner_name = self.game.winner_name()
        winner = self.game.get_won_by()

        if self.openings_book:
            self.openings_book.add_game(self.game, winner)

        if p1.get_name() == "Contender":
            ratio = tt.totals[0] / tt.totals[1]
        else:
            ratio = tt.totals[1] / tt.totals[0]

        print "Game was won by: %s %s" % (["?", "B", "W"][winner], winner_name)
        print tt

        return "%s vs. %s: %s (%sx%s %s) %s" % (p1.get_name(), p2.get_name(), winner_name,
                board_size, board_size, p1.max_depth, tt), winner, winner_name, ratio

    def play_some_games(self):

        self.genome1.use_openings_book = False
        self.genome2.use_openings_book = False
        #self.genome2.use_net_captures = False

        #self.genome2.length_factor = 35
        #self.genome2.take_score_base = 70
        #self.genome2.capture_score_base = 350 # Try this again for high depth
        #self.genome2.threat_score_base = 25 # Wins more for shallower depth
        #self.genome1.enclosed_four_base = 300
        #self.genome2.enclosed_four_base = 500
        #self.genome1.vision = 0.98
        #self.genome2.vision = 0.98

        #self.genome1.filter_num = 4
        #self.genome2.filter_num = 5
        #self.genome2.narrowing = 3
        #self.genome2.max_depth += 2 # Setting max_depth here doesn't work
        #self.genome1.mmpdl = 15
        #self.genome1.chokes = []
        self.genome2.mmpdl = 15
        self.genome2.chokes = []
        self.genome2.bl_cutoff = True
        #self.genome2.chokes = [(4,5)]
        #self.genome2.chokes = [(4,3)]
        #self.genome2.chokes = [(2,2)]
        #self.genome1.max_depth_boost = 2
        #self.genome2.max_depth_boost = 2
        #self.genome2.captures_scale = [1, 1, 1, 1, 2, 4]
        #self.genome2.captures_scale = [0, 0, 0, 0, 0, 0]
        #self.genome2.length_scale = [0, 0, 0, 0, 0, 0]
        #self.genome2.move_factor = 10000000

        #self.genome1.calc_mode = 3

        #self.genome2.calc_mode = 2
        #self.genome2.use_net_captures = False
        #self.genome2.scale_pob = True
        #self.genome2.move_factor = 50
        #self.genome2.move_factor = 45
        #self.genome2.move_factor = 5
        #self.genome2.force_depth = 4 FAIL ;)
        #self.genome2.misjudgement = 8

        results = MatchResults()
        #for game_length in range(2,7):
        #for game_length in range(2,5):
        for game_length in range(2,3):
            #for board_size in [13]:
            for board_size in [13, 19]:
                for first_player in [0, 1]:
                    for rules_type in ['s', 't']:
                        self.set_up(game_length)
                        players = [self.p1, self.p2]
                        second_player = 1 - first_player
                        res = self.play_one_game(board_size, rules_type,
                                                 players[first_player],
                                                 players[second_player])
                        #hits = [players[i].ab_game.transposition_hits for i in [0,1]]
                        #print "Hits: %s" % hits
                        results.add(res)

        print results
        cs = abg_m.choice_stats
        print cs
        cs.set_threshold(0.98)
        print cs.f_t_disp()
        st()

#cs.filtered_bad_relative_to_best_n_worst(depth=2)

import sys

def memory_usage_resource():
    import resource
    rusage_denom = 1024.
    if sys.platform == 'darwin':
        # ... it seems that in OSX the output is different units ...
        rusage_denom = rusage_denom * rusage_denom
    mem = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / rusage_denom
    return mem

import random
import pstats, cProfile

import gc

if __name__ == "__main__":
    z_m.set_db("db.fs")

    '''
    random.seed()
    # the code you want to memory-profile
    '''

    '''
    #while True:
    m = Match()
    m.play_some_games()
    m = None
    gc.collect()

    mem = memory_usage_resource()
    print mem
    '''
    '''
    heap_data = h.heap()
    print heap_data
    print heap_data.more
    st()
    '''
    m = Match()
    cProfile.runctx("m.play_some_games()", globals(), locals(), "Profile.prof")

    s = pstats.Stats("Profile.prof")
    #s.strip_dirs().sort_stats("cumulative").print_stats(20) # or "time"
    s.strip_dirs().sort_stats("time").print_stats(20)
<commit_msg>Testing some long thin branches at ends<commit_after>#!/usr/bin/env python
'''
from guppy import hpy
h = hpy()
h.setref()
'''

import pentai.base.rules as r_m
import pentai.base.game as g_m

import time

from pentai.ai.ai_player import *
#from pentai.db.evaluator import *
from pentai.db.ai_factory import *
from pentai.db.openings_book import *
from pentai.db.games_mgr import *
import pentai.db.zodb_dict as z_m

import pentai.ai.ab_game as abg_m

class TwoTimer:
    def __init__(self):
        self.totals = [0.0, 0.0]
        self.current = 0

    def __enter__(self):
        self.start = time.clock()
        return self

    def __exit__(self, *args):
        self.end = time.clock()
        self.totals[self.current] += self.end - self.start
        self.current = 1 - self.current

    def __repr__(self):
        tot = self.totals
        return "B: %.2fs, W: %.2fs, B/W: %.2f" % (tot[0], tot[1], tot[0]/tot[1])

class MatchResults():
    def __init__(self):
        self.results = []
        self.bw_total = [0, 0, 0]
        self.dc_total = {"Defender":0, "Contender":0}
        self.total_ratio = 0.
        self.games_played = 0

    def __repr__(self):
        return "\n".join(self.results) + "\nB/W:" + str(self.bw_total) \
                + " won: " + str(self.dc_total) \
                + " C/D avg. time: " + str(self.total_ratio / self.games_played)

    def add(self, result):
        res_str, winner_colour, winner_name, ratio = result
        self.results.append(res_str)
        self.bw_total[winner_colour] += 1
        self.dc_total[str(winner_name)] += 1
        self.total_ratio += ratio
        self.games_played += 1

class Match():
    def __init__(self):
        self.genome1 = AIGenome("Defender")
        self.genome2 = AIGenome("Contender")
        # We're not doing player lookups, so we don't need the players_mgr
        self.games_mgr = GamesMgr()
        self.openings_book = OpeningsBook()

    def set_up(self, game_length, p2_depth_boost=0):
        aif = AIFactory()
        self.genome1.max_depth = game_length
        self.genome2.max_depth = game_length + p2_depth_boost
        self.p1 = aif.create_player(self.genome1)
        self.p2 = aif.create_player(self.genome2)

    def play_one_game(self, board_size, rules_type, p1, p2):
        r = r_m.Rules(board_size, rules_type)
        self.game = self.games_mgr.create_game(r, p1, p2)
        #self.evaluator = Evaluator(self.game.current_state)

        tt = TwoTimer()

        while not self.game.finished():
            p = self.game.get_current_player()
            with tt:
                turn, prev_move, m = p.do_the_search()
                self.game.make_move(m)
                #print self.evaluator.utility()

        winner_name = self.game.winner_name()
        winner = self.game.get_won_by()

        if self.openings_book:
            self.openings_book.add_game(self.game, winner)

        if p1.get_name() == "Contender":
            ratio = tt.totals[0] / tt.totals[1]
        else:
            ratio = tt.totals[1] / tt.totals[0]

        print "Game was won by: %s %s" % (["?", "B", "W"][winner], winner_name)
        print tt

        return "%s vs. %s: %s (%sx%s %s) %s" % (p1.get_name(), p2.get_name(), winner_name,
                board_size, board_size, p1.max_depth, tt), winner, winner_name, ratio

    def play_some_games(self):

        self.genome1.use_openings_book = False
        self.genome2.use_openings_book = False
        #self.genome2.use_net_captures = False

        #self.genome2.length_factor = 35
        #self.genome2.take_score_base = 70
        #self.genome2.capture_score_base = 350 # Try this again for high depth
        #self.genome2.threat_score_base = 25 # Wins more for shallower depth
        #self.genome1.enclosed_four_base = 300
        #self.genome2.enclosed_four_base = 500
        #self.genome1.vision = 0.98
        #self.genome2.vision = 0.98

        #self.genome1.filter_num = 4
        #self.genome2.filter_num = 5
        #self.genome2.narrowing = 3
        #self.genome2.max_depth += 2 # Setting max_depth here doesn't work
        #self.genome1.mmpdl = 15
        #self.genome1.chokes = []
        #self.genome2.mmpdl = 15
        #self.genome2.chokes = [(4,2),(6,1)]
        self.genome2.chokes = [(3,1)]
        #self.genome2.bl_cutoff = True
        #self.genome2.chokes = [(4,5)]
        #self.genome2.chokes = [(4,3)]
        #self.genome2.chokes = [(2,2)]
        #self.genome1.max_depth_boost = 2
        #self.genome2.max_depth_boost = 2
        #self.genome2.captures_scale = [1, 1, 1, 1, 2, 4]
        #self.genome2.captures_scale = [0, 0, 0, 0, 0, 0]
        #self.genome2.length_scale = [0, 0, 0, 0, 0, 0]
        #self.genome2.move_factor = 10000000

        #self.genome1.calc_mode = 3

        #self.genome2.calc_mode = 2
        #self.genome2.use_net_captures = False
        #self.genome2.scale_pob = True
        #self.genome2.move_factor = 50
        #self.genome2.move_factor = 45
        #self.genome2.move_factor = 5
        #self.genome2.force_depth = 4 FAIL ;)
        #self.genome2.misjudgement = 8

        results = MatchResults()
        for game_length in range(6,7):
        #for game_length in range(2,5):
        #for game_length in range(2,3):
        #for game_length in range(3,4):
            #for board_size in [13]:
            for board_size in [13, 19]:
                for first_player in [0, 1]:
                    for rules_type in ['s', 't']:
                        self.set_up(game_length) #, 3)
                        players = [self.p1, self.p2]
                        second_player = 1 - first_player
                        res = self.play_one_game(board_size, rules_type,
                                                 players[first_player],
                                                 players[second_player])
                        #hits = [players[i].ab_game.transposition_hits for i in [0,1]]
                        #print "Hits: %s" % hits
                        results.add(res)

        print results
        cs = abg_m.choice_stats
        print cs
        cs.set_threshold(0.98)
        print cs.f_t_disp()
        st()

#cs.filtered_bad_relative_to_best_n_worst(depth=2)

import sys

def memory_usage_resource():
    import resource
    rusage_denom = 1024.
    if sys.platform == 'darwin':
        # ... it seems that in OSX the output is different units ...
        rusage_denom = rusage_denom * rusage_denom
    mem = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / rusage_denom
    return mem

import random
import pstats, cProfile

import gc

if __name__ == "__main__":
    z_m.set_db("db.fs")

    '''
    random.seed()
    # the code you want to memory-profile
    '''

    '''
    #while True:
    m = Match()
    m.play_some_games()
    m = None
    gc.collect()

    mem = memory_usage_resource()
    print mem
    '''
    '''
    heap_data = h.heap()
    print heap_data
    print heap_data.more
    st()
    '''
    m = Match()
    cProfile.runctx("m.play_some_games()", globals(), locals(), "Profile.prof")

    s = pstats.Stats("Profile.prof")
    #s.strip_dirs().sort_stats("cumulative").print_stats(20) # or "time"
    s.strip_dirs().sort_stats("time").print_stats(20)
<|endoftext|>


<commit_before>import tensorflow as tf

from tensor_train_base import TensorTrainBase
from tensor_train_batch import TensorTrainBatch
import ops

def concat_along_batch_dim(tt_list):
  """Concat all TensorTrainBatch objects along batch dimension.

  Args:
    tt_list: a list of TensorTrainBatch objects.

  Returns:
    TensorTrainBatch
  """
  ndims = tt_list[0].ndims()
  if isinstance(tt_list, TensorTrainBase):
    # Not a list but just one element, nothing to concat.
    return tt_list

  for core_idx in range(ndims):
    if not isinstance(tt_list[core_idx], TensorTrainBatch):
      raise ValueError('All objects in the list should be TTBatch objects, got '
                       '%s' % tt_list[core_idx])
  for core_idx in range(1, ndims):
    if tt_list[core_idx].get_raw_shape() != tt_list[0].get_raw_shape():
      raise ValueError('Shapes of all TT-batch objects should coincide, got %s '
                       'and %s' % (tt_list[0].get_raw_shape(),
                                   tt_list[core_idx].get_raw_shape()))
    if tt_list[core_idx].get_tt_ranks() != tt_list[0].get_tt_ranks():
      raise ValueError('TT-ranks of all TT-batch objects should coincide, got '
                       '%s and %s' % (tt_list[0].get_tt_ranks(),
                                      tt_list[core_idx].get_tt_ranks()))

  res_cores = []
  for core_idx in range(ndims):
    curr_core = tf.concat([tt.tt_cores[core_idx] for tt in tt_list], axis=0)
    res_cores.append(curr_core)

  batch_size = sum([tt.batch_size for tt in tt_list])

  return TensorTrainBatch(res_cores, tt_list[0].get_raw_shape(),
                          tt_list[0].get_tt_ranks(), batch_size)

def gram_matrix(tt_vectors, matrix=None):
  """Computes Gramian matrix of a batch of TT-vecors.

  If matrix is None, computes
    res[i, j] = t3f.flat_inner(tt_vectors[i], tt_vectors[j]).
  If matrix is present, computes
      res[i, j] = t3f.flat_inner(tt_vectors[i], t3f.matmul(matrix, tt_vectors[j]))
    or more shorly
      res[i, j] = tt_vectors[i]^T * matrix * tt_vectors[j]

  Args:
    tt_vectors: TensorTrainBatch.
    matrix: None, or TensorTrain matrix.

  Returns:
    tf.tensor with the Gram matrix.
  """
  ndims = tt_vectors.ndims()
  if matrix is None:
    curr_core = tt_vectors.tt_cores[0]
    res = tf.einsum('paijb,qcijd->pqbd', curr_core, curr_core)
    for core_idx in range(1, ndims):
      curr_core = tt_vectors.tt_cores[core_idx]
      res = tf.einsum('pqac,paijb,qcijd->pqbd', res, curr_core, curr_core)
  else:
    # res[i, j] = tt_vectors[i] ^ T * matrix * tt_vectors[j]
    # We enumerate the dummy dimension (that takes 1 value) with `k`.
    vectors_shape = tt_vectors.get_shape()
    if vectors_shape[2] == 1 and vectors_shape[1] != 1:
      # TODO: not very efficient, better to put different order into einsum.
      tt_vectors = ops.transpose(tt_vectors)
    vectors_shape = tt_vectors.get_shape()
    if vectors_shape[1] != 1:
      # TODO: do something so that in case the shape is undefined on compilation
      # it still works.
      raise ValueError('The tt_vectors argument should be vectors (not '
                       'matrices) with shape defined on compilation.')
    curr_core = tt_vectors.tt_cores[0]
    curr_matrix_core = matrix.tt_cores[0]
    res = tf.einsum('pakib,cijd,qekjf->pqbdf', curr_core, curr_matrix_core,
                    curr_core)
    for core_idx in range(1, ndims):
      curr_core = tt_vectors.tt_cores[core_idx]
      curr_matrix_core = matrix.tt_cores[core_idx]
      res = tf.einsum('pqace,pakib,cijd,qekjf->pqbdf', res, curr_core,
                      curr_matrix_core, curr_core)

  # Squeeze to make the result of size batch_size x batch_size instead of
  # batch_size x batch_size x 1 x 1.
  return tf.squeeze(res)<commit_msg>Error checking bug fix: loop through objects, not cores<commit_after>import tensorflow as tf

from tensor_train_base import TensorTrainBase
from tensor_train_batch import TensorTrainBatch
import ops

def concat_along_batch_dim(tt_list):
  """Concat all TensorTrainBatch objects along batch dimension.

  Args:
    tt_list: a list of TensorTrainBatch objects.

  Returns:
    TensorTrainBatch
  """
  ndims = tt_list[0].ndims()

  if isinstance(tt_list, TensorTrainBase):
    # Not a list but just one element, nothing to concat.
    return tt_list

  for batch_idx in range(len(tt_list)):
    if not isinstance(tt_list[batch_idx], TensorTrainBatch):
      raise ValueError('All objects in the list should be TTBatch objects, got '
                       '%s' % tt_list[batch_idx])
  for batch_idx in range(1, len(tt_list)):
    if tt_list[batch_idx].get_raw_shape() != tt_list[0].get_raw_shape():
      raise ValueError('Shapes of all TT-batch objects should coincide, got %s '
                       'and %s' % (tt_list[0].get_raw_shape(),
                                   tt_list[batch_idx].get_raw_shape()))
    if tt_list[batch_idx].get_tt_ranks() != tt_list[0].get_tt_ranks():
      raise ValueError('TT-ranks of all TT-batch objects should coincide, got '
                       '%s and %s' % (tt_list[0].get_tt_ranks(),
                                      tt_list[batch_idx].get_tt_ranks()))

  res_cores = []
  for core_idx in range(ndims):
    curr_core = tf.concat([tt.tt_cores[core_idx] for tt in tt_list], axis=0)
    res_cores.append(curr_core)

  batch_size = sum([tt.batch_size for tt in tt_list])

  return TensorTrainBatch(res_cores, tt_list[0].get_raw_shape(),
                          tt_list[0].get_tt_ranks(), batch_size)

def gram_matrix(tt_vectors, matrix=None):
  """Computes Gramian matrix of a batch of TT-vecors.

  If matrix is None, computes
    res[i, j] = t3f.flat_inner(tt_vectors[i], tt_vectors[j]).
  If matrix is present, computes
      res[i, j] = t3f.flat_inner(tt_vectors[i], t3f.matmul(matrix, tt_vectors[j]))
    or more shorly
      res[i, j] = tt_vectors[i]^T * matrix * tt_vectors[j]

  Args:
    tt_vectors: TensorTrainBatch.
    matrix: None, or TensorTrain matrix.

  Returns:
    tf.tensor with the Gram matrix.
  """
  ndims = tt_vectors.ndims()
  if matrix is None:
    curr_core = tt_vectors.tt_cores[0]
    res = tf.einsum('paijb,qcijd->pqbd', curr_core, curr_core)
    for core_idx in range(1, ndims):
      curr_core = tt_vectors.tt_cores[core_idx]
      res = tf.einsum('pqac,paijb,qcijd->pqbd', res, curr_core, curr_core)
  else:
    # res[i, j] = tt_vectors[i] ^ T * matrix * tt_vectors[j]
    # We enumerate the dummy dimension (that takes 1 value) with `k`.
    vectors_shape = tt_vectors.get_shape()
    if vectors_shape[2] == 1 and vectors_shape[1] != 1:
      # TODO: not very efficient, better to put different order into einsum.
      tt_vectors = ops.transpose(tt_vectors)
    vectors_shape = tt_vectors.get_shape()
    if vectors_shape[1] != 1:
      # TODO: do something so that in case the shape is undefined on compilation
      # it still works.
      raise ValueError('The tt_vectors argument should be vectors (not '
                       'matrices) with shape defined on compilation.')
    curr_core = tt_vectors.tt_cores[0]
    curr_matrix_core = matrix.tt_cores[0]
    res = tf.einsum('pakib,cijd,qekjf->pqbdf', curr_core, curr_matrix_core,
                    curr_core)
    for core_idx in range(1, ndims):
      curr_core = tt_vectors.tt_cores[core_idx]
      curr_matrix_core = matrix.tt_cores[core_idx]
      res = tf.einsum('pqace,pakib,cijd,qekjf->pqbdf', res, curr_core,
                      curr_matrix_core, curr_core)

  # Squeeze to make the result of size batch_size x batch_size instead of
  # batch_size x batch_size x 1 x 1.
  return tf.squeeze(res)<|endoftext|>


<commit_before>from django.db import models
from django.contrib.auth.models import User

class AnonymousUserManager(models.Manager):
    def next_anonymous(self):
        """ If a name isn't provided, find the next Anonymous #N """
        first_name = "Anonymous"
        last_anon = User.objects.filter(first_name=first_name).count()
        last_name = "#%d" % (last_anon+1,)
        return (first_name, last_name)

    def get_or_create_user(self, **kwargs):
        """ Get_or_create user by name, creating anonymous if necessary """
        user__name = kwargs.pop('user__name')
        is_anonymous = False

        if user__name:
            try:
                first_name, last_name = user__name.split(' ', 1)  # simple but stupid
                # TODO, improve name parsing
            except (ValueError, AttributeError):
                # can't split, make first_name blank and last_name as provided
                first_name, last_name = "", user__name
        else:
            # None or blank, make it anonymous
            first_name, last_name = self.next_anonymous()
            is_anonymous = True

        if first_name:
            username = ("{}_{}".format(first_name, last_name)).lower()
        else:
            username = last_name

        user, new_user = User.objects.get_or_create(first_name=first_name, last_name=last_name, username=username)
        if new_user:
            user.anonymous = is_anonymous
            user.save()

        kwargs['user'] = user
        return super(AnonymousUserManager, self).get_or_create(**kwargs)

class AbstractUserBase(models.Model):
    user = models.OneToOneField(User)
    objects = AnonymousUserManager()

    class Meta:
        abstract = True

class Author(AbstractUserBase):
    occupation = models.CharField(max_length=100, null=True, blank=True)
    employer = models.CharField(max_length=100, null=True, blank=True)

    photo = models.ImageField(blank=True, null=True, upload_to="author_photo")
    sms_number = models.CharField(blank=True, null=True, max_length=15)

    employed = models.BooleanField(default=True)
    part_time = models.BooleanField(default=False)

    anonymous = models.BooleanField(default=False, help_text=
                                    "Group account like 'Web Anonymous' or 'SMS Anonymous'")

    def __unicode__(self):
        if self.occupation and self.employer:
            return "{}, {}, {}".format(self.user, self.occupation, self.company)
        elif self.user:
            return self.user.__unicode__()
        else:
            return "Unnamed Author"

class Organizer(AbstractUserBase):
    organization = models.CharField(max_length=100)
    title = models.CharField(max_length=100)
    # city, state, zip
    # website
<commit_msg>improve get_or_create_user to handle unicode, email address<commit_after>from django.db import models
from django.contrib.auth.models import User

class AnonymousUserManager(models.Manager):
    def next_anonymous(self):
        """ If a name isn't provided, find the next Anonymous #N """
        first_name = "Anonymous"
        last_anon = User.objects.filter(first_name=first_name).count()
        last_name = "#%d" % (last_anon+1,)
        return (first_name, last_name)

    def get_or_create_user(self, **kwargs):
        """ Get_or_create user by name, creating anonymous if necessary """
        user__name = kwargs.pop('user__name', None)
        first_name = kwargs.pop('first_name', None)
        last_name = kwargs.pop('last_name', None)
        email = kwargs.pop('email', '')
        is_anonymous = False

        if first_name and last_name:
            pass
        elif user__name:
            try:
                first_name, last_name = user__name.split(' ', 1)  # simple but stupid
                first_name = first_name.strip()
                last_name = last_name.strip()
                # TODO, improve name parsing
            except (ValueError, AttributeError):
                # can't split, make first_name blank and last_name as provided
                first_name, last_name = "", user__name
        elif email:
            # use email address before domain
            last_name, domain = email.split('@', 1)
            first_name = ""
        else:
            # None or blank, make it anonymous
            first_name, last_name = self.next_anonymous()
            is_anonymous = True

        if first_name:
            first_last = u"{}_{}".format(first_name.replace(' ', '_'), last_name.replace(' ', '_'))
            username = first_last.lower()
        else:
            username = last_name.lower()

        username = username.replace('__', '_').replace('.', '')

        user, new_user = User.objects.get_or_create(username=username)
        if new_user:
            user.first_name = first_name
            user.last_name = last_name
            user.anonymous = is_anonymous
            user.email = email
            user.save()

        kwargs['user'] = user
        return super(AnonymousUserManager, self).get_or_create(**kwargs)

class AbstractUserBase(models.Model):
    user = models.OneToOneField(User)
    objects = AnonymousUserManager()

    class Meta:
        abstract = True

class Author(AbstractUserBase):
    occupation = models.CharField(max_length=100, null=True, blank=True)
    employer = models.CharField(max_length=100, null=True, blank=True)

    photo = models.ImageField(blank=True, null=True, upload_to="author_photo")
    sms_number = models.CharField(blank=True, null=True, max_length=15)

    employed = models.BooleanField(default=True)
    part_time = models.BooleanField(default=False)

    anonymous = models.BooleanField(default=False, help_text=
                                    "Group account like 'Web Anonymous' or 'SMS Anonymous'")

    def __unicode__(self):
        if self.occupation and self.employer:
            return "{}, {}, {}".format(self.user, self.occupation, self.employer)
        elif self.user:
            return self.user.__unicode__()
        else:
            return "Unnamed Author"

class Organizer(AbstractUserBase):
    organization = models.CharField(max_length=100)
    title = models.CharField(max_length=100)
    # city, state, zip
    # website
<|endoftext|>