import datetime
import random

def load_pending_tasks():
    try:
        with open("data.txt", "r") as file:
            tasks = file.readlines()
        tasks = [task.strip() for task in tasks]
        return tasks
    except FileNotFoundError:
        return []

def save_pending_tasks(tasks):
    with open("data.txt", "w") as file:
        for task in tasks:
            file.write(task + "\n")

def load_progress():
    try:
        with open("progress.txt", "r") as file:
            progress = file.read().strip()
            if progress:
                return int(progress)
            else:
                return 0
    except FileNotFoundError:
        return 0

def save_progress(progress):
    with open("progress.txt", "w") as file:
        file.write(str(progress))

def calculate_goggins_points(completion_time):
    current_time = datetime.datetime.now()
    time_difference = current_time - completion_time

    if time_difference <= datetime.timedelta(hours=3):
        return 1000
    elif time_difference <= datetime.timedelta(days=1):
        return 400
    else:
        return 0

def display_achievements(progress):
    if progress >= 20000:
        print("Congratulations! You have achieved Goggins' Praise.")
    else:
        print(f"Keep going! Your progress: {progress} Goggins points.")

def fetch_random_paragraph():
    with open("goggins.txt", "r") as file:
        paragraphs = file.read().split("\n\n")
        random_paragraph = random.choice(paragraphs)
        formatted_paragraph = random_paragraph.replace("\n", ".\n")
        return formatted_paragraph

def main():
    paragraph = fetch_random_paragraph()
    print(paragraph)

    tasks = load_pending_tasks()
    progress = load_progress()

    if tasks:
        print("Pending tasks:")
        for task in tasks:
            print(task)

        completion_tasks = []
        for task in tasks:
            completion = input(f"Have you completed the task: {task}? (y/n): ")
            if completion.lower() == "y":
                completion_tasks.append(task)
                points = calculate_goggins_points(datetime.datetime.strptime(task.split('|')[0], '%Y-%m-%d %H:%M:%S'))
                progress += points
                print(f"Congratulations! You earned {points} Goggins points.")

        tasks = [task for task in tasks if task not in completion_tasks]
        save_pending_tasks(tasks)
        save_progress(progress)
        display_achievements(progress)

    while True:
        new_task = input("Enter a new task (or 'q' to quit): ")
        if new_task.lower() == "q":
            break

        timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        tasks.append(f"{timestamp}|{new_task}")
        save_pending_tasks(tasks)

        print("Task added successfully. Progress and tasks saved.")

    print("All the best with completing tasks. See You soon. Exiting program.")

if __name__ == "__main__":
    main()


from nose.tools import *
from mock import MagicMock
import pandas as pd

from philharmonic import Schedule
from philharmonic.scheduler.bcf_scheduler import *

from philharmonic.simulator.environment import FBFSimpleSimulatedEnvironment
from philharmonic.simulator.simulator import FBFSimulator
from philharmonic.simulator import inputgen
from philharmonic import Cloud, VMRequest, \
    VM, Server, State, Migration

def test_bcf_returns_schedule():
    scheduler = BCFScheduler()
    scheduler.cloud = Cloud()
    scheduler.environment = FBFSimpleSimulatedEnvironment()
    scheduler.environment.get_requests = MagicMock(return_value = [])
    scheduler.environment.get_time = MagicMock(return_value = 1)
    schedule = scheduler.reevaluate()
    assert_is_instance(schedule, Schedule)

def test_bcf_reevaluate_initial_placement():
    scheduler = BCFScheduler()
    scheduler.environment = FBFSimpleSimulatedEnvironment()
    s1, s2 = Server(4000, 4), Server(4000, 4)
    s1.loc = 'A'
    s2.loc = 'B'
    vm1 = VM(2000, 1)
    vm2 = VM(1000, 2)
    vm3 = VM(2000, 2)
    cloud = Cloud([s1, s2], [vm1, vm2, vm3])
    scheduler.cloud = cloud
    r1 = VMRequest(vm1, 'boot')
    r2 = VMRequest(vm2, 'boot')
    r3 = VMRequest(vm3, 'boot')
    scheduler.environment.get_requests = MagicMock(return_value = [r1, r2, r3])
    scheduler.environment.t = 1
    #scheduler.environment.get_time = MagicMock(return_value = 1)
    # location B cheaper el. price, temperatures the same
    el = pd.DataFrame({'A': [0.16], 'B': [0.08]}, [1])
    temp = pd.DataFrame({'A': [15], 'B': [15]}, [1])
    scheduler.environment.current_data = MagicMock(return_value = (el, temp))
    schedule = scheduler.reevaluate()
    for action in schedule.actions:
        cloud.apply_real(action)
    current = cloud.get_current()
    #assert location of vms
    assert_true(current.all_allocated())
    assert_true(current.all_within_capacity())
    assert_equals(current.allocation(vm3), s2, 'cheaper location picked first')
    assert_equals(current.allocation(vm2), s2, 'cheaper location picked first')
    assert_equals(current.allocation(vm1), s1, 'when no room, the next best')

def test_bcf_reevaluate_underutilised():
    scheduler = BCFScheduler()
    scheduler.environment = FBFSimpleSimulatedEnvironment()
    s1, s2 = Server(40000, 12, location='A'), Server(20000, 10, location='A')
    vm1 = VM(2000, 1)
    vm2 = VM(2000, 1)
    r2 = VMRequest(vm2, 'boot')
    cloud = Cloud([s1, s2], [vm1, vm2])
    scheduler.cloud = cloud
    scheduler.environment.get_requests = MagicMock(return_value = [r2])
    scheduler.environment.t = 1
    el = pd.DataFrame({'A': [0.08], 'B': [0.08]}, [1])
    temp = pd.DataFrame({'A': [15], 'B': [15]}, [1])
    scheduler.environment.current_data = MagicMock(return_value = (el, temp))
    # the initial state is a VM hosted on an underutilised PM
    cloud.apply_real(Migration(vm1, s2))
    schedule = scheduler.reevaluate()
    for action in schedule.actions:
        cloud.apply_real(action)
    current = cloud.get_current()
    assert_true(current.all_within_capacity())
    assert_equals(current.allocation(vm1), s1)
    assert_equals(current.allocation(vm2), s1)
    assert_true(current.all_allocated())

def test_sort_pms_best_first():
    pm1 = Server(2000, 2, location='B')
    pm2 = Server(4000, 4, location='A')
    pm3 = Server(4000, 4, location='B')
    cost = pd.Series({'A': [0.04], 'B': [0.08], 'C': [0.16]})
    servers = [pm2, pm3, pm1]
    state = State(servers, [])
    sorted_pms = sort_active_pms(servers, state, cost)
    assert_equals(sorted_pms, [pm1, pm2, pm3])

def test_sort_pms_cost_first_same_cost():
    """if costs are equal, check that bigger server returned first"""
    pm1 = Server(2000, 1, location='A')
    pm2 = Server(3000, 1, location='A')
    pm3 = Server(3000, 2, location='A')
    pm4 = Server(3000, 4, location='A')
    cost = pd.Series({'A': [0.08], 'B': [0.08]})
    servers = [pm2, pm1, pm4, pm3]
    state = State(servers, [])
    sorted_pms = sort_inactive_pms(servers, state, cost)
    assert_equals(sorted_pms, [pm4, pm3, pm2, pm1])


from rsf.proj import *
import os

top = project.get('RSFROOT')
libdir = os.path.join(top,'lib')

path = {'exe':WhereIs('matlab')}
for key in ('MATLAB','LD_LIBRARY_'):
    oldpath = os.environ.get(key+'PATH')
    if oldpath:
        paths = oldpath.split(':')
        if libdir not in paths:
            paths.append(libdir)
        path[key] = ':'.join(paths)
    else:
        path[key] = libdir

Flow('spike',None,'spike n1=1000 n2=100 n3=10 nsp=1 k1=500')
Flow('clip','spike clip.m',
     '''
     MATLABPATH=%(MATLAB)s LD_LIBRARY_PATH=%(LD_LIBRARY_)s
     %(exe)s -nosplash -nodesktop
     -r "clip('$SOURCE','$TARGET',0.5);quit"
     ''' % path,stdin=0,stdout=-1)
Flow('test.attr','clip','attr')


# -*- coding: utf-8 -*-

import torch
import random
import os
import json
import argparse
import pandas as pd
import numpy as np
from sub_edge_embedding import DrugDataset
from sub_edge_embedding import DrugDataLoader
from models import RealFakeDDICo
from engine4rf import Engine4RealFakeDDI

def config():
    parser = argparse.ArgumentParser()
    parser.add_argument("--batch_size", type=int, default=256)
    parser.add_argument("--end_lr", type=float, default=1e-6)
    parser.add_argument("--lr_decay_interval", type=int, default=3000)
    parser.add_argument("--lr_decay_rate", type=float, default=0.95)
    parser.add_argument("--device", type=int, default=0)
    parser.add_argument("--epoch", type=int, default=200)
    parser.add_argument("--dropout", type=float, default=0.2)
    parser.add_argument("--ratio_k", type=int, default=26)
    #data 3
    parser.add_argument("--train_filename", type=str,default='data/drugbank_cold_start/3/ddi_train_cleaned.csv')
    #new-new
    parser.add_argument("--test_filename1", type=str,default='data/drugbank_cold_start/3/ddi_bothnew_cleaned.csv')
    #new-old
    parser.add_argument("--test_filename2", type=str,default='data/drugbank_cold_start/3/ddi_eithernew_cleaned.csv')
    parser.add_argument("--sample_file", type=str, default='data/drugbank_cold_start/3/ssi_old_new_3.json')
    parser.add_argument("--block_path", type=str, default='checkpoint/3/no_acc0.7955roc0.8607prc0.8686dp0.2k26bs256')
    parser.add_argument("--fine_model_path", default='checkpoint/3')
    parser.add_argument("--test", default=True)
    args = parser.parse_args()
    return args

