# -*- coding: utf-8 -*-
########################################################################
#
#       License: BSD 3-clause
#       Created: September 22, 2010
#       Author:  Francesc Alted - faltet@gmail.com
#
########################################################################

# flake8: noqa

from __future__ import print_function

import os
import platform
import re
import sys
import io

from setuptools import Extension
from setuptools import setup
from glob import glob
from distutils.version import LooseVersion
from distutils.command.build_ext import build_ext
from distutils.errors import CompileError
from textwrap import dedent

class BloscExtension(Extension):
    """Allows extension to carry architecture-capable flag options.

    Attributes:
        avx2_def (Dict[str]: List[str]):
            AVX2 support dictionary mapping Extension properties to a
            list of values. If compiler is AVX2 capable, then these will
            be appended onto the end of the Extension properties.
    """

    def __init__(self, *args, **kwargs):
        self.avx2_defs = kwargs.pop("avx2_defs", {})
        Extension.__init__(self, *args, **kwargs)

class build_ext_posix_avx2(build_ext):
    """build_ext customized to test for AVX2 support in posix compiler.

    This is because until distutils has actually started the build
    process, we can't be certain what compiler is being used.

    If compiler supports, then the avx2_defs dictionary on any given
    Extension will be used to extend the other Extension attributes.
    """

    def _test_compiler_flags(self, name, flags):
        # type: (List[str]) -> Bool
        """Test that a sample program can compile with given flags.

        Attr:
            flags (List[str]): the flags to test
            name (str): An identifier-like name to cache the results as

        Returns:
            (bool): Whether the compiler accepted the flags(s)
        """
        # Look to see if we have a written file to cache the result
        success_file = os.path.join(self.build_temp, "_{}_present".format(name))
        fail_file = os.path.join(self.build_temp, "_{}_failed".format(name))
        if os.path.isfile(success_file):
            return True
        elif os.path.isfile(fail_file):
            return False
        # No cache file, try to run the compile
        try:
            # Write an empty test file
            test_file = os.path.join(self.build_temp, "test_{}_empty.c".format(name))
            if not os.path.isfile(test_file):
                open(test_file, "w").close()
            objects = self.compiler.compile(
                [test_file], output_dir=self.build_temp, extra_postargs=flags
            )
            # Write a success marker so we don't need to compile again
            open(success_file, 'w').close()
            return True
        except CompileError:
            # Write a failure marker so we don't need to compile again
            open(fail_file, 'w').close()
            return False
        finally:
            pass

    def build_extensions(self):
        # Verify that the compiler supports requested extra flags
        if self._test_compiler_flags("avx2", ["-mavx2"]):
            # Apply the AVX2 properties to each extension
            for extension in self.extensions:
                if hasattr(extension, "avx2_defs"):
                    # Extend an existing attribute with the stored values
                    for attr, defs in extension.avx2_defs.items():
                        getattr(extension, attr).extend(defs)
        else:
            print("AVX2 Unsupported by compiler")

        # Call up to the superclass to do the actual build
        build_ext.build_extensions(self)

if __name__ == '__main__':

    with io.open('README.rst', encoding='utf-8') as f:
        long_description = f.read()

    try:
        import cpuinfo
        cpu_info = cpuinfo.get_cpu_info()
    except Exception:
        # newer cpuinfo versions fail to import on unsupported architectures
        cpu_info = None

    ########### Check versions ##########
    def exit_with_error(message):
        print('ERROR: %s' % message)
        sys.exit(1)

    # Check for Python
    if sys.version_info[0] == 2:
        if sys.version_info[1] < 7:
            exit_with_error("You need Python 2.7 or greater to install blosc!")
    elif sys.version_info[0] == 3:
        if sys.version_info[1] < 4:
            exit_with_error("You need Python 3.4 or greater to install blosc!")
    else:
        exit_with_error("You need Python 2.7/3.4 or greater to install blosc!")

    tests_require = ['numpy', 'psutil']

    ########### End of checks ##########

    # Read the long_description from README.rst
    with open('README.rst') as f:
        long_description = f.read()

    # Blosc version
    VERSION = open('VERSION').read().strip()
    # Create the version.py file
    open('blosc/version.py', 'w').write('__version__ = "%s"\n' % VERSION)

    # Global variables
    CFLAGS = os.environ.get('CFLAGS', '').split()
    LFLAGS = os.environ.get('LFLAGS', '').split()
    # Allow setting the Blosc dir if installed in the system
    BLOSC_DIR = os.environ.get('BLOSC_DIR', '')

    # Check for USE_CODEC environment variables
    try:
        INCLUDE_LZ4 = os.environ['INCLUDE_LZ4'] == '1'
    except KeyError:
        INCLUDE_LZ4 = True
    try:
        INCLUDE_SNAPPY = os.environ['INCLUDE_SNAPPY'] == '1'
    except KeyError:
        INCLUDE_SNAPPY = False  # Snappy is disabled by default
    try:
        INCLUDE_ZLIB = os.environ['INCLUDE_ZLIB'] == '1'
    except KeyError:
        INCLUDE_ZLIB = True
    try:
        INCLUDE_ZSTD = os.environ['INCLUDE_ZSTD'] == '1'
    except KeyError:
        INCLUDE_ZSTD = True

    # Handle --blosc=[PATH] --lflags=[FLAGS] --cflags=[FLAGS]
    args = sys.argv[:]
    for arg in args:
        if arg.find('--blosc=') == 0:
            BLOSC_DIR = os.path.expanduser(arg.split('=')[1])
            sys.argv.remove(arg)
        if arg.find('--lflags=') == 0:
            LFLAGS = arg.split('=')[1].split()
            sys.argv.remove(arg)
        if arg.find('--cflags=') == 0:
            CFLAGS = arg.split('=')[1].split()
            sys.argv.remove(arg)

    # Blosc sources and headers

    # To avoid potential namespace collisions use build_clib.py for each codec
    # instead of co-compiling all sources files in one setuptools.Extension object.
    clibs = [] # for build_clib, libraries TO BE BUILT

    # Below are parameters for the Extension object
    sources = ["blosc/blosc_extension.c"]
    inc_dirs = []
    lib_dirs = []
    libs = []  # Pre-built libraries ONLY, like python36.so
    def_macros = []
    builder_class = build_ext  # To swap out if we have AVX capability and posix
    avx2_defs = {}  # Definitions to build extension with if compiler supports AVX2

    if BLOSC_DIR != '':
        # Using the Blosc library
        lib_dirs += [os.path.join(BLOSC_DIR, 'lib')]
        inc_dirs += [os.path.join(BLOSC_DIR, 'include')]
        libs += ['blosc']
    else:

        # Configure the Extension
        # Compiling everything from included C-Blosc sources
        sources += [f for f in glob('c-blosc/blosc/*.c')
                    if 'avx2' not in f and 'sse2' not in f]

        inc_dirs += [os.path.join('c-blosc', 'blosc')]
        inc_dirs += glob('c-blosc/internal-complibs/*')

        # Codecs to be built with build_clib
        if INCLUDE_LZ4:
            clibs.append( ('lz4', {'sources': glob('c-blosc/internal-complibs/lz4*/*.c')} ) )
            inc_dirs += glob('c-blosc/internal-complibs/lz4*')
            def_macros += [('HAVE_LZ4',1)]

        # Tried and failed to compile Snappy with gcc using 'cflags' on posix
        # setuptools always uses gcc instead of g++, as it only checks for the
        # env var 'CC' and not 'CXX'.
        if INCLUDE_SNAPPY:
            clibs.append( ('snappy', {'sources': glob('c-blosc/internal-complibs/snappy*/*.cc'),
                                    'cflags': ['-std=c++11', '-lstdc++'] } ) )
            inc_dirs += glob('c-blosc/internal-complibs/snappy*')
            def_macros += [('HAVE_SNAPPY',1)]

        if INCLUDE_ZLIB:
            clibs.append( ('zlib', {'sources': glob('c-blosc/internal-complibs/zlib*/*.c')} ) )
            def_macros += [('HAVE_ZLIB',1)]

        if INCLUDE_ZSTD:
            clibs.append( ('zstd', {'sources': glob('c-blosc/internal-complibs/zstd*/*/*.c'),
                        'include_dirs': glob('c-blosc/internal-complibs/zstd*') + glob('c-blosc/internal-complibs/zstd*/common') } ) )
            inc_dirs += glob('c-blosc/internal-complibs/zstd*/common')
            inc_dirs += glob('c-blosc/internal-complibs/zstd*')
            def_macros += [('HAVE_ZSTD',1)]

        # Guess SSE2 or AVX2 capabilities
        # SSE2
        if 'DISABLE_BLOSC_SSE2' not in os.environ and cpu_info != None and 'sse2' in cpu_info.get('flags', {}):
            print('SSE2 detected')
            CFLAGS.append('-DSHUFFLE_SSE2_ENABLED')
            sources += [f for f in glob('c-blosc/blosc/*.c') if 'sse2' in f]
            if os.name == 'posix':
                CFLAGS.append('-msse2')
            elif os.name == 'nt':
                def_macros += [('__SSE2__', 1)]
        # AVX2
        if 'DISABLE_BLOSC_AVX2' not in os.environ and cpu_info != None and 'sse2' in cpu_info.get('flags', {}):
            if os.name == 'posix':
                print("AVX2 detected")
                avx2_defs = {
                    "extra_compile_args": ["-DSHUFFLE_AVX2_ENABLED", "-mavx2"],
                    "sources": [f for f in glob("c-blosc/blosc/*.c") if "avx2" in f]
                }
                # The CPU supports it but the compiler might not..
                builder_class = build_ext_posix_avx2
            elif(os.name == 'nt' and
                    LooseVersion(platform.python_version()) >= LooseVersion('3.5.0')):
                # Neither MSVC2008 for Python 2.7 or MSVC2010 for Python 3.4 have
                # sufficient AVX2 support
                # Since we don't rely on any special compiler capabilities,
                # we don't need to rely on testing the compiler
                print('AVX2 detected')
                CFLAGS.append('-DSHUFFLE_AVX2_ENABLED')
                sources += [f for f in glob('c-blosc/blosc/*.c') if 'avx2' in f]
                def_macros += [('__AVX2__', 1)]
        # TODO: AVX512

    classifiers = dedent("""\
    Development Status :: 5 - Production/Stable
    Intended Audience :: Developers
    Intended Audience :: Information Technology
    Intended Audience :: Science/Research
    License :: OSI Approved :: BSD License
    Programming Language :: Python
    Programming Language :: Python :: 2.7
    Programming Language :: Python :: 3.4
    Programming Language :: Python :: 3.5
    Programming Language :: Python :: 3.6
    Programming Language :: Python :: 3.7
    Topic :: Software Development :: Libraries :: Python Modules
    Topic :: System :: Archiving :: Compression
    Operating System :: Microsoft :: Windows
    Operating System :: Unix
    """)

    setup(name = "blosc",
        version = VERSION,
        description = 'Blosc data compressor',
        long_description = long_description,
        classifiers = [c for c in classifiers.split("\n") if c],
        author = 'Francesc Alted, Valentin Haenel',
        author_email = 'faltet@gmail.com, valentin@haenel.co',
        maintainer = 'Francesc Alted, Valentin Haenel',
        maintainer_email = 'faltet@gmail.com, valentin@haenel.co',
        url = 'http://github.com/blosc/python-blosc',
        license = 'https://opensource.org/licenses/BSD-3-Clause',
        platforms = ['any'],
        libraries = clibs,
        ext_modules = [
            BloscExtension( "blosc.blosc_extension",
                    include_dirs=inc_dirs,
                    define_macros=def_macros,
                    sources=sources,
                    library_dirs=lib_dirs,
                    libraries=libs,
                    extra_link_args=LFLAGS,
                    extra_compile_args=CFLAGS,
                    avx2_defs=avx2_defs
            ),
        ],
        tests_require=tests_require,
        zip_safe=False,
        packages = ['blosc'],
        cmdclass={'build_ext': builder_class},
        )
