<commit_before># encoding: utf-8
"""
A mixin for :class:`~IPython.core.application.Application` classes that
launch InteractiveShell instances, load extensions, etc.

Authors
-------

* Min Ragan-Kelley
"""

#-----------------------------------------------------------------------------
#  Copyright (C) 2008-2011  The IPython Development Team
#
#  Distributed under the terms of the BSD License.  The full license is in
#  the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------

#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------

from __future__ import absolute_import

import glob
import os
import sys

from IPython.config.application import boolean_flag
from IPython.config.configurable import Configurable
from IPython.config.loader import Config
from IPython.core import pylabtools
from IPython.utils import py3compat
from IPython.utils.contexts import preserve_keys
from IPython.utils.path import filefind
from IPython.utils.traitlets import (
    Unicode, Instance, List, Bool, CaselessStrEnum
)

#-----------------------------------------------------------------------------
# Aliases and Flags
#-----------------------------------------------------------------------------

shell_flags = {}

addflag = lambda *args: shell_flags.update(boolean_flag(*args))
addflag('autoindent', 'InteractiveShell.autoindent',
        'Turn on autoindenting.', 'Turn off autoindenting.'
)
addflag('automagic', 'InteractiveShell.automagic',
        """Turn on the auto calling of magic commands. Type %%magic at the
        IPython  prompt  for  more information.""",
        'Turn off the auto calling of magic commands.'
)
addflag('pdb', 'InteractiveShell.pdb',
    "Enable auto calling the pdb debugger after every exception.",
    "Disable auto calling the pdb debugger after every exception."
)
# pydb flag doesn't do any config, as core.debugger switches on import,
# which is before parsing.  This just allows the flag to be passed.
shell_flags.update(dict(
    pydb = ({},
        """Use the third party 'pydb' package as debugger, instead of pdb.
        Requires that pydb is installed."""
    )
))
addflag('pprint', 'PlainTextFormatter.pprint',
    "Enable auto pretty printing of results.",
    "Disable auto auto pretty printing of results."
)
addflag('color-info', 'InteractiveShell.color_info',
    """IPython can display information about objects via a set of func-
    tions, and optionally can use colors for this, syntax highlighting
    source code and various other elements.  However, because this
    information is passed through a pager (like 'less') and many pagers get
    confused with color codes, this option is off by default.  You can test
    it and turn it on permanently in your ipython_config.py file if it
    works for you.  Test it and turn it on permanently if it works with
    your system.  The magic function %%color_info allows you to toggle this
    interactively for testing.""",
    "Disable using colors for info related things."
)
addflag('deep-reload', 'InteractiveShell.deep_reload',
    """Enable deep (recursive) reloading by default. IPython can use the
    deep_reload module which reloads changes in modules recursively (it
    replaces the reload() function, so you don't need to change anything to
    use it). deep_reload() forces a full reload of modules whose code may
    have changed, which the default reload() function does not.  When
    deep_reload is off, IPython will use the normal reload(), but
    deep_reload will still be available as dreload(). This feature is off
    by default [which means that you have both normal reload() and
    dreload()].""",
    "Disable deep (recursive) reloading by default."
)
nosep_config = Config()
nosep_config.InteractiveShell.separate_in = ''
nosep_config.InteractiveShell.separate_out = ''
nosep_config.InteractiveShell.separate_out2 = ''

shell_flags['nosep']=(nosep_config, "Eliminate all spacing between prompts.")
shell_flags['pylab'] = (
    {'InteractiveShellApp' : {'pylab' : 'auto'}},
    """Pre-load matplotlib and numpy for interactive use with
    the default matplotlib backend."""
)

# it's possible we don't want short aliases for *all* of these:
shell_aliases = dict(
    autocall='InteractiveShell.autocall',
    colors='InteractiveShell.colors',
    logfile='InteractiveShell.logfile',
    logappend='InteractiveShell.logappend',
    c='InteractiveShellApp.code_to_run',
    m='InteractiveShellApp.module_to_run',
    ext='InteractiveShellApp.extra_extension',
    gui='InteractiveShellApp.gui',
    pylab='InteractiveShellApp.pylab',
)
shell_aliases['cache-size'] = 'InteractiveShell.cache_size'

#-----------------------------------------------------------------------------
# Main classes and functions
#-----------------------------------------------------------------------------

class InteractiveShellApp(Configurable):
    """A Mixin for applications that start InteractiveShell instances.

    Provides configurables for loading extensions and executing files
    as part of configuring a Shell environment.

    The following methods should be called by the :meth:`initialize` method
    of the subclass:

      - :meth:`init_path`
      - :meth:`init_shell` (to be implemented by the subclass)
      - :meth:`init_gui_pylab`
      - :meth:`init_extensions`
      - :meth:`init_code`
    """
    extensions = List(Unicode, config=True,
        help="A list of dotted module names of IPython extensions to load."
    )
    extra_extension = Unicode('', config=True,
        help="dotted module name of an IPython extension to load."
    )
    def _extra_extension_changed(self, name, old, new):
        if new:
            # add to self.extensions
            self.extensions.append(new)

    # Extensions that are always loaded (not configurable)
    default_extensions = List(Unicode, [u'storemagic'], config=False)

    exec_files = List(Unicode, config=True,
        help="""List of files to run at IPython startup."""
    )
    file_to_run = Unicode('', config=True,
        help="""A file to be run""")

    exec_lines = List(Unicode, config=True,
        help="""lines of code to run at IPython startup."""
    )
    code_to_run = Unicode('', config=True,
        help="Execute the given command string."
    )
    module_to_run = Unicode('', config=True,
        help="Run the module as a script."
    )
    gui = CaselessStrEnum(('qt', 'wx', 'gtk', 'glut', 'pyglet', 'osx'), config=True,
        help="Enable GUI event loop integration ('qt', 'wx', 'gtk', 'glut', 'pyglet', 'osx')."
    )
    pylab = CaselessStrEnum(['tk', 'qt', 'wx', 'gtk', 'osx', 'inline', 'auto'],
        config=True,
        help="""Pre-load matplotlib and numpy for interactive use,
        selecting a particular matplotlib backend and loop integration.
        """
    )
    pylab_import_all = Bool(True, config=True,
        help="""If true, an 'import *' is done from numpy and pylab,
        when using pylab"""
    )
    shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')

    def init_path(self):
        """Add current working directory, '', to sys.path"""
        if sys.path[0] != '':
            sys.path.insert(0, '')

    def init_shell(self):
        raise NotImplementedError("Override in subclasses")

    def init_gui_pylab(self):
        """Enable GUI event loop integration, taking pylab into account."""
        if self.gui or self.pylab:
            shell = self.shell
            try:
                if self.pylab:
                    gui, backend = pylabtools.find_gui_and_backend(self.pylab)
                    self.log.info("Enabling GUI event loop integration, "
                              "toolkit=%s, pylab=%s" % (gui, self.pylab))
                    shell.enable_pylab(gui, import_all=self.pylab_import_all, welcome_message=True)
                else:
                    self.log.info("Enabling GUI event loop integration, "
                                  "toolkit=%s" % self.gui)
                    shell.enable_gui(self.gui)
            except ImportError:
                self.log.warn("pylab mode doesn't work as matplotlib could not be found." + \
                              "\nIs it installed on the system?")
                self.shell.showtraceback()
            except Exception:
                self.log.warn("GUI event loop or pylab initialization failed")
                self.shell.showtraceback()

    def init_extensions(self):
        """Load all IPython extensions in IPythonApp.extensions.

        This uses the :meth:`ExtensionManager.load_extensions` to load all
        the extensions listed in ``self.extensions``.
        """
        try:
            self.log.debug("Loading IPython extensions...")
            extensions = self.default_extensions + self.extensions
            for ext in extensions:
                try:
                    self.log.info("Loading IPython extension: %s" % ext)
                    self.shell.extension_manager.load_extension(ext)
                except:
                    self.log.warn("Error in loading extension: %s" % ext +
                        "\nCheck your config files in %s" % self.profile_dir.location
                    )
                    self.shell.showtraceback()
        except:
            self.log.warn("Unknown error in loading extensions:")
            self.shell.showtraceback()

    def init_code(self):
        """run the pre-flight code, specified via exec_lines"""
        self._run_startup_files()
        self._run_exec_lines()
        self._run_exec_files()
        self._run_cmd_line_code()
        self._run_module()

        # flush output, so itwon't be attached to the first cell
        sys.stdout.flush()
        sys.stderr.flush()

        # Hide variables defined here from %who etc.
        self.shell.user_ns_hidden.update(self.shell.user_ns)

    def _run_exec_lines(self):
        """Run lines of code in IPythonApp.exec_lines in the user's namespace."""
        if not self.exec_lines:
            return
        try:
            self.log.debug("Running code from IPythonApp.exec_lines...")
            for line in self.exec_lines:
                try:
                    self.log.info("Running code in user namespace: %s" %
                                  line)
                    self.shell.run_cell(line, store_history=False)
                except:
                    self.log.warn("Error in executing line in user "
                                  "namespace: %s" % line)
                    self.shell.showtraceback()
        except:
            self.log.warn("Unknown error in handling IPythonApp.exec_lines:")
            self.shell.showtraceback()

    def _exec_file(self, fname):
        try:
            full_filename = filefind(fname, [u'.', self.ipython_dir])
        except IOError as e:
            self.log.warn("File not found: %r"%fname)
            return
        # Make sure that the running script gets a proper sys.argv as if it
        # were run from a system shell.
        save_argv = sys.argv
        sys.argv = [full_filename] + self.extra_args[1:]
        # protect sys.argv from potential unicode strings on Python 2:
        if not py3compat.PY3:
            sys.argv = [ py3compat.cast_bytes(a) for a in sys.argv ]
        try:
            if os.path.isfile(full_filename):
                self.log.info("Running file in user namespace: %s" %
                              full_filename)
                # Ensure that __file__ is always defined to match Python
                # behavior.
                with preserve_keys(self.shell.user_ns, '__file__'):
                    self.shell.user_ns['__file__'] = fname
                    if full_filename.endswith('.ipy'):
                        self.shell.safe_execfile_ipy(full_filename)
                    else:
                        # default to python, even without extension
                        self.shell.safe_execfile(full_filename,
                                                 self.shell.user_ns)
        finally:
            sys.argv = save_argv

    def _run_startup_files(self):
        """Run files from profile startup directory"""
        startup_dir = self.profile_dir.startup_dir
        startup_files = []
        if os.environ.get('PYTHONSTARTUP', False):
            startup_files.append(os.environ['PYTHONSTARTUP'])
        startup_files += glob.glob(os.path.join(startup_dir, '*.py'))
        startup_files += glob.glob(os.path.join(startup_dir, '*.ipy'))
        if not startup_files:
            return

        self.log.debug("Running startup files from %s...", startup_dir)
        try:
            for fname in sorted(startup_files):
                self._exec_file(fname)
        except:
            self.log.warn("Unknown error in handling startup files:")
            self.shell.showtraceback()

    def _run_exec_files(self):
        """Run files from IPythonApp.exec_files"""
        if not self.exec_files:
            return

        self.log.debug("Running files in IPythonApp.exec_files...")
        try:
            for fname in self.exec_files:
                self._exec_file(fname)
        except:
            self.log.warn("Unknown error in handling IPythonApp.exec_files:")
            self.shell.showtraceback()

    def _run_cmd_line_code(self):
        """Run code or file specified at the command-line"""
        if self.code_to_run:
            line = self.code_to_run
            try:
                self.log.info("Running code given at command line (c=): %s" %
                              line)
                self.shell.run_cell(line, store_history=False)
            except:
                self.log.warn("Error in executing line in user namespace: %s" %
                              line)
                self.shell.showtraceback()

        # Like Python itself, ignore the second if the first of these is present
        elif self.file_to_run:
            fname = self.file_to_run
            try:
                self._exec_file(fname)
            except:
                self.log.warn("Error in executing file in user namespace: %s" %
                              fname)
                self.shell.showtraceback()

    def _run_module(self):
        """Run module specified at the command-line."""
        if self.module_to_run:
            # Make sure that the module gets a proper sys.argv as if it were
            # run using `python -m`.
            save_argv = sys.argv
            sys.argv = [sys.executable] + self.extra_args
            try:
                self.shell.safe_run_module(self.module_to_run,
                                           self.shell.user_ns)
            finally:
                sys.argv = save_argv