def set_all_seeds(seed): #6535
    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)
    torch.backends.cudnn.benchmark = False
    torch.backends.cudnn.deterministic = True
def worker_init(worker_init):
    seed= 6535
    np.random.seed(int(seed) + worker_init)

def get_data(args,drug_old_ids, drug_new_ids):
    # train
    df_ddi_train = pd.read_csv(args.train_filename)
    train_tup = [(h, t, r, 1) for h, t, r in zip(df_ddi_train['d1'], df_ddi_train['d2'], df_ddi_train['type'])]
    train_data = DrugDataset(train_tup, all_drug_old_ids=drug_old_ids, all_drug_new_ids=drug_new_ids, mode="ttrain")
    # test(new-new)
    df_ddi_test = pd.read_csv(args.test_filename1)
    test_tup = [(d1, d2, t, 1) for d1, d2, t in zip(df_ddi_test['d1'], df_ddi_test['d2'], df_ddi_test['type'])]
    test_neg_tup = train_data.generate_neg_pair(test_tup)
    test_tup = test_tup + test_neg_tup
    test_data1 = DrugDataset(test_tup,all_drug_old_ids=drug_old_ids, all_drug_new_ids=drug_new_ids, mode="ttest")
    # test(new-old)
    df_ddi_test = pd.read_csv(args.test_filename2)
    test_tup = [(d1, d2, t, 1) for d1, d2, t in zip(df_ddi_test['d1'], df_ddi_test['d2'], df_ddi_test['type'])]
    test_neg_tup = train_data.generate_neg_pair(test_tup)
    test_tup = test_tup + test_neg_tup
    test_data2 = DrugDataset(test_tup,all_drug_old_ids=drug_old_ids, all_drug_new_ids=drug_new_ids, mode="ttest")

    print(f"Training with {args.train_filename}, test with {args.test_filename1},{args.test_filename2}")
    print(f"Training with {len(train_data)} samples, new-new test with {len(test_data1)} samples")
    print(f"New-old test with {len(test_data2)} samples")
    return train_data,test_data1,test_data2

def main():
    set_all_seeds(6535)
    torch.autograd.set_detect_anomaly(True)

    args = config()

    with open(args.sample_file, 'r', encoding='utf8') as f:
        ontology = json.load(f)
    drug_old_ids = np.array(ontology['old'])
    drug_new_ids = np.array(ontology['new'])
    train_data,test_data1,test_data2 = get_data(args, drug_old_ids, drug_new_ids)

    train_dataloader = DrugDataLoader(train_data,
                                      collate_fn=train_data.collate_fn_fake_or_real,
                                      batch_size=args.batch_size,
                                      worker_init_fn=worker_init,
                                      shuffle=True,
                                      num_workers = 3)

    test_dataloader1 = DrugDataLoader(test_data1,
                                      collate_fn=test_data1.collate_fn_fake_or_real,
                                      batch_size=args.batch_size,
                                      shuffle=True,
                                      num_workers=3)

    test_dataloader2 = DrugDataLoader(test_data2,
                                     collate_fn=test_data2.collate_fn_fake_or_real,
                                     batch_size=args.batch_size,
                                     shuffle=True,
                                     num_workers=3)
    if args.device >= 0:
        device = torch.device("cuda:{}".format(args.device))
    else:
        device = torch.device("cpu:{}".format(0))

    model = RealFakeDDICo(with_global=True, ratio_k=args.ratio_k,  dropout_ratio=args.dropout)
    model = model.to(device)

    assert 256 % args.batch_size == 0
    gradient_stack = None
    if args.batch_size < 256:
        gradient_stack = 256 // args.batch_size

    engine = Engine4RealFakeDDI(args,
                                model,
                                train_dataloader,
                                test_dataloader1,
                                test_dataloader2,
                                None,
                                args.end_lr,
                                args.lr_decay_interval,
                                args.lr_decay_rate,
                                args.fine_model_path,
                                device,
                                gradient_stack=gradient_stack)

    if args.test:
        print("load pretrained graph model")
        model.load_state_dict(torch.load(args.block_path))
        print("new-new test:")
        engine.test(test_dataloader1, If_newnew=True)
        print("new-old test:")
        engine.test(test_dataloader2, If_newnew=False)
    else:
        engine.train(args.epoch)

if __name__ == '__main__':
    main()


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as mpl
import genai_evaluation as ge
from genai_evaluation import multivariate_ecdf, ks_statistic
import nogan_synthesizer as ns
from nogan_synthesizer import NoGANSynth
from nogan_synthesizer.preprocessing import wrap_category_columns, unwrap_category_columns

import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)

pd.core.common.random_state(None)
seed = 1003
np.random.seed(seed)

#--- [1] read data, feature selection, remove missing values

url = "https://raw.githubusercontent.com/VincentGranville/Main/main/students.csv"
data = pd.read_csv(url)

data = data.drop(data[data["Curricular_units_2nd_sem_grade"] == 0].index)

features = [
  'Curricular_units_2nd_sem_approved',
  'Curricular_units_2nd_sem_grade',
  'Curricular_units_1st_sem_approved',
  'Curricular_units_1st_sem_grade',
  'Admission_grade',
  'Tuition_fees_up_to_date',
  'Curricular_units_2nd_sem_evaluations',
  'Age_at_enrollment',
  'Previous_qualification_grade',
  'Curricular_units_1st_sem_evaluations',
  'Course',
  'Father_occupation',
  'Mother_occupation',
  'Unemployment_rate',
  'GDP',
  'Application_mode',
  'Father_qualification',
  'Curricular_units_2nd_sem_enrolled',
  'Mother_qualification',
  'Inflation_rate',
  'Target']

target_column = 'Target'

cat_cols = ['Course', 'Father_occupation', 'Mother_occupation',
            'Unemployment_rate', 'GDP', 'Inflation_rate',
            'Application_mode', 'Father_qualification',
            'Mother_qualification', 'Tuition_fees_up_to_date', 'Target'
            ]

num_cols = [f for f in features if f not in cat_cols]

data = data[features]

#--- [2] Split real data into training and validation sets

training_data = data.sample(frac = 0.5)
validation_data = data.drop(training_data.index)

#--- [3] Preprocess Categorical Columns

wrapped_train_data, idx_to_key_train, key_to_idx_train = \
                    wrap_category_columns(training_data,cat_cols)

wrapped_val_data, idx_to_key_val, key_to_idx_val = \
                    wrap_category_columns(validation_data,cat_cols)

#--- [4] Train model

bin_values = [100] * len(wrapped_train_data.columns)
nogan = NoGANSynth(wrapped_train_data,random_seed=seed)
nogan.fit(bins = bin_values)

#--- [5] Generate 3 synth. data: original NoGAN, Gaussian, Uniform

stretch_type_gaussian  = (["Gaussian"] * (len(wrapped_train_data.columns)-1)) + ["Uniform"]
stretch_nogan_orig  = [-1] * len(wrapped_train_data.columns)

print("Synth. NoGAN Original:")
wrapped_nogan_orig_synth_data = nogan.generate_synthetic_data(len(wrapped_train_data),
                                                         debug = True ,
                                                         stretch = stretch_nogan_orig
                                                         )
print("\nSynth. NoGAN Gaussian:")
wrapped_nogan_gauss_synth_data = nogan.generate_synthetic_data(len(wrapped_train_data),
                                                         debug = True ,
                                                         stretch_type= stretch_type_gaussian
                                                         )
print("\nSynth. NoGAN Uniform:")
wrapped_nogan_uniform_synth_data = nogan.generate_synthetic_data(len(wrapped_train_data),
                                                         debug = True ,
                                                         )
print()

#--- [6] Evaluate using ECDF & KS Stat

_, ecdf_val1, ecdf_nogan_orig_synth = \
            multivariate_ecdf(wrapped_val_data,
                              wrapped_nogan_orig_synth_data,
                              n_nodes = 1000,
                              verbose = True,
                              random_seed=seed)

_, ecdf_val2, ecdf_nogan_gauss_synth = \
            multivariate_ecdf(wrapped_val_data,
                              wrapped_nogan_gauss_synth_data,
                              n_nodes = 1000,
                              verbose = True,
                              random_seed=seed)

_, ecdf_val3, ecdf_nogan_uniform_synth = \
            multivariate_ecdf(wrapped_val_data,
                              wrapped_nogan_uniform_synth_data,
                              n_nodes = 1000,
                              verbose = True,
                              random_seed=seed)

_, ecdf_val4, ecdf_train = \
            multivariate_ecdf(wrapped_val_data,
                              wrapped_train_data,
                              n_nodes = 1000,
                              verbose = True,
                              random_seed=seed)
