from sqlalchemy import or_, and_, func

from Database.schema import MapAdminDocType, AdminSchema
from Database import Session
import traceback

session = Session()

def get_admin_without_config():
    try:
        admins = session.query(AdminSchema).filter(AdminSchema.is_config == False)
        return [admin.adminId for admin in admins]
    except:
        session.rollback()
        return -2

# map - Mapping docType with admin
def map_admin(adminId, docTypeId):
    try:
        session.add(MapAdminDocType(docTypeId=docTypeId, adminId =adminId))
        session.commit()
        return 1
    except:
        session.rollback()
        print(traceback.print_exc())
        return -2

# New document Type manually added DocumentDB
doc_list = [14,15,16,17]

# Admin who has no config defined
admin_list = get_admin_without_config()

for admin in admin_list:
    for doc in doc_list:
        print( admin, doc, map_admin(admin, doc))


# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""
import numpy as np
import pandas as pd
import struct

def rangedate(startday,endday):
    p=pd.date_range(startday,endday,freq='D')
    pstr=p.strftime('%Y%m%d')
    return pstr

def ecpre_read(filepath):

    varnum=51;tnum=41;ynum=111;xnum=133;haihe_ynum=25;haihe_xnum=25;

    filename=filepath+'ecmf_medium_surface_total_precipitation.dat'
    print(filename+' dealing...')
    pre_float=np.zeros((varnum,tnum,ynum,xnum))

    f=open(filename,"rb")
    for var in range(varnum):
        for t in range(tnum):
            for y in range(ynum):
                for x in range(xnum):
                    data=f.read(4)
                    pre_float[var][t][y][x]=struct.unpack("f",data)[0]
    f.close()

    # 各成员提取特定时效结果
    forecastdays=10
    var_pre=pre_float
    var_daypre=np.zeros((forecastdays,varnum,ynum,xnum))
    var_daypre_haihe=np.zeros((forecastdays,varnum,haihe_ynum,haihe_xnum))
    for i in range(forecastdays):
        var_daypre[i]=var_pre[:,(i+1)*4,:,:]-var_pre[:,i*4,:,:]
    var_daypre_haihe=var_daypre[:,:,66:91,80:105]
    return var_daypre_haihe

startday='2018-06-01'
endday='2018-09-30'
pstr=rangedate(startday,endday)
ecpre_tmp_path='./Data/ecpre_tmp/'
for daystr in pstr:
    ecpre_filepath='G:/grads/'+daystr+'12/'
    try:
        var_daypre=ecpre_read(ecpre_filepath)
        np.save(ecpre_tmp_path+daystr+'.npy',var_daypre)
    except:
        print(ecpre_filepath+' is empty!')
        continue


import glob
import os
from os.path import basename, splitext
if __name__ == '__main__':
    imageFile = glob.glob("/home/nsl50/Desktop/tanvir/json_augmentation/Augmentated_Image/*.")
    l = len(imageFile)
    for i in range(l):
        imageName = imageFile[i]
        a, b, c, d, e = imageName.split('.jpg')

        if d == 'pre':
            d = '' # Replace with string you require
            os.rename(imageName, "/path_to_folder/" + a + "_" + b + "_" + c + "_" + d + e)


import json
import logging
import os
from enum import Enum
from typing import Any
from typing import AnyStr
from typing import Dict
from typing import IO
from typing import Optional
from typing import Tuple
from typing import Union

from gcloud.aio.auth import AioSession  # pylint: disable=no-name-in-module
from gcloud.aio.auth import BUILD_GCLOUD_REST  # pylint: disable=no-name-in-module
from gcloud.aio.auth import Token  # pylint: disable=no-name-in-module

# Selectively load libraries based on the package
if BUILD_GCLOUD_REST:
    from requests import Session
else:
    from aiohttp import ClientSession as Session  # type: ignore[assignment]

SCOPES = [
    'https://www.googleapis.com/auth/bigquery.insertdata',
    'https://www.googleapis.com/auth/bigquery',
]

log = logging.getLogger(__name__)

def init_api_root(api_root: Optional[str]) -> Tuple[bool, str]:
    if api_root:
        return True, api_root

    host = os.environ.get('BIGQUERY_EMULATOR_HOST')
    if host:
        return True, f'http://{host}/bigquery/v2'

    return False, 'https://www.googleapis.com/bigquery/v2'

class SourceFormat(Enum):
    AVRO = 'AVRO'
    CSV = 'CSV'
    DATASTORE_BACKUP = 'DATASTORE_BACKUP'
    NEWLINE_DELIMITED_JSON = 'NEWLINE_DELIMITED_JSON'
    ORC = 'ORC'
    PARQUET = 'PARQUET'

class Disposition(Enum):
    WRITE_APPEND = 'WRITE_APPEND'
    WRITE_EMPTY = 'WRITE_EMPTY'
    WRITE_TRUNCATE = 'WRITE_TRUNCATE'

class SchemaUpdateOption(Enum):
    ALLOW_FIELD_ADDITION = 'ALLOW_FIELD_ADDITION'
    ALLOW_FIELD_RELAXATION = 'ALLOW_FIELD_RELAXATION'

class BigqueryBase:
    _project: Optional[str]
    _api_root: str
    _api_is_dev: bool

    def __init__(
            self, project: Optional[str] = None,
            service_file: Optional[Union[str, IO[AnyStr]]] = None,
            session: Optional[Session] = None, token: Optional[Token] = None,
            api_root: Optional[str] = None,
    ) -> None:
        self._api_is_dev, self._api_root = init_api_root(api_root)
        self.session = AioSession(session)
        self.token = token or Token(
            service_file=service_file, scopes=SCOPES,
            session=self.session.session,  # type: ignore[arg-type]
        )

        self._project = project
        if self._api_is_dev and not project:
            self._project = (
                os.environ.get('BIGQUERY_PROJECT_ID')
                or os.environ.get('GOOGLE_CLOUD_PROJECT')
                or 'dev'
            )

    async def project(self) -> str:
        if self._project:
            return self._project

        self._project = await self.token.get_project()
        if self._project:
            return self._project

        raise Exception('could not determine project, please set it manually')

    async def headers(self) -> Dict[str, str]:
        if self._api_is_dev:
            return {}

        token = await self.token.get()
        return {
            'Authorization': f'Bearer {token}',
        }

    async def _post_json(
            self, url: str, body: Dict[str, Any], session: Optional[Session],
            timeout: int,
    ) -> Dict[str, Any]:
        payload = json.dumps(body).encode('utf-8')

        headers = await self.headers()
        headers.update({
            'Content-Length': str(len(payload)),
            'Content-Type': 'application/json',
        })

        s = AioSession(session) if session else self.session
        resp = await s.post(url, data=payload, headers=headers,
                            timeout=timeout)
        data: Dict[str, Any] = await resp.json()
        return data

    async def _get_url(
            self, url: str, session: Optional[Session], timeout: int,
            params: Optional[Dict[str, Any]] = None,
    ) -> Dict[str, Any]:
        headers = await self.headers()

        s = AioSession(session) if session else self.session
        resp = await s.get(url, headers=headers, timeout=timeout,
                           params=params or {})
        data: Dict[str, Any] = await resp.json()
        return data

    async def _delete(
        self, url: str, session: Optional[Session], timeout: int,
    ) -> Dict[str, Any]:
        headers = await self.headers()

        s = AioSession(session) if session else self.session
        resp = await s.delete(url, headers=headers, timeout=timeout)
        data: Dict[str, Any] = await resp.json()
        return data

    async def close(self) -> None:
        await self.session.close()

    async def __aenter__(self) -> 'BigqueryBase':
        return self

    async def __aexit__(self, *args: Any) -> None:
        await self.close()


"""
Ok, second attempt. To recap, we need to take a pointcloud and
    1. Determine the k most populated pillars
    2. For each point
        a. determine if in pillar
        b. if so, add to pillar centroid
        c. and if space, add to pillar tensor
"""
import torch
import numpy as np
from numba import jit

def pillarize(pcl, pos, metadata, max_npoints=100, max_npillars=10000):
    """
    sanitize inputs and pass to numba
    """
    #first center the pointcloud
    pcl_center = pcl - pos

    #filter extraneous points
    ox = -metadata['length_x']/2.
    oy = -metadata['length_y']/2.
    lx = metadata['length_x']
    ly = metadata['length_y']
    res = metadata['resolution']
    nx = int(lx / res)
    ny = int(ly / res)
    width = max(nx, ny)
    mask = (pcl_center[:, 0] >= ox) & (pcl_center[:, 0] < ox + lx) & (pcl_center[:, 1] >= oy) & (pcl_center[:, 1] < oy + ly)
    pcl_center = pcl_center[mask]

    #bin points into cells
    grid_xs = ((pcl_center[:, 0] - ox) / res).astype(np.int32)
    grid_ys = ((pcl_center[:, 1] - oy) / res).astype(np.int32)
    grid_hashes = grid_xs * width + grid_ys
    cnts = np.bincount(grid_hashes)
    topk_grid_hashes = np.argsort(cnts)[-max_npillars:]

    pillars = np.ones((max_npillars, max_npoints, 3)) * 0.
    pillar_cnts = np.zeros(max_npillars).astype(np.int32)
    pillar_idxs = np.stack([
        topk_grid_hashes // width,
        topk_grid_hashes % width
    ], axis=-1)

    grid_hash_mapping = -np.ones(cnts.shape[0]).astype(np.int32)
    grid_hash_mapping[topk_grid_hashes] = np.arange(topk_grid_hashes.shape[0])

    pillarize_kernel(
        pcl_center,
        pillars,
        pillar_cnts,
        grid_hash_mapping,
        ox,
        oy,
        nx,
        ny,
        res,
        max_npoints
    )

    #SAM TODO: move decoration to the numba part
    pillar_centroids = pillars.sum(axis=1) / np.expand_dims(pillar_cnts, axis=-1)
    centroid_diffs = pillars - np.expand_dims(pillar_centroids, axis=1)

    pillar_centers = np.stack([
        pillar_idxs[:, 0] * res + ox + res/2.,
        pillar_idxs[:, 1] * res + oy + res/2.,
    ], axis=-1)
    pillar_diffs = pillars[:, :, :2] - np.expand_dims(pillar_centers, axis=1)

    decorated_points = np.concatenate([
        pillars,
        centroid_diffs,
        pillar_diffs
    ], axis=-1)

    k = np.arange(max_npoints).reshape(1, -1)
    k2 = np.tile(k, (max_npillars, 1))
    empty_mask = k2 >= np.expand_dims(pillar_cnts, axis=-1)
    decorated_points[empty_mask] = 1e6

    return torch.tensor(decorated_points).float(), torch.tensor(pillar_idxs).long()

@jit(nopython=True, fastmath=True)
def pillarize_kernel(
                        pcl,
                        pillars,
                        pillar_cnt,
                        grid_hash_mapping,
                        ox,
                        oy,
                        nx,
                        ny,
                        res,
                        max_npoints
                    ):
    """
    speed this part up with numba.
    Args:
        pcl: [N x 3] array of points to pillarize
        pillars: [B x P x 3] accumulator of points
        pillar_cnt: [B] array to keep track of num pts
        grid_hash_mapping:
    """
    width = max(nx, ny)
    for ii, point in enumerate(pcl):
        gx = int((point[0] - ox) / res)
        gy = int((point[1] - oy) / res)
        pt_hash = gx * width + gy
        if grid_hash_mapping[pt_hash] != -1:
            pillar_idx = grid_hash_mapping[pt_hash]
            if pillar_cnt[pillar_idx] < max_npoints:
                pillars[pillar_idx, pillar_cnt[pillar_idx]] = point
                pillar_cnt[pillar_idx] += 1

def plot_pillars(pillars, pillar_idxs, metadata):
    fig = plt.figure(figsize = (18, 9))
    ax0 = fig.add_subplot(1, 2, 1, projection='3d')
    ax1 = fig.add_subplot(1, 2, 2)
    res = metadata['resolution']
    nx = int(metadata['length_x'] / res)
    ny = int(metadata['length_y'] / res)
    pillar_grid = np.zeros([nx, ny])
    pts = []
    for pillar, pidx in zip(pillars, pillar_idxs):
        valid_mask = np.all(np.isfinite(pillar), axis=-1)
        subpillar = pillar[valid_mask]
        pts.append(subpillar)
        pillar_grid[pidx[0], pidx[1]] = subpillar.shape[0]

    allpts = np.concatenate(pts, axis=0)
    ax0.scatter(allpts[:, 0], allpts[:, 1], allpts[:, 2], s=1., alpha=0.2, c=allpts[:, 2]/allpts[:, 2].max(), cmap='rainbow')
    ax1.imshow(pillar_grid.T, origin='lower')
    plt.show()

