#! /usr/bin/env python
import numpy as np
import gdal
import gdalnumeric

def getModis(data,options):
    xoff=options.col
    yoff=options.row
    xsize=options.ncols
    ysize=options.nrows
    verbose=options.verbose
    datadir=options.datadir
    data['mask'] = []
    data['vza'] = []
    data['sza'] = []
    data['raa'] = []
    data['reflectance'] = []
    data['doy'] = []
    for t in range(len(data['date'])):
        print (t,'/',len(data['date']),data['date'][t],':')
        try:
            qamaskFull,vzaFull,szaFull,raaFull,refl = getModist(data,t,datadir=datadir,\
                                                           xsize=xsize,ysize=ysize,\
                                                           xoff=xoff,yoff=yoff)
            if verbose:
                print (t,data['date'][t],qamaskFull,refl[0,0])

            data['doy'].append(data['date'][t])
            data['mask'].append(qamaskFull)
            data['vza'].append(vzaFull)
            data['sza'].append(szaFull)
            data['raa'].append(raaFull)
            data['reflectance'].append(refl)
        except:
            if verbose:
                print (t,data['date'][t],'No files')
    print (' ')
    data['doy'] = np.array(data['doy'])
    data['mask'] = np.array(data['mask'])
    data['vza'] = np.array(data['vza'])
    data['sza'] = np.array(data['sza'])
    data['raa'] = np.array(data['raa'])
    data['reflectance'] = np.array(data['reflectance'])
    data['wavebands'] = np.array([ float(i) for i in "659 865 470  555 1240 1640 2130".split()])
    return data

def getModist(data,t,xoff=0,yoff=0,xsize=2400,ysize=2400,datadir='data'):
    qcstr = '8 72 136 200 1032 1288 2056 2120 2184 2248'
    qc = np.array([int(i) for i in qcstr.split()])

    # angle data
    bad = -32767
    ds = gdal.Open(data['angles'][t])
    sds_md = ds.GetMetadata('SUBDATASETS')
    xsize2 = int(np.max([xsize/2,1]))
    xoff = int(xoff)
    yoff = int(yoff)
    ysize2 = int(np.max([ysize/2,1]))
    saz = gdalnumeric.LoadFile(sds_md['SUBDATASET_1_NAME'],\
                               xoff=xoff/2,yoff=yoff/2,xsize=xsize2,ysize=ysize2)
    szen = gdalnumeric.LoadFile(sds_md['SUBDATASET_2_NAME'],\
                               xoff=xoff/2,yoff=yoff/2,xsize=xsize2,ysize=ysize2)
    vaz = gdalnumeric.LoadFile(sds_md['SUBDATASET_3_NAME'],\
                               xoff=xoff/2,yoff=yoff/2,xsize=xsize2,ysize=ysize2)
    vzen = gdalnumeric.LoadFile(sds_md['SUBDATASET_4_NAME'],\
                               xoff=xoff/2,yoff=yoff/2,xsize=xsize2,ysize=ysize2)
    anglemask = ~((saz == bad) * (szen == bad) * (vaz == bad) * (vzen==bad))
    # qa data
    del ds
    ds = gdal.Open(data['qa'][t])
    sds_md = ds.GetMetadata('SUBDATASETS')
    qa = gdalnumeric.LoadFile(sds_md['SUBDATASET_1_NAME'],\
                               xoff=xoff/2,yoff=yoff/2,xsize=xsize2,ysize=ysize2)
    qamask = anglemask*0
    for i in range(len(qc)): qamask = qamask | (qa == qc[i])
    qamask = qamask * anglemask
    #if qamask.sum():
    #    import pdb
    #    pdb.set_trace()
    # expand
    qamaskFull = np.zeros([xsize,ysize]).astype(bool)
    vzaFull = np.zeros_like(qamaskFull).astype(float)
    szaFull = np.zeros_like(qamaskFull).astype(float)
    raaFull = np.zeros_like(qamaskFull).astype(float)
    del ds
    del qa
    for i in [0,1]:
        for j in [0,1]:
            qamaskFull[i::2,j::2] = qamask
            vzaFull[i::2,j::2] = 0.01*vzen
            szaFull[i::2,j::2] = 0.01*szen
            raaFull[i::2,j::2] = 0.01*(saz - vaz)
    # refl data
    bad = -28672
    ds = gdal.Open(data['refl'][t])
    sds_md = ds.GetMetadata('SUBDATASETS')
    refl = []
    for i in range(1,8):
        r = gdalnumeric.LoadFile(sds_md['SUBDATASET_%d_NAME'%i],\
                               xoff=xoff,yoff=yoff,xsize=xsize,ysize=ysize)
        qamaskFull = qamaskFull * (r != bad) * (r > 0)
        refl.append(r/10000.)
    del ds
    for i in range(7):
        r = refl[i]
        r[~qamaskFull] = 0.
        refl[i] = r
    return np.array(qamaskFull),np.array(vzaFull),np.array(szaFull),\
                                np.array(raaFull),np.array(refl)

def getrefl():
    from optparse import OptionParser

    parser = OptionParser()
    parser.add_option("-r", "--row", dest="row",\
            action="store",type='int',help="starting row (y)",default=100)
    parser.add_option("-c", "--col", dest="col",\
            action="store",type='int',help="starting column (x)",default=100)
    parser.add_option("--nrow", "--nrows", dest="nrows",\
            action="store",type='int',help="number of rows (y)",default=1)
    parser.add_option("--ncol","--ncols", dest="ncols",\
            action="store",type='int',help="number of columns (x)",default=1)
    parser.add_option("-f", "--file", dest="file",\
            action="store",type='string',help="Name for output data file",default=None)
    parser.add_option("-p","--plot",action="store_true", dest="plot")
    parser.add_option("--plotfile",dest="plotfile",\
            action="store",type='string',help="Name for plotfile",default=None)
    parser.add_option("-v","--verbose",action="store_true", dest="verbose")
    parser.add_option("-d", "--datadir", dest="datadir",\
            action="store",type='string',help="Name for input data directory",default=None)

    (options, args) = parser.parse_args()
    # directory for data files
    base = '/data/geospatial_20/plewis/geogg124'
    # list of filenames
    if options.datadir == None:
        options.datadir = base + '/data'
    # get all of the files & work out the dates
    import glob
    Rdata = glob.glob(options.datadir + '/*GHK*hdf')
    Adata = glob.glob(options.datadir + '/*GGAD*hdf')
    Qdata = glob.glob(options.datadir + '/*GST*hdf')
    Rdates = np.array([(i.split('/')[-1]).split('.')[1][-3:] for i in Rdata])
    Adates = np.array([(i.split('/')[-1]).split('.')[1][-3:] for i in Adata])
    Qdates = np.array([(i.split('/')[-1]).split('.')[1][-3:] for i in Qdata])
    alldates = np.append(Rdates,Adates)
    alldates = np.append(alldates,Qdates)
    alldates = np.unique(alldates)
    alldates.sort()
    # now for each date, get all 3 files
    date = []
    angles = []
    qa = []
    refl = []
    for d in alldates:
        all = glob.glob(options.datadir+ '/MOD*.A????%s.*hdf'%d)
        all.sort()
        if len(all) ==3:
            date.append(int(d))
            refl.append(all[0])
            qa.append(all[1])
            angles.append(all[2])

    data = {'date':np.array(date),\
        'angles':np.array(angles),\
        'qa':np.array(qa),\
        'refl':np.array(refl)}

    data = getModis(data,options)

    ds = gdal.Open(options.datadir + '/MODIS-LC')
    landCover = ds.GetRasterBand(1).ReadAsArray(options.col,options.row,\
                                              options.ncols,options.nrows)

    lcovers = \
    "Water,Evergreen Needleleaf forest,Evergreen Broadleaf forest," + \
    "Deciduous needleleaf forest,Deciduous broadleaf forest,Mixed Forests," + \
    "Closed shrublands,Open shrublands,Woody Savannas,Savannas,Grasslands," + \
    "Permanent wetlands,Croplands,Urban and built-up," + \
    "Cropland/natural vegetation mosaic,Permanent snow and ice," + \
    "Barren or sparsely vegetated"
    landCoverLUT = lcovers.split(',')

    ds = gdal.Open(options.datadir + '/activeFire_2003')
    activeFire = ds.GetRasterBand(1).ReadAsArray(options.col,options.row,\
                                              options.ncols,options.nrows)

    # PLOT
    if options.plot:
        if options.verbose:
            print ('plotting ...')
        ds = data['reflectance'].shape
        nt = ds[0]
        nbands = ds[1]
        npixels = ds[2] * ds[3]
        import pylab as plt
        mask = data['mask'][:,0,0]
        f = plt.figure()
        size = f.get_size_inches()
        f.set_figheight(size[1]*2.)
        if mask.sum():
            for i in range(nbands):
                this = data['reflectance'][:,i,0,0]
                doy = data['doy']
                plt.subplot(nbands,1,i+1)
                plt.plot(doy[mask],this[mask],'o')
                plt.ylabel('r %d'%(int(data['wavebands'][i])))
        plt.xlabel('doy')
        try:
            plt.subplot(nbands,1,1)
            plt.title('(%d,%d) %s (fire %d)'%(options.col,options.row,\
                                      landCoverLUT[landCover[0,0]],\
                                      activeFire[0,0]))
        except:
            pass
        if options.plotfile:
           if options.verbose:
               print (' ... to',options.plotfile)
           plt.savefig(options.plotfile)
        else:
           plt.show()
    # output pickle file
    if options.file != None:
        if options.verbose:
            print ('saving data to',options.file)
        np.savez(options.file,doy=data['doy'],\
                             angles=data['angles'],\
                             qa=data['qa'],\
                             landcover=landCover,\
                             landCoverLUT=landCoverLUT,\
                             activeFire=activeFire,\
                             wavebands=data['wavebands'],\
                             mask=data['mask'],\
                             vza=data['vza'],\
                             sza=data['sza'],\
                             raa=data['raa'],\
                             refl=data['refl'],\
                             reflectance=data['reflectance'])

def main():
    getrefl()

if __name__ == "__main__":
    main()


'''
bucket sort
'''
import insertion_sort

# def insertion_sort(input):
#     for iii in range(1, len(input)):
#         key  = input[iii]
#         while iii > 0 and key < input[iii-1]:
#             input[iii-1], input[iii] = input[iii], input[iii-1]
#             iii -= 1
#     return input

def bucket_sort(input):
    import math
    n = len(input)
    temp = [[] for x in range(n)]
    for iii in range(n):
        temp[math.floor(n*input[iii])].append(input[iii])
    result = []
    for iii in range(n):
        if temp[iii] != []:
            result += insertion_sort(temp[iii])
    return result

test = [0.78,0.17,0.39,0.26,0.72,0.94,0.21,0.12,0.23,0.68]
bucket_sort(test)


#!/usr/bin/env python
import glob
import argparse
import logging
import re
import os
import pandas as pd
import matplotlib.pyplot as plt
import json

global data
data = {"names":[], "total_reads":[], "total_bases":[], "q30_rate":[], "duplication":[]}
def main():
    parser = argparse.ArgumentParser(prog=__file__, formatter_class=argparse.ArgumentDefaultsHelpFormatter, description=__doc__)
    parser.add_argument('-fastp',  "--fastp", required=True, help="folder of fastp")
    parser.add_argument('-output',  "--output", required=True, help="folder for fastp stats statistics file")
    parser.add_argument('-v', '--verbose', action='count', default=0, help="Increase verbosity")
    parser.add_argument('-q', '--quiet', action='count', default=0, help="Decrease verbosity")
    args = parser.parse_args()

    logging_level = logging.WARN + 10*args.quiet - 10*args.verbose
    logging.basicConfig(level=logging_level)
    logger = logging.getLogger(__name__)
    logger.debug('args verbose {}:'.format(args.verbose))
    logger.debug('args quiet {}:'.format(args.quiet))
    logger.debug('logging_level {}:'.format(logging_level))
    fns = getfiles(args.fastp)
    [proc_fastp(fn) for fn in fns]
    df = pd.DataFrame.from_dict(data)

    if not os.path.exists(args.output):
         os.makedirs(args.output, exist_ok=True)

    df.to_csv(args.output + "/fastp.stats.csv", index=False)
    #print(df)
    #plot(df)

def getfiles(d):
    fns = glob.glob(d + "/*.fastp.json")
    return fns

