import tensorflow as tf

def ewma_baseline(rewards, tau=0.9):
    """
    Baseline observed rewards using an exponential weighted moving average.

    Args:
        rewards: batch of batch_size reward floats

    Returns:
        baselined_rewards: batch of batch_size reward floats
    """

    avg_reward = tf.Variable(0.0, trainable=False, name="avg_reward")
    new_avg_reward = tau * avg_reward + (1. - tau) * tf.reduce_mean(rewards)
    tf.scalar_summary("baseline", new_avg_reward)

    assign_avg_reward = avg_reward.assign(new_avg_reward)

    with tf.control_dependencies([assign_avg_reward]):
        return avg_reward

def reinforce_episodic_gradients(logits, sampled_outputs, rewards, lengths=None,
                                 baseline_fn=ewma_baseline, params=None):
    """
    Calculate REINFORCE gradients given a batch of single episodic rewards.

    Args:
        logits: list of `num_timesteps` batches, each of size
            `batch_size * num_classes`: logits for distribution over actions
            at each timestep
        sampled_outputs: list of `num_timesteps` batches, each of size
            `batch_size`: ints describing sampled action at each timestep
            for each example
        rewards: float batch `batch_size` describing episodic reward per
            example
        lengths: `None` or a `batch_size` int batch describing length of each
            sequence. If `None`, then all sequences are assumed to have the
            same length `num_timesteps`
        baseline_fn:
        params:

    Return:
        updates: list of (gradient, param) update tuples
    """

    if params is None:
        params = tf.trainable_variables()

    batch_size = tf.shape(logits[0])[0]
    num_classes = tf.shape(logits[0])[1]
    num_timesteps = len(logits)

    # Baseline empirical rewards
    rewards = baseline_fn(rewards)

    # Feed logits through log softmax.
    log_softmaxes = [tf.nn.log_softmax(logits_t) for logits_t in logits]

    # Fetch p(sampled_output) for each timestep.
    # This is a bit awkward -- need to pick a single element out of each
    # example softmax vector.
    # Output is (batch_size, 1) per timestep
    flat_softmaxes = [tf.reshape(log_softmax_t, (-1,))
                      for log_softmax_t in log_softmaxes]
    lookup_offset = tf.range(batch_size) * num_classes
    log_p_sampled = [tf.gather(log_softmax_t, tf.to_int32(sampled_outputs_t) + lookup_offset)
                     for log_softmax_t, sampled_outputs_t
                     in zip(flat_softmaxes, sampled_outputs)]

    # Merge into single (batch_size, num_timesteps) batch
    log_p_sampled = [tf.expand_dims(log_p_sampled_t, 1)
                     for log_p_sampled_t in log_p_sampled]
    log_p_sampled = tf.concat(1, log_p_sampled)

    if lengths is not None:
        # For each example, zero out probabilities after i > example_length
        mask = tf.tile(tf.expand_dims(tf.range(num_timesteps), 0), (batch_size, 1))
        mask = tf.to_float(tf.greater(tf.expand_dims(lengths, 1), mask))
        log_p_sampled *= mask

    # Calculate p(sampled_output) by chain rule. We can merge these ahead of
    # time, since we only have a single episode-level reward.
    # (batch_size,) batch
    log_p_sampled = tf.reduce_sum(log_p_sampled, 1)

    # Main REINFORCE gradient equation.
    # Apply rewards on batch + mean beforehand for efficiency.
    log_p_sampled *= -1 * rewards
    log_p_sampled = tf.reduce_mean(log_p_sampled)
    gradients = tf.gradients(log_p_sampled, params)

    return zip(gradients, params)


'''
Render graph on Crime Tab only
'''

import pandas as pd
import plotly.express as px
from crimes_app.dashboard.maindash import app
from dash import html, dcc
from dash.dependencies import Input, Output
import os

BASE_DIR = os.path.join( os.path.dirname( __file__ ), '..' )
TEST_DATA_DIR = os.path.join(BASE_DIR, "data")

# Read csv
merged_filename = os.path.join(TEST_DATA_DIR, "merged_data.csv")
crimes = pd.read_csv(merged_filename)

# Subset WARD and crime-type columns
crimes = crimes.loc[:, 'WARD': 'WEAPONS VIOLATION']
crimes.drop(['FS AGENCIES', 'SB FUNDS', 'MICRO LOANS'],
            axis = 1,
            inplace = True)

# Remove WARD column
col_names = crimes.columns[1:]

# ---------------------------- FRONT-END -------------------------------
# App layout for Crime tab

crime_layout = html.Div([
        html.H2("Crime Data", style = {'text-align': 'left'}),
        html.Br(),
        html.Label(['Select Type of Crime:'], style={'font-weight': 'bold',
                                                     "text-align": "left"}),
        html.Br(),
        dcc.Dropdown(id = "select_crimetype",
                    options = [{"label": str(x), "value": x} for x in col_names],
                    value = col_names[0],
                    multi = False,
                    style = {'width': "40%"}),
        dcc.Graph(id = 'bar_graph_crime_type_by_ward', figure = {})
    ])

# --------------------------- BACKEND -----------------------------------
# Connect the figure contained in Dash Core Components with the frontend

@app.callback(Output(component_id = 'bar_graph_crime_type_by_ward', component_property = 'figure'),
            [Input(component_id = 'select_crimetype', component_property = 'value')])

def update_graph(crime_slctd):
    '''
    Render the graph on the Crime tab based on
    based on user's input value in Dropdown component
    'select_crimetype'. This returned figure
    feeds into the crime_layout variable which
    stores the frontend for Crime tab.
    Input:
        crime_slctd - value selected by user in the dropdown component
    '''
    crimes_copy = crimes.copy()
    crimes_copy.dropna(subset=[crime_slctd], inplace = True)
    graphtitle = crime_slctd.lower().capitalize() + ' crimes disaggregated at ward-level'
    fig = px.bar(crimes_copy, x = 'WARD',
                            y = crime_slctd,
                            title = graphtitle)
    fig.layout.plot_bgcolor = '#f3f3f0'

    return fig


from rest_framework.routers import DefaultRouter
from django.urls import (
    path,
    include,
)

from event import views

router = DefaultRouter()
router.register('events', views.EventViewSet, basename='events')

app_name = 'event'

urlpatterns = [
    path('', include(router.urls)),
]


from pydantic import PydanticValueError

class ApiError(ValueError):
    code: int = 400

    def __init__(self, message: str, code: int = 400):
        super().__init__(message)
        self.code = code

class ValidationError(PydanticValueError):
    msg_template = "Validation error"

    def __init__(self, *args, **kwargs):
        super().__init__(**kwargs)


import sys
import numpy as np
import importlib
from configobj import ConfigObj
from TackleBox import Set_Bait, Fish, CovRenorm
from ioutils import CosmoResults, InputData, write_fisher
from scipy.linalg.lapack import dgesv

if __name__ == "__main__":

    # Read in the config file
    configfile = sys.argv[1]
    pardict = ConfigObj(configfile)

    # Read in the file containing the redshift bins, nz and bias values
    data = InputData(pardict)

    # Set up the linear power spectrum and derived parameters based on the input cosmology
    cosmo = CosmoResults(pardict, data.zmin, data.zmax)
    if np.any(data.volume > 0):
        cosmo.volume = data.volume * 1.0e9

    # Convert the nz to nbar in (h/Mpc)^3
    data.convert_nbar(cosmo.volume, float(pardict["skyarea"]))

    # Scales the bias so that it goes as b/G(z)
    if pardict.as_bool("scale_bias"):
        data.scale_bias(cosmo.growth)
    print("#  Data nbar")
    print(data.nbar)
    print("#  Data bias")
    print(data.bias)

    # Precompute some things we might need for the Fisher matrix
    recon, derPalpha, derPalpha_BAO_only = Set_Bait(cosmo, data, BAO_only=pardict.as_bool("BAO_only"))
    print("#  Data recon factor")
    print(recon)

    # Loop over redshifts and compute the Fisher matrix and output the 3x3 matrix
    identity = np.eye(len(data.nbar) + 3)
    print(
        "#  z  V(Gpc/h)^3  fsigma8  fsigma8_err(%)  Da(Mpc/h)  Da_err(%)  H(km/s/Mpc)  H_err(%)   alpha_err(%)"
    )
    erralpha = np.zeros(len(cosmo.z))
    FullCatch = np.zeros((len(cosmo.z) * len(data.nbar) + 3, len(cosmo.z) * len(data.nbar) + 3))
    for iz in range(len(cosmo.z)):

        if np.any(data.nz[:, iz] > 1.0e-30):

            Catch = Fish(
                cosmo,
                cosmo.kmin,
                cosmo.kmax,
                data,
                iz,
                recon[iz],
                derPalpha,
                pardict.as_bool("BAO_only"),
                pardict.as_bool("GoFast"),
            )
            # print(Catch)

            # Add on BAO only information from kmax to k = 0.5 Mpc/h but only for alpha_perp and alpha_par
            ExtraCatch = Fish(
                cosmo,
                cosmo.kmax,
                0.5,
                data,
                iz,
                recon[iz],
                derPalpha_BAO_only,
                True,
                pardict.as_bool("GoFast"),
            )
            Catch[-2:, -2:] += ExtraCatch[-2:, -2:]

            # Add the Fisher matrix to the full fisher matrix
            FullCatch[
                iz * len(data.nbar) : (iz + 1) * len(data.nbar),
                iz * len(data.nbar) : (iz + 1) * len(data.nbar),
            ] += Catch[: len(data.nbar), : len(data.nbar)]
            FullCatch[iz * len(data.nbar) : (iz + 1) * len(data.nbar), -3:,] += Catch[: len(data.nbar), -3:]
            FullCatch[-3:, iz * len(data.nbar) : (iz + 1) * len(data.nbar)] += Catch[-3:, : len(data.nbar)]
            FullCatch[-3:, -3:] += Catch[-3:, -3:]

            # Invert the Fisher matrix to get the parameter covariance matrix
            cov = dgesv(Catch, identity)[2]

            # Compute the error on isotropic alpha also
            J = np.array([2.0 / 3.0, 1.0 / 3.0])
            erralpha[iz] = 100.0 * np.sqrt(J @ cov[-2:, -2:] @ J.T)

            # Renormalise the covariance from fsigma8, alpha_perp, alpha_par to fsigma8, Da, H
            means = np.array([cosmo.f[iz] * cosmo.sigma8[iz], cosmo.da[iz], cosmo.h[iz]])
            cov_renorm = CovRenorm(cov, means)

            # Print the parameter means and errors
            errs = 100.0 * np.sqrt(np.diag(cov_renorm)[-3:]) / means
            print(
                " {0:.2f}    {1:.4f}     {2:.3f}       {3:.2f}         {4:.1f}       {5:.2f}        {6:.1f}       {7:.2f}       {8:.3f}".format(
                    cosmo.z[iz],
                    cosmo.volume[iz] / 1e9,
                    means[0],
                    errs[0],
                    means[1],
                    errs[1],
                    means[2],
                    errs[2],
                    erralpha[iz],
                )
            )

            # Output the fisher matrix for the redshift bin
            write_fisher(pardict, cov_renorm, cosmo.z[iz], means)

        else:
            erralpha[iz] = 1.0e30
            print(
                " {0:.2f}     {1:.4f}    {2:.3f}         -          {4:.1f}         -         {6:.1f}         -          -".format(
                    cosmo.z[iz],
                    cosmo.volume[iz] / 1e9,
                    means[0],
                    errs[0],
                    means[1],
                    errs[1],
                    means[2],
                    errs[2],
                    erralpha[iz],
                )
            )

    # Run the cosmological parameters at the centre of the combined redshift bin
    identity = np.eye(len(cosmo.z) * len(data.nbar) + 3)
    cosmo = CosmoResults(pardict, np.atleast_1d(data.zmin[0]), np.atleast_1d(data.zmax[-1]))

    # Invert the Combined Fisher matrix to get the parameter
    # covariance matrix and compute means and errors
    cov = dgesv(FullCatch, identity)[2]
    J = np.array([2.0 / 3.0, 1.0 / 3.0])
    erralpha = 100.0 * np.sqrt(J @ cov[-2:, -2:] @ J.T)
    means = np.array([cosmo.f[0] * cosmo.sigma8[0], cosmo.da[0], cosmo.h[0]])
    cov_renorm = CovRenorm(cov, means)
    errs = 100.0 * np.sqrt(np.diag(cov_renorm)[-3:]) / means
    print("#  Combined errors")
    print("#=================")
    print(
        " {0:.2f}    {1:.4f}     {2:.3f}       {3:.2f}         {4:.1f}       {5:.2f}        {6:.1f}       {7:.2f}       {8:.3f}".format(
            cosmo.z[0],
            cosmo.volume[0] / 1e9,
            means[0],
            errs[0],
            means[1],
            errs[1],
            means[2],
            errs[2],
            erralpha,
        )
    )


