<commit_before># encoding: utf-8
import nose
from datetime import datetime, timedelta
from ckanext.requestdata import helpers as h
import ckan.plugins as p
from ckan.tests import helpers, factories
from ckan import logic

ok_ = nose.tools.ok_
eq_ = nose.tools.eq_
raises = nose.tools.raises

class ActionBase(object):

    @classmethod
    def setup_class(self):
        self.app = helpers._get_test_app()
        if not p.plugin_loaded('requestdata'):
            p.load('requestdata')

    def setup(self):
        helpers.reset_db()

    @classmethod
    def teardown_class(self):
        if p.plugin_loaded('requestdata'):
            p.unload('requestdata')

class TestHelpers(ActionBase):

    def test_time_ago_from_datetime_valid(self):
        d = datetime.today() - timedelta(days=1)
        eq_(h.time_ago_from_datetime(d), '1 day ago')

    def test_time_ago_from_datetime_valid_string_result(self):
        d = datetime.today() - timedelta(days=2)
        assert isinstance(h.time_ago_from_datetime(d), str)

    def test_convert_id_to_emails_valid(self):
        user = factories.User()
        users = [{'name': user['name']}]
        ids = user['id']
        response = h.convert_id_to_email(ids)
        email = 'test_user_05@ckan.org'
        assert email == response

    def test_convert_id_to_emails_invalid(self):
        ids = '231'
        response = h.convert_id_to_email(ids)
        email = 'test_user_05@ckan.org'
        assert email not in response<commit_msg>Add test for json converter<commit_after># encoding: utf-8
import nose
from datetime import datetime, timedelta
from ckanext.requestdata import helpers as h
import ckan.plugins as p
from ckan.tests import helpers, factories
from ckan import logic
from ckan.common import request

ok_ = nose.tools.ok_
eq_ = nose.tools.eq_
raises = nose.tools.raises

class ActionBase(object):

    @classmethod
    def setup_class(self):
        self.app = helpers._get_test_app()
        if not p.plugin_loaded('requestdata'):
            p.load('requestdata')

    def setup(self):
        helpers.reset_db()

    @classmethod
    def teardown_class(self):
        if p.plugin_loaded('requestdata'):
            p.unload('requestdata')

class TestHelpers(ActionBase):

    def test_time_ago_from_datetime_valid(self):
        d = datetime.today() - timedelta(days=1)
        eq_(h.time_ago_from_datetime(d), '1 day ago')

    def test_time_ago_from_datetime_valid_string_result(self):
        d = datetime.today() - timedelta(days=2)
        assert isinstance(h.time_ago_from_datetime(d), str)

    def test_convert_id_to_emails_valid(self):
        user = factories.User()
        users = [{'name': user['name']}]
        ids = user['id']
        response = h.convert_id_to_email(ids)
        email = 'test_user_05@ckan.org'
        assert email == response

    def test_convert_id_to_emails_invalid(self):
        ids = '231'
        response = h.convert_id_to_email(ids)
        email = 'test_user_05@ckan.org'
        assert email not in response

    def test_convert_str_to_json_valid(self):
        str = "test: 123"
        res = h.convert_str_to_json(str)
        assert "string cannot be parsed" == res<|endoftext|>


<commit_before>import nose
from ckan.tests import helpers, factories
from ckan.lib.helpers import url_for

import ckanext.stadtzhtheme.plugin as plugin

eq_ = nose.tools.eq_
assert_true = nose.tools.assert_true

class TestPlugin(helpers.FunctionalTestBase):

    def test_descr_file(self):
        theme_plugin = plugin.StadtzhThemePlugin()

        expected_keys = ['zip', 'wms', 'wmts', 'wfs', 'kml', 'kmz', 'json', 'csv', 'gpkg']
        descr = theme_plugin.get_descr_config()
        assert all(k in descr.keys() for k in expected_keys), "Keys: %s" % descr.keys()

    def test_translations_without_orgs(self):
        dataset = factories.Dataset()

        url = url_for('dataset_read', id=dataset['id'])
        app = self._get_test_app()
        response = app.get(url)

        assert 'Aktualisierungsdatum' in response, response
        assert not 'Date last updated' in response, response

    def test_translations_with_org(self):
        org = factories.Organization()
        dataset = factories.Dataset(owner_org=org['id'])

        url = url_for('dataset_read', id=dataset['id'])
        app = self._get_test_app()
        response = app.get(url)

        assert 'Aktualisierungsdatum' in response, response
        assert not 'Date last updated' in response, response

    def test_translation_with_en_locale(self):
        org = factories.Organization()
        dataset = factories.Dataset(owner_org=org['id'])

        url = url_for('dataset_read', id=dataset['id'], locale='en')
        app = self._get_test_app()
        response = app.get(url)

        assert not 'Aktualisierungsdatum' in response, response
        assert 'Date last updated' in response, response
<commit_msg>fix tests to work with url_for<commit_after>import nose
from ckan.tests import helpers, factories
from ckan.lib.helpers import url_for

import ckanext.stadtzhtheme.plugin as plugin

eq_ = nose.tools.eq_
assert_true = nose.tools.assert_true

class TestPlugin(helpers.FunctionalTestBase):

    def test_descr_file(self):
        theme_plugin = plugin.StadtzhThemePlugin()

        expected_keys = ['zip', 'wms', 'wmts', 'wfs', 'kml', 'kmz', 'json', 'csv', 'gpkg']
        descr = theme_plugin.get_descr_config()
        assert all(k in descr.keys() for k in expected_keys), "Keys: %s" % descr.keys()

    def test_translations_without_orgs(self):
        dataset = factories.Dataset()

        url = url_for('dataset_read', id=dataset['name'])
        app = self._get_test_app()
        response = app.get(url)

        assert 'Aktualisierungsdatum' in response, response
        assert not 'Date last updated' in response, response

    def test_translations_with_org(self):
        org = factories.Organization()
        dataset = factories.Dataset(owner_org=org['id'])

        url = url_for('dataset_read', id=dataset['name'])
        app = self._get_test_app()
        response = app.get(url)

        assert 'Aktualisierungsdatum' in response, response
        assert not 'Date last updated' in response, response

    def test_translation_with_en_locale(self):
        org = factories.Organization()
        dataset = factories.Dataset(owner_org=org['id'])

        url = url_for('dataset_read', id=dataset['name'], locale='en')
        app = self._get_test_app()
        response = app.get(url)

        assert not 'Aktualisierungsdatum' in response, response
        assert 'Date last updated' in response, response
<|endoftext|>


<commit_before>"""Tests for helpers.py."""
from nose.tools import *  # noqa
import mock
import ckanext.switzerland.helpers as helpers
import sys
from copy import deepcopy

if sys.version_info < (2, 7):
    import unittest2 as unittest
else:
    import unittest

organizations = [{'children': [],
                  'highlighted': False,
                  'id': u'7dbaad15-597f-499c-9a72-95de38b95cad',
                  'name': u'swiss-library',
                  'title': u'{"fr": "AAAAA (FR)", "de": "BBBBB (DE)", "en": "YYYYY (EN)", "it": "ZZZZZ (IT)"}'},  # noqa
                 {'children': [],
                  'highlighted': False,
                  'id': u'51941490-5ade-4d06-b708-ff04279ce550',
                  'name': u'italian-library',
                  'title': u'{"fr": "YYYYY (FR)", "de": "ZZZZZ (DE)", "en": "BBBBB (EN)", "it": "AAAAA (IT)"}'},  # noqa
                 {'children': [{'children': [],
                                'highlighted': False,
                                'id': u'589ff525-be2f-4059-bea4-75c92739dfe9',
                                'name': u'child-swiss-library',
                                'title': u'{"fr": "AAAAA (FR)", "de": "YYYYY (DE)", "en": "ZZZZZ (EN)", "it": "BBBBB (IT)"}'},  # noqa
                               {'children': [],
                                'highlighted': False,
                                'id': u'2c559631-e174-4e9f-8c2a-940a08371340',
                                'name': u'child-italian-library',
                                'title': u'{"fr": "YYYYY (FR)", "de": "BBBBB (DE)", "en": "AAAAA (EN)", "it": "ZZZZZ (IT)"}'}],  # noqa
                  'highlighted': False,
                  'id': u'73124d1e-c2aa-4d20-a42d-fa71b8946e93',
                  'name': u'swisstopo',
                  'title': u'{"fr": "Swisstopo FR", "de": "Swisstopo DE", "en": "Swisstopo EN", "it": "Swisstopo IT"}'}]  # noqa

organization_title = u'{"fr": "Swisstopo FR", "de": "Swisstopo DE", "en": "Swisstopo EN", "it": "Swisstopo IT"}'

