from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect, HttpResponse
from django.views.decorators.cache import never_cache
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import TemplateView

from data_services.models import DimUser, SimulationInputFile
from lib.templatetags.base_extras import set_notification

from ts_repr.models import RepresentativeWeather

class ManageWeatherView(TemplateView):
    template_name = 'ts_repr/managers/manage_weather.html'

    def get_context_data(self, **kwargs):
        context = super(ManageWeatherView, self).get_context_data(**kwargs)
        context['dim_user'] = DimUser.objects.get_or_create(username=self.request.user.username)[0]
        context['nav_button'] = 'managers'
        context['current_manager'] = 'weather'
        context['weather_data_url'] = "/ts_repr/weather/data/"

        context['active_weather'] = RepresentativeWeather.objects.filter(is_active=True).order_by('id')
        context['inactive_weather'] = RepresentativeWeather.objects.filter(is_active=False).order_by('id')

        return context

    def post(self, request):
        try:
            self.create_or_update_weather_files(request)
        except Exception as exception:
            set_notification('alert-error', 'Error! The entry may not have been saved! ' + str(exception), self.request.session)

        return HttpResponseRedirect(reverse('ts_repr.manage_weather'))

    def create_or_update_weather_files(self, request):
        username = "khostetl"
        metadata = {}

        weather_id = int(request.POST['weather_id'])
        name = request.POST['name']
        description = request.POST['description']

        if 'is_active' in request.POST:
            is_active = True
        else:
            is_active = False

        if 'rainfall_json' in request.FILES \
            and 'rainfall_bin' in request.FILES \
            and 'humidity_json' in request.FILES \
            and 'humidity_bin' in request.FILES \
            and 'temperature_json' in request.FILES \
            and 'temperature_bin' in request.FILES:
                are_files_here = True
        else:
            are_files_here = False

        if 'rainfall_file_location' in request.POST:
            rainfall_file_location = request.POST['rainfall_file_location']
        else:
            rainfall_file_location = ''

        if 'humidity_file_location' in request.POST:
            humidity_file_location = request.POST['humidity_file_location']
        else:
            humidity_file_location = ''

        if 'temperature_file_location' in request.POST:
            temperature_file_location = request.POST['temperature_file_location']
        else:
            temperature_file_location = ''

        # 0 means we are explicitly requesting a new object, else we are looking for a specific one that supposedly
        # already exists, but if it doesn't, it will be created anyway
        if weather_id == 0:
            weather = RepresentativeWeather.objects.create(name=name,
                                            short_description='a',
                                            description=description,
                                            is_active=is_active,
                                            emod_weather_rainfall_file_location=rainfall_file_location,
                                            emod_weather_humidity_file_location=humidity_file_location,
                                            emod_weather_temperature_file_location=temperature_file_location)
        else:
            try:  # Does exist, so modify with values from above
                weather = RepresentativeWeather.objects.get(id=weather_id)
                weather.name = name
                weather.description = description
                weather.is_active = is_active
                weather.emod_weather_rainfall_file_location = rainfall_file_location
                weather.emod_weather_humidity_file_location = humidity_file_location
                weather.emod_weather_temperature_file_location = temperature_file_location
            except:  # Doesn't exist, so fill in with values from above
                weather = RepresentativeWeather.objects.create(name=name,
                                                short_description='a',
                                                description=description,
                                                is_active=is_active,
                                                emod_weather_rainfall_file_location=rainfall_file_location,
                                                emod_weather_humidity_file_location=humidity_file_location,
                                                emod_weather_temperature_file_location=temperature_file_location)

        # File names to be used.
        rainfall_json_file_name_short = "rainfall.json"
        rainfall_bin_file_name_short = "rainfall.json.bin"
        humidity_json_file_name_short = "humidity.json"
        humidity_bin_file_name_short = "humidity.json.bin"
        temperature_json_file_name_short = "temperature.json"
        temperature_bin_file_name_short = "temperature.json.bin"

        if are_files_here:
            # Grab the contents from said files
            rainfall_json_file_contents = request.FILES['rainfall_json'].read()
            rainfall_bin_file_contents = request.FILES['rainfall_bin'].read()
            humidity_json_file_contents = request.FILES['humidity_json'].read()
            humidity_bin_file_contents = request.FILES['humidity_bin'].read()
            temperature_json_file_contents = request.FILES['temperature_json'].read()
            temperature_bin_file_contents = request.FILES['temperature_bin'].read()

            # Create the simulation input files of these weather files.
            rainfall_json_simulation_file = SimulationInputFile.objects.create_file(
                contents=rainfall_json_file_contents,
                name=rainfall_json_file_name_short,
                metadata=metadata,
                created_by=DimUser.objects.get(username=username)
            )

            rainfall_bin_simulation_file = SimulationInputFile.objects.create_file(
                contents=rainfall_bin_file_contents,
                name=rainfall_bin_file_name_short,
                metadata=metadata,
                created_by=DimUser.objects.get(username=username)
            )

            humidity_json_simulation_file = SimulationInputFile.objects.create_file(
                contents=humidity_json_file_contents,
                name=humidity_json_file_name_short,
                metadata=metadata,
                created_by=DimUser.objects.get(username=username)
            )

            humidity_bin_simulation_file = SimulationInputFile.objects.create_file(
                contents=humidity_bin_file_contents,
                name=humidity_bin_file_name_short,
                metadata=metadata,
                created_by=DimUser.objects.get(username=username)
            )

            temperature_json_simulation_file = SimulationInputFile.objects.create_file(
                contents=temperature_json_file_contents,
                name=temperature_json_file_name_short,
                metadata=metadata,
                created_by=DimUser.objects.get(username=username)
            )

            temperature_bin_simulation_file = SimulationInputFile.objects.create_file(
                contents=temperature_bin_file_contents,
                name=temperature_bin_file_name_short,
                metadata=metadata,
                created_by=DimUser.objects.get(username=username)
            )

            # Save the simulation input files
            rainfall_json_simulation_file.save()
            rainfall_bin_simulation_file.save()
            humidity_json_simulation_file.save()
            humidity_bin_simulation_file.save()
            temperature_json_simulation_file.save()
            temperature_bin_simulation_file.save()

            # Remove old files if they exist
            if weather.emod_weather is not None:
                for weather_file in weather.emod_weather.all():
                    weather.emod_weather.remove(weather_file)

            # Add all the files to the table
            weather.emod_weather.add(rainfall_json_simulation_file)
            weather.emod_weather.add(rainfall_bin_simulation_file)
            weather.emod_weather.add(humidity_json_simulation_file)
            weather.emod_weather.add(humidity_bin_simulation_file)
            weather.emod_weather.add(temperature_json_simulation_file)
            weather.emod_weather.add(temperature_bin_simulation_file)

        weather.save()
        set_notification('alert-success', 'Success! Successfully saved.', self.request.session)


import numpy as np
import torch
from torch import nn
from sklearn.datasets import load_iris
import torch.nn.functional as Fun

# chargement du jeu de données
X, y = load_iris(return_X_y=True)

in_features, out_features = X.shape[1], 3

# Transformation du jeu de données en tensor
X = torch.from_numpy(X.astype(np.float32))
y = torch.from_numpy(y.astype(np.float32))
output = Fun.one_hot(y, num_classes=3)
# plt.show()

## creation du réseau
class LogisticRegression(nn.Module):
    def __init__(self, in_features, out_features=1):
        super(LogisticRegression, self).__init__()
        self.linear = nn.Linear(in_features=in_features, out_features=out_features)

    def forward(self, x):
        x = self.linear(x)
        return x

model = LogisticRegression(in_features, out_features)
print(model(X))

# print(list(model.parameters()))
lr = 0.01
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=lr)

# training loop
n_iters = 100
for epoch in range(n_iters):
    # forward pass
    pred = model(X)

    # loss = criterion(pred, y)

    loss.backward()

    optimizer.step()

    optimizer.zero_grad()


import sys

# for linux env.
sys.path.insert(0, '..')
import time
import pickle
import argparse
from evaluation import *
import os
import random
import pandas as pd
import json
import matplotlib.pyplot as plt
import numpy as np
from PSModels import ml
from misc import utils
import itertools
import functools
from tqdm import tqdm

print = functools.partial(print, flush=True)

def parse_args():
    parser = argparse.ArgumentParser(description='process parameters')
    # Input
    parser.add_argument('--dataset', choices=['oneflorida', 'V15_COVID19'], default='oneflorida',
                        help='data bases')
    parser.add_argument('--site', choices=['COL', 'MSHS', 'MONTE', 'NYU', 'WCM', 'ALL', 'all'], default='all',
                        help='site dataset')
    parser.add_argument('--severity', choices=['all',
                                               'outpatient', 'inpatient', 'icu',
                                               'female', 'male',
                                               'white', 'black',
                                               'less65', '65to75', '75above',
                                               'Anemia', 'Arrythmia', 'CKD', 'CPD-COPD', 'CAD',
                                               'T2D-Obesity', 'Hypertension', 'Mental-substance', 'Corticosteroids'],
                        default='all')

    parser.add_argument("--random_seed", type=int, default=0)
    parser.add_argument('--negative_ratio', type=int, default=5)
    args = parser.parse_args()

    # More args
    args.data_file = r'../data/{}/output/character/matrix_cohorts_covid_4manuNegNoCovid_bool_{}.csv'.format(
        args.dataset,
        args.site)

    args.output_file = r'../data/{}/output/character/matrix_cohorts_covid_4manuNegNoCovid_bool_{}_withPS.csv'.format(
        args.dataset,
        args.site)

    if args.random_seed < 0:
        from datetime import datetime
        args.random_seed = int(datetime.now())

    # args.save_model_filename = os.path.join(args.output_dir, '_S{}{}'.format(args.random_seed, args.run_model))
    # utils.check_and_mkdir(args.save_model_filename)
    return args

def _evaluation_helper(X, T, PS_logits, loss):
    y_pred_prob = logits_to_probability(PS_logits, normalized=False)
    auc = roc_auc_score(T, y_pred_prob)
    max_smd, smd, max_smd_weighted, smd_w, before, after = cal_deviation(X, T, PS_logits, normalized=False,
                                                                         verbose=False)
    n_unbalanced_feature = len(np.where(smd > SMD_THRESHOLD)[0])
    n_unbalanced_feature_weighted = len(np.where(smd_w > SMD_THRESHOLD)[0])
    result = (loss, auc, max_smd, n_unbalanced_feature, max_smd_weighted, n_unbalanced_feature_weighted)
    return result

def _loss_helper(v_loss, v_weights):
    return np.dot(v_loss, v_weights) / np.sum(v_weights)

def summary_covariate(df, label, weights, smd, smd_weighted, before, after):
    # (covariates_treated_mu, covariates_treated_var, covariates_controlled_mu, covariates_controlled_var), \
    # (covariates_treated_w_mu, covariates_treated_w_var, covariates_controlled_w_mu, covariates_controlled_w_var)

    columns = df.columns
    df_pos = df.loc[label == 1, :]
    df_neg = df.loc[label == 0, :]
    df_pos_mean = df_pos.mean()
    df_neg_mean = df_neg.mean()
    df_pos_sum = df_pos.sum()
    df_neg_sum = df_neg.sum()
    df_summary = pd.DataFrame(index=df.columns, data={
        'Positive Total Patients': df_pos.sum(),
        'Negative Total Patients': df_neg.sum(),
        'Positive Percentage/mean': df_pos.mean(),
        'Positive std': before[1],
        'Negative Percentage/mean': df_neg.mean(),
        'Negative std': before[3],
        'Positive mean after re-weighting': after[0],
        'Negative mean after re-weighting': after[2],
        'Positive std after re-weighting': before[1],
        'Negative std after re-weighting': before[3],
        'SMD before re-weighting': smd,
        'SMD after re-weighting': smd_weighted,
    })
    # df_summary.to_csv('../data/V15_COVID19/output/character/outcome-dx-evaluation_encoding_balancing.csv')
    return df_summary

