<commit_before># -*- coding: utf-8 -*-
import os
import sys
import urwid
import subprocess
import threading

from pyhn.config import Config
from pyhn.popup import Popup
from pyhn import __version__ as VERSION

class ItemWidget(urwid.WidgetWrap):
    """ Widget of listbox, represent each story """
    def __init__(self, story):
        self.story = story
        self.number = story.number
        self.title = story.title
        self.url = story.URL
        self.submitter = story.submitter
        self.submitter_url = story.submitterURL
        self.comment_count = story.commentCount
        self.comments_url = story.commentsURL
        self.score = story.score
        self.published_time = story.publishedTime

        if self.submitter == -1:
            self.submitter = "-"
            self.submitter_url = -1

        if self.score == -1:
            self.score = "-"

        if self.comment_count == -1:
            self.comment_count = "-"
            self.comments_url = -1

        self.item = [
            ('fixed', 4, urwid.Padding(urwid.AttrWrap(
                urwid.Text("%s:" % self.number, align="right"),
                'body',
                'focus'))),
            urwid.AttrWrap(urwid.Text('%s' % self.title), 'body', 'focus'),
            ('fixed', 5, urwid.Padding(urwid.AttrWrap(
                urwid.Text(str(self.score), align="right"), 'body', 'focus'))),
            ('fixed', 8, urwid.Padding(urwid.AttrWrap(
                urwid.Text(str(self.comment_count), align="right"),
                'body',
                'focus'))),
        ]
        w = urwid.Columns(self.item, focus_column=1, dividechars=1)
        self.__super.__init__(w)

    def selectable(self):
        return True

    def keypress(self, size, key):
        return key

class HNGui(object):
    """ The Pyhn Gui object """
    def __init__(self, cache_manager):
        self.cache_manager = cache_manager
        self.already_build = False
        self.which = "top"

        self.config = Config()
        self.palette = self.config.get_palette()

    def main(self):
        """
        Main Gui function which create Ui object,
        build interface and run the loop
        """
        self.ui = urwid.raw_display.Screen()
        self.ui.register_palette(self.palette)
        self.build_interface()
        self.ui.run_wrapper(self.run)

    def build_help(self):
        """ Fetch all key bindings and build help message """
        self.bindings = {}
        self.help_msg = []
        self.help_msg.append(urwid.AttrWrap(urwid.Text('\n Key bindings \n'), 'title'))
        self.help_msg.append(urwid.AttrWrap(urwid.Text(''), 'help'))
        for binding in self.config.parser.items('keybindings'):
            self.bindings[binding[0]] = binding[1]
            line = urwid.AttrWrap(
                urwid.Text(' %s: %s ' % (binding[1], binding[0].replace('_', ' '))),
                'help')
            self.help_msg.append(line)
        self.help_msg.append(urwid.AttrWrap(urwid.Text(''), 'help'))
        self.help_msg.append(urwid.AttrWrap(
            urwid.Text(' Thanks for using Pyhn %s! ' % VERSION, align='center'),
            'title'))
        self.help_msg.append(urwid.AttrWrap(urwid.Text(''), 'help'))
        self.help_msg.append(urwid.AttrWrap(urwid.Text(' Website: http://github.com/socketubs/pyhn '), 'help'))
        self.help_msg.append(urwid.AttrWrap(urwid.Text(' Author : socketubs '), 'help'))
        self.help_msg.append(urwid.AttrWrap(urwid.Text(''), 'help'))
        self.help_msg.append(urwid.AttrWrap(urwid.Text(''), 'help'))
        self.help_msg.append(urwid.AttrWrap(urwid.Text(''), 'help'))

        self.help = Popup(self.help_msg, ('help', 'help'), (0, 1), self.view)

    def build_interface(self):
        """
        Build interface, refresh cache if needed, update stories listbox, create
        header, footer, view and the loop.
        """
        if self.cache_manager.is_outdated():
            self.cache_manager.refresh()

        self.stories = self.cache_manager.get_stories()
        self.update_stories(self.stories)
        self.header_content = [
                ('fixed', 4, urwid.Padding(urwid.AttrWrap(urwid.Text(' N°'), 'header'))),
                urwid.AttrWrap(urwid.Text('TOP STORIES', align="center"), 'title'),
                ('fixed', 5, urwid.Padding(urwid.AttrWrap(urwid.Text('SCORE'), 'header'))),
                ('fixed', 8, urwid.Padding(urwid.AttrWrap(urwid.Text('COMMENTS'), 'header')))]

        self.header = urwid.Columns(self.header_content, dividechars=1)
        self.footer = urwid.AttrMap(
            urwid.Text('Welcome in pyhn by socketubs (https://github.com/socketubs/pyhn)', align='center'),
            'footer')

        self.view = urwid.Frame(urwid.AttrWrap(self.listbox, 'body'), header=self.header, footer=self.footer)
        self.loop = urwid.MainLoop(
                        self.view, self.palette,
                        screen=self.ui, handle_mouse=False,
                        unhandled_input=self.keystroke)

        self.build_help()
        self.already_build = True

    def set_help(self):
        """ Set help msg in footer """
        self.view.set_footer(urwid.AttrWrap(urwid.Text(self.help, align="center"), 'help'))

    def set_footer(self, msg, style="normal"):
        """ Set centered footer message """
        if style == "normal":
            self.footer = urwid.AttrWrap(urwid.Text(msg), 'footer')
            self.view.set_footer(self.footer)
        elif style == "error":
            self.footer = urwid.AttrWrap(urwid.Text(msg), 'footer-error')
            self.view.set_footer(self.footer)

    def set_header(self, msg):
        """ Set header story message """
        self.header_content[1] = urwid.AttrWrap(urwid.Text(msg, align="center"), 'title')
        self.view.set_header(urwid.Columns(self.header_content, dividechars=1))

    def keystroke(self, input):
        """ All key bindings are computed here """
        # QUIT
        if input in ('q', 'Q'):
            raise urwid.ExitMainLoop()
        # LINKS
        elif input in self.bindings['open_story_link'].split(','):
            self.open_webbrowser(self.listbox.get_focus()[0].url)
        elif input in self.bindings['show_story_link'].split(','):
            self.set_footer(self.listbox.get_focus()[0].url)
        elif input in self.bindings['open_comments_link'].split(','):
            if self.listbox.get_focus()[0].comments_url == -1:
                self.set_footer('No comments')
            else:
                self.open_webbrowser(self.listbox.get_focus()[0].comments_url)
        elif input in self.bindings['show_comments_link'].split(','):
            if self.listbox.get_focus()[0].comments_url == -1:
                self.set_footer('No comments')
            else:
                self.set_footer(self.listbox.get_focus()[0].comments_url)
        elif input in self.bindings['open_submitter_link'].split(','):
            if self.listbox.get_focus()[0].submitter_url == -1:
                self.set_footer('Anonymous submitter')
            else:
                self.open_webbrowser(self.listbox.get_focus()[0].submitter_url)
        elif input in self.bindings['show_submitter_link'].split(','):
            if self.listbox.get_focus()[0].submitter_url == -1:
                self.set_footer('Anonymous submitter')
            else:
                self.set_footer(self.listbox.get_focus()[0].submitter_url)
        # MOVEMENTS
        elif input in self.bindings['down'].split(','):
            if self.listbox.focus_position - 1 in self.walker.positions():
                self.listbox.set_focus(self.walker.prev_position(self.listbox.focus_position))
        elif input in self.bindings['up'].split(','):
            if self.listbox.focus_position + 1 in self.walker.positions():
                self.listbox.set_focus(self.walker.next_position(self.listbox.focus_position))
        elif input in self.bindings['page_up'].split(','):
            self.listbox._keypress_page_up(self.ui.get_cols_rows())
        elif input in self.bindings['page_down'].split(','):
            self.listbox._keypress_page_down(self.ui.get_cols_rows())
        elif input in self.bindings['first_story'].split(','):
            self.listbox.set_focus(self.walker.positions()[0])
        elif input in self.bindings['last_story'].split(','):
            self.listbox.set_focus(self.walker.positions()[-1])
        # STORIES
        elif input in ('n',):
            threading.Thread(None, self.async_refresher, None, ('newest', 'NEWEST STORIES'), {}).start()
        elif input in ('t',):
            threading.Thread(None, self.async_refresher, None, ('top', 'TOP STORIES'), {}).start()
        elif input in ('b',):
            self.set_footer('Syncing best stories...')
            threading.Thread(None, self.async_refresher, None, ('best', 'BEST STORIES'), {}).start()
        # OTHERS
        elif input in self.bindings['refresh'].split(','):
            threading.Thread(None, self.async_refresher, None, (), {}).start()
        elif input in self.bindings['reload_config'].split(','):
            self.reload_config()
        elif input in ('h', 'H', '?'):
            keys = True
            while True:
                if keys:
                    self.ui.draw_screen(self.ui.get_cols_rows(), self.help.render(self.ui.get_cols_rows(), True))
                    keys = self.ui.get_input()
                    if 'h' or 'H' or '?' or 'escape' in keys:
                        break

    def async_refresher(self, which=None, header=None):
        if which is None:
            which = self.which
        if self.cache_manager.is_outdated(which):
            self.cache_manager.refresh(which)
        stories = self.cache_manager.get_stories(which)
        self.update_stories(stories)
        if header is not None:
            self.set_header(header)
            self.which = which
        self.loop.draw_screen()

    def update_stories(self, stories):
        """ Reload listbox and walker with new stories """
        items = []
        for story in stories:
            items.append(ItemWidget(story))

        if self.already_build:
            self.walker[:] = items
            self.update()
        else:
            self.walker = urwid.SimpleListWalker(items)
            self.listbox = urwid.ListBox(self.walker)

    def open_webbrowser(self, url):
        """ Handle url and open sub process with web browser """
        if self.config.parser.get('settings', 'browser_cmd') == "__default__":
            python_bin = sys.executable
            browser_output = subprocess.Popen(
                [python_bin, '-m', 'webbrowser', '-t', url],
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE)
        else:
            cmd = self.config.parser.get('settings', 'browser_cmd')
            try:
                p = subprocess.Popen(
                    cmd.replace('__url__', url),
                    shell=True,
                    close_fds=True,
                    stderr=subprocess.PIPE)

                returncode = p.wait()
            except KeyboardInterrupt:
                stderr = "User keyboard interrupt detected!"
                self.set_footer(stderr, style="error")
                return
            if returncode > 0:
                stderr = p.communicate()[1]
                self.set_footer("%s" % stderr, style="error")

    def update(self):
        """ Update footer about focus story """
        focus = self.listbox.get_focus()[0]
        if focus.submitter == "":
            msg = "submitted %s" % focus.published_time
        else:
            msg = "submitted %s by %s" % (focus.published_time, focus.submitter)

        self.set_footer(msg)

    def reload_config(self):
        """
        Create new Config object, reload colors, refresh cache
        if needed and redraw screen.
        """
        self.set_footer('Reloading configuration')
        self.config = Config()
        self.build_help()
        self.palette = self.config.get_palette()
        self.build_interface()
        self.loop.draw_screen()
        self.set_footer('Configuration file reloaded!')

        if self.config.parser.get('settings', 'cache') != self.cache_manager.cache_path:
            self.cache_manager.cache_path = self.config.parser.get('settings', 'cache')

    def run(self):
        """ Run the loop """
        urwid.connect_signal(self.walker, 'modified', self.update)

        try:
            self.loop.run()
        except KeyboardInterrupt:
            urwid.ExitMainLoop()
<commit_msg>elif -> if<commit_after># -*- coding: utf-8 -*-
import sys
import urwid
import subprocess
import threading

