import ipdb
import sys, os.path, ast
import inspect, typing, pybx
import dataclasses, enum

class ModuleDef:
    def __init__(self, name):
        self.name = name
        self.interfaces = []
        self.structs = {} # struct name -> struct def
        self.enums = []
        self.struct_order = [] # struct names

    def dump(self):
        print("interfaces:", [x.name for x in self.interfaces])
        print("structs:", [x.name for x in self.structs.values()])
        print("struct_order:", self.struct_order)
        print("enums:", [x.name for x in self.enums])

        for d in self.interfaces:
            d.dump()
        for s in self.structs.values():
            s.dump()
        for e in self.enums:
            e.dump()

class InterfaceDef:
    def __init__(self, def_type):
        self.def_type = def_type
        self.name = self.def_type.__name__
        self.methods = []

    def dump(self):
        print(f"interface {self.def_type}")
        for m in self.methods:
            m.dump()

fundamental_types = {str: 'string', int: 'int', float: 'double', bool: 'bool'}
class Type:
    def __init__(self, py_type):
        self.py_type = py_type

    def __repr__(self):
        ret = "Type " + repr(self.py_type)
        return ret

    def is_none(self):
        return self.py_type == type(None)

    def is_vector_type(self):
        return hasattr(self.py_type, '_name') and self.py_type._name == 'List'

    def is_rop_type(self):
        return hasattr(self.py_type, '_name') and self.py_type._name == 'object'

    def is_struct(self):
        return dataclasses.is_dataclass(self.py_type)

    def is_enum(self):
        return inspect.isclass(self.py_type) and issubclass(self.py_type, enum.Enum)

    def get_py_code_name(self):
        t = self.py_type
        if t == type(None):
            ret = "type(None)"
        elif inspect.isclass(t):
            ret = f"{t.__module__}.{t.__name__}"
        elif hasattr(t, '__origin__'):
            #ipdb.set_trace()
            args_s = ",".join([f"{c.__module__}.{c.__name__}" for c in t.__args__])
            ret = f"{t.__module__}.{t._name}[{args_s}]"
            #ipdb.set_trace()
        else:
            raise Exception(f"can't get py_code_name for type {self.py_type}")
        return ret

    def get_cpp_code_name(self):
        t = self.py_type
        if self.is_none():
            ret = "void"
        elif self.is_struct():
            ret = f"{t.__module__}::{t.__name__}"
        elif self.is_vector_type():
            value_type = self.py_type.__args__[0]
            value_type_cpp_code_name = Type(value_type).get_cpp_code_name()
            ret = f"vector<{value_type_cpp_code_name}>"
        elif self.is_rop_type():
            i_t = t.__args__[0]
            ret = f"{i_t.__module__}::{i_t.__name__}_rop"
        elif self.is_enum():
            ret = f"{t.__module__}::{t.__name__}"
        elif self.py_type in fundamental_types:
            ret = fundamental_types[self.py_type]
        else:
            raise Exception(f"can't get cpp_code_name for type {self.py_type}")
        return ret

    def get_js_code_name(self):
        t = self.py_type
        if self.is_none():
            ret = "null"
        elif self.is_struct():
            ret = f"new {t.__name__}()"
        elif self.is_vector_type():
            ret = "[]";
        elif self.is_rop_type():
            i_t = t.__args__[0]
            ret = f"new {i_t.__name__}_rop()"
        elif self.is_enum():
            ret = f"{t.__module__}::{t.__name__}"
        elif self.py_type in fundamental_types:
            ret = "null"
        else:
            raise Exception(f"can't get cpp_code_name for type {self.py_type}")
        return ret

class InterfaceMethodDef:
    def __init__(self, interface_def, method_name, type_hints, sig):
        self.interface_def = interface_def
        self.name = method_name
        self.type_hints = type_hints
        self.sig = sig

    def get_method_args(self):
        return [x for x in self.sig.parameters if x != 'self']

    def get_method_typed_args(self):
        return [f"{x[1]} {x[0]}" for x in zip(self.get_method_args(), self.get_method_arg_types())]

    def get_method_arg_types(self):
        args = self.get_method_args()
        return [Type(self.type_hints[x]) for x in args]

    def get_method_return_type(self):
        return Type(self.type_hints['return'])

    def dump(self):
        print(f" method {self.name}")
        m_args = self.get_method_args()
        m_arg_types = self.get_method_arg_types()
        m_ret_type = self.get_method_return_type()
        print(f"  arguments: {m_args}")
        print(f"  argument types: {m_arg_types}")
        print(f"  return type: {m_ret_type}")

class StructDef:
    def __init__(self, def_type):
        self.def_type = def_type
        self.name = self.def_type.__name__
        self.fields = [] # List[StructMemberDef]

    def dump(self):
        print(f"struct {self.def_type}")
        for m in self.fields:
            m.dump()

class StructMemberDef:
    def __init__(self, struct_def, field, field_type):
        self.struct_def = struct_def
        self.field = field
        self.field_type = Type(field_type)

    def get_member_name(self):
        return self.field.name

    def get_member_type(self):
        return self.field_type

    def dump(self):
        m_name = self.get_member_name()
        m_type = self.get_member_type()
        print(f" member: {m_name}")
        print(f" member type: {m_type}")

class EnumDef:
    def __init__(self, enum_def):
        self.def_type = enum_def
        self.name = enum_def.__name__
        self.members = []
        self.member_values = []
        #ipdb.set_trace()
        for n, m in enum_def.__members__.items():
            self.members.append(n)
            self.member_values.append(m.value)

    def dump(self):
        print(f"enum {self.def_type}")
        for m, m_v in zip(self.members, self.member_values):
            print(f" enum member {m} {m_v}")

def parse_pybx_interface(interface_class):
    #ipdb.set_trace()
    print("parse_pybx_interface: ", interface_class.__name__)
    interface_def = InterfaceDef(interface_class)
    methods = inspect.getmembers(interface_class, predicate=inspect.isfunction)
    for m_name, m_func in methods:
        th = typing.get_type_hints(m_func)
        sig = inspect.signature(m_func)
        m_def = InterfaceMethodDef(interface_def, m_name, th, sig)
        interface_def.methods.append(m_def)
    return interface_def

def parse_pybx_struct(struct):
    struct_def = StructDef(struct)
    #ipdb.set_trace()
    dataclass_th = typing.get_type_hints(struct)
    for f in dataclasses.fields(struct):
        f_type = dataclass_th[f.name]
        f_def = StructMemberDef(struct_def, f, f_type)
        struct_def.fields.append(f_def)
    return struct_def

def parse_pybx_enum(enum_def):
    enumdef_def = EnumDef(enum_def)
    return enumdef_def

def parse_module(mod):
    #ipdb.set_trace()
    if not inspect.ismodule(mod):
        raise Exception("mod expected to be Module")

    mod_def = ModuleDef(mod.__name__)
    for mod_el_name in dir(mod):
        mod_el = getattr(mod, mod_el_name)
        if inspect.isclass(mod_el) and issubclass(mod_el, pybx.interface):
            mod_def.interfaces.append(parse_pybx_interface(mod_el))
        elif dataclasses.is_dataclass(mod_el):
            struct_def = parse_pybx_struct(mod_el)
            mod_def.structs[struct_def.name] = struct_def
        elif inspect.isclass(mod_el) and issubclass(mod_el, enum.Enum):
            mod_def.enums.append(parse_pybx_enum(mod_el))

    return mod_def

def find_declaration_order(mod_def, mod_fn):
    source_code = "\n".join(open(mod_fn).readlines())
    pt = ast.parse(source_code)
    #ipdb.set_trace()
    for node in ast.iter_child_nodes(pt):
        #print(pt)
        if isinstance(node, ast.ClassDef):
            if len(node.decorator_list) > 0 and node.decorator_list[0].attr == 'dataclass':
                # we got pybx struct
                mod_def.struct_order.append(node.name)


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('tasting', '0006_tastingcategory_singulartitle'),
    ]

    operations = [
        migrations.AddField(
            model_name='whisky',
            name='degAlcool',
            field=models.IntegerField(default=0),
            preserve_default=True,
        ),
        migrations.AddField(
            model_name='wine',
            name='degAlcool',
            field=models.IntegerField(default=0),
            preserve_default=True,
        ),
    ]


import flet as ft

def main(page: ft.Page):
    page.theme = ft.Theme(color_scheme_seed=ft.colors.YELLOW)

    rail = ft.NavigationRail(
        selected_index=0,
        label_type=ft.NavigationRailLabelType.ALL,
        # extended=True,
        min_width=100,
        min_extended_width=400,
        leading=ft.FloatingActionButton(icon=ft.icons.UPLOAD_FILE, text="파일열기"),
        group_alignment=-0.9,

        destinations=[
            ft.NavigationRailDestination(
                icon=ft.icons.FAVORITE_BORDER,
                selected_icon=ft.icons.FAVORITE,
                label="First"
            ),
            ft.NavigationRailDestination(
                icon_content=ft.Icon(ft.icons.BOOKMARK_BORDER),
                selected_icon_content=ft.Icon(ft.icons.BOOKMARK),
                label="Second",
            ),
            ft.NavigationRailDestination(
                icon=ft.icons.SETTINGS_OUTLINED,
                selected_icon_content=ft.Icon(ft.icons.SETTINGS),
                label_content=ft.Text("Settings"),
            ),
        ],
        on_change=lambda e: print("Selected destination:", e.control.selected_index),
    )

    page.add(
        ft.Row(
            [
                rail,
                ft.VerticalDivider(width=1),
                ft.Column([ ft.Text("Body!")], alignment=ft.MainAxisAlignment.START, expand=True),
            ],
            expand=True,
        )
    )

ft.app(target=main)


"""HTTP request and response models for /health endpoints."""
import typing
from pydantic import BaseModel, Field
from opentrons_shared_data.deck.dev_types import RobotModel
from robot_server.service.json_api import BaseResponseBody

class HealthLinks(BaseModel):
    """Useful server links."""

    apiLog: str = Field(
        ...,
        description="The path to the API logs endpoint",
    )
    serialLog: str = Field(
        ...,
        description="The path to the motor control serial communication logs endpoint",
    )
    serverLog: str = Field(
        ...,
        description="The path to the HTTP server logs endpoint",
    )
    oddLog: typing.Optional[str] = Field(
        None,
        description="The path to the ODD app logs endpoint",
    )
    apiSpec: str = Field(
        ...,
        description="The path to the OpenAPI specification of the server",
    )
    systemTime: str = Field(
        ...,
        description="The path to the system time endpoints",
    )

class Health(BaseResponseBody):
    """Information about the server and system."""

    name: str = Field(
        ...,
        description="The robot's name. In most cases the same as its "
        "mDNS advertisement domain name, but this can get out "
        "of sync. Mostly useful for user-facing titles.",
    )
    robot_model: RobotModel = Field(
        ...,
        description="Which model of Opentrons robot this is",
    )
    api_version: str = Field(
        ...,
        description="The API server's software version",
    )
    fw_version: str = Field(
        ...,
        description="The motor controller's firmware version. Doesn't "
        "follow a pattern; suitable only for display or exact matching.",
    )
    board_revision: str = Field(
        ...,
        description="The hardware revision of the OT-2's central routing board.",
    )
    logs: typing.List[str] = Field(
        ...,
        description="List of paths at which to find log endpoints",
    )
    system_version: str = Field(
        ...,
        description="The robot's operating system version.",
    )
    maximum_protocol_api_version: typing.List[int] = Field(
        ...,
        description="The system's maximum supported Protocol API version, "
        "in the format `[major_version, minor_version]`",
        min_items=2,
        max_items=2,
    )
    minimum_protocol_api_version: typing.List[int] = Field(
        ...,
        description="The system's minimum supported Protocol API version, "
        "in the format `[major_version, minor_version]`",
        min_items=2,
        max_items=2,
    )
    robot_serial: typing.Optional[str] = Field(
        default=None,
        description="The robot serial number. Should be used if present; if not present, use result of /server/update/health.",
    )
    links: HealthLinks

    class Config:
        """Health response model schema configuration."""

        schema_extra = {
            "example": {
                "name": "OT2CEP20190604A02",
                "api_version": "3.15.2",
                "fw_version": "v2.15.0",
                "board_revision": "2.1",
                "logs": ["/logs/serial.log", "/logs/api.log"],
                "system_version": "1.2.1",
                "maximum_protocol_api_version": [2, 8],
                "minimum_protocol_api_version": [2, 0],
                "links": {
                    "apiLog": "/logs/api.log",
                    "serialLog": "/logs/serial.log",
                    "apiSpec": "/openapi.json",
                    "systemTime": "/system/time",
                },
                "robot_serial": None,
            }
        }