class TestHelpers(unittest.TestCase):
    def test_simplify_terms_of_use_open(self):
        term_id = 'NonCommercialAllowed-CommercialAllowed-ReferenceRequired'
        result = helpers.simplify_terms_of_use(term_id)
        self.assertEquals(term_id, result)

    def test_simplify_terms_of_use_closed(self):
        term_id = 'NonCommercialNotAllowed-CommercialAllowed-ReferenceNotRequired'  # noqa
        result = helpers.simplify_terms_of_use(term_id)
        self.assertEquals('ClosedData', result)

    def test_get_localized_value_dict(self):
        lang_dict = {
            'de': 'DE value',
            'fr': 'FR value',
            'it': 'IT value',
            'en': 'EN value',
        }
        result = helpers.get_localized_value(lang_dict, 'de')
        self.assertEquals(lang_dict['de'], result)

    def test_get_localized_value_fallback(self):
        lang_dict = {
            'de': 'DE value',
            'fr': 'FR value',
            'it': 'IT value',
            'en': '',
        }
        result = helpers.get_localized_value(lang_dict, 'en')
        # if en does not exist, fallback to de
        self.assertEquals(lang_dict['de'], result)

    @mock.patch('pylons.request')
    def test_get_localized_value_no_lang(self, mock_request):
        mock_request.environ = {'CKAN_LANG': 'fr'}

        lang_dict = {
            'de': 'DE value',
            'fr': 'FR value',
            'it': 'IT value',
            'en': 'EN value',
        }
        result = helpers.get_localized_value(lang_dict)
        self.assertEquals(lang_dict['fr'], result)

    def test_get_localized_value_invalid_dict(self):
        test_dict = {'test': 'dict'}
        result = helpers.get_localized_value(test_dict)
        self.assertEquals(test_dict, result)

    @mock.patch('ckan.lib.i18n.get_lang', return_value='fr')
    def test_get_sorted_orgs_by_translated_title_fr(self, mock_get_lang):
        french_organizations = deepcopy(organizations)
        result_orgs = helpers.get_sorted_orgs_by_translated_title(french_organizations)  # noqa

        for org in result_orgs:
            if org['children']:
                self.assertEqual(0, self.find_position_of_org(org['children'], u'child-swiss-library'))  # noqa
                self.assertEqual(1, self.find_position_of_org(org['children'], u'child-italian-library'))  # noqa

        self.assertEqual(0, self.find_position_of_org(result_orgs, u'swiss-library'))  # noqa
        self.assertEqual(2, self.find_position_of_org(result_orgs, u'italian-library'))  # noqa

    @mock.patch('ckan.lib.i18n.get_lang', return_value='it')
    def test_get_sorted_orgs_by_translated_title_it(self, mock_get_lang):
        italian_organizations = deepcopy(organizations)
        result_orgs = helpers.get_sorted_orgs_by_translated_title(italian_organizations)  # noqa

        for org in result_orgs:
            if org['children']:
                self.assertEqual(0, self.find_position_of_org(org['children'], u'child-swiss-library'))  # noqa
                self.assertEqual(1, self.find_position_of_org(org['children'], u'child-italian-library'))  # noqa

        self.assertEqual(2, self.find_position_of_org(result_orgs, u'swiss-library'))  # noqa
        self.assertEqual(0, self.find_position_of_org(result_orgs, u'italian-library'))  # noqa

    @mock.patch('ckan.lib.i18n.get_lang')
    def test_set_translated_group_title(self, mock_get_lang):
        mock_get_lang.return_value = 'en'
        translated_title = helpers.set_translated_group_title(organization_title)  # noqa
        self.assertEqual('Swisstopo EN', translated_title)

        mock_get_lang.return_value = 'de'
        translated_title = helpers.set_translated_group_title(organization_title)  # noqa
        self.assertEqual('Swisstopo DE', translated_title)

    def find_position_of_org(self, org_list, title):
        index = next(
            (i for i, org in enumerate(org_list) if
             org['title'] == title),
            -1)
        return index<commit_msg>adapt the test-cases to the renamed organizations<commit_after>"""Tests for helpers.py."""
from nose.tools import *  # noqa
import mock
import ckanext.switzerland.helpers as helpers
import sys
from copy import deepcopy

if sys.version_info < (2, 7):
    import unittest2 as unittest
else:
    import unittest

organizations = [{'children': [],
                  'highlighted': False,
                  'id': u'7dbaad15-597f-499c-9a72-95de38b95cad',
                  'name': u'swiss-library',
                  'title': u'{"fr": "AAAAA (FR)", "de": "BBBBB (DE)", "en": "YYYYY (EN)", "it": "ZZZZZ (IT)"}'},  # noqa
                 {'children': [],
                  'highlighted': False,
                  'id': u'51941490-5ade-4d06-b708-ff04279ce550',
                  'name': u'italian-library',
                  'title': u'{"fr": "YYYYY (FR)", "de": "ZZZZZ (DE)", "en": "BBBBB (EN)", "it": "AAAAA (IT)"}'},  # noqa
                 {'children': [{'children': [],
                                'highlighted': False,
                                'id': u'589ff525-be2f-4059-bea4-75c92739dfe9',
                                'name': u'child-swiss-library',
                                'title': u'{"fr": "AAAAA (FR)", "de": "YYYYY (DE)", "en": "ZZZZZ (EN)", "it": "BBBBB (IT)"}'},  # noqa
                               {'children': [],
                                'highlighted': False,
                                'id': u'2c559631-e174-4e9f-8c2a-940a08371340',
                                'name': u'child-italian-library',
                                'title': u'{"fr": "YYYYY (FR)", "de": "BBBBB (DE)", "en": "AAAAA (EN)", "it": "ZZZZZ (IT)"}'}],  # noqa
                  'highlighted': False,
                  'id': u'73124d1e-c2aa-4d20-a42d-fa71b8946e93',
                  'name': u'swisstopo',
                  'title': u'{"fr": "Swisstopo FR", "de": "Swisstopo DE", "en": "Swisstopo EN", "it": "Swisstopo IT"}'}]  # noqa

organization_title = u'{"fr": "Swisstopo FR", "de": "Swisstopo DE", "en": "Swisstopo EN", "it": "Swisstopo IT"}'

class TestHelpers(unittest.TestCase):
    def test_simplify_terms_of_use_open(self):
        term_id = 'NonCommercialAllowed-CommercialAllowed-ReferenceRequired'
        result = helpers.simplify_terms_of_use(term_id)
        self.assertEquals(term_id, result)

    def test_simplify_terms_of_use_closed(self):
        term_id = 'NonCommercialNotAllowed-CommercialAllowed-ReferenceNotRequired'  # noqa
        result = helpers.simplify_terms_of_use(term_id)
        self.assertEquals('ClosedData', result)

    def test_get_localized_value_dict(self):
        lang_dict = {
            'de': 'DE value',
            'fr': 'FR value',
            'it': 'IT value',
            'en': 'EN value',
        }
        result = helpers.get_localized_value(lang_dict, 'de')
        self.assertEquals(lang_dict['de'], result)

    def test_get_localized_value_fallback(self):
        lang_dict = {
            'de': 'DE value',
            'fr': 'FR value',
            'it': 'IT value',
            'en': '',
        }
        result = helpers.get_localized_value(lang_dict, 'en')
        # if en does not exist, fallback to de
        self.assertEquals(lang_dict['de'], result)

    @mock.patch('pylons.request')
    def test_get_localized_value_no_lang(self, mock_request):
        mock_request.environ = {'CKAN_LANG': 'fr'}

        lang_dict = {
            'de': 'DE value',
            'fr': 'FR value',
            'it': 'IT value',
            'en': 'EN value',
        }
        result = helpers.get_localized_value(lang_dict)
        self.assertEquals(lang_dict['fr'], result)

    def test_get_localized_value_invalid_dict(self):
        test_dict = {'test': 'dict'}
        result = helpers.get_localized_value(test_dict)
        self.assertEquals(test_dict, result)

    @mock.patch('ckan.lib.i18n.get_lang')
    def test_set_translated_group_title(self, mock_get_lang):
        mock_get_lang.return_value = 'en'
        translated_title = helpers.set_translated_group_title(organization_title)  # noqa
        self.assertEqual('Swisstopo EN', translated_title)

        mock_get_lang.return_value = 'de'
        translated_title = helpers.set_translated_group_title(organization_title)  # noqa
        self.assertEqual('Swisstopo DE', translated_title)

        mock_get_lang.return_value = 'it'
        translated_title = helpers.set_translated_group_title(organization_title)  # noqa
        self.assertEqual('Swisstopo IT', translated_title)

        mock_get_lang.return_value = 'fr'
        translated_title = helpers.set_translated_group_title(organization_title)  # noqa
        self.assertEqual('Swisstopo FR', translated_title)

    @mock.patch('ckan.lib.i18n.get_lang', return_value='fr')
    def test_get_sorted_orgs_by_translated_title_fr(self, mock_get_lang):
        french_organizations = deepcopy(organizations)
        result_orgs = helpers.get_sorted_orgs_by_translated_title(french_organizations)  # noqa

        for org in result_orgs:
            if org['children']:
                self.assertEqual(0, self.find_position_of_org(org['children'], u'AAAAA (FR)'))  # noqa
                self.assertEqual(1, self.find_position_of_org(org['children'], u'YYYYY (FR)'))  # noqa

        self.assertEqual(0, self.find_position_of_org(result_orgs, u'AAAAA (FR)'))  # noqa
        self.assertEqual(2, self.find_position_of_org(result_orgs, u'YYYYY (FR)'))  # noqa

    @mock.patch('ckan.lib.i18n.get_lang', return_value='it')
    def test_get_sorted_orgs_by_translated_title_it(self, mock_get_lang):
        italian_organizations = deepcopy(organizations)
        result_orgs = helpers.get_sorted_orgs_by_translated_title(italian_organizations)  # noqa

        for org in result_orgs:
            if org['children']:
                self.assertEqual(0, self.find_position_of_org(org['children'], u'BBBBB (IT)'))  # noqa
                self.assertEqual(1, self.find_position_of_org(org['children'], u'ZZZZZ (IT)'))  # noqa

        self.assertEqual(2, self.find_position_of_org(result_orgs, u'ZZZZZ (IT)'))  # noqa
        self.assertEqual(0, self.find_position_of_org(result_orgs, u'AAAAA (IT)'))  # noqa

    def find_position_of_org(self, org_list, title):
        index = next(
            (i for i, org in enumerate(org_list) if
             org['title'] == title),
            -1)
        return index<|endoftext|>


<commit_before>from django.conf import settings
from django.utils.translation import LANGUAGE_SESSION_KEY
import django

if django.VERSION[1]<10:
   class LangMiddleware(object):
      '''
      Inject the lang information into session
      '''

      def process_request(self, req):
         '''
         Get the lang information from req.GET or settings, then inject into the session
         '''
         lang=req.session.get(LANGUAGE_SESSION_KEY, getattr(settings, 'LANGUAGE_CODE', None))
         if req.method=='GET' and 'lang' in req.GET:
            lang=req.GET['lang']
         req.session[LANGUAGE_SESSION_KEY]=lang
else:
   class LangMiddleware(object):
      def __init__(self, get_response):
         self.get_response=get_response

      def __call__(self, req):
         '''
         Get the lang information from req.GET or settings, then inject into the session
         '''
         lang=req.session.get(LANGUAGE_SESSION_KEY, getattr(settings, 'LANGUAGE_CODE', None))
         if req.method=='GET' and 'lang' in req.GET:
            lang=req.GET['lang']
         req.session[LANGUAGE_SESSION_KEY]=lang

         return self.get_response(req)
<commit_msg>Fix the LangMiddleware; Ignore HTTP_ACCEPT_LANGUAGE meta data<commit_after>from django.conf import settings
from django.utils import translation
import django

if django.VERSION[1]<10:
   class LangMiddleware(object):
      '''
      Inject the lang information into session
      '''

      def process_request(self, req):
         '''
         Get the lang information from req.GET or settings, then inject into the session
         '''
         lang=req.session.get(translation.LANGUAGE_SESSION_KEY, getattr(settings, 'LANGUAGE_CODE', None))
         if req.method=='GET' and 'lang' in req.GET:
            lang=req.GET['lang']
         if hasattr(settings, 'FORCE_LANGUAGE_CODE'):
            if 'HTTP_ACCEPT_LANGUAGE' in req.META:
               del req.META['HTTP_ACCEPT_LANGUAGE']
            lang=getattr(settings, 'FORCE_LANGUAGE_CODE')

         req.session[translation.LANGUAGE_SESSION_KEY]=lang
         translation.activate(lang)