if __name__ == '__main__':
    import time
    import rosbag
    import ros_numpy
    from sensor_msgs.msg import PointCloud2
    import matplotlib.pyplot as plt
    from mpl_toolkits import mplot3d

    bag_fp = '/home/striest/Desktop/datasets/yamaha_maxent_irl/big_gridmaps_pointclouds/rosbags_debug/2022-07-10-13-48-09_1.bag'
    bag = rosbag.Bag(bag_fp, 'r')
    pcl_topic = '/merged_pointcloud'
    pose_topic = '/integrated_to_init'
    pose = None
    times = []
    cnt = 0

    metadata = {
        'origin':None,
        'length_x':120.0,
        'length_y':120.0,
        'resolution':0.5
    }

    for topic, msg, t in bag.read_messages(topics=[pcl_topic, pose_topic]):
        if topic == pose_topic:
            pose = msg.pose

        if topic == pcl_topic and pose is not None:
            if (cnt % 100) == 0:
                #dumb rosbag hack
                msg2 = PointCloud2(
                    header = msg.header,
                    height = msg.height,
                    width = msg.width,
                    fields = msg.fields,
                    is_bigendian = msg.is_bigendian,
                    point_step = msg.point_step,
                    row_step = msg.row_step,
                    data = msg.data,
                    is_dense = msg.is_dense
                )
                pcl = ros_numpy.numpify(msg2)
                pcl_tensor = np.stack([pcl[k] for k in 'xyz'], axis=-1)
                pos_tensor = np.array([pose.pose.position.x, pose.pose.position.y, pose.pose.position.z])
                t1 = time.time()
                pillars, pillar_idxs = pillarize(pcl_tensor, pos_tensor, metadata, max_npillars=10000, max_npoints=128)
                t2 = time.time()
                times.append(t2-t1)

                plot_pillars(pillars, pillar_idxs, metadata)
                break

    times = np.array(times)
    print('Avg pillarize time: {:.4f}s'.format(times.mean()))


# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.3'
#       jupytext_version: 0.8.6
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import numpy as np
import matplotlib.pyplot as plt
x = np.random.randn(1000)
y = np.random.randn(1000)
plt.scatter(x, y)
plt.show()

plt.hist(x)

plt.hist(y)


import json
from .processing_utils import flatten_list
from collections import OrderedDict
from typing import *
from pathlib import Path
import os
import re

def get_summaries(directory: Path, docset_id: Optional[str] = None):
    eval_files = OrderedDict()
    pattern = re.compile(f'{docset_id}\.\w') if docset_id else r'D10\d\d-A\.M\.100\.\w\.\w'
    for filename in os.listdir(directory):
        if re.search(pattern, filename):
            with open(os.path.join(directory, filename), 'r', encoding='cp1252') as summary:
                eval_files[filename] = summary.read()
    return eval_files

def docset_loader(
        datafile: Path,
        docset: str,
        sentences_are_documents: Optional[bool] = False,
        merge_sentences_to_doc: Optional[bool] = False
    ) -> Tuple[List[List[str]], Dict[str, int]]:
    '''
    Read a data file and obtain the document set as a list of tokenized strings
    Args:
        - docset: The document set to extract
        - datafile: The datafile to extract the document set from
        - sentences_are_documents: Whether to consider sentences as documents. If True,
          the function returns a list of sentences for all docs in the docset. If False,
          the function returns a list of tokenized documents for all docs in the docset.
        - merge_sentences_to_doc: Whether to merge the sentences in a doc into a single string
    Returns:
        - Tuple: a list of all documents in the dataset and indices to reference those documents
    '''
    with open(datafile, 'r') as dfilestream:
        data = json.load(dfilestream)
    data = data[docset]
    documents, indexes = [], OrderedDict()
    for i, (doc_id, document) in enumerate(data.items()):
        sentences = []
        for sent in document[-1]:
            if isinstance(sent[0], list):
                sentences.extend(sent)
            else:
                sentences.append(sent)
        if sentences_are_documents:
            for j in range(len(sentences)):
                indexes[doc_id + "." + str(j)] = sentences[j]
            documents.extend(sentences)
        else:
            documents.append(flatten_list(sentences))
            indexes[doc_id] = i
    return documents, indexes

def dataset_loader(
        datafile: Path,
        sentences_are_documents: Optional[bool] = False,
        return_dict: Optional[bool] = False
    ) -> Tuple[List[List[str]], Dict[str, int]]:
    '''
    Read the whole data file as a list of documents. Docsets are ignored. This function
    is useful for calculating TFIDF on a training data
    Args:
        - datafile: The datafile to extract the document set from
        - sentences_are_documents: Whether to consider sentences as documents. If True,
          the function returns a list of sentences for all docs in the docset. If False,
          the function returns a list of tokenized documents for all docs in the docset.
        - sent_tokenize: Whether to return the documents as a dictionary of docsets to documents
    Returns:
        - Tuple: a list of all documents in the dataset and indices to reference those documents
    '''
    with open(datafile, 'r') as dfilestream:
        data = json.load(dfilestream)
    if return_dict:
        alldocuments = OrderedDict()
        for docset_id, docset in data.items():
            for doc_id, document in docset.items():
                sentences = []
                for sent in document[-1]:
                    if isinstance(sent[0], list):
                        sentences.extend(sent)
                    else:
                        sentences.append(sent)
                if sentences_are_documents:
                    for j, d in enumerate(sentences):
                        index_key = docset_id + "." + str(j)
                        alldocuments[index_key] = d
                else:
                    index_key = docset_id + "." + doc_id
                    alldocuments[index_key] = flatten_list(sentences)
        indexes = None
    else:
        alldocuments, indexes = [], OrderedDict()
        for docset_id, docset in data.items():
            for i, (doc_id, document) in enumerate(docset.items()):
                sentences = []
                for sent in document[-1]:
                    if isinstance(sent[0], list):
                        sentences.extend(sent)
                    else:
                        sentences.append(sent)
                if sentences_are_documents:
                    for j in range(len(sentences)):
                        index_key = docset_id + "." + str(j)
                        indexes[index_key] = len(alldocuments) + j
                    alldocuments.extend(sentences)
                else:
                    alldocuments.append(flatten_list(sentences))
                    index_key = docset_id + "." + doc_id
                    indexes[index_key] = i
    return alldocuments, indexes


import csv
import numpy as np
from keras import backend as K
samples = {}
ALPHABET = "abcdefghijklmnopqrstuvwxyz0123456789-,;.!?:'\"/\\|_@#$%^&*~`+ =<>()[]{}"
MAX_LENGTH =1014

import numpy as np
from six.moves import cPickle
class Alphabet:
    characters = '''abcdefghijklmnopqrstuvwxyz0123456789-,;.!?:'\"/\\|_@#$%^&*~`+-=<>()[]{}\n'''
    def __init__(self):
        self.lookup = np.zeros(256, dtype = np.int32)
        for i, c in enumerate(self.characters):
            self.lookup[ord(c)] = i + 1

    def transform(self, s):
        ''' Return the index of characters in ``s``, for characters in alphabet, return index from 1,
        from characters out of alphabet, return index 0, transform upper case to lower case
        Args:
            s (string)
        Return:
            array(dtype = np.int32) of same length of ``s``
        '''
        arr = np.frombuffer(s.lower(), dtype=np.uint8)
        return np.take(self.lookup, arr)

    @property
    def vocab_size(self):
        return len(self.characters)

def onehot(arr, dims):
    return np.take(np.identity(dims), arr, axis = 0)

def load_data(filepath, max_document_length):
    alphabet = Alphabet()
    with open(filepath, 'rb') as fi:
        texts, labels = cPickle.load(fi)
        y = onehot(labels, 2)
        x = np.zeros([len(texts), max_document_length], dtype=np.int32)
        for i, text in enumerate(texts):
            text_arr = alphabet.transform(text[:max_document_length])
            x[i, : len(text_arr)] = text_arr
        return x, y

x, y = load_data('../data/aclImdb/chr-imdb-train.pkl', MAX_LENGTH)

np.random.seed(10)
shuffle_indices = np.random.permutation(np.arange(len(y)))
x_shuffled = x[shuffle_indices]
y_shuffled = y[shuffle_indices]
# Split train/test set
n_dev_samples = 1000
# TODO: Create a fuckin' correct cross validation procedure
x_train, x_dev = x_shuffled[:-n_dev_samples], x_shuffled[-n_dev_samples:]
y_train, y_dev = y_shuffled[:-n_dev_samples], y_shuffled[-n_dev_samples:]
print("Train/Dev split: {:d}/{:d}".format(len(y_train), len(y_dev)))

import model_keras
import model
import tensorflow as tf
import os
import time
import datetime
def batch_iter(x, y, batch_size, num_epochs, shuffle=True):
    """
    Generates a batch iterator for a dataset.
    """
    # data = np.array(data)
    data_size = len(x)
    num_batches_per_epoch = int(data_size/batch_size) + 1
    for epoch in range(num_epochs):
        print("In epoch >> " + str(epoch + 1))
        print("num batches per epoch is: " + str(num_batches_per_epoch))
        # Shuffle the data at each epoch
        if shuffle:
            shuffle_indices = np.random.permutation(np.arange(data_size))
            x_shuffled = x[shuffle_indices]
            y_shuffled = y[shuffle_indices]
        else:
            x_shuffled = x
            y_shuffled = y
        for batch_num in range(num_batches_per_epoch-1):
            start_index = batch_num * batch_size
            end_index = min((batch_num + 1) * batch_size, data_size)
            x_batch = x_shuffled[start_index:end_index]
            y_batch = y_shuffled[start_index:end_index]
            batch = list(zip(x_batch, y_batch))
            yield batch

import os
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--batch_size', type=int, default=128)
parser.add_argument('--num_epochs', type=int, default=20)
parser.add_argument('--gpu', type=str, default="")
CharCNN = model.CharCNN
args = parser.parse_args()

os.environ['CUDA_VISIBLE_DEVICES']=args.gpu
batch_size = args.batch_size
num_epochs = args.num_epochs