print()
ks_stat_nogan_orig = ks_statistic(ecdf_val1, ecdf_nogan_orig_synth)
ks_stat_nogan_gauss = ks_statistic(ecdf_val2, ecdf_nogan_gauss_synth)
ks_stat_nogan_uniform = ks_statistic(ecdf_val3, ecdf_nogan_uniform_synth)
ks_stat_train = ks_statistic(ecdf_val4, ecdf_train)

print(f"KS Stat NoGAN Original(Synth vs Validation): {ks_stat_nogan_orig:.5f}")
print(f"KS Stat NoGAN Gauss (Synth vs Validation): {ks_stat_nogan_gauss:.5f}")
print(f"KS Stat NoGAN Uniform (Synth vs Validation): {ks_stat_nogan_uniform:.5f}")
print(f"Base KS Stat (Synth vs Train): {ks_stat_train:.5f}")

#--- [7] Expand Categorical Columns in Synth Data

nogan_orig_synth_data = unwrap_category_columns(data=wrapped_nogan_orig_synth_data,
                                                 idx_to_key=idx_to_key_train, cat_cols=cat_cols)
nogan_orig_synth_data = nogan_orig_synth_data[features]

nogan_gauss_synth_data = unwrap_category_columns(data=wrapped_nogan_gauss_synth_data,
                                                 idx_to_key=idx_to_key_train, cat_cols=cat_cols)
nogan_gauss_synth_data = nogan_gauss_synth_data[features]

nogan_uniform_synth_data = unwrap_category_columns(data=wrapped_nogan_uniform_synth_data,
                                                 idx_to_key=idx_to_key_train, cat_cols=cat_cols)
nogan_uniform_synth_data = nogan_uniform_synth_data[features]

#--- [8] Evaluation scatterplots

dfs_orig = nogan_orig_synth_data
dfs_gauss = nogan_gauss_synth_data
dfs_uniform = nogan_uniform_synth_data
dfv = validation_data

def vg_scatter(df, feature1, feature2, counter, xlim, ylim):

    # customized plots, subplot position based on counter

    label = feature1[0:20] + " vs " + feature2[0:20]
    df = df[(df[feature1] >= xlim[0]) &
            (df[feature1] <= xlim[1]) &
            (df[feature2] >= ylim[0]) &
            (df[feature2] <= ylim[1])
           ]
    x = df[feature1].to_numpy()
    y = df[feature2].to_numpy()
    plt.subplot(3, 2, counter)
    plt.scatter(x, y, s = 0.1, c ="blue")
    plt.xlabel(label, fontsize = 7)
    plt.xticks([])
    plt.yticks([])
    return()

mpl.rcParams['axes.linewidth'] = 0.3

[col1, col2] = [features[4], features[7]]
xlim = [min(dfv[col1]), max(dfv[col1])]
ylim = [min(dfv[col2]), max(dfv[col2])]
vg_scatter(dfs_uniform, col1, col2, 1, xlim, ylim)
vg_scatter(dfv, col1, col2, 2, xlim, ylim)

[col1, col2] = [features[4], features[3]]
xlim = [min(dfv[col1]), max(dfv[col1])]
ylim = [10, max(dfv[features[3]])]
vg_scatter(dfs_uniform, features[4], features[3], 3, xlim, ylim)
vg_scatter(dfv, features[4], features[3], 4, xlim, ylim)

[col1, col2] = [features[2], features[0]]
xlim = [min(dfv[features[2]]), max(dfv[features[0]])]
ylim = [min(dfv[features[2]]), max(dfv[features[0]])]
vg_scatter(dfs_uniform, features[2], features[0], 5, xlim, ylim)
vg_scatter(dfv, features[2], features[0], 6, xlim, ylim)

plt.show()


class BasicInfo:

    def __init__(self):
        self.con = None

    def handleBinfo(self,title,rawinfo,proinfo,sult_plaint,fee,legal):
        sitem = {}
        sitem['事项名称'] = title.get('标题','')
        sitem['基本编码'] = title.get('基本编码','')
        sitem['事项类型'] = rawinfo.get('事项类型','')
        sitem['权力来源'] = rawinfo.get('权力来源','')
        sitem['行使层级'] = rawinfo.get('行使层级','')
        sitem['实施编码'] = title.get('实施编码','')
        sitem['业务办理项编码'] = ''
        sitem['事项状态'] = rawinfo.get("事项状态",'')
        sitem['事项版本'] = rawinfo.get("事项版本",'')
        sitem['实施主体'] = rawinfo.get("实施主体",'')
        sitem['实施主体性质'] = rawinfo.get("实施主体性质",'')
        sitem['委托部门'] = ''

        num_unit = rawinfo.get("法定办结时限（工作日）",'')
        num_unit = num_unit.split(' ')
        if len(num_unit) == 2:
            sitem['法定办结时限'] = num_unit[0] if num_unit[0] != '-' else ''
            sitem['法定办结时限单位'] = num_unit[1]
        else:
            sitem['法定办结时限'] = '-1'       ###办结实现为-1表示该元素点未知
            sitem['法定办结时限单位'] = '工作日'
        sitem['法定办结时限说明'] = ''

        num_unit = rawinfo.get("承诺办结时限（工作日）",'')
        num_unit = num_unit.split(' ')
        if len(num_unit) == 2:
            sitem['承诺办结时限'] = num_unit[0] if num_unit[0] != '-' else ''
            sitem['承诺办结时限单位'] = num_unit[1]
        else:
            sitem['承诺办结时限'] = '-1'       ###办结实现为-1表示该元素点未知
            sitem['承诺办结时限单位'] = '工作日'
        sitem['承诺办结时限说明'] = ''

        sitem['服务对象'] = rawinfo.get("服务对象",'')
        sitem['办件类型'] = rawinfo.get("办件类型",'')
        sitem['办理形式'] = rawinfo.get('办理形式','')
        sitem['到办事现场次数'] = rawinfo.get('到办事现场次数','')
        sitem['特别程度'] = ''
        sitem['办理地点'] = proinfo.get('办理地点','')
        sitem['办理时间'] = proinfo.get('办理时间','')

        consult = sult_plaint.get('咨询方式','')
        sitem['咨询方式'] = str(consult)   ### 转为字符串的方法，一种是json 一种直接str
        complaint = sult_plaint.get("投诉方式",'')
        sitem['监督投诉方式'] = str(complaint)

        sitem['通办范围'] = rawinfo.get("通办范围",'')
        tmp = rawinfo.get("联办机构",'')
        sitem['联办机构'] = tmp if tmp != '-' else ''

        sitem['中介服务'] = ''
        sitem['数量限制'] = ''
        sitem['审批结果类型'] = ''
        sitem['审批结果名称'] = ''
        sitem['审批结果样本'] = ''
        sitem['是否支持预约办理'] = ''

        online_pay = fee.get('是否支持网上支付','')
        sitem['是否支持网上支付'] = online_pay if online_pay != '-' else ''

        sitem['是否支持快递服务'] = rawinfo.get('是否支持物流快递','')
        sitem['是否支持自助终端办理'] = ''
        sitem['是否网办'] = ''
        sitem['必须现场办理原因说明'] = ''
        sitem['适用范围'] = ''
        sitem['是否属于“工改”事项'] = ''
        sitem['业务办理系统名称'] = ''

        sitem['行政诉讼途径'] = str(legal.get("行政诉讼",''))    ######dict转str
        sitem['行政复议途径'] = str(legal.get('行政复议',''))

        sitem['是否进驻政务大厅'] = rawinfo.get('是否进驻政务大厅','')

        self.con = sitem

class OutFormat:

    def __init__(self):
        self.content = {}

    def handleItem(self,region,rawdict):
        item = Item()
        item.handleCon(region,rawdict)
        self.content['事项信息'] = item.con

    def handleSubItem(self,guides):
        self.content['子事项信息'] = []
        for guide in guides:
            # print(guide)
            sitem = SubItem()
            sitem.handleCon(guide)
            self.content['子事项信息'].append(sitem.con)

        if self.content['事项信息'] and self.content['子事项信息']:
            if '基本信息' not in self.content['子事项信息'][0]:
                return
            self.content['事项信息']['基本编码'] = self.content['子事项信息'][0]['基本信息']['基本编码']
            self.content['事项信息']['行使层级'] = self.content['子事项信息'][0]['基本信息']['行使层级']

class Item:

    def __init__(self):
        self.con = {}

    def handleCon(self,region,rawdict):

        ## 基本编码 / 行使层级
        # print(rawdict)
        rawdict = rawdict.get('基本信息','')
        if rawdict == "":
            return
        self.con['事项名称'] = rawdict.get('权责事项名称','')
        self.con['事项类别'] = rawdict.get('事项类型','')
        self.con['基本编码'] = ''
        self.con['实施主体'] = rawdict.get('实施主体','')
        ## 实施层级及权限
        tmp = rawdict.get('实施层级及权限','')
        tmp = tmp.split('|')
        if len(tmp) == 2:
            self.con['行使层级'] = tmp[0].strip()
        else:
            self.con['行使层级'] = ''

        self.con['实施依据'] = rawdict.get('设定、行使依据及有关条款','')
        self.con['所属地区'] = region
        self.con['备注'] = ''
        # print(self.con)

