import sys
sys.path.append('../')

from nltk.corpus import wordnet as wn
import pandas as pd
import json
import os

import config
from utils import create_uri, extract_wn_version_id, map_v31_to_v30, create_df_with_wordnet_nodes

cnfile='../input/conceptnet/conceptnet-en-with-externalurl.csv'
data_source=config.wn_ds
weight="1.0"
VERSION=config.VERSION

EDGE_COLS=config.edges_cols
NODE_COLS=config.nodes_cols

cn_nodes_file=f'../output_v{VERSION}/conceptnet/nodes_v{VERSION}.csv'
wn_nodes_file=f'../output_v{VERSION}/wordnet/nodes_v{VERSION}.csv'
vg_nodes_file=f'../output_v{VERSION}/visualgenome/nodes_v{VERSION}.csv'

wordnet30_ili_file='../input/mappings/ili-map-pwn30.tab'
wordnet31_ili_file='../input/mappings/ili-map-pwn31.tab'

# OUTPUT FILE
output_dir=f'../output_v{VERSION}/mappings'
edges_file=f'{output_dir}/wn_wn_mappings.csv'

MOWGLI_NS=config.mowgli_ns
WORDNET_NS=config.wordnet_ns

SAMEAS_REL=create_uri(MOWGLI_NS, config.sameas)

if not os.path.exists(output_dir):
    os.makedirs(output_dir)

### Load the CN filtered data in pandas ###

df=pd.read_csv(cnfile, sep='\t', header=None, converters={4: json.loads})
df.columns=['assertion','rel','subj','obj','metadata']
df.drop(columns=['assertion'])
print('size of df with external URLs', len(df))

df_wordnet=df.loc[(df['rel'] == '/r/ExternalURL') & (df['obj'].str.contains(r'http://wordnet-'))]
print('size of df with wordnet external links', len(df_wordnet))

### Get WordNet nodes ###

all_nodes=set()
wn_nodes=set()

with open(cn_nodes_file, 'r') as f:
    for line in f:
        first=line.split('\t')[0]
        all_nodes.add(first)

with open(wn_nodes_file, 'r') as f:
    for line in f:
        first=line.split('\t')[0]
        all_nodes.add(first)
        wn_nodes.add(first)
wn_nodes_df=pd.read_csv(wn_nodes_file, sep='\t', header=0, converters={5: eval})

with open(vg_nodes_file, 'r') as f:
    for line in f:
        first=line.split('\t')[0]
        if first.startswith('wn:'):
            all_nodes.add(first)
            wn_nodes.add(first)

print('wordnet30 nodes:', len(wn_nodes))

### Load Wordnet mapings 3.0 to 3.1 ###

mapping_31_30=map_v31_to_v30(wordnet31_ili_file, wordnet30_ili_file)
print('v31 to v30 mappings', len(mapping_31_30))

not_in_ili_nodes=set()
missing_nodes=set()
all_edges=[]
for i, row in df_wordnet.iterrows():
    wn_version, wn_offset_id=extract_wn_version_id(row['obj'])
    if wn_version=='wn31':
        if wn_offset_id not in mapping_31_30.keys():
            not_in_ili_nodes.add(wn_offset_id)
        else:
            wn_30_id=mapping_31_30[wn_offset_id]
            offset, pos=wn_30_id.split('-')
            wn_30_synset=wn.synset_from_pos_and_offset(pos,int(offset)).name()
            wn_30_synset_uri=create_uri(WORDNET_NS, wn_30_synset)

            #if row['subj'] in all_nodes:# and
            if wn_30_synset_uri in wn_nodes:
                an_edge=[row['subj'], SAMEAS_REL, wn_30_synset_uri, data_source, weight, {}]
                all_edges.append(an_edge)
            else:
                missing_nodes.add(wn_30_synset_uri)
    else:
        print('Other WN version', wn_version)

print('missing ILI index nodes version 31', len(not_in_ili_nodes))
print('nodes missing in the node lists of WordNet and VG', len(missing_nodes))

missing_nodes_df=create_df_with_wordnet_nodes(missing_nodes, data_source, NODE_COLS)
combined_wn_nodes_df=pd.concat([wn_nodes_df, missing_nodes_df])
combined_wn_nodes_df.sort_values('id').to_csv(wn_nodes_file, index=False, sep='\t')

edges_df = pd.DataFrame(all_edges, columns = EDGE_COLS)
edges_df.sort_values(by=['subject', 'predicate','object']).to_csv(edges_file, index=False, sep='\t')

print('Number of edges', len(edges_df))


import os
from pathlib import Path

from hydra import compose, initialize
from hydra.core.config_store import ConfigStore
import torch
import wandb

from shared.configs import BaseConfig, register_configs
from stratification.harness import GEORGEHarness
from stratification.utils.parse_args import get_config
from stratification.utils.utils import init_cuda, set_seed

cs = ConfigStore.instance()
register_configs()

def main() -> None:
    config = get_config()
    with initialize(config_path="../configs"):
        hydra_config = compose(
            config_name="biased_data",
            overrides=[f"data={config['data_config']}", f"bias={config['bias_config']}"],
        )
        hydra_config["data"]["data_split_seed"] = config.get("data_split_seed", config["seed"])
        print(hydra_config)
        biased_data_config = BaseConfig.from_hydra(hydra_config)

    use_cuda = config['use_cuda'] and torch.cuda.is_available()
    set_seed(config['seed'], use_cuda)  # set seeds for reproducibility
    init_cuda(config['deterministic'], config['allow_multigpu'])

    # Initialize wandb with online-logging as the default
    local_dir = Path(".", "local_logging")
    local_dir.mkdir(exist_ok=True)
    if config.get("log_offline", False):
        os.environ["WANDB_MODE"] = "dryrun"
    cluster_model_name = config["cluster_config"]["model"]
    wandb.init(
        entity="predictive-analytics-lab",
        project="suds",
        dir=str(local_dir),
        config=config,
        reinit=True,
        group=config.get("group", f"{config['dataset']}.GEORGE.{cluster_model_name}"),
    )

    torch.multiprocessing.set_sharing_strategy('file_system')
    harness = GEORGEHarness(config, use_cuda=use_cuda)
    harness.save_full_config(config)

    first_mode = 'erm' if (config['mode'] == 'george') else config['mode']
    dataloaders = harness.get_dataloaders(
        config=config, data_config=biased_data_config, mode=first_mode, use_cuda=use_cuda
    )
    num_classes = dataloaders['train'].dataset.get_num_classes('superclass')
    model = harness.get_nn_model(config, num_classes=num_classes, mode=first_mode)

    activ_done = config['activations_dir'] != 'NONE'
    rep_done = config['representation_dir'] != 'NONE'
    cluster_done = config['cluster_dir'] != 'NONE'
    rep_done = (
        rep_done or cluster_done
    )  # if we already have clusters, don't need to do reduction step
    activ_done = (
        activ_done or rep_done
    )  # don't need to get activations if we already have reduced ones
    if config['classification_config']['eval_only']:
        assert activ_done
        if config['cluster_dir'] != 'NONE':
            dataloaders = harness.get_dataloaders(
                config=config,
                data_config=biased_data_config,
                mode=first_mode,
                use_cuda=use_cuda,
                subclass_labels=os.path.join(config['cluster_dir'], 'clusters.pt')
                if os.path.isdir(config['cluster_dir'])
                else config['cluster_dir'],
            )

    # Train a model with ERM
    if activ_done and not (
        config['classification_config']['eval_only']
        or config['classification_config']['save_act_only']
    ):
        erm_dir = config['activations_dir']
    else:
        if (
            config['classification_config']['eval_only']
            or config['classification_config']['save_act_only']
        ):
            erm_dir = config['activations_dir']
            model_path = os.path.join(
                erm_dir, f'{config["classification_config"]["eval_mode"]}_model.pt'
            )
            print(f'Loading model from {model_path}...')
            model.load_state_dict(torch.load(model_path)['state_dict'])
        erm_dir = harness.classify(
            config['classification_config'], model, dataloaders, mode=first_mode
        )

    if (
        config['classification_config']['eval_only']
        or config['classification_config']['save_act_only']
    ):
        exit()

    if config['mode'] == 'george':
        if not config['classification_config']['bit_pretrained'] and not rep_done:
            model.load_state_dict(torch.load(os.path.join(erm_dir, 'best_model.pt'))['state_dict'])

        set_seed(config['seed'], use_cuda)
        # Dimensionality-reduce the model activations
        if rep_done:
            reduction_dir = config['representation_dir']
        else:
            reduction_model = harness.get_reduction_model(config, nn_model=model)
            reduction_dir = harness.reduce(
                config['reduction_config'],
                reduction_model,
                inputs_path=os.path.join(erm_dir, 'outputs.pt'),
            )

        # Cluster the per-superclass features
        if cluster_done:
            cluster_dir = config['cluster_dir']
        else:
            cluster_model = harness.get_cluster_model(config)
            cluster_dir = harness.cluster(
                config['cluster_config'],
                cluster_model,
                inputs_path=os.path.join(reduction_dir, 'outputs.pt'),
            )

        set_seed(config['seed'], use_cuda)  # reset random state
        cluster_label_path = os.path.join(cluster_dir, 'clusters.pt')
        dataloaders = harness.get_dataloaders(
            config,
            mode='george',
            data_config=biased_data_config,
            subclass_labels=cluster_label_path,
            use_cuda=use_cuda,
        )
        model = harness.get_nn_model(config, num_classes=num_classes, mode='george')

        # Train the final (GEORGE) model
        george_dir = harness.classify(
            config['classification_config'], model, dataloaders, mode='george'
        )

if __name__ == '__main__':
    main()


class Solution:
	def superEggDrop(self, K: int, N: int) -> int:
		cache = {}

		def dp(k,n):
			if k==1: return n
			if n==0: return 0

			if (k,n) in cache:
				return cache[(k,n)]

			# res = float('inf')
			# for x in range(1, n+1):
			# 	res = min(max(dp(k-1, x-1),dp(k, n-x))+1,res)

			res = float('inf')
			lo, hi = 1, n
			while lo <= hi:
				mid = (lo+hi) // 2

				broken = dp(k - 1, mid - 1)
				not_broken = dp(k, n - mid)

				if broken > not_broken:
					hi = mid - 1
					res = min(res, broken + 1)
				else:
					lo = mid + 1
					res = min(res, not_broken + 1)

			cache[(k,n)] = res
			return res

		return dp(K, N)

if __name__ == '__main__':
	K = 6
	N = 5000

	res = Solution().superEggDrop(K, N)

	print(res)

leetcode-algorithms/887. Super Egg Drop/solution.py


