import math
import random

def plus(number,second_number):
    print("number =")
    number = float(input())
    print("second_number = ")
    second_number = float(input())
    return print(number + second_number)
def minus(number,second_number):
    print("number =")
    number = float(input())
    print("second number = ")
    secondnumber = float(input())
    return print(number - secondnumber)
def deleniye(number,second_number):
    print("number =")
    number = float(input())
    print("second number = ")
    secondnumber = float(input())
    return print(number / secondnumber)
def umnojeniye(number,second_number):
    print("number =")
    number = float(input())
    print("second number = ")
    secondnumber = float(input())
    return print(number * secondnumber)
def stepen(number,second_number):
    print("number =")
    number = float(input())
    print("second number = ")
    secondnumber = float(input())
    return print(number ** secondnumber)
def module(number):
    print("number =")
    number = float(input())
    return print(math.fabs(number))
def arccos(number):
    print("number =")
    number = float(input())
    return print(math.acos(number))
def factorial(n):
    if n == 0:
        return 1
    return n*factorial(n-1)
def random(number,second_number):
    print("Рандомное число в диапазона от: ")
    number = int(input())
    print("До :")
    secondnumber = int(input())
    return print(random.randint(number, secondnumber))

point = True
print("/ деление \n* умножение \n+ сложение \n- разность\nm модуль\narccos - arccos \n^  возведение в степень \n! факториал \nrand рандомное число \nquit выход")

while(point):
    number=0;second_number=0;str=0;
    command = input()

    if command == "/":
        deleniye(number,second_number)

    elif command == "quit":
        point = False

    elif command == "*":
        umnojeniye(number,second_number)

    elif command == "-":
        minus(number,second_number)

    elif command == "+":
        plus(number,second_number)

    elif command == "^":
        stepen(number,second_number)

    elif command == "m":
        module(number)

    elif command == "arccos":
        arccos(number)

    elif command == "!":
        print("number =")
        number = float(input())
        print(factorial(number))

    elif command == "rand":
        random(number,second_number)

    else:
        print("Команда отсутствует")


class Funcionario:

    def __init__(self, nome, salario, divida=0.0):
        self.nome = nome
        self.salario = float(salario)
        self.divida = float(divida)

    def getNome(self):
        return self.nome

    def getSalario(self):
        return self.salario

    def aumentarSalario(self, percentualDeAumento):
        try:
            if percentualDeAumento < 0:
                raise ValueError
            self.salario += self.salario * (percentualDeAumento / 100.0)
        except ValueError:
            print('Não é possível diminuir o salário')

    def demitir(self):
        self.salario = 0

    def endividar(self, divida):
        'Adiciona dívida ao Funcionário e suja seu nome se for incompatível com seu salário'
        try:
            if "[Nome sujo]" in self.nome:
                raise Exception
            if self.salario < divida:
                self.nome = "*{} [Nome sujo]".format(self.nome)
            self.divida += divida
        except Exception:
            print('Endividamento de pessoa com nome sujo não é possível')
        except TypeError as err:
            print('Erro no tipo do valor da dívida: {}'.format(err))

    def pagarDivida(self):
        'Paga a dívida do Funcionário se o salário der e limpa seu nome'
        if self.salario < self.divida:
            print('Incapaz de pagar dívida com salário de %.2f' % self.salario)
        else:
            self.nome = self.nome[1:-11]
            print('Dívida de %.2f foi paga' % self.divida)
            self.divida = 0

    def empregar(self, salario):
        'Emprega o funcionário, dando-lhe um salário'
        self.salario = salario

# TESTE DA CLASSE
harry = Funcionario("Harry", 25000)
harry.aumentarSalario(10)
harry.demitir()
harry.endividar(10000)
print('Nome: %s' % harry.getNome())
harry.empregar(30000)
harry.pagarDivida()
print('Nome: %s' % harry.getNome())
print('Salario: %.2f' % harry.getSalario())
harry.demitir()
harry.endividar('1000')
harry.endividar(1000)
harry.endividar(1)


import urllib
import urllib2
import urlparse
import xml.etree.cElementTree as ET

from datetime import datetime, tzinfo
from decimal import Decimal
from datetime import datetime, timedelta
from django.contrib.sites.models import Site
from django.core import urlresolvers
from django.utils.datastructures import SortedDict
from urllib2 import HTTPError

from hiicart.gateway.amazon import fps
from hiicart.gateway.amazon.errors import AmazonGatewayError
from hiicart.gateway.amazon.settings import default_settings
from hiicart.gateway.base import IPNBase
from hiicart.models import HiiCart, Payment

_FPS_NS = "{http://fps.amazonaws.com/doc/2008-09-17/}"

class AmazonIPN(IPNBase):
    """Payment Gateway for Amazon Payments."""

    def __init__(self):
        super(AmazonIPN, self).__init__("amazon", default_settings)

    def _process_error(self, response):
        """Process error xml into (code, message, requestId)."""
        xml = ET.XML(response)
        code = xml.find(".//Code")
        message = xml.find(".//Message")
        request_id = xml.find(".//RequestID")
        if code is not None and message is not None and request_id is not None:
            return (code.text, message.text, request_id.text)
        return None

    def accept_payment(self, cart, data):
        """Record payment received from IPN."""
        self._update_with_cart_settings(cart)
        total = None
        if "transactionAmount" in data and data["transactionAmount"].startswith("USD "):
            total = Decimal(data["transactionAmount"][4:])
        if data["transactionStatus"] == "PENDING":
            transaction_id = data["transactionId"]
            pending = cart.payments.filter(transaction_id=transaction_id)
            if not pending: # Could already be created in make_pay_request
                self._create_payment(cart, total, transaction_id, "PENDING")
        elif data["transactionStatus"] == "SUCCESS":
            # Was this a pending payment?
            transaction_id = data["transactionId"]
            pending = cart.payments.filter(transaction_id=transaction_id, state="PENDING")
            if pending:
                pending[0].state = "PAID"
                pending[0].save()
            elif cart.payments.filter(transaction_id=transaction_id).count() == 0: # No duplicate payments
                self._create_payment(cart, total, transaction_id, "PAID")
            cart.update_state()
            self.begin_recurring(cart)
        elif data["transactionStatus"] == "CANCELLED":
            message = "Purchase %i (txn:%s) was cancelled with message '%s'" % (
                      cart.id, data['transactionId'], data['statusMessage'])
            self.log.warn(message)
            transaction_id = data["transactionId"]
            cancelled = cart.payments.filter(transaction_id=transaction_id)
            for p in cancelled:
                p.state = "CANCELLED"
                p.notes.create(text=message)
                p.save()
            cart.update_state()
        elif data["transactionStatus"] == "FAILURE":
            self.log.warn("Purchase %i (txn:%s) failed with message '%s'" % (
                cart.id, data['transactionId'], data['statusMessage']))
            cart.update_state()

    def begin_recurring(self, cart):
        """Save token and mark recurring item as active."""
        self._update_with_cart_settings(cart)
        if cart.recurringlineitems.count() == 0:
            return
        for ri in cart.recurringlineitems.all():
            ri.is_active = True
            ri.save()
        cart.update_state()

    def end_recurring(self, cart, token):
        """Mark a recurring item as inactive."""
        self._update_with_cart_settings(cart)
        if cart.recurringlineitems.count() == 0:
            return
        for ri in cart.recurringlineitems.all():
            ri.is_active = False
            ri.save()
        cart.update_state()

    def make_pay_request(self, cart, token, caller_reference=None):
        """
        Make a Pay request for the purchase with the given token.
        Return status received from Amazon.
        """
        self._update_with_cart_settings(cart)
        if caller_reference is None:
            caller_reference = cart.cart_uuid
        response = fps.do_fps("Pay", "GET", self.settings,
                   **{"CallerReference" : caller_reference,
                      "SenderTokenId" : token,
                      "TransactionAmount.CurrencyCode" : "USD",
                      "TransactionAmount.Value" : cart.total})
        xml = ET.XML(response)
        status = xml.find(".//%sTransactionStatus" % _FPS_NS)
        if status is None:
            error = self._process_error(response)
            if not error:
                msg = "Pay request failed for purchase '%s' with response: %s" % (
                            cart, response)
            else:
                msg = "Pay request failed for purchase '%s' with code/message: '%s' '%s' (request id: '%s')" % (
                            cart, error[0], error[1], error[2])
            self.log.warn(msg)
            cart.notes.create(text=msg)
            # An error code, maybe?
            code = xml.find(".//Code")
            if code is not None:
                return code.text
            return None
        else:
            # This work for pending payments?
            transaction_id = xml.find(".//%sTransactionId" % _FPS_NS).text
            if status.text == "Pending":
                self._create_payment(cart, cart.total, transaction_id, "PENDING")
            elif status.text == "Success":
                pending = cart.payments.filter(transaction_id=transaction_id,
                                               state="PENDING")
                if pending:
                    pending[0].state = "PAID"
                    pending[0].save()
                else:
                    self._create_payment(cart, cart.total, transaction_id, "PAID")
                self.begin_recurring(cart)
            else: # Cancelled or Failure
                msg = "Pay request for purchase '%s' returned status '%s'. \nFull response: %s" % (
                            cart, response)
                self.log.warn(msg)
                cart.notes.create(text=msg)
            cart.update_state()
            return status.text

    def save_recurring_token(self, cart, token):
        """Save recurring use token for future use."""
        self._update_with_cart_settings(cart)
        if cart.recurringlineitems.count() == 0:
            return
        ri = cart.recurringlineitems.all()[0]
        ri.payment_token = token
        ri.save()

    def verify_signature(self, raw_data, http_method, endpoint_uri, cart):
        self._update_with_cart_settings(cart)
        response = fps.do_fps("VerifySignature", http_method, self.settings,
                              UrlEndPoint=endpoint_uri,
                              HttpParameters=raw_data)
        xml = ET.XML(response)
        el = xml.find(".//{http://fps.amazonaws.com/doc/2008-09-17/}VerificationStatus")
        return el is not None and el.text == "Success"


def test(p='the sky is blue'):
    p = p.strip()
    start = p.split(' ')[0]
    s = [i for i in reversed(p)]

    r = []
    rs =''
    for i in s:
        if i != ' ':
            rs += i
        elif i == ' ':
            r.append(''.join([i for i in reversed(rs)]))
            rs = ''
            r.append(i)
    print(''.join(r)+s[-1])

def max_length(nums=[10,9,2,5,3,7,101,18]):
    max = 0

    for i in nums:
        count = 0
        start = nums.index(i)+1
        for j in nums[start:]:
            if j > i:
                count +=1
        print('i:', i, 'count:', count)
        if count > max:
            max = count
    print('max', max)
    return max

class Root:
    def __init__(self, value, left, right):
        self.value = value
        self.left = left
        self.right = right

def ml(nums=[10,9,2,5,3,7,101,18], ):
    if nums == None:
        return

    troot = Root(nums[0], None, None)

    if troot.value > nums[1]:
        troot.left = ml(nums=nums[1:])
    else:
        troot.right = ml(nums=nums[1:])

    pass