class SubItem:
    def __init__(self):
        self.con = {}

    def handleCon(self,guide):
        #title,rawinfo,proinfo,sult_plaint,fee,legal
        if '标题信息' not in guide:
            print('No title')
            return
        title = guide["标题信息"]
        rawinfo = guide['基本信息']
        proinfo = guide['办理信息']
        evi = guide['设定依据']
        flow = guide['办理流程']
        legal = guide['法律救济']
        if not legal:
            legal = {}
        cond = guide['受理条件']
        material = guide['材料目录']
        sult_plaint = guide['咨询/投诉方式']
        if '收费信息' in guide:
            fee = guide["收费信息"]
        else:
            fee = {}

        self.handleBasicInfo(title,rawinfo,proinfo,sult_plaint,fee,legal)
        self.handleFlow(flow)
        self.handleMat(material)
        self.handleCond(cond)
        self.hanldeFee(fee)
        self.handleEvi(evi)

    def handleFlow(self,flow):
        items = {}
        items['办理流程图'] = flow['流程图url']
        lst = []
        idx = 0
        for step in flow['办理步骤']:
            newstep = {}
            newstep['序号'] = idx
            idx += 1
            newstep['办理流程环节名称'] = step['环节名称']
            newstep['办理流程环节办理时限'] = step['办理时限']
            newstep['办理流程环节审批标准'] = step['审批标准']
            lst.append(newstep)
        items['办理步骤'] = lst
        self.con['办理流程'] = items

    def handleMat(self,materials):

        idx = 0
        lst = []
        for material in materials:
            newmat = {}
            newmat['序号'] = idx
            idx += 1
            newmat['材料名称'] = material['材料名称']
            newmat['空白表格'] = material['空白表格']
            newmat['来源渠道'] = ''
            newmat['纸质材料份数'] = material['纸质材料份数']
            newmat['材料必要性'] = material['材料必要性']
            newmat['填表须知'] = ''
            newmat['是否已实现数据共享'] = ''
            lst.append(newmat)
        self.con['办理材料'] = lst

    def handleCond(self,cond):
        self.con['受理条件'] = cond

    def hanldeFee(self,fee):
        item = {}
        item['序号'] = 0
        item['费用名称'] = fee.get('收费项目名称:','')
        item['费用标准'] = fee.get('收费标准:','')
        item['费用说明'] = ""
        self.con['收费情况'] = [item]

    def handleEvi(self,evi):
        self.con['设定依据'] = evi

    def handleBasicInfo(self,title,rawinfo,proinfo,sult_plaint,fee,legal):
        sitem = BasicInfo()
        sitem.handleBinfo(title,rawinfo,proinfo,sult_plaint,fee,legal)
        self.con['基本信息'] = sitem.con

if __name__ == '__main__':
    from util import *
    import os
    details = getFromFile('../SDQZdata/pagedetail_sd.json')
    subpath = '../SDQZdata/sd/'
    subnames = os.listdir(subpath)
    # details = getFromFile('../SDQZdata/sd/58_0.json')
    flag = 0
    # for i in range(len(details)):
    # subindx = 0
    for detail in details:
        # detail = details[i]
        subidx = 0
        prename = detail['idx']
        print(prename)
        guides = []
        while True:
            name = str(prename)+'_'+str(subidx)+'.json'
            if name in subnames:
                flag = 1
                subidx += 1
                guide = getFromFile(subpath+name)
                guides.append(guide)
            else:
                break

        out = OutFormat()
        out.handleItem('山东省',detail)
        if flag == 1:
            out.handleSubItem(guides)

        saveToFile('../SDQZdata/excel_sd/'+str(prename)+'.json',out.content)

        # out = OutFormat()
        # out.handleSubItem([details])
        # # out.handleItem('山东省',detail)
        # print(out.content['子事项信息'])
        # saveToFile('1.json',out.content['子事项信息'])
        # break


#Problem A -- Double Values
def double_values(dct):
    out = []
    for key in dct.keys():
        dct[key] = dct[key]*2
    return dct

#Problem B -- Lookup Composition
def get_phones(name_office, office_phone):
    dict1 = {}

    for prof in name_office.keys():
        if name_office[prof] in office_phone.keys():
            dict1[prof] = office_phone[name_office[prof]]
    return dict1

#Problem C -- Total Hours
def log_hours(fname):
    dict1 = {}
    fp_in = open(fname, 'r')
    line = fp_in.read()
    linelist = line.split('\n')
    for ele in linelist:
        list = ele.split()
        print(ele)
        print(dict1.keys())
        if list[0] not in dict1.keys():
            dict1[list[0]] = int(list[1])
        else:
            dict1[list[0]] += int(list[1])
    return dict1


# coding=utf-8

import sublime
import sublime_plugin
import urllib
import urllib2
import thread
import re

class EijiroCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        point = self.view.sel()[0].begin()
        self.word = self.view.substr(self.view.word(point))

        if not self.word:
            sublime.status_message("No word found.")
            return
        else:
            self.consult_dictionary(self.word)

    def consult_dictionary(self, word):
        encoded_word = urllib.quote(word)
        self.url = "http://eow.alc.co.jp/{0}/UTF-8/".format(encoded_word)
        thread.start_new_thread(self.fetch_remote_dictionary, ())

    def fetch_remote_dictionary(self):
        try:
            request = urllib2.Request(self.url, headers={"User-Agent":
                    "Sublime Eijiro"})
            return self.handle_response(urllib2.urlopen(request, timeout=10))

        except urllib2.HTTPError as e:
            self.handle_error("HTTP", e)

        except urllib2.URLError as e:
            self.handle_error("URL", e)

    def handle_response(self, response):
        self.content = response.read()
        sublime.set_timeout(self.show_output_view, 0)

    def show_output_view(self):
        output_view = self.view.window().get_output_panel("eijiro." + self.word)

        output_view.set_scratch(True)
        output_view.set_name("Eijiro Result")

        edit = output_view.begin_edit()
        raw_content = self.extract_content(self.content)
        output_view.replace(edit, sublime.Region(0, output_view.size()), raw_content.decode("utf-8")[:])
        output_view.end_edit(edit)

        output_view.set_read_only(True)
        self.view.window().run_command("show_panel", {"panel": "output.eijiro." + self.word})

    def extract_content(self, content):
        pattern = r"<!-- ▼ 検索結果本体 ▼ -->\s*(.+)\s*<!-- ▲ 検索結果本体 ▲ -->"
        matched = re.search(pattern, content, re.M | re.S)
        if matched:
            raw_content = matched.group(1)
            raw_content = re.sub(r"</li>", "\n", raw_content)
            raw_content = re.sub(r"<[^>]+>", "",   raw_content)
            return raw_content.strip()

    def handle_error(self, type, error):
        self.message = '{0}: {1} error {2} downloading {3}.'.format(__name__, type, error.message, str(error.code), self.url)
        sublime.set_timeout(self.show_status_message, 0)

    def show_status_message(self):
        sublime.status_message(self.message)


import argparse
import logging
import numpy
import mesh_annotate.Config
import mesh_annotate.Mesh
import mesh_annotate.Expression

# parse command line options
l_parser = argparse.ArgumentParser( description='Mesh annotations in EDGE.' )

l_parser.add_argument( '-x', '--xml',
                       dest     = 'xml',
                       required = True,
                       type     = str,
                       help     = 'XML configuration of the mesh annotation.')
l_args = vars( l_parser.parse_args() )

