<commit_before>import os
import sys
import subprocess
from random import random
import tempfile

class SVMLightTreeKernels:
    """
    Base class for interaction with Alessandro Moschitti's Tree Kernels in SVM Light
    """

    def __init__(self, directory, model_path=tempfile.NamedTemporaryFile().name, use_tree_kernel=True):
        self.directory = directory
        """the directory where the executables svm_classify and svm_learn are
        located"""

        executables_extension = '' if sys.platform.startswith('linux') or sys.platform.startswith('darwin') else '.exe'
        self.svm_learn_call = os.path.join(self.directory, ('svm_learn' + executables_extension))
        self.svm_classify_call = os.path.join(self.directory, ('svm_classify' + executables_extension))

        self.model_path = model_path
        """the model (path) to read from / write to"""

        self.use_tree_kernel = use_tree_kernel
        """whether to use tree kernels or not"""

    def create_input_file(self, dataset, mode, features, undersampling=0.4, minority_class=-1, file=None):
        string = ''

        if mode == 'train':
            for edge in dataset.edges():
                if edge.target == minority_class:
                    prob = random()
                    if prob < undersampling:
                        string += str(edge.target)
                        if self.use_tree_kernel:
                            string += ' |BT| '
                            string += edge.part.sentence_parse_trees[edge.sentence_id]
                            string += ' |ET|'
                        values = set(features.values())
                        for key in sorted(edge.features.keys()):
                            if key in values:
                                value = edge.features[key]
                                string += ' ' + str(key) + ':' + str(value)
                        string += '\n'
                else:
                    string += str(edge.target)
                    if self.use_tree_kernel:
                        string += ' |BT| '
                        string += edge.part.sentence_parse_trees[edge.sentence_id]
                        string += ' |ET|'
                    values = set(features.values())
                    for key in sorted(edge.features.keys()):
                        if key in values:
                            value = edge.features[key]
                            string += ' ' + str(key) + ':' + str(value)
                    string += '\n'

        elif mode == 'test':
            for edge in dataset.edges():
                string += str(edge.target)
                if self.use_tree_kernel:
                    string += ' |BT| '
                    string += edge.part.sentence_parse_trees[edge.sentence_id]
                    string += ' |ET|'
                values = set(features.values())
                for key in sorted(edge.features.keys()):
                    if key in values:
                        value = edge.features[key]
                        string += ' ' + str(key) + ':' + str(value)
                string += '\n'

        elif mode == 'predict':
            for edge in dataset.edges():
                string += '?'
                if self.use_tree_kernel:
                    string += ' |BT| '
                    string += edge.part.sentence_parse_trees[edge.sentence_id]
                    string += ' |ET|'
                for key in sorted(edge.features.keys()):
                    if key in features.values():
                        value = edge.features[key]
                        string += ' ' + str(key) + ':' + str(value)
                string += '\n'

        if file is None:
            file = os.path.join(self.directory, mode)

        with open(file, 'w', encoding='utf-8') as f:
            f.write(string)

    def learn(self, file=None, c=0.5):
        if file is None:
            file = os.path.join(self.directory, 'train')

        if self.use_tree_kernel:
            subprocess.call([
                self.svm_learn_call,
                '-v', '0',
                '-t', '5',
                '-T', '1',
                '-W', 'S',
                '-V', 'S',
                '-C', '+',
                '-c', str(c),
                file,
                self.model_path
            ])

        else:
            subprocess.call([
                self.svm_learn_call,
                '-c', str(c),
                '-v', '0',
                file,
                self.model_path
            ])

    def tag(self, file=None, mode='predict', output=None):
        if file is None:
            file = os.path.join(self.directory, mode)
        if output is None:
            output = os.path.join(self.directory, 'predictions')

        call = [
            self.svm_classify_call,
            '-v', '0',
            file,
            self.model_path,
            output
        ]
        exitcode = subprocess.call(call)

        if exitcode != 0:
            raise Exception("Error when tagging: " + ' '.join(call))

    def read_predictions(self, dataset, predictions=None):
        if predictions is None:
            predictions = os.path.join(self.directory, 'predictions')
        values = []
        with open(predictions) as file:
            for line in file:
                if float(line.strip()) > -0.1:
                    values.append(1)
                else:
                    values.append(-1)
        for index, edge in enumerate(dataset.edges()):
            edge.target = values[index]
        dataset.form_predicted_relations()
<commit_msg>make the flow of information more explicit (not assumming implicit files)<commit_after>import os
import sys
import subprocess
from random import random
import tempfile

class SVMLightTreeKernels:
    """
    Base class for interaction with Alessandro Moschitti's Tree Kernels in SVM Light
    """

    def __init__(self, directory, model_path=tempfile.NamedTemporaryFile().name, use_tree_kernel=True):
        self.directory = directory
        """the directory where the executables svm_classify and svm_learn are
        located"""

        executables_extension = '' if sys.platform.startswith('linux') or sys.platform.startswith('darwin') else '.exe'
        self.svm_learn_call = os.path.join(self.directory, ('svm_learn' + executables_extension))
        self.svm_classify_call = os.path.join(self.directory, ('svm_classify' + executables_extension))

        self.model_path = model_path
        """the model (path) to read from / write to"""

        self.use_tree_kernel = use_tree_kernel
        """whether to use tree kernels or not"""

    def create_input_file(self, dataset, mode, features, undersampling=0.4, minority_class=-1):
        string = ''

        if mode == 'train':
            for edge in dataset.edges():
                if edge.target == minority_class:
                    prob = random()
                    if prob < undersampling:
                        string += str(edge.target)
                        if self.use_tree_kernel:
                            string += ' |BT| '
                            string += edge.part.sentence_parse_trees[edge.sentence_id]
                            string += ' |ET|'
                        values = set(features.values())
                        for key in sorted(edge.features.keys()):
                            if key in values:
                                value = edge.features[key]
                                string += ' ' + str(key) + ':' + str(value)
                        string += '\n'
                else:
                    string += str(edge.target)
                    if self.use_tree_kernel:
                        string += ' |BT| '
                        string += edge.part.sentence_parse_trees[edge.sentence_id]
                        string += ' |ET|'
                    values = set(features.values())
                    for key in sorted(edge.features.keys()):
                        if key in values:
                            value = edge.features[key]
                            string += ' ' + str(key) + ':' + str(value)
                    string += '\n'

        elif mode == 'test':
            for edge in dataset.edges():
                string += str(edge.target)
                if self.use_tree_kernel:
                    string += ' |BT| '
                    string += edge.part.sentence_parse_trees[edge.sentence_id]
                    string += ' |ET|'
                values = set(features.values())
                for key in sorted(edge.features.keys()):
                    if key in values:
                        value = edge.features[key]
                        string += ' ' + str(key) + ':' + str(value)
                string += '\n'

        elif mode == 'predict':
            for edge in dataset.edges():
                string += '?'
                if self.use_tree_kernel:
                    string += ' |BT| '
                    string += edge.part.sentence_parse_trees[edge.sentence_id]
                    string += ' |ET|'
                for key in sorted(edge.features.keys()):
                    if key in features.values():
                        value = edge.features[key]
                        string += ' ' + str(key) + ':' + str(value)
                string += '\n'

        instancesfile = tempfile.NamedTemporaryFile()
        instancesfile.write(string)

        return instancesfile.name

    def learn(self, instancesfile_path, c=0.5):

        if self.use_tree_kernel:
            subprocess.call([
                self.svm_learn_call,
                '-v', '0',
                '-t', '5',
                '-T', '1',
                '-W', 'S',
                '-V', 'S',
                '-C', '+',
                '-c', str(c),
                instancesfile_path,
                self.model_path
            ])

        else:
            subprocess.call([
                self.svm_learn_call,
                '-c', str(c),
                '-v', '0',
                instancesfile_path,
                self.model_path
            ])

    def tag(self, instancesfile_path):

        predictionsfile_path = tempfile.NamedTemporaryFile().name

        call = [
            self.svm_classify_call,
            '-v', '0',
            instancesfile_path,
            self.model_path,
            predictionsfile_path
        ]
        exitcode = subprocess.call(call)

        if exitcode != 0:
            raise Exception("Error when tagging: " + ' '.join(call))

        return predictionsfile_path

    def read_predictions(self, dataset, predictionsfile_path):

        values = []
        with open(predictionsfile_path) as file:
            for line in file:
                if float(line.strip()) > -0.1:
                    values.append(1)
                else:
                    values.append(-1)
        for index, edge in enumerate(dataset.edges()):
            edge.target = values[index]
        dataset.form_predicted_relations()
<|endoftext|>


<commit_before># Copyright 2016 Dravetech AB. All rights reserved.
#
# The contents of this file are 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.

"""napalm-extreme-netiron package."""
# from napalm_netiron.netiron import NetironDriver
<commit_msg>fix error with driver<commit_after># Copyright 2016 Dravetech AB. All rights reserved.
#
# The contents of this file are 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.

"""napalm-extreme-netiron package."""
from napalm_netiron.netiron import NetironDriver
<|endoftext|>


<commit_before># Copyright 2009 Shikhar Bhushan
#
# 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 threading import Event, Lock
from uuid import uuid1
from weakref import WeakValueDictionary