if __name__ == "__main__":

    array = [
          [1,   4,  7, 11, 15],
          [2,   5,  8, 12, 19],
          [3,   6,  9, 16, 22],
          [10, 13, 14, 17, 24],
          [18, 21, 23, 26, 30]
          ]

for line in array:
    print(line[0])


# -*- coding: utf-8 -*-
import cv2
import numpy as np

# 重複要素の削除とソート
def unique2d(a):
    a = np.ascontiguousarray(a)
    unique_a = np.unique(a.view([('', a.dtype)]*a.shape[1]))
    return unique_a.view(a.dtype).reshape((unique_a.shape[0], a.shape[1]))

def main():
    im = cv2.imread("hz2.jpg")  # 入力画像を取得
    hsv = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)
    # マスク画像を用いて元画像から指定した色を抽出
    hsv_min = np.array([160,150,0])
    hsv_max = np.array([190,255,255])
    mask = cv2.inRange(hsv, hsv_min, hsv_max)
    x2,x1 = np.array(np.nonzero(mask))
    w = np.dstack((x1,x2))[0]
    cv2.imshow("View",mask)
    cv2.waitKey(0)
    cv2.destroyAllWindows()                     # ウィンドウ破棄
    w = unique2d(w)
    ws = np.c_[w.T[0],w.T[1]]
    np.savetxt("obstacle.csv",ws , delimiter="\t",fmt="%d")

if __name__ == '__main__':
    main()


#  Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License").
#    You may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.

import time
from multiprocessing import Queue, synchronize
from typing import Any, Dict, Optional, cast

import boto3
import yaml
from aws_orbit_image_replicator import load_config, logger

def _generate_buildspec(repo_host: str, repo_prefix: str, src: str, dest: str) -> Dict[str, Any]:
    repo = dest.replace(f"{repo_host}/", "").split(":")[0]
    build_spec = {
        "version": 0.2,
        "phases": {
            "install": {
                "runtime-versions": {"python": 3.7, "docker": 19},
                "commands": [
                    (
                        "nohup /usr/sbin/dockerd --host=unix:///var/run/docker.sock "
                        "--host=tcp://0.0.0.0:2375 --storage-driver=overlay&"
                    ),
                    'timeout 15 sh -c "until docker info; do echo .; sleep 1; done"',
                ],
            },
            "pre_build": {
                "commands": [
                    f"aws ecr get-login-password | docker login --username AWS --password-stdin {repo_host}",
                    (
                        f"aws ecr create-repository --repository-name {repo} "
                        f"--tags Key=Env,Value={repo_prefix} || echo 'Already exists'"
                    ),
                ]
            },
            "build": {"commands": [f"docker pull {src}", f"docker tag {src} {dest}", f"docker push {dest}"]},
        },
    }
    logger.debug("BuildSpec: %s", build_spec)
    return build_spec

def _replicate_image(config: Dict[str, Any], src: str, dest: str) -> str:
    logger.info("Replicating Image: %s -> %s", src, dest)

    buildspec = yaml.safe_dump(_generate_buildspec(config["repo_host"], config["repo_prefix"], src, dest))
    logger.debug("BuildSpec:\n%s", buildspec)

    client = boto3.client("codebuild")
    build_id = client.start_build(
        projectName=config["codebuild_project"],
        sourceTypeOverride="NO_SOURCE",
        buildspecOverride=buildspec,
        timeoutInMinutesOverride=config["codebuild_timeout"],
        privilegedModeOverride=True,
    )["build"]["id"]

    logger.info("Started CodeBuild Id: %s", build_id)

    while True:
        build = client.batch_get_builds(ids=[build_id])["builds"][0]
        status: str = build["buildStatus"]
        phase: str = build["currentPhase"]

        logger.debug("CodeBuild Id: %s, Phase: %s,  Status: %s", build_id, phase, status)

        if status == "IN_PROGRESS":
            time.sleep(10)
            continue
        else:
            return status

def replicate(
    config: Dict[str, Any],
    lock: synchronize.Lock,
    replications_queue: Queue,  # type: ignore
    replication_statuses: Dict[str, str],
    replicator_id: int,
) -> int:
    logger.info("Started Replicator Id: %s", replicator_id)
    replication_task: Optional[Dict[str, str]] = None

    while True:
        try:
            load_config(config["in_cluster_deployment"])

            queue_size = replications_queue.qsize()
            logger.info(f"Queue Size: {queue_size}")

            replication_task = cast(Dict[str, str], replications_queue.get(block=True, timeout=None))
            src, dest = replication_task["src"], replication_task["dest"]

            with lock:
                logger.info("Got Replication Task: %s -> %s", src, replication_task["dest"])

                status = replication_statuses[dest]
                if status == "Complete":
                    logger.info("Skipping Completed Task: %s -> %s", src, dest)
                    continue
                elif status.startswith("Failed"):
                    logger.info("Skipping Failed Task: %s -> %s", src, dest)
                    continue
                elif status.startswith("Replicating"):
                    logger.info("Skipping Replicating Task: %s -> %s", src, dest)
                    continue
                else:
                    attempt = int(status.split(":")[1])
                    replication_statuses[dest] = f"Replicating:{attempt}"

            result = _replicate_image(config, src, dest)

            with lock:
                if result == "SUCCEEDED":
                    logger.info("Replication Complete: %s -> %s", src, dest)
                    replication_statuses[dest] = "Complete"
                else:
                    logger.error(
                        "Image Replication Attempt %s Failed: %s -> %s",
                        attempt,
                        src,
                        dest,
                    )
                    replication_statuses[dest] = f"Failed:{attempt}"

        except Exception as e:
            with lock:
                status = replication_statuses[dest]
                attempt = int(status.split(":")[1])
                logger.error(
                    "Image Replication Attempt %s Failed: %s -> %s",
                    attempt,
                    src,
                    dest,
                )
                logger.exception(e)
                replication_statuses[dest] = f"Failed:{attempt}"
        finally:
            replication_task = None
            time.sleep(5)


import json
import pandas as pd
import pandas.testing as pdt
import pytest

from solarforecastarbiter.io import utils

# data for test Dataframe
TEST_DICT = {'value': [2.0, 43.9, 338.0, -199.7, 0.32],
             'quality_flag': [1, 1, 9, 5, 2]}

DF_INDEX = pd.DatetimeIndex(start=pd.Timestamp('2019-01-24T00:00'),
                            freq='1min',
                            periods=5,
                            tz='UTC', name='timestamp')
TEST_DATA = pd.DataFrame(TEST_DICT, index=DF_INDEX)

@pytest.mark.parametrize('dump_quality,default_flag,flag_value', [
    (False, None, 1),
    (True, 2, 2)
])
def test_obs_df_to_json(dump_quality, default_flag, flag_value):
    td = TEST_DATA.copy()
    if dump_quality:
        del td['quality_flag']
    converted = utils.observation_df_to_json_payload(td, default_flag)
    converted_dict = json.loads(converted)
    assert 'values' in converted_dict
    values = converted_dict['values']
    assert len(values) == 5
    assert values[0]['timestamp'] == '2019-01-24T00:00:00Z'
    assert values[0]['quality_flag'] == flag_value
    assert isinstance(values[0]['value'], float)

def test_obs_df_to_json_no_quality():
    td = TEST_DATA.copy()
    del td['quality_flag']
    with pytest.raises(KeyError):
        utils.observation_df_to_json_payload(td)

def test_obs_df_to_json_no_values():
    td = TEST_DATA.copy().rename(columns={'value': 'val1'})
    with pytest.raises(KeyError):
        utils.observation_df_to_json_payload(td)

def test_forecast_series_to_json():
    series = pd.Series([0, 1, 2, 3, 4], index=pd.date_range(
        start='2019-01-01T12:00Z', freq='5min', periods=5))
    expected = [{'value': 0.0, 'timestamp': '2019-01-01T12:00:00Z'},
                {'value': 1.0, 'timestamp': '2019-01-01T12:05:00Z'},
                {'value': 2.0, 'timestamp': '2019-01-01T12:10:00Z'},
                {'value': 3.0, 'timestamp': '2019-01-01T12:15:00Z'},
                {'value': 4.0, 'timestamp': '2019-01-01T12:20:00Z'}]
    json_out = utils.forecast_object_to_json(series)
    assert json.loads(json_out)['values'] == expected

def test_json_payload_to_observation_df(observation_values,
                                        observation_values_text):
    out = utils.json_payload_to_observation_df(
        json.loads(observation_values_text))
    pdt.assert_frame_equal(out, observation_values)

def test_json_payload_to_forecast_series(forecast_values,
                                         forecast_values_text):
    out = utils.json_payload_to_forecast_series(
        json.loads(forecast_values_text))
    pdt.assert_series_equal(out, forecast_values)

def test_empty_payload_to_obsevation_df():
    out = utils.json_payload_to_observation_df({'values': []})
    assert set(out.columns) == {'value', 'quality_flag'}
    assert isinstance(out.index, pd.DatetimeIndex)

def test_empty_payload_to_forecast_series():
    out = utils.json_payload_to_forecast_series({'values': []})
    assert isinstance(out.index, pd.DatetimeIndex)

def test_hidden_token():
    ht = utils.HiddenToken('THETOKEN')
    assert str(ht) != 'THETOKEN'
    assert ht.token == 'THETOKEN'


#######################################################################
# Copyright (C) 2017 Shangtong Zhang(zhangshangtong.cpp@gmail.com)    #
# Permission given to modify the code as long as you keep this        #
# declaration at the top                                              #
#######################################################################

from .network_utils import *

class NatureConvBody(nn.Module):
    def __init__(self, in_channels=4, noisy=False, noise_std=0.5):
        super(NatureConvBody, self).__init__()
        self.feature_dim = 512
        self.noisy = noisy
        self.noise_std = noise_std
        self.conv1 = layer_init(nn.Conv2d(in_channels, 32, kernel_size=8, stride=4))
        self.conv2 = layer_init(nn.Conv2d(32, 64, kernel_size=4, stride=2))
        self.conv3 = layer_init(nn.Conv2d(64, 64, kernel_size=3, stride=1))
        if noisy:
            self.fc4 = NoisyLinear(7 * 7 * 64, self.feature_dim, noise_std)
        else:
            self.fc4 = layer_init(nn.Linear(7 * 7 * 64, self.feature_dim))

    def forward(self, x):
        y = F.relu(self.conv1(x))
        y = F.relu(self.conv2(y))
        y = F.relu(self.conv3(y))
        y = y.view(y.size(0), -1)
        y = F.relu(self.fc4(y))
        return y