with tf.Graph().as_default():
    cnn = CharCNN(num_classes=2)
    session_conf = tf.ConfigProto(
        log_device_placement=False)
    session_conf.gpu_options.per_process_gpu_memory_fraction = 0.5
    sess = tf.Session(config=session_conf)
    with sess.as_default():
        # Define Training procedure
        global_step = tf.Variable(0, name="global_step", trainable=False)
        optimizer = tf.train.AdamOptimizer(1e-3)
        grads_and_vars = optimizer.compute_gradients(cnn.loss)
        # print('updates oprs = {}'.format(len(cnn.updates.updates)))
        if hasattr(cnn, 'updates'):
            with tf.control_dependencies(cnn.updates):
                train_op = optimizer.apply_gradients(grads_and_vars, global_step=global_step)
        else:
            train_op = optimizer.apply_gradients(grads_and_vars, global_step=global_step)

        # Keep track of gradient values and sparsity (optional)
        grad_summaries = []
        for g, v in grads_and_vars:
            if g is not None:
                grad_hist_summary = tf.summary.histogram(
                    "{}/grad/hist".format(v.name), g)
                sparsity_summary = tf.summary.scalar(
                    "{}/grad/sparsity".format(v.name), tf.nn.zero_fraction(g))
                grad_summaries.append(grad_hist_summary)
                grad_summaries.append(sparsity_summary)
        grad_summaries_merged = tf.summary.merge(grad_summaries)

        # Output directory for models and summaries
        timestamp = str(int(time.time()))
        out_dir = os.path.abspath(os.path.join(
            os.path.curdir, "runs_new", timestamp))
        print("Writing to {}\n".format(out_dir))

        # Summaries for loss and accuracy
        loss_summary = tf.summary.scalar("loss", cnn.loss)
        acc_summary = tf.summary.scalar("accuracy", cnn.accuracy)

        # Train Summaries
        train_summary_op = tf.summary.merge(
            [loss_summary, acc_summary, grad_summaries_merged])
        train_summary_dir = os.path.join(out_dir, "summaries", "train")
        train_summary_writer = tf.summary.FileWriter(
            train_summary_dir, sess.graph)

        # Dev summaries
        dev_summary_op = tf.summary.merge([loss_summary, acc_summary])
        dev_summary_dir = os.path.join(out_dir, "summaries", "dev")
        dev_summary_writer = tf.summary.FileWriter(dev_summary_dir, sess.graph)

        # Checkpoint directory. Tensorflow assumes this directory already
        # exists, so we need to create it.
        checkpoint_dir = os.path.abspath(os.path.join(out_dir, "checkpoints"))
        checkpoint_prefix = os.path.join(checkpoint_dir, "model")
        if not os.path.exists(checkpoint_dir):
            os.makedirs(checkpoint_dir)
        saver = tf.train.Saver(tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES))

        # Initialize all variables
        sess.run(tf.variables_initializer(tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES)))
        def train_step(x_batch, y_batch):
            """
            A single training step
            """
            x_batch = np.array(x_batch)
            feed_dict = {
              cnn.input_x: x_batch,
              cnn.input_y: y_batch,
              cnn.training:1,
              cnn.dropout_keep_prob: 0.5,
              K.learning_phase() : 1
            }

            _, step, summaries, loss, accuracy = sess.run(
                [train_op, global_step, train_summary_op,
                    cnn.loss, cnn.accuracy],
                feed_dict)
            time_str = datetime.datetime.now().isoformat()
            # write fewer training summaries, to keep events file from
            # growing so big.
            if step % (evaluate_every / 2) == 0:
                print("{}: step {}, loss {:g}, acc {:g}".format(
                    time_str, step, loss, accuracy))
                train_summary_writer.add_summary(summaries, step)
        def dev_step(x_batch, y_batch, writer=None):
            dev_size = len(x_batch)
            max_batch_size = 500
            num_batches = dev_size/max_batch_size
            acc = []
            losses = []
            #print("Number of batches in dev set is " + str(num_batches))
            for i in range(num_batches):
                x_batch_dev = x_batch[i * max_batch_size:(i + 1) * max_batch_size]
                y_batch_dev = y_batch[i * max_batch_size: (i + 1) * max_batch_size]
                feed_dict = {
                  cnn.input_x: x_batch_dev,
                  cnn.input_y: y_batch_dev,
                  cnn.training:0,
                  cnn.dropout_keep_prob: 1.0,
                  K.learning_phase() : 0
                }
                step, summaries, loss, accuracy = sess.run(
                    [global_step, dev_summary_op, cnn.loss, cnn.accuracy],
                    feed_dict)
                acc.append(accuracy)
                losses.append(loss)
                time_str = datetime.datetime.now().isoformat()
                #print("batch " + str(i + 1) + " in dev >>" +" {}: loss {:g}, acc {:g}".format(time_str, loss, accuracy))
                if writer:
                    writer.add_summary(summaries, step)
            print("Mean accuracy=" + str(sum(acc)/len(acc)))
            print("Mean loss=" + str(sum(losses)/len(losses)))
        # just for epoch counting
        num_batches_per_epoch = int(len(x_train)/batch_size) + 1
        # Generate batches
        batches = batch_iter(x_train, y_train,batch_size, num_epochs)
        evaluate_every = 300
        checkpoint_every =900
        # Training loop. For each batch...
        for batch in batches:
            x_batch, y_batch = zip(*batch)
            train_step(x_batch, y_batch)
            current_step = tf.train.global_step(sess, global_step)
            if current_step % evaluate_every == 0:
                print("\nEvaluation:")
                dev_step(x_dev, y_dev, writer=dev_summary_writer)
                print("Epoch: {}".format(
                    int(current_step / num_batches_per_epoch)))
                print("")
            if current_step % checkpoint_every == 0:
                path = saver.save(
                    sess, checkpoint_prefix, global_step=current_step)
                print("Saved model checkpoint to {}\n".format(path))


#!/usr/bin/env python3

from pwn import *
import random
import os

num_sent = 0
while True:
    num_sent = 0
    print("Restarting...")
    p = process('./dual')

    last_text_len = None

    fd = open(b'poc', 'wb')
    def sendline(payload):
        global num_sent
        fd.write(payload + b'\n')
        p.sendline(payload)
        num_sent += 1

#context.log_level = 'debug'
    with log.progress('Fuzzing progress') as s:
        while True:
            if num_sent >= 15000:
                p.close()
                break
            elif num_sent % 1000 == 0:
                s.status('Tried %d/150000 operations' % num_sent)

            op = p.recvuntil(b'>\n', drop=True).rsplit(b'\n', 1)[-1]
            #print('Current operation:', op)
            if op[-2:] == b'op':
                sendline(str(random.choice(range(1,7+1))).encode())
            elif op in [b'pred_id', b'succ_id', b'node_id']:
                sendline(random.choice(['1','2','3', str(random.randint(1, 10000))]).encode())
            elif op in [b'text_len', b'bin_len']:
                last_text_len = random.randint(2, 200)
                sendline(str(last_text_len).encode())
            elif op in [b'bin', b'text']:
                sendline(b"A" * (last_text_len-1))
            else:
                continue
                #p.interactive()
                #raise ValueError('Unknown op:' + str(op))


from jh_trakr.job_app import new_app, sanitize_filename
import pytest
import sqlite3
import os

TEST_DB = "test_database.db"
TEST_DB_NO_SFX = TEST_DB.split('.')[0]
TEST_WORK_DIR = "working_test"

@pytest.mark.new_app
def test_new_app_creates_db(cleanup_test_db):
    """ Tests that new_app creates a new database """

    """ Improvemnts
        - test could be improved by checking the .schema of the db matches the
          expected table
        - Refactor test_args so that it can be parameterized and ran with data
          from the faker module
    """
    test_args = ["Company", "Position", "Location", "URL"]

    new_app(database=TEST_DB, test_args=test_args, work_dir=TEST_WORK_DIR)

    assert os.path.exists(TEST_DB)

@pytest.mark.new_app
def test_new_app_creates_dirs(cleanup_test_db):
    """ Tests that new_app creates working directory and application
    subdirectory """

    """ Improvements
        - Refactor test_args so that it can be parameterized and ran with data
          from the faker module
    """
    test_args = ["Company", "Position", "Location", "URL"]
    title = sanitize_filename(f"{test_args[1]}-at-{test_args[0]}")
    id = new_app(database=TEST_DB, test_args=test_args, work_dir=TEST_WORK_DIR)

    assert os.path.exists(TEST_WORK_DIR)
    assert os.path.exists(os.path.join(TEST_WORK_DIR, f"{title}-{id}"))

@pytest.mark.new_app
def test_new_app_correct_db_entry(cleanup_test_db):
    """ Tests that new_app creates a correct entry in the database """

    """ Improvemnts
        - Refactor test_args so that it can be parameterized and ran with data
          from the faker module
    """
    test_args = ["Company", "Position", "Location", "URL"]
    id = new_app(database=TEST_DB, test_args=test_args, work_dir=TEST_WORK_DIR)

    with sqlite3.connect(TEST_DB) as con:
        cur = con.cursor()
        cur.execute(f"SELECT * FROM {TEST_DB_NO_SFX} WHERE id = {id};")
        row = cur.fetchone()

    for arg in test_args:
        assert arg in row
    assert "working" in row
    assert id in row


import glob
import os

import luigi
import numpy as np
import pandas as pd
import pytest
from tifffile import imwrite

from ggjw.tasks.data_management.delete_files import DeleteFilesTask
from ggjw.workflows.delete_files import DeleteFilesWorkflow

# run test
@pytest.mark.parametrize(
    'workflow', [DeleteFilesTask, DeleteFilesWorkflow])  # workflows you want to test
def test_delete_task(tmpdir, workflow):
    files_to_be_there, n_deleted, n_original, test_dir = create_test_data(tmpdir)

    # TEST if the files are created
    img_list = glob.glob(os.path.join(test_dir, "*.stk"))
    print(str(np.size(img_list)))
    print(test_dir)

    assert np.size(img_list) == n_original

    # runs the workflow noted in the pytest.mark.parameterize with given input folder
    result = luigi.build([
        workflow(image_folder=test_dir,  # where the images are
                 csv_document=os.path.join(test_dir, "goodworms.csv"),
                 image_file_pattern='*.stk',
                 data_format='st')
    ],
        local_scheduler=True,
        detailed_summary=True)

    # TEST if the worfklow has ran
    if result.status not in [
        luigi.execution_summary.LuigiStatusCode.SUCCESS,
        luigi.execution_summary.LuigiStatusCode.SUCCESS_WITH_RETRY
    ]:
        raise RuntimeError(
            'Luigi failed to run the workflow! Exit code: {}'.format(result))

    # TEST if original and deleted files csv is there
    assert os.path.exists(os.path.join(test_dir, "original_files.csv"))
    assert os.path.exists(os.path.join(test_dir, "deleted_files.csv"))

    original_files = pd.read_csv(os.path.join(test_dir, "original_files.csv"))
    deleted_files = pd.read_csv(os.path.join(test_dir, "deleted_files.csv"))

    remaining_files = pd.concat([original_files, deleted_files]).drop_duplicates(keep=False)
    for file_path in remaining_files['File']:
        assert os.path.exists(file_path)
        assert file_path in files_to_be_there

    # TEST if gere are _s number of files in the folder
    img_list = glob.glob(os.path.join(test_dir, "*.stk"))
    assert np.size(img_list) == n_deleted

def create_test_data(tmpdir):
    """
    Create test data.
    """
    test_dir = tmpdir
    csv = {'Position': [1, 2, 3], 'Quality': [1, 0, 1],
           "Hatch": [2, 2, 0], "Escape": [4, 2, 6]}
    csv = pd.DataFrame(csv)
    csv.to_csv(os.path.join(test_dir, "goodworms.csv"))
    # create images
    n_original = 15
    n_deleted = 8
    for s in [1, 2, 3]:
        for t in [1, 2, 3, 4, 5]:
            image_to_save = np.ones([5, 20, 20])
            img_name = os.path.join(
                test_dir, "w1Marit-488-BF-Cam0_s" + str(s) + "_t" + str(t) + ".stk")
            imwrite(img_name, image_to_save)
    files_to_be_there = [os.path.join(test_dir, "w1Marit-488-BF-Cam0_s" + str(1) + "_t" + str(2) + ".stk"),
                         os.path.join(test_dir, "w1Marit-488-BF-Cam0_s" + str(1) + "_t" + str(3) + ".stk"),
                         os.path.join(test_dir, "w1Marit-488-BF-Cam0_s" + str(1) + "_t" + str(4) + ".stk"),
                         os.path.join(test_dir, "w1Marit-488-BF-Cam0_s" + str(3) + "_t" + str(1) + ".stk"),
                         os.path.join(test_dir, "w1Marit-488-BF-Cam0_s" + str(3) + "_t" + str(2) + ".stk"),
                         os.path.join(test_dir, "w1Marit-488-BF-Cam0_s" + str(3) + "_t" + str(3) + ".stk"),
                         os.path.join(test_dir, "w1Marit-488-BF-Cam0_s" + str(3) + "_t" + str(4) + ".stk"),
                         os.path.join(test_dir, "w1Marit-488-BF-Cam0_s" + str(3) + "_t" + str(5) + ".stk")]
    return files_to_be_there, n_deleted, n_original, test_dir


'''
Created on Jul 28, 2015

upload the CCLE metadata into CloudSQL

Copyright 2015, Institute for Systems Biology.

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.

@author: michael
'''
from datetime import date
import json
import logging
import sys
import traceback

# from isbcgc_metadata_cloudsql_model import ISBCGC_metadata_database_helper
from util import create_log
from util import import_module

def getlog(configFileName):
    try:
        with open(configFileName) as configFile:
            config = json.load(configFile)
        log_dir = str(date.today()).replace('-', '_') + '_' + config['log_dir_tag'] + '/'
        log_name = create_log(log_dir, 'create_ccle_metadata')
        log = logging.getLogger(log_name)
    except Exception as e:
        traceback.print_exc(5)
        raise e
    return config, log

def checkliteral(fields, value, name2index):
    if "'" == value[0]:
        return value[1:-1]
    else:
        return fields[name2index[value]]

def choice(fields, choices, name2index):
    '''
        choices = [fieldname1, value, fieldname2]
        choice1 = fields[name2index[fieldname1]]
        choice2 = fields[name2index[fieldname2]]
        if choice1 == value:
            return choice2
        else:
            return choice1
    '''
    value = fields[name2index[choices[0]]]
    if value == choices[1]:
        return fields[name2index[choices[2]]]
    else:
        return value

