import sys
import argparse
import logging
import os
import subprocess
from datetime import timedelta
from typing import List
import traceback

# from common import run_command_in_shell, CCM_CLUSTER_IP_PREFIX, CCM_CLUSTER_NODES
from run import Run
from email_sender import create_report, get_driver_origin_remote, send_mail

logging.basicConfig(level=logging.INFO)

class EmptyTestResult(Exception):
    pass

def main(arguments: argparse.Namespace):
    status = 0
    results = dict()
    # TODO: move docker configure to rust-driver-matrix-test.jenkinsfile
    # Start docker configure
    # run_command_in_shell(driver_repo_path=arguments.rust_driver_git,
    #                      cmd=f"pip3 install https://github.com/scylladb/scylla-ccm/archive/master.zip")
    # run_command_in_shell(driver_repo_path=arguments.rust_driver_git,
    #                      cmd=f"ln -s /usr/local/bin/ccm /bin/ccm")
    # run_command_in_shell(driver_repo_path=arguments.rust_driver_git,
    #                      cmd=f"ccm create -i {CCM_CLUSTER_IP_PREFIX}. -n {CCM_CLUSTER_NODES} --scylla -v "
    #                          f"{arguments.scylla_version} scylla-cluster")
    # run_command_in_shell(driver_repo_path=arguments.rust_driver_git,
    #                      cmd=f"ccm start")
    # Finish docker configure

    for driver_version in arguments.versions:
        results[driver_version] = dict()
        for test in arguments.tests:
            logging.info('=== RUST DRIVER VERSION %s. TEST: %s ===', driver_version, test)
            try:
                report = Run(rust_driver_git=arguments.rust_driver_git,
                             tag=driver_version,
                             test=test,
                             scylla_version=arguments.scylla_version).call_test_func()

                if not report:
                    raise EmptyTestResult(f"No result for test '{test}' and driver version {driver_version}")

                logging.info("=== RUST DRIVER MATRIX RESULTS FOR DRIVER VERSION %s ===", driver_version)
                logging.info("\n".join(f"{key}: {value}" for key, value in report.summary.items()))
                if report.is_failed:
                    status = 1
                results[driver_version][test] = report.summary
                results[driver_version][test]['time'] = \
                    str(timedelta(seconds=results[driver_version][test]['testsuite_summary']['time']))[:-3]
            except Exception:
                logging.exception(f"{driver_version} failed")
                status = 1
                exc_type, exc_value, exc_traceback = sys.exc_info()
                results[driver_version] = dict(exception=traceback.format_exception(exc_type, exc_value, exc_traceback))

    if arguments.recipients:
        email_report = create_report(results=results)
        email_report['driver_remote'] = get_driver_origin_remote(arguments.rust_driver_git)
        email_report['status'] = "SUCCESS" if status == 0 else "FAILED"
        send_mail(arguments.recipients, email_report)

    quit(status)

def extract_n_latest_repo_tags(repo_directory: str, major_versions: List[str], latest_tags_size: int = 2) -> List[str]:
    major_versions = sorted(major_versions, key=lambda major_ver: major_ver)
    commands = [f"cd {repo_directory}", "git checkout .", ]
    # if not os.environ.get("DEV_MODE", False):
    #     commands.append("git fetch -p --all")
    commands.append("git tag --sort=-creatordate | grep v0\.")

    selected_tags = {}
    ignore_tags = set()
    result = []
    commands_in_line = "\n".join(commands)
    try:
        lines = subprocess.check_output(commands_in_line, shell=True, stderr=subprocess.STDOUT).decode().splitlines()
    except subprocess.CalledProcessError as e:
        raise RuntimeError("command '{}' return with error (code {}): {}".format(e.cmd, e.returncode, e.output))

    for repo_tag in lines:
        if "." in repo_tag:
            version = tuple(repo_tag.split(".", maxsplit=2)[:2])
            if version not in ignore_tags:
                ignore_tags.add(version)
                selected_tags.setdefault(version, []).append(repo_tag)

    for major_version in major_versions:
        if len(selected_tags[major_version]) < latest_tags_size:
            raise ValueError(f"There are no '{latest_tags_size}' different versions that start with the major version"
                             f" '{major_version}'")
        result.extend(selected_tags[major_version][:latest_tags_size])
    return result

def get_arguments() -> argparse.Namespace:
    versions = ['v0.8.2', 'v0.7.0']
    parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter)
    parser.add_argument('rust_driver_git', help='folder with git repository of rust-driver')
    parser.add_argument('--versions', default=versions,
                        help='rust-driver versions to test, default={}'.format(','.join(versions)))
    parser.add_argument('--tests', choices=['rust', 'serverless', 'tls'], nargs='*', type=str,
                        help='tests to run')
    parser.add_argument('--scylla-version', help="relocatable scylla version to use",
                        default=os.environ.get('SCYLLA_VERSION', None)),
    parser.add_argument('--version-size', help='The number of the latest versions that will test.'
                                               'The version is filtered by the major and minor values.'
                                               'For example, the user selects the 2 latest versions for version 4.'
                                               'The values to be returned are: 4.9.0-scylla-1 and 4.8.0-scylla-0',
                        type=int, default=None, nargs='?')
    parser.add_argument('--recipients', help="whom to send mail at the end of the run",  nargs='+', default=None)
    arguments = parser.parse_args()
    versions = arguments.versions
    if not isinstance(versions, list):
        versions = versions.split(',')

    arguments.versions = versions

    if arguments.version_size:
        arguments.versions = extract_n_latest_repo_tags(repo_directory=arguments.rust_driver_git,
                                                        major_versions=list({tuple(v.split('.', maxsplit=2)[:2]) for v in versions}),
                                                        latest_tags_size=arguments.version_size)

    return arguments

if __name__ == '__main__':
    main(get_arguments())


from ..common.common_imports import *
from ..common.files import *
from dataclasses import dataclass
import os
from pathlib import Path
from evdspy.EVDSlocal.messages.error_classes import BucketFromSeriesFolderCreateError
from evdspy.EVDSlocal.config.config import *
from ..initial.start_args import Args
from ..components.options_class import Options, load_options, SingletonOptions
from evdspy.EVDSlocal.initial.start_options import default_data_folder_name
from evdspy.EVDSlocal.common.folder_name_checks import check_remove_back_slash
from dataclasses import dataclass
import os
from pathlib import Path

from evdspy.EVDSlocal.components.api_params import FrequencyEnum, Frequency, AggregationEnum, Aggregations, \
    get_enum_with_value, FormulasEnum, Formulas

# ------------------------------------------------------------------------------
# /*
#               BucketFromSeriesFile
#
# ------------------------------------------------------------------------------
# */
from evdspy.EVDSlocal.components.request_or_cache import RequestOrCacheResultInfo

@dataclass
class BucketFromSeriesFile:
    folder_name: any
    subject: str
    frequency: str
    formulas: str
    aggregateType: str
    prefix: str = Default_Prefix_
    NullType: bool = False
    abs_path: any = None
    xls_name_path: str = "test"
    request_or_cache_resultInfo_: field(default_factory=RequestOrCacheResultInfo) = False

    def __post_init__(self):
        """ Check 2 => after writing avoid \foldername\foldername instead of foldername\foldername
            we previously checked while getting input from user but
            we are checking if user added any absolute path by mistake
        """

        def get_defaults(item, default_value):
            if item == "":
                return default_value
            return item

        # self.frequency = get_defaults(str(self.frequency), default_freq)
        # self.formulas = get_defaults(self.formulas, default_formulas_)
        # self.aggregateType = get_defaults(self.aggregateType, default_AggregateType_)
        from evdspy.EVDSlocal.setup_project.user_series_config import default_freq, default_formulas_, \
            default_AggregateType_

        self.frequency: str = get_defaults(str(self.frequency), default_freq)
        self.frequency = get_enum_with_value(self.frequency, FrequencyEnum, FrequencyEnum.default)

        self.formulas: str = get_defaults(self.formulas, default_formulas_)
        self.formulas: str = get_enum_with_value(self.formulas, FormulasEnum, FormulasEnum.default)

        self.aggregateType = get_defaults(self.aggregateType, default_AggregateType_)
        self.aggregateType: str = get_enum_with_value(self.aggregateType, AggregationEnum, AggregationEnum.default)

        self.folder_name = check_remove_back_slash(self.folder_name)
        self.abs_path = self.get_folder_name(self.folder_name)
        self.xls_name_path = str(Path(self.abs_path) / (self.prefix + '_' + self.subject))

    def __str__(self):
        s = f"""
frequency : {self.frequency}
formulas : {self.formulas}
aggregateType : {self.aggregateType}
folder_name : {self.folder_name}
abs_path : {self.abs_path}
xls_name_path : {self.xls_name_path}

"""
        return s

    def get_folder_name(self, folder_name: str):
        def adapt_folder_name(relative_folder, reduce=False):
            if reduce:
                relative_folder = Path(folder_name).stem
            abs_modified = Path() / default_data_folder_name / relative_folder
            return abs_modified

        if folder_name is None:
            return ""
        if Avoid_Absolute_Paths_ and SingletonOptions().options_.avoid_absolute_paths_user:
            """ Namespace Protection is on """
            r"""
            This is extra protection to prevent mismatching of unwanted paths and populating some files replacing
            old files, if the user is sure absolute paths are correct they can modify
                Avoid_Absolute_Paths_ variable in start_options.py file
                avoids : C:Users\Userx\XdataFolderName
                changes paths to correct form : ...WorkingDirectory..\SeriesData\XdataFolderName
            """
            reduce = False
            if Path(folder_name).is_absolute():
                reduce = True
            return adapt_folder_name(folder_name, reduce=reduce)
        """ Namespace Protection turned off by user """
        print("Avoid_Absolute_Paths_ was cancelled")

        if self.create_folder():
            """ now we will return absolute path if we can successfully create the folder"""
            return self.abs_path
        return adapt_folder_name(self.abs_path, reduce=True)

    def create_folder(self):
        if is_dir(self.abs_path):
            return True
        try:
            os.makedirs(self.abs_path)
            return True
        except:
            if not config.current_mode_is_test:
                print(f"could not create folder {self.abs_path} ")
                # raise BucketFromSeriesFolderCreateError
            return False

from evdspy.EVDSlocal.setup_project.user_series_config import default_freq, default_formulas_, \
    default_AggregateType_

null_BucketFromSeriesFile = BucketFromSeriesFile(folder_name=None,
                                                 subject="test",
                                                 frequency=default_freq,
                                                 formulas=default_formulas_,
                                                 aggregateType=default_AggregateType_,
                                                 prefix="EV_",
                                                 NullType=True)


from flask import Flask, jsonify

app = Flask(__name__)