# setup logger
logging.basicConfig( level=logging.DEBUG,
                     format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' )

# welcome our users
logging.info( 'performing the mesh annotation, please hold..' )

# parse xml config
logging.info('parsing XML')
l_config = mesh_annotate.Config.Config( l_args['xml'] )

# read mesh
logging.info( 'reading mesh' )
l_mesh = mesh_annotate.Mesh.Mesh( l_config.m_meshes['in'] )

if( l_mesh.m_nDis == 2 ):
  l_elTy = 'tria'
elif( l_mesh.m_nDis == 3 ):
  l_elTy = 'tet'
else: assert( False )

logging.info( 'mesh info:' )
logging.info( '  #vertices: '  + str( l_mesh.nEn( 'vertex' ) ) )
logging.info( '  #edges: '     + str( l_mesh.nEn( 'edge' )   ) )
logging.info( '  #triangles: ' + str( l_mesh.nEn( 'tria' )   ) )
if( l_elTy == 'tet' ):
  logging.info( '  #tetrahedrons: ' + str( l_mesh.nEn( 'tet' ) ) )

# get vertex coordinates
logging.info('querying mesh for vertex coordinates')
l_veCrds = l_mesh.veCrds()
logging.info('querying mesh for element to vertex adjacency')

# get vertices adjacent to the element
l_elVe =  l_mesh.enAd(l_mesh.m_nDis, 0)

# reshape the array according to the number of vertices per element
if( l_mesh.m_nDis == 2 ):
  assert( l_mesh.nEn('tria')*3 == len(l_elVe) )
  l_elVe = numpy.reshape( l_elVe, (-1, 3) )
elif( l_mesh.m_nDis == 3 ):
  assert( l_mesh.nEn('tet')*4 == len(l_elVe) )
  l_elVe = numpy.reshape( l_elVe, (-1, 4) )
else:
  assert( False )

# evaluate expression
logging.info( 'evaluating expression' )
if( l_config.m_type == 'vertices' ):
  l_vals = mesh_annotate.Expression.evalVe( len(l_config.m_vars),
                                            l_config.m_expr,
                                            l_elVe,
                                            l_veCrds )
elif( l_config.m_type == 'centroids' ):
  l_cens = l_mesh.cen( l_elTy )
  l_vals = mesh_annotate.Expression.evalPt( len(l_config.m_vars),
                                            l_config.m_expr,
                                            l_cens )
else: assert( False )

# wipe global ids tag
logging.info( 'wiping entity ids' )
l_mesh.deleteIds()

# store the data
logging.info( 'writing annotated mesh' )
for l_va in range(len(l_config.m_vars)):
  l_mesh.store( l_elTy,
                l_config.m_vars[l_va],
                l_vals[l_va, :].flatten() )

l_mesh.write( l_config.m_meshes['out'] )

logging.info( 'done' )


from matplotlib import pyplot as plt
from colorseq import DistinctColors

dc_h = DistinctColors(20, (0.0, 1.0), 1.0, 1.0)
dc_s = DistinctColors(20, 0.4, (0.1, 1.0), 1.0)
dc_v = DistinctColors(20, 0.4, 1.0, (0.1, 1.0))
dc_comb = DistinctColors(
        20, (0.0, 1.0), (0.1, 1.0), (0.7, 1.0),
        h_shuffle=True, s_shuffle=True, v_shuffle=True
        )
colors_h = dc_h.get_colors()
colors_s = dc_s.get_colors()
colors_v = dc_v.get_colors()
colors_comb = dc_comb.get_colors()
fig, ax = plt.subplots()
ax.scatter(
        [*range(20)], [2 for _ in range(20)], s=200.0,
        c=colors_comb, marker='v', label='combined'
        )
ax.scatter(
        [*range(20)], [1.5 for _ in range(20)], s=200.0,
        c=colors_v, marker='s', label='value'
        )
ax.scatter(
        [*range(20)], [0.5 for _ in range(20)],
        s=200.0, c=colors_s, marker='<', label='saturation'
        )
ax.scatter(
        [*range(20)], [0 for _ in range(20)], s=200.0,
        c=colors_h, label='hue'
        )
ax.legend(loc='best', numpoints=4)
plt.show()


from cx_Freeze import setup, Executable
import os

os.environ['TCL_LIBRARY'] = r'C:\ProgramData\Miniconda3\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\ProgramData\Miniconda3\tcl\tk8.6'

base = None

executables = [Executable("file_open_dialog.py", base=base)]

packages = ["idna"]
options = {
    'build_exe': {
        'packages':['pathlib', 'tkinter'],
    },
}

setup(
    name = "File Dialog print path",
    options = options,
    version = "0.1",
    description = 'Opens a file dialog, chosen excel file is saved as pandas pickle',
    executables = executables
)


import os
import time

def main():
    cls = lambda: os.system('cls')
    try:
        user_name = str(input("What is your name?...."))
        user_age = int(input("How old are you?......"))
        user_city = str(input("Where do you live?...."))

        print(f"\n{'-' * 20}\nHello, {user_name}.\n"
              f"Your age is {user_age}.\n"
              f"You live in {user_city}.")
        time.sleep(3)
    except ValueError:
        print("You entered wrong values! Try again...")
        time.sleep(2)
        cls()
        main()

if __name__ == '__main__':
    main()


import requests
import logging
import json
from SmsSender import SendMessageReceiver
from UrlCreator import get_url, headers

class SendSmsRequest(SendMessageReceiver):
    uuid = ""

    def __init__(self):
        logging.debug(SendSmsRequest.__class__.__name__)

    def send_message_request(self):
        url = get_url("Message")
        params = {
            "src": "14158408589",
            "dst": "14158408583",
            "text": "Hello ! Good Morning"
        }
        try:
            re = requests.post(url, json=params, headers=headers)
            if re.status_code == 202:
                response = json.loads(re.text)
                return response
            else:
                logging.error("Send message Failed with error" + re.status_code)

        except TimeoutError as err:
            logging.error(err)

    def get_uuid_from_message(self):
        self.uuid = super().format_sms_response(self.send_message_request())


from collections import deque
from typing import Optional

'''
116. Populating Next Right Pointers in Each Node | Medium
You are given a perfect binary tree where all leaves are on the same level, and every parent
has two children. The binary tree has the following definition:

struct Node {
  int val;
  Node *left;
  Node *right;
  Node *next;
}

Populate each next pointer to point to its next right node. If there is no next right node,
the next pointer should be set to NULL.
Initially, all next pointers are set to NULL.
--------------------------------------------------------------------------------------------
Have a queue and insert the nodes at each level from right to left. then within a level we
can populate the next right pointer going in order
'''

class Node:
    def __init__(
            self, val: int = 0,
            left: Optional['Node'] = None,
            right: Optional['Node'] = None,
            next: Optional['Node'] = None
            ):
        self.val = val
        self.left = left
        self.right = right
        self.next = next

def connect(root: Optional[Node]) -> Optional[Node]:
    if not root:
        return None

    queue = deque([root])

    while queue:
        nextRight = None

        for _ in range(len(queue)):
            cur = queue.popleft()
            cur.next = nextRight
            nextRight = cur

            if cur.right:
                queue.append(cur.right)
                queue.append(cur.left) # type: ignore

    return root


def __getConversionIndex(value):
    """Calculate an index (enum), that is used for deciding how
    to convert this input value. The index is used in the
    __convertWithIndex function."""
    if isinstance(value, numpy.ndarray):
        if (value.ndim == 2):
            convIndex = 2
        elif (value.ndim ==1):
            convIndex = 1
        else:
            raise Exception("Only ndarrays of dimensions 1 or 2 are supported")
    else:
        convIndex = 0
    return convIndex

def __convertWithIndex(value, convIndex):
    """Conert an input value with the help of a conversion index.
    The index is calculated with the __getConversionIndex function."""
    if (convIndex == 1):
        return value.tolist()
    elif (convIndex == 2):
        return value[0].tolist()

    # Default case is to return value
    return value


import cv2
import math
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import random
import time

from Simulator.Constants import HFOV, VFOV, IMG_WIDTH, IMG_HEIGHT, TARGET_POINTS

class CameraSim:

	#constants for https://www.amazon.com/Microsoft-H5D-00013-LifeCam-Cinema/dp/B009CPC6QA
	#info about pinhole https://staff.fnwi.uva.nl/r.vandenboomgaard/IPCV20172018/LectureNotes/CV/PinholeCamera/PinholeCamera.html
	#also: from Georgia Tech's CS 4476 (Computer Vision): https://www.cc.gatech.edu/~hays/compvision/lectures/02.pdf

	def __init__(self, hFOV=HFOV, vFOV=VFOV, xPix=IMG_WIDTH, yPix=IMG_HEIGHT, targetShape=TARGET_POINTS):
		#horizontal and vertical fov of the camera
		self.hFOV = hFOV #in radians
		self.vFOV = vFOV

		#pixels dimensions of the output image from the camera
		self.xPix = xPix #in pixels
		self.yPix = yPix

		self.xFocal = (xPix / 2) / math.tan(hFOV / 2) #the focal length of the camera in pixels; x and y have
		self.yFocal = (yPix / 2) / math.tan(vFOV / 2) #slightly different values because pixels are not perfectly square

		self.camHeading = 0
		self.camPos = np.array([0.0, 0.0, 0.0])

		self.targetShape = np.array(targetShape)

	def setCamPos(self, xPos, yPos, zPos, heading):
		'''Set the pose of the camera in 3D space
		'''
		self.camPos[0] = xPos
		self.camPos[1] = yPos
		self.camPos[2] = zPos
		self.camHeading = heading

	def convertToPixels(self, point):
		'''Given a point in 3D space, figure out where that point lies on the a 2D image
		'''
		#Note: we're in y-up coords
		camToTarget = point - self.camPos

		pinholeMatrix = np.array([[ self.xFocal, 0, 		  self.xPix / 2], \
								  [ 0, 			 self.yFocal, self.yPix / 2], \
								  [ 0,			 0, 		  1]])

		headingRotationMatrix = np.array([[math.cos(self.camHeading), 0, math.sin(self.camHeading), 0], \
								   		  [0, 1, 0, 0], \
								   		  [-math.sin(self.camHeading), 0, math.cos(self.camHeading), 0]])

		projectedPoint = np.array([camToTarget[0], camToTarget[1], camToTarget[2], 1]) #project into a slice of a 4D enviornment so we can do homogenous coordinate math

		cameraMatrix = np.matmul(pinholeMatrix, headingRotationMatrix)

		imgPos = np.matmul(cameraMatrix, projectedPoint) #* (1 / (camToTarget[2]))

		if imgPos[2] < 1e-5:
			return None

		return [imgPos[0] / imgPos[2], imgPos[1] / imgPos[2]] #convert back from homogenenous coordinates and return value on the screen

	def getFrame(self):
		'''Get a binary image of the target taken by the camera
		'''
		pxTargetPoints = []
		for point in self.targetShape:
			pixelPt = self.convertToPixels(point)
			if pixelPt != None:
				pxTargetPoints.append(pixelPt)

		blankImg = np.zeros((self.yPix, self.xPix))
		if len(pxTargetPoints) != len(self.targetShape):
			return blankImg

		pxTargetPoints = np.array(pxTargetPoints, dtype=np.int32)

		area = 0
		if len(pxTargetPoints) > 0:
			img = cv2.fillPoly(blankImg, pts=[pxTargetPoints], color=255)

		return img

if __name__ == '__main__':

	sim = CameraSim()

	initTime = time.time()
	for heading in range(0, 100, 1):
		sim.setCamPos(0, 0, heading/10, 0)
		print(heading / 10)
		frame = sim.getFrame()
		cv2.imshow("test", np.uint8(frame))
		cv2.waitKey(0)


import traceback

import gevent
from cronjob.apps.spider_app import SpiderJob
from lxml import etree

from ipfeeder.db import db
from ipfeeder.utils import ProxyIP, decode_port

class GoubanjiaProxy(SpiderJob):
    rule = '1h'  # 每一个小时执行一次
    right_now = True  # 程序启动之后立马执行
    cancelled = False

    urls = ['http://www.goubanjia.com/']

    def run(self) -> None:
        for url in self.urls:
            response = self.http.get(url)
            if not response.ok:
                self.logger.error(f'request failed {response.status_code}')
                continue
            html = etree.HTML(response.content)
            trs = html.xpath('..//table[@class="table table-hover"]//tr')
            xpath_str = """./td[@class="ip"]//*[not(contains(@style, 'display: none'))
                                                        and not(contains(@style, 'display:none'))
                                                        and not(contains(@class, 'port'))
                                                        ]/text()
                                                        """
            for tr in trs:
                try:
                    tds = tr.xpath('./td')
                    if len(tds) < 5:
                        continue
                    protocol = tds[2].xpath('.//text()')[0]
                    ip = ''.join(tr.xpath(xpath_str))
                    raw_port = tr.xpath(
                        './/span[contains(@class, "port")]/@class')[0].split(
                            ' ')[1]
                    port = decode_port(raw_port)
                    proxy_ip = ProxyIP(ip, port, protocol)
                    if proxy_ip.ok:
                        self.logger.info(f'got raw proxy_ip {str(proxy_ip)}')
                        db.add_raw(str(proxy_ip))
                except Exception:
                    self.logger.error(f'error occurred when crawl {url}')
                    traceback.print_exc()
            gevent.sleep(10)


#!/usr/bin/env python3

# Script Name:                  Ops 401d8 Challenge 31
# Author:                       David Prutch
# Date of latest revision:      08/21/2023
# Purpose:                      Prompt the user to type in a file name to search for.
                                # Prompt the user for a directory to search in.
                                # Search each file in the directory by name.
                                # TIP: You may need to perform different commands depending on what OS you’re executing the script on.

                                # For each positive detection, print to the screen the file name and location.
                                # At the end of the search process, print to the screen how many files were searched and how many hits were found.

# Import Libraries
# Import required modules for the script
import os
import subprocess
from time import sleep
import platform

# Get your system
# Determine the operating system the script is running on
my_os = platform.system()

# Define Functions

# Function for searching files on Linux
def linux_search():
    # Ask the user for the file name and search directory
    filename = input("Enter the file name to search for: ")
    search_directory = input("Enter the directory to search in: ")

    # Check if the specified directory exists
    if not os.path.exists(search_directory):
        print("Error: The specified directory does not exist.")
        return

    # Initialize counters for searched files and hits
    searched_files = 0
    hits = 0

    # Traverse through the directory and its subdirectories
    for root, _, files in os.walk(search_directory):
        for file in files:
            # Check if the current file matches the desired filename
            if file == filename:
                # Print the location of the found file
                print(f"Found: {filename} at {os.path.join(root, file)}")
                hits += 1
            searched_files += 1

    # Display search summary
    print("\nSearch completed.")
    print(f"Total files searched: {searched_files}")
    print(f"Total hits found: {hits}")

# Function for searching files on Windows
def windows_search():
    # Ask the user for the file name and search directory
    filename = input("Enter the file name to search for: ")
    search_directory = input("Enter the directory to search in: ")

    # Check if the specified directory exists
    if not os.path.exists(search_directory):
        print("Error: The specified directory does not exist.")
        return

    # Build the command to search for the file using Windows dir command
    cmd = f'dir /s /b {os.path.join(search_directory, filename)}'

    try:
        # Execute the command and capture the result
        result = subprocess.check_output(cmd, shell=True, text=True)
        hit_files = result.strip().split('\n')
        if hit_files:
            # Print the locations of the found files
            for file in hit_files:
                print(f"Found: {filename} at {file}")
        else:
            print(f"{filename} not found in the specified directory.")

    except subprocess.CalledProcessError:
        print("An error occurred while searching.")

# Main
# Check the operating system and call the appropriate search function
if my_os == "Linux":
    linux_search()
elif my_os == "Windows":
    windows_search()
else:
    print("Your OS is not compatible with this script.")


# -*- coding:utf-8 -*-

from __future__ import print_function, division, absolute_import

import tensorflow as tf
import os
import time

from mnist_cnn import Model

class Settings(object):
    def __init__(self):
        self.model_name = 'mnist_cnn'
        self.img_size = 28
        self.n_channel = 1
        self.n_class = 10
        self.drop_rate = 0.5
        self.learning_rate = 0.001
        self.decay_step = 2000
        self.decay_rate = 0.5
        self.training_steps = 10000   # 耗时 90s
        self.batch_size = 100

        self.summary_path = 'summary/' + self.model_name + '/'
        self.ckpt_path = 'ckpt/' + self.model_name + '/'

        if not os.path.exists(self.summary_path):
            os.makedirs(self.summary_path)
        if not os.path.exists(self.ckpt_path):
            os.makedirs(self.ckpt_path)

def main():
    """模型训练。"""
    from tensorflow.examples.tutorials.mnist import input_data

    mnist = input_data.read_data_sets("../../data/MNIST_data", one_hot=False)
    print(mnist.test.labels.shape)
    print(mnist.train.labels.shape)

    my_setting = Settings()
    with tf.variable_scope(my_setting.model_name):
        model = Model(my_setting)

    # 模型要保存的变量
    var_list = tf.trainable_variables()
    if model.global_step not in var_list:
        var_list.append(model.global_step)
    # 添加 BN 层的均值和方差
    global_vars = tf.global_variables()
    bn_moving_vars = [v for v in global_vars if 'moving_mean' in v.name]
    bn_moving_vars += [v for v in global_vars if 'moving_variance' in v.name]
    var_list += bn_moving_vars
    # 创建Saver
    saver = tf.train.Saver(var_list=var_list)

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    with tf.Session(config=config) as sess:
        print("initializing variables.")
        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())
        if os.path.exists(my_setting.ckpt_path + 'checkpoint'):
            print("restore checkpoint.")
            saver.restore(sess, tf.train.latest_checkpoint(my_setting.ckpt_path))
        tic = time.time()
        for step in range(my_setting.training_steps):
            if 0 == step % 100:
                X_batch, y_batch = mnist.train.next_batch(my_setting.batch_size, shuffle=True)
                X_batch = X_batch.reshape([-1, 28, 28, 1])
                _, g_step, train_loss, train_acc = sess.run(
                    [model.train_op, model.global_step, model.train_loss, model.train_acc],
                    feed_dict={model.X_inputs: X_batch, model.y_inputs: y_batch})
                X_batch, y_batch = mnist.test.next_batch(my_setting.batch_size, shuffle=True)
                X_batch = X_batch.reshape([-1, 28, 28, 1])
                test_loss, test_acc = sess.run([model.test_loss, model.test_acc],
                                               feed_dict={model.X_inputs: X_batch, model.y_inputs: y_batch})
                print(
                    "Global_step={:.2f}, train_loss={:.2f}, train_acc={:.2f}; test_loss={:.2f}, test_acc={:.2f}; pass {:.2f}s".format(
                        g_step, train_loss, train_acc, test_loss, test_acc, time.time() - tic
                    ))
            else:
                X_batch, y_batch = mnist.train.next_batch(my_setting.batch_size, shuffle=True)
                X_batch = X_batch.reshape([-1, 28, 28, 1])
                sess.run([model.train_op], feed_dict={model.X_inputs: X_batch, model.y_inputs: y_batch})
            if 0 == (step + 1) % 1000:
                path = saver.save(sess, os.path.join(my_setting.ckpt_path, 'model.ckpt'),
                                  global_step=sess.run(model.global_step))
                print("Save model to {} ".format(path))