<commit_msg>update pylab_import_all help string<commit_after># encoding: utf-8
"""
A mixin for :class:`~IPython.core.application.Application` classes that
launch InteractiveShell instances, load extensions, etc.

Authors
-------

* Min Ragan-Kelley
"""

#-----------------------------------------------------------------------------
#  Copyright (C) 2008-2011  The IPython Development Team
#
#  Distributed under the terms of the BSD License.  The full license is in
#  the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------

#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------

from __future__ import absolute_import

import glob
import os
import sys

from IPython.config.application import boolean_flag
from IPython.config.configurable import Configurable
from IPython.config.loader import Config
from IPython.core import pylabtools
from IPython.utils import py3compat
from IPython.utils.contexts import preserve_keys
from IPython.utils.path import filefind
from IPython.utils.traitlets import (
    Unicode, Instance, List, Bool, CaselessStrEnum
)

#-----------------------------------------------------------------------------
# Aliases and Flags
#-----------------------------------------------------------------------------

shell_flags = {}

addflag = lambda *args: shell_flags.update(boolean_flag(*args))
addflag('autoindent', 'InteractiveShell.autoindent',
        'Turn on autoindenting.', 'Turn off autoindenting.'
)
addflag('automagic', 'InteractiveShell.automagic',
        """Turn on the auto calling of magic commands. Type %%magic at the
        IPython  prompt  for  more information.""",
        'Turn off the auto calling of magic commands.'
)
addflag('pdb', 'InteractiveShell.pdb',
    "Enable auto calling the pdb debugger after every exception.",
    "Disable auto calling the pdb debugger after every exception."
)
# pydb flag doesn't do any config, as core.debugger switches on import,
# which is before parsing.  This just allows the flag to be passed.
shell_flags.update(dict(
    pydb = ({},
        """Use the third party 'pydb' package as debugger, instead of pdb.
        Requires that pydb is installed."""
    )
))
addflag('pprint', 'PlainTextFormatter.pprint',
    "Enable auto pretty printing of results.",
    "Disable auto auto pretty printing of results."
)
addflag('color-info', 'InteractiveShell.color_info',
    """IPython can display information about objects via a set of func-
    tions, and optionally can use colors for this, syntax highlighting
    source code and various other elements.  However, because this
    information is passed through a pager (like 'less') and many pagers get
    confused with color codes, this option is off by default.  You can test
    it and turn it on permanently in your ipython_config.py file if it
    works for you.  Test it and turn it on permanently if it works with
    your system.  The magic function %%color_info allows you to toggle this
    interactively for testing.""",
    "Disable using colors for info related things."
)
addflag('deep-reload', 'InteractiveShell.deep_reload',
    """Enable deep (recursive) reloading by default. IPython can use the
    deep_reload module which reloads changes in modules recursively (it
    replaces the reload() function, so you don't need to change anything to
    use it). deep_reload() forces a full reload of modules whose code may
    have changed, which the default reload() function does not.  When
    deep_reload is off, IPython will use the normal reload(), but
    deep_reload will still be available as dreload(). This feature is off
    by default [which means that you have both normal reload() and
    dreload()].""",
    "Disable deep (recursive) reloading by default."
)
nosep_config = Config()
nosep_config.InteractiveShell.separate_in = ''
nosep_config.InteractiveShell.separate_out = ''
nosep_config.InteractiveShell.separate_out2 = ''

shell_flags['nosep']=(nosep_config, "Eliminate all spacing between prompts.")
shell_flags['pylab'] = (
    {'InteractiveShellApp' : {'pylab' : 'auto'}},
    """Pre-load matplotlib and numpy for interactive use with
    the default matplotlib backend."""
)

# it's possible we don't want short aliases for *all* of these:
shell_aliases = dict(
    autocall='InteractiveShell.autocall',
    colors='InteractiveShell.colors',
    logfile='InteractiveShell.logfile',
    logappend='InteractiveShell.logappend',
    c='InteractiveShellApp.code_to_run',
    m='InteractiveShellApp.module_to_run',
    ext='InteractiveShellApp.extra_extension',
    gui='InteractiveShellApp.gui',
    pylab='InteractiveShellApp.pylab',
)
shell_aliases['cache-size'] = 'InteractiveShell.cache_size'

#-----------------------------------------------------------------------------
# Main classes and functions
#-----------------------------------------------------------------------------