from ncclient.content import TreeBuilder
from ncclient.content import qualify as _
from ncclient.content import unqualify as __
from ncclient.glue import Listener

from . import logger
from reply import RPCReply

# Cisco does not include message-id attribute in <rpc-reply> in case of an error.
# This is messed up however we have to deal with it.
# So essentially, there can be only one operation at a time if we are talking to
# a Cisco device.

class RPC(object):

    def __init__(self, session, async=False):
        if session.is_remote_cisco and async:
            raise UserWarning('Asynchronous mode not supported for Cisco devices')
        self._session = session
        self._async = async
        self._id = uuid1().urn
        self._listener = RPCReplyListener(session)
        self._listener.register(self._id, self)
        self._reply = None
        self._reply_event = Event()

    def _build(self, op, encoding='utf-8'):
        if isinstance(op, dict):
            return self.build_from_spec(self._id, op, encoding)
        else:
            return self.build_from_string(self._id, op, encoding)

    def _request(self, op):
        req = self._build(op)
        self._session.send(req)
        if self._async:
            return self._reply_event
        else:
            self._reply_event.wait()
            self._reply.parse()
            return self._reply

    def _set_reply(self, raw):
        self._reply = RPCReply(raw)

    def _set_reply_event(self):
        self._reply_event.set()

    def _delivery_hook(self):
        'For subclasses'
        pass

    def deliver(self, raw):
        self._set_reply(raw)
        self._delivery_hook()
        self._set_reply_event()

    @property
    def has_reply(self):
        return self._reply_event.isSet()

    @property
    def reply(self):
        return self._reply

    @property
    def is_async(self):
        return self._async

    @property
    def id(self):
        return self._id

    @property
    def session(self):
        return self._session

    @property
    def reply_event(self):
        return self._reply_event

    @staticmethod
    def build_from_spec(msgid, opspec, encoding='utf-8'):
        "TODO: docstring"
        spec = {
            'tag': _('rpc'),
            'attributes': {'message-id': msgid},
            'children': opspec
            }
        return TreeBuilder(spec).to_string(encoding)

    @staticmethod
    def build_from_string(msgid, opstr, encoding='utf-8'):
        "TODO: docstring"
        decl = '<?xml version="1.0" encoding="%s"?>' % encoding
        doc = (u'<rpc message-id="%s" xmlns="%s">%s</rpc>' %
               (msgid, BASE_NS, opstr)).encode(encoding)
        return '%s%s' % (decl, doc)

class RPCReplyListener(Listener):

    # TODO - determine if need locking

    # one instance per session
    def __new__(cls, session):
        instance = session.get_listener_instance(cls)
        if instance is None:
            instance = object.__new__(cls)
            instance._id2rpc = WeakValueDictionary()
            instance._cisco = session.is_remote_cisco
            instance._errback = None
            session.add_listener(instance)
        return instance

    def __str__(self):
        return 'RPCReplyListener'

    def set_errback(self, errback):
        self._errback = errback

    def register(self, id, rpc):
        self._id2rpc[id] = rpc

    def callback(self, root, raw):
        tag, attrs = root
        if __(tag) != 'rpc-reply':
            return
        rpc = None
        for key in attrs:
            if __(key) == 'message-id':
                id = attrs[key]
                try:
                    rpc = self._id2rpc.pop(id)
                except KeyError:
                    logger.warning('[RPCReplyListener.callback] no object '
                                   + 'registered for message-id: [%s]' % id)
                except Exception as e:
                    logger.debug('[RPCReplyListener.callback] error - %r' % e)
                break
        else:
            if self._cisco:
                assert(len(self._id2rpc) == 1)
                rpc = self._id2rpc.values()[0]
                self._id2rpc.clear()
            else:
                logger.warning('<rpc-reply> without message-id received: %s' % raw)
        logger.debug('[RPCReplyListener.callback] delivering to %r' % rpc)
        rpc.deliver(raw)

    def errback(self, err):
        if self._errback is not None:
            self._errback(err)
<commit_msg>a vestigial bit excised<commit_after># Copyright 2009 Shikhar Bhushan
#
# 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 threading import Event, Lock
from uuid import uuid1
from weakref import WeakValueDictionary

from ncclient.content import TreeBuilder
from ncclient.content import qualify as _
from ncclient.content import unqualify as __
from ncclient.glue import Listener

from . import logger
from reply import RPCReply

# Cisco does not include message-id attribute in <rpc-reply> in case of an error.
# This is messed up however we have to deal with it.
# So essentially, there can be only one operation at a time if we are talking to
# a Cisco device.

class RPC(object):

    def __init__(self, session, async=False):
        if session.is_remote_cisco and async:
            raise UserWarning('Asynchronous mode not supported for Cisco devices')
        self._session = session
        self._async = async
        self._id = uuid1().urn
        self._listener = RPCReplyListener(session)
        self._listener.register(self._id, self)
        self._reply = None
        self._reply_event = Event()

    def _build(self, op, encoding='utf-8'):
        if isinstance(op, dict):
            return self.build_from_spec(self._id, op, encoding)
        else:
            return self.build_from_string(self._id, op, encoding)

    def _request(self, op):
        req = self._build(op)
        self._session.send(req)
        if self._async:
            return self._reply_event
        else:
            self._reply_event.wait()
            self._reply.parse()
            return self._reply

    def _delivery_hook(self):
        'For subclasses'
        pass

    def deliver(self, raw):
        self._reply = RPCReply(raw)
        self._delivery_hook()
        self._reply_event.set()

    @property
    def has_reply(self):
        return self._reply_event.isSet()

    @property
    def reply(self):
        return self._reply

    @property
    def is_async(self):
        return self._async

    @property
    def id(self):
        return self._id

    @property
    def session(self):
        return self._session

    @property
    def reply_event(self):
        return self._reply_event

    @staticmethod
    def build_from_spec(msgid, opspec, encoding='utf-8'):
        "TODO: docstring"
        spec = {
            'tag': _('rpc'),
            'attributes': {'message-id': msgid},
            'children': opspec
            }
        return TreeBuilder(spec).to_string(encoding)

    @staticmethod
    def build_from_string(msgid, opstr, encoding='utf-8'):
        "TODO: docstring"
        decl = '<?xml version="1.0" encoding="%s"?>' % encoding
        doc = (u'<rpc message-id="%s" xmlns="%s">%s</rpc>' %
               (msgid, BASE_NS, opstr)).encode(encoding)
        return '%s%s' % (decl, doc)

class RPCReplyListener(Listener):

    # TODO - determine if need locking

    # one instance per session
    def __new__(cls, session):
        instance = session.get_listener_instance(cls)
        if instance is None:
            instance = object.__new__(cls)
            instance._id2rpc = WeakValueDictionary()
            instance._cisco = session.is_remote_cisco
            instance._errback = None
            session.add_listener(instance)
        return instance

    def __str__(self):
        return 'RPCReplyListener'

    def set_errback(self, errback):
        self._errback = errback

    def register(self, id, rpc):
        self._id2rpc[id] = rpc

    def callback(self, root, raw):
        tag, attrs = root
        if __(tag) != 'rpc-reply':
            return
        rpc = None
        for key in attrs:
            if __(key) == 'message-id':
                id = attrs[key]
                try:
                    rpc = self._id2rpc.pop(id)
                except KeyError:
                    logger.warning('[RPCReplyListener.callback] no object '
                                   + 'registered for message-id: [%s]' % id)
                except Exception as e:
                    logger.debug('[RPCReplyListener.callback] error - %r' % e)
                break
        else:
            if self._cisco:
                assert(len(self._id2rpc) == 1)
                rpc = self._id2rpc.values()[0]
                self._id2rpc.clear()
            else:
                logger.warning('<rpc-reply> without message-id received: %s' % raw)
        logger.debug('[RPCReplyListener.callback] delivering to %r' % rpc)
        rpc.deliver(raw)

    def errback(self, err):
        if self._errback is not None:
            self._errback(err)
<|endoftext|>


<commit_before>import numpy as np
from blaze import *
from bokeh.plotting import *
from bokeh.objects import HoverTool, ColumnDataSource
from bokeh.embed import *
from bokeh.resources import Resources
from collections import OrderedDict

from toolz import concat
import itertools

"""
Run this script AFTER you have done make html in the same directory you run make html.
It should automatically put the graph where it needs to be.
"""

