<commit_before># Copyright 2017 Taylor DeHaan
#
# 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 TreePrinter class.

The TreePrinter is used to create a graphical representation of a DirectoryTree
object and write it to stdout.
"""

import sys

class TreePrinter(object):
    """Prints a graphical representation of a DirectoryTree obejct.

    This class exposes a print_tree() method which will print a graphical
    representation of the tree.

    Args:
        tree (DirectoryTree): The tree to be printed.
        indentation_width (int): Width in terms of characters for the
            indentation levels
        output_file (str): File to redirect output to. If None, write to stdout.

    Attributes:
        _tree (DirectoryTree): The tree to be printed.
            originate.
        _indentation_width (int): The character width of each indentation level.
        _indent_str (str): String that represents a single indentation level.
        _entry_prefix (str): The prefix printed before an entry under a
            directory.
        _end_prefix (str): The prefix printed before the last entry under a
            directory.
    """

    # Static constants for formatting characters
    LEVEL_CHAR = '|'
    ENTRY_CHAR = '-'
    END_CHAR = '`'

    def __init__(self, tree, indentation_width=4, output_file=None):
        if indentation_width < 2:
            raise Exception("Indentation width must be 2 or greater!")

        self._tree = tree
        self._indentation_width = indentation_width

        # Indentation string
        self._indent_str = TreePrinter.LEVEL_CHAR.ljust(self._indentation_width,
                                                        ' ')

        # Prefix for an entry
        self._entry_prefix = TreePrinter.LEVEL_CHAR.ljust(
            self._indentation_width-1,
            TreePrinter.ENTRY_CHAR)
        self._entry_prefix += ' '

        # Prefix for the end entry
        self._end_prefix = TreePrinter.END_CHAR.ljust(self._indentation_width-1,
                                                      TreePrinter.ENTRY_CHAR)
        self._end_prefix += ' '

        # If an output file is specified, redirect stdout to file
        if output_file:
            sys.stdout = open(output_file, 'wb')

    def _print_recursive(self, node, indent_str):
        """Recursive method called on all nodes to print children and files.

        This method will recursively call itself on all children of the
        specified node.

        Args:
            node (DirectoryNode): The current node being visited.
            indent_str (str): The string to be printed as indentation.
        """
        # Get this node's files and children
        files = node.get_files()
        children = node.get_children()

        # Initialize length variables
        n_files = len(files)
        n_children = len(children)

        # Print this node's files
        for i in range(n_files):
            entry = indent_str + self._entry_prefix + files[i]

            # If this is the last file and there are no children, use the end
            # prefix instead of the normal prefix
            if n_children <= 0 and i + 1 == n_files:
                entry = indent_str + self._end_prefix + files[i]

            print(entry)  # pylint: disable=superfluous-parens

        # Recursively call on all children
        child_indent_str = indent_str + self._indent_str
        for i in range(n_children):
            child = children[i]
            prefix = self._entry_prefix

            # If this is the last child, use the end prefix else use the normal
            # entry prefix
            if i + 1 == n_children:
                child_indent_str = indent_str + " " * self._indentation_width
                prefix = self._end_prefix

            print(indent_str + prefix + child.get_name())  # pylint: disable=superfluous-parens
            self._print_recursive(child, child_indent_str)

    def print_tree(self):
        """Prints the tree provided to the constructor."""
        root = self._tree.get_root()
        print(root.get_name())  # pylint: disable=superfluous-parens
        self._print_recursive(root, "")
<commit_msg>Fixed issue with writing to binary file in python3+<commit_after># Copyright 2017 Taylor DeHaan
#
# 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 TreePrinter class.

The TreePrinter is used to create a graphical representation of a DirectoryTree
object and write it to stdout.
"""

import sys

class TreePrinter(object):
    """Prints a graphical representation of a DirectoryTree obejct.

    This class exposes a print_tree() method which will print a graphical
    representation of the tree.

    Args:
        tree (DirectoryTree): The tree to be printed.
        indentation_width (int): Width in terms of characters for the
            indentation levels
        output_file (str): File to redirect output to. If None, write to stdout.

    Attributes:
        _tree (DirectoryTree): The tree to be printed.
            originate.
        _indentation_width (int): The character width of each indentation level.
        _indent_str (str): String that represents a single indentation level.
        _entry_prefix (str): The prefix printed before an entry under a
            directory.
        _end_prefix (str): The prefix printed before the last entry under a
            directory.
    """

    # Static constants for formatting characters
    LEVEL_CHAR = '|'
    ENTRY_CHAR = '-'
    END_CHAR = '`'

    def __init__(self, tree, indentation_width=4, output_file=None):
        if indentation_width < 2:
            raise Exception("Indentation width must be 2 or greater!")

        self._tree = tree
        self._indentation_width = indentation_width

        # Indentation string
        self._indent_str = TreePrinter.LEVEL_CHAR.ljust(self._indentation_width,
                                                        ' ')

        # Prefix for an entry
        self._entry_prefix = TreePrinter.LEVEL_CHAR.ljust(
            self._indentation_width-1,
            TreePrinter.ENTRY_CHAR)
        self._entry_prefix += ' '

        # Prefix for the end entry
        self._end_prefix = TreePrinter.END_CHAR.ljust(self._indentation_width-1,
                                                      TreePrinter.ENTRY_CHAR)
        self._end_prefix += ' '

        # If an output file is specified, redirect stdout to file
        if output_file:
            sys.stdout = open(output_file, 'w')

    def _print_recursive(self, node, indent_str):
        """Recursive method called on all nodes to print children and files.

        This method will recursively call itself on all children of the
        specified node.

        Args:
            node (DirectoryNode): The current node being visited.
            indent_str (str): The string to be printed as indentation.
        """
        # Get this node's files and children
        files = node.get_files()
        children = node.get_children()

        # Initialize length variables
        n_files = len(files)
        n_children = len(children)

        # Print this node's files
        for i in range(n_files):
            entry = indent_str + self._entry_prefix + files[i]

            # If this is the last file and there are no children, use the end
            # prefix instead of the normal prefix
            if n_children <= 0 and i + 1 == n_files:
                entry = indent_str + self._end_prefix + files[i]

            print(entry)  # pylint: disable=superfluous-parens

        # Recursively call on all children
        child_indent_str = indent_str + self._indent_str
        for i in range(n_children):
            child = children[i]
            prefix = self._entry_prefix

            # If this is the last child, use the end prefix else use the normal
            # entry prefix
            if i + 1 == n_children:
                child_indent_str = indent_str + " " * self._indentation_width
                prefix = self._end_prefix

            print(indent_str + prefix + child.get_name())  # pylint: disable=superfluous-parens
            self._print_recursive(child, child_indent_str)

    def print_tree(self):
        """Prints the tree provided to the constructor."""
        root = self._tree.get_root()
        print(root.get_name())  # pylint: disable=superfluous-parens
        self._print_recursive(root, "")
<|endoftext|>


<commit_before>from __future__ import unicode_literals

import json

from django import http
from django.conf import settings
from django.contrib.admin.actions import delete_selected
from django.contrib.admin.options import ModelAdmin
from django.utils.encoding import force_text
from django.utils.html import escape
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext as _

from mptt.exceptions import InvalidMove
from mptt.forms import MPTTAdminForm, TreeNodeChoiceField
from mptt.models import MPTTModel, TreeForeignKey

__all__ = ('MPTTModelAdmin', 'MPTTAdminForm')
IS_GRAPPELLI_INSTALLED = 'grappelli' in settings.INSTALLED_APPS

class MPTTModelAdmin(ModelAdmin):
    """
    A basic admin class that displays tree items according to their position in
    the tree.  No extra editing functionality beyond what Django admin normally
    offers.
    """

    if IS_GRAPPELLI_INSTALLED:
        change_list_template = 'admin/grappelli_mptt_change_list.html'
    else:
        change_list_template = 'admin/mptt_change_list.html'

    form = MPTTAdminForm

    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if issubclass(db_field.rel.to, MPTTModel) \
                and not isinstance(db_field, TreeForeignKey) \
                and db_field.name not in self.raw_id_fields:
            db = kwargs.get('using')

            limit_choices_to = db_field.get_limit_choices_to()
            defaults = dict(
                form_class=TreeNodeChoiceField,
                queryset=db_field.rel.to._default_manager.using(
                    db).complex_filter(limit_choices_to),
                required=False)
            defaults.update(kwargs)
            kwargs = defaults
        return super(MPTTModelAdmin, self).formfield_for_foreignkey(
            db_field, request, **kwargs)

    def get_ordering(self, request):
        """
        Changes the default ordering for changelists to tree-order.
        """
        mptt_opts = self.model._mptt_meta
        return self.ordering or (mptt_opts.tree_id_attr, mptt_opts.left_attr)

    def delete_selected_tree(self, modeladmin, request, queryset):
        """
        Deletes multiple instances and makes sure the MPTT fields get
        recalculated properly. (Because merely doing a bulk delete doesn't
        trigger the post_delete hooks.)
        """
        # If this is True, the confirmation page has been displayed
        if request.POST.get('post'):
            n = 0
            with queryset.model._tree_manager.delay_mptt_updates():
                for obj in queryset:
                    if self.has_delete_permission(request, obj):
                        obj.delete()
                        n += 1
                        obj_display = force_text(obj)
                        self.log_deletion(request, obj, obj_display)
            self.message_user(
                request,
                _('Successfully deleted %(count)d items.') % {'count': n})
            # Return None to display the change list page again
            return None
        else:
            # (ab)using the built-in action to display the confirmation page
            return delete_selected(self, request, queryset)

    def get_actions(self, request):
        actions = super(MPTTModelAdmin, self).get_actions(request)
        if 'delete_selected' in actions:
            actions['delete_selected'] = (
                self.delete_selected_tree,
                'delete_selected',
                _('Delete selected %(verbose_name_plural)s'))
        return actions

