import datetime
import pymongo

client = pymongo.MongoClient("mongodb://localhost:27017/")
db = client["amazon"]

def add_product_detail(details):

    new = db["products"]
    ASIN = details["url"][len(details["url"])-10:len(details["url"])]

    try:
        details["date"] = datetime.datetime.utcnow()
        new.update_one({"asin":ASIN}, {"$set": {"asin":ASIN}, "$push":{"details":details}}, upsert=True)
        return True
    except Exception as identifier:
        print(identifier)
        return False

def get_product_history(asin):

    new = db["products"]
    try:
        find = new.find_one({"asin": asin}, {"_id": 0})
        if find:
            return find
        else:
            raise Exception("Not found")
    except Exception as identifier:
        print(identifier)
        return None


#Importing packages
import streamlit as st
import numpy as np
import os
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from keras.applications.inception_v3 import InceptionV3
import cv2 as cv
from PIL import Image

classes = ['Pepper__bell___healthy',
 'Potato___Early_blight',
 'Potato___healthy',
 'Potato___Late_blight',
 'Tomato__Target_Spot',
 'Tomato__Tomato_mosaic_virus',
 'Tomato__Tomato_YellowLeaf__Curl_Virus',
 'Tomato_Bacterial_spot',
 'Tomato_Early_blight',
 'Tomato_healthy',
 'Tomato_Late_blight',
 'Tomato_Leaf_Mold',
 'Tomato_Septoria_leaf_spot',
 'Tomato_Spider_mites_Two_spotted_spider_mite',
 'Pepper__bell___Bacterial_spot']

#Title page
st.title("WebApp")

#Image Uploader
uploaded_file = st.file_uploader("Choose a file")
if uploaded_file is not None:

    # pillow opening image and displaying image
    image = Image.open(uploaded_file)
    st.image(image)

    # resizing image with cv2
    img_2rgb = image.convert("RGB")
    np_array = np.array(img_2rgb)
    final_image = cv.resize(np_array,(75,75))

    # expanding image to fit to input into the model
    expanded_img = np.expand_dims(final_image,axis = 0)

    #loading in the model
    model = tf.keras.models.load_model('tl_secondpass-20220125T010409Z-001/tl_secondpass')

    # prediction with the model
    prediction = (model.predict(expanded_img))
    prediction_index = np.argmax(prediction)
    final_prediction = classes[prediction_index]
    confidence = str(prediction[0][prediction_index]*100)

    st.write("The model predicts this image is a", final_prediction,"with a confidence of",confidence+'%')


import torch
import torch.nn as nn
import torch.nn.functional as F

#计算attention
class ScaledDotProductAttention(nn.Module):
    ''' Scaled Dot-Product Attention '''

    def __init__(self, temperature, attn_dropout=0.1):
        super().__init__()
        # 其实就是论文中的根号d_k
        self.temperature = temperature
        # temperature = d_k ** 0.5
        self.dropout = nn.Dropout(attn_dropout)

    def forward(self, q, k, v, mask=None):
        # sz_b: batch_size 批量大小
        # len_q,len_k,len_v: 序列长度 在这里他们都相等
        # n_head: 多头注意力 默认为8
        # d_k,d_v: k v 的dim(维度) 默认都是64
        # 此时q的shape为(sz_b, n_head, len_q, d_k) (sz_b, 8, len_q, 64)
        # 此时k的shape为(sz_b, n_head, len_k, d_k) (sz_b, 8, len_k, 64)
        # 此时v的shape为(sz_b, n_head, len_k, d_v) (sz_b, 8, len_k, 64)
        # q先除以self.temperature(论文中的根号d_k) k交换最后两个维度(这样才可以进行矩阵相乘) 最后两个张量进行矩阵相乘

        # attn的shape为(sz_b, n_head, len_q, len_k)
        attn = torch.matmul(q / self.temperature, k.transpose(2, 3))

        #decoder中的mask
        if mask is not None:
            # 用-1e9代替0 -1e9是一个很大的负数 经过softmax之后接近与0
            # 其一：去除掉各种padding在训练过程中的影响
            # 其二，将输入进行遮盖，避免decoder看到后面要预测的东西。（只用在decoder中）
            attn = attn.masked_fill(mask == 0, -1e9)
            #mask==0取值为true使用-1e9填充

        # 先在attn的最后一个维度做softmax 再dropout 得到注意力分数
        attn = self.dropout(F.softmax(attn, dim=-1))
        # 最后attn与v进行矩阵相乘
        # output的shape为(sz_b, 8, len_q, 64)
        output = torch.matmul(attn, v)
        # 返回 output和注意力分数
        return output, attn


import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots(2, 1, figsize=(10, 6), tight_layout=True,
                       facecolor=(.18, .31, .31))