from pyhn.config import Config
from pyhn.popup import Popup
from pyhn import __version__ as VERSION

class ItemWidget(urwid.WidgetWrap):
    """ Widget of listbox, represent each story """
    def __init__(self, story):
        self.story = story
        self.number = story.number
        self.title = story.title
        self.url = story.URL
        self.submitter = story.submitter
        self.submitter_url = story.submitterURL
        self.comment_count = story.commentCount
        self.comments_url = story.commentsURL
        self.score = story.score
        self.published_time = story.publishedTime

        if self.submitter == -1:
            self.submitter = "-"
            self.submitter_url = -1

        if self.score == -1:
            self.score = "-"

        if self.comment_count == -1:
            self.comment_count = "-"
            self.comments_url = -1

        self.item = [
            ('fixed', 4, urwid.Padding(urwid.AttrWrap(
                urwid.Text("%s:" % self.number, align="right"),
                'body',
                'focus'))),
            urwid.AttrWrap(urwid.Text('%s' % self.title), 'body', 'focus'),
            ('fixed', 5, urwid.Padding(urwid.AttrWrap(
                urwid.Text(str(self.score), align="right"), 'body', 'focus'))),
            ('fixed', 8, urwid.Padding(urwid.AttrWrap(
                urwid.Text(str(self.comment_count), align="right"),
                'body',
                'focus'))),
        ]
        w = urwid.Columns(self.item, focus_column=1, dividechars=1)
        self.__super.__init__(w)

    def selectable(self):
        return True

    def keypress(self, size, key):
        return key

class HNGui(object):
    """ The Pyhn Gui object """
    def __init__(self, cache_manager):
        self.cache_manager = cache_manager
        self.already_build = False
        self.which = "top"

        self.config = Config()
        self.palette = self.config.get_palette()

    def main(self):
        """
        Main Gui function which create Ui object,
        build interface and run the loop
        """
        self.ui = urwid.raw_display.Screen()
        self.ui.register_palette(self.palette)
        self.build_interface()
        self.ui.run_wrapper(self.run)

    def build_help(self):
        """ Fetch all key bindings and build help message """
        self.bindings = {}
        self.help_msg = []
        self.help_msg.append(urwid.AttrWrap(urwid.Text('\n Key bindings \n'), 'title'))
        self.help_msg.append(urwid.AttrWrap(urwid.Text(''), 'help'))
        for binding in self.config.parser.items('keybindings'):
            self.bindings[binding[0]] = binding[1]
            line = urwid.AttrWrap(
                urwid.Text(' %s: %s ' % (binding[1], binding[0].replace('_', ' '))),
                'help')
            self.help_msg.append(line)
        self.help_msg.append(urwid.AttrWrap(urwid.Text(''), 'help'))
        self.help_msg.append(urwid.AttrWrap(
            urwid.Text(' Thanks for using Pyhn %s! ' % VERSION, align='center'),
            'title'))
        self.help_msg.append(urwid.AttrWrap(urwid.Text(''), 'help'))
        self.help_msg.append(urwid.AttrWrap(urwid.Text(' Website: http://github.com/socketubs/pyhn '), 'help'))
        self.help_msg.append(urwid.AttrWrap(urwid.Text(' Author : socketubs '), 'help'))
        self.help_msg.append(urwid.AttrWrap(urwid.Text(''), 'help'))
        self.help_msg.append(urwid.AttrWrap(urwid.Text(''), 'help'))
        self.help_msg.append(urwid.AttrWrap(urwid.Text(''), 'help'))

        self.help = Popup(self.help_msg, ('help', 'help'), (0, 1), self.view)

    def build_interface(self):
        """
        Build interface, refresh cache if needed, update stories listbox, create
        header, footer, view and the loop.
        """
        if self.cache_manager.is_outdated():
            self.cache_manager.refresh()

        self.stories = self.cache_manager.get_stories()
        self.update_stories(self.stories)
        self.header_content = [
                ('fixed', 4, urwid.Padding(urwid.AttrWrap(urwid.Text(' N°'), 'header'))),
                urwid.AttrWrap(urwid.Text('TOP STORIES', align="center"), 'title'),
                ('fixed', 5, urwid.Padding(urwid.AttrWrap(urwid.Text('SCORE'), 'header'))),
                ('fixed', 8, urwid.Padding(urwid.AttrWrap(urwid.Text('COMMENTS'), 'header')))]

        self.header = urwid.Columns(self.header_content, dividechars=1)
        self.footer = urwid.AttrMap(
            urwid.Text('Welcome in pyhn by socketubs (https://github.com/socketubs/pyhn)', align='center'),
            'footer')

        self.view = urwid.Frame(urwid.AttrWrap(self.listbox, 'body'), header=self.header, footer=self.footer)
        self.loop = urwid.MainLoop(
                        self.view, self.palette,
                        screen=self.ui, handle_mouse=False,
                        unhandled_input=self.keystroke)

        self.build_help()
        self.already_build = True

    def set_help(self):
        """ Set help msg in footer """
        self.view.set_footer(urwid.AttrWrap(urwid.Text(self.help, align="center"), 'help'))

    def set_footer(self, msg, style="normal"):
        """ Set centered footer message """
        if style == "normal":
            self.footer = urwid.AttrWrap(urwid.Text(msg), 'footer')
            self.view.set_footer(self.footer)
        elif style == "error":
            self.footer = urwid.AttrWrap(urwid.Text(msg), 'footer-error')
            self.view.set_footer(self.footer)

    def set_header(self, msg):
        """ Set header story message """
        self.header_content[1] = urwid.AttrWrap(urwid.Text(msg, align="center"), 'title')
        self.view.set_header(urwid.Columns(self.header_content, dividechars=1))

    def keystroke(self, input):
        """ All key bindings are computed here """
        # QUIT
        if input in ('q', 'Q'):
            raise urwid.ExitMainLoop()
        # LINKS
        if input in self.bindings['open_story_link'].split(','):
            self.open_webbrowser(self.listbox.get_focus()[0].url)
        if input in self.bindings['show_story_link'].split(','):
            self.set_footer(self.listbox.get_focus()[0].url)
        if input in self.bindings['open_comments_link'].split(','):
            if self.listbox.get_focus()[0].comments_url == -1:
                self.set_footer('No comments')
            else:
                self.open_webbrowser(self.listbox.get_focus()[0].comments_url)
        if input in self.bindings['show_comments_link'].split(','):
            if self.listbox.get_focus()[0].comments_url == -1:
                self.set_footer('No comments')
            else:
                self.set_footer(self.listbox.get_focus()[0].comments_url)
        if input in self.bindings['open_submitter_link'].split(','):
            if self.listbox.get_focus()[0].submitter_url == -1:
                self.set_footer('Anonymous submitter')
            else:
                self.open_webbrowser(self.listbox.get_focus()[0].submitter_url)
        if input in self.bindings['show_submitter_link'].split(','):
            if self.listbox.get_focus()[0].submitter_url == -1:
                self.set_footer('Anonymous submitter')
            else:
                self.set_footer(self.listbox.get_focus()[0].submitter_url)
        # MOVEMENTS
        if input in self.bindings['down'].split(','):
            if self.listbox.focus_position - 1 in self.walker.positions():
                self.listbox.set_focus(self.walker.prev_position(self.listbox.focus_position))
        if input in self.bindings['up'].split(','):
            if self.listbox.focus_position + 1 in self.walker.positions():
                self.listbox.set_focus(self.walker.next_position(self.listbox.focus_position))
        if input in self.bindings['page_up'].split(','):
            self.listbox._keypress_page_up(self.ui.get_cols_rows())
        if input in self.bindings['page_down'].split(','):
            self.listbox._keypress_page_down(self.ui.get_cols_rows())
        if input in self.bindings['first_story'].split(','):
            self.listbox.set_focus(self.walker.positions()[0])
        if input in self.bindings['last_story'].split(','):
            self.listbox.set_focus(self.walker.positions()[-1])
        # STORIES
        if input in ('n',):
            threading.Thread(None, self.async_refresher, None, ('newest', 'NEWEST STORIES'), {}).start()
        if input in ('t',):
            threading.Thread(None, self.async_refresher, None, ('top', 'TOP STORIES'), {}).start()
        if input in ('b',):
            self.set_footer('Syncing best stories...')
            threading.Thread(None, self.async_refresher, None, ('best', 'BEST STORIES'), {}).start()
        # OTHERS
        if input in self.bindings['refresh'].split(','):
            threading.Thread(None, self.async_refresher, None, (), {}).start()
        if input in self.bindings['reload_config'].split(','):
            self.reload_config()
        if input in ('h', 'H', '?'):
            keys = True
            while True:
                if keys:
                    self.ui.draw_screen(self.ui.get_cols_rows(), self.help.render(self.ui.get_cols_rows(), True))
                    keys = self.ui.get_input()
                    if 'h' or 'H' or '?' or 'escape' in keys:
                        break

    def async_refresher(self, which=None, header=None):
        if which is None:
            which = self.which
        if self.cache_manager.is_outdated(which):
            self.cache_manager.refresh(which)
        stories = self.cache_manager.get_stories(which)
        self.update_stories(stories)
        if header is not None:
            self.set_header(header)
            self.which = which
        self.loop.draw_screen()

    def update_stories(self, stories):
        """ Reload listbox and walker with new stories """
        items = []
        for story in stories:
            items.append(ItemWidget(story))

        if self.already_build:
            self.walker[:] = items
            self.update()
        else:
            self.walker = urwid.SimpleListWalker(items)
            self.listbox = urwid.ListBox(self.walker)

    def open_webbrowser(self, url):
        """ Handle url and open sub process with web browser """
        if self.config.parser.get('settings', 'browser_cmd') == "__default__":
            python_bin = sys.executable
            browser_output = subprocess.Popen(
                [python_bin, '-m', 'webbrowser', '-t', url],
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE)
        else:
            cmd = self.config.parser.get('settings', 'browser_cmd')
            try:
                p = subprocess.Popen(
                    cmd.replace('__url__', url),
                    shell=True,
                    close_fds=True,
                    stderr=subprocess.PIPE)

                returncode = p.wait()
            except KeyboardInterrupt:
                stderr = "User keyboard interrupt detected!"
                self.set_footer(stderr, style="error")
                return
            if returncode > 0:
                stderr = p.communicate()[1]
                self.set_footer("%s" % stderr, style="error")

    def update(self):
        """ Update footer about focus story """
        focus = self.listbox.get_focus()[0]
        if focus.submitter == "":
            msg = "submitted %s" % focus.published_time
        else:
            msg = "submitted %s by %s" % (focus.published_time, focus.submitter)

        self.set_footer(msg)

    def reload_config(self):
        """
        Create new Config object, reload colors, refresh cache
        if needed and redraw screen.
        """
        self.set_footer('Reloading configuration')
        self.config = Config()
        self.build_help()
        self.palette = self.config.get_palette()
        self.build_interface()
        self.loop.draw_screen()
        self.set_footer('Configuration file reloaded!')

        if self.config.parser.get('settings', 'cache') != self.cache_manager.cache_path:
            self.cache_manager.cache_path = self.config.parser.get('settings', 'cache')

    def run(self):
        """ Run the loop """
        urwid.connect_signal(self.walker, 'modified', self.update)

        try:
            self.loop.run()
        except KeyboardInterrupt:
            urwid.ExitMainLoop()
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

#http://www.beyondlogic.org/usbnutshell/usb1.shtml

import usb

dev = None
cfg = None
intf = None
epin = None
epout = None