capabilities = {
    'Streaming Python':{
        'Scalar Expressions':True,
        'Reductions':True,
        'Selections':True,
        'Split-Apply-Combine':True,
        'Join':True,
        'Python Functions':True,
        'Indices':False,
        'Column Store': False,
        'Bigger-than-memory data': "Using Streaming",
    },
    'Pandas':{
        'Scalar Expressions':True,
        'Reductions':True,
        'Selections':True,
        'Split-Apply-Combine':True,
        'Join':True,
        'Python Functions':True,
        'Indices':False,
        'Column Store': True,
        'Bigger-than-memory data': False,
    },
    'SQL':{
        'Scalar Expressions':"Generally yes, but math is limited in SQLite",
        'Reductions':True,
        'Selections':True,
        'Split-Apply-Combine':True,
        'Join':True,
        'Python Functions':False,
        'Indices':True,
        'Column Store': False,
        'Bigger-than-memory data': True,
    },
    'PySpark':{
        'Scalar Expressions':True,
        'Reductions':True,
        'Selections':True,
        'Split-Apply-Combine':True,
        'Join':True,
        'Python Functions':True,
        'Indices':False,
        'Column Store': False,
        'Bigger-than-memory data': True,
    },
    'MongoDB':{
        'Scalar Expressions':False,
        'Reductions':True,
        'Selections':"Limited due to lack of scalar expressions",
        'Split-Apply-Combine':"Limited",
        'Join':False,
        'Python Functions':False,
        'Indices':True,
        'Column Store': False,
        'Bigger-than-memory data': True,
    },
    'PyTables':{
        'Scalar Expressions': "Using NumExpr",
        'Reductions': True,
        'Selections': "Using NumExpr",
        'Split-Apply-Combine':"Using Streaming Python",
        'Join':False,
        'Python Functions':"Using streaming Python",
        'Indices':True,
        'Column Store': False,
        'Bigger-than-memory data': "With fast compressed access",
    },
    'BColz':{
        'Scalar Expressions':"Using chunked NumPy",
        'Reductions':"Using chunked NumPy",
        'Selections':"Using NumExpr",
        'Split-Apply-Combine':"Using streaming Python",
        'Join':False,
        'Python Functions': "Using streaming Python",
        'Indices':False,
        'Column Store': True,
        'Bigger-than-memory data': "With fast compressed access",
    }
}

colormap = {True: "#5EDA9E", False: "#FFFFFF"}

backends = ['Streaming Python', 'Pandas', 'PySpark', 'SQL', 'PyTables', 'BColz',
            'MongoDB']
operations = ['Scalar Expressions', 'Selections', 'Reductions',
              'Split-Apply-Combine', 'Join', 'Python Functions', 'Indices',
              'Column Store', 'Bigger-than-memory data']

statuses = [capabilities[backend][op] for backend in backends
                                      for op in operations]

x, y = zip(*itertools.product(backends, operations))
x = list(x)
y = list(y)

colors = [colormap[1] if value else colormap[0] for value in statuses]

reset_output()

output_file('build/html/capabilities.html', mode='cdn')

source = ColumnDataSource(
    data=dict(
        xname=x,
        yname=y,
        statuses=statuses,
        colors=colors,
    )
)

figure()

rect('xname', 'yname', 0.99, 0.99, source=source,
     x_range=backends, y_range=list(reversed(operations)),
     x_axis_location="above",
     color='colors', line_color=None,
     tools="hover,previewsave", title="Blaze Capabilities by Backend",
     plot_width=800, plot_height=600)

grid().grid_line_color = None
axis().axis_line_color = None
axis().major_tick_line_color = None
axis().major_label_text_font_size = "5pt"
axis().major_label_standoff = 0

hover = [t for t in curplot().tools if isinstance(t, HoverTool)][0]
hover.tooltips = OrderedDict([
    ('names', '@yname, @xname'),
    ('status', '@statuses'),
])

save()      # show the plot'
<commit_msg>docs: capabilities use the term SQL style indices<commit_after>import numpy as np
from blaze import *
from bokeh.plotting import *
from bokeh.objects import HoverTool, ColumnDataSource
from bokeh.embed import *
from bokeh.resources import Resources
from collections import OrderedDict

from toolz import concat
import itertools

"""
Run this script AFTER you have done make html in the same directory you run make html.
It should automatically put the graph where it needs to be.
"""

capabilities = {
    'Streaming Python':{
        'Scalar Expressions':True,
        'Reductions':True,
        'Selections':True,
        'Split-Apply-Combine':True,
        'Join':True,
        'Python Functions':True,
        'SQL Style Indices':False,
        'Column Store': False,
        'Bigger-than-memory data': "Using Streaming",
    },
    'Pandas':{
        'Scalar Expressions':True,
        'Reductions':True,
        'Selections':True,
        'Split-Apply-Combine':True,
        'Join':True,
        'Python Functions':True,
        'SQL Style Indices':False,
        'Column Store': True,
        'Bigger-than-memory data': False,
    },
    'SQL':{
        'Scalar Expressions':"Generally yes, but math is limited in SQLite",
        'Reductions':True,
        'Selections':True,
        'Split-Apply-Combine':True,
        'Join':True,
        'Python Functions':False,
        'SQL Style Indices':True,
        'Column Store': False,
        'Bigger-than-memory data': True,
    },
    'PySpark':{
        'Scalar Expressions':True,
        'Reductions':True,
        'Selections':True,
        'Split-Apply-Combine':True,
        'Join':True,
        'Python Functions':True,
        'SQL Style Indices':False,
        'Column Store': False,
        'Bigger-than-memory data': True,
    },
    'MongoDB':{
        'Scalar Expressions':False,
        'Reductions':True,
        'Selections':"Limited due to lack of scalar expressions",
        'Split-Apply-Combine':"Limited",
        'Join':False,
        'Python Functions':False,
        'SQL Style Indices':True,
        'Column Store': False,
        'Bigger-than-memory data': True,
    },
    'PyTables':{
        'Scalar Expressions': "Using NumExpr",
        'Reductions': True,
        'Selections': "Using NumExpr",
        'Split-Apply-Combine':"Using Streaming Python",
        'Join':False,
        'Python Functions':"Using streaming Python",
        'SQL Style Indices':True,
        'Column Store': False,
        'Bigger-than-memory data': "With fast compressed access",
    },
    'BColz':{
        'Scalar Expressions':"Using chunked NumPy",
        'Reductions':"Using chunked NumPy",
        'Selections':"Using NumExpr",
        'Split-Apply-Combine':"Using streaming Python",
        'Join':False,
        'Python Functions': "Using streaming Python",
        'SQL Style Indices':False,
        'Column Store': True,
        'Bigger-than-memory data': "With fast compressed access",
    }
}

colormap = {True: "#5EDA9E", False: "#FFFFFF"}

backends = ['Streaming Python', 'Pandas', 'PySpark', 'SQL', 'PyTables', 'BColz',
            'MongoDB']
operations = ['Scalar Expressions', 'Selections', 'Reductions',
              'Split-Apply-Combine', 'Join', 'Python Functions', 'SQL Style Indices',
              'Column Store', 'Bigger-than-memory data']

statuses = [capabilities[backend][op] for backend in backends
                                      for op in operations]

x, y = zip(*itertools.product(backends, operations))
x = list(x)
y = list(y)

colors = [colormap[1] if value else colormap[0] for value in statuses]

reset_output()

output_file('build/html/capabilities.html', mode='cdn')

source = ColumnDataSource(
    data=dict(
        xname=x,
        yname=y,
        statuses=statuses,
        colors=colors,
    )
)

figure()

rect('xname', 'yname', 0.99, 0.99, source=source,
     x_range=backends, y_range=list(reversed(operations)),
     x_axis_location="above",
     color='colors', line_color=None,
     tools="hover,previewsave", title="Blaze Capabilities by Backend",
     plot_width=800, plot_height=600)

grid().grid_line_color = None
axis().axis_line_color = None
axis().major_tick_line_color = None
axis().major_label_text_font_size = "5pt"
axis().major_label_standoff = 0

hover = [t for t in curplot().tools if isinstance(t, HoverTool)][0]
hover.tooltips = OrderedDict([
    ('names', '@yname, @xname'),
    ('status', '@statuses'),
])

save()      # show the plot'
<|endoftext|>


<commit_before>"""
Pagination serializers determine the structure of the output that should
be used for paginated responses.
"""
from __future__ import unicode_literals
from rest_framework import serializers
from rest_framework.templatetags.rest_framework import replace_query_param

class NextPageField(serializers.Field):
    """
    Field that returns a link to the next page in paginated results.
    """
    page_field = 'page'

    def to_native(self, value):
        if not value.has_next():
            return None
        page = value.next_page_number()
        request = self.context.get('request')
        url = request and request.build_absolute_uri() or ''
        return replace_query_param(url, self.page_field, page)

class PreviousPageField(serializers.Field):
    """
    Field that returns a link to the previous page in paginated results.
    """
    page_field = 'page'

    def to_native(self, value):
        if not value.has_previous():
            return None
        page = value.previous_page_number()
        request = self.context.get('request')
        url = request and request.build_absolute_uri() or ''
        return replace_query_param(url, self.page_field, page)

class DefaultObjectSerializer(serializers.Field):
    """
    If no object serializer is specified, then this serializer will be applied
    as the default.
    """

    def __init__(self, source=None, many=None, context=None):
        # Note: Swallow context and many kwargs - only required for
        # eg. ModelSerializer.
        super(DefaultObjectSerializer, self).__init__(source=source)

class PaginationSerializerOptions(serializers.SerializerOptions):
    """
    An object that stores the options that may be provided to a
    pagination serializer by using the inner `Meta` class.

    Accessible on the instance as `serializer.opts`.
    """
    def __init__(self, meta):
        super(PaginationSerializerOptions, self).__init__(meta)
        self.object_serializer_class = getattr(meta, 'object_serializer_class',
                                               DefaultObjectSerializer)