def _build_tree_structure(queryset):
    """
    Build an in-memory representation of the item tree, trying to keep
    database accesses down to a minimum. The returned dictionary looks like
    this (as json dump):

        {"6": [7, 8, 10]
         "7": [12],
         "8": [],
         ...
         }
    """
    all_nodes = {}

    mptt_opts = queryset.model._mptt_meta
    items = queryset.order_by(
        mptt_opts.tree_id_attr,
        mptt_opts.left_attr,
    ).values_list(
        "pk",
        "%s_id" % mptt_opts.parent_attr,
    )
    for p_id, parent_id in items:
        all_nodes.setdefault(
            str(parent_id) if parent_id else 0,
            [],
        ).append(p_id)
    return all_nodes

class TreeEditor(MPTTModelAdmin):
    """
    The ``TreeEditor`` modifies the standard Django administration change list
    to a drag-drop enabled interface for django-mptt_-managed Django models.

    .. _django-mptt: https://github.com/django-mptt/django-mptt/
    """

    def __init__(self, *args, **kwargs):
        super(TreeEditor, self).__init__(*args, **kwargs)

        opts = self.model._meta
        self.change_list_template = [
            'admin/%s/%s/tree_editor.html' % (
                opts.app_label, opts.object_name.lower()),
            'admin/%s/tree_editor.html' % opts.app_label,
            'admin/tree_editor.html',
        ]

    def get_list_display(self, request):
        list_display = list(super(TreeEditor, self).get_list_display(request))
        list_display = [
            l for l in list_display
            if l not in ('__str__', '__unicode__')
        ]

        if 'indented_title' in list_display:
            return list_display

        place = 1 if 'action_checkbox' in list_display else 0
        list_display[place:place] = [
            'tree_actions',
            'indented_title',
        ]
        return list_display

    def get_list_display_links(self, request, list_display):
        return ('indented_title',)

    def tree_actions(self, item):
        try:
            url = item.get_absolute_url()
        except Exception:  # Yes.
            url = ''

        return mark_safe(' '.join((
            '

',
            (
                '

'
            ) % (
                item.pk,
                escape(url),
            ),
        )))
    tree_actions.short_description = ''
    tree_actions.allow_tags = True

    def indented_title(self, item):
        """
        Generate a short title for an object, indent it depending on
        the object's depth in the hierarchy.
        """
        return mark_safe('

 %s' % (
            item._mpttfield('level') * 20,
            escape('%s' % item),
        ))
    indented_title.short_description = _('title')
    indented_title.allow_tags = True

    def changelist_view(self, request, extra_context=None, *args, **kwargs):
        """
        Handle the changelist view, the django view for the model instances
        change list/actions page.
        """

        # handle common AJAX requests
        if request.is_ajax():
            cmd = request.POST.get('__cmd')
            if cmd == 'move_node':
                return self._move_node(request)

            return http.HttpResponseBadRequest(
                'Oops. AJAX request not understood.')

        extra_context = extra_context or {}
        extra_context['tree_structure'] = mark_safe(
            json.dumps(_build_tree_structure(self.get_queryset(request))))
        extra_context['node_levels'] = mark_safe(json.dumps(
            dict(self.get_queryset(request).order_by().values_list(
                'pk', self.model._mptt_meta.level_attr
            ))
        ))
        extra_context['tree_editor_cookie_name'] = 'tree_%s_%s_collapsed' % (
            self.model._meta.app_label,
            self.model._meta.model_name,
        )

        return super(TreeEditor, self).changelist_view(
            request, extra_context, *args, **kwargs)

    def _move_node(self, request):
        if hasattr(self.model.objects, 'move_node'):
            tree_manager = self.model.objects
        else:
            tree_manager = self.model._tree_manager

        queryset = self.get_queryset(request)
        cut_item = queryset.get(pk=request.POST.get('cut_item'))
        pasted_on = queryset.get(pk=request.POST.get('pasted_on'))
        position = request.POST.get('position')

        if not self.has_change_permission(request, cut_item):
            self.message_user(request, _('No permission'))
            return http.HttpResponse('FAIL')

        if position in ('last-child', 'left', 'right'):
            try:
                tree_manager.move_node(cut_item, pasted_on, position)
            except InvalidMove as e:
                self.message_user(request, '%s' % e)
                return http.HttpResponse('FAIL')

            # Ensure that model save methods have been run (required to
            # update Page._cached_url values, might also be helpful for other
            # models inheriting MPTTModel)
            for item in queryset.filter(id__in=(cut_item.pk, pasted_on.pk)):
                item.save()

            self.message_user(
                request,
                _('%s has been moved to a new position.') % cut_item)
            return http.HttpResponse('OK')

        self.message_user(request, _('Did not understand moving instruction.'))
        return http.HttpResponse('FAIL')
<commit_msg>Better short_description<commit_after>from __future__ import unicode_literals

import json

from django import http
from django.conf import settings
from django.contrib.admin.actions import delete_selected
from django.contrib.admin.options import ModelAdmin
from django.utils.encoding import force_text
from django.utils.html import escape
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext as _

from mptt.exceptions import InvalidMove
from mptt.forms import MPTTAdminForm, TreeNodeChoiceField
from mptt.models import MPTTModel, TreeForeignKey

__all__ = ('MPTTModelAdmin', 'MPTTAdminForm')
IS_GRAPPELLI_INSTALLED = 'grappelli' in settings.INSTALLED_APPS

class MPTTModelAdmin(ModelAdmin):
    """
    A basic admin class that displays tree items according to their position in
    the tree.  No extra editing functionality beyond what Django admin normally
    offers.
    """

    if IS_GRAPPELLI_INSTALLED:
        change_list_template = 'admin/grappelli_mptt_change_list.html'
    else:
        change_list_template = 'admin/mptt_change_list.html'

    form = MPTTAdminForm

    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if issubclass(db_field.rel.to, MPTTModel) \
                and not isinstance(db_field, TreeForeignKey) \
                and db_field.name not in self.raw_id_fields:
            db = kwargs.get('using')

            limit_choices_to = db_field.get_limit_choices_to()
            defaults = dict(
                form_class=TreeNodeChoiceField,
                queryset=db_field.rel.to._default_manager.using(
                    db).complex_filter(limit_choices_to),
                required=False)
            defaults.update(kwargs)
            kwargs = defaults
        return super(MPTTModelAdmin, self).formfield_for_foreignkey(
            db_field, request, **kwargs)

    def get_ordering(self, request):
        """
        Changes the default ordering for changelists to tree-order.
        """
        mptt_opts = self.model._mptt_meta
        return self.ordering or (mptt_opts.tree_id_attr, mptt_opts.left_attr)

    def delete_selected_tree(self, modeladmin, request, queryset):
        """
        Deletes multiple instances and makes sure the MPTT fields get
        recalculated properly. (Because merely doing a bulk delete doesn't
        trigger the post_delete hooks.)
        """
        # If this is True, the confirmation page has been displayed
        if request.POST.get('post'):
            n = 0
            with queryset.model._tree_manager.delay_mptt_updates():
                for obj in queryset:
                    if self.has_delete_permission(request, obj):
                        obj.delete()
                        n += 1
                        obj_display = force_text(obj)
                        self.log_deletion(request, obj, obj_display)
            self.message_user(
                request,
                _('Successfully deleted %(count)d items.') % {'count': n})
            # Return None to display the change list page again
            return None
        else:
            # (ab)using the built-in action to display the confirmation page
            return delete_selected(self, request, queryset)

    def get_actions(self, request):
        actions = super(MPTTModelAdmin, self).get_actions(request)
        if 'delete_selected' in actions:
            actions['delete_selected'] = (
                self.delete_selected_tree,
                'delete_selected',
                _('Delete selected %(verbose_name_plural)s'))
        return actions

def _build_tree_structure(queryset):
    """
    Build an in-memory representation of the item tree, trying to keep
    database accesses down to a minimum. The returned dictionary looks like
    this (as json dump):

        {"6": [7, 8, 10]
         "7": [12],
         "8": [],
         ...
         }
    """
    all_nodes = {}

    mptt_opts = queryset.model._mptt_meta
    items = queryset.order_by(
        mptt_opts.tree_id_attr,
        mptt_opts.left_attr,
    ).values_list(
        "pk",
        "%s_id" % mptt_opts.parent_attr,
    )
    for p_id, parent_id in items:
        all_nodes.setdefault(
            str(parent_id) if parent_id else 0,
            [],
        ).append(p_id)
    return all_nodes