provinces = [
    {
        'province_id': "1",
        'province': "BALE",
        "chefLieu": "BOROMO",
        "departements": "Boromo, Bagassi, Fara, Pa, Pompoï, Poura, Siby, Oury, Yaho",
        "population": "169 543"
    },
    {
        "province_id": "2",
        "province": "BAM",
        "chefLieu": "Kongoussi",
        "departements": "Kongoussi, Bourzanga, Guibaré, Nasséré, Tikaré, Sabcé, Rollo, Rouko, Zitenga",
        "population": "212 295"
    },
    {
        "province_id": "3",
        "province": "BANWA",
        "chefLieu": "Solenzo",
        "departements": "Solenzo, Balavé, Kouka, Tansila, Sami, Sanaba",
        "population": "214 234"
    },
    {
        "province_id": "4",
        "province": "BAZEGA",
        "chefLieu": "Kombissiri",
        "departements": "Kombissiri, Doulougou, Ipelcé, Gaongo, Kayao,Toécé, Saponé.",
        "population": "214 450"
    },
    {
        "province_id": "5",
        "province": "BOUGOURIBA",
        "chefLieu": "Diébougou",
        "departements": "Diébougou, Dolo, Tiankoura, Bonddigui, Nioroniorro, Oronkua",
        "population": "76 444"
    },
    {
        "province_id": "6",
        "province": "BOULGOU",
        "chefLieu": "Tenkodogo",
        "departements": "Tenkodogo, Bané, Bagré, Béguédo, Bittou, Boussouma, Bissiga, Garango, Komtoéga, Niagho, "
                        "Zabré, Zoaga, Zonsé",
        "population": "415 414"
    },
    {
        "province_id": "7",
        "province": "BOUKIEMDE",
        "chefLieu": "Koudougou",
        "departements": "Koudougou, Bingo, Imasgo, Kindi, Kokologo, Nanoro, Niandiala, Pella, Poa, Ramongo, Sabou, "
                        "Siglé, Sourgou, Thiou, Soaw",
        "population": "421 083"
    },
    {
        "province_id": "8",
        "province": " COMOE",
        "chefLieu": "Banfora",
        "departements": "Banfora, Bérégadougou, Mangodara, Moussodougou, Niangoloko, Ouo, Sidéradougou, "
                        "Soubakaniédougou, Tiéfora",
        "population": "240 942"
    },
    {
        "province_id": "9",
        "province": "GANZOURGOU",
        "chefLieu": "Zorgho",
        "departements": "Zorgho, Boudry, Kogo, Méguet, Mogtédo, Salogo, Zam, Zoungou",
        "population": "257 707"
    },
    {
        "province_id": "10",
        "province": "GNAGNA",
        "chefLieu": "Bogandé",
        "departements": "Bogandé, Coalla, Bilanga, Liptougou, Mani, Pièla, Thion",
        "population": "307 386"
    }
]

@app.route('/')
def index():
    return 'Ma première application REST avec Flask!'

# fonction pour lister toutes les provinces

@app.route('/provinces', methods=['GET'])
def get_province():
    return jsonify({'Provinces': provinces})

# fonction pour lister une province avec son id
@app.route("/provinces/<int:province_id>", methods=['GET'])
def get_province_id(province_id):
    return jsonify({'province': provinces[province_id]})

# Fonction de création d'une province
@app.route("/provinces", methods=['POST'])
def create_province():
    province = {
        "province_id": "11",
        "province": "GOURMA",
        "chefLieu": "Fad N’Gourma",
        "departements": "Fada N’Gourma, Diabo, Diapangou, Matiacoali, Tibga, Yamba",
        "population": "221 956"
    }
    provinces.append(province)
    return jsonify({'Crée': province})

# Fonction de mise à jour d'une province
@app.route("/provinces/<int:province_id>", methods=['PUT'])
def update_province(province_id):
    provinces[province_id]['province'] = "Test"
    return jsonify({'province': provinces[province_id]})

# Fonction de suppression d'une province
@app.route("/provinces/<int:province_id>", methods=['DELETE'])
def delete_province(province_id):
    provinces.remove(provinces[province_id])
    return jsonify({'result': True})

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


import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
RUNNING = True
red = 13
green = 19
blue = 26
GPIO.setup(red, GPIO.OUT)
GPIO.setup(green, GPIO.OUT)
GPIO.setup(blue, GPIO.OUT)
Freq = 100
RED = GPIO.PWM(red, Freq)
GREEN = GPIO.PWM(green, Freq)
BLUE = GPIO.PWM(blue, Freq)

try:
    RED.start(1)
    GREEN.start(1)
    BLUE.start(1)
    while RUNNING:
        for x in range(1, 101, 5):
            BLUE.ChangeDutyCycle(101 - x)
            GREEN.ChangeDutyCycle(x)
            time.sleep(0.05)
        for x in range(1, 101, 5):
            GREEN.ChangeDutyCycle(101 - x)
            RED.ChangeDutyCycle(x)
            time.sleep(0.05)
        for x in range(1, 101, 5):
            RED.ChangeDutyCycle(101 - x)
            BLUE.ChangeDutyCycle(x)
            time.sleep(0.05)
except KeyboardInterrupt:
    RUNNING = False
    GPIO.cleanup()


#  File: BabyNames.py

#  Description: This program reads a text file and converrts into a dictionary which a user can easily access

#  Student Name: Ernesto Canales

#  Student UT EID: egc536

#  Course Name: CS 313E

#  Unique Number:

#  Date Created: 03/10/2015

#  Date Last Modified: 03/14/2015

import sys
file = open('names.txt','r')
names_dict = {}

#go through each line and start new key as a string and empty list for ranks
for line in file:
  line.strip('\n')
  name = ''
  ranks = []
  number = ''
  # append to name if str is a letter and to ranks if a number
  for i in line:

    if (ord(i[0]) >= 65) and (ord(i[0]) <= 122):
      name += i
    elif i == '\n':
      number = int(number)
      ranks.append(number)
      number = ''
    elif (i == ' '):
      if number != '':
        number = int(number)
        ranks.append(number)
        number = ''
    elif int(i) >= 0:
      number += i

  names_dict[name] = ranks

file.close()

class names(object):
  # Initializer
  def __init__(self):
    self.names_dict = names_dict

  # Prints options to user
  def print_menu(self):
    print ('The menu choices are: ' "\n\n" '1. to search for names.' "\n"'2. to display data for one name.'"\n" '3. to display all names that appear in one decade.'"\n" '4. to display all names that appear in all decades.'"\n" '5. to display all names that are more popular in every decade.'"\n" '6. to display all names that are less popular in every decade.'"\n" '7. to quit')

  # Asks user for an order
  def ask(self):
    self.choice = eval(input('Enter choice: '))

    self.direct()
  # Runs whichever function the user wants
  def direct(self):
    if self.choice == 1:
      self.one()
    if self.choice == 2:
      self.two()
    if self.choice == 3:
      self.three()
    if self.choice == 4:
      self.four()
    if self.choice == 5:
      self.five()
    if self.choice == 6:
      self.six()
    if self.choice == 7:
      self.seven()

  #Option 1 in menu
  def one(self):
    if self.choice == 1:
      specific = str(input('Enter a name: '))

      if specific not in names_dict.keys():
        print(specific + ' does not appear in any decade.')
        self.ask()
      #Make sure the minimum is not 0,if it is replace w/ 1001 and find min again
      if self.names_dict[specific]:
        ranks = self.names_dict[specific]
        if min(ranks) != 0:
          small = min(ranks)
          decade = 1900 + (10 * ranks.index(small))

        else:
          for i in range(len(ranks)):
            if ranks[i] == 0:
              ranks[i] = 1001
          small = ranks.index(min(ranks))

          decade = 1900 + (10 * small)
      else:
        print(specific, 'does not appear in any decade.')
      print('The matches with their highest ranking decade are: ' + "\n" + str(specific) + ' ' + str(decade))
      self.ask()

  #Option 2 in menu
  def two(self):
    if self.choice == 2:
      specific = str(input('Enter a name: '))
      if specific not in names_dict.keys():
        print(specific + ' does not appear in any decade.')
        self.ask()

      if self.names_dict[specific]:
        ranks = self.names_dict[specific]

      print (specific , ":" , end='')
      for i in ranks:
        print(i, ' ', end='')
      print()
      for i in range (len(ranks)):
        print(str(1900 + (i * 10)) + ": " + str(ranks[i]))
      self.ask()

  #Option 3 in menu
  def three(self):
    if self.choice == 3:
      decade = eval(input('Enter decade: '))
      1
      indx = (decade - 1900) // 10
      names_list = []
      for key in self.names_dict:
        if (self.names_dict[key].count(0) == 10) and (self.names_dict[key][indx] > 0):
          names_list.append(key)
      names_list.sort()
      print("The names are in alphabetical:")
      for i in names_list:
        print(i)
      self.ask()

  #Option 4 in menu
  def four(self):
    if self.choice == 4:

      popular_names = []
      for key in names_dict:
        if self.names_dict[key].count(0) == 0 and (self.names_dict[key].count(1001) == 0):
          popular_names.append(key)
      popular_names.sort()
      print(str(len(popular_names)) , "names appear in every decade. The names are:")
      for i in popular_names:
        print(i)
      self.ask()

  #Option 5 in menu
  def five(self):

    if self.choice == 5:
      rising = []
      for key in self.names_dict:
        x = 0
        for i in range (10):
          if self.names_dict[key][i] > self.names_dict[key][i + 1]:
            x += 1
        if x == 10:
          rising.append(key)
      rising.sort()
      print(str(len(rising)) + " names are more popular in every decade.")
      for i in rising:
        print(i)
      self.ask()

  # Option 6 in menu
  def six(self):
    if self.choice == 6:
      descending = []
      for key in self.names_dict:
        x = 0
        for i in range (10):
          if self.names_dict[key][i] < self.names_dict[key][i + 1]:
            x += 1
        if x == 10:
          descending.append(key)
        if (x == 9) and (self.names_dict[key][10] == 0):
          descending.append(key)
      descending.sort()
      print(str(len(descending)) + " names are less popular in every decade.")
      for i in descending:
        print(i)

      self.ask()

  # Option 7 in menu
  def seven(self):
    if self.choice == 7:
      print('Goodbye.')
      sys.exit()

def main():
  r = names()

  r.print_menu()
  r.ask()

main()


# Counting Strongly Connected Components (SCCs) of a directed graph recursively
# prints lengths of 5 biggest SCCs or 0s if there are less than 5

size = 875714
g = [[] for _ in range(size)]
with open('SCC.txt', 'r') as fh:
    for line in fh:
        h = list(map(int, line.split()))
        g[h[0]-1].append(h[1])
rev = [[] for _ in range(size)]
with open('SCC.txt', 'r') as fh:
    for line in fh:
        k = list(map(int, line.split()))
        rev[k[1]-1].append(k[0])
t = 0
s = 1
exp1 = [0 for _ in range(size)]
exp2 = [0 for _ in range(size)]
tlist = [[] for _ in range(size)]
ti = [0 for _ in range(size)]
leader = [0 for _ in range(size)]

print(g[0])

def SCCs(g):
     DFSloop1()
     TL(g, ti)
     DFSloop2()

def DFSloop1():
    global rev, t, exp1
    for v in range(size, 0, -1):
        if not exp1[v-1]:
            DFS1(v)

def DFS1(v):
    global rev, t, exp1, ti
    exp1[v-1] = 1
    for j in rev[v-1]:
        if not exp1[j-1]:
            DFS1(j)
    t += 1
    ti[v-1] = t

def DFSloop2():
    global s, exp2
    for ve in range(size, 0, -1):
        if not exp2[ve-1]:
            s = ve
            DFS2(ve)

def DFS2(ve):
    global tlist, s, exp2, leader
    exp2[ve-1] = 1
    leader[ve-1] = s
    for j in tlist[ve-1]:
        if not exp2[j-1]:
            DFS2(j)

def TL(g, ti):
    global tlist
    for n in range(size):
        for node in range(len(g[n])):
            tlist[ti[n]-1].append(ti[g[n][node]-1])

SCCs(g)

d = [0]*size
for m in leader:
    d[m-1] += 1
for _ in range(5):
    if max(d) > 0:
        print(max(d))
        d.remove(max(d))
    else:
        print(0)

Course 2/week 1/SCCs of a directed graph.py


from django.db import models

# Create your models here.
from django.contrib.auth.models import AbstractUser
from django.db import models
from django.forms import ModelForm
from tinymce.models import HTMLField

class Headquarter(models.Model):
    """
    Model for School grads
    """

    # Fields
    name = models.CharField(
        max_length=30,
        verbose_name="نام ستاد",
    )

    # Metadata
    class Meta:
        verbose_name = 'نام ستاد'
        verbose_name_plural = 'نام ستاد ها'

    # Methods
    def __str__(self):
        return self.name

# Major Section
class Queue(models.Model):
    """
    Model for School major
    """

    # Fields
    name = models.CharField(
        max_length=30,
        verbose_name="نام صف",
    )

    # Metadata
    class Meta:
        verbose_name = 'صف'
        verbose_name_plural = 'صف ها'

    # Methods
    def __str__(self):
        return self.name