class BasePaginationSerializer(serializers.Serializer):
    """
    A base class for pagination serializers to inherit from,
    to make implementing custom serializers more easy.
    """
    _options_class = PaginationSerializerOptions
    results_field = 'results'

    def __init__(self, *args, **kwargs):
        """
        Override init to add in the object serializer field on-the-fly.
        """
        super(BasePaginationSerializer, self).__init__(*args, **kwargs)
        results_field = self.results_field
        object_serializer = self.opts.object_serializer_class

        if 'context' in kwargs:
            context_kwarg = {'context': kwargs['context']}
        else:
            context_kwarg = {}

        print object_serializer
        self.fields[results_field] = object_serializer(source='object_list',
                                                       many=True,
                                                       **context_kwarg)

class PaginationSerializer(BasePaginationSerializer):
    """
    A default implementation of a pagination serializer.
    """
    count = serializers.Field(source='paginator.count')
    next = NextPageField(source='*')
    previous = PreviousPageField(source='*')
<commit_msg>remove rogue print<commit_after>"""
Pagination serializers determine the structure of the output that should
be used for paginated responses.
"""
from __future__ import unicode_literals
from rest_framework import serializers
from rest_framework.templatetags.rest_framework import replace_query_param

class NextPageField(serializers.Field):
    """
    Field that returns a link to the next page in paginated results.
    """
    page_field = 'page'

    def to_native(self, value):
        if not value.has_next():
            return None
        page = value.next_page_number()
        request = self.context.get('request')
        url = request and request.build_absolute_uri() or ''
        return replace_query_param(url, self.page_field, page)

class PreviousPageField(serializers.Field):
    """
    Field that returns a link to the previous page in paginated results.
    """
    page_field = 'page'

    def to_native(self, value):
        if not value.has_previous():
            return None
        page = value.previous_page_number()
        request = self.context.get('request')
        url = request and request.build_absolute_uri() or ''
        return replace_query_param(url, self.page_field, page)

class DefaultObjectSerializer(serializers.Field):
    """
    If no object serializer is specified, then this serializer will be applied
    as the default.
    """

    def __init__(self, source=None, many=None, context=None):
        # Note: Swallow context and many kwargs - only required for
        # eg. ModelSerializer.
        super(DefaultObjectSerializer, self).__init__(source=source)

class PaginationSerializerOptions(serializers.SerializerOptions):
    """
    An object that stores the options that may be provided to a
    pagination serializer by using the inner `Meta` class.

    Accessible on the instance as `serializer.opts`.
    """
    def __init__(self, meta):
        super(PaginationSerializerOptions, self).__init__(meta)
        self.object_serializer_class = getattr(meta, 'object_serializer_class',
                                               DefaultObjectSerializer)

class BasePaginationSerializer(serializers.Serializer):
    """
    A base class for pagination serializers to inherit from,
    to make implementing custom serializers more easy.
    """
    _options_class = PaginationSerializerOptions
    results_field = 'results'

    def __init__(self, *args, **kwargs):
        """
        Override init to add in the object serializer field on-the-fly.
        """
        super(BasePaginationSerializer, self).__init__(*args, **kwargs)
        results_field = self.results_field
        object_serializer = self.opts.object_serializer_class

        if 'context' in kwargs:
            context_kwarg = {'context': kwargs['context']}
        else:
            context_kwarg = {}

        self.fields[results_field] = object_serializer(source='object_list',
                                                       many=True,
                                                       **context_kwarg)

class PaginationSerializer(BasePaginationSerializer):
    """
    A default implementation of a pagination serializer.
    """
    count = serializers.Field(source='paginator.count')
    next = NextPageField(source='*')
    previous = PreviousPageField(source='*')
<|endoftext|>


<commit_before># Copyright 2012 Anton Beloglazov
#
# 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.

""" The database cleaner module.

This module is responsible for periodic clean up of the database.
"""

from contracts import contract
from neat.contracts_extra import *

import datetime

import neat.common as common
from neat.config import *
from neat.db_utils import *

import logging
log = logging.getLogger(__name__)

@contract
def start():
    """ Start the database cleaner loop.

    :return: The final state.
     :rtype: dict(str: *)
    """
    config = read_and_validate_config([DEFAILT_CONFIG_PATH, CONFIG_PATH],
                                      REQUIRED_FIELDS)

    common.init_logging(
        config['log_directory'],
        'db-cleaner.log',
        int(config['log_level']))

    interval = config['db_cleaner_interval']
    if log.isEnabledFor(logging.INFO):
        log.info('Starting the database cleaner, ' +
                 'iterations every %s seconds', interval)
    return common.start(
        init_state,
        execute,
        config,
        int(interval))

@contract
def init_state(config):
    """ Initialize a dict for storing the state of the database cleaner.

    :param config: A config dictionary.
     :type config: dict(str: *)

    :return: A dictionary containing the initial state of the database cleaner.
     :rtype: dict
    """
    return {
        'db': init_db(config['sql_connection']),
        'time_delta': datetime.timedelta(
            seconds=int(config['db_cleaner_interval']))}

@contract
def execute(config, state):
    """ Execute an iteration of the database cleaner.

    :param config: A config dictionary.
     :type config: dict(str: *)

    :param state: A state dictionary.
     :type state: dict(str: *)

    :return: The updated state dictionary.
     :rtype: dict(str: *)
    """
    datetime_threshold = today() - state['time_delta']
    state['db'].cleanup_vm_resource_usage(datetime_threshold)
    if log.isEnabledFor(logging.INFO):
        log.info('Cleaned up data older than %s',
                 datetime_threshold.strftime('%Y-%m-%d %H:%M:%S'))
    return state

@contract
def today(config, state):
    """ Return the today's datetime.

    :return: A datetime object representing current date and time.
     :rtype: datetime
    """
    return datetime.datetime.today()
<commit_msg>Updated a comment<commit_after># Copyright 2012 Anton Beloglazov
#
# 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.

""" The database cleaner module.

This module is responsible for periodic cleaning up the database.
"""

from contracts import contract
from neat.contracts_extra import *

import datetime

import neat.common as common
from neat.config import *
from neat.db_utils import *

import logging
log = logging.getLogger(__name__)

@contract
def start():
    """ Start the database cleaner loop.

    :return: The final state.
     :rtype: dict(str: *)
    """
    config = read_and_validate_config([DEFAILT_CONFIG_PATH, CONFIG_PATH],
                                      REQUIRED_FIELDS)

    common.init_logging(
        config['log_directory'],
        'db-cleaner.log',
        int(config['log_level']))

    interval = config['db_cleaner_interval']
    if log.isEnabledFor(logging.INFO):
        log.info('Starting the database cleaner, ' +
                 'iterations every %s seconds', interval)
    return common.start(
        init_state,
        execute,
        config,
        int(interval))

@contract
def init_state(config):
    """ Initialize a dict for storing the state of the database cleaner.

    :param config: A config dictionary.
     :type config: dict(str: *)

    :return: A dictionary containing the initial state of the database cleaner.
     :rtype: dict
    """
    return {
        'db': init_db(config['sql_connection']),
        'time_delta': datetime.timedelta(
            seconds=int(config['db_cleaner_interval']))}

@contract
def execute(config, state):
    """ Execute an iteration of the database cleaner.

    :param config: A config dictionary.
     :type config: dict(str: *)

    :param state: A state dictionary.
     :type state: dict(str: *)

    :return: The updated state dictionary.
     :rtype: dict(str: *)
    """
    datetime_threshold = today() - state['time_delta']
    state['db'].cleanup_vm_resource_usage(datetime_threshold)
    if log.isEnabledFor(logging.INFO):
        log.info('Cleaned up data older than %s',
                 datetime_threshold.strftime('%Y-%m-%d %H:%M:%S'))
    return state

@contract
def today(config, state):
    """ Return the today's datetime.

    :return: A datetime object representing current date and time.
     :rtype: datetime
    """
    return datetime.datetime.today()
<|endoftext|>


<commit_before># coding: utf-8
from __future__ import absolute_import, unicode_literals

import uuid

from django.conf import settings
from django.db import models
from django.utils import six
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _

# Prior to Django 1.5, the AUTH_USER_MODEL setting does not exist.
# Note that we don't perform this code in the compat module due to
# bug report #1297
# See: https://github.com/tomchristie/django-rest-framework/issues/1297
from rest_framework_sso.querysets import SessionTokenQuerySet

AUTH_USER_MODEL = getattr(settings, 'AUTH_USER_MODEL', 'auth.User')

@python_2_unicode_compatible
class SessionToken(models.Model):
    """
    The default session token model.
    """
    id = models.UUIDField(
        primary_key=True,
        default=uuid.uuid4,
        editable=False,
        db_index=True,
    )
    user = models.ForeignKey(
        to=AUTH_USER_MODEL,
        related_name='+',
        on_delete=models.CASCADE,
        verbose_name=_("user"),
    )
    ip_address = models.GenericIPAddressField(null=True, blank=True, db_index=True)
    user_agent = models.CharField(max_length=1000, blank=True)
    created_at = models.DateTimeField(auto_now_add=True, db_index=True)
    revoked_at = models.DateTimeField(null=True, blank=True, db_index=True)

    objects = SessionTokenQuerySet.as_manager()

    class Meta:
        # Work around for a bug in Django:
        # https://code.djangoproject.com/ticket/19422
        #
        # Also see corresponding ticket:
        # https://github.com/tomchristie/django-rest-framework/issues/705
        abstract = 'rest_framework_sso' not in settings.INSTALLED_APPS
        verbose_name = _("Session token")
        verbose_name_plural = _("Session tokens")

    def __str__(self):
        return six.text_type(self.id)

    def update_attributes(self, request):
        self.ip_address = request.META.get('HTTP_X_FORWARDED_FOR') \
            if request.META.get('HTTP_X_FORWARDED_FOR') \
            else request.META.get('REMOTE_ADDR')
        self.user_agent = request.META.get('HTTP_USER_AGENT')[:1000]