def mapit(config, fields, mappings, name2index):
    '''
        mappings = [fieldname, mapname]:
        key = fields[name2index[fieldname]]
        return config[mapname][key]
            or
        mappings = [fieldname1!fieldname2, mapname]:
        key1 = fields[name2index[fieldname1]]
        key2 = fields[name2index[fieldname2]]
        return config[mapname][key1][key2]
    '''
    mapkeys = mappings[0].split('!')
    if 1 == len(mapkeys):
        return config[mappings[1]][fields[name2index[mapkeys[0]]]]
    else:
        return config[mappings[1]][fields[name2index[mapkeys[0]]]][fields[name2index[mapkeys[1]]]]

def concat(config, fields, pieces, name2index):
    retVal = ''
    for piece in pieces:
        if 1 < len(piece.split('%')):
            # nested map
            retVal += mapit(config, fields, piece.split('%')[1:], name2index)
        else:
            retVal += checkliteral(fields, piece, name2index)
    return retVal

def substr(config, fields, pieces, name2index):
    return checkliteral(fields, pieces[0], name2index)[int(pieces[1]):int(pieces[2])]

def create_row(config, mapname, fields, name2index):
    retVal = []
    for key, _ in config[mapname]:
        mapping = key.split(':')
        if 1 == len(mapping):
            retVal += [checkliteral(fields, mapping[0], name2index)]
        elif 'choice' == mapping[0]:
            retVal += [choice(fields, mapping[1:], name2index)]
        elif 'map' == mapping[0]:
            retVal += [mapit(config, fields, mapping[1:], name2index)]
        elif 'concat' == mapping[0]:
            retVal += [concat(config, fields, mapping[1:], name2index)]
        elif 'substr' == mapping[0]:
            retVal += [substr(config, fields, mapping[1:], name2index)]
    return tuple(retVal)

def create_insert_columns(mapping):
    retVal = []
    for (_, value) in mapping:
        retVal += [value]
    return retVal

def parse_metadata(config, log):
    clinical_index = 0
    biospeciman_index = 1
    data_index = 2
    samples_index = 3
    try:
        log.info('\tbegin parse %s' % (config['input_file']))
        count = 0
        with open(config['input_file'], 'r') as input_file:
            #red the header line and map the indices
            header = input_file.readline()
            count += 1
            name2index = {}
            for index, name in enumerate(header.split('\t')):
                name2index[name.strip()] = index

            table_columns = [
                create_insert_columns(config['clinical_column_mapping']),
                create_insert_columns(config['biospecimen_column_mapping']),
                create_insert_columns(config['data_column_mapping']),
                create_insert_columns(config['samples_column_mapping'])
            ]
            table_rows = [set(), set(), set(), set()]
            for line in input_file:
                if 0 == count % 128:
                    log.info('\t\tprocessed %s rows' % (count))
                count += 1
                fields = [field.strip() for field in line.split('\t')]
                table_rows[clinical_index].add(create_row(config, 'clinical_column_mapping', fields, name2index))
                table_rows[biospeciman_index].add(create_row(config, 'biospecimen_column_mapping', fields, name2index))
                table_rows[data_index].add(create_row(config, 'data_column_mapping', fields, name2index))
                table_rows[samples_index].add(create_row(config, 'samples_column_mapping', fields, name2index))
        log.info('\tend parse %s.  processed %s total rows' % (config['input_file'], count))
    except Exception as e:
        log.exception('problem parsing on line %s' % (count))
        raise e
    return table_columns, table_rows

def insert_metadata(config, table_columns, table_rows, log):
    try:
        datastore = import_module(config['database_module'])
        tables = ['metadata_clinical', 'metadata_biospecimen', 'metadata_data', 'metadata_samples']
        for index in range(len(tables)):
            datastore.ISBCGC_database_helper.column_insert(config, list(table_rows[index]), tables[index], table_columns[index], log)

    except Exception as e:
        log.exception('problem saving metadata to the database')
        raise e

def main(configFileName):
    config, log = getlog(configFileName)
    log.info('begin create ccle metadata')
    table_columns, table_rows = parse_metadata(config, log)
    insert_metadata(config, table_columns, table_rows, log)
    log.info('end create metadata data dictionary')

if __name__ == '__main__':
    main(sys.argv[1])


from machine import Pin, Timer
import time

led = Pin(15, Pin.OUT)
button = Pin(14, Pin.IN, Pin.PULL_DOWN)

# Enlosschleife die den Druck auf den Knopf erkennt
while True:
    if button.value():
        led.toggle()
        time.sleep(0.5)


n, k = map(int, input().split())
array = []
for _ in range(n):
    array.append(int(input()))

count = 0
for i in range(n-1, -1, -1):
    count += k // array[i]
    k = k % array[i]

print(count)


import smtplib
import ssl

class Mailer:

    """
    This script initiaties the email alert function.
    """

    def send(self,mail,s):
        server = smtplib.SMTP_SSL('smtp.gmail.com', 465)

        # Authentication
        server.login("sandiashwi@gmail.com", "Srini1234")

        # message to be sent
        SUBJECT = 'ALERT!'
        TEXT = 'Social distancing violations exceeded! \nserious violation is :{}'.format(s)
        message = 'Subject: {}\n\n{}'.format(SUBJECT, TEXT)

        # sending the mail
        server.sendmail("sandiashwi@gmail.com", mail, message)

        # terminating the session
        server.quit()


#1
import openpyxl
from openpyxl import Workbook
wb = openpyxl.load_workbook('task10_2.xlsx')
for sh in wb.sheetnames:
    ws = wb[sh]
    print(ws.title, '--------------------')
    for i in range(1, ws.max_row +1):
        for j in range(1, ws.max_column +1):
            print(i+1, j+1, ws.cell(i, j).value)
#2
import openpyxl
from openpyxl import Workbook
wb = openpyxl.load_workbook('task10_2.xlsx')
sheets = wb.sheetnames
sheet = wb[sheets[2]]
tes = set()
dict = {}
for i in range(sheet.max_row):
    tes.add(sheet.cell(row = i+1, column = 1).value)
lst = list(tes)
for n in range(len(lst)):
    dict.setdefault(lst[n], 0)
for i in range(sheet.max_row):
    s = sheet.cell(row = i + 1, column = 1).value
    dict[s] = dict[s] + sheet.cell(row=i + 1, column=2).value
sheet.append(['ИТОГО', sum(dict.values())])
sheet = wb[sheets[3]]
sheet.append({1:'111', 2:'222', 3:'333'})
wb.save('task10_2.xlsx')
#3
import time
x = 1000000
for i in range(0, x*10, x):
    t0 = time.time()
    su = 0
    for j in range(i):
        su += j
    t1 = time.time()
    print(i, t1 - t0)
#4
import time
d = {}
for i in range(11):
    if i % 2 == 0:
        def sl2():
            t0 = time.time()
            time.sleep(2)
            t1 = time.time()
            return (t1 - t0)
        d.setdefault(i, sl2())
    else:
        def sl3():
            t0 = time.time()
            time.sleep(3)
            t1 = time.time()
            return (t1 - t0)
        d.setdefault(i, sl3())
for i in range(len(d)):
    print(i, d[i])
print(sum(d.values()))
#5
import datetime
from datetime import date
import locale
locale.setlocale(locale.LC_ALL, "ru")
birthday = date(1995, 1, 11)
print('Название месяца:', birthday.strftime('%B'))
print('Название дня недели:', birthday.strftime('%A'))
print('Год:', birthday.strftime('%Y'))
print('Месяц:', birthday.strftime('%m'))
print('День:', birthday.strftime('%d'))
#6
import calendar
year = int(input('Введите год: '))
d = {0:0, 1:0, 2:0, 3:0, 4:0, 5:0, 6:0}
for month in range(1, 13):
    for day in range(1, calendar.monthrange(year, month)[1] + 1):
        print(year, month, day)
        print(calendar.weekday(year, month, day))
        dd = calendar.weekday(year, month, day)
        d[dd] += 1
print(d)

Lessons/Lesson 11/Lesson 11 (archive).py


__author__ = "ResearchInMotion"

import random

def rand_list(limit):
    arr = []
    for i in range(1,limit):
        arr.append(random.randint(1,40))
    return arr

list = rand_list(10)

def linearSearch(num):
    val = "Value not found"
    for i in list:
        if i ==num:
            val = "value found"
    return val

ln = linearSearch(3)
print(ln)

def bubbleSort(mylist):
    for i in range(0,len(mylist)):
        for j in range(len(mylist)-i-1):
            if(mylist[j]>mylist[j+1]):
                mylist[j] , mylist[j+1] = mylist[j+1] , mylist[j]
    return mylist

print(bubbleSort(list))


from django.urls import path
from MVT_APP.views import *
from authentication_app.views import *
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path("inicio/", homepage, name="homepage"),
    path("Libros/", libros, name="libros"),
    path("Libros/nuevo", libros_crear, name="libro-crear"),
    path("autores/", autores, name="autores"),
    path("autores/nuevo", autores_crear, name="autores-crear"),
    path("editoriales/", editoriales, name="editoriales"),
    path("editoriales/nuevo", editoriales_crear, name="editoriales-crear"),
    path("editoriales/buscar/resultados/", resultados_busqueda_editoriales, name="editorial_resultados"),
    path("libros/buscar/resultados/", resultados_busqueda_libros, name="libro_resultados"),
    path("autores/buscar/resultados/", resultados_busqueda_autores, name="autor_resultados"),
    path("accounts/login/", iniciar_sesion, name="auth-login"),
    path("accounts/signup/", registrar_usuario, name="auth-register"),
    path("logout/", cerrar_sesion, name="auth-logout"),
    path("accounts/profile/editar/", editar_perfil, name="auth-editar-perfil"),
    path("accounts/profile/avatar/", agregar_avatar, name="auth-avatar"),
    path("editoriales/borrar/<pk>", EditorialDelete.as_view(), name="editorial-delete"),
    path("editoriales/actualizar/<pk>", EditorialEdit.as_view(), name="editorial-edit"),
    path("editoriales/listar/", EditorialList.as_view(), name="editorial-list"),
    path("editoriales/detalle/<pk>", EditorialDetail.as_view(), name="editorial-detail"),
    path("libros/borrar/<pk>", LibroDelete.as_view(), name="libro-delete"),
    path("libros/actualizar/<pk>", LibroEdit.as_view(), name="libro-edit"),
    path("libros/listar/", LibroList.as_view(), name="libro-list"),
    path("libros/detalle/<pk>", LibroDetail.as_view(), name="libro-detail"),
    path("autores/borrar/<pk>", AutorDelete.as_view(), name="autor-delete"),
    path("autores/actualizar/<pk>", AutorEdit.as_view(), name="autor-edit"),
    path("autores/listar/", AutorList.as_view(), name="autor-list"),
    path("autores/detalle/<pk>", AutorDetail.as_view(), name="autor-detail"),
    path("about/", sobre_nosotros, name="about"),

]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


#!/usr/bin/env python3

from optparse import OptionParser
import subprocess, os, re

VERSION = "0.2 beta"
PROGRAM = "/usr/local/bin/HandBrakeCLI"
PROFILE = "iPhone & iPod Touch"
N_THREADS = 2

STYLE = {
    "default"    :     "\033[m",
    # styles
    "bold"       :     "\033[1m",
    "underline"  :     "\033[4m",
    "blink"      :     "\033[5m",
    "reverse"    :     "\033[7m",
    "concealed"  :     "\033[8m",
    # font colors
    "black"      :     "\033[30m",
    "red"        :     "\033[31m",
    "green"      :     "\033[32m",
    "yellow"     :     "\033[33m",
    "blue"       :     "\033[34m",
    "magenta"    :     "\033[35m",
    "cyan"       :     "\033[36m",
    "white"      :     "\033[37m",
    # background colors
    "on_black"   :     "\033[40m",
    "on_red"     :     "\033[41m",
    "on_green"   :     "\033[42m",
    "on_yellow"  :     "\033[43m",
    "on_blue"    :     "\033[44m",
    "on_magenta" :     "\033[45m",
    "on_cyan"    :     "\033[46m",
    "on_white"   :     "\033[47m"
}

def cprint(text, color="white"):
    try:
        print(STYLE[color] + text + STYLE["white"])
    except KeyError:
        print(STYLE["white"] + text)

def printerror(text):
    cprint(text, "red")

def printinfo(text):
    cprint(text, "cyan")

def printmessage(text):
    cprint("\n=======================================================\n" + text, "magenta")

def changecolor(color):
    print(STYLE[color])