if __name__ == "__main__":
    # python data_add_ps.py --dataset V15_COVID19 --site ALL --severity all 2>&1 | tee  log/data_add_ps_insight_ALL_all.txt
    # python data_add_ps.py --dataset oneflorida --site all --severity all 2>&1 | tee  log/data_add_ps_oneflorida_all_all.txt

    start_time = time.time()
    args = parse_args()

    np.random.seed(args.random_seed)
    random.seed(args.random_seed)

    print('args: ', args)
    print('random_seed: ', args.random_seed)
    # print('save_model_filename', args.save_model_filename)

    # %% 1. Load  Data
    # Load Covariates Data
    print('Load data covariates file:', args.data_file)
    df = pd.read_csv(args.data_file, dtype={'patid': str}, parse_dates=['index date'])
    # because a patid id may occur in multiple sites. patid were site specific
    print('df.shape:', df.shape)
    if args.severity == 'inpatient':
        print('Considering inpatient/hospitalized cohorts but not ICU')
        df = df.loc[(df['hospitalized'] == 1) & (df['ventilation']==0) & (df['criticalcare']==0), :].copy()
    elif args.severity == 'icu':
        print('Considering ICU (hospitalized ventilation or critical care) cohorts')
        df = df.loc[(((df['hospitalized'] == 1) & (df['ventilation']==1)) | (df['criticalcare']==1)), :].copy()
    elif args.severity == 'outpatient':
        print('Considering outpatient cohorts')
        df = df.loc[(df['hospitalized'] == 0) & (df['criticalcare']==0), :].copy()
    elif args.severity == 'female':
        print('Considering female cohorts')
        df = df.loc[(df['Female'] == 1), :].copy()
    elif args.severity == 'male':
        print('Considering male cohorts')
        df = df.loc[(df['Male'] == 1), :].copy()
    elif args.severity == 'white':
        print('Considering white cohorts')
        df = df.loc[(df['White'] == 1), :].copy()
    elif args.severity == 'black':
        print('Considering black cohorts')
        df = df.loc[(df['Black or African American'] == 1), :].copy()
    elif args.severity == 'less65':
        print('Considering less65 cohorts')
        df = df.loc[(df['20-<40 years'] == 1) | (df['40-<55 years'] == 1) | (df['55-<65 years'] == 1), :].copy()
    elif args.severity == '65to75':
        print('Considering 65to75 cohorts')
        df = df.loc[(df['65-<75 years'] == 1), :].copy()
    elif args.severity == '75above':
        print('Considering 75above cohorts')
        df = df.loc[(df['75-<85 years'] == 1) | (df['85+ years'] == 1), :].copy()
    elif args.severity == 'Anemia':
        print('Considering Anemia cohorts')
        df = df.loc[(df["DX: Anemia"] == 1), :].copy()
    elif args.severity == 'Arrythmia':
        print('Considering Arrythmia cohorts')
        df = df.loc[(df["DX: Arrythmia"] == 1), :].copy()
    elif args.severity == 'CKD':
        print('Considering CKD cohorts')
        df = df.loc[(df["DX: Chronic Kidney Disease"] == 1), :].copy()
    elif args.severity == 'CPD-COPD':
        print('Considering CPD-COPD cohorts')
        df = df.loc[(df["DX: Chronic Pulmonary Disorders"] == 1) | (df["DX: COPD"] == 1), :].copy()
    elif args.severity == 'CAD':
        print('Considering CAD cohorts')
        df = df.loc[(df["DX: Coronary Artery Disease"] == 1), :].copy()
    elif args.severity == 'T2D-Obesity':
        print('Considering T2D-Obesity cohorts')
        df = df.loc[(df["DX: Diabetes Type 2"] == 1) | (df["DX: Severe Obesity  (BMI>=40 kg/m2)"] == 1), :].copy()
    elif args.severity == 'Hypertension':
        print('Considering Hypertension cohorts')
        df = df.loc[(df["DX: Hypertension"] == 1), :].copy()
    elif args.severity == 'Mental-substance':
        print('Considering Mental-substance cohorts')
        df = df.loc[(df["DX: Mental Health Disorders"] == 1) | (df['DX: Other Substance Abuse'] == 1), :].copy()
    elif args.severity == 'Corticosteroids':
        print('Considering Corticosteroids cohorts')
        df = df.loc[(df["MEDICATION: Corticosteroids"] == 1), :].copy()
    else:
        print('Considering ALL cohorts')
    # 'T2D-Obesity', 'Hypertension', 'Mental-substance', 'Corticosteroids'
    print('Severity cohorts:', args.severity, 'df.shape:', df.shape)
    df_info = df[['Unnamed: 0', 'patid', 'site', 'index date', 'hospitalized',
                  'ventilation', 'criticalcare', 'maxfollowup', 'death', 'death t2e']]
    df_label = df['covid']
    covs_columns = [x for x in
                    list(df.columns)[
                    df.columns.get_loc('20-<40 years'):(df.columns.get_loc('MEDICATION: Immunosuppressant drug') + 1)]
                    if not x.startswith('YM:')
                    ]
    print('len(covs_columns):', len(covs_columns))
    df_covs = df.loc[:, covs_columns].astype('float')

    print('df.shape:', df.shape)
    print('df_covs.shape:', df_covs.shape)

    # model = ml.PropensityEstimator(learner='LR', random_seed=args.random_seed).cross_validation_fit(df_covs, df_label, verbose=0)
    model = ml.PropensityEstimator(learner='LR', random_seed=args.random_seed, paras_grid = {
        'penalty': 'l2',
        'C': 0.03162277660168379,
        'max_iter': 200,
        'random_state': 0}).cross_validation_fit(df_covs, df_label, verbose=0)

    # #
    # , paras_grid = {
    #     'penalty': 'l2',
    #     'C': 0.03162277660168379,
    #     'max_iter': 200,
    #     'random_state': 0}
    ps = model.predict_ps(df_covs)
    model.report_stats()
    iptw = model.predict_inverse_weight(df_covs, df_label, stabilized=True, clip=False)
    smd, smd_weighted, before, after = model.predict_smd(df_covs, df_label, abs=False, verbose=True)
    plt.scatter(range(len(smd)), smd)
    plt.scatter(range(len(smd)), smd_weighted)
    plt.show()

    print('n unbalanced covariates before:after = {}:{}'.format(
        (smd > SMD_THRESHOLD).sum(),
        (smd_weighted > SMD_THRESHOLD).sum())
    )
    df['ps'] = ps
    df['iptw'] = iptw

    utils.check_and_mkdir(args.output_file)
    df.to_csv(args.output_file, index=False)

    print('Done! Total Time used:', time.strftime("%H:%M:%S", time.gmtime(time.time() - start_time)))


from datetime import datetime

from infra.api.projects_api import ProjectsApi
from main_config import config

def test_003_create_project():

    projects_api = ProjectsApi(config['base_url'])

    new_project_name = f"Auto Project {datetime.now().strftime('%d/%m/%Y %H:%M:%S')}"
    new_project_identifier = f"autoproj{datetime.now().strftime('%Y%m%d%H%M%S')}"

    payload = {
        "name": new_project_name,
        "identifier": new_project_identifier
    }

    project = projects_api.create_project(payload)

    assert project["name"] == new_project_name, f"New project name should be '{new_project_name}'"
    assert project["identifier"] == new_project_identifier, f"New project identifier should be '{new_project_identifier}'"


import os

import cv2
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import psutil
import torch
from torch.utils.data import  Dataset
import torchvision
import torchvision.transforms.functional as fn
from tqdm import tqdm

from utils import (DATA_DIR, REPO_DIR, SAVE_DIR, basic_df_columns,
                   extract_filename_info, get_files, read_image)

num_workers = psutil.cpu_count()
print(f"Available workers: {num_workers}")
seed = 42

class InsectImgDataset(Dataset):
    """
    Dataset class that can take a dataset directory as input.
    It creates a dataframe with all relevant insect info such as: sticky plate name, year, date etc.
    """

    def __init__(self, df=pd.DataFrame(), directory='', ext='.png', setting="fuji", img_dim=150, transform=None):
        self.setting = setting
        self.directory = os.path.join(str(directory), self.setting) if len(str(directory)) else ''
        self.ext = ext
        self.df = df

        if not len(self.directory):
            assert len(self.df), "You chose to use a pre-made dataframe."
        else:
            assert len(self.directory)
            assert not len(self.df), "You chose to use a directory and load its filenames into a dataframe."

            self.files = get_files(directory, ext=self.ext)

            self.df = pd.DataFrame(self.files, columns=['filename'])
            self.df = self.df.astype(str).reset_index(drop=True)

        self.img_dim = img_dim
        self.transform = transform

    def extract_df_info(self, fix_cols=False):
        info = []
        for row in tqdm(self.df.itertuples(), total=len(self.df), desc="Extracting info from filenames.."):
            info.append(extract_filename_info(row.filename, setting=self.setting))
        self.df = pd.DataFrame(info, columns=basic_df_columns)
        if fix_cols:
            self.df.columns = [' '.join(col).strip() for col in self.df.columns.values]
        if not len(self.df):
            raise ValueError("Dataframe was not loaded.")
        self.df.year = self.df.year.astype('int64')
        self.df.plate_idx = self.df.plate_idx.astype('int64')
        self.info_extracted = True

    def __len__(self):
        return len(self.df)

    def __getitem__(self, idx):
        if torch.is_tensor(idx):
            idx = idx.tolist()

        if len(self.df.columns) > 1:
            sample = self.df.loc[idx]
        else:
            print("Run extract_df_info to read image data.")
            sample = extract_filename_info(str(self.df.loc[idx].filename), setting=self.setting)
            return sample

        fname = sample["filename"]
        label = sample["label"]
        imgname = sample["imgname"]
        platename = sample["platename"]
        year = sample["year"]
        location = sample["location"]
        date = sample["date"]
        xtra = sample["xtra"]
        plate_idx = sample["plate_idx"]

        tensor_img = torchvision.io.read_image(fname)
        tensor_img = fn.resize(tensor_img, size=(150,150))

        _, width, height = tensor_img.shape#tensor_img.size()

        sample = {"tensor_img": tensor_img,
                "label": label,
                "imgname": imgname,
                "platename": platename,
                "filename": str(fname),
                "plate_idx": plate_idx,
                "location": location,
                "date": date,
                "year": year,
                "xtra": xtra,
                "width": width,
                "height": height}

        if self.transform:
            sample["tensor_img"] = self.transform(sample["tensor_img"])

        return tuple(sample.values())

    def plot_samples(self, df=pd.DataFrame(), noaxis=True, title='label'):
        if not len(df):
            df = self.df.sample(20, replace=False, random_state=seed).reset_index(drop=True)
        else:
            df = df.sample(20, replace=False, random_state=seed).reset_index(drop=True)

        plt.figure(figsize=(20,12))

        for i in tqdm(range(20)):
            plt.subplot(4,5,i+1)
            img = read_image(df.loc[i].filename)
            plt.imshow(img);
            if title == 'label':
                plt.title(df.loc[i].label)
            if noaxis:
                plt.axis('off')


#!/bin/env python
# -*- coding:utf-8 -*-
# -------------------------------------------------
# Filename:    dogit.py
# Revision:    1.0
# Date:        2015-04-22
# Author:      simonzhang
# Email:       simon-zzm@163.com
# web:         www.simonzhang.net
# -------------------------------------------------
import os
import sys
import time
from optparse import OptionParser

ssh_newkey = "Are you sure you want to continue connecting"

def now_time():
    from time import strftime
    return strftime('%Y-%m-%d %H:%M:%S')