<commit_msg>Fix issues when request.META.get('HTTP_USER_AGENT') is None<commit_after># coding: utf-8
from __future__ import absolute_import, unicode_literals

import uuid

from django.conf import settings
from django.db import models
from django.utils import six
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _

# Prior to Django 1.5, the AUTH_USER_MODEL setting does not exist.
# Note that we don't perform this code in the compat module due to
# bug report #1297
# See: https://github.com/tomchristie/django-rest-framework/issues/1297
from rest_framework_sso.querysets import SessionTokenQuerySet

AUTH_USER_MODEL = getattr(settings, 'AUTH_USER_MODEL', 'auth.User')

@python_2_unicode_compatible
class SessionToken(models.Model):
    """
    The default session token model.
    """
    id = models.UUIDField(
        primary_key=True,
        default=uuid.uuid4,
        editable=False,
        db_index=True,
    )
    user = models.ForeignKey(
        to=AUTH_USER_MODEL,
        related_name='+',
        on_delete=models.CASCADE,
        verbose_name=_("user"),
    )
    ip_address = models.GenericIPAddressField(null=True, blank=True, db_index=True)
    user_agent = models.CharField(max_length=1000, blank=True)
    created_at = models.DateTimeField(auto_now_add=True, db_index=True)
    revoked_at = models.DateTimeField(null=True, blank=True, db_index=True)

    objects = SessionTokenQuerySet.as_manager()

    class Meta:
        # Work around for a bug in Django:
        # https://code.djangoproject.com/ticket/19422
        #
        # Also see corresponding ticket:
        # https://github.com/tomchristie/django-rest-framework/issues/705
        abstract = 'rest_framework_sso' not in settings.INSTALLED_APPS
        verbose_name = _("Session token")
        verbose_name_plural = _("Session tokens")

    def __str__(self):
        return six.text_type(self.id)

    def update_attributes(self, request):
        if request.META.get('HTTP_X_FORWARDED_FOR'):
            self.ip_address = request.META.get('HTTP_X_FORWARDED_FOR')
        elif request.META.get('REMOTE_ADDR'):
            self.ip_address = request.META.get('REMOTE_ADDR')
        else:
            self.ip_address = None

        if request.META.get('HTTP_USER_AGENT'):
            self.user_agent = request.META.get('HTTP_USER_AGENT')[:1000]
        else:
            self.user_agent = ''
<|endoftext|>


<commit_before>#!/usr/bin/python

import pxlBuffer as pxb
import random
import math
from time import sleep
import time

def wheel(pos, brightness):
        """Generate rainbow colors across 0-255 positions."""
        if pos < 85:
                return pxb.Color(pos * 3 * brightness, (255 - pos * 3) * brightness, 0)
        elif pos < 170:
                pos -= 85
                return pxb.Color((255 - pos * 3) * brightness, 0, pos * 3 * brightness)
        else:
                pos -= 170
                return pxb.Color(0, pos * 3 * brightness, (255 - pos * 3) * brightness)

def pixRange(layer, color, start, end):
	if start > end:
		t = end
		end = start
		start = t

	#print "%s - %s %s" % (color, start, end)
	for pixel in range(start, end+1):
		layer.setPixelColor(pixel, color)

def calcParticle(layer, angle):
	#print "%s - %s" % (pixStart, pixEnd)

	# this will just shut off unused LEDs
	#layer["obj"][::] = None

	# fade out the unused LEDs to give them a tail
	for pixel in range(layer["obj"].numPixels()):
		layer["obj"].pixelBrightness(pixel, -0.1)

	layer["pixEnd"] = layer["pixStart"]
	#pixEnd=int((math.cos(math.radians(angle))*(layer.numPixels()-1)/2)+layer.numPixels()/2)
	layer["pixStart"]=int((math.sin(math.radians(angle))*(layer["obj"].numPixels()-1)/2)+layer["obj"].numPixels()/2)
	pixRange(layer["obj"], layer["color"], layer["pixStart"], layer["pixEnd"])
	#pixRange(layer["obj"], wheel((math.sin(math.radians(angle))+1)/2*255, 1), layer["pixStart"], layer["pixEnd"])
	#print int(wheel((math.sin(math.radians(angle)+.5)*255+255/2), 1))
	#pixRange(layer["obj"], int(wheel(math.sin(math.radians(angle)*255+255/2), 1)), pixStart, pixEnd)
	layer["obj"].show()

def particles(master, wait_ms=.01, bandwidth=5, runtime=60):
	layer = { 0: {},
		  1: {},
		  2: {},
		  3: {},
		}
	layer[0]["obj"] = master.newLayer()
	layer[0]["pixStart"] = int(layer[0]["obj"].numPixels()/2)
	layer[0]["pixEnd"] = int(layer[0]["obj"].numPixels()/2)
	layer[0]["color"] = pxb.Color(255,0,0)
	layer[1]["obj"] = master.newLayer()
	layer[1]["pixStart"] = int(layer[1]["obj"].numPixels()/2)
	layer[1]["pixEnd"] = int(layer[1]["obj"].numPixels()/2)
	layer[1]["color"] = pxb.Color(0,255,0)
	layer[2]["obj"] = master.newLayer()
	layer[2]["pixStart"] = int(layer[2]["obj"].numPixels()/2)
	layer[2]["pixEnd"] = int(layer[2]["obj"].numPixels()/2)
	layer[2]["color"] = pxb.Color(0,0,255)
	layer[3]["obj"] = master.newLayer()
	layer[3]["pixStart"] = int(layer[3]["obj"].numPixels()/2)
	layer[3]["pixEnd"] = int(layer[3]["obj"].numPixels()/2)
	layer[3]["color"] = pxb.Color(255,255,255)

        endTime=time.time()+runtime
        while time.time() < endTime:
		for a in range(3600):
			angle=a/10.0
			calcParticle(layer[0], angle)
			calcParticle(layer[1], angle+90)
			calcParticle(layer[2], angle+180)
			calcParticle(layer[3], angle+270)
			sleep(wait_ms/1000.0)

	layer[0].dead = 0
	layer[1].dead = 0
	layer[2].dead = 0
	layer[3].dead = 0

# entry function
def NeoFX(master, *args):
	particles(master, *args)

# if we're testing the module, setup and execute
if __name__ == "__main__":
	from neopixel import *
	import threading
	import time
	from pprint import pprint

	# target FPS
	#TARGET_FPS = 1
	TARGET_FPS = 24

	# LED strip configuration:
	LED_COUNT      = 480      # Number of LED pixels.
	LED_PIN        = 18      # GPIO pin connected to the pixels (must support PWM!).
	LED_FREQ_HZ    = 800000  # LED signal frequency in hertz (usually 800khz)
	LED_DMA        = 5       # DMA channel to use for generating signal (try 5)
	LED_BRIGHTNESS = 128     # Set to 0 for darkest and 255 for brightest
	LED_INVERT     = False   # True to invert the signal (when using NPN transistor level shift)

	# Create NeoPixel object with appropriate configuration.
	strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS)
	# Intialize the library (must be called once before other functions).
	strip.begin()

	master = pxb.pixelMaster(strip)
	master.show()
	pprint(master.layers)

	#pprint(master.ledsColorBuffer)

	def masterThread():
		global master
		startTime=time.time()
		iterTime=startTime
		count=1
		runTime=(time.time()-startTime)
		master.show()
		count += 1
		#print "Time: %2.3f FPS: %2.3f" % (runTime, count/runTime)
		iterTime=time.time()

		sleepTime=1/float(TARGET_FPS+0.5)-(time.time()-iterTime)
       		if sleepTime > 0:
			sleep(sleepTime)

	t = threading.Thread(target=masterThread)
	t.daemon=True
	t.start()

	while True:
		NeoFX(master)

<commit_msg>fixed layer call<commit_after>#!/usr/bin/python

import pxlBuffer as pxb
import random
import math
from time import sleep
import time

def wheel(pos, brightness):
        """Generate rainbow colors across 0-255 positions."""
        if pos < 85:
                return pxb.Color(pos * 3 * brightness, (255 - pos * 3) * brightness, 0)
        elif pos < 170:
                pos -= 85
                return pxb.Color((255 - pos * 3) * brightness, 0, pos * 3 * brightness)
        else:
                pos -= 170
                return pxb.Color(0, pos * 3 * brightness, (255 - pos * 3) * brightness)

def pixRange(layer, color, start, end):
	if start > end:
		t = end
		end = start
		start = t

	#print "%s - %s %s" % (color, start, end)
	for pixel in range(start, end+1):
		layer.setPixelColor(pixel, color)