class InteractiveShellApp(Configurable):
    """A Mixin for applications that start InteractiveShell instances.

    Provides configurables for loading extensions and executing files
    as part of configuring a Shell environment.

    The following methods should be called by the :meth:`initialize` method
    of the subclass:

      - :meth:`init_path`
      - :meth:`init_shell` (to be implemented by the subclass)
      - :meth:`init_gui_pylab`
      - :meth:`init_extensions`
      - :meth:`init_code`
    """
    extensions = List(Unicode, config=True,
        help="A list of dotted module names of IPython extensions to load."
    )
    extra_extension = Unicode('', config=True,
        help="dotted module name of an IPython extension to load."
    )
    def _extra_extension_changed(self, name, old, new):
        if new:
            # add to self.extensions
            self.extensions.append(new)

    # Extensions that are always loaded (not configurable)
    default_extensions = List(Unicode, [u'storemagic'], config=False)

    exec_files = List(Unicode, config=True,
        help="""List of files to run at IPython startup."""
    )
    file_to_run = Unicode('', config=True,
        help="""A file to be run""")

    exec_lines = List(Unicode, config=True,
        help="""lines of code to run at IPython startup."""
    )
    code_to_run = Unicode('', config=True,
        help="Execute the given command string."
    )
    module_to_run = Unicode('', config=True,
        help="Run the module as a script."
    )
    gui = CaselessStrEnum(('qt', 'wx', 'gtk', 'glut', 'pyglet', 'osx'), config=True,
        help="Enable GUI event loop integration ('qt', 'wx', 'gtk', 'glut', 'pyglet', 'osx')."
    )
    pylab = CaselessStrEnum(['tk', 'qt', 'wx', 'gtk', 'osx', 'inline', 'auto'],
        config=True,
        help="""Pre-load matplotlib and numpy for interactive use,
        selecting a particular matplotlib backend and loop integration.
        """
    )
    pylab_import_all = Bool(True, config=True,
        help="""If true, IPython will populate the user namespace with numpy, pylab, etc.
        and an 'import *' is done from numpy and pylab, when using pylab mode.

        When False, pylab mode should not import any names into the user namespace.
        """
    )
    shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')

    def init_path(self):
        """Add current working directory, '', to sys.path"""
        if sys.path[0] != '':
            sys.path.insert(0, '')

    def init_shell(self):
        raise NotImplementedError("Override in subclasses")

    def init_gui_pylab(self):
        """Enable GUI event loop integration, taking pylab into account."""
        if self.gui or self.pylab:
            shell = self.shell
            try:
                if self.pylab:
                    gui, backend = pylabtools.find_gui_and_backend(self.pylab)
                    self.log.info("Enabling GUI event loop integration, "
                              "toolkit=%s, pylab=%s" % (gui, self.pylab))
                    shell.enable_pylab(gui, import_all=self.pylab_import_all, welcome_message=True)
                else:
                    self.log.info("Enabling GUI event loop integration, "
                                  "toolkit=%s" % self.gui)
                    shell.enable_gui(self.gui)
            except ImportError:
                self.log.warn("pylab mode doesn't work as matplotlib could not be found." + \
                              "\nIs it installed on the system?")
                self.shell.showtraceback()
            except Exception:
                self.log.warn("GUI event loop or pylab initialization failed")
                self.shell.showtraceback()

    def init_extensions(self):
        """Load all IPython extensions in IPythonApp.extensions.

        This uses the :meth:`ExtensionManager.load_extensions` to load all
        the extensions listed in ``self.extensions``.
        """
        try:
            self.log.debug("Loading IPython extensions...")
            extensions = self.default_extensions + self.extensions
            for ext in extensions:
                try:
                    self.log.info("Loading IPython extension: %s" % ext)
                    self.shell.extension_manager.load_extension(ext)
                except:
                    self.log.warn("Error in loading extension: %s" % ext +
                        "\nCheck your config files in %s" % self.profile_dir.location
                    )
                    self.shell.showtraceback()
        except:
            self.log.warn("Unknown error in loading extensions:")
            self.shell.showtraceback()

    def init_code(self):
        """run the pre-flight code, specified via exec_lines"""
        self._run_startup_files()
        self._run_exec_lines()
        self._run_exec_files()
        self._run_cmd_line_code()
        self._run_module()

        # flush output, so itwon't be attached to the first cell
        sys.stdout.flush()
        sys.stderr.flush()

        # Hide variables defined here from %who etc.
        self.shell.user_ns_hidden.update(self.shell.user_ns)

    def _run_exec_lines(self):
        """Run lines of code in IPythonApp.exec_lines in the user's namespace."""
        if not self.exec_lines:
            return
        try:
            self.log.debug("Running code from IPythonApp.exec_lines...")
            for line in self.exec_lines:
                try:
                    self.log.info("Running code in user namespace: %s" %
                                  line)
                    self.shell.run_cell(line, store_history=False)
                except:
                    self.log.warn("Error in executing line in user "
                                  "namespace: %s" % line)
                    self.shell.showtraceback()
        except:
            self.log.warn("Unknown error in handling IPythonApp.exec_lines:")
            self.shell.showtraceback()

    def _exec_file(self, fname):
        try:
            full_filename = filefind(fname, [u'.', self.ipython_dir])
        except IOError as e:
            self.log.warn("File not found: %r"%fname)
            return
        # Make sure that the running script gets a proper sys.argv as if it
        # were run from a system shell.
        save_argv = sys.argv
        sys.argv = [full_filename] + self.extra_args[1:]
        # protect sys.argv from potential unicode strings on Python 2:
        if not py3compat.PY3:
            sys.argv = [ py3compat.cast_bytes(a) for a in sys.argv ]
        try:
            if os.path.isfile(full_filename):
                self.log.info("Running file in user namespace: %s" %
                              full_filename)
                # Ensure that __file__ is always defined to match Python
                # behavior.
                with preserve_keys(self.shell.user_ns, '__file__'):
                    self.shell.user_ns['__file__'] = fname
                    if full_filename.endswith('.ipy'):
                        self.shell.safe_execfile_ipy(full_filename)
                    else:
                        # default to python, even without extension
                        self.shell.safe_execfile(full_filename,
                                                 self.shell.user_ns)
        finally:
            sys.argv = save_argv

    def _run_startup_files(self):
        """Run files from profile startup directory"""
        startup_dir = self.profile_dir.startup_dir
        startup_files = []
        if os.environ.get('PYTHONSTARTUP', False):
            startup_files.append(os.environ['PYTHONSTARTUP'])
        startup_files += glob.glob(os.path.join(startup_dir, '*.py'))
        startup_files += glob.glob(os.path.join(startup_dir, '*.ipy'))
        if not startup_files:
            return

        self.log.debug("Running startup files from %s...", startup_dir)
        try:
            for fname in sorted(startup_files):
                self._exec_file(fname)
        except:
            self.log.warn("Unknown error in handling startup files:")
            self.shell.showtraceback()

    def _run_exec_files(self):
        """Run files from IPythonApp.exec_files"""
        if not self.exec_files:
            return

        self.log.debug("Running files in IPythonApp.exec_files...")
        try:
            for fname in self.exec_files:
                self._exec_file(fname)
        except:
            self.log.warn("Unknown error in handling IPythonApp.exec_files:")
            self.shell.showtraceback()

    def _run_cmd_line_code(self):
        """Run code or file specified at the command-line"""
        if self.code_to_run:
            line = self.code_to_run
            try:
                self.log.info("Running code given at command line (c=): %s" %
                              line)
                self.shell.run_cell(line, store_history=False)
            except:
                self.log.warn("Error in executing line in user namespace: %s" %
                              line)
                self.shell.showtraceback()

        # Like Python itself, ignore the second if the first of these is present
        elif self.file_to_run:
            fname = self.file_to_run
            try:
                self._exec_file(fname)
            except:
                self.log.warn("Error in executing file in user namespace: %s" %
                              fname)
                self.shell.showtraceback()

    def _run_module(self):
        """Run module specified at the command-line."""
        if self.module_to_run:
            # Make sure that the module gets a proper sys.argv as if it were
            # run using `python -m`.
            save_argv = sys.argv
            sys.argv = [sys.executable] + self.extra_args
            try:
                self.shell.safe_run_module(self.module_to_run,
                                           self.shell.user_ns)
            finally:
                sys.argv = save_argv
<|endoftext|>


<commit_before>"""Base class to manage the interaction with a running kernel
"""

#-----------------------------------------------------------------------------
#  Copyright (C) 2013  The IPython Development Team
#
#  Distributed under the terms of the BSD License.  The full license is in
#  the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------

#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------

from __future__ import absolute_import

import zmq

# Local imports
from IPython.config.configurable import LoggingConfigurable
from IPython.utils.traitlets import (
    Any, Instance, Type,
)

from .zmq.session import Session
from .channels import (
    ShellChannel, IOPubChannel,
    HBChannel, StdInChannel,
)
from .clientabc import KernelClientABC
from .connect import ConnectionFileMixin

#-----------------------------------------------------------------------------
# Main kernel client class
#-----------------------------------------------------------------------------