def find( vendor=0x03f0, product=0x0121 ):
    """Returns list of usb device objects of all connected HPs.
    """
    return usb.core.find( find_all=True, idVendor=vendor, idProduct=product )

def connect( device=0, vendor=0x03f0, product=0x0121 ):
    """Connects to the HP49 in the system numbered by device.
       Returns True if successfull, else False.
    """
    global dev, cfg, intf, epin, epout

    # TODO: this should not be a global singleton
    dev = usb.core.find( find_all=True, idVendor=vendor, idProduct=product )[device]
    if not dev:
      return False

    # use default config
    dev.set_configuration()

    # so far it has always been like this on my hp50g
    assert( dev.bLength == 18 )
    assert( dev.bDescriptorType == 1 )
    assert( dev.bDeviceClass == 255 )
    assert( dev.bDeviceProtocol == 0 )
    assert( dev.bDeviceSubClass == 0 )
    assert( dev.bNumConfigurations == 1 )

    # get an endpoint instance
    cfg = dev.get_active_configuration()

    # so far it has always been like this on my hp50g
    assert( cfg.bNumInterfaces == 1 )

    intf = cfg[(0,0)] # (interface index, altsetting index)

    # so far it has always been like this on my hp50g
    assert( intf.bInterfaceNumber == 0 )
    assert( intf.bAlternateSetting == 0 )

    # not required with just one alt setting
    #intf.set_altsetting()

    # so far it has always been like this on my hp50g
    assert( intf.bNumEndpoints == 2 )

    # hardcoded assumption. should filter for in/out
    epin = intf[0]
    epout = intf[1]

    # so far it has always been like this on my hp50g
    assert( usb.util.endpoint_direction( epin.bEndpointAddress ) == usb.util.ENDPOINT_IN )
    assert( usb.util.endpoint_type( epin.bEndpointAddress ) == usb.util.ENDPOINT_TYPE_ISO )
    assert( usb.util.endpoint_direction( epout.bEndpointAddress ) == usb.util.ENDPOINT_OUT )
    assert( usb.util.endpoint_type( epout.bEndpointAddress ) == usb.util.ENDPOINT_TYPE_INTR )
    assert( epin.bEndpointAddress == 129 ) # srsly?
    assert( epout.bEndpointAddress == 3 )

    return True

def write( data, timeout=1000 ):
    """Sends data to calculator. data can be a string or array.
       Returns the number of bytes written.
    """
    global epout
    return epout.write( data, timeout=timeout )

def read( length, timeout=1000 ):
    """Reads length bytes of data from the calculator and returns them as
       array.array of bytes.
    """
    global epin
    inp = []
    while len( inp ) < length:
    	inp += epin.read( length-len(inp), timeout=timeout )
    return inp

def reset():
    """Resets USB state.
    """
    global dev
    dev.reset()

def flush( self, timeout=200 ):
    """Flushes the input buffer.
    """
    while True:
      try:
        read( 128, timeout=timeout )
      except:
        break
    reset()
<commit_msg>added until option to read()<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

#http://www.beyondlogic.org/usbnutshell/usb1.shtml

import usb

dev = None
cfg = None
intf = None
epin = None
epout = None

def find( vendor=0x03f0, product=0x0121 ):
    """Returns list of usb device objects of all connected HPs.
    """
    return usb.core.find( find_all=True, idVendor=vendor, idProduct=product )

def connect( device=0, vendor=0x03f0, product=0x0121 ):
    """Connects to the HP49 in the system numbered by device.
       Returns True if successfull, else False.
    """
    global dev, cfg, intf, epin, epout

    # TODO: this should not be a global singleton
    dev = usb.core.find( find_all=True, idVendor=vendor, idProduct=product )[device]
    if not dev:
      return False

    # use default config
    dev.set_configuration()

    # so far it has always been like this on my hp50g
    assert( dev.bLength == 18 )
    assert( dev.bDescriptorType == 1 )
    assert( dev.bDeviceClass == 255 )
    assert( dev.bDeviceProtocol == 0 )
    assert( dev.bDeviceSubClass == 0 )
    assert( dev.bNumConfigurations == 1 )

    # get an endpoint instance
    cfg = dev.get_active_configuration()

    # so far it has always been like this on my hp50g
    assert( cfg.bNumInterfaces == 1 )

    intf = cfg[(0,0)] # (interface index, altsetting index)

    # so far it has always been like this on my hp50g
    assert( intf.bInterfaceNumber == 0 )
    assert( intf.bAlternateSetting == 0 )

    # not required with just one alt setting
    #intf.set_altsetting()

    # so far it has always been like this on my hp50g
    assert( intf.bNumEndpoints == 2 )

    # hardcoded assumption. should filter for in/out
    epin = intf[0]
    epout = intf[1]

    # so far it has always been like this on my hp50g
    assert( usb.util.endpoint_direction( epin.bEndpointAddress ) == usb.util.ENDPOINT_IN )
    assert( usb.util.endpoint_type( epin.bEndpointAddress ) == usb.util.ENDPOINT_TYPE_ISO )
    assert( usb.util.endpoint_direction( epout.bEndpointAddress ) == usb.util.ENDPOINT_OUT )
    assert( usb.util.endpoint_type( epout.bEndpointAddress ) == usb.util.ENDPOINT_TYPE_INTR )
    assert( epin.bEndpointAddress == 129 ) # srsly?
    assert( epout.bEndpointAddress == 3 )

    return True

def write( data, timeout=1000 ):
    """Sends data to calculator. data can be a string or array.
       Returns the number of bytes written.
    """
    global epout
    return epout.write( data, timeout=timeout )

def read( length=1, timeout=1000, until=False ):
    """Reads length bytes of data from the calculator and returns them as
       array.array of bytes.
    """
    global epin
    inp = []
    if until==False:
      while len( inp ) < length:
    	inp += epin.read( length-len(inp), timeout=timeout )
    else:
      inp += epin.read( 1, timeout=timeout )
      while inp[-1] != until:
        inp += epin.read( 1, timeout=timeout )
    return inp

def reset():
    """Resets USB state.
    """
    global dev
    dev.reset()

def flush( self, timeout=200 ):
    """Flushes the input buffer.
    """
    while True:
      try:
        read( 128, timeout=timeout )
      except:
        break
    reset()
<|endoftext|>


<commit_before>import cv2
import sqlite3

class Marker(object):

    def __init__(self):
        self.rect = ((0,0),(0,0))
        self.line = ((0,0),(0,0))

        self.mode = 'l'

        self.objs = list()

    def set_img(self, img):
        self.img     = img
        self.overlay = img.copy()

    def set_mode(self, mode):
        self.mode = mode

    def on_mouse(self, event, x, y, flag, params):
        if event == cv2.EVENT_LBUTTONDOWN:
            self.drawing = True
            if self.mode == 'l':
                self.line[0] = (x, y)
            elif self.mode == 'r':
                self.rect[0] = (x, y)
        elif event == cv2.EVENT_MOUSEMOVE:
            if self.drawing is True:
                if self.mode == 'l':
                    cv2.line(self.overlay, self.line[0], self.line[1], (0,255,255))
                elif self.mode == 'r':
                    cv2.rectangle(self.overlay, self.rect[0], self.rect[1], (0,255,255))
            elif self.dragging is True:
                pass
        elif event == cv2.EVENT_LBUTTONUP:
            self.drawing = False
            if self.mode == 'l':
                self.line[1] = (x, y)
                cv2.line(self.overlay, self.line[0], self.line[1], (0,255,255))
            elif self.mode == 'r':
                self.rect[1] = (x, y)
                cv2.rectangle(self.overlay, self.rect[0], self.rect[1], (0,255,255))
        elif event == cv2.EVENT_RBUTTONDOWN:
            self.dragging = True
        elif event == cv2.EVENT_RBUTTONUP:
            self.dragging = False

    def clear(self):
        self.rect = ((0,0),(0,0))
        self.line = ((0,0),(0,0))

    def clear_all(self):
        self.objs = list()

if __name__=="__main__":
    window_name = "Object Marker v0.1"
    input_path  = raw_input("Enter path to input directory: ")
    output_path = raw_input("Enter path to output database: ")

    cap     = cv2.VideoCapture(input_path)
    db      = sqlite3.connect(output_path)
    counter = 0

    marker  = Marker()
    cv2.setMouseCallback(window_name, marker.on_mouse, None)

    buf     = list()

    while True:
        img = cap.read()[1]
        buf.append(img.copy());

        cv2.imshow(window_name, img)
        marker.set_img(img)

        key = cv2.waitKey(100) & 0xff
        if key == 27:         # Stop if ESC is pressed
            break
        elif key == ord('s'): # Skip this image
            print "Skip frame no. " + counter
        elif key == ord('l'): # Add object marked line
            pass
        elif key == ord('r'): # Add object marked rectangle
            pass
        elif key == ord('z'): # Clear all unsaved
            marker.clear()
        elif key == ord('c'): # Clear all
            marker.clearall()
        elif key == ord(' '): # Save all and next
            pass
        elif int(key) in range(0,10): # Labeling last marked object
            pass
        counter += 1<commit_msg>First commit<commit_after>import cv2
import sqlite3

from itertools import izip

def on_mouse(self, event, x, y, flag, marks):
    if event == cv2.EVENT_LBUTTONDOWN or event == cv2.EVENT_LBUTTONUP:
        marks.append((x, y, 'r'))
    elif event == cv2.EVENT_MOUSEMOVE:
        _, _, shape = marks.pop()
        marks.append((x, y, shape))
    elif event == cv2.EVENT_RBUTTONDOWN or event == cv2.EVENT_RBUTTONUP:
        marks.append((x, y, 'l'))

def pairwise(iterable):
    a = iter(iterable)
    return izip(a, a)

def grouped(iterable, n):
    return izip(*[iter(iterable)]*n)

def mark_img(img, marks):
    buf = img.copy()
    for (x0, y0, shape), (x1, y1, shape) in pairwise(marks):
        if shape == 'r':
            cv2.rectangle(buf, (x0, y0), (x1, y1), (0,255,255))
        elif shape == 'l':
            cv2.line(buf, (x0, y0), (x1, y1), (0,255,255))
    return buf

def save_all(img_id, marks, labels, store):
    c = store.cursor()

    objects = list()
    for (p0, p1, p2, p3),label in zip(grouped(marks,4), labels):
        x0, y0, shape = p0
        x1, y1, _     = p1
        x2, y2, _     = p2
        x3, y3, _     = p3
        if shape == 'r':
            list.append((img_id,x0,y0,x1,y1,x2,y2,x3,y3,label))
        elif shape == 'l':
            list.append((img_id,x2,y2,x3,y3,x0,y0,x1,y1,label))

    c.executemany("INSERT INTO objects VALUES (?,?,?,?,?,?,?,?,?,?)", objects)
    store.commit()

if __name__=="__main__":
    window_name = "Object Marker v0.1"
    input_path  = raw_input("Enter path to input directory: ")
    output_path = raw_input("Enter path to output database: ")

    cap     = cv2.VideoCapture(input_path)
    store   = sqlite3.connect(output_path)
    marks   = list()
    labels  = list()
    counter = 0

    cv2.setMouseCallback(window_name, on_mouse, marks)

    while True:
        img    = cap.read()[1]
        marks  = []
        labels = []

        while True:
            marked = mark_img(img, marks)
            cv2.imshow(window_name, marked)

            key = cv2.waitKey(100) & 0xff
            if key == 27:         # Stop if ESC is pressed
                return
            elif key == ord('x'): # Skip this image
                print "Skip frame no. " + counter
                break
            elif key == ord('d'): # Delete last mark
                marks = marks[:-2]
            elif key == ord('r'): # Remove last label
                labels  = labels[:-1]
            elif key == ord('c'): # Clear all
                marks = []
                labels  = []
            elif key == ord('s'): # Save all
                save_all(counter, marks, labels, store)
                img = marked
                marks = []
            elif key == ord(' '): # Save all and next
                save_all(counter, marks, labels, store)
                break
            elif int(key) in range(0,10): # Labeling last object
                labels.append(int(key))
        counter += 1
    store.close()<|endoftext|>


