from odoo import api, fields, models

class MailMessage(models.Model):
    _inherit = 'mail.message'

    parent_author_name = fields.Char(compute="_compute_parent_author_name")
    parent_body = fields.Html(compute="_compute_parent_body")

    @api.depends('parent_id')
    def _compute_parent_author_name(self):
        for message in self:
            author = message.parent_id.author_id or message.parent_id.author_guest_id
            message.parent_author_name = author.name if author else False

    @api.depends('parent_id.body')
    def _compute_parent_body(self):
        for message in self:
            message.parent_body = message.parent_id.body if message.parent_id else False

    def _message_format(self, fnames, format_reply=True):
        """Override to remove email_from and to return the livechat username if applicable.
        A third param is added to the author_id tuple in this case to be able to differentiate it
        from the normal name in client code.

        In addition, if we are currently running a chatbot.script, we include the information about
        the chatbot.message related to this mail.message.
        This allows the frontend display to include the additional features
        (e.g: Show additional buttons with the available answers for this step). """

        vals_list = super()._message_format(fnames=fnames, format_reply=format_reply)
        for vals in vals_list:
            message_sudo = self.browse(vals['id']).sudo().with_prefetch(self.ids)
            discuss_channel = self.env['discuss.channel'].browse(message_sudo.res_id) if message_sudo.model == 'discuss.channel' else self.env['discuss.channel']
            if discuss_channel.channel_type == 'livechat':
                if message_sudo.author_id:
                    vals.pop('email_from')
                if message_sudo.author_id.user_livechat_username:
                    del vals['author']['name']
                    vals['author']['user_livechat_username'] = message_sudo.author_id.user_livechat_username
                # sudo: chatbot.script.step - checking whether the current message is from chatbot
                if discuss_channel.chatbot_current_step_id \
                        and message_sudo.author_id == discuss_channel.chatbot_current_step_id.sudo().chatbot_script_id.operator_partner_id:
                    chatbot_message_id = self.env['chatbot.message'].sudo().search([
                        ('mail_message_id', '=', message_sudo.id)], limit=1)
                    if chatbot_message_id.script_step_id:
                        vals['chatbotStep'] = {
                            'id': chatbot_message_id.script_step_id.id,
                            'answers': [] if chatbot_message_id.script_step_id.step_type != 'question_selection' else [{
                                'id': answer.id,
                                'label': answer.name,
                                'redirectLink': answer.redirect_link,
                            } for answer in chatbot_message_id.script_step_id.answer_ids],
                            'selectedAnswerId': chatbot_message_id.user_script_answer_id.id,

                        }
        return vals_list


""" Упражнение 1
Дополните приведенный код, используя списочный метод append(), чтобы список list1 имел вид:
list1 = [10, 20, [300, 400, [5000, 6000, 7000], 500], 30, 40]
"""
list1 = [10, 20, [300, 400, [5000, 6000], 500], 30, 40]
list1[2][2].append(7000)
print(list1)

""" Упражнение 2
Дополните приведенный код, используя списочный метод extend(), чтобы список list1 имел вид:
list1 = ['a', 'b', ['c', ['d', 'e', ['f', 'g', 'h', 'i', 'j'], 'k'], 'l'], 'm', 'n']
Подсписок для расширения  sub_list = ['h', 'i', 'j'].
"""
list1 = ['a', 'b', ['c', ['d', 'e', ['f', 'g'], 'k'], 'l'], 'm', 'n']
sub_list = ['h', 'i', 'j']
list1[2][1][2].extend(sub_list)
print(list1)

""" Упражнение 3
Дополните приведенный код, используя цикл for и встроенную функцию max(), чтобы он выводил один общий максимальный
элемент среди всех элементов вложенных списков list1.
"""
list1 = [[1, 7, 8], [9, 7, 102], [6, 106, 105], [100, 99, 98, 103], [1, 2, 3]]
maximum = -1
for i in list1:
    x = max(i)
    if maximum < x:
        maximum = x
print(maximum)

""" Упражнение 4
Дополните приведенный код так, чтобы список list1 имел вид:
list1 = [[8, 7, 1], [102, 7, 9], [105, 106, 102], [103, 98, 99, 100], [3, 2, 1]]
"""
list1 = [[1, 7, 8], [9, 7, 102], [102, 106, 105], [100, 99, 98, 103], [1, 2, 3]]
for i in range(len(list1)):
    list1[i].reverse()
print(list1)

""" Упражнение 5
Дополните приведенный код так, чтобы он выводил единственное число:
сумму всех чисел списка list1 разделённую на общее количество всех чисел.
"""
list1 = [[1, 7, 8], [9, 7, 102], [102, 106, 105], [100, 99, 98, 103], [1, 2, 3]]
total = 0
counter = 0
for i in list1:
    total += sum(i)
    counter += len(i)
print(total / counter)

3) Поколение Python - курс для продвинутых/04) Вложенные списки/4.2 Вложенные списки. Часть 1.py

4.2 Вложенные списки. Часть 1.py


import os

import chainer.functions as F
import librosa
import numpy as np

def crop_and_concat(h1, h2, concat=True):
    # s_freq = (h2.shape[2] - h1.shape[2]) // 2
    # e_freq = s_freq + h1.shape[2]
    s_time = (h2.shape[3] - h1.shape[3]) // 2
    e_time = s_time + h1.shape[3]
    h2 = h2[:, :, :, s_time:e_time]
    if concat:
        return F.concat([h1, h2])
    else:
        return h2

def calc_spec(X, hop_length, phase=False):
    n_fft = (hop_length - 1) * 2
    spec_left = librosa.stft(X[0], n_fft, hop_length=hop_length)
    spec_right = librosa.stft(X[1], n_fft, hop_length=hop_length)
    spec = np.asarray([spec_left, spec_right])

    if phase:
        mag = np.abs(spec)
        phase = np.exp(1.j * np.angle(spec))
        return mag, phase
    else:
        mag = np.abs(spec)
        return mag

def mask_uninformative(mask, ref, min_range=64, thres=0.4):
    fade_area = 32
    idx = np.where(ref.mean(axis=(0, 1)) < thres)[0]
    starts = np.insert(idx[np.where(np.diff(idx) != 1)[0] + 1], 0, idx[0])
    ends = np.append(idx[np.where(np.diff(idx) != 1)[0]], idx[-1])
    uninformative = np.where(ends - starts > min_range)[0]
    if len(uninformative) > 0:
        starts = starts[uninformative]
        ends = ends[uninformative]
        old_e = None
        for s, e in zip(starts, ends):
            if old_e is not None and s - old_e < fade_area:
                s = old_e - fade_area * 2
            elif s != 0:
                start_mask = mask[:, :, s:s + fade_area]
                np.clip(start_mask + np.linspace(0, 1, fade_area), 0, 1,
                        out=start_mask)
            if e != mask.shape[2]:
                end_mask = mask[:, :, e - fade_area:e]
                np.clip(end_mask + np.linspace(1, 0, fade_area), 0, 1,
                        out=end_mask)
            mask[:, :, s + fade_area:e - fade_area] = 1
            old_e = e
    return mask

def align_wave_head_and_tail(a, b, sr):
    a_mono = a[:, :sr * 2].sum(axis=0)
    b_mono = b[:, :sr * 2].sum(axis=0)
    a_mono -= a_mono.mean()
    b_mono -= b_mono.mean()
    offset = len(a_mono) - 1
    delay = np.argmax(np.correlate(a_mono, b_mono, 'full')) - offset

    if delay > 0:
        a = a[:, delay:]
    else:
        b = b[:, np.abs(delay):]
    if a.shape[1] < b.shape[1]:
        b = b[:, :a.shape[1]]
    else:
        a = a[:, :b.shape[1]]

    return a, b

def cache_or_load(mix_path, inst_path, sr, hop_length):
    _, ext = os.path.splitext(mix_path)
    spec_mix_path = mix_path.replace(ext, '.npy')
    spec_inst_path = inst_path.replace(ext, '.npy')

    if os.path.exists(spec_mix_path) and os.path.exists(spec_inst_path):
        X = np.load(spec_mix_path)
        y = np.load(spec_inst_path)
    else:
        X, _ = librosa.load(
            mix_path, sr, False, dtype=np.float32, res_type='kaiser_fast')
        y, _ = librosa.load(
            inst_path, sr, False, dtype=np.float32, res_type='kaiser_fast')
        X, _ = librosa.effects.trim(X)
        y, _ = librosa.effects.trim(y)
        X, y = align_wave_head_and_tail(X, y, sr)

        X = calc_spec(X, hop_length)
        y = calc_spec(y, hop_length)

        _, ext = os.path.splitext(mix_path)
        np.save(spec_mix_path, X)
        np.save(spec_inst_path, y)

    coeff = np.max([X.max(), y.max()])
    return X / coeff, y / coeff

def spec_to_wav(mag, phase, hop_length):
    spec = mag * phase
    wav_left = librosa.istft(spec[0], hop_length=hop_length)
    wav_right = librosa.istft(spec[1], hop_length=hop_length)
    wav = np.asarray([wav_left, wav_right])
    return wav


import os
import shutil
import time

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()

import torch
import torchvision

def train(train_loader, model, criterion, optimizer, epoch, args):
    batch_time = AverageMeter('Time', ':6.3f')
    data_time = AverageMeter('Data', ':6.3f')
    losses = AverageMeter('Loss', ':.4e')
    top1 = AverageMeter('Err@1', ':6.2f')
    top5 = AverageMeter('Err@5', ':6.2f')
    progress = ProgressMeter(len(train_loader), batch_time, data_time, losses, top1,
                             top5, prefix="Epoch: [{}]".format(epoch))

    # switch to train mode
    model.train()

    end = time.time()
    for i, (images, target) in enumerate(train_loader):
        # measure data loading time
        data_time.update(time.time() - end)

        if args.gpu is not None:
            images = images.cuda(args.gpu, non_blocking=True)
        # if args.gpu is None, the default GPU device is selected.
        target = target.cuda(args.gpu, non_blocking=True)

        # compute output
        output = model(images)
        loss = criterion(output, target)

        # measure accuracy and record loss
        acc1, acc5 = accuracy(output, target, topk=(1, 5))
        losses.update(loss.item(), images.size(0))
        top1.update(100.0-acc1.item(), images.size(0))
        top5.update(100.0-acc5.item(), images.size(0))

        # compute gradient and do updating step
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        # measure elapsed time
        batch_time.update(time.time() - end)
        end = time.time()

        if i % args.print_freq == 0:
            progress.print(i)
            plt.pause(.01)

    return top1.avg, top5.avg, losses.avg

def validate(val_loader, model, criterion, args):
    batch_time = AverageMeter('Time', ':6.3f')
    losses = AverageMeter('Loss', ':.4e')
    top1 = AverageMeter('Err@1', ':6.2f')
    top5 = AverageMeter('Err@5', ':6.2f')
    progress = ProgressMeter(len(val_loader), batch_time, losses, top1, top5,
                             prefix='Test: ')

    # switch to evaluate mode
    model.eval()

    with torch.no_grad():
        end = time.time()
        for i, (images, target) in enumerate(val_loader):
            if args.gpu is not None:
                images = images.cuda(args.gpu, non_blocking=True)
            target = target.cuda(args.gpu, non_blocking=True)

            # compute output
            output = model(images)
            loss = criterion(output, target)

            # measure accuracy and record loss
            acc1, acc5 = accuracy(output, target, topk=(1, 5))
            losses.update(loss.item(), images.size(0))
            top1.update(100.0-acc1.item(), images.size(0))
            top5.update(100.0-acc5.item(), images.size(0))

            # measure elapsed time
            batch_time.update(time.time() - end)
            end = time.time()

            if i % args.print_freq == 0:
                progress.print(i)
                plt.pause(.01)

        print(' *val* Err@1 {top1.avg:.3f} Err@5 {top5.avg:.3f}'
              .format(top1=top1, top5=top5))

    return top1.avg, top5.avg, losses.avg