# -*- coding: utf-8 -*-
import sys
import xml.etree.ElementTree as ET

from lib import SuggestionCompleter, SuggestionNetworkModel

from PyQt5.QtCore import Qt, QUrl, QUrlQuery
from PyQt5.QtNetwork import QNetworkRequest, QNetworkReply
from PyQt5.QtWidgets import QApplication, QLineEdit

class SearchSuggestionModel(SuggestionNetworkModel):
    def create_request(self, text):
        url = QUrl("http://toolbarqueries.google.com/complete/search")
        query = QUrlQuery()
        query.addQueryItem("q", text)
        query.addQueryItem("output", "toolbar")
        query.addQueryItem("hl", "en")
        url.setQuery(query)
        request = QNetworkRequest(url)
        return request

    def process_reply(self, reply):
        results = []
        error_string = ""
        if reply.error() == QNetworkReply.NoError:
            content = reply.readAll().data()
            suggestions = ET.fromstring(content)
            for data in suggestions.iter("suggestion"):
                results.append(data.attrib["data"])
        elif reply.error() != QNetworkReply.OperationCanceledError:
            error_string = reply.errorString()
        return results, error_string

def main():
    app = QApplication(sys.argv)

    searchbar = QLineEdit()
    completer = SuggestionCompleter(caseSensitivity=Qt.CaseInsensitive)
    model = SearchSuggestionModel()
    completer.setModel(model)
    searchbar.setCompleter(completer)
    searchbar.resize(640, 60)
    searchbar.show()

    ret = app.exec_()
    sys.exit(ret)

if __name__ == "__main__":
    main()


import pandas as panda
from openpyxl import Workbook
from openpyxl.drawing.image import Image
import os
import shutil
import os.path
import openpyxl
from openpyxl import load_workbook
from openpyxl.drawing.image import Image
import tkinter as tk
from tkinter import filedialog

"""This program is to pull certain  photos from a selected pool according to excel sheet.
Photos will be copied to a directory of your choice, and will be seperated to different
folders with sheet names."""

print(" "* 2700)
print("Welcome to PhotoCarry")
print("This program is written for the purpose of copying photos")
print("from a pool to a directory according to an excelfile")
print("You will be asked to choose the pool directory  first and then")
print("the excel file. Lastly the location of the new directory.")
print(" "* 900)
#input('Press any key to continue')

root = tk.Tk()
root.withdraw()

#Pop-up that asks from where the  photos are taken
pool_path = filedialog.askdirectory(title="Choose the location of the photo pool")

#pop-up that asks where the excel file is.
file_path = filedialog.askopenfilename(title="Choose the excel file")
print(file_path)

#Pop-up that asks Where it will be stored
directory_path = filedialog.askdirectory(title="Choose the directory where the photos will be copied to")

file = panda.ExcelFile(file_path)
sheets = file.sheet_names

wb = load_workbook(file_path)

#iterating through the sheets
for sheet in sheets:
    page=file.parse(sheet)
    lenght, widht = page.shape
    print("The lenght of this sheet is :"+ str(lenght))
    ws = wb[sheet]
    path = os.path.join(directory_path, sheet)

    #if a folder with sheet's name exist it will fo nothing, else it will create a new one.
    if sheet in os.listdir(directory_path):
        continue
    else:
        os.mkdir(path)
        print("Directory '%s' created" %sheet)

    store_folder = directory_path+"/"+sheet
    for i in range(0, lenght):
        photo_check = page['Inv. No.'][i]

        #checking if there is a match
        #The algorithm for searching the filename is created according the need of the developer.
         #example of a filename for this algorithm is "ARK_123_4567.jpg" and we want to match with "4567".
        for filename in os.listdir(pool_path):

            #If your photo's name and the cell have the same name simply delete the codes until two lines above the Eureka part.
            first_line=0

            for k in range(0,len(filename)):
                if filename[k] == "_":
                    if first_line==0:
                        first_line=1
                    else:
                        second_line=0
                        for j in range(0,len(filename)):
                            if filename[j] == "_" or filename[j]== ".":
                                if second_line ==0 or second_line == 1:
                                    second_line = second_line + 1

                                elif second_line ==2:
                                    shm_number = "SHM "+ filename[k+1:j]

                                    #This means that the photo's name is the same with the cell.
                                    if shm_number == photo_check:
                                        print("Eureka!!")
                                        shutil.copy(pool_path+"/"+filename, store_folder)


import site
import sys
import os
from backend import install as backend_install
from setuptools import setup

'''Setuptools installer. This script will be called by pip when:
- We want to create a distributable (sdist) tar.gz
- We want to build the C extension (build and build_ext)
- We want to install PyCOMPSs (install)

It can be invoked to do these functionalities with
python setup.py (install|sdist|build)
'''

bindings_location = os.path.join('COMPSs', 'Bindings')
venv = False

def get_virtual_env_target_path():
    '''Get the target path within a virtual environment
    :return: target path
    '''
    from distutils.sysconfig import get_python_lib
    return os.path.join(get_python_lib(), 'pycompss')

def get_root_target_path():
    '''Get the target path for root installations
    :return: target path
    '''
    return os.path.join(site.getsitepackages()[0], 'pycompss')

def get_user_target_path():
    '''Get the target path for user installation
    :return: target path
    '''
    return os.path.join(site.getusersitepackages(), 'pycompss')

if 'VIRTUAL_ENV' in os.environ:
    # We are within a virtual environment
    # This is more legit than within the exception
    venv = True
    target_path = get_virtual_env_target_path()
    print('Installing within virtual environment in: ' + str(target_path))
else:
    try:
        if os.getuid() == 0:
            # Installing as root
            target_path = get_root_target_path()
            print('Installing as root in: ' + str(target_path))
        else:
            # Installing as user
            target_path = get_user_target_path()
            print('Installing as user in: ' + str(target_path))
    except AttributeError:
        # This exception can be raised within virtual environments due to a bug
        # with the site module.
        venv = True
        target_path = get_virtual_env_target_path()
        print('Installing within virtual environment in: ' + str(target_path))

def check_system():
    '''Check that we have a proper python version and a proper OS (i.e: not windows)
    Also, check that we have JAVA_HOME defined.
    This does NOT intend to perform an exhaustive system check, and it is neither
    the role nor in the scope of a distutils installer
    '''
    # check Python version
    assert sys.version_info[:2] >= (3, 6), 'COMPSs does not support Python version %s, only Python >= 3.6.x is supported.'%sys.version

    # check os version not Windows
    assert not sys.platform == "win32", "COMPSs does not support Windows"
    assert not sys.platform == "cygwin", "COMPSs does not support Windows/Cygwin"
    assert not sys.platform == "msys", "COMPSs does not support Windows/MSYS2"

    # check we have JAVA_HOME defined
    assert 'JAVA_HOME' in os.environ, 'JAVA_HOME is not defined'

'''Pre-install operation: download and install COMPSs
This will try to stop the installation if some error is
found during this part. However, some sub-scripts do not
propagate the errors they find, so there is not absolute
guarantee that this script will lead to a perfect, clean
installation.
'''
messages = []
if 'install' in sys.argv or 'bdist_wheel' in sys.argv:
    check_system()
    messages = backend_install(target_path, venv)

'''Setup function.
'''
setup(name='pycompss',
    version=open('VERSION.txt').read().rstrip(),
    description='Python Binding for COMP Superscalar Runtime',
    long_description=open('README.rst').read(),
    long_description_content_type='text/x-rst',
    author='The COMPSs team',
    author_email='support-compss@bsc.es',
    maintainer='The COMPSs team',
    maintainer_email='support-compss@bsc.es',
    url='http://compss.bsc.es',
    classifiers=['Development Status :: 5 - Production/Stable',
                'Environment :: Console',
                'Intended Audience :: Developers',
                'Intended Audience :: Science/Research',
                'License :: OSI Approved :: Apache Software License',
                'Operating System :: POSIX :: Linux',
                'Operating System :: Unix',
                'Programming Language :: C',
                'Programming Language :: C++',
                'Programming Language :: Java',
                'Programming Language :: Python :: 3.6',
                'Programming Language :: Python :: 3.7',
                'Programming Language :: Python :: 3.8',
                'Programming Language :: Python :: 3.9',
                'Programming Language :: Python :: 3.10',
                'Topic :: Software Development',
                'Topic :: Scientific/Engineering',
                'Topic :: System :: Distributed Computing',
                'Topic :: Utilities'],
    install_requires=['setuptools'],
    license='Apache 2.0',
    platforms=['Linux']
)

'''Show final messages
'''
for message in messages:
    print(message)


import sys, os, io, atexit

input = lambda: sys.stdin.readline().rstrip('\r\n')
stdout = io.BytesIO()
sys.stdout.write = lambda s: stdout.write(s.encode("ascii"))
atexit.register(lambda: os.write(1, stdout.getvalue()))

# 221001 17404 rgb거리 2

# 정답코드

N = int(input())

rgb = [list(map(int, input().split())) for _ in range(N)]

ans = sys.maxsize
for i in range(3):
    dp = [[0, 0, 0] for _ in range(N)]
    dp[0] = [sys.maxsize] * 3
    dp[0][i] = rgb[0][i]

    for j in range(1, N):
        dp[j][0] = rgb[j][0] + min(dp[j - 1][1], dp[j - 1][2])
        dp[j][1] = rgb[j][1] + min(dp[j - 1][0], dp[j - 1][2])
        dp[j][2] = rgb[j][2] + min(dp[j - 1][0], dp[j - 1][1])

    for k in range(3):
        if i == k:
            continue
        ans = min(ans, dp[-1][k])

print(ans)


# 导入手写字体加载器
from sklearn.datasets import load_digits
from sklearn.cross_validation import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.svm import LinearSVC
from sklearn.metrics import classification_report

'''
博文：http://www.cnblogs.com/Lin-Yi/p/8970520.html

支持向量机
根据训练样本的分布，搜索所有可能的线性分类器最佳的一个。
从高纬度的数据中筛选最有效的少量训练样本。
节省数据内存，提高预测性能
但是付出更多的cpu和计算时间
'''

'''
1 获取数据
'''
# 通过数据加载器获得手写字体数字的数码图像数据并存储在digits变量中
digits = load_digits()
# 查看数据的特征维度和规模
# print(digits.data.shape)  # (1797, 64)

'''
2 分割训练集合和测试集合
'''
x_train, x_test, y_train, y_test = train_test_split(digits.data,
                                                    digits.target,
                                                    test_size=0.25,
                                                    random_state=33)

'''
3 使用支持向量机分类模型对数字图像进行识别
'''
# 对训练数据和测试数据进行标准化
ss = StandardScaler()
x_train = ss.fit_transform(x_train)
x_test = ss.fit_transform(x_test)

