import asyncio
import collections
import contextlib
import functools
import itertools
import re

import discord
from discord.ext import commands
from more_itertools import chunked, consume, iter_except, unique_everseen

from .misc import maybe_awaitable
from .queue import SimpleQueue

_Trigger = collections.namedtuple('_Trigger', 'emoji pattern blocking fallback')

def trigger(emoji, pattern=None, *, block=False, fallback=None):
    """Add a function that will be called with a certain reaction.

    If pattern is a string, it will be used as a regex pattern for
    messages to trigger that function.

    If fallback is a string, it will be used as a regex pattern like
    pattern. However, it will only be used if the bot can't add
    reactions.

    If block is True, reactions will be ignored for the duration of the
    execution.
    """
    def decorator(func):
        func.__trigger__ = _Trigger(emoji=emoji, pattern=pattern, blocking=block, fallback=fallback)
        return func
    return decorator

paginated = functools.partial(
    commands.bot_has_permissions,
    embed_links=True,
)

# Extract the predicate from the check...
_validate_context = paginated()(lambda: 0).__commands_checks__[0]

class _TriggerCooldown(commands.CooldownMapping):
    def __init__(self):
        super().__init__(commands.Cooldown(rate=5, per=2, type=commands.BucketType.user))

    def _bucket_key(self, tup):
        return tup

    def is_rate_limited(self, message_id, user_id):
        bucket = self.get_bucket((message_id, user_id))
        return bucket.update_rate_limit() is not None

_trigger_cooldown = _TriggerCooldown()

class _Callback(collections.namedtuple('_Callback', 'func blocking')):
    """Wrapper class to store both the resolved descriptor and blocking
    attribute."""

    __slots__ = ()

    # InteractiveSession.reaction_help relies on the wrapped function's __doc__
    @property
    def __doc__(self):
        return self.func.__doc__

class InteractiveSession:
    r"""Base class for all interactive sessions.

    Subclasses must implement 'default' method. If necessary, they can
    override 'start'.

    Example:
        class Thing(InteractiveSession):
            @reaction('\N{HEAVY BLACK HEART}')
            def default(self):
                return discord.Embed(description='hi myst \N{HEAVY BLACK HEART}')

            @reaction('\N{THINKING FACE}')
            def think(self):
                return discord.Embed(description='\N{THINKING FACE}')

    A page should either return a discord.Embed, or None if to indicate
    the page was invalid somehow. e.g. The page number given was out of
    bounds, or there were side effects associated with it.
    """

    # TODO: Context-less __init__
    def __init__(self, ctx):
        self.context = ctx
        self._bot = ctx.bot
        self._channel = ctx.channel
        self._users = {ctx.author.id}
        self._message = None
        self._blocking = False

        # XXX: Remove self._current
        self._current = None

        # Queue is used to avoid having reactions being ignored if they're
        # spammed or if a callback unexpectedly takes a long time.
        self._queue = SimpleQueue()

        # Simple flag variable to see if reactions are being used
        # Might be refactored out later, but this is the simplest solution.
        self._using_reactions = False

    def __init_subclass__(cls, *, stop_emoji='\N{BLACK SQUARE FOR STOP}', stop_pattern=None, stop_fallback='exit', **kwargs):
        super().__init_subclass__(**kwargs)
        cls._reaction_map = callbacks = collections.OrderedDict()

        # These are lists as opposed to dicts because we need to iterate
        # through it to find a match, rather than looking it up in a table.
        cls._message_callbacks = message_callbacks = []
        cls._message_fallbacks = message_fallbacks = []
        seen_patterns = set()

        def trigger_iterator():
            # Can't use inspect.getmembers for two reasons:
            # 1. It sorts the members lexographically, which is not what
            #    we want. We want them in definition order.
            # 2. It resolves descriptors too early. This causes things like
            #    x = page('emoji')(partialmethod(meth, ...)) to not be registered.
            #
            name_members = itertools.chain.from_iterable(b.__dict__.items() for b in cls.__mro__)
            for name, member in unique_everseen(name_members, key=lambda p: p[0]):
                trigger = getattr(member, '__trigger__', None)
                if trigger is None:
                    continue

                # Resolve any descriptors ahead of time so we can do _reaction_map[emoji](self)
                resolved = getattr(cls, name)
                callback = _Callback(resolved, trigger.blocking)
                yield trigger.emoji, trigger.pattern, trigger.fallback, callback

            if stop_emoji or stop_pattern or stop_fallback:
                yield stop_emoji, stop_pattern, stop_fallback, _Callback(cls.stop, False)

        for emoji, pattern, fallback, callback in trigger_iterator():
            if emoji not in callbacks:
                callbacks[emoji] = callback

            if pattern and pattern not in seen_patterns:
                seen_patterns.add(pattern)
                message_callbacks.append((pattern, callback))

            if fallback and fallback not in seen_patterns:
                seen_patterns.add(fallback)
                message_fallbacks.append((fallback, callback))

    def using_reactions(self):
        """Return True if reactions are being used for the current
        session, False otherwise.

        This can return False if the session is not running.
        """
        return self._using_reactions

    def check(self, reaction, _):
        """Extra checks for reactions"""
        return reaction.emoji in self._reaction_map

    async def add_reactions(self):
        """Add the reactions to the message"""
        for emoji in self._reaction_map:
            await self._message.add_reaction(emoji)

    def default(self):
        """Return the first embed to start the controller"""
        raise NotImplementedError

    async def start(self):
        """First thing that gets called"""
        self._current = embed = await maybe_awaitable(self.default)
        self._message = await self._channel.send(embed=embed)

    async def stop(self):
        """Stop running the controller"""
        await self._queue.put(None)

    async def cleanup(self, *, delete_after):
        """Clean up anything else after stopping"""
        msg = self._message
        method = msg.delete if delete_after else msg.clear_reactions
        with contextlib.suppress(Exception):
            await method()

    # XXX: Remove release_connection
    async def run(self, *, timeout=120, delete_after=True, release_connection=True):
        """Run the interactive loop"""
        _validate_context(self.context)
        if release_connection:
            with contextlib.suppress(AttributeError):
                await self.context.release()

        # XXX: Can we accomplish without context???
        self._using_reactions = self._channel.permissions_for(self.context.me).add_reactions
        using_reactions = self._using_reactions

        await self.start()
        if self._message is None:
            # start() was overridden but no message was set
            raise RuntimeError('start() must set self._message')

        message = self._message
        triggers = self._message_callbacks.copy()
        task = None
        listeners = []

        def listen(func):
            listeners.append(func)
            return self._bot.listen()(func)

        if using_reactions:
            task = self._bot.loop.create_task(self.add_reactions())

            @listen
            async def on_reaction_add(reaction, user):
                if (
                    not self._blocking
                    and reaction.message.id == message.id
                    and user.id in self._users
                    and self.check(reaction, user)
                    and not _trigger_cooldown .is_rate_limited(message.id, user.id)
                ):
                    callback, self._blocking = self._reaction_map[reaction.emoji]
                    cleanup = functools.partial(message.remove_reaction, reaction.emoji, user)
                    await self._queue.put((callback, cleanup))
        else:
            triggers.extend(self._message_fallbacks)

        if triggers:
            @listen
            async def on_message(msg):
                if (
                    self._blocking
                    or msg.channel != self._channel
                    or msg.author.id not in self._users
                ):
                    return

                patterns, callbacks = zip(*triggers)
                selectors = map(re.fullmatch, patterns, itertools.repeat(msg.content))
                callback = next(itertools.compress(callbacks, selectors), None)
                if callback is None:
                    return

                if _trigger_cooldown.is_rate_limited(message.id, msg.author.id):
                    return

                callback, self._blocking = callback
                await self._queue.put((callback, msg.delete))

        try:
            while True:
                # TODO: Would async_timeout be better here?
                try:
                    job = await asyncio.wait_for(self._queue.get(), timeout=timeout)
                except asyncio.TimeoutError:
                    break

                if job is None:
                    break

                callback, after = job

                result = await maybe_awaitable(callback, self)

                with contextlib.suppress(discord.HTTPException):
                    await after()

                self._blocking = False

                if result is None:
                    continue
                self._current = result  # backwards compat...

                try:
                    await message.edit(embed=result)
                except discord.NotFound:  # Message was deleted
                    break

        finally:
            self._using_reactions = False

            for listener in listeners:
                self._bot.remove_listener(listener)

            if not (task is None or task.done()):
                task.cancel()

            consume(iter_except(self._queue.get_nowait, asyncio.QueueEmpty))
            await self.cleanup(delete_after=delete_after)

    interact = run  # backwards compat

    @property
    def reaction_help(self):
        return '\n'.join(itertools.starmap('{0} => {1.__doc__}'.format, self._reaction_map.items()))

# ------------- Paginator --------------