# 创建新项目命令
def clone_git(argvs):
    import pexpect
    _line = "/usr/bin/git clone %s %s" % \
            (argvs.HOST, argvs.PATH)
    # 开始连接
    try:
        p = pexpect.spawn('%s' % _line)
        if  p.expect([ssh_newkey]) == 0:
            p.sendline("yes")
        frist_step = p.expect(['Enter passphrase',
                               pexpect.EOF,
                               pexpect.TIMEOUT,
                               'delta 0'], timeout=120)
    except:
        return "error.连接git库失败%s" % _line
    # 判断是否需要输入密码
    if frist_step == 0:
        try:
            p.sendline("%s" % argvs.PASSWD)
            two_step = p.expect([pexpect.EOF,
                                 pexpect.TIMEOUT], timeout=600)
        except:
            return "error.clone 连接后输入密码异常。请检查连接git服务器。"
        # 输入密码完毕获取结束值
        if two_step == 1:
            return "error.clone 输入密码失败。请检查git账号配置。"
        elif two_step == 2:
            return "error.clone 输入密码部分超时。请检查git服务器和部署服务器之间网络。"
        else:
            return "ok.create program and git clone。"
    elif frist_step == 1:
        return "error.连接git服务器出错。请检查获取git库配置。"
    elif frist_step == 2:
        return "error.连接git服务器超时。请检查git服务器和部署服务器之间网络。"
    elif frist_step == 3:
        p.sendline("\n")
        return "ok.clone end"
    else:
        return "error.反正就是错了"

# 已有新项目获取新代码命令
def update_git(argvs):
    import pexpect
    try:
        os.chdir("%s" % argvs.PATH)
    except:
        return "error.切换到git目录错误"
    # 开始连接
    try:
        p = pexpect.spawn("/usr/bin/git pull")
        frist_step = p.expect(['Enter passphrase',
                               pexpect.EOF,
                               pexpect.TIMEOUT,'Already', 'done'], timeout=10)
    except:
        return "error.pull git库异常。"
    # 判断是否需要输入密码
    if frist_step == 0:
        try:
            p.sendline("%s" % argvs.PASSWD)
            two_step = p.expect([pexpect.EOF,
                                 pexpect.TIMEOUT], timeout=600)
        except:
            return "error.pull 连接后输入密码异常。请检查连接git服务器。"
        # 输入密码完毕获取结束值
        if two_step == 1:
            return "error.pull 输入密码失败。请检查git账号配置。"
        elif two_step == 2:
            return "errro.pull 输入密码部分超时。请检查git服务器和部署服务器之间网络。"
        else:
            return "ok.program pull"
    elif frist_step == 1:
        return "error.pull git服务器出错。请检查获取git库配置。"
    elif frist_step == 2:
        return "error.pull git服务器超时。请检查git服务器和部署服务器之间网络。"
    elif frist_step == 3 or frist_step == 4:
        return "ok.program pull"
    else:
        return "error.反正是错了。"

# 打包压缩出指定版本
def archive_git(argvs):
    _line = "cd %s ;/usr/bin/git archive -o ../%s.zip  %s" % \
                (argvs.PATH, argvs.PROGRAM, argvs.ID)
    # 开始连接
    try:
        p = os.popen("%s" % _line).read()
    except:
        return "error.安gitid打包异常。"
    if len(p) == 0:
        return "ok.archive to zip"
    else:
        return "error.按gitid打包错误:%s。" % p

# 直接用zip打包代码，不带版本信息。
def zip_git(argvs):
    _line = "cd %s;/usr/bin/zip -q -r ../%s.zip *" % \
               (argvs.PATH, argvs.PROGRAM)
    # 开始执行
    try:
        p = os.popen("%s" % _line).read()
    except:
        return "error.不带版本号打zip包异常"
    if len(p) == 0:
        return "ok.to zip"
    else:
        return "error.不带版本号打zip包错误信息:%s" % p

# 切换分支部分
def switch_branch(argvs):
    try:
        os.chdir("%s" % argvs.PATH)
    except:
        return "error.切换分支过程，切换到git目录错误%s" % argvs.PATH
    _line = "cd %s;/usr/bin/git branch" % \
               (argvs.PATH)
    try:
        p = os.popen("%s" % _line).read()
    except:
        return "error.获取branch信息出错"
    getStatus = "error"
    for one in p.split('\n'):
        if one.find("*") > -1 and \
           str.strip(one).split(' ')[1] == "%s" % (argvs.BRANCH):
               getStatus = "ok"
    #如果不在分支则切换
    if getStatus == "error":
        _line = "cd %s;/usr/bin/git checkout %s" % \
                   (argvs.PATH, argvs.BRANCH)
        # 开始执行
        try:
            p = os.popen("%s" % _line).read()
        except:
            return "error.进入部署环境中的项目git目录异常。"
        if p.find("Branch") == 0 or len(p) == 0 or p.find("can be fast-forwarded") > -1:
            return "ok.checkout ok."
        else:
            return "error.切换主干或分支错误信息:%s" % p
    else:
        return "ok.已经是该分支不用切换"

# 检查输入id是否存在，
def check_shaid(argvs):
    try:
        os.chdir("%s" % argvs.PATH)
    except:
        return "error.验证gitid，切换到git目录错误"
    _line = "cd %s;/usr/bin/git log -500 |grep commit|awk ' ''{print $2}'" % \
            (argvs.PATH)
    try:
        p = os.popen("%s" % _line).read()
    except:
        return "error.获取该项目全部gitid 异常"
    # 开始判断
    get_num = p.find("%s" % argvs.ID)
    if get_num > -1:
        _status =  "ok.sha id have"
    else:
        _status = "error.没有找到要部署的gitid。"
    return _status

def main():
    usage = """python dogit.py [-H] [-p] [-P] [-g] [-h] [-i] [-b] \
python dogit.py -Hx.x.x.x -pxxxx -P/home/git/deployData/app -g app -i git_id -b branch"""
    parser = OptionParser(usage=usage)
    parser.add_option('-H', '--HOST',
                      help='git server is ip.',
                      action='store', dest='HOST')
    parser.add_option('-p', '--PASSWD',
                      default = "",
                      help='get git sshkey password',
                      action='store', dest='PASSWD')
    parser.add_option('-P', '--PATH',
                      #default="NUll",
                      help='the program save local path',
                      action='store', dest='PATH')
    parser.add_option('-g', '--PROGRAM',
                      #default="NUll",
                      help='the program name',
                      action='store', dest='PROGRAM')
    parser.add_option('-i', '--ID',
                      default="0",
                      help='the program git SHA-1',
                      action='store', dest='ID')
    parser.add_option('-b', '--branch',
                      default="master",
                      help='the program branch name',
                      action='store', dest='BRANCH')
    (opts, args) = parser.parse_args()
    if len(sys.argv) == 1:
        print('python dogit.py -h')
    elif type(opts.HOST) == type(None):
        print("must have git IP.")
    elif type(opts.PROGRAM) == type(None):
        print('must have program name')
    elif type(opts.PATH) == type(None):
        print('must have save local path')
    else:
        # 判断项目是否存在
        # 不存在调用创建，存在调用获取
        from os import path
        if not path.isdir(opts.PATH):
            get_v = clone_git(opts)
            if get_v.split('.')[0] == 'error':
                print(get_v.split('.')[1])
                sys.exit()
        # 如果branch name为空则切git master，不为空则切branch目录
        if len(str.strip(opts.BRANCH)) == 0:
            opts.BRANCH = 'master'
        # 切换分支或主干
        get_v = switch_branch(opts)
        time.sleep(1)
        if get_v.split('.')[0] == 'error':
            print(get_v.split('.')[1])
            sys.exit()
        # 获取最新代码
        get_v = update_git(opts)
        time.sleep(1)
        if get_v.split('.')[0] == 'error':
            print(get_v.split('.')[1])
            sys.exit()
        # 是否有指定git id。有使用archive打包，没有用zip直接打所有文件包,剔除git版本库信息。
        # print "git id %s" % opts.ID
        if opts.ID == "0":
            get_v = zip_git(opts)
            if get_v.split('.')[0] == 'error':
                print(get_v.split('.')[1])
                sys.exit()
        else:
            # 判断项目中是否包含输入id,不包含则退出
            get_v = check_shaid(opts)
            if get_v.split('.')[0] == 'error':
                print(get_v.split('.')[1])
                sys.exit()
            # gitid存在开始按照gitid打包
            else:
                get_v = archive_git(opts)
                if get_v.split('.')[0] == 'error':
                    print(get_v.split('.')[1])
                    sys.exit()
                else:
                    print("ok.更新完成")
        sys.exit()

if __name__ == "__main__":
    main()


from coinor.grumpy.polyhedron2D import Polyhedron2D, Figure

from matplotlib import rcParams

from matplotlib.legend_handler import HandlerPatch
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt

def make_legend_arrow(legend, orig_handle,
                      xdescent, ydescent,
                      width, height, fontsize):
    p = mpatches.FancyArrow(0, 0.5*height, width, 0,
                            length_includes_head=True,
                            head_width=0 )
    return p

rcParams["mathtext.fontset"] = 'cm'

A = [[ 1, -2],
     [-0, -1],
     [-2, -1],
     [-1, -0],
     [-1, -1],
     [1, -3],
     [0,  1],
     [1, 0]]

b = [4,
     0,
     -3,
     0,
     -2,
     3,
     4,
     8]

c = [1, -0.6]
sense = ['Max', '<=']
integerIndices = [0, 1]

#This is for the separation code
#x0 = [1, 1]
x_sep = [0, 1]

points = [[1, 1],
                 [8, 2],
                 [3, 0],
                 [2, 0]
]

locations = [[0.5, 0.7],
             [8.1, 2.1],
             [2.8, -0.35],
             [1.6, -0.4]
]

d = [[0.2, -1.4],
     [-0.5, -1],
     [-1, -1]
     ]

P = Polyhedron2D(A = A, b = b)

iter = 3

f = Figure()

for iter in [1,2,3]:

    rays = [[0,0]]

    for i, e in enumerate(points):
        if (i <= iter):
            f.add_point(e, radius = 0.05, color = 'mediumblue')
            f.add_text(locations[i], f'$x^{i}$')
            if (i != 0):
                rays.append([e[0] - points[0][0],
                             e[1] - points[0][1]])

    C = f.ax.arrow(points[0][0], points[0][1], c[0], c[1],
                   head_width = 0.15, head_length = 0.15,
                   overhang = 0.3, color = 'navy', alpha = 0.7, linewidth = 2)

    D = f.ax.arrow(points[0][0], points[0][1],
                   d[iter-1][0], d[iter-1][1],
                   head_width = 0.15, head_length = 0.15,
                   overhang = 0.3, color = 'indianred',
                   alpha = 0.7, linewidth = 2)

    a = f.ax.arrow(points[0][0], points[0][1],
                   1.2*rays[1][0], 1.2*rays[1][1],
                   head_width = 0.15, head_length = 0.15,
                   overhang = 0.3, color = 'coral',
                   alpha = 0.7, linewidth = 2)
    #linestyle = (0, (1,2))

    if (iter == 2):
        f.ax.arrow(points[0][0], points[0][1],
                   1.2*rays[2][0], 1.2*rays[2][1],
                   head_width = 0.15, head_length = 0.15,
                   overhang = 0.3, color = 'coral',
                   alpha = 0.7, linewidth = 2)

    if (iter == 3):
        f.ax.arrow(points[0][0], points[0][1],
                   1.3*rays[3][0], 1.3*rays[3][1],
                   head_width = 0.15, head_length = 0.15,
                   overhang = 0.3, color = 'coral',
                   alpha = 0.7, linewidth = 2)

    #f.ax.annotate('', xytext = (points[0][0], points[0][1]),
    #              xy = (points[0][0]+1.4*rays[2][0],
    #                    points[0][1]+1.4*rays[2][1]),
    #              arrowprops = {'arrowstyle':'->', 'linestyle':'--'},
    #               head_width = 0.2, head_length = 0.2,
    #               color = 'red', linestyle = (5, (1,10))
    #)

    p = f.add_polyhedron(P, show_int_points = True,
                         color = 'steelblue', linewidth = 2)
    if (iter > 1):
        f.ax.fill([(points[i][0] + 0.4*rays[i][0]) for i in [0,1,iter]],
                  [(points[i][1] + 0.4*rays[i][1]) for i in [0,1,iter]],
                  color = 'peachpuff')

    f.set_xlim(P.xlim)
    f.set_ylim(P.ylim)

    #red_patch = mpatches.Patch(color='red', label='The red data', lw = 0.2,
    #                           linestyle = '-')
    #plt.legend(handles=[red_patch])

    if (iter == 1):
        plt.legend([p, a, C, D], ['$\mathrm{conv}(\mathcal{S}$)',
                                  '$\mathcal{C}_1(x^0)$',
                                  '$c = d^0$', '$d^1$'],
                   handler_map={mpatches.FancyArrow : HandlerPatch(patch_func=make_legend_arrow)})
    else:
        plt.legend([p, a, C, D], ['$\mathrm{conv}(\mathcal{S}$)',
                                  '$\mathcal{C}_'+f'{iter}(x^0)$',
                                  '$c$', f'$d^{iter}$'],
                   handler_map={mpatches.FancyArrow : HandlerPatch(patch_func=make_legend_arrow)})

    plt.savefig(f'inv-iter{iter}-new.png')
    f.show()