def proc_fastp(fn):
    basename = os.path.basename(fn)
    reResult = re.search("(.*).fastp.json", basename)
    if reResult:
        sample_name = reResult.group(1)
        data['names'].append(sample_name)

    with open(fn, newline='') as jsonfile:
        myjson = json.load(jsonfile)
        total_reads = myjson['summary']['before_filtering']['total_reads']
        data['total_reads'].append(total_reads)
        total_bases = myjson['summary']['before_filtering']['total_bases']
        data['total_bases'].append(total_bases)
        q30_rate = myjson['summary']['before_filtering']['q30_rate']
        data['q30_rate'].append(q30_rate)
        duplication = myjson['duplication']['rate']
        data['duplication'].append(duplication)

def plot(data):
    df = data[['>0x', '>=5x', '>=10x', '>=20x', '>=30x', '>=40x', '>=50x', '>=60x', '>=70x', '>=80x', '>=90x', '>=100x']]
    df.index = data['names']
    df = df.T
    #print(df)
    line = df.plot.line()
    plt.show()

if __name__ == '__main__':
    main()


# 2875번 대회 or 인턴
# https://www.acmicpc.net/problem/2875

N, M, K = map(int, input().split())
cnt = 0

for k in range(K+1):
    n = N - k
    m = M - K + k
    if n // 2 > m:
        cnt = max(cnt, m)
    else:
        cnt = max(cnt, n//2)

print(cnt)


from django.conf.urls import url
from . import views

app_name = 'blog'

urlpatterns = [
   url(r'^$', views.blog_list, name="list"),
   url(r'^create/$', views.blog_create, name="create"),
   url(r'^(?P<slug>[\w-]+)/$', views.blog_detail, name="detail"),
]


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

from keras.models import Sequential,Model
from keras.layers import Dense, Reshape, Flatten, Activation,Input,merge

from keras.optimizers import Adam
from keras.layers.convolutional import Convolution2D

import networkx as nx
import pylab as plt
import numpy as np
import logging.config

import random

class PGAgent:
	def __init__(self, state_size, action_size):
		self.state_size = state_size
		self.action_size = action_size
		self.gamma = 0.99
		self.learning_rate = 0.001
		self.states = []
		self.gradients = []
		self.rewards = []
		self.probs = []
		self.model = self._build_model()
		self.model.summary()

	def _build_model(self):

		model = Sequential()
		model.add(Reshape((1, 2, 64), input_shape=(self.state_size,)))
		model.add(Convolution2D(2, 2, 4, subsample=(2, 2), border_mode='same',activation='relu', init='he_uniform'))
		model.add(Flatten())
		model.add(Dense(16, activation='relu', init='he_uniform'))
		model.add(Dense(8, activation='relu', init='he_uniform'))
		model.add(Dense(self.action_size, activation='softmax'))
		opt = Adam(lr=self.learning_rate)
		model.compile(loss='categorical_crossentropy', optimizer=opt)

		return model

	def remember(self, state, action, prob, reward):
		y = np.zeros([self.action_size])
		y[action] = 1
		self.gradients.append(np.array(y).astype('float32') - prob)
		self.states.append(state)
		self.rewards.append(reward)

	def act(self, state):

		state = state.reshape([1, state.shape[0]])

		#print("STATETOMODEL::",state)

		aprob = self.model.predict(state, batch_size=3).flatten()

		self.probs.append(aprob)
		#prob = aprob / np.sum(aprob)
		action = np.random.choice(self.action_size, 1, p=aprob)[0] #exploration and exploitation
		#[]

		#print('np.sum(aprob):', np.sum(aprob))
		#print("aprob::::", aprob)

		#print("ACTION:", action)

		return action, aprob

	def discount_rewards(self, rewards):
		discounted_rewards = np.zeros_like(rewards)
		running_add = 0
		for t in reversed(range(0, rewards.size)):
			if rewards[t] != 0:
				running_add = 0
			running_add = running_add * self.gamma + rewards[t]
			discounted_rewards[t] = running_add
		return discounted_rewards

	def train(self):
		gradients = np.vstack(self.gradients)
		rewards = np.vstack(self.rewards)
		rewards = self.discount_rewards(rewards)
		rewards = (rewards - np.mean(rewards)) / (np.std(rewards) - np.mean(rewards))
		 #normalize

		#print('rewards::', rewards)

		gradients *= rewards
		X = np.squeeze(np.vstack([self.states]))
		Y = self.probs + self.learning_rate * np.squeeze(np.vstack([gradients]))

		self.model.train_on_batch(X, Y)
		self.states, self.probs, self.gradients, self.rewards = [], [], [], []

	def load(self, name):
		self.model.load_weights(name)

	def save(self, name):
		self.model.save_weights(name)

class Environment:

	def __init__(self):
		self.__version__ = "0.1.0"
		logging.info("BananaEnv - Version {}".format(self.__version__))

		self.points_list=[(100,0),(0,1),(0,2),(1,3),(1,4),(2,5),(2,6),(3,7),(3,8),(4,9),
                (4,10),(5,11),(5,12),(6,13),(6,14),(7,15),(7,16), (8,17),
                (8,18),(9,19),(9,20),(10,21),(10,22),(11,23),(11,24),(12,25),
                (12,26), (13,27),(13,28),(14,29),(14,30),(15,31),(15,32),(16,33),
                (16,34),(17,35),(17,36),(18,37),(18,39),(19,40),(19,41),(20,42),(20,43),
                (21,44),(21,45),(22,46),(22,47),(23,48),(23,49),(24,50),(24,51),(25,52),(25,53),
                (26,54),(26,55),(27,56),(27,57),(28,58),(28,59),(29,60),(29,61),(30,62),(30,63)]

		self.attrs = { # all nodes have [1,0] label

        0: {'attr': [[0,1]]}, 1: {'attr': [[0,1]]},
        2: {'attr': [[0,1]]}, 5: {'attr': [[0,1]]},  12: {'attr': [[0,1]]},25: {'attr': [[0,1]]},
        52: {'attr': [[0,1]]}, 6: {'attr': [[0,1]]}, 14: {'attr': [[0,1]]},  8: {'attr': [[0,1]]}, 27: {'attr': [[0,1]]}
        }

		self.G=nx.DiGraph()
		self.G.add_edges_from(self.points_list)

		self.labels = []
		nx.set_node_attributes(self.G, self.labels, 'attr')
		self.labels.append([1,0])

		nx.set_node_attributes(self.G, self.attrs)

		self.state = 100

		self.done = self.is_over() # True or False

		self.curr_episode = -1

		self.action_episode_memory = []
		self.reward_episode_memory = []

	def step(self, action): #action is 0 for left 1 for right

		self.do_action(action)#the state is updated
		ob = self._get_state()
		reward = self._get_reward()
		done = self.is_over()

		return ob,reward,done

	def _neighbors(self):
		tmp = []
		for i in self.G.neighbors(self.state):
			tmp.append(i)
		return tmp

	def do_action(self,action): #0

		self.action_episode_memory[self.curr_episode].append(action)

		neighbors = self._neighbors() # [7,8]
		if len(neighbors) ==1:
			#print("Len neighbors: ", len(neighbors))
			self.state=0

		else:
			#print("Len neighbors: ", len(neighbors))
			self.state = neighbors[action] #neighbors[0] = 7

	def _get_reward(self):
		if self.G.nodes[self.state]['attr'][0] == [0,1]:
			self.reward_episode_memory[self.curr_episode].append(0.2)
			return 0.2
		else:

			if self.G.nodes[self.state]['attr'][0] == [1,0]:
				self.reward_episode_memory[self.curr_episode].append(0.0)
				return 0
			else:
				raise Exception("Go to get reward something wrong with node label")

	def reset(self):
		self.state = 100
		self.curr_episode += 1 #-1 ==>0 for the first time
		self.action_episode_memory.append([])
		self.reward_episode_memory.append([])
		return self.state

	def is_over(self):
		neighbors = self._neighbors()
		if len(neighbors) == 0:
			return True
		else:
			return False

	def _get_state(self):
		return self.state #just returns the state

if __name__ == "__main__":

	env = Environment()
	state = env.reset()
	score = 0
	episode = 0
	path = []
	x_data = []
	y_data = []

	state_size = 64 * 2 #number of nodes in graph
	action_size = 2

	agent = PGAgent(state_size, action_size)

	state, reward, done  = env.step(0)
	i = 0
	while i<10000:

		base = np.zeros((64,2))
		base[state][0] = 1

		#print("base::",base)

		action, prob = agent.act(base.astype(np.float).ravel())

		state, reward, done  = env.step(action)

		path.append(state)
		score += reward
		#print(done)

		base = np.zeros((64,2))
		base[state][0] = 1 # = [0,0,0,0,0,1,0,0,0,...]

		agent.remember(base.astype(np.float).ravel(), action, prob, reward)

		if done==True:
			i+=1
			episode += 1
			agent.train()
			print("path:", path)
			path = []
			print('Episode: %d - Score: %f.' % (episode, score))
			if i%10==0:
				x_data.append(score)
				y_data.append(episode)
			score = 0
			state = env.reset()
			state, reward, done  = env.step(0)

# Plot the data
	plt.plot(y_data, x_data, label='linear')
# Add a legend
	plt.legend()
# Show the plot
	plt.show()


import discord, motor, asyncio, Core.utils as util, Core.confirm as cop_is_gae, typing
from discord.ext import commands
from typing import List
import discord.ui
from Core import utils as util
from datetime import datetime
from Core.utils import get_theme

class autoResponsEEvent(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        self.db = self.bot.db['autoResponder']
        self._cd = commands.CooldownMapping.from_cooldown(1, 6.0, commands.BucketType.member)

    def get_ratelimit(self, message: discord.Message) -> typing.Optional[int]:
        """Returns the ratelimit left"""
        bucket = self._cd.get_bucket(message)
        return bucket.update_rate_limit()

    @commands.Cog.listener()
    async def on_message(self, message):
        try:
            if message.guild is None:
                return
            if message.author == self.bot.user:
                return
            if message.author.bot:
                return
            ctx=await self.bot.get_context(message)
            check = await self.db.count_documents({ "guild_id": message.guild.id })
            mention=f"<@!{self.bot.user.id}>"
            mention2=f"<@{self.bot.user.id}>"
            if message.content == mention or message.content == mention2:
                ratelimit = self.get_ratelimit(message)
                if ratelimit is None:
                    prefix=await util.get_prefix(self=ctx, bot=self.bot, ctx=ctx)
                    try:
                        return await message.channel.send(embed=discord.Embed(color=int(await get_theme(self, bot=self.bot, guild=ctx.guild.id), 16), description=f":mag: Prefix = **{prefix}**"))
                    except:
                        pass
                else:
                    return
            if check:
                check_existing = self.db.find({ "guild_id": message.guild.id })
                for trigger in await check_existing.to_list(length=20):
                    found = trigger["trigger"]
                    if found in message.content.lower():
                        ratelimit = self.get_ratelimit(message)
                        if ratelimit is None:
                            response = trigger['response']
                            channel = message.channel
                            try:
                                return await channel.send(response)
                            except Exception as e:
                                print (e)
                        else:
                            return

            else:
                return
        except:
            pass; return

async def setup(bot):
    await bot.add_cog(autoResponsEEvent(bot))


import allel
import gzip
import numpy as np
import os
import pandas as pd
import random
from scipy.interpolate import interp1d
import string
import sys
from time import time
import pickle
import gzip

def save_dict(D, path):
    with open(path, 'wb') as handle:
        pickle.dump(D, handle, protocol=pickle.HIGHEST_PROTOCOL)

def load_dict(path):
    if not os.path.exists(path):
        return {}
    with open(path, 'rb') as handle:
        return pickle.load(handle)

def get_num_outs(sample_map_paths, r_admixed=1.0):
    # r_admixed: generated r_admixed * num-founders for each set
    # TODO: cap train2 lengths to a pre-defined value.
    num_outs = []
    for path in sample_map_paths:
        with open(path,"r") as f:
            length = len(f.readlines()) # how many founders.
            num_outs.append(int(length *r_admixed))
    return num_outs

def run_shell_cmd(cmd, verb=True):
    if verb:
        print("Running:", cmd)
    rval = os.system(cmd)
    if rval != 0:
        signal = rval & 0xFF
        exit_code = rval >> 8
        if signal != 0:
            sys.stderr.write("\nCommand %s exits with signal %d\n\n" % (cmd, signal))
            sys.exit(signal)
        sys.stderr.write("\nCommand %s failed with return code %d\n\n" % (cmd, exit_code))
        sys.exit(exit_code)

def join_paths(p1,p2="",verb=True):
    path = os.path.join(p1,p2)
    if not os.path.exists(path):
        os.makedirs(path)
        if verb:
            print("path created:", path)
    return path

def read_vcf(vcf_file, chm=None, fields=None, verbose=False):
    """
    Wrapper function for reading vcf files into a dictionary
    fields="*" extracts more information, take out if ruled unecessary
    """
    if fields is None:
        # fields = ['variants/CHROM', 'variants/POS', 'calldata/GT', 'variants/REF', 'samples']
        fields = "*"

    if vcf_file[-3:]==".gz":
        with gzip.open(vcf_file, 'rb') as vcf:
            data = allel.read_vcf(vcf,  region=chm, fields=fields)
    else:
        data = allel.read_vcf(vcf_file, region=chm, fields=fields)

    if data is None:
        if chm is None:
            print("No data found in vcf file {}".format(vcf_file))
        else:
            print('Found no data in vcf file {} in region labeled "{}". Using all data from vcf instead...'.format(vcf_file, chm))
            return read_vcf(vcf_file, None, fields, verbose)

    if verbose:
        chmlen, n, _ = data["calldata/GT"].shape
        print("File read:", chmlen, "SNPs for", n, "individuals")

    return data

def snp_intersection(pos1, pos2, verbose=False):
    """
    Finds interception of snps given two arrays of snp position
    in O(max[size(pos1), size(pos2)])
    """

    assert len(pos2) != 0, "No SNPs of specified chromosome found in query file."

    intersection, idx1, idx2 = np.intersect1d(pos1, pos2, return_indices=True)

    if verbose:
        print("- Number of SNPs from model:", len(pos1))
        print("- Number of SNPs from file:",  len(pos2))
        print("- Number of intersecting SNPs:", len(intersection))
        intersect_percentage = round(len(intersection)/len(pos1),4)*100
        print("- Percentage of model SNPs covered by query file: ",
              intersect_percentage, "%", sep="")

    return idx1, idx2

def vcf_to_npy(vcf_data, snp_pos_fmt=None, snp_ref_fmt=None, miss_fill=2, return_idx=False, verbose=True):
    """
    Converts vcf file to numpy matrix.
    If SNP position format is specified, then comply with that format by filling in values
    of missing positions and ignoring additional positions.
    If SNP reference variant format is specified, then comply with that format by swapping where
    inconsistent reference variants.
    Inputs
        - vcf_data: already loaded data from a vcf file
        - snp_pos_fmt: desired SNP position format
        - snp_ref_fmt: desired reference variant format
        - miss_fill: value to fill in where there are missing snps
    Outputs
        - npy matrix on standard format
    """

    # reshape binary represntation into 2D np array
    data = vcf_data["calldata/GT"]
    chm_len, n_ind, _ = data.shape
    data = data.reshape(chm_len,n_ind*2).T
    mat_vcf_2d = data
    vcf_idx, fmt_idx = np.arange(n_ind*2), np.arange(n_ind*2)

    if snp_pos_fmt is not None:
        # matching SNP positions with standard format (finding intersection)
        vcf_pos = vcf_data['variants/POS']
        fmt_idx, vcf_idx = snp_intersection(snp_pos_fmt, vcf_pos, verbose=verbose)
        # only use intersection of variants (fill in missing values)
        fill = np.full((n_ind*2, len(snp_pos_fmt)), miss_fill)
        fill[:,fmt_idx] = data[:,vcf_idx]
        mat_vcf_2d = fill

    if snp_ref_fmt is not None:
        # adjust binary matrix to match model format
        # - find inconsistent references
        vcf_ref = vcf_data['variants/REF']
        swap = vcf_ref[vcf_idx] != snp_ref_fmt[fmt_idx] # where to swap w.r.t. intersection
        if swap.any() and verbose:
            swap_n = sum(swap)
            swap_p = round(np.mean(swap)*100,4)
            print("- Found ", swap_n, " (", swap_p, "%) different reference variants. Adjusting...", sep="")
        # - swapping 0s and 1s where inconsistent
        fmt_swap_idx = np.array(fmt_idx)[swap]  # swap-index at model format
        mat_vcf_2d[:,fmt_swap_idx] = (mat_vcf_2d[:,fmt_swap_idx]-1)*(-1)

    # make sure all missing values are encoded as required
    missing_mask = np.logical_and(mat_vcf_2d != 0, mat_vcf_2d != 1)
    mat_vcf_2d[missing_mask] = miss_fill

    mat_vcf_2d = mat_vcf_2d.astype(np.int8)

    # return npy matrix
    if return_idx:
        return mat_vcf_2d, vcf_idx, fmt_idx

    return mat_vcf_2d

def read_genetic_map(genetic_map_path, chm=None, header=None):

    gen_map_df = pd.read_csv(genetic_map_path, delimiter="\t", header=header, comment="#", dtype=str)
    gen_map_df.columns = ["chm","pos","pos_cm"]

    try:
        gen_map_df = gen_map_df.astype({'chm': str, 'pos': int, 'pos_cm': float})
    except ValueError:
        if header is None:
            print("WARNING: Something wrong with genetic map format. Trying with header...")
            return read_genetic_map(genetic_map_path, chm=chm, header=0)
        else:
            raise Exception("Genetic map format not understood.")

    if chm is not None:
        chm = str(chm)
        if len(gen_map_df[gen_map_df.chm == chm]) == 0:
            gen_map_df = gen_map_df[gen_map_df.chm == "chr"+chm]
        else:
            gen_map_df = gen_map_df[gen_map_df.chm == chm]

    return gen_map_df

def cM2nsnp(cM, chm_len_pos, genetic_map, chm=None):

    if type(genetic_map) == str:
        if chm is not None:
            gen_map_df = read_genetic_map(genetic_map, chm)
        else:
            print("Need chromosome number to read genetic map")
    else:
        gen_map_df = genetic_map

    chm_len_cM = np.array(gen_map_df["pos_cm"])[-1]
    snp_len = int(round(cM*(chm_len_pos/chm_len_cM)))

    return snp_len

def fb2proba(path_to_fb, n_wind=None):

    with open(path_to_fb) as f:
        header = f.readline().split("\n")[0]
        ancestry = np.array(header.split("\t")[1:])
    A = len(ancestry)

    fb_df = pd.read_csv(path_to_fb, sep="\t", skiprows=[0])

    samples = [s.split(":::")[0] for s in fb_df.columns[4::A*2]]

    # Probabilities in snp space
    fb = np.array(fb_df)[:,4:]
    C, AN = fb.shape
    N = AN//A
    fb_reshaped = fb.reshape(C, N, A)      # (C, N, A)
    proba = np.swapaxes(fb_reshaped, 0, 1) # (N, C, A)

    # Probabilities in window space
    if n_wind is not None:
        gen_pos = np.array(fb_df['genetic_position'])
        w_cM = np.arange(gen_pos[0], gen_pos[-1], step = gen_pos[-1]/n_wind)
        f = interp1d(gen_pos, np.arange(C), fill_value=(0, C), bounds_error=False)
        w_idx = f(w_cM).astype(int)
        proba = proba[:,w_idx,:]

    return proba

def update_vcf(vcf_data, mask=None, Updates=None):

    out = vcf_data.copy()

    if mask is not None:
        for key in vcf_data.keys():
            if key != "samples":
                out[key] = vcf_data[key][mask]

    if Updates is not None:
        for key in Updates.keys():
            if key != "samples":
                out[key] = Updates[key]

    return out

def get_name(name_len=8):
    letters = string.ascii_lowercase + string.ascii_uppercase + string.digits
    return ''.join(random.choice(letters) for i in range(name_len))

def npy_to_vcf(reference, npy, results_file, verbose=False, headers=""):
    """
    - reference: str path to reference file which provides metadata for the results
                 or alternatively, a allel.read_vcf output
    - npy: npy matrix - shape: (num_samples, num_snps)
           make sure npy file has same snp positions
    - results_file: str output vcf path
    - headers: what to put on top - make sure ends with \n

    this is a very light version of npy_to_vcf for LAI applications

    Function behavior
    a vcf file called <results_file> with data in npy file and metadata from reference
    - metadata includes all fields except for genotype data
    - npy file must follow convention where maternal and paternal sequences appear one after the other
      for each sample

    NOTE: New to production. Has not been bullet-tested.
    """

    if results_file.split(".")[-1] not in [".vcf", ".bcf"]:
        results_file += ".vcf"

    # read in the input vcf data
    if type(reference) == str:
        data = allel.read_vcf(reference)
    else:
        data = reference.copy()

    # infer chromosome length and number of samples
    npy = npy.astype(int)
    chmlen, _, _ = data["calldata/GT"].shape
    h, c = npy.shape
    n = h//2
    assert chmlen == c, "reference (" + str(chmlen) + ") and numpy matrix (" + str(c) + ") not compatible"

    # Keep sample names if appropriate
    if "samples" in list(data.keys()) and len(data["samples"]) == n:
        if verbose:
            print("Using same sample names")
        data_samples = data["samples"]
    else:
        data_samples = [get_name() for _ in range(n)]

    # metadata
    df = pd.DataFrame()
    df["CHROM"]  = data["variants/CHROM"]
    df['POS']    = data["variants/POS"]
    df["ID"]     = data["variants/ID"]
    df["REF"]    = data["variants/REF"]
    df["ALT"]    = data["variants/ALT"][:,0] # ONLY THE FIRST SINCE WE ONLY CARE ABOUT BI-ALLELIC SNPS HERE FOR NOW
    df["QUAL"]   = data["variants/QUAL"]
    df["FILTER"] = ["PASS"]*chmlen
    df["INFO"]   = ["."]*chmlen
    df["FORMAT"] = ["GT"]*chmlen

    # genotype data for each sample
    for i in range(n):

        # get that particular individual's maternal and paternal snps
        maternal = npy[i*2,:].astype(str) # maternal is the first
        paternal = npy[i*2+1,:].astype(str) # paternal is the second

        # create "maternal|paternal"
        lst = [maternal, ["|"]*chmlen, paternal]
        genotype_person = list(map(''.join, zip(*lst)))
        df[data_samples[i]] = genotype_person

    if verbose:
        print("writing vcf data in "+results_file)

    # write header
    with open(results_file,"w") as f:
        f.write(headers)
        f.write("##fileformat=VCFv4.1\n")
        f.write("##source=gnomix.py\n")
        f.write('##FORMAT=<ID=GT,Number=1,Type=String,Description="Phased Genotype">\n')
        f.write("#"+"\t".join(df.columns)+"\n") # mandatory header

    # genotype data
    df.to_csv(results_file,sep="\t",index=False,mode="a",header=False)

    return

def read_headers(vcf_file):

    header = ""

    if vcf_file.endswith(".vcf"):
        with open(vcf_file,"r") as f:
            for line in f.readlines():
                if line[0:2] == "##":
                    header += line

    elif vcf_file.endswith(".vcf.gz"):
        with gzip.open(vcf_file, "r") as f:
            for line in f.readlines():
                if line[0:2].decode("utf-8") == "##":
                    header += line.decode("utf-8")

    return header


# -*- coding: utf-8 -*-
"""
Created on Tue May 26 16:00:14 2020

@author: Administrator
"""

from __future__ import print_function

import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from PIL import Image
import matplotlib.pyplot as plt
import torchvision.transforms as transforms
import torchvision.models as models
import copy
import numpy as np
import pandas as pd
import cv2
import os
from torch.autograd import Variable
from torch.nn import Linear, ReLU, CrossEntropyLoss, Sequential, Conv2d, MaxPool2d, Module, Softmax, BatchNorm2d, Dropout
from torch.optim import Adam, SGD
import random
from torch.utils.data.sampler import SubsetRandomSampler
import glob
import skimage.io as io
from skimage.transform import rotate, AffineTransform, warp
from skimage.util import random_noise
from skimage.filters import gaussian
import torchvision.datasets as datasets

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

class ContentLoss(nn.Module):

    def __init__(self, target,):
        super(ContentLoss, self).__init__()
        self.target = target.detach()

    def forward(self, input):
        self.loss = F.mse_loss(input, self.target)
        return input

def gram_matrix(input):
    a, b, c, d = input.size()  # a=batch size(=1)
    # b=number of feature maps
    # (c,d)=dimensions of a f. map (N=c*d)

    features = input.view(a * b, c * d)  # resise F_XL into \hat F_XL

    G = torch.mm(features, features.t())  # compute the gram product

    # we 'normalize' the values of the gram matrix
    # by dividing by the number of element in each feature maps.
    return G.div(a * b * c * d)

class StyleLoss(nn.Module):

    def __init__(self, target_feature):
        super(StyleLoss, self).__init__()
        self.target = gram_matrix(target_feature).detach()

    def forward(self, input):
        G = gram_matrix(input)
        self.loss = F.mse_loss(G, self.target)
        return input

cnn = models.vgg19(pretrained=True).features.to(device).eval()
cnn_normalization_mean = torch.tensor([0.485, 0.456, 0.406]).to(device)
cnn_normalization_std = torch.tensor([0.229, 0.224, 0.225]).to(device)

# create a module to normalize input image so we can easily put it in a
# nn.Sequential
class Normalization(nn.Module):
    def __init__(self, mean, std):
        super(Normalization, self).__init__()
        # .view the mean and std to make them [C x 1 x 1] so that they can
        # directly work with image Tensor of shape [B x C x H x W].
        # B is batch size. C is number of channels. H is height and W is width.
        self.mean = torch.tensor(mean).view(-1, 1, 1)
        self.std = torch.tensor(std).view(-1, 1, 1)

    def forward(self, img):
        # normalize img
        return (img - self.mean) / self.std

content_layers_default = ['conv_4']
style_layers_default = ['conv_1', 'conv_2', 'conv_3', 'conv_4', 'conv_5']

def get_style_model_and_losses(cnn, normalization_mean, normalization_std,
                               style_img, content_img,
                               content_layers=content_layers_default,
                               style_layers=style_layers_default):
    cnn = copy.deepcopy(cnn)

    # normalization module
    normalization = Normalization(normalization_mean, normalization_std).to(device)

    # just in order to have an iterable access to or list of content/syle
    # losses
    content_losses = []
    style_losses = []

    # assuming that cnn is a nn.Sequential, so we make a new nn.Sequential
    # to put in modules that are supposed to be activated sequentially
    model = nn.Sequential(normalization)

    i = 0  # increment every time we see a conv
    for layer in cnn.children():
        if isinstance(layer, nn.Conv2d):
            i += 1
            name = 'conv_{}'.format(i)
        elif isinstance(layer, nn.ReLU):
            name = 'relu_{}'.format(i)
            # The in-place version doesn't play very nicely with the ContentLoss
            # and StyleLoss we insert below. So we replace with out-of-place
            # ones here.
            layer = nn.ReLU(inplace=False)
        elif isinstance(layer, nn.MaxPool2d):
            name = 'pool_{}'.format(i)
        elif isinstance(layer, nn.BatchNorm2d):
            name = 'bn_{}'.format(i)
        else:
            raise RuntimeError('Unrecognized layer: {}'.format(layer.__class__.__name__))

        model.add_module(name, layer)

        if name in content_layers:
            # add content loss:
            target = model(content_img).detach()
            content_loss = ContentLoss(target)
            model.add_module("content_loss_{}".format(i), content_loss)
            content_losses.append(content_loss)

        if name in style_layers:
            # add style loss:
            target_feature = model(style_img).detach()
            style_loss = StyleLoss(target_feature)
            model.add_module("style_loss_{}".format(i), style_loss)
            style_losses.append(style_loss)

    # now we trim off the layers after the last content and style losses
    for i in range(len(model) - 1, -1, -1):
        if isinstance(model[i], ContentLoss) or isinstance(model[i], StyleLoss):
            break

    model = model[:(i + 1)]

    return model, style_losses, content_losses

# Hyperparameters for our network
input_size = 784
hidden_sizes = [64, 128]
output_size = 10
# Build a feed-forward network
NetworkB = nn.Sequential(nn.Linear(input_size, hidden_sizes[0]),
                      nn.ReLU(),
                      nn.Linear(hidden_sizes[0], hidden_sizes[1]),
                      nn.ReLU(),
                      nn.Linear(hidden_sizes[1], output_size),
                      nn.Softmax(dim=1))
print(NetworkB)

def Optimizer_netA(input_img):
    # this line to show that input is a parameter that requires a gradient
    optimizer_netA = optim.LBFGS([input_img.requires_grad_()])
    return optimizer_netA

def run_style_transfer(cnn, normalization_mean, normalization_std,
                       content_img, style_img, input_img, num_steps=300,
                       style_weight=1000000, content_weight=1):
    """Run the style transfer."""
    print('Building the style transfer model..')
    model, style_losses, content_losses = get_style_model_and_losses(cnn,
        normalization_mean, normalization_std, style_img, content_img)
    optimizer_netA = Optimizer_netA(input_img)

    print('Optimizing..')
    run = [0]
    while run[0] <= num_steps:

        def closure():
            # correct the values of updated input image
            input_img.data.clamp_(0, 1)

            optimizer_netA.zero_grad()
            model(input_img)
            style_score = 0
            content_score = 0

            for sl in style_losses:
                style_score += sl.loss
            for cl in content_losses:
                content_score += cl.loss

            style_score *= style_weight
            content_score *= content_weight

            loss_netA = style_score + content_score
            loss_netA.backward()

            run[0] += 1
            if run[0] % 50 == 0:
                print("run {}:".format(run))
                print('Style Loss : {:4f} Content Loss: {:4f}'.format(
                    style_score.item(), content_score.item()))
                print()

            return style_score + content_score

        optimizer_netA.step(closure)

    # a last correction...
    input_img.data.clamp_(0, 1)

    return input_img

#def save_augment_images(img):
 #   cv2.imwrite('C:\Swati\Thesis Project\Code\Swati_Code\Dataset\Augmented Images\Aug1.jpg', img)
  #  print('Augmented images saved successfully')

#save_augment_images(output)

def image_loader(image_name):
    # desired size of the output image
    imsize = 512 if torch.cuda.is_available() else 128  # use small size if no gpu
    Orinal_loader = transforms.Compose([
        transforms.Resize(imsize),  # scale imported image
        transforms.ToTensor(),                 #convert the image to a Tensor
        ])
    image = Image.open(image_name)
    image = Orinal_loader(image).unsqueeze(0)
    return image.to(device, torch.float)

dir_name="C:/Swati/Thesis Project/Code/Swati_Code/Dataset"
base_files=os.listdir(dir_name)
Files = []
for file in base_files:
    Images = os.path.join(dir_name, file)
    Files.append(Images)

epoch = 10

for i in range(epoch):
    total_loss = 0
    print("Augmentation starts")
    Original_img = image_loader(random.choice(Files))
    Augment_img = image_loader(random.choice(Files))

    input_img = Original_img.clone()
    #input_img = torch.randn(Original_img.data.size(), device=device)

    print(Original_img.shape)
    print(Augment_img.shape)

    assert Original_img.size() == Augment_img.size(), \
        "we need to import style and content images of the same size"

    unloader = transforms.ToPILImage()

    plt.ion()

    def imshow(tensor, title=None):
        image = tensor.cpu().clone()  # we clone the tensor to not do changes on it
        image = image.squeeze(0)      # remove the fake batch dimension
        image = unloader(image)
        plt.imshow(image)
        if title is not None:
            plt.title(title)
        plt.pause(0.001) # pause a bit so that plots are updated

    plt.figure()
    imshow(Original_img, title='Random Image_1')

    plt.figure()
    imshow(Augment_img, title='Random Image_2')

    output = run_style_transfer(cnn, cnn_normalization_mean, cnn_normalization_std, Original_img,
                            Augment_img, input_img)

    plt.figure()
    imshow(output, title='Output Image')

    # sphinx_gallery_thumbnail_number = 4
    plt.ioff()
    plt.show()

    print("augment data shape", output.shape)
    outputs = NetworkB(output)

    optimizer_netB = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
    running_loss = 0.0
    criterion_netB = nn.CrossEntropyLoss()

    loss_netB = criterion_netB(outputs)
    loss_netB.backward()
    optimizer_netB.step()

    #loss = self.alpha * loss_netA + self.beta * loss_netB
    #total_loss += loss.data[0]

# zero the parameter gradients
    optimizer_netB.zero_grad()

# print statistics
    running_loss += loss_netB.item()

    if i % 2000 == 1999:    # print every 2000 mini-batches
        print('[%d, %5d] loss: %.3f' %
          (epoch + 1, i + 1, running_loss / 2000))
    running_loss = 0.0

print('Finished Training')


import xlwt
import xlrd
from xlutils.copy import copy

excel_path="D:\PythonProject\data_excel\五中心.xls"
row_i=3
col_j=13
text="你好我的中国"
def write_excel(excel_path,row_i,col_j,text):
    # 打开想要更改的excel文件
    old_excel=xlrd.open_workbook(excel_path,formatting_info=True)
    # 将操作文件对象拷贝，变成可写的workbook对象
    new_excel=copy(old_excel)
    # 获得第一个sheet的对象
    new_sheet=new_excel.get_sheet(0)

    #预定一个字体相关的格式对象style
    style=xlwt.XFStyle()
    #字体参数相关配置
    font=xlwt.Font()
    font.name="Microsoft YaHei UI"
    font.bold = False #是否加粗，True为加粗
    font.height = 250 #  如果是200对应的是10号字体
    style.font = font
    #边框
    borders=xlwt.Borders()
    borders.top=xlwt.Borders.THIN
    borders.bottom=xlwt.Borders.THIN
    borders.left=xlwt.Borders.THIN
    borders.right=xlwt.Borders.THIN
    style.borders=borders
    #写入数据
    new_sheet.write(row_i-1, col_j-1,text,style)
    #另存为excel文件，并将文件命名如果与原来文件重名则修改原文件
    new_excel.save(excel_path)

write_excel(excel_path,row_i,col_j,text)

# import urllib.request
# import urllib.error
# import time
# #url="http://10.45.138.39:8380/bac-ia-account-microservice/account-relation/queryGroupAccountingRelationInfo"
# url="https://www.cnblogs.com/mcq1999/p/python_Crawler_1.html"
# class PortOperation:
#     def __init__(self):
#         print("----init----")
#     def __del__(self):
#         print("----del-----")
#     #捕获请求异常有正确返回后，再进行后续操作
#     def request_error(self,url):
#         request_status=""
#         for i in range(3):
#             try:
#                 #urllib.request.urlopen("https://www.cnblogs.com/mcq1999/p/python_Crawler_1.html")
#                 urllib.request.urlopen(url)
#                 request_status="OK"
#                 time.sleep(3)
#             except urllib.error.URLError as e:
#                 if hasattr(e, "code"):
#                     print(e.code)
#                 if hasattr(e, "reason"):
#                     print(e.reason)
#                 request_status="NO"
#         return request_status
#
#     def port_get(self,url):
#         if self.request_error(url)=="OK":
#             #发起get请求
#             print("发起get请求")
#
# PO=PortOperation()
# PO.port_get(url)


"""views"""

import json
import logging

import requests
import django_filters.rest_framework
from django.db import connection
from django.db.utils import DatabaseError
from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
from django.views.decorators.csrf import csrf_exempt
from rest_framework import renderers, status, viewsets
from rest_framework.decorators import api_view, permission_classes, action
from rest_framework.parsers import JSONParser
from rest_framework.permissions import AllowAny
from rest_framework.response import Response
from rest_framework.views import APIView

from .api import token_finisher
from VEDA import utils
from VEDA_OS01.enums import TranscriptionProviderErrorType
from VEDA_OS01.models import (URL, Course, Encode, TranscriptCredentials,
                              TranscriptProvider, Video)
from VEDA_OS01.serializers import (CourseSerializer, EncodeSerializer,
                                   URLSerializer, VideoSerializer)
from VEDA_OS01.transcripts import CIELO24_API_VERSION
from VEDA_OS01.utils import PlainTextParser
from control.http_ingest_celeryapp import ingest_video_and_upload_to_hotstore

LOGGER = logging.getLogger(__name__)

auth_dict = utils.get_config()
CIELO24_LOGIN_URL = utils.build_url(
    auth_dict['cielo24_api_base_url'],
    '/account/login'
)

class CourseViewSet(viewsets.ModelViewSet):

    queryset = Course.objects.all()
    serializer_class = CourseSerializer
    filter_backends = (django_filters.rest_framework.DjangoFilterBackend,)
    filter_fields = (
        'institution',
        'edx_classid',
        'proc_loc',
        'course_hold',
        'sg_projID'
    )

    @action(detail=True, renderer_classes=[renderers.StaticHTMLRenderer])
    def highlight(self, request, *args, **kwargs):
        course = self.get_object()
        return Response(course.highlighted)

    @csrf_exempt
    def perform_create(self, serializer):
        serializer.save()

class VideoViewSet(viewsets.ModelViewSet):

    queryset = Video.objects.all()
    serializer_class = VideoSerializer
    filter_backends = (django_filters.rest_framework.DjangoFilterBackend,)
    filter_fields = ('inst_class', 'edx_id')

    @action(detail=True, renderer_classes=[renderers.StaticHTMLRenderer])
    def highlight(self, request, *args, **kwargs):
        video = self.get_object()
        return Response(video.highlighted)

    @csrf_exempt
    def perform_create(self, serializer):
        serializer.save()

class EncodeViewSet(viewsets.ModelViewSet):

    queryset = Encode.objects.all()
    serializer_class = EncodeSerializer
    filter_backends = (django_filters.rest_framework.DjangoFilterBackend,)
    filter_fields = ('encode_filetype', 'encode_suffix', 'product_spec')

    @action(detail=True, renderer_classes=[renderers.StaticHTMLRenderer])
    def highlight(self, request, *args, **kwargs):
        encode = self.get_object()
        return Response(encode.highlighted)

    @csrf_exempt
    def perform_create(self, serializer):
        serializer.save()

class URLViewSet(viewsets.ModelViewSet):

    queryset = URL.objects.all()
    serializer_class = URLSerializer
    filter_backends = (django_filters.rest_framework.DjangoFilterBackend,)
    filter_fields = (
        'videoID__edx_id',
        'encode_profile__encode_suffix',
        'encode_profile__encode_filetype'
    )

    @action(detail=True, renderer_classes=[renderers.StaticHTMLRenderer])
    def highlight(self, request, *args, **kwargs):
        url = self.get_object()
        return Response(url.highlighted)

    @csrf_exempt
    def perform_create(self, serializer):
        serializer.save()

class TranscriptCredentialsView(APIView):
    """
    A Transcript credentials View, used by platform to create/update transcript credentials.
    """

    def get_cielo_token_response(self, username, api_secure_key):
        """
        Returns Cielo24 api token.

        Arguments:
            username(str): Cielo24 username
            api_securekey(str): Cielo24 api key

        Returns:
            Response : Http response object
        """
        return requests.get(CIELO24_LOGIN_URL, params={
            'v': CIELO24_API_VERSION,
            'username': username,
            'securekey': api_secure_key
        })

    def get_api_token(self, username, api_key):
        """
        Returns api token if valid credentials are provided.
        """
        response = self.get_cielo_token_response(username=username, api_secure_key=api_key)
        if not response.ok:
            api_token = None
            LOGGER.warning(
                '[Transcript Credentials] Unable to get api token --  response %s --  status %s.',
                response.text,
                response.status_code,
            )
        else:
            api_token = json.loads(response.content)['ApiToken']

        return api_token

    def validate_missing_attributes(self, provider, attributes, credentials):
        """
        Returns error message if provided attributes are not presents in credentials.
        """
        error_message = None
        missing = [attr for attr in attributes if attr not in credentials]
        if missing:
            error_message = u'{missing} must be specified for {provider}.'.format(
                provider=provider,
                missing=' and '.join(missing)
            )

        return TranscriptionProviderErrorType.MISSING_REQUIRED_ATTRIBUTES, error_message

    def validate_transcript_credentials(self, provider, **credentials):
        """
        Validates transcript credentials.

        Validations:
            Providers must be either 3PlayMedia or Cielo24.
            In case of:
                3PlayMedia - 'api_key' and 'api_secret_key' are required.
                Cielo24 - Valid 'api_key' and 'username' are required.
        """
        error_type, error_message, validated_credentials = None, '', {}
        if provider in [TranscriptProvider.CIELO24, TranscriptProvider.THREE_PLAY]:
            if provider == TranscriptProvider.CIELO24:
                must_have_props = ('org', 'api_key', 'username')
                error_type, error_message = self.validate_missing_attributes(provider, must_have_props, credentials)

                if not error_message:
                    # Get cielo api token and store it in api_key.
                    api_token = self.get_api_token(credentials['username'], credentials['api_key'])
                    if api_token:
                        validated_credentials.update({
                            'org': credentials['org'],
                            'api_key': api_token
                        })
                    else:
                        error_message = u'Invalid credentials supplied.'
                        error_type = TranscriptionProviderErrorType.INVALID_CREDENTIALS
            else:
                must_have_props = ('org', 'api_key', 'api_secret_key')
                error_type, error_message = self.validate_missing_attributes(provider, must_have_props, credentials)
                if not error_message:
                    validated_credentials.update({
                        'org': credentials['org'],
                        'api_key': credentials['api_key'],
                        'api_secret': credentials['api_secret_key']
                    })
        else:
            error_message = u'Invalid provider {provider}.'.format(provider=provider)
            error_type = TranscriptionProviderErrorType.INVALID_PROVIDER

        return error_type, error_message, validated_credentials

    def post(self, request):
        """
        Creates or updates the org-specific transcript credentials with the given information.

        Arguments:
            request: A WSGI request.

        **Example Request**

            POST /api/transcript_credentials {
                "provider": "3PlayMedia",
                "org": "test.x",
                "api_key": "test-api-key",
                "api_secret_key": "test-api-secret-key"
            }

        **POST Parameters**

            A POST request can include the following parameters.

            * provider: A string representation of provider.

            * org: A string representing the organizaton code.

            * api_key: A string representing the provider api key.

            * api_secret_key: (Required for 3Play only). A string representing the api secret key.

            * username: (Required for Cielo only). A string representing the cielo username.

            **Example POST Response**

            In case of success:
                Returns an empty response with 201 status code (HTTP 201 Created).

            In case of error:
                Return response with error message and 400 status code (HTTP 400 Bad Request).
                {
                    "error_type": INVALID_CREDENTIALS
                    "message": "Error message."
                }
        """
        # Validate credentials
        provider = request.data.pop('provider', None)
        error_type, error_message, validated_credentials = self.validate_transcript_credentials(
            provider=provider, **request.data
        )
        if error_message:
            return Response(
                status=status.HTTP_400_BAD_REQUEST,
                data=dict(error_type=error_type, message=error_message)
            )

        TranscriptCredentials.objects.update_or_create(
            org=validated_credentials.pop('org'), provider=provider, defaults=validated_credentials
        )

        return Response(status=status.HTTP_201_CREATED)

@permission_classes([AllowAny])
class IngestFromS3View(APIView):
    """
    Endpoint called by Amazon SNS/SQS to ingest video from the s3 bucket.
    """
    parser_classes = (JSONParser, PlainTextParser,)

    def _manage_aws_sns_subscription(self, request_body, subscription_type_url):
        """
        Manage HTTP endpoint subscription to SNS. There are two subscription_type_urls:
            1. subscribeURL
            2. unsubscribeURL
        Upon receiving a request to subscribe or unsunscribe an SNS to an HTTP endpoint,
        the endpoint must visit the URL provided by Amazon to confirm.
        """

        url = request_body.get(subscription_type_url)
        if not url:
            return 400, 'Subscribe/unsubscribe URL not in request body'

        requests.get(url)
        return 200, ''

    def _ingest_from_s3_bucket(self, request_body):
        """
        Handle ingest from s3 bucket.
        """
        status = 400
        reason = ''
        request_message = request_body.get('Message')
        try:
            message_json = json.loads(request_message)
            s3_object = message_json.get('Records')[0].get('s3')
            video_s3_key = s3_object.get('object').get('key')
        except TypeError:
            reason = 'Request message body does not contain expected output'
            LOGGER.error('[HTTP INGEST] {reason}'.format(reason=reason))
            return status, reason

        if not video_s3_key:
            reason = 'Video does not contain s3 key'
            LOGGER.error('[HTTP INGEST] {reason}'.format(reason=reason))
            return status, reason
        ingest_video_and_upload_to_hotstore.apply_async(args=[video_s3_key],
                                                        queue=auth_dict['celery_http_ingest_queue'])
        status = 200
        return status, reason

    @csrf_exempt
    def post(self, request):
        """
        Endpoint to handle requests from SNS.
        Three types of messages can be sent:
            1. A SubscriptionConfirmation - a subscription from SNS to this endpoint
            2. A UnsubscribeConfirmation - unsubscribing SNS from this endpoint
            3. A Notification - a request to ingest a video
        """
        amazon_message_type = request.META.get('HTTP_X_AMZ_SNS_MESSAGE_TYPE')

        if not amazon_message_type:
            return JsonResponse(
                {'Reason': 'Malformed header'},
                status=400
            )

        if not request.data:
            return JsonResponse(
                {'Reason': 'Empty request body'},
                status=400
            )

        json_data = json.loads(request.data)

        if amazon_message_type == 'SubscriptionConfirmation':
            status, reason = self._manage_aws_sns_subscription(json_data, 'SubscribeURL')
            if status == 200:
                LOGGER.info('[HTTP INGEST] SNS subscribed to HTTP endpoint')
        elif amazon_message_type == 'UnsubscribeConfirmation':
            status, reason = self._manage_aws_sns_subscription(json_data, 'UnsubscribeURL')
            if status == 200:
                LOGGER.info('[HTTP INGEST] SNS unsubscribed to HTTP endpoint')
        elif amazon_message_type == 'Notification':
            status, reason = self._ingest_from_s3_bucket(json_data)
            if status == 200:
                LOGGER.info('[HTTP INGEST] Video ingested through HTTP endpoint. Request body = {body}'.format(
                    body=request.data
                ))
            else:
                LOGGER.error('[HTTP INGEST] Video failed ingest through HTTP endpoint. Request body = {body}'.format(
                    body=request.data
                ))
        else:
            status = 400
            reason = 'Unsupported or invalid amazon message type'

        return JsonResponse(
            {'Reason': reason},
            status=status
        )

@csrf_exempt
def token_auth(request):
    """

    This is a hack to override the "Authorize" step in token generation

    """
    if request.method == 'POST':
        complete = token_finisher(request.POST['data'])
        return HttpResponse(complete)
    else:
        return HttpResponse(status=404)

def user_login(request):
    if request.user.is_authenticated:
        return HttpResponseRedirect(request.path)
    else:
        return HttpResponseRedirect('../admin')  # settings.LOGIN_REDIRECT_URL)

@api_view(['GET'])
@permission_classes([AllowAny])
def heartbeat(request):  # pylint: disable=unused-argument
    """
    View to check if database is reachable and ready to handle requests.
    """
    try:
        db_status()
    except DatabaseError:
        return JsonResponse(
            {'OK': False},
            status=status.HTTP_500_INTERNAL_SERVER_ERROR
        )

    return JsonResponse(
        {'OK': True},
        status=status.HTTP_200_OK
    )

def db_status():
    """
    Return database status.
    """
    with connection.cursor() as cursor:
        cursor.execute('SELECT 1')
        cursor.fetchone()


def extractRuinohonyakuWordpressCom(item):
	'''
	Parser for 'ruinohonyaku.wordpress.com'
	'''

	vol, chp, frag, postfix = extractVolChapterFragmentPostfix(item['title'])
	if not (chp or vol) or "preview" in item['title'].lower():
		return None

	tagmap = [
		('kondo wa zettai ni jama shimasen!',              'I Swear I Won\'t Bother You Again!',                      'translated'),
		('I won\'t be a bother for sure this time!',       'I Swear I Won\'t Bother You Again!',                      'translated'),
		('今度は絶対に邪魔しませんっ！',                   'I Swear I Won\'t Bother You Again!',                      'translated'),
		('Haikei Heika, Nidome no Ouhii wa Okotowari',     'Haikei Heika, Nidome no Ouhii wa Okotowari',              'translated'),
		('PRC',       'PRC',                      'translated'),
		('Loiterous', 'Loiterous',                'oel'),
	]

	for tagname, name, tl_type in tagmap:
		if tagname in item['tags']:
			return buildReleaseMessageWithType(item, name, vol, chp, frag=frag, postfix=postfix, tl_type=tl_type)

	return False


"""Tests for Domain Txn Handling Utils."""
import base58
import json

from copy import deepcopy

from unittest import TestCase

from ..domain_txn_handler import (
    _extract_attr_typed_value,
    parse_attr_txn,
    decode_state_value,
    extract_params_write_request,
    hash_of,
    make_state_path_for_attr,
    prepare_attr_for_state,
    prepare_nym_for_state,
    prepare_revoc_reg_entry_for_state,
    prepare_schema_for_state,
    prepare_get_claim_def_for_state,
    prepare_claim_def_for_state,
    prepare_revoc_def_for_state,
    prepare_get_revoc_reg_entry_for_state,
    prepare_revoc_reg_entry_accum_for_state,
)

CLAIM_DEF_TXN = {
    "result": {
        "txn": {
            "data": {
                "ver": 1,
                "signature_type": "CL",
                "ref": 10,
                "tag": "some_tag",
                "data": {"primary": "....", "revocation": "...."},
            },
            "metadata": {
                "reqId": 1514280215504647,
                "from": "L5AD5g65TDQr1PPHHRoiGf",
                "endorser": "D6HG5g65TDQr1PPHHRoiGf",
                "digest": "6cee82226c6e276c983f46d03e3b3d10436d90b67bf33dc67ce9901b44dbc97c",
                "payloadDigest": "21f0f5c158ed6ad49ff855baf09a2ef9b4ed1a8015ac24bccc2e0106cd905685",
            },
        },
        "txnMetadata": {
            "txnTime": 1513945121,
            "seqNo": 10,
            "txnId": "HHAD5g65TDQr1PPHHRoiGf2L5AD5g65TDQr1PPHHRoiGf1|Degree1|CL|key1",
        },
    }
}

REVOC_REG_ENTRY_TXN = {
    "result": {
        "txn": {
            "data": {
                "ver": 1,
                "revocRegDefId": "L5AD5g65TDQr1PPHHRoiGf:3:FC4aWomrA13YyvYC1Mxw7:3:CL:14:some_tag:CL_ACCUM:tag1",
                "revocDefType": "CL_ACCUM",
                "value": {
                    "accum": "accum_value",
                    "prevAccum": "prev_acuum_value",
                    "issued": [],
                    "revoked": [10, 36, 3478],
                },
            },
        },
        "txnMetadata": {
            "txnTime": 1513945121,
            "seqNo": 10,
            "txnId": "5:L5AD5g65TDQr1PPHHRoiGf:3:FC4aWomrA13YyvYC1Mxw7:3:CL:14:some_tag:CL_ACCUM:tag1",
        },
    }
}

class TestDomainTxnHandler(TestCase):
    """Domain Txn Handler Tests"""

    def test_extract_attr_typed_value(self):
        test_txn_data = {"test": {...}}
        with self.assertRaises(ValueError) as cm:
            _extract_attr_typed_value(test_txn_data)
            assert "ATTR should have one" in cm
        test_txn_data = {
            "raw": {...},
            "enc": {...},
            "hash": {...},
        }
        with self.assertRaises(ValueError) as cm:
            _extract_attr_typed_value(test_txn_data)
            assert "ATTR should have only one" in cm

    def test_parse_attr_txn(self):
        test_txn_data = {"raw": '{"name": "Alice"}'}
        assert parse_attr_txn(test_txn_data) == ("raw", "name", '{"name": "Alice"}')
        test_txn_data = {"enc": "test"}
        assert parse_attr_txn(test_txn_data) == ("enc", "test", "test")
        test_txn_data = {"hash": "test"}
        assert parse_attr_txn(test_txn_data) == ("hash", "test", None)

    def test_decode_state_value(self):
        test_value = "test_value"
        test_lsn = "100"
        test_lut = "test_lut"
        test_encoded = {"val": test_value, "lsn": test_lsn, "lut": test_lut}
        assert decode_state_value(json.dumps(test_encoded)) == (
            test_value,
            test_lsn,
            test_lut,
        )

    def test_hash_of(self):
        test = {"test": "test"}
        assert hash_of(test)
        test = "123"
        assert hash_of(test)
        test = b"234"
        assert hash_of(test)

    def test_make_state_path_for_attr(self):
        assert b"did1:1:attrName1" == make_state_path_for_attr(
            "did1", "attrName1", attr_is_hash=True
        )
        assert (
            b"did1:1:677a81e8649df8f1a1e8af7709a5ece1d965cb684b2c185272114c5cc3b7ec49"
            == make_state_path_for_attr("did1", "attrName1", attr_is_hash=False)
        )
        assert (
            b"did1:1:677a81e8649df8f1a1e8af7709a5ece1d965cb684b2c185272114c5cc3b7ec49"
            == make_state_path_for_attr("did1", "attrName1")
        )

    def test_prepare_attr_for_state(self):
        txn = {
            "result": {
                "txn": {
                    "data": {
                        "ver": 1,
                        "dest": "N22KY2Dyvmuu2PyyqSFKue",
                        "raw": '{"name":"Alice"}',
                    },
                },
                "txnMetadata": {
                    "txnTime": 1513945121,
                    "seqNo": 10,
                    "txnId": "N22KY2Dyvmuu2PyyqSFKue|02",
                },
            }
        }
        txn = txn.get("result")
        path, value_bytes = prepare_attr_for_state(txn)
        assert (
            path
            == b"N22KY2Dyvmuu2PyyqSFKue:1:82a3537ff0dbce7eec35d69edc3a189ee6f17d82f353a553f9aa96cb0be3ce89"
        )
        assert (
            value_bytes
            == b'{"lsn": 10, "lut": 1513945121, "val": "6d4a333838d0ef96756cccc680af2531075c512502fb68c5503c63d93de859b3"}'
        )
        path = prepare_attr_for_state(txn, path_only=True)
        assert (
            path
            == b"N22KY2Dyvmuu2PyyqSFKue:1:82a3537ff0dbce7eec35d69edc3a189ee6f17d82f353a553f9aa96cb0be3ce89"
        )

    def test_prepare_nym_for_state(self):
        txn = {
            "result": {
                "txn": {
                    "data": {
                        "dest": "N22KY2Dyvmuu2PyyqSFKue",
                    },
                },
            }
        }
        txn = txn.get("result")
        assert prepare_nym_for_state(txn)

    def test_prepare_schema_for_state(self):
        txn = {
            "result": {
                "txn": {
                    "type": "101",
                    "protocolVersion": 2,
                    "data": {
                        "ver": 1,
                        "data": {
                            "name": "Degree",
                            "version": "1.0",
                            "attr_names": [
                                "undergrad",
                                "last_name",
                                "first_name",
                                "birth_date",
                                "postgrad",
                                "expiry_date",
                            ],
                        },
                    },
                    "metadata": {
                        "reqId": 1514280215504647,
                        "from": "L5AD5g65TDQr1PPHHRoiGf",
                        "endorser": "D6HG5g65TDQr1PPHHRoiGf",
                        "digest": "6cee82226c6e276c983f46d03e3b3d10436d90b67bf33dc67ce9901b44dbc97c",
                        "payloadDigest": "21f0f5c158ed6ad49ff855baf09a2ef9b4ed1a8015ac24bccc2e0106cd905685",
                    },
                },
                "txnMetadata": {
                    "txnTime": 1513945121,
                    "seqNo": 10,
                    "txnId": "L5AD5g65TDQr1PPHHRoiGf1|Degree|1.0",
                },
            }
        }
        txn = txn.get("result")
        path, value_bytes = prepare_schema_for_state(txn)
        assert path == b"L5AD5g65TDQr1PPHHRoiGf:2:Degree:1.0"
        assert (
            prepare_schema_for_state(txn, path_only=True)
            == b"L5AD5g65TDQr1PPHHRoiGf:2:Degree:1.0"
        )

    def test_prepare_get_claim_def_for_state(self):
        txn = deepcopy(CLAIM_DEF_TXN)
        txn.get("result").get("txn").get("data").pop("ref")
        with self.assertRaises(ValueError) as cm:
            prepare_get_claim_def_for_state(txn)
            assert "ref field is absent, but it must contain schema seq no" in cm

    def test_prepare_claim_def_for_state(self):
        txn = deepcopy(CLAIM_DEF_TXN.get("result"))
        txn.get("txn").get("data").pop("ref")
        with self.assertRaises(ValueError) as cm:
            prepare_claim_def_for_state(txn)
            assert "ref field is absent, but it must contain schema seq no" in cm

        txn = deepcopy(CLAIM_DEF_TXN.get("result"))
        txn.get("txn").get("data").pop("data")
        with self.assertRaises(ValueError) as cm:
            prepare_claim_def_for_state(txn)
            assert "data field is absent, but it must contain components of keys" in cm

        txn = deepcopy(CLAIM_DEF_TXN.get("result"))
        path, value_bytes = prepare_claim_def_for_state(txn)
        assert path == b"L5AD5g65TDQr1PPHHRoiGf:3:CL:10:some_tag"
        assert (
            prepare_claim_def_for_state(txn, path_only=True)
            == b"L5AD5g65TDQr1PPHHRoiGf:3:CL:10:some_tag"
        )

    def test_prepare_revoc_def_for_state(self):
        txn = {
            "result": {
                "txn": {
                    "type": "113",
                    "protocolVersion": 2,
                    "data": {
                        "ver": 1,
                        "id": "L5AD5g65TDQr1PPHHRoiGf:3:FC4aWomrA13YyvYC1Mxw7:3:CL:14:some_tag:CL_ACCUM:tag1",
                        "credDefId": "FC4aWomrA13YyvYC1Mxw7:3:CL:14:some_tag",
                        "revocDefType": "CL_ACCUM",
                        "tag": "tag1",
                        "value": {
                            "maxCredNum": 1000000,
                            "tailsHash": "6619ad3cf7e02fc29931a5cdc7bb70ba4b9283bda3badae297",
                            "tailsLocation": "http://tails.location.com",
                            "issuanceType": "ISSUANCE_BY_DEFAULT",
                            "publicKeys": {},
                        },
                    },
                    "metadata": {
                        "reqId": 1514280215504647,
                        "from": "L5AD5g65TDQr1PPHHRoiGf",
                        "endorser": "D6HG5g65TDQr1PPHHRoiGf",
                        "digest": "6cee82226c6e276c983f46d03e3b3d10436d90b67bf33dc67ce9901b44dbc97c",
                        "payloadDigest": "21f0f5c158ed6ad49ff855baf09a2ef9b4ed1a8015ac24bccc2e0106cd905685",
                    },
                },
                "txnMetadata": {
                    "txnTime": 1513945121,
                    "seqNo": 10,
                    "txnId": "L5AD5g65TDQr1PPHHRoiGf:3:FC4aWomrA13YyvYC1Mxw7:3:CL:14:some_tag:CL_ACCUM:tag1",
                },
            },
        }
        txn = txn.get("result")
        path, value_bytes = prepare_revoc_def_for_state(txn)
        assert (
            path
            == b"L5AD5g65TDQr1PPHHRoiGf:4:FC4aWomrA13YyvYC1Mxw7:3:CL:14:some_tag:CL_ACCUM:tag1"
        )
        assert (
            prepare_revoc_def_for_state(txn, path_only=True)
            == b"L5AD5g65TDQr1PPHHRoiGf:4:FC4aWomrA13YyvYC1Mxw7:3:CL:14:some_tag:CL_ACCUM:tag1"
        )

    def test_prepare_get_revoc_reg_entry_for_state(self):
        assert prepare_get_revoc_reg_entry_for_state(REVOC_REG_ENTRY_TXN)
        assert prepare_get_revoc_reg_entry_for_state(REVOC_REG_ENTRY_TXN.get("result"))

    def test_prepare_revoc_reg_entry_for_state(self):
        path, value_bytes = prepare_revoc_reg_entry_for_state(
            REVOC_REG_ENTRY_TXN.get("result")
        )
        assert (
            path
            == b"5:L5AD5g65TDQr1PPHHRoiGf:3:FC4aWomrA13YyvYC1Mxw7:3:CL:14:some_tag:CL_ACCUM:tag1"
        )
        assert (
            prepare_revoc_reg_entry_for_state(
                REVOC_REG_ENTRY_TXN.get("result"), path_only=True
            )
            == b"5:L5AD5g65TDQr1PPHHRoiGf:3:FC4aWomrA13YyvYC1Mxw7:3:CL:14:some_tag:CL_ACCUM:tag1"
        )

    def test_prepare_revoc_reg_entry_accum_for_state(self):
        path, value_bytes = prepare_revoc_reg_entry_accum_for_state(REVOC_REG_ENTRY_TXN)
        assert (
            path
            == b"6:L5AD5g65TDQr1PPHHRoiGf:3:FC4aWomrA13YyvYC1Mxw7:3:CL:14:some_tag:CL_ACCUM:tag1"
        )
        path, value_bytes = prepare_revoc_reg_entry_accum_for_state(
            REVOC_REG_ENTRY_TXN.get("result")
        )
        assert (
            path
            == b"6:L5AD5g65TDQr1PPHHRoiGf:3:FC4aWomrA13YyvYC1Mxw7:3:CL:14:some_tag:CL_ACCUM:tag1"
        )

    def test_extract_params_write_request(self):
        write_request = {
            "result": {
                "txn": {
                    "data": {
                        "data": {
                            "attr_names": ["axuall_proof_id"],
                            "name": "manual_issuance",
                            "version": "4.1.0",
                        }
                    },
                    "metadata": {
                        "digest": "f440da62ab1c38601e41b5e148370b64af3183a2428f327cfb8ac83ca4cbc698",
                        "from": "F72i3Y3Q4i466efjYJYCHM",
                        "payloadDigest": "0ecfd475ed34635b6623bcbcda422ff90dee53f66de20a7441bca04ba0632670",
                        "reqId": 1634854863739554300,
                        "taaAcceptance": {
                            "mechanism": "for_session",
                            "taaDigest": "8cee5d7a573e4893b08ff53a0761a22a1607df3b3fcd7e75b98696c92879641f",
                            "time": 1600300800,
                        },
                    },
                    "protocolVersion": 2,
                    "type": "101",
                },
                "txnMetadata": {
                    "seqNo": 75335,
                    "txnId": "F72i3Y3Q4i466efjYJYCHM:2:manual_issuance:4.1.0",
                    "txnTime": 1634854863,
                },
                "ver": "1",
                "auditPath": [
                    "DhzuS5ZkyxUrQbRzbCfq3NDcTnTzFcJHYJYyTAqgMymT",
                    "AtYz5H13m1ZyFiGVVLwRL7au8muhCMwikeQrMwJBJ5d9",
                    "3GpqpZw4jymXDFm8Mf5gSMHSJ8r1wXutiZ1x26kxwWBE",
                    "EQyUpv9z64prPmDqkb8n2SzBtor3tGaNw4qZP2psG2wD",
                    "9fbpJjWbDuNbCrC7GQxjVxDGJ48k7rdBDf5ATu7PXVx3",
                    "CRbATGbi9DbN1pphXMcREVZTUUqsQbD9mN5ikKm1FkSc",
                    "E19R3Cty6iLtWBoxSqHLrxTSJSKpBtj3wJEdD98pS9g8",
                ],
                "ledgerSize": 75335,
                "reqSignature": {
                    "type": "ED25519",
                    "values": [
                        {
                            "from": "F72i3Y3Q4i466efjYJYCHM",
                            "value": "29sKMwQjm6r2BjpbJdFi1TGvowaxbi9nnAPP9LwPHhuBzwtfWwqJFg4Ur2xAWBLYfPLnMNNSgwwNesRSb1C1L72d",
                        }
                    ],
                },
                "rootHash": "4aM6yCpamk82Uqb414mNRpEdYkwdSMhi3HkXgN7YvRaX",
            }
        }
        txn = deepcopy(write_request)
        (
            tree_size,
            leaf_index,
            decoded_audit_path,
            expected_root_hash,
        ) = extract_params_write_request(txn)

        given_audit_path = [
            "DhzuS5ZkyxUrQbRzbCfq3NDcTnTzFcJHYJYyTAqgMymT",
            "AtYz5H13m1ZyFiGVVLwRL7au8muhCMwikeQrMwJBJ5d9",
            "3GpqpZw4jymXDFm8Mf5gSMHSJ8r1wXutiZ1x26kxwWBE",
            "EQyUpv9z64prPmDqkb8n2SzBtor3tGaNw4qZP2psG2wD",
            "9fbpJjWbDuNbCrC7GQxjVxDGJ48k7rdBDf5ATu7PXVx3",
            "CRbATGbi9DbN1pphXMcREVZTUUqsQbD9mN5ikKm1FkSc",
            "E19R3Cty6iLtWBoxSqHLrxTSJSKpBtj3wJEdD98pS9g8",
        ]
        expected_audit_path = [
            base58.b58decode(hash_str.encode("utf-8")) for hash_str in given_audit_path
        ]
        expected_hash = base58.b58decode(
            "4aM6yCpamk82Uqb414mNRpEdYkwdSMhi3HkXgN7YvRaX".encode("utf-8")
        )
        assert tree_size == 75335
        assert leaf_index == 75334
        assert expected_root_hash == expected_hash
        assert decoded_audit_path == expected_audit_path

        txn = deepcopy(write_request)
        txn = txn.get("result")
        (
            tree_size,
            leaf_index,
            decoded_audit_path,
            expected_root_hash,
        ) = extract_params_write_request(txn)

        assert tree_size == 75335
        assert leaf_index == 75334
        assert expected_root_hash == expected_hash
        assert decoded_audit_path == expected_audit_path

        txn = deepcopy(write_request)
        txn["result"]["txnMetadata"]["seqNo"] = 1000
        with self.assertRaises(Exception) as cm:
            extract_params_write_request(txn)
            assert "auditPath length does not match with given seqNo" in cm


# -*- coding: utf-8 -*-
import pandas as pd
import neurokit2 as nk
import json
import urllib.request
import datetime

def github_stars(repo="user/repo", access_token="access_token"):
    """Access the stars history from GitHub

    You need your access token that you can get by following these instructions:
    https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line

    Examples
    ---------
    >>> import popularipy
    >>> data = popularipy.github_stars("neuropsychology/neurokit", "myaccesstoken")
    >>> data.plot(x="Date")
    """
    page_number = 0
    stars_remaining = True
    stars_times = []
    stars_users = []
    while stars_remaining:
        query_url = "https://api.github.com/repos/%s/stargazers?page=%s&access_token=%s" % (repo, page_number, access_token)

        req = urllib.request.Request(query_url)
        req.add_header('Accept', 'application/vnd.github.v3.star+json')
        try:
            response = urllib.request.urlopen(req)
        except:
            pass
        data = json.loads(response.read())

        for user in data:
            username = user['user']['login']

            star_time = datetime.datetime.strptime(user['starred_at'],'%Y-%m-%dT%H:%M:%SZ')
            star_time = star_time + datetime.timedelta(hours=-5) # EST
            star_time = star_time.strftime('%Y-%m-%d %H:%M:%S')
            stars_times.append(star_time)

            stars_users.append(username)

        if len(data) < 25:
            stars_remaining = False

        page_number += 1

    data = pd.DataFrame({"Date": stars_times,
                         "User": stars_users})
    data = data.drop_duplicates(subset ="User")
    data["Stars"] = 1
    data["Stars"] = data["Stars"].cumsum()
    data = data.append({'Date' : datetime.datetime.today().strftime('%Y-%m-%d %H:%M:%S') ,
                        'User' : "MrBrownstone",
                        "Stars": data["Stars"].values[-1]} , ignore_index=True)
    data = data.drop_duplicates(subset ="Date")
    data.index = pd.DatetimeIndex(data["Date"])

    data = data.resample('D').fillna("backfill")
    data = data.drop(["Date", "User"], axis=1)
    data = data.reset_index()
    data["Date"] = pd.to_datetime(data["Date"]).dt.strftime('%d %b %Y')

    return data


import tk_game
import tk_admin
from queue import Queue

class HEXAGON:
    def __init__(self, idx):
        self.idx = idx
        self.state = "white"
        self.neighbors =[]

        self.left_wall = False
        self.right_wall = False
        self.bottom_wall = False
    def set_state(self, state):
        self.state = state

class QUESTION:
    def __init__(self, question, answer):
        self.question = question
        self.answer = answer
        self.abbreviation = [ word.upper()[0] for word in answer.split() ]

class GAME:
    def __init__(self, player1, player2):
        self.player1 = player1
        self.player2 = player2
        self.rows = 7
        self.prepare_hexagons( self.rows )
        self.finished = False
        self.questions = [ QUESTION("Legenda Holešovic?","Jirka Kára") ]
        self.question_outcome = None
        self.hexagon_idx_opened = -1

    def prepare_hexagons(self, rows):
        idx = 0
        self.hexagons = []
        for row in range(rows+1):
            for col in range(row):
                h = HEXAGON(idx)
                if col == 0:
                    h.left_wall = True
                else:
                    h.neighbors.append(idx-1)
                    self.hexagons[idx-1].neighbors.append(idx)
                if col == row-1:
                    h.right_wall = True
                if row == rows:
                    h.bottom_wall = True
                # vertical neighbors:
                if row != 0:
                    if col != row-1: #not right one
                        h.neighbors.append(idx-row+1)
                        self.hexagons[idx-row+1].neighbors.append(idx)
                    if col != 0: #not left one
                        h.neighbors.append(idx-row)
                        self.hexagons[idx-row].neighbors.append(idx)
                self.hexagons.append(h)
                idx += 1

        print("hexagons prepared like this:")
        for hexagon in self.hexagons:
            print("hexagon idx ", hexagon.idx)
            print(hexagon.neighbors)

    def set_windows(self, admin_window, game_window):
        self.admin_window = admin_window
        self.game_window = game_window

    def set_players(self, player1, player2):
        self.player1 = player1
        self.player2 = player2

        self.game_window.redraw()
        self.admin_window.clear()

    def exit(self):
        self.admin_window.exit()
        self.game_window.exit()
        exit(0)

    def hexagon_clicked(self, idx):
        print("clicked hexagon: ", idx)
        if self.hexagons[idx].state == "white" or self.hexagons[idx].state == "black":
            self.ask_question(idx, self.questions[0])

    def set_question_outcome(self, outcome):
        self.question_outcome = outcome
        self.hexagons[self.hexagon_idx_opened].set_state( self.question_outcome )
        self.game_window.redraw()
        self.admin_window.clear()
        self.check_finnish()

    def ask_question(self, idx, question):
        print("asking question")
        self.question_outcome = None
        self.hexagon_idx_opened = idx
        self.admin_window.show_question(question.question, question.answer)
        self.game_window.show_question(question.abbreviation)

    def start_timer(self):
        self.game_window.start_timer()

    def bfs(self, player, bfs_list):
        print("dostavam bfs s ", bfs_list)
        Q = Queue()

        while True:
            if Q.empty():
                print("restartuju bfs s ", bfs_list)
                node = self.hexagons[ bfs_list[0] ]
                print("new node = ", node.idx)
                bfs_list.remove(node.idx)
                state = [False,False,False]
                closed_list = [node.idx]
            else:
                node = Q.get()
                print("opening node = ", node.idx)
                if node.idx in closed_list:
                    continue
                bfs_list.remove(node.idx)
                closed_list.append(node.idx)

            if node.left_wall:
                state[0] = True
            if node.right_wall:
                state[1] = True
            if node.bottom_wall:
                state[2] = True

            for neighbor_idx in node.neighbors:
                new_node = self.hexagons[neighbor_idx]
                if new_node.state == player:
                    Q.put(new_node)

            if sum(state) == 3:
                print("vracim true")
                return True

            if len(bfs_list) == 0:
                print("vracim false")
                return False

    def check_finnish(self):
        blue_indexes = [ h.idx for h in self.hexagons if h.state == "blue" ]
        orange_indexes = [ h.idx for h in self.hexagons if h.state == "orange" ]

        if len( blue_indexes ) >= self.rows:
            blue_status = self.bfs( "blue", blue_indexes )
            if blue_status:
                self.blue_wins()

        if len( orange_indexes ) >= self.rows:
            orange_status = self.bfs( "orange", orange_indexes )
            if orange_status:
                self.orange_wins()

    def blue_wins(self):
        self.game_window.blue_wins()

    def orange_wins(self):
        self.game_window.orange_wins()

game = GAME("Hráč 1", "Hráč 2")
game_window = tk_game.GAME_WINDOW(game)

admin_window = tk_admin.ADMIN_WINDOW(game)
admin_window.get_players()

game.set_windows(admin_window, game_window)

admin_window.show()
print("jsem tady")


#!/usr/bin/env python3

import time
import sys
import cv2
from color import name_to_bgr, detect_bgr, rect_average
import json
import threading
import color

REGION_SIZE = 32
REGION_PAD = 192
PREVIEW_SIZE = 64
PREVIEW_PAD = 1
PREVIEW_CUBE_SIZE = (PREVIEW_SIZE+PREVIEW_PAD)*3
PREVIEW_X = 1020
PREVIEW_Y = 220
PREVIEW_SIDE_OFFSETS = [
    [PREVIEW_X+PREVIEW_CUBE_SIZE+20, PREVIEW_Y-PREVIEW_CUBE_SIZE-5],
    [PREVIEW_X, PREVIEW_Y],
    [PREVIEW_X+PREVIEW_CUBE_SIZE+5, PREVIEW_Y],
    [PREVIEW_X+PREVIEW_CUBE_SIZE*2+10, PREVIEW_Y],
    [PREVIEW_X+PREVIEW_CUBE_SIZE*3+15, PREVIEW_Y],
    [PREVIEW_X+PREVIEW_CUBE_SIZE+20, PREVIEW_Y+PREVIEW_CUBE_SIZE+5]
]

class Webcam:
    def __init__(self):
        self.update_state()
        print(PREVIEW_SIDE_OFFSETS)

    def draw_regions(self, frame):
        cv2.rectangle(frame, (960,0), (1920, 1080), (0, 0, 0), -1)
        for index in range(9):
            x, y = self.regions[index]
            rect  = frame[y:y+REGION_SIZE, x:x+REGION_SIZE]
            cv2.rectangle(frame, (x,y), (x+REGION_SIZE, y+REGION_SIZE), (255, 255, 255), 2)

            color = detect_bgr(rect_average(rect))[0]

    def draw_state(self, frame):
        for side in range(6):
            offsetx, offsety = PREVIEW_SIDE_OFFSETS[side]
            for y in range(3):
                for x in range(3):
                    color = self.state[side*9 + y*3 + x]
                    cv2.rectangle(frame,
                        (offsetx+x*(PREVIEW_SIZE+PREVIEW_PAD), offsety+y*(PREVIEW_SIZE+PREVIEW_PAD)),
                        (offsetx+x*(PREVIEW_SIZE+PREVIEW_PAD)+PREVIEW_SIZE, offsety+y*(PREVIEW_SIZE+PREVIEW_PAD)+PREVIEW_SIZE),
                        name_to_bgr(color), -1)

    def update_window(self, frame):
        self.draw_regions(frame)
        self.draw_state(frame)
        height, width, layers =  frame.shape
        resize = cv2.resize(frame, (width//2, height//2))
        cv2.imshow("win1", resize)

    def update_state(self, state = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"):
        self.state = state

    def video_loop(self):
        self.cam = cv2.VideoCapture(0)
        cv2.namedWindow("win1");
        cv2.moveWindow("win1", 20, 20);
        while self.running:
            ret, frame = self.cam.read()
            key = cv2.waitKey(10) & 0xff
            if ret:
                self.current_frame = frame.copy()
                self.update_window(frame)

        self.cam.release()
        cv2.destroyAllWindows()

    def start_video(self):
        self.running = True
        self.regions = []
        for y in range(3):
            for x in range(3):
                self.regions.append([220+x*(REGION_SIZE+REGION_PAD), 220+y*(REGION_SIZE+REGION_PAD)])

        self.video_loop()

    def stop_video(self):
        self.running = False

    def scan(self):
        state = []
        for index, (x,y) in enumerate(self.regions):
            rect  = self.current_frame[y:y+REGION_SIZE, x:x+REGION_SIZE]
            state.append(color.rect_average(rect))
        return state

        return None


import re
names = []
spend_money = 0
pattern = r'>>(?P<item>\w+)<<(?P<price>\d+[\.]?\d+)!(?P<quantity>\d+)'
string = input()
while string != 'Purchase':
    matches = re.finditer(pattern, string)
    for match in matches:
        names.append(match.group('item'))
        spend_money += float(match.group('price')) * int(match.group('quantity'))
    string = input()

print("Bought furniture:")
if names:
    print('\n'.join(names))
print(f'Total money spend: {spend_money:.2f}')

09. Regular Expressions/Exercise/05. Furniture.py


from flask import Flask, request, render_template
import poker as p
# jsonify 回傳json

app = Flask(__name__)

@app.route('/poker', methods=['GET', 'POST'])
def poker():
    request_method = request.method
    if request.method == 'GET':
        return render_template('pokercode.html')
    elif request.method == 'POST':
        players = request.form.get('players')
        cards = p.poker(int(players))
        return render_template('pokercode.html',
                               request_method=request_method,
                               players=players,
                               cards=cards)
        # 印 flask 一定要回傳string, 將 json 轉換為 string

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=5000)


import matplotlib.pyplot as plt
import os
import matplotlib.image as mpimg
import numpy as np
import cv2 as cv
import time

os.chdir("C:/Users/fseignat/Desktop/stega Seignat/code_images")

from quantiles import quantiles_theoriques,quantiles_TCL
from lecture_video import tableau,cv_plt,distance,recup_Cover,modif_video, supp_val_extrem
from test_wave import decomposition_image, concatene
from estimation_parametres import sigma_chap, beta_chap,g_prime,val_dep_beta
from scipy.stats import gennorm
from etude_coefs import recup_image

start_time = time.time()

##Recuperation des fichiers

#KB = np.load('pixel_video_KB.npy')
#TR = np.load('pixel_video_TR.npy')
#TR2 = np.load('pixel_video_TR(2).npy')
#SC=np.load("Scarface.npy")
#AZ=np.load("Azur et Asmar.npy")
BA_TR=np.load("BA_TR.npy")

#Vidéo non modifiée
file=BA_TR
File=[file]

Nom_File=["KB","TR","TR2","SC","AZ"]
#File=[KB,TR,TR2,SC,AZ]

nb_echelle=3

# #NB=len(file)

##♥ Analyse d'une vidéo entière

# Count_Ste=[]

# f=0
# for file in File:
#     c=0
#     Rapp_sigma=[]
#     Rapp_beta=[]
#     NB=len(file)
#     for ind in range(1,NB-1):
#         Signe="Cover"
#         for e in range(1):

#             img=file[ind]

#             ind_Cover=recup_Cover(file,ind)
#             img_Cover=file[ind_Cover]

#             WR=concatene(img,nb_echelle,e+1)
#             WR_C=concatene(img_Cover,nb_echelle,e+1)

#             WR=supp_val_extrem(WR,0.002)
#             WR_C=supp_val_extrem(WR_C,0.002)

#             #Image à tester
#             val_dep=val_dep_beta(WR)
#             beta_estim=beta_chap(WR,val_dep,10**(-4))
#             sigma_estim=sigma_chap(WR,beta_estim)

#             #Image "Cover"
#             val_dep_C=val_dep_beta(WR_C)
#             beta_estim_C=beta_chap(WR_C,val_dep_C,10**(-4))
#             sigma_estim_C=sigma_chap(WR_C,beta_estim_C)

#             if beta_estim!="Pas de solution":
#                     num=np.sum(np.abs(WR)**beta_estim)
#                     den=np.sum(np.abs(WR_C)**beta_estim)
#                     rapport=num/den

#                     #Quantiles théoriques
#                     Q_th=quantiles_theoriques(np.size(WR_C),beta_estim,0.05)

#                     #Quantiles TCL
#                     #Q_TCL=quantiles_TCL(np.size(WR_Cov),beta_estim,0.05)

#                     if rapport>Q_th:
#                         Signe="Stego"

#             #print("ind = ", ind,"beta_Cov = ",beta_estim_C,"beta_test =",beta_estim)
#             #print("\n")
#             if Signe=="Stego":
#                     c+=1
#         if beta_estim!='Pas de solution' and beta_estim_C !='Pas de solution':
#             Rapp_sigma.append(sigma_estim/sigma_estim_C)
#             Rapp_beta.append(beta_estim/beta_estim_C)
#         print("F= ",f,"ind =",ind,Signe)
#         print("\n")

#     print("\n")
#     Count_Ste.append(c)

#     B=[i for i in range(len(Rapp_beta))]

#     plt.figure()
#     plt.plot(B,Rapp_beta)
#     plt.title(Nom_File[f]+"_Rapport des beta"+"_2")      #entre l'image test et l'image désignée Cover (par la norme L²)

#     plt.figure()
#     plt.plot(B,Rapp_sigma)
#     plt.title(Nom_File[f]+"_Rapport des sigma"+"_2")
#     f+=1

# print("--- %s seconds ---" % (time.time() - start_time))

## Analyse d'une trame (pour comprendre le comportement d'une image spécifique)

img=file[187]              #154

ind_Cover=recup_Cover(file,187)
img_Cover=file[ind_Cover]

#img=recup_image(7841,"C:/Users/fseignat/Desktop/stega Seignat/code_images/ALASKA/Cover")
WR=concatene(img,2,2)
WR_C=concatene(img_Cover,2,2)

#Seuillage
WR=supp_val_extrem(WR,0.002)
WR_C=supp_val_extrem(WR_C,0.002)

#WR=decomposition_image(img,3,2,1)
val_dep=val_dep_beta(WR)
beta_estim=beta_chap(WR,val_dep,10**(-4))
sigma_estim=sigma_chap(WR,beta_estim)
print("beta = ",beta_estim,"sigma =",sigma_estim,"val =",val_dep)

val_dep_C=val_dep_beta(WR_C)
beta_estim_C=beta_chap(WR_C,val_dep_C,10**(-4))
sigma_estim_C=sigma_chap(WR_C,beta_estim_C)
print("beta = ",beta_estim_C,"sigma =",sigma_estim_C,"val =",val_dep_C)

plt.figure()
n, b, patches=plt.hist(np.reshape(WR,np.size(WR)), bins='sqrt',color='blue',density=True, alpha=0.3, rwidth=0.85)
x = np.linspace(np.min(WR),np.max(WR),150)
F=gennorm.pdf(x,beta_estim,scale=sigma_estim)
plt.plot(x,F,'r-', lw=1, alpha=0.6, label='gennorm pdf')
plt.ylim([0,np.max(F)])
plt.xlim([0,n.max()])

plt.figure()
plt.hist(np.reshape(WR_C,np.size(WR_C)), bins='sqrt',color='blue',density=True, alpha=0.3, rwidth=0.85)
x_C = np.linspace(np.min(WR_C),np.max(WR_C),150)
F_C=gennorm.pdf(x_C,beta_estim_C,scale=sigma_estim_C)
plt.plot(x_C,F_C,'r-', lw=1, alpha=0.6, label='gennorm pdf')
plt.ylim([0,np.max(F_C)])

# ##Dessins animés

# # AZ=np.load("Azur et Asmar.npy")

# # for i in range(10):
# #     img=AZ[i]
# #     for e in range(nb_echelle):
# #         WR=concatene(img,nb_echelle,e+1)
# #         val_dep=val_dep_beta(WR)
# #         beta_estim=beta_chap(WR,val_dep,10**(-4))
# #         sigma_estim=sigma_chap(WR,beta_estim)
# #         print("beta =", beta_estim, "sigma =",sigma_estim)
# #         plt.figure()
# #         plt.hist(np.reshape(WR,WR.shape[0]*WR.shape[1]), bins='auto',color='blue',density=True, alpha=0.5, rwidth=0.85)
# #         x = np.linspace(gennorm.ppf(0.001,beta_estim,scale=sigma_estim),gennorm.ppf(0.999, beta_estim,scale=sigma_estim), 100)
# #         F=gennorm.pdf(x,beta_estim,scale=sigma_estim)
# #         plt.plot(x, F,'r-', lw=1, alpha=0.5,color='red', label='gennorm pdf')
# #         plt.ylim([0,np.max(F)])


import tkinter as tk
import tkinter.font as font
import random

window = tk.Tk()
window.title("Knight-Knaves-Horses")
window.geometry("900x900")
items = {0: "Knights", 1: "Knaves", 2: "Horses"}
myfont = font.Font(family="Arial", size=20)

def change_text():

    myitems = ["Knights", "Knaves", "Horses"]
    newtext = random.choice(myitems)
    btn["text"] = newtext

btn = tk.Button(
    master=window,
    text=items[0],
    command=change_text,
    highlightbackground="#3E4149",
    # fg="#5799ef",
    fg="magenta",
    bg="gray",
    width="40",
    height="5",
    font=myfont,
    activebackground="#686868",
)

btn.place(x=150, y=50)

window.mainloop()


from elasticsearch import Elasticsearch
import json

class Indexer(object):
    def __init__(self, index_name, doc_type):

        # Load configuration from file
        with open("../config.json", "r") as fig:
            config = json.load(fig)

        host = config['host']
        port = int(config['port'])

        # create an Elasticsearch index
        es = Elasticsearch([{'host': host, 'port': port}])

        self.index_name = index_name
        self.es = es
        self.doc_type = doc_type

    def index(self, doc_id=None, doc_body=None):
        self.es.index(
            index=self.index_name,
            doc_type=self.doc_type,
            id=doc_id,
            body=doc_body)


'''
 Exercício Python #085 - Listas com pares e ímpares:
 Crie um programa onde o usuário possa digitar sete valores numéricos
 e cadastre-os em uma lista única que mantenha separados os valores
 pares e ímpares. No final, mostre os valores pares e ímpares em ordem crescente.
'''
print("== Exercício Python #085 - Listas com pares e ímpares ==")
lista_geral = [[],[]]
for x in range(1,8):
    valor = int(input(f'{x}º valor: '))
    if valor % 2 == 0:
        lista_geral[1].append(valor)
    else:
        lista_geral[0].append(valor)
print('-'*56)
lista_geral[0].sort()
lista_geral[1].sort()
print(f"Números pares: {lista_geral[1]}")
print(f"Números ímpares: {lista_geral[0]}")

CURSO DE PYTHON 3/Mundo 3 - Estruturas Compostas/2 - Listas em Python/Parte 02/Exerc085 - Listas com pares e ímpares5.py