class Paginator(InteractiveSession):
    """Class that takes an iterable of entries and paginates them.

    This is what often comes to mind when people talk about 'paginators'
    """
    def __init__(self, ctx, entries, *, per_page=15, title=discord.Embed.Empty, colour=None, **kwargs):
        super().__init__(ctx, **kwargs)
        self._pages = tuple(chunked(entries, per_page))
        self._index = 0

        if colour is None:
            colour = ctx.bot.colour

        # These should probably be removed at some point in the future.
        self.title = title
        self.colour = colour

    def single_page(self):
        """Return True if there is only one page, False otherwise"""
        return len(self._pages) == 1

    def small(self):
        """Return True if there are five pages or less, False otherwise"""
        return len(self._pages) <= 5

    async def start(self):
        await super().start()
        if self.single_page():
            # Don't even start paginating if there is only one page.
            await self.stop()

    async def cleanup(self, *, delete_after):
        if not self.single_page():
            await super().cleanup(delete_after=delete_after)

    async def add_reactions(self):
        if self.single_page():
            return

        fast_forwards = {'\U000023ed', '\U000023ee'}
        small = self.small()

        for emoji in self._reaction_map:
            if not (small and emoji in fast_forwards):
                await self._message.add_reaction(emoji)

    # Main methods
    def create_embed(self, page):
        """Create an embed given a slice of entries"""

        return (discord.Embed(title=self.title, colour=self.colour, description='\n'.join(page))
                .set_footer(text=f'Page: {self._index + 1} / {len(self._pages)} ({self.total} total)')
                )

    def page_at(self, idx):
        """Return the embed that would be created at a certain point.

        None if the index is out of bounds.
        """
        if not 0 <= idx < len(self._pages):
            return None

        self._index = idx
        return self.create_embed(self._pages[idx])

    @trigger('\N{BLACK LEFT-POINTING DOUBLE TRIANGLE WITH VERTICAL BAR}', fallback=r'\<\<')
    def default(self):
        """First page"""
        return self.page_at(0)

    @trigger('\N{BLACK LEFT-POINTING TRIANGLE}', fallback=r'\<')
    def previous(self):
        """Previous page"""
        return self.page_at(self._index - 1)

    @trigger('\N{BLACK RIGHT-POINTING TRIANGLE}', fallback=r'\>')
    def next(self):
        """Next page"""
        return self.page_at(self._index + 1)

    @trigger('\N{BLACK RIGHT-POINTING DOUBLE TRIANGLE WITH VERTICAL BAR}', fallback=r'\>\>')
    def last(self):
        """Last page"""
        return self.page_at(len(self._pages) - 1)

    # --------- Go-to page ------------

    def _goto_embed(self):
        ctx = self.context
        description = (
            f'Please enter a number from 1 to {len(self._pages)}.\n\n'
            'To cancel, click \N{INPUT SYMBOL FOR NUMBERS} again.'
        )
        return (discord.Embed(colour=self.colour, description=description)
                .set_author(name=f'What page do you want to go to, {ctx.author.display_name}?')
                )

    def _goto_parse_input(self, content):
        try:
            index = int(content)
        except ValueError:
            return None

        return self.page_at(index - 1)

    # XXX: This needs to be fully refactored for the reaction-less paginator
    #      or possibly not used at all.
    # XXX: This needs to use the user who actually added the reaction, NOT ctx.author
    @trigger('\N{INPUT SYMBOL FOR NUMBERS}', block=True)
    async def goto(self):
        """Go to page"""
        ctx = self.context
        return_result = None
        user_message = None

        def check(m):
            nonlocal return_result, user_message
            if not (m.channel.id == self._channel.id and m.author.id == ctx.author.id):
                return False

            result = self._goto_parse_input(m.content)
            if result is None:
                return False

            return_result = result
            user_message = m
            return True

        def remove_check(reaction, user):
            return (reaction.message.id == self._message.id
                    and user.id == ctx.author.id
                    and reaction.emoji == '\N{INPUT SYMBOL FOR NUMBERS}')

        # The two futures are such that so the user doesn't get "stuck" in the
        # number page. If they click on the number page by accident, then they
        # should have an easy way out.
        #
        # Thus we have to wait for one of two things:
        # 1. The actual number of the page they want to go to
        # 2. The removal of the numbered reaction if the user wants to go back

        to_wait = [
            self._bot.wait_for('message', check=check),
            self._bot.wait_for('reaction_remove', check=remove_check),
        ]

        try:
            embed = self._goto_embed()
            delete_always = await self._channel.send(embed=embed)

            done, pending = await asyncio.wait(
                to_wait,
                timeout=60,
                return_when=asyncio.FIRST_COMPLETED
            )
            for fut in pending:
                fut.cancel()

            if not done:
                # Effectively a timeout.
                return None

            result = done.pop().result()

            if isinstance(result, discord.Message):
                return return_result
            # The user probably removed a reaction.
            return None
        finally:
            for m in [delete_always, user_message]:
                with contextlib.suppress(Exception):
                    await m.delete()

    # ------------- End go-to page -----------

    @property
    def total(self):
        """Return the total number of entries in the list"""
        return sum(map(len, self._pages))

# -------------- Field Pages ----------------------

class FieldPaginator(Paginator):
    """Similar to Paginator, but uses the fields instead of the description"""
    def __init__(self, context, entries, *, inline=True, **kwargs):
        super().__init__(context, entries, **kwargs)

        self.inline = inline
        if len(self._pages) > 25:
            raise ValueError("too many fields per page (maximum 25)")

    def create_embed(self, page):
        embed = (discord.Embed(title=self.title, colour=self.colour)
                 .set_footer(text=f'Page: {self._index + 1} / {len(self._pages)} ({self.total} total)')
                 )

        add_field = functools.partial(embed.add_field, inline=self.inline)
        for name, value in page:
            add_field(name=name, value=value)
        return embed

EmbedFieldPages = FieldPaginator  # backwards compat


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

# import pandas as pd
import random
import string
import os

# 银行类型
cl_banktype = ['00', '01']
# cl_bussniss_type = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13','15','16']
cl_bussniss_type = ['01', '02', '03', '04', '10', '12', '13','15','16']
cl_pay_target = ['00', '01', '02', '03', '04', '05', '06']
cl_hospital_lv = ['00', '01', '02', '03', '04', '05', '06']
cl_medical_type = ['00', '01', '02', '03', '04', '05']

# 从字符串中随机取n个字符组成字符串
def ran_strx(strx, n):
    items = list(strx)
    str2= ''
    for i in range(0, n):
        random.randint(0, len(items)-1)
        str1 = items[random.randint(0, len(items)-1)]
        str2 += str(str1)
    return str2

#获得当前时间
now = datetime.datetime.now()
#转换为指定的格式:
otherStyleTime = now.strftime("%Y-%m-%d %H:%M:%S")
otherStyleTime1 = now.strftime("%Y%m%d%H")
otherStyleTime2 = now.strftime("%Y-%m-%d")
otherStyleTime3 = now.strftime("%Y%m%d%H%M%S")

def make_sql(agency_code, insurance_code, n):

    path = '../../Data_sql/基金收入/'
    isExists = os.path.exists(path)
    if not isExists:
        # 如果不存在则创建目录
        os.mkdir(path)
    file_name = path + 'SMR_FISCAL_REVENUE_RECON'+str(otherStyleTime3)+'.sql'
    smcs_cash_file = open(file_name, 'w', encoding='utf-8')

    # 主表sql
    str_head_zhu = "insert into SMR_FISCAL_REVENUE_RECON values ("

    no = 1
    while (n > 0):

        ID = otherStyleTime3 + str(n) + ''.join(random.sample(string.ascii_lowercase + string.digits, 13))
        CREATE_BY = '-1'
        CREATE_DATE = otherStyleTime
        LAST_MODIFIED_BY = '-1'
        LAST_MODIFIED_DATE = otherStyleTime
        LAST_MODIFIED_VERSION = '1'
        AAE036 = otherStyleTime2
        AAB001 = '1'
        AAA601 = '03'
        AAA602 = '昌平区医保中心上缴财政专户单7'
        AAE140 = insurance_code
        AAA603 = round(random.uniform(100000, 10), 2)
        AAA604 = round(random.uniform(10000, 10), 2)
        AAA605 = round(random.uniform(1000, 10), 2)
        AAZ400 = '6300000020'
        UUID = ID
        PLAN_ID = ''
        REC_STATUS = ''
        REC_NO = ''
        REC_TIME = ''
        EXCHANGE_PLAN_ID = 'b1a8c8c3e2084e36b7a94ddedf681c3a'
        EXCHANGE_PLAN_STATUS = '1'
        VOU_GUID = ''
        VOU_NO = ''
        VOU_DATE = ''
        VOU_STATUS = '0'
        FIELD01 = ''
        FIELD02 = ''
        FIELD03 = ''
        FIELD04 = ''
        FIELD05 = ''
        FIELD06 = ''
        FIELD07 = ''
        FIELD08 = ''
        FIELD09 = ''
        FIELD10 = ''
        FIELD11 = ''
        FIELD12 = ''
        FIELD13 = ''
        FIELD14 = ''
        FIELD15 = ''
        FIELD16 = ''
        FIELD17 = ''
        FIELD18 = ''
        FIELD19 = ''
        FIELD20 = ''
        AMT01 = ''
        AMT02 = ''
        AMT03 = ''
        AMT04 = ''
        AMT05 = ''
        AMT06 = ''
        AMT07 = ''
        AMT08 = ''
        AMT09 = ''
        AMT10 = ''
        AAB034 = agency_code

        line = str_head_zhu + '"' + ID + '", "' + CREATE_BY + '", ' + 'TO_TIMESTAMP(\'' + CREATE_DATE + '\',\'SYYYY-MM-DD HH24:MI:SS:FF6\'), ' + '"' + LAST_MODIFIED_BY + '", TO_TIMESTAMP(\'' + LAST_MODIFIED_DATE + '\',\'SYYYY-MM-DD HH24:MI:SS:FF6\'), "' + \
               LAST_MODIFIED_VERSION + '", "' + AAE036 + '", "' + str(AAB001) + '", "' + AAA601 + '", "' + AAA602 + '", "' + AAE140 + '", "' + str(AAA603) + '", "' + str(AAA604) + '", "' + str(AAA605) + '", "' + AAZ400 + '", "' + UUID + '", "' + PLAN_ID + '", "' + REC_STATUS + \
               '", "' + REC_NO + '", "' + REC_TIME + '", "' + EXCHANGE_PLAN_ID + '", "' + EXCHANGE_PLAN_STATUS + '", "' + VOU_GUID + '", "' + VOU_NO + '", "' + VOU_DATE + '", "' + VOU_STATUS + '", "' + FIELD01 + '", "' + FIELD02 + '", "' + FIELD03 + '", "' +\
               FIELD04 + '", "' + FIELD05 + '", "' + FIELD06 + '", "' + FIELD07 + '", "' + FIELD08 + '", "' + FIELD09 + '", "' + FIELD10 + '", "' + FIELD11 + '", "' + FIELD12 + '", "' + FIELD13 + '", "' + FIELD14 + '", "' + FIELD15 + '", "' + FIELD16 + '", "' \
               + FIELD17 + '", "' + FIELD18 + '", "' + FIELD19 + '", "' + FIELD20 + '", "' + AMT01 + '", "' + AMT02 + '", "' + AMT03 + '", "' + AMT04 + '", "' + AMT05 + '", "' + AMT06 + '", "' + AMT07 + '", "' + AMT08 + '", "' + AMT09 + '", "' + AMT10 + '", "' + AAB034 + '");'

        line = line.replace('\"', '\'')
        no += 1
        n -= 1

        smcs_cash_file.write(line + '\n')

if __name__ == '__main__':
    # 传参： agency_code, insurance_code, n
    make_sql('00', '110', 1000)

Script/基金收入管理/sql_征缴收入-财政专户流水.py

sql_征缴收入-财政专户流水.py


from collections import Counter
from math import factorial as f

def countcurr(letlist, ch):
	if ch not in letlist: return 0
	letlist.remove(ch)
	return (f(len(letlist)))/(f(letlist.count("A"))*f(letlist.count("B"))*f(letlist.count("C"))*f(letlist.count("D")))

a, b, c, d, n = list(map(int, input().split()))
letnum = [a, b, c, d]
letstr = ["A", "B", "C", "D"]

ordered = []
tot = sum(letnum)
p = 0

while sum(letnum) > 0:
	#Available letters
	aletters = [i for i in Counter(dict(zip(letstr, letnum))).elements()]
	#Find next letter in list
	s, cletter = p, -1
	while s < n:
		cletter += 1
		p = s
		s += countcurr(aletters[:], letstr[cletter])
	ordered.append(letstr[cletter])
	aletters.remove(letstr[cletter])
	letnum[cletter] -= 1

for i in ordered:
	print(i, end="")


import speech_recognition as sr
import moviepy.editor as mp
clip=mp.VideoFileClip(r"Dog_story.mp4")
clip.audio.write_audiofile(r"audio_dog.wav")
r=sr.Recognizer()
audio=sr.AudioFile('audio_dog.wav')
with audio as source:
    audio_file=r.record(source)
result=r.recognize_google(audio_file)
with open('recognized.txt',mode='w') as file:
    file.write("Recgnized Speech:")
    file.write("\n")
    file.write(result)
    print("Your text is ready")


#! /usr/bin/env python