<commit_before>import unittest

class UploaderTestCase(unittest.TestCase):
    pass

if __name__ == '__main__':
    unittest.main()
<commit_msg>Add uploader tests<commit_after>import unittest
try:
    from unittest import mock
except ImportError:
    import mock
from binstar_client import errors
import binstar_client.utils.notebook
from binstar_client.utils.notebook import Uploader

class UploaderTestCase(unittest.TestCase):
    def test_release(self):
        binstar = mock.MagicMock()
        binstar.release.return_value = 'release'
        uploader = Uploader(binstar, 'project')
        self.assertEqual(uploader.release, 'release')

    def test_release_not_exist(self):
        binstar = mock.MagicMock()
        binstar.release.side_effect = errors.NotFound([])
        binstar.add_release.return_value = 'release'
        uploader = Uploader(binstar, 'project')
        self.assertEqual(uploader.release, 'release')

    def test_package(self):
        binstar = mock.MagicMock()
        binstar.package.side_effect = errors.NotFound([])
        binstar.add_package.return_value = 'package'
        uploader = Uploader(binstar, 'project')
        self.assertEqual(uploader.package, 'package')

    def test_version(self):
        binstar = mock.MagicMock
        uploader = Uploader(binstar, 'project', version='version')
        self.assertEqual(uploader.version, 'version')

        uploader = Uploader(binstar, 'project')
        self.assertIsInstance(uploader.version, str)

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


<commit_before>class ASTVisitor():
  def visit(self, astnode):
    'A read-only function which looks at a single AST node.'
    pass
  def return_value(self):
    return None

class ASTNode(object):
  def __init__(self):
    self.parent = None
    self._children = []

  @property
  def children(self):
    return self._children
  @children.setter
  def children(self, children):
    self._children = children
    for child in children:
      child.parent = self

  def pprint(self,indent=''):
    '''Recursively prints a formatted string representation of the AST.'''
    pfx = indent
    if isinstance(self, ASTNode):
        printed = ''.join([pfx,self.__class__.__name__])

        if any(isinstance(child, ASTNode) for child in self.children):
            for i, child in enumerate(self.children):
                printed += '\n'
                printed += child.pprint(indent+'  ')
            return printed
        else:
            # None of the children as nodes, simply join their repr on a single
            # line.
            printed += ', '.join(repr(child) for child in self.children)
            return printed

    else:
        printed = pfx + repr(self)
        return printed

  def walk(self, visitor):
    '''Traverses an AST, calling visitor.visit() on every node.
    This is a depth-first, pre-order traversal. Parents will be visited before
    any children, children will be visited in order, and (by extension) a node's
    children will all be visited before its siblings.
    The visitor may modify attributes, but may not add or delete nodes.'''
    # TODO
    visitor.visit(self)
    for child in self.children:
        child.walk(visitor)
    return visitor.return_value()

class ASTProgram(ASTNode):
  def __init__(self, statements):
    super().__init__()
    self.children = statements

class ASTImport(ASTNode):
  def __init__(self, mod):
    super().__init__()
    self.mod = mod
  @property
  def module(self):
    return self.mod

class ASTComponent(ASTNode):
  def __init__(self,name,expressions):
    super().__init__()
    self.children = [ASTID(name),*expressions]

  @property
  def name(self): # Return an element of self.children
    return self.children[0].name
  @property
  def expressions(self): # Return one or more children
    return self.children[1:]

class ASTInputExpr(ASTNode):
  def __init__(self,declaration_list=None):
    super().__init__()
    if declaration_list:
        self.children = declaration_list

class ASTOutputExpr(ASTNode):
  def __init__(self,declaration_list=None):
    super().__init__()
    if declaration_list:
        self.children = declaration_list

class ASTAssignmentExpr(ASTNode):
  def __init__(self,ID,expression):
    super().__init__()
    self.children = [ASTID(ID), expression]
  @property
  def binding(self):
    return self.children[0]
  @property
  def value(self):
    return self.children[1]

class ASTEvalExpr(ASTNode):
  def __init__(self,oper,argms):
    super().__init__()
    if len(argms) > 0:
      self.children = [ASTID(oper),*argms]
    else:
      self.children = [ASTID(oper)]
  @property
  def op(self):
    return self.children[0]
  @property
  def args(self):
    return self.children[1:]

class ASTID(ASTNode):
  def __init__(self, name, typedecl=None):
    super().__init__()
    self.name = name
    self.type = typedecl

class ASTLiteral(ASTNode):
  def __init__(self, value):
    super().__init__()
    self.value = value
    self.type = 'Scalar'

<commit_msg>added docstrings<commit_after>class ASTVisitor():
    """
    A class that takes no arguments and is generally used with the walk method of an AST

    Parameters
    ----------

    Returns
    -------
    visit(node): None
        read only function which looks at a single AST node
    return_value: None
        returns nothing if the walker exits correctly

    Examples
    --------
    >>> a = ASTVisitor()
    >>> b = ASTNode()
    >>> a.visit(b)
    >>> a.return_value()
    >>> assert isinstance( a, ASTVisitor ) == True
    """
  def visit(self, astnode):
    'A read-only function which looks at a single AST node.'
    pass
  def return_value(self):
    return None

class ASTNode(object):
    """
    A class that takes no arguments and defines a node in an abstract syntax tree

    Parameters
    ----------

    Returns
    -------
    @property
    children: self._children
        returns children of a given node
    @children.setter
    children(child_list): None
        returns nothing, but sets the children of a given node to child_list
    pprint(indent): printed
        returns a string representing the human-readable structure of the AST
    walk(visitor): visitor.return_value()
        returns the value of the return_value method of a visitor that does a depth-first, pre-order traversal of the AST

    Examples
    --------
    >>> a,b,c = ASTNode(),ASTNode(),ASTNode()
    >>> a.parent,b.parent,c.parent
    (None, None, None)
    >>> a.children = b
    Traceback (most recent call last):
        ...
    TypeError: 'ASTNode' object is not iterable
    >>> a.children = [b,c]
    >>> assert [id(a)]*2==[id(b.parent),id(c.parent)]
    >>> [child.__class__.__name__ for child in a.children]
    ['ASTNode', 'ASTNode']
    >>> print(a.pprint())
    ASTNode
      ASTNode
      ASTNode
    >>> d = ASTVisitor()
    >>> a.walk(d)
    """
  def __init__(self):
    self.parent = None
    self._children = []

  @property
  def children(self):
    return self._children
  @children.setter
  def children(self, children):
    self._children = children
    for child in children:
      child.parent = self

  def pprint(self,indent=''):
    '''Recursively prints a formatted string representation of the AST.'''
    pfx = indent
    if isinstance(self, ASTNode):
        printed = ''.join([pfx,self.__class__.__name__])

        if any(isinstance(child, ASTNode) for child in self.children):
            for i, child in enumerate(self.children):
                printed += '\n'
                printed += child.pprint(indent+'  ')
            return printed
        else:
            # None of the children as nodes, simply join their repr on a single
            # line.
            printed += ', '.join(repr(child) for child in self.children)
            return printed

    else:
        printed = pfx + repr(self)
        return printed

  def walk(self, visitor):
    '''Traverses an AST, calling visitor.visit() on every node.
    This is a depth-first, pre-order traversal. Parents will be visited before
    any children, children will be visited in order, and (by extension) a node's
    children will all be visited before its siblings.
    The visitor may modify attributes, but may not add or delete nodes.'''
    # TODO
    visitor.visit(self)
    for child in self.children:
        child.walk(visitor)
    return visitor.return_value()

class ASTProgram(ASTNode):
    """
    A class that takes a list of statements as its argument and defines the overarching program node
    in an abstract syntax tree

    Parameters
    ----------
    statements: a list of statements, which are AST nodes

    Returns
    -------
    __init__(statements): None
        returns nothing, but sets the children of the ASTProgram node to the statement list
    Examples
    --------
    >>> b,c = ASTNode(),ASTNode()
    >>> b.parent,c.parent
    (None, None)
    >>> statements = [b,c]
    >>> a = ASTProgram(statements)
    >>> assert [id(a)]*2==[id(b.parent),id(c.parent)]
    >>> print(a.pprint())
    ASTProgram
      ASTNode
      ASTNode
    >>> d = ASTVisitor()
    >>> a.walk(d)
    """
  def __init__(self, statements):
    super().__init__()
    self.children = statements

class ASTImport(ASTNode):
    """
    A class that takes a string which is the name of a module as its argument and defines an import
    node in an abstract syntax tree

    Parameters
    ----------
    mod: a string representing a module

    Returns
    -------
    __init__(mod): None
        returns nothing, but sets the mod attribute of the ASTImport node to mod
    module: self.mod
        returns the string representing the module to be imported

    Examples
    --------
    >>> b,c = ASTImport('mod'),ASTNode()
    >>> statements = [b,c]
    >>> a = ASTProgram(statements)
    >>> assert [id(a)]*2==[id(b.parent),id(c.parent)]
    >>> print(a.pprint())
    ASTProgram
      ASTImport
      ASTNode
    >>> b.module
    'mod'
    """
  def __init__(self, mod):
    super().__init__()
    self.mod = mod
  @property
  def module(self):
    return self.mod

class ASTComponent(ASTNode):
    """
    A class that takes a string which serves as the name of the component and expressions as its argument
    and defines a component node in an abstract syntax tree

    Parameters
    ----------
    name: a string representing the name of the component
    expressions: a list of expressions, which are AST nodes

    Returns
    -------
    __init__(name,expressions): None
        returns nothing, but sets the children of ASTComponent to ASTID(name) and the expressions
    name: self.children[0].name
        returns a string representing the name of the component
    expressions: self.children[1:]
        returns a list of AST nodes representing the remaining children of the component node

    Examples
    --------
    >>> name,expressions = 'comp1',[ASTNode()]
    >>> a = ASTComponent(name,expressions)
    >>> statements = [a]
    >>> b = ASTProgram(statements)
    >>> a.name
    'comp1'
    >>> [child.__class__.__name__ for child in a.expressions]
    ['ASTNode']
    >>> print(b.pprint())
    ASTProgram
      ASTComponent
        ASTID
        ASTNode
    """
  def __init__(self,name,expressions):
    super().__init__()
    self.children = [ASTID(name),*expressions]

  @property
  def name(self): # Return an element of self.children
    return self.children[0].name
  @property
  def expressions(self): # Return one or more children
    return self.children[1:]

class ASTInputExpr(ASTNode):
    """
    A class that takes a list of declarations as its argument
    and defines an input expression node in an abstract syntax tree

    Parameters
    ----------
    declaration_list: a list of declarations, which are AST nodes

    Returns
    -------
    __init__(declaration_list): None
        returns nothing, but sets the children of ASTInputExpr to declaration_list

    Examples
    --------
    >>> declaration_list = [ASTID('name'),ASTNode()]
    >>> a = ASTInputExpr(declaration_list)
    >>> [child.__class__.__name__ for child in a.children]
    ['ASTID','ASTNode']
    >>> print(a.pprint())
    ASTInputExpr
      ASTID
      ASTNode
    """
  def __init__(self,declaration_list=None):
    super().__init__()
    if declaration_list:
        self.children = declaration_list