x = np.arange(10)
classes = ['plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
cmap = plt.get_cmap('tab10')
for idx, c in enumerate(classes):
    ax[0].plot(x, idx * x)
    ax[1].plot(x, idx * x, color=cmap(idx), linestyle='dotted', label=c)
ax[1].set_title('color map', color='0.7')
ax[1].legend(loc='best', fancybox=True, ncol=5)
for axes in ax:
    axes.tick_params(labelcolor='tab:orange')
plt.tight_layout(pad=0.5)
plt.show()


def getDic(a):
    palavras=[]
    for i in range(a):
        aux = list(input().split())
        palavras.append(aux)

    return palavras

quant = int(input())
words = getDic(quant)

while(True):
    letter = list(input().split())
    traducao = ""
    if letter[0] != '*':
        for i in range(len(letter)):
            for j in range(len(words)):
                if(letter[i]==words[j][0]):
                    traducao+=words[j][2]
                    if(i != len(letter)-1):
                        traducao+=" "
        print(traducao)
    else:
        break


from flask import Flask, render_template
import csv, os

def load_scores(filename):
    with open(filename, newline='') as csvfile:
        reader = csv.DictReader(csvfile)
        players = []
        for line in reader:
            players.append(line)
    return players

def process_scores(players):
    dict_list = []
    for player in players:
        score_dict = {}
        score_dict["name"] = player["name"]
        score_dict["id"] = player["id"]
        score_dict["level"] = player["level"]
        score_dict["score"] = player["score"]
        dict_list.append(score_dict)
    return dict_list

def get_scores():
    filename = 'pvzscore.csv'
    data = load_scores(filename)
    scores = process_scores(data)
    return scores

app = Flask(__name__)

@app.route("/")
def home():
    return render_template("home.html")

@app.route("/about")
def about():
    return render_template("about.html")

@app.route("/scoreboard")
def score():
    scores = get_scores()
    return render_template("scoreboard.html", scores = scores)

@app.route("/scoreboard/<int:player_id>")
def player(player_id):
    scores = get_scores()
    for score in scores:
        if score["id"] == str(player_id):
            return render_template("player.html", score = score)

@app.route("/units")
def units():
    return render_template("units.html")

if __name__ == "__main__":
    port = int(os.environ.get("PORT", 5000))
    app.debug = True
    app.run(host='0.0.0.0', port=port)


import random
import shutil
from collections import defaultdict
from itertools import chain
from pathlib import Path
from typing import Set, Tuple, List, Dict

import Levenshtein
from tqdm import tqdm

from paraphone.tasks.dictionaries import DictionaryCSV
from paraphone.tasks.filters.base import FilteringTaskMixin, CandidatesPairCSV, WordPair, CorpusFinalFilteringTask
from paraphone.tasks.phonemize import PhonemizedWordsCSV
from paraphone.tasks.tokenize import TokenizedWordsCSV
from paraphone.tasks.wuggy_gen import FakeWordsCandidatesCSV
from paraphone.utils import logger
from paraphone.workspace import Workspace

class InitFilteringTask(FilteringTaskMixin):
    requires = [
        "wuggy/candidates.csv",
    ]

    creates = [
        "candidates_filtering/steps/",
        "candidates_filtering/steps/step_1_init.csv"
    ]

    def run(self, workspace: Workspace):
        steps_folder = workspace.candidates_filtering / Path("steps")
        # cleaning up folder in case of re-initing
        shutil.rmtree(steps_folder, ignore_errors=True)
        steps_folder.mkdir(parents=True, exist_ok=True)

        wuggy_candidates_csv = FakeWordsCandidatesCSV(workspace.wuggy / Path("candidates.csv"))
        step_init_csv = CandidatesPairCSV(steps_folder / Path("step_1_init.csv"))

        logger.info(f"Initializing filtering pipeline with {wuggy_candidates_csv.file_path}")
        with step_init_csv.dict_writer as dict_writer:
            dict_writer.writeheader()
            for word, word_pho, _, fake_word_pho, _ in tqdm(wuggy_candidates_csv,
                                                            total=wuggy_candidates_csv.lines_count):
                dict_writer.writerow({
                    "word": word,
                    "word_pho": " ".join(word_pho),
                    "fake_word_pho": " ".join(fake_word_pho)
                })

class PassThroughFinalFilter(CorpusFinalFilteringTask):
    """Just copies the last step of the filtering pipeline into
    the respective corpora"""

    def run_for_corpus(self, workspace: Workspace, corpus_id: int):
        self.filter(workspace, corpus_id)

class RandomFilterTask(FilteringTaskMixin):
    """Keeps a random split of the candidates determined by ratio."""
    step_name = "random"

    def __init__(self, ratio: float):
        super().__init__()
        assert 0 < ratio < 1.0
        self.ratio = ratio

    def keep_pair(self, word_pair: WordPair) -> bool:
        return random.random() < self.ratio

    def run(self, workspace: Workspace):
        random.seed(4577)
        self.filter(workspace)

class RandomPairFilterTask(FilteringTaskMixin):
    """Keeps only one of the word/nonword pairs, at random"""
    step_name = "random-pairs"

    def __init__(self):
        super().__init__()
        self._chosen_pairs: Set[Tuple[str, str]] = set()

    def keep_pair(self, word_pair: WordPair) -> bool:
        return (word_pair.word_pho, word_pair.fake_word_pho) in self._chosen_pairs

    def run(self, workspace: Workspace):
        random.seed(4577)

        previous_step_csv = self.previous_step_csv(workspace)
        word_nonword = defaultdict(list)  # word -> list(nonwords)
        for _, word_pho, fake_word_pho in tqdm(previous_step_csv):
            word_nonword[word_pho].append(fake_word_pho)

        for word, fake_words in tqdm(word_nonword.items()):
            chosen_fake_word = random.choice(fake_words)
            self._chosen_pairs.add((word, chosen_fake_word))
        self.filter(workspace)

class EqualsFilterTask(FilteringTaskMixin):
    """Filters out equal phonetic pairs"""
    step_name = "equals"

    def keep_pair(self, word_pair: WordPair) -> bool:
        return word_pair.word_pho != word_pair.fake_word_pho

    def run(self, workspace: Workspace):
        self.filter(workspace)

class MostFrequentHomophoneFilterTask(FilteringTaskMixin):
    """Filter homophones based on grapheme frequency (the grapheme with
    the largest frequency is the one that "wins" and other graphemic forms
    are filtered out) """
    requires = [
        "datasets/tokenized/all.csv"
    ]
    step_name = "homophones"

    def __init__(self):
        super().__init__()
        # phonetic forms and the frequency of the word that is associated with it
        # for two words with the same word_pho (homophones), only the one
        # with the most frequent occurences is kept
        self.word_pho_freq: Dict[str, int] = defaultdict(int)
        # word_pho -> word
        self.kept_word_pho: Dict[str, str] = dict()
        self.kept_words: Set[str] = set()

    def keep_pair(self, word_pair: WordPair) -> bool:
        return word_pair.word in self.kept_words

    def run(self, workspace: Workspace):
        tokenized_words_csv = TokenizedWordsCSV(
            workspace.tokenized / Path("all.csv")
        )
        tokenized_words = tokenized_words_csv.to_dict()
        previous_step_csv = self.previous_step_csv(workspace)
        for word, word_pho, _ in previous_step_csv:
            if self.word_pho_freq[word_pho] < tokenized_words[word]:
                self.word_pho_freq[word_pho] = tokenized_words[word]
                self.kept_word_pho[word_pho] = word

        self.kept_words = set(self.kept_word_pho.values())
        self.filter(workspace)

class WuggyHomophonesFilterTask(FilteringTaskMixin):
    """Filters out fake word whose phonetic form matches that of a real word."""
    requires = [
        "dictionaries/*/dict_folded.csv",
        "phonemized/all.csv"
    ]
    step_name = "wuggy-homophones"

    def __init__(self):
        super().__init__()
        self.all_words_phonemized: Set[str] = set()

    def keep_pair(self, word_pair: WordPair) -> bool:
        return word_pair.fake_word_pho not in self.all_words_phonemized

    def run(self, workspace: Workspace):
        self.all_words_phonemized = set()
        for dict_filepath in workspace.dictionaries.glob("**/dict_folded.csv"):
            dict_csv = DictionaryCSV(dict_filepath)
            self.all_words_phonemized.update({
                " ".join(pho) for _, pho, _ in dict_csv
            })
        phonemized_words_csv = PhonemizedWordsCSV(workspace.phonemized / Path("all.csv"))
        self.all_words_phonemized.update({
            " ".join(pho) for _, pho in phonemized_words_csv
        })

        self.filter(workspace)

class LevenshteinFilterTask(FilteringTaskMixin):
    """Keeps pairs whose edit distance is lower than a threshold"""
    step_name = "levenshtein"

    def __init__(self, max_distance: int):
        super().__init__()
        assert max_distance > 0
        self.max_distance = max_distance

    def pho_to_str(self, pho_a: List[str], pho_b: List[str]) -> Tuple[str, str]:
        """Remaps phoneme lists to ASCII strings for levenshtein edit distance"""
        pho_set = set(chain.from_iterable([pho_a, pho_b]))
        pho_map = {pho: chr(i) for i, pho in enumerate(pho_set)}
        return "".join(pho_map[pho] for pho in pho_a), "".join(pho_map[pho] for pho in pho_b)

    def keep_pair(self, word_pair: WordPair) -> bool:
        word_pho, fake_word_pho = self.pho_to_str(word_pair.word_pho.split(" "),
                                                  word_pair.fake_word_pho.split(" "))
        return Levenshtein.distance(word_pho, fake_word_pho) <= self.max_distance

    def run(self, workspace: Workspace):
        self.filter(workspace)


"""Autocompletion functionality of epigraphdb entities
"""
from typing import Any, Callable, List

import requests
from elasticsearch import Elasticsearch
from elasticsearch.helpers import bulk

from app.settings import api_key, api_url
from app.utils import api_request_headers, batch_by_n
from app.utils.logging import logger

from .indexers import name_indexer_edge_ngram_long

def index_data(
    input_data: List[Any],
    index_name: str,
    es_client: Elasticsearch,
    indexer_fn: Callable,
    batch_size: int = 500,
    overwrite: bool = True,
) -> None:
    logger.info(f"Creating the index {index_name}.")
    indexer_fn(index_name=index_name, overwrite=overwrite, es_client=es_client)

    # NOTE: batch_data is a generator
    batch_data = batch_by_n(collection=input_data, batch_size=batch_size)
    for batch_docs in batch_data:
        requests = [
            dict(**{"_op_type": "index", "_index": index_name}, **doc)
            for doc in batch_docs
        ]
        bulk(es_client, requests)

    es_client.indices.refresh(index=index_name, request_timeout=300)
    logger.info("Done indexing.")

def index_ac_name_from_cypher(
    cypher_query: str,
    index_name: str,
    es_client: Elasticsearch,
    indexer_fn: Callable = name_indexer_edge_ngram_long,
    overwrite: bool = False,
) -> bool:
    """Index entity names from upstream cypher query.

    Cypher results need to return a data structure like:
    [
       { "name": ... },
       { "name": ... },
    ]
    """
    if not es_client.indices.exists(index=index_name) or overwrite:
        cypher_url = f"{api_url}/raw_cypher/"
        r = requests.get(
            cypher_url,
            params={"query": cypher_query, "api_key": api_key},
            headers=api_request_headers,
        )
        r.raise_for_status()
        input_data = [dict(name=item["name"]) for item in r.json()["results"]]
        index_data(
            input_data=input_data,
            index_name=index_name,
            es_client=es_client,
            indexer_fn=indexer_fn,
        )
    return True


class Solution:
    def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
        length = len(flowerbed)
        if length == 1:
            if flowerbed[0] == 0:
                return True

        if flowerbed[0] == 0 and flowerbed[1] == 0:
            flowerbed[0] = 1
            n -= 1
            #print(flowerbed)
            #print(n)

        for i in range(1,length-1):
            if flowerbed[i] == 0:
                if flowerbed[i-1] == 0 and flowerbed[i+1] == 0:
                    flowerbed[i] = 1
                    n -= 1
                #print(flowerbed)

        if flowerbed[length-1] == 0 and flowerbed[length-2] == 0:
            flowerbed[length-1] = 1
            n -= 1
            #print(flowerbed)
            #print(n)

        if n <= 0:
            return True
        return False


from pprint import pprint

def compute_solution(x, y, dp):
    sol = ""
    n = len(x) - 1
    m = len(y) - 1

    while n:
        if x[n] == y[m]:
            sol = x[n] + sol
            n -= 1
            m -= 1
        elif dp[n - 1][m] < dp[n][m - 1]:
            m -= 1
        else:
            n -= 1

    return sol

def compute_matrix(x, y):

    dp = [[0 for _ in range(len(x) + 1)] for _ in range(len(y) + 1)]

    for i in range(1, len(x)):
        for j in range(1, len(y)):
            if x[i] == y[j]:
                dp[i][j] = dp[i - 1][j - 1] + 1
            else:
                dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
            pprint(dp)
            print("\n")

    return dp

def solve():
    x = " " + input()
    y = " " + input()

    dp = compute_matrix(x, y)

    print("The length of the longest common subsequence of the arrays is ",
          dp[len(x) - 1][len(y) - 1])
    print("One of the longest common subsequences of the arrays is ",
          compute_solution(x, y, dp))

solve()

1st Semester/Fundamentals of Programming/Lab Assignments/A4/dynamic.py


import telegramModule
import os
import sys
import time
from datetime import date
from datetime import timedelta
import sqlite3

import hiworks

# 환경설정 파일 읽기.
exefilePath = os.path.dirname(sys.argv[0])
dbFile = exefilePath+'/holyday.db'

# 텔래그램 아이디를 가지고 오자.
notiGroupTelegramId = os.environ.get("TELEGRAM_GROUP_ID_OF_ADMIN_FOR_HIWORKS_NOTI")
# notiGroupTelegramId = os.environ.get("TELEGRAM_ID_OF_ADMIN_FOR_HIWORKS_NOTI")

# 세션 아이디를 가지고 오자.
sessionid = os.environ.get("SESSION_ID_FOR_HIWORKS_NOTI")
# 로그인 해더를 추가한다.
hiworks.requestHeader = {"Cookie": "PHPSESSID={0};".format(sessionid),
             "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36"
             }

#검사해야 하는 이름
user1Name = os.environ.get("USER_1_NAME_FOR_HIWORKS_NOTI")
user2Name = os.environ.get("USER_2_NAME_FOR_HIWORKS_NOTI")

holyday = set()

# 모든 주말이 아닌 휴일을 holyday 전역 변수에 담는다.
def getHolydayList():
    conn = sqlite3.connect(dbFile)
    # Connection 으로부터 Cursor 생성
    cur = conn.cursor()

    # SQL 쿼리 실행
    cur.execute("select strDate from holyday")

    # 데이타 Fetch
    rows = cur.fetchall()
    for row in rows:
        holyday.add(row[0])

    # Connection 닫기
    conn.close()

# 전달받은 날짜가 일하는 날인지 판단한다.
# 전달 받은 객채가 date객채가 아니면 항상 False를 반환한다.
def getWorkingDay(d):
    if type(d) is not date:
        return False

    if d.weekday() == 5 or d.weekday() == 6:
        return False

    tempDateString = d.strftime("%Y.%m.%d")
    if tempDateString in holyday:
        return False

    return True

# 오늘 이 일하는 날이라면..
# 다음 일하는 날을 골라서 휴가자를 확인하여 전달한다.
def sendVacationUser():
    #휴일을 일단 불러온다.
    getHolydayList()

    oneDay = timedelta(days=1)

    t = date.today()

    targetDateString = ""

    newFileWrites = False

    # 오늘이 일하는 날이라면...
    if getWorkingDay(t):
        #메세지를 보내야 한다.
        #다음 일하는 날짜를 찾는다.
        while targetDateString == "":
            # 하루 추가.
            t = t + oneDay
            #일하는 날짜라면..
            if getWorkingDay(t):
                #목표날짜로 지정.
                targetDateString = t.strftime("%Y.%m.%d")

        print("휴가자 검사중... 검사날짜 : {0}".format(targetDateString))
        #while 문이 끝났다면 목표날짜를 찾음...
        #목표 날짜의 휴가자를 모두 구함.
        (total, month, day, userList) = hiworks.getTargetDayVacationUserList(targetDateString)

        # 휴가자가 있다면..
        if int(total) > 0:
            # 텔레그램으로 전달할 메세지를 작성하기 시작한다.
            resultString = "{0} {1} 총 휴가자 {2}명 내역입니다. \n\n".format(month,day,total)
            # 가나다 순으로 정렬하자 ... 개발팀이 가장 먼저 온다.
            userList.sort()
            # 각각을 개행으로 구분하여 써준다.
            for team, name, duration, approvalStatus in userList:
                if duration != "종일":
                    resultString += f"{team} {name}({duration}) : {approvalStatus}\n"
                else:
                    resultString += f"{team} {name} : {approvalStatus}\n"

                if duration == "종일" or duration == "오전":
                    if user1Name == name:
                        newFileWrites = True
                        vacFilePath = exefilePath+"/user1.vac"
                        with open(vacFilePath, 'w') as fp:
                            fp.write(targetDateString)

                    if user2Name == name:
                        newFileWrites = True
                        vacFilePath = exefilePath+"/user2.vac"
                        with open(vacFilePath, 'w') as fp:
                            fp.write(targetDateString)

            # 텔레그램으로 전달.
            telegramModule.sendMessage(notiGroupTelegramId, resultString)
            print("휴가자 전달\n전달 텔레그램 번호 : {0}\n{1}".format(notiGroupTelegramId,resultString))

        print("휴가자 검사끝... 검사날짜 : {0}".format(targetDateString))

    return newFileWrites

newFileWrites = sendVacationUser()
print(f"::set-output name=new_file::{newFileWrites}")


from enums import *
from scope import *
from randomprob import random_with_prob

def expression_to_string(expr):
    if isinstance(expr, Variable):
        return str(expr)
    else:
        return '({})'.format(expr)

class Statement:
    def __init__(self, scope: Scope, return_type: DataType):
        super().__init__()
        self.return_type = return_type
        self.scope = scope

    def get_return_type(self):
        return self.return_type

    @staticmethod
    def random(scope, return_type: DataType = None, max_depth = 5, current_depth = 0, whitelist = None):

        if whitelist == None:
            statement_type = random_with_prob('binary', 60, 'unary', 40)

            if statement_type == 'binary':
                return BinaryStatement.random(scope, return_type, max_depth = max_depth, current_depth = current_depth)
            elif statement_type == 'unary':
                return UnaryStatement.random(scope, return_type, max_depth = max_depth, current_depth = current_depth)

        else:
            probs = []

            if BinaryStatement in whitelist:
                probs.extend(['binary', 10])

            if UnaryStatement in whitelist:
                probs.extend(['unary', 10])

            if AssignmentStatement in whitelist:
                probs.extend(['assignment', 40])

            if ForStatement in whitelist:
                probs.extend(['for', 10])

            if WhileStatement in whitelist:
                probs.extend(['while', 10])

            if IfStatement in whitelist:
                probs.extend(['if', 10])

            '''
            if ReturnStatement in whitelist:
                probs.extend(['return', 10])
            '''

            p = random_with_prob(probs)

            if p == 'binary':
                return BinaryStatement.random(scope, return_type, max_depth = max_depth, current_depth = current_depth)
            if p == 'unary':
                return UnaryStatement.random(scope, return_type, max_depth = max_depth, current_depth = current_depth)

            if p == 'assignment':
                return AssignmentStatement.random(scope)

            if p == 'for':
                return ForStatement.random(scope)
            if p == 'while':
                return WhileStatement.random(scope)
            if p == 'if':
                return IfStatement.random(scope)
            if p == 'return':
                return ReturnStatement.random(scope, return_type)

# <operator> <statement>
class UnaryStatement(Statement):

    def __init__(self, operator, expression):
        self.operator = operator
        self.expression = expression

    def __str__(self):
        if self.operator == UnaryOperator.MINUS:
            return '-{}'.format(expression_to_string(self.expression))

        elif self.operator == UnaryOperator.PREFIX_DECREMENT:
            return '--{}'.format(expression_to_string(self.expression))

        elif self.operator == UnaryOperator.POSTFIX_DECREMENT:
            return '{}--'.format(expression_to_string(self.expression))

        elif self.operator == UnaryOperator.PREFIX_INCREMENT:
            return '++{}'.format(expression_to_string(self.expression))

        elif self.operator == UnaryOperator.POSTFIX_INCREMENT:
            return '{}++'.format(expression_to_string(self.expression))

        elif self.operator == UnaryOperator.NOT:
            return '!{}'.format(expression_to_string(self.expression))

        elif self.operator == UnaryOperator.ADDRESS_OF:
            return '&{}'.format(expression_to_string(self.expression))

        elif self.operator == UnaryOperator.BITWISE_NOT:
            return '~{}'.format(expression_to_string(self.expression))

        elif self.operator == UnaryOperator.ADDRESS_OF:
            return '&{}'.format(expression_to_string(self.expression))

        elif self.operator == UnaryOperator.SIZE_OF:
            return 'sizeof({})'.format(expression_to_string(self.expression))

    def get_return_type(self):
        if self.operator == UnaryOperator.SIZE_OF or self.operator == UnaryOperator.ADDRESS_OF:
            return DataType.INT
        else:
            return self.expression.get_return_type()

    @staticmethod
    def random(scope: Scope, return_type, max_depth = 5, current_depth = 0):
        e = None

        if current_depth == max_depth:
            e = Constant.get_random(DataType.get_random_data_type())
        else:
            e = scope.get_random_value(return_type, max_depth = max_depth, current_depth = current_depth + 1)

        return UnaryStatement(UnaryOperator.random(exp = e), e)

# <statement> <operator> <statement>
class BinaryStatement(Statement):
    def __init__(self, exp1, operator, exp2):
        self.exp1 = exp1
        self.operator = operator
        self.exp2 = exp2

    def __str__(self):

        exp1 = expression_to_string(self.exp1)
        exp2 = expression_to_string(self.exp2)

        if self.operator == BinaryOperators.BITWISE_AND:
            return '{} & {}'.format(exp1, exp2)

        elif self.operator == BinaryOperators.BITWISE_OR:
            return '{} | {}'.format(exp1, exp2)

        elif self.operator == BinaryOperators.BITWISE_XOR:
            return '{} ^ {}'.format(exp1, exp2)

        elif self.operator == BinaryOperators.LEFT_SHIFT:
            return '{} << {}'.format(exp1, exp2)

        elif self.operator == BinaryOperators.RIGHT_SHIFT:
            return '{} >> {}'.format(exp1, exp2)

        elif self.operator == BinaryOperators.ADD:
            return '{} + {}'.format(exp1, exp2)

        elif self.operator == BinaryOperators.SUBTRACT:
            return '{} - {}'.format(exp1, exp2)

        elif self.operator == BinaryOperators.MULTIPLY:
            return '{} * {}'.format(exp1, exp2)

        elif self.operator == BinaryOperators.DIVIDE:
            return '{} / {}'.format(exp1, exp2)

        elif self.operator == BinaryOperators.MODULO:
            return '{} % {}'.format(exp1, exp2)
        elif self.operator == BinaryOperators.LESS_THAN:
            return '{} < {}'.format(exp1, exp2)
        elif self.operator == BinaryOperators.GREATER_THAN:
            return '{} > {}'.format(exp1, exp2)
        elif self.operator == BinaryOperators.EQUAL:
            return '{} == {}'.format(exp1, exp2)
        elif self.operator == BinaryOperators.LOGICAL_AND:
            return '{} && {}'.format(exp1, exp2)
        elif self.operator == BinaryOperators.LOGICAL_OR:
            return '{} || {}'.format(exp1, exp2)

    def get_return_type(self):

        if self.operator == BinaryOperators.DIVIDE:
            return DataType.FLOAT

        if self.exp2.get_return_type() == self.exp1.get_return_type():
            return self.exp1.get_return_type()

        if self.exp1.get_return_type().is_float() or self.exp2.get_return_type().is_float():
            return DataType.FLOAT

        if self.exp1.get_return_type().is_int() or self.exp2.get_return_type().is_int():
            return DataType.INT

        else:
            return self.exp1.get_return_type()

    @staticmethod
    def random(scope, return_type = None, max_depth = 5, current_depth = 0):

        exp1 = scope.get_random_value(max_depth = max_depth, current_depth = current_depth)
        exp2 = scope.get_random_value(max_depth = max_depth, current_depth = current_depth)

        op = BinaryOperators.get_random(exp1, exp2, return_type)

        return BinaryStatement(exp1, op, exp2)

# <variable> = <statement>
class AssignmentStatement(Statement):
    def __init__(self, variable, value, op = ''):
        self.variable = variable
        self.value = value
        self.op = op

    def __str__(self):
        return "{} {}= {}".format(self.variable, self.op, self.value)

    @staticmethod
    def random(scope: Scope):
        v: Variable = None

        var_type = random_with_prob('new',  60, 'existing', 40)

        if var_type == 'new':
            v = Variable("var" + str(scope.get_num_variables()), DataType.get_random_data_type())
            scope.add_to_scope(v)
        else:
            v = scope.get_random_variable()

            if v == None:
                v = Variable("var" + str(scope.get_num_variables()), DataType.get_random_data_type())
                scope.add_to_scope(v)

        right = scope.get_random_value(v.datatype, blacklist = [v])

        return AssignmentStatement(v, right)

#return <statement>
class ReturnStatement(Statement):
    def __init__(self, return_value):
        self.return_value = return_value

    def __str__(self):
        return 'return {}'.format(self.return_value)

    @staticmethod
    def random(scope: Scope, of_type: DataType):
        return ReturnStatement(scope.get_random_value(of_type, no_consts = True))

#if (<statement>; <statement>; <statement>)
class IfStatement(Statement):
    def __init__(self, scope, condition):
        self.scope = Scope(scope)
        self.else_scope = Scope(scope)
        self.condition = condition
        self.statements = []
        self.else_statements = []

    def add_statement(self, statement: Statement):
        self.statements.append(statement)

    def add_else_statement(self, statement: Statement):
        self.else_statements.append(statement)

    def add_random_statement(self):
        self.add_statement(Statement.random(self.scope))

    def __str__(self):
        s = 'if({})'.format(self.condition)

        s += '{\n'

        for statement in self.statements:
            s += str(statement) + ';\n'

        s+= '}'

        if len(self.else_statements) > 0:
            s += '\nelse{\n'
            for statement in self.else_statements:
                s += str(statement) + ';\n'
            s += '}'

        return s

    @staticmethod
    def random(scope: Scope):

        s = Scope(scope)

        var = s.get_random_value()

        cond = None

        d = random_with_prob('var', 10, 'unary', 2, 'binary', 20)

        if d == 'var':
            cond = var
        elif d == 'unary':
            cond = UnaryStatement(UnaryOperator.random(exp=var), var)
        elif d == 'binary':
            cond = BinaryStatement(var, BinaryOperators.get_random_comparison(), s.get_random_value(var.get_return_type()))

        return IfStatement(s, cond)

class ForStatement(Statement):
    def __init__(self, scope, pre, check, post):
        self.scope = Scope(scope)
        self.pre = pre
        self.check = check
        self.post = post
        self.statements = []

    def __str__(self):
        s = 'for({}; {}; {})'.format(self.pre, self.check, self.post)

        s += '{\n'

        for statement in self.statements:
            s += '{};\n'.format(statement)

        s += '}'

        return s

    def add_statement(self, statement):
        self.statements.append(statement)

    @staticmethod
    def random(scope: Scope):

        s = Scope(scope)

        pre = AssignmentStatement.random(s)
        check = BinaryStatement(pre.variable, BinaryOperators.get_random_comparison(), s.get_random_value(pre.variable.datatype, blacklist = [pre.variable]))
        post = None

        post_type = random_with_prob('unary', 50, 'binary', 50)

        if post_type == 'unary':
            post = UnaryStatement(UnaryOperator.random(exp = pre.variable), pre.variable)
        elif post_type == 'binary':

            op = random_with_prob('+', 10, '-', 10, '*', 5, '', 2)

            post = AssignmentStatement(pre.variable, s.get_random_value(pre.variable.datatype, blacklist = [pre.variable]), op = op)

        return ForStatement(s, pre, check, post)

class WhileStatement(Statement):
    def __init__(self, scope, cond):
        self.cond = cond
        self.statements = []
        self.scope = Scope(scope)

    def __str__(self):
        s = 'while ({})'.format(self.cond)

        s += '{\n'

        for statement in self.statements:
            s += '{};\n'.format(statement)

        s += '}'

        return s

    def add_statement(self, statement):
        self.statements.append(statement)

    @staticmethod
    def random(scope: Scope):
        return WhileStatement(scope, scope.get_random_value())

class SwitchStatement(Statement):
    def __init__(self):
        super().__init__()

class CaseStatement(Statement):
    def __init__(self):
        super().__init__()


import numpy as np
import pandas as pd
from subprocess import check_output
#print(check_output(["ls", "../input"]).decode("utf8"))
import datetime
import time
train = pd.read_csv('../input/train.csv', dtype={'is_booking':bool,'srch_destination_id':np.int32, 'hotel_cluster':np.int32, 'srch_children_cnt':np.int32,'srch_adults_cnt':np.int32,'srch_destination_type_id':np.int32, 'hotel_cluster':np.int32,'orig_destination_distance':np.float64},
                    usecols=['date_time','srch_ci','srch_co','srch_destination_id','is_booking','srch_children_cnt','srch_adults_cnt','srch_destination_type_id','hotel_cluster','orig_destination_distance'], chunksize=1000000)
train = [ ]
for train2 in train:
    train2["srch_ci"] = pd.to_datetime(train2["srch_ci"], format='%Y-%m-%d', errors="coerce")
    train2["srch_co"] = pd.to_datetime(train2["srch_co"], format='%Y-%m-%d', errors="coerce")
    train2["stay_span"] = (train2["srch_co"] - train2["srch_ci"]).astype('timedelta64[D]')
    #train2 = train2.drop('srch_co', axis=1)
    train2["date_time"] = pd.to_datetime(train2["date_time"], format='%Y-%m-%d', errors="coerce")
    train2["search_span"] = (train2["srch_ci"] - train2["date_time"]).astype('timedelta64[D]')
    #train2 = train2.drop('srch_ci', axis=1)
    train2['year'] = train2['date_time'].dt.year
    train2['month'] = train2['date_time'].dt.month
    train2['day_of_week'] = train2['date_time'].dt.dayofweek
    train2['hour'] = train2['date_time'].dt.hour
    #train2 = train2.drop('date_time', axis=1)
    train2.ix[(train2['hour'] >= 10) & (train2['hour'] < 18), 'hour'] = 1
    train2.ix[(train2['hour'] >= 18) & (train2['hour'] < 22), 'hour'] = 2
    train2.ix[(train2['hour'] >= 22) & (train2['hour'] == 24), 'hour'] = 3
    train2.ix[(train2['hour'] >= 1) & (train2['hour'] < 10), 'hour'] = 3
    #train2['Individuals'] = train2['srch_adults_cnt']+train2['srch_children_cnt']
    #train2 = train2.drop('srch_adults_cnt', axis=1)
    #train2 = train2.drop('srch_children_cnt', axis=1)
    #train2 = train2.drop('search_span', axis=1)
    #train2 = train2.drop('user_location_city', axis=1)
    #train2 = train2.drop('hotel_country', axis=1)
    train2 = train2[['orig_destination_distance','srch_destination_id','srch_destination_type_id','is_booking','hotel_cluster','stay_span','search_span','month','day_of_week','hour', 'srch_adults_cnt', 'srch_children_cnt']]
    #agg = train2.groupby(['srch_destination_id','srch_destination_type_id','hotel_cluster','day_of_week','hour', 'srch_adults_cnt', 'srch_children_cnt'], sort = False)['is_booking'].agg(['sum','count'])
    train2.reset_index(inplace=True)
    train.append(train2)
train = pd.DataFrame(train)
train.head()


import argparse
import logging
from concurrent import futures

import grpc
import info_pb2
import info_pb2_grpc
from transformers import pipeline, set_seed

class TextGenerationServiceServicer(info_pb2_grpc.TextGenerationServiceServicer):
    def __init__(self):
        set_seed(42)
        self.generator = pipeline("text-generation", model="gpt2")

    def GenerateText(self, request, context):
        text =  self.generator(request.text, max_length=30, num_return_sequences=1)[0][
            "generated_text"
        ]
        return info_pb2.Message(text=text)

def serve(args):
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    info_pb2_grpc.add_TextGenerationServiceServicer_to_server(
        TextGenerationServiceServicer(), server)
    address = f'{args.ip}:{args.port}'
    port = server.add_insecure_port(address)
    print(f'serving on port {port}')
    server.start()
    server.wait_for_termination()

if __name__ == '__main__':
    logging.basicConfig()

    parser = argparse.ArgumentParser(description="Parse arguments")
    parser.add_argument("--ip", type = str, default="[::]", help="port")
    parser.add_argument("--port", type = str, default="50051", help="port")
    args = parser.parse_args()

    serve(args)


import math
import random

class cell:

    def __init__(self, loc, is_alive) -> None:
        self.loc = loc
        self.is_alive = is_alive
        self.neighbors = self.get_neighbors()
        self.will_live = False
        self.cycles_wout_change = 0
        self.avg_age = 5

    def get_neighbors(self):
        """ returns a list of the coordinates of neighboring cells """
        neighbors = [(1,0),(-1,0),(0,1),(0,-1)]
        return [(self.loc[0]+x[0], self.loc[1]+x[1]) for x in neighbors]

    def chance_death(self):
        """ returns true if the cell dies by random chance/old age """
        rand_num = random.random()
        x = 0.05*self.cycles_wout_change
        return rand_num < 0.5*(1 + math.erf(x - self.avg_age))


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

import base64
import json
import os
import os.path
import shutil
import socket
import sys
import tarfile
import tempfile
import unittest

import pytest
import six

from docker.api.client import APIClient
from docker.constants import IS_WINDOWS_PLATFORM
from docker.errors import DockerException
from docker.utils import (
    parse_repository_tag, parse_host, convert_filters, kwargs_from_env,
    parse_bytes, parse_env_file, exclude_paths, convert_volume_binds,
    decode_json_header, tar, split_command, parse_devices, update_headers,
)

from docker.utils.build import should_check_directory
from docker.utils.ports import build_port_bindings, split_port
from docker.utils.utils import format_environment

from ..helpers import make_tree

TEST_CERT_DIR = os.path.join(
    os.path.dirname(__file__),
    'testdata/certs',
)

class DecoratorsTest(unittest.TestCase):
    def test_update_headers(self):
        sample_headers = {
            'X-Docker-Locale': 'en-US',
        }

        def f(self, headers=None):
            return headers

        client = APIClient()
        client._general_configs = {}

        g = update_headers(f)
        assert g(client, headers=None) is None
        assert g(client, headers={}) == {}
        assert g(client, headers={'Content-type': 'application/json'}) == {
            'Content-type': 'application/json',
        }

        client._general_configs = {
            'HttpHeaders': sample_headers
        }

        assert g(client, headers=None) == sample_headers
        assert g(client, headers={}) == sample_headers
        assert g(client, headers={'Content-type': 'application/json'}) == {
            'Content-type': 'application/json',
            'X-Docker-Locale': 'en-US',
        }

class KwargsFromEnvTest(unittest.TestCase):
    def setUp(self):
        self.os_environ = os.environ.copy()

    def tearDown(self):
        os.environ = self.os_environ

    def test_kwargs_from_env_empty(self):
        os.environ.update(DOCKER_HOST='',
                          DOCKER_CERT_PATH='')
        os.environ.pop('DOCKER_TLS_VERIFY', None)

        kwargs = kwargs_from_env()
        assert kwargs.get('base_url') is None
        assert kwargs.get('tls') is None

    def test_kwargs_from_env_tls(self):
        os.environ.update(DOCKER_HOST='tcp://192.168.59.103:2376',
                          DOCKER_CERT_PATH=TEST_CERT_DIR,
                          DOCKER_TLS_VERIFY='1')
        kwargs = kwargs_from_env(assert_hostname=False)
        assert 'https://192.168.59.103:2376' == kwargs['base_url']
        assert 'ca.pem' in kwargs['tls'].ca_cert
        assert 'cert.pem' in kwargs['tls'].cert[0]
        assert 'key.pem' in kwargs['tls'].cert[1]
        assert kwargs['tls'].assert_hostname is False
        assert kwargs['tls'].verify
        try:
            client = APIClient(**kwargs)
            assert kwargs['base_url'] == client.base_url
            assert kwargs['tls'].ca_cert == client.verify
            assert kwargs['tls'].cert == client.cert
        except TypeError as e:
            self.fail(e)

    def test_kwargs_from_env_tls_verify_false(self):
        os.environ.update(DOCKER_HOST='tcp://192.168.59.103:2376',
                          DOCKER_CERT_PATH=TEST_CERT_DIR,
                          DOCKER_TLS_VERIFY='')
        kwargs = kwargs_from_env(assert_hostname=True)
        assert 'https://192.168.59.103:2376' == kwargs['base_url']
        assert 'ca.pem' in kwargs['tls'].ca_cert
        assert 'cert.pem' in kwargs['tls'].cert[0]
        assert 'key.pem' in kwargs['tls'].cert[1]
        assert kwargs['tls'].assert_hostname is True
        assert kwargs['tls'].verify is False
        try:
            client = APIClient(**kwargs)
            assert kwargs['base_url'] == client.base_url
            assert kwargs['tls'].cert == client.cert
            assert not kwargs['tls'].verify
        except TypeError as e:
            self.fail(e)

    def test_kwargs_from_env_tls_verify_false_no_cert(self):
        temp_dir = tempfile.mkdtemp()
        cert_dir = os.path.join(temp_dir, '.docker')
        shutil.copytree(TEST_CERT_DIR, cert_dir)

        os.environ.update(DOCKER_HOST='tcp://192.168.59.103:2376',
                          HOME=temp_dir,
                          DOCKER_TLS_VERIFY='')
        os.environ.pop('DOCKER_CERT_PATH', None)
        kwargs = kwargs_from_env(assert_hostname=True)
        assert 'tcp://192.168.59.103:2376' == kwargs['base_url']

    def test_kwargs_from_env_no_cert_path(self):
        try:
            temp_dir = tempfile.mkdtemp()
            cert_dir = os.path.join(temp_dir, '.docker')
            shutil.copytree(TEST_CERT_DIR, cert_dir)

            os.environ.update(HOME=temp_dir,
                              DOCKER_CERT_PATH='',
                              DOCKER_TLS_VERIFY='1')

            kwargs = kwargs_from_env()
            assert kwargs['tls'].verify
            assert cert_dir in kwargs['tls'].ca_cert
            assert cert_dir in kwargs['tls'].cert[0]
            assert cert_dir in kwargs['tls'].cert[1]
        finally:
            if temp_dir:
                shutil.rmtree(temp_dir)

    def test_kwargs_from_env_alternate_env(self):
        # Values in os.environ are entirely ignored if an alternate is
        # provided
        os.environ.update(
            DOCKER_HOST='tcp://192.168.59.103:2376',
            DOCKER_CERT_PATH=TEST_CERT_DIR,
            DOCKER_TLS_VERIFY=''
        )
        kwargs = kwargs_from_env(environment={
            'DOCKER_HOST': 'http://docker.gensokyo.jp:2581',
        })
        assert 'http://docker.gensokyo.jp:2581' == kwargs['base_url']
        assert 'tls' not in kwargs

class ConverVolumeBindsTest(unittest.TestCase):
    def test_convert_volume_binds_empty(self):
        assert convert_volume_binds({}) == []
        assert convert_volume_binds([]) == []

    def test_convert_volume_binds_list(self):
        data = ['/a:/a:ro', '/b:/c:z']
        assert convert_volume_binds(data) == data

    def test_convert_volume_binds_complete(self):
        data = {
            '/mnt/vol1': {
                'bind': '/data',
                'mode': 'ro'
            }
        }
        assert convert_volume_binds(data) == ['/mnt/vol1:/data:ro']

    def test_convert_volume_binds_compact(self):
        data = {
            '/mnt/vol1': '/data'
        }
        assert convert_volume_binds(data) == ['/mnt/vol1:/data:rw']

    def test_convert_volume_binds_no_mode(self):
        data = {
            '/mnt/vol1': {
                'bind': '/data'
            }
        }
        assert convert_volume_binds(data) == ['/mnt/vol1:/data:rw']

    def test_convert_volume_binds_unicode_bytes_input(self):
        expected = [u'/mnt/지연:/unicode/박:rw']

        data = {
            u'/mnt/지연'.encode('utf-8'): {
                'bind': u'/unicode/박'.encode('utf-8'),
                'mode': 'rw'
            }
        }
        assert convert_volume_binds(data) == expected

    def test_convert_volume_binds_unicode_unicode_input(self):
        expected = [u'/mnt/지연:/unicode/박:rw']

        data = {
            u'/mnt/지연': {
                'bind': u'/unicode/박',
                'mode': 'rw'
            }
        }
        assert convert_volume_binds(data) == expected

class ParseEnvFileTest(unittest.TestCase):
    def generate_tempfile(self, file_content=None):
        """
        Generates a temporary file for tests with the content
        of 'file_content' and returns the filename.
        Don't forget to unlink the file with os.unlink() after.
        """
        local_tempfile = tempfile.NamedTemporaryFile(delete=False)
        local_tempfile.write(file_content.encode('UTF-8'))
        local_tempfile.close()
        return local_tempfile.name

    def test_parse_env_file_proper(self):
        env_file = self.generate_tempfile(
            file_content='USER=jdoe\nPASS=secret')
        get_parse_env_file = parse_env_file(env_file)
        assert get_parse_env_file == {'USER': 'jdoe', 'PASS': 'secret'}
        os.unlink(env_file)

    def test_parse_env_file_with_equals_character(self):
        env_file = self.generate_tempfile(
            file_content='USER=jdoe\nPASS=sec==ret')
        get_parse_env_file = parse_env_file(env_file)
        assert get_parse_env_file == {'USER': 'jdoe', 'PASS': 'sec==ret'}
        os.unlink(env_file)

    def test_parse_env_file_commented_line(self):
        env_file = self.generate_tempfile(
            file_content='USER=jdoe\n#PASS=secret')
        get_parse_env_file = parse_env_file(env_file)
        assert get_parse_env_file == {'USER': 'jdoe'}
        os.unlink(env_file)

    def test_parse_env_file_newline(self):
        env_file = self.generate_tempfile(
            file_content='\nUSER=jdoe\n\n\nPASS=secret')
        get_parse_env_file = parse_env_file(env_file)
        assert get_parse_env_file == {'USER': 'jdoe', 'PASS': 'secret'}
        os.unlink(env_file)

    def test_parse_env_file_invalid_line(self):
        env_file = self.generate_tempfile(
            file_content='USER jdoe')
        with pytest.raises(DockerException):
            parse_env_file(env_file)
        os.unlink(env_file)

class ParseHostTest(unittest.TestCase):
    def test_parse_host(self):
        invalid_hosts = [
            '0.0.0.0',
            'tcp://',
            'udp://127.0.0.1',
            'udp://127.0.0.1:2375',
        ]

        valid_hosts = {
            '0.0.0.1:5555': 'http://0.0.0.1:5555',
            ':6666': 'http://127.0.0.1:6666',
            'tcp://:7777': 'http://127.0.0.1:7777',
            'http://:7777': 'http://127.0.0.1:7777',
            'https://kokia.jp:2375': 'https://kokia.jp:2375',
            'unix:///var/run/docker.sock': 'http+unix:///var/run/docker.sock',
            'unix://': 'http+unix://var/run/docker.sock',
            '12.234.45.127:2375/docker/engine': (
                'http://12.234.45.127:2375/docker/engine'
            ),
            'somehost.net:80/service/swarm': (
                'http://somehost.net:80/service/swarm'
            ),
            'npipe:////./pipe/docker_engine': 'npipe:////./pipe/docker_engine',
            '[fd12::82d1]:2375': 'http://[fd12::82d1]:2375',
            'https://[fd12:5672::12aa]:1090': 'https://[fd12:5672::12aa]:1090',
            '[fd12::82d1]:2375/docker/engine': (
                'http://[fd12::82d1]:2375/docker/engine'
            ),
        }

        for host in invalid_hosts:
            with pytest.raises(DockerException):
                parse_host(host, None)

        for host, expected in valid_hosts.items():
            assert parse_host(host, None) == expected

    def test_parse_host_empty_value(self):
        unix_socket = 'http+unix://var/run/docker.sock'
        npipe = 'npipe:////./pipe/docker_engine'

        for val in [None, '']:
            assert parse_host(val, is_win32=False) == unix_socket
            assert parse_host(val, is_win32=True) == npipe

    def test_parse_host_tls(self):
        host_value = 'myhost.docker.net:3348'
        expected_result = 'https://myhost.docker.net:3348'
        assert parse_host(host_value, tls=True) == expected_result

    def test_parse_host_tls_tcp_proto(self):
        host_value = 'tcp://myhost.docker.net:3348'
        expected_result = 'https://myhost.docker.net:3348'
        assert parse_host(host_value, tls=True) == expected_result

    def test_parse_host_trailing_slash(self):
        host_value = 'tcp://myhost.docker.net:2376/'
        expected_result = 'http://myhost.docker.net:2376'
        assert parse_host(host_value) == expected_result

class ParseRepositoryTagTest(unittest.TestCase):
    sha = 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'

    def test_index_image_no_tag(self):
        assert parse_repository_tag("root") == ("root", None)

    def test_index_image_tag(self):
        assert parse_repository_tag("root:tag") == ("root", "tag")

    def test_index_user_image_no_tag(self):
        assert parse_repository_tag("user/repo") == ("user/repo", None)

    def test_index_user_image_tag(self):
        assert parse_repository_tag("user/repo:tag") == ("user/repo", "tag")

    def test_private_reg_image_no_tag(self):
        assert parse_repository_tag("url:5000/repo") == ("url:5000/repo", None)

    def test_private_reg_image_tag(self):
        assert parse_repository_tag("url:5000/repo:tag") == (
            "url:5000/repo", "tag"
        )

    def test_index_image_sha(self):
        assert parse_repository_tag("root@sha256:{0}".format(self.sha)) == (
            "root", "sha256:{0}".format(self.sha)
        )

    def test_private_reg_image_sha(self):
        assert parse_repository_tag(
            "url:5000/repo@sha256:{0}".format(self.sha)
        ) == ("url:5000/repo", "sha256:{0}".format(self.sha))

class ParseDeviceTest(unittest.TestCase):
    def test_dict(self):
        devices = parse_devices([{
            'PathOnHost': '/dev/sda1',
            'PathInContainer': '/dev/mnt1',
            'CgroupPermissions': 'r'
        }])
        assert devices[0] == {
            'PathOnHost': '/dev/sda1',
            'PathInContainer': '/dev/mnt1',
            'CgroupPermissions': 'r'
        }

    def test_partial_string_definition(self):
        devices = parse_devices(['/dev/sda1'])
        assert devices[0] == {
            'PathOnHost': '/dev/sda1',
            'PathInContainer': '/dev/sda1',
            'CgroupPermissions': 'rwm'
        }

    def test_permissionless_string_definition(self):
        devices = parse_devices(['/dev/sda1:/dev/mnt1'])
        assert devices[0] == {
            'PathOnHost': '/dev/sda1',
            'PathInContainer': '/dev/mnt1',
            'CgroupPermissions': 'rwm'
        }

    def test_full_string_definition(self):
        devices = parse_devices(['/dev/sda1:/dev/mnt1:r'])
        assert devices[0] == {
            'PathOnHost': '/dev/sda1',
            'PathInContainer': '/dev/mnt1',
            'CgroupPermissions': 'r'
        }

    def test_hybrid_list(self):
        devices = parse_devices([
            '/dev/sda1:/dev/mnt1:rw',
            {
                'PathOnHost': '/dev/sda2',
                'PathInContainer': '/dev/mnt2',
                'CgroupPermissions': 'r'
            }
        ])

        assert devices[0] == {
            'PathOnHost': '/dev/sda1',
            'PathInContainer': '/dev/mnt1',
            'CgroupPermissions': 'rw'
        }
        assert devices[1] == {
            'PathOnHost': '/dev/sda2',
            'PathInContainer': '/dev/mnt2',
            'CgroupPermissions': 'r'
        }

class ParseBytesTest(unittest.TestCase):
    def test_parse_bytes_valid(self):
        assert parse_bytes("512MB") == 536870912
        assert parse_bytes("512M") == 536870912
        assert parse_bytes("512m") == 536870912

    def test_parse_bytes_invalid(self):
        with pytest.raises(DockerException):
            parse_bytes("512MK")
        with pytest.raises(DockerException):
            parse_bytes("512L")
        with pytest.raises(DockerException):
            parse_bytes("127.0.0.1K")

    def test_parse_bytes_float(self):
        with pytest.raises(DockerException):
            parse_bytes("1.5k")

    def test_parse_bytes_maxint(self):
        assert parse_bytes("{0}k".format(sys.maxsize)) == sys.maxsize * 1024

class UtilsTest(unittest.TestCase):
    longMessage = True

    def test_convert_filters(self):
        tests = [
            ({'dangling': True}, '{"dangling": ["true"]}'),
            ({'dangling': "true"}, '{"dangling": ["true"]}'),
            ({'exited': 0}, '{"exited": [0]}'),
            ({'exited': [0, 1]}, '{"exited": [0, 1]}'),
        ]

        for filters, expected in tests:
            assert convert_filters(filters) == expected

    def test_decode_json_header(self):
        obj = {'a': 'b', 'c': 1}
        data = None
        if six.PY3:
            data = base64.urlsafe_b64encode(bytes(json.dumps(obj), 'utf-8'))
        else:
            data = base64.urlsafe_b64encode(json.dumps(obj))
        decoded_data = decode_json_header(data)
        assert obj == decoded_data

class SplitCommandTest(unittest.TestCase):
    def test_split_command_with_unicode(self):
        assert split_command(u'echo μμ') == ['echo', 'μμ']

    @pytest.mark.skipif(six.PY3, reason="shlex doesn't support bytes in py3")
    def test_split_command_with_bytes(self):
        assert split_command('echo μμ') == ['echo', 'μμ']

class PortsTest(unittest.TestCase):
    def test_split_port_with_host_ip(self):
        internal_port, external_port = split_port("127.0.0.1:1000:2000")
        assert internal_port == ["2000"]
        assert external_port == [("127.0.0.1", "1000")]

    def test_split_port_with_protocol(self):
        internal_port, external_port = split_port("127.0.0.1:1000:2000/udp")
        assert internal_port == ["2000/udp"]
        assert external_port == [("127.0.0.1", "1000")]

    def test_split_port_with_host_ip_no_port(self):
        internal_port, external_port = split_port("127.0.0.1::2000")
        assert internal_port == ["2000"]
        assert external_port == [("127.0.0.1", None)]

    def test_split_port_range_with_host_ip_no_port(self):
        internal_port, external_port = split_port("127.0.0.1::2000-2001")
        assert internal_port == ["2000", "2001"]
        assert external_port == [("127.0.0.1", None), ("127.0.0.1", None)]

    def test_split_port_with_host_port(self):
        internal_port, external_port = split_port("1000:2000")
        assert internal_port == ["2000"]
        assert external_port == ["1000"]

    def test_split_port_range_with_host_port(self):
        internal_port, external_port = split_port("1000-1001:2000-2001")
        assert internal_port == ["2000", "2001"]
        assert external_port == ["1000", "1001"]

    def test_split_port_random_port_range_with_host_port(self):
        internal_port, external_port = split_port("1000-1001:2000")
        assert internal_port == ["2000"]
        assert external_port == ["1000-1001"]

    def test_split_port_no_host_port(self):
        internal_port, external_port = split_port("2000")
        assert internal_port == ["2000"]
        assert external_port is None

    def test_split_port_range_no_host_port(self):
        internal_port, external_port = split_port("2000-2001")
        assert internal_port == ["2000", "2001"]
        assert external_port is None

    def test_split_port_range_with_protocol(self):
        internal_port, external_port = split_port(
            "127.0.0.1:1000-1001:2000-2001/udp")
        assert internal_port == ["2000/udp", "2001/udp"]
        assert external_port == [("127.0.0.1", "1000"), ("127.0.0.1", "1001")]

    def test_split_port_with_ipv6_address(self):
        internal_port, external_port = split_port(
            "2001:abcd:ef00::2:1000:2000")
        assert internal_port == ["2000"]
        assert external_port == [("2001:abcd:ef00::2", "1000")]

    def test_split_port_invalid(self):
        with pytest.raises(ValueError):
            split_port("0.0.0.0:1000:2000:tcp")

    def test_non_matching_length_port_ranges(self):
        with pytest.raises(ValueError):
            split_port("0.0.0.0:1000-1010:2000-2002/tcp")

    def test_port_and_range_invalid(self):
        with pytest.raises(ValueError):
            split_port("0.0.0.0:1000:2000-2002/tcp")

    def test_port_only_with_colon(self):
        with pytest.raises(ValueError):
            split_port(":80")

    def test_host_only_with_colon(self):
        with pytest.raises(ValueError):
            split_port("localhost:")

    def test_with_no_container_port(self):
        with pytest.raises(ValueError):
            split_port("localhost:80:")

    def test_split_port_empty_string(self):
        with pytest.raises(ValueError):
            split_port("")

    def test_split_port_non_string(self):
        assert split_port(1243) == (['1243'], None)

    def test_build_port_bindings_with_one_port(self):
        port_bindings = build_port_bindings(["127.0.0.1:1000:1000"])
        assert port_bindings["1000"] == [("127.0.0.1", "1000")]

    def test_build_port_bindings_with_matching_internal_ports(self):
        port_bindings = build_port_bindings(
            ["127.0.0.1:1000:1000", "127.0.0.1:2000:1000"])
        assert port_bindings["1000"] == [
            ("127.0.0.1", "1000"), ("127.0.0.1", "2000")
        ]

    def test_build_port_bindings_with_nonmatching_internal_ports(self):
        port_bindings = build_port_bindings(
            ["127.0.0.1:1000:1000", "127.0.0.1:2000:2000"])
        assert port_bindings["1000"] == [("127.0.0.1", "1000")]
        assert port_bindings["2000"] == [("127.0.0.1", "2000")]

    def test_build_port_bindings_with_port_range(self):
        port_bindings = build_port_bindings(["127.0.0.1:1000-1001:1000-1001"])
        assert port_bindings["1000"] == [("127.0.0.1", "1000")]
        assert port_bindings["1001"] == [("127.0.0.1", "1001")]

    def test_build_port_bindings_with_matching_internal_port_ranges(self):
        port_bindings = build_port_bindings(
            ["127.0.0.1:1000-1001:1000-1001", "127.0.0.1:2000-2001:1000-1001"])
        assert port_bindings["1000"] == [
            ("127.0.0.1", "1000"), ("127.0.0.1", "2000")
        ]
        assert port_bindings["1001"] == [
            ("127.0.0.1", "1001"), ("127.0.0.1", "2001")
        ]

    def test_build_port_bindings_with_nonmatching_internal_port_ranges(self):
        port_bindings = build_port_bindings(
            ["127.0.0.1:1000:1000", "127.0.0.1:2000:2000"])
        assert port_bindings["1000"] == [("127.0.0.1", "1000")]
        assert port_bindings["2000"] == [("127.0.0.1", "2000")]

def convert_paths(collection):
    return set(map(convert_path, collection))

def convert_path(path):
    return path.replace('/', os.path.sep)

class ExcludePathsTest(unittest.TestCase):
    dirs = [
        'foo',
        'foo/bar',
        'bar',
        'target',
        'target/subdir',
        'subdir',
        'subdir/target',
        'subdir/target/subdir',
        'subdir/subdir2',
        'subdir/subdir2/target',
        'subdir/subdir2/target/subdir'
    ]

    files = [
        'Dockerfile',
        'Dockerfile.alt',
        '.dockerignore',
        'a.py',
        'a.go',
        'b.py',
        'cde.py',
        'foo/a.py',
        'foo/b.py',
        'foo/bar/a.py',
        'bar/a.py',
        'foo/Dockerfile3',
        'target/file.txt',
        'target/subdir/file.txt',
        'subdir/file.txt',
        'subdir/target/file.txt',
        'subdir/target/subdir/file.txt',
        'subdir/subdir2/file.txt',
        'subdir/subdir2/target/file.txt',
        'subdir/subdir2/target/subdir/file.txt',
    ]

    all_paths = set(dirs + files)

    def setUp(self):
        self.base = make_tree(self.dirs, self.files)

    def tearDown(self):
        shutil.rmtree(self.base)

    def exclude(self, patterns, dockerfile=None):
        return set(exclude_paths(self.base, patterns, dockerfile=dockerfile))

    def test_no_excludes(self):
        assert self.exclude(['']) == convert_paths(self.all_paths)

    def test_no_dupes(self):
        paths = exclude_paths(self.base, ['!a.py'])
        assert sorted(paths) == sorted(set(paths))

    def test_wildcard_exclude(self):
        assert self.exclude(['*']) == set(['Dockerfile', '.dockerignore'])

    def test_exclude_dockerfile_dockerignore(self):
        """
        Even if the .dockerignore file explicitly says to exclude
        Dockerfile and/or .dockerignore, don't exclude them from
        the actual tar file.
        """
        assert self.exclude(['Dockerfile', '.dockerignore']) == convert_paths(
            self.all_paths
        )

    def test_exclude_custom_dockerfile(self):
        """
        If we're using a custom Dockerfile, make sure that's not
        excluded.
        """
        assert self.exclude(['*'], dockerfile='Dockerfile.alt') == set(
            ['Dockerfile.alt', '.dockerignore']
        )

        assert self.exclude(
            ['*'], dockerfile='foo/Dockerfile3'
        ) == convert_paths(set(['foo/Dockerfile3', '.dockerignore']))

    def test_exclude_dockerfile_child(self):
        includes = self.exclude(['foo/'], dockerfile='foo/Dockerfile3')
        assert convert_path('foo/Dockerfile3') in includes
        assert convert_path('foo/a.py') not in includes

    def test_single_filename(self):
        assert self.exclude(['a.py']) == convert_paths(
            self.all_paths - set(['a.py'])
        )

    def test_single_filename_leading_dot_slash(self):
        assert self.exclude(['./a.py']) == convert_paths(
            self.all_paths - set(['a.py'])
        )

    # As odd as it sounds, a filename pattern with a trailing slash on the
    # end *will* result in that file being excluded.
    def test_single_filename_trailing_slash(self):
        assert self.exclude(['a.py/']) == convert_paths(
            self.all_paths - set(['a.py'])
        )

    def test_wildcard_filename_start(self):
        assert self.exclude(['*.py']) == convert_paths(
            self.all_paths - set(['a.py', 'b.py', 'cde.py'])
        )

    def test_wildcard_with_exception(self):
        assert self.exclude(['*.py', '!b.py']) == convert_paths(
            self.all_paths - set(['a.py', 'cde.py'])
        )

    def test_wildcard_with_wildcard_exception(self):
        assert self.exclude(['*.*', '!*.go']) == convert_paths(
            self.all_paths - set([
                'a.py', 'b.py', 'cde.py', 'Dockerfile.alt',
            ])
        )

    def test_wildcard_filename_end(self):
        assert self.exclude(['a.*']) == convert_paths(
            self.all_paths - set(['a.py', 'a.go'])
        )

    def test_question_mark(self):
        assert self.exclude(['?.py']) == convert_paths(
            self.all_paths - set(['a.py', 'b.py'])
        )

    def test_single_subdir_single_filename(self):
        assert self.exclude(['foo/a.py']) == convert_paths(
            self.all_paths - set(['foo/a.py'])
        )

    def test_single_subdir_single_filename_leading_slash(self):
        assert self.exclude(['/foo/a.py']) == convert_paths(
            self.all_paths - set(['foo/a.py'])
        )

    def test_single_subdir_with_path_traversal(self):
        assert self.exclude(['foo/whoops/../a.py']) == convert_paths(
            self.all_paths - set(['foo/a.py'])
        )

    def test_single_subdir_wildcard_filename(self):
        assert self.exclude(['foo/*.py']) == convert_paths(
            self.all_paths - set(['foo/a.py', 'foo/b.py'])
        )

    def test_wildcard_subdir_single_filename(self):
        assert self.exclude(['*/a.py']) == convert_paths(
            self.all_paths - set(['foo/a.py', 'bar/a.py'])
        )

    def test_wildcard_subdir_wildcard_filename(self):
        assert self.exclude(['*/*.py']) == convert_paths(
            self.all_paths - set(['foo/a.py', 'foo/b.py', 'bar/a.py'])
        )

    def test_directory(self):
        assert self.exclude(['foo']) == convert_paths(
            self.all_paths - set([
                'foo', 'foo/a.py', 'foo/b.py', 'foo/bar', 'foo/bar/a.py',
                'foo/Dockerfile3'
            ])
        )

    def test_directory_with_trailing_slash(self):
        assert self.exclude(['foo']) == convert_paths(
            self.all_paths - set([
                'foo', 'foo/a.py', 'foo/b.py',
                'foo/bar', 'foo/bar/a.py', 'foo/Dockerfile3'
            ])
        )

    def test_directory_with_single_exception(self):
        assert self.exclude(['foo', '!foo/bar/a.py']) == convert_paths(
            self.all_paths - set([
                'foo/a.py', 'foo/b.py', 'foo', 'foo/bar',
                'foo/Dockerfile3'
            ])
        )

    def test_directory_with_subdir_exception(self):
        assert self.exclude(['foo', '!foo/bar']) == convert_paths(
            self.all_paths - set([
                'foo/a.py', 'foo/b.py', 'foo', 'foo/Dockerfile3'
            ])
        )

    @pytest.mark.skipif(
        not IS_WINDOWS_PLATFORM, reason='Backslash patterns only on Windows'
    )
    def test_directory_with_subdir_exception_win32_pathsep(self):
        assert self.exclude(['foo', '!foo\\bar']) == convert_paths(
            self.all_paths - set([
                'foo/a.py', 'foo/b.py', 'foo', 'foo/Dockerfile3'
            ])
        )

    def test_directory_with_wildcard_exception(self):
        assert self.exclude(['foo', '!foo/*.py']) == convert_paths(
            self.all_paths - set([
                'foo/bar', 'foo/bar/a.py', 'foo', 'foo/Dockerfile3'
            ])
        )

    def test_subdirectory(self):
        assert self.exclude(['foo/bar']) == convert_paths(
            self.all_paths - set(['foo/bar', 'foo/bar/a.py'])
        )

    @pytest.mark.skipif(
        not IS_WINDOWS_PLATFORM, reason='Backslash patterns only on Windows'
    )
    def test_subdirectory_win32_pathsep(self):
        assert self.exclude(['foo\\bar']) == convert_paths(
            self.all_paths - set(['foo/bar', 'foo/bar/a.py'])
        )

    def test_double_wildcard(self):
        assert self.exclude(['**/a.py']) == convert_paths(
            self.all_paths - set(
                ['a.py', 'foo/a.py', 'foo/bar/a.py', 'bar/a.py']
            )
        )

        assert self.exclude(['foo/**/bar']) == convert_paths(
            self.all_paths - set(['foo/bar', 'foo/bar/a.py'])
        )

    def test_single_and_double_wildcard(self):
        assert self.exclude(['**/target/*/*']) == convert_paths(
            self.all_paths - set(
                ['target/subdir/file.txt',
                 'subdir/target/subdir/file.txt',
                 'subdir/subdir2/target/subdir/file.txt']
            )
        )

    def test_trailing_double_wildcard(self):
        assert self.exclude(['subdir/**']) == convert_paths(
            self.all_paths - set(
                ['subdir/file.txt',
                 'subdir/target/file.txt',
                 'subdir/target/subdir/file.txt',
                 'subdir/subdir2/file.txt',
                 'subdir/subdir2/target/file.txt',
                 'subdir/subdir2/target/subdir/file.txt',
                 'subdir/target',
                 'subdir/target/subdir',
                 'subdir/subdir2',
                 'subdir/subdir2/target',
                 'subdir/subdir2/target/subdir']
            )
        )

    def test_exclude_include_absolute_path(self):
        base = make_tree([], ['a.py', 'b.py'])
        assert exclude_paths(
            base,
            ['/*', '!' + os.path.join(base, '*.py')]
        ) == set(['a.py', 'b.py'])

class TarTest(unittest.TestCase):
    def test_tar_with_excludes(self):
        dirs = [
            'foo',
            'foo/bar',
            'bar',
        ]

        files = [
            'Dockerfile',
            'Dockerfile.alt',
            '.dockerignore',
            'a.py',
            'a.go',
            'b.py',
            'cde.py',
            'foo/a.py',
            'foo/b.py',
            'foo/bar/a.py',
            'bar/a.py',
        ]

        exclude = [
            '*.py',
            '!b.py',
            '!a.go',
            'foo',
            'Dockerfile*',
            '.dockerignore',
        ]

        expected_names = set([
            'Dockerfile',
            '.dockerignore',
            'a.go',
            'b.py',
            'bar',
            'bar/a.py',
        ])

        base = make_tree(dirs, files)
        self.addCleanup(shutil.rmtree, base)

        with tar(base, exclude=exclude) as archive:
            tar_data = tarfile.open(fileobj=archive)
            assert sorted(tar_data.getnames()) == sorted(expected_names)

    def test_tar_with_empty_directory(self):
        base = tempfile.mkdtemp()
        self.addCleanup(shutil.rmtree, base)
        for d in ['foo', 'bar']:
            os.makedirs(os.path.join(base, d))
        with tar(base) as archive:
            tar_data = tarfile.open(fileobj=archive)
            assert sorted(tar_data.getnames()) == ['bar', 'foo']

    @pytest.mark.skipif(
        IS_WINDOWS_PLATFORM or os.geteuid() == 0,
        reason='root user always has access ; no chmod on Windows'
    )
    def test_tar_with_inaccessible_file(self):
        base = tempfile.mkdtemp()
        full_path = os.path.join(base, 'foo')
        self.addCleanup(shutil.rmtree, base)
        with open(full_path, 'w') as f:
            f.write('content')
        os.chmod(full_path, 0o222)
        with pytest.raises(IOError) as ei:
            tar(base)

        assert 'Can not read file in context: {}'.format(full_path) in (
            ei.exconly()
        )

    @pytest.mark.skipif(IS_WINDOWS_PLATFORM, reason='No symlinks on Windows')
    def test_tar_with_file_symlinks(self):
        base = tempfile.mkdtemp()
        self.addCleanup(shutil.rmtree, base)
        with open(os.path.join(base, 'foo'), 'w') as f:
            f.write("content")
        os.makedirs(os.path.join(base, 'bar'))
        os.symlink('../foo', os.path.join(base, 'bar/foo'))
        with tar(base) as archive:
            tar_data = tarfile.open(fileobj=archive)
            assert sorted(tar_data.getnames()) == ['bar', 'bar/foo', 'foo']

    @pytest.mark.skipif(IS_WINDOWS_PLATFORM, reason='No symlinks on Windows')
    def test_tar_with_directory_symlinks(self):
        base = tempfile.mkdtemp()
        self.addCleanup(shutil.rmtree, base)
        for d in ['foo', 'bar']:
            os.makedirs(os.path.join(base, d))
        os.symlink('../foo', os.path.join(base, 'bar/foo'))
        with tar(base) as archive:
            tar_data = tarfile.open(fileobj=archive)
            assert sorted(tar_data.getnames()) == ['bar', 'bar/foo', 'foo']

    @pytest.mark.skipif(IS_WINDOWS_PLATFORM, reason='No symlinks on Windows')
    def test_tar_with_broken_symlinks(self):
        base = tempfile.mkdtemp()
        self.addCleanup(shutil.rmtree, base)
        for d in ['foo', 'bar']:
            os.makedirs(os.path.join(base, d))

        os.symlink('../baz', os.path.join(base, 'bar/foo'))
        with tar(base) as archive:
            tar_data = tarfile.open(fileobj=archive)
            assert sorted(tar_data.getnames()) == ['bar', 'bar/foo', 'foo']

    @pytest.mark.skipif(IS_WINDOWS_PLATFORM, reason='No UNIX sockets on Win32')
    def test_tar_socket_file(self):
        base = tempfile.mkdtemp()
        self.addCleanup(shutil.rmtree, base)
        for d in ['foo', 'bar']:
            os.makedirs(os.path.join(base, d))
        sock = socket.socket(socket.AF_UNIX)
        self.addCleanup(sock.close)
        sock.bind(os.path.join(base, 'test.sock'))
        with tar(base) as archive:
            tar_data = tarfile.open(fileobj=archive)
            assert sorted(tar_data.getnames()) == ['bar', 'foo']

    def tar_test_negative_mtime_bug(self):
        base = tempfile.mkdtemp()
        filename = os.path.join(base, 'th.txt')
        self.addCleanup(shutil.rmtree, base)
        with open(filename, 'w') as f:
            f.write('Invisible Full Moon')
        os.utime(filename, (12345, -3600.0))
        with tar(base) as archive:
            tar_data = tarfile.open(fileobj=archive)
            assert tar_data.getnames() == ['th.txt']
            assert tar_data.getmember('th.txt').mtime == -3600

class ShouldCheckDirectoryTest(unittest.TestCase):
    exclude_patterns = [
        'exclude_rather_large_directory',
        'dir/with/subdir_excluded',
        'dir/with/exceptions'
    ]

    include_patterns = [
        'dir/with/exceptions/like_this_one',
        'dir/with/exceptions/in/descendents'
    ]

    def test_should_check_directory_not_excluded(self):
        assert should_check_directory(
            'not_excluded', self.exclude_patterns, self.include_patterns, '.'
        )
        assert should_check_directory(
            convert_path('dir/with'), self.exclude_patterns,
            self.include_patterns, '.'
        )

    def test_shoud_check_parent_directories_of_excluded(self):
        assert should_check_directory(
            'dir', self.exclude_patterns, self.include_patterns, '.'
        )
        assert should_check_directory(
            convert_path('dir/with'), self.exclude_patterns,
            self.include_patterns, '.'
        )

    def test_should_not_check_excluded_directories_with_no_exceptions(self):
        assert not should_check_directory(
            'exclude_rather_large_directory', self.exclude_patterns,
            self.include_patterns, '.'
        )
        assert not should_check_directory(
            convert_path('dir/with/subdir_excluded'), self.exclude_patterns,
            self.include_patterns, '.'
        )

    def test_should_check_excluded_directory_with_exceptions(self):
        assert should_check_directory(
            convert_path('dir/with/exceptions'), self.exclude_patterns,
            self.include_patterns, '.'
        )
        assert should_check_directory(
            convert_path('dir/with/exceptions/in'), self.exclude_patterns,
            self.include_patterns, '.'
        )

    def test_should_not_check_siblings_of_exceptions(self):
        assert not should_check_directory(
            convert_path('dir/with/exceptions/but_not_here'),
            self.exclude_patterns, self.include_patterns, '.'
        )

    def test_should_check_subdirectories_of_exceptions(self):
        assert should_check_directory(
            convert_path('dir/with/exceptions/like_this_one/subdir'),
            self.exclude_patterns, self.include_patterns, '.'
        )

class FormatEnvironmentTest(unittest.TestCase):
    def test_format_env_binary_unicode_value(self):
        env_dict = {
            'ARTIST_NAME': b'\xec\x86\xa1\xec\xa7\x80\xec\x9d\x80'
        }
        assert format_environment(env_dict) == [u'ARTIST_NAME=송지은']

    def test_format_env_no_value(self):
        env_dict = {
            'FOO': None,
            'BAR': '',
        }
        assert sorted(format_environment(env_dict)) == ['BAR=', 'FOO']


from aocd.models import Puzzle
import numpy as np
from collections import Counter

DAY = 3
YEAR = 2021

def main():
    puzzle = Puzzle(year=YEAR, day=DAY)
    puzzle.answer_a = solve_a(puzzle.input_data)
    puzzle.answer_b = solve_b(puzzle.input_data)

def parse_report(raw_input):
    return np.array([list(row) for row in raw_input.splitlines()]).astype(int)

def binary_to_int(binary):
    return int("".join(binary.astype(str)), 2)

def calculate_gamma_rate(report):
    column_sum = report.sum(axis=0)
    threshold = len(report) / 2
    column_sum[column_sum < threshold] = 0
    column_sum[column_sum > threshold] = 1
    return binary_to_int(column_sum)

def calculate_epsilon_rate(report):
    column_sum = report.sum(axis=0)
    threshold = len(report) / 2
    column_sum[column_sum < threshold] = 1
    column_sum[column_sum > threshold] = 0
    return binary_to_int(column_sum)

def calculate_oxygen_rating(report):
    candidates = report.copy()
    for i in range(len(report[0])):
        new_candidates = []
        digits = candidates[:, i]
        most_common_picks = Counter(digits).most_common(2)
        if most_common_picks[0][1] == most_common_picks[1][1]:
            most_common = 1
        else:
            most_common = most_common_picks[0][0]

        for candidate in candidates:
            if candidate[i] == most_common:
                new_candidates.append(candidate)

        if len(new_candidates) == 1:
            return binary_to_int(new_candidates[0])
        candidates = np.array(new_candidates)

def calculate_carbon_rating(report):
    candidates = report.copy()
    for i in range(len(report[0])):
        new_candidates = []
        digits = candidates[:, i]
        most_common_picks = Counter(digits).most_common(2)
        if most_common_picks[0][1] == most_common_picks[1][1]:
            least_common = 0
        else:
            least_common = most_common_picks[1][0]

        for candidate in candidates:
            if candidate[i] == least_common:
                new_candidates.append(candidate)

        if len(new_candidates) == 1:
            return binary_to_int(new_candidates[0])
        candidates = np.array(new_candidates)

def solve_a(puzzle_input):
    report = parse_report(puzzle_input)
    gamma = calculate_gamma_rate(report)
    epsilon = calculate_epsilon_rate(report)
    return gamma * epsilon

def solve_b(puzzle_input):
    report = parse_report(puzzle_input)
    oxygen = calculate_oxygen_rating(report)
    carbon = calculate_carbon_rating(report)
    return oxygen * carbon

if __name__ == "__main__":
    main()


'''
recv()  特征
* 如何连接的另一端断开连接，则recv立即返回空子串
* recv是从接受缓冲区取出内容，当缓冲区为空则阻塞
* recv如果一次接受不完缓冲区内容，下次会继续接收

send() 特征
* 如果发送的另外一段不存在则会产生Pipe Broken
* recv是向发送缓冲区发送内容，当缓冲区为满阻塞
'''

import socket

sockfd = socket.socket(socket.AF_INET,socket.SOCK_STREAM)   #AF_INET:ipv4协议

sockfd.bind(("0.0.0.0",9998))
sockfd.listen()

print("Waiting for connect...")
connfd,addr = sockfd.accept()
print("Connect from",addr)
print("开始对话：")
print()

while True:
    data = connfd.recv(1024)
    if data.decode("utf-8")=="q":
        connfd.send(b"qq")
        print("服务器关闭，Bye~~")
        break
    print("服务器接收消息：",data.decode("utf-8"))
    connfd.send(b"I have received your message")
connfd.close()

sockfd.close()


def insertion_sorter(list):
    for key_pos in range(1, len(list)):
        key_value = list[key_pos]
        scan_pos = key_pos - 1

        while (scan_pos >= 0) and (list[scan_pos] > key_value):
            list[scan_pos + 1] = list[scan_pos]
            scan_pos = scan_pos - 1

        list[scan_pos + 1] = key_value
    return list
file = open('world.txt')

lang_list = []
for line in file:
    line = line.strip()
    lang_list.append(line)
print(lang_list)

key = 'Russian'
i = 0
while i < len(lang_list) and lang_list[i] != key:
    i += 1
if i == len(lang_list):
    print('Language is not in the list')
else:
    print('%s is in index %s of the list' % (key, i))

sorted_list = insertion_sorter(lang_list)
for language in sorted_list:
    print(language)


# while문

'''
# 10952
두 정수 A와 B를 입력받은 다음, A+B를 출력하는 프로그램을 작성하시오.
입력은 여러 개의 테스트 케이스로 이루어져 있다.
각 테스트 케이스는 한 줄로 이루어져 있으며, 각 줄에 A와 B가 주어진다. (0 < A, B < 10)

입력의 마지막에는 0 두 개가 들어온다.
'''

while True:
    a,b = map(int,input().split())
    if a == 0 and b == 0:
        break
    else:
        print(a+b)

'''
# 10951

두 정수 A와 B를 입력받은 다음, A+B를 출력하는 프로그램을 작성하시오.
입력은 여러 개의 테스트 케이스로 이루어져 있다.

각 테스트 케이스는 한 줄로 이루어져 있으며, 각 줄에 A와 B가 주어진다. (0 < A, B < 10)
'''

while True:
    try:
        a,b = map(int, input().split())
        print(a+b)
    except:
        break

'''
# 1110

0보다 크거나 같고, 99보다 작거나 같은 정수가 주어질 때 다음과 같은 연산을 할 수 있다. 먼저 주어진 수가 10보다 작다면 앞에 0을 붙여 두 자리 수로 만들고, 각 자리의 숫자를 더한다. 그 다음, 주어진 수의 가장 오른쪽 자리 수와 앞에서 구한 합의 가장 오른쪽 자리 수를 이어 붙이면 새로운 수를 만들 수 있다. 다음 예를 보자.
26부터 시작한다. 2+6 = 8이다. 새로운 수는 68이다. 6+8 = 14이다. 새로운 수는 84이다. 8+4 = 12이다. 새로운 수는 42이다. 4+2 = 6이다. 새로운 수는 26이다.
위의 예는 4번만에 원래 수로 돌아올 수 있다. 따라서 26의 사이클의 길이는 4이다.
N이 주어졌을 때, N의 사이클의 길이를 구하는 프로그램을 작성하시오.

첫째 줄에 N이 주어진다. N은 0보다 크거나 같고, 99보다 작거나 같은 정수이다.

'''
result = int(input())
num = result
count = 0

while True:
  ten = num//10
  one = num%10
  res = int(ten)+int(one)
  count += 1
  num = int(str(num%10) + str(res%10))

  if (result == num):
    break
print(count)


"""
Functions that aid weighting the visibility data prior to imaging.

There are two classes of functions:
    - Changing the weight dependent on noise level or sample density or a combination
    - Tapering the weihght spatially to avoid effects of sharp edges or to emphasize a given scale size in the image

"""

__all__ = ["weight_visibility", "taper_visibility_gaussian", "taper_visibility_tukey"]

import logging

import numpy

from rascil import phyconst
from rascil.processing_components.griddata.gridding import (
    grid_blockvisibility_weight_to_griddata,
    griddata_blockvisibility_reweight,
)
from rascil.processing_components.griddata.operations import create_griddata_from_image
from rascil.processing_components.image.operations import image_is_canonical
from rascil.processing_components.util.array_functions import tukey_filter

log = logging.getLogger("rascil-logger")

def weight_visibility(vis, model, weighting="uniform", robustness=0.0, **kwargs):
    """Weight the visibility data

    This is done collectively so the weights are summed over all vis_lists and then
    corrected

    :param vis_list:
    :param model_imagelist: Model required to determine weighting parameters
    :param weighting: Type of weighting (uniform or robust or natural)
    :param robustness: Robustness parameter
    :param kwargs: Parameters for functions in graphs
    :return: Reweighted vis
    """

    assert image_is_canonical(model)

    griddata = create_griddata_from_image(
        model, polarisation_frame=vis.blockvisibility_acc.polarisation_frame
    )
    griddata, sumwt = grid_blockvisibility_weight_to_griddata(vis, griddata)
    vis = griddata_blockvisibility_reweight(
        vis, griddata, weighting=weighting, robustness=robustness
    )

    return vis

def weight_blockvisibility(vis, model, weighting="uniform", robustness=0.0, **kwargs):
    """Weight the visibility data

    This is done collectively so the weights are summed over all vis_lists and then
    corrected

    :param vis_list:
    :param model_imagelist: Model required to determine weighting parameters
    :param weighting: Type of weighting
    :param kwargs: Parameters for functions in graphs
    :return: List of vis_graphs
    """
    log.info("weight_blockvisibility: is deprecated, use weight_visibility")
    return weight_visibility(vis, model, weighting, robustness, **kwargs)

def taper_visibility_gaussian(vis, beam=None):
    """Taper the visibility weights

    These are cumulative. If You can reset the imaging_weights
    using :py:mod:`processing_components.imaging.weighting.weight_visibility`

    :param vis: blockvisibility with imaging_weight's to be tapered
    :param beam: desired resolution (Full width half maximum, radians)
    :return: visibility with imaging_weight column modified
    """
    # assert isinstance(vis, BlockVisibility), vis

    if beam is None:
        raise ValueError("Beam size not specified for Gaussian taper")

    # assert isinstance(vis, BlockVisibility), vis
    # See http://mathworld.wolfram.com/FourierTransformGaussian.html
    scale_factor = numpy.pi**2 * beam**2 / (4.0 * numpy.log(2.0))

    for chan, freq in enumerate(vis.frequency.data):
        wave = phyconst.c_m_s / freq
        uvdistsq = (
            vis.blockvisibility_acc.u.data**2 + vis.blockvisibility_acc.v.data**2
        ) / wave**2
        wt = numpy.exp(-scale_factor * uvdistsq)
        vis["imaging_weight"].data[..., chan, :] = (
            vis.blockvisibility_acc.flagged_imaging_weight[..., chan, :]
            * wt[..., numpy.newaxis]
        )

    return vis

def taper_visibility_tukey(vis, tukey=0.1):
    """Taper the visibility weights

    This algorithm is present in WSClean.

    See https://sourceforge.net/p/wsclean/wiki/Tapering

    tukey, a circular taper that smooths the outer edge set by -maxuv-l
    inner-tukey, a circular taper that smooths the inner edge set by -minuv-l
    edge-tukey, a square-shaped taper that smooths the edge set by the uv grid and -taper-edge.

    These are cumulative. If You can reset the imaging_weights
    using :py:mod:`processing_components.imaging.weighting.weight_visibility`

    :param vis: blockvisibility with imaging_weight's to be tapered
    :return: visibility with imaging_weight column modified
    """

    # assert isinstance(vis, BlockVisibility), vis

    oshape = vis["imaging_weight"].data[..., 0, 0].shape
    for chan, freq in enumerate(vis.frequency.data):
        wave = phyconst.c_m_s / freq
        uvdist = numpy.sqrt(
            vis.blockvisibility_acc.u.data**2 + vis.blockvisibility_acc.v.data**2
        )
        uvdist = uvdist.flatten() / wave
        uvdistmax = numpy.max(uvdist)
        uvdist /= uvdistmax
        wt = numpy.array([tukey_filter(uv, tukey) for uv in uvdist]).reshape(oshape)
        vis["imaging_weight"].data[..., chan, :] = (
            vis.blockvisibility_acc.flagged_imaging_weight[..., chan, :]
            * wt[..., numpy.newaxis]
        )

    return vis


# Demonstrating file capture
#
# Author: Roger Meier, 06-11-2020
# CoolTerm version: 1.8.0

import sys
import time
import CoolTerm
s = CoolTerm.CoolTermSocket()

# Get the ID of the first open window
ID = s.GetWindowID(0)
if ID < 0:
    print("No open windows")
    sys.exit()

# Start the file capture. Use a path with write access for this test.
FilePath = "/Users/Shared/capture.txt"
if not s.CaptureStart(ID, FilePath):
    print("Could not start capture")
    sys.exit()

# Open the serial port
if s.Connect(ID):

    # Turn on local echo
    s.SetParameter(ID, "LocalEcho", True)
    s.SetParameter(ID, "CaptureLocalEcho", True)
    s.SetParameter(ID, "CaptureWaitForTerminationString", False)

    # Send some data
    s.Write(ID, "Hello CoolTerm")

    # Stop the capture
    s.CaptureStop(ID)

    # Close the port
    s.Disconnect(ID)

    # Disconnect from CoolTerm
    s.Close()

else:
    print("Not Connected")

Laboratorio-Electronica-Analogica-Digital/Anexos/CoolTermWin64Bit/Scripting/Python/Examples/Example 3 - Capture received data to a text file.py


"""Dataclip test"""

import json
import time
import sys
from threading import Thread

from eventplayback import EventPlayback, PlaybackTarget
from dataclip_source import DataClipSource

class TestTarget(PlaybackTarget):
    def on_event(self, event):
        #print(json.dumps(event))
        pass

def main():
    if len(sys.argv) != 2:
        print("Usage: {} DATA_CLIP_CONFIG_FILE".format(sys.argv[0]))
        sys.exit(1)

    source = DataClipSource(sys.argv[1])
    event_playback = EventPlayback(TestTarget(), source)
    event_playback.start()
    while True:
        event_playback.tick()
        time.sleep(0.001)

if __name__ == "__main__":
    main()


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

import os
import time
import argparse
import numpy as np
import logging

import torch
from torchvision import models
from torchvision import transforms
from PIL import Image

import tensorrt as trt
import engine as eng
from onnx import ModelProto

import pycuda.driver as cuda
import pycuda.autoinit

# #### Directly convert keras model to TensorRT
def convert_tf_to_onnx():
    # tf2onnx module needs to be installed in order to use this function
    import onnx
    import tf2onnx
    img0, x = process_images('./data/img0.jpg')
    model_proto, external_tensor_storage = tf2onnx.convert.from_keras(model, input_signature=[tf.TensorSpec(shape=x.shape, dtype=tf.float32)])
    onnx.save(model_proto,'./resnet50_model_tf/resnet50_tf_onnx.onnx"')
    # ! python3 -m tf2onnx.convert --saved-model "./resnet50_model_tf" --output "./resnet50_model_tf/resnet50_tf_onnx.onnx"
    return

def convert_torch_to_onnx(model_name):
    # https://pytorch.org/docs/stable/onnx.html
    size= 224
    # Pre-process the image and convert into a tensor
    transform = transforms.Compose([
        transforms.Resize(size),
        transforms.CenterCrop(size),
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                        std=[0.229, 0.224, 0.225]),
    ])
    if model_name == "mobilenet_v2":
        trained_model = models.mobilenet_v2(pretrained=True)
    else:
        trained_model = models.resnet50(pretrained=True)

    img_path = './data/img0.jpg'
    img = Image.open(img_path).convert('RGB')
    dummy_input = transform(img).unsqueeze(0)
    onnx_output = 'model/' + model_name + '_torch_onnx.onnx'
    torch.onnx.export(trained_model, dummy_input, onnx_output)
    # torch.onnx.export(trained_model, dummy_input, onnx_output, dynamic_axes={"actual_input_1":{0:"batch_size"}, "output2":{0:"batch_size"}})

    # visualise onnx model
    # ! python3 -m pip install netron
    # http://localhost:8080/
    return