class KernelClient(LoggingConfigurable, ConnectionFileMixin):
    """Communicates with a single kernel on any host via zmq channels.

    There are four channels associated with each kernel:

    * shell: for request/reply calls to the kernel.
    * iopub: for the kernel to publish results to frontends.
    * hb: for monitoring the kernel's heartbeat.
    * stdin: for frontends to reply to raw_input calls in the kernel.

    """

    # The PyZMQ Context to use for communication with the kernel.
    context = Instance(zmq.Context)
    def _context_default(self):
        return zmq.Context.instance()

    # The Session to use for communication with the kernel.
    session = Instance(Session)
    def _session_default(self):
        return Session(config=self.config)

    # The classes to use for the various channels
    shell_channel_class = Type(ShellChannel)
    iopub_channel_class = Type(IOPubChannel)
    stdin_channel_class = Type(StdInChannel)
    hb_channel_class = Type(HBChannel)

    # Protected traits
    _shell_channel = Any
    _iopub_channel = Any
    _stdin_channel = Any
    _hb_channel = Any

    # def __init__(self, *args, **kwargs):
    #     super(KernelClient, self).__init__(*args, **kwargs)
    #     # setup channel proxy methods, e.g.
    #     # Client.execute => shell_channel.execute
    #     for channel in ['shell', 'iopub', 'stdin', 'hb']:
    #         cls = getattr(self, '%s_channel_class' % channel)
    #         for method in cls.proxy_methods:
    #             setattr(self, method, self._proxy_method(channel, method))
    #
    #--------------------------------------------------------------------------
    # Channel proxy methods
    #--------------------------------------------------------------------------

    def _get_msg(channel, *args, **kwargs):
        return channel.get_msg(*args, **kwargs)

    def get_shell_msg(self, *args, **kwargs):
        """Get a message from the shell channel"""
        return self.shell_channel.get_msg(*args, **kwargs)

    def get_iopub_msg(self, *args, **kwargs):
        """Get a message from the iopub channel"""
        return self.iopub_channel.get_msg(*args, **kwargs)

    def get_stdin_msg(self, *args, **kwargs):
        """Get a message from the stdin channel"""
        return self.stdin_channel.get_msg(*args, **kwargs)

    #--------------------------------------------------------------------------
    # Channel management methods
    #--------------------------------------------------------------------------

    def start_channels(self, shell=True, iopub=True, stdin=True, hb=True):
        """Starts the channels for this kernel.

        This will create the channels if they do not exist and then start
        them (their activity runs in a thread). If port numbers of 0 are
        being used (random ports) then you must first call
        :method:`start_kernel`. If the channels have been stopped and you
        call this, :class:`RuntimeError` will be raised.
        """
        if shell:
            self.shell_channel.start()
            for method in self.shell_channel.proxy_methods:
                setattr(self, method, getattr(self.shell_channel, method))
        if iopub:
            self.iopub_channel.start()
            for method in self.iopub_channel.proxy_methods:
                setattr(self, method, getattr(self.iopub_channel, method))
        if stdin:
            self.stdin_channel.start()
            for method in self.stdin_channel.proxy_methods:
                setattr(self, method, getattr(self.stdin_channel, method))
            self.shell_channel.allow_stdin = True
        else:
            self.shell_channel.allow_stdin = False
        if hb:
            self.hb_channel.start()

    def stop_channels(self):
        """Stops all the running channels for this kernel.

        This stops their event loops and joins their threads.
        """
        if self.shell_channel.is_alive():
            self.shell_channel.stop()
        if self.iopub_channel.is_alive():
            self.iopub_channel.stop()
        if self.stdin_channel.is_alive():
            self.stdin_channel.stop()
        if self.hb_channel.is_alive():
            self.hb_channel.stop()

    @property
    def channels_running(self):
        """Are any of the channels created and running?"""
        return (self.shell_channel.is_alive() or self.iopub_channel.is_alive() or
                self.stdin_channel.is_alive() or self.hb_channel.is_alive())

    @property
    def shell_channel(self):
        """Get the shell channel object for this kernel."""
        if self._shell_channel is None:
            self._shell_channel = self.shell_channel_class(
                self.context, self.session, self._make_url('shell')
            )
        return self._shell_channel

    @property
    def iopub_channel(self):
        """Get the iopub channel object for this kernel."""
        if self._iopub_channel is None:
            self._iopub_channel = self.iopub_channel_class(
                self.context, self.session, self._make_url('iopub')
            )
        return self._iopub_channel

    @property
    def stdin_channel(self):
        """Get the stdin channel object for this kernel."""
        if self._stdin_channel is None:
            self._stdin_channel = self.stdin_channel_class(
                self.context, self.session, self._make_url('stdin')
            )
        return self._stdin_channel

    @property
    def hb_channel(self):
        """Get the hb channel object for this kernel."""
        if self._hb_channel is None:
            self._hb_channel = self.hb_channel_class(
                self.context, self.session, self._make_url('hb')
            )
        return self._hb_channel

    def is_alive(self):
        """Is the kernel process still running?"""
        if self._hb_channel is not None:
            # We didn't start the kernel with this KernelManager so we
            # use the heartbeat.
            return self._hb_channel.is_beating()
        else:
            # no heartbeat and not local, we can't tell if it's running,
            # so naively return True
            return True

#-----------------------------------------------------------------------------
# ABC Registration
#-----------------------------------------------------------------------------

KernelClientABC.register(KernelClient)
<commit_msg>remove commented-out Client.init<commit_after>"""Base class to manage the interaction with a running kernel
"""

#-----------------------------------------------------------------------------
#  Copyright (C) 2013  The IPython Development Team
#
#  Distributed under the terms of the BSD License.  The full license is in
#  the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------

#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------

from __future__ import absolute_import

import zmq

# Local imports
from IPython.config.configurable import LoggingConfigurable
from IPython.utils.traitlets import (
    Any, Instance, Type,
)

from .zmq.session import Session
from .channels import (
    ShellChannel, IOPubChannel,
    HBChannel, StdInChannel,
)
from .clientabc import KernelClientABC
from .connect import ConnectionFileMixin

#-----------------------------------------------------------------------------
# Main kernel client class
#-----------------------------------------------------------------------------

class KernelClient(LoggingConfigurable, ConnectionFileMixin):
    """Communicates with a single kernel on any host via zmq channels.

    There are four channels associated with each kernel:

    * shell: for request/reply calls to the kernel.
    * iopub: for the kernel to publish results to frontends.
    * hb: for monitoring the kernel's heartbeat.
    * stdin: for frontends to reply to raw_input calls in the kernel.

    """

    # The PyZMQ Context to use for communication with the kernel.
    context = Instance(zmq.Context)
    def _context_default(self):
        return zmq.Context.instance()

    # The Session to use for communication with the kernel.
    session = Instance(Session)
    def _session_default(self):
        return Session(config=self.config)

    # The classes to use for the various channels
    shell_channel_class = Type(ShellChannel)
    iopub_channel_class = Type(IOPubChannel)
    stdin_channel_class = Type(StdInChannel)
    hb_channel_class = Type(HBChannel)

    # Protected traits
    _shell_channel = Any
    _iopub_channel = Any
    _stdin_channel = Any
    _hb_channel = Any

    #--------------------------------------------------------------------------
    # Channel proxy methods
    #--------------------------------------------------------------------------

    def _get_msg(channel, *args, **kwargs):
        return channel.get_msg(*args, **kwargs)

    def get_shell_msg(self, *args, **kwargs):
        """Get a message from the shell channel"""
        return self.shell_channel.get_msg(*args, **kwargs)

    def get_iopub_msg(self, *args, **kwargs):
        """Get a message from the iopub channel"""
        return self.iopub_channel.get_msg(*args, **kwargs)

    def get_stdin_msg(self, *args, **kwargs):
        """Get a message from the stdin channel"""
        return self.stdin_channel.get_msg(*args, **kwargs)

    #--------------------------------------------------------------------------
    # Channel management methods
    #--------------------------------------------------------------------------

    def start_channels(self, shell=True, iopub=True, stdin=True, hb=True):
        """Starts the channels for this kernel.

        This will create the channels if they do not exist and then start
        them (their activity runs in a thread). If port numbers of 0 are
        being used (random ports) then you must first call
        :method:`start_kernel`. If the channels have been stopped and you
        call this, :class:`RuntimeError` will be raised.
        """
        if shell:
            self.shell_channel.start()
            for method in self.shell_channel.proxy_methods:
                setattr(self, method, getattr(self.shell_channel, method))
        if iopub:
            self.iopub_channel.start()
            for method in self.iopub_channel.proxy_methods:
                setattr(self, method, getattr(self.iopub_channel, method))
        if stdin:
            self.stdin_channel.start()
            for method in self.stdin_channel.proxy_methods:
                setattr(self, method, getattr(self.stdin_channel, method))
            self.shell_channel.allow_stdin = True
        else:
            self.shell_channel.allow_stdin = False
        if hb:
            self.hb_channel.start()

    def stop_channels(self):
        """Stops all the running channels for this kernel.

        This stops their event loops and joins their threads.
        """
        if self.shell_channel.is_alive():
            self.shell_channel.stop()
        if self.iopub_channel.is_alive():
            self.iopub_channel.stop()
        if self.stdin_channel.is_alive():
            self.stdin_channel.stop()
        if self.hb_channel.is_alive():
            self.hb_channel.stop()

    @property
    def channels_running(self):
        """Are any of the channels created and running?"""
        return (self.shell_channel.is_alive() or self.iopub_channel.is_alive() or
                self.stdin_channel.is_alive() or self.hb_channel.is_alive())

    @property
    def shell_channel(self):
        """Get the shell channel object for this kernel."""
        if self._shell_channel is None:
            self._shell_channel = self.shell_channel_class(
                self.context, self.session, self._make_url('shell')
            )
        return self._shell_channel

    @property
    def iopub_channel(self):
        """Get the iopub channel object for this kernel."""
        if self._iopub_channel is None:
            self._iopub_channel = self.iopub_channel_class(
                self.context, self.session, self._make_url('iopub')
            )
        return self._iopub_channel

    @property
    def stdin_channel(self):
        """Get the stdin channel object for this kernel."""
        if self._stdin_channel is None:
            self._stdin_channel = self.stdin_channel_class(
                self.context, self.session, self._make_url('stdin')
            )
        return self._stdin_channel

    @property
    def hb_channel(self):
        """Get the hb channel object for this kernel."""
        if self._hb_channel is None:
            self._hb_channel = self.hb_channel_class(
                self.context, self.session, self._make_url('hb')
            )
        return self._hb_channel

    def is_alive(self):
        """Is the kernel process still running?"""
        if self._hb_channel is not None:
            # We didn't start the kernel with this KernelManager so we
            # use the heartbeat.
            return self._hb_channel.is_beating()
        else:
            # no heartbeat and not local, we can't tell if it's running,
            # so naively return True
            return True

#-----------------------------------------------------------------------------
# ABC Registration
#-----------------------------------------------------------------------------

KernelClientABC.register(KernelClient)
<|endoftext|>


<commit_before>"""
Imports Camden
"""
import tempfile
import urllib.request
from fastkml import kml
from django.contrib.gis.geos import GEOSGeometry, Point
from data_collection.base_importers import BaseGenericApiImporter, CsvMixin
from data_collection.geo_utils import convert_linestring_to_multiploygon