# 初始化线性假设的支持向量机分类器
lsvc = LinearSVC()
# 进行训练
lsvc.fit(x_train, y_train)
# 利用训练好的模型对测试集合进行预测 测试结果存储在y_predict中
y_predict = lsvc.predict(x_test)

'''
4 支持向量机分类器 模型能力评估
'''
print("准确率：", lsvc.score(x_test, y_test))
print("其他评估数据：\n", classification_report(y_test, y_predict, target_names=digits.target_names.astype(str)))
'''
准确率： 0.9488888888888889
其他评估数据：  精确率      召回率  f1指标     数据个数
              precision    recall  f1-score   support

          0       0.92      0.97      0.94        35
          1       0.95      0.98      0.96        54
          2       0.98      1.00      0.99        44
          3       0.93      0.93      0.93        46
          4       0.97      1.00      0.99        35
          5       0.94      0.94      0.94        48
          6       0.96      0.98      0.97        51
          7       0.90      1.00      0.95        35
          8       0.98      0.83      0.90        58
          9       0.95      0.91      0.93        44

avg / total       0.95      0.95      0.95       450
'''

01_监督学习_分类预测/02_(支持向量机分类器)手写数字识别.py

02_(支持向量机分类器)手写数字识别.py


#!/usr/bin/env python
import numpy as np
import matplotlib.pyplot as plt
import cv2

from dataclasses import dataclass
from typing import Any, List

@dataclass
class Filter:
  psi: float
  filter: Any

def gabor_patch(size, psi):
    theta = 0.0
    sigma = 4.0
    lambd = 31
    kern = cv2.getGaborKernel(
      (size, size),
      sigma, theta, lambd, 0.7, psi, ktype=cv2.CV_32F
    )

    kern /= 1.5*kern.sum()
    return kern

def build_filters():
  filters = []
  ksize = 31

  start = 0
  end = np.pi

  for psi in np.arange(start, end, (end - start) / 16):
    filters.append(Filter(psi, gabor_patch(ksize, psi)))
  return filters

if __name__ == '__main__':
  filters: List[Filter] = build_filters()
  fig, axes = plt.subplots(ncols=4, nrows=4)

  for ida, row in enumerate(axes):
    for idr, ax in enumerate(row):
      f = filters[ida*4+idr]
      ax.set_title("Phase: {:2f}".format(f.psi))
      ax.imshow(f.filter)

  plt.show()


#!/usr/bin/python3

""" Export all photos to specified directory using album names as folders
    If file has been edited, also export the edited version,
    otherwise, export the original version
    This will result in duplicate photos if photo is in more than album """

import os.path
import pathlib
import sys

import click
from pathvalidate import is_valid_filepath, sanitize_filepath

import osxphotos

@click.command()
@click.argument("export_path", type=click.Path(exists=True))
@click.option(
    "--default-album",
    help="Default folder for photos with no album. Defaults to 'unfiled'",
    default="unfiled",
)
@click.option(
    "--library-path",
    help="Path to Photos library, default to last used library",
    default=None,
)
def export(export_path, default_album, library_path):
    export_path = os.path.expanduser(export_path)
    library_path = os.path.expanduser(library_path) if library_path else None

    photosdb = osxphotos.PhotosDB()

    photos = photosdb.photos(albums=["TEST"])

    for p in photos:
        if not p.ismissing:
            for [TEST] in albums:
                click.echo(f"exporting {p.filename} in album {album}")

                # make sure no invalid characters in destination path (could be in album name)
                album_name = sanitize_filepath(album, platform="auto")

                # create destination folder, if necessary, based on album name
                dest_dir = os.path.join(export_path, album_name)

                # verify path is a valid path
                if not is_valid_filepath(dest_dir, platform="auto"):
                    sys.exit(f"Invalid filepath {dest_dir}")

                # create destination dir if needed
                if not os.path.isdir(dest_dir):
                    os.makedirs(dest_dir)

                # export the photo
                if p.hasadjustments:
                    # export edited version
                    exported = p.export(dest_dir, edited=True)
                    edited_name = pathlib.Path(p.path_edited).name
                    click.echo(f"Exported {edited_name} to {exported}")
                # export unedited version
                exported = p.export(dest_dir)
                click.echo(f"Exported {p.filename} to {exported}")
        else:
            click.echo(f"Skipping missing photo: {p.filename}")

if __name__ == "__main__":
    export()


import os
from tkinter import *
from tkinter import messagebox
import tkinter as tk
import numpy

from tkinter import *
import json
import asyncio
import websockets
import threading

class WebSocketThread (threading.Thread):
    global buttons,clicked,count,root
    '''WebSocketThread will make websocket run in an a new thread'''

    # overide self init
    def __init__(self,name):
        threading.Thread.__init__(self)
        self.name=name
        self.uri = "ws://127.0.0.1:6789"
        # self.USERS = set()
        print("Start thread", self.name)

    # overide run method
    def run(self):
        # must set a new loop for asyncio
        asyncio.set_event_loop(asyncio.new_event_loop())
        # setup a server
        asyncio.get_event_loop().run_until_complete(self.listen())
        # keep thread running
        asyncio.get_event_loop().run_forever()

    # listener
    async def listen(self):
        # x = buttons[0]
        # b_click(x)
        '''listenner is called each time new client is connected
        websockets already ensures that a new thread is run for each client'''
        print("listen is called")
        async with websockets.connect(self.uri) as websocket:
            while True:
                print(" inside listen is called")
                s = await websocket.recv()
                print("recieved ",s)
                await self.handle_message(int(s))

    # message handler
    async def handle_message(self,data):
        print("handle_message: ", data)
        cnt = 0
        for x in buttons:
            if cnt == int(data):
                bclick(x)
                break
            cnt += 1

    # example of an action
    # action: notify

    # action: action
    async def action(self,cnt):
        async with websockets.connect(self.uri) as websocket:
            message = str(cnt)
            print("sending" ,message)
            await websocket.send(message)

    # expose action
    def do_activate(self,cnt):
        '''this method is exposed to outside, not an async coroutine'''
        # use asyncio to run action
        # must call self.action(), not use self.action, because it must be a async coroutine
        asyncio.get_event_loop().run_until_complete(self.action(cnt))

threadWebSocket = WebSocketThread("websocket_server")
threadWebSocket.start()

def game_over():
    print("\n\n")
    print( "███▀▀▀██ ███▀▀▀███ ███▀█▄█▀███ ██▀▀▀")
    print( "██    ██ ██     ██ ██   █   ██ ██   ")
    print( "██   ▄▄▄ ██▄▄▄▄▄██ ██   ▀   ██ ██▀▀▀")
    print( "██    ██ ██     ██ ██       ██ ██   ")
    print( "███▄▄▄██ ██     ██ ██       ██ ██▄▄▄")
    print( "                                    ")
    print( "███▀▀▀███ ▀███  ██▀ ██▀▀▀ ██▀▀▀▀██▄ ")
    print( "██     ██   ██  ██  ██    ██     ██ ")
    print( "██     ██   ██  ██  ██▀▀▀ ██▄▄▄▄▄▀▀ ")
    print( "██     ██   ██  █▀  ██    ██     ██ ")
    print( "███▄▄▄███    ▀█▀    ██▄▄▄ ██     ██▄")
    print("\n\n")
    return True

def check_winner ():
    global buttons,clicked,count,root
    arr = numpy.zeros((3,3))
    cnt = 0
    for x in buttons:
        if x["text"] == "X":
            i = cnt//3
            j = cnt%3
            arr[i][j] += 1
        elif x["text"] == "O":
            i = cnt//3
            j = cnt%3
            arr[i][j] -= 1
            #elif self.s[i][j]["text"] == 'O':
            #    arr[i][j] -= 1
        cnt += 1
    colsum = numpy.sum(arr,axis=0)
    rowsum = numpy.sum(arr,axis=1)
    if rowsum[0] == 3 or rowsum[1] == 3 or rowsum[2] == 3 or rowsum[0] == -3 or rowsum[1] == -3 or rowsum[2] == -3:
        return game_over()
    elif colsum[0] == 3 or colsum[1] == 3 or colsum[2] == 3 or colsum[0] == -3 or colsum[1] == -3 or colsum[2] == -3:
        return game_over()
    elif abs(arr[0][0]+arr[1][1]+arr[2][2]) == 3 or abs(arr[0][2]+arr[1][1]+arr[2][0]) == 3:
        return game_over()
    elif count == 9:
        return game_over()
    return False

def clicked():
    global buttons,clicked,count,root
    #threadWebSocket.do_activate()
    lbl.configure(text="Button was clicked !!")
def bclick(b):
    global buttons,clicked,count,root

    if b["text"] == " " and clicked == True:
        b["text"] = "X"
        clicked = False
        count += 1
        if check_winner():
            messagebox.showinfo("Tic Tac Toe", "Hey X won" )
            root.destroy()
    elif b["text"] == " " and clicked == False:
        b["text"] = "O"
        clicked = True
        count += 1
        if check_winner():
            messagebox.showinfo("Tic Tac Toe", "Hey O won" )
            root.destroy()
        #self.checkifwon()
    else:
        messagebox.showerror("Tic Tac Toe", "Hey! That box has already been selected\nPick Another Box..." )

def b_click(b):
    global buttons,clicked,count,root
    cnt = 0
    for x in buttons:
        if x == b:
            break
        cnt += 1

    if b["text"] == " " and clicked == True:
        b["text"] = "X"
        clicked = False
        count += 1
        threadWebSocket.do_activate(cnt)
        if check_winner():
            messagebox.showinfo("Tic Tac Toe", "Hey X won" )
            root.destroy()
    elif b["text"] == " " and clicked == False:
        b["text"] = "O"
        clicked = True
        count += 1
        threadWebSocket.do_activate(cnt)
        if check_winner():
            messagebox.showinfo("Tic Tac Toe", "Hey O won" )
            root.destroy()
        #self.checkifwon()
    else:
        messagebox.showerror("Tic Tac Toe", "Hey! That box has already been selected\nPick Another Box..." )

# 3 X 3 tictac

root = Tk()
root.title('Tic-Tac-Toe')
clicked = True
count = 0
R = -1
C = -1
# btn = Button(root, text="Start Me", command=clicked)
# btn.grid(column=4, row=2)
b1 = Button(root, text=" ", font=("Helvetica", 20), height=3, width=6, bg="white", command=lambda: b_click(b1))
b2 = Button(root, text=" ", font=("Helvetica", 20), height=3, width=6, bg="white", command=lambda: b_click(b2))
b3 = Button(root, text=" ", font=("Helvetica", 20), height=3, width=6, bg="white", command=lambda: b_click(b3))
b4 = Button(root, text=" ", font=("Helvetica", 20), height=3, width=6, bg="white", command=lambda: b_click(b4))
b5 = Button(root, text=" ", font=("Helvetica", 20), height=3, width=6, bg="white", command=lambda: b_click(b5))
b6 = Button(root, text=" ", font=("Helvetica", 20), height=3, width=6, bg="white", command=lambda: b_click(b6))
b7 = Button(root, text=" ", font=("Helvetica", 20), height=3, width=6, bg="white", command=lambda: b_click(b7))
b8 = Button(root, text=" ", font=("Helvetica", 20), height=3, width=6, bg="white", command=lambda: b_click(b8))
b9 = Button(root, text=" ", font=("Helvetica", 20), height=3, width=6, bg="white", command=lambda: b_click(b9))

b1.grid(row=0, column=0)
b2.grid(row=0, column=1)
b3.grid(row=0, column=2)
b4.grid(row=1, column=0)
b5.grid(row=1, column=1)
b6.grid(row=1, column=2)
b7.grid(row=2, column=0)
b8.grid(row=2, column=1)
b9.grid(row=2, column=2)
buttons = [b1,b2,b3,b4,b5,b6,b7,b8,b9]
root.mainloop()