def process_images(img_path):
    # read input image
    input_img = Image.open(img_path).convert('RGB')

    # transform image for the input data
    preprocess = transforms.Compose([
            transforms.Resize(256),
            transforms.CenterCrop(224),
            transforms.ToTensor(),
            transforms.Normalize(
            mean=[0.485, 0.456, 0.406],
            std=[0.229, 0.224, 0.225]
        )])
    input_data = preprocess(input_img)
    batch_data = torch.unsqueeze(input_data, 0)
    return input_img, batch_data#.cuda()

def decode_predictions(out):
    # Load the file containing the 1,000 labels for the ImageNet dataset classes
    # imagenet label: https://gist.github.com/yrevar/942d3a0ac09ec9e5eb3a

    with open('./data/imagenet_classes.txt') as f:
        labels = [line.strip() for line in f.readlines()]

    # find the index (tensor) corresponding to the maximum score in the out tensor.
    out_list = out.tolist()
    max_value = max(out_list)
    max_index = out_list.index(max_value)

    return labels[max_index]

def predict_images(num_images, engine, h_input_1, d_input_1, h_output, d_output, stream):

    height, width = 224, 224
    time_list = []
    for i in range(num_images):
        img_path = 'data/img%d.jpg'%i
        input_img, batch_data = process_images(img_path)
        pics_1 = batch_data
        batch_size = 1
        # start timing # ========
        # start_time = time.time()
        out, inference_time = do_inference(engine, pics_1, h_input_1, d_input_1, h_output, d_output, stream, batch_size, height, width)
        # end_time = time.time()
        # end timing # ========

        # calculate inference time
        # inference_time = ( end_time - start_time ) * 1000
        time_list.append(inference_time)

        # decode the result
        decoded_result = decode_predictions(out)
        print('Image {}: {:4.1f}ms => {}'.format(i, inference_time, decoded_result))
    print ("median time: {} ms".format(np.median(time_list)))
    return