class Manager(models.Model):
    """
    Model for administrator classes
    """

    Manager_STATUS = (
        ('فعال', 'فعال'),
        ('غیر فعال', 'غیر فعال'),
    )

    # Fields
    persian_name = models.CharField(
        max_length=35,
        verbose_name="نام مدیریت",
    )
    code = models.CharField(
        max_length=10,
        null=False,
        blank=False,
        verbose_name="کد مدیریت",
    )
    english_name = models.CharField(
        max_length=35,
        verbose_name="نام انگلیسی مدیریت",
    )

    order = models.IntegerField(
        max_length=35,
        verbose_name="ترتیب",
    )

    manager_status = models.CharField(
        choices=Manager_STATUS,
        max_length=15,
        null=True,
        blank=True,
        verbose_name="وضعیت مدیر",
    )

    queue = models.ForeignKey(
        Queue,
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name="queue",
        verbose_name="صف",
    )
    headquarter = models.ForeignKey(
        Headquarter,
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name="headquarter",
        verbose_name="ستاد",
    )

    # Metadata
    class Meta:
        verbose_name = 'مدیریت'
        verbose_name_plural = 'مدیران'

    # Methods
    def __str__(self):
        return self.persian_name

class Company(models.Model):
    """
    Model for company classes
    """

    Company_STATUS = (
        ('فعال', 'فعال'),
        ('غیر فعال', 'غیر فعال'),
    )

    # Fields
    title = models.CharField(
        max_length=35,
        verbose_name="نام شرکت",
    )
    code = models.CharField(
        max_length=10,
        null=False,
        blank=False,
        verbose_name="کد شرکت",
    )
    english_title = models.CharField(
        max_length=35,
        verbose_name="نام انگلیسی شرکت",
    )

    company_status = models.CharField(
        choices=Company_STATUS,
        max_length=15,
        null=True,
        blank=True,
        verbose_name="وضعیت شرکت",
    )

    content = HTMLField(blank=True, null=True)

    # Metadata
    class Meta:
        verbose_name = 'شرکت'
        verbose_name_plural = 'شرکت ها'

    # Methods
    def __str__(self):
        return self.title

class Department(models.Model):
    """
    Model for department classes
    """

    Department_STATUS = (
        ('فعال', 'فعال'),
        ('غیر فعال', 'غیر فعال'),
    )

    Department_TYPE = (
        ('صف', 'صف'),
        ('ستاد', 'ستاد'),
    )

    # Fields
    title = models.CharField(
        max_length=35,
        verbose_name="نام معاونت",
    )
    code = models.CharField(
        max_length=10,
        null=False,
        blank=False,
        verbose_name="کد معاونت",
    )
    english_title = models.CharField(
        max_length=35,
        verbose_name="نام انگلیسی معاونت",
    )

    department_status = models.CharField(
        choices=Department_STATUS,
        max_length=15,
        null=True,
        blank=True,
        verbose_name="وضعیت معاونت",
    )

    department_type = models.CharField(
        choices=Department_TYPE,
        max_length=15,
        null=True,
        blank=True,
        verbose_name="نوع معاونت",
    )

    order = models.IntegerField(
        max_length=35,
        verbose_name="ترتیب معاونت",
    )

    company = models.ForeignKey(Company, on_delete=models.CASCADE,blank=True,null=True)

    # Metadata
    class Meta:
        verbose_name = 'معاونت'
        verbose_name_plural = 'معاونت ها'

    # Methods
    def __str__(self):
        return self.title

class OU(models.Model):
    """
    Model for OU classes
    """

    OU_STATUS = (
        ('فعال', 'فعال'),
        ('غیر فعال', 'غیر فعال'),
    )

    # Fields
    title = models.CharField(
        max_length=35,
        verbose_name="نام واحد سازمانی",
    )
    code = models.CharField(
        max_length=10,
        null=False,
        blank=False,
        verbose_name="کد واحد سازمانی",
    )
    english_title = models.CharField(
        max_length=35,
        verbose_name="نام انگلیسی واحد سازمانی",
    )

    ou_status = models.CharField(
        choices=OU_STATUS,
        max_length=15,
        null=True,
        blank=True,
        verbose_name="وضعیت واحد سازمانی",
    )

    order = models.IntegerField(
        max_length=35,
        verbose_name="ترتیب واخد سازمانی",
    )

    department = models.ForeignKey(Department, on_delete=models.CASCADE, blank=True, null=True)

    # Metadata
    class Meta:
        verbose_name = 'واحد سازمانی'
        verbose_name_plural = 'واحد های سازمانی'

    # Methods
    def __str__(self):
        return self.title

class Level(models.Model):
    """
    Model for level classes
    """

    Position_STATUS = (
        ('فعال', 'فعال'),
        ('غیر فعال', 'غیر فعال'),
    )

    # Fields
    title = models.CharField(
        max_length=35,
        verbose_name="نام دسته سمت",
    )
    code = models.CharField(
        max_length=10,
        null=False,
        blank=False,
        verbose_name="کد دسته سمت",
    )

    english_title = models.CharField(
        max_length=35,
        verbose_name="نام انگلیسی دسته سمت",
    )

    position_status = models.CharField(
        choices=Position_STATUS,
        max_length=15,
        null=True,
        blank=True,
        verbose_name="وضعیت دسته سمت",
    )

    grade = models.IntegerField(
        max_length=35,
        verbose_name="ترتیب دسته سمت",
    )

    # Metadata
    class Meta:
        verbose_name = 'دسته سمت'
        verbose_name_plural = 'دسته های سمت'

    # Methods
    def __str__(self):
        return self.title

class Position(models.Model):
    """
    Model for position classes
    """

    Position_STATUS = (
        ('فعال', 'فعال'),
        ('غیر فعال', 'غیر فعال'),
    )

    Position_TYPE = (
        ('ساختاری', 'ساختاری'),
        ('غیر ساختاری', 'غیر ساختاری'),
    )

    # Fields
    title = models.CharField(
        max_length=35,
        verbose_name="نام سمت",
    )
    code = models.CharField(
        max_length=10,
        null=False,
        blank=False,
        verbose_name="کد سمت",
    )

    serial = models.CharField(
        max_length=10,
        null=False,
        blank=False,
        verbose_name="سریال سمت",
    )

    english_title = models.CharField(
        max_length=35,
        verbose_name="نام انگلیسی سمت",
    )

    position_status = models.CharField(
        choices=Position_STATUS,
        max_length=15,
        null=True,
        blank=True,
        verbose_name="وضعیت سمت",
    )

    position_type = models.CharField(
        choices=Position_TYPE,
        max_length=15,
        null=True,
        blank=True,
        verbose_name="نوع سمت",
    )

    order = models.IntegerField(
        max_length=35,
        verbose_name="ترتیب سمت",
    )

    ou = models.ForeignKey(OU, on_delete=models.CASCADE, blank=True, null=True)
    report_to = models.ForeignKey('self', on_delete=models.CASCADE, blank=True, null=True)
    deputy = models.ForeignKey(Department, on_delete=models.CASCADE, blank=True, null=True)
    level = models.ForeignKey(Level, on_delete=models.CASCADE, blank=True, null=True)

    # Metadata
    class Meta:
        verbose_name = 'سمت'
        verbose_name_plural = 'سمت ها'

    # Methods
    def __str__(self):
        return self.title


import sys
import json
import base64
import os
import math
from shutil import copyfile

if len(sys.argv) > 2:
    input_path = sys.argv[1]
    out_path = sys.argv[2]
    try:
        jsonFile = open(input_path,'r')
        values = json.load(jsonFile)
        assets= values["assets"]

        #create train and test dir in outpath
        train_dir = out_path + "/train"
        test_dir = out_path + "/test"

        #get training index
        training_index = math.ceil(75 * ( len(assets) /100 ))
        print("Training IndeX: ",training_index , " - ",len(assets))
        try:
            if not os.path.exists(out_path):
                os.mkdir(out_path)
            os.mkdir(train_dir)
            os.mkdir(test_dir)
        except OSError:
            print ("Creation of the directory %s failed" % out_path)

        for key,value in enumerate(assets):
            data = {}
            info = assets[value]["asset"]
            regions = assets[value]["regions"]
            imagePath = info["path"].replace("file:","")

            if len(regions) > 0:

                #Create each image jsonfile

                data["version"] = "4.5.6"
                data["flags"] = {}
                data["shapes"] = []
                data["imagePath"] = info["name"]
                data["imageHeight"] = info["size"]["width"]
                data["imageWidth"] = info["size"]["height"]

                #set Region
                for region in regions:
                    region_item = {}
                    region_item["points"] = []
                    region_item["label"] =   region["tags"][0] if len(region["tags"]) > 0 else ""
                    region_item["group_id"] = None
                    region_item["shape_type"] = "polygon"
                    region_item["flags"] = {}

                    for point in region["points"]:
                        item = [point["x"],point["y"]]
                        region_item["points"].append(item)

                    data["shapes"].append(region_item)
                #image Data
                with open(imagePath, "rb") as image_file:
                    encoded_string = base64.b64encode(image_file.read())
                    imageData = encoded_string.decode("utf-8")
                    data["imageData"] = imageData

                #write json file
                fileName = ""
                if key < training_index:
                    if os.path.exists(train_dir):
                        fileName = train_dir + "/" + info["name"].replace(".jpg",".json")
                        copyImageFileName = train_dir + "/" + info["name"]
                        copyfile(imagePath,copyImageFileName)
                else:
                    if os.path.exists(test_dir):
                        fileName = test_dir + "/" + info["name"].replace(".jpg",".json")
                        copyImageFileName = test_dir + "/" + info["name"]
                        copyfile(imagePath,copyImageFileName)

                # Serializing json
                json_object = json.dumps(data, indent = 4)
                with open(fileName, "w") as outfile:
                    outfile.write(json_object)
                print("Write To ",fileName)

        jsonFile.close()
    except IOError:
        print("Error occured when opeing file or json parse error.")

else:
    print("Set Input and Output Path.")


import sys, heapq
import numpy as np
import pymysql as mdb
import json
from graph_to_json import graphToDict

NULL_HASH = 64*'0'

class PriorityDict():
	def __init__(self):
		self.heap = [] # for keys of self.elements
		self.elements = {}
	def add(self,element):
		priority = element.blockNumber
		key = (element.transactionHash,element.outputIndex)
		if key not in self.elements:
			heapq.heappush(self.heap,(priority,key))
			self.elements[key] = element
	def pop(self):
		priority,key = heapq.heappop(self.heap)
		return self.elements[key]
	def __len__(self):
		return len(self.heap)
	def __getitem__(self,output):
		return self.elements[(output.transactionHash,output.outputIndex)]

class Output:
	def __init__(self,parameters,taint=0):
		self.amount = 0.0
		self.taint = 0.0
		self.blockNumber = parameters[0]
		self.outputIndex = parameters[1]
		self.spendHash = parameters[2]
		self.transactionHash = parameters[3]
		self.value = parameters[4]
	def __hash__(self):
		return hash((self.outputIndex,self.transactionHash))
	def __eq__(self,other):
		return hash(self) == hash(other)

con = mdb.connect('localhost','root','','bitcoin')
cur = con.cursor()

def getTxInputValue(transaction_hash):
	command = 'SELECT SUM(value) FROM outputs WHERE spendHash = "%s"'
	command = command % (transaction_hash)
	cur.execute(command)
	return cur.fetchone()[0]

def getTxOutputValue(transaction_hash):
	command = 'SELECT SUM(value) FROM outputs WHERE transactionHash = "%s"'
	command = command % (transaction_hash)
	cur.execute(command)
	return cur.fetchone()[0]

def getSourceOutputs(source_address):
	command = 'SELECT blockNumber, outputIndex, spendHash, transactionHash, value '
	command += 'FROM outputs WHERE address = "%s"' %source_address
	cur.execute(command)
	sources = PriorityDict()
	for entry in cur.fetchall():
		output = Output(entry)
		output.amount = output.value
		output.taint = 1.0
		sources.add(output)
	return sources