class TreeEditor(MPTTModelAdmin):
    """
    The ``TreeEditor`` modifies the standard Django administration change list
    to a drag-drop enabled interface for django-mptt_-managed Django models.

    .. _django-mptt: https://github.com/django-mptt/django-mptt/
    """

    def __init__(self, *args, **kwargs):
        super(TreeEditor, self).__init__(*args, **kwargs)

        opts = self.model._meta
        self.change_list_template = [
            'admin/%s/%s/tree_editor.html' % (
                opts.app_label, opts.object_name.lower()),
            'admin/%s/tree_editor.html' % opts.app_label,
            'admin/tree_editor.html',
        ]

        # Set a better column header than "title"
        self.__class__.indented_title.short_description = opts.verbose_name

    def get_list_display(self, request):
        list_display = list(super(TreeEditor, self).get_list_display(request))
        list_display = [
            l for l in list_display
            if l not in ('__str__', '__unicode__')
        ]

        if 'indented_title' in list_display:
            return list_display

        place = 1 if 'action_checkbox' in list_display else 0
        list_display[place:place] = [
            'tree_actions',
            'indented_title',
        ]
        return list_display

    def get_list_display_links(self, request, list_display):
        return ('indented_title',)

    def tree_actions(self, item):
        try:
            url = item.get_absolute_url()
        except Exception:  # Yes.
            url = ''

        return mark_safe(' '.join((
            '

',
            (
                '

'
            ) % (
                item.pk,
                escape(url),
            ),
        )))
    tree_actions.short_description = ''
    tree_actions.allow_tags = True

    def indented_title(self, item):
        """
        Generate a short title for an object, indent it depending on
        the object's depth in the hierarchy.
        """
        return mark_safe('

 %s' % (
            item._mpttfield('level') * 20,
            escape('%s' % item),
        ))
    indented_title.short_description = _('title')
    indented_title.allow_tags = True

    def changelist_view(self, request, extra_context=None, *args, **kwargs):
        """
        Handle the changelist view, the django view for the model instances
        change list/actions page.
        """

        # handle common AJAX requests
        if request.is_ajax():
            cmd = request.POST.get('__cmd')
            if cmd == 'move_node':
                return self._move_node(request)

            return http.HttpResponseBadRequest(
                'Oops. AJAX request not understood.')

        extra_context = extra_context or {}
        extra_context['tree_structure'] = mark_safe(
            json.dumps(_build_tree_structure(self.get_queryset(request))))
        extra_context['node_levels'] = mark_safe(json.dumps(
            dict(self.get_queryset(request).order_by().values_list(
                'pk', self.model._mptt_meta.level_attr
            ))
        ))
        extra_context['tree_editor_cookie_name'] = 'tree_%s_%s_collapsed' % (
            self.model._meta.app_label,
            self.model._meta.model_name,
        )

        return super(TreeEditor, self).changelist_view(
            request, extra_context, *args, **kwargs)

    def _move_node(self, request):
        if hasattr(self.model.objects, 'move_node'):
            tree_manager = self.model.objects
        else:
            tree_manager = self.model._tree_manager

        queryset = self.get_queryset(request)
        cut_item = queryset.get(pk=request.POST.get('cut_item'))
        pasted_on = queryset.get(pk=request.POST.get('pasted_on'))
        position = request.POST.get('position')

        if not self.has_change_permission(request, cut_item):
            self.message_user(request, _('No permission'))
            return http.HttpResponse('FAIL')

        if position in ('last-child', 'left', 'right'):
            try:
                tree_manager.move_node(cut_item, pasted_on, position)
            except InvalidMove as e:
                self.message_user(request, '%s' % e)
                return http.HttpResponse('FAIL')

            # Ensure that model save methods have been run (required to
            # update Page._cached_url values, might also be helpful for other
            # models inheriting MPTTModel)
            for item in queryset.filter(id__in=(cut_item.pk, pasted_on.pk)):
                item.save()

            self.message_user(
                request,
                _('%s has been moved to a new position.') % cut_item)
            return http.HttpResponse('OK')

        self.message_user(request, _('Did not understand moving instruction.'))
        return http.HttpResponse('FAIL')
<|endoftext|>


<commit_before>#!/usr/bin/env python
#
# MSP430 clock callibration utility.
#
# This tool can measure the internal oscillator of F1xx, F2xx and F4xx devices
# that are connected to the JTAG. It can  display the supported frequencies,
# or run a software FLL to find the settings for a specified frequency.
#
# (C) 2005 Chris Liechti <cliechti@gmx.net>
# this is distributed under a free software license, see license.txt
#
# $Id: msp430-dco.py,v 1.3 2005/12/29 14:01:48 cliechti Exp $

from msp430 import jtag, clock
import sys
import struct

debug = False

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def nice_frequency(frequency):
    """return a string of the frequency with SI unit and a reasonable number
       of digits"""
    if frequency < 1e3:
        return "%dHz" % frequency
    elif frequency < 10e3:
        return "%.3fkHz" % (frequency/1e3)
    elif frequency < 100e3:
        return "%.2fkHz" % (frequency/1e3)
    elif frequency < 1e6:
        return "%.1fkHz" % (frequency/1e3)
    elif frequency < 10e6:
        return "%.3fMHz" % (frequency/1e6)
    elif frequency < 1e9:
        return "%.2fMHz" % (frequency/1e6)
    return "%.2fGHz" % (frequency/1e9)

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

def get_msp430_type():
    """return the MSP430 type id that is stored in the ROM"""
    (device, ) = struct.unpack(">H", jtag._parjtag.memread(0x0ff0, 2))
    if debug: sys.stderr.write("MSP430 device: 0x%04x\n" % (device, ))
    return device

def adjust_clock(out, frequency, tolerance=0.02, dcor=False):
    """detect MSP430 type and try to set the clock to the given frequency.
    when successful, print the clock control register settings.

    this function assumes that the jtag connection to the device has already
    been initialized and that the device is under jtag control and stopped.
    """
    if tolerance < 0.005 or tolerance > 50:
        raise ValueError('tolerance out of range %f' % (tolerance,))
    device = get_msp430_type() >> 8
    if device == 0xf1:
        frequency, dco, bcs1 = clock.setDCO(
            frequency*(1-tolerance),
            frequency*(1+tolerance),
            maxrsel=7,
            dcor=dcor
        )
        out.write('BCSCTL1 = 0x%02x; DCOCTL = 0x%02x; //%dHz\n' % (bcs1, dco, frequency))
    elif device == 0xf2:
        frequency, dco, bcs1 = clock.setDCO(
            frequency*(1-tolerance),
            frequency*(1+tolerance),
            maxrsel=15
        )
        out.write('BCSCTL1 = 0x%02x; DCOCTL = 0x%02x; //%dHz\n' % (bcs1, dco, frequency))
    elif device == 0xf4:
        frequency, scfi0, scfi1, scfqctl, fll_ctl0, fll_ctl1 = clock.setDCOPlus(
            frequency*(1-tolerance),
            frequency*(1+tolerance)
        )
        out.write('SCFI0 = 0x%02x; SCFI1 = 0x%02x; SCFQCTL = 0x%02x; FLL_CTL0 = 0x%02x; FLL_CTL1 = 0x%02x; //%dHz\n' % (
            scfi0, scfi1, scfqctl, fll_ctl0, fll_ctl1, frequency
        ))
    else:
        IOError("unknown MSP430 type %02x" % device)

def measure_clock(out):
    """measure fmin and fmax"""
    device = get_msp430_type() >> 8
    if device == 0xf1:
        for rsel in range(8):
            fmin = clock.getDCOFreq(0x00, rsel)
            fmax = clock.getDCOFreq(0xff, rsel)
            out.write('%s < f(rsel_%d) < %s\n' % (
                nice_frequency(fmin),
                rsel,
                nice_frequency(fmax)
            ))
        fmin = clock.getDCOFreq(0, 0)
        fmax = clock.getDCOFreq(0xff, 0x07)
    elif device == 0xf2:
        for rsel in range(16):
            fmin = clock.getDCOFreq(0x00, rsel)
            fmax = clock.getDCOFreq(0xff, rsel)
            out.write('%s < f(rsel_%d) < %s\n' % (
                nice_frequency(fmin),
                rsel,
                nice_frequency(fmax)
            ))
        fmin = clock.getDCOFreq(0, 0)
        fmax = clock.getDCOFreq(0xff, 0x0f)
    elif device == 0xf4:
        fmin = clock.getDCOPlusFreq(0, 0, 0x80, 0x80, 0)
        #XXX the F4xx has clock settings that go higher, but not all are valid
        #for the CPU
        #~ fmax = getDCOPlusFreq(0x03, 0xff, 0x80, 0x80, 0) # should be around 6MHz
        fmax = clock.getDCOPlusFreq(0x13, 0xbf, 0x80, 0x80, 0) # should be around 16MHz
    else:
        IOError("unknown MSP430 type %02x" % device)
    out.write('fmin = %8dHz (%s)\n' % (fmin, nice_frequency(fmin)))
    out.write('fmax = %8dHz (%s)\n' % (fmax, nice_frequency(fmax)))

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

def main():
    from optparse import OptionParser

    parser = OptionParser(usage="""\
    usage: %prog [options] frequency

MSP430 clock callibration utility.

This tool can measure the internal oscillator of F1xx, F2xx and F4xx devices,
display the supported frequencies, or run a software FLL to find the settings
for a specified frequency.

The target device has to be connected to the JTAG interface.

Examples:
    See min and max clock speeds:
        %prog --measure

    Get clock settings for 2.0MHz +/-2%:
        %prog --tolerance=0.02 2.0e6""")
    parser.add_option("-o", "--output", dest="output",
                      help="write result to given file", metavar="FILE")
    parser.add_option("", "--dcor", dest="dcor",
                      help="use external resistor",
                      default=False, action='store_true')
    parser.add_option("-d", "--debug", dest="debug",
                      help="print debug messages",
                      default=False, action='store_true')
    parser.add_option("-l", "--lpt", dest="lpt",
                      help="set the parallel port",
                      default=None)

    parser.add_option("", "--measure", dest="measure",
                      help="measure min and max clock settings and exit",
                      default=False, action="store_true")

    parser.add_option("-t", "--tolerance", dest="tolerance",
                      help="set the clock tolerance as factor. e.g. 0.1 means 10%",
                      default=0.01, type="float")

    (options, args) = parser.parse_args()

    global debug
    debug = options.debug
    clock.debug = options.debug

    if not options.measure:
        if len(args) != 1:
            parser.error('exacly one argument expected: the target frequency')
        frequency = float(args[0])

    #prepare output
    if options.output is None or options.output == '-':
        out = sys.stdout
    else:
        out = file(options.output, 'w')

    #
    jtagobj = jtag.JTAG()
    jtagobj.open(options.lpt)               #try to open port
    jtagobj.connect()                       #try to connect to target
    try:
        if options.measure:
            measure_clock(out)
        else:
            adjust_clock(out, frequency, options.tolerance, options.dcor)
        #~ print "%.2f kHz" % (getDCOFreq(0, 0)/1e3)
    finally:
        if sys.exc_info()[:1]:              #if there is an exception pending
            jtagobj.verbose = 0             #do not write any more messages
        jtagobj.reset(1, 1)                 #reset and release target
        jtagobj.close()                     #Release communication port