import requests
import logging
from uuid import uuid4
import urllib
from flask import (Flask, abort, request, redirect, url_for)

logging.basicConfig()
LOGGER = logging.getLogger(__name__)
LOGGER.setLevel(logging.DEBUG)

CLIENT_ID = 'RBcaJYcKibya8NaoHpZKEo8u4LU7gvBlhe5GmImS'
CLIENT_SECRET = (
    'Dn7zahEcBBTgmAtCiCvz2kOjI8uQiQsJstrtuTzNObOcFbfaZS2K241yttO4L20'
    'PmuMItDnKaiWzkUNL3r5KcLIH6nMBuGqmBvwOQO75ByJc5JniUO47Liwf4JFLliwk'
)
REDIRECT_URI = 'http://localhost:5000/auth_callback/'
BASE_URL = 'https://simeng2.dev.sunpower.com'

app = Flask(__name__)
@app.route('/')
def homepage():
    text = (
        '
AuthCodeTest

Authenticate with PVAPI
'
    )
    return text % make_authorization_url()

def make_authorization_url():
    # Generate a random string for the state parameter
    # Save it for use later to prevent xsrf attacks
    state = str(uuid4())
    save_created_state(state)
    params = {"client_id": CLIENT_ID,
              "response_type": "code",
              "state": state,
              "redirect_uri": REDIRECT_URI,
              "duration": "temporary",
              "scope": "read write groups"}
    url = BASE_URL + '/o/authorize/?' + urllib.urlencode(params)
    LOGGER.debug('authorization URL:\n%s', url)
    return url

# Left as an exercise to the reader.
# You may want to store valid states in a database or memcache,
# or perhaps cryptographically sign them and verify upon retrieval.

def save_created_state(state):
    pass

def is_valid_state(state):
    return True

@app.route('/auth_callback/')
def auth_callback():
    error = request.args.get('error', '')
    if error:
        return "Error: " + error
    state = request.args.get('state', '')
    if not is_valid_state(state):
        # Uh-oh, this request wasn't started by us!
        abort(403)
    code = request.args.get('code')
    # We'll change this next line in just a moment
    LOGGER.debug("got a code! %s", code)
    return redirect(url_for('success', code=get_token(code)))

def get_token(code):
    # client id/secret can either be supplied as POST data or as basic auth
    post_data = {"grant_type": "authorization_code",
                 "code": code,
                 "redirect_uri": REDIRECT_URI}
    response = requests.post(BASE_URL + '/o/token/',
                             auth=(CLIENT_ID, CLIENT_SECRET),
                             data=post_data)
    token_json = response.json()
    LOGGER.debug('response:\n%r', token_json)
    return token_json["access_token"]

@app.route('/success/')
def success(code):
    text = (
        '
AuthCodeTest

"Got access token!
%s
'
        'Return home or go to PVAPI?
'
    )
    return text % (code, BASE_URL)

if __name__ == '__main__':
    app.run(debug=True)


from django import forms
from django.forms import modelform_factory, ModelForm

from .models import *

class SubmissionsForm(forms.ModelForm):
	class Meta:
		model = Submissions
		fields = ['file_path']

class NewDwgForm(forms.ModelForm):

	#Getting the reverse of the many2many field. It is filtered out in the view
	submissions = forms.ModelMultipleChoiceField(Submissions.objects.all())
	class Meta:
		model = Drawings
		# fields = ['project','dn_project','dn_originator','dn_volume_system','dn_type','dn_discipline','dn_series','dn_level','dn_zone_sequence','drawing_title1','drawing_title2','drawing_title3','studio','model_location','revision_offset','scale','paper','dwg_type','discipline','phase','originator',
		# 'submissions',

		# ]
		fields = ['data_store']
		widgets = {'project': forms.HiddenInput()}

	#This is some function that saves the special reverse m2m field
	def save(self, *args, **kwargs):
		instance = super(NewDwgForm, self).save(*args, **kwargs)
		# instance.submissions.add(self.cleaned_data['submissions'])
		instance.submissions.set(self.cleaned_data['submissions'])

		#This has to run to make sure the drawing number gets generated after the form is made!!!
		instance.drawingNumber()
		instance.save()
		return instance

class NewSubForm(forms.ModelForm):
	class Meta:
		model = Submissions
		fields = ['sub_date','req_drawings','project']
		labels = {
			'sub_date':('Submission Date')
		}
		help_texts = {
			'sub_date':('This should be in the format of... Year(XX), Month(XX), Day(XX)')
		}

		widgets = {'project': forms.HiddenInput(),

		}
  #           'sub_date': Charfield(attrs={'cols': 80, 'rows': 20}),
  #       }


"""
Convenience module for pytorch training and visualization. Uses sacred to
log experiments and visdom for visualization.
"""

import torch
import torch.nn as nn
from torch import optim
from torch.utils.data import *
import os, time
import traceback
from pytorch_utils.updaters import *
from tqdm import tqdm

def getTimeName():
    """Return the current time in format <day>-<month>_<hour><minute> for use in filenames."""
    from datetime import datetime
    t = datetime.now()
    return "{:02d}-{:02d}_{:02d}{:02d}".format(t.day,t.month,t.hour,t.minute)

def accuracy(scores, labels):
    """Return accuracy percentage. Assumes scores are in dim -1."""
    with torch.no_grad():
        total = scores.size()[0]
        pred = torch.argmax(scores, dim=-1)
        correct = (pred == labels).sum().cpu().numpy().astype('float32')
        acc = correct/total * 100
        return acc

def save_model(model, epoch, directory, metrics, filename=None):
    """Save the state dict of the model in the directory,
    with the save name metrics at the given epoch.

    epoch: epoch number(<= 3 digits)
    directory: where to save statedict
    metrics: dictionary of metrics to append to save filename
    filename: if a name is given, it overrides the above created filename

    Returns the save file name
    """
    # save state dict
    postfix = ""
    if filename is None:
        filename = f"epoch{epoch:03d}_{getTimeName()}_"
        postfix = "_".join([f"{name}{val:0.4f}" for name, val in metrics.items()])

    filename = os.path.join(directory, filename + postfix + ".statedict.pkl")

    torch.save(model.state_dict(), filename)
    print(f"Saved model at {filename}")

    return filename

def loop(_run,
     model,
     batch_metric_names,
     train_loader,
     trainOnBatch,
     optimizer,

     updaters,
     save_dir,

     callback=None,
     callback_metric_names=[],
     val_loader=None,

     epochs=10,
     save_every=1,
     start_epoch=1,

     **kwargs,
    ):
    """
    Arguments:
    -------------------
     _run: Sacred run instance

    model: Model instance to be trained

    trainOnBatch: Train on batch function with the following signature:
        trainOnBatch(model, batch, optimizer) -> tuple of batch metrics
    batch_metric_names: Names of the batch metrics returned by trainOnBatch

    train_loader: DataLoader which yields batches of training data
    optimizer: Optimizer instance which is passed to trainOnBatch
    updaters: List of running metric updaters which aggregate batch metrics per epoch.
        They should be generator functions which return the running value when
        .send is called with a batch value.
    save_dir: Top level directory in which to save configs, checkpoints and metrics
    of each run

    callback=None : Optional callback function, should have the following signature:
        callback(model, val_loader) -> tuple of callback metrics
        Usually used for calculating validation metrics.
    callback_metric_names=[]: Names of the above returned callback metrics
    val_loader=None: DataLoader for validation data.
    """
    run_dir = os.path.join(save_dir, str(_run._id))
    os.makedirs(run_dir, exist_ok=True)

    try:
        for e in range(start_epoch,start_epoch+epochs,1):

            i=0

            upds = [updater() for updater in updaters]
            [next(u) for u in upds]

            t = tqdm(train_loader, desc=f'Epoch: {e}')
            for i,batch in enumerate(t):
                # Perform train step
                batch_metrics = trainOnBatch(model, batch, optimizer)

                # Update running metrics
                batch_metrics = [upds[i].send(b_metric)
                                    for i,b_metric in enumerate(batch_metrics)]

                postfix = " ".join([f"{name}={value:.4f}"
                    for name, value in zip(batch_metric_names, batch_metrics)])

                t.set_postfix_str(postfix)

            batch_metrics_dict = dict(zip(batch_metric_names, batch_metrics))

            # execute callback
            callback_metrics = ()
            if callback != None:
                callback_metrics = callback(model,
                                    val_loader=val_loader,
                                    batch_metrics_dict=batch_metrics_dict)

            callback_metrics_dict = dict(zip(callback_metric_names,
                                             callback_metrics))
            mets = {**callback_metrics_dict, **batch_metrics_dict}

            if len(callback_metrics)!=0:
                cb_info = "Callback metrics: " + " ".join([f"{name}={val:.6f}"
                                for name,val in callback_metrics_dict.items()])
                print(cb_info)

            # log metrics
            for name,val in zip(batch_metric_names, batch_metrics):
                _run.log_scalar(name, val, e)
            for name,val in zip(callback_metric_names, callback_metrics):
                _run.log_scalar(name, val, e)

            # Checkpointing
            fname = save_model(model, e, run_dir, mets,
                               filename='latest')
            if e%(save_every)==0:
                fname = save_model(model, e, run_dir, mets)
#                 _run.add_artifact(fname)

    except:
        print("Exception occured, saving optimizer and model")
        traceback.print_exc()
    finally:

        # save optimizer state
        fname = os.path.join(
                    run_dir,
                    f"optimizer_state_epoch{e:03d}.statedict.pkl"
                )
        torch.save(optimizer.state_dict(),fname)
#         _run.add_artifact(fname)

        # save model dict
        fname = save_model(model, e, run_dir, mets)
#         _run.add_artifact(fname)

CLASSIFICATION = dict(
    batch_metric_names=['loss', 'acc'],
    callback_metric_names=['val_loss', 'val_acc'],
    updaters=[averager, averager],
)


import setuptools

with open("README.md", "r") as fh:
    long_description = fh.read()

setuptools.setup(
    name="salesforcecli",
    version="0.0.12",
    author="Will Watkinson",
    author_email="wjwats4295@gmail.com",
    description="A package to explore a Salesforce org, perform queries, bulk creates, updates and deletes",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://gitlab.com/wjwatkinson/salesforcecli",
    packages=["sfcli"],
    install_requires=['simple-salesforce',
                      'tabulate',
                      'pandas',
                      'python-dotenv'],
    entry_points={
        'console_scripts': [
            'salesforcecli = sfcli.__main__:main',
        ],
    },
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    python_requires='>=3.6',
)


import cv2
import random
from albumentations import (
    HorizontalFlip,
    IAAPerspective,
    ShiftScaleRotate,
    CLAHE,
    RandomRotate90,
    Transpose,
    ShiftScaleRotate,
    Blur,
    OpticalDistortion,
    GridDistortion,
    HueSaturationValue,
    IAAAdditiveGaussianNoise,
    GaussNoise,
    MotionBlur,
    MedianBlur,
    IAAPiecewiseAffine,
    IAASharpen,
    IAAEmboss,
    RandomContrast,
    RandomBrightness,
    JpegCompression,
    Flip,
    OneOf,
    Compose,
    RandomGamma,
    ElasticTransform,
    ChannelShuffle,
    RGBShift,
    Rotate,
    Normalize,
    RandomBrightnessContrast,
    Resize,
    CenterCrop,
    PadIfNeeded
)