class file:
    namelist = set()
    append = 0

    def __init__(self, filename):
        self.name, self.extension = ( x for x in os.path.splitext(filename) )
        if self.isvideo():
            self.nname = re.sub('\W+', '_', self.name)
            if self.nname in self.namelist:
                self.nname += str(self.append)
                self.append += 1
            self.namelist.add(self.nname)
            self.nextension = ".avi"
        else:
            self.nname, self.nextension = None, None

    #@staticmethod
    def isvideo(self):
        if self.name == "Makefile": return False
        if self.name.startswith("."): return False
        if self.extension == ".py": return False
        if self.extension == "": return False
        return True

    def fullname(self):
        return self.name + self.extension

    def nfullname(self):
        if self.isvideo():
            return self.nname + self.nextension
        else:
            return ""

    def renameforward(self):
        if self.isvideo():
            try:
                os.rename(self.fullname(), self.nfullname())
                printinfo("...Renamed " + self.fullname() + " -> " + self.nfullname())
                return True
            except OSError:
                printerror("Failed to rename file " + self.fullname() + " to " + self.nfullname())
                return False
        return False

    def renameback(self):
        if self.isvideo():
            try:
                os.rename(self.nfullname(), self.fullname())
                printinfo("...Renamed " + self.nfullname() + " -> " + self.fullname())
                return True
            except OSError:
                printerror("Failed to rename file " + self.nfullname() + " to " + self.fullname())
                return False
        return False

    def removenfile(self):
        if self.isvideo():
            try:
                os.remove(self.nfullname())
                printinfo("...Removed " + self.nfullname())
                return True
            except OSError:
                printerror("Cannot remove " + self.nfullname())
                return False
        return False

class filelist():
    def __init__(self, path):
        self.list = []
        i = 0
        os.chdir(path)
        printmessage("Building file list...")

        for filename in os.listdir(path):
            newfile = file(filename)
            if newfile.isvideo():
                i += 1
                self.list.append(newfile)
                newfile.renameforward()

        self.length = i

    def writemakefile(self):
        line = "all:"
        for f in self.list:
            line += " " + f.nname + ".m4v"
        line += "\n\n%.m4v: %.avi\n\t"
        line += PROGRAM + " -Z '" + PROFILE
        line += "' -i $< -o $@\n"

        fh = None
        try:
            fh = open("Makefile", "w")
            fh.write(line)
        except EnvironmentError:
            printerror("Error writing Makefile, exiting")
            return False
        finally:
            if fh: fh.close()

        return True

    def convert(self, n_threads):
        printmessage("Converting...")
        try:
            changecolor("green")
            n_threads = n_threads if (n_threads >= 1) & (n_threads <= 10) else N_THREADS
            res = subprocess.Popen("make -j" + str(n_threads), shell=True)
            res.wait()
            return True
        except OSError:
            printerror("Execution failed")
            return False

    def cleanfiles(self, leave, remove):
        printmessage("Cleaning up...")
        try:
            if not leave:
                os.remove("Makefile")
            else:
                printinfo("...Leaving Makefile")
        except OSError:
            printerror("Cannot remove Makefile", "red")

        for f in self.list:
            if remove:
                f.removenfile()
            else:
                f.renameback()

def main():
    # Program options definition
    usage = "Usage: %prog [path]"
    parser = OptionParser(usage=usage, version=VERSION)
    parser.add_option("-l", "--leave",
                      action="store_true",
                      default=False,
                      help="leave Makefile after process completion [default: off]")
    parser.add_option("-r", "--remove",
                      action="store_true",
                      default=False,
                      help="remove source files after conversion [default: off]")
    parser.add_option("-t", "--threads",
                      type="int",
                      default=2,
                      help="number of threads [default: 2]")

    opts, args = parser.parse_args()
    if args == []: args = "."

    fl = filelist(args[0])
    if fl.length > 0:
        fl.writemakefile()
        fl.convert(opts.threads)
        fl.cleanfiles(opts.leave, opts.remove)
        printmessage("Finished!")
    else:
        printerror("Nothing to convert, exiting")

main()


# -*- coding: utf-8 -*-
'''
Verification of the Brinch Hansen formula.

See Brinch Hansen. A general formula for bearing capacity. The Danish Geotechnical Institute. Bulletin 11. Copenhagen 1961. Example on page 45
'''

from __future__ import division
from __future__ import print_function

__author__= "Luis C. Pérez Tato (LCPT)"
__copyright__= "Copyright 2016, LCPT"
__license__= "GPL"
__version__= "3.0"
__email__= "l.pereztato@gmail.com"

from geotechnics import frictional_cohesive_soil as fcs
import math

# Short-term bearing capacity.
shortTermSoil= fcs.FrictionalCohesiveSoil(0.0,c=10.3,rho= (2.2-1.0)/9.81)

D= 2.0 # Foundation depth
Beff= 5.5 # Effective foundation width
Leff= 9.0 # Effective foundation length
V= 3000.0 # Vertical load
H= 225.0 # Horizontal load.
deltaB= math.atan(H/V)
deltaL= 0.0
q= 4.4

NgammaSTS= shortTermSoil.Ngamma(1.8)
NqSTS= shortTermSoil.Nq()
NcSTS= shortTermSoil.Nc()
dcSTS= shortTermSoil.dc(D,Beff)
scSTS= shortTermSoil.sc(Beff,Leff)
icSTS= shortTermSoil.ic(Vload= V, HloadB= H, HloadL= 0.0,Beff= Beff, Leff= Leff)
quSTS= shortTermSoil.qu(q,D,Beff,Leff,V,H,0.0,1.8)

err= abs(NgammaSTS)**2
err+= abs(NqSTS-1.0)**2
err+= (abs(NcSTS-5.1)/5.1)**2
err+= (abs(dcSTS-1.13)/1.13)**2
err+= (abs(scSTS-1.12)/1.12)**2
err+= (abs(icSTS-0.89)/0.89)**2 #Better approximation of i_c than
                                #those employed in reference example
err+= (abs(quSTS-64.0)/64.0)**2 #Better approximation of i_c than
                                #those employed in reference example

# Long-term bearing capacity.
longTermSoil= fcs.FrictionalCohesiveSoil(math.radians(30.0),c=1.7,rho= (2.2-1.0)/9.81)

NgammaLTS= longTermSoil.Ngamma(1.8)
NqLTS= longTermSoil.Nq()
NcLTS= longTermSoil.Nc()
dcLTS= longTermSoil.dc(D,Beff)
scLTS= longTermSoil.sc(Beff,Leff)
sgammaLTS= longTermSoil.sgamma(Beff,Leff)
dgammaLTS= longTermSoil.dgamma()
iqLTS= longTermSoil.iq(Vload= V, HloadB= H, HloadL= 0.0)
sqLTS= longTermSoil.sq(Beff,Leff)
dqLTS= longTermSoil.dq(D,Beff)
igammaLTS= longTermSoil.igamma(Vload= V, HloadB= H, HloadL= 0.0, Beff= Beff, Leff= Leff)
icLTS= longTermSoil.ic(Vload= V, HloadB= H, HloadL= 0.0, Beff= Beff, Leff= Leff)
quGammaLTS= longTermSoil.quGamma(D,Beff,Leff,V,H,0.0,1.8)
quGammaTeorLTS= 0.5*1.2*Beff*NgammaLTS*sgammaLTS*dgammaLTS*igammaLTS
quCohesionLTS= longTermSoil.quCohesion(D= D, Beff= Beff, Leff= Leff, Vload= V, HloadB= H, HloadL= 0.0, psi= 0.0, eta= 0.0)
quCohesionTeorLTS= 1.7*NcLTS*scLTS*dcLTS*icLTS
quQLTS= longTermSoil.quQ(q,D,Beff,Leff,V,H,0.0)
quQTeorLTS= q*NqLTS*sqLTS*dqLTS*iqLTS
quLTS= longTermSoil.qu(q,D,Beff,Leff,V,H,0.0,1.8)
quTeorLTS= quGammaTeorLTS+quCohesionTeorLTS+quQTeorLTS

err+= (abs(NgammaLTS-18)/18)**2
err+= (abs(NqLTS-18.5)/18.5)**2
err+= (abs(NcLTS-30)/30)**2
err+= (abs(dcLTS-1.11)/1.11)**2 #dc calculated as in reference [3]
                                #(see module documentation).
err+= (abs(scLTS-1.37)/1.37)**2 #sc calculated as in reference [3]
                                #(see module documentation).
err+= (abs(sgammaLTS-0.82)/0.82)**2 # sgamma calculated as in reference [3]
                                    # (see module documentation).
err+= (abs(iqLTS-0.87)/0.87)**2
err+= (abs(igammaLTS-0.80)/0.80)**2 #igamma calculated as in reference [3]
                                    #(see module documentation).
err+= (abs(icLTS-0.86)/0.86)**2
err+= (abs(quGammaLTS-quGammaTeorLTS)/quGammaTeorLTS)**2
err+= (abs(quCohesionLTS-quCohesionTeorLTS)/quCohesionTeorLTS)**2
err+= (abs(quQLTS-quQTeorLTS)/quQTeorLTS)**2
err+= (abs(quLTS-quTeorLTS)/quTeorLTS)**2

'''
print('NgammaSTS= ', NgammaSTS)
print('NqSTS= ', NqSTS)
print('NcSTS= ', NcSTS)
print('dcSTS= ', dcSTS)
print('scSTS= ', scSTS)
print('icSTS= ', icSTS)
print('quSTS=',quSTS)
print('NgammaLTS= ', NgammaLTS)
print('NqLTS= ', NqLTS)
print('NcLTS= ', NcLTS)
print('dcLTS= ', dcLTS)
print('scLTS= ', scLTS)
print('sgammaLTS= ', sgammaLTS)
print('iqLTS= ', iqLTS)
print('igammaLTS= ', igammaLTS)
print('icLTS= ', icLTS)
print('quGammaLTS=',quGammaLTS)
print('quGammaTeorLTS=',quGammaTeorLTS)
print('quCohesionLTS=',quCohesionLTS)
print('quCohesionTeorLTS=',quCohesionTeorLTS)
print('quQLTS=',quQLTS)
print('quQTeorLTS=',quQTeorLTS)
print('quLTS=',quLTS)
print('quTeorLTS=',quTeorLTS)
print('err= ', err)
'''

import os
from misc_utils import log_messages as lmsg
fname= os.path.basename(__file__)
if (err<0.0005):
    print('test: '+fname+': ok.')
else:
    lmsg.error('test: '+fname+' ERROR.')


# ********** ap27 6.24.2020 **********
# Granville will filter all outgoing requests
# based on keywords
# log the request and eventually mail a report
# based on https://docs.mitmproxy.org/stable/addons-scripting/
# ************************************

from mitmproxy import ctx
from mitmproxy import http
import logging
import os

# initiate logging
my_path = os.path.abspath(os.path.dirname(__file__))
alertlog = os.path.join(my_path, './logs/alert.log')
logging.basicConfig(filename=alertlog, format='%(asctime)s : %(message)s', level=logging.INFO)

# https://github.com/mitmproxy/mitmproxy/issues/237
# We don't want console to explode with flows, set max to 50 and code below clears flows
view = ctx.master.addons.get("view")
MAX_STORE_COUNT = 50

# filtering is done here - log and send artificial response back
# -> None means this function returns nothing (perhaps similar to void in Java)
def request(flow: http.HTTPFlow) -> None:

	# calls function below
	clearFlows()

	if "jenna" in flow.request.pretty_url:

		logging.info(str(flow.request.pretty_url))
		#flow.request.host = "0.0.0.0"
		#flow.kill()
		# This sends a reply from the proxy immediately
		flow.response = http.HTTPResponse.make(200,b"stop, not so fast yall...",{"Content-Type": "text/html"})

def clearFlows():

	# Use the below code to clear the flows every 50
	if view.store_count() >= MAX_STORE_COUNT:

		view.clear() # Same as hitting 'z' on console to clear


# Let Operation be some struct with fields http_method and data:
# struct Operation {
#   http_method: HTTPMethod,
#   Data: String
# }

# and HTTPMethod some enumeration of valid HTTP verbs:
# enum HTTPMethod {
#   Get,
#   Post,
#   Put,
#   Delete,
# }

# Write a function called process_in_order() that takes a vector of operations and returns a vector of results. The order of results should correspond to the order of the input operations (i.e. the first result element should be the response to the first operation), but the operations should be processed in a different order, specifically in order of the method as follows:

# 1)DELETE
# 2)POST
# 3)PUT
# 4)GET

# Assume you can call process_operation() on a single operation to get back a single result. You should not have to implement a sorting algorithm.