from copy import deepcopy
from functools import lru_cache
class Solution:
    def shortestSuperstring(self, A: List[str]) -> str:
        def ext(s1, s2):
            l1, l2 = len(s1), len(s2)
            kkk, le = 0, float('inf')
            for k in range(min(l1, l2)):
                if s1[len(s1) - k:] == s2[0 : k]:
                    nc = l1 + l2 - k
                    if nc < le:
                        kkk = k
                        le = nc
            return kkk # means save first one k chars

        l = len(A)
        g = [[0 for _ in range(l)] for _ in range(l)]

        for i in range(l):
            for j in range(i + 1, l):
                g[i][j] = ext(A[i], A[j])
                g[j][i] = ext(A[j], A[i])

        rs, ct = "", -1
        vs = 2 ** l - 1
        path = []
        ks = {}
        su = 0
        for i in range(l):
            ks[2 ** i] = i
            su += 2 ** i

        @lru_cache(None)
        def dp(i, hd): # is means state, hd means head
            nonlocal l, ks, g
            if i - (1 << hd) in ks.keys(): # i is the last node to visit
                gv = g[hd][ks[i - (1 << hd)]] # (what's next and how many saved by going this way)
                return [None, [ks[i - hd], gv]]
            i2 = i - (1 << hd)
            le, prev, idx = -1, [], 0
            for j in range(l):
                d = 1 << j
                if i2 & d > 0:
                    (prv2, cur) = dp(i2, j)
                    if le < cur[1] + g[hd][j]:
                        le = cur[1] + g[hd][j]
                        prev = [prv2, cur]
                        idx = j
            return [prev, [idx, le]]

        r = ""
        # print(path)
        maxl, st = 0, []
        for i in range(l):
            cur = dp(su, i)
            if cur[1][1] > maxl:
                maxl = cur[1][1]
                st = [cur, [i, 0]]

        (prev, cur) = st
        while cur != None:
            # (prev, cur) = s
            r += A[cur[0]] if prev == None else A[cur[0]][:len(A[cur[0]]) - prev[1][1]]

            if prev == None:
                break
            (prev, cur) = prev

        return r

"ttccatgcatc"
"gctaagttcatgcatc"


def htest():
    i = 1
    while i < 4:
        n = yield i
        if i == 3:
            return 100
        i += 1

def itest():
    val = yield from htest()
    print("itest",val)

t = itest()
x=t.send(None)

j = 0
while j < 3:
    j += 1
    try:
        t.send(j)
    except StopIteration as e:
        print('异常了')


#
# Copyright 2018-2023 - Swiss Data Science Center (SDSC)
# A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and
# Eidgenössische Technische Hochschule Zürich (ETHZ).
#
# 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.
"""Custom console echo."""

import functools
import os

import click

INFO = click.style("Info: ", bold=True, fg="blue")
WARNING = click.style("Warning: ", bold=True, fg="yellow")
ERROR = click.style("Error: ", bold=True, fg="red")

def echo_via_pager(*args, **kwargs):
    """Display pager only if it does not fit in one terminal screen.

    NOTE: The feature is available only on ``less``-based pager.
    """
    restore = "LESS" not in os.environ
    try:
        os.environ.setdefault("LESS", "-iXFR")
        click.echo_via_pager(*args, **kwargs)
    finally:
        if restore:
            os.environ.pop("LESS", None)

progressbar = functools.partial(
    click.progressbar, fill_char=click.style(" ", bg="green"), show_pos=True, item_show_func=lambda x: x
)

def yellow_text(text: str) -> str:
    """Return text in yellow."""
    return click.style(text, fg="yellow")

def red_text(text: str) -> str:
    """Return text in red."""
    return click.style(text, fg="red")


# Copyright 2018 Changan Wang

# 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.
# =============================================================================from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import tensorflow as tf

from . import resnet_v2
from . import depth_conv2d

#initializer_to_use = tf.glorot_uniform_initializer
initializer_to_use = tf.glorot_normal_initializer
conv_bn_initializer_to_use = tf.glorot_normal_initializer#lambda : tf.truncated_normal_initializer(mean=0.0, stddev=0.005)

def dilate_conv2d(inputs, filters, kernel_size, dilation_rate, data_format):
  """Strided 2-D convolution with explicit padding."""
  # The padding is consistent and is based only on `kernel_size`, not on the
  # dimensions of `inputs` (as opposed to using `tf.layers.conv2d` alone).
  return tf.layers.conv2d(
      inputs=inputs, filters=filters, kernel_size=kernel_size, strides=1, dilation_rate = dilation_rate,
      padding='SAME', use_bias=False,#True,
      kernel_initializer=conv_bn_initializer_to_use(),
      bias_initializer=None,#tf.zeros_initializer(),
      data_format=data_format)

def xdet_bottleneck_block(inputs, filters, is_training, projection_shortcut,
                     dilation_rate, data_format):
  """Bottleneck block variant for residual networks with BN before convolutions.

  Args:
    inputs: A tensor of size [batch, channels, height_in, width_in] or
      [batch, height_in, width_in, channels] depending on data_format.
    filters: The number of filters for the first two convolutions. Note that the
      third and final convolution will use 4 times as many filters.
    is_training: A Boolean for whether the model is in training or inference
      mode. Needed for batch normalization.
    projection_shortcut: The function to use for projection shortcuts (typically
      a 1x1 convolution when downsampling the input).
    dilation_rate: The block's stride. If greater than 1, this block will ultimately
      downsample the input.
    data_format: The input format ('channels_last' or 'channels_first').

  Returns:
    The output tensor of the block.
  """
  shortcut = inputs
  inputs = resnet_v2.batch_norm_relu(inputs, is_training, data_format)

  # The projection shortcut should come after the first batch norm and ReLU
  # since it performs a 1x1 convolution.
  if projection_shortcut is not None:
    shortcut = projection_shortcut(inputs)

  inputs = resnet_v2.conv2d_fixed_padding(
      inputs=inputs, filters=filters, kernel_size=1, strides=1,
      data_format=data_format, kernel_initializer=conv_bn_initializer_to_use)

  inputs = resnet_v2.batch_norm_relu(inputs, is_training, data_format)
  inputs = dilate_conv2d(
      inputs=inputs, filters=filters, kernel_size=3, dilation_rate=dilation_rate, data_format=data_format)

  inputs = resnet_v2.batch_norm_relu(inputs, is_training, data_format)
  # default activation is None
  inputs = resnet_v2.conv2d_fixed_padding(
      inputs=inputs, filters=4 * filters, kernel_size=1, strides=1,
      data_format=data_format, kernel_initializer=conv_bn_initializer_to_use)

  return inputs + shortcut

def xdet_block_layer(inputs, filters, block_fn, blocks, dilation_rate, is_training, name,
                data_format):
  """Creates one layer of blocks for the ResNet model.

  Args:
    inputs: A tensor of size [batch, channels, height_in, width_in] or
      [batch, height_in, width_in, channels] depending on data_format.
    filters: The number of filters for the first convolution of the layer.
    block_fn: The block to use within the model, either `building_block` or
      `bottleneck_block`.
    blocks: The number of blocks contained in the layer.
    dilation_rate: The stride to use for the first convolution of the layer. If
      greater than 1, this layer will ultimately downsample the input.
    is_training: Either True or False, whether we are currently training the
      model. Needed for batch norm.
    name: A string name for the tensor output of the block layer.
    data_format: The input format ('channels_last' or 'channels_first').

  Returns:
    The output tensor of the block layer.
  """
  # Bottleneck blocks end with 4x the number of filters as they start with
  filters_out = 4 * filters if block_fn is xdet_bottleneck_block else filters

  def projection_shortcut(inputs):
    return tf.layers.conv2d(
                            inputs=inputs, filters=filters_out, kernel_size=1, strides=1,
                            padding='SAME', use_bias=False,
                            kernel_initializer=initializer_to_use(),
                            data_format=data_format)

  # Only the first block per block_layer uses projection_shortcut and dilation_rate
  inputs = block_fn(inputs, filters, is_training, projection_shortcut, dilation_rate, data_format)

  for _ in range(1, blocks):
    inputs = block_fn(inputs, filters, is_training, None, dilation_rate, data_format)

  return tf.identity(inputs, name)

def xdet_resnet_v2_generator(block_fn, layers, data_format=None):
  """Generator for X-Det ResNet v2 models.

  Args:
    block_fn: The block to use within the model, either `building_block` or
      `bottleneck_block`.
    layers: A length-4 array denoting the number of blocks to include in each
      layer. Each layer consists of blocks that take inputs of the same size.
    data_format: The input format ('channels_last', 'channels_first', or None).
      If set to None, the format is dependent on whether a GPU is available.

  Returns:
    The model function that takes in `inputs` and `is_training` and
    returns the output tensor of the ResNet model.
  """
  if data_format is None:
    data_format = (
        'channels_first' if tf.test.is_built_with_cuda() else 'channels_last')

  def model(inputs, is_training):
    """Constructs the ResNet model given the inputs."""
    # we do this in preprocess func
    #if data_format == 'channels_first':
      # Convert the inputs from channels_last (NHWC) to channels_first (NCHW).
      # This provides a large performance boost on GPU. See
      # https://www.tensorflow.org/performance/performance_guide#data_formats
      #inputs = tf.transpose(inputs, [0, 3, 1, 2])

    inputs = resnet_v2.conv2d_fixed_padding(
        inputs=inputs, filters=64, kernel_size=7, strides=2,
        data_format=data_format, kernel_initializer=conv_bn_initializer_to_use)
    inputs = tf.identity(inputs, 'initial_conv')
    inputs = tf.layers.max_pooling2d(
        inputs=inputs, pool_size=3, strides=2, padding='SAME',
        data_format=data_format)
    inputs = tf.identity(inputs, 'initial_max_pool')

    inputs = resnet_v2.block_layer(
        inputs=inputs, filters=64, block_fn=block_fn, blocks=layers[0],
        strides=1, is_training=is_training, name='block_layer1',
        data_format=data_format)
    inputs = resnet_v2.block_layer(
        inputs=inputs, filters=128, block_fn=block_fn, blocks=layers[1],
        strides=2, is_training=is_training, name='block_layer2',
        data_format=data_format)
    output_conv4 = xdet_block_layer(
        inputs=inputs, filters=256, block_fn=xdet_bottleneck_block, blocks=layers[2],
        dilation_rate=2, is_training=is_training, name='block_layer3',
        data_format=data_format)
    output_conv5 = xdet_block_layer(
        inputs=output_conv4, filters=512, block_fn=xdet_bottleneck_block, blocks=layers[3],
        dilation_rate=4, is_training=is_training, name='block_layer4',
        data_format=data_format)
    with tf.variable_scope('xdet_additional_conv', default_name = None, values = [output_conv5], reuse=tf.AUTO_REUSE):
      output_conv6 = xdet_block_layer(
          inputs=output_conv5, filters=512, block_fn=xdet_bottleneck_block, blocks=layers[4],
          dilation_rate=8, is_training=is_training, name='block_layer5',
          data_format=data_format)
    with tf.variable_scope('xdet_multi_path', default_name = None, values = [output_conv4, output_conv5, output_conv6], reuse=tf.AUTO_REUSE):
      output_conv4 = resnet_v2.batch_norm_relu(output_conv4, is_training, data_format)
      output_conv4 = tf.layers.conv2d(inputs=output_conv4, filters=256, kernel_size=1, strides=1,
                                  padding='SAME', use_bias=True, activation=None,
                                  kernel_initializer=initializer_to_use(),
                                  bias_initializer=tf.zeros_initializer(),
                                  data_format=data_format)
      output_conv5 = resnet_v2.batch_norm_relu(output_conv5, is_training, data_format)
      output_conv5 = tf.layers.conv2d(inputs=output_conv5, filters=256, kernel_size=1, strides=1,
                                  padding='SAME', use_bias=True, activation=None,
                                  kernel_initializer=initializer_to_use(),
                                  bias_initializer=tf.zeros_initializer(),
                                  data_format=data_format)
      output_conv6 = resnet_v2.batch_norm_relu(output_conv6, is_training, data_format)
      output_conv6 = tf.layers.conv2d(inputs=output_conv6, filters=256, kernel_size=1, strides=1,
                                  padding='SAME', use_bias=True, activation=None,
                                  kernel_initializer=initializer_to_use(),
                                  bias_initializer=tf.zeros_initializer(),
                                  data_format=data_format)
      if data_format == 'channels_first':
        concat_output = tf.concat([output_conv4, output_conv5, output_conv6], axis = 1)
      else:
        concat_output = tf.concat([output_conv4, output_conv5, output_conv6], axis = -1)
      # use batch_norm or only Relu? (modify the bias above)
      inputs = tf.nn.relu(concat_output)
      #inputs = resnet_v2.batch_norm_relu(concat_output, is_training, data_format)

      inputs = depth_conv2d.depth_conv2d(inputs, 3, stride=1,
                                      padding='SAME',
                                      data_format=(depth_conv2d.DATA_FORMAT_NCHW if data_format == 'channels_first' else depth_conv2d.DATA_FORMAT_NHWC),
                                      rate=1,
                                      activation_fn=None,
                                      normalizer_fn=None,
                                      weights_initializer=initializer_to_use())
      # it has been proved in Xception that BN and Relu after depth-wise conv may lose information
      # so they just add that in the whole seperable conv
      # but is it better to add BN or Relu here? (both BN and Relu will cut-off information)
      # return bresnet_v2.atch_norm_relu(concat_output, is_training, data_format)
      return inputs

  return model