class ASTOutputExpr(ASTNode):
    """
    A class that takes a list of declarations as its argument
    and defines an output expression node in an abstract syntax tree

    Parameters
    ----------
    declaration_list: a list of declarations, which are AST nodes

    Returns
    -------
    __init__(declaration_list): None
        returns nothing, but sets the children of ASTOutputExpr to declaration_list

    Examples
    --------
    >>> a = ASTOutputExpr()
    >>> [child.__class__.__name__ for child in a.children]
    []
    >>> declaration_list = [ASTID('name'),ASTNode()]
    >>> a = ASTOutputExpr(declaration_list)
    >>> [child.parent.__class__.__name__ for child in a.children]
    ['ASTOutputExpr','ASTOutputExpr']
    """
  def __init__(self,declaration_list=None):
    super().__init__()
    if declaration_list:
        self.children = declaration_list

class ASTAssignmentExpr(ASTNode):
    """
    A class that takes an ID to assign to and an expression to be assigned as its arguments
    and defines an assignment expression node in an abstract syntax tree

    Parameters
    ----------
    ID: string
    expression: an AST node

    Returns
    -------
    __init__(ID,expression): None
        returns nothing, but sets the children of ASTAssignmentExpr to ASTID(ID) and expression
    binding: self.children[0]
        returns the ASTID node representing the variable to be assigned to
    value: self.children[1]
        returns the AST node representing the value of the variable

    Examples
    --------
    >>> a = ASTAssignmentExpr('var1',ASTNode())
    >>> a.binding.__class__.__name__
    'ASTID'
    >>> a.value.__class__.__name__
    'ASTNode'
    """
  def __init__(self,ID,expression):
    super().__init__()
    self.children = [ASTID(ID), expression]
  @property
  def binding(self):
    return self.children[0]
  @property
  def value(self):
    return self.children[1]

class ASTEvalExpr(ASTNode):
    """
    A class that takes an operator and its arguments as its arguments
    and defines an expression evaluation node in an abstract syntax tree

    Parameters
    ----------
    oper: some function represented as a string
    argms: a list of AST nodes

    Returns
    -------
    __init__(oper,argms): None
        returns nothing, but sets the children of ASTEvalExpr to ASTID(oper) and argms
    op: self.children[0]
        returns the ASTID node representing the operator/function
    args: self.children[1:]
        returns the list of AST nodes representing the arguments of the operator/function

    Examples
    --------
    >>> a = ASTEvalExpr('op',[ASTNode(),ASTNode()])
    >>> a.op.__class__.__name__
    'ASTID'
    >>> [child.__class__.__name__ for child in a.args]
    ['ASTNode','ASTNode']
    >>> a = ASTEvalExpr('op2',[])
    >>> [child.__class__.__name__ for child in a.children]
    ['ASTID']
    """
  def __init__(self,oper,argms):
    super().__init__()
    if len(argms) > 0:
      self.children = [ASTID(oper),*argms]
    else:
      self.children = [ASTID(oper)]
  @property
  def op(self):
    return self.children[0]
  @property
  def args(self):
    return self.children[1:]

class ASTID(ASTNode):
    """
    A class that takes a name and a declaration of its type as its arguments
    and defines an ID node in an abstract syntax tree

    Parameters
    ----------
    name: some string
    typedecl: some string

    Returns
    -------
    __init__(name,typedecl): None
        returns nothing, but sets the name attribute of the node to name and the type attribute of the node to typedecl

    Examples
    --------
    >>> a = ASTID('Joe','component')
    >>> a.__class__.__name__
    'ASTID'
    >>> a.name
    'Joe'
    >>> a.type
    'component'
    """
  def __init__(self, name, typedecl=None):
    super().__init__()
    self.name = name
    self.type = typedecl

class ASTLiteral(ASTNode):
    """
    A class that takes a value as its argument
    and defines a literal node in an abstract syntax tree

    Parameters
    ----------
    value: some scalar

    Returns
    -------
    __init__(value): None
        returns nothing, but sets the value attribute of the node to value and the type attribute of the node to 'Scalar'

    Examples
    --------
    >>> a = ASTLiteral(5)
    >>> a.__class__.__name__
    'ASTLiteral'
    >>> a.value
    5
    >>> a.type
    'Scalar'
    """
  def __init__(self, value):
    super().__init__()
    self.value = value
    self.type = 'Scalar'

<|endoftext|>


<commit_before>#!/usr/bin/env python
"""
Rename files or directories using regular expression.
Does nothing without the --force option.

example:
    ./pyrename.py '(.*)\.py' '\g<1>_renamed.py'

"""
import itertools
import argparse
import logging
import shutil
import os
import re

def setup_logging():
    lfmt = '[%(asctime)s][%(process)5d][%(levelname)-5s]: %(message)s'
    dfmt = "%Y-%m-%d %H:%M:%S"
    logging.basicConfig(level=logging.INFO, format=lfmt, datefmt=dfmt)

setup_logging()

def log_option(opts, attr):
    obj = getattr(opts, attr)

    if isinstance(obj, list) or isinstance(obj, tuple):
        for i, item in enumerate(obj):
            logging.info('%-10s = %s', '{}[{:d}]'.format(attr, i), obj[i])
        return

    if isinstance(obj, set):
        i = 0
        for item in obj:
            logging.info('%-10s = %s', '{}[{:d}]'.format(attr, i), item)
            i += 1
        return

    logging.info('%-10s = %s', attr, obj)

class Options(object):
    def __init__(self, work):
        self.pattern = None
        self.replace = None
        self.nomatch = None
        self.exclude = []
        self.top     = os.path.realpath(os.path.expanduser(work))

        self.recursive  = False
        self.dirs       = False
        self.files      = True
        self.both       = False
        self.force      = False
        self.ignorecase = False
        self.silent     = False

def get_arguments(work, args=None):
    parser = argparse.ArgumentParser(description=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter)
    opts = Options(work)

    # regular expressions
    parser.add_argument(dest='pattern', type=str,
        help='expression: pattern')
    parser.add_argument(dest='replace', type=str,
        help='expression: replace')
    parser.add_argument('--nomatch', type=str, default=opts.nomatch,
        help='expression: nomatch')
    parser.add_argument('--exclude', type=str, nargs='*', default=opts.exclude,
        help='list of files to exclude')

    # searching
    parser.add_argument('-t', '--top', type=str, default=opts.top,
        help='top level directory')
    parser.add_argument('-r', '--recursive', action='store_true',
        help='search recursivly')

    fgroup = parser.add_mutually_exclusive_group()
    fgroup.add_argument('-d', '--dirs' , action='store_true',
        help='match directories')
    fgroup.add_argument('-b', '--both' , action='store_true',
        help='match directories and files')

    # mode
    parser.add_argument('-f', '--force', action='store_true',
        help='rename files')
    parser.add_argument('-i', '--ignorecase', action='store_true',
        help='ignore case')
    parser.add_argument('-s', '--silent', action='store_true',
        help='silent')

    # parse
    parser.parse_args(args, namespace=opts)

    # logging
    if opts.silent:
        logging.getLogger().setLevel(logging.ERROR)

    # update opts
    if opts.dirs:
        opts.files = False
    if opts.both:
        opts.dirs  = True
        opts.files = True

    # fix path
    opts.top = os.path.realpath(opts.top)

    # fix exclude
    opts.exclude = set(opts.exclude)
    opts.exclude.add(os.path.basename(__file__))

    # log options
    log_option(opts, 'pattern')
    log_option(opts, 'replace')
    log_option(opts, 'nomatch')
    log_option(opts, 'top')

    log_option(opts, 'recursive')
    log_option(opts, 'files')
    log_option(opts, 'dirs')
    log_option(opts, 'both')

    log_option(opts, 'exclude')

    log_option(opts, 'force')
    log_option(opts, 'ignorecase')

    return opts

def walk(top, r=False, dirs=False, files=False):
    if dirs or files:
        if r:
            for root, dbases, fbases in os.walk(top, followlinks=False):
                if dirs and files:
                    for p in itertools.chain(dbases, fbases):
                        yield root, p
                elif dirs:
                    for d in dbases:
                        yield root, d
                elif files:
                    for f in fbases:
                        yield root, f
        else:
            root, dbases, fbases = next(os.walk(top, followlinks=False))
            if dirs and files:
                for p in itertools.chain(dbases, fbases):
                    yield root, p
            elif dirs:
                for d in dbases:
                    yield root, d
            elif files:
                for f in fbases:
                    yield root, f
    else:
        raise StopIteration

def main(args=None):
    work = os.getcwd()
    opts = get_arguments(work, args)

    # check top level directory
    if not os.path.exists(opts.top) or not os.path.isdir(opts.top):
        raise RuntimeError('invalid top level directory: %s' % opts.top)

    # compile regex
    if opts.ignorecase:
        regex1 = re.compile(opts.pattern, re.IGNORECASE)
        try:
            regex2 = re.compile(opts.nomatch, re.IGNORECASE)
        except TypeError:
            regex2 = None
    else:
        regex1 = re.compile(opts.pattern)
        try:
            regex2 = re.compile(opts.nomatch)
        except TypeError:
            regex2 = None

    # record errors
    error = False

    # find paths
    opaths = []
    npaths = []
    for root, p in walk(opts.top, r=opts.recursive, dirs=opts.dirs, files=opts.files):
        match = regex1.match(p)
        if match:
            # exclude list
            if p in opts.exclude:
                logging.info('path excluded!\n\n\t%s\n', os.path.join(root, p))
                continue

            # exclude nomatch
            if not regex2 is None and regex2.match(p):
                logging.info('path excluded!\n\n\t%s\n', os.path.join(root, p))
                continue

            # construct new base
            try:
                n = regex1.sub(opts.replace, p)
            except re.error:
                logging.exception('regex error')
                error = True
                n = p

            # construct paths
            opath = os.path.join(root, p)
            npath = os.path.join(root, n)
            opaths.append((root, p, opath))
            npaths.append((root, n, npath))

            # output match
            logging.info('found a match!\n\n\topath (%d): %s\n\tnpath (%d): %s\n',
                os.path.exists(opath), opath, os.path.exists(npath), npath)

    # descibe paths
    oset = set(opaths)
    nset = set(npaths)
    iset = oset.intersection(nset)

    logging.info('%d old', len(opaths))
    logging.info('%d old (unique)', len(oset))
    logging.info('%d new', len(npaths))
    logging.info('%d new (unique)', len(nset))
    logging.info('%d same', len(iset))

    # make sure paths were found
    try:
        assert opaths
    except AssertionError:
        logging.error('no old paths found')
        error = True

    # make sure paths were found
    try:
        assert npaths
    except AssertionError:
        logging.error('no new paths found')
        error = True

    # make sure old paths are unique
    try:
        assert len(oset) is len(opaths)
    except AssertionError:
        logging.error('old paths are not unique')
        error = True

    # make sure new paths are unique
    try:
        assert len(nset) is len(npaths)
    except AssertionError:
        logging.error('new paths are not unique')
        error = True

    # make sure old paths and new paths do not intersect
    try:
        assert not iset
    except AssertionError:
        logging.error('some paths are the same')
        error = True

    # check if old paths exist
    found = []
    for root, base, path in opaths:
        try:
            assert os.path.exists(path)
        except AssertionError:
            found.append(path)
    if found:
        logging.error('some old paths do not exist\n\n\t%s\n',
            '\n\t'.join(found))
        error = True

    # check if new paths exist
    found = []
    for root, base, path in npaths:
        try:
            assert not os.path.exists(path)
        except AssertionError:
            found.append(path)
    if found:
        logging.error('some new paths already exist\n\n\t%s\n',
            '\n\t'.join(found))
        error = True

    # stop if there were errors
    if error:
        raise RuntimeError('invalid configuration')

    # move files
    if opts.force:
        logging.info('moving paths!')
        for (oroot, obase, opath), (nroot, nbase, npath) in zip(opaths, npaths):
            logging.info('\n\n\tmv %s\n\t   %s\n', opath, npath)
            shutil.move(opath, npath)
            pass
    else:
        logging.info('\n\n\tThis was a dry run, please use --force to perform renaming\n')