"""
题目：输入一颗二叉树和一个整数，打印出二叉树中结点值的和为输入整数的所有路径。
路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。
"""
from sword_offer.binary_serach_tree import BinarySearchTree, Node

class Solution:
    # 返回二维列表，内部每个列表表示找到的路径
    def FindPath(self, root, expectNumber):
        if not root:
            return []
        self.res = []
        self.exp = expectNumber
        self.find_one(root, [], 0)
        return self.res

    def find_one(self, root, path, sum):
        sum += root.val
        path.append(root.val)
        if sum == self.exp and not root.left and not root.right:
            self.res.append(path[:])  # 由于共用path，所以需要添加path的副本到结果列表中
        elif sum < self.exp:
            if root.left:
                self.find_one(root.left, path, sum)
            if root.right:
                self.find_one(root.right, path, sum)
        path.pop()

class S(object):
    def main(self, root, num):
        if not root:
            return []
        self.res = list()
        self.get_path([], root, num)
        return self.res

    def get_path(self, path, root, num):
        path.append(root.val)
        num -= root.val
        if num == 0 and not root.left and not root.right:
            self.res.append(path[:])
        elif num > 0:
            if root.left:
                self.get_path(path, root.left, num)
            if root.right:
                self.get_path(path, root.right, num)
        path.pop()

class S2(object):
    def find(self, root, expectNumber):
        if not root:
            return []
        self.res = list()
        self.get_path([], root, expectNumber)
        return self.res

    def get_path(self, path, root, num):
        if not root:
            return
        num -= root.val
        path.append(root.val)
        if num == 0 and not root.left and not root.right:
            self.res.append(path[:])
        elif num > 0:
            if root.left:
                self.get_path(path, root.left, num)
            if root.right:
                self.get_path(path, root.right, num)
        path.pop()

if __name__ == '__main__':
    bst = BinarySearchTree()
    for i in range(10):
        bst.add(i)
    for i in range(50):
        s = Solution()
        print(i, s.FindPath(bst.root, i))
        s2 = S()
        print(i, s2.main(bst.root, i))

剑指Offer/A23_二叉树中和为某一值的路径.py

A23_二叉树中和为某一值的路径.py


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 29 18:33:36 2021