class RainbowNet(nn.Module, BaseNet):
    def __init__(self, action_dim, num_atoms, body):
        super(RainbowNet, self).__init__()
        if body.noisy:
            self.fc_value = NoisyLinear(body.feature_dim, num_atoms, body.noise_std)
            self.fc_advantage = NoisyLinear(body.feature_dim, action_dim * num_atoms, body.noise_std)
        else:
            self.fc_value = layer_init(nn.Linear(body.feature_dim, num_atoms))
            self.fc_advantage = layer_init(nn.Linear(body.feature_dim, action_dim * num_atoms))

        self.action_dim = action_dim
        self.num_atoms = num_atoms
        self.body = body
        self.to(Config.DEVICE)

    def forward(self, x):
        phi = self.body(tensor(x))
        value = self.fc_value(phi).view(-1, 1, self.num_atoms)
        advantange = self.fc_advantage(phi).view(-1, self.action_dim, self.num_atoms)
        advantange -= advantange.mean(dim=1, keepdim=True)
        q = advantange + value

        prob = F.softmax(q, dim=-1)
        log_prob = F.log_softmax(q, dim=-1)

        return prob, log_prob

    def reset_noise(self):
        for name, module in self.named_children():
            if isinstance(module, NoisyLinear):
                module.reset_noise()
        for name, module in self.body.named_children():
            if isinstance(module, NoisyLinear):
                module.reset_noise()

class DDPGConvBody(nn.Module):
    def __init__(self, in_channels=4):
        super(DDPGConvBody, self).__init__()
        self.feature_dim = 39 * 39 * 32
        self.conv1 = layer_init(nn.Conv2d(in_channels, 32, kernel_size=3, stride=2))
        self.conv2 = layer_init(nn.Conv2d(32, 32, kernel_size=3))

    def forward(self, x):
        y = F.elu(self.conv1(x))
        y = F.elu(self.conv2(y))
        y = y.view(y.size(0), -1)
        return y

class FCBody(nn.Module):
    def __init__(self, state_dim, hidden_units=(64, 64), gate=F.relu):
        super(FCBody, self).__init__()
        dims = (state_dim,) + hidden_units
        self.layers = nn.ModuleList(
            [layer_init(nn.Linear(dim_in, dim_out)) for dim_in, dim_out in zip(dims[:-1], dims[1:])])
        self.gate = gate
        self.feature_dim = dims[-1]

    def forward(self, x):
        for layer in self.layers:
            x = self.gate(layer(x))
        return x

class TwoLayerFCBodyWithAction(nn.Module):
    def __init__(self, state_dim, action_dim, hidden_units=(64, 64), gate=F.relu):
        super(TwoLayerFCBodyWithAction, self).__init__()
        hidden_size1, hidden_size2 = hidden_units
        self.fc1 = layer_init(nn.Linear(state_dim, hidden_size1))
        self.fc2 = layer_init(nn.Linear(hidden_size1 + action_dim, hidden_size2))
        self.gate = gate
        self.feature_dim = hidden_size2

    def forward(self, x, action):
        x = self.gate(self.fc1(x))
        phi = self.gate(self.fc2(torch.cat([x, action], dim=1)))
        return phi

class OneLayerFCBodyWithAction(nn.Module):
    def __init__(self, state_dim, action_dim, hidden_units, gate=F.relu):
        super(OneLayerFCBodyWithAction, self).__init__()
        self.fc_s = layer_init(nn.Linear(state_dim, hidden_units))
        self.fc_a = layer_init(nn.Linear(action_dim, hidden_units))
        self.gate = gate
        self.feature_dim = hidden_units * 2

    def forward(self, x, action):
        phi = self.gate(torch.cat([self.fc_s(x), self.fc_a(action)], dim=1))
        return phi

class DummyBody(nn.Module):
    def __init__(self, state_dim):
        super(DummyBody, self).__init__()
        self.feature_dim = state_dim

    def forward(self, x):
        return x


# -------------------------------------------------------------------
# Author:   Nate Wilson
# Name:     Program5a
#
# Description:
# This program reads data from a document named Billing.txt that is
# created by program3 and produces a management report that includes
# The employees name, hourly rate, hours work for week 1-4, and a total
# invoice due.  It also provides a report on total billable hours,
# average billable hours, and a total billing due for all employees.
# -------------------------------------------------------------------

import module5

def main():

    # constants and variables

    listAllBillableHours = [] # a list of all billable hours
    numberOfEmployees = 0     # an accumulator of employees
    totalAmountDue = 0        # sum of all amount due
    employeeName = ''         # employees name string
    hourlyRate = 0            # employees hourly rate
    listEmployeeWeeklyHours =[] # list of weekly hours for each employee
    Billing = 0               # file variable
    totalHours_AvarageHours = () # tuple of employee total and average hours
    totalAllHours_AverageAllhours = () # tuple of all total and average hours
    totalAllHours = 0            # sum of all employees hours
    averageAllHours = 0          # average of all employees hours
    mostHours = 0               # max hours worked
    leastHours = 0             # min hours worked

    # print the begining of the display
    print()
    print('Employee','\t\tRate','\tWeek1','\tWeek2','\tWeek3',
    '\tWeek4','\tTotal',sep='')

    # opens the Billing.txt file in read mode

    Billing = open('Billing.txt', 'r')

    # read first line for employee name string

    employeeName = module5.readNextString(Billing)

    # while loop to read all records

    while employeeName != '':

        numberOfEmployees += 1
        hourlyRate = module5.readNextFloat(Billing)
        listEmployeeWeeklyHours = module5.readFloatsToList(Billing,4)
        listAllBillableHours.extend(listEmployeeWeeklyHours)
        week1 = listEmployeeWeeklyHours[0]
        week2 = listEmployeeWeeklyHours[1]
        week3 = listEmployeeWeeklyHours[2]
        week4 = listEmployeeWeeklyHours[3]
        totalHours_AvarageHours = module5.calcTotalHours(listEmployeeWeeklyHours,4)
        totalBillableHours = totalHours_AvarageHours[0]
        invoiceDataList = module5.calcInvoiceTotal(totalBillableHours, hourlyRate)
        amountDue = invoiceDataList[0]
        totalAmountDue += amountDue

        # display record for each employee

        print(employeeName,'\t\t',hourlyRate,'\t',week1,'\t',week2,
        '\t',week3,'\t',week4,'\t','$',format(amountDue,',.2f'),sep='')

        # check for next employee

        employeeName = module5.readNextString(Billing)

    # close file

    Billing.close()

    totalAllHours_AverageAllhours = module5.calcTotalHours(listAllBillableHours,numberOfEmployees)
    totalAllHours = totalAllHours_AverageAllhours[0]
    averageAllHours = totalAllHours_AverageAllhours[1]
    leastHours = min(listAllBillableHours)
    mostHours = max(listAllBillableHours)

    # displays the additional control data

    print()
    print('Total Billable Due: $', format(totalAmountDue, ',.2f'),sep='')
    print('Total Billable Hours:', format(totalAllHours, '.2f'))
    print('Average Billable Hours:', format(averageAllHours,'.2f'))
    print('Least hours worked:', format(leastHours,'.2f'))
    print('Most hours worked:', format(mostHours,'.2f'))


import socket

ip = "192.168.0.105"
port = 41990

# Tipo de endereçamento (hostname ou IPV4 neste caso)
# Tipo do socket (TCP neste caso)
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

print(f"Conectando em {ip}:{port}...")
client.connect((ip, port))
print("Conectado!")

while True:
    # Mandando
    bufferOUT = input("Digite sua mensagem:\n")
    if bufferOUT == "quit":
        client.send(bufferOUT.encode("UTF-8"))
        print("Fechando conexao!")
        client.close()
        break
    else:
        client.send(bufferOUT.encode("UTF-8"))

    # Recebendo
    print("Esperando mensagem...")
    bufferIN = client.recvfrom(1024)
    if bufferIN[0].decode("UTF-8") == "quit":
        print("Servidor fechou conexao!!")
        break
    else:
        print("Mensagem recebida:\n", bufferIN[0].decode("UTF-8"))


class Student:

    def __init__(self, i, last, first, ph, yr):
        self.i = i
        self.last = last
        self.first = first
        self.ph = ph
        self.yr = yr
        if int(self.yr) > 4:
            self.yr = 4
        if 1 <= int(self.yr) <=4:
            self.yr = yr

    def __str__(self):
        return f"{self.i};{self.first};{self.last};{self.ph},{self.yr}"
    #def __repr__(self):
        return f"<str{str(self)}>"
    def update_year(self):
        if self.yr < 4:
            self.yr += 1

s = Student("1111", "Carter", "Marilyn", "312/473-4837", 1)

Other Projects/Javascript, Python, Java Projects/Class ID Practice and Unit Tests/A.py


from tensorflow.lite.python.interpreter import Interpreter
import cv2
import numpy as np

class TFLiteModel:
    def load_model(self, model_path):
        self.interpreter = Interpreter(
            model_path=model_path,)

        self.interpreter.allocate_tensors()
        self.input_details = self.interpreter.get_input_details()
        self.output_details = self.interpreter.get_output_details()
        self.height = self.input_details[0]['shape'][1]
        self.width = self.input_details[0]['shape'][2]
        return self

    def preprocess(self, image):
        image = cv2.resize(image, (self.width, self.height))
        image = (np.asarray(image, dtype=np.float32))/255.0
        image = np.expand_dims(image, axis=0)
        return image

    def get_model_output(self, image):
        self.interpreter.set_tensor(self.input_details[0]['index'], image)
        self.interpreter.invoke()
        outputs = [self.interpreter.get_tensor(self.output_details[i]['index']) for i in range(len(self.output_details))]
        outputs = [np.squeeze(x) for x in outputs]
        return outputs

    def get_model_details(self):
        return self.interpreter.get_tensor_details()

    def model_path(self):
        return None

    def speedtest(self, image):
        image = cv2.imread(image)
        image = self.preprocess(image)
        from tqdm import tqdm
        for i in tqdm(range(1000)):
            output = self.get_model_output(image)

if __name__ == "__main__":
    import argparse, os, glob, traceback
    parser = argparse.ArgumentParser()
    parser.add_argument("--model", default=None, help="pass either a directory or a tflite model", required=True)
    parser.add_argument("--image", default=None, help="pass a sample image", required=False)

    args = parser.parse_args()

    image = None
    if args.image:
        image = cv2.imread(args.image)
    path = args.model
    try:
        tfutil = TFLiteModel().load_model(path)
        print (f"--------{path}---------")
        print ([(i['name'], i['shape']) for i in tfutil.input_details])
        print ([(i['name'], i['shape']) for i in tfutil.output_details])
        for x in tfutil.get_model_details():
            print (x)

        if image is not None:
            image = tfutil.preprocess(image)
            from tqdm import tqdm
            for i in tqdm(range(1000)):
                output = tfutil.get_model_output(image)
            print(output[0].shape)

    except:
        print("Exception in user code:")
        print("-" * 60)
        traceback.print_exc()
        print("-" * 60)


from sklearn import svm
from hog import HOG
from sklearn.svm import LinearSVC
import detection
import argparse
import cPickle

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-d", "--dataset", required = True,
	help = "path to the dataset file")
ap.add_argument("-m", "--model", required = True,
	help = "path to where the model will be stored")
args = vars(ap.parse_args())