# For example, if I receive
# [{ GET, "a" }, { POST, "ab" }, { POST, "abb"} ,{ PUT, "abc" }]
# I want to process it in the order
# [{POST, "abb"}{ POST, "ab" }, { PUT, "abc" }, { GET, "a" }]

# Let’s say procees_operation() just gives you the length of the input string, then we’ll get
# process_operation({POST, "ab"}) => 2
# process_operation({PUT, "abc"}) => 3
# process_operation({GET, "a"}) => 1

# So we want process_in_order() to return
# [3, 1, 2]

"""
In the case of one big list:

[ ["DELETE", ... ], ["POST", ... ] ]

for array in arrays:
    unpack(array)

for unpacking:
    1) input: [1,2,3,4]
    2) output: 1, 2, 3, 4

"""

# Time complexity: O(N)
# Space complexity: O(N)

def process_in_order(operations: [tuple]) -> [int]:
    delete_list = []
    post_list = []
    put_list = []
    get_list = []
    positions = {}
    processed_operations = [None for i in range(len(operations))]

    for i, op in enumerate(operations):
        positions[op] = i

        if "DELETE" in op:
            delete_list.append(op)
        elif "POST" in op:
            post_list.append(op)
        elif "PUT" in op:
            put_list.append(op)
        else:
            get_list.append(op)

    for op_list in [delete_list, post_list, put_list, get_list]:
        for op in (op_list):
            processed_operations[positions[op]] = process_operation(op)

    return processed_operations

# For example, if I receive
# [{ GET, "a" }, { POST, "ab" }, { POST, "abb"} ,{ PUT, "abc" }]
# I want to process it in the order
# [{POST, "abb"}{ POST, "ab" }, { PUT, "abc" }, { GET, "a" }]

def process_operation(operation):
    return len(operation[1])

print(process_in_order([("GET", "a"), ("POST", "ab"), ("DELETE", "abba"), ("POST", "abb"), ("PUT", "abc")]))


"""
@author : Julie Bogoin
Master 2 BIB Paris Diderot - 2019-2020
Projet Long
"""

## Import des modules ##
import pandas

## Fonction ##

# Selectionner les séquences avec un score Kosak=3 et PholoP>=2
def select (fsATI_file):

    #Lecture de fsATI.csv
    df_fsATI = pandas.read_csv(fsATI_file)
    #Reset des indexs
    df_fsATI.reset_index(drop=True)
    #Tri
    df_fsATI = df_fsATI[df_fsATI['Kosak_strength']==3 & (df_fsATI['PhyloP_score']>=2)]

    return df_fsATI

### MAIN ##
df_validated = select('fsATI.csv')
print('\nSelection des fsATI réalisée.')

#Export vers csv
pandas.DataFrame.to_csv(df_validated, 'fsATI_validated.csv')
print('Fichier fsATI_validated.csv généré.\n')
print('Job done.\n')


import random
import string

reci = ['python', 'java', 'kotlin', 'javascript']
rec = random.choice(reci)
rec_l = list(rec)

st_in = len(rec_l) * '-'
lives = 8
pog = set()
prob = set()

print('H A N G M A N')
print()
print(st_in)
st_in = list(st_in)
pisi = len(rec_l) * '-'

while lives != 0:
    nn = len(pog)
    print('Input a letter: > ')
    n = input()

    if len(n) != 1:
        print('You should print a single letter')
        print()
        print(pisi)
        continue
    if not (n.isascii() and n.islower()):
        print('It is not an ASCII lowercase letter')
        print()
        print(pisi)
        continue

    if n in prob:
        print('You already typed this letter')

    for a in range(len(rec_l)):
        if n == rec_l[a]:
            pog.add(n)
        if rec_l[a] in pog:
            st_in[a] = rec_l[a]
    pisi = ''
    for b in range(len(rec_l)):
        pisi += st_in[b]
    if n not in pog and n not in prob:
        print('No such letter in the word')
        lives -= 1
        if lives == 0:
            break
    prob.add(n)
    if pisi == rec:
        print('You guessed the word!')
        print('You survived!')
        break
    print()
    print(pisi)

if lives == 0:
    print('You are hanged!')


import julia
import numpy as np

JULIA_UTILS_PATH = "/home/claudio/postdoc/Castle/castle/julia_utils.jl"

jl = julia.Julia(compiled_modules=False)
from julia import Main
Main.using("JuLIP: Chemistry.AtomicNumber, Atoms")
Main.using("ACE: rpi_basis")
Main.using(
    """IPFitting.Data: read_Atoms, read_energy,
     read_forces, read_virial, read_configtype""")
Main.using("IPFitting: Dat")
Main.include(JULIA_UTILS_PATH)

matrix_indices_in_voigt_notation = [
    (0, 0),
    (1, 1),
    (2, 2),
    (1, 2),
    (0, 2),
    (0, 1),
]

def frame_to_julia_at(frame, energy_name="energy",
                      force_name="force", virial_name="virial"):
    at = Main.read_Atoms(frame)
    E = Main.read_energy(frame, energy_name)
    F = Main.read_forces(frame, force_name)
    V = Main.read_virial(frame, virial_name)
    cf = Main.read_configtype(frame)
    if cf is None:
        cf = 'unknown'
    at_ = Main.Dat(at, cf, E=E, F=F, V=V)
    return at_

def descriptors_from_at(basis, at):
    X, X_std, dX_dr, dX_ds = Main.extract_info_frame(basis, at.at)
    return (X.T, X_std.T, np.transpose(np.array(dX_dr), (1, 2, 0)),
            np.transpose(np.array(dX_ds), (1, 2, 0)))

def get_basis(N, maxdeg, rcut, species, r0=1.0,
              rin=1.0, constants=False):
    basis = Main.rpi_basis(N=N, r0=r0,
                        maxdeg=maxdeg, rcut=rcut,
                        species=species,
                        rin=rin, constants=constants)
    return basis, Main.length(basis)

def descriptors_from_frame(basis, frame, species,
                           energy_name="energy",
                           force_name="force",
                           virial_name="virial"):

    at = frame_to_julia_at(frame, energy_name,
                           force_name, virial_name)
    X, X_std, dX_dr, dX_ds = descriptors_from_at(basis, at)
    dX_ds = dX_ds[matrix_indices_in_voigt_notation, :][:, 0, 0, :]
    X, dX_dr, dX_ds = add_onebody_term(frame, species, X, dX_dr, dX_ds)
    return X, X_std, dX_dr, dX_ds

def descriptors_from_frame_no_forces(basis, frame, species, energy_name="energy"):
    at = frame_to_julia_at(frame, energy_name)
    X, X_std = Main.sum_descriptor(basis, at.at)
    X, _, __ = add_onebody_term(frame, species, X)
    return X, X_std

def local_descriptors_from_frame(basis, frame, species,
                           energy_name="energy",
                           force_name="force",
                           virial_name="virial"):
    at = frame_to_julia_at(frame, energy_name,
                           force_name, virial_name)
    X = Main.environment_descriptor(basis, at.at)
    dX_dr, dX_ds = Main.environment_d_descriptor(basis, at.at)
    dX_ds = dX_ds[matrix_indices_in_voigt_notation, :][:, 0, 0, :]
    dX_dr = np.array(dX_dr)
    X, dX_dr, dX_ds = add_onebody_local_term(frame, species, X, dX_dr, dX_ds)
    return  X, dX_dr, dX_ds

def local_descriptors_from_frame_no_forces(basis, frame, species, energy_name="energy"):
    at = frame_to_julia_at(frame, energy_name)
    X = Main.environment_descriptor(basis, at.at)
    X, _, __ = add_onebody_local_term(frame, species, X)
    return X

def add_onebody_term(frame, species, X, dX_dr=None, dX_ds=None):
    at_ns = frame.get_atomic_numbers()
    unique, counts = np.unique(at_ns, return_counts=True)
    unique_counts = dict(zip(unique, counts))
    tally = np.zeros(len(species))
    for i, s in enumerate(species):
        # Fix to prevent error messages when not all declared species are present in a frame
        try:
            tally[i] = unique_counts[s]
        except KeyError:
            pass
    if dX_dr is not None:
        dX_dr = np.concatenate([dX_dr, np.zeros((dX_dr.shape[0], 3, len(tally)))], axis = -1)
    if dX_ds is not None:
        dX_ds = np.concatenate([dX_ds, np.zeros((6, len(tally)))], axis = -1)
    X = np.concatenate([X, tally])
    return X, dX_dr, dX_ds

def add_onebody_local_term(frame, species, X, dX_dr=None, dX_ds=None):
    at_ns = frame.get_atomic_numbers()
    tally = np.zeros((len(at_ns), len(species)))
    for i in np.arange(len(at_ns)):
        # Fix to prevent error messages when not all declared species are present in a frame
        try:
            tally[i, :] = (species == at_ns[i]).astype('float')
        except KeyError:
            pass
    X = np.concatenate([X, tally], axis = -1)
    if dX_dr is not None:
        dX_dr = np.concatenate([dX_dr, np.zeros((dX_dr.shape[0], dX_dr.shape[1], len(species), 3))], axis = -2)
    if dX_ds is not None:
        dX_ds = np.concatenate([dX_ds, np.zeros((6, len(species)))], axis = -1)
    return X, dX_dr, dX_ds


""":class:`domain_intel.awis.actions.TrafficHistory` unit test cases.

"""
import os
import json
import pytest

import domain_intel.awis.actions

def test_awis_traffichistory_init():
    """Initialise a domain_intel.awis.actions.TrafficHistory object.
    """
    # When I initialise an Alexa Web Information object
    awis = domain_intel.awis.actions.TrafficHistory()

    # I should get a domain_intel.awis.actions.TrafficHistory instance
    msg = 'Object not a domain_intel.awis.actions.TrafficHistory instance'
    assert isinstance(awis, domain_intel.awis.actions.TrafficHistory), msg

@pytest.mark.usefixtures('docker_compose', 'kafka_ready')
def test_add_domains(add_traffic_domains):
    """Load of TrafficHistory domains into Kafka topic.
    """
    msg = 'Load of TrafficHistory domains into Kafka error'
    assert add_traffic_domains == 100, msg

@pytest.mark.usefixtures('docker_compose', 'kafka_ready')
def test_parse_raw_traffichistory(parse_raw_traffichistory):
    """Parse raw Alexa TrafficHistory action as JSON.
    """
    msg = 'Re-load of raw TrafficHistory Alexa JSON into Kafka error'
    assert parse_raw_traffichistory == (20123, 100, 100), msg

def test_flatten_single_traffichistory_xml():
    """Flatten a single TrafficHistory XML response from Alexa AWIS.
    """
    # Given an Alexa AWIS TrafficHistory XML response
    with open(os.path.join('domain_intel',
                           'test',
                           'files',
                           'samples',
                           'single_traffichistory_result.xml')) as _fh:
        source_xml = _fh.read().rstrip()

    # when I flatten the XML
    awis = domain_intel.awis.actions.TrafficHistory()
    received = json.loads(awis.flatten_xml(source_xml))

    # then I should receive a list of JSON-encode domains
    msg = 'Flattened domain should produce JSON-encoded domain list'
    expected = '2017-06-01'
    assert received['TrafficHistory']['Start']['$'] == expected, msg

def test_flatten_empty_traffichistory_xml():
    """Flatten an empty TrafficHistory XML response from Alexa AWIS.
    """
    # Given an Alexa AWIS TrafficHistory XML response
    with open(os.path.join('domain_intel',
                           'test',
                           'files',
                           'samples',
                           'single_traffichistory_empty.xml')) as _fh:
        source_xml = _fh.read().rstrip()

    # when I flatten the XML
    awis = domain_intel.awis.actions.TrafficHistory()
    received = json.loads(awis.flatten_xml(source_xml))

    # then I should receive a list of JSON-encode domains
    msg = 'Flattened domain should produce JSON-encoded domain list'
    expected = '2017-07-01'
    assert received['TrafficHistory']['Start']['$'] == expected, msg

def test_flatten_errored_traffichistory_xml():
    """Flatten an errored TrafficHistory XML response from Alexa AWIS.
    """
    # Given an Alexa AWIS TrafficHistory XML response
    with open(os.path.join('domain_intel',
                           'test',
                           'files',
                           'samples',
                           'single_traffichistory_alexa_error.xml')) as _fh:
        source_xml = _fh.read().rstrip()

    # when I flatten the XML
    awis = domain_intel.awis.actions.TrafficHistory()
    received = awis.flatten_xml(source_xml)

    # then I should receive a list of JSON-encode domains
    msg = 'Flattened domain should produce JSON-encoded domain list'
    assert received is None, msg