else:
   class LangMiddleware(object):
      '''
      Inject the lang information into session
      '''

      def __init__(self, get_response):
         self.get_response=get_response

      def __call__(self, req):
         '''
         Get the lang information from req.GET or settings, then inject into the session
         '''
         lang=req.session.get(translation.LANGUAGE_SESSION_KEY, getattr(settings, 'LANGUAGE_CODE', None))
         if req.method=='GET' and 'lang' in req.GET:
            lang=req.GET['lang']
         if hasattr(settings, 'FORCE_LANGUAGE_CODE'):
            if 'HTTP_ACCEPT_LANGUAGE' in req.META:
               del req.META['HTTP_ACCEPT_LANGUAGE']
            lang=getattr(settings, 'FORCE_LANGUAGE_CODE')

         req.session[translation.LANGUAGE_SESSION_KEY]=lang
         translation.activate(lang)

         return self.get_response(req)

<|endoftext|>


<commit_before>import neovim
import os, subprocess
import json
import threading
import time
from functools import partial

class RPC:
    def __init__(self, infile, outfile, handler):
        self.infile = infile
        self.outfile = outfile
        self.handler = handler

    def call(self, method, params, mid=None):
        content = {
                "jsonrpc": "2.0",
                "method": method,
                "params": params
                }
        if mid is not None:
            content["id"] = mid
        content = json.dumps(content)
        message = (
                "Content-Length: {}\r\n\r\n"
                "{}".format(len(content), content)
                )
        print(content)
        print()
        self.outfile.write(message)
        self.outfile.flush()

    def serve(self):
        while True:
            line = self.infile.readline()
            if line:
                contentLength = int(line.split(":")[1])
                self.infile.readline()
                content = self.infile.read(contentLength)
                print(content)
                print()
                self.handler.handle(json.loads(content))

@neovim.plugin
class LanguageClient:
    def __init__(self, nvim):
        self.nvim = nvim
        self.server = subprocess.Popen(
            ["/bin/bash", "/opt/rls/wrapper.sh"],
            # ["cargo", "run", "--manifest-path=/opt/rls/Cargo.toml"],
            # ['langserver-go', '-trace', '-logfile', '/tmp/langserver-go.log'],
            stdin=subprocess.PIPE,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            universal_newlines=True)
        self.rpc = RPC(self.server.stdout, self.server.stdin, self)
        threading.Thread(target=self.rpc.serve, name="RPC Server", daemon=True).start()
        self.mid = 0
        self.queue = {}
        self.capabilities = {}

    def incMid(self) -> int:
        mid = self.mid
        self.mid += 1
        return mid

    def echo(self, message):
        self.nvim.command('echo "{}"'.format(message))

    @neovim.command('LanguageClientInitialize')
    def initialize(self, rootPath: str=None, cb=None):
        if rootPath is None:
            rootPath = getRootPath(self.nvim.current.buffer.name)

        mid = self.incMid()
        self.queue[mid] = partial(self.handleInitializeResponse, cb=cb)

        self.rpc.call('initialize', {
            "processId": os.getpid(),
            "rootPath": rootPath,
            "capabilities":{},
            "trace":"verbose"
            }, mid)

    def handleInitializeResponse(self, result: dict, cb):
        self.capabilities = result['capabilities']
        self.nvim.command('echom "LanguageClient started."')
        if cb is not None:
            cb(result)

    @neovim.function('LanguageClient_textDocument_didOpen')
    def textDocument_didOpen(self, filename: str=None):
        if filename is None:
            filename = self.nvim.current.buffer.name

        uri = convertToURI(filename)
        languageId = self.nvim.eval('&filetype')
        with open(filename) as f:
            text = f.read()

        self.rpc.call('textDocument/didOpen', {
            "uri": uri,
            "languageId": languageId,
            "version": 1,
            "text": text
            })

    @neovim.function('LanguageClient_textDocument_hover')
    def textDocument_hover(self, filename: str, line: int, character: int, cb=None):
       mid = self.incMid()
       self.queue[mid] = partial(self.handleTextDocumentHoverResponse, cb=cb)

       self.rpc.call('textDocument/hover', {
           "textDocument": {
               "uri": convertToURI(filename)
               },
           "position": {
               "line": line,
               "character": character
               }
           }, mid)

    def handleTextDocumentHoverResponse(self, result: dict, cb):
        value = ''
        for content in result['contents']:
            value += content['value']
        self.echo(value)
        if cb is not None:
            cb(value)

    def textDocument_publishDiagnostics(self, params):
        uri = params['uri']
        for diagnostic in params['diagnostics']:
            source = diagnostic['source']
            severity = diagnostic['severity']
            message = diagnostic['message']
            # TODO: escape speical character
            # self.nvim.command('echom "{}"'.format(message))
            self.nvim.command('echom "Diagnostic message received"')

    def handle(self, message):
        if 'result' in message: # got response
            mid = message['id']
            self.queue[mid](message['result'])
            del self.queue[mid]
        else: # request/notification
            methodname = message['method'].replace('/', '_')
            if hasattr(self, methodname):
                getattr(self, methodname)(message['params'])

def getRootPath(filename: str) -> str:
    if filename.endswith('.rs'):
        return traverseUp(filename, lambda path:
                os.path.exists(os.path.join(path, 'Cargo.toml')))
    # TODO: detect for other filetypes
    else:
        return filename

def traverseUp(path: str, stop) -> str:
    if stop(path):
        return path
    else:
        return traverseUp(os.path.dirname(path), stop)

def test_getRootPath():
    assert getRootPath("/tmp/sample-rs/src/main.rs") == "/tmp/sample-rs"

def convertToURI(filename: str) -> str:
    return "file://" + filename

def test_convertToURI():
    assert convertToURI("/tmp/sample-rs/src/main.rs") == "file:///tmp/sample-rs/src/main.rs"

def assertEqual(v1, v2):
    if v1 != v2:
        raise Exception('Assertion failed, {} == {}'.format(v1, v2))

class TestLanguageClient():
    @classmethod
    def setup_class(cls):
        nvim = neovim.attach('child', argv=['/usr/bin/env', 'nvim', '--embed'])
        cls.client = LanguageClient(nvim)

    def test_initialize(self):
        # initialize
        self.client.initialize("/private/tmp/sample-rs")
        while len(self.client.queue) > 0:
            time.sleep(0.1)

        ## wait for notification
        # time.sleep(300)

    def test_textDocument_hover(self):
        # textDocument/didOpen
        self.client.textDocument_didOpen("/private/tmp/sample-rs/src/main.rs")

        time.sleep(3)

        # textDocument/hover
        self.client.textDocument_hover("/private/tmp/sample-rs/src/main.rs", 8, 22,
                lambda value: assertEqual(value, 'fn () -> i32'))
        while len(self.client.queue) > 0:
            time.sleep(0.1)

<commit_msg>Escape special character in echo.<commit_after>import neovim
import os, subprocess
import json
import threading
import time
from functools import partial

class RPC:
    def __init__(self, infile, outfile, handler):
        self.infile = infile
        self.outfile = outfile
        self.handler = handler

    def call(self, method, params, mid=None):
        content = {
                "jsonrpc": "2.0",
                "method": method,
                "params": params
                }
        if mid is not None:
            content["id"] = mid
        content = json.dumps(content)
        message = (
                "Content-Length: {}\r\n\r\n"
                "{}".format(len(content), content)
                )
        print(content)
        print()
        self.outfile.write(message)
        self.outfile.flush()

    def serve(self):
        while True:
            line = self.infile.readline()
            if line:
                contentLength = int(line.split(":")[1])
                self.infile.readline()
                content = self.infile.read(contentLength)
                print(content)
                print()
                self.handler.handle(json.loads(content))

@neovim.plugin
class LanguageClient:
    def __init__(self, nvim):
        self.nvim = nvim
        self.server = subprocess.Popen(
            ["/bin/bash", "/opt/rls/wrapper.sh"],
            # ["cargo", "run", "--manifest-path=/opt/rls/Cargo.toml"],
            # ['langserver-go', '-trace', '-logfile', '/tmp/langserver-go.log'],
            stdin=subprocess.PIPE,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            universal_newlines=True)
        self.rpc = RPC(self.server.stdout, self.server.stdin, self)
        threading.Thread(target=self.rpc.serve, name="RPC Server", daemon=True).start()
        self.mid = 0
        self.queue = {}
        self.capabilities = {}

    def incMid(self) -> int:
        mid = self.mid
        self.mid += 1
        return mid

    def echo(self, message):
        self.nvim.command("echom '{}'".format(message.replace("'", "''")))

    @neovim.command('LanguageClientInitialize')
    def initialize(self, rootPath: str=None, cb=None):
        if rootPath is None:
            rootPath = getRootPath(self.nvim.current.buffer.name)

        mid = self.incMid()
        self.queue[mid] = partial(self.handleInitializeResponse, cb=cb)

        self.rpc.call('initialize', {
            "processId": os.getpid(),
            "rootPath": rootPath,
            "capabilities":{},
            "trace":"verbose"
            }, mid)

    def handleInitializeResponse(self, result: dict, cb):
        self.capabilities = result['capabilities']
        self.nvim.command('echom "LanguageClient started."')
        if cb is not None:
            cb(result)

    @neovim.function('LanguageClient_textDocument_didOpen')
    def textDocument_didOpen(self, filename: str=None):
        if filename is None:
            filename = self.nvim.current.buffer.name

        uri = convertToURI(filename)
        languageId = self.nvim.eval('&filetype')
        with open(filename) as f:
            text = f.read()

        self.rpc.call('textDocument/didOpen', {
            "uri": uri,
            "languageId": languageId,
            "version": 1,
            "text": text
            })

    @neovim.function('LanguageClient_textDocument_hover')
    def textDocument_hover(self, filename: str, line: int, character: int, cb=None):
       mid = self.incMid()
       self.queue[mid] = partial(self.handleTextDocumentHoverResponse, cb=cb)

       self.rpc.call('textDocument/hover', {
           "textDocument": {
               "uri": convertToURI(filename)
               },
           "position": {
               "line": line,
               "character": character
               }
           }, mid)

    def handleTextDocumentHoverResponse(self, result: dict, cb):
        value = ''
        for content in result['contents']:
            value += content['value']
        self.echo(value)
        if cb is not None:
            cb(value)

    def textDocument_publishDiagnostics(self, params):
        uri = params['uri']
        for diagnostic in params['diagnostics']:
            source = diagnostic['source']
            severity = diagnostic['severity']
            message = diagnostic['message']
            # TODO: escape speical character
            # self.nvim.command('echom "{}"'.format(message))
            self.nvim.command('echom "Diagnostic message received"')

    def handle(self, message):
        if 'result' in message: # got response
            mid = message['id']
            self.queue[mid](message['result'])
            del self.queue[mid]
        else: # request/notification
            methodname = message['method'].replace('/', '_')
            if hasattr(self, methodname):
                getattr(self, methodname)(message['params'])