# load the dataset and initialize the data matrix
(digits, target) = detection.load_digits(args["dataset"])
data = []

# initialize the HOG descriptor
hog = HOG(orientations = 18, pixelsPerCell = (10, 10),
	cellsPerBlock = (1, 1), normalize = True)

# loop over the images
for image in digits:
	# deskew the image, center it
	image = detection.deskew(image, 20)
	image = detection.center_extent(image, (20, 20))

	# describe the image and update the data matrix
	hist = hog.describe(image)

	data.append(hist)

model = LinearSVC(random_state=42)

model.fit(data, target)

# dump the model to file
f = open(args["model"], "w")
f.write(cPickle.dumps(model))
f.close()


from selenium import webdriver
import os
import base64
import time
#driverfile_path = r'C:\\Users\\12091\\Desktop\\msedgedriver.exe'
#driver = webdriver.Edge(executable_path=driverfile_path)
driver = webdriver.Firefox()

def get_img():
    bstr_1 = driver.find_element('css selector', '.verify-sub-block img').get_attribute('src')
    dd_1 = bstr_1.split(",")[1]
    imgdata_1 = base64.urlsafe_b64decode(dd_1)
    file_path_1 = 'C:\\Users\\12091\\Desktop\\123.jpg'
    file = open(file_path_1, 'wb')
    file.write(imgdata_1)
    file.close()
    print("图一写入成功")
    bstr_2 = driver.find_element('css selector', '.verify-img-panel img').get_attribute('src')
    dd_2 = bstr_2.split(",")[1]
    imgdata_2 = base64.urlsafe_b64decode(dd_2)
    file_path_2 = 'C:\\Users\\12091\\Desktop\\456.jpg'
    file = open(file_path_2, 'wb')
    file.write(imgdata_2)
    file.close()
    print("图二写入成功")

def calculate_offset():
    frame1 = image.open("./全屏截图.jpg")
    frame1 = frame1.convert('RGB')
    data = frame1.getdata()#获取rgb
    weight, height = frame1.size#获取宽高
    XCoordinates = 0
    pass

def verify_hhj():
    get_img()
    #calculate_offset()

def hhj_login():
    link="https://ymh5.scmttec.com/index.html#/login?redirect=/vaccineList&query={%22customId%22:%223%22,%22vaccName%22:%22%E4%B9%9D%E4%BB%B7HPV%E7%96%AB%E8%8B%97%EF%BC%88%E8%BF%9B%E5%8F%A3%EF%BC%89%22}"
    driver.get(link)
    hhj_0=r"form_item"
    time.sleep(0.5)#暂缓0.5s让网页加载完元素
    driver.find_element_by_xpath("//input[@placeholder='请输入手机号']").send_keys("13691896612")
    driver.find_element_by_xpath("//input[@placeholder='请输入密码']").send_keys("12580hhjH")
    change_class = driver.find_elements_by_class_name("btn-checkbox")#勾选同意协议
    change_class[0].click()
    driver.find_elements_by_class_name("btn-login")[0].click()
    verify_hhj()

hhj_login()


from django.core.paginator import Paginator
from django.db import transaction
from django.shortcuts import render, HttpResponse
from banner.models import Bnner
import json
from django.views.decorators.csrf import csrf_exempt

# 存储轮播图
@csrf_exempt
def save_banner(request):
    try:
        # 获取名称
        title = request.POST.get('title')
        # 获取状态
        status = request.POST.get('status')
        # 获取文件
        pic = request.FILES.get('pic')
        print(title, status, pic)
        if not pic:
            # pic 不存在
            return HttpResponse("图片不能为空")
        # 存入数据库
        with transaction.atomic():
            Bnner.objects.create(title=title, status=status, pic=pic)
        return HttpResponse('成功保存')
    except:
        return HttpResponse('保存失败')

# 序列化所有默认函数
def my_default(b):
    if isinstance(b, Bnner):
        return {'id': b.id, 'title': b.title, 'status': b.status, 'create_time': str(b.create_time), 'pic': str(b.pic)}

# 展示轮播图数据
def show_banner(request):
    print(Bnner.objects.get(pk=1).pic)
    # 从前端获取每页显示行数
    row_num = request.GET.get('rows')
    # 从前端获取页码
    page_num = request.GET.get('page')
    # 从数据库获取数据 进行分页管理
    pgtor = Paginator(Bnner.objects.all(), per_page=row_num)
    # 生成page
    pg = pgtor.page(page_num)
    # 返回的数据
    data = {
        'page': page_num,
        'total': pgtor.num_pages,
        'records': pgtor.count,
        'rows': list(pg)
    }
    # 进行序列化
    json_str = json.dumps(data, default=my_default)
    return HttpResponse(json_str)

# 编辑轮播图列表
@csrf_exempt
def edit_banner(request):
    try:
        # 获取oper,添，修改edit，删除del
        oper = request.POST.get('oper')
        print(oper)
        # 判断是修改，还是删除
        if oper == 'edit':
            # 修改 , 获取 需要修改的数据
            id = request.POST.get('id')
            title = request.POST.get('title')
            status = request.POST.get('status')
            # 通过id获取modal对象
            banner = Bnner.objects.get(pk=id)
            # 进行修改
            with transaction.atomic():
                banner.title = title
                banner.status = status
                banner.save()
            return HttpResponse('成功修改')
        elif oper == 'del':
            # 获取id
            id = request.POST.get('id')
            # 从数据库删除
            Bnner.objects.get(pk=id).delete()
            return HttpResponse('成功删除')
    except:
        return HttpResponse('操作失败')


import env as env_package
import parser.ast.ast_expression as ast_expression
import parser.ast.ast_type as ast_type
import parser.ast.ast_variable_declaration as ast_variable_declaration
import parser.ast.statement.ast_block as ast_block
import parser.ast.statement.ast_for as ast_for

def link_names(ast):
  '''Link all the names in an AST
  Expects an ASTRoot as input.
  '''
  if not ast.class_or_interface:
    return

  ast = ast.class_or_interface
  env = ast.environment

  this_type = ast_type.ASTType.from_str(str(ast.name), is_primitive=False)
  this_type.definition = ast

  check_forward_field_init(ast.fields, env)
  for f in ast.fields:
    substitute_type = this_type
    # Static fields cannot use "this", so we give the None type for it.
    if f.is_static:
      substitute_type = None
    _link_name_in_expr_or_stmt(f.children[3], env, substitute_type)

  for m in ast.methods:
    if m.body is None:
      continue
    block = m.body
    substitute_type = this_type
    # Static methods cannot use "this", so we give the None type instead.
    if m.is_static:
      substitute_type = None

    for ex in block.children:
      _link_name_in_expr_or_stmt(ex, block.environment, substitute_type)

def check_forward_field_init(fields, env):
  '''Checks to make sure no fields do a forward declaration
  JLS 8.3.2.3
  TODO(songandrew): Do static/non-static checks here
  '''
  if len(fields) == 0:
    return

  for ix, f in enumerate(fields):
    # Check if any identifiers are fields of this class.  If they are,
    # check to see if they are declared after the current field.
    idens = get_all_field_identifiers(f)
    for id in idens:
      # Just use the first part of the name since this is only an error for
      # short names.
      name = id.parts[0]
      tmp, enclosing_type = env.lookup_field(name)
      if tmp and tmp in fields and fields.index(tmp) >= ix:
        # A field existed that was declared in the enclosing class, and
        # its declaration appears after the current declaration or is itself
        # the current declaration.
        raise NameLinkingError(
            'Forward initialization of field {0} with {1}.'.format(
                f.identifier, name))

def get_all_field_identifiers(f):
  '''Gets all identifiers that appear in a list initialization expression'''
  if f.expression is None:
    return []
  return _get_all_identifiers(f.expression)

def _get_all_identifiers(expr):
  '''Helper to get all identifiers from an expression or statement node'''
  acc = []
  if isinstance(expr, ast_expression.ASTMethodInvocation):
    # Only check the left side and the arguments.
    acc.extend(_get_all_identifiers(expr.left))
    for arg in expr.arguments:
      acc.extend(_get_all_identifiers(arg))
  elif isinstance(expr, ast_expression.ASTFieldAccess):
    # We check only the left side.
    acc.extend(_get_all_identifiers(expr.left))
  elif isinstance(expr, ast_expression.ASTIdentifiers):
    # Really the base case -- if we are an ASTIdentifiers node, then add
    # ourself to the list.
    acc.append(expr)
  elif isinstance(expr, ast_expression.ASTAssignment):
    # We are allowed to use a field declared after us if we are assigning
    # to it on the LHS of a method.
    if not isinstance(expr.left, ast_expression.ASTIdentifiers):
      acc.extend(_get_all_identifiers(expr.left))

    # Always get things from the RHS of an assignment.
    acc.extend(_get_all_identifiers(expr.right))
  else:
    for ex in expr.expressions:
      acc.extend(_get_all_identifiers(ex))

  return acc

def _link_name_in_expr_or_stmt(expr, env, this_type):
  if expr is None:
    return

  if isinstance(expr, ast_expression.ASTMethodInvocation):
    # We check everything except the right part of a method invocation,
    # since that will depend on the type of the left part.  That is, for the
    # expression (a.b).c(foo, bar), we will link a.b, foo, and bar.
    if expr.right is None:
      # The method name will be on the left hand side.
      if isinstance(expr.left, ast_expression.ASTIdentifiers):
        if len(expr.left.parts) == 1:
          # In this case the method should be called on "this", so we can set
          # the first definition and let it be handled by the method invocation
          # rule.
          expr.left.first_definition = ('', this_type)
        else:
          # A qualified name can just be done in a recursive call.
          _link_name_in_expr_or_stmt(expr.left, env, this_type)
      else:
        # Something more complex.  Just recurse.
        _link_name_in_expr_or_stmt(expr.left, env, this_type)
    else:
      # If there's something on the right, then just recuse on the left side
      # since the method name will not be there.
      _link_name_in_expr_or_stmt(expr.left, env, this_type)

    # Always check the arguments
    for arg in expr.arguments:
      _link_name_in_expr_or_stmt(arg, env, this_type)

  elif isinstance(expr, ast_expression.ASTFieldAccess):
    # We check only the left side since the "right" field access will depend
    # on the type of the left expression.
    _link_name_in_expr_or_stmt(expr.left, env, this_type)
  elif isinstance(expr, ast_expression.ASTIdentifiers):
    is_static = this_type is None
    defn, name = find_first_definition(expr, env, is_static)
    expr.first_definition = (name, defn)
  elif isinstance(expr, ast_block.ASTBlock) or isinstance(expr, ast_for.ASTFor):
    # ASTBlock and ASTFor need to use their containing environment.
    _link_containing_expressions(expr, expr.environment, this_type)
  elif isinstance(expr, ast_expression.ASTThis):
    # ASTThis only needs a pointer to the current class.
    expr.expr_type = this_type
  else:
    # If it's not one of the special cases, just recuse on all the expressions
    # and statements.
    _link_containing_expressions(expr, env, this_type)