elif __name__ == '__mp_main__':
    # This occurs from `cpuinfo 4.0.0` using multiprocessing to interrogate the
    # CPUID flags
    # https://github.com/workhorsy/py-cpuinfo/issues/108
    pass


# Copyright 2017 Open Source Robotics Foundation, Inc.
#
# 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.

import argparse
import os
import sys

from superflore.generate_installers import generate_installers
from superflore.generators.bitbake.gen_packages import regenerate_installer
from superflore.generators.bitbake.ros_meta import RosMeta
from superflore.TempfileManager import TempfileManager
from superflore.utils import err
from superflore.utils import info
from superflore.utils import ok
from superflore.utils import warn

# Modify if a new distro is added
active_distros = ['indigo', 'kinetic', 'lunar']
# just update packages, by default.
mode = 'update'
preserve_existing = True
overlay = None

def file_pr(overlay, delta, missing_deps):
    try:
        overlay.pull_request('{0}\n{1}'.format(delta, missing_deps))
    except Exception as e:
        err('Failed to file PR with allenh1/meta-ros repo!')
        err('  Exception: {0}'.format(e))
        sys.exit(1)

def main():
    global overlay
    global preserve_existing

    parser = argparse.ArgumentParser('Deploy ROS packages into Yocto Linux')
    parser.add_argument(
        '--ros-distro',
        help='regenerate packages for the specified distro',
        type=str
    )
    parser.add_argument(
        '--all',
        help='regenerate all packages in all distros',
        action="store_true"
    )
    parser.add_argument(
        '--output-repository-path',
        help='location of the Git repo',
        type=str
    )
    parser.add_argument(
        '--tar-archive-dir',
        help='location to store archived packages',
        type=str
    )
    selected_targets = active_distros
    args = parser.parse_args(sys.argv[1:])
    if args.all:
        warn('"All" mode detected... this may take a while!')
        preserve_existing = False
    elif args.ros_distro:
        warn('"{0}" distro detected...'.format(args.ros_distro))
        selected_targets = [args.ros_distro]
        preserve_existing = False
    with TempfileManager(args.output_repository_path) as _repo:
        if not args.output_repository_path:
            # give our group write permissions to the temp dir
            os.chmod(_repo, 17407)
        # clone if args.output-repository_path is None
        overlay = RosMeta(_repo, not args.output_repository_path)
        # generate installers
        total_installers = dict()
        total_broken = set()
        total_changes = dict()

        with TempfileManager(args.tar_archive_dir) as tar_dir:
            for distro in selected_targets:
                distro_installers, distro_broken, distro_changes =\
                    generate_installers(
                        distro,
                        overlay,
                        regenerate_installer,
                        preserve_existing,
                        tar_dir
                    )
                for key in distro_broken.keys():
                    for pkg in distro_broken[key]:
                        total_broken.add(pkg)

                total_changes[distro] = distro_changes
                total_installers[distro] = distro_installers

        num_changes = 0
        for distro_name in total_changes:
            num_changes += len(total_changes[distro_name])

        if num_changes == 0:
            info('ROS distro is up to date.')
            info('Exiting...')
            sys.exit(0)

        # remove duplicates
        inst_list = total_broken

        delta = "Changes:\n"
        delta += "========\n"

        if 'indigo' in total_changes and len(total_changes['indigo']) > 0:
            delta += "Indigo Changes:\n"
            delta += "---------------\n"

            for d in sorted(total_changes['indigo']):
                delta += '* {0}\n'.format(d)
            delta += "\n"

        if 'kinetic' in total_changes and len(total_changes['kinetic']) > 0:
            delta += "Kinetic Changes:\n"
            delta += "----------------\n"

            for d in sorted(total_changes['kinetic']):
                delta += '* {0}\n'.format(d)
            delta += "\n"

        if 'lunar' in total_changes and len(total_changes['lunar']) > 0:
            delta += "Lunar Changes:\n"
            delta += "--------------\n"

            for d in sorted(total_changes['lunar']):
                delta += '* {0}\n'.format(d)
            delta += "\n"

        missing_deps = ''

        if len(inst_list) > 0:
            missing_deps = "Missing Dependencies:\n"
            missing_deps += "=====================\n"
            for pkg in sorted(inst_list):
                missing_deps += " * [ ] {0}\n".format(pkg)

        # Commit changes and file pull request
        overlay.commit_changes(args.ros_distro)
        file_pr(overlay, delta, missing_deps)
        ok('Successfully synchronized repositories!')


from __future__ import print_function, division

import numpy as np
from astropy.table import Table

CART_COL_NAMES = ['X', 'Y', 'Z', 'U', 'V', 'W',
                  'dX', 'dY', 'dZ', 'dU', 'dV', 'dW',
                  'c_XY', 'c_XZ', 'c_XU', 'c_XV', 'c_XW',
                  'c_YZ', 'c_YU', 'c_YV', 'c_YW',
                  'c_ZU', 'c_ZV', 'c_ZW',
                  'c_UV', 'c_UW',
                  'c_VW']

def buildMeanAndCovMatFromRow(row):
    """
    Build a covariance matrix from a row

    Paramters
    ---------
    row : astropy Table row
        Entries: {X, Y, Z, U, V, W, dX, dY, ..., cXY, cXZ, ...}

    Return
    ------
    cov_mat : [6,6] numpy array
        Diagonal elements are dX^2, dY^2, ...
        Off-diagonal elements are cXY*dX*dY, cXZ*dX*dZ, ...
    """
    dim = 6
    # CART_COL_NAMES = ['X', 'Y', 'Z', 'U', 'V', 'W',
    #                   'dX', 'dY',   'dZ',   'dU',   'dV',   'dW',
    #                         'c_XY', 'c_XZ', 'c_XU', 'c_XV', 'c_XW',
    #                                 'c_YZ', 'c_YU', 'c_YV', 'c_YW',
    #                                         'c_ZU', 'c_ZV', 'c_ZW',
    #                                                 'c_UV', 'c_UW',
    #                                                         'c_VW']
    mean = np.zeros(dim)
    for i, col_name in enumerate(CART_COL_NAMES[:6]):
        mean[i] = row[col_name]

    std_vec = np.zeros(dim)
    for i, col_name in enumerate(CART_COL_NAMES[6:12]):
        std_vec[i] = row[col_name]
    corr_tri = np.zeros((dim,dim))
    # Insert upper triangle (top right) correlations
    for i, col_name in enumerate(CART_COL_NAMES[12:]):
        corr_tri[np.triu_indices(dim,1)[0][i],np.triu_indices(dim,1)[1][i]]\
            =row[col_name]
    # Build correlation matrix
    corr_mat = np.eye(6) + corr_tri + corr_tri.T
    # Multiply through by standard deviations
    cov_mat = corr_mat * std_vec * std_vec.reshape(6,1)
    return mean, cov_mat

def insertCartesainData(gt_row, xyzuvw_mean, xyzuvw_cov):
    dim = 6
    # CART_COL_NAMES = ['X', 'Y', 'Z', 'U', 'V', 'W',
    #                   'dX', 'dY', 'dZ', 'dU', 'dV', 'dW',
    #                   'c_XY', 'c_XZ', 'c_XU', 'c_XV', 'c_XW',
    #                           'c_YZ', 'c_YU', 'c_YV', 'c_YW',
    #                                   'c_ZU', 'c_ZV', 'c_ZW',
    #                                           'c_UV', 'c_UW',
    #                                                   'c_VW']

    # fill in cartesian mean
    try:
        for col_ix, col_name in enumerate(CART_COL_NAMES[:6]):
            gt_row[col_name] = xyzuvw_mean[col_ix]
    except IndexError:
        import pdb; pdb.set_trace()

    # fill in standard deviations
    xyzuvw_stds = np.sqrt(xyzuvw_cov[np.diag_indices(dim)])
    for col_ix, col_name in enumerate(CART_COL_NAMES[6:12]):
        gt_row[col_name] = xyzuvw_stds[col_ix]

    correl_matrix = xyzuvw_cov / xyzuvw_stds / xyzuvw_stds.reshape(6, 1)
    # fill in correlations
    for col_ix, col_name in enumerate(CART_COL_NAMES[12:]):
        gt_row[col_name] = correl_matrix[
            np.triu_indices(dim, k=1)[0][col_ix],
            np.triu_indices(dim, k=1)[1][col_ix]
        ]

def eraseCartesianData(gt_row):
    """
    Replace all cartesian data with blanks (for erasing binary companions that
    have been incoroporated into host star's kinematics)
    """
    dim = 6

    for col_name in CART_COL_NAMES:
        gt_row[col_name] = np.nan

if __name__ == '__main__':
    gagne_filename = '../data/gagne_bonafide_full_kinematics_with_lit' \
                     '_and_best_radial_velocity.fits'
    gt = Table.read(gagne_filename)
    new_gagne_filename = '../data/gagne_bonafide_full_kinematics_with_lit' \
                     '_and_best_radial_velocity_comb_binars.fits'

    # companion_ixs = np.where(gt['Companion'] == 'True')
    # host_ixs = (companion_ixs[0]-1,)

    row_ix = 0
    while row_ix < len(gt['Companion']) - 1:
        # find next instance of host
        system_ixs = []
        while (row_ix+1 < len(gt['Companion']) and
               gt[row_ix+1]['Companion'] == 'False'):
            row_ix += 1
        # Check if we've reached end of table
        if row_ix == len(gt['Companion'])-1:
            break
        system_ixs.append(row_ix)

        # find subsequent associated companions
        row_ix += 1
        while row_ix < len(gt['Companion']) and gt[row_ix]['Companion'] == 'True':
            system_ixs.append(row_ix)
            row_ix += 1

        print(system_ixs)

        masses = []
        means = []
        cov_mats = []
        for row_ix in system_ixs:
            # Check 'U' (for e.g.) has been calculated
            # (requires both RV and parallax so good guide for 6D completeness)
            if np.isfinite(gt[row_ix]['U']):
                masses.append(gt[row_ix]['approx_mass'])
                mean, cov_mat = buildMeanAndCovMatFromRow(gt[row_ix])
                means.append(mean)
                cov_mats.append(cov_mat)

        masses = np.array(masses)
        means = np.array(means)
        cov_mats = np.array(cov_mats)

        nstars = len(means)
        if nstars != 0:
            # establish average of available masses
            av_present_mass = masses[np.where(np.isfinite(masses))].mean()
            # replace everything with 1 if there are no valid masses
            if np.isnan(av_present_mass):
                masses[:] = 1.0
            # replace any missing masses with average of available masses
            else:
                masses[np.where(np.isnan(masses))] = av_present_mass

            assert np.all(np.isfinite(masses))
            total_mass = np.sum(masses)

            aver_mean = (means.T * masses).sum(axis=-1) / total_mass
            aver_cov_mat = (cov_mats.T * masses).sum(axis=-1) / total_mass

            insertCartesainData(gt[system_ixs[0]], aver_mean, aver_cov_mat)

            for system_ix in system_ixs[1:]:
                eraseCartesianData(gt[system_ix])

    for pos_dim in CART_COL_NAMES[:3]:
        gt[pos_dim].unit = 'pc'
    for vel_dim in CART_COL_NAMES[3:6]:
        gt[vel_dim].unit = 'km/s'
    for pos_std in CART_COL_NAMES[6:9]:
        gt[pos_std].unit = 'pc'
    for vel_std in CART_COL_NAMES[9:12]:
        gt[vel_std].unit = 'km/s'
    gt['approx_mass'].unit = 'M_solar'

    gt.write(new_gagne_filename)