from albumentations.torch import ToTensor
from albumentations.core.transforms_interface import ImageOnlyTransform

def resize_sa(img, size):
    '''Resize, keeping the aspect ratio same
    Larger side is set to `size`, smaller side adjusted
    acc to aspect ratio
    image.shape -> height, width
    cv2.resize takes (width, height)
    '''

    h, w, _ = img.shape
    if h <= w: # equality is imp.
        nh, nw = int(size * (h/w) ), size
    elif h > w:
        nh, nw = size, int(size * (w/h) )
    nimg = cv2.resize(img, (nw, nh))
    return nimg

def strong_aug(p=1):
    """for reference, doesn't help"""
    return Compose(
        [
            OneOf([IAAAdditiveGaussianNoise(), GaussNoise()], p=0.2),
            OneOf(
                [
                    MotionBlur(p=0.2),
                    MedianBlur(blur_limit=3, p=0.1),
                    Blur(blur_limit=3, p=0.1),
                ],
                p=0.2,
            ),
            # ShiftScaleRotate(shift_limit=0.0625, scale_limit=0.2, rotate_limit=45, p=.2),
            OneOf(
                [
                    OpticalDistortion(p=0.3),
                    GridDistortion(p=0.1),
                    IAAPiecewiseAffine(p=0.3),
                ],
                p=0.2,
            ),
            OneOf(
                [
                    CLAHE(clip_limit=2),
                    IAASharpen(),
                    IAAEmboss(),
                    RandomContrast(),
                    RandomBrightness(),
                ],
                p=0.3,
            ),
            # HueSaturationValue(p=0.3),
        ],
        p=p,
    )

class MyCenterCrop(ImageOnlyTransform):
    """Customized center crop, randomly center crops, keeping the aspect ratio
    intact.

    Targets:
        image

    Image types:
        uint8, float32
    """

    def __init__(self, always_apply=False, p=1.0):
        super(MyCenterCrop, self).__init__(always_apply, p)

    def apply(self, image, **params):
        w, h, _ = image.shape
        ratio = random.choice([0.8, 0.85, 0.9, 0.95])
        aug = CenterCrop(int(w * ratio), int(h * ratio), p=1)
        image = aug(image=image)['image']
        return image

    def get_transform_init_args_names(self):
        return ()

def get_transforms(phase, cfg):
    size = cfg["size"]
    mean = eval(cfg["mean"])
    std = eval(cfg["std"])

    list_transforms = []
    img_trfms = []
    if phase == "train":
        list_transforms.extend(
            [
                #HorizontalFlip(),
                ShiftScaleRotate(
                    shift_limit=0.1,
                    scale_limit=0.1,
                    rotate_limit=20,
                    p=0.7,
                    border_mode=1 #cv2.BORDER_CONSTANT
                ),
            ]
        )
        img_trfms = Compose([
                OneOf(
                    [
                        CLAHE(clip_limit=2),
                        IAASharpen(),
                        IAAEmboss(),
                        RandomBrightnessContrast(),
                        JpegCompression(),
                        Blur(),
                        GaussNoise(),
                    ],
                    p=0.5,
                ),
        ])

    list_transforms.extend(
        [
            Resize(size, size),
            Normalize(mean=mean, std=std, p=1),
            ToTensor(),  # [6]
        ]
    )

    list_trfms = Compose(list_transforms)

    return list_trfms, img_trfms

def get_tta():
    tta = Compose(
        [
            Rotate(limit=180, p=0.5),
            Transpose(p=0.5),
            Flip(p=0.5),
            #RandomScale(scale_limit=0.1),
            ShiftScaleRotate(
                shift_limit=0,  # no resizing
                scale_limit=0.1,
                rotate_limit=120,
                p=0.5,
                border_mode=cv2.BORDER_CONSTANT
            ),

        ]
    )
    return tta


import six

from marshmallow import Schema as MarshmallowSchema
from marshmallow import fields

from .base import DynamORMSchema
from ..exceptions import ValidationError

class Schema(MarshmallowSchema, DynamORMSchema):
    """This is the base class for marshmallow based schemas"""

    @staticmethod
    def field_to_dynamo_type(field):
        """Given a marshmallow field object return the appropriate Dynamo type character"""
        if isinstance(field, fields.Raw):
            return 'B'
        if isinstance(field, fields.Number):
            return 'N'
        return 'S'

    @classmethod
    def dynamorm_fields(cls):
        return cls().fields

    @classmethod
    def dynamorm_validate(cls, obj, partial=False, native=False):
        if native:
            data, errors = cls().load(obj, partial=partial)
        else:
            data, errors = cls(partial=partial).dump(obj)
        if errors:
            raise ValidationError(obj, cls.__name__, errors)

        # When asking for partial native objects (during model init) we want to return None values
        # This ensures our object has all attributes and we can track partial saves properly
        if partial and native:
            for name in six.iterkeys(cls().fields):
                if name not in data:
                    data[name] = None

        return data

    @staticmethod
    def base_field_type():
        return fields.Field


import json

from example.tests import TestBase

from django.contrib.auth import get_user_model
from django.core.urlresolvers import reverse, reverse_lazy
from django.conf import settings

class FormatKeysSetTests(TestBase):
    """
    Test that camelization and underscoring of key names works if they are activated.
    """
    list_url = reverse_lazy('user-list')

    def setUp(self):
        super(FormatKeysSetTests, self).setUp()
        self.detail_url = reverse('user-detail', kwargs={'pk': self.miles.pk})

        # Set the format keys settings.
        setattr(settings, 'REST_EMBER_FORMAT_KEYS', True)
        setattr(settings, 'REST_EMBER_PLURALIZE_KEYS', True)

    def tearDown(self):
        # Remove the format keys settings.
        delattr(settings, 'REST_EMBER_FORMAT_KEYS')
        delattr(settings, 'REST_EMBER_PLURALIZE_KEYS')

    def test_camelization(self):
        """
        Test that camelization works.
        """
        response = self.client.get(self.list_url)
        self.assertEqual(response.status_code, 200)

        user = get_user_model().objects.all()[0]
        expected = {
            u'user': [{
                u'id': user.pk,
                u'firstName': user.first_name,
                u'lastName': user.last_name,
                u'email': user.email
            }]
        }

        json_content = json.loads(response.content)
        meta = json_content.get('meta')

        self.assertEquals(expected.get('user'), json_content.get('user'))
        self.assertEqual('http://testserver/user-viewset/?page=2', meta.get('nextLink'))

    def test_pluralization(self):
        """
        Test that the key name is pluralized.
        """
        response = self.client.get(self.list_url, {'page_size': 2})
        self.assertEqual(response.status_code, 200)

        users = get_user_model().objects.all()
        expected = {
            u'users': [{
                u'id': users[0].pk,
                u'firstName': users[0].first_name,
                u'lastName': users[0].last_name,
                u'email': users[0].email
            },{
                u'id': users[1].pk,
                u'firstName': users[1].first_name,
                u'lastName': users[1].last_name,
                u'email': users[1].email
            }]
        }

        json_content = json.loads(response.content)
        self.assertEquals(expected.get('users'), json_content.get('users'))


from __future__ import print_function, division

import os
import matplotlib.pyplot as plt
#import matplotlib.ticker as ticker
import numpy as np
import argparse

import LHCMeasurementTools.mystyle as ms
import LHCMeasurementTools.savefig as sf
from LHCMeasurementTools.mystyle import colorprog
import LHCMeasurementTools.TimestampHelpers as TH
import LHCMeasurementTools.LHC_Heatloads as HL
from LHC_Heat_load_dict import mask_dict, main_dict as large_hl_dict, arc_list
import dict_utils as du

plt.close('all')

moment = 'scrubbing'
main_dict = du.load_dict('./scrubbing_dict_2017.pkl')
plot_fmt = '.-'

def legend(sp, bbox_to_anchor=(1,1), loc='upper left', **kwargs):
    sp.legend(bbox_to_anchor=bbox_to_anchor, loc=loc, **kwargs)

date_on_xaxis = False
filln_range = None # Tuple of min / max fill

fontsz = 16
markersize = 10
linewidth = 3
ms.mystyle_arial(fontsz=fontsz, dist_tick_lab=5)

# mask fill nrs

mask = main_dict['filln'] > 5720
main_dict = mask_dict(main_dict,mask)

if date_on_xaxis:
    time_in = 'datetime'
    t_plot_tick_h = None #'4weeks'
    time_conv = TH.TimeConverter(time_in, t_plot_tick_h=t_plot_tick_h)
    tc = time_conv.from_unix
    x_axis = tc(main_dict[moment]['t_stamps'])
else:
    x_axis = main_dict['filln']

fig2 = ms.figure('Arc heat loads')

sp5 = plt.subplot(2,1,1, sharex=None)
sp1 = sp5
sp5.grid('on')
sp5.set_ylim(0,200)

sp6 = plt.subplot(2,1,2, sharex=sp1)
sp6.grid('on')
sp6.set_ylim(0, 6e-13)

#~ sp5.set_title('Arc heat loads')
#~ sp6.set_title('Normalized arc heat loads.')

# plot vs integrated heat load
figinteg = plt.figure(100)
spinteg = plt.subplot(111)
figinteg.set_facecolor('w')

#Plot vs allocated scrubbing time
t_start_prescrubbing_string = '2017_05_29 08:00:00'
t_start_prescrubbing = TH.localtime2unixstamp(t_start_prescrubbing_string)
t_start_scrubbing_string = '2017_06_06 08:00:00'
t_start_scrubbing = TH.localtime2unixstamp(t_start_scrubbing_string)

t_scrub = 0.*main_dict[moment]['t_stamps']
mask_prescrub = main_dict[moment]['t_stamps']<t_start_scrubbing
t_scrub[mask_prescrub] = main_dict[moment]['t_stamps'][mask_prescrub]-t_start_prescrubbing
t_scrub[~mask_prescrub] = main_dict[moment]['t_stamps'][~mask_prescrub]-t_start_scrubbing+24*3600

figtscrub = plt.figure(200)
sptscrub = plt.subplot(111)
figtscrub.set_facecolor('w')

zeros = []

average, divisor = 0, 0
for key in arc_list:
    hl = main_dict[moment]['heat_load']['arc_averages'][key]
    average += np.nan_to_num(hl)
    divisor += np.isfinite(hl)
average /= divisor

tot_int = main_dict[moment]['intensity']['total']
#~ #plot average
#~ sp5.plot(x_axis, average, plot_fmt, label='Average', color='black', markersize=markersize, linewidth=linewidth)
#~ sp6.plot(x_axis, average/tot_int, plot_fmt, label='Average', color='black', markersize=markersiz, linewidth=linewidth)