class Command(BaseGenericApiImporter, CsvMixin):
    """
    Imports the Polling Station data from Camden Council
    """

    stations_filetype = "csv"
    districts_filetype = "kml"
    srid = 4326
    districts_srid = 4326
    council_id = "E09000007"
    districts_url = "https://raw.githubusercontent.com/wdiv-scrapers/data/master/E09000007/districts.kml"
    stations_url = "https://raw.githubusercontent.com/wdiv-scrapers/data/master/E09000007/stations.csv"
    elections = ["parl.2019-12-12"]

    def parse_kml_features(self, data):
        k = kml.KML()
        k.from_string(data.encode("utf-8"))
        document = next(k.features())
        folder = next(document.features())
        return list(folder.features())

    def parse_extended_data(self, record):
        extended_data = {}
        for pair in record.extended_data.elements:
            if not pair.value:
                extended_data[pair.name] = ""
            else:
                extended_data[pair.name] = pair.value
        return extended_data

    def get_districts(self):
        with tempfile.NamedTemporaryFile() as tmp:
            urllib.request.urlretrieve(self.districts_url, tmp.name)
            data = open(tmp.name, "r").read()
            return self.parse_kml_features(data)

    def district_record_to_dict(self, record):

        # polygon
        geometry_collection = self.clean_poly(
            GEOSGeometry(record.geometry.wkt, srid=self.get_srid("districts"))
        )
        poly = GEOSGeometry(
            convert_linestring_to_multiploygon(geometry_collection),
            srid=self.get_srid("districts"),
        )

        # meta-data
        extended_data = self.parse_extended_data(record)
        district_id = str(extended_data["polling_district_code"]).strip()

        return {
            "internal_council_id": district_id,
            "name": "Camden - %s" % (district_id),
            "area": poly,
            "polling_station_id": district_id,
        }

    def station_record_to_dict(self, record):
        address = "\n".join([record.organisation, record.street])

        location = Point(
            float(record.longitude), float(record.latitude), srid=self.get_srid()
        )

        return {
            "internal_council_id": record.polling_district_name,
            "postcode": record.postcode,
            "address": address,
            "location": location,
        }
<commit_msg>fix for Camden<commit_after>"""
Imports Camden
"""
import tempfile
import urllib.request
from fastkml import kml
from django.contrib.gis.geos import GEOSGeometry, Point
from data_collection.base_importers import BaseGenericApiImporter, CsvMixin
from data_collection.geo_utils import convert_linestring_to_multiploygon

class Command(BaseGenericApiImporter, CsvMixin):
    """
    Imports the Polling Station data from Camden Council
    """

    stations_filetype = "csv"
    districts_filetype = "kml"
    srid = 4326
    districts_srid = 4326
    council_id = "E09000007"
    districts_url = "https://raw.githubusercontent.com/wdiv-scrapers/data/master/E09000007/districts.kml"
    stations_url = "https://raw.githubusercontent.com/wdiv-scrapers/data/master/E09000007/stations.csv"
    elections = ["parl.2019-12-12"]

    def parse_kml_features(self, data):
        k = kml.KML()
        k.from_string(data.encode("utf-8"))
        document = next(k.features())
        folder = next(document.features())
        return list(folder.features())

    def parse_extended_data(self, record):
        extended_data = {}
        for pair in record.extended_data.elements:
            if not pair.value:
                extended_data[pair.name] = ""
            else:
                extended_data[pair.name] = pair.value
        return extended_data

    def get_districts(self):
        with tempfile.NamedTemporaryFile() as tmp:
            urllib.request.urlretrieve(self.districts_url, tmp.name)
            data = open(tmp.name, "r").read()
            return self.parse_kml_features(data)

    def district_record_to_dict(self, record):

        # polygon
        geometry_collection = self.clean_poly(
            GEOSGeometry(record.geometry.wkt, srid=self.get_srid("districts"))
        )
        poly = GEOSGeometry(
            convert_linestring_to_multiploygon(geometry_collection),
            srid=self.get_srid("districts"),
        )

        # meta-data
        extended_data = self.parse_extended_data(record)
        district_id = str(extended_data["polling_district_code"]).strip()

        return {
            "internal_council_id": district_id,
            "name": "Camden - %s" % (district_id),
            "area": poly,
            "polling_station_id": district_id,
        }

    def station_record_to_dict(self, record):
        address = "\n".join([record.organisation, record.street])

        location = Point(
            float(record.longitude), float(record.latitude), srid=self.get_srid()
        )

        # user error report #227
        if (
            record.polling_district_name == "PB"
            and record.organisation == "Kings Cross Neighbourhood Community Centre"
        ):
            location = None

        return {
            "internal_council_id": record.polling_district_name,
            "postcode": record.postcode,
            "address": address,
            "location": location,
        }
<|endoftext|>


<commit_before>import os, sys
from twisted.internet import reactor
from twisted.python import log
from twisted.application import service
from foolscap.api import Tub, fireEventually
from gcloud import datastore

class GetGridConfig:
    def __init__(self):
        f = open(os.path.expanduser("~/.tahoe/private/control.furl"), "r")
        self.control_furl = f.read().strip()
        f.close()
        self.base_service = service.MultiService()
        self.failed = None

    def run(self):
        d = fireEventually()
        d.addCallback(lambda res: self.setUp())
        # use RIClient.get_nodeid() to measure the foolscap-level RTT
        d.addCallback(lambda res: self.client_rref.callRemote("measure_peer_response_time"))
        d.addCallback(self.got_rtt)
        d.addBoth(self.tearDown)
        def _err(err):
            self.failed = err
            log.err(err)
            print err
        d.addErrback(_err)
        def _done(res):
            reactor.stop()
            return res
        d.addBoth(_done)
        reactor.run()
        if self.failed:
            print "EXCEPTION"
            print self.failed
            sys.exit(1)

    def setUp(self):
        self.base_service.startService()
        self.tub = Tub()
        self.tub.setOption("expose-remote-exception-types", False)
        self.tub.setServiceParent(self.base_service)
        d = self.tub.getReference(self.control_furl)
        def _gotref(rref):
            self.client_rref = rref
            print "Got Client Control reference"
        d.addCallback(_gotref)
        return d

    def got_rtt(self, rtts):
        # rtts: nodeid -> latency (in seconds)
        times = rtts.values()
        key = datastore.Key("GridConfig")
        c = datastore.Entity(key)
        c.update({
            "num_server_instances": 3,
            "server_instance_types": [u"n1-standard-1"]*3,
            "num_servers": 6,
            "server_versions": [u"1.10.0"]*6,
            "server_latencies": times,
            "avg_server_latency": sum(times) / len(times),
            "max_server_latency": max(times),
            "client_instance_type": u"n1-standard-1",
            "client_version": u"1.10.0",
            })
        datastore.put([c])
        print "configid:", c.key.id

    def tearDown(self, res):
        d = self.base_service.stopService()
        d.addCallback(lambda ignored: res)
        return d

if __name__ == '__main__':
    ggc = GetGridConfig()
    ggc.run()
<commit_msg>show avg latency<commit_after>import os, sys
from twisted.internet import reactor
from twisted.python import log
from twisted.application import service
from foolscap.api import Tub, fireEventually
from gcloud import datastore

class GetGridConfig:
    def __init__(self):
        f = open(os.path.expanduser("~/.tahoe/private/control.furl"), "r")
        self.control_furl = f.read().strip()
        f.close()
        self.base_service = service.MultiService()
        self.failed = None

    def run(self):
        d = fireEventually()
        d.addCallback(lambda res: self.setUp())
        # use RIClient.get_nodeid() to measure the foolscap-level RTT
        d.addCallback(lambda res: self.client_rref.callRemote("measure_peer_response_time"))
        d.addCallback(self.got_rtt)
        d.addBoth(self.tearDown)
        def _err(err):
            self.failed = err
            log.err(err)
            print err
        d.addErrback(_err)
        def _done(res):
            reactor.stop()
            return res
        d.addBoth(_done)
        reactor.run()
        if self.failed:
            print "EXCEPTION"
            print self.failed
            sys.exit(1)

    def setUp(self):
        self.base_service.startService()
        self.tub = Tub()
        self.tub.setOption("expose-remote-exception-types", False)
        self.tub.setServiceParent(self.base_service)
        d = self.tub.getReference(self.control_furl)
        def _gotref(rref):
            self.client_rref = rref
            print "Got Client Control reference"
        d.addCallback(_gotref)
        return d

    def got_rtt(self, rtts):
        # rtts: nodeid -> latency (in seconds)
        times = rtts.values()
        print "avg latency:", sum(times) / len(times)
        key = datastore.Key("GridConfig")
        c = datastore.Entity(key)
        c.update({
            "num_server_instances": 3,
            "server_instance_types": [u"n1-standard-1"]*3,
            "num_servers": 6,
            "server_versions": [u"1.10.0"]*6,
            "server_latencies": times,
            "avg_server_latency": sum(times) / len(times),
            "max_server_latency": max(times),
            "client_instance_type": u"n1-standard-1",
            "client_version": u"1.10.0",
            })
        datastore.put([c])
        print "configid:", c.key.id

    def tearDown(self, res):
        d = self.base_service.stopService()
        d.addCallback(lambda ignored: res)
        return d

if __name__ == '__main__':
    ggc = GetGridConfig()
    ggc.run()
<|endoftext|>


<commit_before>#!/usr/bin/env python2.7

# Configuration.py
from GlobalConfiguration import GlobalConfiguration
from ColorUtils import *
from WebData import WebData
import math
#from DrawThread import DrawThread
#from UpdateThread import UpdateThread

# import the rpi_ws281x library
# make sure this is set up first
from neopixel import *

import logging
# set up logging (I really wanted to call it yule.log)
formatStr = "%(levelname) -10s %(asctime)s %(module)s:%(lineno)s %(funcName)s %(message)s"
logging.basicConfig(filename='xmas.log', level=logging.DEBUG, format = formatStr )
logger = logging.getLogger(__name__)