@author: peter
"""
from pathlib import Path

from vsd_cancer.functions import cancer_functions as canf

import numpy as np
import pandas as pd
import scipy.ndimage as ndimage
import matplotlib.pyplot as plt
import f.plotting_functions as pf

import matplotlib.cm
import matplotlib.gridspec as gridspec
import matplotlib as mpl

import seaborn as sns

import datetime

def make_figures(initial_df, save_dir, figure_dir, filetype=".png"):
    figsave = Path(figure_dir, "231_figure")
    if not figsave.is_dir():
        figsave.mkdir()

    trial_string_use = "cancer_20201207_slip1_area1_long_acq_corr_corr_long_acqu_blue_0.03465_green_0.07063_heated_to_37_1"

    df = pd.read_csv(initial_df)

    if True:
        num_traces = 15
        sep = 25
        make_example_trace_fig(
            trial_string_use, num_traces, sep, df, save_dir, figsave, filetype
        )
        # make example for synchrony fig
        make_example_trace_fig(
            trial_string_use,
            15,
            sep,
            df,
            save_dir,
            Path(figure_dir, "wave_figure"),
            filetype,
        )

        plot_positive_negative_events(save_dir, figsave, filetype)

    plot_percent_quiet(save_dir, figsave, filetype)

def plot_percent_quiet(save_dir, figsave, filetype):

    df = pd.read_csv(Path(save_dir, "non_ttx_active_df_by_cell.csv"))
    df2 = pd.read_csv(Path(save_dir, "TTX_active_df_by_cell.csv"))

    df2 = df2[df2.stage == "pre"]

    df = pd.concat([df, df2])

    T = 0.2

    df["exp_stage"] = df.expt + "_" + df.stage
    df["day_slip"] = df.day.astype(str) + "_" + df.slip.astype(str)

    df["event_rate"] = (df["n_neg_events"] + df["n_pos_events"]) / (
        df["obs_length"] * T
    )
    df["neg_event_rate"] = (df["n_neg_events"]) / (df["obs_length"] * T)

    df["integ_rate"] = (df["integrated_events"]) / (df["obs_length"] * T)
    df["neg_integ_rate"] = -1 * (df["neg_integrated_events"]) / (df["obs_length"] * T)

    mda = df[
        [x in ["standard", "TTX_10um", "TTX_10um_washout", "TTX_1um"] for x in df.expt]
    ][["neg_event_rate", "day_slip"]]

    mda["active"] = 100 * (mda["neg_event_rate"] > 0).astype(int)

    active_d = mda[mda.active != 0]
    active_rate = active_d.groupby("day_slip").mean()["neg_event_rate"].to_numpy()

    active = mda.groupby("day_slip").mean()["active"].to_numpy()

    with open(Path(figsave, "event_rate_info.txt"), "w") as f:
        f.write(f"{datetime.datetime.now()}\n")
        f.write(f"Mean per coverslip active cell rate: {np.mean(active_rate)}")
        f.write(f", SEM: {np.std(active_rate)/np.sqrt(len(active_rate))}\n")
        f.write(f"n = {len(active_rate)}\n")

    fig, ax4 = plt.subplots()
    # sns.violinplot(y=active,saturation = 0.5)
    # ax4.plot(np.random.normal(loc = 1,scale = scale,size = sens.shape[0]),sens[:,-1],'.k',markersize = 12)
    sns.swarmplot(y=active, ax=ax4, color="k", size=7)
    ax4.xaxis.set_visible(False)
    ax4.set_ylabel("Active Cells (%)")
    pf.set_thickaxes(ax4, 3, remove=["top", "right", "bottom"])
    pf.set_all_fontsize(ax4, 16)
    pf.set_all_fontsize(ax4, 16)

    pf.make_square_plot(ax4)
    fig.savefig(
        Path(figsave, f"231_active_cells{filetype}"),
        bbox_inches="tight",
        dpi=300,
        transparent=True,
    )
    figdata_name = "/home/peter/Dropbox/Papers/cancer/v2/figure_data/" + "fig_3_"
    np.savetxt(figdata_name + f"_3C_y.txt", active)

    fig, ax = plt.subplots()
    # sns.violinplot(y=active,saturation = 0.5)
    # ax4.plot(np.random.normal(loc = 1,scale = scale,size = sens.shape[0]),sens[:,-1],'.k',markersize = 12)
    sns.swarmplot(y=active_rate * 10**3, ax=ax, color="k", size=7)
    ax.xaxis.set_visible(False)
    ax.set_ylabel("Mean event rate\n(active cells, x10$^3$ s$^{-1}$)")
    pf.set_thickaxes(ax, 3, remove=["top", "right", "bottom"])
    pf.set_all_fontsize(ax, 16)
    pf.set_all_fontsize(ax, 16)

    pf.make_square_plot(ax)
    fig.savefig(
        Path(figsave, f"231_active_rates{filetype}"),
        bbox_inches="tight",
        dpi=300,
        transparent=True,
    )

    fig1, ax1 = plt.subplots()
    ax1.hist(active_d.neg_event_rate * 1000, bins=10, log=True, color=(0.2, 0.2, 0.2))
    ax1.set_xlabel("Event rate (active cells, x10$^3$ s$^{-1}$)")
    ax1.set_ylabel("Number of cells")
    pf.set_thickaxes(ax1, 3)
    pf.set_all_fontsize(ax1, 16)
    pf.set_all_fontsize(ax1, 16)
    fig1.savefig(
        Path(figsave, f"231_active_hists{filetype}"),
        bbox_inches="tight",
        dpi=300,
        transparent=True,
    )

    figdata_name = "/home/peter/Dropbox/Papers/cancer/v2/figure_data/" + "fig_3_"
    np.savetxt(figdata_name + f"_3D_y.txt", active_d.neg_event_rate * 1000)

def make_example_trace_fig(
    trial_string_use, num_traces, sep, df, save_dir, figsave, filetype
):
    T = 0.2

    im_scalebar_length_um = 100

    for idx, data in enumerate(df.itertuples()):
        trial_string = data.trial_string
        # print(trial_string)
        trial_save = Path(save_dir, "ratio_stacks", trial_string)

        if trial_string != trial_string_use:
            continue
        else:
            break

    im = np.load(Path(trial_save, f"{trial_string}_im.npy"))
    seg = np.load(Path(trial_save, f"{trial_string}_seg.npy"))

    masks = canf.lab2masks(seg)

    tcs = np.load(Path(trial_save, f"{trial_string}_all_tcs.npy"))

    event_dict = np.load(
        Path(trial_save, f"{trial_string}_event_properties.npy"), allow_pickle=True
    ).item()

    idx = 0
    events = event_dict["events"][idx]

    keep = [x for x in np.arange(tcs.shape[0])]

    # sort by event amounts
    sort_order = np.array(
        [
            np.sum(np.abs(events["event_props"][x][:, -1])) if x in events.keys() else 0
            for x in range(tcs.shape[0])
        ]
    )

    tcs = tcs[keep, :]
    masks = masks[keep, ...]
    sort_order = np.argsort(sort_order[keep])[::-1]

    tcs = tcs[sort_order, :]
    masks = masks[sort_order, :]
    so = np.array(keep)[sort_order]

    tcs = tcs[:num_traces, ...]
    so = so[:num_traces]
    masks = masks[:num_traces, ...]

    # now sort back in position
    llocs = np.array([ndimage.measurements.center_of_mass(x) for x in masks])
    llocs = llocs[:, 0] * masks.shape[-2] + llocs[:, 1]
    order2 = np.argsort(llocs)[::-1]

    tcs = tcs[order2, ...]
    so = so[order2, ...]
    masks = masks[order2, ...]

    tc_filt = ndimage.gaussian_filter(
        tcs, (0, 3)
    )  # np.array([prox_tv.tv1_1d(t,0.01) for t in tcs])

    cmap = matplotlib.cm.tab20

    with open(Path(figsave, "cell_ids.txt"), "w") as f:
        f.write(f"{trial_string}\n")
        for s in so:
            f.write(f"{s}\n")

    fig = plt.figure(constrained_layout=True)
    gs = fig.add_gridspec(2, 5)
    ax = fig.add_subplot(gs[:, -2:])
    colors = []
    for i in range(num_traces):
        ax.plot([0, tcs.shape[-1] * T], np.ones(2) * i * 100 / sep, "k", alpha=0.5)
        line = ax.plot(
            np.arange(tcs.shape[-1]) * T,
            (tcs[i] - 1) * 100 + i * 100 / sep,
            color=cmap(i / num_traces),
        )
        _ = ax.plot(
            np.arange(tcs.shape[-1]) * T,
            (tc_filt[i] - 1) * 100 + i * 100 / sep,
            color="k",
        )
        colors.append(line[0].get_c())
        ev = events[so[i]]
        ax.text(
            -10,
            (i - 0.15) * 100 / sep,
            f"{i}",
            fontdict={"fontsize": 14},
            color=colors[i],
            ha="right",
            va="center",
        )
        if False:
            for l in ev.T:
                ax.fill_betweenx(
                    [(i - 0.5 * 0.9) * 100 / sep, (i + 0.5 * 0.9) * 100 / sep],
                    l[0] * T,
                    l[1] * T,
                    facecolor="r",
                    alpha=0.5,
                )

    plt.axis("off")
    pf.plot_scalebar(ax, 0, (tcs[:num_traces].min() - 1) * 100, 200, 3, thickness=3)

    figdata_name = "/home/peter/Dropbox/Papers/cancer/v2/figure_data/" + "fig_3_"
    np.savetxt(figdata_name + f"_3A_x.txt", np.arange(tcs.shape[-1]) * T)
    np.savetxt(figdata_name + f"_3A_y.txt", (tcs - 1) * 100)
    np.savetxt(figdata_name + f"_3A_y_filtered.txt", (tc_filt - 1) * 100)

    colors = (np.array(colors) * 255).astype(np.uint8)
    # colors = np.hstack([colors,np.ones((colors.shape[0],1))])

    over = masks[:num_traces]
    struct = np.zeros((3, 3, 3))
    struct[1, ...] = 1
    over = np.logical_xor(
        ndimage.binary_dilation(over, structure=struct, iterations=3), over
    ).astype(int)
    over = np.sum(over[..., None] * colors[:, None, None, :], 0).astype(np.uint8)
    length = int(im_scalebar_length_um / 1.04)

    over[-20:-15, -length - 10 : -10] = np.ones(4, dtype=np.uint8) * 255

    ax1 = fig.add_subplot(gs[:, :-2])
    ax1.imshow(im, cmap="Greys_r")
    ax1.imshow(over)
    plt.axis("off")
    pf.label_roi_centroids(
        ax1, masks[:num_traces, ...], colors / 255, fontdict={"fontsize": 0}
    )

    fig.savefig(
        Path(figsave, f"example_tcs{filetype}"),
        bbox_inches="tight",
        dpi=300,
        transparent=True,
    )

def plot_positive_negative_events(save_dir, figsave, filetype):

    df = pd.read_csv(Path(save_dir, "all_events_df.csv"))
    df["exp_stage"] = df.expt + "_" + df.stage

    use = ["TTX_10um_washout_pre", "TTX_1um_pre", "TTX_10um_pre", "standard_none"]

    fig1 = plot_events(df, use, log=True)
    fig1.savefig(
        Path(figsave, f"231_histograms_log{filetype}"),
        bbox_inches="tight",
        dpi=300,
        transparent=True,
    )

    fig2 = plot_events(df, use, log=False)
    fig2.savefig(
        Path(figsave, f"231_histograms_no_log{filetype}"),
        bbox_inches="tight",
        dpi=300,
        transparent=True,
    )

    fig3 = plot_events2(df, use, log=True)
    fig3.savefig(
        Path(figsave, f"231_histograms_log_both_pos{filetype}"),
        bbox_inches="tight",
        dpi=300,
        transparent=True,
    )

    fig4 = plot_events2(df, use, log=False)
    fig4.savefig(
        Path(figsave, f"231_histograms_no_log_both_pos{filetype}"),
        bbox_inches="tight",
        dpi=300,
        transparent=True,
    )

    get_example_event_locs(df, use, log=True)

def get_example_event_locs(
    df, use, log=True, upper_lim=6.6, lower_lim=0, T=0.2, nbins=50, only_neg=True
):
    dfn = df.copy()

    use_bool = np.array([np.any(x in use) for x in dfn.exp_stage])
    dfn = dfn[use_bool]

    too_big = np.abs(dfn.event_amplitude) > 6.6 / 100
    too_small = np.abs(dfn.event_amplitude) < 0 / 100
    dfn = dfn[np.logical_not(np.logical_or(too_big, too_small))]

    length_bins = np.histogram(dfn["event_length"] * T, bins=nbins)[1]

    amp_bins = np.histogram(dfn["event_amplitude"] * 100, bins=nbins)[1]

    with open(
        Path(
            Path("/home/peter/Dropbox/Papers/cancer/v2/"), "231_figure", "cell_ids.txt"
        )
    ) as f:
        dat = f.readlines()

    trial_string = dat[0].strip()
    cids = dat[1:]
    cids = [int(x.strip()) for x in cids]

    dfn = dfn[dfn.trial_string == trial_string]

    trial_save = Path(
        Path("/home/peter/data/Firefly/cancer/analysis/full"),
        "ratio_stacks",
        trial_string,
    )
    tcs = np.load(Path(trial_save, f"{trial_string}_all_tcs.npy"))

    tcs = ndimage.gaussian_filter(tcs, (0, 3))

    # now for each trace display where the things are binned
    for c in cids:
        fig, ax = plt.subplots(nrows=2)
        ax[1].hist2d(
            dfn[dfn.cell_id == f"{trial_string}_cell_{c}"].event_amplitude * 100,
            dfn[dfn.cell_id == f"{trial_string}_cell_{c}"].event_length * T,
            bins=(amp_bins, length_bins),
            norm=mpl.colors.LogNorm(),
        )
        ax[0].plot(np.arange(tcs.shape[1]) * T, tcs[c, :])
        for data in dfn[dfn.cell_id == f"{trial_string}_cell_{c}"].itertuples():
            ax[0].plot(data.event_time * T, tcs[c, :].max(), ".r")

        ax[1].set_xlabel(f"{trial_string}_cell_{c}")
        fig.savefig(
            Path(
                "/home/peter/Dropbox/Papers/cancer/v2/",
                "231_figure/example_trace_bins",
                f"bins_cell_{c}.png",
            )
        )

        dfn[dfn.cell_id == f"{trial_string}_cell_{c}"].to_csv(
            Path(
                "/home/peter/Dropbox/Papers/cancer/v2/",
                "231_figure/example_trace_bins",
                f"df_cell_{c}.csv",
            )
        )

def plot_events(
    df, use, log=True, upper_lim=6.6, lower_lim=0, T=0.2, nbins=50, only_neg=True
):

    dfn = df.copy()

    use_bool = np.array([np.any(x in use) for x in dfn.exp_stage])
    dfn = dfn[use_bool]

    too_big = np.abs(dfn.event_amplitude) > 6.6 / 100
    too_small = np.abs(dfn.event_amplitude) < 0 / 100
    dfn = dfn[np.logical_not(np.logical_or(too_big, too_small))]

    length_bins = np.histogram(dfn["event_length"] * T, bins=nbins)[1]

    amp_bins = np.histogram(np.abs(dfn["event_amplitude"]) * 100, bins=nbins)[1]

    neg = dfn[dfn.event_amplitude < 0]
    pos = dfn[dfn.event_amplitude > 0]

    gs = gridspec.GridSpec(2, 2)
    gs.update(wspace=0.3, hspace=0.3)

    fig, axarr = plt.subplots(figsize=(8, 6))

    ax0 = plt.subplot(gs[0])
    ax0.hist(np.abs(neg["event_amplitude"]) * 100, bins=amp_bins, log=log, label="-ve")
    ax0.hist(pos["event_amplitude"] * 100, bins=amp_bins, log=log, label="+ve")
    ax0.set_xlabel("Absolute event amplitude (% $\Delta$R/R$_0$)")
    ax0.set_ylabel("Observed Frequency")
    ax0.legend(frameon=False)

    ax1 = plt.subplot(gs[1])
    ax1.hist(np.abs(neg["event_length"]) * T, bins=length_bins, log=log, label="-ve")
    ax1.hist(pos["event_length"] * T, bins=length_bins, log=log, label="+ve")
    ax1.set_xlabel("Event length (s)")
    ax1.set_ylabel("Observed Frequency")
    ax1.legend(frameon=False)

    if log:
        norm = mpl.colors.LogNorm()
    else:
        norm = None

    ax2 = plt.subplot(gs[2])
    hh = ax2.hist2d(
        np.abs(neg["event_amplitude"]) * 100,
        neg["event_length"] * T,
        bins=(amp_bins, length_bins),
        norm=norm,
    )
    plt.colorbar(hh[3])

    ax2.set_xlabel("Negative event amplitude (% $\Delta$R/R$_0$)")
    ax2.set_ylabel("Event length (s)")

    ax3 = plt.subplot(gs[3])

    hh2 = ax3.hist2d(
        np.abs(pos["event_amplitude"]) * 100,
        pos["event_length"] * T,
        bins=(amp_bins, length_bins),
        norm=norm,
    )
    plt.colorbar(hh2[3])
    ax3.set_xlabel("Positive event size (% $\Delta$R/R$_0$)")
    ax3.set_ylabel("Event length (s)")

    return fig

def plot_events2(
    df, use, log=True, upper_lim=6.6, lower_lim=0, T=0.2, nbins=50, only_neg=True
):

    dfn = df.copy()

    use_bool = np.array([np.any(x in use) for x in dfn.exp_stage])
    dfn = dfn[use_bool]

    too_big = np.abs(dfn.event_amplitude) > 6.6 / 100
    too_small = np.abs(dfn.event_amplitude) < 0 / 100
    dfn = dfn[np.logical_not(np.logical_or(too_big, too_small))]

    length_bins = np.histogram(dfn["event_length"] * T, bins=nbins)[1]

    amp_bins = np.histogram(dfn["event_amplitude"] * 100, bins=nbins)[1]

    neg = dfn[dfn.event_amplitude < 0]
    pos = dfn[dfn.event_amplitude > 0]

    gs = gridspec.GridSpec(2, 2)
    gs.update(wspace=0.3, hspace=0.3)

    fig, axarr = plt.subplots(figsize=(8, 6))

    ax0 = plt.subplot(gs[0])
    ax0.hist(neg["event_amplitude"] * 100, bins=amp_bins, log=log, label="-ve")
    ax0.hist(pos["event_amplitude"] * 100, bins=amp_bins, log=log, label="+ve")
    ax0.set_xlabel("Event amplitude (% $\Delta$R/R$_0$)")
    ax0.set_ylabel("Observed Frequency")
    ax0.legend(frameon=False)

    ax1 = plt.subplot(gs[1])
    ax1.hist(np.abs(neg["event_length"]) * T, bins=length_bins, log=log, label="-ve")
    ax1.hist(pos["event_length"] * T, bins=length_bins, log=log, label="+ve")
    ax1.set_xlabel("Event length (s)")
    ax1.set_ylabel("Observed Frequency")
    ax1.legend(frameon=False)

    if log:
        norm = mpl.colors.LogNorm()
    else:
        norm = None

    ax2 = plt.subplot(gs[2])
    hh = ax2.hist2d(
        dfn["event_amplitude"] * 100,
        dfn["event_length"] * T,
        bins=(amp_bins, length_bins),
        norm=norm,
    )
    plt.colorbar(hh[3])
    ax2.set_xlabel("Event amplitude (% $\Delta$R/R$_0$)")
    ax2.set_ylabel("Event length (s)")

    figdata_name = "/home/peter/Dropbox/Papers/cancer/v2/figure_data/" + "fig_3_"
    save = dfn[["event_amplitude", "event_length"]].copy()
    save["event_amplitude"] *= 100
    save["event_length"] *= T
    save.to_csv(figdata_name + "fig_3B_data.csv")

    # get number of events before/after TTX
    thresh = 2
    iid = np.argwhere(hh[1] < -thresh)[-1][0]
    iid2 = np.argwhere(hh[1] > thresh)[0][0]
    n_events_neg = np.sum(hh[0][:iid, :])
    n_events_pos = np.sum(hh[0][iid2:, :])

    with open(
        Path(
            "/home/peter/Dropbox/Papers/cancer/v2/231_figure", "num_negpos_events.txt"
        ),
        "w",
    ) as f:
        f.write(f"{datetime.datetime.now()}\n")
        f.write(f"Number events in bins up to edge at {hh[1][iid]:.3f} %\n")
        f.write(f"neg: {n_events_neg} \n")
        f.write(f"Number events in bins from edge at {hh[1][iid2]:.3f} %\n")
        f.write(f"post: {n_events_pos} \n")

    thresh = 1.5
    iid = np.argwhere(hh[1] < -thresh)[-1][0]
    iid2 = np.argwhere(hh[1] > thresh)[0][0]
    n_events_neg = np.sum(hh[0][:iid, :])
    n_events_pos = np.sum(hh[0][iid2:, :])

    with open(
        Path(
            "/home/peter/Dropbox/Papers/cancer/v2/231_figure", "num_negpos_events.txt"
        ),
        "a",
    ) as f:
        f.write(f"{datetime.datetime.now()}\n")
        f.write(f"Number events in bins up to edge at {hh[1][iid]:.3f} %\n")
        f.write(f"neg: {n_events_neg} \n")
        f.write(f"Number events in bins from edge at {hh[1][iid2]:.3f} %\n")
        f.write(f"post: {n_events_pos} \n")

    return fig

if __name__ == "__main__":
    top_dir = Path("/home/peter/data/Firefly/cancer")
    save_dir = Path(top_dir, "analysis", "full")
    figure_dir = Path("/home/peter/Dropbox/Papers/cancer/v2/")
    initial_df = Path(top_dir, "analysis", "long_acqs_20210428_experiments_correct.csv")
    make_figures(initial_df, save_dir, figure_dir, filetype=".pdf")


from __future__ import unicode_literals
import frappe
from frappe.model.document import Document
from toolz.curried import groupby, merge, first, excepts

class BatchRecall(Document):
    def fetch_invoices(self):
        invoices = frappe.db.sql(
            """
                SELECT
                    si.name AS sales_invoice,
                    si.posting_date,
                    si.grand_total,
                    si.customer,
                    si.customer_name,
                    si.contact_email,
                    SUM(sii.qty) AS qty
                FROM `tabSales Invoice Item` AS sii
                LEFT JOIN  `tabSales Invoice` AS si ON
                    si.name = sii.parent
                WHERE si.docstatus = 1 AND sii.batch_no = %(batch_no)s
                GROUP BY si.name
            """,
            values={"batch_no": self.batch},
            as_dict=1,
        )
        self.invoices = []
        for invoice in invoices:
            self.append("invoices", invoice)

        self.no_of_invoices = len(invoices)
        self.no_of_customers = len(set([x.get("customer") for x in invoices]))
        self.total_qty_sold = sum([x.get("qty") for x in invoices])

    def send_emails(self):
        if not self.email_template:
            frappe.throw(frappe._("Email Template required to send emails"))

        subject, response = frappe.get_cached_value(
            "Email Template", self.email_template, ["subject", "response"]
        )
        get_first_details = excepts(StopIteration, first, lambda _: {})

        for contact_email, invoices in groupby(
            "contact_email", [x.as_dict() for x in self.invoices if x.contact_email]
        ).items():
            context = merge(
                get_first_details(invoices), {"batch": self.batch, "invoices": invoices}
            )

            _subject = frappe.render_template(subject, context)
            _message = frappe.render_template(response, context)
            frappe.sendmail(
                recipients=[contact_email],
                subject=_subject,
                message=_message,
                reference_doctype="Sales Invoice",
                reference_name=context.get("sales_invoice"),
            )


import pytz
import pandas as pd
import joblib,pickle

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from opti_recruit.similarity import cosine_recommendation, filter_params,get_list_dict
from opti_recruit.pipeline import Trainer
from opti_recruit.value_predict import prediction,value_show
from opti_recruit.data import get_api_data, get_data

PATH_TO_LOCAL_MODEL = 'model.joblib'

app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # Allows all origins
    allow_credentials=True,
    allow_methods=["*"],  # Allows all methods
    allow_headers=["*"],  # Allows all headers
)

dfs = get_api_data()
df22 = dfs[22]

with open("similarity_matrix_v3.pickle", 'rb') as file:
    sim_matrix = pickle.load(file)

@app.get("/")
def index():
    return {"greeting": "Hello world"}

@app.get("/similarities")
def compute_player_similarity(player_id, age_min=1, age_max=99, value_min=0, value_max=999999999, position=None):

    my_reco_df = cosine_recommendation(player_id, sim_matrix, df22)

    #transform reco_list into dataframe
    df_22_filtered = filter_params(df22, int(age_min), int(age_max), int(value_min), int(value_max), position)

    #filter my_reco_df with df_filtered
    boolean_series = my_reco_df.sofifa_id.isin(df_22_filtered)
    my_reco_filt = my_reco_df[boolean_series]
    # my_reco_list = cosine_recommendation(player_id, sim_matrix, df_22_filtered)

    return get_list_dict(my_reco_filt)

# @app.get("/value")
# def value_show(player_id):
#     predict_value=prediction()
#     res_value=predict_value.loc[int(player_id)]
#     return res_value

# @app.get("/predict/marketvalue")
# def predict_marketvalue(player_id):
#     trainer = Trainer()
#     trainer.load_model()
#     trainer.predict(df.loc[player_id])
#     return


# Generadores

def my_gen():
    a = 1
    yield a

    a = 2
    yield a

    a = 3
    yield a

my_first_gen = my_gen()

""" print(next(my_first_gen))
print(next(my_first_gen))
print(next(my_first_gen)) """

# Reto: Hacer un generador que muestre en pantalla los primeros 100 números pares.

def pairs():
    for i in range(1, 101):
        if i%2 == 0:
            yield i

firstHundredPairs = pairs()

for i in range(50):
    print(next(firstHundredPairs))


from django.core.management import BaseCommand

from users.models import CustomUser, Post, Comment

class Command(BaseCommand):

    def handle(self, *args, **options):
        a = Post.objects.all()
        b = CustomUser.objects.all()
        for i in a:#Smozhem zapolnit esli est posti
            # print(i.id)
            for ii in b.filter(id=i.id):#Chtobi lishnie perebori ne delat vstavlyaem id=i.id
                # print(ii.username)
                comment = Comment.objects.create(
                    comment_author=ii,#Chei comment
                    post=i,# K kakomu postu otnositsya
                    text='DataGrip is a database management environment for developers. It is designed to query, т'
                )
                comment.save()


"""Blocks table schema is defined here."""
from typing import Callable

from geoalchemy2 import Geometry
from sqlalchemy import Column, DateTime, Float, ForeignKey, Integer, Sequence, Table, func

from idu_balance_db.db import metadata

func: Callable

blocks_id_seq = Sequence("blocks_id_seq")

t_blocks = Table(
    "blocks",
    metadata,
    Column("id", Integer, primary_key=True, server_default=blocks_id_seq.next_value()),
    Column("city_id", ForeignKey("cities.id")),
    Column(
        "geometry",
        Geometry(srid=4326, spatial_index=False, from_text="ST_GeomFromEWKT", name="geometry", nullable=False),
        nullable=False,
    ),
    Column(
        "center",
        Geometry("POINT", 4326, spatial_index=False, from_text="ST_GeomFromEWKT", name="geometry", nullable=False),
        nullable=False,
    ),
    Column("population", Integer),
    Column("created_at", DateTime(True), nullable=False, server_default=func.now()),
    Column("updated_at", DateTime(True), nullable=False, server_default=func.now()),
    Column("municipality_id", ForeignKey("municipalities.id")),
    Column("administrative_unit_id", ForeignKey("administrative_units.id")),
    Column("area", Float(53)),
)
"""Blocks as a lower administrative division unit.