from absl import app, flags
from mlperf_logging import mllog
import os

flags.DEFINE_multi_float('lr_rates', None,
                    'lr rates')
flags.DEFINE_multi_float('lr_boundaries', None,
                    'learning rate boundaries')
flags.DEFINE_float('l2_strength', None,
                    'weight decay')
flags.DEFINE_integer('conv_width', None,
                    'conv width')
flags.DEFINE_integer('fc_width', None,
                    'fc width')
flags.DEFINE_integer('trunk_layers', None,
                    'trunk layers')
flags.DEFINE_float('value_cost_weight', None,
                    'value cost weight')
flags.DEFINE_integer('summary_steps', None,
                    'summary steps')
flags.DEFINE_integer('bool_features', None,
                    'bool features')
flags.DEFINE_string('input_features', None,
                    'input features')
flags.DEFINE_string('input_layout', None,
                    'input layout')
flags.DEFINE_integer('shuffle_buffer_size', None,
                    'shuffle buffer size')
flags.DEFINE_boolean('shuffle_examples', None,
                    'shuffle examples')
flags.DEFINE_integer('keep_checkpoint_max', None,
                    'keep_checkpoint_max')
flags.DEFINE_integer('train_batch_size', None,
                    'train_batch_size')

FLAGS = flags.FLAGS

def main(argv):
    mllogger = mllog.get_mllogger()
    mllog.config(filename="train.log")

    mllog.config(
      default_namespace = "worker1",
      default_stack_offset = 1,
      default_clear_line = False)

    mllogger.event(key=mllog.constants.OPT_BASE_LR, value=FLAGS.lr_rates)
    mllogger.event(key='lr_rates', value=FLAGS.lr_rates)
    mllogger.event(key=mllog.constants.OPT_LR_DECAY_BOUNDARY_EPOCHS, value=FLAGS.lr_boundaries[1])
    mllogger.event(key='lr_boundaries', value=FLAGS.lr_boundaries[1])
    mllogger.event(key=mllog.constants.OPT_WEIGHT_DECAY, value=FLAGS.l2_strength)
    mllogger.event(key='opt_learning_rate_decay_boundary_steps', value=FLAGS.lr_boundaries)
    mllogger.event(key='train_batch_size', value=FLAGS.train_batch_size)

if __name__ == '__main__':
    app.run(main)


import cv2

vidcap = cv2.VideoCapture('BadApple.mp4')
success,image = vidcap.read()
count = 0

AsciVid = []

while success:
    image = cv2.resize(image,(150,50))
    chars = []
    for i in image:
        for j in i:
            if(sum(j)<191):
                chars.append(' ')
            elif (sum(j)<382):
                chars.append('.')
            elif (sum(j)<573):
                chars.append('*')
            else:
                chars.append('@')
        chars.append('\n')

    AsciVid.append(''.join(chars))
    f = open("./Frames/Frame%d.txt"%(count), "w")
    f.write(''.join(chars))
    f.close()
    success,image = vidcap.read()
    count += 1
    print("converting..........")


from flask import Flask, request, render_template, jsonify
from chatbot.chatbot import get_response, predict_class
import json

app= Flask(__name__)

intents = json.loads(open('Project/chatbot/intents.json').read())

@app.route('/', methods=['GET', 'POST'])
def home():
    chatbot_response = ''

    if request.method == 'POST':
        user_input = request.form['user_input']
        chatbot_response = get_response(predict_class(user_input), intents)
        return jsonify({'response': chatbot_response})

    return render_template('index.html')

if __name__ == '__main__':
    app.run(debug=True)


import json
import os
import subprocess
import multiprocessing
from datetime import datetime
from multiprocessing import Manager
from typing import Dict, Tuple
import logging

def send_disc(packet, nc_command):
    echo_command = "echo " + "\"" + packet + "\"" + " | " + nc_command
    os.system(echo_command)

def send_resp(packet, nc_cmd):
    echo_cmd = "echo " + "\"" + packet + "\"" + " | " + nc_cmd
    os.system(echo_cmd)

def listen(nc_command):
    os.system(nc_command)

def get_name_and_ip(response_str) -> Tuple[str, str]:
    start = response_str.find("NAME") + 6
    finish = response_str.find("MY_IP") - 2
    name = response_str[start:finish]
    start = response_str.find("MY_IP") + 7
    finish = response_str.find("TYPE") - 2
    ip = response_str[start:finish]
    return name, ip