@pytest.mark.usefixtures('docker_compose',
                         'kafka_ready',
                         'parse_raw_traffichistory')
def test_flatten_traffic(flatten_traffic):
    """Flatten Alexa TrafficHistory XML and publish.
    """
    msg = 'Flattening TrafficHistory Alexa XML back into Kafka error'
    assert flatten_traffic == (100, 100), msg

@pytest.mark.usefixtures('docker_compose',
                         'kafka_ready',
                         'parse_raw_urlinfo',
                         'flatten_domains',
                         'init_arango',
                         'persist_domains',
                         'parse_raw_traffichistory',
                         'flatten_traffic')
def test_persist(persist_traffichistory):
    """Persist TrafficHistory to the database.
    """
    # ... then I should receive a record count if domains stored
    msg = 'Count against the "traffic" collection incorrect'
    assert persist_traffichistory == (100, 100), msg


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from sparse_operation_kit import kit_lib
from tensorflow.distribute import MirroredStrategy
from tensorflow.distribute import get_replica_context
from tensorflow.distribute import has_strategy, get_strategy
from tensorflow.python.framework import sparse_tensor
from tensorflow.python.ops import check_ops
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import resource_variable_ops
import sys, os

CommToolSet = set(["Strategy", "MPI", "Horovod", "OneDevice"])
def get_global_replica_id(comm_tool=None):
    def _strategy():
        replica_ctx = get_replica_context()
        return replica_ctx.replica_id_in_sync_group

    def _MPI():
        return int(os.getenv("OMPI_COMM_WORLD_RANK"))

    def _Horovod():
        import horovod.tensorflow as hvd
        return hvd.local_rank()

    def _OneDevice():
        return 0

    if comm_tool is None:
        raise RuntimeError("SparseOperationKit can only works with "
                        "tf.distribute.Strategy, MPI, Horovod or single GPU.")

    if comm_tool not in CommToolSet:
        raise RuntimeError("SparseOperationKit only works with tf.distribute.Strategy, "
                        "MPI, Horovod or single GPU. But got %s" %comm_tool)

    if "Strategy" == comm_tool:
        return _strategy()
    elif "MPI" == comm_tool:
        return _MPI()
    elif "Horovod" == comm_tool:
        return _Horovod()
    elif "OneDevice" == comm_tool:
        return _OneDevice()

def _get_comm_tool():
    if "horovod.tensorflow" in sys.modules:
        return "Horovod"
    elif has_strategy():
        return "Strategy"
    elif os.getenv("OMPI_COMM_WORLD_SIZE") is not None:
        return "MPI"
    else:
        return "OneDevice"

def _get_embedding_variable_attr(embedding_variable, attr):
    if not isinstance(attr, str):
        raise ValueError("attr must be a string, but got {}".format(type(attr)))
    if hasattr(embedding_variable, "values"):
        return getattr(embedding_variable.values[0], attr)
    else:
        return getattr(embedding_variable, attr)

def embedding_lookup(embedding_variable, values, training=True, dynamic_input=False):
    """
    This function is a wrapper of SOK's dense forward propagation.
    """
    embedding_layer = embedding_variable.embedding_layer

    resource_variable_ops.variable_accessed(embedding_variable)

    comm_tool = _get_comm_tool()

    return kit_lib.plugin_dense_fprop(embedding_variable._handle,
                                      embedding_layer.handle,
                                      values=values,
                                      global_replica_id=get_global_replica_id(comm_tool),
                                      training=training,
                                      unique_op_name=embedding_variable.name,
                                      dynamic_input=dynamic_input,
                                      dtype=embedding_variable.dtype)

def embedding_lookup_sparse(embedding_variable, sp_ids, slot_num, training=True):
    """
    This function is a wrapper of SOK's sparse forward propagation.
    """
    if not isinstance(sp_ids, sparse_tensor.SparseTensor):
        raise TypeError("sp_ids must be SparseTensor")

    values = sp_ids.values
    indices = check_ops.ensure_shape(sp_ids.indices, shape=(None, 2))
    row_indices = array_ops.transpose(indices, perm=[1, 0])[0]

    embedding_layer = embedding_variable.embedding_layer

    resource_variable_ops.variable_accessed(embedding_variable)

    comm_tool = _get_comm_tool()

    return kit_lib.plugin_sparse_fprop(embedding_variable._handle,
                                       embedding_layer.handle,
                                       values, row_indices,
                                       get_global_replica_id(comm_tool),
                                       slot_num=slot_num,
                                       training=training,
                                       unique_op_name=embedding_variable.name,
                                       dtype=embedding_variable.dtype)


# -*- coding: utf-8 -*-
from encodings.utf_8 import encode
from bs4 import BeautifulSoup
import requests

def find_date(tag, classe, url):
    r = requests.get(url)
    soup = BeautifulSoup(r.text, "html.parser")
    arr = []
    if tag == 'div':
        for t_tag in soup.find_all(tag, classe):
            arr.append(t_tag.text)
    return arr

list =find_date('div',{'class':'fEDvV'},"https://www.tripadvisor.fr/Attraction_Review-g297683-d317329-Reviews-Taj_Mahal-Agra_Agra_District_Uttar_Pradesh.html")
print(list)


from selenium import webdriver
from selenium.webdriver import ActionChains

driver = webdriver.Chrome(executable_path=r"C:\Users\Manoj\Desktop\Python - Selenium Practice\Drivers\chromedriver.exe")

driver.get('http://www.dhtmlgoodies.com/scripts/drag-drop-custom/demo-drag-drop-3.html')

source_elem = driver.find_element_by_xpath('//*[@id="box6"]')
target_elem = driver.find_element_by_xpath('//*[@id="box106"]')

actions = ActionChains(driver)

actions.drag_and_drop(source_elem,target_elem).perform()


import mne
import numpy as np
import scipy.io
import matplotlib.pyplot as plt
import numpy as np
#设置通道名
biosemi_montage = mne.channels.make_standard_montage('biosemi32')
#生成数据

datafile1='D:\\Project\\paper4\\统计\\06Topography\\results\\hc_offsets.mat'
hc_offsets = scipy.io.loadmat(datafile1)
hc_offsets = hc_offsets .get('hc_offsets')
#创建info对象
info = mne.create_info(ch_names=biosemi_montage.ch_names, sfreq=250.,ch_types='eeg')
#创建evokeds对象 1
evoked1= mne.EvokedArray(hc_offsets, info)
#evokeds设置通道1
evoked1.set_montage(biosemi_montage)
print(evoked1.data)

datafile12='D:\\Project\\paper4\\统计\\06Topography\\results\\pdoff_offsets.mat'
pdoff_offsets = scipy.io.loadmat(datafile12)
pdoff_offsets = pdoff_offsets .get('pdoff_offsets')
#创建info对象
info = mne.create_info(ch_names=biosemi_montage.ch_names, sfreq=250.,ch_types='eeg')
#创建evokeds对象 2
evoked2= mne.EvokedArray(pdoff_offsets, info)
#evokeds设置通道2
evoked2.set_montage(biosemi_montage)
print(evoked2.data)

datafile13='D:\\Project\\paper4\\统计\\06Topography\\results\\pdon_offsets.mat'
pdon_offsets = scipy.io.loadmat(datafile13)
pdon_offsets = pdon_offsets .get('pdon_offsets')
#创建info对象
info = mne.create_info(ch_names=biosemi_montage.ch_names, sfreq=250.,ch_types='eeg')
#创建evokeds对象 3
evoked3= mne.EvokedArray(pdon_offsets, info)
#evokeds设置通道3
evoked3.set_montage(biosemi_montage)
print(evoked3.data)

#画图
fig, ax = plt.subplots(ncols=3, figsize=(8, 4), gridspec_kw=dict(top=0.9),dpi=300,
                       sharex=True, sharey=True)
#mne.viz.plot_topomap(evoked1.data[:, 0], evoked1.info, axes=ax[0], show=False, cmap='RdBu_r', contours='6', image_interp='bilinear', res=1200)

#mne.viz.plot_topomap(evoked2.data[:, 0], evoked2.info, axes=ax[1], show=False, cmap='RdBu_r', contours='6', res=1200)

# add titles
ax[0].set_xlabel('\n'+'HC')
ax[1].set_xlabel('\n'+'PD_OFF')
ax[2].set_xlabel('\n'+'PD_ON')

#将64通道的weight映射到脑电头皮地形中，xy /= 5.3是为了将头皮轮廓扩大到和地形图一样大

im, cn = mne.viz.plot_topomap(evoked1.data[:, 0], evoked1.info, axes=ax[0], show=False, cmap='RdBu_r', contours='6', image_interp='bilinear', res=300)

im1, cn = mne.viz.plot_topomap(evoked2.data[:, 0], evoked2.info, axes=ax[1], show=False, cmap='RdBu_r', contours='6', image_interp='bilinear', res=300)

im2, cn = mne.viz.plot_topomap(evoked3.data[:, 0], evoked3.info, axes=ax[2], show=False, cmap='RdBu_r', contours='6', image_interp='bilinear', res=300)

plt.colorbar(im, ax=[ax[0], ax[1], ax[2]], fraction=0.02, label="Offsets")

#plt.colorbar(im1, ax=ax[1])
plt.show()


class Solution:
    def removeDuplicates(self, nums: List[int]) -> int:

        #快慢指针
        #从第一个开始考虑
        #如果相同，快指针增加，
        #如果不同，快慢指针才同时增加

        if nums == []:
            return 0

        fast = 1
        slow = 1
        while fast < len(nums):
            if nums[fast] != nums[fast-1]:
                nums[slow] = nums[fast]
                slow += 1
            fast += 1

        return slow

26.删除有序数组中的重复项.py

26.删除有序数组中的重复项.py


from bisect import bisect_left

def find_pos(xs, query):
    # Invariant lo <= pos < hi
    lo, hi = 0, len(xs)

    while lo < hi:
        mid = (lo + hi) // 2
        if query < xs[mid]:
            hi = mid        # [lo, mid)
        elif query > xs[mid]:
            lo = mid + 1    # [mid + 1, hi)
        else:
            return mid

    return -1

def find_pos2(xs, query):
    lo = bisect_left(xs, query)
    # i < lo : xs[i] < query
    # i > lo : xs[i] >= query

    if lo < len(xs) and xs[lo] == query:
        return lo
    else:
        return -1

def test():
    assert find_pos([], 100) == -1
    assert find_pos([100], 100) == 0
    assert find_pos([100], 200) == -1
    assert find_pos([1, 4, 7, 9, 22, 77, 102, 555], 9) == 3

def test2():
    assert find_pos2([], 100) == -1
    assert find_pos2([100], 100) == 0
    assert find_pos2([100], 200) == -1
    assert find_pos2([1, 4, 7, 9, 22, 77, 102, 555], 9) == 3

if __name__ == "__main__":
    test()
    test2()


T = int(input())
for tc in range(1, T+1):
	N, M, C = map(int, input().split())
	honey = [list(map(int, input().split())) for _ in range(N)]
	on_line = True if 2*M <= N else False
	results = []
	tmp_dmax = 0
	if on_line:
		for i in range(N):
			for j in range(N - 2*M):
				tmpd = 0
				for m in range(2*M):
					tmpd += (min(C, honey[i][j+m]))**2
				if tmpd > tmp_dmax:
					tmp_dmax = tmpd
	results.append(tmp_dmax)
	tmp_max1 = honey[0][0]
	tmp_idx1 = [0, 0]
	for i in range(N):
		for j in range(N-M):
			tmp = 0
			for m in range(M):
				tmp += (min(C, honey[i][j + m])) ** 2
			if tmp > tmp_max1:
				tmp_max1 = tmp
				tmp_idx1 = [i, j]
	for m in range(M):
		honey[tmp_idx1[0]][tmp_idx1[1]+m] = -1

	tmp_max2 = honey[0][0]
	tmp_idx2 = [0, 0]
	for i in range(N):
		for j in range(N - M):
			tmp = 0
			for m in range(M):
				tmp += (min(C, honey[i][j + m])) ** 2
			if tmp > tmp_max2:
				tmp_max2 = tmp
				tmp_idx2 = [i, j]
	results.append(tmp_max1+tmp_max2)
	print(f'#{tc} {max(results)}')


import requests
import json

import eodnharvester.history as history
import eodnharvester.settings as settings
import eodnharvester.auth as auth

from eodnharvester.product import Product