from django.urls import path

from . import views

app_name = 'encyclopedia'
urlpatterns = [
    path('', views.index, name='index'),
    path('wiki/<str:title>', views.entry, name='entry'),
    path('search', views.search, name='search'),
    path('new_page', views.new, name='new'),
    path('save', views.save, name='save'),
    path('random_page', views.random_page, name='random_page'),
    path('edit_page/<str:title>', views.edit_page, name='edit_page')
]


from __future__ import print_function
import sys
from operator import add
from pyspark.sql import SparkSession
def toF(strF):
    strNum = strF.split(" ")
    return [int(strNum[0]), int(strNum[1])]
def standardF(f):
    newF = [f[0], 1]
    if (f[1] != 1):
        if (f[1] == 0):
            newF = [0, 1]
        else:
            newF[1] = f[1]
    return newF
def sumF(f1, f2):
    return [f1[0] * f2[1] + f2[0] * f1[1], f1[1] * f2[1]]
if __name__ == "__main__":
    if len(sys.argv) != 2:
        print("Start: run <file>", file=sys.stderr)
        sys.exit(-1)
    spark = SparkSession\
        .builder\
        .appName("Run")\
        .getOrCreate()

    lines = spark.read.text(sys.argv[1]).rdd.map(lambda r: r[0])
    fractions = [standardF(toF(x)) for x in lines]
    sumOfF = [0, 1]
    for f in fractions:
        sumOfF = sumF(sumOfF, f)
    print("%f %f : %f" % (sumOfF[0], sumOfF[1], sumOfF[0] / sumOfF[1]))
    spark.stop()


import os
import struct
import numpy as np
import cv2
import pandas as pd
from sklearn.model_selection import train_test_split
from skimage.transform import resize
from keras.preprocessing.image import ImageDataGenerator
import matplotlib.pyplot as plt

os.chdir("/run/user/1000/gvfs/sftp:host=192.168.84.212/KALpydev/icr_pickle_files/CNN_Data")

#Emnist data
train_img = 'emnist-byclass-train-images-idx3-ubyte'
train_lbl = 'emnist-byclass-train-labels-idx1-ubyte'
test_img = 'emnist-byclass-test-images-idx3-ubyte'
test_lbl = 'emnist-byclass-test-labels-idx1-ubyte'

with open(train_lbl, 'rb') as flbl:
        magic, num = struct.unpack(">II", flbl.read(8))
        train_lbl = np.fromfile(flbl, dtype=np.int8)

with open(train_img, 'rb') as fimg:
	magic, num, rows, cols = struct.unpack(">IIII", fimg.read(16))
	train_img = np.fromfile(fimg, dtype=np.uint8).reshape(len(train_lbl), rows, cols)

with open(test_lbl, 'rb') as flbl:
        magic, num = struct.unpack(">II", flbl.read(8))
        test_lbl = np.fromfile(flbl, dtype=np.int8)

with open(test_img, 'rb') as fimg:
	magic, num, rows, cols = struct.unpack(">IIII", fimg.read(16))
	test_img = np.fromfile(fimg, dtype=np.uint8).reshape(len(test_lbl), rows, cols)

#Change Background color

train_img=np.array(list(map(lambda x:255-np.transpose(x),train_img)))
test_img=np.array(list(map(lambda x:255-np.transpose(x),test_img)))

#0-9 class are numericals

train_img=train_img[train_lbl<=9]
train_lbl=train_lbl[train_lbl<=9]

test_img=test_img[test_lbl<=9]
test_lbl=test_lbl[test_lbl<=9]

def fg1(x):
	x[x>np.mean(x)]=1
	return(x)

size = 3

# generating the kernel
kernel_motion_blur = np.zeros((size, size))
kernel_motion_blur[int((size-1)/2), :] = np.ones(size)
kernel_motion_blur = kernel_motion_blur / size

# applying the kernel to the input image

x_train = np.array(list(map(lambda v:cv2.filter2D(v, -1, kernel_motion_blur),train_img)))
x_test = np.array(list(map(lambda v:cv2.filter2D(v, -1, kernel_motion_blur),test_img)))

y_train=train_lbl
y_test=test_lbl

#Normalization
x_train = np.array(list(map(lambda v:(v-v.min())/(v.max()-v.min()),x_train)))
x_test = np.array(list(map(lambda v:(v-v.min())/(v.max()-v.min()),x_test)))

p=np.array(pd.Series(np.arange(x_train.shape[0])).sample(x_train.shape[0]))
x=x_train[p]
y=y_train[p]

#Choose certain numbers of images for making the set balanced
x1=list()
y1=list()
v=5200
for i in range(10):
	x2=x[y==i]

	if x2.shape[0]>v:
		y2=np.repeat(i,v)
		y1.extend(y2)
		x2=x2[0:v]
	else:

		y2=np.repeat(i,x2.shape[0])
		y1.extend(y2)

	x1.extend(x2)

x1=np.array(x1)
y1=np.array(y1)

x1=np.array(list(map(lambda v:fg1(v),x1)))

x_train,x_test,y_train,y_test = train_test_split(x1,y1, test_size = 0.20, stratify=y1)

#Data Augmentation functon

datagen=ImageDataGenerator(
    rotation_range=1,
    width_shift_range=0.10,
    height_shift_range=0.10,
    fill_mode='reflect')

#One side grid function
def line1(e,c):

	c = cv2.filter2D(c, -1, kernel_motion_blur)
	arr = np.linspace(70,150,48).reshape((24,2))
	u=np.random.shuffle(arr)
	a=np.int(28*e/100)
	c[2:26,a:(a+2)]=arr
	return(c)

#both sides grids function
def line2(c):

	c = cv2.filter2D(c, -1, kernel_motion_blur)
	arr = np.linspace(70,150,48).reshape((24,2))
	u=np.random.shuffle(arr)
	a=np.int(28*15/100)
	c[2:26,a:(a+2)]=arr
	u=np.random.shuffle(arr)
	a=np.int(28*85/100)
	c[2:26,a:(a+2)]=arr
	return(c)

#Data augmentation and grids in the images

def data_aug_line(x,y,v):
	x_test2=list()
	y_test2=list()

	#Grids location in the image
	g=np.array([15,30,45,60,75,85])

	for k in range(10):

		x1=x[y==k]
		y1=y[y==k]
		x_test1=list()
		y_test1=list()
		i = 0
		n_s=int(v*10/100)

		#Data augmentation
		for x_batch,y_batch in datagen.flow(np.reshape(x1,[x1.shape[0],28,28,1]),y1, batch_size=1):
			i += 1
			x_batch=x_batch.reshape(28,28)
			x_test1.append(x_batch)
			y_test1.extend(y_batch)
			if i > np.int(v*50/100)-1:
				break

		x2=x1.copy()
		x3=list()
		#one grid
		kl= list(map(lambda w:x3.extend(list(map(lambda v:line1(w,v),x2[np.array(pd.Series(np.arange(len(x2))).sample(n_s))]))),g))

		#two grids
		kl= x3.extend(list(map(lambda v:line2(v),x2[np.array(pd.Series(np.arange(len(x2))).sample(n_s))])))

		x_test1=np.array(x_test1)
		y_test1=np.array(y_test1)

		#Add Motion blur noise
		x_test1 = np.array(list(map(lambda v:cv2.filter2D(v, -1, kernel_motion_blur),x_test1)))

		x3=np.array(x3)
		y3=np.repeat(k,(len(g)+1)*n_s)

		x1=np.concatenate((x_test1,x3))
		y1=np.concatenate((y_test1,y3))

		#Normalization
		x1=np.array(list(map(lambda v:(v-v.min())/(v.max()-v.min()),x1)))
		x1=np.array(list(map(lambda v:fg1(v),x1)))

		x_test2.extend(x1)
		y_test2.extend(y1)

	x_test2=np.array(x_test2)
	y_test2=np.array(y_test2)

	return(x_test2,y_test2)

#Calling function and Creating train-test set

x_test2,y_test2=data_aug_line(train_img,train_lbl,4160)
x_test3,y_test3=data_aug_line(train_img,train_lbl,1040)

len(x_test2)
plt.imshow(x_test2[49908])
plt.show()
plt.imshow(x_test2[2100])

x_train=np.concatenate((x_train,x_test2))
y_train=np.concatenate((y_train,y_test2))

x_test=np.concatenate((x_test,x_test3))
y_test=np.concatenate((y_test,y_test3))

p=np.array(pd.Series(np.arange(x_train.shape[0])).sample(x_train.shape[0]))

x_train=x_train[p]
y_train=y_train[p]

p=np.array(pd.Series(np.arange(x_test.shape[0])).sample(x_test.shape[0]))

x_test=x_test[p]
y_test=y_test[p]

np.savez("m2.npz",x_train,y_train,x_test,y_test)


from __future__ import absolute_import
from __future__ import unicode_literals

import flask

from git_code_debt.server import logic
from git_code_debt.server import metric_config
from git_code_debt.server.presentation.commit_delta import CommitDeltaPresenter
from git_code_debt.server.presentation.delta import DeltaPresenter
from git_code_debt.server.render_mako import render_template

commit = flask.Blueprint('commit', __name__)

@commit.route('/commit/<sha>')
def show(sha):
    changes = logic.get_metric_changes(flask.g.db, sha)

    commit_deltas = sorted([
        CommitDeltaPresenter.from_data(
            metric_name, DeltaPresenter('javascript:;', change),
        )
        for metric_name, change in changes
    ])

    links = [
        (link_name, link.format(sha=sha))
        for link_name, link in metric_config.commit_links
    ]

    return render_template(
        'commit.mako',
        sha=sha,
        short_sha=sha[:8],
        commit_deltas=commit_deltas,
        links=links,
    )


#!/usr/bin/env python3

# Ref: https://techbase.kde.org/Development/Tutorials/Sensors#Protocol

import subprocess
import xml.etree.ElementTree as ET
import collections

CmdInfo = collections.namedtuple('CmdInfo', ['current', 'min', 'max'])

zero = lambda _: 0

supported_query = {
    'Temperature(C)': CmdInfo(lambda gpu: gpu.find('./temperature/gpu_temp').text.split(' ')[0], zero, zero),
    'Utilization(%)': CmdInfo(lambda gpu: gpu.find('./utilization/gpu_util').text.split(' ')[0], zero, lambda _: 100),
    'Memory_Used(MiB)': CmdInfo(lambda gpu: gpu.find('./fb_memory_usage/used').text.split(' ')[0], zero, lambda gpu: gpu.find('./fb_memory_usage/total').text.split(' ')[0])}

def make_cmd(gpu_id, key):
    return f'{gpu_id}_{key}'

def break_cmd(cmd):
    return cmd.split('_', maxsplit=1)