def getRootPath(filename: str) -> str:
    if filename.endswith('.rs'):
        return traverseUp(filename, lambda path:
                os.path.exists(os.path.join(path, 'Cargo.toml')))
    # TODO: detect for other filetypes
    else:
        return filename

def traverseUp(path: str, stop) -> str:
    if stop(path):
        return path
    else:
        return traverseUp(os.path.dirname(path), stop)

def test_getRootPath():
    assert getRootPath("/tmp/sample-rs/src/main.rs") == "/tmp/sample-rs"

def convertToURI(filename: str) -> str:
    return "file://" + filename

def test_convertToURI():
    assert convertToURI("/tmp/sample-rs/src/main.rs") == "file:///tmp/sample-rs/src/main.rs"

def assertEqual(v1, v2):
    if v1 != v2:
        raise Exception('Assertion failed, {} == {}'.format(v1, v2))

class TestLanguageClient():
    @classmethod
    def setup_class(cls):
        nvim = neovim.attach('child', argv=['/usr/bin/env', 'nvim', '--embed'])
        cls.client = LanguageClient(nvim)

    def test_initialize(self):
        # initialize
        self.client.initialize("/private/tmp/sample-rs")
        while len(self.client.queue) > 0:
            time.sleep(0.1)

        ## wait for notification
        # time.sleep(300)

    def test_textDocument_hover(self):
        # textDocument/didOpen
        self.client.textDocument_didOpen("/private/tmp/sample-rs/src/main.rs")

        time.sleep(3)

        # textDocument/hover
        self.client.textDocument_hover("/private/tmp/sample-rs/src/main.rs", 8, 22,
                lambda value: assertEqual(value, 'fn () -> i32'))
        while len(self.client.queue) > 0:
            time.sleep(0.1)

<|endoftext|>


<commit_before># This file is part of LiSE, a framework for life simulation games.
# Copyright (c) 2013 Zachary Spector,  zacharyspector@gmail.com
"""Common utility functions and data structures.

"""
from operator import attrgetter

def getatt(attribute_name):
    """An easy way to make an alias"""
    return property(attrgetter(attribute_name))

def path_len(graph, path, weight=None):
    """Return the number of ticks it will take to follow ``path``,
    assuming the portals' ``weight`` attribute is how long it will
    take to go through that portal--if unspecified, 1 tick.

    """
    n = 0
    path = list(path)  # local copy
    prevnode = path.pop(0)
    while path:
        nextnode = path.pop(0)
        edge = graph.edge[prevnode][nextnode]
        n += edge[weight] if weight and hasattr(edge, weight) else 1
        prevnode = nextnode
    return n

def dict_diff(old, new):
    """Return a dictionary containing the items of ``new`` that are either
    absent from ``old`` or whose values are different; as well as the
    value ``None`` for those keys that are present in ``old``, but
    absent from ``new``.

    Useful for describing changes between two versions of a dict.

    """
    r = {}
    for k in set(old.keys()).union(new.keys()):
        if k in old:
            if k not in new:
                r[k] = None
            elif old[k] != new[k]:
                r[k] = new[k]
        else:  # k in new
            r[k] = new[k]
    return r

def list_diff(old, new):
    r = {item: True for item in new}
    for item in old:
        if item not in new:
            r[item] = False
    return r

def keycache_iter(keycache, branch, tick, get_iterator):
    if branch not in keycache:
        keycache[branch] = {}
    if tick not in keycache[branch]:
        keycache[branch][tick] = set(get_iterator())
    yield from keycache[branch][tick]
<commit_msg>Make diffs more parsimonious still<commit_after># This file is part of LiSE, a framework for life simulation games.
# Copyright (c) 2013 Zachary Spector,  zacharyspector@gmail.com
"""Common utility functions and data structures.

"""
from operator import attrgetter

def getatt(attribute_name):
    """An easy way to make an alias"""
    return property(attrgetter(attribute_name))

def path_len(graph, path, weight=None):
    """Return the number of ticks it will take to follow ``path``,
    assuming the portals' ``weight`` attribute is how long it will
    take to go through that portal--if unspecified, 1 tick.

    """
    n = 0
    path = list(path)  # local copy
    prevnode = path.pop(0)
    while path:
        nextnode = path.pop(0)
        edge = graph.edge[prevnode][nextnode]
        n += edge[weight] if weight and hasattr(edge, weight) else 1
        prevnode = nextnode
    return n

def dict_diff(old, new):
    """Return a dictionary containing the items of ``new`` that are either
    absent from ``old`` or whose values are different; as well as the
    value ``None`` for those keys that are present in ``old``, but
    absent from ``new``.

    Useful for describing changes between two versions of a dict.

    """
    r = {}
    for k in set(old.keys()).union(new.keys()):
        if k in old:
            if k not in new:
                r[k] = None
            elif old[k] != new[k]:
                r[k] = new[k]
        else:  # k in new
            r[k] = new[k]
    return r

def list_diff(old, new):
    r = {}
    for item in old:
        if item not in new:
            r[item] = False
    for item in new:
        if item not in old:
            r[item] = True
    return r

def keycache_iter(keycache, branch, tick, get_iterator):
    if branch not in keycache:
        keycache[branch] = {}
    if tick not in keycache[branch]:
        keycache[branch][tick] = set(get_iterator())
    yield from keycache[branch][tick]
<|endoftext|>


<commit_before>"""*
Vector Quantization Module
    Provides several routines used in creating a code book from a set of
    observations and comparing a set of observations to a code book.

    All routines expect an "observation vector" to be stored in each
    row of the obs matrix.  Similarly the codes are stored row wise
    in the code book matrix.

    whiten(obs) --
        Normalize a group of observations on a per feature basis
    vq(obs,code_book,return_dist=0) --
        Calculate code book membership of obs
    kmeans(obs,k_or_guess,iter=20,thresh=1e-5) --
        Train a codebook for mimimum distortion using the kmeans algorithm

*"""
from Numeric import *
from RandomArray import randint
import scipy
from scipy.common.misc import _common_type

def whiten(obs):
    """* Normalize a group of observations on a per feature basis

    Description:
        Before running kmeans algorithms, it is beneficial to "whiten", or scale,
        the observation data on a per feature basis.  This is done by dividing
        each feature by its standard deviation across all observations.
    Arguments:
        obs -- 2D array.
                Each row of the array is an observation.  The
                columns are the "features" seen during each observation

                          #   f0    f1    f2
                    obs = [[  1.,   1.,   1.],  #o0
                           [  2.,   2.,   2.],  #o1
                           [  3.,   3.,   3.],  #o2
                           [  4.,   4.,   4.]]) #o3

        XXX perhaps should have an axis variable here.
    Outputs:
        result -- 2D array.
                Contains the values in obs scaled by the standard devation of
                each column.

    Test:
        >>> features  = array([[  1.9,2.3,1.7],
        ...                    [  1.5,2.5,2.2],
        ...                    [  0.8,0.6,1.7,]])
        >>> whiten(features)
        array([[ 3.41250074,  2.20300046,  5.88897275],
               [ 2.69407953,  2.39456571,  7.62102355],
               [ 1.43684242,  0.57469577,  5.88897275]])

    *"""
    std_dev = scipy.stdev(obs,axis=0)
    return obs / std_dev

def vq(obs,code_book,return_dist=0):
    """* Vector Quantization: assign features sets to codes in a code book.

    Description:
        Vector quantization determines which code in the code book best
        represents an observation of a target.  The features of each
        observation are compared to each code in the book, and assigned the
        one closest to it.  The observations are contained in the obs array.
        These features should be "whitened," or nomalized by the standard
        deviation of all the features before being quantized.  The code book
        can be created using the kmeans algorithm or something similar.

    Note:
       This currently forces 32 bit math precision for speed.  Anyone know
       of a situation where this undermines the accuracy of the algorithm?

    Arguments:
        obs -- 2D array.
                Each row of the array is an observation.  The
                columns are the "features" seen during each observation
                The features must be whitened first using the
                whiten function or something equivalent.
        code_book -- 2D array.
                The code book is usually generated using the kmeans
                algorithm.  Each row of the array holds a different
                code, and the columns are the features of the code.
                                #   c0    c1    c2   c3
                    code_book = [[  1.,   2.,   3.,   4.],  #f0
                                 [  1.,   2.,   3.,   4.],  #f1
                                 [  1.,   2.,   3.,   4.]]) #f2
    Outputs:
        result -- 1D array.
                If obs is a NxM array, then a length M array
                is returned that holds the selected code book index for
                each observation.
    Reference:
    Test:
        >>> code_book = array([[1.,1.,1.],
        ...                    [2.,2.,2.]])
        >>> features  = array([[  1.9,2.3,1.7],
        ...                    [  1.5,2.5,2.2],
        ...                    [  0.8,0.6,1.7]])
        >>> vq(features,code_book,return_dist=1)
        (array([1, 1, 0]), array([ 0.43588989,  0.73484692,  0.83066239]))

    *"""
    No,Nf = shape(obs) #No = observation count, Nf = feature count
    # code books and observations should have same number of features
    assert(Nf == code_book.shape[1])
    code = [];min_dist = []
    #create a memory block to use for the difference calculations
    diff = zeros(shape(code_book),_common_type(obs,code_book))
    for o in obs:
        subtract(code_book,o,diff) # faster version of --> diff = code_book - o
        dist = sqrt(sum(diff*diff,-1))
        code.append(argmin(dist))
        #something weird here dst does not work reliably because it sometime
        # returns an array of goofy length. use min() until I figure it out.
        if return_dist:
            dst = minimum.reduce(dist,0)
            try:    dst = dst[0]
            except: pass
            min_dist.append(dst)
    if return_dist: return array(code), array(min_dist)
    else:           return array(code)