##############################

# build engine using onnx model
def build_engine_from_onnx(onnx_path, engine_name, batch_size, TRT_LOGGER):
    model = ModelProto()
    with open(onnx_path, "rb") as f:
        model.ParseFromString(f.read())

    d0 = model.graph.input[0].type.tensor_type.shape.dim[1].dim_value
    d1 = model.graph.input[0].type.tensor_type.shape.dim[2].dim_value
    d2 = model.graph.input[0].type.tensor_type.shape.dim[3].dim_value
    shape = [batch_size , d0, d1 ,d2]
    engine = eng.build_engine(TRT_LOGGER, onnx_path, shape= shape)
    eng.save_engine(engine, engine_name)
    return engine

# Running inference from the TensorRT engine:
def allocate_buffers(engine, batch_size, data_type):

    """
    This is the function to allocate buffers for input and output in the device
    Args:
        engine : The path to the TensorRT engine.
        batch_size : The batch size for execution time.
        data_type: The type of the data for input and output, for example trt.float32.

    Output:
        h_input_1: Input in the host.
        d_input_1: Input in the device.
        h_output_1: Output in the host.
        d_output_1: Output in the device.
        stream: CUDA stream.

    """

    # Determine dimensions and create page-locked memory buffers (which won't be swapped to disk) to hold host inputs/outputs.
    h_input_1 = cuda.pagelocked_empty(batch_size * trt.volume(engine.get_binding_shape(0)), dtype=trt.nptype(data_type))
    h_output = cuda.pagelocked_empty(batch_size * trt.volume(engine.get_binding_shape(1)), dtype=trt.nptype(data_type))

    # Allocate device memory for inputs and outputs.
    d_input_1 = cuda.mem_alloc(h_input_1.nbytes)
    d_output = cuda.mem_alloc(h_output.nbytes)

    # Create a stream in which to copy inputs/outputs and run inference.
    stream = cuda.Stream()
    return h_input_1, d_input_1, h_output, d_output, stream