import colorsys
import math
import time
import random
import threading

# These values shouldn't have to be changed when already working
# moved to configuration file
#LED_COUNT     = 600 # Number of LEDS
LED_PIN     = 18 # GPIO pin connected to the pixels
LED_FREQ_HZ    = 800000 # LED signal frequency in hz
LED_DMA        = 5 # DMA channel for generating signal
# moved to configuration file
#LED_BRIGHTNESS    = 20 # Between 0 and 255, if used with something like 600 leds, have the brightness no more than ~75
LED_INVERT    = False

# fast linear sin approx
def fastApprox(val):
    return 1.0 - math.fabs( math.fmod(val, 2.0) - 1.0)

# globals
#global strip
global stripData
global config
global webData
global updateThread
global drawThread

# start stuff
# load the configuration data
config = GlobalConfiguration()
config.load()

#define the strip data
stripData = [(0,0,0) for c in range(config.LEDCount)]

# Create the NeoPixel strip
strip = Adafruit_NeoPixel(config.LEDCount, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, config.Brightness, strip_type=ws.WS2811_STRIP_GRB)
strip.begin()
# define web data
webData = WebData(config)

# update thread stuff
class UpdateThread(object):

    def __init__(self):
        self.canRun = True
        self.thread = threading.Thread(target=self.run, args=())
        self.thread.daemon = True
        self.thread.start()

    def stop(self):
        self.canRun = False

    def run(self):
        while self.canRun:
            try:
                color1 = webData.color1
                color2 = webData.color2

                if webData.isRandom1:
                    color1 = hsv(random.random(), 1, 1)
                    #logging.info("RAND C1 " + str(color1))
                if webData.isRandom2:
                    color2 = hsv(random.random(), 1, 1)
                    #logging.info("RAND C2 " + str(color2))
                # this is where I compare the values of web data pattern
                #logging.info(webData.pattern)
                #logging.info(str(config.Patterns))
                #logging.info(str(config.Patterns.get("pattern_solid")))
                # to those in the configuration
                if webData.pattern == int(config.Patterns.get("pattern_solid")):
                    #logging.info("SOLID COLOR")
                    # set solid color
                    for x in range(config.LEDCount):
                        stripData[x] = color1
                if webData.pattern == int(config.Patterns.get("pattern_pulse")):
                    amp = math.sin(time.time() * 1000.0 / float(webData.delay))
                    color = color1
                    if amp < 0:
                        color = color2
                    color = multiplyArray(color, math.fabs(amp))
                    for x in range(config.LEDCount):
                        stripData[x] = color
                if webData.pattern == int(config.Patterns.get("pattern_blink")):
                    #logging.info("BLINK")
                    # set all color 1
                    for x in range(config.LEDCount):
                        stripData[x] = color1
                    # wait
                    time.sleep(webData.delay / 1000.0)
                    #logging.info("BLINK CHANGE")
                    # set all color 2
                    for x in range(config.LEDCount):
                        stripData[x] = color2
                    # wait
                    time.sleep(webData.delay / 1000.0)
                if webData.pattern == int(config.Patterns.get("pattern_scrollsmooth")):
                    #logging.info("Smooth")
                    for x in range(config.LEDCount):
                        c1 = multiplyArray(color1, fastApprox(x / float(webData.length + 1) + float(time.time()) * 1000.0 / float(webData.delay)))
                        c2 = multiplyArray(color2, fastApprox(x / float(webData.length + 1) + 1 + float(time.time()) * 1000.0 / float(webData.delay)))
                        c3 = rgbToTuple(c1[0] + c2[0], c1[1] + c2[1], c1[2] + c2[2])
                        #stripData[x] = addArray(c1, c2)
                        stripData[x] = c3
                if webData.pattern == int(config.Patterns.get("pattern_scroll")):
                    # iterate through
                    for x in range(config.LEDCount):
                        # default color 1
                        color = webData.color1
                        # offset by time, x pos divided by length mod 2
                        # time.time is in seconds not ms
                        # sets color 2
                        if (time.time() / 1000.0 * float(webData.delay) + x / float(webData.length + 1)) % 2 == 1:
                            color = color2
                        # set color
                        stripData[x] = color
                if webData.pattern == int(config.Patterns.get("pattern_wipeup")):
                    # wipe color 1 first
                    for x in range(config.LEDCount):
                        stripData[x] = color1
                        # set the color and wait after each set
                        time.sleep(webData.delay / 1000.0 / float(config.LEDCount))
                    for x in range(config.LEDCount):
                        stripData[x] = color2
                        # set the color and wait after each set
                        time.sleep(webData.delay / 1000.0 / float(config.LEDCount))
                if webData.pattern == int(config.Patterns.get("pattern_wipedown")):
                    # does the same as wipe up but is backwards
                    # wipe color 1 first
                    for x in reversed(range(config.LEDCount)):
                        stripData[x] = color1
                        # set the color and wait after each set
                        time.sleep(webData.delay / 1000.0 / float(config.LEDCount))
                    for x in reversed(range(config.LEDCount)):
                        stripData[x] = color2
                        # set the color and wait after each set
                        time.sleep(webData.delay / 1000.0 / float(config.LEDCount))
                if webData.pattern == int(config.Patterns.get("pattern_random")):
                    #logging.info("RANDOM")
                    # set all to color2
                    for x in range(config.LEDCount):
                        stripData[x] = color2
                    # iterate through length times
                    #logging.info(str(webData.length))
                    for x in range(webData.length):
                        # randomly pick an led and set it to color1
                        i = random.randint(0, config.LEDCount - 1 )
                        #logging.info(str(i))
                        stripData[i] = color1

                    time.sleep(webData.delay / 1000.0)
                if webData.pattern == int(config.Patterns.get("pattern_larson")):
                    #logging.info("larson")
                    # set all to color2
                    for x in range(config.LEDCount):
                        stripData[x] = color2
                    # calculate lit area
                    center = config.LEDCount / 2 + ((config.LEDCount / 2) - webData.length) * math.sin(6.28 * time.time() * 1000.0 / float(webData.delay))
                    #logging.info("center " + str(center))
                    # set the values for the given width
                    for x in range(-1 * webData.length, webData.length):
                        stripData[int(center + x)] = color1

                # for now just set everything to color1
                #for x in range(config.LEDCount):
                #    #logging.info("UPDATE PIXEL " + str(x) + " val " + str( webData.color1));
                #    stripData[x] = webData.color1
            except Exception as e:
                logging.error("Exception " + str(e))

# draw thread stuff
class DrawThread(object):
    def __init__(self):
        self.canRun = True
        self.thread = threading.Thread(target=self.run, args=())
        self.thread.daemon = True
        self.thread.start()

    def stop(self):
        self.canRun = False

    def run(self):
        while self.canRun:
            try:
                for x in range(config.LEDCount):
                    try:
                        #logging.info("DRAW RUN")
                        #if(stripData[x] >= 0 and stripData[x] <= (255 << 16 | 255 << 8 | 255)):
                        if True:
                            #logging.info(str(stripData[x]))
                            #logging.info(str(get24BitColorValueArray(stripData[x])))
                            #logging.info("SET PIXEL " + str(x) + "val " + str(int(stripData[x], 16)) + " " + str(stripData[x]))
                            # also clamps
                            strip.setPixelColor(x, get24BitColorValueArray(stripData[x]))
                            #strip.setPixelColor(x, int(stripData[x], 16))
                    except OverflowError:
                        logging.error("Led Overflow error")
                strip.show()
            except Exception as e:
                logging.error("err1" + str(e))

if __name__ == "__main__":
    # Intialize the library (must be called once before other functions).
    strip.begin()

    #start update thread
    updateThread = UpdateThread()

    # start draw thread
    drawThread = DrawThread()

    # loop
    try:
        while True:
            # webData is running when file changes
            # update thread is running
            # draw thread is running
            time.sleep(1)
    except (KeyboardInterrupt, SystemExit):
        logger.debug("End of program")
        webData.close()
        #drawTh.canRun = False
        #cleanup()
    except Exception as e:
        logger.error("Other exception!" + e)
<commit_msg>Fixed Scroll pattern. Everything is complete, yay<commit_after>#!/usr/bin/env python2.7

# Configuration.py
from GlobalConfiguration import GlobalConfiguration
from ColorUtils import *
from WebData import WebData
import math
#from DrawThread import DrawThread
#from UpdateThread import UpdateThread

# import the rpi_ws281x library
# make sure this is set up first
from neopixel import *

import logging
# set up logging (I really wanted to call it yule.log)
formatStr = "%(levelname) -10s %(asctime)s %(module)s:%(lineno)s %(funcName)s %(message)s"
logging.basicConfig(filename='xmas.log', level=logging.DEBUG, format = formatStr )
logger = logging.getLogger(__name__)

import colorsys
import math
import time
import random
import threading

# These values shouldn't have to be changed when already working
# moved to configuration file
#LED_COUNT     = 600 # Number of LEDS
LED_PIN     = 18 # GPIO pin connected to the pixels
LED_FREQ_HZ    = 800000 # LED signal frequency in hz
LED_DMA        = 5 # DMA channel for generating signal
# moved to configuration file
#LED_BRIGHTNESS    = 20 # Between 0 and 255, if used with something like 600 leds, have the brightness no more than ~75
LED_INVERT    = False