if __name__ == '__main__':
    try:
        main()
    except SystemExit:
        pass
    except:
        logging.exception('caught unhandled exception')
<commit_msg>Add git mv.<commit_after>#!/usr/bin/env python
"""
Rename files or directories using regular expression.
Does nothing without the --force option.

example:
    ./pyrename.py '(.*)\.py' '\g<1>_renamed.py'

"""
from subprocess import check_call
import itertools
import argparse
import logging
import shutil
import os
import re

def setup_logging():
    lfmt = '[%(asctime)s][%(process)5d][%(levelname)-5s]: %(message)s'
    dfmt = "%Y-%m-%d %H:%M:%S"
    logging.basicConfig(level=logging.INFO, format=lfmt, datefmt=dfmt)

setup_logging()

def log_option(opts, attr):
    obj = getattr(opts, attr)

    if isinstance(obj, list) or isinstance(obj, tuple):
        for i, item in enumerate(obj):
            logging.info('%-10s = %s', '{}[{:d}]'.format(attr, i), obj[i])
        return

    if isinstance(obj, set):
        i = 0
        for item in obj:
            logging.info('%-10s = %s', '{}[{:d}]'.format(attr, i), item)
            i += 1
        return

    logging.info('%-10s = %s', attr, obj)

def move(opath, npath, git=False):
    if git:
        logging.info('\n\n\tgit mv %s\n\t       %s\n', opath, npath)
        check_call(['git', 'mv', opath, npath])
    else:
        logging.info('\n\n\tmv %s\n\t   %s\n', opath, npath)
        shutil.move(ipath, opath)

class Options(object):
    def __init__(self, work):
        self.pattern = None
        self.replace = None
        self.nomatch = None
        self.exclude = []
        self.top     = os.path.realpath(os.path.expanduser(work))

        self.recursive  = False
        self.dirs       = False
        self.files      = True
        self.both       = False
        self.force      = False
        self.ignorecase = False
        self.silent     = False
        self.git        = False

def get_arguments(work, args=None):
    parser = argparse.ArgumentParser(description=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter)
    opts = Options(work)

    # regular expressions
    parser.add_argument(dest='pattern', type=str,
        help='expression: pattern')
    parser.add_argument(dest='replace', type=str,
        help='expression: replace')
    parser.add_argument('--nomatch', type=str, default=opts.nomatch,
        help='expression: nomatch')
    parser.add_argument('--exclude', type=str, nargs='*', default=opts.exclude,
        help='list of files to exclude')

    # searching
    parser.add_argument('-t', '--top', type=str, default=opts.top,
        help='top level directory')
    parser.add_argument('-r', '--recursive', action='store_true',
        help='search recursivly')

    fgroup = parser.add_mutually_exclusive_group()
    fgroup.add_argument('-d', '--dirs' , action='store_true',
        help='match directories')
    fgroup.add_argument('-b', '--both' , action='store_true',
        help='match directories and files')

    # mode
    parser.add_argument('-f', '--force', action='store_true',
        help='rename files')
    parser.add_argument('-g', '--git', action='store_true',
        help='use git mv')
    parser.add_argument('-i', '--ignorecase', action='store_true',
        help='ignore case')
    parser.add_argument('-s', '--silent', action='store_true',
        help='silent')

    # parse
    parser.parse_args(args, namespace=opts)

    # logging
    if opts.silent:
        logging.getLogger().setLevel(logging.ERROR)

    # update opts
    if opts.dirs:
        opts.files = False
    if opts.both:
        opts.dirs  = True
        opts.files = True

    # fix path
    opts.top = os.path.realpath(opts.top)

    # fix exclude
    opts.exclude = set(opts.exclude)
    opts.exclude.add(os.path.basename(__file__))

    # log options
    log_option(opts, 'pattern')
    log_option(opts, 'replace')
    log_option(opts, 'nomatch')
    log_option(opts, 'top')

    log_option(opts, 'recursive')
    log_option(opts, 'files')
    log_option(opts, 'dirs')
    log_option(opts, 'both')

    log_option(opts, 'exclude')

    log_option(opts, 'force')
    log_option(opts, 'ignorecase')

    return opts

def walk(top, r=False, dirs=False, files=False):
    if dirs or files:
        if r:
            for root, dbases, fbases in os.walk(top, followlinks=False):
                if dirs and files:
                    for p in itertools.chain(dbases, fbases):
                        yield root, p
                elif dirs:
                    for d in dbases:
                        yield root, d
                elif files:
                    for f in fbases:
                        yield root, f
        else:
            root, dbases, fbases = next(os.walk(top, followlinks=False))
            if dirs and files:
                for p in itertools.chain(dbases, fbases):
                    yield root, p
            elif dirs:
                for d in dbases:
                    yield root, d
            elif files:
                for f in fbases:
                    yield root, f
    else:
        raise StopIteration

def main(args=None):
    work = os.getcwd()
    opts = get_arguments(work, args)

    # check top level directory
    if not os.path.exists(opts.top) or not os.path.isdir(opts.top):
        raise RuntimeError('invalid top level directory: %s' % opts.top)

    # compile regex
    if opts.ignorecase:
        regex1 = re.compile(opts.pattern, re.IGNORECASE)
        try:
            regex2 = re.compile(opts.nomatch, re.IGNORECASE)
        except TypeError:
            regex2 = None
    else:
        regex1 = re.compile(opts.pattern)
        try:
            regex2 = re.compile(opts.nomatch)
        except TypeError:
            regex2 = None

    # record errors
    error = False

    # find paths
    opaths = []
    npaths = []
    for root, p in walk(opts.top, r=opts.recursive, dirs=opts.dirs, files=opts.files):
        match = regex1.match(p)
        if match:
            # exclude list
            if p in opts.exclude:
                logging.info('path excluded!\n\n\t%s\n', os.path.join(root, p))
                continue

            # exclude nomatch
            if not regex2 is None and regex2.match(p):
                logging.info('path excluded!\n\n\t%s\n', os.path.join(root, p))
                continue

            # construct new base
            try:
                n = regex1.sub(opts.replace, p)
            except re.error:
                logging.exception('regex error')
                error = True
                n = p

            # construct paths
            opath = os.path.join(root, p)
            npath = os.path.join(root, n)
            opaths.append((root, p, opath))
            npaths.append((root, n, npath))

            # output match
            logging.info('found a match!\n\n\topath (%d): %s\n\tnpath (%d): %s\n',
                os.path.exists(opath), opath, os.path.exists(npath), npath)

    # descibe paths
    oset = set(opaths)
    nset = set(npaths)
    iset = oset.intersection(nset)

    logging.info('%d old', len(opaths))
    logging.info('%d old (unique)', len(oset))
    logging.info('%d new', len(npaths))
    logging.info('%d new (unique)', len(nset))
    logging.info('%d same', len(iset))

    # make sure paths were found
    try:
        assert opaths
    except AssertionError:
        logging.error('no old paths found')
        error = True

    # make sure paths were found
    try:
        assert npaths
    except AssertionError:
        logging.error('no new paths found')
        error = True

    # make sure old paths are unique
    try:
        assert len(oset) is len(opaths)
    except AssertionError:
        logging.error('old paths are not unique')
        error = True

    # make sure new paths are unique
    try:
        assert len(nset) is len(npaths)
    except AssertionError:
        logging.error('new paths are not unique')
        error = True

    # make sure old paths and new paths do not intersect
    try:
        assert not iset
    except AssertionError:
        logging.error('some paths are the same')
        error = True

    # check if old paths exist
    found = []
    for root, base, path in opaths:
        try:
            assert os.path.exists(path)
        except AssertionError:
            found.append(path)
    if found:
        logging.error('some old paths do not exist\n\n\t%s\n',
            '\n\t'.join(found))
        error = True

    # check if new paths exist
    found = []
    for root, base, path in npaths:
        try:
            assert not os.path.exists(path)
        except AssertionError:
            found.append(path)
    if found:
        logging.error('some new paths already exist\n\n\t%s\n',
            '\n\t'.join(found))
        error = True

    # stop if there were errors
    if error:
        raise RuntimeError('invalid configuration')

    # move files
    if opts.force:
        logging.info('moving paths!')
        for (oroot, obase, opath), (nroot, nbase, npath) in zip(opaths, npaths):
            move(opath, npath, git=opts.git)
    else:
        logging.info('\n\n\tThis was a dry run, please use --force to perform renaming\n')

if __name__ == '__main__':
    try:
        main()
    except SystemExit:
        pass
    except:
        logging.exception('caught unhandled exception')
<|endoftext|>


<commit_before>import sublime
import sublime_plugin

import glob
import os
import re

RX_CMD_LINE_CD = re.compile(r'^(?P<cmd>:\s*cd)\s+(?P<path>.*)$')
RX_CMD_LINE_WRITE = re.compile(r'^(?P<cmd>:\s*w)\s+(?P<path>.*)$')
RX_CMD_LINE_EDIT = re.compile(r'^(?P<cmd>:\s*e)\s+(?P<path>.*)$')

COMPLETIONS_FILE = 1
COMPLETIONS_DIRECTORY = 2

completion_types = [
    (RX_CMD_LINE_CD, True),
    (RX_CMD_LINE_WRITE, True),
    (RX_CMD_LINE_EDIT, False),
]

def iter_paths(prefix=None, only_dirs=False):
    start_at = os.path.expandvars(os.path.expanduser(prefix))
    stuff = glob.iglob(start_at + "*")
    for path in glob.iglob(start_at + '*'):
        if not only_dirs or os.path.isdir(path):
            yield path

def parse(text):
    found = None
    for (pattern, only_dirs) in completion_types:
        found = pattern.search(text)
        if found:
            return found.groupdict()['cmd'], found.groupdict()['path'], only_dirs
    return (None, None, None)

def escape(path):
    return path.replace(' ', '\\ ')

def unescape(path):
    return path.replace('\\ ', ' ')

def wants_fs_completions(text):
    return parse(text)[0] is not None

# class WriteFsCompletion(sublime_plugin.TextCommand):
#     def run(self, edit, cmd, completion):
#         OpenTestInputBox.receiving_user_input = False
#         self.view.sel().clear()
#         self.view.replace(edit, sublime.Region(0, self.view.size()),
#                           cmd + ' ' + escape(completion))
#         self.view.sel().add(sublime.Region(self.view.size()))

# class FsCompletion(sublime_plugin.TextCommand):
#     current_prefix = ''
#     must_recalculate = False
#     dirs = None

#     def run(self, edit):
#         if self.view.score_selector(0, 'text.excmdline') == 0:
#             return

#         cmd, prefix = parse(self.view.substr(self.view.line(0)))
#         if not cmd:
#             return
#         if not FsCompletion.current_prefix and prefix:
#             FsCompletion.current_prefix = prefix
#             FsCompletion.must_recalculate = True
#         elif not FsCompletion.current_prefix:
#             FsCompletion.current_prefix = os.getcwd() + '/'