def load_images_to_buffer(pics, pagelocked_buffer):
    preprocessed = np.asarray(pics).ravel()
    np.copyto(pagelocked_buffer, preprocessed)

def do_inference(engine, pics_1, h_input_1, d_input_1, h_output, d_output, stream, batch_size, height, width):
    """
    This is the function to run the inference
    Args:
        engine : Path to the TensorRT engine
        pics_1 : Input images to the model.
        h_input_1: Input in the host
        d_input_1: Input in the device
        h_output_1: Output in the host
        d_output_1: Output in the device
        stream: CUDA stream
        batch_size : Batch size for execution time
        height: Height of the output image
        width: Width of the output image

    Output:
        The list of output images

    """

    load_images_to_buffer(pics_1, h_input_1)

    with engine.create_execution_context() as context:
        # Transfer input data to the GPU.
        cuda.memcpy_htod_async(d_input_1, h_input_1, stream)

        # Run inference.
        # start timing # ========
        start_time = time.time()
        # context.profiler = trt.Profiler()
        context.execute(batch_size=1, bindings=[int(d_input_1), int(d_output)])
        end_time = time.time()
        # end timing # ========
        # calculate inference time
        inference_time = ( end_time - start_time ) * 1000
        # print ("inference time: {}".format(inference_time))

        # Transfer predictions back from the GPU.
        cuda.memcpy_dtoh_async(h_output, d_output, stream)
        # Synchronize the stream
        stream.synchronize()
        # Return the host output.
        # out = h_output.reshape((batch_size, -1, height, width))
        out = h_output
        return out, inference_time