class Entity(object):
    def __init__(self, **kwargs):
        self.entity_id = kwargs.get("entityId", None)

        self.populated = False
        self.log = history.Record()

        self.metadata = {}
        self.products = []

        self.metadata["acquisitionDate"] = kwargs.get("acquisitionDate", None)
        self.metadata["startTime"]       = kwargs.get("startTime", None)
        self.metadata["endTime"]         = kwargs.get("endTime", None)
        self.metadata["browseUrl"]       = kwargs.get("browseUrl", None)
        self.metadata["downloadUrl"]     = kwargs.get("downloadUrl", None)
        self.metadata["lowerLeftCoordinate"]  = kwargs.get("lowerLeftCoordinate", None)
        self.metadata["upperRightCoordinate"] = kwargs.get("upperRightCoordinate", None)
        self.metadata["upperLeftCoordinate"]  = kwargs.get("upperLeftCoordinate", None)
        self.metadata["lowerRightCoordinate"] = kwargs.get("lowerRightCoordinate", None)
        self.metadata["datasetName"] = kwargs.get("datasetName", settings.DATASET_NAME)
        self.metadata["node"]        = kwargs.get("node", settings.NODE)

    def GetProducts(self):
        logger = history.GetLogger()
        apiKey = auth.login(self.log)

        self.products = []
        response = ""
        url = "http://{usgs_host}/inventory/json/{request_code}"

        sceneRequest = {
            "datasetName": self.metadata["datasetName"],
            "apiKey":      apiKey,
            "node":        self.metadata["node"],
            "entityIds":   [self.entity_id]
        }
        scene_url = url.format(usgs_host    = settings.USGS_HOST,
                               request_code = "downloadoptions")

        try:
            logger.info("Getting information on {product} products".format(product = self.entity_id))
            logger.debug("{url}?jsonRequest={params}".format(url = scene_url, params = json.dumps(sceneRequest)))
            response = requests.get(scene_url, params = { 'jsonRequest': json.dumps(sceneRequest) }, timeout = settings.TIMEOUT)
            response = response.json()
            logger.debug(response)
        except requests.exceptions.RequestException as exp:
            error = "Failed to get scene metadata - {exp}".format(exp = exp)
            logger.error(error)
            self.log.error(self.entity_id, error)
            auth.logout(self.log)
            return False
        except ValueError as exp:
            error = "Error while decoding scene json - {exp}".format(exp = exp)
            logger.error(error)
            self.log.error(self.entity_id, error)
            auth.logout(self.log)
            return False
        except Exception as exp:
            error = "Unknown error while getting scene metadata - {exp}".format(exp = exp)
            logger.error(error)
            self.log.error(self.entity_id, error)
            auth.logout(self.log)
            return False

        for entity in response["data"][0]["downloadOptions"]:
            product = Product(self.entity_id, entity["downloadCode"], entity["filesize"], self.metadata)
            self.products.append(product)

        auth.logout(self.log)
        self.populated = True
        return True

    def __iter__(self):
        return _entity_iter(self)

class _entity_iter(object):
    def __init__(self, entity):
        self.entity = entity
        self.index = 0

    def __iter__(self):
        return self

    # Python2.x backwards compatibility
    def next(self):
        return self.__next__()

    def __next__(self):
        try:
            if not self.entity.populated:
                self.entity.GetProducts()

            product = self.entity.products[self.index]
            self.index += 1
        except Exception as exp:
            raise StopIteration()

        return product


import json
import imageio
import numpy as np

FILES = ["EasyStandard.dat"]
RATIO = 16  # length of a beat in BeatWalls units
SCALE = 0.05
THICKNESS = 0.1
SCROLL_DIST = 8
LETTER_SPACING = 2
DIV_WALL_WIDTH = 0.2
DIV_WALL_HEIGHT = 5

def load_font():
    alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ,'#1234567890-"
    font_img = imageio.imread("font.png")
    img_dim = font_img.shape[:2]

    # Separate letters
    letter_offsets = []
    letter_widths = []
    start = 0
    for i in range(1, img_dim[1]):
        if font_img[0, i, 2] > 100:
            if i > start:
                letter_offsets.append(start)
                letter_widths.append(i - start)
            start = i + 1
    assert len(letter_offsets) == len(alphabet)

    # Convert the letters to walls (1 pixel = 1 unit)
    font = {}
    for k in range(len(alphabet)):
        letter = alphabet[k]
        letter_img = font_img[
            :, letter_offsets[k]:letter_offsets[k] + letter_widths[k], :3]
        letter_matrix = (
            (letter_img[:, :, 0]
                + letter_img[:, :, 1]
                + letter_img[:, :, 2]) > 100)
        letter_walls = []  # list of x, y, w, h
        while True:
            h_lines = []
            v_lines = []
            # Horizontal lines
            for i in range(img_dim[0]):
                start_j = None
                for j in range(letter_widths[k]):
                    if letter_matrix[i, j] and start_j is None:
                        start_j = j
                    if not letter_matrix[i, j] and start_j is not None:
                        h_lines.append((j - start_j, i, start_j))
                        start_j = None
                if start_j is not None:
                    h_lines.append(
                        (letter_widths[k] - start_j, i, start_j))
            # Vertical lines
            for j in range(letter_widths[k]):
                start_i = None
                for i in range(img_dim[0]):
                    if letter_matrix[i, j] and start_i is None:
                        start_i = i
                    if not letter_matrix[i, j] and start_i is not None:
                        v_lines.append((i - start_i, start_i, j))
                        start_i = None
                if start_i is not None:
                    v_lines.append(
                        (img_dim[0] - start_i, start_i, j))
            # Choose the longest line, add it and clear the pixels
            h_lines.sort(key=lambda x: x[0], reverse=True)
            v_lines.sort(key=lambda x: x[0], reverse=True)
            if not h_lines and not v_lines:
                break
            elif not h_lines or v_lines[0][0] >= h_lines[0][0]:
                h, i, j = v_lines[0]
                letter_walls.append((j, img_dim[0] - i - h, 1, h))
                for r in range(i, i + h):
                    letter_matrix[r, j] = False
            else:
                w, i, j = h_lines[0]
                letter_walls.append((j, img_dim[0] - i - 1, w, 1))
                for c in range(j, j + w):
                    letter_matrix[i, c] = False
        font[letter] = (letter_widths[k], letter_walls)

    font[" "] = (5, [])

    return (img_dim[0], font)

def add_text(walls, font, text):
    text_width = sum(font[letter][0] for letter in text["content"])
    text_width += LETTER_SPACING * (len(text["content"]) - 1)

    offset = text["offset"] if "offset" in text else 0
    if text["type"] in {"ground", "air"}:
        offset -= text_width // 2

    for letter in text["content"]:
        for letter_wall in font[letter][1]:
            if text["type"] == "ground":
                pos = [SCALE * (letter_wall[0] + offset),
                        0,
                        text["time"] + SCALE * letter_wall[1] / RATIO]
                dim = [SCALE * letter_wall[2],
                        THICKNESS,
                        SCALE * letter_wall[3] / RATIO]
            elif text["type"] == "air":
                pos = [SCALE * (letter_wall[0] + offset),
                        SCALE * (letter_wall[1] + text["height"]),
                        text["time"]]
                dim = [SCALE * letter_wall[2],
                        SCALE * letter_wall[3],
                        THICKNESS / RATIO]
            elif text["type"] == "sidescroll":
                pos = [-SCROLL_DIST,
                        SCALE * (letter_wall[1] + text["height"]),
                        text["time"] + SCALE * (letter_wall[0] + offset) / RATIO]
                dim = [THICKNESS,
                        SCALE * letter_wall[3],
                        SCALE * letter_wall[2] / RATIO]
            walls.append({
                "_time": pos[2],
                "_lineIndex": 0,
                "_type": 0,
                "_duration": dim[2],
                "_width": 0,
                "_customData": {
                    "_position": pos[:2],
                    "_scale": dim[:2],
                    "_rotation": 90 if text["type"] == "sidescroll" else text["angle"] if "angle" in text else 0,
                    "_localRotation": [0, 0, 0],
                }
            })
        offset += font[letter][0] + LETTER_SPACING

def main():
    with open("text.json", encoding='raw_unicode_escape') as json_file:
        content_json = json.loads(
            json_file.read().encode('raw_unicode_escape').decode())

    _, font = load_font()

    walls = []
    notes = []

    for text in content_json["text"]:
        add_text(walls, font, text)

    for choice in content_json["choices"]:
        walls.append({
            "_time": choice["time"],
            "_lineIndex": 0,
            "_type": 0,
            "_duration": 2.1,
            "_width": 0,
            "_customData": {
                "_position": [-DIV_WALL_WIDTH / 2, 0],
                "_scale": [DIV_WALL_WIDTH, DIV_WALL_HEIGHT],
                "_rotation": 0,
                "_localRotation": [0, 0, 0],
            }
        })
        add_text(walls, font, {
            "type": "air",
            "content": "bravo",
            "time": choice["time"] + 2,
            "height": 60,
            "offset": -50 if choice["side"] == "left" else 50
        })
        add_text(walls, font, {
            "type": "air",
            "content": "wrong",
            "time": choice["time"] + 2,
            "height": 60,
            "offset": -50 if choice["side"] == "right" else 50
        })
        notes.append({
            "_time": choice["time"] + 2,
            "_lineIndex": 0 if choice["side"] == "left" else 3,
            "_lineLayer": 0,
            "_type": 1,
            "_cutDirection": 1
        })

    walls.sort(key=lambda x: x["_time"])

    for filename in FILES:
        with open(filename, "r") as json_file:
            song_json = json.load(json_file)

        song_json["_obstacles"] = walls
        song_json["_notes"] = notes

        with open(filename, "w") as json_file:
            json.dump(song_json, json_file)

main()


# -*- coding: utf-8 -*-
import unittest
from flask import Flask
from todos import app
from todos.models.Todo import Todo
from todos.database import db
import tempfile, os
import json

class TestTodos(unittest.TestCase):
    def setUp(self):
        self.db_fd,app.config['DATABASE'] = tempfile.mkstemp()
        app.config['TESTING'] = True
        self.app = app.test_client()
        db.drop_all()
        db.create_all()

        " Populate the db with some todos "
        todo1 = Todo(title='do stuff')
        todo2 = Todo(title='do more stuff')
        todo3 = Todo(title='do even more stuff')
        db.session.add(todo1)
        db.session.add(todo2)
        db.session.add(todo3)
        db.session.commit()

    def tearDown(self):
        os.close(self.db_fd)
        os.unlink(app.config['DATABASE'])

    def test_get(self):
        right_resp=dict()
        right_resp["todos"] = []
        right_resp["todos"].append({"done": False, "id": 1,"order": None, "title": "do stuff"})
        right_resp["todos"].append({"done": False, "id": 2,"order": None, "title": "do more stuff"})
        right_resp["todos"].append({"done": False, "id": 3,"order": None, "title": "do even more stuff"})

        response = self.app.get('/api/')
        response_dict = json.loads(response.data)

        self.assertEqual(right_resp,response_dict)

    def test_add_todo(self):
        data = dict(title="new todo item")
        response = self.app.post('/api/', data=json.dumps(data),
        content_type='application/json')
        response_dict = json.loads(response.data)

        right_resp=dict()
        right_resp['title'] = 'new todo item';
        right_resp['done'] =  False;
        right_resp['order'] = None;
        right_resp['id'] = 4;

        self.assertEqual(right_resp,response_dict)

    def test_update_todo(self):
        data = dict()
        data['title'] = 'new title';
        data['done'] =  False;
        data['order'] = 1;

        todo_testid = 1
        response = self.app.put('/api/'+str(todo_testid), data=json.dumps(data),
        content_type='application/json')
        response_dict = json.loads(response.data)

        self.assertEqual(dict(response='OK'),response_dict)

        todos = Todo()
        todo = todos.get(todo_testid)
        self.assertEqual(todo['title'],data['title'])
        self.assertEqual(todo['order'],data['order'])

if __name__ == '__main__':
    unittest.main()


def enter_move(board):
    ok = False
    while not ok:
        move = input('Enter your move: ')
        ok = len(move) == 1 and move >= '1' and move <= '9'
        if not ok:
            print('Not a correct move - repeat your move')
            continue
        move = int(move) - 1
        row = move // 3
        col = move % 3
        sign = board[row][col]
        ok = sign not in ['O', 'X']
        if not ok:
            print('Filed already occupied')
            continue
        board[row][col] = 'O'
board = [[3 * j + i + 1 for i in range(3)] for j in range(3)]
board[1][1] = 'X'
print(enter_move(board))