if __name__ == '__main__':
    try:
        main()
    except SystemExit:
        raise                                           #let pass exit() calls
    except KeyboardInterrupt:
        if debug: raise                                 #show full trace in debug mode
        sys.stderr.write("User abort.\n")               #short messy in user mode
        sys.exit(1)                                     #set errorlevel for script usage
    except Exception, msg:                              #every Exception is caught and displayed
        if debug: raise                                 #show full trace in debug mode
        sys.stderr.write("\nAn error occoured:\n%s\n" % msg) #short messy in user mode
        sys.exit(1)                                     #set errorlevel for script usage
<commit_msg>- some F2xx device seem to have the DCOR bit too - include BCSCTL2/3 in output, sligtly change output - --define option that prints defines (with frequency in name, so mutltiple clock settings can be included in a single header file)<commit_after>#!/usr/bin/env python
#
# MSP430 clock callibration utility.
#
# This tool can measure the internal oscillator of F1xx, F2xx and F4xx devices
# that are connected to the JTAG. It can  display the supported frequencies,
# or run a software FLL to find the settings for a specified frequency.
#
# (C) 2005 Chris Liechti <cliechti@gmx.net>
# this is distributed under a free software license, see license.txt
#
# $Id: msp430-dco.py,v 1.4 2005/12/29 19:25:39 cliechti Exp $

from msp430 import jtag, clock
import sys
import struct

debug = False

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def nice_frequency(frequency):
    """return a string of the frequency with SI unit and a reasonable number
       of digits"""
    if frequency < 1e3:
        return "%dHz" % frequency
    elif frequency < 10e3:
        return "%.3fkHz" % (frequency/1e3)
    elif frequency < 100e3:
        return "%.2fkHz" % (frequency/1e3)
    elif frequency < 1e6:
        return "%.1fkHz" % (frequency/1e3)
    elif frequency < 10e6:
        return "%.3fMHz" % (frequency/1e6)
    elif frequency < 1e9:
        return "%.2fMHz" % (frequency/1e6)
    return "%.2fGHz" % (frequency/1e9)

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

def get_msp430_type():
    """return the MSP430 type id that is stored in the ROM"""
    (device, ) = struct.unpack(">H", jtag._parjtag.memread(0x0ff0, 2))
    if debug: sys.stderr.write("MSP430 device: 0x%04x\n" % (device, ))
    return device

def adjust_clock(out, frequency, tolerance=0.02, dcor=False, define=False):
    """detect MSP430 type and try to set the clock to the given frequency.
    when successful, print the clock control register settings.

    this function assumes that the jtag connection to the device has already
    been initialized and that the device is under jtag control and stopped.
    """
    if tolerance < 0.005 or tolerance > 50:
        raise ValueError('tolerance out of range %f' % (tolerance,))
    device = get_msp430_type() >> 8
    if device == 0xf1:
        measured_frequency, dco, bcs1 = clock.setDCO(
            frequency*(1-tolerance),
            frequency*(1+tolerance),
            maxrsel=7,
            dcor=dcor
        )
        out.write('// BCS settings for %s\n' % (nice_frequency(measured_frequency), ))
        if define:
            suffix = '_%s' % nice_frequency(frequency).replace('.','_')
            out.write('#define DCOCTL%s 0x%02x\n' % (suffix, dco,))
            out.write('#define BCSCTL1%s 0x%02x\n' % (suffix, bcs1,))
            if dcor:
                out.write('#define BCSCTL2%s 0x01 //select external RSOC\n' % (suffix,))
            else:
                out.write('#define BCSCTL2%s 0x00 //select internal RSOC\n' % (suffix,))
        else:
            out.write('DCOCTL = 0x%02x;\n' % (dco,))
            out.write('BCSCTL1 = 0x%02x;\n' % (bcs1,))
            if dcor:
                out.write('BCSCTL2 = 0x01; //select external RSOC\n')
            else:
                out.write('BCSCTL2 = 0x00; //select internal RSOC\n')
    elif device == 0xf2:
        measured_frequency, dco, bcs1 = clock.setDCO(
            frequency*(1-tolerance),
            frequency*(1+tolerance),
            maxrsel=15,
            dcor=dcor
        )
        out.write('// BCS+ settings for %s\n' % (nice_frequency(measured_frequency), ))
        if define:
            suffix = '_%s' % nice_frequency(frequency).replace('.','_')
            out.write('#define DCOCTL%s 0x%02x\n' % (suffix, dco,))
            out.write('#define BCSCTL1%s 0x%02x\n' % (suffix, bcs1,))
            if dcor:
                out.write('#define BCSCTL2%s 0x01 //select external RSOC\n' % (suffix,))
            else:
                out.write('#define BCSCTL2%s 0x00 //select internal RSOC\n' % (suffix,))
            out.write('#define BCSCTL3%s 0x00\n' % (suffix,))
        else:
            out.write('DCOCTL = 0x%02x;\n' % (dco,))
            out.write('BCSCTL1 = 0x%02x;\n' % (bcs1,))
            if dcor:
                out.write('BCSCTL2 = 0x01; //select external RSOC\n')
            else:
                out.write('BCSCTL2 = 0x00; //select internal RSOC\n')
            out.write('BCSCTL3 = 0x00;\n')
    elif device == 0xf4:
        measured_frequency, scfi0, scfi1, scfqctl, fll_ctl0, fll_ctl1 = clock.setDCOPlus(
            frequency*(1-tolerance),
            frequency*(1+tolerance)
        )
        out.write('// FLL+ settings for %s\n' % (nice_frequency(measured_frequency), ))
        if define:
            suffix = '_%s' % nice_frequency(frequency).replace('.','_')
            out.write('#define SCFI0%(suffix)s 0x%(scfi0)02x\n'
                      '#define SCFI1%(suffix)s 0x%(scfi1)02x\n'
                      '#define SCFQCTL%(suffix)s 0x%(scfqctl)02x\n'
                      '#define FLL_CTL0%(suffix)s 0x%(fll_ctl0)02x\n'
                      '#define FLL_CTL1%(suffix)s 0x%(fll_ctl1)02x\n' % vars()
            )
        else:
            out.write('SCFI0 = 0x%02x;\nSCFI1 = 0x%02x;\nSCFQCTL = 0x%02x;\nFLL_CTL0 = 0x%02x;\nFLL_CTL1 = 0x%02x;\n' % (
                scfi0, scfi1, scfqctl, fll_ctl0, fll_ctl1
            ))
    else:
        IOError("unknown MSP430 type %02x" % device)

def measure_clock(out):
    """measure fmin and fmax"""
    device = get_msp430_type() >> 8
    if device == 0xf1:
        for rsel in range(8):
            fmin = clock.getDCOFreq(0x00, rsel)
            fmax = clock.getDCOFreq(0xff, rsel)
            out.write('%s < f(rsel_%d) < %s\n' % (
                nice_frequency(fmin),
                rsel,
                nice_frequency(fmax)
            ))
        fmin = clock.getDCOFreq(0, 0)
        fmax = clock.getDCOFreq(0xff, 0x07)
    elif device == 0xf2:
        for rsel in range(16):
            fmin = clock.getDCOFreq(0x00, rsel)
            fmax = clock.getDCOFreq(0xff, rsel)
            out.write('%s < f(rsel_%d) < %s\n' % (
                nice_frequency(fmin),
                rsel,
                nice_frequency(fmax)
            ))
        fmin = clock.getDCOFreq(0, 0)
        fmax = clock.getDCOFreq(0xff, 0x0f)
    elif device == 0xf4:
        fmin = clock.getDCOPlusFreq(0, 0, 0x80, 0x80, 0)
        #XXX the F4xx has clock settings that go higher, but not all are valid
        #for the CPU
        #~ fmax = getDCOPlusFreq(0x03, 0xff, 0x80, 0x80, 0) # should be around 6MHz
        fmax = clock.getDCOPlusFreq(0x13, 0xbf, 0x80, 0x80, 0) # should be around 16MHz
    else:
        IOError("unknown MSP430 type %02x" % device)
    out.write('fmin = %8dHz (%s)\n' % (fmin, nice_frequency(fmin)))
    out.write('fmax = %8dHz (%s)\n' % (fmax, nice_frequency(fmax)))

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