def main():
    # set up arguments
    run_case = args.case
    num_images = int(args.num_img)
    model_name = args.model_name

    # set up logging to file - see previous section for more details
    log_filename = "logs/output_tensorrt_{}_{}.log".format(model_name, run_case)
    os.makedirs(os.path.dirname(log_filename), exist_ok=True)
    for handler in logging.root.handlers[:]:
        logging.root.removeHandler(handler)
    logging.basicConfig(level=logging.DEBUG,
                        format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
                        datefmt='%m-%d %H:%M',
                        filename=log_filename,
                        filemode='w')
    logging.getLogger('tensorrt')
    logging.info("#### start model prediction ####")

    # logger to capture errors, warnings, and other information during the build and inference phases
    TRT_LOGGER = trt.Logger(trt.Logger.WARNING)
    trt_runtime = trt.Runtime(TRT_LOGGER)

    # check model directory
    os.makedirs(os.path.dirname("model/"), exist_ok=True)

    # check data availability
    logging.info("check if data is available")
    logging.info("check data")
    if not os.path.exists("data"):
        return logging.error('check if data is available or run download_images.py for generating sample dataset')

    batch_size = 1
    # running inference from tensorrt
    logging.info("check engine")
    engine_name = "model/" + model_name + "_" + run_case + ".plan"
    if os.path.exists(engine_name):
        engine = eng.load_engine(trt_runtime, engine_name)
    else:
        onnx_path = "model/" + model_name + "_" + run_case + ".onnx"
        if not os.path.exists(onnx_path):
            logging.info("convert_torch_to_onnx")
            convert_torch_to_onnx(model_name)
        logging.info("build_engine_from_onnx")
        engine = build_engine_from_onnx(onnx_path, engine_name, batch_size, TRT_LOGGER)

    logging.info("start interference")
    data_type = trt.float32
    logging.info("allocate_buffers")
    h_input_1, d_input_1, h_output, d_output, stream = allocate_buffers(engine, batch_size, data_type)

    # predict images
    logging.info("do_inference")
    predict_images(num_images, engine, h_input_1, d_input_1, h_output, d_output, stream)

    return

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description = 'TensorRT')
    parser.add_argument('-model_name', help='select model: resnet50(default), mobilenet_v2', default="resnet50", type=str, required=True)
    parser.add_argument('-case', help='select case: ', type=str, required=True)
    parser.add_argument('-num_img', help='set number of images to be run', type=int, required=True)
    args = parser.parse_args()
    main()

# python3 compare_tensorrt_native.py -model_name "resnet50" -case "torch_onnx" -num_img 10
# python3 compare_tensorrt_native.py -model_name "resnet50" -case "tf_onnx" -num_img 10