# -*- coding:utf-8 -*-
import os
import configparser

cp = configparser.ConfigParser()
cp.read(os.path.dirname(os.path.abspath(__file__))+'/config.ini', encoding='utf-8')

# WX_BOT相关
WX_ROBOT_ID = cp.get('WX_BOT', 'wx_robot_id')
WX_ROBOT_WORK_GROUP = str(cp.get('WX_BOT', 'wx_robot_work_group')).split(',')
WX_ROBOT_KW_REPLY = cp.get('WX_BOT', 'wx_robot_kw_reply')
WX_ROBOT_AT_KW_REPLY = cp.get('WX_BOT', 'wx_robot_at_kw_reply')
MOVIE_SEARCH_URL = cp.get('WX_BOT', 'movie_search_url')
MUSIC_SEARCH_URL = cp.get('WX_BOT', 'music_search_url')
MUSIC_COVER_URL = cp.get('WX_BOT', 'music_cover_url')
MUSIC_DETAIL_URL = cp.get('WX_BOT', 'music_detail_url')
WELCOME_IMG_URL = cp.get('WX_BOT', 'welcome_img_url')
DY_JX_URL = cp.get('WX_BOT', 'dy_jx_url')
MUSIC_VKEY_URL = 'https://u.y.qq.com/cgi-bin/musicu.fcg?-=getplaysongvkey05137740976859173&' \
                 'g_tk=5381&loginUin=0&hostUin=0&format=json&inCharset=utf8&outCharset=utf-8&' \
                 'notice=0&platform=yqq.json&needNewCode=0&data=%7B%22req%22%3A%7B%22module' \
                 '%22%3A%22CDN.SrfCdnDispatchServer%22%2C%22method%22%3A%22GetCdnDispatch' \
                 '%22%2C%22param%22%3A%7B%22guid%22%3A%22953482270%22%2C%22calltype%22%3A0%2C%22' \
                 'userip%22%3A%22%22%7D%7D%2C%22req_0%22%3A%7B%22module%22%3A%22vkey.GetVkeyServer' \
                 '%22%2C%22method%22%3A%22CgiGetVkey%22%2C%22param%22%3A%7B%22guid%22%3A%22953482270%22%2C%22' \
                 'songmid%22%3A%5B%22@songmid@%22%5D%2C%22songtype%22%3A%5B0%5D%2C%22uin%22%3A%220%22%2C%22' \
                 'loginflag%22%3A1%2C%22platform%22%3A%2220%22%7D%7D%2C%22comm%22%3A%7B%22uin%22%3A0%2C%22' \
                 'format%22%3A%22json%22%2C%22ct%22%3A24%2C%22cv%22%3A0%7D%7D'
# 新浪短地址服务
SINA_URL = str(cp.get('SHORT_URL', 'sina_url'))
SINA_KEY = str(cp.get('SHORT_URL', 'sina_appkey'))
# 天气预报接口url
WEATHER_URL = str(cp.get('WEATHER', 'weather_url'))
# 百度机器人
BOT_URL = str(cp.get('BD_BOT', 'bot_url'))
BOT_OAUTH = str(cp.get('BD_BOT', 'oauth_url'))
BOT_API_KEY = str(cp.get('BD_BOT', 'apikey'))
BOT_API_SECRET = str(cp.get('BD_BOT', 'apisecret'))
BOT_ID = str(cp.get('BD_BOT', 'service_id'))
# 关键字功能
FUNC_MAP, AT_FUNC_MAP = dict(), dict()
FUNC_KW_LIST, AT_FUNC_KW_LIST = [], []
# 配置聊天关键字功能
for f in str(WX_ROBOT_KW_REPLY).split(';'):
    kvs = str(f).split(':')
    k = kvs[0]
    for kk in k.split(','):
        if kk:
            FUNC_KW_LIST.append(kk)
            FUNC_MAP[kk] = kvs[1]
# 配置at(@)聊天关键字功能
for ff in str(WX_ROBOT_AT_KW_REPLY).split(';'):
    kvs = str(ff).split(':')
    k = kvs[0]
    for kk in k.split(','):
        if kk:
            AT_FUNC_KW_LIST.append(kk)
            AT_FUNC_MAP[kk] = kvs[1]
# func type
FUNC_TYPE_AT = 'at'
FUNC_TYPE_NL = 'normal'


from collections import defaultdict
import sys
import time

class GDE(object):

    """
    implements a very simple version of the hitting set algorithm behind the GDE diagnosis algorithm
    (de Kleer and Williams 1987: Diagnosing Multiple Faults). Note that there is NO predication algorithm
    /ATMS or whatsoever here.
    This code is based on the pseudo-code in "A Generalized Minimal Hitting-Set Algorithm to Handle
    Diagnosis With Behavioral Modes" (Mattias Nyberg, 2011) - Algorithm 1.
    """

    def __init__(self, oracle, handle=None, options={}):
        self.oracle = oracle
        self.options = options
        self.delta = handle
        self.scs = None
        self.min_card = options.get('min_card',1)
        self.max_card = options.get('max_card',sys.maxint)
        self.max_time = options.get('max_time',None)

    def start(self):
        max_time = self.max_time

        if max_time:
            end_time = time.time() + max_time
        else:
            end_time = None

        self.scs = list(self.oracle.get_conflict_sets())
        if not self.delta:
            self.delta = defaultdict(list)
            for c in self.scs[0]:
                self.delta[1] += [(frozenset([c]),0)]

        csi = 0
        for cs in self.scs[1:]:
            csi += 1
            self.gde(cs, csi)
            if (end_time and time.time() > end_time) or len(self.delta) == 0:
                break

        result = set()
        for lst in self.delta.values():
            for sol, _ in lst:
                result.add(frozenset(sol))
        return result

    def gde(self, c, idx):

        for card in self.delta.keys():
            if card > self.max_card:
                continue

            delta_card = self.delta[card]
#            while self.delta[card]:
            i = 0
            while i < len(delta_card):
#                if self.delta[card][0][1] >= idx:
#                    break

#                delta_i, _ = self.delta[card].pop(0) # maybe use queues here and their popleft() method
                delta_i = delta_card[i][0]

                if len(delta_i & c) == 0:
                    del delta_card[i]
                    if len(delta_i) < self.max_card:
                        for c_i in c:
                            delta_new = delta_i | frozenset([c_i])
    #                        if len(delta_new) <= self.max_card:
                            found_subset = False
                            for k in self.delta:
                                if k < len(delta_new):
                                    for delta_k, _ in self.delta[k]:
                                        if delta_k <= delta_new:
                                            found_subset = True
                                            break

                            if not found_subset:
                                self.delta[len(delta_new)].append((delta_new, idx))

                    #end for c_i
                else:
#                    self.delta[len(delta_i)].append((delta_i, idx))
                    delta_card[i] = (delta_i,idx)
                    i = i + 1


'''
给你一个字符串 s ，请你去除字符串中重复的字母，使得每个字母只出现一次。需保证 返回结果的字典序最小（要求不能打乱其他字符的相对位置）。
注意：该题与 1081 https://leetcode-cn.com/problems/smallest-subsequence-of-distinct-characters 相同
示例 1：
输入：s = "bcabc"
输出："abc"
示例 2：
输入：s = "cbacdcbc"
输出："acdb"
'''
import collections

class Solution(object):
    # 时间复杂度O(n)，空间复杂度O(n)
    def removeDuplicateLetters(self, s):
        if not s:
            return s
        stack = []
        count = collections.Counter(s)
        for c in s:
            if c not in stack:
                while stack and c < stack[-1] and count[stack[-1]] > 0:
                    stack.pop()
                stack.append(c)
            count[c] -= 1
        return ''.join(stack)

if __name__ == '__main__':
    u = Solution()
    print(u.removeDuplicateLetters("bcabc"))  # abc
    print(u.removeDuplicateLetters("cbacdcbc"))  # acdb

Hot100之外的高频面试题/316-去除重复字母.py


from os import listdir
from os.path import isfile, join
import comare
import re
import transliterate

files= [f for f in listdir("izbs") if isfile(join("izbs", f))]

result = ""
scscs = 0
for f in files:
    scscs += 1
    print(f)
    try:
        my_file = open("izbs/"+f, "r", encoding="utf-8")
        lines = my_file.readlines()
        j = 0
        for st in lines:
            if re.search("[а-яА-я]", st):
                try:
                    result = transliterate.translit(st, reversed=True)
                except:
                    x = 1
                lines[j] = result
            j+=1
        my_file.close()
        my_file = open("izbs/"+f, "w")
        for line in lines:
            my_file.write(line)
    except:
        print("Hitlar file")

# my_file = open("izbs/2999126_OK_91.obj", "r", encoding="utf-8")
# j = 0
# lines = my_file.readlines()
# # print(transliterate.translit( lines[19393], reversed=True))
# for st in lines:
#     if re.search("[а-яА-я]", st):
#         print('true')
#     result=st
#     try:
#         result = transliterate.translit(st, reversed=True)
#     except:
#         x = 1
#     lines[j] = result
#     j+=1
# my_file.close()
# my_file = open("izbs/2999126_OK_91.obj", "w")
# for line in lines:
#     my_file.write(line)
# my_file.close()

# j=0
# lines = ["жопа", "xyi", "сися"]
# for st in lines:
#     len_st = len(st)
#     if re.match("""^[а-яА-ЯёЁ][а-яё0-9 !?:;"'.,]+$""", st):
#         print(st)
#         result=""
#         for i in range(0,len_st):
#             if st[i] in alphabet:
#                 simb = dic[st[i]]
#             else:
#                 simb = st[i]
#             result = result + simb
#         lines[j] = result
#     j+=1
# print(lines)


import pyrealsense2 as realsense
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy
import time
import struct

import sys
if sys.version_info.major < 3 or sys.version_info.minor < 10:
    exit(f"You're running Python version {sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}. At least 3.10 is required!")

import logging

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s : %(name)s : %(levelname)s : %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)

def _start_stream(realsense_config):
    """Create a context object. This object owns the handles to all connected realsense devices."""
    pipe = realsense.pipeline()
    config = realsense.config()

    config.enable_stream(realsense.stream.depth,
            width=realsense_config['resolution'][0],
            height=realsense_config['resolution'][1],
            format=realsense.format.z16,
            framerate=realsense_config['fps'])

    config.enable_stream(realsense.stream.color,
            width=realsense_config['resolution'][0],
            height=realsense_config['resolution'][1],
            format=realsense.format.rgb8,
            framerate=realsense_config['fps'])
    profile = pipe.start(config)
    return pipe, profile