def get_response(resp_dict, user_dict):
    port: str = "12345"
    resp_packet = str(json.dumps(resp_dict))
    while True:
        server = subprocess.run(["nc", "-l", port], stdout=subprocess.PIPE)
        response_str = str(server.stdout)
        if response_str != "":
            user_name, user_ip = get_name_and_ip(response_str)
            nc_command: str = "nc -vn -w 1 " + user_ip + " " + port
            initial_type = response_str.find("TYPE") + 6
            if user_ip not in list(user_dict.values()):
                user_dict[user_name] = user_ip
                print(user_name + " is added to your address list!")
                print("Address list: " + str(user_dict.keys()))
                print("Select a user name from the address list: ")
                if response_str[initial_type:initial_type + 1] == "D":
                    p_response = multiprocessing.Process(target=send_resp, args=(resp_packet, nc_command,))
                    p_response.start()
            elif response_str[initial_type: initial_type + 1] == "M" and user_ip in list(user_dict.values()):
                with open('chat_history.txt', "a") as f:
                    initial_m = response_str.find("PAYLOAD") + 9
                    last_m = response_str.rfind("}")
                    recv_msg = response_str[initial_m:last_m]
                    f.write("FROM {0} to {1}: \"{2}\" at {3}\n".format(user_name, resp_dict["NAME"], recv_msg,
                                                                       datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
            elif response_str[initial_type: initial_type + 1] == "M" and user_ip not in list(user_dict.values()):
                print("{0} is not in the address list. The message is hidden from the user!".format(user_ip))
            else:
                logging.error("Something wrong happened!")

def discover_ip(disc_dict):
    common_ip: str = disc_dict["MY_IP"][:-3]
    my_ip = int(disc_dict["MY_IP"][-3:])
    port: str = "12345"
    packet = str(json.dumps(disc_dict))
    for i in range(254, 255):
        if i != my_ip:
            nc_command: str = "nc -vn -w 1 " + common_ip + str(i) + " " + port
            p1 = multiprocessing.Process(target=send_disc, args=(packet, nc_command,))
            p1.start()

def create_jsons() -> Tuple[Dict[str, str], Dict[str, str], Dict[str, str]]:
    name = os.popen("whoami").read()[:-1]
    my_ip = os.popen("ipconfig getifaddr en0").read()[:-1]
    discover_dict = {
        "NAME": name,
        "MY_IP": my_ip,
        "TYPE": "DISCOVER",
        "PAYLOAD": ""
    }
    respond_dict = {
        "NAME": name,
        "MY_IP": my_ip,
        "TYPE": "RESPOND",
        "PAYLOAD": ""
    }
    message_dict = {
        "NAME": name,
        "MY_IP": my_ip,
        "TYPE": "MESSAGE",
        "PAYLOAD": ""
    }
    return discover_dict, respond_dict, message_dict

def simulation(disc_json, resp_json, user_dict):
    p_discover = multiprocessing.Process(target=discover_ip, args=(disc_json,))
    p_discover.start()

    while True:
        p2 = multiprocessing.Process(target=get_response, args=(resp_json, user_dict,))
        p2.start()
        p2.join()
        print(1)

if __name__ == '__main__':
    if os.path.exists("chat_history.txt"):
        os.remove("chat_history.txt")

    print("Welcome to BuChat!")
    disc_json, resp_json, msg_json = create_jsons()
    manager = Manager()
    user_dict = manager.dict()
    p_simulate = multiprocessing.Process(target=simulation, args=(disc_json, resp_json, user_dict,))
    p_simulate.start()
    while True:
        print("Address list: " + str(user_dict.keys()))
        user_name = input("Select a user name from the address list: \n")
        if user_name in user_dict.keys():
            message_to_sent = input("USERNAME FOUND!\nWrite your message to {0}: ".format(user_name))
            msg_json["PAYLOAD"] = message_to_sent
            msg_packet = str(json.dumps(msg_json))
            common_ip: str = msg_json["MY_IP"][:-3]
            port = "12345"
            nc_command: str = "nc -vn -w 1 " + user_dict[user_name] + " " + port
            echo_command = "echo " + "\"" + msg_packet + "\"" + " | " + nc_command
            os.system(echo_command)
            with open('chat_history.txt', "a+") as f:
                f.write("FROM {0} to {1}: \"{2}\" at {3}\n".format(msg_json["NAME"], user_name, message_to_sent,
                                                                   datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
        else:
            logging.info("{0} is not in the user dict! IP NOT FOUND!".format(user_name))

bogazici/CMPE487 | Applied Computer Networks/NetcatChat/chat.py


from pathlib import Path
from urllib.request import urlopen, Request

import click
import yaml

# Default site configs from GitHub
DEFAULT_SITE_CONFIGS = (
    "https://raw.githubusercontent.com/EGI-Foundation/fedcloud-catchall-operations/master/sites/100IT.yaml",
    "https://raw.githubusercontent.com/EGI-Foundation/fedcloud-catchall-operations/master/sites/BIFI.yaml",
    "https://raw.githubusercontent.com/EGI-Foundation/fedcloud-catchall-operations/master/sites/CESGA.yaml",
    "https://raw.githubusercontent.com/EGI-Foundation/fedcloud-catchall-operations/master/sites/CESNET-MCC.yaml",
    "https://raw.githubusercontent.com/EGI-Foundation/fedcloud-catchall-operations/master/sites/CETA-GRID.yaml",
    "https://raw.githubusercontent.com/EGI-Foundation/fedcloud-catchall-operations/master/sites/CLOUDIFIN.yaml",
    "https://raw.githubusercontent.com/EGI-Foundation/fedcloud-catchall-operations/master/sites/CYFRONET-CLOUD.yaml",
    "https://raw.githubusercontent.com/EGI-Foundation/fedcloud-catchall-operations/master/sites/DESY-HH.yaml",
    'https://raw.githubusercontent.com/EGI-Foundation/fedcloud-catchall-operations/master/sites/IFCA-LCG2.yaml',
    "https://raw.githubusercontent.com/EGI-Foundation/fedcloud-catchall-operations/master/sites/IISAS-FedCloud-cloud.yaml",
    "https://raw.githubusercontent.com/EGI-Foundation/fedcloud-catchall-operations/master/sites/IISAS-GPUCloud.yaml",
    "https://raw.githubusercontent.com/EGI-Foundation/fedcloud-catchall-operations/master/sites/IN2P3-IRES.yaml",
    "https://raw.githubusercontent.com/EGI-Foundation/fedcloud-catchall-operations/master/sites/INFN-CATANIA-STACK.yaml",
    "https://raw.githubusercontent.com/EGI-Foundation/fedcloud-catchall-operations/master/sites/INFN-PADOVA-STACK.yaml",
    "https://raw.githubusercontent.com/EGI-Foundation/fedcloud-catchall-operations/master/sites/Kharkov-KIPT-LCG2.yaml",
    "https://raw.githubusercontent.com/EGI-Foundation/fedcloud-catchall-operations/master/sites/NCG-INGRID-PT.yaml",
    "https://raw.githubusercontent.com/EGI-Foundation/fedcloud-catchall-operations/master/sites/RECAS-BARI.yaml",
    "https://raw.githubusercontent.com/EGI-Foundation/fedcloud-catchall-operations/master/sites/SCAI.yaml",
    "https://raw.githubusercontent.com/EGI-Foundation/fedcloud-catchall-operations/master/sites/TR-FC1-ULAKBIM.yaml",
    "https://raw.githubusercontent.com/EGI-Foundation/fedcloud-catchall-operations/master/sites/UA-BITP.yaml",
    "https://raw.githubusercontent.com/EGI-Foundation/fedcloud-catchall-operations/master/sites/UNIV-LILLE.yaml",
    "https://raw.githubusercontent.com/EGI-Foundation/fedcloud-catchall-operations/master/sites/fedcloud.srce.hr.yaml",
)

LOCAL_CONFIG_DIR = ".fedcloud-site-config/"

site_config_data = []

def read_site_config():
    """
    Read site configurations from local config dir if exist, otherwise from default GitHub location. Storing
    site configurations in global variable, that will be used by other functions. Call read_local_site_config()
    or read_default_site_config()

    :return: None
    """
    if len(site_config_data) > 0:
        return
    config_dir = Path.home() / LOCAL_CONFIG_DIR
    if config_dir.exists():
        read_local_site_config(config_dir)
    else:
        read_default_site_config()

def read_default_site_config():
    """
    Read default site configurations from GitHub.  Storing
    site configurations in a global variable, that will be used by other functions

    :return: None
    """
    site_config_data.clear()
    for filename in DEFAULT_SITE_CONFIGS:
        if filename.lower().startswith('http'):
            req = Request(filename)
        else:
            raise ValueError from None

        # URLs already checked, so ignore bandit test
        with urlopen(req) as yaml_file:       # nosec
            site_info = yaml.safe_load(yaml_file)
            site_config_data.append(site_info)

def read_local_site_config(config_dir):
    """
    Read site configurations from local directory specified in config_dir. Storing
    site configurations in global variable, that will be used by other functions

    :param config_dir: path to directory containing site configuration

    :return: None
    """
    site_config_data.clear()
    config_dir = Path(config_dir)
    for f in sorted(config_dir.glob('*.yaml')):
        yaml_file = f.open()
        site_info = yaml.safe_load(yaml_file)
        site_config_data.append(site_info)

def save_site_config(config_dir):
    """
    Save site configs to local directory specified in config_dir. Overwrite local configs if exist

    :param config_dir: path to directory containing site configuration

    :return: None
    """
    config_dir = Path(config_dir)
    config_dir.mkdir(parents=True, exist_ok=True)
    for site_info in site_config_data:
        config_file = config_dir / (site_info["gocdb"] + ".yaml")
        with config_file.open("w", encoding="utf-8") as f:
            yaml.dump(site_info, f)

def list_sites():
    """
    List of all sites IDs in site configurations

    :return: list of site IDs
    """
    read_site_config()
    result = []
    for site_info in site_config_data:
        result.append(site_info["gocdb"])
    return result

def find_site_data(site_name):
    """
    Return configuration of the correspondent site with site_name

    :param site_name: site ID in GOCDB
    :return: configuration of site if found, otherwise None
    """
    read_site_config()

    for site_info in site_config_data:
        if site_info["gocdb"] == site_name:
            return site_info
    return None

def find_endpoint_and_project_id(site_name, vo):
    """
    Return Keystone endpoint and project ID from site name and VO according to site configuration

    :param site_name: site ID in GOCDB
    :param vo: VO name. None if finding only site endpoint

    :return: endpoint, project_id, protocol if the VO exist on the site, otherwise None, None, None
    """
    site_info = find_site_data(site_name)
    if site_info is None:
        return None, None, None

    protocol = site_info.get("protocol")
    # If only site name is given, return endpoint without project ID
    if vo is None:
        return site_info["endpoint"], None, protocol

    for vo_info in site_info["vos"]:
        if vo_info["name"] == vo:
            return site_info["endpoint"], vo_info["auth"]["project_id"], protocol

    # Return None, None if VO not found
    return None, None, None

@click.group()
def site():
    """
    Site command group for manipulation with site configurations
    """
    pass

@site.command()
@click.option(
    "--site",
    help="Name of the site",
    required=True,
    envvar="EGI_SITE",
)
def show(site):
    """
    Print configuration of the specified site
    """
    site_info = find_site_data(site)
    if site_info:
        print(yaml.dump(site_info, sort_keys=True))
    else:
        print("Site %s not found" % site)

@site.command()
@click.option(
    "--site",
    help="Name of the site",
    required=True,
    envvar="EGI_SITE",
)
@click.option(
    "--vo",
    help="Name of the VO",
    required=True,
    envvar="EGI_VO",
)
def show_project_id(site, vo):
    """
    Printing Keystone endpoint and project ID of the VO on the site
    """
    endpoint, project_id, protocol = find_endpoint_and_project_id(site, vo)
    if endpoint:
        print(" Endpoint : %s \n Project ID : %s" % (endpoint, project_id))
    else:
        print("VO %s not found on site %s" % (vo, site))

@site.command()
def show_all():
    """
    Print all site configuration
    """
    read_site_config()
    for site_info in site_config_data:
        site_info_str = yaml.dump(site_info, sort_keys=True)
        print(site_info_str)

@site.command()
def save_config():
    """
    Read default site configs from GitHub and save them to local folder in home directory
    Overwrite local configs if exist
    """
    read_default_site_config()
    config_dir = Path.home() / LOCAL_CONFIG_DIR
    print("Saving site configs to directory %s" % config_dir)
    save_site_config(config_dir)

@site.command()
def list():
    """
    List site IDs
    """
    read_site_config()
    for site_info in site_config_data:
        print(site_info["gocdb"])


import os
import cv2 as cv
import json
import requests
import pandas as pd

class BaselineFunctions:
    @staticmethod
    def load_all_images_of_folder(folder):
        images = []
        file_names = []
        for file_name in os.listdir(folder):
            img = cv.imread(os.path.join(folder, file_name))
            if img is not None:
                images.append(img)
                file_names.append(file_name)
        return images, file_names

    @staticmethod
    def delete_all_files_in_folder(folder):
        for file_name in os.listdir(folder):
            os.remove(os.path.join(folder, file_name))

    @staticmethod
    def download_image(path, phone_src, model_name, web_prefix=''):
        path_to_file = path + str(model_name) + ".webp"
        if not os.path.exists(path_to_file):
            try:
                phone_link = web_prefix + str(phone_src)
                print("Downloading: " + phone_link)
                phone_img = requests.get(phone_link, stream=True).content
                with open(path_to_file, "wb+") as f:
                    f.write(phone_img)
            except:
                print("Error: Download of " + str(model_name) + " did not succeed")
                pass
        else:
            print("Image already exists at : " + path_to_file)

    @staticmethod
    def check_if_data_base_entry_exists(path, db_file_name, model_name):
        path_to_file = path + str(db_file_name)
        try:
            with open(path_to_file, 'r') as f:
                data_entries = json.load(f)
            for data_entry in data_entries:
                if data_entry["marketingName"] == str(model_name):
                    return True
            return False
        except:
            print("Error: DataBaseFile " + str(db_file_name) + " could not be opened")
            pass

    @staticmethod
    def nfc_chip_coordinates_in_percent(x0_nfc, y0_nfc, x1_nfc, y1_nfc, x0_edge, y0_edge, x1_edge, y1_edge):
        # x0-phoneEdge is 0
        x0_nfc = x0_nfc - x0_edge
        x1_nfc = x1_nfc - x0_edge
        x1_edge = x1_edge - x0_edge
        # y0-phoneEdge is 0
        y0_nfc = y0_nfc - y0_edge
        y1_nfc = y1_nfc - y0_edge
        y1_edge = y1_edge - y0_edge
        # NFC Chip Coordinates in Percent of Phone Screen
        x0_nfc = BaselineFunctions.normalize(float(x0_nfc / x1_edge))
        y0_nfc = BaselineFunctions.normalize(float(y0_nfc / y1_edge))
        x1_nfc = BaselineFunctions.normalize(float(x1_nfc / x1_edge))
        y1_nfc = BaselineFunctions.normalize(float(y1_nfc / y1_edge))
        # mirror x-axis
        x0_nfc = 1 - x0_nfc
        x1_nfc = 1 - x1_nfc
        return x1_nfc, y0_nfc, x0_nfc, y1_nfc

    @staticmethod
    def normalize(x):
        if x < 0.0:
            return 0.0
        elif x > 1.0:
            return 1.0
        else:
            return x

    @staticmethod
    def format_coordinates(manufacturer, name, model_names, x0, y0, x1, y1):
        name = name.split(".")[0]
        nfc_chip_location = {
            "manufacturer": manufacturer,
            "marketingName": name,
            "modelNames": model_names,
            "nfcPos": {
                "x0": x0,
                "y0": y0,
                "x1": x1,
                "y1": y1
            }
        }
        return nfc_chip_location

    @staticmethod
    def write_to_json_file(nfc_chip_locations, path, filename):
        full_path = path + filename
        try:
            if os.path.exists(full_path):
                data = []
                with open(full_path, 'r') as f:
                    data_entries = json.load(f)
                for data_entry in data_entries:
                    data.append(data_entry)
                for nfc_chip_location in nfc_chip_locations:
                    if not nfc_chip_location["marketingName"] in data:
                        data.append(nfc_chip_location)
                with open(full_path, 'w', encoding='utf-8') as f:
                    json.dump(data, f, ensure_ascii=False, indent=5)
            else:
                with open(full_path, 'w', encoding='utf-8') as f:
                    json.dump(nfc_chip_locations, f, ensure_ascii=False, indent=5)
        except:
            print("Error: Path " + str(full_path) + " could not be reached")
            pass

    @staticmethod
    def load_google_play_device_list():
        url = "https://storage.googleapis.com/play_public/supported_devices.csv"
        return pd.read_csv(url, encoding="utf-16")


import spacy
import requests

def main():
    nlp = spacy.load('en_core_web_md')
    nlp.add_pipe('entityLinker', last=True)
    #question = input('Please ask a question:\n')
    parse = nlp("What movie won best picture in the 2020 Academy Awards?")
    for sent in parse.sents:
        sent._.linkedEntities.pretty_print()
    #entities = parse._.linkedEntities.pretty_print()
    verb_dict = {'play': ('actor', 'role'), 'won': ('award', 'winner'), 'release': ('release date', 'publication place')}
    if len(entities) == 2:
        for word in parse:
            if word.dep_ == 'root' and word.lemma_ in verb_dict:
                prop = verb_dict[word.lemma_]
    # Get property uri
    url = 'https://www.wikidata.org/w/api.php'
    params = {'action': 'wbsearchentities',
              'language': 'en',
              'format': 'json',
              'type': 'property'}
    prop_ids = list()
    for p in prop:
        params['search'] = p
        json = requests.get(url, params).json()
        if json['search']:
            prop_ids.append(json['search'][0]['id'])
        else:
            return 'No answer found'

    # create SPARQL query for each key-word
    answers = list()
    entity1 = 'Q' + entities[0]
    entity2 = 'Q' + entities[1]
    query = "SELECT ?answerLabel WHERE { wd:" + entity2 + " p:" + prop_ids[0] \
            + " [ ps:" + prop_ids[0] + " ?answer; pq:" + prop_ids[1] + "wd:" + entity1 \
            + "]. SERVICE wikibase:label { bd:serviceParam wikibase:language 'en' }}"
    url = 'https://query.wikidata.org/sparql'
    results = requests.get(url, params={'query': query,
                                        'format': 'json'}).json()
    if results['results']['bindings']:
        for result in results['results']['bindings']:
            answers.append(result['answerLabel']['value'])
    return '\n'.join(set(answers))

if __name__ == '__main__':
    main()


import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris

iris = load_iris()

def short(x):
    if x < 0.6:
        return -(x/0.6)+1
    else:
        return 0

def medium(x):
    if x < 0.6:
        return x/0.6
    else:
        return (x-1)/(0.6-1)

def tall(x):
    if x > 0.6:
        return (x-0.6)/(1-0.6)
    else:
        return 0

# Returns a list where every element is the probability of each species
def rule_eval(SL, SW, PL, PW):
    rule1 = min(min(min( max(SL[0], SL[2]), max(SW[1], SW[2] )), max(PL[1], PL[2])), PW[1])
    rule2 = min(max(PL[0], PL[1]), PW[0])
    rule3 = min(min(max(SW[0], SW[1]), PL[2]), PW[2])
    rule4 = min(min(min(SL[1], max(SW[0], SW[1])), PL[0]), PW[2])

    return [rule2, rule1 + rule4, rule3]

correct_gusses = 0

for x, y in enumerate(iris['data']):

    inp1 = y[0]
    inp2 = y[1]
    inp3 = y[2]
    inp4 = y[3]

    an = ((inp1 - 4.3) / (7.9 - 4.3))
    bn = ((inp2 - 2) / (4.4 - 2))
    cn = ((inp3 - 1) / (6.9 - 1))
    dn = ((inp4 - 0.1) / (2.50 - 0.1))

    SL = [short(an), medium(an), tall(an)]
    SW = [short(bn), medium(bn), tall(bn)]
    PL = [short(cn), medium(cn), tall(cn)]
    PW = [short(dn), medium(dn), tall(dn)]

    result = rule_eval(SL, SW, PL, PW)

    species = result.index(max(result))
    if species == 0:
        print(x, '\tSetosa\t\t', iris['target'][x], '\t', result)

    elif species == 1:
        print(x, '\tVersicolor\t', iris['target'][x], '\t', result)

    elif species == 2:
        print(x, '\tVirginica\t', iris['target'][x], '\t', result)

    if species == iris['target'][x]:
        correct_gusses = correct_gusses + 1

print('Predictability: ', correct_gusses/150)


import logging
import jsonpickle
from core.EmailEnvelope import EmailEnvelope
from core.filtering.filters.PastFilter import PastFilter
from typing import Sequence

class AIFilter(PastFilter):
    checks: Sequence[str] = ["get_count_urls", "get_count_images", "get_from_return_path", "get_from_reply_to",
                             "get_id_email_client"]
    all_content_types = ["text/html", "text/plain", "multipart/mixed", "application/octet-stream",
                         "multipart/alternative", "multipart/related", "image/jpeg", "image/gif", "message/rfc822",
                         "text/plain charset=us-ascii", "image/png", "text/x-vcard", "image/bmp",
                         "application/x-zip-compressed", "multipart/signed", "application/pgp-signature",
                         "text/enriched", "application/ms-tnef", "video/mng", "application/x-pkcs7-signature",
                         "multipart/report", "message/delivery-status", "text/rfc822-headers",
                         "application/x-java-applet", "application/x-patch"]
    all_extension_types = ["txt", "html", "jpg", "png", "gif", "lst", "JPG", "htm", "doc", "GIF", "JPE", "b64", "BIN",
                           "Jpg", "zip", "dat", "rar", "bmp", "jpe", "gz", "PDF.html", "url", "ng", "spec.patch",
                           "spec", "patch", "p7s", "am", "jpeg"]

    def set_initial_data(self, data):
        """
        This method initialize the data which was read from the respective model's file
        :param data: data read of the model's file
        """
        self.data = jsonpickle.decode(data)

    def filter(self, envelope: EmailEnvelope) -> bool:
        """
        This method return the classification of the email, if 1 is Spa,
        :param envelope: email
        :return: if 1 then is spam and return true, else false
        """
        email_params = envelope.ai_matrix_for_email()
        is_spam = True if self.data.predict([email_params]) == [1] else False
        if is_spam:
            logging.info(f"An AIFilter has detected the email as spam")
        return is_spam


import sqlite3
import time
import random
import json
import requests

try:
    import telepot
except(ImportError, IOError):
    print('not found a telegram api - require telepot' + '\n'
          'run command: "pip install telepot"')
    exit('Closing...')

token = input('Введите ваш токен ключ: ')

"""создание шаблона базы данных"""
DataBaseName = 'userd.db'
conn = sqlite3.connect(DataBaseName)
c = conn.cursor()
c.execute("CREATE TABLE IF NOT EXISTS cities (names)")
c.execute("CREATE TABLE IF NOT EXISTS users (id, msg)")
j = requests.get('http://api.hh.ru/areas')
foo = json.loads(j.content.decode('utf-8'))

"""проводит проверку на заполненность поля names в таблице
   cities и если нет заполняет их именами городов"""
c.execute('SELECT * FROM cities')
if len(c.fetchall()) == 0:
    for i in foo:
        f = i['areas']
        for j in f:
            d = j['areas']
            for e in d:
                g = [(e['name'])]
                c.execute("INSERT INTO cities VALUES (?)", g)
    conn.commit()
else:
    print('-' * 70)

def handle(msg):
    text_type, chat_type, chat_id = telepot.glance(msg)
    answer_names = []
    g = msg
    ask = g['text']

    """открытие бд для чтение внутри функции"""
    DataBaseName = 'userd.db'
    conn = sqlite3.connect(DataBaseName)
    c = conn.cursor()
    c.execute('SELECT * FROM cities')

    """получение все имен для отбора из них по первой букве"""
    all_names = c.fetchall()
    if ask != '/start':
        bot.sendChatAction(chat_id, 'typing')
        answer_names.clear()
        c.execute("INSERT INTO users VALUES (?,?)", (chat_id, ask))
        conn.commit()
        for row in all_names:
            h = row[0]
            if h[0].title() == ask[-1].title():
                answer_names.append(row[0])
        g = len(answer_names)
        if g == 0:
            bot.sendMessage(chat_id, 'Воав, потише братишка. Я не '
                            'знаю городов на такую букву. '
                            'Давай на другую букву.')
        else:
            choice = random.randrange(0, g)
            time.sleep(1)
            bot.sendMessage(chat_id, answer_names[choice])

bot = telepot.Bot(token)
bot.message_loop(handle)
print('Listening ...')
while True:
    f = input('input yes for closing ==> ')
    print('-' * 72)
    if f == 'yes':
        conn.close()
        break


import unittest
import requests
from pyapp import data_collection
from pyapp import data_obj

class TestDatapublish(unittest.TestCase):
    def setUp(self):
        # creating fake data
        node = data_obj.Node()
        node.course_name = 'test_course'
        node.node_label = 'test_node_name'
        node.gpu_type = '1080ti'
        node.gpu_used =1
        node.gpu_total=2
        self.data = {'test_node':node}
        # function to be tested
        self.data_publish = data_collection.data_publish

    def test_hello_world(self):
        # start the server
        data_collection.start_server(9000)
        gpu_metric = data_collection.create_guage()
        # create the metric
        self.data_publish(nodes=self.data,gpu_metric=gpu_metric)
        # use http get method to read data
        r =requests.get('http://127.0.0.1:9000')
        # check the return data
        assert r.status_code == 200
        assert 'dsmlp_gpu{course="test_course",hostname="test_node_name",status="total",type="1080ti"} 2.0' in r.text
        assert 'dsmlp_gpu{course="test_course",hostname="test_node_name",status="reserved",type="1080ti"} 1.0' in r.text
        assert 'dsmlp_gpu{course="test_course",hostname="test_node_name",status="available",type="1080ti"} 1.0' in r.text


from os.path import expanduser

import torch
from joblib import Memory

def torch_cached(func):
    mem = Memory(expanduser('~/cache'))

    def cached_func(*args, **kwargs):
        def false_func(*processed_args, **processed_kwargs):
            return func(*args, **kwargs)
        processed_args = []
        for arg in args:
            if isinstance(arg, torch.Tensor):
                arg = arg.cpu().numpy()
            processed_args.append(arg)
        processed_kwargs = {}
        for k, kwarg in kwargs.items():
            if isinstance(kwarg, torch.Tensor):
                kwarg = kwarg.cpu().numpy()
            processed_kwargs[k] = kwarg
        return mem.cache(false_func)(*processed_args, **processed_kwargs)

    return cached_func


import random
import json
class car(object):
    def __init__(self,  hp, wd, price, seats,
    running, dtp, type, v_engine,
    engine_type, years, fuel_economy, acceleration, top_speed,
     v_trunk, box):
        self.hp=hp
        self.wd=wd
        self.price=price
        self.seats=seats
        self.running=running
        self.dtp=dtp
        self.type=type
        self.v_engine=v_engine
        self.v_trunk=v_trunk
        self.engine_type=engine_type
        self.years=years
        self.fuel_economy=fuel_economy
        self.acceleration=acceleration
        self.box=box
        self.top_speed=top_speed
def how_many_seats(type):
    if type in ['SUV', 'Sedan', 'Hatcback', 'Crossover', 'Pickup truck']:
        return 5
    elif type in ['Coupe', 'Sports car', 'Racecar', 'hypercar', 'muscle car', 'gt']:
        return 2
    elif type=='Minivan':
        return 7

def generate_car():
    hp=random.randint(50, 2000)
    wd=random.choice(['AWD', 'FWD', 'RWD'])
    price=random.randint(50, 100000)*1000
    running=random.randint(1, 500)*1000*random.choice([0, 1])
    dtp=random.randint(0, 10)*random.choice([0, 0, 0, 0, 0, 0, 1, 1, 0])
    type=random.choice(['SUV', 'Sedan', 'Hatcback', 'Crossover', 'Coupe', 'Minivan', 'Sports car', 'Racecar', 'Pickup truck', 'hypercar', 'muscle car', 'gt'])
    seats = how_many_seats(type)
    v_engine=round(random.uniform(1.6, 10.), 1)
    v_trunk=random.randint(100, 1000)
    engine_type=random.choice(['disel', 'gasoline', 'electro', 'hybrid'])
    years=random.randint(0, 40)
    fuel_economy=round(random.uniform(5., 20.), 1)
    acceleration=round(random.uniform(1.9,10.), 1)
    box=random.choice(['auto', 'robot', 'mannual'])
    top_speed=random.randint(150, 500)
    return car( hp, wd, price, seats,
    running, dtp, type, v_engine,
    engine_type, years, fuel_economy, acceleration, top_speed,
     v_trunk, box)
lists_of_cars={}
v=generate_car()
lis=[]
for i in range(1000):
    v=generate_car()
    lis=[v.hp, v.wd, v.price, v.seats,
    v.running, v.dtp, v.type, v.v_engine,
    v.engine_type, v.years, v.fuel_economy, v.acceleration, v.top_speed,
     v.v_trunk, v.box]
    lists_of_cars.update({'car #'+str(i) : lis})

print (lists_of_cars)

with open("cars.json", "w") as ile:
     json.dump(lists_of_cars, ile)


# common
import threading
# selenium
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from crawler_log import *

class Sem():
    def __init__(self, num = 2):
        self.sem = threading.Semaphore(value = num)
        self.flag = False
    def init(self, num):
        if not self.flag:
            self.sem = threading.Semaphore(value = num)
            self.flag = True
    def acquire(self):
        self.sem.acquire()
    def release(self):
        self.sem.release()
sem = Sem()

def getChromeDriver(options):
    opts = webdriver.ChromeOptions()
    for opt in options:
        opts.add_argument(opt)
    driver = webdriver.Chrome(options = opts)
    return driver

def isElementPresent(driver, by, value):
        """
        用来判断元素标签是否存在，
        """
        try:
            driver.find_element(by=by, value=value)
        # 原文是except NoSuchElementException, e:
        except NoSuchElementException as e:
            # 发生了NoSuchElementException异常，说明页面中未找到该元素，返回False
            return False
        else:
            # 没有发生异常，表示在页面中找到了该元素，返回True
            return True

# 点击元素并打开新标签页
def clickOpenWindow(driver, element, index, by, value):
    LOGI('switch to window {}'.format(index))
    driver.switch_to.window(index)
    LOGI('click {}'.format(value))
    element.find_element(by, value).click()
    newWindow= driver.window_handles[len(driver.window_handles)-1]
    return newWindow


from __future__ import absolute_import

from concurrent import futures
import time
import json
import functools

import grpc

from coconut import registry
from coconut.rpc_server import Runner
from coconut.protos.runner import runner_pb2_grpc
from coconut.protos.tasks import task_pb2
from coconut.backend import Backend
from coconut.registry.service import Service
from coconut.task import Task
from coconut.utils import get_lan_ip, new_uuid

_ONE_DAY_IN_SECONDS = 60 * 60 * 24

_DEFAULT_SERVICE_NAME = 'coconut-python-runner'

class Coconut(object):
    def __init__(
            self,
            name=_DEFAULT_SERVICE_NAME,
            host=None,
            port=6666,
            registry_host='127.0.0.1',
            registry_port=8500):

        self.name = name
        if host is None:
            self.host = get_lan_ip()
        else:
            self.host = host
        self.port = port
        self.kind = 'coconut-runner'

        self.registry = registry.Consul(registry_host, registry_port)
        self.backend = Backend(self.registry)
        self.funcs = {}

    def __process(self, request, context):
        if request.key not in self.funcs:
            context.set_code(500)
            context.set_details("Task not found")
            return task_pb2.Reply()

        f = self.funcs[request.key]

        try:
            task = Task(f, request)
            results = task.call()

            return task_pb2.Reply(results=[task_pb2.Result(type=result.type, value=result.encode_value()) for result in results])
        except Exception as e:
            print("Call task failed {}".format(e))
            context.set_code(500)
            context.set_details(str(e))
            return task_pb2.Reply()

    def serve(self):
        """
        serve task request
        """
        server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
        runner_pb2_grpc.add_RunnerServicer_to_server(
            Runner(self.__process), server)
        server.add_insecure_port('{}:{}'.format(self.host, self.port))
        server.start()

        self.server = server

        print('grpc Server started on {}:{}'.format(self.host, self.port))
        self.__register_service()

        try:
            while True:
                time.sleep(_ONE_DAY_IN_SECONDS)
        except KeyboardInterrupt:
            self.__deregister_service()
            server.stop(0)

    def publish(self, service_name, key, args=None, countdown=0):
        response = self.backend.publish(service_name, key, args, countdown)
        return response.uuid

    def task(self, *args, **kwargs):
        """
        register task function
        """
        def wrapper(f):

            key = kwargs.get('key') or f.__name__
            self.funcs[key] = f

            @functools.wraps(f)
            def __func(*args, **kwargs):
                return f(*args, **kwargs)

            return __func
        return wrapper

    def __register_service(self):
        service = Service(
            self.name,
            new_uuid(),
            self.kind,
            self.host,
            self.port
        )
        print("Registering {}: {}".format(service.kind, service.service_id))
        self.registry.register(service)
        self.service = service

    def __deregister_service(self):
        if self.service is not None:
            print("Deregistering {}...".format(self.service.kind))
            self.registry.deregister(self.service)


from anndata import AnnData
import numpy as np
from gssnng.smoothing import get_smoothing_matrix
from scipy import sparse

def test_get_smoothing_matrix():
    """
    assert the the rows of the smoothing matrix are normalized to 1
    """
    ncells = 4
    ngenes = 3

    adjacency = np.array([
        [0, 0.1, 0, 0],
        [0, 0, 0, 0],  # weird edge case where one cell doesnt have any neighbors!
        [0, 0, 0, 1],
        [0, 0, 1, 0]])

    adata = AnnData(np.random.rand(ncells,ngenes))
    adata.obsp['distances'] = sparse.csr_matrix(adjacency)

    # adjacency has to ignore the weightings of the neighbors
    S = get_smoothing_matrix(adata, mode='adjacency', add_diag=False)
    np.testing.assert_allclose(S.A.sum(1), [1, 0, 1, 1])

    adata.obsp['connectivities'] = sparse.csr_matrix(adjacency)
    S = get_smoothing_matrix(adata, mode='connectivity', add_diag=False)
    np.testing.assert_allclose(S.A.sum(1), [1, 0, 1, 1])

    # if there's diagnoal elements the results are differnt
    S = get_smoothing_matrix(adata, mode='adjacency', add_diag=True)
    np.testing.assert_allclose(S.A.sum(1), [1, 1, 1, 1])

    adata.obsp['connectivities'] = sparse.csr_matrix(adjacency)
    S = get_smoothing_matrix(adata, mode='connectivity', add_diag=True)
    np.testing.assert_allclose(S.A.sum(1), [1, 1, 1, 1])

test_get_smoothing_matrix()
print('test done')


from pyspark.sql import SparkSession
from datetime import datetime
import sys
import configparser
import pyspark.sql.functions as F
from pyspark.sql.types import *
import pandas as pd

config = configparser.ConfigParser()
config.read('/app/mount/parameters.conf')
data_lake_name=config.get("general","data_lake_name")
data_path=config.get("general","data_path")
username=config.get("general","username")

cloudPath=data_lake_name+data_path

print("Running as Username: ", username)

spark = SparkSession \
    .builder \
    .appName("ICEBERG LOAD") \
    .config("spark.sql.catalog.spark_catalog", "org.apache.iceberg.spark.SparkSessionCatalog")\
    .config("spark.sql.catalog.spark_catalog.type", "hive")\
    .config("spark.sql.extensions", "org.apache.iceberg.spark.extensions.IcebergSparkSessionExtensions")\
    .config("spark.kubernetes.access.hadoopFileSystems", data_lake_name)\
    .getOrCreate()

#---------------------------------------------------
#               INSERT DATA
#---------------------------------------------------

# STORE TIMESTAMP BEFORE INSERTS
now = datetime.now()
timestamp = datetime.timestamp(now)
print("PRE-INSERT TIMESTAMP: ", timestamp)

print("PRE-INSERT TABLE SNAPSHOTS AND HISTORY")
spark.sql("SELECT * FROM spark_catalog.CDE_WORKSHOP_{0}.CAR_SALES_{0}.history".format(username)).show(20, False)
spark.sql("SELECT * FROM spark_catalog.CDE_WORKSHOP_{0}.CAR_SALES_{0}.snapshots".format(username)).show(20, False)

# PRE-INSERT COUNT
print("PRE-INSERT COUNT")
spark.sql("SELECT COUNT(*) FROM spark_catalog.CDE_WORKSHOP_{0}.CAR_SALES_{0}".format(username)).show()

# INSERT DATA APPROACH 1 - APPEND FROM DATAFRAME
temp_df = spark.sql("SELECT * FROM spark_catalog.CDE_WORKSHOP_{0}.CAR_SALES_{0}".format(username)).sample(fraction=0.1, seed=3)
temp_df.writeTo("spark_catalog.CDE_WORKSHOP_{0}.CAR_SALES_{0}".format(username)).append()

# INSERT DATA APPROACH 2 - INSERT VIA SQL
spark.sql("DROP TABLE IF EXISTS spark_catalog.CDE_WORKSHOP_{0}.CAR_SALES_SAMPLE_{0}".format(username))
temp_df.writeTo("spark_catalog.CDE_WORKSHOP_{0}.CAR_SALES_SAMPLE_{0}".format(username)).create()

print("INSERT DATA VIA SPARK SQL")
query_5 = """INSERT INTO spark_catalog.CDE_WORKSHOP_{0}.CAR_SALES_{0} SELECT * FROM spark_catalog.CDE_WORKSHOP_{0}.CAR_SALES_SAMPLE_{0}""".format(username)
print(query_5)
spark.sql(query_5)

#---------------------------------------------------
#               TIME TRAVEL
#---------------------------------------------------

# NOTICE SNAPSHOTS HAVE BEEN ADDED
#spark.read.format("iceberg").load("spark_catalog.{}_CAR_DATA.CAR_SALES.history".format(username)).show(20, False)
spark.sql("SELECT * FROM spark_catalog.CDE_WORKSHOP_{0}.CAR_SALES_{0}.history".format(username)).show(20, False)
spark.sql("SELECT * FROM spark_catalog.CDE_WORKSHOP_{0}.CAR_SALES_{0}.snapshots".format(username)).show(20, False)

# POST-INSERT COUNT
print("POST-INSERT COUNT")
spark.sql("SELECT COUNT(*) FROM spark_catalog.CDE_WORKSHOP_{0}.CAR_SALES_{0}".format(username)).show()

# TIME TRAVEL AS OF PREVIOUS TIMESTAMP
df = spark.read.option("as-of-timestamp", int(timestamp*1000)).format("iceberg").load("spark_catalog.CDE_WORKSHOP_{0}.CAR_SALES_{0}".format(username))

# POST TIME TRAVEL COUNT
print("POST-TIME TRAVEL COUNT")
print(df.count())

#---------------------------------------------------
#               INCREMENTAL REPORT
#---------------------------------------------------

# ICEBERG TABLE HISTORY (SHOWS EACH SNAPSHOT AND TIMESTAMP)
spark.sql("SELECT * FROM CDE_WORKSHOP_{0}.CAR_SALES_{0}.history;".format(username)).show()

# ICEBERG TABLE SNAPSHOTS (USEFUL FOR INCREMENTAL QUERIES AND TIME TRAVEL)
spark.sql("SELECT * FROM CDE_WORKSHOP_{0}.CAR_SALES_{0}.snapshots;".format(username)).show()

# STORE FIRST AND LAST SNAPSHOT ID'S FROM SNAPSHOTS TABLE
snapshots_df = spark.sql("SELECT * FROM CDE_WORKSHOP_{0}.CAR_SALES_{0}.snapshots;".format(username))

print("SHOWING SNAPSHOTS DF")
#snapshots_df.show()

snapshots = snapshots_df.toPandas()

print("SHOWING ICEBERG SNAPSHOTS DF")
print(snapshots.head())

last_snapshot = str(snapshots[["snapshot_id"]].iloc[-1,0])
second_last_snapshot = str(snapshots[["snapshot_id"]].iloc[-2,0])

print("PICKING LAST AND SECOND LAST ICEBERG SNAPSHOTS FOR REPORT \n")

print(last_snapshot)
print(second_last_snapshot)

#last_snapshot = snapshots_df.select("snapshot_id").tail(1)[0][0]
#first_snapshot = snapshots_df.select("snapshot_id").head(1)[0][0]
print("INCREMENTAL REPORT\n")
print("SELECT ALL UPDATES BETWEEN THE SELECTED SNAPSHOTS\n")
spark.read\
    .format("iceberg")\
    .option("start-snapshot-id", second_last_snapshot)\
    .option("end-snapshot-id", last_snapshot)\
    .load("spark_catalog.CDE_WORKSHOP_{0}.CAR_SALES_{0}".format(username)).show()

#---------------------------------------------------
#               ANALYTICAL QUERIES
#---------------------------------------------------

"""reports_df = spark.sql("SELECT * FROM CDE_WORKSHOP_{0}.SALES_REPORT_{0}".format(username))

print("GROUP TOTAL SALES BY MODEL")
model_sales_df = reports_df.groupBy("Model").sum("Saleprice").na.drop().sort(F.asc('sum(Saleprice)')).withColumnRenamed("sum(Saleprice)", "sales_by_model")
model_sales_df = model_sales_df.withColumn('total_sales_by_model', model_sales_df.sales_by_model.cast(DecimalType(18, 2)))
model_sales_df.select(["Model", "total_sales_by_model"]).sort(F.asc('Model')).show()

print("GROUP TOTAL SALES BY GENDER")
gender_sales_df = reports_df.groupBy("Gender").sum("Saleprice").na.drop().sort(F.asc('sum(Saleprice)')).withColumnRenamed("sum(Saleprice)", "sales_by_gender")
gender_sales_df = gender_sales_df.withColumn('total_sales_by_gender', gender_sales_df.sales_by_gender.cast(DecimalType(18, 2)))
gender_sales_df.select(["Gender", "total_sales_by_gender"]).sort(F.asc('Gender')).show()
"""


import numpy as np
from scipy import stats
import seaborn as sns
import statsmodels.stats.api as sms

"""
Bir fabrikada rassal olarak seçilen 100 ürünün ortalama ağırlığı 1040 gr,
std sapması 25 gr olarak bulunmuştur. Bu fabrikada üretilen tüm ürünlerin
ortalama oğırlıkları %95 güven ile kaçtır?
"""
n = 100
x = 1040
s = 25
conf = 0.95
interval = stats.norm.interval(alpha=conf, loc=x, scale=s/np.sqrt(n))
print(interval)

"""
85 ev sahibi ile yapılan bir ankette, ev bakımına aylık ortalama 67$
(std sapma 14$) harcadıkları tespit edilmiştir. Tüm ev sahiplerinin
aylık ev bakım harcamaları için %95 güven aralığı oluşturunuz.
"""
interval = stats.norm.interval(alpha=0.95, loc=67, scale=14/np.sqrt(85))
print(interval)

"""
Piyasaya yeni sürülen bir ürünün uzunluğunun std sapması 2 cm'dir.
Rastgele seçilen 16 ürünün ortalama uzunluğu 4 cm olarak hesaplanmıştır.
%95 güvenle anakütle ortalamasını tahmin ediniz.(n<30 fakat pop. std sapması biliniyor)
"""
interval = stats.norm.interval(alpha=0.95, loc=4, scale=2/np.sqrt(16))
print(interval)

"""
Bir fabrikada üretilen margarin paketlerinin ağırlığının varyansı 100 gr'dır.
Rastgele seçilen 25 paketin ağırlığının ortalaması 120 gr'dır. Ana kütle ortalamasını
%90 ve %99 güvenle tahmin ediniz.
"""
interval = stats.norm.interval(alpha=0.9, loc=120, scale=np.sqrt(100)/np.sqrt(25))
print(interval)
interval = stats.norm.interval(alpha=0.99, loc=120, scale=np.sqrt(100)/np.sqrt(25))
print(interval)

"""
Bir firmanın ürettiği ürünlerin 100 tanesi rassal örneklem olarak seçilmiştir.
Ortalama ağırlık 385 gr ve std sapması 12 gr olarak hesaplanmıştır.
Üretilen ürünlerin ortalama ağırlığını %95 güven düzeyi ile tahmin ediniz.
"""
interval = stats.norm.interval(alpha=0.95, loc=385, scale=12/np.sqrt(100))
print(interval)

###########################################################################
# T Dist.
n = 30
x = 140
s = 25
conf = 0.95
df = n - 1
interval = stats.t.interval(alpha=conf, loc=x, df=df, scale=s/np.sqrt(n))
print(interval)

"""
Rassal olarak seçilen 20 bilgisayarın tamirat maliyetleri kaydedilmiştir.
Seçilen 20 bilgisayarın maliyet ortalamasının 216.53$ olduğu tespit edilmiştir.
Bu örneklemin std sapması 15.86$ olarak hesaplanmıştır.
Tüm bilgisayarların ortalama tamirat maliyetini %95 güven ile tahmin ediniz
"""
n = 20
x = 216.53
s = 15.86
conf = 0.95
df = n - 1
interval = stats.t.interval(alpha=conf, loc=x, df=df, scale=s/np.sqrt(n))
print(interval)

##########################################################################################
# tips veri setindeki sayısal değişkenler için güven aralığı
# confidence interval for numeric variables in tips dateset
df = sns.load_dataset("tips")
df.describe().T
sms.DescrStatsW(df["total_bill"]).tconfint_mean()
sms.DescrStatsW(df["tip"]).tconfint_mean()

# titanic veri setindeki sayısal değişkenler için güven aralığı
# confidence interval for numeric variables in titanic dateset
df = sns.load_dataset("titanic")
df.describe().T
sms.DescrStatsW(df["age"].dropna()).tconfint_mean()
sms.DescrStatsW(df["fare"].dropna()).tconfint_mean()


from os import path, listdir, pardir
from pathlib import Path

def table_of_contents():
    content = ""
    for dir in [p for p in listdir() if path.isdir(p) and not p.startswith(".")]:
        content += f"- [{dir}]({dir})\n"
        for file in [dir+"/"+p for p in listdir(dir) if p.endswith(".c")]:
            pdf = dir+"/"+Path(file).stem+".pdf"
            label = " ".join([a.capitalize() for a in Path(file).stem.replace("_"," ").split(" ")])
            content += f"    - **{label}** [[Code]({file})] [[Description]({pdf})]\n"
    return content

open("README.md","w",encoding="UTF-8").write(f"""
# Curriculum Challenges

This repository contains codefiles and challenge descriptions to all challenges in RobotC Virtual Worlds Curriculum.

## Challenges

{table_of_contents()}

## Code description

**For all code files applies that**:
- At the top there is defined robot to use to succesfully complete this challenge, so check it before challenge start to have correct robot in usage
- All constants are defined via C-keyword `#define` at the top
- I am trying to write code as efficient as possible

""".strip())


from lib import util
from lib.targetop import TargetOp

CS = "CS"
SCK = "SCK"
SI = "SI"
SO = "SO"
HOLD = "HOLD"
WP = "WP"

SIZE = 512
CHUNK = 64

Tcss = Tcsd = 500e-9
Tsu = 50e-9
Thd = 100e-9
Ths = 200e-9
Thi = Tlo = 475e-9
Twc = 5e-3

READ = "0000a011 aaaaaaaa"  # iiiiiiii*[0,512]
WRITE = "0000a010 aaaaaaaa"  # dddddddd*[1,16]
DATA_BYTE = "dddddddd"
# WRDI = "00000100"
WREN = "00000110"
RDSR = "00000101" + "xxxxxxxx"
# WRSR = "00000001 0000ddrr"

class Ee25lc040:
    """25LC040 SPI EEPROM
    CS SCK SI SO WP HOLD"""

    def __init__(self, pinproxy, progressbar):
        self.pinproxy = pinproxy
        self.progressbar = progressbar

    def _open(self):
        self.pinproxy.set_as_input(SO)
        for pin in (CS, SCK, SI, HOLD, WP):
            self.pinproxy.set_as_output(pin)
            self.pinproxy.reset_pin(pin)
        self.pinproxy.set_pin(HOLD)
        self.pinproxy.set_pin(WP)

    def read(self):
        self._open()
        for addr in range(0, SIZE, CHUNK):
            self.progressbar.update(addr, SIZE)
            cmd = util.cmd(READ, a=addr) + [0] * 8 * CHUNK
            self._pump(cmd, 16)
        return dict(enumerate(self.pinproxy.pop_fetched(SO)))

    def erase(self):
        self._open()
        self.write({})

    def write(self, mem):
        self._open()
        for page in range(0, SIZE, 16):
            data = [mem.get(page + address, 0xff) for address in range(16)]
            self.progressbar.update(page, SIZE)
            self._wren()
            self.rdsr()
            self._write_page(page, data)
        sr_stats = self.pinproxy.pop_fetched(SO)
        # status_register = _, _, _, _, BP1, BP0, WEL, WIP
        if not all(map(lambda sr: sr & 2, sr_stats)):
            raise Exception("Write failed.")

    def _write_page(self, address, data):
        assert 1 <= len(data) <= 16
        cmd = util.cmd(WRITE, a=address)
        for byte in data:
            cmd += util.cmd(DATA_BYTE, d=byte)
        self._pump(cmd)
        self.pinproxy.wait(Twc)

    def _wren(self):  # enable write
        self._pump(util.cmd(WREN))

#    def _wrdi(self):
#        self._pump(util.cmd(WRDI))

    def rdsr(self):  # read status register
        self._pump(util.cmd(RDSR), 8)

#    def _wrsr(self, bp1, bp0):  # write status register
#        self._pump(util.cmd(WRSR, d=bp1, r=bp0))

    def _pump(self, sequence, read_after=None):
        p = self.pinproxy
        p.reset_pin(CS)
        p.wait(Tcss)

        for i, bit in enumerate(sequence):
            p.set_pin(SI, bit)
            p.wait(Tsu)
            p.set_pin(SCK)
            p.wait(Thi)
            if read_after and i >= read_after:
                p.fetch_pin(SO)
            p.wait(Thd)
            p.reset_pin(SCK)
            p.wait(Tlo)

        p.wait(Tcsd)
        p.set_pin(CS)

@TargetOp
def read(pinproxy, progressbar):
    return Ee25lc040(pinproxy, progressbar).read()

@TargetOp
def write(pinproxy, progressbar, mem):
    return Ee25lc040(pinproxy, progressbar).write(mem)

@TargetOp
def erase(pinproxy, progressbar):
    return Ee25lc040(pinproxy, progressbar).erase()


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

import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm
import matplotlib as mpl

mpl.rcParams['axes.unicode_minus'] = False

## 한글 폰트
path = "/home/gsdc/.fonts/NanumGothicCoding.ttf"
font_name = fm.FontProperties(fname=path, size=50).get_name()
print(font_name)
plt.rc('font', family=font_name)

data = pd.read_csv("output.csv", header=None, names=["date", "journal","keyword","count"])

data.head()

dg =data.groupby(["keyword"]).agg({"count":sum})
dg_sort = dg.sort_values(['count'],ascending=False)

dgg = dg_sort.reset_index()
#dgg_filtered = dgg[ ~dgg.keyword.isin(["있다.", "수","등","있는"])].head(20)
#print(dgg_filtered.head(20))

print("주요 키워드?")
print(dgg.head(20))
### 특정 단어를

dgg[:20].plot(x="keyword",y="count")
plt.show()


from urllib.request import urlopen
from urllib.error import HTTPError
from bs4 import BeautifulSoup

def get_title(url):
    try:
        html = urlopen(url)
    except HTTPError as e:
        print("We can't take a page")
        return None

    try:
        bsObj = BeautifulSoup(html.read(), "html.parser")
        title = bsObj.body.h1
    except AttributeError as e:
        print("We can't find a tag")
        return None

    return title

title = get_title("http://www.pythonscraping.com/exercises/exercise1.html")
if title is None:
    print("We have a problem")
else:
    print(title)

projects from book/base_examples/first scraper.py


import pygame
import events

from scores import Scores
from hero import Hero
from sound import bg_music
from pearl import Pearl

import background as bg

def run():
    pygame.init()
    pygame.display.set_caption("Underwater Adventure")
    window = pygame.display.set_mode((bg.WIDTH, bg.HEIGHT))
    clock = pygame.time.Clock()
    hero = Hero(window)

    enemies = pygame.sprite.Group()
    friends = pygame.sprite.Group()
    group_pearls = pygame.sprite.Group()

    background = bg.Background()
    score = Scores(window)
    pygame.time.set_timer(pygame.USEREVENT, 10000)

    bg_music()
    while True:
        pearl = Pearl()  # создаем в цикле новый объект perl для разных картинок

        events.event(enemies, score, group_pearls, pearl, window)

        if score.game == True:
            background.update()
            background.render(window)
            hero.update()
            score.show_health(hero)
            score.draw_photo()
            score.finish(hero)
            events.make_friend(friends, window)
            events.make_fish(enemies, window, friends)
            events.move_pearl(window, group_pearls)
            events.collide(hero, enemies, group_pearls)
        pygame.display.update()
        clock.tick(60)

if __name__ == '__main__':
    run()