def _link_containing_expressions(expr, env, this_type):
  for ex in expr.expressions:
    _link_name_in_expr_or_stmt(ex, env, this_type)

def find_first_definition(ast_idens, env, is_static):
  '''Looks up the first definiton for an ast_identifiers node
  TODO(songandrew): Comment what "first definition" means here.
  Returns: an AST node of the definition'''

  # Suppose we are resolving 'a.b.c'.
  full_name = str(ast_idens)

  if len(ast_idens.children) == 1:
    # The ast contains a simple name. Check if it is a local variable first and
    # then resort to a field lookup.
    try:
      ret = env.lookup_local(full_name)
      if ret:
        return ret, full_name
    except env_package.EnvironmentError:
      # Not in a block environment since identifiers can be in a field
      # definition.
      pass

    ret, enclosing_type = env.lookup_field(full_name)
    if ret is None:
      raise NameLinkingError('No definition found for simple name {0}'.format(
        full_name))
    # There is no implicit this for static fields so the first definition can
    # not be static.
    if ret.is_static:
      raise NameLinkingError('Static lookup in non-static context')
    if is_static:
      raise NameLinkingError(
        'Using implicit this for {0} in static method'.format(full_name))
    return ret, full_name

  # Name is qualified, i.e. contains a dot character.
  parts = list(ast_idens.children)
  # Try to resolve 'a' to a local variable or formal parameter.
  try:
    local_var = env.lookup_local(parts[0])
    if local_var:
      return local_var, parts[0]
  except env_package.EnvironmentError:
    # Not in a block environment: identifiers can be in a method definition,
    # field definition or class/interface.
    pass

  # Try to resolve 'a' to a field variable of the enclosing class.
  field, enclosing_type = env.lookup_field(parts[0])
  if field:
    if is_static and not field.is_static:
      raise NameLinkingError('Static lookup in non-static context')
    return field, parts[0]

  # Try all prefixes of 'a.b.c' until the shortest matches to a type.
  ast_class_or_interface = None
  for type_index, p in enumerate(parts[:-1]):
    possible_type_name = '.'.join(parts[:type_index + 1])
    ast_class_or_interface = env.lookup_type(possible_type_name)
    if ast_class_or_interface:
      break

  if ast_class_or_interface is None:
    raise NameLinkingError(
        'Could not resolve a prefix of {0} as a type'.format(full_name))
  return ast_class_or_interface, '.'.join(parts[:type_index + 1])

class NameLinkingError(Exception):
  def __init__(self, msg):
    self.msg = msg


#!/usr/bin/env python3

import argparse
import os
import sys
import yaml
import sqlite3
import json
import re

class CravatFilter ():
    def __init__ (self, dbpath=None, filterpath=None, filtername=None,
            filterstring=None, filter=None, mode='sub'):
        self.mode = mode
        if self.mode == 'main':
            self.stdout = True
        else:
            self.stdout = False
        self.dbpath = None
        self.filterpath = None
        self.cmd = None
        self.level = None
        self.filter = None
        self.filtertable = 'filter'
        self.savefiltername = None
        self.filtername = None
        self.filterstring = None
        self.conn = None
        self.cursor = None

        if dbpath != None:
            self.dbpath = dbpath
            if mode == 'sub':
                self.connect_db()

        if filter != None:
            self.filter = filter
        else:
            if filterstring != None:
                self.filterstring = filterstring
            elif filtername != None:
                self.filtername = filtername
            elif filterpath != None:
                self.filterpath = filterpath
            if mode == 'sub':
                self.loadfilter()

    def run (self, cmd=None, args=None, dbpath=None, filter=None):
        if args != None:
            self.parse_args(args)
        if cmd != None:
            self.cmd = cmd

        if dbpath != None:
            self.dbpath = dbpath
            self.connect_db()
        elif self.dbpath != None and self.cursor == None:
            self.connect_db()

        # Loads filter.
        if filter != None:
            self.filter = filter
        elif (self.filtername != None or self.filterpath != None or
            self.filterstring != None) and self.filter == None:
            self.loadfilter()

        ret = None
        if self.cursor != None and self.filter != None:
            if self.cmd == 'uidpipe':
                ret = self.run_level_based_func(self.getuiditerator)
            elif self.cmd == 'count':
                ret = self.run_level_based_func(self.getcount)
            elif self.cmd == 'rows':
                ret = self.run_level_based_func(self.getrows)
            elif self.cmd == 'pipe':
                ret = self.run_level_based_func(self.getiterator)
        elif self.cursor != None and self.cmd == 'list':
            ret = self.listfilter()

        # Saves filter.
        if self.filter != None:
            if self.cmd == 'save' or self.savefiltername != None:
                ret = self.savefilter()

        return ret

    def run_level_based_func (self, cmd):
        ret = {}
        if self.level != None:
            ret[self.level] = cmd(level=self.level)
        else:
            levels = ['variant', 'gene']
            ret = {}
            for level in levels:
                ret_onelevel = cmd(level=level)
                ret[level] = ret_onelevel
        return ret

    def parse_args (self, args):
        parser = argparse.ArgumentParser()
        parser.add_argument('-d',
            dest='dbpath',
            required=True,
            help='Path of a result database file (.sqlite)')
        parser.add_argument('-f',
            dest='filterpath',
            help='Path of a filtering criteria file')
        parser.add_argument('-F',
            dest='filtername',
            help='Name of the filter to apply (saved in the database)')
        parser.add_argument('--filterstring',
            dest='filterstring',
            default=None,
            help='Filter in JSON')
        parser.add_argument('-l',
            dest='level',
            default=None,
            choices=['variant', 'gene'],
            help='Analysis level to filter')
        parser.add_argument('-s',
            dest='savefiltername',
            help='Name to save the filter as (in the database)')
        if self.mode == 'main':
            parser.add_argument('command',
            choices=['uidpipe', 'count', 'rows', 'pipe', 'save', 'list'],
            help='Command')

        parsed_args = parser.parse_args(args)
        self.dbpath = parsed_args.dbpath
        self.filterpath = parsed_args.filterpath
        self.level = parsed_args.level
        if self.mode == 'main':
            self.cmd = parsed_args.command
        self.savefiltername = parsed_args.savefiltername
        self.filtername = parsed_args.filtername
        self.filterstring = parsed_args.filterstring

    def connect_db (self, dbpath=None):
        if dbpath != None:
            self.dbpath = dbpath

        self.conn = sqlite3.connect(self.dbpath)
        self.cursor = self.conn.cursor()
        self.conn.create_function('regexp', 2, regexp)

    def close_db (self):
        self.cursor.close()
        self.conn.close()

    def create_filtertable (self):
        if self.cursor == None:
            return
        sql = 'create table ' + self.filtertable + ' (name text, criteria text)'
        self.cursor.execute(sql)
        self.conn.commit()

    def filtertable_exists (self):
        sql = 'select name from sqlite_master where ' +\
            'type="table" and name="' + self.filtertable + '"'
        self.cursor.execute(sql)
        row = self.cursor.fetchone()
        if row == None:
            ret = False
        else:
            ret = True
        return ret

    def loadfilter (self, filterpath=None, filtername=None,
            filterstring=None, filter=None):
        if filterpath != None:
            self.filtername = filtername
        if filtername != None:
            self.filterpath = filterpath
        if filterstring != None:
            self.filterstring = filterstring
        if filter != None:
            self.filter = filter

        if self.filterstring != None:
            self.filterstring = self.filterstring.replace("'", '"')
            self.filter = json.loads(self.filterstring)
        elif self.filtername != None and self.filtertable_exists():
            self.cursor.execute('select criteria from ' + self.filtertable +
                ' where name="' + self.filtername + '"')
            criteria = self.cursor.fetchone()
            if criteria != None:
                self.filter = json.loads(criteria[0])
        elif self.filterpath != None and os.path.exists(self.filterpath):
            with open(self.filterpath) as f:
                self.filter = yaml.load(f)

    def delete_filtered_uid_table (self):
        self.cursor.execute('pragma synchronous=0')
        q = 'drop table if exists variant_filtered'
        self.cursor.execute(q)
        q = 'drop table if exists gene_filtered'
        self.cursor.execute(q)
        self.conn.commit()
        self.cursor.execute('pragma synchronous=2')

    def getwhere (self, level):
        if self.filter == None:
            return ''
        if level not in self.filter:
            return ''
        criteria = self.filter[level]
        columns = criteria.keys()
        where = ''
        for column in columns:
            op_val = criteria[column]
            where += column + ' ' + op_val + ' and '
        if where != '':
            where = ' where ' + where
            where = where.rstrip(' and ')
        return where

    def getvariantcount (self):
        return self.getcount('variant')

    def getgenecount (self):
        return self.getcount('gene')

    def getcount (self, level='variant'):
        level = 'variant'
        self.make_filtered_uid_table()
        ftable = level + '_filtered'
        q = 'select count(*) from ' + ftable
        self.cursor.execute(q)
        n = self.cursor.fetchone()[0]
        if self.stdout == True:
            print('#' + level)
            print(str(n))
        return n

    def getvariantrows (self):
        return self.getrows('variant')

    def getgenerows (self):
        return self.getrows('gene')

    def getrows (self, level='variant'):
        where = self.getwhere(level)
        q = 'select *  from ' + level + where
        self.cursor.execute(q)

        ret = [list(v) for v in self.cursor.fetchall()]

        if self.stdout == True:
            print('#' + level)
            for row in ret:
                print('\t'.join([str(v) for v in row]))

        return ret

    def get_gene_row (self, hugo):
        q = 'select * from gene where base__hugo="' + hugo + '"'
        self.cursor.execute(q)
        row = self.cursor.fetchone()
        return row

    def getvariantiterator (self):
        return self.getiterator('variant')

    def getgeneiterator (self):
        return self.getiterator('gene')

    def getiterator (self, level='variant'):
        where = self.getwhere(level)
        sql = 'select * from ' + level + where
        self.cursor.execute(sql)
        it = self.cursor.fetchall()
        return it

    def get_filtered_iterator (self, level='variant'):
        if level == 'variant':
            kcol = 'base__uid'
            ftable = 'variant_filtered'
        elif level == 'gene':
            kcol = 'base__hugo'
            ftable = 'gene_filtered'
        elif level == 'sample':
            kcol = 'base__uid'
            ftable = 'variant_filtered'
        elif level == 'mapping':
            kcol = 'base__uid'
            ftable = 'variant_filtered'
        table = level
        if level in ['variant', 'gene', 'sample', 'mapping']:
            sql = 'select t.* from ' + table + ' as t inner join ' + ftable +\
                ' as f on t.' + kcol + '=f.' + kcol
        self.cursor.execute(sql)
        it = self.cursor.fetchall()
        return it

    def make_filtered_uid_table (self):
        self.cursor.execute('pragma synchronous=0')
        level = 'variant'
        vtable = level
        vftable = level + '_filtered'
        q = 'drop table if exists ' + vftable
        self.cursor.execute(q)
        where = self.getwhere(level)
        q = 'create table ' + vftable +\
            ' as select base__uid from ' + level + where
        self.cursor.execute(q)
        self.cursor.execute('pragma synchronous=2')

    def make_filtered_hugo_table (self):
        self.cursor.execute('pragma synchronous=0')
        level = 'gene'
        vtable = 'variant'
        vftable = vtable + '_filtered'
        gftable = level + '_filtered'
        q = 'drop table if exists ' + gftable
        self.cursor.execute(q)
        q = 'create table ' + gftable +\
            ' as select distinct v.base__hugo from ' + vtable + ' as v'\
            ' inner join ' + vftable + ' as vf on vf.base__uid=v.base__uid'
        self.cursor.execute(q)
        self.cursor.execute('pragma synchronous=2')

    def savefilter (self, name=None):
        if self.cursor == None or self.filter == None:
            return

        if name == None:
            if self.savefiltername != None:
                name = self.savefiltername
            else:
                name = 'default'

        # Creates filter save table if not exists.
        self.cursor.execute('select name from sqlite_master where ' +
            'type="table" and name="' + self.filtertable + '"')
        ret = self.cursor.fetchone()
        if ret == None:
            self.cursor.execute('create table ' + self.filtertable +
                ' (name text unique, criteria text)')

        # Saves the filter.
        filterstr = json.dumps(self.filter)
        sql = 'insert or replace into ' + self.filtertable +\
            ' values ("' + name + '", \'' + filterstr + '\')'
        self.cursor.execute(sql)
        self.conn.commit()

    def listfilter (self, name=None):
        if name == None:
            if self.savefiltername != None:
                name = self.savefiltername
            else:
                name = 'default'

        # Creates filter save table if not exists.
        self.cursor.execute('select name from sqlite_master where ' +
            'type="table" and name="' + self.filtertable + '"')
        ret = self.cursor.fetchone()
        if ret == None:
            self.cursor.execute('create table ' + self.filtertable +
                ' (name text, criteria text)')

        sql = 'select name, criteria from ' + self.filtertable
        self.cursor.execute(sql)
        ret = {}
        for row in self.cursor.fetchall():
            name = row[0]
            criteria = json.loads(row[1])
            ret[name] = criteria
            if self.stdout:
                print('#' + name)
                for level in criteria:
                    print('    ' + level + ':')
                    for column in criteria[level]:
                        print('        ' + column + ': ' +
                              criteria[level][column])

        return ret

    def addvariantfilter (self, column, condition):
        self.addfilter(column, condition, 'variant')

    def addgenefilter (self, column, condition):
        self.addcriterion(column, condition, 'gene')

    def addfilter (self, column, condition, level='variant'):
        if self.filter == None:
            self.filter = {}
        if level not in self.filter:
            self.filter[level] = {}
        self.filter[level][column] = condition

    def removevariantfilter (self, column):
        self.removefilter(column, 'variant')

    def removegenefilter (self, column):
        self.removefilter(column, 'gene')

    def removefilter (self, column, level='variant'):
        if self.filter == None:
            return
        if level in self.filter and column in self.filter[level]:
            del self.filter[level][column]

    def table_exists (self, table):
        sql = 'select name from sqlite_master where type="table" and ' +\
            'name="' + table + '"'
        self.cursor.execute(sql)
        if self.cursor.fetchone() == None:
            return False
        else:
            return True

    def get_variant_iterator_filtered_uids_cols (self, cols):
        q = 'select ' + ','.join(cols) + ' from variant as v ' +\
            'inner join variant_filtered as f on v.base__uid=f.base__uid'
        self.cursor.execute(q)
        for row in self.cursor.fetchall():
            d = {}
            for i in range(len(row)):
                d[cols[i].split('__')[1]] = row[i]
            yield d

    def get_result_levels (self):
        q = 'select name from sqlite_master where type="table" and ' +\
            'name like "%_header"'
        self.cursor.execute(q)
        table_names = []
        for row in self.cursor.fetchall():
            table_names.append(row[0].replace('_header', ''))
        return table_names