if __name__ == '__main__':
    main()


from unittest import TestCase
import pandas as pd

import src.feat_engineering as feat

class FeatureEngineeringTests(TestCase):
    def test_date_diff_addition(self):
        dates = pd.to_datetime(
            ["2012-01-04", "2012-01-01", "2012-01-03"],
             format="%Y-%m-%d")

        input_dt = pd.DataFrame({
            "index": [1, 2, 3],
            "dates": dates
        })

        expected = pd.DataFrame({
            "index": [1, 2, 3],
            "dates": dates,
            "diff_from_start": [3, 0, 2]
        })

        expected["diff_from_start"] = pd.to_timedelta(expected["diff_from_start"], unit="days")
        result = feat.add_date_difference_from_start(input_dt)
        pd.testing.assert_frame_equal(result, expected)


import os
import logging
import array

from franklin.backbone.analysis import Analyzer, scrape_info_from_fname
from franklin.pipelines.pipelines import seq_pipeline_runner
from franklin.backbone.specifications import (BACKBONE_DIRECTORIES,
                                              BACKBONE_BASENAMES,
                                              PLOT_FILE_FORMAT)
from franklin.utils.seqio_utils import seqs_in_file
from franklin.seq.writers import SequenceWriter
from franklin.statistics import (write_distribution, draw_histogram,
                                 draw_boxplot, IntsStats)