# fast linear sin approx
def fastApprox(val):
    return 1.0 - math.fabs( math.fmod(val, 2.0) - 1.0)

# globals
#global strip
global stripData
global config
global webData
global updateThread
global drawThread

# start stuff
# load the configuration data
config = GlobalConfiguration()
config.load()

#define the strip data
stripData = [(0,0,0) for c in range(config.LEDCount)]

# Create the NeoPixel strip
strip = Adafruit_NeoPixel(config.LEDCount, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, config.Brightness, strip_type=ws.WS2811_STRIP_GRB)
strip.begin()
# define web data
webData = WebData(config)

# update thread stuff
class UpdateThread(object):

    def __init__(self):
        self.canRun = True
        self.thread = threading.Thread(target=self.run, args=())
        self.thread.daemon = True
        self.thread.start()

    def stop(self):
        self.canRun = False

    def run(self):
        while self.canRun:
            try:
                color1 = webData.color1
                color2 = webData.color2

                if webData.isRandom1:
                    color1 = hsv(random.random(), 1, 1)
                    #logging.info("RAND C1 " + str(color1))
                if webData.isRandom2:
                    color2 = hsv(random.random(), 1, 1)
                    #logging.info("RAND C2 " + str(color2))
                # this is where I compare the values of web data pattern
                #logging.info(webData.pattern)
                #logging.info(str(config.Patterns))
                #logging.info(str(config.Patterns.get("pattern_solid")))
                # to those in the configuration
                if webData.pattern == int(config.Patterns.get("pattern_solid")):
                    #logging.info("SOLID COLOR")
                    # set solid color
                    for x in range(config.LEDCount):
                        stripData[x] = color1
                if webData.pattern == int(config.Patterns.get("pattern_pulse")):
                    amp = math.sin(time.time() * 1000.0 / float(webData.delay))
                    color = color1
                    if amp < 0:
                        color = color2
                    color = multiplyArray(color, math.fabs(amp))
                    for x in range(config.LEDCount):
                        stripData[x] = color
                if webData.pattern == int(config.Patterns.get("pattern_blink")):
                    #logging.info("BLINK")
                    # set all color 1
                    for x in range(config.LEDCount):
                        stripData[x] = color1
                    # wait
                    time.sleep(webData.delay / 1000.0)
                    #logging.info("BLINK CHANGE")
                    # set all color 2
                    for x in range(config.LEDCount):
                        stripData[x] = color2
                    # wait
                    time.sleep(webData.delay / 1000.0)
                if webData.pattern == int(config.Patterns.get("pattern_scrollsmooth")):
                    #logging.info("Smooth")
                    for x in range(config.LEDCount):
                        c1 = multiplyArray(color1, fastApprox(x / float(webData.length + 1) + float(time.time()) * 1000.0 / float(webData.delay)))
                        c2 = multiplyArray(color2, fastApprox(x / float(webData.length + 1) + 1 + float(time.time()) * 1000.0 / float(webData.delay)))
                        c3 = rgbToTuple(c1[0] + c2[0], c1[1] + c2[1], c1[2] + c2[2])
                        #stripData[x] = addArray(c1, c2)
                        stripData[x] = c3
                if webData.pattern == int(config.Patterns.get("pattern_scroll")):
                    # iterate through
                    offset = int(time.time() * 1000 / float(webData.delay))
                    for x in range(config.LEDCount):
                        # default color 1
                        color = webData.color1
                        # offset by time, x pos divided by length mod 2
                        # time.time is in seconds not ms
                        # sets color 2
                        if ((offset + x) / webData.length) % 2 == 1:
                            color = color2
                        # set color
                        stripData[x] = color
                if webData.pattern == int(config.Patterns.get("pattern_wipeup")):
                    # wipe color 1 first
                    for x in range(config.LEDCount):
                        stripData[x] = color1
                        # set the color and wait after each set
                        time.sleep(webData.delay / 1000.0 / float(config.LEDCount))
                    for x in range(config.LEDCount):
                        stripData[x] = color2
                        # set the color and wait after each set
                        time.sleep(webData.delay / 1000.0 / float(config.LEDCount))
                if webData.pattern == int(config.Patterns.get("pattern_wipedown")):
                    # does the same as wipe up but is backwards
                    # wipe color 1 first
                    for x in reversed(range(config.LEDCount)):
                        stripData[x] = color1
                        # set the color and wait after each set
                        time.sleep(webData.delay / 1000.0 / float(config.LEDCount))
                    for x in reversed(range(config.LEDCount)):
                        stripData[x] = color2
                        # set the color and wait after each set
                        time.sleep(webData.delay / 1000.0 / float(config.LEDCount))
                if webData.pattern == int(config.Patterns.get("pattern_random")):
                    #logging.info("RANDOM")
                    # set all to color2
                    for x in range(config.LEDCount):
                        stripData[x] = color2
                    # iterate through length times
                    #logging.info(str(webData.length))
                    for x in range(webData.length):
                        # randomly pick an led and set it to color1
                        i = random.randint(0, config.LEDCount - 1 )
                        #logging.info(str(i))
                        stripData[i] = color1

                    time.sleep(webData.delay / 1000.0)
                if webData.pattern == int(config.Patterns.get("pattern_larson")):
                    #logging.info("larson")
                    # set all to color2
                    for x in range(config.LEDCount):
                        stripData[x] = color2
                    # calculate lit area
                    center = config.LEDCount / 2 + ((config.LEDCount / 2) - webData.length) * math.sin(6.28 * time.time() * 1000.0 / float(webData.delay))
                    #logging.info("center " + str(center))
                    # set the values for the given width
                    for x in range(-1 * webData.length, webData.length):
                        stripData[int(center + x)] = color1

                # for now just set everything to color1
                #for x in range(config.LEDCount):
                #    #logging.info("UPDATE PIXEL " + str(x) + " val " + str( webData.color1));
                #    stripData[x] = webData.color1
            except Exception as e:
                logging.error("Exception " + str(e))

# draw thread stuff
class DrawThread(object):
    def __init__(self):
        self.canRun = True
        self.thread = threading.Thread(target=self.run, args=())
        self.thread.daemon = True
        self.thread.start()

    def stop(self):
        self.canRun = False

    def run(self):
        while self.canRun:
            try:
                for x in range(config.LEDCount):
                    try:
                        #logging.info("DRAW RUN")
                        #if(stripData[x] >= 0 and stripData[x] <= (255 << 16 | 255 << 8 | 255)):
                        if True:
                            #logging.info(str(stripData[x]))
                            #logging.info(str(get24BitColorValueArray(stripData[x])))
                            #logging.info("SET PIXEL " + str(x) + "val " + str(int(stripData[x], 16)) + " " + str(stripData[x]))
                            # also clamps
                            strip.setPixelColor(x, get24BitColorValueArray(stripData[x]))
                            #strip.setPixelColor(x, int(stripData[x], 16))
                    except OverflowError:
                        logging.error("Led Overflow error")
                strip.show()
            except Exception as e:
                logging.error("err1" + str(e))

if __name__ == "__main__":
    # Intialize the library (must be called once before other functions).
    strip.begin()

    #start update thread
    updateThread = UpdateThread()

    # start draw thread
    drawThread = DrawThread()

    # loop
    try:
        while True:
            # webData is running when file changes
            # update thread is running
            # draw thread is running
            time.sleep(1)
    except (KeyboardInterrupt, SystemExit):
        logger.debug("End of program")
        webData.close()
        #drawTh.canRun = False
        #cleanup()
    except Exception as e:
        logger.error("Other exception!" + e)
<|endoftext|>


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

import dupelib

import argparse
import itertools
import os

def get_arg_parser():
    p = argparse.ArgumentParser(
        description="Find files with identical content."
    )

    p.add_argument("paths",
        nargs="*",
        metavar="PATH",
        help="""List of files to consider. Specify --recurse (-r) to also
                consider the contents of directories."""
    )

    p.add_argument("-s", "--symlinks",
        action="store_true",
        help="""Include symlinks."""
    )

    p.add_argument("-z", "--zero",
        action="store_true",
        help="""Include zero-length files. All zero-length files are considered
                to have identical content."""
    )

    p.add_argument("-a", "--aliases",
        action="store_true",
        help="""Check whether a single file has more than one name, which is
                possible through hardlinks, as well as symlinks if the
                -s/--symlinks option is specified. This check is used to skip
                redundant content comparisons, add extra information to
                reports, and preserve all paths pointing to a selected file
                when the -p/--prefer option is used."""
    )

    p.add_argument("-r", "--recurse",
        action="store_true",
        help="""Recurse into subdirectories."""
    )

    p.add_argument("-p", "--prefer",
        metavar="CRITERIA",
        help="""For each set of duplicate files, automatically select one
                for preservation according to the provided criteria. Other
                duplicates can be deleted by passing the generated report to
                the -x/--execute option."""
    )

    p.add_argument("-x", "--execute",
        metavar="PATH",
        help="""Delete unmarked files in the report at %(metavar)s. Sets where
                no files are marked will be skipped."""
    )

    return p