def pred_inception_module(net_input, depth_output, is_training, data_format, var_scope):
  with tf.variable_scope(var_scope):
    with tf.variable_scope('Branch_0'):
      branch_0 = tf.layers.conv2d(inputs=net_input, filters=depth_output, kernel_size=3, strides=1,
                                  padding='SAME', use_bias=True, activation=None,
                                  kernel_initializer=conv_bn_initializer_to_use(),
                                  bias_initializer=tf.zeros_initializer(),
                                  data_format=data_format)
    with tf.variable_scope('Branch_1'):
      branch_1 = tf.layers.conv2d(inputs=net_input, filters=depth_output, kernel_size=1, strides=1,
                                  padding='SAME', use_bias=True, activation=None,
                                  kernel_initializer=conv_bn_initializer_to_use(),
                                  bias_initializer=tf.zeros_initializer(),
                                  data_format=data_format)

    if data_format == 'channels_first':
      net_input = tf.concat([branch_0, branch_1], axis = 1)
    else:
      net_input = tf.concat([branch_0, branch_1], axis = -1)

    return resnet_v2.batch_norm_relu(net_input, is_training, data_format)

def xdet_head(body_input, num_classes, num_anchors, is_training, data_format=None):
  with tf.variable_scope('xdet_head', default_name = None, values = [body_input], reuse=tf.AUTO_REUSE):
    if data_format is None:
      data_format = ('channels_first' if tf.test.is_built_with_cuda() else 'channels_last')

    def pred_submodule(input_feature, output_channals):
      inputs = resnet_v2.conv2d_fixed_padding(inputs=input_feature, filters=output_channals, kernel_size=3, strides=1, data_format=data_format, kernel_initializer=conv_bn_initializer_to_use)
      return resnet_v2.batch_norm_relu(inputs, is_training, data_format)

    # never cause information bottleneck
    # classification module
    # cls_inputs = body_input
    # cls_depth_list = [512, 256]
    # for depth_ in cls_depth_list:
    #   if num_anchors * num_classes < 0.8 * depth_:
    #     cls_inputs = pred_submodule(cls_inputs, depth_)

    cls_inputs = pred_inception_module(body_input, 256, is_training, data_format, 'inception_1')
    cls_inputs = pred_inception_module(cls_inputs, 256, is_training, data_format, 'inception_2')

    # if num_anchors * num_classes < 1024:
    #   cls_inputs = pred_submodule(cls_inputs, 1024)
    # if num_anchors * num_classes < 512:
    #   cls_inputs = pred_submodule(cls_inputs, 512)
    # if num_anchors * num_classes < 256:
    #   cls_inputs = pred_submodule(cls_inputs, 256)

    cls_outputs = tf.layers.conv2d(inputs=cls_inputs, filters=num_anchors * num_classes, kernel_size=3, strides=1,
                                padding='SAME', use_bias=True, activation=None,
                                kernel_initializer=initializer_to_use(),
                                data_format=data_format)

    # regress module
    regress_inputs = body_input
    regress_depth_list = [512, 256]
    for depth_ in regress_depth_list:
      if num_anchors < depth_/(4 + 1):
        regress_inputs = pred_submodule(regress_inputs, depth_)

    regress_outputs = tf.layers.conv2d(
                                inputs=regress_inputs, filters=num_anchors * 4, kernel_size=3, strides=1,
                                padding='SAME', use_bias=True, activation=None,
                                kernel_initializer=initializer_to_use(), bias_initializer=tf.zeros_initializer(),
                                data_format=data_format)

    return tf.identity(cls_outputs, 'class_module'), tf.identity(regress_outputs, 'location_module')

def xdet_resnet_v2(resnet_size, data_format=None):
  """Returns the ResNet model for a given size and number of output classes."""
  model_params = {
      18: {'block': resnet_v2.building_block, 'layers': [2, 2, 2, 2, 2]},
      34: {'block': resnet_v2.building_block, 'layers': [3, 4, 6, 3, 3]},
      50: {'block': resnet_v2.bottleneck_block, 'layers': [3, 4, 6, 3, 3]},
      101: {'block': resnet_v2.bottleneck_block, 'layers': [3, 4, 23, 3, 3]},
      152: {'block': resnet_v2.bottleneck_block, 'layers': [3, 8, 36, 3, 3]},
      200: {'block': resnet_v2.bottleneck_block, 'layers': [3, 24, 36, 3, 3]}
  }

  if resnet_size not in model_params:
    raise ValueError('Not a valid resnet_size:', resnet_size)

  params = model_params[resnet_size]
  return xdet_resnet_v2_generator(
      params['block'], params['layers'], data_format)


import math
import pymongo
import urllib.parse
import requests
import json

class Zl_axjx():

    def __init__(self):
        self.client = pymongo.MongoClient('localhost')
        self.db = self.client['YUAN_QU']
        self.base_url = "https://fe-api.zhaopin.com/c/i/sou"

    def get_ajax_data(self,url,i,city_code,kw):
        headers = {
            'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'}

        params = {
            "start": i * 90,
            "pageSize": 90,
            "cityId": city_code,
            "kw": kw,
            "kt": 3
        }
        response = requests.get(url, headers=headers, params=params)
        print("number page ======", i, "=======")
        print(len(json.loads(response.text)["data"]["results"]))
        for datas in json.loads(response.text)["data"]["results"]:
            # print(datas)
            item = {}
            item["positionURL"] = datas["positionURL"] if datas["positionURL"] else None
            item["name"] = datas["company"]["name"] if datas["company"]["name"] else None
            item["updateDate"] = datas["updateDate"] if datas["updateDate"] else None
            item["jobName"] = datas["jobName"] if datas["jobName"] else None
            item["display"] = datas["city"]["display"] if datas["city"]["display"] else None
            item["companyURL"] = datas["company"]["url"] if datas["company"]["url"] else None
            print(item)
            self.save_to_mongo(item)

    def get_page_num(self,url,city_code, kw):
        headers = {
            'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'}

        params = {
            "start": 0,
            "pageSize": 90,
            "cityId": city_code,
            "kw": kw,
            "kt": 3
        }
        response = requests.get(url, headers=headers, params=params)
        try:
            num = json.loads(response.text)["data"]["numFound"]
            if int(num) > 90:
                page_num = math.ceil(int(num) / 90)
                if page_num > 12:
                    end_num = 12
                else:
                    end_num = page_num
            else:
                end_num = 1
            return end_num
        except KeyError:
            pass

    def save_to_mongo(self,data):
        if self.db['zhilian'].update({'name': data['name']}, {'$set': data}, True):
            print('Save to Mongodb', data['name'])
        else:
            print('Saved to Mongodb Failed', data['name'])

    def run(self):
        with open("/home/parrot/PycharmProjects/zhaopin_web/zhilian/A_code.txt","r") as f:
            for str_data in f.readlines():
                data = json.loads(str_data.strip('\n'))
                city_id = data["code"]
                page_number = self.get_page_num(self.base_url,city_id,"创业")
                for i in range(page_number):
                    self.get_ajax_data(self.base_url,i,city_id,"创业")

if __name__ == '__main__':
    Z = Zl_axjx()
    Z.run()

# paras = {
#         'jl': 530,
#         're': 2005,
#         'kw': 'python',
#         'kt': 3
#    }
# headers = {
#        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
#        # 'Host': 'sou.zhaopin.com',
#        # 'Referer': 'https://www.zhaopin.com/',
#    }
#
# url = 'https://company.zhaopin.com/CZ680574820.htm'
# try:
#    # 获取网页内容，返回html数据
#    response = requests.get(url, headers=headers)
#    response.encoding = "utf-8"
#    # 通过状态码判断是否获取成功
#    if response.status_code == 200:
#        print(response.text)
# except:
#    print("wowo")


class DFS:
    def __init__(self):
        self.depth = 0

    def dfs(self, b, t, d2, words):
        if b == t and (self.depth == 0 or self.depth >= d2):
            self.depth = d2
        else:
            for i in range(len(words)):
                w = words[i]
                if self.check(b, w):
                    words2 = [*words]
                    words2.remove(w)
                    self.dfs(w, t, d2+1, words2)

    def check(self, v, w):
        v_chars = list(v)
        w_chars = list(w)

        for i in range(len(v)):
            _v_chars = [*v_chars]
            _w_chars = [*w_chars]
            del _v_chars[i]
            del _w_chars[i]

            if _v_chars == _w_chars:
                return True

        return False

def solution(v, t, array):
    dfs = DFS()
    dfs.dfs(v, t, 0, array)
    return dfs.depth