def getLinkedOutputs(output):
	command = 'SELECT blockNumber,outputIndex,spendHash,transactionHash,value '
	command += 'FROM outputs WHERE transactionHash = "%s"'
	command = command % output.spendHash
	cur.execute(command)
	return [Output(entry) for entry in cur.fetchall()]

#try:
address = sys.argv[1]
#except IndexError:
#	address = '1eHhgW6vquBYhwMPhQ668HPjxTtpvZGPC'#'17SkEw2md5avVNyYgj6RiXuQKNwkXaxFyQ'
	#address = '17SkEw2md5avVNyYgj6RiXuQKNwkXaxFyQ'

tainteds = getSourceOutputs(address)

total_amount = 0
for tainted in tainteds.elements.values():
	tainteds[tainted].amount = tainted.value
	total_amount += tainted.value
unspent_outputs = []

graph = []

while len(tainteds) > 0:
	output = tainteds.pop()
	if output.amount < total_amount / 100 or output.amount / output.value < .1:
		continue
	elif output.spendHash == NULL_HASH:
		unspent_outputs.append(output)
	else:
		entry = [output.transactionHash]
		entry.append(output.spendHash)
		entry.append(output.amount)
		entry.append(output.amount/output.value)
		graph.append(entry)
		txOutputValue = float(getTxOutputValue(output.spendHash))
		for linked_output in getLinkedOutputs(output):
			tainteds.add(linked_output)
		for linked_output in getLinkedOutputs(output):
			weight = linked_output.value / txOutputValue
			increment = weight * tainteds[output].amount
			tainteds[linked_output].amount += increment

json_output = open('/Users/mason/Desktop/app/static/data/output.json','w')
json.dump(graphToDict(graph),json_output)


import turtle as t
import random

t.up()
t.goto(-140,140)
t.speed(0)
t.hideturtle()

#경기장 만들기
for step in range(15):
    t.write(step, align = "center")
    t.right(90)
    t.forward(10)
    t.down()
    t.forward(150)
    t.up()
    t.backward(160)
    t.left(90)
    t.forward(20)

#거북이 선수 만들기
t_list = []
t_name = []
t_num = int(input("거북이 몇 마리?(최대5) :"))
for x in range(t_num):  #거북이 생성
    t_list.append(t.Turtle())

#거북이 속성 설정
for x in range(t_num):
    t_name.append(input(str(x+1)+"번 거북이 이름은? :"))
    color = input(str(x+1)+"번 거북이 색깔은? :")
    t_list[x].color(color)
    t_list[x].shape("turtle")
    t_list[x].up()
    t_list[x].goto(-160, 100-(x*20))
    t_list[x].write(t_name[x], align = "center")
    t_list[x].down()

#경주 시작
input("경주를 시작하려면 엔터를 누르세요!")

for turn in range(100):
    for x in range(t_num):
        t_list[x].forward(random.randint(1,5))

#1등 거북이 구하기
dist = []
for x in range(t_num):
    dist.append(t_list[x].xcor())
first = max(dist)
result = dist.index(first)
print(t_name[result],"승리!!")

메이킷기초파이썬/8주차/거북이랜덤경주.py


from datetime import datetime

from meetings import db

"""
For sake of implementation these are persistency-centered models: they contain all the information but have no method
and perform no "action". All the behavior is coded inside so.called "routes", that act de-facto like controllers.
"""

class Person(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(150), unique=True, nullable=False)
    available = db.Column(db.Boolean, nullable=False, default=lambda: True)

    def to_dict(self):
        return {
            'id': self.id,
            'username': self.username,
            'available': self.available
        }

invitation = db.Table('invitation',
                     db.Column('person_id', db.Integer, db.ForeignKey('person.id'), primary_key=True),
                     db.Column('meeting_id', db.Integer, db.ForeignKey('meeting.id'), primary_key=True)
                     )

class Meeting(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    creation_ts = db.Column(db.DateTime, nullable=False, default=datetime.now)
    start_ts = db.Column(db.DateTime, nullable=True)
    stop_ts = db.Column(db.DateTime, nullable=True)
    presenter_id = db.Column(db.Integer, db.ForeignKey('person.id'), nullable=True)
    status = db.Column(db.String, nullable=False, default='created')

    # relations are handy to retrieve the whole related object/set of related object without querying directly for them
    presenter = db.relationship('Person', primaryjoin='Meeting.presenter_id == Person.id')
    participants = db.relationship('Person', secondary=invitation, lazy='subquery',
                                   backref=db.backref('meetings', lazy=True))

class Event(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    ts = db.Column(db.DateTime, nullable=False, default=datetime.now)
    event_code = db.Column(db.Integer, nullable=False)
    person_id = db.Column(db.Integer, db.ForeignKey('person.id'))
    meeting_id = db.Column(db.Integer, db.ForeignKey('meeting.id'))

    person = db.relationship('Person', primaryjoin='Event.person_id == Person.id')
    meeting = db.relationship('Meeting', primaryjoin='Event.meeting_id == Meeting.id')

    """  for simplicity and extendability no enum but int - there should be a static table with definitions - but for a
         demo hardcoded values are enough.
         0 - meeting created
         1 - meeting start
         2 - meeting finish
         3 - person added
         4 - person present
         5 - person absent
         6 - person invited
         7 - person excluded
         """

def seed(n_users=10):
    """
    Convenience method generating users. Max 49 unique names are available.
    :return:
    """
    from random import randint, random

    if n_users > 49 or n_users <= 0:
        raise ValueError("The number of users to seed should be greater than 0 and lower than 50")

    names = ['Jane', 'Jack', 'John', 'Jill', 'Jade', 'Jim', 'Jes']
    surnames = ['Doe', 'Dam', 'Der', 'Dew', 'Dat', 'Dow', 'Dyl']
    ppl = 0
    while ppl < n_users:
        name = names[randint(0, 6)] + ' ' + surnames[randint(0, 6)]
        if Person.query.filter_by(username=name).first():
            continue
        new_user = Person(username=name, available=(random() < 0.7))
        db.session.add(new_user)
        db.session.commit()
        ppl += 1


import socket
import json
import time
import board
HOST = '127.0.0.1'  # The server's hostname or IP address
PORT = 60000  # The port used by the server
FORMAT = 'utf-8'
ADDR = (HOST, PORT)  # Creating a tuple of IP+PORT
PLAYER = 'B'
SERVER = 'Y'
INPUT_REQUEST = "please input your move"
SERVER_MOVE = "server move is"
GAME_WON = 'GAME WON'
MATCH_WON = 'MATCH_WON'
GAME_START = 'GAME STARTED'
#

def start_client():
    client_socket.connect((HOST, PORT))  # Connecting to server's socket
    msg = client_socket.recv(1024).decode(FORMAT)  # "game is staring"
    print(msg)
    msg = client_socket.recv(1024).decode(FORMAT)  # "please input number of games"
    print(msg)
    Nog = input()
    client_socket.send(Nog.encode(FORMAT))         # sending number of games to the server
    while True:
        msg = client_socket.recv(1024).decode(FORMAT)  # receiving msg from server
        if msg == GAME_START:
            msg = client_socket.recv(1024).decode(FORMAT)
            print(msg)                                 # "game number {i} is staring"
            cur = board.Board()
        if msg == INPUT_REQUEST:
            print(msg)                                 # "please enter your move"
            cur.printBoard()                                 # "printing game board"
            move = input(msg)
            cur.insert(move, PLAYER)
            cur.printBoard()
            client_socket.send(move.encode(FORMAT))    # sending move to server
            print("sent to server")
        if msg == SERVER_MOVE:
            print(msg)
            move = client_socket.recv(1024).decode(FORMAT)
            print(move)
            cur.insert(move, SERVER)
            cur.printBoard()
        if msg == GAME_WON:
            msg = client_socket.recv(1024).decode(FORMAT)
            print(msg)                                 # "the winner of the game is ..."
        if msg == MATCH_WON:
            print(msg)
            break
    msg = client_socket.recv(1024).decode(FORMAT)
    print(msg)                                         # "the winner of the match is ..."
    msg = client_socket.recv(1024).decode(FORMAT)
    print(msg)                                         # "thanks for playing"
    client_socket.close()  # Closing client's connection with server (<=> closing socket)
    print("\n[CLOSING CONNECTION] client closed socket!")

if __name__ == "__main__":
    IP = socket.gethostbyname(socket.gethostname())
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    print("[CLIENT] Started running")
    start_client()
    print("\nGoodbye client:)")


# import tensorflow as tf
import tensorflow._api.v2.compat.v1 as tf   # 将TensorFlow2.0转换为1.0
tf.disable_v2_behavior()

def gram(layer):
    shape = tf.shape(layer)
    num_images = shape[0]
    width = shape[1]
    height = shape[2]
    num_filters = shape[3]
    filters = tf.reshape(layer, tf.stack([num_images, -1, num_filters]))
    grams = tf.matmul(filters, filters, transpose_a=True) / tf.to_float(width * height * num_filters)

    return grams

# 定义内容损失
# endpoints_dict是损失网络各层的计算结果
# content_layers是定义使用哪些层的差距来计算损失，默认配置是conv3_3
def content_loss(endpoints_dict, content_layers):
    content_losses = 0
    for layer in content_layers:
        # 之前把生成图像、原始图像同时传入损失网络中计算
        # 这里需要先把它们区分开
        # tf.concat 和 tf.split
        generated_images, content_images = tf.split(endpoints_dict[layer], num_or_size_splits=2, axis=0)
        size = tf.size(generated_images)
        # 所谓的内容损失，是生成图片的激活generated_images与原始图片的激活content_images的L2距离
        content_losses += tf.nn.l2_loss(generated_images - content_images) * 2 / tf.to_float(size)

    return content_losses

# 定义风格损失
# endpoints_dict是损失网络各层的计算结果
# style_layers是定义使用哪些层计算风格损失。默认是conv1_2, conv2_2, conv3_3, conv4_3
# style_features_t是利用原始的风格图片计算的层的激活
# 例如在wave模型中是img/wave.jpg计算的激活
def style_loss(endpoints_dict, style_feature_t, style_layers):
    style_losses = 0
    # summary是为TensorBoard服务的
    style_loss_summary = {}
    for style_gram, layer in zip(style_feature_t, style_layers):
        # 计算风格损失，只需要计算生成图片generated_images与目标风格style_feature_t的差距。因此不需要取出content_images
        generated_images, _ = tf.split(endpoints_dict[layer], 2, 0)
        size = tf.size(generated_images)
        # 调用gram函数计算Gram矩阵。风格损失定义为生成图片与目标风格Gram矩阵的L2 loss
        layer_style_loss = tf.nn.l2_loss(gram(generated_images) - style_gram) * 2 / tf.to_float(size)
        style_loss_summary[layer] = layer_style_loss
        style_losses += layer_style_loss

    return style_losses, style_loss_summary


from Ui import Ui_LoginWin, Ui_LeaderWin
from PyQt5 import QtWidgets

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    LoginWin = QtWidgets.QMainWindow()
    #LeaderWin=QtWidgets.QMainWindow()

    ui_login = Ui_LoginWin()
    ui_login.setupUi(LoginWin)

    #LeaderWin.show()
    LoginWin.show()
    sys.exit(app.exec_())


import numpy as np
def dirichlet_bc(Ue,UBC,args,main):
  UBC[0] = args[0]
  return UBC

def periodic_bc(Ue,UBC,args,main):
   return UBC

def isothermalwall_bc(Ue,UBC,args,main):
  uw = args[0]
  vw = args[1]
  ww = args[2]
  Tw = args[3]
  gamma = main.gas.gamma
  Cv = main.gas.Cv
  Cp = main.gas.Cp
  R = main.gas.R

  UBC[:] = 0.
  p = (gamma - 1.)*(Ue[4] - 0.5*Ue[1]**2/Ue[0] - 0.5*Ue[2]**2/Ue[0] - 0.5*Ue[3]**2/Ue[0]) #extraploate pressure
  #print(p/(Ue[0]*R))
  T = Tw
  E = Cv*T  + 0.5*(uw**2 + vw**2 + ww**2) #wall velocity is zero
  #p = rho R T
  UBC[0] = p/(R*T)
  print(np.mean(UBC[0]))

  UBC[1] = UBC[0]*uw
  UBC[2] = UBC[0]*vw
  UBC[3] = UBC[0]*ww
  UBC[4] = UBC[0]*E
  return UBC

def incompwall_bc(Ue,UBC,args,main):
  uw = args[0]
  vw = args[1]
  ww = args[2]
  UBC[:] = 0.
  UBC[0] = uw
  UBC[1] = vw
  UBC[2] = ww
  #UBC[3] = Ue[3]
  return UBC

def reflectingwall_bc(Ue,UBC,args,main):
  uw = args[0]
  vw = args[1]
  ww = args[2]
  gamma = main.gas.gamma
  Cv = main.gas.Cv
  Cp = main.gas.Cp
  R = main.gas.R

  UBC[0] = Ue[0]
  UBC[1] = -Ue[1]
  UBC[2] = -Ue[2]
  UBC[3] = -Ue[3]
  UBC[4] = Ue[4]
  return UBC

def adiabaticwall_bc(Ue,UBC,args,main):
  uw = args[0]
  vw = args[1]
  ww = args[2]
  gamma = main.gas.gamma
  Cv = main.gas.Cv
  Cp = main.gas.Cp
  R = main.gas.R

  UBC[:] = 0.
  #p = (gamma - 1.)*(Ue[4] - 0.5*Ue[1]**2/Ue[0] - 0.5*Ue[2]**2/Ue[0] - 0.5*Ue[3]**2/Ue[0]) #extraploate pressure
  #print(p/(Ue[0]*R))
  #T = p/(Ue[0]*main.gas.R) #extraplate temperature (zero heat flux)
  T = 1./Cv*(Ue[4]/Ue[0] - 0.5/Ue[0]**2*( Ue[1]**2 + Ue[2]**2 + Ue[3]**2 ) ) #extraplate temperature (zero heat flux)
  E = Cv*T  + 0.5*(uw**2 + vw**2 + ww**2) #wall velocity is zero
  p = (gamma - 1.)*E  #compute pressure at wall using extrapolate temp
  #p = rho R T
  UBC[0] = p/(R*T)
  UBC[1] = UBC[0]*uw
  UBC[2] = UBC[0]*vw
  UBC[3] = UBC[0]*ww
  UBC[4] = UBC[0]*E
  return UBC

def neumann_bc(Ue,UBC,args,main):
  return Ue

def subsonic_outflow(Ue,UBC,args,main):
  p,T = computePressure_and_Temperature(main,Ue)
  pB = p
  S_plus = p/Ue[0]**main.gamma


from os.path import expanduser
from pathlib import Path
from typing import Optional, Union

from dotenv import dotenv_values, find_dotenv

def config_to_bool(config_value: Optional[str]):
    """Turn a config value into a bool."""
    if not config_value or config_value.lower() in ["false", "0", "no"]:
        return False
    else:
        assert config_value.lower() in [
            "true",
            1,
            "yes",
        ], f"Invalid boolean config value: {config_value}"
        return True

def load_config():
    """Load the configuration files and convert known values"""
    config_dict: dict[str, Union[None, str, int, Path]] = {
        **dotenv_values(find_dotenv(".env")),
        **dotenv_values(find_dotenv(".env.local")),
    }

    bool_config_values = ["remove_duplicate_bi_encoder_results"]
    integer_config_values = [
        "num_processes",
        "top_k",
        "batch_size",
        "max_paragraphs_per_page",
        "max_sentences_per_paragraph",
    ]
    path_config_values = ["data_path", "crawl_result_file"]

    for key in bool_config_values:
        config_dict[key] = config_to_bool(config_dict[key])
    for key in integer_config_values:
        config_dict[key] = int(config_dict[key])
    for key in path_config_values:
        config_dict[key] = Path(expanduser(config_dict[key]))
    return config_dict


from tkinter import *
from tkinter import filedialog
from tkinter import messagebox

import shutil, os

def abreFichero():

    fichero = filedialog.askopenfilename(title="Abrir")

    vrutavideo.set(fichero)

def enviaVideo():

    try:
        #shutil.copy(vrutavideo.get(), r"\\10.8.65.29\TECNICOS\TELEDERMA\video.mp4" )
        shutil.copy(vrutavideo.get(), r"\\10.8.65.80\videos\video.mp4" )

        messagebox.showinfo("Aviso", "Video Enviado")
        #showinfo(title="Aviso", message="VIDEO ENVIADO")
    except:
        messagebox.showinfo("Aviso", "Error en eNVIO")
        #showinfo(title="Aviso", message="VIDEO ENVIADO")

raiz = Tk()

raiz.title("Pantallas Salud")
raiz.geometry('300x100+350+150')

miFrame = Frame(raiz)
miFrame.pack()

etiqueta = Label(miFrame, text="Seleccionar Video :")
etiqueta.grid(row=0, column=0)

boton = Button(miFrame, text="Buscar Video", command=abreFichero)
boton.grid(row=1, column=0)

vrutavideo = StringVar()

rutafichero = Label(miFrame, textvariable=vrutavideo)
rutafichero.grid(row=2, column=0)

botonEnviar = Button(miFrame, text="Enviar Video", command=enviaVideo)
botonEnviar.grid(row=3, column=0)

raiz.mainloop()


import json
import urllib.parse
import boto3
import os

def lambda_handler(event, context):
    # Get the bucket name and file/key name
    bucket = event['Records'][0]['s3']['bucket']['name']
    key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], encoding='utf-8')

    try:

        # print(json.dumps(event))

        # Read the file
        print('############# Loading function ################')
        s3 = boto3.client('s3')
        response = s3.get_object(Bucket=bucket, Key=key)

        text = response["Body"].read().decode()
        print('############# Body in the file ################')
        print(text)

        print('############# Reversed words ################')
        body = text.split()
        for i in range(len(body)):
            body[i] = body[i][::-1]

        # creating a string from the list
        reversedWords = (" ").join(body)
        print(reversedWords)

        finalWrite = '############# Loading function ################\n' + \
                     '############# Body in the file ################\n' + text + \
                     '\n############# Reversed words   ################\n' + reversedWords + \
                     '\n############# End of filed     ################\n'

        # writing to the tmp storage
        with open("/tmp/output.txt", "w") as myfile:
            myfile.write(finalWrite)

        # check if the output file got created
        print(os.listdir('/tmp/'))

        # Reading the output file
        with open("/tmp/output.txt", "r") as myfile:
            response = myfile.read()
            print(response)

        return 'Success!'

    except Exception as e:
        print(e)
        raise e