def regexp (y, x, search=re.search):
    if x is None:
        return 0
    return 1 if search(y, x) else 0

def main ():
    cv = CravatFilter(mode='main')
    cv.run(args=sys.argv[1:])

def test ():
    cf = CravatFilter(dbpath='d:\\git\\cravat-newarch\\tmp\\job\\in1000.sqlite',
                      mode='sub',
                      filterstring='{"variant": {"thousandgenomes__af": ">0.1"}}')
    print(cf.getcount())
    cf.make_filtered_uid_table()
    for row in cf.get_filtered_iterator(level='variant'):
        print(row)
    for row in cf.get_filtered_iterator(level='gene'):
        print(row)

if __name__ == '__main__':
    #main()
    test()


import tensorflow as tf
from frankModel import FrankNet  # The model we are gonna use to train
from logReader import Reader
from sklearn.model_selection import train_test_split
import time
import numpy as np
import os
print("Observed TF Version: ",tf.__version__)
print("Observed Numpy Version: ",np.__version__)

#! Training Configuration
EPOCHS = 1000000 #EPOCHS
INIT_LR = 1e-3   #LEARNING RATE
BS = 8          #Batch Size
GPU_COUNT = 1    # Change this value if you are using multiple GPUs
MULTI_GPU = True #Change this to enable multi-GPU

#! Log Interpretation
STORAGE_LOCATION = "trained_models/behavioral_cloning"

#! Global training data storage
# TODO: This should be optimized?
observation = []
linear = []
angular = []

def load_data():
    global observation, linear, angular
    reader = Reader('train.log')
    observation, linear, angular = reader.read()
    observation = np.array(observation)
    linear = np.array(linear)
    angular = np.array(angular)
    print('Observation Length: ', len(observation))
    print('Linear Length: ', len(linear))
    print('Angular Length: ', len(angular))
    # exit()
    return

# -----------------------------------------------------------------------------
# Define custom loss functions for regression in Keras
# -----------------------------------------------------------------------------
# root mean squared error (rmse) for regression
def rmse(y_true, y_pred):
    from tensorflow.keras import backend
    return backend.sqrt(backend.mean(backend.square(y_pred - y_true), axis=-1))

# mean squared error (mse) for regression
def mse(y_true, y_pred):
    from tensorflow.keras import backend
    return backend.mean(backend.square(y_pred - y_true), axis=-1)

# coefficient of determination (R^2) for regression
def r_square(y_true, y_pred):
    from tensorflow.keras import backend
    SS_res = K.sum(K.square(y_true - y_pred))
    SS_tot = K.sum(K.square(y_true - K.mean(y_true)))
    return (1 - SS_res/(SS_tot + K.epsilon()))

#!================================================================
# 0. Create directories
try:
    os.makedirs("trainedModel")
except FileExistsError:
    print("Directory already exists!")
except OSError:
    print("Create folder for trained model failed. Please check system permissions.")
    exit()

# 1. Load all the datas
load_data()
print('Load all complete')

# 2. Split training and testing
observation_train, observation_valid, linear_train, linear_valid, angular_train, angular_valid = train_test_split(
    observation, linear, angular, test_size=0.2, shuffle=True)

# 3. Build the model using strategy
if MULTI_GPU:
    print("Currently using multiple GPUs")
    if tf.config.list_physical_devices('gpu'):
        strategy = tf.distribute.MirroredStrategy()
    else:
        print("Selected MultiGPU but only observe single GPU. Cancel Multi GPU training!")
        strategy = tf.distribute.get_strategy()
else:
    print("Currently using single GPUs")
    strategy = tf.distribute.get_strategy()

with strategy.scope():
    model = FrankNet.build(200, 150)

# 4. Define the loss function and weight
losses = {
    "Linear": "mse",
    "Angular": "mse"
}
lossWeights = {"Linear": 0.001, "Angular": 10.0}

# 5. Select optimizer
opt = tf.keras.optimizers.Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS)

# 6. Select loss metrics
metrics_list = ["mse", rmse, r_square]

# 8. Compile model and plot to see
model.compile(optimizer=opt, loss=losses, loss_weights=lossWeights,
              metrics=metrics_list)

# 9. Setup tensorboard
tensorboard = tf.keras.callbacks.TensorBoard(
    log_dir='logs/{}'.format(time.ctime()))

# 10. checkpoint
#? Keep track of the best validation loss model
filepath1 = "trainedModel/FrankNetBest_Validation.h5"
checkpoint1 = tf.keras.callbacks.ModelCheckpoint(
    filepath1, monitor='val_loss', verbose=1, save_best_only=True, mode='min')

#? Keep track of the best loss model
filepath2 = "trainedModel/FrankNetBest_Loss.h5"
checkpoint2 = tf.keras.callbacks.ModelCheckpoint(
    filepath2, monitor='loss', verbose=1, save_best_only=True, mode='min')

callbacks_list = [checkpoint1, checkpoint2, tensorboard]

# 11. GO!
history = model.fit(observation_train,
                    {"Linear": linear_train,
                        "Angular": angular_train}, validation_data=(observation_valid, {
                            "Linear": linear_valid, "Angular": angular_valid}),
                    epochs=EPOCHS, callbacks=callbacks_list, verbose=0)

model.save('trainedModel/FrankNet.h5')


# python3 -m pip install isort autoflake astpretty black
# requires python3.9 to run
import os
import argparse
import ast
import subprocess
import multiprocessing
from pathlib import Path
import astpretty
import sys

parser = argparse.ArgumentParser()
parser.add_argument(
    "--out_dir", type=str, default="python",
)
parser.add_argument("--verbose", "-v", action="store_true")
parser.add_argument("--debug", "-d", action="store_true")
parser.add_argument("--autoflake", "-a", action="store_true")
parser.add_argument("--black", "-b", action="store_true")
parser.add_argument("--isort", "-i", action="store_true")
parser.add_argument("--license", "-l", action="store_true")
parser.add_argument("--ast", action="store_true")
args = parser.parse_args()

OUT_PATH = Path(args.out_dir)
SHOULD_SAVE_AST = args.ast
COMPATIBLE_MODULE = "oneflow.compatible.single_client"

def dumpprint(node):
    astpretty.pprint(node)

def is_decorator(d, name=None):
    return (isinstance(d, ast.Name) and d.id == name) or (
        isinstance(d, ast.Call) and isinstance(d.func, ast.Name) and d.func.id == name
    )

def is_stable(node: ast.AST):
    for d in node.decorator_list:
        if is_decorator(d, "stable_api"):
            return True
    return False

def is_experimental(node: ast.AST):
    for d in node.decorator_list:
        if is_decorator(d, "experimental_api"):
            return True
    return False

def get_parent_module(value):
    return ".".join(value.split(".")[0:-1])

def join_module(*args):
    return ".".join([m for m in args if m])

def path_from_module(module, is_init=False):
    if is_init:
        return Path("/".join(module.split(".") + ["__init__.py"]))
    else:
        return Path("/".join(module.split(".")) + ".py")