from franklin.seq.seq_stats import create_nucleotide_freq_histogram

class CleanReadsAnalyzer(Analyzer):
    'It does a cleaning reads analysis'

    @staticmethod
    def _guess_cleaning_pipepile(file_info):
        'It returns the pipeline suited to clean the given file'
        if 'pl' not in file_info:
            raise RuntimeError('platform not in the file name: %s' %
                               file_info['fpath'])
        platform = file_info['pl']
        if file_info['format'] == 'fasta':
            return 'sanger_without_qual'
        elif platform == 'illumina':
            return 'solexa'
        elif platform == '454':
            return 'sanger_with_qual'
        elif platform == 'sanger':
            return 'sanger_with_qual'
        elif platform == 'solid':
            return 'solid'
        else:
            raise ValueError('Unable to guess the cleaning pipeline: %s' %
                             str(file_info))

    def _create_cleaning_configuration_solid(self, platform):
        '''It returns the pipeline configuration for solid pipeline looking at
        the project settings'''
        settings = self._project_settings['Cleaning']
        configuration = {}
        # min length settings
        min_length = settings['min_seq_length'][platform]
        configuration['remove_short'] = {}
        configuration['remove_short']['length'] = min_length
        return configuration

    def _create_cleaning_configuration_no_solid(self, platform, library=None):
        '''It returns the pipeline configuration for no solid pipeline looking
        at the project settings'''
        settings = self._project_settings['Cleaning']
        configuration = {}

        configuration['remove_vectors_blastdb'] = {}
        vec_db = settings['vector_database']
        configuration['remove_vectors_blastdb']['vectors'] = vec_db

        configuration['remove_vectors_file'] = {}
        vec_file = settings['vector_file']
        configuration['remove_vectors_file']['vectors'] = vec_file

        # adaptors settings
        adap_param = 'adaptors_file_%s' % platform
        adaptors_fpath = settings[adap_param]
        configuration['remove_adaptors'] = {}
        configuration['remove_adaptors']['adaptors'] = adaptors_fpath

        # Words settings
        word_param = 'short_adaptors_%s' % platform
        words = settings[word_param]
        configuration['remove_short_adaptors'] = {}
        configuration['remove_short_adaptors']['words'] = words

        # lucy settings.
        lucy_settings = settings['lucy']
        lucy_vector_settings = lucy_settings['vector_settings']
        if lucy_vector_settings is not None:
            lucy_settings_dir = os.path.dirname(lucy_vector_settings)
            lucy_settings_fhand = open(lucy_vector_settings)
            lucy_libraries = eval(lucy_settings_fhand.read())
            lucy_settings_fhand.close()
            if library in lucy_libraries:
                vector = os.path.join(lucy_settings_dir,
                                      lucy_libraries[library]['vector_file'])
                splice = os.path.join(lucy_settings_dir,
                                      lucy_libraries[library]['splice_file'])
                vector_settings = [vector, splice]
            else:
                vector_settings = None
        else:
            vector_settings = None

        parameters = {'bracket': lucy_settings['bracket'],
                      'window': lucy_settings['window'],
                      'error': lucy_settings['error']}
        if vector_settings:
            parameters['vector'] = vector_settings

        configuration['strip_lucy'] = {}
        configuration['strip_lucy']['parameters'] = parameters

        n_trim = settings['strip_n_percent']
        configuration['strip_trimpoly'] = {}
        configuration['strip_trimpoly']['ntrim_above_percent'] = n_trim

        #edge_remover
        left = settings['edge_removal']['%s_left' % platform]
        right = settings['edge_removal']['%s_right' % platform]
        configuration['edge_removal'] = {}
        configuration['edge_removal']['left_length'] = left
        configuration['edge_removal']['right_length'] = right

        # min length settings
        min_length = settings['min_seq_length'][platform]
        configuration['remove_short'] = {}
        configuration['remove_short']['length'] = min_length

        return configuration

    def create_cleaning_configuration(self, platform, library=None):
        'It returns the pipeline configuration looking at the project settings'
        if platform == 'solid':
            return self._create_cleaning_configuration_solid(platform)
        else:
            return self._create_cleaning_configuration_no_solid(platform,
                                                                library=library)

    def run(self):
        '''It runs the analysis. It checks if the analysis is already done per
        input file'''
        logger = logging.getLogger("franklin")
        self._log({'analysis_started': True})
        input_paths = self._get_input_fpaths()['reads']
        output_dir = self._create_output_dirs()['reads']
        for input_path in input_paths:
            input_fpath = str(input_path)
            fname = os.path.split(input_fpath)[-1]
            output_fpath = os.path.join(output_dir, fname)
            if os.path.exists(output_fpath):
                msg = '%s already cleaned. Not cleaned again' % output_fpath
                logger.info(msg)
                continue
            file_info = scrape_info_from_fname(input_path)
            input_fhand = open(input_fpath)
            output_fhand = open(output_fpath, 'w')
            pipeline = self._guess_cleaning_pipepile(file_info)
            infhands = {'in_seq': input_fhand}
            writer = SequenceWriter(output_fhand,
                                    file_format=file_info['format'])

            configuration = self.create_cleaning_configuration(
                                                      platform=file_info['pl'],
                                                      library=file_info['lb'])
            try:
                seq_pipeline_runner(pipeline, configuration, infhands,
                                    file_info['format'],
                                    processes=self.threads,
                                    writers={'seq': writer})
            except Exception as error:
                output_fhand.close()
                os.remove(output_fpath)
                raise(error)
            output_fhand.close()
            input_fhand.close()

        self._log({'analysis_finished': True})
        return

BASENAME_FOR_ALL_TOGETHER = 'all'

PLOT_LABELS = {
        'seq_length': {'title': 'Sequence length distribution',
                       'xlabel': 'Sequence length',
                       'ylabel': 'Number of sequences'},
        'seq_qual': {'title': 'Sequence quality distribution',
                     'xlabel': 'quality',
                     'ylabel': 'Number of bp'},
        'qual_boxplot': {'title': 'Qualities along the sequence',
                         'xlabel': 'seqence position',
                         'ylabel': 'quality distribution'},
        }