Columns:
- `id` - identitier, integer
- `city_id` - identifier of city, int
- `type_id` - identifier of administrative_unit_type, int
- `name` - name of administrative unit, varchar(50)
- `geometry` - geometry(4326)
- `center` - geometry(point, 4326)
- `population` - total population, integer
- `created_at` - time of insertion, DateTimeTz
- `updated_at` - time of last edit, DateTimeTz
- `municipality_id` - identifier of municipality as parent, int nullable
- `administrative_unit_id` - identifier of administrative unit parent (unused mostly), int nullable
- `area` - area of block, float
"""


from collections import Counter

def solution(str1, str2):
    answer = 0

    str1 = str1.lower()
    str2 = str2.lower()

    l1 = len(str1)
    l2 = len(str2)

    s1 = [str1[i]+str1[i+1] for i in range(l1-1) if (str1[i]+str1[i+1]).isalpha()]
    s2 = [str2[i]+str2[i+1] for i in range(l2-1) if (str2[i]+str2[i+1]).isalpha()]

    print(s1, s2)

    cs1 = Counter(s1)
    cs2 = Counter(s2)

    print(cs1, cs2)

    inter = list((cs1 & cs2).elements())
    union = list((cs1 | cs2).elements())

    print(inter, union)

    inter, union = len(inter), len(union)

    if not inter and not union:
        answer = 65536

    else:
        answer = int(inter/union*65536)

    return answer

solution("aa1+aa2", "AAAA12")

programmers/2018 KAKAO BLIND RECRUITMENT/[1차] 뉴스 클러스터링/cheat.py


# -*- coding:utf-8 -*-
# @TIME : 2021/3/2 17:27
# @Author : MYH
# @File : 04.豆瓣电影爬取.py
# @Software : PyCharm
# @需求 : 爬取豆瓣电影排行榜

import requests
import json

if __name__ == "__main__":
    url = 'https://movie.douban.com/j/chart/top_list'
    headers = {
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'
    }
    params = {
        'type': '24',
        'interval_id': '100:90',
        'action':'',
        'start': '0',# 从库中的第几部电影取
        'limit': '60',# 一次请求取出的个数
    }

    response = requests.get(url=url, params=params, headers=headers)
    list_data = response.json()

    fp = open('./douban.json', 'w', encoding='utf-8')
    json.dump(list_data, fp=fp, ensure_ascii=False)

    print('over.')

04.豆瓣电影爬取.py


from __future__ import annotations

from typing import Any, Optional, Dict, TypeVar

from fastapi.exceptions import HTTPException

_T = TypeVar("_T")

class HTTPExceptionWrapper(HTTPException):

    __status_code__: int = 400
    __detail__: str = 'error'
    __headers__: Optional[Dict[str, Any]] = None

    def __init__(
            self,
            status_code: int = None,
            detail: Any = None,
            headers: Optional[Dict[str, Any]] = None,
    ) -> None:

        status_code = status_code or self.__status_code__
        detail = detail or self.__detail__
        headers = headers or self.__headers__

        super().__init__(status_code=status_code, detail=detail, headers=headers)


import os
import json

my_path = os.path.abspath(os.path.dirname(__file__))

with open(os.path.join(my_path, 'static/data/mammamiadata.json')) as file:
    file = json.load(file)

df = file

pizzas = []
for i in df:
    if i['model'] == 'mammamiaapp.pizza':
        pizzas.append(i)
        print(i['fields']['nombre'])

print(pizzas)


"""
判断该范围内的正整数是不是回文数
回文数是指将一个正整数从左往右排列和从右往左排列值一样的数
知识点：1. 利用字符串切片来操作字符串