def calcParticle(layer, angle):
	#print "%s - %s" % (pixStart, pixEnd)

	# this will just shut off unused LEDs
	#layer["obj"][::] = None

	# fade out the unused LEDs to give them a tail
	for pixel in range(layer["obj"].numPixels()):
		layer["obj"].pixelBrightness(pixel, -0.1)

	layer["pixEnd"] = layer["pixStart"]
	#pixEnd=int((math.cos(math.radians(angle))*(layer.numPixels()-1)/2)+layer.numPixels()/2)
	layer["pixStart"]=int((math.sin(math.radians(angle))*(layer["obj"].numPixels()-1)/2)+layer["obj"].numPixels()/2)
	pixRange(layer["obj"], layer["color"], layer["pixStart"], layer["pixEnd"])
	#pixRange(layer["obj"], wheel((math.sin(math.radians(angle))+1)/2*255, 1), layer["pixStart"], layer["pixEnd"])
	#print int(wheel((math.sin(math.radians(angle)+.5)*255+255/2), 1))
	#pixRange(layer["obj"], int(wheel(math.sin(math.radians(angle)*255+255/2), 1)), pixStart, pixEnd)
	layer["obj"].show()

def particles(master, wait_ms=.01, bandwidth=5, runtime=60):
	layer = { 0: {},
		  1: {},
		  2: {},
		  3: {},
		}
	layer[0]["obj"] = master.newLayer()
	layer[0]["pixStart"] = int(layer[0]["obj"].numPixels()/2)
	layer[0]["pixEnd"] = int(layer[0]["obj"].numPixels()/2)
	layer[0]["color"] = pxb.Color(255,0,0)
	layer[1]["obj"] = master.newLayer()
	layer[1]["pixStart"] = int(layer[1]["obj"].numPixels()/2)
	layer[1]["pixEnd"] = int(layer[1]["obj"].numPixels()/2)
	layer[1]["color"] = pxb.Color(0,255,0)
	layer[2]["obj"] = master.newLayer()
	layer[2]["pixStart"] = int(layer[2]["obj"].numPixels()/2)
	layer[2]["pixEnd"] = int(layer[2]["obj"].numPixels()/2)
	layer[2]["color"] = pxb.Color(0,0,255)
	layer[3]["obj"] = master.newLayer()
	layer[3]["pixStart"] = int(layer[3]["obj"].numPixels()/2)
	layer[3]["pixEnd"] = int(layer[3]["obj"].numPixels()/2)
	layer[3]["color"] = pxb.Color(255,255,255)

        endTime=time.time()+runtime
        while time.time() < endTime:
		for a in range(3600):
			angle=a/10.0
			calcParticle(layer[0], angle)
			calcParticle(layer[1], angle+90)
			calcParticle(layer[2], angle+180)
			calcParticle(layer[3], angle+270)
			sleep(wait_ms/1000.0)

	layer[0]["obj"].dead = 0
	layer[1]["obj"].dead = 0
	layer[2]["obj"].dead = 0
	layer[3]["obj"].dead = 0

# entry function
def NeoFX(master, *args):
	particles(master, *args)

# if we're testing the module, setup and execute
if __name__ == "__main__":
	from neopixel import *
	import threading
	import time
	from pprint import pprint

	# target FPS
	#TARGET_FPS = 1
	TARGET_FPS = 24

	# LED strip configuration:
	LED_COUNT      = 480      # Number of LED pixels.
	LED_PIN        = 18      # GPIO pin connected to the pixels (must support PWM!).
	LED_FREQ_HZ    = 800000  # LED signal frequency in hertz (usually 800khz)
	LED_DMA        = 5       # DMA channel to use for generating signal (try 5)
	LED_BRIGHTNESS = 128     # Set to 0 for darkest and 255 for brightest
	LED_INVERT     = False   # True to invert the signal (when using NPN transistor level shift)

	# Create NeoPixel object with appropriate configuration.
	strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS)
	# Intialize the library (must be called once before other functions).
	strip.begin()

	master = pxb.pixelMaster(strip)
	master.show()
	pprint(master.layers)

	#pprint(master.ledsColorBuffer)

	def masterThread():
		global master
		startTime=time.time()
		iterTime=startTime
		count=1
		runTime=(time.time()-startTime)
		master.show()
		count += 1
		#print "Time: %2.3f FPS: %2.3f" % (runTime, count/runTime)
		iterTime=time.time()

		sleepTime=1/float(TARGET_FPS+0.5)-(time.time()-iterTime)
       		if sleepTime > 0:
			sleep(sleepTime)

	t = threading.Thread(target=masterThread)
	t.daemon=True
	t.start()

	while True:
		NeoFX(master)

<|endoftext|>


<commit_before>'''
Python bindings for working with Vagrant and Vagrantfiles.  Do useful things
with the `vagrant` CLI without the boilerplate (and errors) of calling
`vagrant` and parsing the results.

Quick and dirty test to up, look at, and destroy (!) a Vagrant box.  Run from
the directory holding your Vagrantfile.

python -c 'import vagrant
v = vagrant.Vagrant()
v.up()
print v.status()
print v.user()
print v.hostname()
print v.port()
print v.keyfile()
print v.user_hostname()
print v.user_hostname_port()
print v.conf()
v.destroy();
'

For unit tests, see tests/test_vagrant.py

Dependencies:

- `vagrant` should be installed and in your PATH.

'''

import os
import subprocess