def _get_frame(pipe, BYTES=True):
    """Gets new RGB and depth frames, and a new timestamp."""
    dataFrame = pipe.wait_for_frames()

    # Convert time reference point from epoch to same as time.perf_counter() and representation from milliseconds to seconds
    timestamp = dataFrame.get_timestamp()*1e-3 + time.perf_counter_ns()*1e-9 - time.time_ns()*1e-9

    rgb_image = numpy.array(dataFrame.get_color_frame().get_data(), dtype='<B')
    depth_image = numpy.array(dataFrame.get_depth_frame().get_data(), dtype='<H')

    #print(f'{timestamp}, {time.perf_counter()}')

    if BYTES:
        return {'RGB': rgb_image.tobytes('C'), 'D': depth_image.tobytes('C'), 'timestamp': timestamp }
    else:
        return {'RGB': rgb_image, 'D': depth_image, 'timestamp': round(dataFrame.get_timestamp()) }

def record(realsense_config, out, signals):
    """Starts the sensor, reads data, and writes into `out` -queue"""
    pipe, profile = _start_stream(realsense_config)
    logger.debug("Camera started!")
    qsize = signals.qsize()
    signals.put('STARTED')
    while signals.qsize() > qsize:
        pass

    first_frame = _get_frame(pipe)
    rgb_res = f"{realsense_config['resolution'][0]}x{realsense_config['resolution'][1]}"
    d_res = f"{realsense_config['resolution'][0]}x{realsense_config['resolution'][1]}"

    metadata = {
            'RGB_resolution': rgb_res,
            'D_resolution': d_res,
            'framerate': realsense_config['fps'] ,
            'start_time': first_frame['timestamp'] }
    out.put(metadata)
    out.put(first_frame)

    while signals.empty() or signals.get() != 'STOP':
        out.put(_get_frame(pipe))

if __name__ == '__main__':
    """Used for testing"""
    def _update_frame(self, pipe, rgb, depth):
        start = time.perf_counter()
        frame = _get_frame(pipe, BYTES=True)
        #print(f'FPS: {1/(time.perf_counter() - start)}')
        rgb.set_data(numpy.frombuffer(frame['RGB'], dtype='<B', count=640*480*3).reshape((480,640,3)))
        depth.set_data(numpy.frombuffer(frame['D'], dtype='<H', count=640*480).reshape((480,640)))
        return rgb, depth

    realsense_config = {
                'resolution' : [640, 480],
                'fps' : 30
            }

    pipe, profile = _start_stream(realsense_config)

    # Make a window to show camera stream in
    fig = plt.figure()
    rgb_ax = fig.add_subplot(121)
    depth_ax = fig.add_subplot(122)

    # Start streaming data from depth camera
    frame = _get_frame(pipe, BYTES=False)

    rgb_im = rgb_ax.imshow(frame['RGB'])
    depth_im = depth_ax.imshow(frame['D'], vmin=0, vmax=10000) # 10 m in millimeters

    cam_animation = FuncAnimation(fig, _update_frame, fargs=(pipe, rgb_im, depth_im), interval= 1e-3/realsense_config['fps'], blit=True)

    plt.show()


from datetime import datetime

import ihm.console as ihm

def saisie_date(message, date_seuil, duree_limite=1):
    """
    Methode qui permet de saisir la date du vol

    :param message:
    :param date_seuil:
    :param duree_limite:
    :return: la date saisie
    """

    date_saisie = None
    dans1an = date_seuil.replace(year=date_seuil.year + duree_limite)
    while True:
        try:
            date_saisie = ihm.demander(
                "Saisissez la {} (AAAA-MM-JJ) :".format(message))
            date_saisie = datetime.strptime(date_saisie, '%Y-%m-%d')
            if date_saisie <= date_seuil or date_saisie > dans1an:
                raise ValueError
        except ValueError:
            ihm.afficher("Ceci n'est pas une date valide.")
            pass
        else:
            break
    ihm.afficher("Vous avez choisi le {:%d/%m/%Y}".format(date_saisie))
    return date_saisie

def saisie_aeroport(message, aeroports):
    """
    Methode qui permet de saisir un aeroport

    :param message:
    :param aeroports:
    :return:
    """

    aero = None
    while aero is None:
        code = ihm.demander(
            "Saisissez l'{} (code IATA ou ICAO ou ville) :".format(message))
        results = [x for x in aeroports
                   if x.id_code_iata == code or x.code_icao == code or
                   x.municipalite.lower().startswith(code)]
        if len(results) == 0:
            ihm.afficher("Désolé, nous n'avons pas trouvé votre aéroport !")
        elif len(results) > 1:
            aero = ihm.choisir_paginer(results, "Précisez votre choix :")
        else:
            aero = results[0]
    ihm.afficher("Vous avez choisi : {}".format(aero))
    return aero

def saisie_compagnie(compagnies):
    """
    Methode qui permet de choisir une compagnie par ses différents codes

    :param compagnies: liste des compagnies
    :return: la compagnie choisie
    """

    compagnie = None
    code = ihm.demander(
        "Tapez le code IATA (2 caractères) ou ICAO (3 caractères) :")
    results = [x for x in compagnies
               if x.id_code_iata == code or x.code_icao == code]
    if len(results) == 0:
        ihm.afficher("Désolé, nous n'avons pas trouvé votre compagnie !")
    else:
        compagnie = results[0]
    if compagnie is not None:
        ihm.afficher("Vous avez choisi la compagnie {}".format(compagnie))
    return compagnie


__copyright__ = "Copyright © Stichting SciPost (SciPost Foundation)"
__license__ = "AGPL v3"

import datetime
import hashlib
import random
import string

from django.urls import reverse
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.postgres.fields import ArrayField
from django.db import models
from django.utils import timezone

from .behaviors import TimeStampedModel, orcid_validator
from .constants import (
    NORMAL_CONTRIBUTOR,
    DISABLED,
    TITLE_CHOICES,
    INVITATION_STYLE,
    INVITATION_TYPE,
    INVITATION_CONTRIBUTOR,
    INVITATION_FORMAL,
    AUTHORSHIP_CLAIM_PENDING,
    AUTHORSHIP_CLAIM_STATUS,
    CONTRIBUTOR_STATUSES,
    NEWLY_REGISTERED,
)
from .fields import ChoiceArrayField
from .managers import (
    ContributorQuerySet,
    UnavailabilityPeriodManager,
    AuthorshipClaimQuerySet,
)

from conflicts.models import ConflictOfInterest

today = timezone.now().date()

def get_sentinel_user():
    """Temporary fix to be able to delete Contributor instances.

    Eventually the 'to-be-removed-Contributor' should be status: "deactivated" and anonymized.
    Fallback user for models relying on Contributor that is being deleted.
    """
    user, __ = get_user_model().objects.get_or_create(username="deleted")
    return Contributor.objects.get_or_create(status=DISABLED, user=user)[0]

class TOTPDevice(models.Model):
    """
    Any device used by a User for 2-step authentication based on the RFC 6238 TOTP protocol.
    """

    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    name = models.CharField(max_length=128)
    token = models.CharField(max_length=16)
    last_verified_counter = models.PositiveIntegerField(default=0)

    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)

    class Meta:
        default_related_name = "devices"
        verbose_name = "TOTP Device"

    def __str__(self):
        return "{}: {}".format(self.user, self.name)

class Contributor(models.Model):
    """Contributor is an extension of the User model.

    *Professionally active scientist* users of SciPost are Contributors.

    The username, password, email, first_name and last_name are inherited from User.

    Other information is carried by the related Profile.
    """

    profile = models.OneToOneField(
        "profiles.Profile", on_delete=models.SET_NULL, null=True, blank=True
    )
    user = models.OneToOneField(
        settings.AUTH_USER_MODEL, on_delete=models.PROTECT, unique=True
    )
    invitation_key = models.CharField(max_length=40, blank=True)
    activation_key = models.CharField(max_length=40, blank=True)
    key_expires = models.DateTimeField(default=timezone.now)
    status = models.CharField(
        max_length=16, choices=CONTRIBUTOR_STATUSES, default=NEWLY_REGISTERED
    )
    address = models.CharField(max_length=1000, verbose_name="address", blank=True)
    vetted_by = models.ForeignKey(
        "self",
        on_delete=models.SET(get_sentinel_user),
        related_name="contrib_vetted_by",
        blank=True,
        null=True,
    )
    # If this Contributor is merged into another, then this field is set to point to the new one:
    duplicate_of = models.ForeignKey(
        "scipost.Contributor",
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name="duplicates",
    )

    objects = ContributorQuerySet.as_manager()

    class Meta:
        ordering = ["user__last_name", "user__first_name"]

    @property
    def roles(self):
        r = []
        if self.user.is_superuser:
            r.append("su")
        if self.user.is_staff:
            r.append("st")
        return r if len(r) > 0 else None

    def __str__(self):
        val = "%s, %s" % (self.user.last_name, self.user.first_name)
        if self.user.is_superuser:
            val += " (su)"
        return val

    def save(self, *args, **kwargs):
        """Generate new activitation key if not set."""
        if not self.activation_key:
            self.generate_key()
        return super().save(*args, **kwargs)

    def get_absolute_url(self):
        """Return public information page url."""
        return reverse("scipost:contributor_info", args=(self.id,))

    @property
    def formal_str(self):
        return "%s %s" % (self.profile.get_title_display(), self.user.last_name)

    @property
    def is_active(self):
        """
        Checks if the Contributor is registered, vetted,
        and has not been deactivated for any reason.
        """
        return self.user.is_active and self.status == NORMAL_CONTRIBUTOR

    @property
    def is_duplicate(self):
        return self.duplicate_of is not None

    @property
    def is_currently_available(self):
        """Check if Contributor is currently not marked as unavailable."""
        return not self.unavailability_periods.today().exists()

    @property
    def available_again_after_date(self):
        unav = self.unavailability_periods.today().first()
        if unav:
            return unav.end

    @property
    def is_scipost_admin(self):
        """Check if Contributor is a SciPost Administrator."""
        return (
            self.user.groups.filter(name="SciPost Administrators").exists()
            or self.user.is_superuser
        )

    @property
    def is_ed_admin(self):
        """Check if Contributor is an Editorial Administrator."""
        return (
            self.user.groups.filter(name="Editorial Administrators").exists()
            or self.user.is_superuser
        )

    @property
    def is_in_advisory_board(self):
        """Check if Contributor is in the Advisory Board."""
        return (
            self.user.groups.filter(name="Advisory Board").exists()
            or self.user.is_superuser
        )

    @property
    def is_active_fellow(self):
        """Check if Contributor is a member of the Editorial College."""
        return self.fellowships.active().exists() or self.user.is_superuser

    @property
    def is_active_senior_fellow(self):
        return self.fellowships.active().senior().exists() or self.user.is_superuser

    def session_fellowship(self, request):
        """Return session's fellowship, if any; if Fellow, set session_fellowship_id if not set."""
        fellowships = self.fellowships.active()
        if fellowships.exists():
            if request.session["session_fellowship_id"]:
                from colleges.models import Fellowship

                try:
                    return self.fellowships.active().get(
                        pk=request.session["session_fellowship_id"]
                    )
                except Fellowship.DoesNotExist:
                    return None
            # set the session's fellowship_id to default
            fellowship = fellowships.first()
            request.session["session_fellowship_id"] = fellowship.id
            return fellowship
        return None

    @property
    def is_vetting_editor(self):
        """Check if Contributor is a Vetting Editor."""
        return (
            self.user.groups.filter(name="Vetting Editors").exists()
            or self.user.is_superuser
        )

    def generate_key(self, feed=""):
        """Generate a new activation_key for the contributor, given a certain feed."""
        for i in range(5):
            feed += random.choice(string.ascii_letters)
        feed = feed.encode("utf8")
        salt = self.user.username.encode("utf8")
        self.activation_key = hashlib.sha1(salt + feed).hexdigest()
        self.key_expires = timezone.now() + datetime.timedelta(days=2)

    def conflict_of_interests(self):
        if not self.profile:
            return ConflictOfInterest.objects.none()
        return ConflictOfInterest.objects.filter_for_profile(self.profile)