first Author and learning from:骆昊 https://github.com/ZiniuLu/Python-100-Days/blob/master/Day01-15/Day05/palindrome.py
Version:1.1
Author：袁亮亮
Date:2019-11-26
"""
begin = int(input("开始数字："))
finish = int(input("结束数字："))
for i in range(begin, finish + 1):
    number = str(i)
    number02 = number[::-1]
    if number == number02:
        print("%s 是回文数" % number)


import airsim
import numpy as np
from record_data import lidar_car_data as lidar
from record_data import camera_car_data as camera
from record_data import depth_camera_car_data as depth_vis
import pandas as pd
import time

# connect to the AirSim simulator
client = airsim.CarClient()
client.confirmConnection()

start_data = pd.read_csv("../start_data.csv")
position = airsim.Vector3r(start_data.X[0], start_data.Y[0] / 100, 0)
heading = airsim.utils.to_quaternion(0, 0, 0)
pose = airsim.Pose(position, heading)
client.simSetVehiclePose(pose, True)

# depth_vis

depthVisTest = depth_vis.DepthVisRecord(client)

# Camera

cameraTest = camera.CameraRecord(client)

# Lidar

lidarTest = lidar.LidarTest(client)

# record
is_start_record = True
if is_start_record:
    cameraTest.start_recording()
    lidarTest.start_recording()
    depthVisTest.start_recording()

while True:

    # print(np.degrees(airsim.to_eularian_angles(client.getImuData().orientation)))
    time.sleep(0.5)
    if is_start_record:
        cameraTest.record()
        # time.sleep(0.025)
        lidarTest.record()
        depthVisTest.record()
        # Необходимо!!! Иначе ошибки при записи


import sqlite3
from sqlite3 import Error

def create_connection(db_file):
    """ create a database connection to a SQLite database """
    conn = None
    try:
        conn = sqlite3.connect(db_file)
        print(sqlite3.version)
    except Error as e:
        print(e)

    return conn

def create_table(conn, create_table_sql):
    """ create a table from the create_table_sql statement
    :param conn: Connection object
    :param create_table_sql: a CREATE TABLE statement
    :return:
    """
    try:
        c = conn.cursor()
        c.execute(create_table_sql)
    except Error as e:
        print(e)

def select_rows(conn, whatsapp_number):

	sql_select = "SELECT count from users WHERE whatsapp_number = ?"

	cur = conn.cursor()

	cur.execute(sql_select, (whatsapp_number,))

	rows = cur.fetchall()
	print(rows, "rows")
	if not rows:
		rows = 0
	else:
		rows = rows[0][0]

	print(rows)

	return rows

def insert_rows(conn, whatsapp_number, count):

	sql_update = "UPDATE users SET count = ? WHERE whatsapp_number = ?"

	cur = conn.cursor()

	cur.execute(sql_update, (count+1, whatsapp_number,))
	conn.commit()

def create_row(conn, whatsapp_number, count):

	sql_create = "INSERT INTO users(whatsapp_number, count) VALUES(?, ?)"

	cur = conn.cursor()

	cur.execute(sql_create, (whatsapp_number,count,))
	conn.commit()

if __name__ == '__main__':

	database = r"user.db"

	conn = create_connection(r"user.db")

	create_table(conn, "CREATE TABLE IF NOT EXISTS users (whatsapp_number text, count integer ); ")


# -*- coding:utf-8 -*-
import json
from base64 import b64encode
from urllib2 import urlopen, Request
import requests
from contextlib import closing

from django.http import HttpResponse, HttpResponseRedirect

APP_ID = r"dj0zaiZpPTd4bGlNdFN0UGFPTyZkPVlXazlaV3QzWjFsdE56QW1jR285TUEtLSZzPWNvbnN1bWVyc2VjcmV0Jng9YTc-"
SECRET = r"754acc20690c6f6dc45d03261af20704521f825c"
REDIRECT_URI = r"http://localhost:7001/authorization"

def welcome(request, *args, **kwargs):
    """Authorization Endpoint."""
    url = ("https://auth.login.yahoo.co.jp/yconnect/v1/authorization?"
           "response_type=code&"
           "client_id=%s&"
           "state=foo&"
           "display=page&"
           "prompt=login&"
           "scope=openid+email&"
           "nonce=foo&"
           "redirect_uri=%s") % (APP_ID, REDIRECT_URI)
    return HttpResponseRedirect(url)

def authorization(request, *args, **kwargs):
    """When passed Token Endpoint, request UserInfo API."""
    ###########################
    # step1: Token Endpoint
    ###########################
    url = r"https://auth.login.yahoo.co.jp/yconnect/v1/token?"
    data = {
        "grant_type": "authorization_code",
        "code": request.GET["code"],
        "redirect_uri": REDIRECT_URI,
    }
    basic_token = b64encode("%s:%s" % (APP_ID, SECRET))
    headers = {
        "content-type": "application/x-www-form-urlencoded",
        "Authorization": "Basic " + basic_token,
    }
    response = requests.post(url, data=data, headers=headers)
    if response.status_code != 200:
        return HttpResponse(response.text)

    ###########################
    # step2: UserInfo API
    ###########################
    dct = json.loads(response.text)
    url = "https://userinfo.yahooapis.jp/yconnect/v1/attribute?schema=openid"
    response = requests.get(url, headers={
        "Authorization": "Bearer %s" % dct["access_token"]
    })
    return HttpResponse(response.text)


# main

import sys
from ast import arg
# sys.argv = argumentos en input, base_prefix = ruta arhcivo, byteorder = endian, excecutable = ruta python

from input_file_processor import get_content
from data_controller import get_data_content
from code_controller import get_code_content, get_labels, get_instructions
import instructions_opcodes

input_file_path = sys.argv[1]

available_assembly_instructions = instructions_opcodes.load_assembly_instructions("CPU - instructions_opcodes.tsv")
print(available_assembly_instructions)

file_content = get_content(input_file_path)
data = get_data_content(file_content)
code = get_code_content(file_content)
labels = get_labels(file_content)

for elem in code:
    instructions = get_instructions(elem, labels)
    if instructions is not None:
        if instructions == 'RET' or instructions == 'NOP':
            command = instructions
            params = ''
        else:
            command, params = instructions.split(' ')

        opcode = available_assembly_instructions[command][params]
        if command == 'RET' or command == 'POP':
            for elem in opcode:
                print(elem)
        else:
            print(opcode)

sys.exit()


import pytest

from libs.docusign import constants, exceptions

from apps.business.models import Matter

from .. import factories, models, services

def test_get_matter_initial_envelope(
    envelope: models.Envelope, another_envelope: models.Envelope
):
    """Check `get_matter_initial_envelope` service method.

    Only `initial` matter envelopes should be returned.

    """
    matter = envelope.matter
    factories.EnvelopeFactory(matter=matter, type=models.Envelope.TYPE_EXTRA)
    result = services.get_matter_initial_envelope(matter)
    assert result == envelope

def test_get_matter_initial_envelope_no_envelopes(another_matter: Matter):
    """Test `get_matter_initial_envelope` method when matter has no envelopes.

    Check when matter has no envelopes - nothing would be returned.

    """
    assert services.get_matter_initial_envelope(another_matter) is None

def test_get_matter_transition_name_by_envelope_no_initial_envelope(
    another_matter: Matter
):
    """Test `get_matter_transition_name_by_envelope` service method.

    Check when matter has no `initial` envelope -> original
    `transition_name` would be returned without any mapping transformations.

    """
    result = services.get_matter_transition_name_by_envelope(
        another_matter, 'test'
    )
    assert result == 'test'

@pytest.mark.parametrize(
    'envelope_status,result_transition', [
        (constants.ENVELOPE_STATUS_CREATED, 'make_draft'),
        (constants.ENVELOPE_STATUS_COMPLETED, 'activate'),
        (constants.ENVELOPE_STATUS_DELIVERED, 'pending'),
    ]
)
def test_get_matter_transition_name_by_envelope(
    envelope: models.Envelope, envelope_status: str, result_transition: str
):
    """Test `get_matter_transition_name_by_envelope` service method.

    Check when `get_matter_transition_name_by_envelope` method is called,
    it changes original transition name to another one according to envelope
    status.

    """
    envelope.status = envelope_status
    envelope.save()
    transition = services.get_matter_transition_name_by_envelope(
        envelope.matter, 'test'
    )
    assert transition == result_transition

def test_update_matter_status_from_envelope_not_initial(
    matter: Matter, mocker
):
    """Test `update_matter_status_from_envelope` service method.

    Check when `is_initial` flag is set to False on method call, no matter
    `status` updates are made.

    """
    save_matter = mocker.patch('apps.business.models.Matter.save')
    services.update_matter_status_from_envelope(matter, matter.status, False)
    save_matter.assert_not_called()

def test_update_matter_status_from_envelope_the_same_status(
    matter: Matter, mocker
):
    """Test `update_matter_status_from_envelope` service method.

    Check when matter status is not actually changed (should become the same
    as it used to be), no matter `status` updates are made.

    """
    save_matter = mocker.patch('apps.business.models.Matter.save')
    services.update_matter_status_from_envelope(matter, matter.status, True)
    save_matter.assert_not_called()

def test_update_matter_status_from_envelope(matter: Matter, mocker):
    """Test `update_matter_status_from_envelope` service method.

    Check when matter status is really changed and `is_initial` flag is set to
    True, matter `status` updates are performed.

    """
    matter.status = Matter.STATUS_OPEN
    matter.save()

    save_matter = mocker.patch('apps.business.models.Matter.save')
    services.update_matter_status_from_envelope(
        matter, Matter.STATUS_OPEN, True
    )
    save_matter.assert_called()

def test_clean_up_removed_in_docusign_envelopes_from_db_no_exceptions(
    envelope, mocker
):
    """Test `clean_up_removed_in_docusign_envelopes_from_db` exceptions.

    Check method behavior when no exception is raised - no envelope deletion
    should be performed.

    """
    mocker.patch(
        'apps.esign.adapters.DocuSignAdapter.__init__',
        return_value=None
    )
    get_envelope_edit_link = mocker.patch(
        'apps.esign.adapters.DocuSignAdapter.get_envelope_edit_link',
        return_value=None
    )
    delete_envelope = mocker.patch(
        'apps.esign.models.Envelope.delete', return_value=None
    )

    # check envelope that all required methods were called
    services.clean_up_removed_in_docusign_envelopes_from_db(envelope)
    get_envelope_edit_link.assert_called()
    delete_envelope.assert_not_called()

def test_clean_up_removed_in_docusign_envelopes_from_db_another_exception(
    envelope, mocker
):
    """Test `clean_up_removed_in_docusign_envelopes_from_db` exceptions.

    Check method behavior when another (not `NoEnvelopeExistsException`)
    exception is raised - no envelope deletion should be performed.

    """
    mocker.patch(
        'apps.esign.adapters.DocuSignAdapter.__init__',
        return_value=None
    )
    get_envelope_edit_link = mocker.patch(
        'apps.esign.adapters.DocuSignAdapter.get_envelope_edit_link',
        side_effect=Exception
    )
    delete_envelope = mocker.patch(
        'apps.esign.models.Envelope.delete', return_value=None
    )

    # check envelope that all required methods were called
    services.clean_up_removed_in_docusign_envelopes_from_db(envelope)
    get_envelope_edit_link.assert_called()
    delete_envelope.assert_not_called()

def test_clean_up_removed_in_docusign_envelopes_from_db_valid(
    envelope, mocker
):
    """Test `clean_up_removed_in_docusign_envelopes_from_db` exceptions.

    Check method behavior when `NoEnvelopeExistsException` exception is
    raised - envelope deletion should be performed and matter should be moved
    to `draft` status.

    """
    mocker.patch(
        'apps.esign.adapters.DocuSignAdapter.__init__',
        return_value=None
    )
    get_envelope_edit_link = mocker.patch(
        'apps.esign.adapters.DocuSignAdapter.get_envelope_edit_link',
        side_effect=exceptions.NoEnvelopeExistsException
    )
    delete_envelope = mocker.patch('apps.esign.models.Envelope.delete')

    # check envelope that all required methods were called
    services.clean_up_removed_in_docusign_envelopes_from_db(envelope)
    get_envelope_edit_link.assert_called()
    delete_envelope.assert_called()

def test_clean_up_removed_in_docusign_envelopes_from_db_make_draft(
    envelope, mocker
):
    """Test `clean_up_removed_in_docusign_envelopes_from_db` `make_draft`.

    Check that for `initial` envelope, when it's matter not in `draft` state
    `make_draft` method is called.

    """
    mocker.patch(
        'apps.esign.adapters.DocuSignAdapter.__init__',
        return_value=None
    )
    mocker.patch(
        'apps.esign.adapters.DocuSignAdapter.get_envelope_edit_link',
        side_effect=exceptions.NoEnvelopeExistsException
    )
    mocker.patch('apps.esign.models.Envelope.delete')
    make_matter_draft = mocker.patch('apps.business.models.Matter.make_draft')

    # check that for `extra` envelope `make_draft` wasn't called
    envelope.type = models.Envelope.TYPE_EXTRA
    services.clean_up_removed_in_docusign_envelopes_from_db(envelope)
    make_matter_draft.assert_not_called()

    # check for `initial` envelope and `open` matter
    envelope.type = models.Envelope.TYPE_INITIAL
    envelope.matter.status = models.Matter.STATUS_OPEN
    services.clean_up_removed_in_docusign_envelopes_from_db(envelope)
    make_matter_draft.assert_not_called()

    # check for `initial` envelope and `open` matter
    envelope.type = models.Envelope.TYPE_INITIAL
    envelope.matter.status = models.Matter.STATUS_OPEN
    services.clean_up_removed_in_docusign_envelopes_from_db(envelope)
    make_matter_draft.assert_called()


from funcoes.formatacao import title
import funcoes.autodefs_empresas as autodefs_empresas

def empresas():
    title(title1='Bem vindo à area para Empresas!')

    loop = True
    while loop:

        escolha_menu = input('Digite (1) se deseja fazer cadastro.\nDigite (2) se deseja fazer login\nDigite (3) se deseja voltar ao menu principal.\n')
        lista_escolha_menu = ['1', '2', '3']

        if escolha_menu not in lista_escolha_menu:
            print('\nOpção inválida! Tente novamente\n')

        elif escolha_menu in lista_escolha_menu[2]:
            autodefs_empresas.go_to_menu()

        elif escolha_menu == lista_escolha_menu[0]:
            autodefs_empresas.registrar_empresas()

        elif escolha_menu == lista_escolha_menu[1]:
            json_validado = autodefs_empresas.carregar_json()
            autodefs_empresas.fazer_login(json_validado)


# coding: utf-8

import csv
import math
import random
import re
import time

import requests
from pyquery import PyQuery as pq
from selenium import webdriver
from selenium.common import exceptions
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from user_agent import generate_user_agent

class DianPingSpider(object):
    def __init__(self):
        # self.have_data = self.compare_down_data()
        self.header_css = {
            'Host': 's3plus.meituan.net',
            'Accept-Encoding': 'gzip',
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36'
        }
        self.iframe = (By.TAG_NAME, 'iframe')

    def start_driver(self):
        '''启动firefox57,设置firefox的请求头'''
        random_user_agent = generate_user_agent(os=('win', 'linux'))
        fp = webdriver.FirefoxProfile()
        fp.set_preference("general.useragent.override", random_user_agent)
        fp.update_preferences()
        shopdriver = webdriver.Firefox(firefox_profile=fp)
        return shopdriver

    def dianping_login(self, shopdriver):
        shopdriver.get('http://account.dianping.com/login')
        print(input('请扫描屏幕二维码登录'))
        return 'ok, 登陆成功了!'

    def get_dianping_provinces(self, shopdriver):
        '''访问大众点评城市列表,获取所有的城市列表和的对应的森马门店链接'''
        shopdriver.get('http://www.dianping.com/citylist')
        shopdriver.find_element_by_css_selector('#main > div.underline.province > div.choose-city > div > div.province-choose > p').click()
        all_provinces = shopdriver.find_elements_by_css_selector('#main > div.underline.province > div.choose-city > div > div.province-choose > div > div > span')
        # 获取所有的省份列表
        total_provinces = len(all_provinces)
        for index in range(1, total_provinces + 1):
            # 点击省份下拉框列表
            shopdriver.find_element_by_css_selector('#main > div.underline.province > div.choose-city > div > div.province-choose > p').click()
            # 获取其中某个省份的名称
            name_css_selector = '#main > div.underline.province > div.choose-city > div > div.province-choose > div > div > span:nth-child({index})'.format(index=index)
            province_name = shopdriver.find_element_by_css_selector(name_css_selector).text
            # 选择某个省份
            province_select_js = '''document.querySelector('#main > div.underline.province > div.choose-city > div > div.province-choose > div > div > span:nth-child({index})').click()'''.format(index=index)
            # 不知什么原因,导致用js无法获得省份名称,只能用selenium定位元素
            #     province_name_js = '''return document.querySelector('#main > div.underline.province > div.choose-city > div > div.province-choose > div > div > span:nth-child({index})').innerText;'''.format(index=index)
            #     province_name = shopdriver.execute_script(province_name_js)
            shopdriver.execute_script(province_select_js)
            #     shopdriver.implicitly_wait(10)
            time.sleep(2)
            # 再选择一个省份之后,点击获取城市的清单
            shopdriver.find_element_by_css_selector('#main > div.underline.province > div.choose-city > div > div.city-choose').click()
            citys = shopdriver.find_elements_by_css_selector('#main > div.underline.province > div.choose-city > div > div.city-choose > div > div > a')
            # 循环获取每一个城市的名称和对应链接
            time.sleep(3)
            for city_index in range(1, len(citys) + 1):
                #         city_name_js = '''return document.querySelector('#main > div.underline.province > div.choose-city > div > div.city-choose > div > div > a:nth-child({city_index})').innerText'''.format(city_index=city_index)
                #         one_city_name = shopdriver.execute_script(city_name_js)
                # 获取城市列表中的城市名称
                city_css_selector = '#main > div.underline.province > div.choose-city > div > div.city-choose > div > div > a:nth-child({index})'.format(index=city_index)
                one_city_name = shopdriver.find_element_by_css_selector(city_css_selector).text
                # 获得城市列表中每个城市的链接
                one_city_href = shopdriver.find_element_by_css_selector(city_css_selector).get_attribute('href')
                #         city_click_js = '''document.querySelector('#main > div.underline.province > div.choose-city > div > div.city-choose > div > div > a:nth-child({index})').click()'''.format(index=city_index)
                #         shopdriver.execute_script(city_click_js)
                # 将访问获得的城市链接保存本地
                with open('d:\\test\\dianping_provinces', 'a+', encoding='utf-8') as f:
                    if one_city_name != '':
                        one_row = '{}, {}, {}\n'.format(province_name, one_city_name, one_city_href)
                        print(one_row)
                        f.write(one_row)
                yield province_name, one_city_name, one_city_href

    def search_semir_new_window(self, one_city_href, shopdriver):
        shopdriver.get(one_city_href)
        default_handle = shopdriver.current_window_handle
        search_input = (By.ID, 'J-search-input')
        input_box = WebDriverWait(shopdriver, 5).until(EC.presence_of_element_located(search_input))
        input_box.clear()
        try:
            shopdriver.implicitly_wait(10)
            shopdriver.find_element_by_id('J-search-input').send_keys('森马')
            shopdriver.find_element_by_id('J-all-btn').click()
        except exceptions.NoSuchElementException as e:
            print('搜索框无法搜索到')
        print('搜索跳转成功,开始循环抓取店铺列表\n')
        return default_handle

    def get_total_semir_href(self, shopdriver):
        for province_name, one_city_name, one_city_href in self.get_dianping_provinces(shopdriver):
            default_handle = self.search_semir_new_window(one_city_href, shopdriver)
            handles = shopdriver.window_handles
            if len(handles) >= 2:
                handles.remove(default_handle)
                search_handle = handles[0]
                shopdriver.switch_to.window(search_handle)
            time.sleep(random.random() * 5)
            total_shop_contents = shopdriver.find_element_by_css_selector('body > div.section.Fix.J-shop-search > div.bread.J_bread > span:nth-child(3)').text
            total_shop_counts = int(re.findall('[0-9]{1,10}', total_shop_contents)[0])
            shop_lst_pages = math.ceil(total_shop_counts / 15)
            real_url = shopdriver.current_url
            one_city = []
            for i in range(1, shop_lst_pages + 1):
                page_url = real_url + '/p' + str(i)
                shopdriver.get(page_url)
                time.sleep(random.random() * 5)
                total_shops = shopdriver.find_elements_by_css_selector('#shop-all-list > ul > li')
                for one_shop in total_shops:
                    one_shop_href = one_shop.find_element_by_css_selector('div.pic > a').get_attribute('href')
                    one_shop_title = one_shop.find_element_by_css_selector('div.txt > div.tit > a').get_attribute(
                        'title')
                    one_shop_comment_star = one_shop.find_element_by_css_selector(
                        'div.txt > div.comment > span').get_attribute('title')
                    one_shop_comment_href = one_shop.find_element_by_css_selector(
                        'div.txt > div.comment > a').get_attribute('href')
                    one_city.append((one_shop_href, one_shop_title, one_shop_comment_star))
                    with open('d:\\test\\dianping_semir', 'a+', encoding='utf-8') as f:
                        one_row = '{}, {}, {}, {}, {}, {}\n'.format(province_name, one_city_name, total_shop_counts,
                                                                    one_shop_href, one_shop_title,
                                                                    one_shop_comment_star)
                        print(one_row)
                        f.write(one_row)
            shopdriver.close()
            shopdriver.switch_to.window(default_handle)

    def pinglun_deal(self, data):
        #     每一条评论的提取,删除评论中不需要的信息
        pinglun = data("div.review-words.Hide").html()
        try:
            len(pinglun)
        except:
            pinglun = data("div.review-words").html()
        less_words_loc = pinglun.find('less-words')
        if less_words_loc != -1:
            pinglun = pinglun[:less_words_loc - 14]
        return pinglun

    def css_get(self, doc):
        total_lst_dict = {}
        css_link = "http:" + doc("head > link:nth-child(9)").attr("href")
        background_link = requests.get(css_link, headers=self.header_css, timeout=30)
        css_text = background_link.text
        r = r'background-image: url(.*?);'
        r2 = r'(background-image: url.*?;)'
        need_replace_text = re.findall(r2, css_text)
        total_matches = re.findall(r, background_link.text)
        for i in need_replace_text:
            css_text = css_text.replace(i, '')
        total_lst = []
        if total_matches:
            for one_match in total_matches:
                svg_link = one_match.replace(")", "").replace("(", "http:")
                status, list_svg_y = self.svg_text(svg_link)
                total_lst.extend(list_svg_y)
        # 对css进行解析,明白css中background对应的坐标的数据的转换
        dict_css = self.css_dict(css_text)
        for x, y, c in total_lst:
            total_lst_dict[y] = (x, c)
        return total_lst_dict, dict_css

    # 2-字体库链接和字体库
    def svg_text(sefl, url):
        html = requests.get(url, timeout=30)
        svg_text_r2 = r'<text x="(.*?)" y="(.*?)">(.*?)</text>'
        svg_text_re = re.findall(svg_text_r2, html.text)
        if svg_text_re:
            return 1, svg_text_re
        else:
            svg_text_r = r'<textPath xlink:href="(.*?)" textLength="(.*?)">(.*?)</textPath>'
            svg_text_re = re.findall(svg_text_r, html.text)
            dict_avg = {}
            # 生成svg加密字体库字典
            for data in svg_text_re:
                dict_avg[data[0].replace("#", "")] = list(data[2])
            svg_y_r = r'<path id="(.*?)" d="(.*?) (.*?) (.*?)"/>'
            svg_y_re = re.findall(svg_y_r, html.text)
            list_y = []
            # 存储('18', 'M0', '748', 'H600') eg:(x坐标，未知，y坐标，未知)
            for data in svg_y_re:
                list_y.append([data[0], data[2]])
            res_yindex_words = []
            for index, one_y in enumerate(list_y):
                mixed_words = ''.join(dict_avg[str(index + 1)])
                one_y.append(mixed_words)
                res_yindex_words.append(one_y)
            return 2, res_yindex_words

    # 3-生成css字库字典
    def css_dict(self, html):
        css_text_r = r'.([a-zA-Z0-9]{1,6}){background:(.*?)px (.*?)px;}'
        css_text_re = re.findall(css_text_r, html)
        dict_css = {}
        for data in css_text_re:
            """
            加密字库.gqi4j {background: -98.0px -745.0px;}与svg文件对应关系，x/14，就是svg文件加密字体下标
            y，原样返回，需要在svg函数中做处理
            """
            x = int(float(data[1]) / -14)
            y = -int(float(data[2]))
            dict_css[data[0]] = (data[1], data[2], x, y)
        return dict_css

    def css_decode(self, total_lst_dict, dict_css, pinglun):
        #     每一条评论数据,将其字符串化,然后删除掉不需要的信息
        pinglun_text = pinglun.replace('<svgmtsi class="', ',').replace('"/>', ",").replace('">', ",")
        pinglun_list = [x.strip('\n').strip(' ') for x in pinglun_text.split(",") if x != '']
        pinglun_list = [x for x in pinglun_list if x != '']
        #     获取加密后的所有文字列表
        encryt_words_lst = list(dict_css.keys())
        #     循环每个字符,对加密的字符,匹配解密
        pinglun_str = []
        for msg in pinglun_list:
            # 如果有加密标签
            if msg in encryt_words_lst:
                # 参数说明：[x,y] css样式中background 的[x/14，y]
                x = dict_css[msg][2]
                y = dict_css[msg][3]
                for g in total_lst_dict.keys():
                    if y < int(g):
                        try:
                            pinglun_str.append(total_lst_dict[g][1][x])
                            break
                        except IndexError:
                            pinglun_str.append(' ')
                            break
            # 没有加密标签
            else:
                pinglun_str.append(msg.replace("\n", ""))
        #     将所有的解密后的字符,拼接起来.得到真实评论数据
        if pinglun_str:
            str_pinglun = ''.join(pinglun_str)
            # 删除掉所有的空格数据
            str_pinglun = str_pinglun.strip(' ')
        else:
            str_pinglun = ''
        return str_pinglun

    def down_one_shop_comments(self, shopdriver, have_data):
        fieldnames = ['province_name', 'city_name', 'one_city_one_shop_url', 'one_city_one_shop_name', 'userName',
                      'userID', 'startShop', 'describeShop', 'pinglunTime', 'pinglun']
        # 对于已经下载的大众点评的所有森马门店数据重新读入(这里筛选了没有星级评分的店铺)
        with open(r"D:\Test\dazhongdianping_semir_address.csv", 'r', encoding='utf-8') as f:
            total_rows = f.read().split('\n')
        # 将每一条记录变成列表
        # file = [i.split(',') for i in total_rows]
        # file =  self.have_data
        file = have_data
        for province_name, city_name, total_shops, one_city_one_shop_url, one_city_one_shop_name, shop_stars in file:
            # 根据实际情况,得到所有的评论链接,后缀为 "/review_all",所有点评的页面的数据的加密方式比较一致
            review_all_url = one_city_one_shop_url + '/review_all'
            shopdriver.get(review_all_url)
            shopdriver.implicitly_wait(30)
            time.sleep(2)
            try:
                # 读取页面上评论计数，并将文字描述中的数字提取出来，当作总评论数，根据每页15条评论，计算出总页数
                comments_contents = shopdriver.find_element_by_css_selector('html > body > div#index-main >  div#review-list.review-list > div.review-list-container > div.review-shop-wrap > div.rank-info span.reviews').text
                comment_count = int(re.findall('[0-9]{1,10000}', comments_contents)[0])
                commnet_pages = math.ceil(comment_count / 15)
                print('{}一共有{}页{}条评论'.format(one_city_one_shop_name, commnet_pages, comment_count))
                # 如果评论为0，直接跳过，对下一个门店继续操作
                if comment_count == 0:
                    one_comment_dict = {
                        "province_name": province_name,
                        "city_name": city_name,
                        "one_city_one_shop_url": one_city_one_shop_url,
                        "one_city_one_shop_name": one_city_one_shop_name,
                        "userName": '',
                        "userID": '',
                        "startShop": '',
                        "describeShop": '',
                        "pinglunTime": '',
                        "pinglun": ''
                    }
                    with open('total_semir_comments_deal.csv', 'a+', newline='', errors='ignore',  encoding='utf-8') as f:
                        f_csv = csv.DictWriter(f, fieldnames=fieldnames)
                        f_csv.writeheader()
                        f_csv.writerow(one_comment_dict)
                    print('没有评论, 跳过')
                else:
                    # 通过总页数，每页访问，获得评论数据
                    for one_page in range(1, commnet_pages + 1):
                        print('{}的第{}页评论正在下载中……'.format(one_city_one_shop_name, one_page))
                        if one_page == 1:
                            # 如果是第一页，不对链接进行操作，如果是后续页面，则加上“/p页数”
                            pass
                        else:
                            review_all_url = one_city_one_shop_url + '/review_all/p{}'.format(one_page)
                            # 访问新的页数,抓取评论
                            shopdriver.get(review_all_url)
                            shopdriver.implicitly_wait(5)
                            time.sleep(3)
                        # 调用pyquery,对网页源码进行解析,参考csdn上大神的思路
                        doc = pq(shopdriver.page_source)
                        # 获取当前页面的字体css和svg数据,并处理成规范格式
                        time.sleep(1)
                        total_lst_dict, dict_css = self.css_get(doc)
                        pinglunLi = list(doc("div.reviews-items > ul > li").items())
                        time.sleep(8)
                        # 通过解密，获得一页评论的所有数据
                        # 针对部分店铺数据计数有两条评论,但是实际上没有评论的问题,在pinglunLi的评论列表中进行判断,如果pinglunLi不为空,继续执行
                        if pinglunLi:
                            for data in pinglunLi:
                                # 用户名
                                userName = data("div.main-review > div.dper-info > a").text()
                                if userName == '':
                                    userName = data("div.main-review > div.dper-info > span").html()
                                # 用户ID链接
                                try:
                                    userID = "http://www.dianping.com" + data("div.main-review > div.dper-info > a").attr("href")
                                # except TypeError:
                                #     userID = "http://www.dianping.com/" + data("div.main-review > div.dper-info > span").html()
                                except:
                                    userID = ''
                                # 用户评分星级[10-50]
                                try:
                                    startShop = str(data("div.review-rank > span").attr("class")).split(" ")[1].replace("sml-str", "")
                                except IndexError:
                                    startShop = ''
                                # 用户描述：机器：非常好 环境：非常好 服务：非常好 人均：0元
                                describeShop = data("div.review-rank > span.score").text()
                                # 关键部分，评论HTML,待处理，评论包含隐藏部分和直接展示部分，默认从隐藏部分获取数据，没有则取默认部分。（查看更多）
                                time.sleep(1)
                                pinglun = self.pinglun_deal(data)
                                #  print(css_decode(total_lst_dict, dict_css, pinglun))
                                # 发表评论的时间
                                pinglunTime = data("div.main-review > div.misc-info.clearfix > span.time").text()
                                one_comment_dict = {
                                    "province_name": province_name,
                                    "city_name": city_name,
                                    "one_city_one_shop_url": one_city_one_shop_url,
                                    "one_city_one_shop_name": one_city_one_shop_name,
                                    "userName": userName,
                                    "userID": userID,
                                    "startShop": startShop,
                                    "describeShop": describeShop,
                                    "pinglunTime": pinglunTime,
                                    "pinglun": self.css_decode(total_lst_dict, dict_css, pinglun)
                                }
                                print('{}: {}'.format(userName, one_comment_dict["pinglun"]))
                                with open('total_semir_comments_deal.csv', 'a+', newline='', errors='ignore', encoding='utf-8') as f:
                                    f_csv = csv.DictWriter(f, fieldnames=fieldnames)
                                    f_csv.writeheader()
                                    f_csv.writerow(one_comment_dict)
                                print('{}的评论下载完成\n'.format(one_city_one_shop_name))
                        else:
                            print('计数上有数据,但实际无数据')
            except exceptions.InvalidSessionIdException:
                print('浏览器已经关闭')
                shopdriver.quit()
                break
            except Exception as e:
                print(e)
                wrong_page = shopdriver.find_elements_by_css_selector('html > body > div.not-found > div.not-found-content > div.not-found-right > p.not-found-words1')
                if wrong_page:
                    print('需要重新登录,更换请求头', one_city_one_shop_url)
                    shopdriver.quit()
                    break
                    # continue
                else:
                    print('出现了错误,请及时处理', one_city_one_shop_url)
                    continue

    def compare_down_data(self):
        not_down_shop_file = []
        with open(r'D:\Python\VisualCode\total_semir_comments_deal.csv', 'r', encoding='utf-8') as f1:
            f_csv1 = csv.reader(f1)
            file_down = list(f_csv1)
        coments_shop_url = [i[2] for i in file_down if i[2] != 'one_city_one_shop_url']
        with open(r"D:\Test\dazhongdianping_semir_address.csv", 'r', encoding='utf-8') as f2:
            total_rows =  csv.reader(f2)
            file_config = list(total_rows)
        config_shop_url = [i[3] for i in file_config]
        not_down_href = list(set(config_shop_url).difference(set(coments_shop_url)))
        for one_href in not_down_href:
            for one_row in file_config:
                if one_href in one_row:
                    not_down_shop_file.append(one_row)
        print('还有{}家等待抓取'.format(len(not_down_shop_file)))
        return not_down_shop_file

    def restart_driver_down(self):
        count = 1
        while True:
            have_data = self.compare_down_data()
            shopdriver = self.start_driver()
            self.dianping_login(shopdriver)
            self.down_one_shop_comments(shopdriver, have_data)
            if count > 10:
                break
            count += 1

if __name__ == "__main__":
    one_comment = DianPingSpider()
    # shopdriver = one_comment.start_driver()
    # one_comment.dianping_login(shopdriver)
    # one_comment.get_total_semir_href(shopdriver)
    one_comment.restart_driver_down()
    # print(one_comment.have_data)


a = int(input())
b = []
for i in range(0, a):
    b.append(int(input()))
for j in range(0, len(b)-1):
    Max = b[0]
    idx = 0
    for k in range(1, len(b)-j):
        if b[k] > Max:
            Max = b[k]
            idx = k
    temp = b[idx]
    b[idx] = b[len(b)-1-j]
    b[len(b)-1-j] = temp

for i in b:
    print(i)


from django.urls import include, path

from . import views

urlpatterns = [
	path("", views.index),
	path("yazar/<slug:slug>/", views.author, name="author"),
	path("y/<slug:slug>/", views.article, name="article"),
	path("kat/<slug:slug>/", views.cat),
	path("kat/<slug:slug>/<int:page>", views.cat, name="cat"),
	path("arsiv/", views.archive),
	path("arsiv/<int:page>", views.archive, name="archive"),
	path("hakkinda/", views.about, name="about"),
	path("kunye/", views.team, name="team"),
	path("ara/", views.search, name="search"),
]


from typing import Callable, Dict, Hashable, Optional

import networkx as nx
import numpy as np
from nfp.frameworks import tf
from nfp.preprocessing import features
from nfp.preprocessing.preprocessor import Preprocessor
from nfp.preprocessing.tokenizer import Tokenizer

try:
    import rdkit.Chem
except ImportError:
    rdkit = None

class MolPreprocessor(Preprocessor):
    def __init__(
        self,
        atom_features: Optional[Callable[["rdkit.Chem.Atom"], Hashable]] = None,
        bond_features: Optional[Callable[["rdkit.Chem.Bond"], Hashable]] = None,
        **kwargs,
    ) -> None:
        super(MolPreprocessor, self).__init__(**kwargs)

        self.atom_tokenizer = Tokenizer()
        self.bond_tokenizer = Tokenizer()

        if atom_features is None:
            atom_features = features.atom_features_v1

        if bond_features is None:
            bond_features = features.bond_features_v1

        self.atom_features = atom_features
        self.bond_features = bond_features

    def create_nx_graph(self, mol: "rdkit.Chem.Mol", **kwargs) -> nx.DiGraph:
        g = nx.Graph(mol=mol)
        g.add_nodes_from(((atom.GetIdx(), {"atom": atom}) for atom in mol.GetAtoms()))
        g.add_edges_from(
            (
                (bond.GetBeginAtomIdx(), bond.GetEndAtomIdx(), {"bond": bond})
                for bond in mol.GetBonds()
            )
        )
        return nx.DiGraph(g)

    def get_edge_features(
        self, edge_data: list, max_num_edges
    ) -> Dict[str, np.ndarray]:
        bond_feature_matrix = np.zeros(max_num_edges, dtype=self.output_dtype)
        for n, (start_atom, end_atom, bond_dict) in enumerate(edge_data):
            flipped = start_atom == bond_dict["bond"].GetEndAtomIdx()
            bond_feature_matrix[n] = self.bond_tokenizer(
                self.bond_features(bond_dict["bond"], flipped=flipped)
            )

        return {"bond": bond_feature_matrix}

    def get_node_features(
        self, node_data: list, max_num_nodes
    ) -> Dict[str, np.ndarray]:
        atom_feature_matrix = np.zeros(max_num_nodes, dtype=self.output_dtype)
        for n, atom_dict in node_data:
            atom_feature_matrix[n] = self.atom_tokenizer(
                self.atom_features(atom_dict["atom"])
            )
        return {"atom": atom_feature_matrix}

    def get_graph_features(self, graph_data: dict) -> Dict[str, np.ndarray]:
        return {}

    @property
    def atom_classes(self) -> int:
        """The number of atom types found (includes the 0 null-atom type)"""
        return self.atom_tokenizer.num_classes + 1

    @property
    def bond_classes(self) -> int:
        """The number of bond types found (includes the 0 null-bond type)"""
        return self.bond_tokenizer.num_classes + 1

    @property
    def output_signature(self) -> Dict[str, tf.TensorSpec]:
        if tf is None:
            raise ImportError("Tensorflow was not found")
        return {
            "atom": tf.TensorSpec(shape=(None,), dtype=self.output_dtype),
            "bond": tf.TensorSpec(shape=(None,), dtype=self.output_dtype),
            "connectivity": tf.TensorSpec(shape=(None, 2), dtype=self.output_dtype),
        }

    @property
    def padding_values(self) -> Dict[str, tf.constant]:
        """Defaults to zero for each output"""
        if tf is None:
            raise ImportError("Tensorflow was not found")
        return {
            key: tf.constant(0, dtype=self.output_dtype)
            for key in self.output_signature.keys()
        }

    @property
    def tfrecord_features(self) -> Dict[str, tf.io.FixedLenFeature]:
        """For loading preprocessed inputs from a tf records file"""
        if tf is None:
            raise ImportError("Tensorflow was not found")
        return {
            key: tf.io.FixedLenFeature(
                [], dtype=self.output_dtype if len(val.shape) == 0 else tf.string
            )
            for key, val in self.output_signature.items()
        }

class SmilesPreprocessor(MolPreprocessor):
    def __init__(self, *args, explicit_hs: bool = True, **kwargs):
        super(SmilesPreprocessor, self).__init__(*args, **kwargs)
        self.explicit_hs = explicit_hs
        assert rdkit is not None, "rdkit required for SmilesPreprocessor"

    def create_nx_graph(self, smiles: str, *args, **kwargs) -> nx.DiGraph:
        mol = rdkit.Chem.MolFromSmiles(smiles)
        if self.explicit_hs:
            mol = rdkit.Chem.AddHs(mol)
        return super(SmilesPreprocessor, self).create_nx_graph(mol, *args, **kwargs)

class BondIndexPreprocessor(MolPreprocessor):
    def get_edge_features(
        self, edge_data: list, max_num_edges
    ) -> Dict[str, np.ndarray]:
        bond_indices = np.zeros(max_num_edges, dtype=self.output_dtype)
        for n, (_, _, edge_dict) in enumerate(edge_data):
            bond_indices[n] = edge_dict["bond"].GetIdx()
        edge_features = super(BondIndexPreprocessor, self).get_edge_features(
            edge_data, max_num_edges
        )
        return {"bond_indices": bond_indices, **edge_features}

    @property
    def output_signature(self) -> Dict[str, tf.TensorSpec]:
        if tf is None:
            raise ImportError("Tensorflow was not found")

        signature = super(BondIndexPreprocessor, self).output_signature
        signature["bond_indices"] = tf.TensorSpec(
            shape=(None,), dtype=self.output_dtype
        )
        return signature

class SmilesBondIndexPreprocessor(SmilesPreprocessor, BondIndexPreprocessor):
    pass


import os
import random
import traceback
import unittest
import util.eliot_logging as log
from functools import wraps
import itertools

class ApolloTest(unittest.TestCase):

    @property
    def test_seed(self):
        return self._test_seed

    def setUp(self):
        self._test_seed = os.getenv('APOLLO_SEED', random.randint(0, 1 << 32))
        random.seed(self._test_seed)
        print(f'Test seed set to {self._test_seed}')

def parameterize(**parameterize_kwargs):
    """
    """
    def decorator(async_fn):
        @wraps(async_fn)
        async def wrapper(*args, **kwargs):
            test_instance = args[0]
            new_dict = dict()
            for key in parameterize_kwargs:
                assert key not in kwargs, f"Parameter {key} already passed to {test_instance}"
                new_dict[key] = [(key, value) for value in parameterize_kwargs[key]]

            for kv_tuples in itertools.product(*new_dict.values()):
                with test_instance.subTest(params=kv_tuples):
                    for kv_tuple in kv_tuples:
                        kwargs[kv_tuple[0]] = kv_tuple[1]
                    await async_fn(*args, **kwargs)
        return wrapper

    return decorator

def repeat_test(max_repeats: int, break_on_first_failure: bool, break_on_first_success: bool):
    """
    Runs a test  max_repeats times when both break_on_first_failure and break_on_first_success et to False.
    Only one of break_on_first_failure or break_on_first_success can be True (both can be False).

    break_on_first_success is True: run test up to max_repeats times and breaks after the 1st successful test.
    break_on_first_failure is True: run test up to max_repeats times and breaks after the 1st failing test.
    """
    assert not(break_on_first_failure and break_on_first_success), \
        "Cannot break on first failure and on first success at the same time"
    def decorator(async_fn):
        @wraps(async_fn)
        async def wrapper(*args, **kwargs):
            for i in range(1, max_repeats + 1):
                try:
                    if max_repeats > 1:
                        print(f"Running iteration {i}/{max_repeats}, (break_on_first_failure={break_on_first_failure},"
                              f" break_on_first_success={break_on_first_success})")
                    await async_fn(*args, **kwargs)
                    if break_on_first_success:
                        break
                except Exception as e:
                    is_last = (i == max_repeats) or break_on_first_failure
                    log.log_message(message_type='Test attempt failed', run=i, max_repeats=max_repeats,
                                    is_last=is_last,
                                    )
                    if is_last:
                        raise e
                    else:
                        print(traceback.format_exc())

        return wrapper
    return decorator


from datetime import datetime, timezone
from pathlib import Path
from typing import Optional, List, Dict

from datamodel_code_generator import PythonVersion, chdir
from datamodel_code_generator.format import CodeFormatter
from jinja2 import Environment, FileSystemLoader

from fastapi_scaffolder.consts import MODEL_PATH, BUILTIN_TEMPLATE_DIR, BUILTIN_VISITOR_DIR
from fastapi_scaffolder.parser import OpenAPIParser
from fastapi_scaffolder.utils.load_module import dynamic_load_module
from fastapi_scaffolder.visitor import Visitor

class BackendGenerator:
    def __init__(
        self,
        input_name: str,
        input_text: str,
        output_dir: Path,
        template_dir: Optional[Path],
        model_path: Optional[Path] = None,
        enum_field_as_literal: Optional[str] = None,
        custom_visitors: Optional[List[Path]] = None,
    ):
        self.input_name = input_name
        self.input_text = input_text
        self.output_dir = output_dir
        self.template_dir = template_dir or BUILTIN_TEMPLATE_DIR
        self.model_path = model_path or MODEL_PATH
        self.enum_field_as_literal = enum_field_as_literal
        self.custom_visitors = custom_visitors

        self.parser = None
        self.environment = Environment(
            loader=FileSystemLoader(
                self.template_dir or f"{Path(__file__).parent}/template",
                encoding="utf8",
            ),
        )
        self.code_formatter = CodeFormatter(PythonVersion.PY_38, Path().resolve())
        self.visitors: List[Visitor] = []

    def generate_code(self):
        if not self.output_dir.exists():
            self.output_dir.mkdir(parents=True)
        self.generate_models()
        if not self.parser:
            return
        self.generate_templates()
        if self.custom_visitors:
            self.generate_templates(self.custom_visitors)

    def generate_models(self):
        self.parser = OpenAPIParser(self.input_text, enum_field_as_literal=self.enum_field_as_literal or None)
        with chdir(self.output_dir):
            models = self.parser.parse()
        if not models:
            return
        elif isinstance(models, str):
            output = self.output_dir / self.model_path
            modules = {output: (self.input_name, models)}
        else:
            raise Exception('Modular references are not supported in this version')

        timestamp = datetime.now(timezone.utc).replace(microsecond=0).isoformat()
        header = f'''\
#   models file generated by fastapi-scaffolder:
#   filename:  {{filename}}
#   timestamp: {timestamp}'''

        for path, filename_and_body in modules.items():
            filename, body = filename_and_body
            if path is None:
                file = None
            else:
                if not path.parent.exists():
                    path.parent.mkdir(parents=True)
                file = path.open('wt', encoding='utf8')

            print(header.format(filename=filename), file=file)
            if body:
                print('', file=file)
                sql_modeled = body\
                    .rstrip()\
                    .replace("from pydantic", "from sqlmodel")\
                    .replace("BaseModel", "SQLModel")
                print(sql_modeled, file=file)

            if file is not None:
                file.close()

    def generate_templates(self, custom_visitors: Optional[List[Path]] = None):
        results: Dict[Path, str] = {}

        # vars in key-value format
        # keys are used in template, values are injected by template.render
        # initially adding info section from openapi file
        template_vars: Dict[str, object] = {"info": self.parser.parse_info()}

        # load visitors
        visitors_path = custom_visitors or BUILTIN_VISITOR_DIR.rglob("*.py")
        for visitor_path in visitors_path:
            module = dynamic_load_module(visitor_path)
            if hasattr(module, "visit"):
                self.visitors.append(module.visit)
            else:
                raise Exception(f"{visitor_path.stem} does not have any visit function")

        # Call visitors to build template_vars
        for visitor in self.visitors:
            visitor_result = visitor(self.parser, self.model_path)
            template_vars = {**template_vars, **visitor_result}

        for target in self.template_dir.rglob("*"):
            relative_path = target.relative_to(self.template_dir)
            template = self.environment.get_template(str(relative_path))
            result = template.render(template_vars)
            results[relative_path] = self.code_formatter.format_code(result)

        timestamp = datetime.now(timezone.utc).replace(microsecond=0).isoformat()
        header = f"""\