#  !!! json bize veri taşımada yardım eder !!!

import json

personString = '{"name":"Ali", "languages":["python","C#"]}'

personDict = { "name": "Ali","languages": ["Python","C#"]}

# JSON string to Dict
"""
jsonDonusturme = json.loads(personString)
#jsonDonusturme = jsonDonusturme["name"]

print(jsonDonusturme)
"""
# Bunun yerine bu kullanılabilir
with open("person.json") as f:
    data = json.load(f)
    print(data)
# ***************************************************

# Dict to JSON string
"""
print(type(personDict))

jsonCevirme = json.dumps(personDict) # json un bilgiye ulaşabilmesi için bilgiyi str yapar
print(type(jsonCevirme))
"""
personDict = json.loads(personString)
result = json.dumps(personDict, indent= 4) # okunaklığı arttırmak için belirtilen sayıda boşluk bıraktırır

print(personDict)
print(result)


from distutils.log import debug
from flask import Flask, render_template, url_for
app = Flask(__name__)

posts = [
    {
        'author' : 'Akshit Chugh',
        'title' : 'Blog Post One',
        'content' : 'I detect little communism',
        'date_posted' : 'April 17, 2022'
    },
    {
        'author' : 'John Doe',
        'title' : 'Blog Post Two',
        'content' : 'Ain\'t he right?',
        'date_posted' : 'April 18, 2022'
    }

]

@app.route("/")
@app.route("/home")
def home():
    return render_template('home.html', posts =  posts)

@app.route("/about")
def about():
    return render_template('about.html', title = 'About')

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


# V0

# V1
# https://blog.csdn.net/danspace1/article/details/87854254
# http://bookshadow.com/weblog/2016/11/13/leetcode-4sum-ii/
class Solution(object):
    def fourSumCount(self, A, B, C, D):
        """
        :type A: List[int]
        :type B: List[int]
        :type C: List[int]
        :type D: List[int]
        :rtype: int
        """
        ans = 0
        cnt = collections.defaultdict(int)
        for a in A:
            for b in B:
                cnt[a + b] += 1
        for c in C:
            for d in D:
                ans += cnt[-(c + d)]
        return ans

# V1'
# https://www.jiuzhang.com/solution/4sum-ii/#tag-highlight-lang-python
class Solution:
    """
    @param A: a list
    @param B: a list
    @param C: a list
    @param D: a list
    @return: how many tuples (i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero
    """
    def fourSumCount(self, A, B, C, D):
        # Write your code here
        map = {}
        ans = 0
        for i in range(len(A)):
            for j in range(len(B)):
                sum = A[i] + B[j]
                if sum in map:
                    map[sum] += 1
                else:
                    map[sum] = 1
        for i in range(len(C)):
            for j in range(len(D)):
                sum = -(C[i] + D[j])
                if sum in map:
                    ans += map[sum]
        return ans

# V2
# Time:  O(n^2)
# Space: O(n^2)
import collections
class Solution(object):
    def fourSumCount(self, A, B, C, D):
        """
        :type A: List[int]
        :type B: List[int]
        :type C: List[int]
        :type D: List[int]
        :rtype: int
        """
        A_B_sum = collections.Counter(a+b for a in A for b in B)
        return sum(A_B_sum[-c-d] for c in C for d in D)


#socket就是表示打开了一个网络连接，而打卡一个socket需要知道目标计算机的IP地址和端口号，再指定协议类型
import socket
#创建一个socket,AF_INET是指定IPv4协议，如果用IPv6就是AF_INET6，后面的参数指的是使用面向流的TCP协议，这样就建立起了一个socket对象
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

#建立连接
#下面是发起连接，须知道IP和端口号，域名会自动转换成IP地址，80端口号是web服务的标准端口，其他服务都有对应的端口号
#小于1024的端口号是internet标准服务端口号，大于1024的，可以任意使用
s.connect(('www.sina.com.cn',80))

#以下是发送数据
s.send(b'GET/HTTP/1.1\r\nConnection:close\r\n\r\n')
#以下是接受数据
buffer = []
while True:
    #以下是每次最多接受1k字节，在while循环中，只要有数据就不停接受，当空数据的时候，就跳出循环
    d = s.recv(1024)
    if d:
        buffer.append(d)
    else:
        break
data = b''.join(buffer)
print(data)
print(buffer)
header,html = data.split(b'\r\n\r\n',1)
print(header.decode('utf-8'))
with open('sina.html','wb') as f:
    f.write(html)

#当接受完数据之后，用close关闭socket，一次完整的网络通信就结束了
s.close()


import tkinter as tk #imports tkinter to create GUI
from tkinter import * #imports everything in tkinter
from datetime import date #imports date so it can be added to shipment log
today = date.today()
d = today.strftime("%b-%d-%Y") #creates a variable of the date from the system in Jan-1-2020 format