class GPURenamer:
    def __init__(self):
        self.id2name = {}
        self.name2id = {}

    def name(self, identifier):
        name = self.id2name.get(identifier)
        if name is None:
            name = "GPU%d" % len(self.id2name)
            self.id2name[identifier] = name
            self.name2id[name] = identifier
        return name

    def identifier(self, name):
        return self.name2id[name]

if __name__ == '__main__':
    print('ksysguardd 1.2.0')
    keys = supported_query.keys()
    names_mapped = False
    renamer = GPURenamer()
    while True:
        cmd = input('ksysguardd> ')
        info = ET.fromstring(subprocess.check_output(['nvidia-smi', '-q', '-x']))
        if not names_mapped:
            names_mapped = True
            for gpu in info.findall('gpu'):
                renamer.name(gpu.attrib['id'])
        if cmd == 'monitors':
            for gpu in info.findall('gpu'):
                gpu_id = gpu.attrib['id']
                for key in keys:
                    print(f'{make_cmd(renamer.name(gpu_id), key)}\tfloat')
        else:
            gpu_name, key = break_cmd(cmd)
            gpu_id = renamer.identifier(gpu_name)
            gpu = info.find(f'.//gpu/[@id="{gpu_id}"]')
            if key.endswith('?'):
                cmd_info = supported_query[key[:-1]]
                print(f'{make_cmd(gpu_name, key[:-1])}\t{cmd_info.min(gpu)}\t{cmd_info.max(gpu)}')
            else:
                result = supported_query[key].current(gpu)
                print(result)


from cache_utils2 import cached
from sorl.thumbnail.parsers import parse_geometry

from django import template

from cpi.apps.accounts.models import UserProfilePerInstance, CPIUser

import logging
log = logging.getLogger(__name__)

register = template.Library()

@register.inclusion_tag('accounts/_avatar.html')
@cached(None)
def avatar(player, geometry):
    #log.debug(u"avatar for %s from disk" % player)

    avatar_path = None
    if isinstance(player, UserProfilePerInstance):
        avatar_path = player.get_avatar_url()
        #log.debug(u"avatar url %s" % avatar_path)
    elif isinstance(player, CPIUser):
        try:
            avatar_path = player.avatar.url
        except ValueError:
            pass
    if not avatar_path:
        return {}

    return {
        'avatar_path': avatar_path,
        'geometry': geometry,
        'geometry_width': parse_geometry(geometry)[0],
        'geometry_height': parse_geometry(geometry)[1],
    }


# -*- coding: utf-8 -*-
# License, author and contributors information in:
# __openerp__.py file at the root folder of this module.
from openerp import api, models, fields
import logging
_log = logging.getLogger(__name__)

class ResPartner(models.Model):
    _inherit = 'res.partner'

    root_partner_id = fields.Many2one(
        comodel_name='res.partner',
        compute='_compute_root_partner_id',
        store=True,
        string='Root partner')

    @api.one
    @api.depends('parent_id')
    def _compute_root_partner_id(self):
        if self.is_company:
            self.root_partner_id = self.id
        else:
            partner = self
            if partner.parent_id:
                while partner.parent_id:
                    partner = partner.parent_id
                    if partner.is_company:
                        self.root_partner_id = partner.id
            else:
                self.root_partner_id = partner.id


#!/usr/bin/env python
import sys
import struct
import re
import argparse

def hex2bytes(text):
    m_iter = re.finditer(r'(?P<hex>((?<=[\s])[0-9a-fA-F]{2}|^[0-9a-fA-F]{2}))(?:$|\s)',
                        text,
                        re.MULTILINE)
    bytes = [int(m.group('hex'), 16) for m in m_iter]
    return bytes

def bytes2bin(bytes):
    bin = "".join([struct.pack('B', x) for x in bytes])
    return bin

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description="Convert hex in text representation into real binary.")
    args = parser.parse_args()

    input = sys.stdin.read()
    bytes = hex2bytes(input)
    bin = bytes2bin(bytes)
    sys.stdout.write(bin)


# Convolutional Neural Network

# Installing Theano
# pip install --upgrade --no-deps git+git://github.com/Theano/Theano.git

# Installing Tensorflow
# pip install tensorflow

# Installing Keras
# pip install --upgrade keras

# Part 1 - Building the CNN

# Importing the Keras libraries and packages
from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense

# Initialising the CNN
classifier = Sequential()

# Step 1 - Convolution
classifier.add(Conv2D(32, (3, 3), input_shape = (64, 64, 3), activation = 'relu'))

# Step 2 - Pooling
classifier.add(MaxPooling2D(pool_size = (2, 2)))

# Adding a second convolutional layer
classifier.add(Conv2D(32, (3, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))

# Step 3 - Flattening
classifier.add(Flatten())

# Step 4 - Full connection
classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dense(units = 1, activation = 'sigmoid'))

# Compiling the CNN
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])

# Part 2 - Fitting the CNN to the images

from keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(rescale = 1./255,
                                   shear_range = 0.2,
                                   zoom_range = 0.2,
                                   horizontal_flip = True)

test_datagen = ImageDataGenerator(rescale = 1./255)

training_set = train_datagen.flow_from_directory('dataset/training_set',
                                                 target_size = (64, 64),
                                                 batch_size = 32,
                                                 class_mode = 'binary')

test_set = test_datagen.flow_from_directory('dataset/test_set',
                                            target_size = (64, 64),
                                            batch_size = 32,
                                            class_mode = 'binary')

# CheckPoints
from keras.callbacks import ModelCheckpoint
#checkpointer = ModelCheckpoint(filepath="tmp/weights.hdf5", verbose=1, save_best_only=True)

# checkpoint
#filepath="tmp/weights-improvement-{epoch:02d}-{val_acc:.2f}.hdf5"
#checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
#callbacks_list = [checkpoint]

# best checkpoint
filepath="tmp/weights.best.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
callbacks_list = [checkpoint]

classifier.fit_generator(training_set,
                         steps_per_epoch = 8000,
                         epochs = 25,
                         validation_data = test_set,
                         validation_steps = 2000,
                         callbacks=callbacks_list)

# --------------------------
## Try to save the Model weights which can be called later
# serialize model to JSON

model_json = classifier.to_json()
with open("cnn_model.json", "w") as json_file:
    json_file.write(model_json)

# serialize weights to HDF5

classifier.save_weights("cnn_model.h5")
print("Saved model to disk")
# --------------------------
#classifer.save('One_convolutional_layer.h5')

# Part 3 - Making new predictions
#==============================================================================
#
# import numpy as np
# from keras.preprocessing import image
# test_image = image.load_img('dataset/single_prediction/cat_or_dog_1.jpg', target_size = (64, 64))
# test_image = image.img_to_array(test_image)
# test_image = np.expand_dims(test_image, axis = 0)  # 4th dim becoz it predict method requires a batch
# result = classifier.predict(test_image)
# training_set.class_indices
# if result[0][0] == 1:
#     prediction = 'dog'
# else:
#     prediction = 'cat'
#==============================================================================


from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait  # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC  # available since 2.26.0
from selenium.common.exceptions import NoSuchElementException  # for exception handling
from selenium.webdriver.common.by import By
import time
import re
import psycopg2  # for CRUD to DB
import os
from switcher import *  # name to code

# login
def login(driver):
    inputElement = driver.find_element_by_name("userID")
    inputElement.send_keys(os.environ['crawler_id'])
    inputElement = driver.find_element_by_name("userPW")
    inputElement.send_keys(os.environ['crawler_pw'])
    inputElement = driver.find_element_by_name("userPW")
    #  submit() didn't working
    # inputElement.submit()
    submit = driver.find_element_by_xpath('//*[@id="myiweb"]/div[1]/div/div[2]/div[3]/div[1]/div/div[2]/div[1]/form/p[1]/input')
    submit.click()

    WebDriverWait(driver, 15).until(EC.alert_is_present())  # alert 나올 시간 기다리기
    accept_alert(driver)

# accept alert when attempt to login
def accept_alert(driver):
    try:
        WebDriverWait(driver, 10).until(EC.alert_is_present())
        alert = driver.switch_to_alert()
        alert.accept()
        print("alert accepted")
    except:
        print("no alert")

# Connect to DB & Insert to DB
def insert_to_DB(data):
    try:
        connect_str="dbname='{}' user='{}' host='{}' password='{}'".format(
            os.environ['MJUTT_NAME'],
            os.environ['MJUTT_USER'],
            os.environ['MJUTT_HOST'],
            os.environ['MJUTT_PW']
        )
        conn = psycopg2.connect(connect_str)
        # print("Insert to DATABASE!")
    except psycopg2.Error as e:
        print("Unable to connect to the database")
        print(e)

    cur = conn.cursor()
    SQL = "INSERT INTO classitem (grade, classname, krcode, credit, timeperweek, prof, classcode, limitstud, " \
          "time, note, dept) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);"
    # testcode
    # print("DB 저장하는 query")
    # print(data)
    cur.execute(SQL, data)
    conn.commit()

# Resize to crawl
def resize_page(driver):
    WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, "crownix-toolbar-ratio")))
    fullscreen = driver.find_element_by_xpath("//div[@class='crownix-toolbar-icon-color']/a")
    fullscreen.click()
    WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, "crownix-toolbar-height")))
    align_vertical = driver.find_element_by_id("crownix-toolbar-height")
    align_vertical.click()

def create_item():
    seq = ['grade', 'classname', 'krcode', 'credit', 'timeperweek', 'classcode', 'limit', 'dept']
    addlist = {'prof': [], 'time': [], 'note': []}

    dictionary = dict.fromkeys(seq, '')  # create dict and init with ''
    dictionary.update(addlist)  # add list
    return dictionary

def init_height():
    seq = ['limit', 'note']
    dictionary = dict.fromkeys(seq, '')
    return dictionary

def item_to_list(item, dept):
    item['dept'] = dept # 과 정보는 db에 넣기 전 여기서 넣고 바로 list로 보냄
    itemlist = [item['grade'], item['classname'], item['krcode'], item['credit'],
                item['timeperweek'], "".join(item['prof']), item['classcode'],
                item['limit'], ", ".join(item['time']), "".join(item['note']),
                "".join(item['dept'])]

    return itemlist

# Global variables (type : Dict.)
item = create_item()
page_lastitem = create_item()
item_height = init_height()

# page_lastitem랑 첫 시작일 때 code랑  비교하기 위해서 따로 저장

def crawling_page(driver, dept, end=None):
    global item, item_height, page_lastitem

    resize_page(driver)
    WebDriverWait(driver, 10).until(EC.visibility_of_element_located(
        (By.ID, 'm2soft-crownix-text')))  # visibility_of_element_located(locator), locator is tuple of (by, path)
    context = driver.find_elements_by_xpath("//div[@id='m2soft-crownix-text']/div")
    option = {'append_on': '0', 'prev': '', 'note_stack': 0, 'note_init': False}
    # option은 한 페이지 긁을 동안 살아있는 value

    for i in range(len(context)):
        attr = context[i].get_attribute("style")
        x_pos = re.search('(left:) (\d{2,3})', attr)
        # left(x좌표)의 position(px)를 가져오는 정규식, position은 2자리 or 3자리 수
        y_pos = re.search('(top:) (\d{3,4})', attr)
        # top(y좌표)의 position(px)를 가져오는 정규식, position은 3자리 or 4자리 수

        pos = (x_pos.groups()[1], y_pos.groups()[1])  # position tuple (x,y)

        text = context[i].text

        if text in ('년', '학년', '교과목명', '한글코드', '학점', '시간', '담당교수', '강좌번호', '제한인원', '시간(강의실)', '비  고'):
            # 표의 index 제거,'년' 삭제 , 아래 코드에서 '전학'-> '전학년'
            continue
        elif pos[0] in ('259', '347', '584'):
            # '강의시간표', 출력일자, pagenum 제거
            continue
        elif pos[0] == "34":
            if text in ('전학년', '1학년', '2학년', '3학년', '4학년'):
                fill_item(driver, dept, pos, text, option)
            elif text == '전학':
                text = "전학년"
                fill_item(driver, dept, pos, text, option)
            else:
                # '2017 학년도 1학기   컴퓨터공학과' 같은 것 제거
                continue
        else:
            fill_item(driver, dept, pos, text, option)

        if(i == len(context)-5):
            # page last elem (end of for loop)
            # excepted last 4 elem is not in table row
            page_lastitem = item

    # 마지막 페이지 마지막 item 일 경우 : Insert to  Database
    if end:
        insert_to_DB(item_to_list(item, dept))
        # item = create_item()  # for Initialize when 자캠 - > 인캠

    # init
    item = create_item()  # init when page finished
    page_lastitem = create_item() # init when page finished
    item_height = init_height()