tmp = solution('hit', 'cog', ['hot', 'dot', 'dog', 'lot', 'log', 'cog'])
print(tmp)


import serial
import argparse
import time
from threading import Thread, Event, Lock
import random

# Argparser
parser = argparse.ArgumentParser(description="Simula controlador de maquina de ensayos pin on disk")
parser.add_argument('--port', action='store', help='Puerto al cual va a streamear', required=True, dest='port')
port = parser.parse_args().port

ser = serial.Serial(port, timeout=None ) # Puerto al que escucha el controlador
ser.write(('Controlador escuchando en puerto ' + port + '\n').encode())
exitEvent = Event()
lock = Lock()
commands = ['SEND', 'STAR', 'CONN', 'DCON', 'TMHM', 'TEST', 'PAUS', 'STOP', 'SPEE']
radios = [5, 6, 7]
vueltasT = 0.0
velocidades = [10.0,12.5,13.2,15.3,9.6,8.6,7.9]
#Comandos
command = ''
radio = 0
vueltas = 0

#Estado default
state = 'WAIT'
isConnected = False
isRunning = False
test = False

def listener():
    global command, radio, vueltas
    while True:
        read = ser.readline().decode().strip()
        if (read[0] == '<' and read[-1] == '>' and read[1:5] in commands):
            if (len(read) == 6):      #Comando regular, sin argumentos
                lock.acquire()
                command = read[1:5]
                lock.release()
                print(command)
            elif (read[1:5] == 'STAR'):
                lock.acquire()
                command = read[1:5]
                radio = int(read.split(',')[1])
                vueltas = int(read.split(',')[2].split('>')[0])
                lock.release()
                print(f'<STAR,{radio},{vueltas}>')
        read = ''

def resetVars():
    global vueltasT
    vueltasT = 0

def getTemp():
    ser.write('55\n'.encode())
    ser.write('25.5\n'.encode())

def main():
    while True:
        global state, command, vueltasT, isRunning, isConnected, test
        if (state == 'STAR'):
            if (not isRunning):
                resetVars()
                isRunning = True
            if (vueltasT >= vueltas):
                ser.write('-1\n'.encode())
                isRunning = False
                state = 'WAIT'
            if (command == 'PAUS'):
                ser.write('0\n'.encode())
                state = 'PAUS'
            elif (command == 'STOP'):
                state = 'STOP'
            elif (command == 'TMHM'):
                getTemp()
                command = ''
            elif (command == 'SEND'):
                ser.write((str(vueltasT) + '\n').encode())
                command = ''
            elif (command == 'SPEE'):
                ser.write((str(random.choice(velocidades)) + '\n').encode())
                command = ''
            vueltasT += 1.0

        elif (state == 'PAUS'):
            if (command == 'STAR'):
                state = 'STAR'
                ser.write('0\n'.encode())

            elif (command == 'STOP'):
                state = 'STOP'

        elif (state == 'STOP'):
            print('Detenido')
            ser.write('-1\n'.encode())
            isRunning = False
            state = 'WAIT'

        elif (state == 'CONN'):
            print('Conectado')
            ser.write('0\n'.encode())
            isConnected = True
            state = 'WAIT'

        elif (state == 'DCON'):
            print('Desconectado')
            ser.write('0\n'.encode())
            isConnected = False
            resetVars()
            state = 'WAIT'

        elif (state == 'WAIT'):

            if (command == 'CONN' and isConnected == False):
                state = 'CONN'
                command = ''

            elif (command == 'STAR' and isConnected and (radio in radios) and vueltas > 0):
                state = 'STAR'
                print('Empezando experimento')
                ser.write('0\n'.encode())
                command = ''

            elif (command == 'TEST' and isConnected):
                state = 'TEST'
                command = ''

            elif (command == 'STOP' and isConnected):
                state = 'STOP'
                command = ''

            elif(command == 'DCON' and isConnected):
                state = 'DCON'
                command = ''

        elif (state == 'TEST'):
            test = not test
            if (test):
                print('Comenzando test')
                ser.write('0\n'.encode())
                state = 'WAIT'
            else:
                print('Finalizando test')
                ser.write('-1\n'.encode())
                state = 'WAIT'

        time.sleep(0.1)

t1 = Thread(target=listener, daemon=True)
t2 = Thread(target=main, daemon=True)
t1.start()
t2.start()

while True:
    time.sleep(0.1)

t1.join()
t2.join()
print('Cerrando')


class Queue:

    def append(self, name):
        self.create_part(name)

    def create_part(self, name):
        if not self.head:
            self.head = self.tail = self.create_node(name)
        else:
            self.tail['next'] = {'name': name, 'next': None, 'prev': None}
            self.tail['next']['prev'] = self.tail
            self.tail = self.tail['next']

    def pop_left(self):
        if not self.head:
            return
        if self.head is self.tail:
            value = self.head['name']
            self.head = self.tail = dict()
            return value
        value = self.head['name']
        self.head = self.head['next']
        self.head['prev'] = None
        return value

    def __init__(self):
        self.head = dict()
        self.tail = dict()

    def __str__(self):
        to_print = []
        curr = self.head
        while curr is not self.tail:
            to_print.append(curr['name'])
            curr = curr['next']
        to_print.append(curr['name'])
        return ' '.join(map(str, to_print))

    @staticmethod
    def create_node(name):
        return {'name': name, 'next': None, 'prev': None}

class Graph:

    def __init__(self, edges, count_vertexes, oriented=False):
        self.graph = dict()
        for v in range(count_vertexes):
            self.graph[v] = set()
        self.create_graph(edges, oriented)

    def create_graph(self, edges, oriented):
        for edge in edges:
            self.graph[edge[0]].add(edge[1])
            if not oriented:
                self.graph[edge[1]].add(edge[0])

    def distances(self, start_vertex):
        distances = [0] * len(self.graph)
        queue = Queue()
        queue.append(start_vertex)
        while queue.head:
            cur_vertex = queue.pop_left()
            for v in self.graph[cur_vertex]:
                if v != start_vertex and not distances[v]:
                    distances[v] = distances[cur_vertex] + 1
                    queue.append(v)
        return distances

    def cycle(self, vertex=0, white=None, grey=None, black=None):
        if white is None:
            white = list(k for k in range(vertex, len(self.graph)))
        if grey is None:
            grey = list()
        if black is None:
            black = list()
        cycle = []

        if vertex in white:
            white.remove(vertex)
            grey.append(vertex)
        for v in self.graph[vertex]:
            if cycle:
                break
            if v in white:
                cycle = self.cycle(v, white, grey, black)
                continue
            if v in grey:
                cycle = self.create_cycle(grey, v)
                return cycle
        if vertex in grey:
            grey.remove(vertex)
            black.append(vertex)
        return cycle

    def dfs(self, vertex, used, ans):
        if vertex not in used:
            used[vertex] = True
        for v in self.graph[vertex]:
            if v not in used:
                self.dfs(v, used, ans)
        ans.append(vertex)

    def topological_sort(self):
        used = dict()
        ans = []
        for vertex in self.graph:
            if vertex not in used:
                self.dfs(vertex, used, ans)
        ans[:] = ans[::-1]
        return ans

    @staticmethod
    def create_cycle(grey, start_vertex):
        return grey[grey.index(start_vertex):]

    def is_cycle(self):
        c = []
        ver = 0
        while not c and ver < n:
            c = self.cycle(ver)
            ver += 1
        if c:
            return True
        else:
            return False

if __name__ == '__main__':
    n, m = map(int, input().split())
    graph_edges = []
    for _ in range(m):
        graph_edges.append(list(map(int, input().split())))
    graph = Graph(graph_edges, n)
    graph_edges = None
    start = 0
    dists = graph.distances(start)
    for i in range(len(dists)):
        print(dists[i])

contest_6/F - Поиск расстояний от заданной.py

F - Поиск расстояний от заданной.py


'''
Barath Vedantham
CIS 41A   Fall 2017
Unit H take-home assignment
'''

# First script

def main():
    find_state()

def find_state():
    states_and_pres = {}
    count = 0

    with open('USPresidents.txt') as pres_file:
        for line in pres_file:
            fields = line.split()

            if fields[0] in states_and_pres:  # if state already exists in dict
                states_and_pres[fields[0]].append(fields[1])  # append to list
            else:
                states_and_pres[fields[0]] = [fields[1]]  # else start new list

    pres_file.close()

    for state_key, pres_list in states_and_pres.items():
        if len(pres_list) > count:  # if current list is biggest
            count = len(pres_list)  # store length of list
            state = state_key  # store name of state assoc. with list

    print("The state with the most presidents is", state, end = " ")
    print("with", count, "presidents:")

    for pres in states_and_pres[state]:
        print(pres)

main()

'''
Results:
The state with the most presidents is VA with 8 presidents:
George_Washington
James_Madison
James_Monroe
John_Tyler
Thomas_Jefferson
William_Henry_Harrison
Woodrow_Wilson
Zachary_Taylor
'''


class ListNode(object):
    def _init_(self, x):
        self.val = x
        self.next = None

class Solutions:
    def addTwoNumbers(self, l1, l2):

        print('')
        print('inside function...')

        # Declare pointers for traversal. Added for clarity.
        p1 = l1
        p2 = l2

        # Declare current carryover.

        currentCarry = 0

        # Declare curr variable to traverse and add new nodes to new list

        head = cur = ListNode(0)

        # Iteration condition.

        while p1 or p2 or currentCarry:

            print(p1.val, p2.val, currentCarry)

            # Determine current value nd carry over.

            currentVal = currentCarry
            currentVal += 0 if p1 is None else p1.val
            currentVal += 0 if p2 is None else p2.val
            if currentVal >= 10:
                currentCarry -= 10
                currentCarry = 1
            else:
                currentCarry = 0

            print(currentVal, currentCarry)

            # Ad current value as it will always as least be "1".
            cur.next = ListNode(currentVal)
            cur = cur.next

            # Add base cases for iterating linked lists.
            if p1 is None and p2 is None:
                break
            elif p1 is None:
                p2 = p2.next
            elif p2 is None:
                p1 = p1.next
            else:
                p1 = p1.next
                p2 = p2.next

            print('exiting...')
            print('')

            # Return next node.
            return head.next


import logging
import re
from datetime import datetime, timezone, timedelta
import pandas

from jal.widgets.helpers import g_tr
from jal.db.update import JalDB
from jal.constants import Setup, DividendSubtype, PredefinedCategory