def main():
    from optparse import OptionParser

    parser = OptionParser(usage="""\
    usage: %prog [options] frequency

MSP430 clock callibration utility.

This tool can measure the internal oscillator of F1xx, F2xx and F4xx devices,
display the supported frequencies, or run a software FLL to find the settings
for a specified frequency.

The target device has to be connected to the JTAG interface.

Examples:
    See min and max clock speeds:
        %prog --measure

    Get clock settings for 2.0MHz +/-2%:
        %prog --tolerance=0.02 2.0e6""")
    parser.add_option("-o", "--output", dest="output",
                      help="write result to given file", metavar="FILE")
    parser.add_option("", "--dcor", dest="dcor",
                      help="use external resistor",
                      default=False, action='store_true')
    parser.add_option("-d", "--debug", dest="debug",
                      help="print debug messages",
                      default=False, action='store_true')
    parser.add_option("-l", "--lpt", dest="lpt",
                      help="set the parallel port",
                      default=None)

    parser.add_option("", "--measure", dest="measure",
                      help="measure min and max clock settings and exit",
                      default=False, action="store_true")

    parser.add_option("-t", "--tolerance", dest="tolerance",
                      help="set the clock tolerance as factor. e.g. 0.1 means 10%",
                      default=0.01, type="float")

    parser.add_option("", "--define", dest="define",
                      help="output #defines instead of assignments",
                      default=False, action='store_true')

    (options, args) = parser.parse_args()

    global debug
    debug = options.debug
    clock.debug = options.debug

    if not options.measure:
        if len(args) != 1:
            parser.error('exacly one argument expected: the target frequency')
        frequency = float(args[0])

    #prepare output
    if options.output is None or options.output == '-':
        out = sys.stdout
    else:
        out = file(options.output, 'w')

    #
    jtagobj = jtag.JTAG()
    jtagobj.open(options.lpt)               #try to open port
    jtagobj.connect()                       #try to connect to target
    try:
        if options.measure:
            measure_clock(out)
        else:
            adjust_clock(
                out,
                frequency,
                options.tolerance,
                options.dcor,
                options.define
            )
        #~ print "%.2f kHz" % (getDCOFreq(0, 0)/1e3)
    finally:
        if sys.exc_info()[:1]:              #if there is an exception pending
            jtagobj.verbose = 0             #do not write any more messages
        jtagobj.reset(1, 1)                 #reset and release target
        jtagobj.close()                     #Release communication port

if __name__ == '__main__':
    try:
        main()
    except SystemExit:
        raise                                           #let pass exit() calls
    except KeyboardInterrupt:
        if debug: raise                                 #show full trace in debug mode
        sys.stderr.write("User abort.\n")               #short messy in user mode
        sys.exit(1)                                     #set errorlevel for script usage
    except Exception, msg:                              #every Exception is caught and displayed
        if debug: raise                                 #show full trace in debug mode
        sys.stderr.write("\nAn error occoured:\n%s\n" % msg) #short messy in user mode
        sys.exit(1)                                     #set errorlevel for script usage
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
# Copyright 2013,2014 Music Technology Group - Universitat Pompeu Fabra
#
# This file is part of Dunya
#
# Dunya is free software: you can redistribute it and/or modify it under the
# terms of the GNU Affero General Public License as published by the Free Software
# Foundation (FSF), either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program.  If not, see http://www.gnu.org/licenses/
#
# If you are using this extractor please cite the following paper:
#
# Atlı, H. S., Uyar, B., Şentürk, S., Bozkurt, B., and Serra, X. (2014). Audio
# feature extraction for exploring Turkish makam music. In Proceedings of 3rd
# International Conference on Audio Technologies for Music and Media, Ankara,
# Turkey.
import os
import re
import json
import tempfile

import compmusic.extractors
from tomato.symbolic.scoreconverter import ScoreConverter

from docserver import util
from os.path import isfile, join
from compmusic import dunya
from settings import token
from compmusic.dunya import makam
dunya.set_token(token)

class Symbtr2Png(compmusic.extractors.ExtractorModule):
    _version = "0.2"
    _sourcetype = "symbtrtxt"
    _slug = "score"
    _output = {
            "xmlscore": {"extension": "xml", "mimetype": "application/xml"},
            "score": {"extension": "svg", "mimetype": "image/svg+xml", "parts": True},
    }

    def run(self, musicbrainzid, fpath):
        symbtrmu2 = util.docserver_get_symbtrmu2(musicbrainzid)
        print symbtrmu2
        symbtr = compmusic.dunya.makam.get_symbtr(musicbrainzid)
        fname = symbtr['name']
        finfo = fname.split('/')[-1]

        fp, tmpxml = tempfile.mkstemp(".xml")
        os.close(fp)

        tmply = tmpxml.replace(".xml",".ly")
        tmp_dir = tempfile.mkdtemp()

        # parameters
        render_metadata = True  # Add the metadata stored in MusicXML to Lilypond
        svg_paper_size = 'junior-legal'  # The paper size of the svg output pages

        # instantiate analyzer object
        scoreConverter = ScoreConverter()

        xml_output, ly_output, svg_output, txt_ly_mapping = scoreConverter.convert(
                    fpath, symbtrmu2, symbtr_name=finfo, mbid=musicbrainzid,
                    render_metadata=render_metadata, xml_out=tmpxml, ly_out=tmply,
                    svg_out=tmp_dir, svg_paper_size=svg_paper_size)

        ret = {'score': [], 'xmlscore': ''}

        musicxml = open(xml_output)
        ret['xmlscore'] = musicxml.read()
        musicxml.close()

        os.unlink(tmpxml)
        os.unlink(tmpxml.replace('.xml','.ly'))
        os.unlink(tmpxml.replace('.xml','.json'))

        files = [os.path.join(tmp_dir, f) for f in os.listdir(tmp_dir)]
        files = filter(os.path.isfile, files)
        files.sort(key=lambda x: os.path.getmtime(x))

        for f in files:
            if f.endswith('.svg'):
                svg_file = open(f)
                score = svg_file.read()
                ret['score'].append(score)
                svg_file.close()
                os.remove(f)
        os.rmdir(tmp_dir)
        return ret

<commit_msg>Undo remove unexistent file on svg generation<commit_after># -*- coding: utf-8 -*-
# Copyright 2013,2014 Music Technology Group - Universitat Pompeu Fabra
#
# This file is part of Dunya
#
# Dunya is free software: you can redistribute it and/or modify it under the
# terms of the GNU Affero General Public License as published by the Free Software
# Foundation (FSF), either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program.  If not, see http://www.gnu.org/licenses/
#
# If you are using this extractor please cite the following paper:
#
# Atlı, H. S., Uyar, B., Şentürk, S., Bozkurt, B., and Serra, X. (2014). Audio
# feature extraction for exploring Turkish makam music. In Proceedings of 3rd
# International Conference on Audio Technologies for Music and Media, Ankara,
# Turkey.
import os
import re
import json
import tempfile

import compmusic.extractors
from tomato.symbolic.scoreconverter import ScoreConverter

from docserver import util
from os.path import isfile, join
from compmusic import dunya
from settings import token
from compmusic.dunya import makam
dunya.set_token(token)

class Symbtr2Png(compmusic.extractors.ExtractorModule):
    _version = "0.2"
    _sourcetype = "symbtrtxt"
    _slug = "score"
    _output = {
            "xmlscore": {"extension": "xml", "mimetype": "application/xml"},
            "score": {"extension": "svg", "mimetype": "image/svg+xml", "parts": True},
    }

    def run(self, musicbrainzid, fpath):
        symbtrmu2 = util.docserver_get_symbtrmu2(musicbrainzid)
        print symbtrmu2
        symbtr = compmusic.dunya.makam.get_symbtr(musicbrainzid)
        fname = symbtr['name']
        finfo = fname.split('/')[-1]

        fp, tmpxml = tempfile.mkstemp(".xml")
        os.close(fp)

        tmply = tmpxml.replace(".xml",".ly")
        tmp_dir = tempfile.mkdtemp()

        # parameters
        render_metadata = True  # Add the metadata stored in MusicXML to Lilypond
        svg_paper_size = 'junior-legal'  # The paper size of the svg output pages

        # instantiate analyzer object
        scoreConverter = ScoreConverter()

        xml_output, ly_output, svg_output, txt_ly_mapping = scoreConverter.convert(
                    fpath, symbtrmu2, symbtr_name=finfo, mbid=musicbrainzid,
                    render_metadata=render_metadata, xml_out=tmpxml, ly_out=tmply,
                    svg_out=tmp_dir, svg_paper_size=svg_paper_size)

        ret = {'score': [], 'xmlscore': ''}

        musicxml = open(xml_output)
        ret['xmlscore'] = musicxml.read()
        musicxml.close()

        os.unlink(tmpxml)
        os.unlink(tmpxml.replace('.xml','.ly'))

        files = [os.path.join(tmp_dir, f) for f in os.listdir(tmp_dir)]
        files = filter(os.path.isfile, files)
        files.sort(key=lambda x: os.path.getmtime(x))

        for f in files:
            if f.endswith('.svg'):
                svg_file = open(f)
                score = svg_file.read()
                ret['score'].append(score)
                svg_file.close()
                os.remove(f)
        os.rmdir(tmp_dir)
        return ret

<|endoftext|>


<commit_before>import datetime
import numpy as np
import pandas as pd
from sklearn import preprocessing

from random_forest import RandomForestModel
from naive_bayes import NaiveBayesModel, BernoulliNaiveBayesModel

CATEGORICAL_VARS = ('DayOfWeek', 'PdDistrict', 'Address') #, 'Block',
                    # 'Junction_min', 'Junction_max')
TIME_SERIES_VARS = ('Date', 'Time', 'Dates')