class Vagrant(object):
    '''
    Object to up (launch) and destroy (terminate) vagrant virtual machines,
    to check the status of the machine and to report on the configuration
    of the machine.

    Works by using the `vagrant` executable and a `Vagrantfile`.

    Currently does not support specifying `vm-name` arguments in a multi-vm
    environment.
    '''

    # statuses
    RUNNING = 'running' # vagrant up
    NOT_CREATED = 'not created' # vagrant destroy
    POWEROFF = 'poweroff' # vagrant halt

    def __init__(self, root=None):
        '''
        root: a directory containing a file named Vagrantfile.  Defaults to
        os.getcwd(). This is the directory and Vagrantfile that the Vagrant
        instance will operate on.
        '''
        self.root = os.path.abspath(root) if root is not None else os.getcwd()
        self._cached_conf = None

    def init(self, box_name):
        '''
        Init the VM.
        '''
        command = "init {}".format( box_name )
        self._call_vagrant_command(command)
        #self.conf() # cache configuration

    def up(self, no_provision=False):
        '''
        Launch the Vagrant box.
        '''
        command = "up"
        if no_provision:
            command = "{} --no-provision".format(command)
        self._call_vagrant_command(command)
        self.conf() # cache configuration

    def halt(self):
        '''
        Halt the Vagrant box.
        '''
        command = "halt"
        self._call_vagrant_command(command)
        self.conf() # cache configuration

    def destroy(self):
        '''
        Terminate the running Vagrant box.
        '''
        command = "destroy -f"
        self._call_vagrant_command(command)
        self._cached_conf = None # remove cached configuration

    def status(self):
        '''
        Returns the status of the Vagrant box:
            'not created' if the box is destroyed
            'running' if the box is up
            'poweroff' if the box is halted
            None if no status is found
        There might be other statuses, but the Vagrant docs were unclear.
        '''
        command = "status"
        output = self._vagrant_command_output(command)
        # example output
        '''
        Current VM states:

        default                  poweroff

        The VM is powered off. To restart the VM, simply run `vagrant up`
        '''
        status = None
        for line in output.splitlines():
            if line.startswith('default'):
                status = line.strip().split(None, 1)[1]

        return status

    def conf(self, ssh_config=None):
        '''
        Return a dict containing the keys defined in ssh_config, which
        should include these keys (listed with example values): 'User' (e.g.
        'vagrant'), 'HostName' (e.g. 'localhost'), 'Port' (e.g. '2222'),
        'IdentityFile' (e.g. '/home/todd/.ssh/id_dsa')

        Raises an Exception if the Vagrant box has not yet been created or
        has been destroyed.

        ssh_config: a valid ssh confige file host section.  Defaults to
        the value returned from ssh_config().  For speed, the configuraiton
        parsed from ssh_config is cached for subsequent calls.
        '''
        if self._cached_conf is None or ssh_config is not None:
            conf = self._parse_config(ssh_config)
            self._cached_conf = conf

        return self._cached_conf

    def ssh_config(self):
        '''
        Return the output of 'vagrant ssh-config' which appears to be a valid
        Host section suitable for use in an ssh config file.
        Raises an Exception if the Vagrant box has not yet been created or
        has been destroyed.

        Example output:
            Host default
                HostName 127.0.0.1
                User vagrant
                Port 2222
                UserKnownHostsFile /dev/null
                StrictHostKeyChecking no
                PasswordAuthentication no
                IdentityFile /Users/todd/.vagrant.d/insecure_private_key
                IdentitiesOnly yes
        '''
        # capture ssh configuration from vagrant
        command = "ssh-config"
        return self._vagrant_command_output(command)

    def user(self):
        '''
        Return the ssh user of the vagrant box, e.g. 'vagrant'
        or None if there is no user in the ssh_config.

        Raises an Exception if the Vagrant box has not yet been created or
        has been destroyed.
        '''
        return self.conf().get('User')

    def hostname(self):
        '''
        Return the vagrant box hostname, e.g. '127.0.0.1'
        or None if there is no hostname in the ssh_config.

        Raises an Exception if the Vagrant box has not yet been created or
        has been destroyed.
        '''
        return self.conf().get('HostName')

    def port(self):
        '''
        Return the vagrant box ssh port, e.g. '2222'
        or None if there is no port in the ssh_config.

        Raises an Exception if the Vagrant box has not yet been created or
        has been destroyed.
        '''
        return self.conf().get('Port')

    def keyfile(self):
        '''
        Return the path to the private key used to log in to the vagrant box
        or None if there is no keyfile (IdentityFile) in the ssh_config.
        E.g. '/Users/todd/.vagrant.d/insecure_private_key'

        Raises an Exception if the Vagrant box has not yet been created or
        has been destroyed.

        KeyFile is a synonym for IdentityFile.
        '''
        return self.conf().get('IdentityFile')

    def user_hostname(self):
        '''
        Return a string combining user and hostname, e.g. 'vagrant@127.0.0.1'.
        This string is suitable for use in an ssh commmand.  If user is None
        or empty, it will be left out of the string, e.g. 'localhost'.  If
        hostname is None, have bigger problems.

        Raises an Exception if the Vagrant box has not yet been created or
        has been destroyed.
        '''
        user = self.user()
        user_prefix = user + '@' if user else ''
        return user_prefix + self.hostname()

    def user_hostname_port(self):
        '''
        Return a string combining user, hostname and port, e.g.
        'vagrant@127.0.0.1:2222'.  This string is suitable for use with Fabric,
        in env.hosts.  If user or port is None or empty, they will be left
        out of the string.  E.g. 'vagrant@localhost', or 'localhost:2222' or
        'localhost'.  If hostname is None, you have bigger problems.

        Raises an Exception if the Vagrant box has not yet been created or
        has been destroyed.
        '''
        user = self.user()
        port = self.port()
        user_prefix = user + '@' if user else ''
        port_suffix = ':' + port if port else ''
        return user_prefix + self.hostname() + port_suffix

    def sandbox_status(self):
        '''
        Returns the status of the sandbox mode.

        Possible values are:
        - on
        - off
        - unknown
        - not installed
        '''
        command = "sandbox status"
        vagrant_sandbox_output = self._vagrant_command_output(command)
        return self._parse_vagrant_sandbox_status(vagrant_sandbox_output)

    def sandbox_enable(self):
        '''
        Enables the sandbox mode.

        This requires the Sahara gem to be installed
        (https://github.com/jedi4ever/sahara).
        '''
        command = "sandbox on"
        self._call_vagrant_command(command)

    def sandbox_disable(self):
        '''
        Disables the sandbox mode.
        '''
        command = "sandbox off"
        self._call_vagrant_command(command)

    def sandbox_commit(self):
        '''
        Permanently writes all the changes made to the VM.
        '''
        command = "sandbox commit"
        self._call_vagrant_command(command)

    def sandbox_rollback(self):
        '''
        Reverts all the changes made to the VM since the last commit.
        '''
        command = "sandbox rollback"
        self._call_vagrant_command(command)

    def box_add(self, box_name, box_url):
        '''
        Adds a box with given name, from given url.
        '''
        command = "box add '{}' '{}'".format(box_name, box_url)
        self._call_vagrant_command(command)

    def box_list(self):
        '''
        Returns a list of all available box names.
        '''
        command ="box list"
        output = self._vagrant_command_output(command)
        return [line.strip() for line in output.splitlines()]

    def box_remove(self, box_name):
        '''
        Removes the box with given name.
        '''
        command = "box remove '{}'".format(box_name)
        self._call_vagrant_command(command)

    def provision(self):
        '''
        Runs the provisioners defined in the Vagrantfile.
        '''
        command = "provision"
        self._call_vagrant_command(command)

    def _parse_vagrant_sandbox_status(self, vagrant_output):
        '''
        Returns the status of the sandbox mode given output from
        'vagrant sandbox status'.
        '''
        # typical output
        # [default] - snapshot mode is off
        # or
        # [default] - machine not created
        # if the box VM is down
        tokens = [ token.strip() for token in vagrant_output.split(' ') ]
        if tokens[0] == 'Usage:':
            sahara_status = 'not installed'
        elif "{} {}".format(tokens[-2], tokens[-1]) == 'not created':
            sahara_status = 'unknown'
        else:
            sahara_status = tokens[-1]
        return sahara_status

    def _parse_config(self, ssh_config=None):
        '''
        This ghetto parser does not parse the full grammar of an ssh config
        file.  It makes assumptions that are (hopefully) correct for the output
        of `vagrant ssh-config`.  Specifically it assumes that there is only
        one Host section, the default vagrant host.  It assumes that every line
        is of the form 'key  value', where key is a single token without any
        whitespace and value is the remaining part of the line.  All leading
        and trailing whitespace is removed from key and value.  For example:

        '    User vagrant\n'

        Lines with '#' as the first non-whitespace character are considered
        comments and ignored.  Whitespace-only lines are ignored.  This parser
        does NOT handle using an '=' in options.

        See https://github.com/bitprophet/ssh/blob/master/ssh/config.py for a
        more compliant ssh config file parser.
        '''
        if ssh_config is None:
            ssh_config = self.ssh_config()

        # skip blank lines and comment lines
        conf = dict(line.strip().split(None, 1) for line in
                    ssh_config.splitlines() if line.strip() and
                    not line.strip().startswith('#'))

        return conf

    def _vagrant_command_string(self, command):
        '''
        Returns the command with 'vagrant ' prepended.
        '''
        return "vagrant {}".format(command)

    def _call_vagrant_command(self, command):
        '''
        Wrapper around subprocess.check_call

        :param command: string which will be appended to 'vagrant ' and called
        in a shell using subprocess check_call.
        '''
        command = self._vagrant_command_string(command)
        return subprocess.check_call(command, shell=True, cwd=self.root)

    def _vagrant_command_output(self, command):
        '''
        Wrapper around subprocess.check_output

        :param command: same as for _call_vagrant_command.
        '''
        command = self._vagrant_command_string(command)
        return subprocess.check_output(command, shell=True, cwd=self.root)
<commit_msg>Added auto download to init(). Looks up a dict for urls<commit_after>'''
Python bindings for working with Vagrant and Vagrantfiles.  Do useful things
with the `vagrant` CLI without the boilerplate (and errors) of calling
`vagrant` and parsing the results.

Quick and dirty test to up, look at, and destroy (!) a Vagrant box.  Run from
the directory holding your Vagrantfile.

python -c 'import vagrant
v = vagrant.Vagrant()
v.up()
print v.status()
print v.user()
print v.hostname()
print v.port()
print v.keyfile()
print v.user_hostname()
print v.user_hostname_port()
print v.conf()
v.destroy();
'

For unit tests, see tests/test_vagrant.py

Dependencies:

- `vagrant` should be installed and in your PATH.

'''

import os
import subprocess