class UnavailabilityPeriod(models.Model):
    contributor = models.ForeignKey(
        "scipost.Contributor",
        on_delete=models.CASCADE,
        related_name="unavailability_periods",
    )
    start = models.DateField()
    end = models.DateField()

    objects = UnavailabilityPeriodManager()

    class Meta:
        ordering = ["-start"]

    def __str__(self):
        return "%s (%s to %s)" % (self.contributor, self.start, self.end)

class Remark(models.Model):
    """A form of non-public communication for VGMs and/or submissions and recommendations."""

    contributor = models.ForeignKey(Contributor, on_delete=models.CASCADE)
    submission = models.ForeignKey(
        "submissions.Submission", on_delete=models.CASCADE, blank=True, null=True
    )
    recommendation = models.ForeignKey(
        "submissions.EICRecommendation", on_delete=models.CASCADE, blank=True, null=True
    )
    date = models.DateTimeField(auto_now_add=True)
    remark = models.TextField()

    class Meta:
        default_related_name = "remarks"
        ordering = ["date"]

    def __str__(self):
        return (
            self.contributor.user.first_name
            + " "
            + self.contributor.user.last_name
            + " on "
            + self.date.strftime("%Y-%m-%d")
        )

###############
# Invitations #
###############

class RegistrationInvitation(models.Model):
    """Deprecated: Use the `invitations` app"""

    title = models.CharField(max_length=4, choices=TITLE_CHOICES)
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    email = models.EmailField()
    invitation_type = models.CharField(
        max_length=2, choices=INVITATION_TYPE, default=INVITATION_CONTRIBUTOR
    )
    cited_in_submission = models.ForeignKey(
        "submissions.Submission",
        on_delete=models.CASCADE,
        blank=True,
        null=True,
        related_name="registration_invitations",
    )
    cited_in_publication = models.ForeignKey(
        "journals.Publication", on_delete=models.CASCADE, blank=True, null=True
    )
    message_style = models.CharField(
        max_length=1, choices=INVITATION_STYLE, default=INVITATION_FORMAL
    )
    personal_message = models.TextField(blank=True)
    invitation_key = models.CharField(max_length=40, unique=True)
    key_expires = models.DateTimeField(default=timezone.now)
    date_sent = models.DateTimeField(default=timezone.now)
    invited_by = models.ForeignKey(
        "scipost.Contributor", on_delete=models.CASCADE, blank=True, null=True
    )
    nr_reminders = models.PositiveSmallIntegerField(default=0)
    date_last_reminded = models.DateTimeField(blank=True, null=True)
    responded = models.BooleanField(default=False)
    declined = models.BooleanField(default=False)

    def __str__(self):
        return "DEPRECATED"

class CitationNotification(models.Model):
    """Deprecated: Use the `invitations` app"""

    contributor = models.ForeignKey("scipost.Contributor", on_delete=models.CASCADE)
    cited_in_submission = models.ForeignKey(
        "submissions.Submission", on_delete=models.CASCADE, blank=True, null=True
    )
    cited_in_publication = models.ForeignKey(
        "journals.Publication", on_delete=models.CASCADE, blank=True, null=True
    )
    processed = models.BooleanField(default=False)

class AuthorshipClaim(models.Model):
    claimant = models.ForeignKey(
        "scipost.Contributor", on_delete=models.CASCADE, related_name="claimant"
    )
    submission = models.ForeignKey(
        "submissions.Submission", on_delete=models.CASCADE, blank=True, null=True
    )
    commentary = models.ForeignKey(
        "commentaries.Commentary", on_delete=models.CASCADE, blank=True, null=True
    )
    thesislink = models.ForeignKey(
        "theses.ThesisLink", on_delete=models.CASCADE, blank=True, null=True
    )
    vetted_by = models.ForeignKey(
        "scipost.Contributor", on_delete=models.CASCADE, blank=True, null=True
    )
    status = models.SmallIntegerField(
        choices=AUTHORSHIP_CLAIM_STATUS, default=AUTHORSHIP_CLAIM_PENDING
    )

    objects = AuthorshipClaimQuerySet.as_manager()

    def __str__(self):
        if self.submission:
            return "Authorship claim: %s for %s %s" % (
                self.claimant,
                "Submission",
                self.submission,
            )
        elif self.commentary:
            return "Authorship claim: %s for %s %s" % (
                self.claimant,
                "Commentary",
                self.commentary,
            )
        elif self.thesislink:
            return "Authorship claim: %s for %s %s" % (
                self.claimant,
                "Thesis Link",
                self.thesislink,
            )
        return "Authorship claim: %s for [undefined]" % self.claimant

class PrecookedEmail(models.Model):
    """
    Each instance contains an email template in both plain and html formats.
    Can only be created by Admins.
    For further use in scipost:send_precooked_email method.
    """

    email_subject = models.CharField(max_length=300)
    email_text = models.TextField()
    email_text_html = models.TextField()
    date_created = models.DateField(default=timezone.now)
    emailed_to = ArrayField(models.EmailField(blank=True), blank=True)
    date_last_used = models.DateField(default=timezone.now)
    deprecated = models.BooleanField(default=False)

    def __str__(self):
        return self.email_subject


import datetime
from flask import Flask
import sys
from flask_restful import Api, Resource, reqparse, inputs, marshal_with, fields
from flask_sqlalchemy import SQLAlchemy

parser = reqparse.RequestParser()
app = Flask(__name__)
api = Api(app)
db = SQLAlchemy(app)
app.config['SQLALCHEMY_DATABASE_URL'] = 'sqlite://events.db'

class Events(db.Model):
    __tablename__ = 'events'
    id = db.Column(db.Integer, primary_key=True)
    event = db.Column(db.String(80), nullable=False)
    date = db.Column(db.Date, nullable=False)

db.create_all()

parser.add_argument(
    'event',
    type=str,
    help='The event name is required!',
    required=True
)

parser.add_argument(
    'date',
    type=inputs.date,
    help='The event date with the correct format is required! The correct format is YYYY-MM-DD!',
    required=True
)

resource_fields = {
    'id': fields.Integer,
    'event': fields.String,
    'date': fields.String,
    'message': fields.String
}

class TodayEventResource(Resource):
    @marshal_with(resource_fields)
    def get(self):
        return Events.query.filter(Events.date == datetime.date.today()).all()

class EventResource(Resource):
    @marshal_with(resource_fields)
    def get(self):
        return Events.query.all()

    @marshal_with(resource_fields)
    def post(self):
        args = parser.parse_args()
        event = Events(event=args.event, date=args.date)
        db.session.add(event)
        db.session.commit()
        return {'message': 'The event has been added!', 'event': args.event, 'date': args.date.strftime('%Y-%m-%d')}

api.add_resource(EventResource, '/event')
api.add_resource(TodayEventResource, '/event/today')

if __name__ == '__main__':
    if len(sys.argv) > 1:
        arg_host, arg_port = sys.argv[1].split(':')
        app.run(host=arg_host, port=arg_port, debug=True)
    else:
        app.run(debug=True)

Medium-Level/Web Calendar/Stage 3 Relax/calendar_app.py


from rest_framework import serializers
from .models import brandList, order, orderDetail, ratevote, scentNote, perfume, rate, cartDetail

class brandSerializer(serializers.ModelSerializer):
    class Meta:
        model = brandList
        fields = (
            "id",
            "name",
            "place",
            "route",
            "image",
        )

class scentNoteSerializer(serializers.ModelSerializer):
    class Meta:
        model = scentNote
        fields = (
            "id",
            "name",
            "description",
            "route",
            "image",
        )

class perfumeSerializer(serializers.ModelSerializer):
    scentnote = serializers.SlugRelatedField(slug_field='name', read_only=True)
    is_rated = serializers.SerializerMethodField()
    point = serializers.SerializerMethodField()
    longevity = serializers.SerializerMethodField()
    sillage = serializers.SerializerMethodField()

    def get_point(self, obj):
        try:
            return obj.point
        except:
            return None

    def get_longevity(self, obj):
        try:
            return obj.longevity
        except:
            return None

    def get_sillage(self, obj):
        try:
            return obj.sillage
        except:
            return None

    def get_is_rated(self, obj):
        try:
            return obj.isRated
        except:
            return None

    class Meta:
        model = perfume
        fields = (
            "id",
            "name",
            "sname",
            "description",
            "brand",
            "topnote",
            "midnote",
            "basenote",
            "scentnote",
            "yearofreleasing",
            'price',
            'sale',
            "point",
            "longevity",
            "sillage",
            "sex",
            'is_rated',
            "image",
        )

class rateSerializer(serializers.ModelSerializer):
    perfume_id = serializers.PrimaryKeyRelatedField(
        source='perfume', queryset=perfume.objects.all())
    perfume = serializers.StringRelatedField(read_only=True)
    user = serializers.StringRelatedField(
        default=serializers.CurrentUserDefault(), read_only=True)
    is_up_voted = serializers.SerializerMethodField()
    is_down_voted = serializers.SerializerMethodField()
    vote_id = serializers.SerializerMethodField()
    vote = serializers.SerializerMethodField()

    def get_vote(self, obj):
        try:
            return obj.vote
        except:
            return None

    def get_is_up_voted(self, obj):
        try:
            return obj.isUpVoted
        except:
            return None

    def get_is_down_voted(self, obj):
        try:
            return obj.isDownVoted
        except:
            return None

    def get_vote_id(self, obj):
        try:
            return obj.voteID
        except:
            return None

    class Meta:
        model = rate
        fields = ('perfume_id', 'perfume', 'user', 'perfumepoint', 'longevity',
                  'sillage', 'content', 'vote', 'is_up_voted', 'is_down_voted',
                  'id', 'vote_id')

class rateVoteSerializer(serializers.ModelSerializer):
    user = serializers.StringRelatedField(
        default=serializers.CurrentUserDefault(), read_only=True)

    class Meta:
        model = ratevote
        fields = (
            'rate',
            'user',
            'point',
        )

class cartDetailSerializer(serializers.ModelSerializer):
    user = serializers.StringRelatedField(
        default=serializers.CurrentUserDefault(), read_only=True)
    perfume_data = perfumeSerializer(source='perfume', read_only=True)

    def create(self, validated_data):
        user = validated_data.get("user", None)
        perfume = validated_data.get("perfume", None)
        cartItem = cartDetail.objects.filter(user=user,
                                             perfume=perfume).first()
        if cartItem is not None:
            cartItem.amount = cartItem.amount + 1
            cartItem.save()
            return cartItem
        else:
            return cartDetail.objects.create(**validated_data)

    class Meta:
        model = cartDetail
        fields = ('id', 'user', 'perfume', 'amount', 'perfume_data')

class orderDetailSerializer(serializers.ModelSerializer):
    perfume_data = perfumeSerializer(source='perfume', read_only=True)

    def create(self, validated_data):
        perfume = validated_data.get('perfume', None)
        amount = validated_data.get('amount', None)
        validated_data.update({"price": perfume.price * amount})
        return orderDetail.objects.create(**validated_data)

    class Meta:
        model = orderDetail
        fields = ('order', 'perfume', 'amount', 'price', 'perfume_data')
        read_only_fields = ('price', 'perfume_data')

class orderSerializer(serializers.ModelSerializer):
    user = serializers.StringRelatedField(
        default=serializers.CurrentUserDefault(), read_only=True)
    details = orderDetailSerializer(many=True, read_only=True)
    totalPrice = serializers.SerializerMethodField()

    def get_totalPrice(self, obj):
        try:
            return obj.totalPrice
        except:
            return None

    class Meta:
        model = order
        fields = ('id', 'user', 'status', 'createdDate', 'totalPrice',
                  'details')
        read_only_fields = ('id', 'createdDate', 'status', 'totalPrice')