# -----------------------------------------------------------------------------------------------------------------------
class KITFinance:
    Header = "КИТ Финанс (АО)"
    AccountPattern = "(?P<ACCOUNT>.*)-(.*)"
    PeriodPattern = "(?P<S>\d\d\.\d\d\.\d\d\d\d) - (?P<E>\d\d\.\d\d\.\d\d\d\d)"

    def __init__(self, parent, filename):
        self._parent = parent
        self._filename = filename
        self._statement = None
        self._account_id = 0
        self._settled_cash = {}

    def load(self):
        self._statement = pandas.read_excel(self._filename, header=None, na_filter=False)
        if not self.validate():
            return False
        self.load_cash_balance()
        self.load_stock_deals()
        self.load_cash_transactions()
        logging.info(g_tr('KIT', "KIT Finance statement loaded; Planned cash: ")
                     + f"{self._settled_cash[self._account_id]}")
        return True

    def validate(self):
        if self._statement[4][0] != self.Header:
            logging.error(g_tr('KIT', "Can't find KIT Finance report header"))
            return False
        parts = re.match(self.AccountPattern, self._statement[5][5], re.IGNORECASE)
        if parts is None:
            logging.error(g_tr('KIT', "Can't parse KIT Finance account number"))
            return False
        account_name = parts.groupdict()['ACCOUNT']
        parts = re.match(self.PeriodPattern, self._statement[5][8], re.IGNORECASE)
        if parts is None:
            logging.error(g_tr('KIT', "Can't parse KIT Finance statement period"))
            return False
        statement_dates = parts.groupdict()
        report_start = int(datetime.strptime(statement_dates['S'], "%d.%m.%Y").replace(tzinfo=timezone.utc).timestamp())
        if not self._parent.checkStatementPeriod(account_name, report_start):
            return False
        logging.info(g_tr('KIT', "Loading KIT Finance statement for account ") +
                     f"{account_name}: {statement_dates['S']} - {statement_dates['E']}")
        self._account_id = JalDB().get_account_id(account_name)
        return True

    def find_section_start(self, header, columns) -> (int, dict):
        start_row = -1
        headers = {}
        for i, row in self._statement.iterrows():
            if row[0] == header:
                start_row = i + 1  # points to columns header row
                break
        if start_row > 0:
            for col in range(self._statement.shape[1]):  # Load section headers from next row
                headers[self._statement[col][start_row]] = col
        column_indices = {column: headers.get(columns[column], -1) for column in columns}
        if start_row > 0:
            for idx in column_indices:
                if column_indices[idx] < 0:
                    logging.error(g_tr('KIT', "Column not found in section ") + f"{header}: {idx}")
                    start_row = -1
        start_row += 1
        return start_row, column_indices

    def load_cash_balance(self):
        row, headers = self.find_section_start("Состояние денежных средств на счете", {})
        if row < 0:
            return False
        while row < self._statement.shape[0]:
            if self._statement[0][row] == '':
                break
            if self._statement[0][row] == 'Позиция по ДС на T+N':
                try:
                    self._settled_cash[self._account_id] = float(self._statement[6][row])
                except (ValueError, TypeError):
                    pass
            row += 1

    def load_stock_deals(self):
        cnt = 0
        columns = {
            "number": "Номер\nсделки",
            "date": "Дата сделки",
            "time": "Время сделки",
            "asset": "Наименование\nЦБ",
            "isin": "ISIN",
            "B/S": "Тип операции",
            "price": "Цена сделки ",
            "qty": "Количество",
            "amount": "Сумма сделки",
            "accrued_int": " НКД",
            "settlement": "Дата поставки\n(план.)",
            "fee_ex": "Комиссия\nТС",
            "fee_broker": "Комиссия\nброкера"
        }

        row, headers = self.find_section_start("Заключенные сделки с ценными бумагами", columns)
        if row < 0:
            return False
        while row < self._statement.shape[0]:
            if self._statement[0][row] == '' and self._statement[0][row + 1] == '':
                break
            asset_id = JalDB().get_asset_id('', isin=self._statement[headers['isin']][row],
                                                name=self._statement[headers['asset']][row])
            if self._statement[headers['B/S']][row] == 'Покупка':
                amount = -self._statement[headers['amount']][row]
                qty = self._statement[headers['qty']][row]
                bond_interest = -self._statement[headers['accrued_int']][row]
            elif self._statement[headers['B/S']][row] == 'Продажа':
                amount = self._statement[headers['amount']][row]
                qty = -self._statement[headers['qty']][row]
                bond_interest = self._statement[headers['accrued_int']][row]
            else:
                row += 1
                logging.warning(g_tr('KIT', "Unknown trade type: ") + self._statement[headers['B/S']][row])
                continue
            price = self._statement[headers['price']][row]
            fee = round(abs(self._statement[headers['fee_ex']][row] + self._statement[headers['fee_broker']][row]), 8)
            if abs(abs(price * qty) - amount) >= Setup.DISP_TOLERANCE:
                price = abs(amount / qty)
            number = self._statement[headers['number']][row]
            # Dates are loaded as datetime objects but time is loaded as string
            t_date = self._statement[headers['date']][row]
            t_time = datetime.strptime(self._statement[headers['time']][row], "%H:%M:%S").time()
            trade_datetime = t_date + timedelta(hours=t_time.hour, minutes=t_time.minute, seconds=t_time.second)
            timestamp = int(trade_datetime.replace(tzinfo=timezone.utc).timestamp())
            settlement = int(self._statement[headers['settlement']][row].replace(tzinfo=timezone.utc).timestamp())
            JalDB().add_trade(self._account_id, asset_id, timestamp, settlement, number, qty, price, -fee)
            if bond_interest != 0:
                JalDB().add_dividend(DividendSubtype.BondInterest, timestamp, self._account_id, asset_id,
                                     bond_interest, "НКД", number)
            cnt += 1
            row += 1
        logging.info(g_tr('KIT', "Trades loaded: ") + f"{cnt}")

    def load_cash_transactions(self):
        cnt = 0
        columns = {
            "date": "Дата",
            "operation": "Тип операции",
            "amount": "Сумма",
            "currency": " Валюта",
            "reason": "Основание",
            "note": "Примечание",
            "market": "Секция"
        }
        operations = {
            'Внесение д/с в торг': self.transfer_in,
            'Вывод дс': self.transfer_out,
            'Ком бр аб плата спот': self.fee,
            'Комиссия НРД': self.fee
        }
        row, headers = self.find_section_start("Движение денежных средств по неторговым операциям", columns)
        if row < 0:
            return False
        while row < self._statement.shape[0]:
            if self._statement[0][row] == '':
                break
            operation = self._statement[headers['operation']][row]
            if operation not in operations:  # not supported type of operation
                row += 1
                continue
            timestamp = int(self._statement[headers['date']][row].replace(tzinfo=timezone.utc).timestamp())
            amount = self._statement[headers['amount']][row]
            reason = self._statement[headers['reason']][row]
            description = self._statement[headers['note']][row]
            operations[operation](timestamp, amount, reason, description)
            cnt += 1
            row += 1
        logging.info(g_tr('KIT', "Cash operations loaded: ") + f"{cnt}")

    def transfer_in(self, timestamp, amount, reason, note):
        currency_name = JalDB().get_asset_name(JalDB().get_account_currency(self._account_id))
        text = g_tr('KIT', "Deposit of ") + f"{amount:.2f} {currency_name} " + \
               f"@{datetime.utcfromtimestamp(timestamp).strftime('%d.%m.%Y')}\n" + \
               g_tr('KIT', "Select account to withdraw from:")
        pair_account = self._parent.selectAccount(text, self._account_id)
        if pair_account == 0:
            return
        description = reason + ", " + note
        JalDB().add_transfer(timestamp, pair_account, amount, self._account_id, amount, 0, 0, description)

    def transfer_out(self, timestamp, amount, reason, note):
        currency_name = JalDB().get_asset_name(JalDB().get_account_currency(self._account_id))
        text = g_tr('KIT', "Withdrawal of ") + f"{-amount:.2f} {currency_name} " + \
               f"@{datetime.utcfromtimestamp(timestamp).strftime('%d.%m.%Y')}\n" + \
               g_tr('KIT', "Select account to deposit to:")
        pair_account = self._parent.selectAccount(text, self._account_id)
        if pair_account == 0:
            return
        description = reason + ", " + note                # amount is negative in XLSX file
        JalDB().add_transfer(timestamp, self._account_id, -amount, pair_account, -amount, 0, 0, description)

    def fee(self, timestamp, amount, _reason, description):
        JalDB().add_cash_transaction(self._account_id, self._parent.getAccountBank(self._account_id),
                                     timestamp, amount, PredefinedCategory.Fees, description)


# -*- coding: utf-8 -*-
#
# Copyright 2005 Fundación Via Libre
#
# This file is part of PAPO.
#
# PAPO is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# PAPO is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# PAPO; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA 02111-1307 USA

__revision__ = int('$Rev$'[5:-1])

from mx.DateTime import now
from time import time
from random import uniform

from fvl.luca.model import PointOfSale, CustomerAccount, PettyCash, \
     MovementAccount, Client, Person, Invoice, PointOfSaleOpening, Money
from fvl.luca.transaction import Transaction, Qualifier

from testWithDatabase import testWithDatabase

class TestPointOfSale(testWithDatabase):
    def setUp(self):
        super(TestPointOfSale, self).setUp()
        self.pettyCashName = 'Piece of POS'
        self.pos = PointOfSale(name=self.pettyCashName)
        self.client = Client(subject=Person(name='Cacho',
                                                surname='Moo'))
        self.category = CustomerAccount(name='FonCyT')
        self.account = MovementAccount(name='Impuestos', code="1")
        self.subAccount = MovementAccount(name='IVA', code="11",
                                          parent=self.account)
        self.trans = Transaction()
        self.qual = Qualifier()
        self.trans.track(self.pos, self.category, self.account, self.subAccount,
                         self.client, self.client.subject)
        self.trans.save()

    def testRegisterDocument(self):
        self.pos.registerDocument(documentClass=Invoice,
                                  number='some number',
                                  type='X',
                                  detail='blah blah blah',
                                  amount=100,
                                  actualDate=now(),
                                  otherParty=self.client
                                  )
        self.trans.save()
    def testTotal(self):
        self.testRegisterDocument()
        self.assertEqual(self.pos.total(), 100)

def TestPettyCash(testWithDatabase):
    def setUp(self):
        pass


#!/usr/bin/env python3
import sys
from math import log10

def truncate(f):
    return '%.10f' % f

def add_sentence_markers(sentence):
    return "<s> " + sentence + " </s>"

def read_lm_file():
    ngram_dict = dict()
    unigram_token_size = 0
    bigram_token_size = 0
    trigram_token_size = 0

    with open(sys.argv[1]) as fp:
        for line in fp:
            if line == "\n":
                continue

            if line[0:5] == "ngram":
                splits = line.split()
                token_size = int(splits[3].replace("token=", ""))

                if line[0:7] == "ngram 1":
                    unigram_token_size = token_size
                elif line[0:7] == "ngram 2":
                    bigram_token_size = token_size
                else:
                    trigram_token_size = token_size
                continue

            if len(line) > 0:
                if line[0] == "\\":
                    continue
                else:
                    splits = line.split()
                    if len(splits) >= 4:
                        count = int(splits[0])
                        ngram = " ".join(splits[3:])

                        ngram_dict[ngram] = count
    return unigram_token_size, bigram_token_size, trigram_token_size, ngram_dict