class Vagrant(object):
    '''
    Object to up (launch) and destroy (terminate) vagrant virtual machines,
    to check the status of the machine and to report on the configuration
    of the machine.

    Works by using the `vagrant` executable and a `Vagrantfile`.

    Currently does not support specifying `vm-name` arguments in a multi-vm
    environment.
    '''

    # statuses
    RUNNING = 'running'  # vagrant up
    NOT_CREATED = 'not created'  # vagrant destroy
    POWEROFF = 'poweroff'  # vagrant halt
    BASE_BOXES = {
        'ubuntu-Lucid32': 'http://files.vagrantup.com/lucid32.box',
        'ubuntu-lucid64': 'http://files.vagrantup.com/lucid64.box',
        'ubuntu-precise32': 'http://files.vagrantup.com/precise32.box',
        'ubuntu-precise64': 'http://files.vagrantup.com/precise64.box',
    }

    def __init__(self, root=None):
        '''
        root: a directory containing a file named Vagrantfile.  Defaults to
        os.getcwd(). This is the directory and Vagrantfile that the Vagrant
        instance will operate on.
        '''
        self.root = os.path.abspath(root) if root is not None else os.getcwd()
        self._cached_conf = None

    def init(self, box_name):
        '''
        Init the VM.
        '''
        print "Checking for " + box_name
        if box_name not in self.box_list():
            if self._confirm(box_name + " not installed. Add box?"):
                self.box_add(box_name, self.BASE_BOXES[box_name])
            else:
                exit()
        command = "init {}".format(box_name)
        self._call_vagrant_command(command)
        #self.conf() # cache configuration

    def up(self, no_provision=False):
        '''
        Launch the Vagrant box.
        '''
        command = "up"
        if no_provision:
            command = "{} --no-provision".format(command)
        self._call_vagrant_command(command)
        self.conf()  # cache configuration

    def halt(self):
        '''
        Halt the Vagrant box.
        '''
        command = "halt"
        self._call_vagrant_command(command)
        self.conf()  # cache configuration

    def destroy(self):
        '''
        Terminate the running Vagrant box.
        '''
        command = "destroy -f"
        self._call_vagrant_command(command)
        self._cached_conf = None  # remove cached configuration

    def status(self):
        '''
        Returns the status of the Vagrant box:
            'not created' if the box is destroyed
            'running' if the box is up
            'poweroff' if the box is halted
            None if no status is found
        There might be other statuses, but the Vagrant docs were unclear.
        '''
        command = "status"
        output = self._vagrant_command_output(command)
        # example output
        '''
        Current VM states:

        default                  poweroff

        The VM is powered off. To restart the VM, simply run `vagrant up`
        '''
        status = None
        for line in output.splitlines():
            if line.startswith('default'):
                status = line.strip().split(None, 1)[1]

        return status

    def conf(self, ssh_config=None):
        '''
        Return a dict containing the keys defined in ssh_config, which
        should include these keys (listed with example values): 'User' (e.g.
        'vagrant'), 'HostName' (e.g. 'localhost'), 'Port' (e.g. '2222'),
        'IdentityFile' (e.g. '/home/todd/.ssh/id_dsa')

        Raises an Exception if the Vagrant box has not yet been created or
        has been destroyed.

        ssh_config: a valid ssh confige file host section.  Defaults to
        the value returned from ssh_config().  For speed, the configuraiton
        parsed from ssh_config is cached for subsequent calls.
        '''
        if self._cached_conf is None or ssh_config is not None:
            conf = self._parse_config(ssh_config)
            self._cached_conf = conf

        return self._cached_conf

    def ssh_config(self):
        '''
        Return the output of 'vagrant ssh-config' which appears to be a valid
        Host section suitable for use in an ssh config file.
        Raises an Exception if the Vagrant box has not yet been created or
        has been destroyed.

        Example output:
            Host default
                HostName 127.0.0.1
                User vagrant
                Port 2222
                UserKnownHostsFile /dev/null
                StrictHostKeyChecking no
                PasswordAuthentication no
                IdentityFile /Users/todd/.vagrant.d/insecure_private_key
                IdentitiesOnly yes
        '''
        # capture ssh configuration from vagrant
        command = "ssh-config"
        return self._vagrant_command_output(command)

    def user(self):
        '''
        Return the ssh user of the vagrant box, e.g. 'vagrant'
        or None if there is no user in the ssh_config.

        Raises an Exception if the Vagrant box has not yet been created or
        has been destroyed.
        '''
        return self.conf().get('User')

    def hostname(self):
        '''
        Return the vagrant box hostname, e.g. '127.0.0.1'
        or None if there is no hostname in the ssh_config.

        Raises an Exception if the Vagrant box has not yet been created or
        has been destroyed.
        '''
        return self.conf().get('HostName')

    def port(self):
        '''
        Return the vagrant box ssh port, e.g. '2222'
        or None if there is no port in the ssh_config.

        Raises an Exception if the Vagrant box has not yet been created or
        has been destroyed.
        '''
        return self.conf().get('Port')

    def keyfile(self):
        '''
        Return the path to the private key used to log in to the vagrant box
        or None if there is no keyfile (IdentityFile) in the ssh_config.
        E.g. '/Users/todd/.vagrant.d/insecure_private_key'

        Raises an Exception if the Vagrant box has not yet been created or
        has been destroyed.

        KeyFile is a synonym for IdentityFile.
        '''
        return self.conf().get('IdentityFile')

    def user_hostname(self):
        '''
        Return a string combining user and hostname, e.g. 'vagrant@127.0.0.1'.
        This string is suitable for use in an ssh commmand.  If user is None
        or empty, it will be left out of the string, e.g. 'localhost'.  If
        hostname is None, have bigger problems.

        Raises an Exception if the Vagrant box has not yet been created or
        has been destroyed.
        '''
        user = self.user()
        user_prefix = user + '@' if user else ''
        return user_prefix + self.hostname()

    def user_hostname_port(self):
        '''
        Return a string combining user, hostname and port, e.g.
        'vagrant@127.0.0.1:2222'.  This string is suitable for use with Fabric,
        in env.hosts.  If user or port is None or empty, they will be left
        out of the string.  E.g. 'vagrant@localhost', or 'localhost:2222' or
        'localhost'.  If hostname is None, you have bigger problems.

        Raises an Exception if the Vagrant box has not yet been created or
        has been destroyed.
        '''
        user = self.user()
        port = self.port()
        user_prefix = user + '@' if user else ''
        port_suffix = ':' + port if port else ''
        return user_prefix + self.hostname() + port_suffix

    def sandbox_status(self):
        '''
        Returns the status of the sandbox mode.

        Possible values are:
        - on
        - off
        - unknown
        - not installed
        '''
        command = "sandbox status"
        vagrant_sandbox_output = self._vagrant_command_output(command)
        return self._parse_vagrant_sandbox_status(vagrant_sandbox_output)

    def sandbox_enable(self):
        '''
        Enables the sandbox mode.

        This requires the Sahara gem to be installed
        (https://github.com/jedi4ever/sahara).
        '''
        command = "sandbox on"
        self._call_vagrant_command(command)

    def sandbox_disable(self):
        '''
        Disables the sandbox mode.
        '''
        command = "sandbox off"
        self._call_vagrant_command(command)

    def sandbox_commit(self):
        '''
        Permanently writes all the changes made to the VM.
        '''
        command = "sandbox commit"
        self._call_vagrant_command(command)

    def sandbox_rollback(self):
        '''
        Reverts all the changes made to the VM since the last commit.
        '''
        command = "sandbox rollback"
        self._call_vagrant_command(command)

    def box_add(self, box_name, box_url):
        '''
        Adds a box with given name, from given url.
        '''
        command = "box add '{}' '{}'".format(box_name, box_url)
        self._call_vagrant_command(command)

    def box_list(self):
        '''
        Returns a list of all available box names.
        '''
        command = "box list"
        output = self._vagrant_command_output(command)
        return [line.strip() for line in output.splitlines()]

    def box_remove(self, box_name):
        '''
        Removes the box with given name.
        '''
        command = "box remove '{}'".format(box_name)
        self._call_vagrant_command(command)

    def provision(self):
        '''
        Runs the provisioners defined in the Vagrantfile.
        '''
        command = "provision"
        self._call_vagrant_command(command)

    def _parse_vagrant_sandbox_status(self, vagrant_output):
        '''
        Returns the status of the sandbox mode given output from
        'vagrant sandbox status'.
        '''
        # typical output
        # [default] - snapshot mode is off
        # or
        # [default] - machine not created
        # if the box VM is down
        tokens = [token.strip() for token in vagrant_output.split(' ')]
        if tokens[0] == 'Usage:':
            sahara_status = 'not installed'
        elif "{} {}".format(tokens[-2], tokens[-1]) == 'not created':
            sahara_status = 'unknown'
        else:
            sahara_status = tokens[-1]
        return sahara_status

    def _parse_config(self, ssh_config=None):
        '''
        This ghetto parser does not parse the full grammar of an ssh config
        file.  It makes assumptions that are (hopefully) correct for the output
        of `vagrant ssh-config`.  Specifically it assumes that there is only
        one Host section, the default vagrant host.  It assumes that every line
        is of the form 'key  value', where key is a single token without any
        whitespace and value is the remaining part of the line.  All leading
        and trailing whitespace is removed from key and value.  For example:

        '    User vagrant\n'

        Lines with '#' as the first non-whitespace character are considered
        comments and ignored.  Whitespace-only lines are ignored.  This parser
        does NOT handle using an '=' in options.

        See https://github.com/bitprophet/ssh/blob/master/ssh/config.py for a
        more compliant ssh config file parser.
        '''
        if ssh_config is None:
            ssh_config = self.ssh_config()

        # skip blank lines and comment lines
        conf = dict(line.strip().split(None, 1) for line in
                    ssh_config.splitlines() if line.strip() and
                    not line.strip().startswith('#'))

        return conf

    def _vagrant_command_string(self, command):
        '''
        Returns the command with 'vagrant ' prepended.
        '''
        return "vagrant {}".format(command)

    def _call_vagrant_command(self, command):
        '''
        Wrapper around subprocess.check_call

        :param command: string which will be appended to 'vagrant ' and called
        in a shell using subprocess check_call.
        '''
        command = self._vagrant_command_string(command)
        return subprocess.check_call(command, shell=True, cwd=self.root)

    def _vagrant_command_output(self, command):
        '''
        Wrapper around subprocess.check_output

        :param command: same as for _call_vagrant_command.
        '''
        command = self._vagrant_command_string(command)
        return subprocess.check_output(command, shell=True, cwd=self.root)

    def _confirm(self, prompt=None, resp=False):
        '''
        Prompts for yes or no response from the user. Returns True for yes
        and False for no.

        :prompt resp: The default value assumed by the caller when
        user simply types ENTER.

        >>> confirm(prompt='Create Directory?', resp=True)
        Create Directory? [Y/n]:
        True
        >>> confirm(prompt='Create Directory?', resp=False)
        Create Directory? [y/N]:
        False
        >>> confirm(prompt='Create Directory?', resp=False)
        Create Directory? [y/N]: y
        True

        '''

        if prompt is None:
            prompt = 'Confirm'

        if resp:
            prompt = '%s [%s/%s]: ' % (prompt, 'Y', 'n')
        else:
            prompt = '%s [%s/%s]: ' % (prompt, 'y', 'N')

        while True:
            ans = raw_input(prompt)
            if not ans:
                return resp
            if ans not in ['y', 'Y', 'n', 'N']:
                print 'Please enter y or n.'
                continue
            if ans == 'y' or ans == 'Y':
                return True
            if ans == 'n' or ans == 'N':
                return False
<|endoftext|>