#         if not FsCompletion.dirs or FsCompletion.must_recalculate:
#             FsCompletion.dirs = iter_paths(FsCompletion.current_prefix,
#                                            only_dirs=True)
#             FsCompletion.must_recalculate = False

#         try:
#             self.view.run_command('write_fs_completion',
#                                   { 'cmd': cmd,
#                                     'completion': next(FsCompletion.dirs)})
#         except StopIteration:
#             try:
#                 FsCompletion.dirs = iter_paths(FsCompletion.current_prefix,
#                                                only_dirs=True)
#                 self.view.run_command('write_fs_completion',
#                                       { 'cmd': cmd,
#                                         'completion': next(FsCompletion.dirs)})
#             except StopIteration:
#                 return

# class OpenTestInputBox(sublime_plugin.WindowCommand):
#     receiving_user_input = True
#     def run(self):
#         v = self.window.show_input_panel('Test', '',
#                                          None,
#                                          self.on_change,
#                                          None)
#         v.set_syntax_file('Packages/User/Dummy.tmLanguage')

#     def on_change(self, s):
#         print("FuCK")
#         if OpenTestInputBox.receiving_user_input:
#             cmd, prefix = parse(unescape(s))
#             if not cmd:
#                 return
#             FsCompletion.current_prefix = prefix
#             FsCompletion.must_recalculate = True
#             print("XXX", FsCompletion.current_prefix)
#         OpenTestInputBox.receiving_user_input = True
<commit_msg>remove dead code<commit_after>import sublime
import sublime_plugin

import glob
import os
import re

RX_CMD_LINE_CD = re.compile(r'^(?P<cmd>:\s*cd)\s+(?P<path>.*)$')
RX_CMD_LINE_WRITE = re.compile(r'^(?P<cmd>:\s*w)\s+(?P<path>.*)$')
RX_CMD_LINE_EDIT = re.compile(r'^(?P<cmd>:\s*e)\s+(?P<path>.*)$')

COMPLETIONS_FILE = 1
COMPLETIONS_DIRECTORY = 2

completion_types = [
    (RX_CMD_LINE_CD, True),
    (RX_CMD_LINE_WRITE, True),
    (RX_CMD_LINE_EDIT, False),
]

def iter_paths(prefix=None, only_dirs=False):
    start_at = os.path.expandvars(os.path.expanduser(prefix))
    stuff = glob.iglob(start_at + "*")
    for path in glob.iglob(start_at + '*'):
        if not only_dirs or os.path.isdir(path):
            yield path

def parse(text):
    found = None
    for (pattern, only_dirs) in completion_types:
        found = pattern.search(text)
        if found:
            return found.groupdict()['cmd'], found.groupdict()['path'], only_dirs
    return (None, None, None)

def escape(path):
    return path.replace(' ', '\\ ')

def unescape(path):
    return path.replace('\\ ', ' ')

def wants_fs_completions(text):
    return parse(text)[0] is not None
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""Misspellings.

---
layout:     post
source:     Garner's Modern American Usage
source_url: http://amzn.to/15wF76r
title:      misspellings
date:       2014-06-10 12:31:19
categories: writing
---

Points out misspellings.

"""
from tools import memoize, preferred_forms_check

@memoize
def check(text):
    """Suggest the preferred forms."""
    err = "garner.misspellings"
    msg = "Misspelling. '{}' is the preferred form."

    misspellings = [
        ["a lot",             ["alot"]],
        ["academically",      ["academicly"]],
        ["accidentally",      ["accidently"]],
        ["accommodable",      ["accomodatable"]],
        ["anilingus",         ["analingus"]],
        ["aren't i",          ["amn't i"]],
        ["aren't i",          ["an't i"]],
        ["barbed wire",       ["bob wire"]],
        ["chargeable",        ["chargable"]],
        ["chiffonier",        ["chiffonnier"]],
        ["chintzy",           ["chinchy"]],
        ["chipotle",          ["chipolte"]],
        ["chlorophyll",       ["chlorophyl"]],
        ["chocolaty",         ["chocolatey"]],
        ["chronicle",         ["chronical"]],
        ["chronicles",        ["chronicals"]],
        ["coleslaw",          ["coldslaw"]],
        ["choosy",            ["choosey"]],
        ["cummerbund",        ["kummerbund"]],
        ["financeable",       ["financable"]],
        ["fluorescent",       ["flourescent"]],
        ["fluoridation",      ["flouridation"]],
        ["fluoride",          ["flouride"]],
        ["foreclose",         ["forclose"]],
        ["forswear",          ["foreswear"]],
        ["free rein",         ["free reign"]],
        ["gist",              ["jist"]],
        ["glamour",           ["glamor"]],
        ["granddad",          ["grandad"]],
        ["grandpa",           ["granpa"]],
        ["highfalutin",       ["highfaluting", "highfalutin'", "hifalutin"]],
        ["Hippocratic",       ["hypocratic"]],
        ["hirable",           ["hireable"]],
        ["holistic",          ["wholistic"]],
        ["ideology",          ["idealogy"]],
        ["idiosyncrasy",      ["ideosyncracy"]],
        ["improvise",         ["improvize"]],
        ["incurrence",        ["incurment"]],
        ["inevitability",     ["inevitableness"]],
        ["innovate",          ["inovate"]],
        ["inoculation",       ["innoculation", "inocculation"]],
        ["integral",          ["intergral"]],
        ["inundate",          ["innundate"]],
        ["inundated",         ["innundated"]],
        ["inundates",         ["innundates"]],
        ["inundating",        ["innundating"]],
        ["iridescent",        ["irridescent"]],
        ["irrelevant",        ["irrevelant"]],
        ["jujitsu",           ["jiujutsu"]],
        ["kaleidoscope",      ["kaleidascope"]],
        ["knickknack",        ["knicknack"]],
        ["lassos",            ["lassoes"]],
        ["lessee",            ["leasee"]],
        ["lessor",            ["leasor"]],
        ["liaison",           ["liason"]],
        ["liaison",           ["laison"]],
        ["lightning rod",     ["lightening rod"]],
        ["struck by lightning", ["struck by lightening"]],
        ["liquefy",           ["liquify"]],
        ["loathsome",         ["loathesome"]],
        ["lodestar",          ["loadstar"]],
        ["to make do",        ["to make due"]],
        ["mademoiselle",      ["madamoiselle"]],
        ["martial arts",      ["marshall arts"]],
        ["court-martial",     ["court marshall", "court-marshall"]],
        ["maelstrom",         ["maelstorm"]],
        ["mafia",             ["maffia"]],
        ["mafiosi",           ["mafiosos"]],
        ["mangoes",           ["mangos"]],
        ["marijuana",         ["marihuana"]],
        ["marshmallow",       ["marshmellow"]],
        ["martial law",       ["marshall law"]],
        ["massacring",        ["massacering", "massacreing"]],
        ["measles",           ["measels"]],
        ["memento",           ["momento"]],
        ["minuscule",         ["miniscule"]],
        ["mileage",           ["milage"]],
        ["milieu",            ["mileau"]],
        ["millennium",        ["millenium"]],
        ["millennia",         ["millenia"]],
        ["millenniums",       ["milleniums"]],
        ["millipede",         ["millepede"]],
        ["millionaire",       ["millionnaire"]],
        ["milquetoast",       ["milktoast"]],
        ["mimicked",          ["mimiced"]],
        ["mimicking",         ["mimicing"]],
        ["misspelling",       ["mispelling"]],
        ["mischievous",       ["mischievious"]],
        ["naphtha",           ["naptha"]],
        ["neckwear",          ["neckware"]],
        ["newsstand",         ["newstand"]],
        ["occurred",          ["occured"]],
        ["plantar fasciitis", ["planter fasciitis", "plantar fascitis"]],
        ["pledgeable",        ["pledgable", "pledgeble"]],
        ["portentous",        ["portentuous", "portentious"]],
        ["praying mantis",    ["preying mantis"]],
        ["preestablished",    ["prestablished"]],
        ["preexisting",       ["prexisting"]],
        ["presumptuous",      ["presumptious"]],
        ["rarefy",            ["rarify"]],
        ["reckless",          ["wreckless"]],
        ["remuneration",      ["renumeration"]],
        ["restaurateur",      ["restauranteur"]],
        ["retractable",       ["retractible"]],
        ["reverie",           ["revery"]],
        ["spicy",             ["spicey"]],
        ["stupefy",           ["stupify"]],
        ["subtly",            ["subtley"]],
        ["till",              ["\'till?"]],
        ["tinderbox",         ["tenderbox"]],
        ["timpani",           ["tympani"]],
        ["a timpani",         ["a timpano"]],
        ["verbiage",          ["verbage"]],
    ]

    return preferred_forms_check(text, misspellings, err, msg)
<commit_msg>Add rule on naval vs. navel oranges<commit_after># -*- coding: utf-8 -*-
"""Misspellings.

---
layout:     post
source:     Garner's Modern American Usage
source_url: http://amzn.to/15wF76r
title:      misspellings
date:       2014-06-10 12:31:19
categories: writing
---

Points out misspellings.