for arc_ctr, key in enumerate(arc_list):
    color = colorprog(arc_ctr, 8)
    arc_hl = main_dict[moment]['heat_load']['arc_averages'][key]

    #~ #insert interruption between pre-scrubbing and scrubbing
    #~ interr_after_fill = 5728
    #~ n_before = np.sum(main_dict['filln']<=interr_after_fill)
    #~ x_axis_cut = np.array(list(x_axis[:n_before])+[np.nan]+list(x_axis[n_before:]))
    #~ arc_hl_cut = np.array(list(arc_hl[:n_before])+[np.nan]+list(arc_hl[n_before:]))
    #~ tot_int_cut = np.array(list(tot_int[:n_before])+[np.nan]+list(tot_int[n_before:]))
    #~ sp5.plot(x_axis_cut, arc_hl_cut, plot_fmt, label=key, color=color, markersize=markersize, linewidth=linewidth)
    #~ sp6.plot(x_axis_cut, arc_hl_cut/tot_int_cut, plot_fmt, label=key, color=color, markersize=markersize, linewidth=linewidth)

    sp5.plot(x_axis, arc_hl, plot_fmt, label=key, color=color, markersize=markersize, linewidth=linewidth)
    sp6.plot(x_axis, arc_hl/tot_int, plot_fmt, label=key, color=color, markersize=markersize, linewidth=linewidth)

    sptscrub.plot(t_scrub/3600/24., arc_hl/tot_int, plot_fmt, label=key, color=color, markersize=markersize, linewidth=linewidth)

    integ_hl_this = np.cumsum(main_dict['hl_integrated']['arc_averages'][key])

    spinteg.plot(integ_hl_this, arc_hl/tot_int, '.-', color=color, markersize=markersize, linewidth=linewidth)

arc_average = average

#sp6.legend(bbox_to_anchor=(1.22,1.04))
legend(sp6)
sp5.set_ylabel('Heat load [W/hcell]')
sp6.set_ylabel('Norm. heat load [W/hcell/p+]')
plt.setp(sp5.get_xticklabels(), visible = False)

if date_on_xaxis:
    time_conv.set_x_for_plot(fig2, sp1)
else:
    sp6.set_xlabel('Fill nr')

spinteg.set_ylim(0, None)
spinteg.grid('on')
spinteg.set_ylabel('Norm. heat load [W/hcell/p+]')
spinteg.set_xlabel('Integrated heat load [J/hcell]')
figinteg.subplots_adjust(bottom=.12)

sptscrub.set_ylim(0, None)
sptscrub.grid('on')
sptscrub.set_ylabel('Norm. heat load [W/hcell/p+]')
sptscrub.set_xlabel('Scrubbing time [days]')
figtscrub.subplots_adjust(bottom=.12)

plt.show()


import sys
from PyQt5 import QtCore

def timerEvent():
    global time
    time = time.addSecs(-1)
    print(time.toString("hh:mm:ss"))

app = QtCore.QCoreApplication(sys.argv)

timer = QtCore.QTimer()
time = QtCore.QTime(0,0,10)
timer.timeout.connect(timerEvent)
timer.start(1000)

sys.exit(app.exec_()) #파이참에서 실행시키면 카운트다운이 되긴하는데 이걸 UI랑 합치는 법을 모르겠음


# -*-coding:utf8 -*-
#顶头加一句编码集，不然注释会报错
import os
from flask import Flask,g
from flask_ckeditor import CKEditor

ckeditor = CKEditor()
def create_app():
	app = Flask(__name__)

	#一定要添加SECRET_KEY啊，不然post请求会报错no secret key was set
	app.config['SECRET_KEY']='dev'
	app.DEBUG=True
	app.config['CKEDITOR_SERVE_LOCAL'] = True
	app.config['CKEDITOR_HEIGHT'] = 400

	# @app.template_filter('ellipsis')
	# def do_ellipsis(arg):
	# 	import re
	# 	return re.sub(r"<.*?>",'',arg)

	# from . import db
	db.init_app(app)
	# from . import auth
	# app.register_blueprint(auth.bp)

	# from . import blog
	# app.register_blueprint(blog.bp)
	# app.add_url_rule('/',endpoint='index')

	# ckeditor.init_app(app)

	return app


print('工号1810108407为恁服务，谢绝野蛮人借鉴，qqqxx!')
import matplotlib.pyplot as plt

show_height = int(input('那么首先能告诉我字符画的高度吗:'))
show_width = int(input('输入宽度，请:'))
#这两个数字是可以调的，非常文明，，，

ascii_char = list("$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. ")
#生成一个ascii字符列表
char_len = len(ascii_char)

newpic = input('在?把你jpg格式图片文件名交了，请:')
pic = plt.imread(newpic)
#使用plt.imread方法来读取图像，对于彩图，则生成size = heigth*width*3的图像。matplotlib 中色彩排列是R G B，opencv的cv2中色彩排列是B G R。

pic_height,pic_width,_ = pic.shape
#获取图像的高和宽，，，

gray = 0.2126 * pic[:,:,0] + 0.7152 * pic[:,:,1] + 0.0722 * pic[:,:,2]
#RGB转灰度图的公式:gray = 0.2126 * r + 0.7152 * g + 0.0722 * b。

#思路：根据灰度值，映射到相应的ascii_char。
for i in range(show_height):
    #根据比例映射到对应的像素。
    y = int(i * pic_height / show_height)
    text = ""
    for j in range(show_width):
        x = int(j * pic_width / show_width)
        text += ascii_char[int(gray[y][x] / 256 * char_len)]
    print(text)


import pygame

screen=pygame.display.set_mode((800,600))
pygame.display.set_caption('Game')
running=True
while running:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            running=False
    for n in range(255):
        pygame.draw.rect(screen, (n,255,255),((n,n),(255,255)))
        pygame.draw.rect(screen,(255,n,255),((n,n),(n,n)))
    pygame.display.update()
pygame.quit()


"""Tester module for TimeDecay"""

__author__ = 'Andrea Tacchetti - atacchet@room77.com - Contact: kyle@'
__copyright__ = '2013, Room77, Inc'

import datetime
import unittest

from pylib.util.time_decay import (TimeDecay, TimeStampInTheFutureError)

class TimeDecayTest(unittest.TestCase):

  def test_basic(self):
    x = 10.0
    timestamp = datetime.datetime(2013,2,27)
    today = datetime.datetime.utcnow()
    td = TimeDecay(datetime.timedelta(30))
    (new_timestamp, y) = td.update_value((timestamp, x))
    self.assertTrue(y < x)
    self.assertEqual(new_timestamp.hour, today.hour)
    self.assertEqual(new_timestamp.minute, today.minute)
    self.assertEqual(new_timestamp.second, today.second)

  def test_half_life(self):
    x = 10.0
    timestamp = datetime.datetime.utcnow()- datetime.timedelta(days=15)
    td = TimeDecay(decay_time=datetime.timedelta(15))
    (new_timestamp, y) = td.update_value((timestamp, x))
    self.assertAlmostEqual(y, 5.0)

  def test_future(self):
    x = 10.0
    timestamp = datetime.datetime.utcnow() + datetime.timedelta(days=1)
    td = TimeDecay(decay_time=datetime.timedelta(30))
    self.assertRaises(TimeStampInTheFutureError, td.update_value, (timestamp,x))

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


#!/usr/bin/env python3.6

import feedparser as fp
import time
from datetime import datetime, timedelta
import pytz
import sys
from string import Template

with open(sys.argv[1], "r") as f:
    subscriptions = f.readlines()

# Date and time setup. I only want posts from 10PM the day before
utc = pytz.utc
homeTZ = pytz.timezone('Europe/London')

current_time = datetime.now(homeTZ)

start = (current_time - timedelta(hours=24)).replace(hour=22, minute=0, second=0, microsecond=0)
start = start.astimezone(utc)

# Collect all of today's posts and put them in a list of tuples.
blogs = []
try:
    for s in subscriptions:
        f = fp.parse(s)
        try:
            blog = f['feed']['title']
        except KeyError:
            continue

        posts = []
        for e in f['entries']:
            try:
                when = e['updated_parsed']
            except KeyError:
                when = e['published_parsed']
            when =  utc.localize(datetime.fromtimestamp(time.mktime(when)))
            if when > start:
                title = e['title']
                link = e['link']
                posts.append((title, link))

        if posts:
            blogs.append((blog, posts))
except Exception as e:
    print(f'''

    <title>Oh dear</title>

There was an error {e}

''')
    exit()

blogs.sort()
# Turn them into an HTML list.
listTemplate = '''
{0}

        {1}

'''

listItemTemplate = '''

{0}

'''

ulitems = []
for b in blogs:
    litems = []
    for p in b[1]:
        litems.append(listItemTemplate.format(*p))

    ulitems.append(listTemplate.format(b[0], '\n'.join(litems)))