#   file generated by fastapi-scaffolder:
#   filename:  {Path(self.input_name).name}
#   timestamp: {timestamp}"""

        for path, code in results.items():
            with self.output_dir.joinpath(path.with_suffix(".py")).open("wt") as file:
                print(header, file=file)
                print("", file=file)
                print(code.rstrip(), file=file)


import requests, jsonpath, pytest, ast, os, allure, time, webbrowser, subprocess
from xToolkit import xfile
from string import Template
from Utils.parameter_dict import parameter_dict

"""
Created by: juzi
Created time: 2023/6/5 1:17
Purpose:
"""
# 读取测试用例文档
testSuite = xfile.read("接口测试用例.xls").excel_to_dict(sheet=0)

# 创建以时间戳命名的文件夹
timestamp = time.strftime("%Y-%m-%d_%H%M%S", time.localtime())

# pytest装饰器 循环读取测试用例文档内容 自带循环 遍历数据  括号内的含义  变量名，数据
@pytest.mark.parametrize("case_info", testSuite)
def test_requests(case_info):
    url = case_info["请求url"]
    text_dict = parameter_dict().show_parameter()
    print("保存的参数字典：", text_dict)
    if "$" in url:
        url = Template(url).substitute(text_dict)
    api_url = case_info["host参数"] + url
    api_method = case_info["请求方式"]
    api_params = case_info["url携带参数"]
    api_data = case_info["data参数"]
    api_headers = case_info["请求头参数"]
    api_code = case_info["预期响应码"]
    if "$" in api_data:
        api_data = Template(api_data).substitute(text_dict)
    api_response = requests.request(
        url=api_url,
        data=eval(api_data),
        headers=ast.literal_eval(api_headers),
        params=eval(api_params),
        method=api_method)
    # assert login_response.status_code == 200
    if api_response.status_code == api_code:
        print("状态码断言成功！", api_response.status_code)
    else:
        print("状态码断言失败", api_response.status_code)
    extract_parameters = case_info["提取参数"].strip()
    if extract_parameters != "" or len(extract_parameters) != 0:
        if "," in extract_parameters:
            extract_parameters = ast.literal_eval(extract_parameters)
            for i in extract_parameters:
                response_parameters_data = jsonpath.jsonpath(
                    api_response.json(), "$.." + i)
                # 循环保存需要的参数数据到字典
                parameter_dict().set_parameter(i, response_parameters_data[0])
                print("提取参数：" + i + ":" + response_parameters_data[0] + " 保存字典完毕!")
        else:
            extract_parameters = ast.literal_eval(extract_parameters)
            response_parameters_data = jsonpath.jsonpath(
                api_response.json(), "$.." + extract_parameters)
            # 循环保存需要的参数数据到字典
            parameter_dict().set_parameter(extract_parameters, response_parameters_data[0])
            print("提取参数：" + extract_parameters + "：" + response_parameters_data[0] + " 保存字典完毕!")

    else:
        print("提取参数为空，不做相关提取操作,继续执行下一条案例！")

    print(case_info["用例名称"], "执行完毕！")
    print(api_response.text)

    """
    自动生成测试报告 -- allure
    """