def module_from_path(path: Path):
    assert path.name.endswith(".py")
    parts = path.parts
    if parts[-1] == "__init__.py":
        return ".".join(path.parts[0:-1])
    else:
        return ".".join(path.parts)[0:-3]

def is_compatible_root_module(module: str):
    if module == COMPATIBLE_MODULE:
        return True
    assert module == "oneflow"
    return False

class ReservedKeywordsVisitor(ast.NodeVisitor):
    def __init__(self, keywords=None) -> None:
        self.keywords = keywords
        self.has_reserved_keyword = False

    def visit_Name(self, node: ast.Name):
        if node.id in self.keywords:
            self.has_reserved_keyword = True

def replace_filename(name: str):
    return name.replace("name_scope", "namescope")

def replace_str(name: str):
    name = replace_filename(name)
    name = name.replace("lib.core", "support")
    name = name.replace("compatible.single_client.core", "core")
    name = name.replace("enable_typing_check", "typing_check")
    if name.startswith("oneflow.python."):
        return name.replace("oneflow.python.", "oneflow.")
    elif name == "oneflow.python":
        return "oneflow"
    elif "single_client.python." in name or name.endswith("single_client.python"):
        return name.replace("single_client.python", "single_client")
    else:
        return name

class ExportVisitor(ast.NodeTransformer):
    def __init__(self, root_module="oneflow", src_target_module: str = None) -> None:
        super().__init__()
        self.staging_decorators = []
        self.root_module = root_module
        self.export_modules = {}
        self.top_imports = []
        self.src_target_module = src_target_module

    def append_export(self, target_module=None, node=None):
        if target_module not in self.export_modules:
            module = ast.Module(body=[], type_ignores=[])
            self.export_modules[target_module] = module
        else:
            module = self.export_modules[target_module]
        if isinstance(node, list):
            module.body += node
        else:
            module.body.append(node)

    def visit_Expr(self, node):
        if isinstance(node.value, ast.Constant) and isinstance(node.value.value, str):
            if "Copyright 2020 The OneFlow Authors" in node.value.value:
                return None
        return node

    def visit_ImportFrom(self, node):
        for name in node.names:
            if isinstance(name, ast.alias) and name.name == "oneflow_deprecate":
                return ast.ImportFrom(
                    module="oneflow",
                    names=[ast.alias(name="oneflow_deprecate")],
                    level=0,
                )
        for name in node.names:
            if not self.visit(name):
                return None
        if node.module:
            if node.module == "__future__" or "oneflow_export" in node.module:
                return None
            node.module = replace_str(node.module)
        self.top_imports.append(node)
        return node

    def visit_Import(self, node):
        for name in node.names:
            if not super().visit(name):
                return None
        self.top_imports.append(node)
        return node

    def visit_alias(self, node: ast.alias) -> ast.alias:
        if node.name.startswith("oneflow.python."):
            node.name = replace_str(node.name)
            return node
        elif node.name == "oneflow_export":
            return None
        elif "__export_symbols__" in node.name:
            return None
        else:
            node.name = replace_str(node.name)
            return node

    def visit_Name(self, node: ast.AST):
        if node.id == "oneflow_export":
            return None
        if node.id == "stable_api":
            return None
        if node.id == "experimental_api":
            return None
        return node

    def visit_Call(self, node: ast.AST):
        if not self.visit(node.func):
            return None
        return node

    def visit_ClassDef(self, node):
        node.body = [self.visit(n) for n in node.body]
        return self.visit_FunctionDef(node)

    def visit_FunctionDef(self, node):
        is_compatible_and_experimental = is_compatible_root_module(
            self.root_module
        ) and is_experimental(node)
        if not is_compatible_root_module(self.root_module) and is_stable(node):
            return None
        compact_decorator_list = [self.visit(d) for d in node.decorator_list]
        compact_decorator_list = [d for d in compact_decorator_list if d]
        node.body = [self.visit(n) for n in node.body]
        rkv = ReservedKeywordsVisitor(keywords=set({"int", "float"}))
        rkv.visit(node)
        has_reserved_keyword = rkv.has_reserved_keyword
        is_deprecated = False
        for d in node.decorator_list:
            if is_decorator(d, name="oneflow_deprecate"):
                is_deprecated = True
        for d in node.decorator_list:
            if is_decorator(d, name="register_tensor_op"):
                import_src = ast.parse(f"import {self.src_target_module}")
                self.append_export(target_module=self.root_module, node=import_src)
            if is_decorator(d, name="oneflow_export"):
                is_kept_in_src = (
                    True
                    or has_reserved_keyword
                    or self.src_target_module == target_module
                    or target_module in ["oneflow", "oneflow.scope", COMPATIBLE_MODULE]
                )
                arg0 = d.args[0]
                experimental_module = None
                if is_compatible_and_experimental:
                    experimental_module = "experimental"
                target_module0 = join_module(
                    self.root_module, experimental_module, get_parent_module(arg0.value)
                )
                target_symbol0 = arg0.value.split(".")[-1]

                if ".".join([target_module0, target_symbol0]) == self.src_target_module:
                    raise ValueError(
                        "[colition][both func and module]", self.src_target_module
                    )
                if is_kept_in_src:
                    target_module = self.src_target_module
                    target_symbol = node.name
                else:
                    target_module = target_module0
                    target_symbol = target_symbol0
                # nth export: import from first export
                for argN in d.args[1::]:
                    target_moduleN = join_module(
                        self.root_module,
                        experimental_module,
                        get_parent_module(argN.value),
                    )
                    target_nameN = argN.value.split(".")[-1]
                    assert arg0 != argN, {"arg0": arg0, "argN": argN}
                    import_from_first_export = ast.ImportFrom(
                        module=target_module,
                        names=[ast.alias(name=target_symbol, asname=target_nameN),],
                        level=0,
                    )
                    self.append_export(
                        target_module=target_moduleN, node=import_from_first_export
                    )

                if is_deprecated:
                    import_oneflow_deprecate = ast.ImportFrom(
                        module="oneflow",
                        names=[ast.alias(name="oneflow_deprecate")],
                        level=0,
                    )

                node.decorator_list = compact_decorator_list
                if is_kept_in_src:
                    asname = target_symbol0
                    if node.name == target_symbol0:
                        asname = None
                    if target_module0 == target_module and node.name == target_symbol0:
                        # print("[skip]", target_module0, target_symbol0)
                        pass
                    else:
                        import_from_src = ast.ImportFrom(
                            module=self.src_target_module,
                            names=[ast.alias(name=node.name, asname=asname),],
                            level=0,
                        )
                        self.append_export(
                            target_module=target_module0, node=import_from_src
                        )
                    if is_deprecated:
                        return [import_oneflow_deprecate, node]
                    else:
                        return node
                else:
                    if is_deprecated:
                        self.append_export(
                            target_module=target_module, node=import_oneflow_deprecate
                        )
                    # prepend imports in target module
                    self.append_export(
                        target_module=target_module, node=self.top_imports
                    )
                    if target_module != "oneflow":
                        import_star_from_src = ast.ImportFrom(
                            module=self.src_target_module,
                            names=[ast.alias(name="*")],
                            level=0,
                        )
                        # node.body.insert(0, import_star_from_src)
                        self.append_export(
                            target_module=target_module, node=import_star_from_src
                        )
                    # save func name for src import as before modifing node.name
                    src_asname = None
                    if node.name != target_symbol:
                        src_asname = node.name
                    # save first export in target module
                    node.name = target_symbol
                    self.append_export(target_module=target_module, node=node)

                    # src: import from first export
                    return ast.ImportFrom(
                        module=target_module,
                        names=[ast.alias(name=target_symbol, asname=src_asname),],
                        level=0,
                    )
            if is_decorator(d, name="oneflow_export_value"):
                assert len(node.body) == 2
                assert len(d.args) == 1
                target_module = join_module(
                    self.root_module, get_parent_module(d.args[0].value)
                )
                call = node.body[1].value
                assign = ast.Assign(
                    targets=[
                        ast.Name(id=d.args[0].value.split(".")[-1], ctx=ast.Store())
                    ],
                    value=call,
                )
                self.append_export(target_module=target_module, node=assign)
                # TODO: the doc is not dumped properly
                # doc = node.body[0]
                # self.append_export(target_module=target_module, node=doc)
                return None
        node.decorator_list = compact_decorator_list
        return node

class SrcFile:
    def __init__(self, spec) -> None:
        is_test = "is_test" in spec and spec["is_test"]
        self.export_visitor = None
        self.tree = None
        self.dst = Path(spec["dst"])
        self.src: Path = spec["src"]
        self.target_module = module_from_path(self.dst)
        self.target_module = replace_str(self.target_module)
        if is_test and args.verbose:
            print("[skip test]", self.src)
        else:
            txt = self.src.read_text()
            self.tree = ast.parse(txt)
            root_module = "oneflow"
            if (
                "compatible_single_client_python" in self.src.parts
                or self.src.name == "single_client_init.py"
                or self.src.name == "single_client_main.py"
            ):
                root_module = COMPATIBLE_MODULE
            self.export_visitor = ExportVisitor(
                root_module=root_module, src_target_module=self.target_module
            )
            self.export_visitor.visit(self.tree)
            if self.target_module == root_module:
                self.export_visitor.append_export(
                    target_module=root_module,
                    node=ast.parse(f"from . import distributed"),
                )
                if self.target_module == "oneflow":
                    self.export_visitor.append_export(
                        target_module=root_module,
                        node=ast.parse(f"from . import saved_model"),
                    )
                else:
                    self.export_visitor.append_export(
                        target_module="oneflow.compatible.single_client",
                        node=ast.parse(
                            f"from . import env, scope, math, optimizer, losses, config, layers, summary, random, typing, train, data, profiler, sysconfig, checkpoint, distribute, util, model, image, tensorrt, saved_model, regularizers"
                        ),
                    )
                    self.export_visitor.append_export(
                        target_module="oneflow.compatible.single_client.experimental",
                        node=ast.parse(
                            f"""from . import scope
from oneflow.compatible.single_client import unittest
"""
                        ),
                    )
                    self.export_visitor.append_export(
                        target_module="oneflow.compatible.single_client.deprecated",
                        node=ast.parse(f"from . import nn"),
                    )
                    self.export_visitor.append_export(
                        target_module="oneflow.compatible.single_client.config",
                        node=ast.parse(f"from . import collective_boxing"),
                    )
                    self.export_visitor.append_export(
                        target_module="oneflow.compatible.single_client.unittest",
                        node=ast.parse(f"from . import env"),
                    )
                    self.export_visitor.append_export(
                        target_module="oneflow.compatible.single_client.optimizer",
                        node=ast.parse(
                            f"from . import warmup, loss_scale, grad_clipping"
                        ),
                    )
            #     self.export_visitor.append_export(
            #         target_module=".".join([root_module, "lib.core"]), node=ast.parse(f"from . import async_util")
            #     )