def compare_stack(height, option):
    if item_height['note'] != '':
        # item height가 비어있지 않으면.. (note 한번도 이상 갔으면)

        # classcode랑 같거나 아주 조금 차이나게 있을 경우 (그런경우는 아직 못봄 안전빵)
        if abs(item_height['note'] - height) <= 5:
            return
        elif item_height['note'] > height:
            # classcode가 더 위에 있을 경우
            option['note_stack'] += 1
        # classcode 가 더 밑에 있을 경우
        elif item_height['note'] < height:
            option['note_stack'] -= 1

        if option['note_stack'] == 0:
            option['note_init'] = True  # intsert 후 note 비워주도록 하는 flag

        # print(item['classname'] + " / " + ''.join(item['prof']) + " / " + item['classcode'])
        # print(option['note_stack'])
        # print(item['prof'])

def fill_item(driver, dept, pos, text, option):
    global item, item_height, page_lastitem
    section = switch_to_classname(pos[0])  # covert css location to string key
    height = int(pos[1])  # y 좌표 int 형으로 가지고 있어야 비교 가능

    if section in ('grade', 'classname', 'krcode'):
        # 페이지 넘어온 경우 prev 가 없으므로 print 못하고 그냥 item 덮어씌움

        if option['prev'] in ('time', 'note'):
            insert_to_DB(item_to_list(item, dept))
            item['prof'] = [] # init prof
            if option['note_init'] == True:
                item['note'] = []  # 노트를 비워줌
                item_height['note'] = ''  # note 위치도 초기화
                option['note_init'] = False

            item[section] = text
            option['prev'] = section
        else:
            # linear 하게 넣는 경우
            item[section] = text
            option['prev'] = section

    elif section == 'prof':

        if option['prev'] == 'prof':
            item['prof'].append(text)

        elif option['prev'] in ('time', 'note'):
            insert_to_DB(item_to_list(item, dept))
            item['prof'] =[]
            if option['note_init'] == True:
                item['note'] = []  # 노트를 비워줌
                item_height['note'] = ''  # note 위치도 초기화
                option['note_init'] = False

            item['prof'].append(text)
            option['prev'] = section

        else:
            # linear 하게 넣는 경우
            item['prof'].append(text)
            option['prev'] = section

    elif section == 'classcode':

        if text == page_lastitem['classcode']:
            # 이전 페이지의 마지막 item과 현재 item의 class code가 같은 경우
            # append_on 켜주고 시간에서 append
            item_height = init_height()  # new page can't compare to old page height
            option['append_on'] = '2'
            option['prev'] = section

        elif page_lastitem['classcode'] != "" and page_lastitem['classcode'] != text:
            # 처음 page last item이 비어있는 상태가 아닌 차 있을 때
            # 마지막 code, 현재 code가 다른 경우
            # 마지막 item 은 그대로 출력해주고 현재 item은 그대로 저장해나가면 됨.
            insert_to_DB(item_to_list(page_lastitem, dept))

            if option['note_init'] == True:
                item['note'] = []  # 노트를 비워줌
                option['note_init'] = False

            page_lastitem = create_item()  # init
            item_height = init_height()  # init
            item[section] = text
            option['prev'] = section

        elif option['prev'] in ('time', 'note'):
            # 한 교수가 한 과목에서 두개 이상의 강좌 할 경우
            # 이미 크롤러가 note에 대한 정보를 item에 담고 있음
            insert_to_DB(item_to_list(item, dept))
            if option['note_init'] == True:
                item['note'] = []  # 노트를 비워줌
                item_height['note'] = ''  # note 위치도 초기화
                option['note_init'] = False

            item[section] = text
            option['prev'] = section

        else:
            item[section] = text
            option['prev'] = section

    elif section == 'limit':
        item_height['limit'] = height
        # 같은 item 으로 처리하고 시간만 추가해줘야 함. append_on 을 켜서 time 저장할 때 같은 item으로 저장하게함
        if option['prev'] in ('time', 'note'):
            compare_stack(height, option)
            option['append_on'] = '1'
            option['prev'] = section

        else:
            # linear 하게 넣는 경우
            compare_stack(height, option)
            item[section] = text
            option['prev'] = section

    elif section == 'time':
        if option['append_on'] == '1':
            # 같은 item 내에서 시간을 추가하는 경우
            item['time'].append(text)
            option['append_on'] = '0'
            option['prev'] = section
        elif option['append_on'] == '2':
            # 이전 item에 append 시켜주자!
            # note 는 time처럼 append 시키면안되므로 비워줌
            page_lastitem['time'].append(text)
            item = page_lastitem
            item['note'] = []
            option['append_on'] = '0'
            page_lastitem = create_item()  # 초기화 해야 elif section == 'classcode' 에서 오류 안남
            option['prev'] = section
        else:
            # 비어있는 item['time'] 에 시간을 넣는 경우
            item['time'] = []  # 이전에 담긴 time을 지워주고!
            item['time'].append(text)  # append
            option['prev'] = section

    elif section == 'note':
        # 'note' 섹션은 같은 note끼리라도 크롤러에서 한번 지나가는걸 명심하자 헷갈리기 쉽다. 그 경우에 대한 분기다.

        if option['prev'] == 'note':
            # note가 두 줄 이상인 경우
            item['note'].append(text)

            tmp = item_height['note']
            item_height['note'] = (height + int(tmp)) / 2.0  # 비고가 두줄일 떄 중간값으로! 아직 3줄 이상인경우는 생각안했다.

            if abs(round(float(item_height['note'])) - int(item_height['limit'])) <= 5:
                # 한 개의 limit 과 두줄짜리 note 인 경우
                # 중간값과 classcode의 높이가 정확하게 일치하지 않는 경우가 있어서 이렇게. (1~2픽셀정도 차이난다.)
                # 이 경우 limit에 일치하는 note가 하나이므로, stack 삭제 후 그냥 pass!
                # 혹시몰라서 절댓값 abs() 를 이용했다
                # float을 통해 str -> float으로 해주고
                # round는 item_height가 소숫점 값이니까 반올림 시켜서 정수로 만들어주기 위해 했음
                option['note_stack'] -= 1
                option['note_init'] = True

        else:
            # 노트 첫번째 줄 진입
            item_height['note'] = height  # item에 위치 저장

            if abs(round(float(item_height['note'])) - int(item_height['limit'])) <= 2:
                # 한 개의 limit 과 한줄짜리 note 일 경우
                # 이런 경우에도 px가 안맞고 1~2 차이나는 경우가 있어서 이렇게 해줌..
                option['note_init'] = True
            else:  # 한 개의 limit 과 두줄 짜리 note 인 경우 or limit이 여러개 있어 note와 높이 차이가 나는 경우
                option['note_stack'] += 1

            item['note'].append(text)
            option['prev'] = section

            # 한줄짜리 note 도 한개 인 경우 출력 후 note 비워주어야 함

            # 이렇게 note에서 item_height 를 구성해두고 이제 classcode에서 매번 item_height['note'] 와 비교해서
            # stack I/O 하는것이 목적

    else:
        item[section] = text
        option['prev'] = section

def check_empty(driver):
    try:
        # 비어있는 페이지일 경우 viewer 안에 빈 페이지임을 알려주는 inneralert 이 생성됨
        inneralert = driver.find_element_by_xpath("//div[@class='aButtons']/button")
    except NoSuchElementException as e:
        # print(str(e))
        print("정상적인 페이지 입니다. 크롤링 하러 고고")
        return
    else:
        inneralert.click()
        print("비어있는 페이지 입니다.")
        return 'EXIT'

# Crawl Timetable
def crawl_timetable(driver, send_year, semester, dept):
    # Set table
    print("{} / {} / {} ".format(send_year, semester, switch_to_deptname(dept)))
    driver.get("https://myiweb.mju.ac.kr/servlet/MyLocationPage?link=/su/sue/sue01/w_sue337pr.jsp")
    year = driver.find_element_by_name("year")
    year.send_keys(send_year)
    Select(driver.find_element_by_name("smt")).select_by_value(semester)
    Select(driver.find_element_by_name("dept_cd")).select_by_value(dept)

    # Search!
    driver.execute_script("thisPage1()")
    WebDriverWait(driver, 10).until(EC.invisibility_of_element_located((By.ID, 'progress_mask')))
    # progress mask 는 로딩중일떄만 생성되는 element 이게 사라지는 순간까지 wait

    # 없는 페이지인지 확인
    if 'EXIT' == check_empty(driver):
        return

    # Find end page
    resize_page(driver)
    find_end = driver.find_element_by_class_name("crownix-pagenum")
    endpage = int(find_end.text.split(" ")[2])
    # Crawling!start
    for i in range(endpage):

        # 특정 page만 디버깅 해볼 경우
        # if i in (11,12,13):
        #     crawling_page(driver, dept)
        # else:
        #     next_page = driver.find_element_by_xpath('//li[@id="crownix-toolbar-next"]/a')
        #     next_page.click()

        if i == endpage - 1:
            crawling_page(driver, dept, True) # set end page option
        else:
            crawling_page(driver, dept)
            # Go to the next page
            next_page = driver.find_element_by_xpath('//li[@id="crownix-toolbar-next"]/a')
            next_page.click()

# logout
def logout(driver):
    driver.get("https://myiweb.mju.ac.kr")
    WebDriverWait(driver, 10).until(EC.alert_is_present())  # alert 나올 시간 기다리기
    accept_alert(driver)

    # logout javascript function located in "Top" frame
    driver.switch_to.frame("Top")
    driver.execute_script("logOut()")
    time.sleep(2)
    return driver.quit()

if __name__ == "__main__":
    st = time.time()
    # set driver (default : Chrome), firefox need geckodriver
    driver = webdriver.Chrome()
    # driver = webdriver.Firefox()
    driver.get("http://myiweb.mju.ac.kr")
    login(driver)

    deptlist = ['10000', '20000', '12000', '16600', '11910', '11915', '11920',
                '11940', '11960', '12250', '12361', '12371', '12450', '12500',
                '12701', '12755', '12901', '12900', '12908', '12913', '13725',
                '13730', '13735', '13750', '13781', '13793', '13794', '18012',
                '18013', '18014', '18016', '14120', '14130', '14140', '14150',
                '16445', '16450', '14190', '14200', '14210', '14212', '16450',
                '14240', '14250', '16610', '16410', '16420', '16425', '16440',
                '16640', '16650', '16660', '16810', '18510', '18520']

    # 제외명단 ( 2017. 02 기준 없는학과)
    # 16400 : 사회과학대학, 12370: 환경생명공학과 , 12611: 컴퓨터소프트웨어학과, 12831: 정보공학과
    # 12832 : 통신공학과, 13720: 의상디자인학과, 18015: 공간디자인학과, 16615:경영학부 경영학전공
    # 16620 : 경영학부 경영정보전공, 16430: 북한학과

    # for test
    # crawl_timetable(driver,"2017","10","12913")

    for i in deptlist:
        crawl_timetable(driver,"2017","10",i)

    print(" Crawling completed. ")
    logout(driver)
    print("running time : {}".format(time.time() - st))