def interpolate(unigram_token_size, bigram_token_size, trigram_token_size, ngram_dict, lambda1, lambda2, lambda3):
    output_file = open(sys.argv[6], "w+")

    with open(sys.argv[5]) as f:  # Read test data
        sentences = f.read().splitlines()

    sent_num = len(sentences)
    total_word_num = 0
    total_oov_num = 0
    total_sum_lgprob = 0

    for i, sentence in enumerate(sentences):
        if sentence == "\n":
            continue

        sum_lgprob = 0
        word_num = 0
        oov_num = 0

        tokens = add_sentence_markers(sentence).split()
        # word_num += number of words in the sent excluding BOS and EOS
        word_num = word_num + len(tokens) - 2
        total_word_num = total_word_num + word_num
        tokens.remove("<s>")

        output_file.write("\n" + "Sent #" + str((i + 1)) + ": " + sentence + "\n")

        for index, token in enumerate(tokens):
            sub_tokens = tokens[:index + 1] if index < 3 else tokens[index - 2: index + 1]
            if token in ngram_dict:
                token_numerator = ""
                token_denominator = ""
                prob = 0

                is_unseen = None
                if len(sub_tokens) == 1:
                    token_numerator = token
                    token_denominator = "<s>"

                    bigram_probab_value, bigram_is_unseen = bigram_prob(["<s>", token], ngram_dict)
                    is_unseen = bigram_is_unseen
                    prob = lambda2 * bigram_probab_value \
                           + lambda1 * (ngram_dict[" ".join(sub_tokens)] / unigram_token_size)
                elif len(sub_tokens) == 2:
                    sub_tokens = ["<s>"] + sub_tokens
                    token_numerator = sub_tokens[2]
                    token_denominator = " ".join(sub_tokens[0:2])

                    prob, is_unseen = trigram_interpolation(sub_tokens, ngram_dict, lambda1, lambda2, lambda3, unigram_token_size)
                elif len(sub_tokens) == 3:
                    token_numerator = sub_tokens[2]
                    token_denominator = " ".join(sub_tokens[0:2])
                    prob, is_unseen = trigram_interpolation(sub_tokens, ngram_dict, lambda1, lambda2, lambda3, unigram_token_size)

                if is_unseen:
                    output_file.write(str(index + 1) + ": " + "lg P(" + token_numerator + " | " + token_denominator + ") = " + truncate(log10(prob)) + " " + "(unseen ngrams)" + "\n")
                else:
                    output_file.write(str(index + 1) + ": " + "lg P(" + token_numerator + " | " + token_denominator + ") = " + truncate(log10(prob)) + "\n")
                sum_lgprob = sum_lgprob + log10(prob)
            else:
                if len(sub_tokens) == 1:
                    sub_tokens = ["<s>"] + sub_tokens

                output_file.write(str(index + 1) + ": " + "lg P(" + token + " | " + " ".join(sub_tokens[:len(sub_tokens) - 1]) + ") = -inf (unknown word)" + "\n")
                oov_num = oov_num + 1

        cnt = word_num + 1 - oov_num
        total = -1 * (sum_lgprob / cnt)
        ppl = 10 ** total

        total_oov_num = total_oov_num + oov_num
        total_sum_lgprob = total_sum_lgprob + sum_lgprob

        output_file.write("1 sentence, " + str(word_num) + " words, " + str(oov_num) + " OOVs" + "\n")
        output_file.write("lgprob=" + truncate(sum_lgprob) + " ppl=" + truncate(ppl) + "\n\n\n")

    N = total_word_num + sent_num - total_oov_num
    ave_lgprob = total_sum_lgprob / N
    ppl = 10 ** (-1 * ave_lgprob)

    output_file.write("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" + "\n")
    output_file.write("sent_num=" + str(sent_num) + " word_num=" + str(total_word_num) + " oov_num=" + str(total_oov_num) + "\n")
    output_file.write("lgprob= " + truncate(total_sum_lgprob) + " ave_lgprob=" + truncate(ave_lgprob) + " ppl=" + truncate(ppl) + "\n")

    output_file.close()

def trigram_interpolation(tokens, ngram_dict, lambda1, lambda2, lambda3, unigram_token_size):
    trigram_prob_value, is_unseen = trigram_prob(tokens, ngram_dict)
    bigram_prob_value, useless_is_unseen = bigram_prob(tokens[1:], ngram_dict)
    prob = lambda3 * trigram_prob_value\
               + lambda2 *  bigram_prob_value\
               + lambda1 * (ngram_dict[" ".join(tokens[2:])] / unigram_token_size)

    return prob, is_unseen

def bigram_prob(tokens, ngram_dict):
    key1 = " ".join(tokens)
    key2 = tokens[0]

    if (key1 in ngram_dict) & (key2 in ngram_dict):
        return ngram_dict[key1] / ngram_dict[key2], False
    else:
        return 0, True

def trigram_prob(tokens, ngram_dict):
    key1 = " ".join(tokens)
    key2 = " ".join(tokens[0:2])

    if (key1 in ngram_dict) & (key2 in ngram_dict):
        return ngram_dict[key1] / ngram_dict[key2], False
    else:
        return 0, True

def main():
    unigram_token_size, bigram_token_size, trigram_token_size, ngram_dict = read_lm_file()

    lambda1 = float(sys.argv[2])
    lambda2 = float(sys.argv[3])
    lambda3 = float(sys.argv[4])

    interpolate(unigram_token_size, bigram_token_size, trigram_token_size, ngram_dict, lambda1, lambda2, lambda3)

if __name__ == '__main__':
    main()


from os.path import abspath, dirname, join

import pytest

from iscan.scan import ImportScanner, convert_source_to_tree, get_base_name, scan_directory  # isort:skip # noqa: E501

CURRENT_DIR = abspath(dirname(__file__))

@pytest.mark.parametrize('module_name, expected', [
    ('absolute.py', ['logging', 'os', 'os.path', 'subprocess', 'sys']),
    ('relative.py', []),
    ('mixed.py', ['logging', 'os', 'os.path', 'subprocess', 'sys']),
])
def test_import_scanner(module_name, expected):
    path_to_module = join(CURRENT_DIR, 'test_modules', module_name)
    tree = convert_source_to_tree(path_to_module)
    scanner = ImportScanner()
    scanner.visit(tree)
    assert scanner.get_imports() == expected

@pytest.mark.parametrize('full_name, expected', [
    ('foo', 'foo'),
    ('foo.bar', 'foo'),
    ('foo.bar.baz', 'foo'),
    ('foo_bar_baz.batman', 'foo_bar_baz')
])
def test_get_base_name(full_name, expected):
    assert get_base_name(full_name) == expected

def test_convert_source_to_tree():
    # This function doesn't do anything other than converting source code into its
    # AST representation using the ast module. No need to test
    assert True

@pytest.mark.parametrize('dir_to_exclude, expected', [
    (None, ['ctypes', 'datetime', 'matplotlib', 'numpy', 'os', 'pandas', 'shutil', 'time']),
    (join(CURRENT_DIR, 'test_package', 'city'), ['matplotlib', 'numpy', 'os', 'pandas', 'shutil', 'time'])  # noqa: E501
])
def test_scan_directory(dir_to_exclude, expected):
    dir_to_scan = join(CURRENT_DIR, 'test_package')
    assert scan_directory(dir_to_scan, dir_to_exclude) == expected


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

class Restaurant():

    def __init__(self,restaurant_name,cuisine_type):
        self.restaurant_name = restaurant_name
        self.cuisine_name = cuisine_type

    def describle_restaurant(self):
        print(self.restaurant_name + "是一家百年老店，主营正宗" + self.cuisine_name + "，味道正宗！")

    def open_restaurant(self):
        print("正在营业，欢迎光临！")

Chuan_Cai = Restaurant('蜀味居', '川菜')
print('<<<' + Chuan_Cai.restaurant_name + '>>>')
Chuan_Cai.describle_restaurant()
Chuan_Cai.open_restaurant()

Yue_Cai = Restaurant('静风阁', '粤菜')
print('<<<' + Yue_Cai.restaurant_name + '>>>')
Yue_Cai.describle_restaurant()
Yue_Cai.open_restaurant()


import subprocess  # safest module for running shell commands through python
import sys  # only for exit
import os.path  # only for checking existence
import argparse

# Parse command line arguments
parser = argparse.ArgumentParser(
    description = "Simple parser for running flake8 on git repo"
    )
parser.add_argument("-r", "--repo",
                    help = "GitHub repository, as 'owner/repo'")
parser.add_argument("-p", "--path",
                    help = "Path where the git repo will be cloned")
parser.add_argument("-s", "--sha",
                    help = "SHA of the commit to checkout")
args = parser.parse_args()
# origin of git repo
repo_uri = "https://github.com/{}.git".format(args.repo)
# where the repo will be cloned
git_path = args.path
# SHA of the repo that needs to be checked out
git_sha = args.sha  # eg. "10213a290beafdae8518023cb2235086ebdf4506"
(owner, repo) = args.repo.split('/')

# mkdir using subprocess run
if os.path.exists(git_path):
    print("The git_path location already exists")
else:
    try:
        mkdir_process = subprocess.run(["mkdir", git_path], check=True)
    except subprocess.CalledProcessError:
        sys.exit("The directory could not be created. Script ending prematurely")
    print("Directory to clone git repo into created successfully")

# git clone
if os.path.exists(git_path + "/{}/.git".format(repo)):
    print("The repo has already been cloned at specified git_path")
else:
    try:
        clone_process = subprocess.run(["git", "clone", repo_uri], cwd=git_path, check=True)
    except subprocess.CalledProcessError:
        sys.exit("The git repository could not be cloned. Script ending prematurely")
    print("Repo cloned successfully")