import openpyxl
import random

def create_word(GENRENUM):

  workbook = openpyxl.load_workbook('ワードウルフ_質問データベース_No1.xlsx')

  #sheet = workbook["General"]
  sheet=workbook.worksheets[GENRENUM-1]
  print("sheet名",sheet)
  max_row_num = sheet.max_row
  word_data = []

  for i in range(2,max_row_num+1):
    word1 = sheet.cell(row=i, column=1).value #word1
    word2 = sheet.cell(row=i, column=2).value #word2
    qest1 = sheet.cell(row=i, column=3).value #質問1
    qest2 = sheet.cell(row=i, column=4).value #質問2
    qest3 = sheet.cell(row=i, column=5).value #質問3

    #if cell_value not in suppliers:
    word_data.append([word1,word2,qest1,qest2,qest3])

  #print("max_row_num==>",max_row_num,len(word_data))
  word_num = random.randint(0,len(word_data)-1)
  #print("word_num-->",word_num)
  #print("word_data[0]-->",word_data[word_num])
  #print("word_data[7][0]-->",word_data[word_num][0])
  #print("word_data[7][1]-->",word_data[word_num][1])
  # 戻り地として、重複のない乱数列とデータ一覧を戻す。
  return word_data,max_row_num

# 重複のない乱数を生成する
def rand_ints_nodup(a, b, k):
  ns = []
  while len(ns) < k:
    n = random.randint(a, b)
    if not n in ns:
      ns.append(n)
  return ns

def check_genre():
  workbook = openpyxl.load_workbook('ワードウルフ_質問データベース_No1.xlsx')
  sheets = workbook.sheetnames

  print(sheets)

  return sheets


# 영상의 명암비 조절

# 명암비(Contrast)란?
# 밝은 곳과 어두운 곳 사이에 드러나는 밝기 정도의 차이
# 컨트라스트, 대비

# 기본적인 명암비 조절 함수
# dst(x, y) = saturate(s • src(x, y))  # 입력 영상에 scaling factor를 곱해서 결과 영상의 픽셀 값으로 셋팅
# => 단점 : s의 값에 따라 영상이 어두워지거나 전체적으로 포화되어 너무 밝아질 수 있다.

# 효과적인 명암비 조절 함수
# dst(X, y) = saturate(src(x, y) + (src(x, y) - 128) • α)
# (128, 128)을 무조건 지나고 기울기가 α의 값에 조절되는 직선의 방적식 그래프. (128은 고정이 아니고 영상에 따라 처리해줘야 한다.)
# src(x, y) + (src(x, y) - 128) • α ==> (1 + α) • src(x, y) - 128α

import sys
import numpy as np
import cv2

src = cv2.imread('lenna.bmp', cv2.IMREAD_GRAYSCALE)

if src is None:
    print('Image load failed!')
    sys.exit()

alpha = 1.0  # contrast를 올릴 것이기 때문에 alpha값을 1.0으로 설정
dst = np.clip((1 + alpha) * src - 128 * alpha, 0, 255).astype(np.uint8)  # 0보다 작으면 0, 255보다 크면 255로 clipping

cv2.imshow('src', src)
cv2.imshow('dst', dst)
cv2.waitKey()

cv2.destroyAllWindows()


import fileinput
from os import path
from gensim.models import KeyedVectors

from SmartAnno.gui.Workflow import logMsg

NotInitiated = 0
Initiating = 1
Initiated = 2

class GloveModel:
    glove_model = None
    status = NotInitiated

    def __init__(self, word2vec_file='models/saved/glove/glove.42B.300d.bin', vocab=1900000, vect=300):
        glove_model = None
        if GloveModel.glove_model is None and GloveModel.status == NotInitiated:
            if path.isfile(word2vec_file):
                GloveModel.status = Initiating
                logMsg('Load glove model in the backend...')
                print('Load glove model in the backend...')
                if word2vec_file.endswith('.bin'):
                    glove_model = KeyedVectors.load_word2vec_format(word2vec_file, binary=True)
                    GloveModel.status = Initiated
                else:
                    glove_model = KeyedVectors.load_word2vec_format(word2vec_file, binary=False)
                    logMsg('convert txt model to binary model...')
                    glove_model.save_word2vec_format(word2vec_file[:-3] + '.bin', binary=True)
                    GloveModel.status = Initiated
            elif path.isfile(word2vec_file[:-3] + 'txt'):
                GloveModel.status = Initiating
                logMsg('Load glove model in the backend...')
                print('Load glove model in the backend...')
                txt_model = word2vec_file[:-3] + 'txt'
                self.addDimensions(txt_model, line_to_prepend=str(vocab) + ' ' + str(vect))
                glove_model = KeyedVectors.load_word2vec_format(txt_model, binary=False)
                logMsg('convert txt model to binary model...')
                glove_model.save_word2vec_format(word2vec_file, binary=True)
                GloveModel.status = Initiated
            else:
                logMsg(("Either ", path.abspath(word2vec_file), ' or ', path.abspath(word2vec_file[:-3] + 'txt'),
                           ' exists.'))
                print(("Either ", path.abspath(word2vec_file), ' or ', path.abspath(word2vec_file[:-3] + 'txt'),
                            ' exists.'))
            GloveModel.glove_model = glove_model
        pass

    def checkModelExistance(self, word2vec_file='models/glove/glove.42B.300d.bin'):
        if path.isfile(word2vec_file) or path.isfile(word2vec_file[:-3] + 'txt'):
            return True
        else:
            return False

    def addDimensions(self, filename, line_to_prepend):
        with open(filename, 'r') as f:
            line = f.readline()
            if line.startswith(line_to_prepend):
                return
        f = fileinput.input(filename, inplace=1)
        for xline in f:
            if f.isfirstline():
                print(line_to_prepend + '\n' + xline.rstrip('\r\n'))
            else:
                print(xline.rstrip('\r\n'))
        pass


import importlib
from oslo_config import cfg

# Add default values for the database to be used for dockyard.
DATABASE_OPT = [
    cfg.StrOpt("driver",
                default="consul.consul_client.ConsulKV",
                help="Database driver for dockyard"),
    cfg.IntOpt('synchronization_time',
                default=20,
                help='Time after which data will be synced .')
]

CONF = cfg.CONF
opt_group = cfg.OptGroup(name='database',
                         title='Group for database')
CONF.register_group(opt_group)
CONF.register_opts(DATABASE_OPT, opt_group)
db_driver_loc = 'dockyard.common.stores'
db_driver_info = (('%s.%s') % (db_driver_loc, CONF.database.driver))
module_name, db = db_driver_info.rsplit(".", 1)
module = importlib.import_module(module_name)

#import synchronizer


# 실버1. 숨바꼭질
# 전혀 모르겠는데 답 보니까 코드는 짧아서 약올라
# not 구문 처음보는거같은데 진짠가

from collections import deque

N, K = map(int, input().split())
max = 100000
visited = [0] * (max+1)

def bfs():
    q = deque()
    q.append(N)
    while q:
        x = q.popleft()
        if x == K:
            print(visited[x])
            break
        for j in (x-1, x+1, x*2):
            if 0<= j <= max and not visited[j]:
                visited[j] = visited[x] + 1
                q.append(j)

bfs()


from playwright.sync_api import sync_playwright

def on_new_page(page):
    print("New page opened:", page.url)
    page.wait_for_load_state("load")

    # Wait for page to finish loading
    page.wait_for_timeout(1000)

    # Get page title
    title = page.title()
    print("Page title:", title)

    # Perform actions on the new page
    # ...
    # Close the page after you're done
    # try:
    #     page.close()
    # except Exception as e:
    #     print("Error closing page:", e)

def main():
    with sync_playwright() as p:
        # Launch a browser
        browser = p.chromium.launch()
        # Create a context
        context = browser.new_context()

        # Set up event listener for new pages
        context.on("page", on_new_page)

        # Open an initial page
        page = context.new_page()
        page.goto("https://example.com")
        # Simulate clicking a link that opens in a new tab
        page.evaluate("window.open('https://google.com', '_blank')")

        # Wait for a while to let the new tab open and perform actions
        page.wait_for_timeout(5000)

        # Close the browser
        browser.close()

main()


# -*- coding: utf-8 -*-
"""
Created on Fri Feb 12 11:22:31 2021

@author: VERNET Marlone

"""

#from tpmontrouge.instrument.analog_input.test.simu_ai import AnalogInputThreadSimulation
#from tpmontrouge.instrument.daqmx.analog_input import DAQmxAnalogInput

from analog_input_marlon import DAQmxAnalogInput

from time import time
import PyDAQmx
import pickle

import numpy as np
import matplotlib.pyplot as plt

import scipy.signal as scs

from IPython import get_ipython
get_ipython().run_line_magic('matplotlib','qt')

plt.rcParams.update({'font.size': 22})

#%%

folder="C:/Users/admin-si/Desktop/Faraday_2_ML/ ????? /"

RECORDING = True
number = 1
freq_ = 30

# name = f'low_noise_ut_{number}_A1000G.pkl'
name1 = f'farad_{number}f{freq_}Hz.pkl'
# name2 = f'ur_{number}A1000G.pkl'

t1 = time()

plt.ion()

""" NI parameter """
DEV = '/dev2'
sample_rate = 2000
duration = 120 # en s

channel_list = ['0'] #['0', '1', '2'] #['1', '2'] #list of the recorded channels
N_ch = len(channel_list)
block_size = sample_rate//5
N_block = 5*duration
volt_range = 1 # en Volt

""" welch method parameters """
signal_size = sample_rate*duration
n = int(np.log(signal_size)/np.log(2)) - 1
segment_size = 2**n
number_segment = int(2*signal_size/segment_size) - 1
overlap_size = int( (signal_size - segment_size)/(number_segment - 1) )

"""mode diff, single_ended"""
acquisition_mode = PyDAQmx.DAQmx_Val_Diff
# acquisition_mode = PyDAQmx.DAQmx_Val_PseudoDiff
# acquisition_mode = PyDAQmx.DAQmx_Val_RSE

fig = plt.figure(figsize=(15,15))
ax = fig.subplots(3,N_ch)

fig.show()
fig.canvas.draw()

#task = AnalogInputThreadSimulation('AI')
task = DAQmxAnalogInput(DEV)
task.start(channel_list, sample_rate, block_size, N_block, volt_range, acquisition_mode)
t0 = time()
t=0

all_in = dict() #data are recorded in a dict, each key == one channel

for i in range(N_ch):
    all_in[i] = np.array([])

for _ in range(N_block):
    t=time() - t0
    # t+=block_size/sample_rate
    data = task.get_one_block()
    for k in range(N_ch):
        all_in[k] = np.concatenate([all_in[k], data[ channel_list[k] ]])
        ax[0,k].clear()
        #ax[0].plot(all_1)
        ax[0,k].plot(data[ channel_list[k] ])
        ax[0,k].set_title('t='+str(round(t,2)))

        ax[1,k].clear()
        #ax[0].plot(all_1)
        ax[1,k].plot(all_in[ k ])
        # ax[1,k].set_title('t='+str(round(t,2)))

        if len(all_in[k]) > segment_size:
            signal = all_in[k] - np.mean(all_in[k])
            f,psd = scs.welch(signal, fs=sample_rate, nperseg=segment_size, noverlap=overlap_size)
            ax[2,k].clear()
            ax[2,k].loglog(f,psd)

    fig.canvas.draw()
    plt.pause(0.01)
task.stop()

if RECORDING == True:

    with open(folder+name1, "wb") as fp:   #Pickling
        pickle.dump(all_in, fp)

print(time() - t1)

#%%
number = 100
name = f'low_noise_ut_{number}_A1000G.pkl'

with open(folder+name_, "rb") as fp:   #Pickling
    M = pickle.load(fp)

plt.figure()
plt.plot(M)
plt.grid()
plt.show()