"""HassOS Cli docker object."""
import logging

import docker

from ..coresys import CoreSysAttributes
from .interface import DockerInterface

_LOGGER: logging.Logger = logging.getLogger(__name__)

class DockerHassOSCli(DockerInterface, CoreSysAttributes):
    """Docker Supervisor wrapper for HassOS Cli."""

    @property
    def image(self):
        """Return name of HassOS CLI image."""
        return f"homeassistant/{self.sys_arch.supervisor}-hassio-cli"

    def _stop(self, remove_container=True):
        """Don't need stop."""
        return True

    def _attach(self, tag: str):
        """Attach to running Docker container.
        Need run inside executor.
        """
        try:
            image = self.sys_docker.images.get(f"{self.image}:{tag}")

        except docker.errors.DockerException:
            _LOGGER.warning("Can't find a HassOS CLI %s", self.image)

        else:
            self._meta = image.attrs
            _LOGGER.info(
                "Found HassOS CLI %s with version %s", self.image, self.version
            )


from scipy.optimize import curve_fit
import numpy as np
import matplotlib.pyplot as plt
import csv
exp=lambda x,a,mu,sigma,y0:y0+a*np.exp(-1/2/sigma**2*(x-mu)**2)
def max_finder(y):
    z=[]
    for i in range(10,len(y)-10):
        if(y[i]>=y[i+1] and y[i]>0.01 and y[i]>=y[i-1] and y[i]>=y[i+2] and y[i]>=y[i-2] and y[i]>=y[i+3] and y[i]>=y[i-3] and y[i]>=y[i+4] and y[i]>=y[i-4]): z.append(i)
    return z
f=open("NewFile1.csv")

data=csv.reader(f)
t=[]
U=[]
for row in data:
    t.append(row[0])
    U.append(row[1])
t=t[2::1]
U=U[2::1]
for i in range(len(U)):
    U[i]=float(U[i])
    t[i]=float(t[i])
plt.xlim(-0.05,0.04)
z=max_finder(U)
Uneu=[]
tneu=[]
for i in range(len(z)):
    Uneu.append(U[z[i]])
    tneu.append(t[z[i]])
werte,fehler=curve_fit(exp,tneu,Uneu)

plt.plot(t,U,label="Messwerte")
plt.plot(t,exp(t,*werte),color="red",label="Gauß-Fit \n$y_0+A*e^\\frac{-(x-\mu)^2}{2*\sigma^2}$"+"\n$\mu ={:.3f}$\n$\sigma ={:.3f}$\n$y_0={:.3f}$\n$A={:.3f}$".format(werte[1],werte[2],werte[3],werte[0]))
plt.legend()
plt.plot(0,0.15)
plt.title("Diagramm 2: Signal bei Interferenz")
plt.xlabel("Zeit $t [s]$")
plt.ylabel("CH 1 $[Volt]$")
plt.savefig("liebsteDiagramm 2.jpg")
print(werte[2])
print(fehler[2][2])

#plt.scatter(x_werte,y_werte)

plt.show()


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

import argparse
import datetime
import logging
import requests

from bs4 import BeautifulSoup
from gnucash import Session, GncNumeric
from gnucash_patch import GncPrice

def get_quote_onvista_bond(isin):
    url = 'http://www.onvista.de/anleihen/snapshot.html?ISIN={}'.format(isin)
    r = requests.get(url)
    soup = BeautifulSoup(r.text)
    price = soup.select('.INHALT #KURSINFORMATIONEN ~ .t span:nth-of-type(2)')[0].get_text()
    currency = 'EUR'
    logging.info('Got quote for %s: %s%%', isin, price)
    return GncNumeric(int(price.replace(',', '')), 100 * 100), currency

def get_quote_onvista_stock(isin):
    url = 'http://www.onvista.de/suche/?searchValue={}'.format(isin)
    r = requests.get(url)
    soup = BeautifulSoup(r.text)
    spans = soup.select('.INHALT ul.KURSDATEN li:nth-of-type(1) span')
    price = spans[0].get_text()
    currency = str(spans[1].get_text())
    logging.info('Got quote for %s: %s %s', isin, price, currency)
    return GncNumeric(int(price.replace(',', '')), 1000), currency

def update_quote(commodity, book):
    fullname = commodity.get_fullname()
    mnemonic = commodity.get_mnemonic()
    isin = commodity.get_cusip()
    if len(isin) != 12:
        return
    logging.info('Processing %s (%s, %s)..', fullname, mnemonic, isin)
    value, currency = None, None
    try:
        if name == 'BOND':
            value, currency = get_quote_onvista_bond(isin)
        else:
            value, currency = get_quote_onvista_stock(isin)
    except:
        logging.exception('Failed to get quote for %s', isin)
    if value and currency:
        table = book.get_table()
        gnc_currency = table.lookup('ISO4217', currency)
        p = GncPrice(book)
        p.set_time(datetime.datetime.now())
        p.set_commodity(commodity)
        p.set_currency(gnc_currency)
        p.set_value(value)
        book.get_price_db().add_price(p)

parser = argparse.ArgumentParser()
parser.add_argument('gnucash_file')
parser.add_argument('--dry-run', action='store_true', help='Do not write anything, noop-mode')

args = parser.parse_args()

logging.basicConfig(level=logging.INFO)
logging.getLogger('urllib3').setLevel(logging.WARN)

s = Session(args.gnucash_file)
try:
    book = s.book
    table = book.get_table()
    for namespace in table.get_namespaces_list():
        name = namespace.get_name()
        if name != 'CURRENCY':
            for commodity in namespace.get_commodity_list():
                update_quote(commodity, book)
    if not args.dry_run:
        s.save()
finally:
    s.end()


from flask import Flask
from app.register import register
from settings import Config

def create_app():
    app = Flask(__name__)
    app.config.from_object(Config)
    register_extensions(app)
    register(app)
    return app

def register_extensions(app):
    from app.extensions import db, migrate
    db.init_app(app)
    migrate.init_app(app, db)

app = create_app()


import jinja2
import openpyxl
import os
import string
import sys

from ntmap import *

# Get the path of this file
dir_path = os.path.dirname(os.path.realpath(__file__))

# Load the Jinja2 template
loader = jinja2.FileSystemLoader(dir_path)
environment = jinja2.Environment(loader=loader)
controls_template = environment.get_template('security_control.j2')
crm_template = environment.get_template('crm.j2')

# Open the SCTM Excel workbook rw so we can look at row dimensions.
wb = openpyxl.load_workbook(sys.argv[1], read_only=False)
ws = wb['Matrix']

# Build the column index
column_index = {}
for row in ws.iter_rows(min_row=1, max_row=1):
    for i, cell in enumerate(row):
        column_index[i] = cell.value

# Init the sctm dict, to be keyed to control title
sctm = {}

# Iterate over every row in the sheet, starting at the 2nd row
for i, row in enumerate(ws.iter_rows(min_row=2), start=2):
    # Skip hidden rows
    if ws.row_dimensions[i].hidden is True:
        continue

    # Temporary dict for this iteration
    r = {}

    # Iterate over the cells and insert the values into the temporary dict r
    # keyed with the column name.
    for j, cell in enumerate(row):
        # If the cell is non-blank, insert the cell's contents into the temp
        # dict r keyed by column name.
        if cell.value and cell.value != '':
            r[column_index[j]] = cell.value
        else:
            r[column_index[j]] = 'undefined'

    # Key for the temp dict
    ref_num = r['NIST Ref #']

    # Build the control title with nice capitalization
    r["Title"] = " ".join(w.capitalize() for w in str(nt_map[ref_num]).split())

    # First we check to see if this control has been processed before
    # Remove all those pesky new lines
    r['Requirements'] = r['Requirements'].replace(u"\n", u" ")
    if sctm.get(ref_num):
        # If it has, we concat the control language
        sctm[ref_num][0]['Original Requirements'] += (u" " + r['Requirements'])
    else:
        # Otherwise we start fresh
        r['Original Requirements'] = r['Requirements']

    # Build an alphabetic part number to jive with the FedRAMP template
    r['Part'] = string.lowercase[int(r['Org Ref #'].split("_")[-1])]

    # Now we build a list of control parts and store it under the same key
    if sctm.get(ref_num):
        sctm[ref_num].append(r)
    else:
        sctm[ref_num] = [r]

# Process the master sctm data structure and look for Tenant responsible
# controls
crm = {}
for key in sctm.keys():
    for part in sctm[key]:
        if u"OpenShift Tenant" in part['Role']:
            crm[key] = sctm[key]

output = controls_template.render(sctm=sctm)
with open('controls.rst', 'w') as controls_file:
    controls_file.write(output.encode('utf-8'))

output = crm_template.render(sctm=crm)
with open('crm.rst', 'w') as controls_file:
    controls_file.write(output.encode('utf-8'))


import argparse
import json
import os
import sys
import time

import numpy as np
import cv2
import glob

# Root directory of the project
from mrcnn.visualize import random_colors, apply_mask

ROOT_DIR = os.path.abspath("../")

# Import Mask RCNN
sys.path.append(ROOT_DIR)  # To find local version of the library
from mrcnn import utils
import mrcnn.model as modellib

# Import COCO config
sys.path.append(os.path.join(ROOT_DIR, "samples/coco/"))  # To find local version
import coco

# %matplotlib inline

# Directory to save logs and trained model
MODEL_DIR = os.path.join(ROOT_DIR, "logs")

# Local path to trained weights file
COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")
# Download COCO trained weights from Releases if needed
if not os.path.exists(COCO_MODEL_PATH):
    utils.download_trained_weights(COCO_MODEL_PATH)

# Directory of images to run detection on
IMAGE_DIR = os.path.join(ROOT_DIR, "images")

class InferenceConfig(coco.CocoConfig):
    # Set batch size to 1 since we'll be running inference on
    # one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU
    GPU_COUNT = 1
    IMAGES_PER_GPU = 1

config = InferenceConfig()
config.display()

# Create model object in inference mode.
model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config)

# Load weights trained on MS-COCO
model.load_weights(COCO_MODEL_PATH, by_name=True)

# COCO Class names
# Index of the class in the list is its ID. For example, to get ID of
# the teddy bear class, use: class_names.index('teddy bear')
class_names = ['BG', 'person', 'bicycle', 'car', 'motorcycle', 'airplane',
               'bus', 'train', 'truck', 'boat', 'traffic light',
               'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird',
               'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear',
               'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie',
               'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball',
               'kite', 'baseball bat', 'baseball glove', 'skateboard',
               'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup',
               'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',
               'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza',
               'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed',
               'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote',
               'keyboard', 'cell phone', 'microwave', 'oven', 'toaster',
               'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors',
               'teddy bear', 'hair drier', 'toothbrush']