def vq2(obs,code_book,return_dist=0):
    """* This could be faster when number of codebooks is small, but it becomes
         a real memory hog when codebook is large.  It requires NxMxO storage
         where N=number of obs, M = number of features, and O = number of codes.
    *"""
    No,Nf = shape(obs) #No = observation count, Nf = feature count
    # code books and observations should have same number of features
    assert(Nf == code_book.shape[1])
    diff = obs[NewAxis,:,:]-code_book[:,NewAxis,:]
    dist = sqrt(sum(diff*diff,-1))
    code = argmin(dist,0)
    if return_dist:
        min_dist = minimum.reduce(dist,0) #the next line I think is equivalent - and should be faster
        #min_dist = choose(code,dist) # but in practice, didn't seem to make much difference.
        return code, min_dist
    else:
        return code
"""*
    A third approach that combines ideas from vq and vq2 could possibly be faster.
    It would segment the obs into small chunks of obs (maybe 5 or 10 or even
    calculated for best memory usage...) and then reuse the memory allocated for
    diff for every new chunk.  This moves as much of the loops to C without
    having to allocate huge blocks of memory (as in vq2).

    A quick test showed this might gain 10-20% on windows, but thats it.
*"""

def kmeans_(obs,guess,thresh=1e-5):
    """* See kmeans
    Outputs:
        code_book -- the lowest distortion codebook found.
        avg_dist -- the average distance a observation is
                    from a code in the book.  Lower means
                    the code_book matches the data better.
    Test:
        Note: not whitened in this example.

        >>> features  = array([[ 1.9,2.3],
        ...                    [ 1.5,2.5],
        ...                    [ 0.8,0.6],
        ...                    [ 0.4,1.8],
        ...                    [ 1.0,1.0]])
        >>> book = array((features[0],features[2]))
        >>> kmeans_(features,book)
        (array([[ 1.7       ,  2.4       ],
               [ 0.73333333,  1.13333333]]), 0.40563916697728591)

    *"""

    code_book = array(guess,copy=1)
    Nc = code_book.shape[0]
    avg_dist=[]
    diff = thresh+1.
    while diff>thresh:
        print diff
        #compute membership and distances between obs and code_book
        obs_code, distort = vq(obs,code_book,return_dist=1)
        avg_dist.append(scipy.mean(distort))
        #recalc code_book as centroids of associated obs
        if(diff > thresh):
            has_members = []
            for i in arange(Nc):
                cell_members = compress(equal(obs_code,i),obs,0)
                if cell_members.shape[0] > 0:
                    code_book[i] = scipy.mean(cell_members,0)
                    has_members.append(i)
            #remove code_books that didn't have any members
            code_book = take(code_book,has_members,0)
        if len(avg_dist) > 1:
            diff = avg_dist[-2] - avg_dist[-1]
    #print avg_dist
    return code_book, avg_dist[-1]

def kmeans(obs,k_or_guess,iter=20,thresh=1e-5):
    """* Generate a code book with minimum distortion

    Description:

    Arguments:
        obs -- 2D array
                Each row of the array is an observation.  The
                columns are the "features" seen during each observation
                The features must be whitened first using the
                whiten function or something equivalent.
        k_or_guess -- integer or 2D array.
            If integer, it is the number of code book elements.
            If a 2D array, the array is used as the intial guess for
            the code book.  The array should have k rows, and the
            same number of columns (features) as the obs array.
        iter -- integer.
            The number of times to restart the kmeans algorithm with
            a new initial guess.  If k_or_guess is a 2D array (codebook),
            this argument is ignored and only 1 iteration is run.
        thresh -- float
            Terminate each kmeans run when the distortion change from
            one iteration to the next is less than this value.
    Outputs:
        codesbook -- 2D array.
            The codes that best fit the observation
        distortion -- float
            The distortion between the observations and the codes.
    Reference:
    Test:
        ("Not checked carefully for accuracy..." he said sheepishly)

        >>> features  = array([[ 1.9,2.3],
        ...                    [ 1.5,2.5],
        ...                    [ 0.8,0.6],
        ...                    [ 0.4,1.8],
        ...                    [ 0.1,0.1],
        ...                    [ 0.2,1.8],
        ...                    [ 2.0,0.5],
        ...                    [ 0.3,1.5],
        ...                    [ 1.0,1.0]])
        >>> whitened = whiten(features)
        >>> book = array((whitened[0],whitened[2]))
        >>> kmeans(whitened,book)
        (array([[ 2.3110306 ,  2.86287398],
               [ 0.93218041,  1.24398691]]), 0.85684700941625547)

        >>> import RandomArray
        >>> RandomArray.seed(1000,2000)
        >>> codes = 3
        >>> kmeans(whitened,codes)
        (array([[ 2.3110306 ,  2.86287398],
               [ 1.32544402,  0.65607529],
               [ 0.40782893,  2.02786907]]), 0.5196582527686241)

    *"""
    if int(iter) < 1:
        raise ValueError, 'iter must be >= to 1.'
    if type(k_or_guess) == type(array([])):
        guess = k_or_guess
        result = kmeans_(obs,guess,thresh=thresh)
    else:
        best_dist = 100000 #initialize best distance value to a large value
        No = obs.shape[0]
        k = k_or_guess
        #print 'kmeans iter: ',
        for i in range(iter):
            #print i,
            #the intial code book is randomly selected from observations
            guess = take(obs,randint(0,No,k),0)
            book,dist = kmeans_(obs,guess,thresh=thresh)
            if dist < best_dist:
                best_book = book
                best_dist = dist
        #print
        result = best_book,best_dist
    return result               <commit_msg>Changed vq interface removing option to not return distortion values.  This makes it compatible with the SWIGGED version (which can't handle optional arguments)<commit_after>"""*
Vector Quantization Module
    Provides several routines used in creating a code book from a set of
    observations and comparing a set of observations to a code book.

    All routines expect an "observation vector" to be stored in each
    row of the obs matrix.  Similarly the codes are stored row wise
    in the code book matrix.

    whiten(obs) --
        Normalize a group of observations on a per feature basis
    vq(obs,code_book) --
        Calculate code book membership of obs
    kmeans(obs,k_or_guess,iter=20,thresh=1e-5) --
        Train a codebook for mimimum distortion using the kmeans algorithm

*"""
from Numeric import *
from RandomArray import randint
import scipy
from scipy.common.misc import _common_type

def whiten(obs):
    """* Normalize a group of observations on a per feature basis

    Description:
        Before running kmeans algorithms, it is beneficial to "whiten", or scale,
        the observation data on a per feature basis.  This is done by dividing
        each feature by its standard deviation across all observations.
    Arguments:
        obs -- 2D array.
                Each row of the array is an observation.  The
                columns are the "features" seen during each observation

                          #   f0    f1    f2
                    obs = [[  1.,   1.,   1.],  #o0
                           [  2.,   2.,   2.],  #o1
                           [  3.,   3.,   3.],  #o2
                           [  4.,   4.,   4.]]) #o3

        XXX perhaps should have an axis variable here.
    Outputs:
        result -- 2D array.
                Contains the values in obs scaled by the standard devation of
                each column.

    Test:
        >>> features  = array([[  1.9,2.3,1.7],
        ...                    [  1.5,2.5,2.2],
        ...                    [  0.8,0.6,1.7,]])
        >>> whiten(features)
        array([[ 3.41250074,  2.20300046,  5.88897275],
               [ 2.69407953,  2.39456571,  7.62102355],
               [ 1.43684242,  0.57469577,  5.88897275]])

    *"""
    std_dev = scipy.stdev(obs,axis=0)
    return obs / std_dev

def vq(obs,code_book):
    """* Vector Quantization: assign features sets to codes in a code book.

    Description:
        Vector quantization determines which code in the code book best
        represents an observation of a target.  The features of each
        observation are compared to each code in the book, and assigned the
        one closest to it.  The observations are contained in the obs array.
        These features should be "whitened," or nomalized by the standard
        deviation of all the features before being quantized.  The code book
        can be created using the kmeans algorithm or something similar.

    Note:
       This currently forces 32 bit math precision for speed.  Anyone know
       of a situation where this undermines the accuracy of the algorithm?

    Arguments:
        obs -- 2D array.
                Each row of the array is an observation.  The
                columns are the "features" seen during each observation
                The features must be whitened first using the
                whiten function or something equivalent.
        code_book -- 2D array.
                The code book is usually generated using the kmeans
                algorithm.  Each row of the array holds a different
                code, and the columns are the features of the code.
                                #   c0    c1    c2   c3
                    code_book = [[  1.,   2.,   3.,   4.],  #f0
                                 [  1.,   2.,   3.,   4.],  #f1
                                 [  1.,   2.,   3.,   4.]]) #f2
    Outputs:
        code -- 1D array.
                If obs is a NxM array, then a length M array
                is returned that holds the selected code book index for
                each observation.
        dist -- 1D array.
        		The distortion (distance) between the observation and
        		its nearest code
    Reference:
    Test:
        >>> code_book = array([[1.,1.,1.],
        ...                    [2.,2.,2.]])
        >>> features  = array([[  1.9,2.3,1.7],
        ...                    [  1.5,2.5,2.2],
        ...                    [  0.8,0.6,1.7]])
        >>> vq(features,code_book)
        (array([1, 1, 0]), array([ 0.43588989,  0.73484692,  0.83066239]))

    *"""
    No,Nf = shape(obs) #No = observation count, Nf = feature count
    # code books and observations should have same number of features
    assert(Nf == code_book.shape[1])
    code = [];min_dist = []
    #create a memory block to use for the difference calculations
    diff = zeros(shape(code_book),_common_type(obs,code_book))
    for o in obs:
        subtract(code_book,o,diff) # faster version of --> diff = code_book - o
        dist = sqrt(sum(diff*diff,-1))
        code.append(argmin(dist))
        #something weird here dst does not work reliably because it sometime
        #returns an array of goofy length. Try except fixes it, but is ugly.
        dst = minimum.reduce(dist,0)
        try:    dst = dst[0]
        except: pass
        min_dist.append(dst)
    return array(code), array(min_dist)

def vq2(obs,code_book):
    """* This could be faster when number of codebooks is small, but it becomes
         a real memory hog when codebook is large.  It requires NxMxO storage
         where N=number of obs, M = number of features, and O = number of codes.
    *"""
    No,Nf = shape(obs) #No = observation count, Nf = feature count
    # code books and observations should have same number of features
    assert(Nf == code_book.shape[1])
    diff = obs[NewAxis,:,:]-code_book[:,NewAxis,:]
    dist = sqrt(sum(diff*diff,-1))
    code = argmin(dist,0)
    min_dist = minimum.reduce(dist,0) #the next line I think is equivalent - and should be faster
    #min_dist = choose(code,dist) # but in practice, didn't seem to make much difference.
    return code, min_dist

"""*
    A third approach that combines ideas from vq and vq2 could possibly be faster.
    It would segment the obs into small chunks of obs (maybe 5 or 10 or even
    calculated for best memory usage...) and then reuse the memory allocated for
    diff for every new chunk.  This moves as much of the loops to C without
    having to allocate huge blocks of memory (as in vq2).

    A quick test showed this might gain 10-20% on windows, but thats it.