def time_diff(start, end):
    dt_start = datetime.datetime.combine(datetime.date.today(), start)
    dt_end = datetime.datetime.combine(datetime.date.today(), end)
    return float((dt_end - dt_start).seconds) / (60 * 60)

def models():
    return [NaiveBayesModel(), BernoulliNaiveBayesModel()]

class KaggleDataModel(object):

    def feature_engineering(self, train_filename=None, test_filename=None):
        df = pd.read_csv(train_filename and train_filename or test_filename)

        temp = pd.DatetimeIndex(df['Dates'])
        df['Date'] = temp.date
        df['Time'] = temp.time
        df['Dates'] = pd.to_datetime(df['Dates'])

        # df['Block'] = 'Block of' in df['Address'] and df['Address'] or ''
        # df['Junction_min'] =  ' / ' in df['Address'] and \
        #         min(df['Address'].split(' / ')) or ''
        # df['Junction_max'] =  ' / ' in df['Address'] and \
        #         max(df['Address'].split(' / ')) or ''

        # There are df['Y'] == 90 in the test set too. In this case, we
        # guess the Y value by find others with the same road Address and
        # take an average. Where there are no other same address crimes we
        # just use 90 still
        # df_temp = df.copy(deep=True)
        # for n, row in df.iterrows():
        #     if row['Y'] == 90:
        #         same_addresses = df[df['Address'] == row['Address']]
        #         x_median = np.median(same_addresses['X'])
        #         y_median = np.median(same_addresses['Y'])
        #         df_temp['X'][n] = x_median
        #         df_temp['Y'][n] = y_median
        # df = df_temp

        if train_filename:
            encoders = {}
            for c in CATEGORICAL_VARS:
                all_choices = set(df[c])
                if c == 'Address':
                    test_choices = set(pd.read_csv(test_filename)['Address'])
                    all_choices = all_choices.union(test_choices)
                le = preprocessing.LabelEncoder()
                le.fit(tuple(all_choices))
                encoders[c] = le
            for t in TIME_SERIES_VARS:
                encoders[t] = min(df[t])
            self.encoders = encoders

        for c in CATEGORICAL_VARS:
            df[c] = self.encoders[c].transform(df[c])
        for v in TIME_SERIES_VARS:
            if v == 'Date':
                df[v] = df[v].apply(lambda t: (t - self.encoders[v]).days)
            elif v == 'Time':
                df[v] = df[v].apply(lambda t: time_diff(self.encoders[v], t))
            elif v == 'Dates':
                f = lambda t: (t - self.encoders[v]).total_seconds() / (60*60.)
                df[v] = df[v].apply(f)

        if train_filename:
            # Omit when df['Y'] == 90 - seems to be a NA value
            df = df[df['Y'] != 90]

            del df['Descript']
            del df['Resolution']

            # Ensure y value is first column of the result
            cols = ['Category'] + [x for x in df.columns if x != 'Category']
            df = df[cols]

        # Scaling coordinates so they are "prettier" to human-interpretation
        df['X'] = (df['X'] + 122) * 100
        df['Y'] = (df['Y'] -  37) * 100

        return df

    def load_training(self, training_filename):
        ''' For use with model_test_harness '''
        training = self.feature_engineering(training_filename, is_training=True)
        self.columns = list(training.ix[:,1:].columns)
        return training.ix[:,1:], training.ix[:,0]

    def trial_model(self):
        import lib_linear
        from sklearn.cross_validation import train_test_split

        Xs, y = sf_model.KaggleDataModel().load_training('train.csv')
        X_train, X_test, y_train, y_test = train_test_split(Xs, y,
                                                            test_size=0.25)
        y_hat = lib_linear.NaiveBayesModel().model_and_predict()
        print sum(y_hat == y_test) / float(len(y_hat))
<commit_msg>Repair bad coords where possible by finding the same street name in the same input file and substituting where appropriate<commit_after>import datetime
import numpy as np
import pandas as pd
from sklearn import preprocessing

from random_forest import RandomForestModel
from naive_bayes import NaiveBayesModel, BernoulliNaiveBayesModel

CATEGORICAL_VARS = ('DayOfWeek', 'PdDistrict', 'Address') #, 'Block',
                    # 'Junction_min', 'Junction_max')
TIME_SERIES_VARS = ('Date', 'Time', 'Dates')

def time_diff(start, end):
    dt_start = datetime.datetime.combine(datetime.date.today(), start)
    dt_end = datetime.datetime.combine(datetime.date.today(), end)
    return float((dt_end - dt_start).seconds) / (60 * 60)

def models():
    return [NaiveBayesModel(), BernoulliNaiveBayesModel()]

class KaggleDataModel(object):

    def feature_engineering(self, train_filename=None, test_filename=None):
        df = pd.read_csv(train_filename and train_filename or test_filename)

        temp = pd.DatetimeIndex(df['Dates'])
        df['Date'] = temp.date
        df['Time'] = temp.time
        df['Dates'] = pd.to_datetime(df['Dates'])

        # df['Block'] = 'Block of' in df['Address'] and df['Address'] or ''
        # df['Junction_min'] =  ' / ' in df['Address'] and \
        #         min(df['Address'].split(' / ')) or ''
        # df['Junction_max'] =  ' / ' in df['Address'] and \
        #         max(df['Address'].split(' / ')) or ''

        # There are df['Y'] == 90 in the test set too. In this case, we
        # guess the Y value by find others with the same road Address and
        # take an average. Where there are no other same address crimes we
        # just use 90 still
        df_temp = df.copy(deep=True)
        for n, row in df[df['Y'] == 90].iterrows():
            same_addresses = df[(df['Address'] == row['Address']) &
                                (df['Y'] != 90)]
            if same_addresses.empty:
                same_addresses = df[df['PdDistrict'] == row['PdDistrict']]
            x_median = np.median(same_addresses['X'])
            y_median = np.median(same_addresses['Y'])
            df_temp.set_value(n, 'X', x_median)
            df_temp.set_value(n, 'Y', y_median)
        df = df_temp

        if train_filename:
            encoders = {}
            for c in CATEGORICAL_VARS:
                all_choices = set(df[c])
                if c == 'Address':
                    test_choices = set(pd.read_csv(test_filename)['Address'])
                    all_choices = all_choices.union(test_choices)
                le = preprocessing.LabelEncoder()
                le.fit(tuple(all_choices))
                encoders[c] = le
            for t in TIME_SERIES_VARS:
                encoders[t] = min(df[t])
            self.encoders = encoders

        for c in CATEGORICAL_VARS:
            df[c] = self.encoders[c].transform(df[c])
        for v in TIME_SERIES_VARS:
            if v == 'Date':
                df[v] = df[v].apply(lambda t: (t - self.encoders[v]).days)
            elif v == 'Time':
                df[v] = df[v].apply(lambda t: time_diff(self.encoders[v], t))
            elif v == 'Dates':
                f = lambda t: (t - self.encoders[v]).total_seconds() / (60*60.)
                df[v] = df[v].apply(f)

        if train_filename:
            # Omit when df['Y'] == 90 - seems to be a NA value
            df = df[df['Y'] != 90]

            del df['Descript']
            del df['Resolution']

            # Ensure y value is first column of the result
            cols = ['Category'] + [x for x in df.columns if x != 'Category']
            df = df[cols]

        # Scaling coordinates so they are "prettier" to human-interpretation
        df['X'] = (df['X'] + 122) * 100
        df['Y'] = (df['Y'] -  37) * 100

        return df

    def load_training(self, training_filename):
        ''' For use with model_test_harness '''
        training = self.feature_engineering(training_filename, is_training=True)
        self.columns = list(training.ix[:,1:].columns)
        return training.ix[:,1:], training.ix[:,0]

    def trial_model(self):
        import lib_linear
        from sklearn.cross_validation import train_test_split

        Xs, y = sf_model.KaggleDataModel().load_training('train.csv')
        X_train, X_test, y_train, y_test = train_test_split(Xs, y,
                                                            test_size=0.25)
        y_hat = lib_linear.NaiveBayesModel().model_and_predict()
        print sum(y_hat == y_test) / float(len(y_hat))
<|endoftext|>


<commit_before>from toolchain import Recipe, shprint
from os.path import join, exists
import sh
import shutil

class FreetypeRecipe(Recipe):
    version = "2.5.5"
    url = "http://download.savannah.gnu.org/releases/freetype/freetype-{version}.tar.bz2"
    library = "objs/.libs/libfreetype.a"
    include_dir = "include"

    def build_arch(self, arch):
        build_env = arch.get_env()
        configure = sh.Command(join(self.build_dir, "configure"))
        shprint(configure,
                "CC={}".format(build_env["CC"]),
                "LD={}".format(build_env["LD"]),
                "CFLAGS={}".format(build_env["CFLAGS"]),
                "LDFLAGS={}".format(build_env["LDFLAGS"]),
                "--prefix=/",
                "--host={}".format(arch.triple),
                "--enable-static=yes",
                "--enable-shared=no")
        shprint(sh.make, "clean")
        shprint(sh.make)

recipe = FreetypeRecipe()

<commit_msg>freetype: fix include as they contain platform specific bits<commit_after>from toolchain import Recipe, shprint
from os.path import join, exists
import sh
import shutil