def chart(pieces,order): #creates a chart for conversions based on piece count
    Names = [] #creates a list to hold variable names
    count = 1
    counter1 = 1
    counter2 = 0
    pieces2 = pieces
    for x in range(pieces*4): #creates the list of variable names neeeded to create the chart
        Names += ["e"+str(count)]
        count = count + 1
    def convertustometric():#function to take user input and convert it from US to metric. It displays the conversions and writes it to the shipment log
        counter3 = 0
        counter4 = 1
        counter5 = 0
        Names2 = []
        for x in range(pieces):
            Names2 += [float(Names[0+counter3].get()) * .0254]
            Names2 += [float(Names[1+counter3].get()) * .0254]
            Names2 += [float(Names[2+counter3].get()) * .0254]
            Names2 += [float(Names[3+counter3].get()) * .45359237]
            file1 = open("shipment_log.txt","a")
            file1.write("Order Number {} Date {} Piece {} measures {} x {} x {} meters and {} kgs\n".format(order,d,counter4,Names2[0+counter3],Names2[1+counter3],Names2[2+counter3],Names2[3+counter3]))
            file1.close()
            counter3 = counter3 + 4
            counter4 = counter4 + 1
        for x in range(pieces):
            Label(Converter,text="{} meters".format(Names2[0+counter5])).grid(row=3 + counter5,column=2)
            Label(Converter,text="{} meters".format(Names2[1+counter5])).grid(row=4 + counter5,column=2)
            Label(Converter,text="{} meters".format(Names2[2+counter5])).grid(row=5 + counter5,column=2)
            Label(Converter,text="{} KG".format(Names2[3+counter5])).grid(row=6 + counter5,column=2)
            counter5 = counter5 + 4

    def convertmetrictous():#function to take user input and convert it from metric to US. It displays the conversions and writes it to the shipment log
        counter6 = 0
        counter7 = 1
        counter8 = 0
        Names3 = []
        for x in range(pieces):
            Names3 += [float(Names[0+counter6].get()) * 39.37]
            Names3 += [float(Names[1+counter6].get()) * 39.37]
            Names3 += [float(Names[2+counter6].get()) * 39.37]
            Names3 += [float(Names[3+counter6].get()) * 2.205]
            file1 = open("shipment_log.txt","a")
            file1.write("Order Number {} Date {} Piece {} measures {} x {} x {} meters and {} kgs\n".format(order,d,counter7,Names3[0+counter6],Names3[1+counter6],Names3[2+counter6],Names3[3+counter6]))
            file1.close()
            counter6 = counter6 + 4
            counter7 = counter7 + 1
        for x in range(pieces):
            Label(Converter,text="{} meters".format(Names3[0+counter8])).grid(row=3 + counter8,column=2)
            Label(Converter,text="{} meters".format(Names3[1+counter8])).grid(row=4 + counter8,column=2)
            Label(Converter,text="{} meters".format(Names3[2+counter8])).grid(row=5 + counter8,column=2)
            Label(Converter,text="{} KG".format(Names3[3+counter8])).grid(row=6 + counter8,column=2)
            counter8 = counter8 + 4

    while pieces2 > 0:#creates the chart to enter measurmentrs based on piece count. Uses list created earlier so each text entry box has a unique name.
        Label(Converter,text="piece {} length".format(counter1)).grid(row=3 + counter2,column=0)
        Label(Converter,text="piece {} width".format(counter1)).grid(row=4 + counter2,column=0)
        Label(Converter,text="piece {} heigth".format(counter1)).grid(row=5 + counter2,column=0)
        Label(Converter,text="piece {} weigth".format(counter1)).grid(row=6 + counter2,column=0)
        length= DoubleVar()
        width= DoubleVar()
        heigth= DoubleVar()
        weigth= DoubleVar()
        Names[0+counter2] = Entry(Converter,textvariable=length)
        Names[1+counter2] = Entry(Converter,textvariable=width)
        Names[2+counter2] = Entry(Converter,textvariable=heigth)
        Names[3+counter2] = Entry(Converter,textvariable=weigth)
        Names[0+counter2].grid(row=3 + counter2,column=1)
        Names[1+counter2].grid(row=4 + counter2,column=1)
        Names[2+counter2].grid(row=5 + counter2,column=1)
        Names[3+counter2].grid(row=6 + counter2,column=1)
        counter1 = counter1 + 1
        counter2 = counter2 + 4
        pieces2 = pieces2 - 1
        ustometric = Button(Converter,text="Convert US to Metric",command= lambda: convertustometric()).grid(row=2,column=2)
        metrictous = Button(Converter,text="Convert Metric to US",command= lambda: convertmetrictous()).grid(row=2,column=0)
    return;

Converter=tk.Tk() #creates the main window
Converter.title("Measurement Converter") #labels the main window
x=Label(Converter,text="Order Number").grid(row=0) #creates a label for the ordernumber entry box and places it
y=Label(Converter,text="Number of pieces").grid(row=1) #creates a label for the pieces entry box and places it
order_number = IntVar() #creates a variable type for the order number
pieces = IntVar() #creates a variable type for the piece count
entry1 = Entry(Converter,textvariable=order_number) #creates a text entry box that corresponds to the order number variable
entry2 = Entry(Converter,textvariable=pieces) #creates a text entry box that corresponds to the piece count
entry1.grid(row=0,column=1) #places the order number entry box
entry2.grid(row=1,column=1) #places the piece count entry box

next = Button(Converter,text="next", command= lambda: chart(int(pieces.get()),int(order_number.get()))).grid(row=2,column=1)
#this is a button that calls the chart funtion when clicked
Converter.mainloop()


'''
Author: You-Ting Wu @ NTUAS
Date: 2022.06.09
'''
from inspect import TPFLAGS_IS_ABSTRACT
from unittest import skip
import numpy as np
import math as m
from numpy.core.fromnumeric import sort
import pandas as pd
import datetime as dt
import glob
import xarray as xr
from xarray.core import variable
print('\n--------------------------------------------------------------------------')
print('feedback analysis: calculating partitioning radiative feedbacks using CESM-CAM5')
print('start time: %s'%(dt.datetime.now().strftime("%d/%m/%Y %H:%M:%S")))
print('--------------------------------------------------------------------------\n')

######################### initial data setting ###############################
# initial data setting
run_num = 30
ref_year = 1951
year_interval = [2006, 2100]
water_density = 997 #[kg/m^3]
smile_name = ['CESM1-CAM5']
smile_folder_name = ['cesm_lens']
ens = ["001","002","003","004","005","006","007","008","009","010","011","012","013","014","015","016","017","018","019","020", "021","022","023","024","025","026","027","028","029","030","031","032","033","034","035","101","102","103","104","105",]
#############################################################################

########################### calculating function #############################
# fx. of grouping the data (into 12 months) and calculating running mean
def group_running_mean(data):

    # groupby based on month and calculate moving data
    data_moving = data.groupby('time.month')[1].rolling(time = run_num, center=True).mean()
    for i in range(11):
        tmp = data.groupby('time.month')[i+2].rolling(time = run_num, center=True).mean()
        data_moving = xr.concat([data_moving, tmp], dim = 'month', join = 'override')

    # revise dimensions
    data_moving = data_moving.rename({'time': 'year'})
    year_interval = [data_moving['year.year'][0].values, data_moving['year.year'][-1].values]
    data_moving['year'] = np.arange(year_interval[0] - int(run_num/2), year_interval[1] - int(run_num/2) + 1)
    data_moving['month'] = np.arange(1, 13)

     # drop nan value
    data_moving = data_moving.sel(year = slice(year_interval[0], year_interval[1] - int(run_num/2) - int((run_num-1)/2) ))

    return(data_moving)

# fx. of claculating saturation specific humidity [g/kg]
def calcsatspechum(t,p):

    #T is temperature, P is pressure in hPa

    # Formulae from Buck (1981):
    # saturation mixing ratio wrt liquid water [g/kg]
    es = (1.0007+(3.46e-6*p))*6.1121*np.exp(17.502*(t-273.15)/(240.97+(t-273.15)))
    wsl = 0.622*es/(p-es)

    # saturation mixing ratio wrt ice [g/kg]
    es = (1.0003+(4.18e-6*p))*6.1115*np.exp(22.452*(t-273.15)/(272.55+(t-273.15)))
    wsi = 0.622*es/(p-es)

    ws = wsl.where(t > 273.15, wsi)
    qs = ws/(1+ws)  # saturation specific humidity, g/kg

    return(qs)
##############################################################################

############################ feedback function ###############################

def data_merge(variable, ens):

    file_hist = sorted(glob.glob('/work6/L.yuchiao/cesm1_le/%s/b.e11.B20TRC5CNBDRD.f09_g16.%s.*.nc'%(variable, ens)))
    file_proj = sorted(glob.glob('/work6/L.yuchiao/cesm1_le/%s/b.e11.BRCP85C5CNBDRD.f09_g16.%s.*.nc'%(variable, ens)))
    print(file_hist)
    print(file_proj)

    data_hist = xr.open_dataset(file_hist[0])['%s'%(variable)]
    data_hist['time'] = data_hist.indexes['time'].to_datetimeindex(unsafe=True) - np.timedelta64(10*60*60*24, 's')
    data_hist = data_hist[data_hist['time.year'] >= ref_year, :, :]

    if len(file_proj)>1:
        data1 = xr.open_dataset(file_proj[0])['%s'%(variable)]
        data2 = xr.open_dataset(file_proj[1])['%s'%(variable)]
        data1['time'] = data1.indexes['time'].to_datetimeindex(unsafe=True) - np.timedelta64(10*60*60*24, 's')
        data2['time'] = data2.indexes['time'].to_datetimeindex(unsafe=True) - np.timedelta64(10*60*60*24, 's')
        data = xr.concat([data_hist, data1, data2], dim='time', join = 'override')
    else:
        data3 = xr.open_dataset(file_proj[0])['%s'%(variable)]
        data3['time'] = data3.indexes['time'].to_datetimeindex(unsafe=True) - np.timedelta64(10*60*60*24, 's')
        data = xr.concat([data_hist, data3], dim='time', join = 'override')

    print('running mean...')
    data_mean = group_running_mean(data)

    if variable == 'PS':
        hyam = t_kernel['hyam']
        hybm = t_kernel['hybm']
        p0 = t_kernel['P0']
        data_mean = (hyam * p0 + hybm * data_mean)

    return(data_mean)

def feedback_ensemble(type):
    file_list = sorted(glob.glob('%s/*.nc'%(type)))
    data = xr.open_mfdataset(file_list, concat_dim = 'ensemble', combine='nested', parallel = True)
    data.to_netcdf('%s.nc'%(type))
    return()