"""
from tools import memoize, preferred_forms_check

@memoize
def check(text):
    """Suggest the preferred forms."""
    err = "garner.misspellings"
    msg = "Misspelling. '{}' is the preferred form."

    misspellings = [
        ["a lot",             ["alot"]],
        ["academically",      ["academicly"]],
        ["accidentally",      ["accidently"]],
        ["accommodable",      ["accomodatable"]],
        ["anilingus",         ["analingus"]],
        ["aren't i",          ["amn't i"]],
        ["aren't i",          ["an't i"]],
        ["barbed wire",       ["bob wire"]],
        ["chargeable",        ["chargable"]],
        ["chiffonier",        ["chiffonnier"]],
        ["chintzy",           ["chinchy"]],
        ["chipotle",          ["chipolte"]],
        ["chlorophyll",       ["chlorophyl"]],
        ["chocolaty",         ["chocolatey"]],
        ["chronicle",         ["chronical"]],
        ["chronicles",        ["chronicals"]],
        ["coleslaw",          ["coldslaw"]],
        ["choosy",            ["choosey"]],
        ["cummerbund",        ["kummerbund"]],
        ["financeable",       ["financable"]],
        ["fluorescent",       ["flourescent"]],
        ["fluoridation",      ["flouridation"]],
        ["fluoride",          ["flouride"]],
        ["foreclose",         ["forclose"]],
        ["forswear",          ["foreswear"]],
        ["free rein",         ["free reign"]],
        ["gist",              ["jist"]],
        ["glamour",           ["glamor"]],
        ["granddad",          ["grandad"]],
        ["grandpa",           ["granpa"]],
        ["highfalutin",       ["highfaluting", "highfalutin'", "hifalutin"]],
        ["Hippocratic",       ["hypocratic"]],
        ["hirable",           ["hireable"]],
        ["holistic",          ["wholistic"]],
        ["ideology",          ["idealogy"]],
        ["idiosyncrasy",      ["ideosyncracy"]],
        ["improvise",         ["improvize"]],
        ["incurrence",        ["incurment"]],
        ["inevitability",     ["inevitableness"]],
        ["innovate",          ["inovate"]],
        ["inoculation",       ["innoculation", "inocculation"]],
        ["integral",          ["intergral"]],
        ["inundate",          ["innundate"]],
        ["inundated",         ["innundated"]],
        ["inundates",         ["innundates"]],
        ["inundating",        ["innundating"]],
        ["iridescent",        ["irridescent"]],
        ["irrelevant",        ["irrevelant"]],
        ["jujitsu",           ["jiujutsu"]],
        ["kaleidoscope",      ["kaleidascope"]],
        ["knickknack",        ["knicknack"]],
        ["lassos",            ["lassoes"]],
        ["lessee",            ["leasee"]],
        ["lessor",            ["leasor"]],
        ["liaison",           ["liason"]],
        ["liaison",           ["laison"]],
        ["lightning rod",     ["lightening rod"]],
        ["struck by lightning", ["struck by lightening"]],
        ["liquefy",           ["liquify"]],
        ["loathsome",         ["loathesome"]],
        ["lodestar",          ["loadstar"]],
        ["to make do",        ["to make due"]],
        ["mademoiselle",      ["madamoiselle"]],
        ["martial arts",      ["marshall arts"]],
        ["court-martial",     ["court marshall", "court-marshall"]],
        ["maelstrom",         ["maelstorm"]],
        ["mafia",             ["maffia"]],
        ["mafiosi",           ["mafiosos"]],
        ["mangoes",           ["mangos"]],
        ["marijuana",         ["marihuana"]],
        ["marshmallow",       ["marshmellow"]],
        ["martial law",       ["marshall law"]],
        ["massacring",        ["massacering", "massacreing"]],
        ["measles",           ["measels"]],
        ["memento",           ["momento"]],
        ["minuscule",         ["miniscule"]],
        ["mileage",           ["milage"]],
        ["milieu",            ["mileau"]],
        ["millennium",        ["millenium"]],
        ["millennia",         ["millenia"]],
        ["millenniums",       ["milleniums"]],
        ["millipede",         ["millepede"]],
        ["millionaire",       ["millionnaire"]],
        ["milquetoast",       ["milktoast"]],
        ["mimicked",          ["mimiced"]],
        ["mimicking",         ["mimicing"]],
        ["misspelling",       ["mispelling"]],
        ["mischievous",       ["mischievious"]],
        ["naphtha",           ["naptha"]],
        ["navel orange",      ["naval orange"]],
        ["neckwear",          ["neckware"]],
        ["newsstand",         ["newstand"]],
        ["occurred",          ["occured"]],
        ["plantar fasciitis", ["planter fasciitis", "plantar fascitis"]],
        ["pledgeable",        ["pledgable", "pledgeble"]],
        ["portentous",        ["portentuous", "portentious"]],
        ["praying mantis",    ["preying mantis"]],
        ["preestablished",    ["prestablished"]],
        ["preexisting",       ["prexisting"]],
        ["presumptuous",      ["presumptious"]],
        ["rarefy",            ["rarify"]],
        ["reckless",          ["wreckless"]],
        ["remuneration",      ["renumeration"]],
        ["restaurateur",      ["restauranteur"]],
        ["retractable",       ["retractible"]],
        ["reverie",           ["revery"]],
        ["spicy",             ["spicey"]],
        ["stupefy",           ["stupify"]],
        ["subtly",            ["subtley"]],
        ["till",              ["\'till?"]],
        ["tinderbox",         ["tenderbox"]],
        ["timpani",           ["tympani"]],
        ["a timpani",         ["a timpano"]],
        ["verbiage",          ["verbage"]],
    ]

    return preferred_forms_check(text, misspellings, err, msg)
<|endoftext|>


<commit_before>from django.contrib import admin

from agir.api.admin import admin_site
from agir.payments.actions import notify_status_change
from . import models

def notify_status_action(model_admin, request, queryset):
    for p in queryset:
        notify_status_change(p)

notify_status_action.short_description = "Renotifier le statut"

@admin.register(models.Payment, site=admin_site)
class PaymentAdmin(admin.ModelAdmin):
    list_display = (
        "id",
        "type",
        "person",
        "email",
        "first_name",
        "last_name",
        "price",
        "status",
        "created",
        "mode",
    )
    readonly_fields = (
        "type",
        "mode",
        "person",
        "email",
        "first_name",
        "last_name",
        "price",
        "phone_number",
        "location_address1",
        "location_address2",
        "location_zip",
        "location_city",
        "location_country",
        "meta",
        "events",
    )
    fields = readonly_fields + ("status",)
    list_filter = ("price", "status")
    search_fields = ("email", "person__emails__address__iexact")

    actions = (notify_status_action,)
<commit_msg>change les filtres des payments<commit_after>from django.contrib import admin

from agir.api.admin import admin_site
from agir.payments.actions import notify_status_change
from . import models

def notify_status_action(model_admin, request, queryset):
    for p in queryset:
        notify_status_change(p)

notify_status_action.short_description = "Renotifier le statut"

@admin.register(models.Payment, site=admin_site)
class PaymentAdmin(admin.ModelAdmin):
    list_display = (
        "id",
        "type",
        "person",
        "email",
        "first_name",
        "last_name",
        "price",
        "status",
        "created",
        "mode",
    )
    readonly_fields = (
        "type",
        "mode",
        "person",
        "email",
        "first_name",
        "last_name",
        "price",
        "phone_number",
        "location_address1",
        "location_address2",
        "location_zip",
        "location_city",
        "location_country",
        "meta",
        "events",
    )
    fields = readonly_fields + ("status",)
    list_filter = ("status", "type", "mode")
    search_fields = ("email", "person__emails__address__iexact")

    actions = (notify_status_action,)
<|endoftext|>


<commit_before>from django.core.exceptions import MiddlewareNotUsed
from airbrake.utils.client import Client

class AirbrakeNotifierMiddleware(object):
    def __init__(self):
        self.client = Client()

    def process_exception(self, request, exception):
        self.client.notify(exception=exception, request=request)
<commit_msg>only notify if not in debug<commit_after>from django.conf import settings
from django.core.exceptions import MiddlewareNotUsed
from airbrake.utils.client import Client

class AirbrakeNotifierMiddleware(object):
    def __init__(self):
        self.client = Client()

    def process_exception(self, request, exception):
        if not settings.DEBUG:
            self.client.notify(exception=exception, request=request)
<|endoftext|>


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

"""
Gym cart pole experiment

# TODO: Currently fails to learn. Need update.
"""

from nprl import ModelBased
import gym
import numpy as np
import math
import matplotlib.pylab as plt

#  Create Environment
env = gym.make('CartPole-v0')

# observation_encoder
n_tick = np.array([1, 1, 6, 3], dtype=np.float32)
tick = env.observation_space.high - env.observation_space.low
tick[1] = 1.0
tick[3] = 2*math.radians(50)
tick = tick / n_tick
lower_bound = env.observation_space.low
lower_bound[1] = -0.5
lower_bound[3] = -math.radians(50)
def encoder(obs):
    state = []
    for i in range(env.observation_space.shape[0]):
        j = 0
        tmp = lower_bound[i]
        if obs[i] > tmp:
            while tmp < obs[i]:
                tmp += tick[i]
                j += 1
        state.append(j)
    return str(state)

# Create Agent
agent = ModelBased(discount_factor=0.95,
                   exploration_rate=0.1,
                   exploration_reward=0.0,
                   vi_error_limit=0.01,
                   vi_iteration=100,
                   vi_interval=1)
agent.init()

obs = env.reset()
action = agent.step(new_state=encoder(obs),
                    reward=1.0,
                    terminal=False,
                    action_list=range(env.action_space.n),
                    test=False)

result = []
result_average = []
t = []
N = 1000

terminal = False
episode_time = 0
episode = 0
while True:

    #env.render()

    if terminal:
        agent.reset()

        obs = env.reset()
        reward = 1.0
        terminal = False

        #agent.exploration_rate = max(0.05, min(1.0, 1.0 - np.log10((episode+1.0)/25)))

        # Plot result
        result.append(episode_time)

        if episode % 10 == 0:
            t.append(episode)
            result_average.append(np.mean(result))
            result = []

            plt.clf()
            plt.plot(t, result_average, 'b')
            plt.hold(True)
            plt.plot([0, N], [200, 200], '-.r')
            plt.hold(False)
            plt.xlim([0, N])
            plt.ylim([0, 250])
            plt.pause(0.001)

        print("{}-th EPISODE : {} steps".format(episode, episode_time))
        episode_time = 0
        episode += 1
    else:
        obs, reward, terminal, info = env.step(action)
        print reward, terminal
        episode_time += 1

    if not terminal:
        action = agent.step(new_state=encoder(obs),
                            reward=reward,
                            terminal=terminal,
                            action_list=range(env.action_space.n),
                            test=False)

    print "action : ", action
    print "state : ", encoder(obs)
    if episode == N:
        break

print ("Close plotting window to finish.")
print "Environment Model", agent.get_model()
print "---"
print agent.get_value_function()
print "Terminal States", agent._terminal_state_set
agent.show_model()

plt.show()
print ("Finish. ")
<commit_msg>fix experimental setting<commit_after># -*- coding: utf-8 -*-

"""
Gym cart pole experiment

# TODO: Currently fails to learn. Need update.
"""

from nprl import ModelBased
import gym
import numpy as np
import math
import matplotlib.pylab as plt

#  Create Environment
env = gym.make('CartPole-v0')

# observation_encoder
n_tick = np.array([1, 1, 6, 3], dtype=np.float32)
tick = env.observation_space.high - env.observation_space.low
tick[1] = 1.0
tick[3] = 2 * math.radians(50)
tick = tick / n_tick
lower_bound = env.observation_space.low
lower_bound[1] = -0.5
lower_bound[3] = -math.radians(50)

def encoder(obs):
    state = []
    for i in range(env.observation_space.shape[0]):
        j = 0
        tmp = lower_bound[i]
        if obs[i] > tmp:
            while tmp < obs[i]:
                tmp += tick[i]
                j += 1
        state.append(j)
    return str(state)

# Create Agent
agent = ModelBased(discount_factor=0.95,
                   exploration_rate=0.1,
                   exploration_reward=0.0,
                   vi_error_limit=0.01,
                   vi_iteration=100,
                   vi_interval=1)
agent.init()

obs = env.reset()
action = agent.step(new_state=encoder(obs),
                    reward=1.0,
                    terminal=False,
                    action_list=range(env.action_space.n),
                    test=False)

result = []
result_average = []
t = []
N = 1000

terminal = False
episode_time = 0
episode = 0
while True:

    # env.render()

    if terminal:
        agent.reset()

        obs = env.reset()
        reward = 1.0
        terminal = False

        # agent.exploration_rate = max(0.05, min(1.0, 1.0 - np.log10((episode+1.0)/25)))

        # Plot result
        result.append(episode_time)

        if episode % 10 == 0:
            t.append(episode)
            result_average.append(np.mean(result))
            result = []

            plt.clf()
            plt.plot(t, result_average, 'b')
            plt.hold(True)
            plt.plot([0, N], [200, 200], '-.r')
            plt.hold(False)
            plt.xlim([0, N])
            plt.ylim([0, 250])
            plt.pause(0.001)

        print("{}-th EPISODE : {} steps".format(episode, episode_time))
        episode_time = 0
        episode += 1
    else:
        obs, reward, terminal, info = env.step(action)
        print reward, terminal
        episode_time += 1

    action = agent.step(new_state=encoder(obs),
                        reward=reward,
                        terminal=terminal,
                        action_list=range(env.action_space.n),
                        test=False)

    print "action : ", action
    print "state : ", encoder(obs)
    if episode == N:
        break

print ("Close plotting window to finish.")
print "Environment Model", agent.get_model()
print "---"
print agent.get_value_function()
print "Terminal States", agent._terminal_state_set
agent.show_model()

plt.show()
print ("Finish. ")
<|endoftext|>