# git checkout
try:
    checkout_process = subprocess.run(["git", "checkout", git_sha], cwd=git_path + "/{}".format(repo), check=True,
                                      stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    # for capturing output and redirecting stderr to stdout
except subprocess.CalledProcessError:
    sys.exit("The checkout could not be performed. Script ending prematurely")
print(checkout_process.stdout.decode("utf-8"))

# running flake8
try:
    flake8_process = subprocess.run(["flake8", "-v"], cwd=git_path + "/{}".format(repo),
                                    stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
except (OSError, ValueError, subprocess.CalledProcessError) as e:
    sys.exit(e+"Could not run flake8. Exiting")

if flake8_process.returncode == 0:
    print("OK") # This is printed if there are no errors and flake8 ends successfully
print("The list of violations/errors found by flake8 are:\n", flake8_process.stdout.decode("utf-8"), sep='')


''' Jose Antonio Garcia Martinez  1218100524'''
import random

class aleatori():
   def aleatorio(self):
      num = int(input("Introduce un número: "))
      i=0
      while(i<num):
         num2 = random.choice(range(1000))
         print(num2)
         i=i+1

al=aleatori()
al.aleatorio()


import random
from math import log
from collections import defaultdict
from tqdm import tqdm
from sys import argv

random.seed(42)

class Topic_Model:
    def __init__(self, iteration_num, NUM_TOPICS, α, β, is_tqdm=True):
        self.iteration_num = iteration_num
        self.NUM_TOPICS = NUM_TOPICS
        self.α = α
        self.β = β
        self.is_tqdm = is_tqdm
        self.num_wordtype = 0
        self.xcorpus = []
        self.ycorpus = []
        self.xcounts = defaultdict(lambda: 0)
        self.ycounts = defaultdict(lambda: 0)

    def sampleone(self, probs):
        z = sum(probs)  # 確率の和(正規化項 ) を計算
        remaining = random.random() * z
        for i in range(len(probs)):
            remaining -= probs[i]
            if remaining <= 0:
                return i
        print('error')
        exit()

    def addCounts(self, word, topic, docid, amount):
        assert topic >= 0
        assert docid >= 0
        assert amount >= 0 or amount == -1

        self.xcounts[topic] += amount
        self.xcounts[f'{word}|{topic}'] += amount

        self.ycounts[docid] += amount
        self.ycounts[f'{topic}|{docid}'] += amount
        # バグチェック
        # < 0 の場合はエラー終了

    def init(self, in_path):
        wordtype = []
        for line in map(lambda x: x.rstrip(), open(in_path)):
            docid = len(self.xcorpus)
            words = line.split()
            topics = []
            for word in words:
                topic = random.randint(0, self.NUM_TOPICS - 1)
                topics.append(topic)
                self.addCounts(word, topic, docid, 1)  # カウントを追加
                if word not in wordtype:
                    wordtype.append(word)
            self.xcorpus.append(words)
            self.ycorpus.append(topics)
        self.num_wordtype = len(wordtype)

    def sampling(self, out_path='out.txt'):
        for _ in tqdm(range(self.iteration_num)):
            ll = 0
            for i in tqdm(range(len(self.xcorpus))):
                for j in range(len(self.xcorpus[i])):
                    probs = []
                    x = self.xcorpus[i][j]
                    y = self.ycorpus[i][j]
                    self.addCounts(x, y, i, -1)  # 各種カウントの減算 (-1)
                    for k in range(self.NUM_TOPICS):
                        p_x_k = (self.xcounts[f'{x}|{k}'] + self.α) / \
                            (self.xcounts[k] + self.α * self.num_wordtype)
                        p_k_y = (self.ycounts[f'{k}|{i}'] + self.β) / \
                            (self.ycounts[i] + self.β * self.NUM_TOPICS)
                        probs.append(p_x_k * p_k_y)      # トピック k の確率
                    new_y = self.sampleone(probs)
                    ll += log(probs[new_y])        # 対数尤度の計算
                    self.addCounts(x, new_y, i, 1)      # 各カウントの加算
                    self.ycorpus[i][j] = new_y
        #         print(ll)
        # print(self.xcounts)
        # print(self.ycounts)
        # print(self.xcorpus)
        # print(self.ycorpus)

        # word_topic = {i: [] for i in range(self.NUM_TOPICS)}
        word_topic_list = [[] for i in range(self.NUM_TOPICS)]
        # for i in range(len(self.xcorpus)):
        #     for j in range(len(self.xcorpus[i])):
        #         if self.xcorpus[i][j] not in word_topic:
        #             word_topic[self.xcorpus[i][j]] = self.ycorpus[i][j]
        for i in range(len(self.xcorpus)):
            for j in range(len(self.xcorpus[i])):
                word = self.xcorpus[i][j]
                topic = self.ycorpus[i][j]
                if word not in word_topic_list[topic]:
                    word_topic_list[topic].append(word)
        # with open(out_path, "w") as f_out:
        #     for k, v in word_topic.items():
        #         print(k, v, file=f_out)

        for i in range(len(word_topic_list)):
            random.shuffle(word_topic_list[i])
        with open(out_path, "w") as f_out:
            for i, values in enumerate(word_topic_list):
                print(i, file=f_out)
                for v in values:
                    print(v, end=' ', file=f_out)
                print(file=f_out)

if __name__ == '__main__':
    if argv[1:] == ['test']:
        in_path = '../../../nlptutorial/test/07-train.txt'
        is_tqdm = False
    else:
        in_path = '../../../nlptutorial/data/wiki-en-documents.word'
        is_tqdm = True
    topic_model = Topic_Model(20, 2, 0.01, 0.01, is_tqdm)  # iteration_num, NUM_TOPICS, α, β
    topic_model.init(in_path)
    topic_model.sampling('out_sep_shuffle_test.txt')


'''
TRAPEZOID PROGRAM
-------------------
Create a new program that will ask the user for the information needed to find the area of a trapezoid, and then print the area.

Test with the following:

base 1: 2       base 2: 3    height: 4    area: 10
base 1: 5       base 2: 7    height: 2    area: 12
base 1: 1       base 2: 2    height: 3    area: 4.5
base 1: 7       base 2: 2    height: 4    area: 18
'''
print("Want to know the area of a trapezoid?\n\nPut the figures below to find out the area\n")
bsa=float(input("Base A: "))
bsb=float(input("Base B: "))
hgt=float(input("Height: "))
area=bsa+bsb
area=area/2
area=area*hgt
print("\nThe area of the trapezoid is:",area)


"""
Utilities for Python version compatibility
"""
# Please organize additions to this file by version

import sys

if sys.version_info < (3, 9):
    # https://docs.python.org/3/library/asyncio-task.html#asyncio.to_thread

    import asyncio
    import functools

    async def asyncio_to_thread(fn, *args, **kwargs):
        loop = asyncio.get_event_loop()
        return await loop.run_in_executor(None, functools.partial(fn, *args, **kwargs))

else:
    from asyncio import to_thread as asyncio_to_thread

if sys.version_info < (3, 8):
    # https://docs.python.org/3/library/asyncio-policy.html#asyncio.ThreadedChildWatcher
    # `ThreadedChildWatcher` is the default child process watcher for Python 3.8+ but it
    # does not exist in Python 3.7. This backport allows us to ensure consistent
    # behavior when spawning async child processes without dropping support for 3.7.

    import asyncio
    import itertools
    import logging
    import os
    import threading
    import time
    import warnings

    logger = logging.getLogger()

    # Python 3.7 preferred API
    _get_running_loop = getattr(asyncio, "get_running_loop", asyncio.get_event_loop)

    class ThreadedChildWatcher(asyncio.AbstractChildWatcher):
        def __init__(self):
            self._pid_counter = itertools.count(0)
            self._threads = {}

        def is_active(self):
            return True

        def close(self):
            pass

        def __enter__(self):
            return self

        def __exit__(self, exc_type, exc_val, exc_tb):
            pass

        def __del__(self, _warn=warnings.warn):
            threads = [t for t in list(self._threads.values()) if t.is_alive()]
            if threads:
                _warn(
                    f"{self.__class__} has registered but not finished child processes",
                    ResourceWarning,
                    source=self,
                )

        def add_child_handler(self, pid, callback, *args):
            loop = _get_running_loop()
            thread = threading.Thread(
                target=self._do_waitpid,
                name=f"waitpid-{next(self._pid_counter)}",
                args=(loop, pid, callback, args),
                daemon=True,
            )
            self._threads[pid] = thread
            thread.start()

        def remove_child_handler(self, pid):
            # asyncio never calls remove_child_handler() !!!
            # The method is no-op but is implemented because
            # abstract base class requires it
            return True

        def attach_loop(self, loop):
            pass

        def _do_waitpid(self, loop, expected_pid, callback, args):
            assert expected_pid > 0

            try:
                pid, status = os.waitpid(expected_pid, 0)
            except ChildProcessError:
                # The child process is already reaped
                # (may happen if waitpid() is called elsewhere).
                pid = expected_pid
                returncode = 255
                logger.warning(
                    "Unknown child process pid %d, will report returncode 255", pid
                )
            else:
                if os.WIFSIGNALED(status):
                    returncode = -os.WTERMSIG(status)
                elif os.WIFEXITED(status):
                    returncode = os.WEXITSTATUS(status)
                else:
                    returncode = status

                if loop.get_debug():
                    logger.debug(
                        "process %s exited with returncode %s", expected_pid, returncode
                    )

            if loop.is_closed():
                logger.warning("Loop %r that handles pid %r is closed", loop, pid)
            else:
                loop.call_soon_threadsafe(callback, pid, returncode, *args)

            self._threads.pop(expected_pid)

else:
    from asyncio import ThreadedChildWatcher


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import unittest
from HtmlTestRunner import HTMLTestRunner

if __name__ == '__main__':
    test_dir = './test_project/test_case'
    # use the following command according to your position
    # (C:\Users\Han\Anaconda3) C:\Users\Han\selenium_test\ch07>python test_project\runtest.py
    # test_baidu
    # .test_youdao
    # .
    # ----------------------------------------------------------------------
    # Ran 2 tests in 23.785s

    # OK

    # Anaconda3: output to test_project\report\log.txt
    # (C:\Users\Han\Anaconda3) C:\Users\Han\selenium_test\ch07>python test_project\runtest.py > test_project\report\log.txt 2>&1

    # ms-dos: output to test_project\report\log.txt
    # C:\Users\Han\selenium_test\ch07>C:\Users\Han\Anaconda3\python.exe test_project\runtest.py > test_project\report\log.txt 2>&1

    # test_dir = './test_case'
    # use the following command
    # (C:\Users\Han\Anaconda3) C:\Users\Han\selenium_test\ch07\test_project>python runtest.py
    # test_baidu
    # .test_youdao
    # .
    # ----------------------------------------------------------------------
    # Ran 2 tests in 28.556s

    # OK

    discover = unittest.defaultTestLoader.discover(test_dir, pattern='test*.py')
    # runner = unittest.TextTestRunner()
    runner = HTMLTestRunner(output='example_suite')
    runner.run(discover)

    # https://github.com/oldani/HtmlTestRunner

    # Just import HtmlTestRunner from package, then pass it to unittest.main with the testRunner keyword. This class have only one required parameter, with is output this is use to place the report of the TestCase, this is saved under a reports directory.

    # C:\Users\Han\selenium_test\ch07\reports\example_suite

    # (C:\Users\Han\Anaconda3) C:\Users\Han\selenium_test\ch07>python test_project\runtest_html.py

    # Running tests...
    # ----------------------------------------------------------------------
    # test_baidu (test_baidu.MyTest) ... OK (9.728881)s
    # test_youdao (test_youdao.MyTest) ... OK (11.525394)s

    # ----------------------------------------------------------------------
    # Ran 2 tests in 0:00:21

    # OK

    # Generating HTML reports...

    # (C:\Users\Han\Anaconda3) C:\Users\Han\selenium_test\ch07>dir reports\example_suite \s
    # 磁碟區 C 中的磁碟沒有標籤。
    # 磁碟區序號:  3E9E-61A9

    # C:\Users\Han\selenium_test\ch07\reports\example_suite 的目錄

    # 2018/03/26  上午 12:56    <DIR>          .
    # 2018/03/26  上午 12:56    <DIR>          ..
    # 2018/03/26  上午 12:56             2,989 Test_test_baidu.MyTest_2018-03-26_00-56-34.html
    # 2018/03/26  上午 12:56             2,991 Test_test_youdao.MyTest_2018-03-26_00-56-34.html
                # 2 個檔案           5,980 位元組


from django.contrib.auth.models import User
from django.contrib.sites.shortcuts import get_current_site
from django.contrib.sites.models import Site
from django.core.mail import send_mail
from django.core.urlresolvers import reverse
from django.db.models.signals import post_save, post_delete
from django.dispatch import receiver
from django.conf import global_settings as settings
from . import models

@receiver(post_save, sender=User, dispatch_uid='blog: make blog')
def make_blog(sender, **kwargs):
    if kwargs['created']:
        new_blog = models.Blog()
        new_blog.owner = kwargs['instance']
        new_blog.save()

def notify(post, subject, text):
    adr_from = settings.DEFAULT_FROM_EMAIL
    subscribers = post.blog.subscribers
    for user in subscribers:
        if user.email:
            send_mail(subject, text, adr_from, [user.email], fail_silently=False)

@receiver(post_save, sender=models.Post, dispatch_uid='blog: create post')
def create_post(sender, **kwargs):
    if kwargs['created']:
        site = Site.objects.get_current()
        post = kwargs['instance']
        url = reverse('blog-post', kwargs={'id': post.id})
        full_url = ''.join(['http://', site.domain, url])
        notify(post, 'New post for you', full_url)

@receiver(post_delete, sender=models.Post, dispatch_uid='blog: delete post')
def delete_post(sender, **kwargs):
    post = kwargs['instance']
    notify(post, 'Post was deleted', 'Post "{}" was deleted from "{}"'.format(post.title, post.blog))


def insertionSort(lst):
    for index in range(1, len(lst)):

        currentvalue = lst[index]
        position = index

#
        while position > 0 and lst[position - 1] > currentvalue:
            lst[position] = lst[position - 1]
            position = position - 1

        lst[position] = currentvalue

lst = [54, 26, 93, 17, 77, 31, 44, 55, 20]#changeable
insertionSort(lst)

#prints the final result
print(lst)


#We are using the Gradient Boosting Classifier
#Combinations of Logistics Regressions and Gradient Booster ...
#Not sure, how the results will be in the submitted kaggle file.

import numpy as np
from sklearn.preprocessing import StandardScaler, MinMaxScaler
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression

data = np.load('train.npy');
data_test = np.load('test_distribute.npy')
Y_train = data[:, 0]
X_train = data[:, 1:]
X_test = data_test[:, 1:]

std_scaler = MinMaxScaler()
X_train = std_scaler.fit_transform(X_train)

clf = RandomForestClassifier(n_estimators=100,max_depth=None)
clf.fit(X_train, Y_train)
X_test = std_scaler.fit_transform(X_test)
Y_pred = clf.predict(X_test)

fout = open('rahul_ram-beats_random_forest_100est_noneMaxDepth.kaggle', 'w')
fout.write("ID,Category\n");
for i in range(Y_pred.shape[0]):
      fout.write("\n" + str(i+1) + "," + str(int(Y_pred[i]))) ## str(int(Y_pred[i]
## now make kaggle submission Y_pred
fout.close()


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

import os
import sys
import sqlite3
from Bio import SeqIO
from Bio.Alphabet import IUPAC
import pandas
import glob

DB_PATH = "/Volumes/Groups/workgroup/1002_project/SQL/"
DB_NAME = "fasta.db"
ORGANISM = "SACE"

def do_correspondance_dict(data = "/Users/dputil/Projets/1002/1002_strains_total_dataset.xlsx"):

    correspondance_dict = {}
    strains_infos = pandas.read_excel(data, 'included_dataset', index_col=None, na_values=['NA'])
    strain_name = strains_infos["Strain_Name"]
    strain_id = strains_infos["Standardized_name"]
    yjs_name = strains_infos["YJS_name"]

    for id, yjs in zip(strain_id, yjs_name):
        correspondance_dict[str(id).upper()] = str(yjs).upper()

    return correspondance_dict

def populate_db_with(fasta_files, correspondance):

    conn = sqlite3.connect(DB_PATH + DB_NAME)
    c = conn.cursor()
    c.execute("DROP TABLE IF EXISTS fasta")
    c.execute('''CREATE TABLE fasta(
    ID  INTEGER PRIMARY KEY AUTOINCREMENT,
    GENOSCOPE_ID    TEXT,
    STRAIN  TEXT,
    GENE_ID   TEXT,
    GENE_NAME   TEXT,
    SEQUENCE TEXT,
    ORGANISM    TEXT,
    CHROMOSOME  TEXT,
    START   INT,
    END INT,
    STRAND CHAR(1)
    )''')

    count = len(fasta_files)

    for fasta in fasta_files:
        count -= 1
        for record in SeqIO.parse(fasta, "fasta", alphabet=IUPAC.ambiguous_dna):

            gene_infos, chromo_infos, strand = record.description.strip().split("\t")
            strain = gene_infos.split("_")[:-2]

            if len(strain) == 1:
                strain = strain[0]
            else:
                strain = strain[0] + "_" + strain[1]

            gene_id, gene_name = gene_infos.split("_")[-2:]
            strand = strand[0]
            chromosome, positions = chromo_infos.split(":")
            start, end = positions.split("-")

            if strain not in correspondance.keys():
                pass
            else:

                data_for_db = (strain, correspondance[strain], gene_id, gene_name, str(record.seq), ORGANISM, chromosome, start, end, strand)

                c.execute("INSERT INTO fasta (GENOSCOPE_ID, STRAIN, GENE_ID, GENE_NAME, SEQUENCE, ORGANISM, CHROMOSOME, START, END, STRAND)  VALUES (?,?,?,?,?,?,?,?,?,?)", data_for_db)

        print("Data load for ", gene_id, count)

    conn.commit()
    conn.close()

if __name__ == "__main__":

    correspondance = do_correspondance_dict()

    fasta_files = glob.glob("/Users/dputil/Projets/1002/VARIANTS_INFERENCE/*.fasta")

    print(len(fasta_files))
    populate_db_with(fasta_files, correspondance)


import logging
import os

from google.cloud import storage

def parse_uri_to_bucket_and_filename(file_path):
    """Divides file uri to bucket name and file name"""
    path_parts = file_path.split("//")
    if len(path_parts) >= 2:
        main_part = path_parts[1]
        if "/" in main_part:
            divide_index = main_part.index("/")
            bucket_name = main_part[:divide_index]
            file_name = main_part[divide_index + 1 - len(main_part):]

            return bucket_name, file_name
    return "", ""

def from_gcs_to_local_file(src_gcs_bucket, src_gcs_name, local_file_path):
    storage_client = storage.Client(os.environ['PROJECT_ID'])
    # Create a bucket object for our bucket
    bucket = storage_client.get_bucket(src_gcs_bucket)
    # Create a blob object from the filepath
    blob = bucket.blob(src_gcs_name)
    # Download the file to a destination
    logging.info("Downloading gs://{}/{} to {}...".format(src_gcs_bucket, src_gcs_name, local_file_path))
    blob.download_to_filename(local_file_path)
    logging.info("Successfully downloaded gs://{}/{} to {}".format(src_gcs_bucket, src_gcs_name, local_file_path))

def is_gcs_blob_exists(bucket, blob_name):
    storage_client = storage.Client(os.environ['PROJECT_ID'])
    # Create a bucket object for our bucket
    bucket = storage_client.get_bucket(bucket)
    # Create a blob object from the filepath
    blob = bucket.blob(blob_name)
    return blob.exists()

def upload_file_to_gcs(filename, destination_bucket_name, destination_blob_name):
    """
    Uploads a file to a given Cloud Storage bucket and returns the public url
    to the new object.
    """
    bucket = storage.Client().bucket(destination_bucket_name)
    blob = bucket.blob(destination_blob_name)
    logging.info("Uploading of {} to gs://{}/{}...".format(filename, destination_bucket_name, destination_blob_name))
    blob.upload_from_filename(
        filename,
        content_type="text/plain")
    logging.info(
        "Finished uploading of {} to gs://{}/{}".format(filename, destination_bucket_name, destination_blob_name))


import pandas as pd
import yaml
from pylab import *

import AsteroidData as Ad

pd.set_option('display.width', 1000)

with open('NASA_API_KEY.yaml', 'rb') as open_yaml:
    NASA_API_SECRET = yaml.load(open_yaml)['NASA_API_KEY']

requestURL = 'https://api.nasa.gov/neo/rest/v1/feed?api_key={}'.format(NASA_API_SECRET)

asteroids = Ad.AsteroidData(requestURL)
asteroids.get_data()
asteroids.clean_data()
df = asteroids.df

# Plot data
y_axis = df['miss_distance.lunar'].tolist()
x_axis = df['days_from_now'].tolist()
area = df[Ad.SIZE_COL].tolist()
colors = df['relative_velocity.kilometers_per_hour'].tolist()

fig = plt.figure(num='Near-Earth Asteroids this Week')
fig.add_subplot(1, 1, 1, axisbg='black')
c = scatter(x_axis, y_axis, c=colors, s=area, cmap='Reds_r')
kilo_colors = plt.colorbar(c, orientation='horizontal')

## Labels
title('Near-Earth Asteroids this Week')
plt.xlabel('Days until close approach')
plt.ylabel('Min. Est. Lunar Distances of Close Approach')
kilo_colors.set_label('Kilometers Per Hour')

## Area Legend
legend_suffix = ' Est. min. ø m'
minSize, avgSize, maxSize = [int(round(calc(df[Ad.SIZE_COL]), 0)) for calc in [min, mean, max]]
minCol, avgCol, maxCol = [plt.scatter([], [], s=num, marker='o', color='#000000') for num in [minSize, avgSize, maxSize]]
minText, avgText, maxText = [str(num) + legend_suffix for num in [minSize, avgSize, maxSize]]

size_legend = plt.legend((minCol, avgCol, maxCol),
                         (minText, avgText, maxText),
                         bbox_to_anchor=(1.34, .5),
                         scatterpoints=1,
                         loc='right',
                         ncol=1,
                         fontsize='medium',
                         labelspacing=4,
                         handletextpad=3)

plt.subplots_adjust(left=.12,
                    bottom=.11,
                    right=.72,
                    top=.88,
                    wspace=.2,
                    hspace=.2)

plt.show()