# calculating radiatvie feedback
def feedback_calculation(type):

    print('\n***  calculating %s feedback  ***'%(type))

    # Albedo feedback
    if type == 'albedo':
        # collect surface shortwave radiation fields for calculating albedo change
        # ref.mean (and ensemble mean)
        file_fsns = sorted(glob.glob('/work6/YCL.youtingw/CS2022/FSNS/*.nc'))
        file_fsds = sorted(glob.glob('/work6/YCL.youtingw/CS2022/FSDS/*.nc'))
        file_ts = sorted(glob.glob('/work6/YCL.youtingw/CS2022/TS/*.nc'))

        # read TOA albedo kernel
        alb_kernel = xr.open_dataset('/home/youtingw/radiative_kernel/cesm_cam5_kernel/kernels/alb.kernel.nc')['FSNT'].rename({'time':'month'}).transpose('month', 'lat', 'lon')
        alb_kernel['month'] = range(1,13)

        for i in range(40):
            SW_sfc_net_2 = xr.open_dataset(file_fsns[i])['FSNS']
            SW_sfc_down_2 = xr.open_dataset(file_fsds[i])['FSDS']
            ts = xr.open_dataset(file_ts[i])['TS']
            print(file_fsns[i])
            print(file_fsds[i])
            print(file_ts[i])

            SW_sfc_net_1 = SW_sfc_net_2.sel(year = ref_year)
            SW_sfc_down_1 = SW_sfc_down_2.sel(year = ref_year)

            # calculate albedo difference
            alb1 = (1 - SW_sfc_net_1/SW_sfc_down_1).fillna(0)
            alb2 = (1 - SW_sfc_net_2/SW_sfc_down_2).fillna(0)
            dalb = (alb2-alb1) * 100

            # calculate albedo feedback in Arctic
            dts = (ts - ts.sel(year = ref_year)).transpose('year', 'month', 'lat', 'lon')
            dSW_alb = (alb_kernel * dalb).transpose('year', 'month', 'lat', 'lon')
            alb_parameter = (dSW_alb / dts)
            dSW_alb_zonal = dSW_alb.mean(dim = 'lon', skipna = True)
            alb_parameter_zonal = alb_parameter.mean(dim = 'lon', skipna = True)
            filename = 'albedo/albedo_feedback_%s.nc'%(i)
            print('output to the file: %s'%(filename))
            file = alb_parameter_zonal.to_dataset(name ='parameter')
            file['energy'] = dSW_alb_zonal
            file.to_netcdf(filename)

            dts_zonal = dts.mean(dim = 'lon', skipna = True)
            filename = 'ts/ts_%s.nc'%(i)
            print('output to the file: %s'%(filename))
            file = dts_zonal.to_dataset(name ='dts_zonal').to_netcdf(filename)

    # calculating Lapse rate feedback
    elif type == 'lapse_rate_planck':

            # calculate air temperature change and mask stratosphere
            # ref.mean and projection mean (and ensemble mean)

        for i in range(1,40):

            p = data_merge('PS', ens[i]) / 100
            t = data_merge('T', ens[i])
            ts = data_merge('TS', ens[i])

            dta = (t - t.sel(year = ref_year)).transpose('year', 'month', 'lat', 'lon', 'lev')
            dts = (ts - ts.sel(year = ref_year)).transpose('year', 'month', 'lat', 'lon')

            lat = dts['lat']
            x = np.cos(np.radians(lat))
            p_tropopause_zonalmean = 300 - 200 * x
            p_tropopause, _ = xr.broadcast(p_tropopause_zonalmean, lon)
            p_tropopause, _ = xr.broadcast(p_tropopause, lev)
            p_tropopause, _ = xr.broadcast(p_tropopause, p['month'])
            p_tropopause, _ = xr.broadcast(p_tropopause, p['year'])
            p_tropopause = p_tropopause.transpose('year', 'month', 'lat', 'lon', 'lev')

            dta = dta * (p >= p_tropopause)
            dts3d, _ = xr.broadcast(dts, lev)
            dt_planck = dts3d * (p >= p_tropopause)
            dt_lapserate = (dta - dt_planck) * (p >= p_tropopause)

            # Convolve air temperature kernel with air temperature change
            ta_kernel = xr.open_dataset('/home/youtingw/radiative_kernel/cesm_cam5_kernel/kernels/t.kernel.nc')['FLNT'].rename({'time':'month'}).transpose('month', 'lat', 'lon', 'lev')
            ta_kernel['month'] = range(1,13)
            ta_kernel['lat'] = dts['lat']
            ts_kernel = xr.open_dataset('/home/youtingw/radiative_kernel/cesm_cam5_kernel/kernels/ts.kernel.nc')['FLNT'].rename({'time':'month'}).transpose('month', 'lat', 'lon')
            ts_kernel['month'] = range(1,13)
            ts_kernel['lat'] = dts['lat']

            # LW calculation
            dLW_ts = (dts * ts_kernel).transpose('year', 'month', 'lat', 'lon')
            dLW_planck = (dt_planck * ta_kernel).sum(dim = 'lev', skipna = True).transpose('year', 'month', 'lat', 'lon')
            dLW_lapserate = (dt_lapserate * ta_kernel).sum(dim = 'lev', skipna = True).transpose('year', 'month', 'lat', 'lon')

            # calculate Planck feedback in Arctic
            dLW_planck_zonal = ((- dLW_planck - dLW_ts)).mean(dim = 'lon', skipna = True)
            planck_feedback_zonal = ((- dLW_planck - dLW_ts)/ dts).mean(dim = 'lon', skipna = True)

            # output to the file
            filename = 'planck/planck_feedback_%s.nc'%(i)
            print('output to the file: %s'%(filename))
            file = planck_feedback_zonal.to_dataset(name ='parameter')
            file['energy'] = dLW_planck_zonal
            file.to_netcdf(filename)

            # calculate lapse rate feedbacks in Arctic
            dLW_lapserate_zonal = (-dLW_lapserate).mean(dim = 'lon', skipna = True)
            lapserate_feedback_zonal = (-dLW_lapserate / dts).mean(dim = 'lon', skipna = True)

            # output to the file
            filename = 'lapserate/lapserate_feedback_%s.nc'%(i)
            print('output to the file: %s'%(filename))
            file = lapserate_feedback_zonal.to_dataset(name ='parameter')
            file['energy'] = dLW_lapserate_zonal
            file.to_netcdf(filename)

    # calculating water vapor feedback
    elif type == 'water_vapor':

        file_dq1k = sorted(glob.glob('/work6/YCL.youtingw/CS2022/dq1k/*.nc'))
        file_q = sorted(glob.glob('/work6/YCL.youtingw/CS2022/Q/*.nc'))
        file_ts = sorted(glob.glob('/work6/YCL.youtingw/CS2022/TS/*.nc'))
        file_p_sigma = sorted(glob.glob('/work6/YCL.youtingw/CS2022/P_sigma/*.nc'))

        # Read q kernels
        q_LW_kernel_raw = xr.open_dataset('/home/youtingw/radiative_kernel/cesm_cam5_kernel/kernels/q.kernel.nc')['FLNT'].rename({'time':'month'}).transpose('month', 'lat', 'lon', 'lev')
        q_SW_kernel_raw = xr.open_dataset('/home/youtingw/radiative_kernel/cesm_cam5_kernel/kernels/q.kernel.nc')['FSNT'].rename({'time':'month'}).transpose('month', 'lat', 'lon', 'lev')

        for i in range(40):
            # get q data
            q = xr.open_dataset(file_q[i])['Q']
            ts = xr.open_dataset(file_ts[i])['TS']
            dts = (ts - ts.sel(year = ref_year)).transpose('year', 'month', 'lat', 'lon')
            p = xr.open_dataset(file_p_sigma[i])['pmid'] / 100 #[hPa]

            lat = dts['lat']
            x = np.cos(np.radians(lat))
            p_tropopause_zonalmean = 300 - 200 * x
            p_tropopause, _ = xr.broadcast(p_tropopause_zonalmean, lon)
            p_tropopause, _ = xr.broadcast(p_tropopause, lev)
            p_tropopause, _ = xr.broadcast(p_tropopause, p['month'])
            p_tropopause, _ = xr.broadcast(p_tropopause, p['year'])
            p_tropopause = p_tropopause.transpose('year', 'month', 'lat', 'lon', 'lev')

            # Normalize kernels by the change in moisture for 1 K warming at constant RH (linear)
            dq1k = xr.open_dataset(file_dq1k[i])['dq1k']
            q_LW_kernel = q_LW_kernel_raw / dq1k
            q_SW_kernel = q_SW_kernel_raw / dq1k

            # mask out the stratosphere
            dq = (q - q.sel(year = ref_year)).transpose('year', 'month', 'lat', 'lon', 'lev')
            dq = dq * (p >= p_tropopause)

            # calculate water vapor feedback in zonal mean
            dLW_q = (q_LW_kernel * dq).sum(dim = 'lev', skipna = True)
            dSW_q = (q_SW_kernel * dq).sum(dim = 'lev', skipna = True)
            dR_q_zonal = (-dLW_q+dSW_q).mean(dim = 'lon', skipna = True)
            q_feedback_zonal = ((-dLW_q+dSW_q) / dts).mean(dim = 'lon', skipna = True)

            filename = 'water_vapor/water_vapor_%s.nc'%(i)
            file = q_feedback_zonal.to_dataset(name ='parameter')
            file['energy'] = dR_q_zonal
            file.to_netcdf(filename)
            print('output to the file: %s'%(filename))

    # calculating cloud feedaback
    elif type == 'cloud':
        # read kernel data

        ts_kernel = xr.open_dataset('/home/youtingw/radiative_kernel/cesm_cam5_kernel/kernels/ts.kernel.nc')['FLNT'].rename({'time':'month'}).transpose('month', 'lat', 'lon').mean(dim = 'lon', skipna = True)
        ts_kernel_clearsky = xr.open_dataset('/home/youtingw/radiative_kernel/cesm_cam5_kernel/kernels/ts.kernel.nc')['FLNTC'].rename({'time':'month'}).transpose('month', 'lat', 'lon').mean(dim = 'lon', skipna = True)
        ta_kernel = xr.open_dataset('/home/youtingw/radiative_kernel/cesm_cam5_kernel/kernels/t.kernel.nc')['FLNT'].rename({'time':'month'}).transpose('month', 'lat', 'lon', 'lev').mean(dim = 'lon', skipna = True)
        ta_kernel_clearsky = xr.open_dataset('/home/youtingw/radiative_kernel/cesm_cam5_kernel/kernels/t.kernel.nc')['FLNTC'].rename({'time':'month'}).transpose('month', 'lat', 'lon', 'lev').mean(dim = 'lon', skipna = True)
        alb_kernel = xr.open_dataset('/home/youtingw/radiative_kernel/cesm_cam5_kernel/kernels/alb.kernel.nc')['FSNT'].rename({'time':'month'}).transpose('month', 'lat', 'lon').mean(dim = 'lon', skipna = True)
        alb_kernel_clearsky = xr.open_dataset('/home/youtingw/radiative_kernel/cesm_cam5_kernel/kernels/alb.kernel.nc')['FSNTC'].rename({'time':'month'}).transpose('month', 'lat', 'lon').mean(dim = 'lon', skipna = True)
        q_LW_kernel_raw = xr.open_dataset('/home/youtingw/radiative_kernel/cesm_cam5_kernel/kernels/q.kernel.nc')['FLNT'].rename({'time':'month'}).transpose('month', 'lat', 'lon', 'lev').mean(dim = 'lon', skipna = True)
        q_SW_kernel_raw = xr.open_dataset('/home/youtingw/radiative_kernel/cesm_cam5_kernel/kernels/q.kernel.nc')['FSNT'].rename({'time':'month'}).transpose('month', 'lat', 'lon', 'lev').mean(dim = 'lon', skipna = True)
        q_LW_kernel_clearsky_raw = xr.open_dataset('/home/youtingw/radiative_kernel/cesm_cam5_kernel/kernels/q.kernel.nc')['FLNTC'].rename({'time':'month'}).transpose('month', 'lat', 'lon', 'lev').mean(dim = 'lon', skipna = True)
        q_SW_kernel_clearsky_raw = xr.open_dataset('/home/youtingw/radiative_kernel/cesm_cam5_kernel/kernels/q.kernel.nc')['FSNTC'].rename({'time':'month'}).transpose('month', 'lat', 'lon', 'lev').mean(dim = 'lon', skipna = True)

        # file paths
        file_q = sorted(glob.glob('/work6/YCL.youtingw/CS2022/Q_zonal/*.nc'))
        file_ts = sorted(glob.glob('/work6/YCL.youtingw/CS2022/TS/*.nc'))
        file_t = sorted(glob.glob('/work6/YCL.youtingw/CS2022/T_zonal/*.nc'))
        file_p_sigma = sorted(glob.glob('/work6/YCL.youtingw/CS2022/P_sigma_zonal/*.nc'))
        file_fsns = sorted(glob.glob('/work6/YCL.youtingw/CS2022/FSNS/*.nc'))
        file_fsds = sorted(glob.glob('/work6/YCL.youtingw/CS2022/FSDS/*.nc'))
        file_fsnt = sorted(glob.glob('/work6/YCL.youtingw/CS2022/FSNT/*.nc'))
        file_fsntc = sorted(glob.glob('/work6/YCL.youtingw/CS2022/FSNTC/*.nc'))
        file_flnt = sorted(glob.glob('/work6/YCL.youtingw/CS2022/FLNT/*.nc'))
        file_flntc = sorted(glob.glob('/work6/YCL.youtingw/CS2022/FLNTC/*.nc'))

        for i in range(40):
            # get basic variables
            ts = xr.open_dataset(file_ts[i])['TS']
            dts = (ts - ts.sel(year = ref_year)).transpose('year', 'month', 'lat','lon').mean(dim = 'lon', skipna = True)
            t = xr.open_dataset(file_t[i])['T_zonal']
            dta = (t - t.sel(year = ref_year)).transpose('year', 'month', 'lat', 'lev')

            q = xr.open_dataset(file_q[i])['Q_zonal']
            p = xr.open_dataset(file_p_sigma[i])['P_sigma_zonal'] / 100 #[hPa]
            dq = (q - q.sel(year = ref_year)).transpose('year', 'month', 'lat', 'lev')

            lat = dts['lat']
            x = np.cos(np.radians(lat))
            p_tropopause_zonalmean = 300 - 200 * x
            p_tropopause, _ = xr.broadcast(p_tropopause_zonalmean, lev)
            p_tropopause, _ = xr.broadcast(p_tropopause, p['month'])
            p_tropopause, _ = xr.broadcast(p_tropopause, p['year'])
            p_tropopause = p_tropopause.transpose('year', 'month', 'lat', 'lev')

            # mask out the stratosphere
            dta = dta * (p >= p_tropopause)
            dq = dq * (p >= p_tropopause)

            # Read change in mixing ratio per degree warming at constant RH
            print('calculating q feedback')
            p['lat'] = t['lat']
            q['lat'] = t['lat']
            qs1 = calcsatspechum(t[0,:,:,:], p[0,:,:,:])
            qs2 = calcsatspechum(t, p)
            dqsdt = (qs2 - qs1) / dta
            rh = q[0,:,:,:] / qs1
            dqdt = rh * dqsdt

            # Normalize kernels by the change in moisture for 1 K warming at constant RH (linear)
            q_LW_kernel = q_LW_kernel_raw / dqdt
            q_SW_kernel = q_SW_kernel_raw / dqdt
            q_LW_kernel_clearsky = q_LW_kernel_clearsky_raw / dqdt
            q_SW_kernel_clearsky = q_SW_kernel_clearsky_raw / dqdt

            # Collect surface shortwave radiation fields for calculating albedo change
            print('calculating alb feedback')
            SW_sfc_net_2 = xr.open_dataset(file_fsns[i])['FSNS']
            SW_sfc_down_2 = xr.open_dataset(file_fsds[i])['FSDS']
            SW_sfc_net_1 = SW_sfc_net_2.sel(year = ref_year)
            SW_sfc_down_1 = SW_sfc_down_2.sel(year = ref_year)
            alb1 = (1 - SW_sfc_net_1/SW_sfc_down_1).fillna(0)
            alb2 = (1 - SW_sfc_net_2/SW_sfc_down_2).fillna(0)
            dalb = ((alb2-alb1) * 100).mean(dim = 'lon', skipna = True)

            # LW for sfc. temp. change
            print('calculating energy in y/n cloud')
            dLW_ts = ts_kernel * dts
            dLW_ts_cs = ts_kernel_clearsky * dts

            # LW for temp. change in each layer
            dLW_ta = (ta_kernel * dta).sum(dim = 'lev', skipna = True)
            dLW_ta_cs = (ta_kernel_clearsky * dta).sum(dim = 'lev', skipna = True)

            # SW for albedo change
            dSW_alb = alb_kernel * dalb
            dSW_alb_cs = alb_kernel_clearsky * dalb

            # LW & SW for moisture change
            dLW_q = (q_LW_kernel * dq).sum(dim = 'lev', skipna = True)
            dSW_q = (q_SW_kernel * dq).sum(dim = 'lev', skipna = True)
            dLW_q_cs = (q_LW_kernel_clearsky * dq).sum(dim = 'lev', skipna = True)
            dSW_q_cs = (q_SW_kernel_clearsky * dq).sum(dim = 'lev', skipna = True)

            # Change in Cloud Radiative Effect (CRE)
            print('calculating CRE')
            d_sw = xr.open_dataset(file_fsnt[i])['FSNT'] - xr.open_dataset(file_fsnt[i])['FSNT'].sel(year = ref_year)
            d_sw_cs = xr.open_dataset(file_fsntc[i])['FSNTC'] - xr.open_dataset(file_fsntc[i])['FSNTC'].sel(year = ref_year)
            d_lw = xr.open_dataset(file_flnt[i])['FLNT'] - xr.open_dataset(file_flnt[i])['FLNT'].sel(year = ref_year)
            d_lw_cs = xr.open_dataset(file_flntc[i])['FLNTC'] - xr.open_dataset(file_flntc[i])['FLNTC'].sel(year = ref_year)
            d_cre_sw = (d_sw_cs - d_sw).mean(dim = 'lon', skipna = True)
            d_cre_lw = (d_lw_cs - d_lw).mean(dim = 'lon', skipna = True)

            # Cloud masking of radiative forcing
            # GHG forcing
            ghg_sw = 0
            ghg_lw = 0
            # aerosol forcing
            aerosol_sw = 0
            aerosol_lw = 0
            cloud_masking_of_forcing_sw = aerosol_sw + ghg_sw
            cloud_masking_of_forcing_lw = aerosol_lw + ghg_lw

            # cloud feedback (CRE + cloud masking of radiative forcing + corrections for each feedback)
            dLW_cloud = -d_cre_lw + cloud_masking_of_forcing_lw + (dLW_q_cs-dLW_q) + (dLW_ta_cs-dLW_ta) + (dLW_ts_cs-dLW_ts)
            dSW_cloud = -d_cre_sw + cloud_masking_of_forcing_sw + (dSW_q_cs-dSW_q) + (dSW_alb_cs-dSW_alb)

            # Take the annual average and global area average
            dLW_cloud_zonal = (-dLW_cloud)
            dSW_cloud_zonal = (dSW_cloud)

            # Divide by the global annual mean surface warming (units: W/m2/K)
            lw_cloud_feedback_zonal = (dLW_cloud / dts)
            sw_cloud_feedback_zonal = (dSW_cloud / dts)

            # output to the files
            filename_lw = 'lw_cloud/lw_cloud_%s.nc'%(i)
            file = lw_cloud_feedback_zonal.to_dataset(name ='parameter')
            file['energy'] = dLW_cloud_zonal
            file.to_netcdf(filename_lw)
            print('output to the file: %s'%(filename_lw))

            filename_sw = 'sw_cloud/sw_cloud_%s.nc'%(i)
            file = sw_cloud_feedback_zonal.to_dataset(name ='parameter')
            file['energy'] = dSW_cloud_zonal
            file.to_netcdf(filename_sw)
            print('output to the file: %s'%(filename_sw))

    elif type == 'dR':
        file_fsnt = sorted(glob.glob('/work6/YCL.youtingw/CS2022/FSNT/*.nc'))
        file_flnt = sorted(glob.glob('/work6/YCL.youtingw/CS2022/FLNT/*.nc'))

        fsnt = xr.open_mfdataset(file_fsnt, concat_dim = 'ensemble', combine='nested', parallel = True)['FSNT']
        flnt = xr.open_mfdataset(file_flnt, concat_dim = 'ensemble', combine='nested', parallel = True)['FLNT']

        dfsnt = fsnt - fsnt.sel(year = 1951)
        dflnt = flnt - flnt.sel(year = 1951)
        dR = (dfsnt - dflnt).mean(dim = 'lon', skipna = True)

        filename = 'dR.nc'
        print('output to the file: %s'%(filename))
        dR.to_dataset(name ='dR').to_netcdf(filename)

    return()