*"""

def kmeans_(obs,guess,thresh=1e-5):
    """* See kmeans
    Outputs:
        code_book -- the lowest distortion codebook found.
        avg_dist -- the average distance a observation is
                    from a code in the book.  Lower means
                    the code_book matches the data better.
    Test:
        Note: not whitened in this example.

        >>> features  = array([[ 1.9,2.3],
        ...                    [ 1.5,2.5],
        ...                    [ 0.8,0.6],
        ...                    [ 0.4,1.8],
        ...                    [ 1.0,1.0]])
        >>> book = array((features[0],features[2]))
        >>> kmeans_(features,book)
        (array([[ 1.7       ,  2.4       ],
               [ 0.73333333,  1.13333333]]), 0.40563916697728591)

    *"""

    code_book = array(guess,copy=1)
    Nc = code_book.shape[0]
    avg_dist=[]
    diff = thresh+1.
    while diff>thresh:
        print diff
        #compute membership and distances between obs and code_book
        obs_code, distort = vq(obs,code_book)
        avg_dist.append(scipy.mean(distort))
        #recalc code_book as centroids of associated obs
        if(diff > thresh):
            has_members = []
            for i in arange(Nc):
                cell_members = compress(equal(obs_code,i),obs,0)
                if cell_members.shape[0] > 0:
                    code_book[i] = scipy.mean(cell_members,0)
                    has_members.append(i)
            #remove code_books that didn't have any members
            code_book = take(code_book,has_members,0)
        if len(avg_dist) > 1:
            diff = avg_dist[-2] - avg_dist[-1]
    #print avg_dist
    return code_book, avg_dist[-1]

def kmeans(obs,k_or_guess,iter=20,thresh=1e-5):
    """* Generate a code book with minimum distortion

    Description:

    Arguments:
        obs -- 2D array
                Each row of the array is an observation.  The
                columns are the "features" seen during each observation
                The features must be whitened first using the
                whiten function or something equivalent.
        k_or_guess -- integer or 2D array.
            If integer, it is the number of code book elements.
            If a 2D array, the array is used as the intial guess for
            the code book.  The array should have k rows, and the
            same number of columns (features) as the obs array.
        iter -- integer.
            The number of times to restart the kmeans algorithm with
            a new initial guess.  If k_or_guess is a 2D array (codebook),
            this argument is ignored and only 1 iteration is run.
        thresh -- float
            Terminate each kmeans run when the distortion change from
            one iteration to the next is less than this value.
    Outputs:
        codesbook -- 2D array.
            The codes that best fit the observation
        distortion -- float
            The distortion between the observations and the codes.
    Reference:
    Test:
        ("Not checked carefully for accuracy..." he said sheepishly)

        >>> features  = array([[ 1.9,2.3],
        ...                    [ 1.5,2.5],
        ...                    [ 0.8,0.6],
        ...                    [ 0.4,1.8],
        ...                    [ 0.1,0.1],
        ...                    [ 0.2,1.8],
        ...                    [ 2.0,0.5],
        ...                    [ 0.3,1.5],
        ...                    [ 1.0,1.0]])
        >>> whitened = whiten(features)
        >>> book = array((whitened[0],whitened[2]))
        >>> kmeans(whitened,book)
        (array([[ 2.3110306 ,  2.86287398],
               [ 0.93218041,  1.24398691]]), 0.85684700941625547)

        >>> import RandomArray
        >>> RandomArray.seed(1000,2000)
        >>> codes = 3
        >>> kmeans(whitened,codes)
        (array([[ 2.3110306 ,  2.86287398],
               [ 1.32544402,  0.65607529],
               [ 0.40782893,  2.02786907]]), 0.5196582527686241)

    *"""
    if int(iter) < 1:
        raise ValueError, 'iter must be >= to 1.'
    if type(k_or_guess) == type(array([])):
        guess = k_or_guess
        result = kmeans_(obs,guess,thresh=thresh)
    else:
        best_dist = 100000 #initialize best distance value to a large value
        No = obs.shape[0]
        k = k_or_guess
        #print 'kmeans iter: ',
        for i in range(iter):
            #print i,
            #the intial code book is randomly selected from observations
            guess = take(obs,randint(0,No,k),0)
            book,dist = kmeans_(obs,guess,thresh=thresh)
            if dist < best_dist:
                best_book = book
                best_dist = dist
        #print
        result = best_book,best_dist
    return result               <|endoftext|>


<commit_before>"""Class based built-in exception hierarchy.

New with Python 1.5, all standard built-in exceptions are now class objects by
default.  This gives Python's exception handling mechanism a more
object-oriented feel.  Traditionally they were string objects.  Python will
fallback to string based exceptions if the interpreter is invoked with the -X
option, or if some failure occurs during class exception initialization (in
this case a warning will be printed).

Most existing code should continue to work with class based exceptions.  Some
tricky uses of IOError may break, but the most common uses should work.

Here is a rundown of the class hierarchy.  You can change this by editing this
file, but it isn't recommended because the old string based exceptions won't
be kept in sync.  The class names described here are expected to be found by
the bltinmodule.c file.  If you add classes here, you must modify
bltinmodule.c or the exceptions won't be available in the __builtin__ module,
nor will they be accessible from C.

The classes with a `*' are new since Python 1.5.  They are defined as tuples
containing the derived exceptions when string-based exceptions are used.  If
you define your own class based exceptions, they should be derived from
Exception.

Exception(*)
 |
 +-- SystemExit
 +-- StandardError(*)
      |
      +-- KeyboardInterrupt
      +-- ImportError
      +-- EnvironmentError(*)
      |    |
      |    +-- IOError
      |    +-- OSError(*)
      |
      +-- EOFError
      +-- RuntimeError
      |    |
      |    +-- NotImplementedError(*)
      |
      +-- NameError
      |    |
      |    +-- UnboundLocalError(*)
      |
      +-- AttributeError
      +-- SyntaxError
      +-- TypeError
      +-- AssertionError
      +-- LookupError(*)
      |    |
      |    +-- IndexError
      |    +-- KeyError
      |
      +-- ArithmeticError(*)
      |    |
      |    +-- OverflowError
      |    +-- ZeroDivisionError
      |    +-- FloatingPointError
      |
      +-- ValueError
      +-- SystemError
      +-- MemoryError
"""

class Exception:
    """Proposed base class for all exceptions."""
    def __init__(self, *args):
        self.args = args

    def __str__(self):
        if not self.args:
            return ''
        elif len(self.args) == 1:
            return str(self.args[0])
        else:
            return str(self.args)

    def __getitem__(self, i):
        return self.args[i]

class StandardError(Exception):
    """Base class for all standard Python exceptions."""
    pass

class SyntaxError(StandardError):
    """Invalid syntax."""
    filename = lineno = offset = text = None
    msg = ""
    def __init__(self, *args):
        self.args = args
        if len(self.args) >= 1:
            self.msg = self.args[0]
        if len(self.args) == 2:
            info = self.args[1]
            try:
                self.filename, self.lineno, self.offset, self.text = info
            except:
                pass
    def __str__(self):
        return str(self.msg)

class EnvironmentError(StandardError):
    """Base class for I/O related errors."""
    def __init__(self, *args):
        self.args = args
        self.errno = None
        self.strerror = None
        self.filename = None
        if len(args) == 3:
            # open() errors give third argument which is the filename.  BUT,
            # so common in-place unpacking doesn't break, e.g.:
            #
            # except IOError, (errno, strerror):
            #
            # we hack args so that it only contains two items.  This also
            # means we need our own __str__() which prints out the filename
            # when it was supplied.
            self.errno, self.strerror, self.filename = args
            self.args = args[0:2]
        if len(args) == 2:
            # common case: PyErr_SetFromErrno()
            self.errno, self.strerror = args

    def __str__(self):
        if self.filename is not None:
            return '[Errno %s] %s: %s' % (self.errno, self.strerror,
                                          repr(self.filename))
        elif self.errno and self.strerror:
            return '[Errno %s] %s' % (self.errno, self.strerror)
        else:
            return StandardError.__str__(self)

class IOError(EnvironmentError):
    """I/O operation failed."""
    pass

class OSError(EnvironmentError):
    """OS system call failed."""
    pass

class RuntimeError(StandardError):
    """Unspecified run-time error."""
    pass

class NotImplementedError(RuntimeError):
    """Method or function hasn't been implemented yet."""
    pass

class SystemError(StandardError):
    """Internal error in the Python interpreter.

    Please report this to the Python maintainer, along with the traceback,
    the Python version, and the hardware/OS platform and version."""
    pass

class EOFError(StandardError):
    """Read beyond end of file."""
    pass

class ImportError(StandardError):
    """Import can't find module, or can't find name in module."""
    pass

class TypeError(StandardError):
    """Inappropriate argument type."""
    pass

class ValueError(StandardError):
    """Inappropriate argument value (of correct type)."""
    pass

class KeyboardInterrupt(StandardError):
    """Program interrupted by user."""
    pass

class AssertionError(StandardError):
    """Assertion failed."""
    pass

class ArithmeticError(StandardError):
    """Base class for arithmetic errors."""
    pass

class OverflowError(ArithmeticError):
    """Result too large to be represented."""
    pass

class FloatingPointError(ArithmeticError):
    """Floating point operation failed."""
    pass

class ZeroDivisionError(ArithmeticError):
    """Second argument to a division or modulo operation was zero."""
    pass

class LookupError(StandardError):
    """Base class for lookup errors."""
    pass

class IndexError(LookupError):
    """Sequence index out of range."""
    pass

class KeyError(LookupError):
    """Mapping key not found."""
    pass

class AttributeError(StandardError):
    """Attribute not found."""
    pass

class NameError(StandardError):
    """Name not found globally."""
    pass

class UnboundLocalError(NameError):
    """Local name referenced but not bound to a value."""
    pass

class MemoryError(StandardError):
    """Out of memory."""
    pass

class SystemExit(Exception):
    """Request to exit from the interpreter."""
    def __init__(self, *args):
        self.args = args
        if len(args) == 0:
            self.code = None
        elif len(args) == 1:
            self.code = args[0]
        else:
            self.code = args
<commit_msg>Added WindowsError, for Mark Hammond's extensions.<commit_after>"""Class based built-in exception hierarchy.

New with Python 1.5, all standard built-in exceptions are now class objects by
default.  This gives Python's exception handling mechanism a more
object-oriented feel.  Traditionally they were string objects.  Python will
fallback to string based exceptions if the interpreter is invoked with the -X
option, or if some failure occurs during class exception initialization (in
this case a warning will be printed).

Most existing code should continue to work with class based exceptions.  Some
tricky uses of IOError may break, but the most common uses should work.

Here is a rundown of the class hierarchy.  You can change this by editing this
file, but it isn't recommended because the old string based exceptions won't
be kept in sync.  The class names described here are expected to be found by
the bltinmodule.c file.  If you add classes here, you must modify
bltinmodule.c or the exceptions won't be available in the __builtin__ module,
nor will they be accessible from C.

The classes with a `*' are new since Python 1.5.  They are defined as tuples
containing the derived exceptions when string-based exceptions are used.  If
you define your own class based exceptions, they should be derived from
Exception.