class ReadsStatsAnalyzer(Analyzer):
    '''It calculates stats for original and cleaned reads.
    It calculates the distribution between both type off reads'''

    def run(self):
        'It runs the analysis'
        self._log({'analysis_started': True})
        self._create_output_dirs()

        clean_paths = self._get_input_fpaths()['cleaned_reads']
        raw_paths = self._get_input_fpaths()['raw_reads']

        clean_stats_dir = os.path.join(self._get_project_path(),
                             BACKBONE_DIRECTORIES['cleaned_reads_stats'])
        raw_stats_dir = os.path.join(self._get_project_path(),
                             BACKBONE_DIRECTORIES['raw_reads_stats'])

        for path in raw_paths:
            self._do_stats_for_file(path, raw_stats_dir)

        for path in clean_paths:
            self._do_stats_for_file(path, clean_stats_dir)

        self._log({'analysis_finished': True})

    def _do_stats_for_file(self, path, stats_dir):
        'It calculates the stats for a file os seqs'
        stats_fpath = os.path.join(stats_dir,
                                       BACKBONE_BASENAMES['statistics_file'])
        stats_fhand = open(stats_fpath, 'a')

        fpath = path.last_version
        basename = path.basename
        #output_files
        freq_pos_out_fpath = os.path.join(stats_dir,
                                          basename + '.freq_position')
        length_out_fpath = os.path.join(stats_dir, basename + '.length')
        qual_out_fpath = os.path.join(stats_dir, basename + '.qual')
        qual_boxplot_out_fpath = os.path.join(stats_dir,
                                              basename + '.qual.boxplot')

        # nucleotide freq per position
        if not os.path.exists(freq_pos_out_fpath):
            plot_fpath = freq_pos_out_fpath + '.' + PLOT_FILE_FORMAT
            seqs = seqs_in_file(open(fpath))
            create_nucleotide_freq_histogram(seqs, fhand=open(plot_fpath, 'w'))

        #Extract lengths and quals
        if (not os.path.exists(length_out_fpath) or
            not  os.path.exists(qual_out_fpath)):
            lengths_, quals_ = self._get_lengths_quals_from_file(fpath)

        #the distributions for the lengths
        if not os.path.exists(length_out_fpath):
            plot_fpath = length_out_fpath + '.' + PLOT_FILE_FORMAT
            distrib_fpath = length_out_fpath + '.dat'
            distrib = lengths_.calculate_distribution()
            lengths_.draw_distribution(distrib,
                                 labels=PLOT_LABELS['seq_length'],
                                 distrib_fhand=open(distrib_fpath, 'w'),
                                 plot_fhand=open(plot_fpath, 'w'))

        #the distributions for the quals
        if not  os.path.exists(qual_out_fpath) or quals_.count != 0:
            plot_fpath = qual_out_fpath + '.' + PLOT_FILE_FORMAT
            distrib_fpath = qual_out_fpath + '.dat'

            distrib = quals_.calculate_distribution()
            quals_.draw_distribution(distrib,
                                     labels=PLOT_LABELS['seq_qual'],
                                     plot_fhand=open(plot_fpath, 'w'),
                                 distrib_fhand=open(distrib_fpath, 'w'))

        # qual boxplot
        if not os.path.exists(qual_boxplot_out_fpath):
            quals = self._get_quals_by_length_from_file(fpath)
            if (quals and quals[0]):
                plot_fpath = qual_boxplot_out_fpath + '.' + PLOT_FILE_FORMAT
                distrib_fpath = qual_boxplot_out_fpath + '.dat'
                #boxplot
                draw_boxplot(quals, fhand=open(plot_fpath, 'w'),
                             title=PLOT_LABELS['qual_boxplot']['title'],
                             xlabel=PLOT_LABELS['qual_boxplot']['xlabel'],
                             ylabel=PLOT_LABELS['qual_boxplot']['ylabel'],
                             stats_fhand=open(distrib_fpath, 'w'),
                             max_plotted_boxes=30)

        #the statistics for the statistics file
        self._write_statistics(stats_fhand, fpath, lengths_, quals_)

    @staticmethod
    def _get_quals_by_length_from_file(fpath):
        'It returns the qualities along the sequence as a list of lists'

        quals_by_position = []
        for seq in seqs_in_file(open(fpath)):
            quals = seq.qual
            if quals:
                for base_number, qual in enumerate(quals):
                    try:
                        quals_by_position[base_number]
                    except IndexError:
                        quals_by_position.append(array.array('B'))
                    quals_by_position[base_number].append(qual)
        return quals_by_position

    @staticmethod
    def _write_statistics(stats_fhand, seq_fpath, lengths, quals):
        'It writes some statistics'

        to_print = 'statistics for %s\n' % os.path.basename(seq_fpath)
        stats_fhand.write(to_print)
        if lengths.count == 0:
            stats_fhand.write('File empty, no stats for this file\n')
            return
        stats_fhand.write('-' * (len(to_print) - 1) + '\n')

        stats_fhand.write('Num sequences: %i\n' % lengths.count)

        stats_fhand.write('Total sequence length: %i\n' % lengths.sum)

        stats_fhand.write('Sequence length minimum: %i\n' % lengths.min)

        stats_fhand.write('Sequence length maximum: %i\n' % lengths.max)

        stats_fhand.write('Sequence length average: %.2f\n' % lengths.average)

        stats_fhand.write('Sequence length variance: %.2f\n' % lengths.variance)
        if quals and quals.count != 0:
            stats_fhand.write('Sequence qualities minimum: %i\n' % quals.min)

            stats_fhand.write('Sequence qualities maximum: %i\n' % quals.max)

            stats_fhand.write('Sequence qualities average: %.2f\n' % \
                                                                  quals.average)
            stats_fhand.write('Sequence qualities variance: %.2f\n' % \
                                                                 quals.variance)
        stats_fhand.write('\n')

    @staticmethod
    def _get_lengths_quals_from_file(seq_fpath):
        'Given a sequence file it returns the lengths and quals'
        lengths = IntsStats(init_len=1000)
        quals = IntsStats(init_len=100)
        for seq in seqs_in_file(open(seq_fpath)):
            lengths.append(len(seq))
            qual = seq.qual
            if qual:
                quals.extend(qual)
        return lengths, quals

DEFINITIONS = {
    'clean_reads':
        {'inputs': {
            'reads':
                {'directory': 'raw_reads',
                 'file_kinds': 'sequence_files'}
            },
         'outputs': {'reads': {'directory': 'cleaned_reads'}},
         'analyzer': CleanReadsAnalyzer,
        },
    'read_stats':
        {'inputs': {
            'raw_reads':
                {'directory': 'raw_reads',
                 'file_kinds': 'sequence_files'},
            'cleaned_reads':
                {'directory': 'cleaned_reads',
                 'file_kinds': 'sequence_files'}
                },
         'outputs': {'raw_reads': {'directory': 'raw_reads_stats'},
                    'cleaned_reads': {'directory': 'cleaned_reads_stats'}},
         'analyzer': ReadsStatsAnalyzer,
        },
     }


#!/usr/bin/python3
"""Matrix division"""

def matrix_divided(matrix, div):
    """Divides every element of a matrix
    Args:
        matrix (list): A list of lists of integers
        or floats div (int/float): The divisor.
    """

    if type(div) not in [int, float]:
        raise TypeError("div must be a number")
    if div == 0:
        raise ZeroDivisionError(
            "Each row of the matrix must have the same size")

    if not all(len(row) == len(matrix[0]) for row in matrix):
        raise TypeError("Each row of the matrix must have the same size")
    if not all(type(num) in [int, float] for row in matrix for num in row):
        raise TypeError("matrix must be a matrix (list of lists)"
                        " of integers/floats")

    new_matrix = [[eval("{:.2f}".format(num / div)) for num in row]
                  for row in matrix]
    return new_matrix


# Replace ___ with your code

# create a function with two default arguments
# print two default values
def func(n1=10, n2=100):
    print(n1)
    print(n2)

# take integer input
n = int(input())

# call the function
func(n)


import sys

busNum, arriveTime = map(int, sys.stdin.readline().split())

answer = 2147483648
for n in range(busNum):
    startTime, gap, busCount = map(int, sys.stdin.readline().split())

    while startTime < arriveTime and busCount != 1: # != 0일 경우 buscount+1만큼 반복
        startTime += gap
        busCount -= 1

    # print(startTime)
    if startTime >= arriveTime: # >= 같을 경우에는 0
        answer = min(answer, startTime-arriveTime)

if answer == 2147483648:
    print(-1)
else:
    print(answer)


import netifaces as nif

def hello():
    return {
            'mac_addresses': __mac()
            }

def __mac():
    macs = {}
    for iface in nif.interfaces():
        macs[iface] = nif.ifaddresses(iface)[nif.AF_LINK][0]['addr']

    return macs


import trailer_hybrid_a_star
import matplotlib
import math
matplotlib.use('GTK3Agg')
from matplotlib import pyplot as plt

def main():
    print("Start!!")

    # initial state
    sx = -15.0  # [m]
    sy = 8.0  # [m]
    syaw0 = math.radians(0)
    syaw1 = math.radians(0)

    # goal state
    gx = 1.0  # [m]
    gy = 3.0  # [m]
    gyaw0 = math.radians(0.0)
    gyaw1 = math.radians(0.0)

    # set obstacles
    ox = []
    oy = []

    for i in range(-25, 25):
        ox.append(i)
        oy.append(15.0)

    for i in range(-10, 10):
        ox.append(i)
        oy.append(0.0)

    for i in range(-25, -10):
        ox.append(i)
        oy.append(5.0)

    for i in range(10, 25):
        ox.append(i)
        oy.append(5.0)

    for i in range(0, 5):
        ox.append(10.0)
        oy.append(i)

    for i in range(0, 5):
        ox.append(-10.0)
        oy.append(i)

    for i in range(5, 15):
        ox.append(-25)
        oy.append(i)

    for i in range(5, 15):
        ox.append(25)
        oy.append(i)

    oox = ox[:]
    ooy = oy[:]
    # plt.plot(oox, ooy, ".k")
    # plt.axis("equal")
    # plt.show()

    # path generation
    path = trailer_hybrid_a_star.calc_hybrid_astar_path(sx, sy, syaw0, syaw1, gx, gy, gyaw0, gyaw1, ox, oy,
                                                        trailer_hybrid_a_star.XY_GRID_RESOLUTION,
                                                        trailer_hybrid_a_star.YAW_GRID_RESOLUTION)

    # ====Animation=====
    print('Displaying Animation...')
    show_animation(path, oox, ooy, sx, sy, syaw0, syaw1, gx, gy, gyaw0, gyaw1)

    print("Done!!")

def show_animation(path, oox, ooy, sx, sy, syaw0, syaw1, gx, gy, gyaw0, gyaw1):
    plt.plot(oox, ooy, ".k")
    trailer_hybrid_a_star.trailerlib.plot_trailer(sx, sy, syaw0, syaw1, 0.0)
    trailer_hybrid_a_star.trailerlib.plot_trailer(gx, gy, gyaw0, gyaw1, 0.0)
    x = path.x
    y = path.y
    yaw = path.yaw
    yaw1 = path.yaw1
    direction = path.direction

    plt.ion()
    plt.show()
    for ii in range(1, len(yaw1)-1):
        plt.cla()
        plt.plot(oox, ooy, ".k")
        plt.plot(x, y, "-r", label="Hybrid A* path")

        if ii < (len(x) - 1):
            k = (yaw[ii + 1] - yaw[ii]) / trailer_hybrid_a_star.MOTION_RESOLUTION
            if not direction[ii]:
                k *= -1
            steer = math.atan2(trailer_hybrid_a_star.WB * k, 1.0)
        else:
            steer = 0.0

        trailer_hybrid_a_star.trailerlib.plot_trailer(x[ii], y[ii], yaw[ii], yaw1[ii], steer)
        plt.grid(True)
        plt.axis("equal")
        plt.draw()
        plt.pause(.001)

main()