class FreetypeRecipe(Recipe):
    version = "2.5.5"
    url = "http://download.savannah.gnu.org/releases/freetype/freetype-{version}.tar.bz2"
    library = "objs/.libs/libfreetype.a"
    include_dir = ["include", ("builds/unix/ftconfig.h", "config/ftconfig.h")]
    include_per_arch = True

    def build_arch(self, arch):
        build_env = arch.get_env()
        configure = sh.Command(join(self.build_dir, "configure"))
        shprint(configure,
                "CC={}".format(build_env["CC"]),
                "LD={}".format(build_env["LD"]),
                "CFLAGS={}".format(build_env["CFLAGS"]),
                "LDFLAGS={}".format(build_env["LDFLAGS"]),
                "--prefix=/",
                "--host={}".format(arch.triple),
                "--enable-static=yes",
                "--enable-shared=no")
        shprint(sh.make, "clean")
        shprint(sh.make)

recipe = FreetypeRecipe()

<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

"""
kay.utils.datastructures
~~~~~~~~~~~~~~~~~~~~~~~~~

Various data structures.

:copyright: (c) 2009 by Kay Team, see AUTHORS for more details.
:copyright: (c) 2009 by the Zine Team, see AUTHORS for more details.
:license: BSD, see LICENSE for more details.

This file originally derives from Zine project.
"""

from itertools import izip, imap
from copy import deepcopy

class _Missing(object):

    def __reduce__(self):
        return 'missing'

    def __repr__(self):
        return 'no default'

missing = _Missing()
del _Missing

class ReadOnlyMultiMapping(object):
    """Provides a read only view to multiple mappings."""

    def __init__(self, *mappings):
        self.__dict__['mappings'] = mappings

    @property
    def mappings(self):
        """The tuple of mappings this multi mapping looks up."""
        return self.__dict__['mappings']

    def __getitem__(self, name):
        for mapping in self.mappings:
            if name in mapping:
                return mapping[name]
        raise KeyError(name)

    def get(self, name, default=None):
        """Return a key or the default value if no value exists."""
        try:
            return self[name]
        except KeyError:
            return default

    def __contains__(self, name):
        try:
            self[name]
        except KeyError:
            return False
        return True

    def _dict_method(name):
        def proxy(self):
            return getattr(self.as_dict(), name)()
        proxy.__name__ = name
        proxy.__doc__ = getattr(dict, name).__doc__
        return proxy

    __iter__ = iterkeys = _dict_method('iterkeys')
    itervalues = _dict_method('itervalues')
    iteritems = _dict_method('iteritems')
    keys = _dict_method('keys')
    values = _dict_method('values')
    items = _dict_method('items')
    __len__ = _dict_method('__len__')
    del _dict_method

    def as_dict(self):
        result = {}
        for mapping in reversed(self.mappings):
            result.update(mapping)
        return result

class _PickleProtocol2Sucks(object):
    """This class implements a dummy container that just eats up all
    the stuff appended to it.  It exists because pickle protocol 2 feeds
    stuff to the ordered dict with opcodes *before* `__setstate__` is
    called.  As a matter of fact no `_keys` is present on the dict that point
    and the unpickling crashes with an `AttributeError`.

    This behavior is confusing and yet undocumented, see `#4612`_.

    .. _#4612: http://bugs.python.org/issue4712
    """

    def append(self, value):
        pass

class OrderedDict(dict):
    """Simple ordered dict implementation.

    It's a dict subclass and provides some list functions.  The implementation
    of this class is inspired by the implementation of Babel but incorporates
    some ideas from the `ordereddict`_ and Django's ordered dict.

    The constructor and `update()` both accept iterables of tuples as well as
    mappings:

    >>> d = OrderedDict([('a', 'b'), ('c', 'd')])
    >>> d.update({'foo': 'bar'})
    >>> d
    OrderedDict([('a', 'b'), ('c', 'd'), ('foo', 'bar')])

    Keep in mind that when updating from dict-literals the order is not
    preserved as these dicts are unsorted!

    You can copy an OrderedDict like a dict by using the constructor, `copy.copy`
    or the `copy` method and make deep copies with `copy.deepcopy`:

    >>> from copy import copy, deepcopy
    >>> copy(d)
    OrderedDict([('a', 'b'), ('c', 'd'), ('foo', 'bar')])
    >>> d.copy()
    OrderedDict([('a', 'b'), ('c', 'd'), ('foo', 'bar')])
    >>> OrderedDict(d)
    OrderedDict([('a', 'b'), ('c', 'd'), ('foo', 'bar')])
    >>> d['spam'] = []
    >>> d2 = deepcopy(d)
    >>> d2['spam'].append('eggs')
    >>> d
    OrderedDict([('a', 'b'), ('c', 'd'), ('foo', 'bar'), ('spam', [])])
    >>> d2
    OrderedDict([('a', 'b'), ('c', 'd'), ('foo', 'bar'), ('spam', ['eggs'])])

    All iteration methods as well as `keys`, `values` and `items` return
    the values ordered by the the time the key-value pair is inserted:

    >>> d.keys()
    ['a', 'c', 'foo', 'spam']
    >>> d.values()
    ['b', 'd', 'bar', []]
    >>> d.items()
    [('a', 'b'), ('c', 'd'), ('foo', 'bar'), ('spam', [])]
    >>> list(d.iterkeys())
    ['a', 'c', 'foo', 'spam']
    >>> list(d.itervalues())
    ['b', 'd', 'bar', []]
    >>> list(d.iteritems())
    [('a', 'b'), ('c', 'd'), ('foo', 'bar'), ('spam', [])]

    Index based lookup is supported too by `byindex` which returns the
    key/value pair for an index:

    >>> d.byindex(2)
    ('foo', 'bar')

    You can reverse the OrderedDict as well:

    >>> d.reverse()
    >>> d
    OrderedDict([('spam', []), ('foo', 'bar'), ('c', 'd'), ('a', 'b')])

    And sort it like a list:

    >>> d.sort(key=lambda x: x[0].lower())
    >>> d
    OrderedDict([('a', 'b'), ('c', 'd'), ('foo', 'bar'), ('spam', [])])

    .. _proposal: http://thread.gmane.org/gmane.comp.python.devel/95316
    .. _ordereddict: http://www.xs4all.nl/~anthon/Python/ordereddict/
    """
    _keys = _PickleProtocol2Sucks()

    def __init__(self, *args, **kwargs):
        dict.__init__(self)
        self._keys = []
        self.update(*args, **kwargs)

    def __delitem__(self, key):
        dict.__delitem__(self, key)
        self._keys.remove(key)

    def __setitem__(self, key, item):
        if key not in self:
            self._keys.append(key)
        dict.__setitem__(self, key, item)

    def __deepcopy__(self, memo=None):
        if memo is None:
            memo = {}
        d = memo.get(id(self), missing)
        if d is not missing:
            return d
        memo[id(self)] = d = self.__class__()
        dict.__init__(d, deepcopy(self.items(), memo))
        d._keys = self._keys[:]
        return d

    def __getstate__(self):
        return {'items': dict(self), 'keys': self._keys}

    def __setstate__(self, d):
        self._keys = d['keys']
        dict.update(self, d['items'])

    def __reversed__(self):
        return reversed(self._keys)

    def __eq__(self, other):
        if isinstance(other, OrderedDict):
            if not dict.__eq__(self, other):
                return False
            return self.items() == other.items()
        return dict.__eq__(self, other)

    def __ne__(self, other):
        return not self.__eq__(other)

    def __cmp__(self, other):
        if isinstance(other, OrderedDict):
            return cmp(self.items(), other.items())
        elif isinstance(other, dict):
            return dict.__cmp__(self, other)
        return NotImplemented

    @classmethod
    def fromkeys(cls, iterable, default=None):
        return cls((key, default) for key in iterable)

    def clear(self):
        del self._keys[:]
        dict.clear(self)

    def copy(self):
        return self.__class__(self)

    def items(self):
        return zip(self._keys, self.values())

    def iteritems(self):
        return izip(self._keys, self.itervalues())

    def keys(self):
        return self._keys[:]

    def iterkeys(self):
        return iter(self._keys)

    def pop(self, key, default=missing):
        if default is missing:
            return dict.pop(self, key)
        elif key not in self:
            return default
        self._keys.remove(key)
        return dict.pop(self, key, default)

    def popitem(self, key):
        self._keys.remove(key)
        return dict.popitem(self, key)

    def setdefault(self, key, default=None):
        if key not in self:
            self._keys.append(key)
        dict.setdefault(self, key, default)

    def update(self, *args, **kwargs):
        sources = []
        if len(args) == 1:
            if hasattr(args[0], 'iteritems'):
                sources.append(args[0].iteritems())
            else:
                sources.append(iter(args[0]))
        elif args:
            raise TypeError('expected at most one positional argument')
        if kwargs:
            sources.append(kwargs.iteritems())
        for iterable in sources:
            for key, val in iterable:
                self[key] = val

    def values(self):
        return map(self.get, self._keys)

    def itervalues(self):
        return imap(self.get, self._keys)

    def index(self, item):
        return self._keys.index(item)

    def byindex(self, item):
        key = self._keys[item]
        return (key, dict.__getitem__(self, key))

    def reverse(self):
        self._keys.reverse()

    def sort(self, cmp=None, key=None, reverse=False):
        if key is not None:
            self._keys.sort(key=lambda k: key((k, self[k])))
        elif cmp is not None:
            self._keys.sort(lambda a, b: cmp((a, self[a]), (b, self[b])))
        else:
            self._keys.sort()
        if reverse:
            self._keys.reverse()

    def __repr__(self):
        return '%s(%r)' % (type(self).__name__, self.items())

    __copy__ = copy
    __iter__ = iterkeys
<commit_msg>Reindent kay/utils/datastructures.py.<commit_after># -*- coding: utf-8 -*-