if not ulitems:
    ulitems.append("
No news yet...
")

# Print the HTMl.
with open(sys.argv[2], "r") as f:
    template = Template(f.read())

print(template.substitute(content="\n".join(ulitems)))


"""pro2 URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin
from django.conf.urls import include
from app2.views import model_form_upload, contact_page
from .views import login_page,home_page, logout_page,dept
from django.conf import settings
from django.conf.urls.static import static
from subjects.views import Books, Articles, Papers, cse_watch,ece_watch,it_watch, read

urlpatterns = [
    url(r'^$',home_page,name="home"),
    url(r'^upload/$',model_form_upload,name='upload'),
    url(r'^login/$',login_page,name="login"),
    url(r'^admin/', admin.site.urls),
    url(r'^search/', include('search.urls', namespace='search')),
    url(r'^logout/$',logout_page,name="logout"),
    url(r'^books/$',Books,name="books"),
    url(r'^articles/$',Articles,name="articles"),
    url(r'^papers/$',Papers,name="papers"),
    url(r'^cse_watch/$',cse_watch,name="cse_watch"),
    url(r'^ece_watch/$',ece_watch,name="ece_watch"),
    url(r'^it_watch/$',it_watch,name="it_watch"),
    url(r'^read/$',read,name="read"),
    url(r'^dept/$',dept,name="dept"),
    url(r'^contact/$',contact_page,name="contact"),
]

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)


import unittest
import re
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait

class TestFooter(unittest.TestCase):
    def setUp(self):
        self.browser = webdriver.Chrome()

    def testfamiliespage(self):
        """Launch the web page"""
        self.browser.get('https://higherground-boston.org/')
        """Check the navigation to partners pages"""
        whatwedo="//*[@id='menu-item-140']/div/a"
        WebDriverWait(self.browser, 5).until(lambda driver: self.browser.find_element_by_xpath(whatwedo)).click()
        ForPartners = "//*[@id='menu-item-135']/a"
        WebDriverWait(self.browser, 5).until(lambda driver: self.browser.find_element_by_xpath(ForPartners)).click()
        """Check JoinUs feature"""
        JoinUs="//*[@id='fl-post-129']/div/div[1]/div/div/div/div[2]/div/div/div/div/div/a/span"
        WebDriverWait(self.browser, 5).until(lambda driver: self.browser.find_element_by_xpath(JoinUs)).click()
        ContactUs = WebDriverWait(self.browser, 5).until(lambda driver: self.browser.find_element_by_xpath("//*[@id='fl-post-21']/div/div[1]/div/div/div/div/div/div/div[3]/div/div/h2/strong"))
        if ContactUs.is_displayed():
            print("Test Passed")
        else:
            print("Error! Try Again")

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


import os
import sys
import traceback
from termcolor import cprint
from functools import wraps

def unwrap(func):
    while hasattr(func, '__wrapped__'):
        func = func.__wrapped__
    return func

def watch(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        print('\n')
        func_name_stdout = f'{func.__code__.co_filename} -> {func.__name__}: Line number {func.__code__.co_firstlineno}'
        cprint(func_name_stdout, 'blue', 'on_white')

        args_stdout = ""
        if args:
            args_stdout += f"args: {args}"
        if kwargs:
            args_stdout += f"kwargs: {kwargs}"
        if args_stdout:
            cprint(args_stdout, 'blue', 'on_white')

        response = None
        try:
            response = func(*args, **kwargs)
        except Exception as e:
            traceback.print_exc()

        return response
    return wrapper


from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
import sys
import glob
import matplotlib.gridspec as gridspec
from mpl_toolkits.mplot3d import Axes3D
import os

d = 3

#Set up plotting environment
if (d == 3):
    fig = plt.figure(figsize=(10,10))
    ax1 = plt.subplot2grid((1,1), (0,0),projection='3d')
elif  (d == 2):
    fig = plt.figure(figsize=(20,10))
    ax1 = plt.subplot2grid((1,2), (0,0))
    ax2 = plt.subplot2grid((1,2), (0,1))

#Load data

#path = os.environ['IliadDir']
#data = np.loadtxt(path + 'MPDFormatData.txt')

#Load example data
data = np.loadtxt('../example_data/ExampleFormattedData.txt')

r = data[:,0]
theta = data[:,1]
phi = data[:,2]
a = data[0,3]

mm = np.sqrt(r**2 + a**2)
x = mm * np.sin(theta)*np.cos(phi)
y = mm * np.sin(theta)*np.sin(phi)
z = r  * np.cos(theta)

#Plot it

if (d == 3):
    ax1.plot(x,y,z)
    limit = max(max(x),max(y),max(z))
    ax1.set_xlim(-limit,+limit)
    ax1.set_ylim(-limit,+limit)
    ax1.set_zlim(-limit,+limit)

if (d == 2):
    ax1.plot(x,y)
    ax2.plot(x,z)

plt.rc('text', usetex=True)
plt.rc('font', family='serif')
fs = 20

plt.show()


import sys

import numpy as np
from matplotlib import pylab as plt
from sklearn.decomposition import PCA

from data.dataimport import import_data
from encoders.baseencoder import AbstractEncoder

if __name__ == '__main__':
    if len(sys.argv) != 3:
        print("Usage <encoderPkl> <dataset.json.gz>")
        sys.exit(-1)

    data = import_data(sys.argv[2])
    encoder = AbstractEncoder.load(sys.argv[1])

    query_expressions_with_annotations = {'a': 'b', '~a': 'r', 'b': 'b', '~b': 'r', 'c': 'b', '~c': 'r',
                                          'a&b': 'b', '~(a&b)': 'r', 'a|b': 'b', '~(a|b)': 'r',
                                          # 'a^b':'b', '~(a^b)':'r', 'a>>b':'b', '~(a>>b)':'r', 'b>>a':'b', '~(b>>a)':'r',
                                          'a&c': 'b', '~(a&c)': 'r', 'a|c': 'b', '~(a|c)': 'r',
                                          # 'a^c', '~(a^c)', 'a>>c', '~(a>>c)', 'c>>a', '~(c>>a)',
                                          'b&c': 'b', '~(b&c)': 'r', 'b|c': 'b', '~(b|c)': 'r',
                                          # 'b^c', '~(b^c)', 'b>>c', '~(b>>c)', 'c>>b', '~(c>>b)',
                                          'a&(b&c)': 'b', '~(a&(b&c))': 'r', 'a|(b|c)': 'b', '~(a|(b|c))': 'r',
                                          'a|(b&c)': 'b', '~(a|(b&c))': 'r', 'a&(b|c)': 'b', '~(a&(b|c))': 'r'
                                          }
    pairs = [
        ('a', '~a'), ('b', '~b'), ('c', '~c'),
        ('a&b', '~(a&b)'), ('a|b', '~(a|b)'),
        ('a&c', '~(a&c)'), ('a|c', '~(a|c)'),
        ('b&c', '~(b&c)'), ('b|c', '~(b|c)'),
        ('a&(b&c)', '~(a&(b&c))'), ('a|(b|c)', '~(a|(b|c))'),
        ('a|(b&c)', '~(a|(b&c))'), ('a&(b|c)', '~(a&(b|c))')
    ]

    used_expressions = {}

    def add_matching(expr_data):
        expr = ''.join(expr_data[0])
        if expr in query_expressions_with_annotations:
            used_expressions[expr] = expr_data

    for name, code in data.items():
        add_matching(code['original'])
        for noisy_sample in code['noise']:
            add_matching(noisy_sample)

    query_expressions = list(query_expressions_with_annotations)
    query_encodings = [encoder.get_encoding(used_expressions[e]) for e in query_expressions]
    query_encodings = np.array(query_encodings)

    pca = PCA(n_components=2)
    pca.fit(query_encodings)
    query_encodings = pca.transform(query_encodings) * .8

    for exp1, exp2 in pairs:
        enc1 = query_encodings[query_expressions.index(exp1)]
        enc2 = query_encodings[query_expressions.index(exp2)]
        plt.plot([enc1[0], enc2[0]], [enc1[1], enc2[1]], '--', color=(.7, .7, .7))

    def make_latex_expr(expr: str):
        expr = expr.replace('&', '\\wedge ').replace('|', '\\vee ').replace('~', '\\neg ')
        return '$' + expr + '$'

    for i, expression in enumerate(query_expressions):
        color = query_expressions_with_annotations[expression]
        plt.scatter(query_encodings[i, 0], query_encodings[i, 1], color=color, s=30, marker='o')
        plt.annotate(make_latex_expr(expression), xy=(query_encodings[i, 0], query_encodings[i, 1]), color=color,
                     fontsize=15)

    plt.tick_params(
        which='both', bottom='off',
        top='off', left='off', right='off',
        labelbottom='off', labelleft='off')
    plt.tight_layout()
    plt.show()


import argparse

from src.data_processing import data_preprocess
from src.classifiers import train_model

def main():
    ap = argparse.ArgumentParser()
    ap.add_argument('-model', '--model_path', help='Path to the keras model', default='model.h5')
    ap.add_argument('-i', '--input_path', help='Directory containing all images', default='data/x')
    ap.add_argument('-e', '--epochs', help='Number of epochs during training', default=100)
    ap.add_argument('-height', '--img_height', help='Images height', default=336)
    ap.add_argument('-width', '--img_width', help='Images width', default=336)
    ap.add_argument('-channels', '--num_channels', help='Number of channels on the images', default=3)
    ap.add_argument('-batch_size', '--batch_size', help='Size of batches during training', default=32)
    ap.add_argument('-val_batch_size', '--val_batch_size', help='Size of batches during validation and test', default=16)
    ap.add_argument('-classes', '--class_names', help='List of classes', default=['class1',
                                                                                'class2'])
    args = vars(ap.parse_args())

    train_batches, val_batches, num_training_steps, num_val_steps = data_preprocess(args['input_path'],
                                                                                args['img_height'],
                                                                                args['img_width'],
                                                                                args['num_channels'],
                                                                                args['batch_size'],
                                                                                args['val_batch_size'],
                                                                                args['class_names'])

    train_model(args['model_path'],
                args['epochs'],
                train_batches,
                val_batches,
                num_training_steps,
                num_val_steps,
                segmentation=False)

if __name__ == '__main__':
    main()


from prefect.storage import GitHub
from prefect.run_configs import ECSRun
from prefect.executors import DaskExecutor
from prefect import task, Flow, Parameter
import prefect

STORAGE = GitHub(repo="lauralorenz/fourteen-oh", ref="main", path="/14-fargate-executor.py")

RUN_CONFIG = ECSRun(
	run_task_kwargs={
		#"networkMode":"awsvpc",
		#"taskDefinition":"14-fargate",
		"cluster":"prefect-demo-cluster",
	 	"networkConfiguration": {'awsvpcConfiguration': {'assignPublicIp': 'ENABLED', 'subnets': ['subnet-7410175c'], 'securityGroups': []}},
 		#"executionRoleArn": "arn:aws:iam::136638793011:role/ecsTaskExecutionRole"
 		},
	task_role_arn="arn:aws:iam::136638793011:role/prefect-demo-fargate-task-role",
	image='prefecthq/prefect:0.14.0',
	memory="512",
	cpu="256")

EXECUTOR = DaskExecutor()

@task
def hi():
   logger = prefect.context.get('logger')
   logger.info("Hello!")

with Flow("prefect-14-dask", storage=STORAGE, run_config=RUN_CONFIG, executor=EXECUTOR) as flow:
	hi()

flow.register("14-demo")

#########
# agent #
#########

if __name__ == '__main__':
	from prefect.agent.ecs.agent import ECSAgent

	AGENT = ECSAgent(#labels=["s3-flow-storage"],
		#cpu="256",
		#memory="512",
		cluster="prefect-demo-cluster",
		#networkConfiguration={'awsvpcConfiguration': {'assignPublicIp': 'ENABLED', 'subnets': ['subnet-7410175c'], 'securityGroups': []}},
		task_role_arn="arn:aws:iam::136638793011:role/prefect-demo-fargate-task-role",
	 	#executionRoleArn="arn:aws:iam::136638793011:role/ecsTaskExecutionRole"
	 	)

	AGENT.start()


# -*- coding：utf-8 -*-
import pytest
import allure
#单元自动化测试脚本
@allure.feature("这是加法的计算器，加数{0}与加数{1}相加功能")
def add_caculator(num1,num2):
    sum=num1+num2
    return sum
@allure.feature("加法的计算器测试")
@allure.description("这是加法的计算器，测试加法的各种情况是否正确")
@allure.severity("critical")
@allure.story("正确的测试用例")
@allure.issue("http://59.110.52.156:8081/zentao/my-bug.html","曾经的bug")
@allure.testcase("http://59.110.52.156:8081/zentao/my-testtask.html","这是加法测试用例")
@pytest.mark.parametrize("num1,num2,result",[[1,2,3],[2,3,5],[5,5,10],[6,2,8]],ids=["10以内","10以外","负数","小数"])
def test_coculator(num1,num2,result):
    with allure.step("第一步：调用加法方法"):
        sum=add_caculator(num1,num2)
    with allure.step("第二步：断言加法运算结果与预期结果是否一致"):
        assert  sum==num1+num2

@allure.description("这是加法的计算器，测试加法的各种无效是否正确")
@allure.severity("normal")
@allure.story("无效的测试用例")
@allure.testcase("http://59.110.52.156:8081/zentao/my-testtask.html")
@pytest.mark.parametrize("num1,num2,result", [[1, '2', 3], [2, '', 5], [-5, '#@wg.,,,,,', -1], [6, 'root', 8]],
                             ids=["字符串数字", "空格", "特殊字符", "字母"])
def test_add_caculator_error(num1,num2,result):
    with allure.step("如果错误截图"):
        allure.attach.file("1.png",
                           attachment_type=allure.attachment_type.PNG)
    with allure.step("html网页的漂亮"):
        allure.attach("这是我们要测试的加法",
                      "这是个网页", allure.attachment_type.HTML)
    with allure.step("执行了"):
        sum=add_caculator(num1,num2)
        allure.attach("加数1+加数2=", f"{0}+{1}={2}")
        assert sum ==result


# This Tool design by Hamed Pariazar 2019
#https://github.com/hamedpa
import cv2
import os, sys
import numpy as np
import matplotlib.pyplot as plt
import uuid
from PIL import Image

# Default inputs
hasImageCompress = "n"
vQuality = 100
frameR = 10
seconds = 10

# Get Inputs from user
videoFormat = input("format of your video (Example: mp4) : ")
imagesFormat = input("format of your images (Example: jpg) : ")
path = input("please enter a path that your videos exist there : ")
imagesPath = input("please enter directory path that you want images save there : ")
frameR = input("please enter frame Rate : ")
seconds = input("Please enter seconds for the video to start : ")

if imagesFormat=="jpg" or imagesFormat=="jpeg":
    hasImageCompress = input("do you need to resize images (n/y) : ")
    if hasImageCompress == "y":
        sw = input("enter width (Example: 200) : ")
        sh = input("enter hight (Example: 200) : ")
        vQuality = input("enter quality (Example: 90) : ")

def getFrameWithResize(sec,file):
    vidcap.set(cv2.CAP_PROP_POS_MSEC,sec*1000)
    hasFrames,image = vidcap.read()
    if hasFrames:
        fileName = "images/"+file+str(uuid.uuid1())+str(count)+"."+imagesFormat
        cv2.imwrite(fileName, image)     # save frame as JPG file
        im = Image.open(fileName)
        f, e = os.path.splitext(fileName)
        imResize = im.resize((int(sw),int(sh)), Image.ANTIALIAS)
        imResize.save("images/"+file+'_resized'+str(uuid.uuid1())+str(count)+"."+imagesFormat, 'JPEG', quality=int(vQuality))
        if os.path.exists(fileName):
              os.remove(fileName)
    return hasFrames

def getFrame(sec,file):
    vidcap.set(cv2.CAP_PROP_POS_MSEC,sec*1000)
    hasFrames,image = vidcap.read()
    if hasFrames:
        fileName = "images/"+file+str(uuid.uuid1())+str(count)+"."+imagesFormat
        cv2.imwrite(fileName, image)     # save frame as JPG file
        im = Image.open(fileName)
    return hasFrames

path = path + "/"

for root, dirs, files in os.walk(path):
    for file in files:
        if file.endswith("."+videoFormat):
                        vidcap = cv2.VideoCapture(path+file)
                        sec = int(seconds)
                        frameRate = int(frameR) #//it will capture image in each 0.5 second
                        count=1
                        if hasImageCompress == "y":
                            success = getFrameWithResize(sec,file)
                        else:
                            success = getFrame(sec,file)

                        while success:
                            count = count + 1
                            sec = sec + frameRate
                            sec = round(sec, 2)
                            success = getFrame(sec,file)

print(videoFormat)


"""
Created on Mon Feb  4 23:06:53 2019

@author: Robert Marc
"""

import numpy as np
import matplotlib.pyplot as plt

def draw_squares(ax,d,p,s):
    if d > 0:
        ax.plot(p[:,0],p[:,1],color = 'k')
        shrink = s/4                                #Shrink value for the corner squares of the current square.
        j = len(p)-2                                #Counter for number of corner squares
        for i in range(4):                          #For and while loop for drawing the corner squares for the current level
            while j >= 0:
                nd = np.array([[p[j,0]-shrink,p[j,1]-shrink],[p[j,0]-shrink,p[j,1]+shrink],  #Builds array for coordinates of next square
                               [p[j,0]+shrink,p[j,1]+shrink],[p[j,0]+shrink,p[j,1]-shrink],
                               [p[j,0]-shrink,p[j,1]-shrink]])
                draw_squares(ax,d-1,nd,s/2)                                                  #Calls for a corner square to be drawn, and that square's corner squares if need be
                j -= 1

plt.close("all")
orig_size = 1000
p = np.array([[0,0],[0,orig_size],[orig_size,orig_size],[orig_size,0],[0,0]])
fig, ax = plt.subplots()
draw_squares(ax,4,p,orig_size)
ax.set_aspect(1.0)
ax.axis('off')
plt.show()
fig.savefig('squares.png')


import logging
import azure.functions as func
import yake
import json

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    try:
        body = json.dumps(req.get_json())
    except ValueError:
        return func.HttpResponse(
             "Invalid body",
             status_code=400
        )

    if body:
        result = compose_response(body)
        return func.HttpResponse(result, mimetype="application/json")
    else:
        return func.HttpResponse(
             "Invalid body",
             status_code=400
        )

def compose_response(json_data):
    values = json.loads(json_data)['values']

    # Prepare the Output before the loop
    results = {}
    results["values"] = []

    for value in values:
        output_record = transform_value(value)
        if output_record != None:
            results["values"].append(output_record)
    return json.dumps(results, ensure_ascii=False)

## Perform an operation on a record
def transform_value(value):
    try:
        recordId = value['recordId']
    except AssertionError  as error:
        return None

    # Validate the inputs
    try:
        assert ('data' in value), "'data' field is required."
        data = value['data']
        assert ('content' in data), "'content' field is required in 'data' object."
        # assert ('text2' in data), "'text2' field is required in 'data' object."
    except AssertionError  as error:
        return (
            {
            "recordId": recordId,
            "errors": [ { "message": "Error:" + error.args[0] }   ]
            })

    try:
            # if text:
        language = "nl"
        max_ngram_size = 3

        deduplication_thresold = 0.9
        deduplication_algo = 'seqm'
        windowSize = 1
        numOfKeywords = 20

        custom_kw_extractor = yake.KeywordExtractor(lan=language, n=max_ngram_size, dedupLim=deduplication_thresold, dedupFunc=deduplication_algo, windowsSize=windowSize, top=numOfKeywords, features=None)
        keywords = custom_kw_extractor.extract_keywords(value['data']['content'])

        keywords = sorted(keywords, key=lambda tup: tup[1],reverse=True)

        keyslist= [tup[0] for tup in keywords]
        valuelist = [tup[1] for tup in keywords]

        # Here you could do something more interesting with the inputs

    except:
        return (
            {
            "recordId": recordId,
            "errors": [ { "message": "Could not complete operation for record." }   ]
            })

    return ({
            "recordId": recordId,
            "data": {
                "keywords": keyslist,
                "keywordscores":valuelist
                    }
            })

    # text = req.params.get('text')
    # ngram = req.params.get('ngram')
    # nkey = req.params.get('nkey')
    # if not text:
    #     try:
    #         req_body = req.get_json()
    #     except ValueError:
    #         pass
    #     else:
    #         text = req_body.get('text')

    # if text:
    #     language = "nl"
    #     if ngram:
    #         max_ngram_size = int(ngram)
    #     else:
    #         max_ngram_size = 3

    #     deduplication_thresold = 0.9
    #     deduplication_algo = 'seqm'
    #     windowSize = 1
    #     if nkey:
    #         numOfKeywords = int(nkey)
    #     else:
    #         numOfKeywords = 20

    #     custom_kw_extractor = yake.KeywordExtractor(lan=language, n=max_ngram_size, dedupLim=deduplication_thresold, dedupFunc=deduplication_algo, windowsSize=windowSize, top=numOfKeywords, features=None)
    #     keywords = custom_kw_extractor.extract_keywords(text)

    #     # for kw in keywords:
    #     #     print(kw)

    #     return func.HttpResponse(str(dict(keywords)))
    # else:
    #     return func.HttpResponse(
    #          "Please pass a text on the query string or in the request body",
    #          status_code=400
    #     )


import scrapy

# this is the spyder that will scrape the url(s) specified by the user and retrieve the data that they want
class Spyder(scrapy.Spider):

    name = "spyder"

    def __init__(self, *args, **kwargs):
        super(Spyder, self).__init__(*args, **kwargs)
        # set the url to be scraped
        self.start_urls = kwargs.get("urls")
        # set the target dict for this spyder
        self.target_dict = kwargs.get("target_dict")
        # set the file dict for this spider
        self.file_dict = kwargs.get("file_dict")

    def start_requests(self):
        for url in self.start_urls:
            yield scrapy.Request(url, self.parse)

    def parse(self, response):
        selector_keypairs = self.target_dict[response.url]

        # create dictionary that will be the item that is yielded
        # add the url of this target to the dictionary
        item = {"url": response.url}

        # for each selector key-pair, use the key (pair[0]) as the key in item and extract the data using
        # the css selector itself (pair[1])
        for pair in selector_keypairs:
            item[pair[0]] = response.css(pair[1] + "::text").extract()

        yield item


# -*- coding: utf-8 -*-
'''
@File  : handleCase.py
@Date  : 2019/1/15/015 18:24
'''
from common.parseExc import *
from common.config import CASEPATH
from common.log import Log
from common.config import CASENAME
import json

# 去掉换行符
def quchu_n(str):
    str = str.replace("\n", "")
    return str

class HandleCase(object):
    def __init__(self):
        # 实例parseExc对象
        self.log = Log()
        self.pe = PaserExc(CASEPATH, 0)

    # 总用例数
    def get_totals(self):
        return self.pe.get_nrows() - 1

    # 处理检查点中数据
    def handle_checkPoint(self, item):
        global key, value
        checkPints = {}
        key, value = item.split("=")
        if ":" in value:
            value = value.replace(":", "：")
        if "." in key:
            temp = {}
            key1 = (str(key).split("."))[0]  # payload.coin类型的集合点解析
            key2 = (str(key).split("."))[1]
            temp[key2] = value
            checkPints[key1] = temp
        else:
            checkPints[key] = value
        return checkPints

    # 处理关联参数
    def handle_related_params(self, item):
        related_params = []
        if ";" in item:
            for i in item.split(";"):
                if "." in i:
                    related_params.append(i.split("."))
                else:
                    related_params.append(i)
        elif "." in item:
            related_params.append(item.split("."))
        else:
            related_params.append(item)
        return related_params

    # 处理每条用例的数据格式
    def handle_data(self, datas):
        global cid, apiId, describe, host, expect, method, params, checkPints, relatedApi, relatedParams
        checkPints = {}
        # relatedParamsInfo = {}
        if isinstance(datas, dict):
            cid = int(datas["caseId"])
            apiId = int(datas["apiId"])
            describe = str(quchu_n(datas["caseDescribe"]))
            host = str(quchu_n(datas["apiHost"]))
            expect = str(datas["expect"])
            method = str(datas["method"])
            params = str(datas["params"])
            relatedParams = str(datas["relatedParams"])
            if expect:
                if expect.split(";")[-1] != "":
                    for item in expect.split(";"):
                        checkPints.update(self.handle_checkPoint(item))
                else:
                    checkPints = self.handle_checkPoint(expect.replace(";", ""))
                datas["expect"] = checkPints
            else:
                datas["expect"] = {}
            return datas
        else:
            raise Exception("参数错误，所传参数datas必须是字典")

    # 获得所有测试用例
    def get_cases(self):
        values = []
        cases = []
        result = []
        rowValues = self.pe.get_row()[1:]
        for row in rowValues:
            values.append(dict(zip(CASENAME, row)))
        # 去掉不执行的用例
        for case in values:
            if case["isExcute"] == "y" or case["isExcute"] == "Y" or case["isExcute"] == "":
                cases.append(case)
            case.pop("isExcute")
        # 转换用例字段的数据格式
        for case in cases:
            case["caseId"] = int(case["caseId"])
            case["apiId"] = int(case["apiId"])
            case["caseDescribe"] = quchu_n(str(case["caseDescribe"]))
            case["apiHost"] = quchu_n(str(case["apiHost"]))
            case["params"] = quchu_n(case["params"])
            case["apiHeaders"] = quchu_n(case["apiHeaders"])
            case["method"] = quchu_n(case["method"])
            if isinstance(case["relatedApi"], float):
                case["relatedApi"] = int(case["relatedApi"])
            else:
                case["relatedApi"] = None
            case["relatedParams"] = quchu_n(case["relatedParams"])
            case["relatedParams"] = self.handle_related_params(case["relatedParams"])
            case["expect"] = quchu_n(case["expect"])
            self.handle_data(case)
            result.append(case)
        return result

if __name__ == '__main__':
    a = 'code;c.code'
    s = HandleCase()
    j=s.get_cases()[0]
    l=j["relatedParams"]


# -*- coding: utf-8 -*-
"""
Created on Fri Jan 26 11:02:46 2018

@author: lenovo
"""

import random
import matplotlib.pyplot as plt
from scipy.stats import norm
#Parameters
n = 100 #names in credit index
rho = 0.5
num_sims = 1000
prob_default = 0.1

#For equity tranche 0-20%, mezzanine 20%-80%
tranche_cutoffs = [0,0.2,1]
tranche_to_watch = 1 #1 is quity, 2 mezz, etc

#derived parameters
z_score_of_default = norm.ppf(prob_default)  #the return when the default happens
beta = rho**0.5
alpha = (1 -rho)**0.5
max_defaults_protected = n*tranche_cutoffs[tranche_to_watch-1]
wiped_out_defaults = n*tranche_cutoffs[tranche_to_watch]
names_in_tranche = wiped_out_defaults-max_defaults_protected
# run simulation
#histogram = [0] * (n+1)
trial_results = []
names_remaining_in_tranche = []
for _ in range(num_sims):
    M = random.gauss(0, 1)
    K = 0 # number of variables that are > 0
    for _ in range(n):
        R_i = beta * M + alpha * random.gauss(0, 1)
        if R_i < z_score_of_default:
            K += 1
    #histogram[K] += 1
    trial_results.append(K)
    remaining_in_tranche = max(0,names_in_tranche - max(0,max_defaults_protected))
    names_remaining_in_tranche.append(remaining_in_tranche)
#print(histogram)
plt.hist(trial_results, bins=n+1, normed=0, align='mid')
plt.title('Distribution of Defaults in ' + str(num_sims) + ' trials')
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.show()

Friday Morning Workshop/tranched credit index.py


#! /usr/bin/env python
##
import requests
import re
import time
import pickle
import logging
import sys

from telegram.ext import Updater, CommandHandler
from telegram.bot import Bot

ADMIN_USERNAME = 'USERNAME_WITHOUT_@'
BOT_TOKEN = 'FULLTOKENHEREPLS'

bot_help_message = '''\
/subscribe yes - Subscribes to GCTF Scoreboard.
/subscribe no - Unsubscribes to GCTF Scoreboard.'''

logging.basicConfig(
        format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
        level=logging.INFO)

def save_scoreboard(scoreboard):
    with open('scoreboard.txt', 'w') as file:
        pickle.dump(scoreboard, file)

def load_scoreboard():
    try:
        with open('scoreboard.txt', 'r') as file:
            scoreboard = pickle.load(file)
            return scoreboard
    except IOError:
        return set()

def load_subscribers():
    try:
        with open('subscribers.txt', 'r') as file:
            return pickle.load(file)
    except IOError:
        return set()

bot = Bot(token=BOT_TOKEN)
updater = Updater(bot=bot)
subscribers = load_subscribers()

def send_subscribe(bot, update, args):
    if len(args) > 0:
        if args[0] == 'yes':
            subscribers.add(update.message.chat_id)
            bot.sendMessage(chat_id=update.message.chat_id, text="You've been subscribed to GCTF's Scoreboard.")
            print("%s has subscribed to GCTF Scoreboard." % update.message.chat_id)
        elif args[0] == 'no':
            subscribers.remove(update.message.chat_id)
            bot.sendMessage(chat_id=update.message.chat_id, text="You've been unsubscribed from GCTF's Scoreboard.")
            print("%s has unsubscribed from GCTF Scoreboard." % update.message.chat_id)
        elif args[0] == 'list' and update.message.from_user.username == 'LFlare':
            message = ''
            for subscriber in subscribers:
                message += str(subscriber) + '\n'
            bot.sendMessage(chat_id=update.message.chat_id, text=message)
        elif args[0] == 'unsub' and update.message.from_user.username == 'LFlare':
            remove_id = int(args[1])
            try:
                subscribers.remove(remove_id)
                bot.sendMessage(chat_id=update.message.chat_id, text='%d has been removed!' % remove_id)
            except KeyError:
                bot.sendMessage(chat_id=update.message.chat_id, text='%d cannot be found!' % remove_id)
    else:
        bot.sendMessage(chat_id=update.message.chat_id, text=bot_help_message)
    print(args)

    with open('subscribers.txt', 'w') as file:
        pickle.dump(subscribers, file)

def send_scoreboard(bot, update):
    print('Sending scoreboard to %s' % update.message.chat_id)
    telegram_message = ""
    for placing in sorted(scoreboard):
        telegram_message += '%d - %s - %d' % (placing[0], placing[1], placing[2]) + "\n"
    bot.send_message(chat_id=update.message.chat_id, text=telegram_message)
    print('Sent.')

def send_ping(bot, update):
    print('Replying pong to %s' % update.message.chat_id)
    bot.send_message(chat_id=update.message.chat_id, text="pong")
    print('Replied')

def compare_scoreboards(a, b):
    difference = a ^ b

    if len(difference) != 0:
        return True
    return False

updater.dispatcher.add_handler(CommandHandler('subscribe', send_subscribe, pass_args=True))
updater.dispatcher.add_handler(CommandHandler('scoreboard', send_scoreboard))
updater.dispatcher.add_handler(CommandHandler('ping', send_ping))
updater.start_polling()

while True:
    print('\nRetrieving scoreboard...')
    data = requests.get('https://www.spgame.site/scoreboard').text
    data = re.findall('(?s)(?:
)(.*)(?:
)', data)[0].split('\n')

    newscoreboard = set()
    for line in data:
        try:
            place = int(re.findall('(?:
)([0-9]+)', line)[0])
            name = str(re.findall('([a-zA-Z0-9\s/|!]+)', line)[0])
            score = int(re.findall('(?:	)([0-9]+)(?:	)', line)[0])
            team = (place, name, score)
            newscoreboard.add(team)
        except IndexError:
            pass

    scoreboard = load_scoreboard()
    if compare_scoreboards(newscoreboard, scoreboard):
        print('Change detected!')

        telegram_message = ''
        for placing in sorted(scoreboard - newscoreboard):
            telegram_message += '%d - %s - %d' % (placing[0], placing[1], placing[2]) + "\n"
        telegram_message += '=+=+=\n'
        for placing in sorted(newscoreboard - scoreboard):
            telegram_message += '%d - %s - %d' % (placing[0], placing[1], placing[2]) + "\n"

        for subscriber in subscribers:
            bot.send_message(chat_id=subscriber, text=telegram_message)

        save_scoreboard(newscoreboard)
    else:
        print(list(newscoreboard - scoreboard))

    try:
        for i in range(5):
            sys.stdout.write('\rSleeping... %d_' % (5 - i))
            sys.stdout.flush()
            time.sleep(1)
    except KeyboardInterrupt:
        continue


import matplotlib.pylab as mplot
import seaborn as sbn
import numpy as np

sbn.set_context("paper")

points = np.linspace(-60, 60, 601)
tau_plus = 10.
tau_minus = -10.
a_plus = 0.4
a_minus = -0.4

out_points = []

for i in points:
    w = 0
    if i > 0:
         w = -(a_plus * np.e ** (-i / tau_plus))
    if i < 0:
         w = -(a_minus * np.e ** (-i / tau_minus))
    out_points.append(w)

fig = mplot.figure(0)
mplot.xlim([-60,60])
# mplot.title("Stereotyped STDP Weight Modulation Curve")
mplot.xlabel("Time Difference Between Presynaptic and Postsynaptic Spike (ms)")
mplot.ylabel("Weight Modulation")
mplot.plot(points, out_points)
mplot.show()
fig.savefig("../chapters/review/fig/stdp.pdf", bbox_inches='tight')


import json

from braces.views import LoginRequiredMixin, AjaxResponseMixin, JSONResponseMixin
from django.contrib.auth import authenticate, login as auth_login, logout as auth_logout
from django.core.urlresolvers import reverse
from django.db.models.fields import AutoField
from django.shortcuts import redirect
from django.views import generic as cbv

from apps.api.client_service import api_service
from apps.office.office.forms import UserForm

class LoginView(AjaxResponseMixin, JSONResponseMixin, cbv.RedirectView):

    def get_redirect_url(self, *args, **kwargs):
        if self.request.user.is_authenticated():
            return reverse('office:main')
        else:
            return '/'

    def post_ajax(self, request, *args, **kwargs):
        hash = request.POST.get('hash')
        auth = False
        result = {}
        if hash:
            user_id = api_service.get_user_id_by_hash(hash)
            if user_id:
                user = authenticate(user_id)
                if user:
                    autofield = AutoField()
                    autofield.attname = 'pk'
                    user._meta.pk = autofield
                    auth_login(self.request, user)
                    auth = True
                else:
                    auth_logout(self.request)
        if auth:
            result = {
                'url': reverse('office:main')
            }
        return self.render_json_response(json.dumps(result))

login_view = LoginView.as_view()

class LogoutView(cbv.RedirectView):
    url = '/'
    permanent = False

    def get_redirect_url(self, *args, **kwargs):
        return self.url

    def get(self, request, *args, **kwargs):
        auth_logout(request)
        return super(LogoutView, self).get(request, *args, **kwargs)

logout_view = LogoutView.as_view()

class UpdateUserView(LoginRequiredMixin, cbv.View):
    http_method_names = ['post']

    def post(self, request, *args, **kwargs):
        form = UserForm(request.POST)
        if form.is_valid():
            form.save()
        return redirect(reverse('office:main'))

update_user_view = UpdateUserView.as_view()