def save_checkpoint(state, is_best, save_last, filename='checkpoint.pth.tar'):
    torch.save(state, filename)
    if is_best:
        shutil.copyfile(filename, os.path.join(os.path.dirname(filename),'model_best.pth.tar'))
    if save_last:
        last_filename = filename.replace('epoch'+str(state['epoch']), 'epoch'+str(state['epoch']-1))
        if os.path.isfile(last_filename):
            os.remove(last_filename)

class AverageMeter(object):
    """Computes and stores the average and current value"""
    def __init__(self, name, fmt=':f'):
        self.name = name
        self.fmt = fmt
        self.reset()

    def reset(self):
        self.val = 0
        self.avg = 0
        self.sum = 0
        self.count = 0

    def update(self, val, n=1):
        self.val = val
        self.sum += val * n
        self.count += n
        self.avg = self.sum / self.count

    def __str__(self):
        fmtstr = '{name} {val' + self.fmt + '} ({avg' + self.fmt + '})'
        return fmtstr.format(**self.__dict__)

class ProgressMeter(object):
    def __init__(self, num_batches, *meters, prefix=""):
        self.batch_fmtstr = self._get_batch_fmtstr(num_batches)
        self.meters = meters
        self.prefix = prefix

    def print(self, batch):
        entries = [self.prefix + self.batch_fmtstr.format(batch)]
        entries += [str(meter) for meter in self.meters]
        print('\t'.join(entries))

    def _get_batch_fmtstr(self, num_batches):
        num_digits = len(str(num_batches // 1))
        fmt = '{:' + str(num_digits) + 'd}'
        return '[' + fmt + '/' + fmt.format(num_batches) + ']'

def adjust_learning_rate(optimizer, epoch, args):
    """Sets the learning rate to the one specified by a user"""
    lr = args.lrs[epoch]
    for param_group in optimizer.param_groups:
        if 'lr_scale' in param_group.keys():
            param_group['lr'] = lr * param_group['lr_scale']
        else:
            param_group['lr'] = lr
    return lr

def accuracy(output, target, topk=(1,)):
    """Computes the accuracy over the k top predictions for the specified values of k"""
    with torch.no_grad():
        maxk = max(topk)
        batch_size = target.size(0)

        _, pred = output.topk(maxk, 1, True, True)
        pred = pred.t()
        correct = pred.eq(target.view(1, -1).expand_as(pred))

        res = []
        for k in topk:
            correct_k = correct[:k].view(-1).float().sum(0, keepdim=True)
            res.append(correct_k.mul_(100.0 / batch_size))
        return res

class ProgressPlotter(object):
    """Shows the learning status by graphs via matplotlib.pyplot"""
    def __init__(self, titles=('Err.'), legends=(('train','val')), ylims=((0,100)), yscales=None, vals=(([],[])), figsize=(9.6,4.8) ):
        # figure window
        self.fig = plt.figure(figsize=figsize)
        # number of subplot
        self.num = len(titles)
        # axies for subplots
        self.ax = [self.fig.add_subplot(1, self.num, i+1) for i in range(self.num)]
        # titles & legends for subplots
        for i in range(self.num):
            for j in range(len(legends[i])):
                self.ax[i].plot(range(1,1+len(vals[i][j])),vals[i][j])
            self.ax[i].legend(legends[i])
            self.ax[i].set_title(titles[i])
            self.ax[i].set_ylim(bottom=ylims[i][0], top=ylims[i][1])
            if yscales is not None:
                self.ax[i].set_yscale(yscales[i])

    def plot(self, vals=((0,0)) ):
        for i in range(self.num):
            # xymax = [0, 0]
            for j in range(len(vals[i])):
                bx, by = self.ax[i].lines[j].get_data()
                bx = np.append(bx, len(bx)+1)
                by = np.append(by, vals[i][j])
                self.ax[i].lines[j].set_data(bx, by)
                self.ax[i].set_xlim(left=0, right=max(bx))
                # xymax = np.max([xymax, [max(bx),max(by)]], axis=0)
            # self.ax[i].set_xlim(left=0, right=xymax[0])
            # self.ax[i].set_ylim(bottom=0, top=xymax[1])
        plt.pause(.01)

    def save(self, filename='plot.pdf' ):
        self.fig.savefig(filename)


from sklearn.cluster import KMeans
import numpy as np
from collections import Counter
import numpy.linalg as LA

def loadData(filepath, has_id, class_position):
    with open(filepath, encoding="latin1") as f:
        lines = (line.strip() for line in f)
        dataset = np.loadtxt(lines, delimiter=',', dtype=np.str, comments="#")
    if has_id:
        # Remove the first column (ID)
        dataset = np.delete(dataset, 0, axis=1)
    if class_position == 'first':
        classes = dataset[:, 0]
        dataset = np.delete(dataset, 0, axis=1)
        dataset = np.asarray(dataset, dtype=np.float)
    else:
        classes = dataset[:, -1]
        dataset = np.delete(dataset, -1, axis=1)
        dataset = np.asarray(dataset, dtype=np.float)
    return dataset.tolist(), classes.tolist()

dataset, classes = loadData(filepath="BERT/ATT_DPTC.txt", has_id=False, class_position="last")
clf = KMeans(n_clusters=len(set(classes)), n_init=20)
y_pred = clf.fit_predict(dataset).tolist()
print(y_pred)
print(dict(Counter(y_pred)))

from collections import defaultdict

d = defaultdict(list)
for k, va in [(v, i) for i, v in enumerate(y_pred)]:
    d[k].append(va)
d = dict(d)
print(d)

acc = 0
for i in np.arange(len(set(classes))):
    acc += Counter(np.array(classes)[d[i]]).most_common(1)[0][1]
print(acc / len(dataset))


import numpy as np
import paho.mqtt.client
import paho.mqtt.publish
import datetime
import json
import time
import ssl
import sys

def on_connect(cliente, userdata,flags,rc):
    print('conectando al publicador')

def main():
    client =paho.mqtt.client.Client("olla", False)
    client.qos=0

    client.connect(host='localhost')

    while True:

        tem_olla=round(np.random.uniform(0,150), 4)
        datehora= datetime.datetime.now()
        info={
            "id_instru":2,
            "tem_olla": tem_olla,
            "time":str(datehora)

        }
        client.publish('casa/cocina/olla', json.dumps(info), qos=0)
        print(info)
        time.sleep(1)

if __name__ == '__main__':
	main()
	sys.exit(0)


from pyqtgraph import RectROI

from pyqtgraph import QtCore
from pyqtgraph import QtGui
from pyqtgraph import LayoutWidget

from .utils import delete_content
from .utils import compute_bbox_of_points

from .color import JChooseColor
from .color import setup_color

from .remove_item import JRemoveItem

class Jrectangle(RectROI, JChooseColor, JRemoveItem):

    def __init__(self, pos, size, viewbox=None):
        RectROI.__init__(self, pos, size)

        JChooseColor.__init__(self)
        self.set_black_color()
        JRemoveItem.__init__(self, viewbox)

        self.info_dock = viewbox.info_dock

        self._menu = self._build_menu()
        self._display_info_dock()

        for h in self.handles:
            handle = h["item"]
            handle.currentPen.setColor(QtGui.QColor(0, 0, 0))

    @classmethod
    def load(cls, s, viewbox=None):
        if "*JRectangle" not in s:
            print("Error reading a rectangle from a string %s" % s)

        s = s.replace("*JRectangle", "")

        if s[0] != "{" or s[-1] != "}":
            print("Error the string is in the wrong format")

        data = eval(s)
        rectangle = cls(data["pos"], data["size"], viewbox=viewbox)
        setup_color(rectangle, data["color"])

        if viewbox is not None:
            viewbox.label.setText("Rectangle loaded.")

        return rectangle

    def get_save_control_points(self):
        return self.get_drawing_points()

    def save(self, file, pos=None, size=None):

        if pos is None and size is None:
            pos = [self.pos().x(), self.pos().y()]
            size = [self.size().x(), self.size().y()]

        data = {
            "color": self.color,
            "pos": pos,
            "size": size
        }

        file.write("*JRectangle\n")
        file.write(str(data) + "\n")

    def _build_menu(self):
        menu = QtGui.QMenu()
        menu.setTitle("Rectangle")
        menu.addAction("Remove", self.remove_item)
        return menu

    def mouseClickEvent(self, ev):
        self._display_info_dock()
        if ev.button() == QtCore.Qt.RightButton:
            self._raise_menu(ev)

    def get_drawing_points(self):
        low = [self.pos().x(), self.pos().y()]
        disp = [self.size().x(), self.size().y()]

        low_x = low[0]
        low_y = low[1]
        dx = disp[0]
        dy = disp[1]

        pts = [low, [low_x, low_y + dy], [low_x + dx, low_y + dy],
               [low_x + dx, low_y], low]
        return pts

    def compute_bbox(self):
        return compute_bbox_of_points(self.get_drawing_points())

    def paint(self, p, *args):

        p.setRenderHint(QtGui.QPainter.Antialiasing)
        p.setPen(self.currentPen)
        super().paint(p, *args)

    def _raise_menu(self, event):
        pos = event.screenPos()
        self._menu.popup(QtCore.QPoint(pos.x(), pos.y()))

    def _display_info_dock(self):
        if self.info_dock is None:
            return

        delete_content(self.info_dock)

        container = LayoutWidget()
        label1 = QtGui.QLabel("Rectangle")
        container.addWidget(label1, row=0, col=0)

        color_dock_widget = self.get_color_dock_widget()
        container.addWidget(color_dock_widget, row=1, col=0)

        remove_item_widget = self.get_remove_item_dock_widget()
        container.addWidget(remove_item_widget, row=2, col=0)

        vertical_spacer = QtGui.QSpacerItem(1, 1, QtGui.QSizePolicy.Minimum,
                                            QtGui.QSizePolicy.Expanding)
        container.layout.addItem(vertical_spacer, 3, 0)

        self.info_dock.addWidget(container)


import bs4
from bs4 import BeautifulSoup
import requests

# this url does not work due to react
# URL = "https://www.empireonline.com/movies/features/best-movies-2/"
URL = "http://web.archive.org/web/20200322005914/https://www.empireonline.com/movies/features/best-movies-2/"
INPUT_FILENAME = "movies.html"
OUTPUT_FILENAME = "movies.txt"

def get_webpage(url):
    #
    #   get webpage as text
    #
    response = requests.get(url)
    webpage = response.text
    return webpage

def save_webpage(filename, webpage):
    #
    #   save webpage to file
    #
    with open(filename, "w") as fp:
        fp.write(webpage)

def load_webpage(filename):
    #
    #   load webpage from file
    #
    with open(filename) as fp:
        webpage = fp.read()
        return webpage

try:
    #
    #   if file exist, already downloaded, goto else:
    #
    with open(INPUT_FILENAME) as fp:
        pass
except FileNotFoundError:
    print(f"{INPUT_FILENAME} does not exists")
    print(f"downloading {URL}...")
    webpage = get_webpage(URL)
    print(f"saving {URL} to {INPUT_FILENAME}...")
    save_webpage(INPUT_FILENAME, webpage)
else:
    print(f"{INPUT_FILENAME} exists")
    print(f"loading {URL} from {INPUT_FILENAME}...")
    webpage = load_webpage(INPUT_FILENAME)
finally:
    pass
    # print(webpage)

soup = BeautifulSoup(webpage, "html.parser")

h3s = soup.find_all(name="h3", class_="title")

h3s_inverted = h3s[::-1]
#
# or
#
h3s.reverse()
h3: bs4.Tag
movies = [h3.getText() for h3 in h3s_inverted]

print(f"Creating {OUTPUT_FILENAME}")
with open(OUTPUT_FILENAME, "w", encoding="utf-8") as fp:
    fp.write("\n".join(movies))


# -*- coding: utf-8 -*-
#assignment5_functions.py
#Introduction to Bioinformatics
#Python Assignment 5
#Purpose: putting together the functions I'll use in the other programs
#Your Name: Diane Kaplan
#Date: December 5, 2014

#Function takes a sequence and returns the reverse complement
def reverseComplement(seq):
    given_seq = seq
    seq_backward = seq[::-1]
    base_partners = {"A":"T", "C":"G", "G":"C", "T":"A"}
    reverse_complement = ""

    #Loop through and look up the complement to create the new sequence
    for char in seq_backward:
        reverse_complement += base_partners[char]

    return reverse_complement

#Function takes a sequence and length N and returns a list of Nmers from that sequence
def make_Nmers_list(seq, n):
    thisSeq = seq
    seqLen = len(thisSeq)
    Nmer_list = []
    cursor = 0

    #loop through our sequence, saving Nmers to a list
    while cursor < seqLen-(n-1):
        thisChunk = thisSeq[cursor:cursor+(n)]
        cursor += 1
        Nmer_list.append(thisChunk)

    return Nmer_list


# search.py
# ---------------
# Licensing Information:  You are free to use or extend this projects for
# educational purposes provided that (1) you do not distribute or publish
# solutions, (2) you retain this notice, and (3) you provide clear
# attribution to the University of Illinois at Urbana-Champaign
#
# Created by Jongdeog Lee (jlee700@illinois.edu) on 09/12/2018

"""
This file contains search functions.
"""
# Search should return the path and the number of states explored.
# The path should be a list of tuples in the form (alpha, beta, gamma) that correspond
# to the positions of the path taken by your search algorithm.
# Number of states explored should be a number.
# maze is a Maze object based on the maze from the file specified by input filename
# searchMethod is the search method specified by --method flag (bfs)
# You may need to slight change your previous search functions in MP1 since this is 3-d maze

from collections import deque
from heapq import heappop, heappush

def search(maze, searchMethod):
    return {
        "bfs": bfs,
    }.get(searchMethod, [])(maze)

def bfs(maze, ispart1=False):
    # Write your code here
    """
    This function returns optimal path in a list, which contains start and objective.
    If no path found, return None.

    Args:
        maze: Maze instance from maze.py
        ispart1: pass this variable when you use functions such as getNeighbors and isObjective. DO NOT MODIFY THIS
    """

    path = []
    start = maze.getStart()
    curr = start
    prev = {}
    visited = set()
    visited.add(curr)
    foundsoln = False

    if (maze.isObjective(curr[0], curr[1], curr[2], ispart1)):
        foundsoln = True
        path.append(curr)
        return path

    queue = []
    queue.append(curr)

    while (queue):
        curr = queue.pop(0)
        if (maze.isObjective(curr[0], curr[1], curr[2], ispart1)):
            foundsoln = True
            break

        curr_neighbors = maze.getNeighbors(curr[0], curr[1], curr[2], ispart1)

        for neighbor in curr_neighbors:
            if (neighbor not in visited) and (neighbor not in queue):
                prev[neighbor] = curr
                queue.append(neighbor)
                visited.add(neighbor)

    if (foundsoln):
        while (curr != start):
            path.append(curr)
            curr = prev[curr]
        path.append(curr)
        path.reverse()
        return path

    return None


import itertools
from abc import ABC, abstractmethod
from collections import defaultdict
from typing import DefaultDict, List, Mapping, Optional, TYPE_CHECKING

import numpy as np

from .context import Context
from .telemetry import logger
from .types import AgentID
from .message import Message

if TYPE_CHECKING:
    from .network import Network

class Resolver(ABC):
    """Network message resolver.

    This type is responsible for resolution processing. That is, the order in which
    (and any special logic therein) messages are handled in a Network.

    In many cases, this type can be arbitrary since the sequence doesn't matter (i.e.
    the problem is not path dependent). In other cases, however, this is not the case;
    e.g. processing incoming market orders in an LOB.

    Implementations of this class must provide implementations of the abstract methods
    below.

    Arguments:
        enable_tracking: If True, the resolver will save all messages in a time-ordered
            list that can be accessed with :attr:`tracked_messages`.
    """

    def __init__(self, enable_tracking: bool = False) -> None:
        self.enable_tracking = enable_tracking
        self._tracked_messages: List[Message] = []

    def push(self, message: Message) -> None:
        """Called by the Network to add messages to the resolver."""
        if self.enable_tracking:
            self._tracked_messages.append(message)

        logger.log_msg_send(message)

        self.handle_push(message)

    def clear_tracked_messages(self) -> None:
        """Clears any stored messages.

        Useful for when incrementally processing/storing batches of tracked messages.
        """
        self._tracked_messages.clear()

    @property
    def tracked_messages(self) -> List[Message]:
        """
        Returns all messages that have passed through the resolver if tracking is enabled.
        """
        return self._tracked_messages

    @abstractmethod
    def handle_push(self, message: Message) -> None:
        """
        Called by the resolver to handle batches of messages. Any further created
        messages (e.g. responses from agents) must be handled by being passed to the
        `push` method (not `handle_push`).
        """
        raise NotImplementedError

    @abstractmethod
    def resolve(self, network: "Network", contexts: Mapping[AgentID, Context]) -> None:
        """Process queues messages for a (sub) set of network contexts.

        Arguments:
            network: An instance of the Network class to resolve.
            contexts: The contexts for all agents for the current step.
        """
        raise NotImplementedError

    @abstractmethod
    def reset(self) -> None:
        """Resets the resolver and clears any potential message queues.

        Note:
            Does not clear any tracked messages.
        """
        raise NotImplementedError

class BatchResolver(Resolver):
    """
    Resolver that handles messages in multiple discrete rounds. Each round, all agents
    have the opportunity to respond to previously received messages with new messages.
    These messages are held in a queue until all agents have been processed before being
    consumed in the next round. Messages for each agent are delivered in a batch,
    allowing the recipient agent to decide how to handle the messages within the batch.

    Arguments:
        enable_tracking: If True, the resolver will save all messages in a time-ordered
            list that can be accessed with :attr:`tracked_messages`.
        round_limit: The maximum number of rounds of messages to resolve. If the limit
            is reached an exception will be thrown. By default the resolver will keep
            resolving until no more messages are sent.
        shuffle_batches: If True, the order in which messages for a particular
            recipient are sent to the recipient will be randomised.
    """

    def __init__(
        self,
        enable_tracking: bool = False,
        round_limit: Optional[int] = None,
        shuffle_batches: bool = False,
    ) -> None:
        super().__init__(enable_tracking)

        self.round_limit = round_limit
        self.shuffle_batches = shuffle_batches

        self.messages: DefaultDict[AgentID, List[Message]] = defaultdict(list)

    def reset(self) -> None:
        self.messages.clear()

    def handle_push(self, message: Message) -> None:
        self.messages[message.receiver_id].append(message)

    def resolve(self, network: "Network", contexts: Mapping[AgentID, Context]) -> None:
        iterator = (
            itertools.count() if self.round_limit is None else range(self.round_limit)
        )

        for i in iterator:
            if len(self.messages) == 0:
                break

            logger.log_resolver_round(i, self.round_limit)

            processing_messages = self.messages
            self.messages = defaultdict(list)

            for receiver_id, messages in processing_messages.items():
                if receiver_id not in contexts:
                    continue

                msgs = [
                    m for m in messages if network.has_edge(m.sender_id, m.receiver_id)
                ]

                if self.shuffle_batches:
                    np.random.shuffle(msgs)

                ctx = contexts[receiver_id]
                responses = ctx.agent.handle_batch(ctx, msgs)

                if responses is not None:
                    for sub_receiver_id, sub_payload in responses:
                        network.send(receiver_id, sub_receiver_id, sub_payload)

        if len(self.messages) > 0:
            raise RuntimeError(
                f"{len(self.messages)} message(s) still in queue after BatchResolver round limit reached."
            )


# -*- coding: utf-8 -*-
import scrapy
from collectips.items import CollectipsItem

class GetipsSpider(scrapy.Spider):
    name = "getips"
    allowed_domains = ["xicidaili.com"]
    start_urls = ['http://www.xicidaili.com']

    #获取多个网址的响应
    def start_requests(self):
        reqs = []
        for i in range(1, 206):
             req= scrapy.Request("http://www.xicidaili.com/nn/%s" % i)
        reqs.append(req)
        return reqs

    def parse(self, response):

        ip_list = response.xpath('//table[@id="ip_list"]/tr[@class="odd"]')

        for ip in ip_list:

            pre_item = CollectipsItem()
            pre_item['ip']=ip.xpath('td[2]/text()').extract()[0]
            pre_item['port'] = ip.xpath('td[3]/text()').extract()[0]
            pre_item['type'] = ip.xpath('td[6]/text()').extract()[0]
            pre_item['position'] = ip.xpath('td[4]/a/text()').extract()[0]
            pre_item['speed'] = ip.xpath('td[7]/div/@title').re('\d{0,2}\.\d{0,}')[0]

            yield pre_item


string = input()
#string = string.lower()

def longest_palindrome(text):
    longest = ""
    for i in range(len(text)):
        for j in range(0, i):
            chunk = text[j:i + 1]
            if chunk == chunk[::-1]:
                if len(chunk) > len(longest):
                    longest = chunk
    return longest

string2 = longest_palindrome(string)

if string2 == '':
    print(string[0])
#elif string == string2:
#    print(string[1:-1])
else:
    print(string2)

1297 Палиндромы.py

1297 Палиндромы.py


import gym
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

from sys import path; path.append("..")
from analyze_random import seaborn_heatmap

env = gym.make('CartPole-v0')

def play_random(viz=True):
    obs = [env.reset()]
    total_reward = 0
    terminal = False

    while not terminal:
        if viz: env.render()
        action = env.action_space.sample()
        observation, reward, terminal, info = env.step(action)
        obs.append(observation)
        total_reward += reward

    return obs, total_reward

def eval_performance(agent, epochs, frames=250, viz=False):

    rewards = []
    for ep in range(epochs):
        obs = [env.reset()]
        total_reward = 0
        terminal = False
        ep_acts = []

        for f in range(frames):
            if ep % (epochs//5) == 0:
                if viz: env.render()
            action = agent.act(obs[-1], time_t=f)
            observation, reward, terminal, info = env.step(action)
            obs.append(observation)
            total_reward += reward
            ep_acts.append(action)

            reward = total_reward

            if reward < 20: # if less than random average
                reward = 0

            if not terminal:
                agent.update_policy(action, obs[-2], obs[-1], reward)
            else:
                reward = 0
                agent.update_policy(action, obs[-2], obs[-1], reward, ep_acts)
                break

        if ep % (epochs//5) == 0:
            print("Terminal ep:", ep,
                      "\tep_rwd:", total_reward,
                      f"\tlr:{round(agent.lr, 2)}",
                      "\tP(r_act):", round(agent.rand_act_prob, 2))

        rewards.append(total_reward)

    df = pd.DataFrame({'epoch':np.arange(epochs), 'reward':rewards})
    df['max_reward'] = df[['reward']].rolling(100).max()
    df['mean_reward'] = df[['reward']].rolling(25).mean()
    df['min_reward'] = df[['reward']].rolling(100).min()

    df = df.set_index('epoch')
    print(df)
    df.plot()

    plt.ylabel('Episode Reward')
    plt.show()

    # Plot observation axis for Q table
    ax = 0
    q_axis = agent.Q[ax]
    print('q axis', q_axis)
    q_len = len(q_axis)
    q_pivot = {}
    q_pivot['q_value'] = np.concatenate([q_axis[:, 0], q_axis[:, 1]])
    q_pivot[f'obs_{ax}'] = np.concatenate([np.arange(q_len),
                                                                        np.arange(q_len)])
    q_pivot['action'] = np.concatenate([np.zeros(q_len),
                                                                 np.ones(q_len)])
    q_pivot = pd.DataFrame(q_pivot)
    seaborn_heatmap(q_pivot, xyz=('action', f'obs_{ax}', 'q_value'))

    return df

class q_learning():
    def __init__(self, env):
        # env.observation_space.high
        # --> [max(val) for val in obs_space]
        # env.observation_space.low
        # --> [min(val) for val in obs_space]

        # model parameters
        digitize_max = 75 #800 # digitizer maps obs to rng(0, digi_max)
        self.gamma = 6.9e-1 # discounted future reward

        self.decay = 1 - (2e-5)#1 - (1e-5) # decay factor for below params
        self.rand_act_prob = 1 # 1 # Initial P(random action)
        self.act_prob_min = 0.001 #0.08 #0.15
        self.lr = 2e-2 # 1e-1 # Initial learning rate
        self.lr_min = 0.5e-2 #.5e-2
        # env options
        obs_max = env.observation_space.high
        obs_min = env.observation_space.low
        self.act_len = env.action_space.n
        obs_len = len(obs_min)
        init_limits = [1, 1]

        self.best_acts = []

        # Determine best num_bins for environment
        # num_bins  -->  Divisor on X in sigmoid_array
        eval_fx = lambda X, n_b: self.sigmoid_array(X, n_b, digitize_max)
        num_bins = 2
        '''
        for n_b in range(1, 100, 1):
            if min(eval_fx(obs_max, n_b)) == digitize_max:
                num_bins = n_b
                break
        '''
        print("Determined num bins:", num_bins)

        # Digitizer converts array of observations into discrete obs
        fx = lambda X: self.sigmoid_array(X, num_bins, digitize_max)
        self.digitize = fx

        print("Discrete window:\n\tmax:", fx(obs_max))
        print("\tmin:", fx(obs_min))

        # Q table size is (n, m, k)
        # where: (n=obs_len, m=digi_max, k=act_len)
        #      := tensor(obs_len, [digi_max, act_len])
        q_table_size = (obs_len, digitize_max + 1, self.act_len)
        print("Q_table shape:", q_table_size)
        #self.Q = np.random.uniform(init_limits[0], init_limits[1],
        #                          size=(q_table_size))
        self.Q = np.ones((q_table_size))
        print("\tNumber of values:", len(self.Q.flatten()))

    def sigmoid_array(self, X, num_bins = 20, upper_lim = 10):
        X = upper_lim / (1 + np.exp( -X /num_bins))
        X = tuple(map(lambda x: int(x), X))
        return X

    def get_action_q_values(self, obs):
        acts = np.zeros((self.act_len))
        obs = self.digitize(obs)
        for idx, q_table in enumerate(self.Q):
            for a in range(self.act_len):
                # Normalize contribution for each dim in obs
                acts[a] += self.Q[idx][obs[idx]][a] / len(self.Q)
        return acts

    def set_action_q_values(self, obs, action, q_updated):
        obs = self.digitize(obs)
        for idx in range(len(self.Q)):
            self.Q[idx][obs[idx]][action] = q_updated
            #acts = self.Q[idx][obs[idx]]
            # Normalize q_values to one
            #self.Q[idx][obs[idx]]= acts / sum(acts)

    def act(self, obs, time_t=0):

        if np.random.random() < self.rand_act_prob:
            '''
            if len(self.best_acts) > 100: # better than random
                if time_t > len(self.best_acts)/3:
                    if time_t < (2*len(self.best_acts))/3: # only use some of best acts
                        return self.best_acts[time_t] #np.argmax(self.get_action_q_values(obs))
            '''
            return np.random.randint(self.act_len)

        return np.argmax(self.get_action_q_values(obs))

    def update_policy(self, action, state, next_state, reward, ep_acts=[]):
        next_reward = np.max(self.get_action_q_values(next_state))
        state_value = self.get_action_q_values(next_state)[action]

        q_updated = self.lr * (reward + (self.gamma * next_reward))
        q_updated += state_value * (1 - self.lr)

        self.set_action_q_values(state, action, q_updated)

        if (2*len(ep_acts))/3 > len(self.best_acts)/2:
            self.best_acts = ep_acts

        if self.rand_act_prob > self.act_prob_min:
            self.rand_act_prob *= self.decay
        if self.lr > self.lr_min:
            self.lr *= self.decay

if __name__ == "__main__":
    epochs = 1000 #500 * 1000
    agent = q_learning(env)
    df = eval_performance(agent, epochs, viz=False)

    # Mean reward is 20.5 from q_learning after init, no training, 100ep


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

import pickle

longest_cont = []
with open('neko.txt.mecab.pickle', 'rb') as f:
    article = pickle.load(f)
    for sent in article:
        n_cont = []
        for i in range(len(sent)):
            if sent[i]['pos'] == '名詞':
                n_cont.append(sent[i]['surface'])
            else:
                if len(n_cont) > len(longest_cont):
                    longest_cont = n_cont
                n_cont = []

print(longest_cont)


import pygame as pg
from pygame.math import Vector2 as vector
from Code.forHelp.autoload import *
from Code.forHelp.helpers import *

class Block(NewSprite):
    def __init__(self, start_pos):
        super().__init__()
        all_sprites.add(self)
        collision_sprites.add(self)

        self.scale = 2
        self.image = pg.Surface((32 * self.scale,32 * self.scale))
        self.image.fill((0,0,0))
        self.rect = self.image.get_rect(topleft = start_pos)
        self.sprite_img = self.image
        self.offsprite_pos = vector(0,0)


def largest_seq(seq):
    nums_map = {n: 0 for n in seq}
    left, right = 0, 0

    for n in seq:
        if nums_map[n] == 0:
            left_ = n - 1
            right_ = n + 1
            while left_ in nums_map:
                nums_map[left_] = 1
                left_ -= 1
            left_ += 1
            while right_ in nums_map:
                nums_map[right_] = 1
                right_ += 1
            right_ -= 1

            if (right - left) <= (right_ - left_):  # if the range/size of the previous sub array is small
                right, left = right_, left_    # then replace it by the current one to get the largest sequence

    return [left, right]  # the smallest num and the largest num in the sequential numbers

print(largest_seq([10, 24, 5, 9, 1, 0, 3, 7, 6, 2, 20, 8, 4, 21, 25, 23, 22]))
print(largest_seq([10, 24, 5, 1, 0, 2, 20, 8, 4, 21, 25, 23, 22]))


# Licensed under a 3-clause BSD style license - see LICENSE.rst

import importlib
import itertools
import inspect
import numpy as np

from scipy.sparse import csr_matrix
from sklearn.neighbors import BallTree
import sofia_redux.toolkit.resampling.tree as tree_module
from sofia_redux.toolkit.utilities.func import byte_size_of_object

__all__ = ['BaseTree']

class BaseTree(object):

    def __init__(self, argument, shape=None, build_type='all', leaf_size=40,
                 large_data=False, **distance_kwargs):
        r"""
        Create a tree structure for use with the resampling algorithm.

        The resampling tree is primarily responsible for deriving and
        storing all independent variables necessary for polynomial fitting,
        as well as allowing fast access to those variables that belong to
        coordinates within a certain radius of a given point.

        TREE STRUCTURE AND ACCESS

        The tree itself is divided into N-dimensional blocks, each of which
        is allocated a set of coordinates.  The width of these blocks should
        correspond to the `window` (:math:`\Omega`) defined in the resampling
        algorithm, and coordinates should be scaled accordingly.  For example,
        if the window radius is set to :math:`\Omega=4` in (arbitrary) units
        for the purposes of resampling 1-dimensional data, and the independent
        values are:

            x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

        They should be supplied to the tree as :math:`x^\prime = x / \Omega`.

        .. math::

            x^\prime = \frac{x}{\Omega} =
                [0.25, 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2, 2.25, 2.5]

        The tree defines blocks by grouping all coordinates with the same
        floored values into a single block.  Therefore, in this case the tree
        will contain 3 blocks.  The first contains [0.25, 0.5, 0.75], the
        second contains [1, 1.25, 1.5, 1.75], and the third contains
        [2, 2.25, 2.5].

        The reasoning behind the whole tree structure is to allow for easy
        extraction of all coordinates within range of a user supplied
        coordinate.  This is done in two stages:  The first is to find out
        which block the user supplied coordinate belongs to.  We can then
        quickly narrow down the search by recognizing that coordinates in the
        tree population inside the window region of the supplied coordinate
        must either belong to the same block, or to immediately neighboring
        blocks since each block of the tree is the same width as the window
        radius.

        Once all candidates have been identified, the next step is to keep
        only those that are within a radius :math:`\Omega` of the user supplied
        coordinate.  This can be accomplished quickly using the ball-tree
        algorithm (see :func:`sklearn.neighbors.BallTree`.

        In practice, the resampling algorithm loops through each block of
        the tree in parallel.  For each block, all user supplied coordinates
        (points at which a fit is required) within that block, and all tree
        members within the neighborhood (the block and all adjacent blocks
        including diagonals) are evaluated in one step by the ball-tree
        algorithm so that for each point, we quickly get all tree members
        within that point's window region.

        Parameters
        ----------
        argument : numpy.ndarray (n_features, n_samples) or n-tuple
            Either the independent coordinates of samples in n_features-space,
            or the shape defining the skeleton of the tree.
        shape : n-tuple, optional
            If coordinates were supplied with `argument`, the shape of the
            tree to build.  Otherwise, the shape will be determined from the
            coordinate values in each dimension as
            floor(max(coordinates[i])) + 1 for dimension i.
        build_type : str, optional
            Must be one of {'hood', 'balltree', 'all', None}.  Defines the
            type of tree structures to create.
        balltree_metric : str or sklearn.neighbors.DistanceMetric object
            The distance metric to use for the tree. Default=’minkowski’ with
            p=2 (that is, a euclidean metric). See the documentation of the
            :func:`sklearn.neighbors.DistanceMetric` class for a list of
            available metrics. ball_tree.valid_metrics gives a list of the
            metrics which are valid for BallTree.
        leaf_size : int, optional
            If `build_type` was set to 'all' or 'balltree', defines the leaf
            size of the BallTree.  Please see
            :func:`sklearn.neighbors.BallTree` for further details.
        large_data : bool, optional
            If `True`, indicates that this resampling algorithm will run on
            a large set of data, and the tree should be created on subsets
            of the data only when necessary.
        distance_kwargs : dict, optional
            Optional keyword arguments passed into
            :func:`sklearn.neighbors.DistanceMetric`.  The default is to use
            the "minkowski" definition with `p=2`, i.e., the Euclidean
            definition.
        """
        self._shape = None
        self._n_features = None
        self._n_blocks = None
        self._search = None
        self._tree = None
        self._balltree = None
        self._ball_initialized = False
        self._hood_initialized = False
        self.block_offsets = None
        self.block_population = None
        self.populated = None
        self.hood_population = None
        self.coordinates = None
        self.large_data = large_data
        self.ball_tree_block = None
        self.ball_tree_members = None
        self.leaf_size = leaf_size
        self.distance_kwargs = distance_kwargs

        arg = np.asarray(argument)
        if np.asarray(arg).ndim > 1:
            self.build_tree(arg, shape=shape, method=build_type,
                            leaf_size=self.leaf_size, **self.distance_kwargs)
        else:
            self._set_shape(tuple(arg))

    @property
    def shape(self):
        """
        Return the shape of the coordinate bins of the tree.

        The coordinate bins divide coordinates into bins where all values
        between 2 <= x_i < 3 for coordinate x in dimension i are in bin 0.
        Note that coordinates should have been scaled accordingly such that
        they range between 0 -> n, where n are the maximum number of bins.

        Returns
        -------
        shape : tuple (int)
            The number of bins in each dimension.
        """
        return tuple(self._shape)

    @property
    def features(self):
        """
        Returns the number of features (dimensions) in the tree.

        Returns
        -------
        int
        """
        return self._n_features

    @property
    def n_blocks(self):
        """
        Return the total number of blocks in the tree.

        The total number of blocks is simply the number of available coordinate
        bins.

        Returns
        -------
        int
        """
        return self._n_blocks

    @property
    def search(self):
        """
        Return a search array used to identify neighbouring blocks.

        Returns
        -------
        search_array : numpy.ndarray (int)
            An (n_features, n_permutations) containing values {-1, 0, 1} where
            each permutation gives a block offset in relation to a central
            block.
        """
        return self._search

    @property
    def balltree_initialized(self):
        """
        Return `True` if the BallTree has been initialized.

        Returns
        -------
        bool
        """
        return self._ball_initialized

    @property
    def hood_initialized(self):
        """
        Return `True` if the neighbourhood tree has been initialized.

        Returns
        -------
        bool
        """
        return self._hood_initialized

    @property
    def n_members(self):
        """
        Return the total number of coordinates stored in the tree.

        Returns
        -------
        int
        """
        if self.coordinates is None:
            return 0
        else:
            return self.coordinates.shape[1]

    @classmethod
    def estimate_ball_tree_bytes(cls, coordinates, leaf_size=40):
        """
        Estimate the maximum number of bytes used to construct the ball-tree.

        According to sklearn documentation, the memory needed to store the
        tree scales as::

             2 ** (1 + floor(log2((n-1) / leaf_size))) - 1

        However, empirically the data also scale as (ndim+1) * leaf_size.

        The estimated number of elements is given as::

            (ndim+1) * leaf_size * (2 ** (1+floor(log2((n-1) / leaf_size)))-1)

        This is then scaled by the number of bytes in a float (typically 8).

        Parameters
        ----------
        coordinates : numpy.ndarray
            The coordinates of shape (n_dimensions, n)
        leaf_size : int
            The number of leaves used to construct the ball-tree

        Returns
        -------
        max_size : int
            The maximum number of bytes used to construct the tree.
        """
        if leaf_size is None:
            leaf_size = 40
        float_bytes = np.empty(0, dtype=float).itemsize
        if coordinates.ndim == 1:
            coordinates = coordinates[None]
        n_dimensions = coordinates.shape[0]
        n = coordinates.size // n_dimensions

        scale = 2 ** (1 + np.floor(np.log2((n - 1) / leaf_size))) - 1
        correcting_factor = leaf_size * (n_dimensions + 1)
        n_elements = scale * correcting_factor
        return int(n_elements * float_bytes)

    @classmethod
    def estimate_split_tree_bytes(cls, coordinates,
                                  window=1, leaf_size=40):
        """
        Return the size of a ball tree for a single neighborhood.

        Parameters
        ----------
        coordinates : numpy.ndarray
            The coordinates of shape (n_dimensions, n)
        window : int or float or numpy.ndarray, optional
            The window binning size.  If a numpy array is supplied, it should
            be of shape (n_dimensions,).
        leaf_size : int
            The number of leaves used to construct the ball-tree

        Returns
        -------
        max_size : int
            The maximum number of bytes used to construct the tree.
        """
        if leaf_size is None:
            leaf_size = 40
        n_bins = cls.estimate_n_bins(coordinates, window=window)
        if coordinates.ndim == 1:  # pragma: no cover
            coordinates = coordinates[None]
        n_dimensions = coordinates.shape[0]
        n_samples = coordinates[0].size
        samples_per_bin = n_samples / n_bins
        bins_per_hood = 3 ** n_dimensions
        n = bins_per_hood * samples_per_bin  # samples in each hood tree
        n = int(np.clip(n, 0, n_samples))
        scale = 2 ** (1 + np.floor(np.log2((n - 1) / leaf_size))) - 1
        correcting_factor = leaf_size * (n_dimensions + 1)
        float_bytes = np.empty(0, dtype=float).itemsize
        n_elements = scale * correcting_factor
        return int(n_elements * float_bytes)

    @classmethod
    def estimate_n_bins(cls, coordinates, window=1):
        """
        Estimate the number of bins in the hood tree.

        Parameters
        ----------
        coordinates : numpy.ndarray
            The coordinates of shape (n_dimensions, n).
        window : int or float or numpy.ndarray, optional
            The window binning size.  If a numpy array is supplied, it should
            be of shape (n_dimensions,).

        Returns
        -------
        bytes : int
        """
        if coordinates.ndim == 1:  # pragma: no cover
            coordinates = coordinates[None]

        n_dimensions = coordinates.shape[0]
        span = np.empty(n_dimensions, dtype=float)
        for i in range(n_dimensions):
            span[i] = np.nanmax(coordinates[i]) - np.nanmin(coordinates[i])

        span /= window
        bins = np.ceil(span).astype(int)
        n_bins = np.clip(int(np.prod(bins)), 1, None)
        return n_bins

    @classmethod
    def estimate_hood_tree_bytes(cls, coordinates, window=1):
        """
        Estimate the maximum byte size for the neighborhood tree.

        Parameters
        ----------
        coordinates : numpy.ndarray
            The coordinates of shape (n_dimensions, n).
        window : int or float or numpy.ndarray, optional
            The window binning size.  If a numpy array is supplied, it should
            be of shape (n_dimensions,).

        Returns
        -------
        bytes : int
        """
        if coordinates.ndim == 1:
            coordinates = coordinates[None]

        n_dimensions = coordinates.shape[0]
        n_bins = cls.estimate_n_bins(coordinates, window=window)
        n_samples = coordinates.size // n_dimensions

        test_array = np.zeros(0, dtype=int)
        item_byte_size = test_array.itemsize
        empty_byte_size = byte_size_of_object(test_array)
        average_samples = n_samples / n_bins
        array_byte_size = empty_byte_size + (item_byte_size * average_samples)
        # Add all sub arrays and the container array
        hood_tree_byte_size = (array_byte_size * n_bins) + empty_byte_size
        return int(np.ceil(hood_tree_byte_size))

    @classmethod
    def estimate_max_bytes(cls, coordinates, window=1, leaf_size=40,
                           full_tree=True, **kwargs):
        """
        Estimate the maximum number of bytes required by the tree.

        Parameters
        ----------
        coordinates : numpy.ndarray
            The coordinates for the tree of shape (n_dimensions, n)
        window : int or float, optional
            The size of the tree windows
        leaf_size : int, optional
            The number of leaves used to construct the ball-tree
        full_tree : bool, optional
            Calculate the maximum number of bytes if the full ball-tree is
            pre-calculated.  Otherwise, calculates the size of a single
            neighborhood sized ball-tree.
        kwargs : dict, optional
            Additional keyword arguments to pass that may be used by subclasses
            of the BaseTree.

        Returns
        -------
        max_size : int
            The maximum number of bytes used to construct the tree.
        """
        float_bytes = np.empty(0, dtype=float).itemsize

        # 1 for coordinates, 1 for coordinate offsets.
        coordinate_bytes = 2 * coordinates.size * float_bytes
        if full_tree:
            ball_tree_bytes = cls.estimate_ball_tree_bytes(
                coordinates, leaf_size=leaf_size)
        else:
            ball_tree_bytes = cls.estimate_split_tree_bytes(
                coordinates, window=window, leaf_size=leaf_size)

        hood_tree_bytes = cls.estimate_hood_tree_bytes(
            coordinates, window=window)

        return int(ball_tree_bytes + hood_tree_bytes + coordinate_bytes)

    @staticmethod
    def get_class_for(thing):
        """
        Return a Tree class specific to a given grid, resampler, or name.

        Parameters
        ----------
        thing : BaseGrid or ResampleBase or str
            Either a sub-class of a BaseGrid, ResampleBase, or a string.

        Returns
        -------
        BaseTree subclass
        """
        if isinstance(thing, str):
            name = thing
        else:
            if inspect.isclass(thing):
                class_name = thing.__name__
            else:
                class_name = thing.__class__.__name__
            if 'Grid' in class_name:
                name = class_name.split('Grid')[0]
            elif 'Resample' in class_name:
                name = class_name.split('Resample')[-1]
            else:
                name = None

        return BaseTree.get_class_for_name(name)

    @staticmethod
    def get_class_for_name(name):
        """
        Return a Tree class of the given name.

        Parameters
        ----------
        name : str
            The name of the tree.

        Returns
        -------
        BaseTree subclass
        """
        tree_path = tree_module.__name__
        name = 'base' if name in ['', None] else name
        module_path = (tree_path + f'.{name}_tree').lower().strip()
        class_name = ''.join(
            [s[0].upper() + s[1:] for s in name.split('_')]) + 'Tree'
        try:
            module = importlib.import_module(module_path)  # Allow errors
            tree_class = getattr(module, class_name)
            if tree_class is None:  # pragma: no cover
                return BaseTree
            return tree_class
        except ModuleNotFoundError:
            return BaseTree

    def _set_shape(self, shape):
        r"""
        Set the shape of the tree.

        The shape of the tree determines the number of n-dimensional coordinate
        bins.  Once defined, a number of useful factors are pre-calculated to
        allow for easy determination of neighboring bins to any given bin.

        The search kernel is every permutation of [-1, 0, 1] over all features,
        meaning each bin not on the edge of the tree bounds has :math:`3^n - 1`
        neighbors.

        Parameters
        ----------
        shape : n-tuple of int
            The size for each of the n features of the tree.

        Returns
        -------
        None
        """
        self._shape = np.asarray(shape).ravel().astype(int)
        self._n_features = len(self._shape)
        self._n_blocks = np.prod(self._shape)

        searches = np.array(list(
            itertools.product([-1, 0, 1], repeat=self.features)))

        self._search = searches.T

        # self._multipliers = abs(searches)
        # self._deltas = -1 * (searches < 0)
        # reverse_search = searches * -1
        # self._reverse_deltas = -1 * (reverse_search < 0)

        # Reset necessary attributes
        self._tree = None
        self.block_offsets = None
        self.block_population = None
        self.populated = None
        self.hood_population = None

    def to_index(self, coordinates):
        r"""
        Find the tree block indices of a coordinate or set of coordinates.

        Examples
        --------
        A tree of shape (5, 6) in 2 dimensions contains coordinates ranging
        from 0-5 in x, and 0-6 in y.  The coordinate (x, y) = (2.7, 3.1) would
        exist in tree block [2, 3] (floored values), but the tree block
        structure is one-dimensional.  The actual block index would be
        15 since it is calculated as (2 * 6) + 3, or (x * shape[1]) + y.

        Parameters
        ----------
        coordinates : numpy.ndarray (n_feature,) or (n_feature, n_coordinates)
            The coordinates for which to return tree block indices.
            Coordinates should be scaled such that they range from
            0 -> tree.shape[k] for the feature k.

        Returns
        -------
        index or indices : np.int64 or np.ndarray (n_coordinates,)
            The tree block indices for the supplied coordinates.
        """
        c = np.asarray(coordinates, dtype=int)
        single = c.ndim == 1
        if single:
            c = c[:, None]
        index = np.ravel_multi_index(c, self._shape, mode='clip')
        return index[0] if single else index

    def from_index(self, index):
        r"""
        Find the lower corner coordinate for a tree block index.

        Examples
        --------
        The lower corner coordinate in a tree block is given as the floored
        values of all coordinates that exist inside the block.  For example,
        the index 15 in a tree of shape (5, 6) corresponds to coordinates in
        the range x = (2->3), y = (3->4).  Therefore, the lower coordinate is
        (2, 3).

        Parameters
        ----------
        index : int or numpy.ndarray (n_indices,)
            The indices for which to return the lower corner coordinate(s).

        Returns
        -------
        lower_corner_coordinates : numpy.ndarray of int
           An array of shape (n_features,) or (n_features, n_indices)
           containing the lower corner coordinates of supplied tree block
           indices.
        """
        c = np.asarray(index, dtype=int)
        shape = c.shape
        single = shape == ()
        index = np.unravel_index(np.atleast_1d(c).ravel(), self._shape)
        coordinates = np.empty((self._n_features, c.size), dtype=int)
        coordinates[:] = index
        return coordinates.ravel() if single else coordinates

    def build_tree(self, coordinates, shape=None, method='all',
                   leaf_size=40, **distance_kwargs):
        r"""
        Create the ball and hood tree structures from coordinates.

        Populates the tree with coordinates.

        Parameters
        ----------
        coordinates : numpy.ndarray (n_features, n_coordinates)
            The coordinates from which to build the trees.
        shape : n-tuple of int, optional
            The shape of the tree.  If not supplied it is calculated as
            floor(max(coordinate[k])) + 1 for the feature k.
        method : str, optional
            Specifies the trees to build.  Available options are {'hood',
            'balltree', 'all', None}.  The default ('all') builds both
            the balltree and hood tree.
        leaf_size : int, optional
            An specifies the point at which the ball tree switches over to
            the brute force method.  See :func:`sklearn.neighbors.BallTree`
            for further information.
        distance_kwargs : dict, optional
            Optional keyword arguments passed into
            :func:`sklearn.neighbors.DistanceMetric`.  The default is to use
            the "minkowski" definition with `p=2`, i.e., the Euclidean
            definition.

        Returns
        -------
        None
        """
        if shape is None:
            self._set_shape(coordinates.astype(int).max(axis=1) + 1)
        else:
            self._set_shape(shape)
        method = str(method).lower().strip()

        self.coordinates = np.asarray(coordinates).astype(float, order='F')

        if method == 'hood':
            self._build_hood_tree()
        elif method == 'balltree':
            self._build_ball_tree(leaf_size=leaf_size, **distance_kwargs)
        elif method == 'none':
            pass
        elif method == 'all':
            self._build_hood_tree()
            self._build_ball_tree(leaf_size=leaf_size, **distance_kwargs)
        else:
            raise ValueError(f"Unknown tree building method: {method}")

    def build_ball_tree_for_block(self, block):
        """
        Build a ball tree for a neighborhood around a single block.

        Parameters
        ----------
        block : int
            The center block of the neighborhood.

        Returns
        -------
        None
        """
        if not self.hood_initialized:
            self._build_hood_tree()
        self.ball_tree_block = int(block)
        self.ball_tree_members, coordinates = self.hood_members(
            self.ball_tree_block, get_locations=True)
        if self.leaf_size is None:
            self._balltree = BallTree(
                coordinates.T, **self.distance_kwargs)
        else:
            self._balltree = BallTree(
                coordinates.T, leaf_size=self.leaf_size,
                **self.distance_kwargs)

    def _build_ball_tree(self, leaf_size=40, **distance_kwargs):
        r"""
        Build the balltree structure.

        The balltree is created using :func:`sklearn.neighbors.BallTree` and
        allows rapid calculation of relative distances from a supplied set
        of coordinates to those within the tree.

        Parameters
        ----------
        leaf_size : int, optional
            An specifies the point at which the ball tree switches over to
            the brute force method.  See :func:`sklearn.neighbors.BallTree`
            for further information.
        distance_kwargs : dict, optional
            Optional keyword arguments passed into
            :func:`sklearn.neighbors.DistanceMetric`.  The default is to use
            the "minkowski" definition with `p=2`, i.e., the Euclidean
            definition.

        Returns
        -------
        None
        """
        self._ball_initialized = False
        self.ball_tree_block = None
        if self.large_data:
            # Do not attempt to build a full ball tree for large data
            return
        if leaf_size is None:
            self._balltree = BallTree(self.coordinates.T,
                                      **distance_kwargs)
        else:
            self._balltree = BallTree(self.coordinates.T,
                                      leaf_size=leaf_size,
                                      **distance_kwargs)
        self._ball_initialized = True

    def _build_hood_tree(self):
        r"""
        Build the hood tree structure.

        The hood tree defines a number of attributes allowing for fast access
        to all coordinates within a tree block, or the coordinates within
        a tree block and all neighboring blocks.  A single block and all its
        neighboring blocks are referred to as the "neighborhood" or "hood" of
        the block.  In addition, the population of each block and hood are also
        stored.

        Returns
        -------
        None
        """
        self._hood_initialized = False
        bins = self.to_index(self.coordinates)
        indices = np.arange(bins.size)
        n_bins = np.prod(self._shape)
        self._tree = csr_matrix((indices, [bins, indices]),
                                shape=(n_bins, bins.size))
        self._tree = np.split(self._tree.data, self._tree.indptr[1:-1])

        self.block_offsets = self.coordinates - self.coordinates.astype(int)
        self.block_population = np.array([s.size for s in self._tree])
        self.populated = np.nonzero(self.block_population > 0)[0]
        self.hood_population = np.zeros_like(self.block_population)
        self.max_in_hood = np.zeros_like(self.block_population)
        for block in range(self.n_blocks):
            hoods = self.neighborhood(block, cull=True)
            if hoods.size == 0:  # pragma: no cover
                self.hood_population[block] = 0
                self.max_in_hood[block] = 0
            else:
                self.hood_population[block] = np.sum(
                    self.block_population[hoods])
                self.max_in_hood[block] = np.max(
                    self.block_population[hoods])
        self._hood_initialized = True

    def query_radius(self, coordinates, radius=1.0, block=None, **kwargs):
        r"""
        Return all tree members within a certain distance of given points.

        Quickly retrieves all members of the tree population within the radius
        of some defined coordinate(s).  The default distance definition is
        "minkowski" with p=2, which is equivalent to the "euclidean"
        definition. For a list of available distance metric please see
        `sklearn.neighbors.BallTree.valid_metrics`.  The distance metric may
        be defined during PolynomialTree initialization.

        Parameters
        ----------
        coordinates : numpy.ndarray
            An (n_features, n_coordinates) or (n_features,) array containing
            coordinates around which to determine tree members within a
            certain radius.
        radius : float, optional
            The search radius around each coordinate.
        block : int, optional
            Used to create a temporary ball tree for the given block when
            operating on large data.
        kwargs : dict, optional
            Keywords for :func:`sklearn.neighbors.BallTree.query_radius`.

        Returns
        -------
        indices : numpy.ndarray of object (n_coordinates,)
            Each element is a numpy integer array containing the indices of
            tree members within `radius`.  This return value may change
            depending on the options in `kwargs`.  Please see
            :func:`sklearn.neighbors.BallTree.query_radius` for further
            information.
        """
        if coordinates.ndim == 1:
            c = coordinates[None]
        else:
            c = coordinates.T

        if self.large_data:
            if block is None:
                raise ValueError("No block number for large data query has "
                                 "been supplied.")
            self.build_ball_tree_for_block(block)
            indices = self._balltree.query_radius(c, radius, **kwargs)
            if kwargs.get('return_distance'):
                return_distance = True
                indices, distances = indices
            else:
                return_distance = False
                distances = None

            for i, point_indices in enumerate(indices):
                indices[i] = self.ball_tree_members[point_indices]

            if return_distance:
                return indices, distances
            else:
                return indices

        elif not self.balltree_initialized:
            raise RuntimeError("Ball tree not initialized")
        else:
            return self._balltree.query_radius(c, radius, **kwargs)

    def block_members(self, block, get_locations=False):
        if not self._hood_initialized:
            raise RuntimeError("Neighborhood tree not initialized.")
        members = self._tree[block]
        if not get_locations:
            return members
        else:
            return members, self.coordinates[:, members]

    def neighborhood(self, index, cull=False, valid_neighbors=False):
        expanded = self.from_index(index)[:, None] + self.search
        bad = np.any(
            (expanded < 0) | (expanded >= self._shape[:, None]), axis=0)
        hood = self.to_index(expanded)
        keep = ~bad
        if cull:
            hood = hood[keep]
        else:
            hood[bad] = -1

        return (hood, keep) if valid_neighbors else hood

    def hood_members(self, center_block, get_locations=False):
        r"""
        Find all members within the neighborhood of a single block.

        Parameters
        ----------
        center_block : int
            The index of the block at the center of the neighborhood.
        get_locations : bool, optional
            If `True`, return the coordinates of the hood members.

        Returns
        -------
        members, [coordinates]
            members is a numpy.ndarray of int and shape (found_members,).
            If `get_locations` was set to `True,` coordinates is of shape
            (n_features, found_members).
        """
        if not self._hood_initialized:
            raise RuntimeError("Neighborhood tree not initialized.")

        blocks = self.neighborhood(center_block, cull=True)
        hood_members = np.empty(self.block_population[blocks].sum(), dtype=int)
        found = 0
        for block in blocks:
            block_members = self.block_members(block)
            hood_members[found: found + block_members.size] = block_members
            found += block_members.size

        hood_members = hood_members[:found]
        if not get_locations:
            return hood_members
        else:
            return hood_members, self.coordinates[:, hood_members]

    def __call__(self, x, reverse=False):
        r"""
        Returns the block index of a coordinate

        Parameters
        ----------
        x : int or numpy.ndarray (features,) or (features, n_coordinates)
            The block for which to find the lower coordinate.  If `reverse` is
            `False`, returns the block index for a given coordinate.
        reverse : bool, optional
            Specifies if the return value should be the lower coordinate or
            block index.

        Returns
        -------
        block_index or coordinate : int or numpy.ndarray
        """
        if reverse:
            return self.from_index(x)
        else:
            return self.to_index(x)


from odoo.tests.common import TransactionCase

class TestTransactionEnvs(TransactionCase):
    def test_transation_envs_weakrefs(self):
        transaction = self.env.transaction
        starting_envs = set(transaction.envs)
        base_x = self.env['base'].with_context(test_stuff=False)
        self.assertIn(base_x.env, transaction.envs)
        del base_x
        self.assertEqual(set(transaction.envs), starting_envs)

    def do_stuff_with_env(self):
        base_test = self.env['base'].with_context(test_stuff=False)
        base_test |= self.env['base'].with_context(test_stuff=1)
        base_test |= self.env['base'].with_context(test_stuff=2)
        return base_test

    def test_transation_envs_weakrefs_call(self):
        transaction = self.env.transaction
        starting_envs = set(transaction.envs)
        self.do_stuff_with_env()
        self.assertEqual(set(transaction.envs), starting_envs)

    def test_transation_envs_weakrefs_return(self):
        transaction = self.env.transaction
        starting_envs = set(transaction.envs)
        base_test = self.do_stuff_with_env()
        self.assertEqual(set(transaction.envs), starting_envs | {base_test.env})

    def test_transation_envs_ordered(self):
        transaction = self.env.transaction
        starting_envs = set(transaction.envs)
        # create environments in a certain order, not sorted on item
        items = [3, 8, 1, 5, 2, 7, 6, 9, 0, 4]
        envs = [self.env(context={'item': item}) for item in items]
        # check that those environments appear in order in transaction.envs
        env_items = [env.context['item'] for env in transaction.envs if env not in starting_envs]
        self.assertEqual(env_items, items)
        del envs
        self.assertEqual(set(transaction.envs), starting_envs)


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

   File Name：     hasCycle
   Author :       jing
   Date：          2020/3/19

   https://leetcode-cn.com/explore/interview/card/tencent/222/linked-list/916/

   链表是否有环
"""
from tencent.linkedlist.ListNode import ListNode

class Solution:
    def hasCycle(self, head: ListNode) -> bool:
        if head is None or head.next is None:
            return False
        fast = head
        slow = head

        while True:
            if fast.next is not None and fast.next.next is not None:
                fast = fast.next.next
                slow = slow.next
            else:
                return False
            if slow == fast:
                return True

if __name__ == '__main__':
    root = ListNode(1)
    root.next = ListNode(2)
    print(Solution().hasCycle(root))


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

import sys
from .sohu_quotation import SohuQuotation
from .sina_quotation import SinaQuotation
from .tencent_quotation import TencentQuotation
from .akshare_quotation import AkshareQuotation

class Quotation:
    """ 行情类 """

    def __init__(self):
        self.sohu = SohuQuotation()
        self.sina = SinaQuotation()
        self.tecent = TencentQuotation()
        self.akshare = AkshareQuotation()
        return

    def get_realtime_quotes(self, codes):
        """
        获取当前行情
        """
        d = self.sina.get_realtime_quotes(codes)
        return d

    def get_today_ticks(self, code):
        """
        获取当天tick数据
        """
        d = self.sohu.get_today_ticks(code)
        return d

    def get_daily_data(self, code, expire=60*6):
        """
        获取一支股票所有历史数据保存到本地
        """
        d = self.akshare.get_daily_data(code, expire)
        return d

    def get_tick_data(self, code, date, expire=60*24*365*10):
        """
        获取一支股票一天的tick数据保存到本地
        --------
        code: string,股票代码
        date: string,1900-01-01
        """
        d = self.akshare.get_tick_data(code, date, expire)
        return d

    #def get_one_realtime_quotes(self, code):
    #    """
    #    获取当前行情
    #    """
    #    d = self.eastmoney.get_realtime_quotes(code)
    #    return d

    #def get_today_shibor_ON(self):
    #    """
    #    获取今天的银行间拆借利率 隔夜(O/N)
    #    """
    #    d = self.tushare.get_today_shibor_ON()
    #    return d

    #def get_sina_dd(self, code, date='', vol=400):
    #    """
    #    大单交易数据
    #    """
    #    d = self.tushare.get_sina_dd(code, date, vol)
    #    return d

    #def get_hgt_capital(self):
    #    """
    #    得到当前沪股通资金情况
    #    """
    #    d = self.eastmoney.get_hgt_capital()
    #    return d

    #def get_hsgt_top(self, date_str, expire = 60*24*30):
    #    """
    #    得到沪股通、深股通 十大成交股
    #    """
    #    d = self.eastmoney.get_hsgt_top(date_str, expire)
    #    return d

    #def get_hsgt_his(self, days=30, market_type=1, expire = 60*6):
    #    """
    #    得到沪股通、深股通 历史资金数据
    #    资金单位百万
    #    """
    #    d = self.eastmoney.get_hsgt_his(days, market_type, expire)
    #    return d

    def get_trade_date(self, days = -1):
        """
        得到交易日期
        通过上证指数的日期来得到有交易的时间
        """
        d = self.get_h_data('000001')

        if days != -1:
            d = d.head(days)

        return list(d.index)

    def test(self):
        import tushare as ts
        #print(ts.get_hist_data('600848', ktype='1', start='2020-01-01'))
        print(ts.get_tick_data('sh',date='2017-12-12',src='tt'))

def main(argv):
    q = Quotation()

    #q.test()

    #r = q.get_realtime_quotes('sh')
    #for (k, v) in list(r.items()):
    #    string = v.__str__()
    #    print((string.encode('utf-8')))
    #d = q.get_realtime_quotes(['000001', '000002'])
    #print((len(d)))
    #for (k, v) in list(d.items()):
    #    print(k)
    #    string = v.__str__()
    #    print((string.encode('utf-8')))
    #    print((v.name.encode('utf-8')))

    #d = q.get_today_ticks('sh')
    #print((d.symbol))
    #print((d.df))

    d = q.get_daily_data('000001')
    print(d)
    d = q.get_tick_data('000001', '2020-12-02')
    print(d)
    print((len(d)))

    ##d = q.get_one_realtime_quotes('131800')
    ##for (k,v) in list(d.items()):
    ##    string = v.__str__()
    ##    print((string.encode('utf-8')))

    ##d = q.get_stock_basics()
    ##print(d)
    ##print((d.index))
    ##print((len(d['name'])))

    #d = q.get_today_shibor_ON()
    #print(d)
    #d = q.get_hgt_capital()
    #print(d)

    #d = q.get_sina_dd('600340', date='2017-04-21', vol=400)
    #d.to_csv('tt.csv', sep='\t')
    #d = q.get_trade_date(30)
    #print(d)

    return

if __name__ == "__main__":
    main(sys.argv)


from contextlib import contextmanager
from datetime import datetime
import sqlite3

things_table = 'things'

@contextmanager
def iota_db(path):
    db = sqlite3.connect(path)
    try:
        yield db
    finally:
        db.commit()
        db.close()

def initialize(db):
    """Initialize iota database non-destructively."""
    cmd = f'CREATE TABLE IF NOT EXISTS {things_table} (name TEXT)'

    execute(db, cmd)

def get_things(db):
    """Get things in iota."""
    cmd = f'SELECT name FROM {things_table}'

    res = execute(db, cmd)
    things = sorted((item[0] for item in res.fetchall()))

    return things

def add_thing(db, descriptor):
    """Add thing to iota."""
    new_thing = descriptor['id']
    if new_thing in get_things(db):
        # thing already exists.
        return False

    # Register thing.
    cmd = f'INSERT INTO {things_table} VALUES ("{new_thing}")'

    execute(db, cmd)

    # Create table for thing.
    columns = sorted((k for k in descriptor.keys() if k != 'id'))
    cmd = f'CREATE TABLE {new_thing} (timestamp INTEGER, '
    cmd += ', '.join((f'{k} {descriptor[k]}' for k in columns))
    cmd += ')'

    execute(db, cmd)

    return True

def get_latest_thing_entry(db, thing):
    """Get most recent thing entry."""
    columns = table_columns(db, thing)

    cmd = f'SELECT * FROM {thing} WHERE ROWID IN (SELECT max(ROWID) FROM {thing})'

    res = execute(db, cmd)
    entries = [dict(zip(columns, e)) for e in res]

    return entries[-1] if len(entries) > 0 else []

def get_most_recent_thing_entries(db, thing, n):
    """Get `n` most recent thing data entries"""
    columns = table_columns(db, thing)

    cmd = f'SELECT * from {thing} WHERE ROWID > (SELECT max(ROWID) FROM {thing}) - {n}'

    res = execute(db, cmd)
    entries = [dict(zip(columns, e)) for e in res]

    return entries

def get_thing_entries(db, thing, start, finish):
    """Get range of thing data entries based on timestamp."""
    columns = table_columns(db, thing)

    key = 'timestamp'
    start_date = datetime.fromisoformat(start)
    finish_date = datetime.fromisoformat(finish)
    cmd = f'SELECT * FROM {thing} WHERE {key} >= {start_date.timestamp()} AND {key} <= {finish_date.timestamp()}'

    res = execute(db, cmd)
    entries = [dict(zip(columns, e)) for e in res]

    return entries

def add_to_thing(db, thing, datapoint):
    """Add datapoint to thing."""
    timestamp = datetime.now().timestamp()
    columns = sorted((k for k in datapoint.keys() if k != 'id'))
    cmd = f'INSERT INTO {thing} VALUES ({timestamp}, '
    cmd += ', '.join((f'{datapoint[k]}' for k in columns))
    cmd += ')'

    execute(db, cmd)

def table_columns(db, table):
    """Get column names for table"""
    cmd = f'SELECT name FROM pragma_table_info("{table}")'

    res = execute(db, cmd)
    entries = [r[0] for r in res]

    return entries

def execute(db, cmd):
    """Execute SQL command"""
    print(f'SQL: {cmd}')

    cur = db.cursor()
    return cur.execute(cmd)


# -*- coding: utf-8 -*-
"""
Created on Sat Jun 27 17:15:34 2020

@author: Thoma
"""

# %% codecell
# imports

import numpy as np
import numpy.linalg as la
import scipy.linalg as scila
from numpy import kron

# %% codecell
# setup functions

#Adjacency standard of Aij is a line from i to j

def indegrees(M,node):
    #This is the sum of the colume at node X, counting from 0
    sumCurrent = 0
    for index in range(0,M.shape[0]):
        sumCurrent += M[index][node]
    return sumCurrent

def outdegrees(M,node):
    #Counts the entries in the node row
    return sum(M[node])

def aVal(M,x):
    indegree = indegrees(M,x)
    outdegree = outdegrees(M,x)
    if indegree == 0 and outdegree == 0:
        return 1
    else:
        return indegree / (indegree + outdegree)

def constructC(M):

    # Force input matrix to already have dimensions as a power of 2
    if not np.isclose(np.log2(M.shape[0]), int(np.log2(M.shape[0]))):
        print("Please pad your adjacency matrix so its dimensions are a power of 2")

    output = np.zeros([2 * x for x in M.shape])

    for x in range(M.shape[0]):

        xVec = np.zeros(M.shape[0])
        xVec[x] = 1
        outerX = np.diag(xVec)

        a = aVal(M,x)
        cellElement = np.array([[np.sqrt(1/(a + 1)),np.sqrt(a/(1 + a))],[np.sqrt(a/(1 + a)),-1 * np.sqrt(1/(a + 1))]])
        output = output + kron(cellElement,outerX)

    return output

def constructS(M):

    # Force input matrix to already have dimensions as a power of 2
    if not np.isclose(np.log2(M.shape[0]), int(np.log2(M.shape[0]))):
        print("Please pad your adjacency matrix so its dimensions are a power of 2")

    up_proj = np.diag([1,0])
    down_proj = np.diag([0,1])

    P,D,Q = la.svd(M)
    U = P @ scila.expm(1j*np.diag(D)) @ Q

    if_up = np.kron(up_proj, np.identity(M.shape[0]))
    if_down = np.kron(down_proj, U)
    S = if_up+if_down

    return S

# adjacency matrix for binary trees
def binaryTree(levels):
    n_nodes = 2**(levels+1)
    # final node recieving a connection
    n_final = 2**levels - 1

    adj_mat = np.zeros([n_nodes, n_nodes])
    for j in range(n_final):
        adj_mat[2*j+1, j] = adj_mat[2*j+2, j] = 1

    return adj_mat

# adjacency matrix for scale-free network
def scaleFree(nodes):
    adj_mat = np.zeros([nodes,nodes])

    adj_mat[0,0]=1

    for node in range(1,nodes):
        probs = np.array([sum(adj_mat[i,:])+sum(adj_mat[:,i]) for i in range(node)])
        if not sum(probs) == 0:
            probs = probs/sum(probs)

        for join in range(node):
            if np.random.random() < probs[join]:
                adj_mat[node, join] = 1
    return adj_mat


import sys
import collections

N = int(sys.stdin.readline())

tmp_list = [0]*N
tmp_dict = collections.defaultdict(list)
for index, value in enumerate(map(int, sys.stdin.readline().split())):
    tmp_dict[value].append(index)

for compressed_coordinate, value in enumerate(sorted(tmp_dict)):
    for index in tmp_dict[value]:
        tmp_list[index] = compressed_coordinate

print(' '.join(map(str, tmp_list)))

Baekjoon Online Judge/problem/18870/26767911 맞았습니다!! 271240 KB 3008 ms.py


# -----------------------------------------------------------------------------
# Name:        main (python.py)
# Purpose:     To analyze the statistics of the sentences in a text file
#
#
# Author:      Ethan Wang
# Created:     21-Sep-2018
# Updated:     24-Sep-2018
# ------

# Location of the text file
filename = '/Users/EthanWang/Python/scan/text.txt'
# Opens the file for reading
file = open(filename, 'r')
# Stores file into a string
contents = file.read()

class Sentence():
    """
    A sentence with the number of words in each word class

    Attributes
    ----------
    nouns : int
        Number of nouns in self
    verbs : int
        Number of verbs in self
    adjectives : int
        Number of adjectives in self
    adverbs : int
        Number of adverbs in self
    pronouns : int
        Number of pronouns in self
    prepositions : int
        Number of prepositions in self
    conjunctions : int
        Number of conjunctions in self
    determiners : int
        Number of determiners in self

    Methods
    -------
    # note, do not list private methods in this section.  Do not include this line
    printAuthor() -> None
    	Prints the name of the author to the console
    printPrice() -> None
    	Prints the price of the book to the console
    printTitle() -> None
    	Prints the name of the book to the console
    increasePrice(increase : float) -> None
    	Attempts to increase the price by a float value

    """

    def __init__(self, content):
        """
        Constructor to build a sentence object

        Parameters
        ----------
        content : str
            The sentence

        """

        self.content = content

print(contents.index('.'))


# Atividade final do módulo - EX06 inferência
# Autor: Newmar Wegner, Paulo, Kleberson
# Date: 23/05/2021

import pandas as pd
from sklearn import preprocessing
from pickle import load
pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)

# Function to normalize data considering Min Max Scaler sklearn
def normalize_data_minmax(df):
    normalizer = preprocessing.MinMaxScaler()

    return normalizer.fit(df), normalizer.fit_transform(df)

# Function to normalize dummy data
def normalize_data_dummies(df):
    return pd.get_dummies(df, prefix=df.columns)

# function to create categorical and numerical dataset
def create_catnum(df, dummy_columns, class_column):
    categorical = df[dummy_columns]
    numerical = df.drop(columns=dummy_columns + class_column, axis=1)

    return categorical, numerical

# Function to divide input in dummy_columns and numerical columns
def dummy_num(data_to_predict):
    dummy_columns = []
    numerical = []

    for i in data_to_predict:
        if isinstance(i, str):
            dummy_columns.append(i)
        else:
            numerical.append(i)
    dummy_columns = pd.DataFrame(dummy_columns).T
    dummy_columns.columns = ['job', 'marital', 'education', 'default', 'housing', 'loan', 'contact', 'month',
                             'poutcome']

    return dummy_columns, pd.DataFrame(numerical)

if __name__ == '__main__':
    # Input parameters
    df = pd.read_csv('../datasets/bank.csv', sep=';')
    dummy_columns = ['job', 'marital', 'education', 'default', 'housing', 'loan', 'contact', 'month', 'poutcome']
    class_column = ['y', ]

    # Creating categorical and numerical dataset
    categorical, numerical = create_catnum(df, dummy_columns, class_column)

    # Get dummies to categorical dataset
    norm_categorical = normalize_data_dummies(categorical)

    # Join norm_categorical with norm_numerical dataset
    model_norm, num_normalize = normalize_data_minmax(numerical)
    num_normalize = pd.DataFrame(num_normalize, columns=numerical.columns)
    dados = num_normalize.join(norm_categorical, how='left')
    print(dados)

    # Load model and make predictions
    rf = load(open("./result_ft/model_bank.pkl",'rb'))
    print(rf.predict(dados))


#!/usr/bin/env python3

import argparse
import json
from collections import defaultdict
from pathlib import Path

from asdc.schema.example import Example

def operation(path_in: Path, path_out: Path) -> None:
    text2purpose2ids = {}
    with path_in.open() as inf, path_out.open("w") as outf:
        for line in inf:
            ex = Example.model_validate_json(line)

            for t in ex.targets:
                if t not in text2purpose2ids:
                    text2purpose2ids[t] = defaultdict(list)
                text2purpose2ids[t][ex.purpose].append(ex.sid.id)

        for text, purpose2ids in sorted(text2purpose2ids.items()):
            outf.write(f"{text}\t")
            if "test" in purpose2ids:
                outf.write("test")
            elif "dev" in purpose2ids:
                outf.write("dev")
            else:
                outf.write("train")
            outf.write("\t")

            outf.write(json.dumps(purpose2ids, sort_keys=True))
            outf.write("\n")

def get_opts() -> argparse.Namespace:
    oparser = argparse.ArgumentParser()
    oparser.add_argument("--input", "-i", type=Path, default="/dev/stdin", required=False)
    oparser.add_argument("--output", "-o", type=Path, default="/dev/stdout", required=False)
    return oparser.parse_args()

def main() -> None:
    opts = get_opts()
    operation(opts.input, opts.output)

if __name__ == "__main__":
    main()


import requests
import json
import os.path
def p():
    with open("/home/bhagyashri/Desktop/request/task.json","r") as jf:
        pobj=json.loads(jf.read())
    return pobj
def bubblesort_id():
    pobj=p()
    idlist=[]
    for i in pobj["data"]:
        idlist.append(i["id"])
    max=0
    for i in range(len(idlist)):
        for k in range(len(idlist)-i-1):
            if idlist[k]>idlist[k+1]:
                max=idlist[k]
                idlist[k]=idlist[k+1]
                idlist[k+1]=max
    return idlist
def final(idlist,pobj):
    sr_no=1
    for s in idlist:
        for b in pobj['data']:
            if b["id"]==s:
                print(str(sr_no)+".",b['name'],b["id"])
                sr_no+=1
def main():
    aORd=input("Enter 'a' for ASCENDING ORDER according to the id\nEnter 'd' for DESCENDING ORDER according to the id")
    if aORd=='a' or aORd=='A':
        idlist=bubblesort_id()
        pobj=p()
        final(idlist,pobj)
    elif aORd=='d' or aORd=='D':
        idlist=bubblesort_id()
        pobj=p()
        idlist=idlist[::-1]
        final(idlist,pobj)
    else:
        print("INVALID INPUT")
file_exists=os.path.exists("/home/bhagyashri/Desktop/request/task.json")
r=requests.get("https://join.navgurukul.org/api/partners")
r=r.json()
if file_exists==True:
    main()
else:
    with open("/home/bhagyashri/Desktop/request/task.json","w+") as jf:
        json.dump(r,jf,indent=2)
    main()


import simplejson as json
from mtga_log import MtgaLogParsingError, get_mtga_file_path
import scryfall

MTGA_FORMATS_FILENAME = "formats.json"
MTGA_FORMATS_KEYWORD = "PlayerInventory.GetFormats"

def normalize_set(set_id, conversion=None):
    """Convert set id readable by goldfish/deckstats"""
    conversion = {} if conversion is None else conversion
    conversion.update({'DAR': 'DOM'})
    return conversion.get(set_id.upper(), set_id.upper())

class MtgaFormats(object):
    """Process MTGA/Unity formats file"""

    def __init__(self, mtga_log, formats_filename=None):
        self.mtga_log = mtga_log
        self.formats_filename = formats_filename

    def get_full_filename(self):
        if self.formats_filename is None:
            return get_mtga_file_path(MTGA_FORMATS_FILENAME)
        return self.formats_filename

    def _get_formats_json(self):
        """Gets the formats json"""
        try:
            with open(self.get_full_filename()) as formats_file:
                return json.load(formats_file)
        except FileNotFoundError:
            return self.mtga_log.get_last_json_block(MTGA_FORMATS_KEYWORD)

    def get_format_sets(self, mtg_format):
        """Returns list of current sets in standard format"""
        try:
            json_data = self._get_formats_json()
        except ValueError as exception:
            raise MtgaLogParsingError(exception)

        sets = []
        for item in json_data:
            if item.get("name").lower() == str(mtg_format):
                for mtga_set in item.get("sets"):
                    sets.append(mtga_set)
                    if mtga_set == "DAR":
                        sets.append("DOM")
        return sets

    def get_set_info(self, mtga_set):
        return scryfall.get_set_info(mtga_set)

    def get_set_card_count(self, mtga_set):
        set_info = self.get_set_info(mtga_set)
        return set_info.get('card_count', 0)


# https://github.com/t-systems-on-site-services-gmbh/german-wikipedia-text-corpus

import os
import gensim
from gensim.parsing.preprocessing import *

dataset = os.path.join('..', 'dataset', 'wiki-all-shuf.txt')
reduced_dataset = os.path.join('..', 'dataset', 'wiki-all-shuf-reduced.txt')
german_model = os.path.join('..', 'dataset', 'german.model')
chunksize = 1073741824

def read_wiki_file() -> [string]:
   """
   Read the wiki file and return an array of text blocks.
   """

   print("Read wiki file set")
   with open(reduced_dataset, "r") as f:
       data_string = f.read()
       f.close()

   return data_string.split("\n")

def reduce_wiki_file():
   """
   Reduce the 6gb wiki dataset to given chunk size.
   """
   if os.path.isfile(reduced_dataset):
       return

   with open(dataset, "rb") as f:
       chunk = f.read(chunksize)
       f.close()

   with open(reduced_dataset, "w") as w:
       w.write(chunk.decode("utf-8"))
       w.close()

def create_german_language_model(vector_size=100, window=5, min_count=1, epochs=10, workers=3):
   """
   Preprocess the text blocks and creates a german word2vec model with given settings.
   """
   FILTERS = [strip_tags, strip_punctuation, strip_multiple_whitespaces, strip_numeric, strip_short]
   texts = read_wiki_file()

   print("Preprocess dataset")
   texts_tokenized = [preprocess_string(text.lower(), FILTERS) for text in texts]

   print("Create german word2vec model")
   model = gensim.models.Word2Vec(sentences=texts_tokenized,
                                  vector_size=vector_size,
                                  window=window,
                                  min_count=min_count,
                                  epochs=epochs,
                                  workers=workers)
   model.save(german_model)

def compute_german_words():
   """
   Compute 3 German words the top-3 most similar words
   """
   model = gensim.models.Word2Vec.load(german_model)
   model_wv = model.wv

   print("Top 3 Planeten: ", model_wv.most_similar("planeten", topn=3))
   print("Top 3 Arzt: ", model_wv.most_similar("arzt", topn=3))
   print("Top 3 Winter: ", model_wv.most_similar("winter", topn=3))


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

from newspaper.parsers import Parser
import re
"""
1. Check HTML and find special things of website
2. Redirect to real article url
"""
class SiteFilter(object):

    def __init__(self, url, doc):
        self.url = url
        self.doc = doc
        self.parser = Parser
        self.origin = None
        return

    def filter_naver(self):
        """If the article has meta wlwmanifest link set in the url
        """
        kwargs = {'tag': 'link', 'attr': 'rel', 'value': 'wlwmanifest'}
        meta = self.parser.getElementsByTag(self.doc, **kwargs)
        if meta is not None and len(meta) > 0:
            href = self.parser.getAttribute(meta[0], 'href')
            m = re.match(r"http://blog\.naver\.com/NBlogWlwLayout\.nhn\?blogId=\w*", href)
            if m is not None:
                blog_id = href.split('blogId=')[-1]
                log_no = self.url.split('/')[-1]
                self.origin = 'http://blog.naver.com/PostView.nhn?blogId=%s&logNo=%s' % (blog_id, log_no)
                return True
        return False

    def get_real_url(self):
        self.filter_naver()
        if(self.origin is not None):
            return self.origin
        return self.url