def display_instances(image, boxes, masks, ids, names, scores):
    """
        take the image and results and apply the mask, box, and Label
    """
    n_instances = boxes.shape[0]
    colors = random_colors(n_instances)
    resultsForJSON = []

    if not n_instances:
        print('NO INSTANCES TO DISPLAY')
    else:
        assert boxes.shape[0] == masks.shape[-1] == ids.shape[0]

    for i, color in enumerate(colors):
        if not np.any(boxes[i]):
            continue

        y1, x1, y2, x2 = boxes[i]
        label = names[ids[i]]

        score = scores[i] if scores is not None else None

        caption = '{} {:.2f}'.format(label, score) if score else label

        mask = masks[:, :, i]

        # image = apply_mask(image, mask, color)
        image = cv2.rectangle(image, (x1, y1), (x2, y2), color, 2)
        image = cv2.putText(
            image, caption, (x1, y1), cv2.FONT_HERSHEY_COMPLEX, 0.7, color, 2
        )

        resultsForJSON.append(
            {"label": label, "confidence": float('%.2f' % score), "topleft": {"x": int(x1), "y": int(y1)},
             "bottomright": {"x": int(x2), "y": int(y2)}})

    return image, json.dumps(resultsForJSON)

def saveLabels(elapsed, json):
    if not os.path.exists(json_save_dir):
        os.makedirs(json_save_dir)
    with open(json_save_dir + '/frame' + str(elapsed - 1) + '.json', 'w+') as f:
        f.write(json)

def saveTime(endTime):
    data = {}
    data['time'] = []
    data['time'].append({'time': endTime})

    if not os.path.exists(time_path):
        os.makedirs(time_path)
    with open(time_path + '/time' + '.json', 'w+') as f:
        json.dump(data['time'], f)

def make_video(outvid, images=None, fps=30, size=None,
               is_color=True, format="FMP4"):
    """
    Create a video from a list of images.

    @param      outvid      output video
    @param      images      list of images to use in the video
    @param      fps         frame per second
    @param      size        size of each frame
    @param      is_color    color
    @param      format      see http://www.fourcc.org/codecs.php
    @return                 see http://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_gui/py_video_display/py_video_display.html

    The function relies on http://opencv-python-tutroals.readthedocs.org/en/latest/.
    By default, the video will have the size of the first image.
    It will resize every image to this size before adding them to the video.
    """

    fourcc = cv2.VideoWriter_fourcc(*format)
    vid = None
    for image in images:
        if not os.path.exists(image):
            raise FileNotFoundError(image)
        img = cv2.imread(image)
        if vid is None:
            if size is None:
                size = img.shape[1], img.shape[0]
            vid = cv2.VideoWriter(outvid, fourcc, float(fps), size, is_color)
        if size[0] != img.shape[1] and size[1] != img.shape[0]:
            img = cv2.resize(img, size)
        vid.write(img)
    vid.release()
    return vid

def parse_arguments():
    parser = argparse.ArgumentParser()
    parser.add_argument("video_name", help="Set video name", type=str)
    parser.add_argument("format", help="Set video format", type=str)
    args = parser.parse_args()
    return args

if __name__ == "__main__":
    args = parse_arguments()
    video_name = args.__dict__['video_name']
    format = args.__dict__['format']

    video_save_dir = '/Users/filipdabrowski/Documents/git/Comparer/mask_RCNN/' + video_name
    video_dir = '/Users/filipdabrowski/Documents/video/' + video_name
    json_save_dir = video_save_dir + '/boxes'
    time_path = video_save_dir + '/time'

    batch_size = 1
    capture = cv2.VideoCapture(video_dir + '.' + format)
    try:
        if not os.path.exists(video_save_dir):
            os.makedirs(video_save_dir)
    except OSError:
        print('Error: Creating directory of data')
    frames = []
    frame_count = 0
    # these 2 lines can be removed if you dont have a 1080p camera.
    capture.set(cv2.CAP_PROP_FRAME_WIDTH, int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)))
    capture.set(cv2.CAP_PROP_FRAME_HEIGHT, int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT)))
    fps = capture.get(cv2.CAP_PROP_FPS)

    start = time.time()
    while True:
        ret, frame = capture.read()
        # Bail out when the video file ends
        if not ret:
            break

        # Save each frame of the video to a list
        frame_count += 1
        frames.append(frame)
        print('frame_count :{0}'.format(frame_count))
        if len(frames) == batch_size:
            results = model.detect(frames, verbose=0)
            print('Predicted')
            for i, item in enumerate(zip(frames, results)):
                frame = item[0]
                r = item[1]
                frame, jsonContent = display_instances(
                    frame, r['rois'], r['masks'], r['class_ids'], class_names, r['scores']
                )
                saveLabels(frame_count, jsonContent)
                name = 'frame{0}.jpg'.format(frame_count + i - batch_size)
                name = os.path.join(video_save_dir, name)
                cv2.imwrite(name, frame)
                print('writing to file:{0}'.format(name))
            # Clear the frames array to start the next batch
            frames = []

    capture.release()
    endTime = time.time() - start
    saveTime(endTime)
    # Directory of images to run detection on

    images = list(glob.iglob(os.path.join(video_save_dir, '*.*')))
    # Sort the images by integer index
    images = sorted(images, key=lambda x: float(os.path.split(x)[1][:-3]))

    outvid = os.path.join('', "out.mp4")
    make_video(outvid, images, fps=fps)


from datetime import date
from datetime import datetime
from ftw.builder import Builder
from ftw.builder import create
from ftw.testbrowser import browsing
from ftw.testbrowser.pages import editbar
from ftw.testbrowser.pages.statusmessages import info_messages
from opengever.meeting.period import Period
from opengever.meeting.reopen import ReopenMeeting
from opengever.testing import IntegrationTestCase
import pytz

class TestReopenMeeting(IntegrationTestCase):

    features = ('meeting',)
    REOPEN_MEETING_ACTION = 'Reopen meeting'

    def test_cannot_reopen_meeting_if_feature_is_deactivated(self):
        self.login(self.committee_responsible)

        self.schedule_ad_hoc(self.meeting, 'Foo')
        self.schedule_paragraph(self.meeting, u'Para')
        agenda_item = self.schedule_proposal(
            self.meeting, self.submitted_proposal
        )
        agenda_item.decide()

        self.deactivate_feature('meeting')
        self.assertEqual(
            ["Can't reopen meeting when feature is disabled."],
            ReopenMeeting(self.meeting.model).get_errors())

    def test_reopen_held_meeting(self):
        self.login(self.committee_responsible)

        self.schedule_ad_hoc(self.meeting, 'Foo')
        self.schedule_paragraph(self.meeting, u'Para')
        agenda_item = self.schedule_proposal(
            self.meeting, self.submitted_proposal
        )
        agenda_item.decide()
        model = self.meeting.model
        period = Period.get_current(
            model.committee.resolve_committee(),
            model.start.date()
        )

        self.assertEqual(3, period.decision_sequence_number)
        self.assertEqual(2, period.meeting_sequence_number)
        self.assertEqual('held', model.workflow_state)
        self.assertEqual(
            ['pending', 'pending', 'decided'],
            [each.workflow_state for each in model.agenda_items]
        )
        self.assertEqual(
            [2, None, 3],
            [each.decision_number for each in model.agenda_items]
        )
        self.assertEqual(2, model.meeting_number)

        ReopenMeeting(model).reopen_meeting()

        self.assertEqual('pending', model.workflow_state)
        self.assertEqual(
            ['pending', 'pending', 'pending'],
            [each.workflow_state for each in model.agenda_items]
        )
        self.assertEqual(1, period.decision_sequence_number)
        self.assertEqual(1, period.meeting_sequence_number)

    def test_reopen_closed_meeting(self):
        self.login(self.committee_responsible)

        agenda_item = self.schedule_ad_hoc(self.meeting, 'Foo')
        self.schedule_paragraph(self.meeting, u'Para')
        agenda_item.decide()
        model = self.meeting.model
        model.close()
        period = Period.get_current(
            model.committee.resolve_committee(),
            model.start.date()
        )

        self.assertEqual(2, period.decision_sequence_number)
        self.assertEqual(2, period.meeting_sequence_number)
        self.assertEqual('closed', model.workflow_state)
        self.assertEqual(
            ['decided', 'pending'],  # the paragraph is always pending
            [each.workflow_state for each in model.agenda_items]
        )
        self.assertEqual(
            [2, None],
            [each.decision_number for each in model.agenda_items]
        )
        self.assertEqual(2, model.meeting_number)

        ReopenMeeting(model).reopen_meeting()

        self.assertEqual('pending', model.workflow_state)
        self.assertEqual(
            ['pending', 'pending'],
            [each.workflow_state for each in model.agenda_items]
        )
        self.assertEqual(1, period.decision_sequence_number)
        self.assertEqual(1, period.meeting_sequence_number)

    def test_error_invalid_meeting_state(self):
        self.login(self.committee_responsible)

        reopener = ReopenMeeting(self.meeting.model)
        self.assertEqual(
            [u"Can't reopen meeting in state 'pending'.",
             u"Unexpected state, can't reset meeting number."],
            reopener.get_errors()
        )

    def test_error_invalid_agenda_item_state(self):
        self.login(self.committee_responsible)

        agenda_item = self.schedule_ad_hoc(self.meeting, 'Foo')
        agenda_item.decide()
        agenda_item.decision_number = None

        reopener = ReopenMeeting(self.meeting.model)
        self.assertEqual(
            [u"Unexpected state, have agenda items but "
             u"can't reset decision number."],
            reopener.get_errors()
        )

    def test_error_newer_meetings_with_meeting_number_exist(self):
        self.login(self.committee_responsible)

        agenda_item = self.schedule_ad_hoc(self.meeting, 'Foo')
        self.schedule_paragraph(self.meeting, u'Para')
        agenda_item.decide()
        model = self.meeting.model
        model.close()

        meeting_dossier = create(
            Builder('meeting_dossier')
            .within(self.leaf_repofolder)
        )
        newer_meeting = create(
            Builder('meeting')
            .having(
                title=u'8. Sitzung der Rechnungspr\xfcfungskommission',
                committee=self.committee.load_model(),
                start=datetime(2016, 9, 12, 19, 30, tzinfo=pytz.UTC),
            )
            .link_with(meeting_dossier)
        )
        agenda_item = self.schedule_ad_hoc(newer_meeting, 'Foo')
        agenda_item.decide()

        reopener = ReopenMeeting(self.meeting.model)
        self.assertEqual(
            [u"The meetings '8. Sitzung der Rechnungspr\xfcfungskommission' "
             u"need to be reopened first."],
            reopener.get_errors()
        )

    def test_only_considers_newer_meetings_from_same_period(self):
        self.login(self.committee_responsible)

        agenda_item = self.schedule_ad_hoc(self.meeting, 'Foo')
        self.schedule_paragraph(self.meeting, u'Para')
        agenda_item.decide()
        model = self.meeting.model
        model.close()

        # Create meeting with higher meeting number but in different period
        period = create(Builder('period').having(
                title=u'2015',
                start=date(2015, 1, 1),
                end=date(2015, 12, 31)).within(self.committee))
        period.meeting_sequence_number = 20

        meeting_dossier = create(
            Builder('meeting_dossier')
            .within(self.leaf_repofolder)
        )
        old_meeting = create(
            Builder('meeting')
            .having(
                title=u'8. Sitzung der Rechnungspr\xfcfungskommission',
                committee=self.committee.load_model(),
                start=datetime(2015, 9, 12, 19, 30, tzinfo=pytz.UTC),
            )
            .link_with(meeting_dossier)
        )
        agenda_item = self.schedule_ad_hoc(old_meeting, 'Foo')
        agenda_item.decide()

        self.assertTrue(old_meeting.meeting_number > self.meeting.model.meeting_number)
        reopener = ReopenMeeting(self.meeting.model)
        self.assertEqual([], reopener.get_errors())

    @browsing
    def test_browser_reopen_meeting(self, browser):
        self.login(self.committee_responsible)

        self.schedule_ad_hoc(self.meeting, 'Foo')
        agenda_item = self.schedule_proposal(
            self.meeting, self.submitted_proposal
        )
        agenda_item.decide()
        model = self.meeting.model
        period = Period.get_current(
            model.committee.resolve_committee(),
            model.start.date()
        )

        self.assertEqual(3, period.decision_sequence_number)
        self.assertEqual(2, period.meeting_sequence_number)
        self.assertEqual('held', model.workflow_state)
        self.assertEqual(
            ['pending', 'decided'],
            [each.workflow_state for each in model.agenda_items]
        )
        self.assertEqual(
            [2, 3],
            [each.decision_number for each in model.agenda_items]
        )
        self.assertEqual(2, model.meeting_number)

        self.login(self.manager, browser)
        browser.open(model.get_url())
        editbar.menu_option('Actions', self.REOPEN_MEETING_ACTION).click()
        browser.find('Confirm').click()

        self.assertEqual([u"The meeting has been reopened."], info_messages())

        self.assertEqual('pending', model.workflow_state)
        self.assertEqual(
            ['pending', 'pending'],
            [each.workflow_state for each in model.agenda_items]
        )
        self.assertEqual(1, period.decision_sequence_number)
        self.assertEqual(1, period.meeting_sequence_number)

    @browsing
    def test_non_manager_cannot_reopen(self, browser):
        self.login(self.administrator, browser)
        browser.open(self.meeting.model.get_url())
        self.assertNotIn(
            self.REOPEN_MEETING_ACTION, editbar.menu_options('Actions')
        )
        with browser.expect_unauthorized():
            browser.open(self.meeting.model.get_url(view='reopen_meeting'))