import datetime
import queue

img = queue.Queue(0)
img = queue.Queue(0)

class FPS:
    def __init__(self):
        # store the start time, end time, and total number of frames
        # that were examined between the start and end intervals
        self._start = None
        self._end = None
        self._numFrames = 0

    def start(self):
        # start the timer
        self._start = datetime.datetime.now()
        return self

    def stop(self):
        # stop the timer
        self._end = datetime.datetime.now()

    def update(self):
        # increment the total number of frames examined during the
        # start and end intervals
        self._numFrames += 1

    def elapsed(self):
        # return the total number of seconds between the start and
        # end interval
        return (self._end - self._start).total_seconds()

    def fps(self):
        # compute the (approximate) frames per second
        return self._numFrames / self.elapsed()

from threading import Thread
import cv2

class WebcamVideoStream:
    def __init__(self, src=0):
        # initialize the video camera stream and read the first frame
        # from the stream
        self.stream = cv2.VideoCapture(src)
        (self.grabbed, self.frame) = self.stream.read()

        # initialize the variable used to indicate if the thread should
        # be stopped
        self.stopped = False

    def start(self):
        # start the thread to read frames from the video stream
        Thread(target=self.update, args=()).start()
        return self

    def update(self):
        # keep looping infinitely until the thread is stopped
        while True:
            # if the thread indicator variable is set, stop the thread
            if self.stopped:
                break

            # otherwise, read the next frame from the stream
            (self.grabbed, self.frame) = self.stream.read()

    def read(self):
        # return the frame most recently read
        return self.frame

    def stop(self):
        # indicate that the thread should be stopped
        self.stopped = True

from imutils.video import WebcamVideoStream
from imutils.video import FPS
import argparse
import imutils
import cv2
import numpy as np
# construct the argument parse and parse the argumen

def onmouse(k, x, y, s, p):
    global hsv
    if k == 1:  # left mouse, print pixel at x,y
        print(hsv[y, x])

def distance_to_camera(Kwidth, focalLength, pixelWidth):
    return (Kwidth * focalLength) / pixelWidth

def contourArea(contours):

    area = []
    for i in range(0,len(contours)):
       area.append([cv2.contourArea(contours[i]),i])

    area.sort()
    if(area[len(area) - 1] >= 5 * area[0]):
        return area[len(area)-1]

    else: return 0

if __name__ == "__main__":
    ap = argparse.ArgumentParser()
    ap.add_argument("-n", "--num-frames", type=int, default=100,
                    help="# of frames to loop over for FPS test")
    ap.add_argument("-d", "--display", type=int, default=-1,
                    help="Whether or not frames should be displayed")
    args = vars(ap.parse_args())

    print("[INFO] sampling frames from webcam...")
    stream = cv2.VideoCapture(0)
    fps = FPS().start()

    # loop over some frames
    while fps._numFrames < args["num_frames"]:
        # grab the frame from the stream and resize it to have a maximum
        # width of 400 pixels
        (grabbed, frame) = stream.read()
        frame = imutils.resize(frame, width=400)

        # check to see if the frame should be displayed to our screen
        if args["display"] > 0:
            cv2.imshow("Frame", frame)
            key = cv2.waitKey(1) & 0xFF

        # update the FPS counter
        fps.update()

    # stop the timer and display FPS information
    fps.stop()
    print("[INFO] elasped time: {:.2f}".format(fps.elapsed()))
    print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))

    # do a bit of cleanup
    stream.release()
    cv2.destroyAllWindows()

    print("[INFO] sampling THREADED frames from webcam...")
    lower_green = np.array([70, 50, 120])

    upper_green = np.array([120, 200, 255])

    vs = WebcamVideoStream(src=0).start()
    fps = FPS().start()
    # loop over some frames...this time using the threaded stream
    while fps._numFrames < args["num_frames"]:
        # grab the frame from the threaded video stream and resize it
        # to have a maximum width of 400 pixels
        img = vs.read()

        im2, contours, hierarchy = cv2.findContours(img.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)

        # update the FPS counter
        fps.update()

    # stop the timer and display FPS information
    fps.stop()
    print("[INFO] elasped time: {:.2f}".format(fps.elapsed()))
    print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))

    # do a bit of cleanup
    cv2.destroyAllWindows()
    vs.stop()


import logging
import serial, time
import serial.tools.list_ports

class MatrixReadException(Exception):
    """Exception raised when reading from the arduino failed"""
    pass

class MatrixProtocolException(Exception):
    """Exception raised when the arduino did not follow the communication protocol as expected"""
    pass

def get_connected_arduinos():
    ports = list(serial.tools.list_ports.comports())
    return ports

def guess_arduino():
    ports = get_connected_arduinos()
    probably_an_arduino = None
    for port in ports:
        # this is the raspberry GPIO UART, where we are not connected
        if port.device == "/dev/ttyAMA0":
            pass
        # my arduino UNO
        elif "/dev/ttyACM" in port.device:
            probably_an_arduino = port.device
        # my arduino nano
        elif "/dev/ttyUSB" in port.device:
            probably_an_arduino = port.device
    return probably_an_arduino

class MatrixSerial:
    """This class handles the communication with the arduino"""

    def __init__(self, interface: str, led_count: int, baud: int = 115200, connect: bool = False,
                 enable_arduino_connection=False):
        """
        Create new MatrixSerial, immediately connecting to the arduino.

        :param interface the device name
        :param led_count: number of leds to use
        :param baud: baud rate to be used
        :param fps the maximum fps to use for updating
        """
        # store serial config
        self.interface = interface
        self.baud = baud
        self.serial = None

        # init buffer
        self.buffer = bytearray(led_count * 3)

        self.was_connected = False

        self.enable_arduino_connection = enable_arduino_connection

        self.logger = logging.getLogger("matrixserial")

        # convenience function for immediately connecting
        if connect and enable_arduino_connection:
            self.connect()

    def connect(self, timeout: float = 4):
        """
        connect to the arduino given the current configuration

        :param timeout: the number of seconds after which the connection attempt will be aborted
        :return: true if the handshake was completed successfully, false otherwise
        :raises: ValueError Will be raised when parameter are out of range, e.g. baud rate, data bits.
        :raises: SerialException In case the device can not be found or can not be configured.
        :raises: MatrixProtocolException if the arduino does not shake hands
        :
        """
        if not self.enable_arduino_connection:
            self.logger.info("the matrix serial was started in silent mode. connect() was ignored.")
            return

        self.was_connected = True

        self.logger.info("beginning arduino connection attempt to {} which has {} LEDs".format(self.interface, self.get_led_count()))

        # begin serial connection
        self.serial = serial.Serial(self.interface, self.baud, timeout=timeout)
        self.serial.flushInput()
        self.serial.flushOutput()

        # wait for arduino reset
        time.sleep(2)

        # begin handshake
        self.serial.write(b'hello')

        # wait for response
        response = self.serial.read(3)

        if response != b'SAM' and response != b'kSA':
            self.logger.warning("Handshake failed: expected b'SAM', got " + str(response))
            raise MatrixProtocolException("Handshake failed: expected b'SAM', got " + str(response))

        self.logger.info("successfully connected to arduino")

    def stop(self):
        if self.serial is not None:
            self.serial.close()

    def get_led_count(self) -> int:
        """ Query the number of leds this serial connection is configured for """
        return len(self.buffer) // 3

    def get_buffer_length(self) -> int:
        """ Query the length of the buffer required to send data to the arduino """
        return len(self.buffer)

    @staticmethod
    def current_time_ms() -> int:
        """Helper function. Returns a timestamp in ms"""
        return int(round(time.time() * 1000))

    def update(self, data: bytearray):
        """
        Update the data sent to the connected arduino
        :param data: a bytearray containing the new data, of len
        :raises: MatrixProtocolException if the arduino did not acknowledge the updated data
        """
        # ensure correct data length
        if len(data) != len(self.buffer):
            self.logger.error(
                "Bad data length! len(data) should be " + str(len(self.buffer)) + ", is " + str(len(data)))
            raise MatrixProtocolException(
                "Bad data length! len(data) should be " + str(len(self.buffer)) + ", is " + str(len(data)))

        # copy buffer
        self.buffer[:] = data

        # trigger internal draw call
        self.__draw()

    def __draw(self):
        """
        Draw the current buffer content by sending it to the arduino as-is
        :raises: MatrixProtocolException if the arduino did not acknowledge a sent buffer
        """

        if not self.enable_arduino_connection:
            return

        # we need to wait some time before writing the next set of data. testing required for better accuracy
        time.sleep(0.009)

        # write out internal buffer to arduino
        self.serial.write(self.buffer)

        # read arduino acknowledgement char
        try:
            ack = self.serial.read(1)
        except serial.serialutil.SerialException as e:
            self.logger.exception("A serial error occurred when trying to read from the matrix!")
            raise MatrixReadException(str(e))
        else:
            # check acknowledgement char correctness
            if ack != b'k':
                self.logger.exception("No acknowledgement received, expected b'k', got " + str(ack))
                raise MatrixProtocolException("No acknowledgement received, expected b'k', got " + str(ack))


import matplotlib.pyplot as plt;
import numpy as np;
import glob;
import os;
import sys;

if len(sys.argv) == 1:
  print("Usage -- python3 plotData.py <list of filenames>")

if os.path.isdir("./plots_data")==False:
  os.mkdir("./plots_data");

# Get all the data files
sim_files = [];
for i in range(1, len(sys.argv)):
  sim_files.append(glob.glob(sys.argv[i]));

# We need to flatten the array.
sim_flat = [];
for l in sim_files:
  for item in l:
    sim_flat.append(item);

n = len(sim_flat);

# Find the maximum and minimum values across all plots.
max_y = 0;
min_y = 0;
for i in range(0,n):
  data = np.genfromtxt(fname=sim_flat[i], delimiter=",");
  ydata = data[:,2];
  tmax = np.amax(ydata);
  tmin = np.amin(ydata);
  if tmax > max_y:
    max_y = tmax;
  if tmin > min_y:
    min_y = tmin;

# Now that we have a min and a max range, let's plot our data.

for i in range(0,n):
  data = np.genfromtxt(fname=sim_flat[i], delimiter=",");

  # Get the head of the file name.
  name = os.path.split(sim_flat[i])[1];
  name = os.path.splitext(name)[0];

  # Make the plots
  plt.plot(data[:,1], data[:,2],'.');
  plt.ylim(min_y,max_y);
  plt.xlabel('x');
  plt.ylabel(name);
  plt.tight_layout();

  plt.savefig('./plots_data/' + name + '.png');
  plt.clf();


#encoding=utf-8

from scrapy.selector import HtmlXPathSelector
from scrapy.spider import BaseSpider
from scrapy.contrib.loader import XPathItemLoader
from scrapy.http import Request
import re

from douban_crawler.items import GroupUserItem
from douban_crawler.init import get_start_urls

class GroupSpider(BaseSpider):
    name = "group"
    allowed_domains = ["douban.com"]
    start_urls = get_start_urls("group_user")

    def parse(self, response):
        url = response.url
        group_name = url[url.find('group'):].split('/')[1]
        hxs = HtmlXPathSelector(response)

        dls = hxs.select('//dl[@class="obu"]')
        items = []
        for dl in dls:
            item = GroupUserItem()
            l = XPathItemLoader(item=item, selector=dl)
            l.add_xpath('homepage', 'dt/a/@href')
            l.add_xpath('image', 'dt/a/img/@src')
            l.add_xpath('name', 'dd/a/text()')
            l.add_value('group', group_name)
            yield l.load_item()

        links = hxs.select('//span[@class="next"]/a/@href').extract()
        for url in links:
            yield Request(url, callback=self.parse)
        if len(links) < 1:
            p = re.compile('.*?',re.S)
            m = p.search(response.body_as_unicode())
            if m:
                url = m.group(1)
                yield Request(url, callback=self.parse)