SELECTION_MARKER_UNIQUE =    ">"
SELECTION_MARKER_NONUNIQUE = "?"
def handle_dupe_set(dupe_set, hardlink_info=True, selector=None):
    file_size = os.stat(dupe_set[0].path()).st_size

    print("## Size: {file_size} Instances: {inst_count} Excess: {excess_size} Names: {name_count}".format(
        inst_count=len(dupe_set),
        name_count=sum(len(instance.paths) for instance in dupe_set),
        file_size=dupelib.unitformat.format_file_size(file_size),
        excess_size=dupelib.unitformat.format_file_size(file_size * (len(dupe_set) - 1))
    ))

    selected_paths = set()
    selected_instances = set()
    if selector is not None:
        all_names = itertools.chain(*(instance.paths for instance in dupe_set))
        try:
            selected_paths.update(selector.pick(all_names))
            for instance in dupe_set:
                if len(selected_paths.intersection(instance.paths)) > 0:
                    selected_instances.add(instance)
        except EnvironmentError as ee:
            print("## Skipping selection due to error: {!s}".format(ee))

    if len(selected_instances) == 1:
        keep_marker = SELECTION_MARKER_UNIQUE
    else:
        keep_marker = SELECTION_MARKER_NONUNIQUE

    instance_header = hardlink_info
    for index, instance in enumerate(sorted(dupe_set, key=lambda i: len(i.paths), reverse=True)):
        if instance_header:
            if len(instance.paths) == 1:
                print("# Separate instances follow")
                instance_header = False
            else:
                print("# Instance {}".format(index + 1))
        for path in sorted(instance.paths):
            print("{keep_marker} {path}".format(
                keep_marker=keep_marker if instance in selected_instances else " ",
                path=path
            ))
    print()

def main():
    p = get_arg_parser()
    args = p.parse_args()

    selector = None
    if args.prefer:
        selector = dupelib.criteria.parse_selector(args.prefer)

    if args.execute:
        raise NotImplementedError("--execute")

    if args.zero:
        raise NotImplementedError("--zero")

    if args.symlinks:
        raise NotImplementedError("--symlinks")

    if args.recurse:
        path_iterator = dupelib.walk.recurse_iterator(args.paths)
    else:
        path_iterator = args.paths

    for dupe_set in dupelib.find_duplicate_files(
        path_iterator,
        collect_inodes=args.aliases,
        error_cb="print_stderr",
        #log_cb="print_stderr"
    ):
        handle_dupe_set(dupe_set, hardlink_info=args.aliases, selector=selector)

if __name__ == "__main__":
    main()
<commit_msg>various fussing for clarity<commit_after>#!/usr/bin/env python3

import dupelib

import argparse
import itertools
import os

def get_arg_parser():
    p = argparse.ArgumentParser(
        description="Find files with identical content."
    )

    p.add_argument("paths",
        nargs="*",
        metavar="PATH",
        help="""List of files to consider. Specify --recurse (-r) to also
                consider the contents of directories."""
    )

    p.add_argument("-s", "--symlinks",
        action="store_true",
        help="""Include symlinks."""
    )

    p.add_argument("-z", "--zero",
        action="store_true",
        help="""Include zero-length files. All zero-length files are considered
                to have identical content."""
    )

    p.add_argument("-a", "--aliases",
        action="store_true",
        help="""Check whether a single file has more than one name, which is
                possible through hardlinks, as well as symlinks if the
                -s/--symlinks option is specified. This check is used to skip
                redundant content comparisons, add extra information to
                reports, and preserve all paths pointing to a selected file
                when the -p/--prefer option is used."""
    )

    p.add_argument("-r", "--recurse",
        action="store_true",
        help="""Recurse into subdirectories."""
    )

    p.add_argument("-p", "--prefer",
        metavar="CRITERIA",
        help="""For each set of duplicate files, automatically select one
                for preservation according to the provided criteria. Other
                duplicates can be deleted by passing the generated report to
                the -x/--execute option."""
    )

    p.add_argument("-x", "--execute",
        metavar="PATH",
        help="""Delete unmarked files in the report at %(metavar)s. Sets where
                no files are marked will be skipped."""
    )

    return p

SELECTION_MARKER_UNIQUE =    ">"
SELECTION_MARKER_NONUNIQUE = "?"
def handle_dupe_set(dupe_set, show_hardlink_info=True, selector=None):
    file_size = os.stat(dupe_set[0].path()).st_size

    print("## Size: {file_size} Instances: {inst_count} Excess: {excess_size} Names: {name_count}".format(
        inst_count=len(dupe_set),
        name_count=sum(len(instance.paths) for instance in dupe_set),
        file_size=dupelib.unitformat.format_file_size(file_size),
        excess_size=dupelib.unitformat.format_file_size(file_size * (len(dupe_set) - 1))
    ))

    selected_paths = set()
    selected_instances = set()
    if selector is not None:
        all_names = itertools.chain(*(instance.paths for instance in dupe_set))
        try:
            selected_paths.update(selector.pick(all_names))
            for instance in dupe_set:
                if len(selected_paths.intersection(instance.paths)) > 0:
                    selected_instances.add(instance)
        except EnvironmentError as ee:
            print("## Skipping selection due to error: {!s}".format(ee))

    keep_marker = (
        SELECTION_MARKER_UNIQUE if len(selected_instances) == 1
        else SELECTION_MARKER_NONUNIQUE
    )

    instance_header = show_hardlink_info
    for index, instance in enumerate(
        sorted(
            dupe_set,
            key=lambda i: len(i.paths), reverse=True
        )
    ):
        if instance_header:
            if len(instance.paths) == 1:
                print("# Separate instances follow")
                instance_header = False
            else:
                print("# Instance {}".format(index + 1))

        for path in sorted(instance.paths):
            print("{keep_marker} {path}".format(
                keep_marker=keep_marker if instance in selected_instances else " ",
                path=path
            ))
    print()

def main():
    p = get_arg_parser()
    args = p.parse_args()

    selector = None
    if args.prefer:
        selector = dupelib.criteria.parse_selector(args.prefer)

    if args.execute:
        raise NotImplementedError("--execute")

    if args.zero:
        raise NotImplementedError("--zero")

    if args.symlinks:
        raise NotImplementedError("--symlinks")

    if args.recurse:
        path_iterator = dupelib.walk.recurse_iterator(args.paths)
    else:
        path_iterator = args.paths

    for dupe_set in dupelib.find_duplicate_files(
        path_iterator,
        collect_inodes=args.aliases,
        error_cb="print_stderr",
        #log_cb="print_stderr"
    ):
        handle_dupe_set(dupe_set, show_hardlink_info=args.aliases, selector=selector)

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


<commit_before>from data_collection.management.commands import BaseXpressDemocracyClubCsvImporter

class Command(BaseXpressDemocracyClubCsvImporter):
    council_id = "E07000090"
    addresses_name = "local.2019-05-02/Version 1/Democracy_Club__02May2019Havant.tsv"
    stations_name = "local.2019-05-02/Version 1/Democracy_Club__02May2019Havant.tsv"
    elections = ["local.2019-05-02"]
    csv_delimiter = "\t"

    def address_record_to_dict(self, record):
        rec = super().address_record_to_dict(record)
        uprn = record.property_urn.strip().lstrip("0")

        if uprn == "100060444834":
            rec["postcode"] = "PO11 0LJ"
            rec["accept_suggestion"] = False

        if uprn in [
            "100062455644",  # PO88BB -> PO78NU : Plough & Barleycorn, Tempest Avenue, Cowplain, Waterlooville, Hampshire
            "10013680951",  # PO107NH -> PO107DN : 1E St James Road, Emsworth, Hampshire
            "10013675502",  # PO89UB -> PO89GX : 7A The Kestrels, 76 Eagle Avenue, Waterlooville, Hampshire
        ]:
            rec["accept_suggestion"] = True

        if uprn in [
            "100060458913"  # PO75AA -> PO75JU : 108 London Road, Widley, Waterlooville, Hampshire
        ]:
            rec["accept_suggestion"] = False

        return rec
<commit_msg>import Havant (closes #1898)<commit_after>from data_collection.management.commands import BaseXpressDemocracyClubCsvImporter

class Command(BaseXpressDemocracyClubCsvImporter):
    council_id = "E07000090"
    addresses_name = "europarl.2019-05-23/Version 1/Democracy_Club__23May2019havant.tsv"
    stations_name = "europarl.2019-05-23/Version 1/Democracy_Club__23May2019havant.tsv"
    elections = ["europarl.2019-05-23"]
    csv_delimiter = "\t"

    def address_record_to_dict(self, record):
        rec = super().address_record_to_dict(record)
        uprn = record.property_urn.strip().lstrip("0")

        if uprn == "100060444834":
            rec["postcode"] = "PO11 0LJ"
            rec["accept_suggestion"] = False

        if uprn in [
            "100062455644",  # PO88BB -> PO78NU : Plough & Barleycorn, Tempest Avenue, Cowplain, Waterlooville, Hampshire
            "10013680951",  # PO107NH -> PO107DN : 1E St James Road, Emsworth, Hampshire
            "10013675502",  # PO89UB -> PO89GX : 7A The Kestrels, 76 Eagle Avenue, Waterlooville, Hampshire
            "10013679840",  # PO93EZ -> PO95EZ : 41 Tyler House, Bishopstoke Road, Havant, Hampshire
            "10013679413",  # PO92DT -> PO93DT : 2 Mead Terrace, Hooks Lane, Bedhampton, Havant, Hampshire
        ]:
            rec["accept_suggestion"] = True

        if uprn in [
            "100060458913"  # PO75AA -> PO75JU : 108 London Road, Widley, Waterlooville, Hampshire
        ]:
            rec["accept_suggestion"] = False

        return rec
<|endoftext|>