import random
import traceback

import discord
from discord.ext import commands
from requests import HTTPError

from poediscordbot.cogs.pob import util
from poediscordbot.cogs.pob.output import pob_output
from poediscordbot.cogs.pob.poe_data import poe_consts
from poediscordbot.cogs.pob.util import pastebin
from poediscordbot.pob_xml_parser import pob_xml_parser
from poediscordbot.util.logging import log

def setup(bot, active_channels, allow_pming=True):
    bot.add_cog(PoBCog(bot, active_channels, allow_pming))

class PoBCog(commands.Cog):
    def __init__(self, bot, active_channels, allow_pming=True):
        self.bot = bot
        self.active_channels = active_channels
        self.allow_pming = allow_pming

    @commands.Cog.listener()
    async def on_message(self, message):
        """
        Handle message events
        :param message: received
        :return: None
        """
        react_to_dms = isinstance(message.channel, discord.abc.PrivateChannel) and self.allow_pming

        if message.author.bot:
            return

        if 'help' in message.content.lower() and react_to_dms:
            await message.channel.send("Paste your pastebin here for a quick overview or use '!pob <pastebin>' for a "
                                       "detailed response.")
            return

        if (react_to_dms or message.channel.name in self.active_channels) \
                and not util.starts_with("!pob", message.content[:4]) \
                and "pastebin.com/" in message.content:
            # check if valid xml
            # send message
            log.debug(f"A| {message.channel}: {message.content}")
            try:
                embed = self._parse_pob(message.author, message.content, minify=True)
                if embed:
                    await message.channel.send(embed=embed)
            except HTTPError as err:
                log.error(f"Pastebin: Invalid pastebin-url msg={err}")
            except pastebin.CaptchaError as err:
                log.error(f"Pastebin: Marked as spam msg={err}")
                await message.channel.send(err.message)

    @commands.command(pass_context=True)
    async def pob(self, ctx, *, key):
        if not self.allow_pming and ctx.message.channel.is_private:
            return
        embed = self._parse_pob(ctx.message.author, ctx.message.content)
        try:
            if embed:
                await ctx.message.channel.send(embed=embed)
        except discord.Forbidden:
            log.info("Tried pasting in channel without access.")

    @staticmethod
    def _parse_pob(author, content, minify=False):
        """
        Trigger the parsing of the pastebin link, pass it to the output creating object and send a message back
        :param minify: show minified version of the embed
        :param content: of the message
        :param author: user sending the message
        :return: Embed
        """
        paste_keys = pastebin.fetch_paste_key(content)
        if paste_keys:
            paste_key = random.choice(paste_keys)
            log.info(f"Parsing pastebin with key={paste_key} from author={author}")
            raw_data = pastebin.get_as_xml(paste_key)
            if not raw_data:
                log.error(f"Unable to obtain raw data for pastebin with key {paste_key}")
                return

            xml = pastebin.decode_to_xml(raw_data)
            if not xml:
                log.error(f"Unable to obtain xml data for pastebin with key {paste_key}")
                return

            web_poe_token = util.fetch_xyz_pob_token(raw_data)
            build = pob_xml_parser.parse_build(xml)
            try:
                embed = pob_output.generate_response(author, build, minified=minify, pastebin_key=paste_key,
                                                     non_dps_skills=poe_consts, web_poe_token=web_poe_token)
                log.debug(f"embed={embed}; thumbnail={embed.thumbnail}")
                return embed
            except Exception as e:
                ex_msg = ''.join(traceback.format_exception(etype=type(e), value=e, tb=e.__traceback__))
                log.error(f"Could not parse pastebin={paste_key} - Exception={ex_msg}")


import os
from random import randint

import pygame
import pickle

WINDOW_SIZE = WINDOW_WIDTH, WINDOW_HEIGHT = 800, 600
TIMER_EVENT_TYPE = 30
TIMER_END_GAME = 31
DATA_DIR = 'data'

class HighScore:
    def __init__(self):
        self.save_file_name = f'{DATA_DIR}/clickme.highscore.dat'
        self.size = 10
        self.visible = False
        self.table = self.load()

    def render(self, p_screen):
        if not self.visible:
            return
        font = pygame.font.Font(None, 40)
        text = font.render(f'High score:', 1, pygame.Color('green'))
        p_screen.blit(text, (20, 50))
        for i in range(len(self.table)):
            text = font.render(f'{i+1}', 1, pygame.Color('green'))
            p_screen.blit(text, (20, 100 + 30*i))
            text = font.render(f'{self.table[i]}', 1, pygame.Color('green'))
            p_screen.blit(text, (80, 100 + 30*i))

    def update(self, score):
        if score < min(self.table):
            return
        self.table.append(score)
        self.table.sort(reverse=True)
        self.table = self.table[:self.size]
        self.save()

    def save(self):
        with open(self.save_file_name, 'wb') as file:
            pickle.dump(self.table, file)

    def load(self):
        if not os.path.isfile(self.save_file_name):
            return [0 for _ in range(self.size)]
        with open(self.save_file_name, 'rb') as file:
            return pickle.load(file)

    def show(self):
        self.visible = True

    def hide(self):
        self.visible = False

class ClickMe:
    def __init__(self):
        self.width = 200
        self.height = 60
        self.x, self.y = self.random_xy()
        self.delay = 1000
        self.game_timer = 1000 * 10
        self.max_clicks = 25
        self.score = 0
        self.is_paused = False
        self.is_stopped = False
        self.save_file_name = f'{DATA_DIR}/clickme.save.dat'
        self.click_count = 0
        self.reset_btn_timer()
        self.update_caption()
        pygame.time.set_timer(TIMER_END_GAME, self.game_timer)

    def update_caption(self):
        pygame.display.set_caption(f'Clicks: {self.click_count}, Delay: {self.delay}, Game paused: {self.is_paused}, '
                                   f'Game over: {self.is_stopped}')

    def decrease_delay(self):
        self.delay = max(self.delay - 50, 50)
        # self.update_caption()

    def reset_btn_timer(self):
        pygame.time.set_timer(TIMER_EVENT_TYPE, self.delay)

    def random_xy(self):
        return randint(0, WINDOW_WIDTH - self.width), randint(0, WINDOW_HEIGHT - self.height)

    def pos(self):
        return self.x, self.y, self.width, self.height

    def render(self, p_screen):
        if self.is_stopped:
            font = pygame.font.Font(None, 80)
            text = font.render('Game Over!', 1, (232, 170, 160))
            p_screen.blit(text, ((WINDOW_WIDTH - text.get_width()) // 2, (WINDOW_HEIGHT - text.get_height()) // 2))
            return
        font = pygame.font.Font(None, 50)
        text = font.render('Click me!', 1, (50, 70, 0))
        pygame.draw.rect(p_screen, (200, 150, 50), self.pos())
        p_screen.blit(text, (self.x + (self.width - text.get_width()) // 2,
                             self.y + (self.height - text.get_height()) // 2))
        font = pygame.font.Font(None, 24)
        text = font.render(f'Hits: {self.score}', 1, (200, 200, 200))
        p_screen.blit(text, (20, 20))

    def move(self):
        self.x, self.y = self.random_xy()

    def on_lmb_click(self, pos):
        if self.is_paused or self.is_stopped:
            return
        self.click_count += 1
        self.check(pos)
        self.update_caption()
        self.check_max_click()

    def check(self, pos):
        x, y = pos
        if self.x <= x <= self.x + self.width and self.y <= y <= self.y + self.height:
            self.move()
            self.decrease_delay()
            self.reset_btn_timer()
            self.inc_score()

    def inc_score(self):
        self.score += (1000 - self.delay) // 10

    def switch_pause(self):
        if self.is_stopped:
            return
        self.is_paused = not self.is_paused
        pygame.time.set_timer(TIMER_EVENT_TYPE, 0 if self.is_paused else self.delay)

    def save(self):
        with open(self.save_file_name, 'wb') as file:
            pickle.dump(self, file)

    def load(self):
        if not os.path.isfile(self.save_file_name):
            return
        with open(self.save_file_name, 'rb') as file:
            return pickle.load(file)

    def end(self):
        self.is_stopped = True
        pygame.time.set_timer(TIMER_END_GAME, 0)
        pygame.time.set_timer(TIMER_EVENT_TYPE, 0)

    def check_max_click(self):
        if self.click_count >= self.max_clicks:
            self.is_stopped = True

def main():
    pygame.init()
    screen = pygame.display.set_mode(WINDOW_SIZE)
    bg = pygame.Color('black')

    clickme = ClickMe()
    highscore = HighScore()

    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
                break
            if event.type == TIMER_EVENT_TYPE:
                clickme.move()
            if event.type == TIMER_END_GAME or clickme.is_stopped:
                clickme.end()
                highscore.update(clickme.score)
                highscore.show()
            if event.type == pygame.MOUSEBUTTONDOWN:
                clickme.on_lmb_click(event.pos)
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_p:
                    clickme.switch_pause()
                if event.key == pygame.K_s:
                    clickme.save()
                if event.key == pygame.K_l:
                    clickme = clickme.load()

        if not running:
            break

        screen.fill(bg)
        clickme.render(screen)
        highscore.render(screen)
        pygame.display.flip()

    pygame.display.quit()

if __name__ == '__main__':
    main()