if __name__ == '__main__':
    # 运行pytest命令并指定保存结果的文件夹
    pytest_command = f"pytest -vs --capture=sys main.py --clean-alluredir --alluredir=allure-results/{timestamp}"
    subprocess.run(pytest_command, shell=True)
    # 保存pytest测试数据到allure-results/下
    serve_command = f"allure generate allure-results/{timestamp} -o allure_html/{timestamp}/ --clean"
    subprocess.run(serve_command, shell=True)
    # 打开浏览器运行测试报告
    file_path = f"http://localhost:63342/接口自动化框架/allure_html/{timestamp}/index.html"  # 替换为你的index.html文件路径
    webbrowser.open(file_path)


from abc import ABC, abstractmethod
from typing import Generic, TypeVar

Params = TypeVar('Params')
Return = TypeVar('Return')

class UseCase(ABC, Generic[Params, Return]):

    @abstractmethod
    def execute(self, params: Params) -> Return:
        raise NotImplementedError()


from bitdeli.insight import insight, segment, segment_label
from bitdeli.widgets import Text, Bar, Table, Widget
from discodb.query import Q, Literal, Clause

class TokenInput(Widget):
    pass

class Funnel(Widget):
    pass

def unique(events):
    seen = set()
    for event in events:
        if event not in seen:
            yield event
            seen.add(event)

def query(model, seq):
    return model.query(Q([Clause([Literal(event)]) for event in seq]))

@insight
def view(model, params):
    chosen = list(unique(params['events']['value'] if 'events' in params else []))
    def steps(events):
        for i in range(len(events)):
            yield events[i], len(query(model, events[:i + 1]))

    yield Text(size=(12, 'auto'),
               label='Analyzing user flows',
               data={'text': "## Of the people who did X, how many of continued and did Y?\n"})

    yield TokenInput(id='events',
                     size=(12, 1),
                     label='Event Sequence',
                     value=chosen,
                     data=list(model))
    if chosen:
        yield Funnel(id='funnel',
                     size=(12, 6),
                     data=list(steps(chosen)))

def segment_sequence(params):
    events = params['params']['events']['value']
    return events[:events.index(params['value']) + 1]

@segment
def segment(model, params):
    q = query(model, segment_sequence(params))
    return q

@segment_label
def label(segment, model, params):
    return 'Users who have gone through %s' %\
            ', '.join(segment_sequence(params))