"""
kay.utils.datastructures
~~~~~~~~~~~~~~~~~~~~~~~~~

Various data structures.

:copyright: (c) 2009 by Kay Team, see AUTHORS for more details.
:copyright: (c) 2009 by the Zine Team, see AUTHORS for more details.
:license: BSD, see LICENSE for more details.

This file originally derives from Zine project.
"""

from itertools import izip, imap
from copy import deepcopy

class _Missing(object):

  def __reduce__(self):
    return 'missing'

  def __repr__(self):
    return 'no default'

missing = _Missing()
del _Missing

class ReadOnlyMultiMapping(object):
  """Provides a read only view to multiple mappings."""

  def __init__(self, *mappings):
    self.__dict__['mappings'] = mappings

  @property
  def mappings(self):
    """The tuple of mappings this multi mapping looks up."""
    return self.__dict__['mappings']

  def __getitem__(self, name):
    for mapping in self.mappings:
      if name in mapping:
        return mapping[name]
    raise KeyError(name)

  def get(self, name, default=None):
    """Return a key or the default value if no value exists."""
    try:
      return self[name]
    except KeyError:
      return default

  def __contains__(self, name):
    try:
      self[name]
    except KeyError:
      return False
    return True

  def _dict_method(name):
    def proxy(self):
      return getattr(self.as_dict(), name)()
    proxy.__name__ = name
    proxy.__doc__ = getattr(dict, name).__doc__
    return proxy

  __iter__ = iterkeys = _dict_method('iterkeys')
  itervalues = _dict_method('itervalues')
  iteritems = _dict_method('iteritems')
  keys = _dict_method('keys')
  values = _dict_method('values')
  items = _dict_method('items')
  __len__ = _dict_method('__len__')
  del _dict_method

  def as_dict(self):
    result = {}
    for mapping in reversed(self.mappings):
      result.update(mapping)
    return result

class _PickleProtocol2Sucks(object):
  """This class implements a dummy container that just eats up all
  the stuff appended to it.  It exists because pickle protocol 2 feeds
  stuff to the ordered dict with opcodes *before* `__setstate__` is
  called.  As a matter of fact no `_keys` is present on the dict that point
  and the unpickling crashes with an `AttributeError`.

  This behavior is confusing and yet undocumented, see `#4612`_.

  .. _#4612: http://bugs.python.org/issue4712
  """

  def append(self, value):
    pass

class OrderedDict(dict):
  """Simple ordered dict implementation.

  It's a dict subclass and provides some list functions.  The implementation
  of this class is inspired by the implementation of Babel but incorporates
  some ideas from the `ordereddict`_ and Django's ordered dict.

  The constructor and `update()` both accept iterables of tuples as well as
  mappings:

  >>> d = OrderedDict([('a', 'b'), ('c', 'd')])
  >>> d.update({'foo': 'bar'})
  >>> d
  OrderedDict([('a', 'b'), ('c', 'd'), ('foo', 'bar')])

  Keep in mind that when updating from dict-literals the order is not
  preserved as these dicts are unsorted!

  You can copy an OrderedDict like a dict by using the constructor, `copy.copy`
  or the `copy` method and make deep copies with `copy.deepcopy`:

  >>> from copy import copy, deepcopy
  >>> copy(d)
  OrderedDict([('a', 'b'), ('c', 'd'), ('foo', 'bar')])
  >>> d.copy()
  OrderedDict([('a', 'b'), ('c', 'd'), ('foo', 'bar')])
  >>> OrderedDict(d)
  OrderedDict([('a', 'b'), ('c', 'd'), ('foo', 'bar')])
  >>> d['spam'] = []
  >>> d2 = deepcopy(d)
  >>> d2['spam'].append('eggs')
  >>> d
  OrderedDict([('a', 'b'), ('c', 'd'), ('foo', 'bar'), ('spam', [])])
  >>> d2
  OrderedDict([('a', 'b'), ('c', 'd'), ('foo', 'bar'), ('spam', ['eggs'])])

  All iteration methods as well as `keys`, `values` and `items` return
  the values ordered by the the time the key-value pair is inserted:

  >>> d.keys()
  ['a', 'c', 'foo', 'spam']
  >>> d.values()
  ['b', 'd', 'bar', []]
  >>> d.items()
  [('a', 'b'), ('c', 'd'), ('foo', 'bar'), ('spam', [])]
  >>> list(d.iterkeys())
  ['a', 'c', 'foo', 'spam']
  >>> list(d.itervalues())
  ['b', 'd', 'bar', []]
  >>> list(d.iteritems())
  [('a', 'b'), ('c', 'd'), ('foo', 'bar'), ('spam', [])]

  Index based lookup is supported too by `byindex` which returns the
  key/value pair for an index:

  >>> d.byindex(2)
  ('foo', 'bar')

  You can reverse the OrderedDict as well:

  >>> d.reverse()
  >>> d
  OrderedDict([('spam', []), ('foo', 'bar'), ('c', 'd'), ('a', 'b')])

  And sort it like a list:

  >>> d.sort(key=lambda x: x[0].lower())
  >>> d
  OrderedDict([('a', 'b'), ('c', 'd'), ('foo', 'bar'), ('spam', [])])

  .. _proposal: http://thread.gmane.org/gmane.comp.python.devel/95316
  .. _ordereddict: http://www.xs4all.nl/~anthon/Python/ordereddict/
  """
  _keys = _PickleProtocol2Sucks()

  def __init__(self, *args, **kwargs):
    dict.__init__(self)
    self._keys = []
    self.update(*args, **kwargs)

  def __delitem__(self, key):
    dict.__delitem__(self, key)
    self._keys.remove(key)

  def __setitem__(self, key, item):
    if key not in self:
      self._keys.append(key)
    dict.__setitem__(self, key, item)

  def __deepcopy__(self, memo=None):
    if memo is None:
      memo = {}
    d = memo.get(id(self), missing)
    if d is not missing:
      return d
    memo[id(self)] = d = self.__class__()
    dict.__init__(d, deepcopy(self.items(), memo))
    d._keys = self._keys[:]
    return d

  def __getstate__(self):
    return {'items': dict(self), 'keys': self._keys}

  def __setstate__(self, d):
    self._keys = d['keys']
    dict.update(self, d['items'])

  def __reversed__(self):
    return reversed(self._keys)

  def __eq__(self, other):
    if isinstance(other, OrderedDict):
      if not dict.__eq__(self, other):
        return False
      return self.items() == other.items()
    return dict.__eq__(self, other)

  def __ne__(self, other):
    return not self.__eq__(other)

  def __cmp__(self, other):
    if isinstance(other, OrderedDict):
      return cmp(self.items(), other.items())
    elif isinstance(other, dict):
      return dict.__cmp__(self, other)
    return NotImplemented

  @classmethod
  def fromkeys(cls, iterable, default=None):
    return cls((key, default) for key in iterable)

  def clear(self):
    del self._keys[:]
    dict.clear(self)

  def copy(self):
    return self.__class__(self)

  def items(self):
    return zip(self._keys, self.values())

  def iteritems(self):
    return izip(self._keys, self.itervalues())

  def keys(self):
    return self._keys[:]

  def iterkeys(self):
    return iter(self._keys)

  def pop(self, key, default=missing):
    if default is missing:
      return dict.pop(self, key)
    elif key not in self:
      return default
    self._keys.remove(key)
    return dict.pop(self, key, default)

  def popitem(self, key):
    self._keys.remove(key)
    return dict.popitem(self, key)

  def setdefault(self, key, default=None):
    if key not in self:
      self._keys.append(key)
    dict.setdefault(self, key, default)

  def update(self, *args, **kwargs):
    sources = []
    if len(args) == 1:
      if hasattr(args[0], 'iteritems'):
        sources.append(args[0].iteritems())
      else:
        sources.append(iter(args[0]))
    elif args:
      raise TypeError('expected at most one positional argument')
    if kwargs:
      sources.append(kwargs.iteritems())
    for iterable in sources:
      for key, val in iterable:
        self[key] = val

  def values(self):
    return map(self.get, self._keys)

  def itervalues(self):
    return imap(self.get, self._keys)

  def index(self, item):
    return self._keys.index(item)

  def byindex(self, item):
    key = self._keys[item]
    return (key, dict.__getitem__(self, key))

  def reverse(self):
    self._keys.reverse()

  def sort(self, cmp=None, key=None, reverse=False):
    if key is not None:
      self._keys.sort(key=lambda k: key((k, self[k])))
    elif cmp is not None:
      self._keys.sort(lambda a, b: cmp((a, self[a]), (b, self[b])))
    else:
      self._keys.sort()
    if reverse:
      self._keys.reverse()

  def __repr__(self):
    return '%s(%r)' % (type(self).__name__, self.items())

  __copy__ = copy
  __iter__ = iterkeys
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
#  _  __
# | |/ /___ ___ _ __  ___ _ _ ®
# | ' </ -_) -_) '_ \/ -_) '_|
# |_|\_\___\___| .__/\___|_|
#              |_|
#
# Keeper Commander
# Copyright 2020 Keeper Security Inc.
# Contact: ops@keepersecurity.com
#

__version__ = '4.69'
<commit_msg>Bumping release version to 4.70<commit_after># -*- coding: utf-8 -*-
#  _  __
# | |/ /___ ___ _ __  ___ _ _ ®
# | ' </ -_) -_) '_ \/ -_) '_|
# |_|\_\___\___| .__/\___|_|
#              |_|
#
# Keeper Commander
# Copyright 2020 Keeper Security Inc.
# Contact: ops@keepersecurity.com
#

__version__ = '4.70'
<|endoftext|>