Exception(*)
 |
 +-- SystemExit
 +-- StandardError(*)
      |
      +-- KeyboardInterrupt
      +-- ImportError
      +-- EnvironmentError(*)
      |    |
      |    +-- IOError
      |    +-- OSError(*)
      |         |
      |         +-- WindowsError(*)
      |
      +-- EOFError
      +-- RuntimeError
      |    |
      |    +-- NotImplementedError(*)
      |
      +-- NameError
      |    |
      |    +-- UnboundLocalError(*)
      |
      +-- AttributeError
      +-- SyntaxError
      +-- TypeError
      +-- AssertionError
      +-- LookupError(*)
      |    |
      |    +-- IndexError
      |    +-- KeyError
      |
      +-- ArithmeticError(*)
      |    |
      |    +-- OverflowError
      |    +-- ZeroDivisionError
      |    +-- FloatingPointError
      |
      +-- ValueError
      +-- SystemError
      +-- MemoryError
"""

class Exception:
    """Proposed base class for all exceptions."""
    def __init__(self, *args):
        self.args = args

    def __str__(self):
        if not self.args:
            return ''
        elif len(self.args) == 1:
            return str(self.args[0])
        else:
            return str(self.args)

    def __getitem__(self, i):
        return self.args[i]

class StandardError(Exception):
    """Base class for all standard Python exceptions."""
    pass

class SyntaxError(StandardError):
    """Invalid syntax."""
    filename = lineno = offset = text = None
    msg = ""
    def __init__(self, *args):
        self.args = args
        if len(self.args) >= 1:
            self.msg = self.args[0]
        if len(self.args) == 2:
            info = self.args[1]
            try:
                self.filename, self.lineno, self.offset, self.text = info
            except:
                pass
    def __str__(self):
        return str(self.msg)

class EnvironmentError(StandardError):
    """Base class for I/O related errors."""
    def __init__(self, *args):
        self.args = args
        self.errno = None
        self.strerror = None
        self.filename = None
        if len(args) == 3:
            # open() errors give third argument which is the filename.  BUT,
            # so common in-place unpacking doesn't break, e.g.:
            #
            # except IOError, (errno, strerror):
            #
            # we hack args so that it only contains two items.  This also
            # means we need our own __str__() which prints out the filename
            # when it was supplied.
            self.errno, self.strerror, self.filename = args
            self.args = args[0:2]
        if len(args) == 2:
            # common case: PyErr_SetFromErrno()
            self.errno, self.strerror = args

    def __str__(self):
        if self.filename is not None:
            return '[Errno %s] %s: %s' % (self.errno, self.strerror,
                                          repr(self.filename))
        elif self.errno and self.strerror:
            return '[Errno %s] %s' % (self.errno, self.strerror)
        else:
            return StandardError.__str__(self)

class IOError(EnvironmentError):
    """I/O operation failed."""
    pass

class OSError(EnvironmentError):
    """OS system call failed."""
    pass

class WindowsError(OSError):
    """MS-Windows OS system call failed."""
    pass

class RuntimeError(StandardError):
    """Unspecified run-time error."""
    pass

class NotImplementedError(RuntimeError):
    """Method or function hasn't been implemented yet."""
    pass

class SystemError(StandardError):
    """Internal error in the Python interpreter.

    Please report this to the Python maintainer, along with the traceback,
    the Python version, and the hardware/OS platform and version."""
    pass

class EOFError(StandardError):
    """Read beyond end of file."""
    pass

class ImportError(StandardError):
    """Import can't find module, or can't find name in module."""
    pass

class TypeError(StandardError):
    """Inappropriate argument type."""
    pass

class ValueError(StandardError):
    """Inappropriate argument value (of correct type)."""
    pass

class KeyboardInterrupt(StandardError):
    """Program interrupted by user."""
    pass

class AssertionError(StandardError):
    """Assertion failed."""
    pass

class ArithmeticError(StandardError):
    """Base class for arithmetic errors."""
    pass

class OverflowError(ArithmeticError):
    """Result too large to be represented."""
    pass

class FloatingPointError(ArithmeticError):
    """Floating point operation failed."""
    pass

class ZeroDivisionError(ArithmeticError):
    """Second argument to a division or modulo operation was zero."""
    pass

class LookupError(StandardError):
    """Base class for lookup errors."""
    pass

class IndexError(LookupError):
    """Sequence index out of range."""
    pass

class KeyError(LookupError):
    """Mapping key not found."""
    pass

class AttributeError(StandardError):
    """Attribute not found."""
    pass

class NameError(StandardError):
    """Name not found globally."""
    pass

class UnboundLocalError(NameError):
    """Local name referenced but not bound to a value."""
    pass

class MemoryError(StandardError):
    """Out of memory."""
    pass

class SystemExit(Exception):
    """Request to exit from the interpreter."""
    def __init__(self, *args):
        self.args = args
        if len(args) == 0:
            self.code = None
        elif len(args) == 1:
            self.code = args[0]
        else:
            self.code = args
<|endoftext|>


<commit_before>#!/usr/bin/env python

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

"""
Google Cloud Vision API Python Beta Snippets

Example Usage:
python beta_snippets.py -h
python beta_snippets.py object-localization INPUT_IMAGE
python beta_snippets.py object-localization-uri gs://...
python beta_snippets.py handwritten-ocr INPUT_IMAGE
python beta_snippets.py handwritten-ocr-uri gs://...

For more information, the documentation at
https://cloud.google.com/vision/docs.
"""

import argparse
import io

# [START vision_localize_objects]
def localize_objects(path):
    """Localize objects in the local image.

    Args:
    path: The path to the local file.
    """
    from google.cloud import vision_v1p3beta1 as vision
    client = vision.ImageAnnotatorClient()

    with open(path, 'rb') as image_file:
        content = image_file.read()
    image = vision.types.Image(content=content)

    objects = client.object_localization(
        image=image).localized_object_annotations

    print('Number of objects found: {}'.format(len(objects)))
    for object_ in objects:
        print('\n{} (confidence: {})'.format(object_.name, object_.score))
        print('Normalized bounding polygon vertices: ')
        for vertex in object_.bounding_poly.normalized_vertices:
            print(' - ({}, {})'.format(vertex.x, vertex.y))
# [END vision_localize_objects]

# [START vision_localize_objects_gcs]
def localize_objects_uri(uri):
    """Localize objects in the image on Google Cloud Storage

    Args:
    uri: The path to the file in Google Cloud Storage (gs://...)
    """
    from google.cloud import vision_v1p3beta1 as vision
    client = vision.ImageAnnotatorClient()

    image = vision.types.Image()
    image.source.image_uri = uri

    objects = client.object_localization(
        image=image).localized_object_annotations

    print('Number of objects found: {}'.format(len(objects)))
    for object_ in objects:
        print('\n{} (confidence: {})'.format(object_.name, object_.score))
        print('Normalized bounding polygon vertices: ')
        for vertex in object_.bounding_poly.normalized_vertices:
            print(' - ({}, {})'.format(vertex.x, vertex.y))
# [END vision_localize_objects_gcs]

# [START vision_handwritten_ocr]
def detect_handwritten_ocr(path):
    """Detects handwritten characters in a local image.

    Args:
    path: The path to the local file.
    """
    from google.cloud import vision_v1p3beta1 as vision
    client = vision.ImageAnnotatorClient()

    with io.open(path, 'rb') as image_file:
        content = image_file.read()

    image = vision.types.Image(content=content)

    # Language hint codes for handwritten OCR:
    # en-t-i0-handwrit, mul-Latn-t-i0-handwrit
    # Note: Use only one language hint code per request for handwritten OCR.
    image_context = vision.types.ImageContext(
        language_hints=['en-t-i0-handwrit'])

    response = client.document_text_detection(image=image,
                                              image_context=image_context)

    print('Full Text: {}'.format(response.full_text_annotation.text))
    for page in response.full_text_annotation.pages:
        for block in page.blocks:
            print('\nBlock confidence: {}\n'.format(block.confidence))

            for paragraph in block.paragraphs:
                print('Paragraph confidence: {}'.format(
                    paragraph.confidence))

                for word in paragraph.words:
                    word_text = ''.join([
                        symbol.text for symbol in word.symbols
                    ])
                    print('Word text: {} (confidence: {})'.format(
                        word_text, word.confidence))

                    for symbol in word.symbols:
                        print('\tSymbol: {} (confidence: {})'.format(
                            symbol.text, symbol.confidence))
# [END vision_handwritten_ocr]

# [START vision_handwritten_ocr_gcs]
def detect_handwritten_ocr_uri(uri):
    """Detects handwritten characters in the file located in Google Cloud
    Storage.

    Args:
    uri: The path to the file in Google Cloud Storage (gs://...)
    """
    from google.cloud import vision_v1p3beta1 as vision
    client = vision.ImageAnnotatorClient()
    image = vision.types.Image()
    image.source.image_uri = uri

    # Language hint codes for handwritten OCR:
    # en-t-i0-handwrit, mul-Latn-t-i0-handwrit
    # Note: Use only one language hint code per request for handwritten OCR.
    image_context = vision.types.ImageContext(
        language_hints=['en-t-i0-handwrit'])

    response = client.document_text_detection(image=image,
                                              image_context=image_context)

    print('Full Text: {}'.format(response.full_text_annotation.text))
    for page in response.full_text_annotation.pages:
        for block in page.blocks:
            print('\nBlock confidence: {}\n'.format(block.confidence))

            for paragraph in block.paragraphs:
                print('Paragraph confidence: {}'.format(
                    paragraph.confidence))

                for word in paragraph.words:
                    word_text = ''.join([
                        symbol.text for symbol in word.symbols
                    ])
                    print('Word text: {} (confidence: {})'.format(
                        word_text, word.confidence))

                    for symbol in word.symbols:
                        print('\tSymbol: {} (confidence: {})'.format(
                            symbol.text, symbol.confidence))