def get_specs_under_python(python_path=None, dst_path=None):
    specs = []
    for p in Path(python_path).rglob("*.py"):
        if p.name == "version.py":
            continue
        rel = p.relative_to(python_path)
        dst = Path(dst_path).joinpath(rel)
        dst = Path(replace_filename(str(dst)))
        spec = {"src": p, "dst": dst}
        if rel.parts[0] == "test":
            spec["is_test"] = True
        specs.append(spec)
    return specs

def get_files():
    srcs = (
        get_specs_under_python(python_path="oneflow/python", dst_path="oneflow")
        + get_specs_under_python(
            python_path="oneflow/compatible_single_client_python",
            dst_path="oneflow/compatible/single_client",
        )
        + [
            {"src": Path("oneflow/init.py"), "dst": "oneflow/__init__.py"},
            {"src": Path("oneflow/__main__.py"), "dst": "oneflow/__main__.py"},
            {
                "src": Path("oneflow/single_client_init.py"),
                "dst": "oneflow/compatible/single_client/__init__.py",
            },
            {
                "src": Path("oneflow/single_client_main.py"),
                "dst": "oneflow/compatible/single_client/__main__.py",
            },
        ]
    )
    srcs = list(filter(lambda x: ("oneflow_export" not in x["src"].name), srcs))
    if args.debug:
        srcs = [
            {
                "src": Path("oneflow/python/ops/nn_ops.py"),
                "dst": "oneflow/ops/nn_ops.py",
            },
            {
                "src": Path("oneflow/python/advanced/distribute_ops.py"),
                "dst": "oneflow/advanced/distribute_ops.py",
            },
        ]
    pool = multiprocessing.Pool()
    srcs = pool.map(SrcFile, srcs,)
    pool.close()
    return srcs

class ModuleNode:
    def __init__(self, name=None, parent=None) -> None:
        self.children = dict()
        self.parent = parent
        self.level = 0
        if parent:
            self.level = parent.level + 1
        self.name = name

    def add_or_get_child(self, name):
        if name in self.children:
            return self.children[name]
        else:
            self.children[name] = ModuleNode(name=name, parent=self)
            return self.children[name]

    @property
    def is_leaf(self):
        return len(self.children.keys()) == 0

    def walk(self, cb):
        cb(self)
        for child in self.children.values():
            child.walk(cb)

    @property
    def leafs(self):
        ret = []

        def add_leafs(node: ModuleNode):
            if node.is_leaf:
                ret.append(node)

        self.walk(add_leafs)
        return ret

    @property
    def full_name(self):
        current_parent = self
        ret = self.name
        while current_parent.parent:
            current_parent = current_parent.parent
            ret = current_parent.name + "." + ret
        return ret

    def __str__(self) -> str:
        return "\n".join(
            [f"{self.full_name}"]
            + [child.__str__() for child in self.children.values()]
        )

    @staticmethod
    def add_sub_module(root=None, module=None):
        parts = module.split(".")
        current_node = root
        assert current_node.name == parts[0]
        for part in parts[1::]:
            current_node = current_node.add_or_get_child(part)

def save_trees(args=None):
    dst: Path = args["dst"]
    trees = args["trees"]
    dst_full = OUT_PATH.joinpath(dst)
    dst_full.parent.mkdir(parents=True, exist_ok=True)
    dst_full.touch(exist_ok=False)
    # TODO: append "doctest.testmod(raise_on_error=True)"
    trees = [ast.fix_missing_locations(tree) for tree in trees]
    if SHOULD_SAVE_AST:
        new_txt = "\n".join([str(astpretty.pformat(tree)) for tree in trees])
        new_txt = f"""from ast import *
{new_txt}
"""
        dst_full.with_suffix(".ast.py").write_text(new_txt)
    new_txt = ""
    if dst.name.startswith("test_"):
        if "compatible" in str(dst):
            new_txt += f"""
import {COMPATIBLE_MODULE}.unittest
"""
        else:
            new_txt += """
import oneflow.unittest
"""
    new_txt += "\n".join([ast.unparse(tree) for tree in trees])
    dst_full.write_text(new_txt)

def append_trees(tree_dict: dict, module: str, tree: ast.AST):
    tree_dict[module] = tree_dict.get(module, [])
    tree_dict[module].append(tree)

if __name__ == "__main__":
    out_oneflow_dir = os.path.join(args.out_dir, "oneflow")
    assert args.out_dir
    assert args.out_dir != "~"
    assert args.out_dir != "/"
    subprocess.check_call(f"mkdir -p {OUT_PATH}", shell=True)

    for py_f in Path(out_oneflow_dir).glob("**/*.py"):
        if py_f.name != "version.py":
            py_f.unlink()
    for pyc in Path(out_oneflow_dir).glob("**/*.pyc"):
        pyc.unlink()
    for d in reversed(
        [
            Path(dirpath)
            for dirpath, dirnames, files in os.walk(out_oneflow_dir)
            if not files and not dirnames
        ]
    ):
        if "include" not in str(d) and "core" not in str(d):
            d.rmdir()

    srcs = get_files()
    final_trees = {}

    root_module = ModuleNode(name="oneflow")
    src_module_added = {}
    for s in srcs:
        # src
        append_trees(tree_dict=final_trees, module=s.target_module, tree=s.tree)
        if (
            str(s.src) == "oneflow/python/__init__.py"
            or str(s.src) == "oneflow/compatible_single_client_python/__init__.py"
        ):
            assert not s.src.read_text()
            continue
        assert s.target_module not in src_module_added, {
            "target_module": s.target_module,
            "new": str(s.src),
            "exist": str(src_module_added[s.target_module]),
        }
        src_module_added[s.target_module] = s.src
        ModuleNode.add_sub_module(root=root_module, module=s.target_module)
    for s in srcs:
        # exports
        for export_path, export_tree in s.export_visitor.export_modules.items():
            append_trees(tree_dict=final_trees, module=export_path, tree=export_tree)
            ModuleNode.add_sub_module(root=root_module, module=export_path)
    leaf_modules = set([leaf.full_name for leaf in root_module.leafs])
    pool = multiprocessing.Pool()

    def is_init(module: str):
        is_leaf = module in leaf_modules
        is_magic = module.endswith("__")
        return is_leaf == False and is_magic == False

    srcs = pool.map(
        save_trees,
        [
            {"dst": path_from_module(module, is_init=is_init(module)), "trees": trees,}
            for module, trees in final_trees.items()
        ],
    )
    pool.close()
    # TODO: touch __init__.py, oneflow/F/__init__.py
    Path(os.path.join(OUT_PATH, "oneflow", "F")).mkdir(exist_ok=True)
    Path(os.path.join(OUT_PATH, "oneflow", "F/__init__.py")).touch()
    Path(os.path.join(OUT_PATH, COMPATIBLE_MODULE.replace(".", "/"), "F")).mkdir(
        parents=True, exist_ok=True
    )
    Path(
        os.path.join(OUT_PATH, COMPATIBLE_MODULE.replace(".", "/"), "F/__init__.py")
    ).touch()
    Path(
        os.path.join(OUT_PATH, COMPATIBLE_MODULE.replace(".", "/"), "experimental/F")
    ).mkdir(parents=True, exist_ok=True)
    Path(
        os.path.join(
            OUT_PATH, COMPATIBLE_MODULE.replace(".", "/"), "experimental/F/__init__.py"
        )
    ).touch()
    Path(os.path.join(OUT_PATH, f"oneflow/compatible/__init__.py")).touch()
    # step 1: extract all exports
    # step 2: merge exports into src in python/
    # step 4: finalize __all__, if it is imported by another module or wrapped in 'oneflow.export', it should appears in __all__
    # step 5: save file and post process (sort imports, format, etc)
    extra_arg = ""
    if args.verbose == False:
        extra_arg += "--quiet"
    if args.autoflake:
        print("[postprocess]", "autoflake")
        subprocess.check_call(
            f"{sys.executable} -m autoflake --in-place --remove-all-unused-imports --exclude '**/*.ast.py' --recursive .",
            shell=True,
            cwd=args.out_dir,
        )
    if args.isort:
        print("[postprocess]", "isort")
        subprocess.check_call(
            f"{sys.executable} -m isort --skip oneflow/utils/data/__init__.py . {extra_arg}",
            shell=True,
            cwd=args.out_dir,
        )
    if args.license:
        print("[postprocess]", "license")
        subprocess.check_call(
            f"`which python3` ci/check/run_license_format.py -i {OUT_PATH} --fix --silent",
            shell=True,
        )
    if args.black:
        print("[postprocess]", "black")
        subprocess.check_call(
            f"`which python3` -m black --exclude '\\.ast\\.py' . {extra_arg}",
            shell=True,
            cwd=args.out_dir,
        )


import random
#import defs

rlist = []
copies = []
counter = 0
i = 0
user_range = int(input('range: '))
user_rand_range = int(input('rand range: '))
if user_range > user_rand_range:
	print('Sorry, not possible!')
else:
	while i < user_range:
		rnum = random.randrange(1, user_rand_range, 1)
		if i == 0:
			rlist.append(rnum)
			i += 1
		else:
			if rnum not in rlist:
				rlist.append(rnum)
				i += 1
			else:
				copies.append(rnum)
				counter += 1
				continue

print(rlist)
#print('\n')
print(copies)
print(counter)
mylist = [7, 17, 20]
print('Items from mylist not found:')
for item in mylist:
	if item not in rlist:
		print(item, end=" ")


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Licensed under the GNU General Public License, version 3.
# See the file http://www.gnu.org/licenses/gpl.txt

from inary.actionsapi import mesontools
from inary.actionsapi import inarytools
from inary.actionsapi import shelltools
from inary.actionsapi import get

if get.buildTYPE()=="emul32":
    shelltools.export("CC", "gcc -m32")
    shelltools.export("CXX", "g++ -m32")
    shelltools.export("LDFLAGS","-m32")
    shelltools.export("PKG_CONFIG_PATH", "/usr/lib32/pkgconfig")

def setup():
    shelltools.export("CFLAGS", "{} -DG_DISABLE_CAST_CHECKS".format(get.CFLAGS()))
    options="-Dselinux=disabled -Dman=false -Ddoc=false -Dinternal_pcre=true\
            "
    if get.buildTYPE()=="emul32":
        shelltools.system("patch -p1 < multilib.patch")
    mesontools.meson_configure(options)

def build():
    if get.buildTYPE()=="emul32":
        shelltools.export("CC", "gcc -m32 -mstackrealign -mfpmath=sse")
        shelltools.export("CXX", "g++ -m32 -mstackrealign -mfpmath=sse")
        shelltools.export("PKG_CONFIG_PATH", "/usr/lib32/pkgconfig")

    mesontools.ninja_build()

def install():
    if get.buildTYPE()=="emul32":
        shelltools.system('DESTDIR="{}/emul32" ninja install -C inaryPackageBuild'.format(get.installDIR()))
        inarytools.domove("/emul32/usr/lib32", "/usr/")
        inarytools.domove("/emul32/usr/bin/*", "/usr/bin/32")
        inarytools.removeDir("/emul32")
        return
    else:
        mesontools.ninja_install()