##############################################################################

############################### base field part ##############################
# coordinate info.
ps = xr.open_dataset('/home/youtingw/radiative_kernel/cesm_cam5_kernel/kernels/PS.nc')
t_kernel = xr.open_dataset('/home/youtingw/radiative_kernel/cesm_cam5_kernel/kernels/t.kernel.nc')
lat = ps['lat']
lon = ps['lon']
lev = t_kernel['lev']
gw = t_kernel['gw']
#############################################################################

################################# main part ###################################
# feedback_calculation('albedo')
# feedback_calculation('lapse_rate_planck')
# feedback_calculation('water_vapor')
feedback_calculation('cloud')
# feedback_calculation('dR')

# feedback_ensemble('albedo')
# feedback_ensemble('lapse_rate_planck')
# feedback_ensemble('water_vapor')
feedback_ensemble('lw_cloud')
feedback_ensemble('sw_cloud')
# feedback_ensemble('LHF')
# feedback_ensemble('SHF')
#feedback_ensemble('dOHT')
#############################################################################

print('--------------------------------------------------------------------------')
print('finish time: %s'%(dt.datetime.now().strftime("%d/%m/%Y %H:%M:%S")))
print('--------------------------------------------------------------------------\n')


'''
从有道词典下载更多例句
'''
from urllib import request
import json, re
import requests
from lxml import etree
import time

class Youdao:
    def __init__(self):
        self.url_1 = 'http://fanyi.youdao.com/openapi.do'
        self.key = '993123434'  #有道API key
        self.keyfrom = 'pdblog' #有道keyfrom

        self.headers = {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
    # "Accept-Encoding": "gzip, deflate",
    "Accept-Language": "zh-CN,zh;q=0.9",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36",
    "Connection": "keep-alive",
    "referer": "http://dict.youdao.com/"
}
        self.url_2 = 'http://dict.youdao.com/w/eng/****/#keyfrom=dict2.index'

    def get_url_1(self, word):
        url = self.url_1 + '?keyfrom=' + self.keyfrom + '&key='+self.key + '&type=data&doctype=json&version=1.1&q=' + word
        result = request.urlopen(url).read().decode('utf-8')
        self.res = json.loads(result)
        cadidates = []
        try:
            for dic in self.res['web']:
                if word in dic['key']:
                    s = dic['key'].replace(word, '****').lower()
                    s = s.replace('****', word)
                    cadidates.append(s)
        except:
            cadidates = []
        return cadidates

    def get_url_2(self, word):
        url = self.url_2.replace('****', word)
        response = requests.get(url=url, headers=self.headers)
        selector = etree.HTML(response.text)
        cadidates = []
        try:
            for n in ['1','2','3']:
                content = selector.xpath('//*[@id="bilingual"]/ul/li[%s]/p[1]'%n)
                if content:
                    sentence = []
                    for i in content[0]:
                        sentence.append(i.xpath('string(.)'))
                    if word in ' '.join(sentence):
                        s = ' '.join(sentence).replace(word, '****')
                        s = re.sub('t[ ’\']+s', 'ts', s)
                        s = re.sub('["…‘“”,.:?]', ' ', s)
                        s = ' '.join(s.lower().split()).replace('****', word)
                        cadidates.append(s)
        except:
            cadidates = []
        return cadidates

if __name__ == '__main__':
    youdao = Youdao()
    with open('./tmp_data/word_dict.json', encoding='utf-8') as f:
        vocab_dict = json.load(f)
    with open('./tmp_data/wordnet', encoding='utf-8') as f:
        wordnet = json.load(f)
    for w in vocab_dict:
        if w in wordnet and len(wordnet[w]["examples"])<=3:
            print(w)
            downloads = youdao.get_url_1(w)+youdao.get_url_2(w)
            examples = []
            try:
                for sent in downloads:
                    sent += ' <SEP>'
                    if len(sent.split())>2:
                        idx = sent.split().index(w)
                        examples.append([sent, idx])
                wordnet[w]["examples"].extend(examples)
            except:
                examples = []
            print(examples)
            time.sleep(3)
    with open('./tmp_data/wordnet', 'w', encoding='utf-8') as f:
        json.dump(wordnet, f, indent=2)