# [END vision_handwritten_ocr_gcs]

if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter)
    subparsers = parser.add_subparsers(dest='command')

    object_parser = subparsers.add_parser(
        'object-localization', help=localize_objects.__doc__)
    object_parser.add_argument('path')

    object_uri_parser = subparsers.add_parser(
        'object-localization-uri', help=localize_objects_uri.__doc__)
    object_uri_parser.add_argument('uri')

    handwritten_parser = subparsers.add_parser(
        'handwritten-ocr', help=detect_handwritten_ocr.__doc__)
    handwritten_parser.add_argument('path')

    handwritten_uri_parser = subparsers.add_parser(
        'handwritten-ocr-uri', help=detect_handwritten_ocr_uri.__doc__)
    handwritten_uri_parser.add_argument('uri')

    args = parser.parse_args()

    if 'uri' in args.command:
        if 'object-localization-uri' in args.command:
            localize_objects_uri(args.uri)
        elif 'handwritten-ocr-uri' in args.command:
            detect_handwritten_ocr_uri(args.uri)
    else:
        if 'object-localization' in args.command:
            localize_objects(args.path)
        elif 'handwritten-ocr' in args.command:
            detect_handwritten_ocr(args.path)
<commit_msg>Udpate Beta Vision samples to use beta tags (#1640)<commit_after>#!/usr/bin/env python

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

"""
Google Cloud Vision API Python Beta Snippets

Example Usage:
python beta_snippets.py -h
python beta_snippets.py object-localization INPUT_IMAGE
python beta_snippets.py object-localization-uri gs://...
python beta_snippets.py handwritten-ocr INPUT_IMAGE
python beta_snippets.py handwritten-ocr-uri gs://...

For more information, the documentation at
https://cloud.google.com/vision/docs.
"""

import argparse
import io

# [START vision_localize_objects_beta]
def localize_objects(path):
    """Localize objects in the local image.

    Args:
    path: The path to the local file.
    """
    from google.cloud import vision_v1p3beta1 as vision
    client = vision.ImageAnnotatorClient()

    with open(path, 'rb') as image_file:
        content = image_file.read()
    image = vision.types.Image(content=content)

    objects = client.object_localization(
        image=image).localized_object_annotations

    print('Number of objects found: {}'.format(len(objects)))
    for object_ in objects:
        print('\n{} (confidence: {})'.format(object_.name, object_.score))
        print('Normalized bounding polygon vertices: ')
        for vertex in object_.bounding_poly.normalized_vertices:
            print(' - ({}, {})'.format(vertex.x, vertex.y))
# [END vision_localize_objects_beta]

# [START vision_localize_objects_gcs_beta]
def localize_objects_uri(uri):
    """Localize objects in the image on Google Cloud Storage

    Args:
    uri: The path to the file in Google Cloud Storage (gs://...)
    """
    from google.cloud import vision_v1p3beta1 as vision
    client = vision.ImageAnnotatorClient()

    image = vision.types.Image()
    image.source.image_uri = uri

    objects = client.object_localization(
        image=image).localized_object_annotations

    print('Number of objects found: {}'.format(len(objects)))
    for object_ in objects:
        print('\n{} (confidence: {})'.format(object_.name, object_.score))
        print('Normalized bounding polygon vertices: ')
        for vertex in object_.bounding_poly.normalized_vertices:
            print(' - ({}, {})'.format(vertex.x, vertex.y))
# [END vision_localize_objects_gcs_beta]

# [START vision_handwritten_ocr_beta]
def detect_handwritten_ocr(path):
    """Detects handwritten characters in a local image.

    Args:
    path: The path to the local file.
    """
    from google.cloud import vision_v1p3beta1 as vision
    client = vision.ImageAnnotatorClient()

    with io.open(path, 'rb') as image_file:
        content = image_file.read()

    image = vision.types.Image(content=content)

    # Language hint codes for handwritten OCR:
    # en-t-i0-handwrit, mul-Latn-t-i0-handwrit
    # Note: Use only one language hint code per request for handwritten OCR.
    image_context = vision.types.ImageContext(
        language_hints=['en-t-i0-handwrit'])

    response = client.document_text_detection(image=image,
                                              image_context=image_context)

    print('Full Text: {}'.format(response.full_text_annotation.text))
    for page in response.full_text_annotation.pages:
        for block in page.blocks:
            print('\nBlock confidence: {}\n'.format(block.confidence))

            for paragraph in block.paragraphs:
                print('Paragraph confidence: {}'.format(
                    paragraph.confidence))

                for word in paragraph.words:
                    word_text = ''.join([
                        symbol.text for symbol in word.symbols
                    ])
                    print('Word text: {} (confidence: {})'.format(
                        word_text, word.confidence))

                    for symbol in word.symbols:
                        print('\tSymbol: {} (confidence: {})'.format(
                            symbol.text, symbol.confidence))
# [END vision_handwritten_ocr_beta]

# [START vision_handwritten_ocr_gcs_beta]
def detect_handwritten_ocr_uri(uri):
    """Detects handwritten characters in the file located in Google Cloud
    Storage.

    Args:
    uri: The path to the file in Google Cloud Storage (gs://...)
    """
    from google.cloud import vision_v1p3beta1 as vision
    client = vision.ImageAnnotatorClient()
    image = vision.types.Image()
    image.source.image_uri = uri

    # Language hint codes for handwritten OCR:
    # en-t-i0-handwrit, mul-Latn-t-i0-handwrit
    # Note: Use only one language hint code per request for handwritten OCR.
    image_context = vision.types.ImageContext(
        language_hints=['en-t-i0-handwrit'])

    response = client.document_text_detection(image=image,
                                              image_context=image_context)

    print('Full Text: {}'.format(response.full_text_annotation.text))
    for page in response.full_text_annotation.pages:
        for block in page.blocks:
            print('\nBlock confidence: {}\n'.format(block.confidence))

            for paragraph in block.paragraphs:
                print('Paragraph confidence: {}'.format(
                    paragraph.confidence))

                for word in paragraph.words:
                    word_text = ''.join([
                        symbol.text for symbol in word.symbols
                    ])
                    print('Word text: {} (confidence: {})'.format(
                        word_text, word.confidence))

                    for symbol in word.symbols:
                        print('\tSymbol: {} (confidence: {})'.format(
                            symbol.text, symbol.confidence))
# [END vision_handwritten_ocr_gcs_beta]

if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter)
    subparsers = parser.add_subparsers(dest='command')

    object_parser = subparsers.add_parser(
        'object-localization', help=localize_objects.__doc__)
    object_parser.add_argument('path')

    object_uri_parser = subparsers.add_parser(
        'object-localization-uri', help=localize_objects_uri.__doc__)
    object_uri_parser.add_argument('uri')

    handwritten_parser = subparsers.add_parser(
        'handwritten-ocr', help=detect_handwritten_ocr.__doc__)
    handwritten_parser.add_argument('path')

    handwritten_uri_parser = subparsers.add_parser(
        'handwritten-ocr-uri', help=detect_handwritten_ocr_uri.__doc__)
    handwritten_uri_parser.add_argument('uri')

    args = parser.parse_args()

    if 'uri' in args.command:
        if 'object-localization-uri' in args.command:
            localize_objects_uri(args.uri)
        elif 'handwritten-ocr-uri' in args.command:
            detect_handwritten_ocr_uri(args.uri)
    else:
        if 'object-localization' in args.command:
            localize_objects(args.path)
        elif 'handwritten-ocr' in args.command:
            detect_handwritten_ocr(args.path)
<|endoftext|>


<commit_before>from LibraryItem import LibraryItem
from LibraryFrame import LibraryFrame
from wx.py import dispatcher

class Library(object):

    def __init__(self):
        self._library = {}
        self._frame = LibraryFrame()

    def add(self, item):
        if not issubclass(item.__class__, LibraryItem):
            raise ValueError, 'Library items must be instances of a subclass of LibraryItem'

        if item.__class__.__name__ in self._library:
            # This class of item has been added before.
            dict = self._library[item.__class__.__name__]
        else:
            # Create and retain a new dictionary for this class of item.
            dict = {}
            self._library[item.__class__.__name__] = dict
            # Add a method to ourself that returns the full list of items of this class.
            setattr(self, item.__class__.listProperty(), lambda: sorted(dict.values(), cmp=lambda x,y: cmp(x.name.lower(), y.name.lower())))
            # Add a method to ourself that performs a lookup of items of this class.
            setattr(self, item.__class__.lookupProperty(), lambda itemId: dict[itemId] if itemId in dict else None)
            self._frame.addItemClass(item.__class__)

        dict[item.identifier] = item

        dispatcher.send(('addition', item.__class__), self)

    def browse(self):
        self._frame.Show()
        self._frame.Raise()

<commit_msg>Internationalized text.<commit_after>from LibraryItem import LibraryItem
from LibraryFrame import LibraryFrame
from wx.py import dispatcher

class Library(object):

    def __init__(self):
        self._library = {}
        self._frame = LibraryFrame()

    def add(self, item):
        if not issubclass(item.__class__, LibraryItem):
            raise ValueError, gettext('Library items must be instances of a subclass of LibraryItem')

        if item.__class__.__name__ in self._library:
            # This class of item has been added before.
            dict = self._library[item.__class__.__name__]
        else:
            # Create and retain a new dictionary for this class of item.
            dict = {}
            self._library[item.__class__.__name__] = dict
            # Add a method to ourself that returns the full list of items of this class.
            setattr(self, item.__class__.listProperty(), lambda: sorted(dict.values(), cmp=lambda x,y: cmp(x.name.lower(), y.name.lower())))
            # Add a method to ourself that performs a lookup of items of this class.
            setattr(self, item.__class__.lookupProperty(), lambda itemId: dict[itemId] if itemId in dict else None)
            self._frame.addItemClass(item.__class__)

        dict[item.identifier] = item

        dispatcher.send(('addition', item.__class__), self)

    def browse(self):
        self._frame.Show()
        self._frame.Raise()

<|endoftext|>