import torch,time
import torch.nn as nn
import torch.nn.functional as F
import torch
from .afnonet import BaseModel
# $$
# \begin{align}
# \partial_t V_s &= F -  V_s \cdot \nabla_s   V_s  - \omega\partial_p V_s - \nabla_s\phi\\
# \partial_t T &= Q/C_v + \frac{RT}{C_pp}\omega  - V_s  \cdot \nabla _s   T -\omega \partial_pT\\
# \partial_t \phi&= wg  - V_s \cdot\nabla_s \phi-\omega\partial_p\phi\\
# 0&=\nabla_s\cdot V+ \frac{\partial \omega}{\partial p}
# \end{align}
# $$

class First_Derivative_Layer(torch.nn.Module):
    '''
    use high dimenstion conv is faster
        B=2
        P=4
        a=torch.randn(B,P,3,32,64).cuda()
        layer=  First_Derivative_Layer(dim=3).cuda()
        runtime_weight=layer.runtime_weight

        x = torch.conv3d(a.flatten(0,1).unsqueeze(1),runtime_weight).reshape(*a.shape[:-1],-1)
        --> 34.8 µs ± 130 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)

        x2 = torch.conv1d(a.flatten(0,-2).unsqueeze(1),runtime_weight[0,0]).reshape(*a.shape[:-1],-1)
        --> 43.1 µs ± 1.63 µs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
    '''
    def __init__(self,position=-1,dim=2,mode='five-point-stencil',pad_mode='circular',intervel=torch.Tensor([1])):
        super().__init__()
        self.postion=position
        self.dim    =dim
        self.mode   =mode
        self.pad_mode=pad_mode
        self.intervel= intervel
        self.conv_engine = [torch.conv1d, torch.conv2d, torch.conv3d][self.dim-1]
        if self.mode=='five-point-stencil':
             self.weight = torch.nn.Parameter(torch.Tensor([1/12,-8/12,0,8/12,-1/12]),requires_grad=False)
             self.pad_num = 2
        elif self.mode=='three-point-stencil':
             self.weight = torch.nn.Parameter(torch.Tensor([-1/2,0,1/2]),requires_grad=False)
             self.pad_num = 1
        elif isinstance(self.mode, int):
             self.weight = torch.nn.Parameter(torch.randn(self.mode)*0.01)
             self.pad_num     = self.mode
        else:
            raise NotImplementedError(f"the self.mode must be five-point-stencil or three-point-stencil or a int number to activate trainable derivate")
        padtuple = [0]*self.dim*2
        padtuple[(-self.postion-1)*2]   = self.pad_num
        padtuple[(-self.postion-1)*2+1] = self.pad_num
        self.padtuple = tuple(padtuple)

    @property
    def runtime_weight(self):
        if isinstance(self.mode, int):
            weight = torch.cat([-torch.flip(self.weight,(0,)),F.pad(self.weight,(1,0))])
        else:
            weight = self.weight
        return weight[(None,)*(self.dim + 1)].transpose(self.postion,-1)
    def forward(self, x):
        expandQ=False
        if len(x.shape) == self.dim + 1 and x.shape[1]!=1:
            x = x.unsqueeze(1)
            expandQ = True
        assert len(x.shape) == self.dim + 2
        # if only x dim, then the input should be (Batch, 1 , x)
        # if (x,y) pannel, then the input should be (Batch, 1 , x , y)
        # if (x,y,z) pannel, the the input should be (Batch, 1 , x , y , z)
        x = self.conv_engine(F.pad(x, self.padtuple, mode=self.pad_mode),self.runtime_weight)
        x = x/self.intervel.to(x.device)

        return x.squeeze(1) if expandQ else x

class Second_Derivative_Layer(torch.nn.Module):
    def __init__(self,position=(-2,-1),mode='nine-point-stencil',pad_mode='circular',intervel=torch.Tensor([1])):
        super().__init__()
        self.postion=position
        self.mode   =mode
        self.pad_mode=pad_mode
        self.intervel= intervel
        if self.mode=='nine-point-stencil':
            self.weight = torch.nn.Parameter(torch.Tensor([[1/3, 1/3,1/3],
                                                           [1/3,-8/3,1/3],
                                                           [1/3, 1/3,1/3]]),requires_grad=False)
            self.pad_num = 1
        else:
            raise NotImplementedError(f"the self.mode must be nine-point-stencil")

    @property
    def runtime_weight(self):
        return self.weight[None,None] #(1,1,3,3)
    def forward(self, x):
        x = x.transpose(-2,self.postion[0])
        x = x.transpose(-1,self.postion[1])

        oshape = x.shape
        x = x.flatten(0,-3).unsqueeze(1)
        x = F.pad(x, (self.pad_num,self.pad_num,self.pad_num,self.pad_num), mode=self.pad_mode)
        x = torch.conv2d(x,self.runtime_weight)
        x = x/self.intervel.to(x.device)
        x = x.reshape(*oshape)
        x = x.transpose(-1,self.postion[1])
        x = x.transpose(-2,self.postion[0])
        return x

class OnlineNormModel(BaseModel):
    def __init__(self, args, backbone):
        super().__init__()
        self.backbone =  backbone
    def forward(self, Field):
        mean = Field.mean(dim=(0,2,3),keepdim=True)
        std  = Field.std(dim=(0,2,3),keepdim=True)
        return self.backbone((Field-mean)/std)*std+mean

class DeltaModel(BaseModel):
    def __init__(self, args, backbone):
        super().__init__()
        self.backbone =  backbone
    def forward(self, Field):
        Delta = self.backbone(Field)
        return Delta + Field

class EulerEquationModel(BaseModel):
    def __init__(self, args, backbone):
        super().__init__()
        self.Dx= First_Derivative_Layer(position=-1, dim=3)
        self.Dy= First_Derivative_Layer(position=-2, dim=3)
        self.Dz= First_Derivative_Layer(position=-3, dim=3, mode='three-point-stencil')
        self.thermal_factor = nn.Parameter(torch.randn(1))
        self.p_list         = nn.Parameter(torch.Tensor([10,8.5,5]).reshape(1,3,1,1),requires_grad=False)
        self.backbone =  backbone
        self.monitor = True

    def forward(self, Field):
        #Delta_u = Fx - u*u_dx - v*u_dy - o*u_dz - p_dx
        #Delta_v = Fy - u*v_dx - v*v_dy - o*v_dz - p_dy
        #Delta_T =  Q - u*T_dx - v*T_dy - o*T_dz + A*T/p*o
        #Delta_p =  W - u*P_dx - v*P_dy - o*P_dz
        #      0 = u_dx + v_dy + o_dz
        # input -> Field  = [u ,v, T, p] --> (Batch, 4, z, y ,x)
        # need generate unknown data [Fx, Fy , Q, W, o]
        b, si_z, i_y, i_x = Field.shape
        s=4
        i_z= si_z//4
        MachineLearningPart = self.backbone(Field).reshape(b, s+1, i_z, i_y, i_x) #(Batch, 5, z, y ,x)
        ExternalForce = MachineLearningPart[:,:4] #(Batch, 4, z, y ,x)
        o     = MachineLearningPart[:,4:5] #(Batch, 1, z, y ,x)
        Field = Field.reshape(b, s, i_z, i_y,  i_x) #(Batch, 5, z, y ,x)
        u = Field[:,0:1]
        v = Field[:,1:2]
        T = Field[:,2:3]
        p = Field[:,3:4]
        Field_dx = self.Dx(Field.flatten(0,1)).reshape(Field.shape)#(Batch, 4, z, y ,x)
        Field_dy = self.Dy(Field.flatten(0,1)).reshape(Field.shape)#(Batch, 4, z, y ,x)
        Field_dz = self.Dz(Field.flatten(0,1)).reshape(Field.shape)#(Batch, 4, z, y ,x)
        PhysicsPart  = torch.stack([-Field_dx[:,3], -Field_dy[:,3], self.thermal_factor*T[:,0]/self.p_list*o[:,0]],1)#(Batch,3,z, y ,x)
        PhysicsPart  = F.pad(PhysicsPart,(0,0,0,0,0,0,0,1)) #(Batch,4,z, y ,x)
        xydirection  = - u*Field_dx - v*Field_dy
        PhysicsPart  = xydirection - o*Field_dz + PhysicsPart
        Delta_Fd = ExternalForce + PhysicsPart
        Field    = Field+ Delta_Fd
        constrain= Field_dx[:,0] + Field_dy[:,1] + self.Dz(o[:,0])
        if not self.monitor:
            return Field.flatten(1,2),(constrain**2).mean()
        else:
            return Field.flatten(1,2),(constrain**2).mean(),{"ExternalForceFactor":(ExternalForce**2).mean().item(),
                                                             "PhysicsDrivenFactor":(PhysicsPart**2).mean().item(),
                                                             "xypannelDrivenFactor":(xydirection**2).mean().item()}

class EulerEquationModel2(BaseModel):
    def __init__(self, args, backbone):
        super().__init__()
        self.Dx= First_Derivative_Layer(position=-1, dim=3, mode=2)
        self.Dy= First_Derivative_Layer(position=-2, dim=3, mode=2)
        self.Dz= First_Derivative_Layer(position=-3, dim=3, mode=1)
        self.thermal_factor = nn.Parameter(torch.randn(3).reshape(1,3,1,1))
        #self.p_list         = nn.Parameter(torch.Tensor([10,8.5,5]).reshape(1,3,1,1),requires_grad=False)
        self.backbone =  backbone
        self.monitor  = True
        self.physics_num= args.physics_num if hasattr(args,'physics_num') else 4
    def calculateEPI(self,Field):
        # u^{t+1} &= u^{t} + F_x - \nabla (Vu)  + u \nabla\cdot V - \partial_x\phi\\
        # v^{t+1} &= v^{t} + F_y - \nabla (Vv)  + v \nabla\cdot V - \partial_y\phi\\
        # T^{t+1} &= T^{t} + Q/C_v + \frac{RT}{C_pp}\omega  - \nabla (VT) +T \nabla\cdot V\\
        # \phi^{t+1}&=\phi^{t} + wg  - \nabla (V\phi)+ \phi \nabla\cdot V \\
        # 0&\approx \nabla\cdot V
        # input -> Field  = [u ,v, T, p] --> (Batch, 4, z, y ,x)
        # need generate unknown data [Fx, Fy , Q, W, o]
        #print(Field.shape)
        b, si_z, i_y, i_x = Field.shape
        s=self.physics_num
        i_z= si_z//self.physics_num
        MachineLearningPart = self.backbone(Field).reshape(b, s+1, i_z, i_y, i_x) #(Batch, 5, z, y ,x)
        ExternalForce = MachineLearningPart[:,:-1] #(Batch, 4, z, y ,x)
        o = MachineLearningPart[:,-1:] #(Batch, 1, z, y ,x)
        Field = Field.reshape(b, s, i_z, i_y,  i_x) #(Batch, 5, z, y ,x)
        u = Field[:,0:1]#(Batch, 1, z, y ,x)
        v = Field[:,1:2]#(Batch, 1, z, y ,x)
        T = Field[:,2:3]#(Batch, 1, z, y ,x)
        p = Field[:,3:4]#(Batch, 1, z, y ,x)
        V = torch.cat([u,v,o],1)#(Batch, 3, z, y ,x)
        Nabla_cdot_V = (self.Dx(u[:,0]) + self.Dy(v[:,0]) + self.Dz(o[:,0])).unsqueeze(1)#(Batch, 1, z, y ,x)
        Nabla_V_Field= Nabla_cdot_V*Field #(Batch, 4, z, y ,x)
        Vphysics     = torch.stack([V*u,V*v,V*T,V*p],1)#(Batch,4, 3, z, y ,x)

        Vphysics_dx  = self.Dx(Vphysics[:,:,0].flatten(0,1)).reshape(Field.shape)#(Batch, 4, z, y ,x)
        Vphysics_dy  = self.Dy(Vphysics[:,:,1].flatten(0,1)).reshape(Field.shape)#(Batch, 4, z, y ,x)
        Vphysics_dz  = self.Dz(Vphysics[:,:,2].flatten(0,1)).reshape(Field.shape)#(Batch, 4, z, y ,x)
        PhysicsPart  = -Vphysics_dx - Vphysics_dy - Vphysics_dz + Nabla_V_Field #(Batch,4,z, y ,x)

        InteractionPart= torch.stack([self.Dx(p[:,0]),
                                      self.Dy(p[:,0]),
                                      self.thermal_factor*T[:,0]*o[:,0]],1)#(Batch,3,z, y ,x)
        InteractionPart= F.pad(InteractionPart,(0,0,0,0,0,0,0,1)) #(Batch,4,z, y ,x)
        return Field, ExternalForce , PhysicsPart , InteractionPart, Nabla_cdot_V

    def forward(self, Field):
        Field, ExternalForce , PhysicsPart , InteractionPart, Nabla_cdot_V = self.calculateEPI(Field)

        Delta_Fd     = ExternalForce + PhysicsPart + InteractionPart
        Field        = Field+ Delta_Fd
        constrain    = Nabla_cdot_V
        if not self.monitor:
            return Field.flatten(1,2),(constrain**2).mean()
        else:
            return Field.flatten(1,2),(constrain**2).mean(),{"ExternalForceFactor":(ExternalForce**2).mean().item(),
                                                             "PhysicsDrivenFactor":(PhysicsPart**2).mean().item(),
                                                             "InteractionPart":(InteractionPart**2).mean().item()}

class EulerEquationModel3(EulerEquationModel2):
    def forward(self, Field):
        Field, ExternalForce , PhysicsPart , InteractionPart, Nabla_cdot_V = self.calculateEPI(Field)
        Delta_Fd     = ExternalForce + PhysicsPart + InteractionPart
        Field        = Field+ Delta_Fd
        constrain    = ((ExternalForce**2).mean() - ((PhysicsPart + InteractionPart)**2).mean())**2
        if not self.monitor:
            return Field.flatten(1,2),constrain
        else:
            return Field.flatten(1,2),constrain,{"ExternalForceFactor":(ExternalForce**2).mean().item(),
                                                             "PhysicsDrivenFactor":(PhysicsPart**2).mean().item(),
                                                             "InteractionPart":(InteractionPart**2).mean().item()}

class EulerEquationModel4(EulerEquationModel2):
    def forward(self, Field):
        Field, ExternalForce , PhysicsPart , InteractionPart, Nabla_cdot_V = self.calculateEPI(Field)
        Delta_Fd     = ExternalForce + PhysicsPart + InteractionPart
        Field        = Field+ Delta_Fd
        constrain    = torch.zeros_like(self.thermal_factor).mean()
        if not self.monitor:
            return Field.flatten(1,2),constrain
        else:
            return Field.flatten(1,2),constrain,{"ExternalForceFactor":(ExternalForce**2).mean().item(),
                                        "PhysicsDrivenFactor":(PhysicsPart**2).mean().item(),
                                        "InteractionPart":(InteractionPart**2).mean().item(),
                                        "Nabla_cdot_V":(Nabla_cdot_V**2).mean().item()}

import numpy as np
class ConVectionModel(BaseModel):
    def __init__(self, args, backbone):
        super().__init__()
        h, w = backbone.img_size[-2:]
        print(f'h intervel: {h} , w intervel: {w}')
        if h in [51,49,47]:
            Hdx = 6371000*torch.sin(torch.linspace(0,720,49)/720*np.pi)*2*np.pi/w
            Hdx = Hdx.reshape(1,1,1,49,1)[...,1:-1,:] # (1,1,1,47,1)
            shape = Hdx.shape
            # the input will be (1,1,1,47,1)
            # if h == 51:Hdx = F.pad(Hdx.flatten(0,1),(0,0,2,2),mode='replicate').reshape(*shape[:-2],-1,shape[-1])# (1,1,1,51,1)
            # if h == 49:Hdx = F.pad(Hdx.flatten(0,1),(0,0,1,1),mode='replicate').reshape(*shape[:-2],-1,shape[-1])# (1,1,1,49,1)

            Hdy = torch.Tensor([6371000*np.pi/48])
            print(f"please notice we will using dt= 3600*6 as intertime")
            self.DT = 3600*6
            Hdx = Hdx/self.DT
            Hdy = Hdy/self.DT
            self.Hdx = Hdx
            self.Hdy = Hdy
        elif h in [32]:
            Hdx = 6371000*torch.sin(torch.linspace(0,1,34)*np.pi)*2*np.pi/w
            Hdx = Hdx[1:-1].reshape(1,1,1,32,1)
            shape = Hdx.shape
            Hdy = torch.Tensor([6371000*np.pi/32])
            print(f"please notice we will using dt= 3600*1 as intertime")
            self.DT = 3600*1 # notice for weatherbench module we have 1 hour dataset and 6 hour dataset.
            Hdx = Hdx/self.DT
            Hdy = Hdy/self.DT
            self.Hdx = Hdx
            self.Hdy = Hdy
        else:
            raise NotImplementedError(f"for h not in [47,49,51], TODO.......")
        self.Dx= First_Derivative_Layer(position=-1, dim=3, mode=2, pad_mode='replicate', intervel=Hdx)
        self.Dy= First_Derivative_Layer(position=-2, dim=3, mode=2, pad_mode='replicate', intervel=Hdy)
        self.backbone =  backbone
        self.monitor = True

    def calculate_Advection(self,Field):
        u            = Field[:,0:1]
        v            = Field[:,1:2]
        Field_dx     = self.Dx(Field.flatten(0,1)).reshape(Field.shape)
        Field_dy     = self.Dy(Field.flatten(0,1)).reshape(Field.shape)

        Advection    = (u*Field_dx + v*Field_dy) #(B,P,z,y,x)
        return Advection

    def forward(self, normlized_Field):
        normlized_Dt = self.backbone(normlized_Field) #(B,P,z,y,x)
        return normlized_Dt

class DirectSpace_Feature_Model(BaseModel):
    def __init__(self, args, backbone):
        super().__init__()
        h, w = backbone.img_size[-2:]
        print(f'h intervel: {h} , w intervel: {w}')
        if h in [51,49,47]:
            Hdx = 6371000*torch.sin(torch.linspace(0,720,49)/720*np.pi)*2*np.pi/w
            Hdx = Hdx.reshape(1,1,1,49,1)[...,1:-1,:] # (1,1,1,47,1)
            shape = Hdx.shape
            # the input will be (1,1,1,47,1)
            # if h == 51:Hdx = F.pad(Hdx.flatten(0,1),(0,0,2,2),mode='replicate').reshape(*shape[:-2],-1,shape[-1])# (1,1,1,51,1)
            # if h == 49:Hdx = F.pad(Hdx.flatten(0,1),(0,0,1,1),mode='replicate').reshape(*shape[:-2],-1,shape[-1])# (1,1,1,49,1)

            Hdy = torch.Tensor([6371000*np.pi/48])
            print(f"please notice we will using dt= 3600*6 as intertime")
            self.DT = 3600*6
            Hdx = Hdx/self.DT
            Hdy = Hdy/self.DT
            self.Hdx = Hdx
            self.Hdy = Hdy
        elif h in [32]:
            Hdx = 6371000*torch.sin(torch.linspace(0,1,34)*np.pi)*2*np.pi/w
            Hdx = Hdx[1:-1].reshape(1,1,1,32,1)
            shape = Hdx.shape
            Hdy = torch.Tensor([6371000*np.pi/32])
            print(f"please notice we will using dt= 3600*1 as intertime")
            self.DT = 3600*1 # notice for weatherbench module we have 1 hour dataset and 6 hour dataset.
            Hdx = Hdx/self.DT
            Hdy = Hdy/self.DT
            self.Hdx = Hdx
            self.Hdy = Hdy
        else:
            raise NotImplementedError(f"for h not in [47,49,51], TODO.......")

        self.Dx= First_Derivative_Layer(position=-1, dim=2, mode='three-point-stencil', pad_mode='replicate', intervel=self.Hdx)
        self.Dy= First_Derivative_Layer(position=-2, dim=2, mode='three-point-stencil', pad_mode='replicate', intervel=self.Hdy)
        self.Dxy= Second_Derivative_Layer(position=(-2,-1), mode= 'nine-point-stencil', pad_mode='replicate', intervel=self.Hdy*self.Hdx)
        self.backbone    =  backbone
        self.physics_num = args.physics_num

    def forward(self, Field):
        # --> [Batch, z, P,  y, x] or --> [Batch, z, T, P, y, x]
        oshape = Field.shape
        if  Field.shape[1]!=self.physics_num:
            Field = Field.reshape(Field.shape[0],self.physics_num,-1,*Field.shape[2:])
        u  = Field[:,0:1] # --> [Batch, 1 ,P, y, x]
        v  = Field[:,1:2] # --> [Batch, 1 ,P, y, x]
        with torch.no_grad():
            Field_dx   =  self.Dx(Field.flatten(0,-3).unsqueeze(1)).reshape(Field.shape) # --> [Batch, z, P,  y, x]
            Field_dy   =  self.Dy(Field.flatten(0,-3).unsqueeze(1)).reshape(Field.shape) # --> [Batch, z, P,  y, x]
            Field_dxy  = self.Dxy(Field.flatten(0,-3).unsqueeze(1)).reshape(Field.shape) # --> [Batch, z, P,  y, x]

            Field = torch.cat([
                1*Field,u*v,
                1*Field_dx,  1*Field_dy,  1*Field_dxy,
                u*Field_dx,  u*Field_dy,  u*Field_dxy,
                v*Field_dx,  v*Field_dy,  v*Field_dxy,
                u*v*Field_dx,u*v*Field_dy,u*v*Field_dxy,
            ],dim=1)
        Field = self.backbone(Field)
        return Field.reshape(oshape)


from collections import OrderedDict

import numpy

from theano import config

from fuel.transformers import Transformer
from picklable_itertools.extras import equizip

class MaximumFrameCache(Transformer):
    """Cache examples, and create batches of maximum number of frames.

    Given a data stream which reads large chunks of data, this data
    stream caches these chunks and returns batches with a maximum number
    of acoustic frames.

    Parameters
    ----------
    max_frames : int
        maximum number of frames per batch

    Attributes
    ----------
    cache : list of lists of objects
        This attribute holds the cache at any given point. It is a list of
        the same size as the :attr:`sources` attribute. Each element in
        this list is a deque of examples that are currently in the
        cache. The cache gets emptied at the start of each epoch, and gets
        refilled when needed through the :meth:`get_data` method.

    """
    def __init__(self, data_stream, max_frames, rng):
        super(MaximumFrameCache, self).__init__(
            data_stream)
        self.max_frames = max_frames
        self.cache = OrderedDict([(name, []) for name in self.sources])
        self.num_frames = []
        self.rng = rng

    def next_request(self):
        curr_max = 0
        for i, n_frames in enumerate(self.num_frames):
            # Select max number of frames because of future padding
            curr_max = max(n_frames, curr_max)
            total = curr_max * (i + 1)
            if total >= self.max_frames:
                return i + 1
        return len(self.num_frames)

    def get_data(self, request=None):
        if not self.cache[self.cache.keys()[0]]:
            self._cache()
        data = []
        request = self.next_request()
        for source_name in self.cache:
            data.append(numpy.asarray(self.cache[source_name][:request]))
        self.cache = OrderedDict([(name, dt[request:]) for name, dt
                                  in self.cache.iteritems()])
        self.num_frames = self.num_frames[request:]

        return tuple(data)

    def get_epoch_iterator(self, **kwargs):
        self.cache = OrderedDict([(name, []) for name in self.sources])
        self.num_frames = []
        return super(MaximumFrameCache, self).get_epoch_iterator(**kwargs)

    def _cache(self):
        data = next(self.child_epoch_iterator)
        indexes = range(len(data[0]))
        self.rng.shuffle(indexes)
        data = [[dt[i] for i in indexes] for dt in data]
        self.cache = OrderedDict([(name, self.cache[name] + dt) for name, dt
                                  in equizip(self.data_stream.sources, data)])
        self.num_frames.extend([x.shape[0] for x in data[0]])

class Transpose(Transformer):
    """Transpose axes of datastream.
    """
    def __init__(self, datastream, axes_list):
        super(Transpose, self).__init__(datastream)
        self.axes_list = axes_list

    def get_data(self, request=None):
        data = next(self.child_epoch_iterator)
        transposed_data = []
        for axes, data in zip(self.axes_list, data):
            transposed_data.append(numpy.transpose(data, axes))
        return transposed_data

class AddUniformAlignmentMask(Transformer):
    """Adds an uniform alignment mask to the incoming batch.

    Parameters
    ----------

    """
    def __init__(self, data_stream):
        super(AddUniformAlignmentMask, self).__init__(data_stream)
        self.sources = self.data_stream.sources + ('alignment',)

    def get_data(self, request=None):
        data = next(self.child_epoch_iterator)
        sources = self.data_stream.sources

        x_idx = sources.index('x')
        y_idx = sources.index('y')
        x_mask_idx = sources.index('x_mask')
        y_mask_idx = sources.index('y_mask')

        batch_size = data[x_idx].shape[1]
        max_len_output = data[y_idx].shape[0]
        max_len_input = data[x_idx].shape[0]
        mask_shape = (max_len_output, batch_size, max_len_input)
        alignment = numpy.zeros(mask_shape, dtype=config.floatX)

        for k in xrange(batch_size):
            in_size = numpy.count_nonzero(data[x_mask_idx][:,k])
            out_size = numpy.count_nonzero(data[y_mask_idx][:,k])
            n = int(in_size/out_size) # Maybe clever way than int to do this
            v = numpy.hstack([numpy.ones(n, dtype=config.floatX),
                              numpy.zeros(max_len_input - n,
                                          dtype=config.floatX)])
            alignment[0,k] = v
            for i in xrange(1, out_size):
                alignment[i,k] = numpy.roll(v, i*n)

            # DEBUG
            #plt.figure()
            #plt.imshow(alignment[:,k,:], cmap='gray', interpolation='none')
            #plt.show()
        data = data + (alignment,)

        return data

class AlignmentPadding(Transformer):
    def __init__(self, data_stream, alignment_source):
        super(AlignmentPadding, self).__init__(data_stream)
        self.alignment_source = alignment_source

    def get_data(self, request=None):
        data = next(self.child_epoch_iterator)
        data = OrderedDict(equizip(self.sources, data))

        alignments = data[self.alignment_source]

        input_lengths = [alignment.shape[1] for alignment in alignments]
        output_lengths = [alignment.shape[0] for alignment in alignments]
        max_input_length = max(input_lengths)
        max_output_length = max(output_lengths)

        batch_size = len(alignments)

        padded_alignments = numpy.zeros((max_output_length, batch_size,
                                         max_input_length))

        for i, alignment in enumerate(alignments):
            out_size, inp_size = alignment.shape
            padded_alignments[:out_size, i, :inp_size] = alignment

        data[self.alignment_source] = padded_alignments

        return data.values()

class Reshape(Transformer):
    """Reshapes data in the stream according to shape source."""
    def __init__(self, data_source, shape_source, **kwargs):
        super(Reshape, self).__init__(**kwargs)
        self.data_source = data_source
        self.shape_source = shape_source
        self.sources = tuple(source for source in self.data_stream.sources
                             if source != shape_source)

    def get_data(self, request=None):
        data = next(self.child_epoch_iterator)
        data = OrderedDict(zip(self.data_stream.sources, data))
        shapes = data.pop(self.shape_source)
        reshaped_data = []
        for dt, shape in zip(data[self.data_source], shapes):
            reshaped_data.append(dt.reshape(shape))
        data[self.data_source] = reshaped_data
        return data.values()

class Subsample(Transformer):
    def __init__(self, data_stream, source, step):
        super(Subsample, self).__init__(data_stream)
        self.source = source
        self.step = step

    def get_data(self, request=None):
        data = next(self.child_epoch_iterator)
        data = OrderedDict(equizip(self.sources, data))
        dt = data[self.source]

        indexes = ((slice(None, None, self.step),) +
                   (slice(None),) * (len(dt.shape) - 1))
        subsampled = dt[indexes]
        data[self.source] = subsampled
        return data.values()

class WindowFeatures(Transformer):
    def __init__(self, data_stream, source, window_size):
        super(WindowFeatures, self).__init__(data_stream)
        self.source = source
        self.window_size = window_size

    def get_data(self, request=None):
        data = next(self.child_epoch_iterator)
        data = OrderedDict(equizip(self.sources, data))
        feature_batch = data[self.source]

        windowed_features = []
        for features in feature_batch:
            features_padded = features.copy()

            features_shifted = [features]
            # shift forward
            for i in xrange(self.window_size / 2):
                feats = numpy.roll(features_padded, i + 1, axis=0)
                feats[:i + 1, :] = 0
                features_shifted.append(feats)
            features_padded = features.copy()

            # shift backward
            for i in xrange(self.window_size / 2):
                feats = numpy.roll(features_padded, -i - 1, axis=0)
                feats[-i - 1:, :] = 0
                features_shifted.append(numpy.roll(features_padded, -i - 1,
                                                   axis=0))
            windowed_features.append(numpy.concatenate(
                features_shifted, axis=1))
        data[self.source] = windowed_features
        return data.values()

class Normalize(Transformer):
    """Normalizes each features : x = (x - means)/stds"""
    def __init__(self, data_stream, means, stds):
        super(Normalize, self).__init__(data_stream)
        self.means = means
        self.stds = stds

    def get_data(self, request=None):
        data = next(self.child_epoch_iterator)
        data = OrderedDict(zip(self.data_stream.sources, data))
        for i in range(len(data['features'])):
            data['features'][i] -= self.means
            data['features'][i] /= self.stds
        return data.values()

def length_getter(dt):
    def get_length(k):
        return dt[k].shape[0]
    return get_length

class SortByLegth(Transformer):
    def __init__(self, data_stream, source='features'):
        super(SortByLegth, self).__init__(data_stream)
        self.source = source

    def get_data(self, request=None):
        data = next(self.child_epoch_iterator)
        data = OrderedDict(zip(self.data_stream.sources, data))
        dt = data[self.source]
        indexes = sorted(range(len(dt)), key=length_getter(dt))
        for source in self.sources:
            data[source] = [data[source][k] for k in indexes]
        return data.values()

class Cut(Transformer):
    """Cuts input to given length, and prepare targets, and cut the features
    as well.

    """
    def __init__(self, data_stream, seq_len):
        super(Cut, self).__init__(data_stream)
        self.seq_len = seq_len
        self.sources = ('features', 'targets')

    def get_data(self, request=None):
        data = next(self.child_epoch_iterator)
        data = OrderedDict(zip(self.data_stream.sources, data))
        for i in range(len(data['features'])):
            data['targets'][i] = data['features'][i, 1:seq_len+1, :]
            data['features'][i] = data['features'][i, :seq_len, :]
        return data.values()


import datetime
import pendulum
from airflow.decorators import dag, task
import pandas
from sqlalchemy import create_engine, text

CONN_STRING = "sqlite:///melb_housing_data.sqlite3"
MELB_URI = "https://cs.famaf.unc.edu.ar/~mteruel/datasets/diplodatos/melb_data.csv"
AIRBNB_URI = "https://cs.famaf.unc.edu.ar/~mteruel/datasets/diplodatos/cleansed_listings_dec18.csv"

@dag(
    'etl_dag',
    schedule_interval="0 0 * * *",
    start_date=pendulum.datetime(2022, 5, 1, tz="UTC"),
    catchup=False,
    dagrun_timeout=datetime.timedelta(minutes=60),
)
def Etl():
    @task
    def extract():
        # NOTA: Se consideró "extracción" a la obtención de los datos desde el repositorio remoto, para
        # cargarlos en la base de datos SQLite local. Se usa esta base de datos para compartir la información
        # entre las Task en lugar de usar XComs.
        #
        # Por lo tanto, en cada task los datos se van a obtener desde la BD
        engine = create_engine(CONN_STRING, echo=True)

        melb_df = pandas.read_csv(MELB_URI)
        melb_df.to_sql("mlb_data", con=engine, if_exists="replace")

        airbnb_df_all = pandas.read_csv(AIRBNB_URI)
        airbnb_df_all.to_sql("airbnb_data", con=engine, if_exists="replace")

    @task
    def transform():
        engine = create_engine(CONN_STRING, echo=True)
        melb_df = pandas.read_sql_table('mlb_data', engine)
        airbnb_df_all = pandas.read_sql_table('airbnb_data', engine)

        colums_predict_price = [
            "Address",
            "Rooms",
            "Price",
            "Bedroom2",
            "Bathroom",
            "Landsize",
            "Car",
        ]
        melb_df.loc[:, colums_predict_price]

        min_price = melb_df.Price.quantile(0.05)
        max_price = melb_df.Price.quantile(0.95)

        melb_df_filtered = melb_df[
            (melb_df["Price"] > min_price) & (melb_df["Price"] < max_price)
        ]

        airbnb_df_all["zipcode"] = pandas.to_numeric(airbnb_df_all.zipcode, errors="coerce")

        airbnb_df_all["airbnb_zipcode_counter"] = airbnb_df_all.zipcode.value_counts()

        airbnb_df_all.loc[:3, ["accommodates", "bathrooms", "bedrooms", "beds"]]

        relevant_cols = [
            "price",
            "weekly_price",
            "monthly_price",
            "security_deposit",
            "review_scores_rating",
            "review_scores_location",
            "zipcode",
        ]

        airbnb_price_by_zipcode = (
            airbnb_df_all[relevant_cols]
            .groupby("zipcode")
            .agg(
                airbnb_price_mean=("price", "mean"),
                airbnb_price_median=("price", "median"),
                airbnb_price_min=("price", "min"),
                airbnb_price_max=("price", "max"),
                airbnb_weekly_price_mean=("weekly_price", "mean"),
                airbnb_monthly_price_mean=("monthly_price", "mean"),
                airbnb_security_deposit_mean=("security_deposit", "mean"),
                airbnb_review_scores_rating_mean=("review_scores_rating", "mean"),
                airbnb_review_scores_location_mean=("review_scores_location", "mean"),
            )
            .reset_index()
        )

        reg_mayor_50 = airbnb_df_all[airbnb_df_all["airbnb_zipcode_counter"] >= 25]
        zipcode_reg = reg_mayor_50["zipcode"]
        airbnb_price_by_zipcode_filtered = airbnb_price_by_zipcode[
            airbnb_price_by_zipcode["zipcode"].isin(zipcode_reg)
        ]

        merged_sales_df = melb_df_filtered.merge(
            airbnb_price_by_zipcode_filtered, how="left", left_on="Postcode", right_on="zipcode"
        )
        merged_sales_df.to_sql("merged_sales_data", con=engine, if_exists="replace")

    @task
    def load():
        # En este punto, el "load" en lugar de cargar el resultado en el data warehouse, se
        # desacrgó en un archivo csv.
        engine = create_engine(CONN_STRING, echo=True)
        merged_sales_df = pandas.read_sql_table('merged_sales_data', engine)
        merged_sales_df.to_csv("melb_data_extended_{}.csv".format(datetime.date.today()), index=None)

    extract() >> transform() >> load()

dag = Etl()


# File name:    MichaelTang_Homework6b.py
# Due date:     Saturday, June 12, 2021
# Description:  Fix all the compilation, run time, and
#               logic errors from "SlotMachine Start.py".
#
#               Version 6b introduces a score file using a
#               dictionary to write and update player scores.

import random
import time
from FileReadWrite import *

# writeALine - write to a multiline file
# readALine - reading a file

# item "lemon" in list "reelList" is missing a beginning single quote
reelList = ['orange', 'orange', 'orange', 'lemon', 'lemon', 'lemon', \
              'plum', 'plum', 'plum', 'cherries', 'cherries', 'cherries', \
              'banana', 'banana', 'banana', 'bar', 'bar', 'Lucky 7']
nPicturesInReel = len(reelList) # nPicturesInReel should produce a number for the ending number
                                # of the 'randrange' call

DATA_FILE_PATH = 'SlotMachineData_MichaelTang3.txt'
namesAndScoresDict = {} # dictionary that will store data from DATA_FILE_PATH

# for the extra extra challenge - obtaining the player's name
playerName = input('What is your name? ')
print()

# check to see if a score data file exists
fileInDir = fileExists(DATA_FILE_PATH)

nCoins = 100  # new player gets 100 coins

def newPlayerPrintStatement():
    print('Welcome to the Slot Machine game ' + playerName + '!\nYou have', nCoins, 'coins to start.  Good luck.')

# Should a score data file exits, open it up and read to see if the person has played before
# If so, grab their score
if fileInDir:
    openedFile = openFileForReading(DATA_FILE_PATH)

    while True:  # read through file, build up list of lists
        savedDataString = readALine(openedFile)
        if savedDataString == False:  # no more lines
            break
        savedDataDict = savedDataString.split(',')  # split the key-value pair up by the comma

        # Save the dictionary to "namesAndScoresDict"
        # Casting the value automatically to an integer right now for simplicity

        name = savedDataDict[0] # get the name
        score = int(savedDataDict[1]) # get the previous score
        namesAndScoresDict[name] = score

    closeFile(openedFile)

    # See if this player is already in the list
    # If the player's name is found, get the score
    if playerName in namesAndScoresDict:
        nCoins = namesAndScoresDict[playerName]
        print('Hello ' + playerName + '!!!')
        print('Thanks for coming back to the Slot Machine game. Your current score is', nCoins, '.')
    else:
        newPlayerPrintStatement()

else:
    newPlayerPrintStatement()

print()

def payTable(myList):           # functions in Python are defined by keyword "def", not "function"
    picture1 = myList[0]
    picture2 = myList[1]
    picture3 = myList[2]
    if myList.count(picture1) == 3:
        if picture1== 'Lucky 7':
            nCoinsWon = 500
        elif picture1== 'bar':
            nCoinsWon = 100
        else:
            nCoinsWon = 10

    else:
        # second statement originally showing picture2 equalling itself
        # instead, it should see if picture2 is the same as picture 3,
        # which is another logical combination of two slots having the same "image"
        if (picture1 == picture2) or (picture2 == picture3) or (picture1 == picture3):
            nCoinsWon = 2
        else:
            nCoinsWon = 0

    return nCoinsWon # function should return the number of coins won, as indicated by the function call

while True:

    bet = input('How many coins do you want to bet (defaults to 1, enter 0 to quit): ')

    # put either single or double quotes around the 0 because the comparison should be to a string,
    # which is how Python stores a value from an "input" statement
    if bet == '0':
        break

    elif bet == '':
        bet = 1

    try:
        bet = int(bet)  # cast the variable "bet" as an integer so math can be done on it
    except:
        print('Sorry, \'' + bet + '\' is not a valid bet.')
        print('Your entry must be a positive integer. Please try again.')
        print()
        continue

    if bet < 0:
        print('Sorry, \'' + '{:d}'.format(bet) + '\' is not a valid bet.')
        print('Your entry must be a positive integer. Please try again.')
        print()
        continue

    elif bet > int(nCoins):
        print('Sorry, you do not have that many coins, you only have: ' + '{:d}'.format(nCoins))
        print()
        continue

    resultList = []
    for spin in range(3):
        thisReelIndex = random.randrange(0, nPicturesInReel)
        thisPicture = reelList[thisReelIndex]
        resultList.append(thisPicture)

    print( 'Spinning ... ')
    print()
    time.sleep(.5)
    print( '     ', resultList[0])
    time.sleep(.5)
    print( '     ', resultList[1])
    time.sleep(.5)
    print( '     ', resultList[2])
    print( )

    nCoins = int(nCoins) - bet   # nCoins should show the amount after having placed the bet...minus sign instead of plus
    payOut = bet * payTable(resultList)

    if payOut == 0:         # this is comparing to see if the payout is 0, not assigning it to 0 - therefore, should be a double equals sign operator
        print( 'Sorry - you lose.')
    else:
        print( 'You won', payOut, 'coins.  Cha-ching!')
        if payOut > 50:
            print( '                         WOOOOO  HOOOOOOOOOOO!!!!')

    nCoins = nCoins + payOut

    # format variable "nCoins" as an integer because it will not print correctly under the stock print command
    # also - added a few spaces were added for clearer display
    print( 'You now have ' + '{:d}'.format(nCoins) + ' coins.')
    print()

print( 'Sorry to see you go.  Come back again soon.')

# ending the game - write the name and score to the data file
openedFile = openFileForWriting(DATA_FILE_PATH)

# Update the player's score
namesAndScoresDict[playerName] = nCoins # update the score to the player already in the dictionary

# This for loop will iterate through the dictionary and write each line to the file
for entry in namesAndScoresDict:
    if entry == '':
        break

    textToWrite = ''
    textToWrite = entry + ',' + str(namesAndScoresDict[entry])
    writeALine(openedFile, textToWrite)

openedFile.close()


from mock import patch

import jenkins
from tests.base import JenkinsTestBase
from tests.helper import build_response_mock

class JenkinsGetJobFolderTest(JenkinsTestBase):

    @patch.object(jenkins.Jenkins, 'jenkins_open')
    def test_simple(self, jenkins_mock):
        folder, name = self.j._get_job_folder('my job')
        self.assertEqual(folder, '')
        self.assertEqual(name, 'my job')

    @patch.object(jenkins.Jenkins, 'jenkins_open')
    def test_single_level(self, jenkins_mock):
        folder, name = self.j._get_job_folder('my folder/my job')
        self.assertEqual(folder, 'job/my folder/')
        self.assertEqual(name, 'my job')

    @patch.object(jenkins.Jenkins, 'jenkins_open')
    def test_multi_level(self, jenkins_mock):
        folder, name = self.j._get_job_folder('folder1/folder2/my job')
        self.assertEqual(folder, 'job/folder1/job/folder2/')
        self.assertEqual(name, 'my job')

class JenkinsCreateFolderTest(JenkinsTestBase):

    @patch('jenkins.requests.Session.send', autospec=True)
    def test_create_new_folder(self, session_send_mock):
        session_send_mock.side_effect = iter([
            build_response_mock(200, self.crumb_data),  # crumb
            build_response_mock(200, None),  # request
        ])
        self.j.create_folder(u'New-Test-Folder')

    @patch('jenkins.requests.Session.send', autospec=True)
    def test_create_new_folder_that_already_exists(self, session_send_mock):
        session_send_mock.side_effect = iter([
            build_response_mock(200, self.crumb_data),  # crumb
            build_response_mock(400),  # request
        ])

        folder_name = u'Folder-that-already-exists'

        with self.assertRaises(jenkins.JenkinsException) as context_manager:
            self.j.create_folder(folder_name)
        self.assertEqual(
            str(context_manager.exception),
            u'Error creating folder [' + folder_name + ']. Probably it already exists.')

    @patch('jenkins.requests.Session.send', autospec=True)
    def test_create_new_folder_that_already_exists_ignoring_errors(self, session_send_mock):
        session_send_mock.side_effect = iter([
            build_response_mock(200, self.crumb_data),  # crumb
            build_response_mock(400),  # request
        ])

        self.j.create_folder(u'Folder-that-already-exists', ignore_failures=True)


from django.shortcuts import render, redirect
from jeune.models import subscribers
import datetime
from jeune.forms import SubscribersForm, MailMessageForm
from django.contrib import messages
from django.core.mail import send_mail
from django_pandas.io import read_frame
from django.db.models import Q
# Create your views here.

def dashboard(request):
    today = datetime.date.today()
    year = today.year
    month = today.month
    getAllOfThem = subscribers.objects.all()
    email_count = subscribers.objects.all().count()
    new_email_this_month_count = subscribers.objects.filter(created_at__year=year, created_at__month=month).count()
    unsubscribed_email_count = subscribers.objects.exclude(status="True").count()
    return render(request, 'jeune/dashbord.html', {'getAllOfThem':getAllOfThem,
    'email_count':email_count, 'new_email_this_month_count':new_email_this_month_count,
    'unsubscribed_email_count':unsubscribed_email_count})

def welcome(request):
    if request.method == 'POST':
        form = SubscribersForm(request.POST)
        if form.is_valid():
            instance = form.save(commit=False)
            if subscribers.objects.filter(Q(email=instance.email)).exists() and subscribers.objects.filter(Q(status=True)):
                messages.warning(request, 'Subscriber already exist!')
                return redirect('/unsubscribe')
            form.save()
            messages.success(request, 'User successfully subscribed')
            return redirect('/welcome')
    else:
        form = SubscribersForm()
    context = {
       'form': form,
    }
    return render(request, "jeune/welcome.html", context)

def emails(request):
    emails = subscribers.objects.exclude(status=False)
    df = read_frame(emails, fieldnames=['email'])
    mail_list = df['email'].values.tolist()
    # print(mail_list)

    if request.method == 'POST':
        form = MailMessageForm(request.POST)
        if form.is_valid():
            form.save()

            title = form.cleaned_data.get('title')
            message = form.cleaned_data.get('message')
            send_mail(
            title,
            message,
            '',
            mail_list,
            fail_silently=False,
            )

            messages.success(request, 'Message has been sent to the mail list')
            return redirect('/welcome')
    else:
        form = MailMessageForm()
        context = {
            'form': form,
        }
    return render(request, "jeune/emails.html", context)

def unsubscribe(request):
    form = SubscribersForm(request.POST or None)
    if form.is_valid():
        instance = form.save(commit=False)
        subscribers.objects.filter(email=instance.email).update(status = False)
        if  subscribers.objects.filter(email=instance.email).exists():
            messages.success(request, 'User has been unsubscribed successfully!')
            return redirect('/unsubscribe')
        else:
            messages.success(request, 'Sorry, we did not find your email!')
            return redirect('/unsubscribe')
    context = {
    "form" : form,
    }
    return render(request, "jeune/unsubscribe.html", context)

# Code to print the lists of all the time zones
# import pytz

# print('Timezones')
# for timeZone in pytz.all_timezones:
#     print(timeZone)


### pdfからテキストの抽出

import os
import subprocess

# フォルダは予め作っておくこと
input_folder = "../pdf/"
output_folder = "../tmp/process1/"

documents = os.listdir(input_folder)
for i, doc in enumerate(documents):

	if doc.startswith("."):
		continue

	input_path = input_folder + doc
	output_path = output_folder + str(i) + ".txt"

	commands = ["sh", "extract_text.sh", input_path, output_path]
	subprocess.call(commands)


import aiohttp
import datetime
import json
import random

import common.constants as const

from common.uhtml import ItemInfo
from common.utils import gen_uhtml_img_code

async def anilist_num_entries(query, query_vars, session):
    '''
    Oneshot query to determine the total number of entries
    in an anilist query. Requires the query to have pageInfo.

    Does not have the anilist context manager lock.
    '''
    async with session.post(const.ANILIST_API, json={'query': query, 'variables': query_vars}) as r:
        resp = await r.json()

        if r.status != 200:
            return

    return resp['data']['Page']['pageInfo']['total']

async def anilist_search(medium, search, anilist_man):
    query = '''
    query ($search: String) {
        Page (page: 1, perPage: 1) {
            media (MEDIUM_PLACEHOLDER, search: $search, isAdult: false) {
                id
                idMal
                title {
                    english
                    romaji
                }
                coverImage {
                    extraLarge
                }
                status
                episodes
                chapters
                description
                averageScore
            }
        }
    }
    '''

    mq = 'type: ANIME' if medium == 'anime' else 'type: MANGA'
    query = query.replace('MEDIUM_PLACEHOLDER', mq)
    query_vars = {
        'search': search
    }

    series_data = {}

    async with anilist_man.lock():
        async with aiohttp.ClientSession() as session:
            async with session.post(const.ANILIST_API, json={'query': query, 'variables': query_vars}) as r:
                resp = await r.json()

                if r.status != 200:
                    err_msg = resp['errors'][0]['message']
                    return f'hippoerror, Status code {r.status} when fetching {search}: {err_msg}'

                series_list = resp['data']['Page']['media']
                if not series_list:
                    return f'hippoerror, No {medium} found for {search}.'

                series_data = series_list[0]

    mal_url = f'https://myanimelist.net/{medium}/{series_data["idMal"]}'
    img_uhtml = gen_uhtml_img_code(series_data['coverImage']['extraLarge'], dims=(65, 100))
    title = series_data['title']['english']
    if not title:
        title = series_data['title']['romaji']

    parts = 'Episodes' if medium == 'anime' else 'Chapters'
    score = f'{series_data["averageScore"]}%' if series_data['averageScore'] else 'N/A'

    item_info = ItemInfo(title, mal_url, 'mal')

    kwargs = {'img_uhtml': img_uhtml,
              'al_link': f'https://anilist.co/{medium}/{series_data["id"]}',
              'ongoing': series_data['status'],
              'parts': f'{parts}: {series_data[parts.lower()]}',
              'score': score,
              'synopsis': series_data['description']}

    uhtml = item_info.animanga(**kwargs)

    return f'hippo{medium}{series_data["idMal"]}, {uhtml}'

async def anilist_rand_series(medium, anilist_man, genres=[], tags=[]):
    query = '''
    query ($page: Int) {
        Page (page: $page, perPage: 1) {
            pageInfo {
                total
            }
            media (CATEGORIES_PLACEHOLDER minimumTagRank: 50, type: TYPE_PLACEHOLDER, isAdult: false) {
                id
                idMal
                title {
                    english
                    userPreferred
                }
                coverImage {
                    extraLarge
                }
                status
                episodes
                chapters
                description
                averageScore
            }
        }
    }
    '''

    category_params = []
    if genres:
        category_params.append(f'genre_in: {json.dumps(genres)}')
    if tags:
        category_params.append(f'tag_in: {json.dumps(tags)}')

    category_params_str = ','.join(category_params)
    if category_params_str:
        category_params_str += ','

    query = query.replace('CATEGORIES_PLACEHOLDER', category_params_str)
    query = query.replace('TYPE_PLACEHOLDER', medium.upper())
    query_vars = {'page': 1}

    num_entries = 0
    async with aiohttp.ClientSession() as session:
        async with anilist_man.lock():
            num_entries = await anilist_num_entries(query, query_vars, session)

        if not num_entries:
            return 'No series found with the given specifications.'

        query_vars['page'] = random.randint(1, num_entries)

        series_data = {}
        async with anilist_man.lock():
            async with session.post(const.ANILIST_API, json={'query': query, 'variables': query_vars}) as r:
                resp = await r.json()

                if r.status != 200:
                    err_msg = resp['errors'][0]['message']
                    return f'Status code {r.status} when fetching {query}: {err_msg}'

                series_data = resp['data']['Page']['media'][0]

    mal_url = f'https://myanimelist.net/{medium}/{series_data["idMal"]}'
    img_uhtml = gen_uhtml_img_code(series_data['coverImage']['extraLarge'], dims=(65, 100))
    title = series_data['title']['english']
    if not title:
        title = series_data['title']['userPreferred']

    parts = 'Episodes' if medium == 'anime' else 'Chapters'
    score = f'{series_data["averageScore"]}%' if series_data['averageScore'] else 'N/A'

    item_info = ItemInfo(title, mal_url, 'mal')

    kwargs = {'img_uhtml': img_uhtml,
              'al_link': f'https://anilist.co/{medium}/{series_data["id"]}',
              'ongoing': series_data['status'],
              'parts': f'{parts}: {series_data[parts.lower()]}',
              'score': score,
              'synopsis': series_data['description']}

    uhtml = item_info.animanga(**kwargs)

    return f'hippo{medium}{series_data["idMal"]}, {uhtml}'

async def get_related_series(medium, series, anilist_man):
    related_series = [(medium, series)]
    query = '''
    query ($mal_id: Int) {
        Page (page: 1, perPage: 1) {
            media (type: TYPE_PLACEHOLDER, idMal: $mal_id, isAdult: false) {
                title {
                    userPreferred
                }
                relations {
                    edges {
                        relationType
                        node {
                            idMal
                            type
                        }
                    }
                }
            }
        }
    }
    '''
    title = ''

    for s in related_series:
        async with anilist_man.lock():
            async with aiohttp.ClientSession() as session:
                series_query = query.replace('TYPE_PLACEHOLDER', s[0].upper())
                query_vars = {'mal_id': s[1]}
                async with session.post(const.ANILIST_API, json={'query': series_query, 'variables': query_vars}) as r:
                    resp = await r.json()

                    if r.status != 200:
                        continue

                    if s == (medium, series):
                        title = resp['data']['Page']['media'][0]['title']['userPreferred']

                    for rs in resp['data']['Page']['media'][0]['relations']['edges']:
                        rs_info = (rs['node']['type'].lower(), rs['node']['idMal'])
                        if rs_info not in related_series and rs['relationType'] != 'CHARACTER':
                            related_series.append(rs_info)

    return (related_series, title)

async def add_anotd_bl(medium, series, anilist_man, db_man):
    related_series, title = await get_related_series(medium, series, anilist_man)

    expiration = str(datetime.datetime.now() + datetime.timedelta(days=365))

    await db_man.execute("INSERT INTO anotd_banlist (medium, mal_id, name, expiration) "
                            f"VALUES ('{medium}', {series}, '{title}', '{expiration}')")

    for s in related_series:
        exists = await db_man.execute(f"SELECT * FROM mal_banlist WHERE medium='{s[0]}' AND mal_id={s[1]}")

        if not exists:
            await db_man.execute("INSERT INTO mal_banlist (medium, mal_id, anotd_source) "
                                    f"VALUES ('{s[0]}', {s[1]}, 1)")

async def rm_anotd_bl(medium, series, anilist_man, db_man):
    related_series, _ = await get_related_series(medium, series, anilist_man)

    for s in related_series:
        await db_man.execute(f"DELETE FROM mal_banlist WHERE medium='{s[0]}' AND mal_id={s[1]} AND anotd_source=1")

    await db_man.execute(f"DELETE FROM anotd_banlist WHERE medium='{medium}' AND mal_id={series}")


# flake8: noqa
import sys
import time
from tech_news.color import color
from tech_news.scraper import get_tech_news
from tech_news.analyzer.ratings import top_5_categories
from tech_news.analyzer.search_engine import (
    get_all_news,
    search_by_title,
    search_by_date,
    search_by_category,
)

def printnews(data):
    if len(data) == 0:
        print(f"\n*** {color.PURPLE}Aviso{color.END}: Nenhuma notícia encontrada")
    else:
        for new in data:
            print(new)

def analyzer_menu():
    print(
        f"""\n{color.BOLDBLUE}Python scraper by @devsakae{color.END}\n
 {color.GREY}Digite {color.END}1{color.GREY} para atualizar o banco de dados
 Digite {color.END}2{color.GREY} para ler todas as manchetes
 Digite {color.END}3{color.GREY} para pesquisar notícias por TÍTULO
 Digite {color.END}4{color.GREY} para pesquisar notícias por DATA
 Digite {color.END}5{color.GREY} para pesquisar notícias por CATEGORIA\n
Ou digite {color.END}q{color.GREY} para {color.END}{color.RED}finalizar{color.END}{color.GREY} o script.{color.END}\n"""
    )
    answer = input(">> Digite a opção desejada: ")
    if answer == "1":
        value = input("Digite quantas notícias serão buscadas: ")
        get_tech_news(int(value))
    elif answer == "2":
        printnews(get_all_news())
    elif answer == "3":
        printnews(search_by_title(input("Pesquisar por qual string? ")))
    elif answer == "4":
        printnews(search_by_date(input("Digite a data a pesquisar (formato aaaa-mm-dd): ")))
    elif answer == "5":
        print(top_5_categories())
        printnews(search_by_category(input("Escreva qual categoria deseja ler: ")))
    elif answer == "q":
        return print(f"\n{color.BOLDRED}*** Encerrando script ***{color.END}\n")
    else:
        print(f"\n*** {color.BOLDRED}Erro{color.END}: Opção inválida", file=sys.stderr)
    time.sleep(1)
    analyzer_menu()


from autograd import numpy as np

def ionstr_DOE94(Sal):
    """Ionic strength following DOE94."""
    # === CO2SYS.m comments: =======
    # This is from the DOE handbook, Chapter 5, p. 13/22, eq. 7.2.4.
    return 19.924 * Sal / (1000 - 1.005 * Sal)

def borate_C65(Sal):
    """Total borate in mol/kg-sw following C65."""
    # === CO2SYS.m comments: =======
    # This is .00001173*Sali, about 1% lower than Uppstrom's value
    # Culkin, F., in Chemical Oceanography, ed. Riley and Skirrow, 1965:
    # GEOSECS references this, but this value is not explicitly given here
    # Output in mol/kg-SW
    return 0.0004106 * Sal / 35

def borate_U74(Sal):
    """Total borate in mol/kg-sw following U74."""
    # === CO2SYS.m comments: =======
    # Uppstrom, L., Deep-Sea Research 21:161-162, 1974:
    # this is .000416*Sali/35. = .0000119*Sali
    # TB[FF] = (0.000232/10.811)*(Sal[FF]/1.80655); in mol/kg-SW.
    return 0.0004157 * Sal / 35

def borate_LKB10(Sal):
    """Total borate in mol/kg-sw following LKB10."""
    # === CO2SYS.m comments: =======
    # Lee, Kim, Byrne, Millero, Feely, Yong-Ming Liu. 2010.
    # Geochimica Et Cosmochimica Acta 74 (6): 1801-1811.
    # Output in mol/kg-SW.
    return 0.0004326 * Sal / 35

def calcium_C65(Sal):
    """Calcium in mol/kg-sw following C65."""
    # === CO2SYS.m comments: =======
    # *** CalculateCaforGEOSECS:
    # Culkin, F, in Chemical Oceanography, ed. Riley and Skirrow, 1965:
    # (quoted in Takahashi et al, GEOSECS Pacific Expedition v. 3, 1982)
    # Culkin gives Ca = (.0213/40.078)*(Sal/1.80655) in mol/kg-SW
    # which corresponds to Ca = .01030*Sal/35.
    return 0.01026 * Sal / 35

def calcium_RT67(Sal):
    """Calcium in mol/kg-sw following RT67."""
    # === CO2SYS.m comments: =======
    # This is .010285*Sal/35
    return 0.02128 / 40.087 * Sal / 1.80655

def fluoride_R65(Sal):
    """Total fluoride in mol/kg-sw following R65."""
    # === CO2SYS.m comments: =======
    # Riley, J. P., Deep-Sea Research 12:219-220, 1965:
    # this is .000068*Sali/35. = .00000195*Sali; in mol/kg-SW.
    return (0.000067 / 18.998) * Sal / 1.80655

def sulfate_MR66(Sal):
    """Total sulfate in mol/kg-sw following MR66."""
    # === CO2SYS.m comments: =======
    # Morris, A. W., and Riley, J. P., Deep-Sea Research 13:699-705, 1966:
    # this is .02824*Sali/35. = .0008067*Sali; in mol/kg-SW.
    return (0.14 / 96.062) * Sal / 1.80655

def _co2sys_TB(Sal, WhichKs, WhoseTB):
    """Calculate total borate from salinity for the given options."""
    TB = np.where(WhichKs == 8, 0.0, np.nan)  # pure water
    TB = np.where((WhichKs == 6) | (WhichKs == 7), borate_C65(Sal), TB)
    F = (WhichKs != 6) & (WhichKs != 7) & (WhichKs != 8)
    TB = np.where(F & (WhoseTB == 1), borate_U74(Sal), TB)
    TB = np.where(F & (WhoseTB == 2), borate_LKB10(Sal), TB)
    return TB

def _co2sys_TCa(Sal, WhichKs):
    """Calculate total calcium from salinity for the given options."""
    F = (WhichKs == 6) | (WhichKs == 7)  # GEOSECS values
    TCa = np.where(F, calcium_C65(Sal), calcium_RT67(Sal))
    return TCa

def fromSal(Sal, WhichKs, WhoseTB, totals=None):
    """Estimate total molinities of calcium, borate, fluoride and sulfate from salinity.

    Subfunctions based on Constants, version 04.01, 10-13-97, by Ernie Lewis.
    """
    if totals is None:
        totals = {}
    if "TB" not in totals:
        TB = _co2sys_TB(Sal, WhichKs, WhoseTB)
        totals["TB"] = TB
    if "TF" not in totals:
        TF = fluoride_R65(Sal)
        totals["TF"] = TF
    if "TSO4" not in totals:
        TSO4 = sulfate_MR66(Sal)
        totals["TSO4"] = TSO4
    if "TCa" not in totals:
        TCa = _co2sys_TCa(Sal, WhichKs)
        totals["TCa"] = TCa
    if "alpha" not in totals:
        totals["alpha"] = 0.0
    if "beta" not in totals:
        totals["beta"] = 0.0
    return totals

def assemble(Sal, TSi, TPO4, TNH3, TH2S, WhichKs, WhoseTB, totals=None):
    """Estimate total molinities from salinity and assemble along with other salts and
    related variables.
    """
    # Pure Water case:
    Sal = np.where(WhichKs == 8, 0.0, Sal)
    # GEOSECS and Pure Water:
    F = (WhichKs == 6) | (WhichKs == 8)
    TPO4 = np.where(F, 0.0, TPO4)
    TSi = np.where(F, 0.0, TSi)
    TNH3 = np.where(F, 0.0, TNH3)
    TH2S = np.where(F, 0.0, TH2S)
    # Convert micromol to mol
    TPO4 = TPO4 * 1e-6
    TSi = TSi * 1e-6
    TNH3 = TNH3 * 1e-6
    TH2S = TH2S * 1e-6
    totals = fromSal(Sal, WhichKs, WhoseTB, totals=totals)
    # The vector `PengCorrection` is used to modify the value of TA, for those
    # cases where WhichKs==7, since PAlk(Peng) = PAlk(Dickson) + TP.
    # Thus, PengCorrection is 0 for all cases where WhichKs is not 7.
    PengCorrection = np.where(WhichKs == 7, TPO4, 0.0)
    # Add everything else to `totals` dict
    totals["TPO4"] = TPO4
    totals["TSi"] = TSi
    totals["TNH3"] = TNH3
    totals["TH2S"] = TH2S
    totals["Sal"] = Sal  # this is input `Sal` but with pure water case forced to 0.0
    totals["PengCorrection"] = PengCorrection
    return totals


Tabs = 0
OpenCurlyBracket = 0
OpenSquareBrackets = 0
OpenSingleQuotationMarks = False
OpenDoubleQuotationMarks = False

SourcFile = "C:\\Git\\Python\\JsonFormater\\Test.json"
DestinaFile = "C:\\Git\\Python\\JsonFormater\\TestNew.json"

writer = open(DestinaFile, 'w')

def WirteWithTabs(c):
    writer.write(c + "\n")
    for x in range(Tabs):
        writer.write("\t")

def WirteWithTabsBefore(c):
    writer.write("\n")
    for x in range(Tabs):
        writer.write("\t")
    writer.write(c)

with open(SourcFile) as f:
    while True:
        c = f.read(1)
        if not c:
            print("End of file")
            break
        if(c == "["):
            if not(OpenDoubleQuotationMarks | OpenSingleQuotationMarks):
                OpenSquareBrackets = OpenSquareBrackets + 1
                Tabs = Tabs + 1
                WirteWithTabs(c)
                continue

        if (c == "{"):
            if not (OpenDoubleQuotationMarks | OpenSingleQuotationMarks):
                OpenCurlyBracket = OpenCurlyBracket + 1
                Tabs = Tabs + 1
                WirteWithTabs(c)
                continue

        if (c == "]"):
            if not (OpenDoubleQuotationMarks | OpenSingleQuotationMarks):
                OpenSquareBrackets = OpenSquareBrackets - 1
                Tabs = Tabs - 1
                WirteWithTabsBefore(c)
                continue

        if (c == "}"):
            if not (OpenDoubleQuotationMarks | OpenSingleQuotationMarks):
                OpenCurlyBracket = OpenCurlyBracket - 1
                Tabs = Tabs - 1
                WirteWithTabsBefore(c)
                continue

        if (c == '"'):
            if not (OpenSingleQuotationMarks):
                if (OpenDoubleQuotationMarks):
                    OpenDoubleQuotationMarks = False
                else:
                    OpenDoubleQuotationMarks = True

        if (c == "'"):
            if not (OpenDoubleQuotationMarks):
                if (OpenSingleQuotationMarks):
                    OpenSingleQuotationMarks = False
                else:
                    OpenSingleQuotationMarks = True

        if (c == ","):
            if not (OpenDoubleQuotationMarks | OpenSingleQuotationMarks):
                WirteWithTabs(c)
                continue

        else:
            writer.write(c)
        print("Read a character:", c)


class Team:
    def __init__(self,name,div,conf_div_home_1,conf_div_home_2,conf_div_away_1,conf_div_away_2,
           out_conf_home_1,out_conf_home_2,out_conf_away_1,out_conf_away_2,rank_home,rank_away):
           self.name = name
           self.div = div
           self.conf_div_home_1 = conf_div_home_1
           self.conf_div_home_2 = conf_div_home_2
           self.conf_div_away_1 = conf_div_away_1
           self.conf_div_away_2 = conf_div_away_2
           self.out_conf_home_1 = out_conf_home_1
           self.out_conf_home_2 = out_conf_home_2
           self.out_conf_away_1 = out_conf_away_1
           self.out_conf_away_2 = out_conf_away_2
           self.rank_home = rank_home
           self.rank_away = rank_away

    def do_they_play_this_week(self, week, ls):
        if [x for x in ls if x.week == week and x.home == self] or [x for x in ls if x.week == week and x.away == self]:
            return True
        return False

    def div_opponents(self,ls):
        opponents = []
        for team in ls:
            if team.name != self.name and team.div == self.div:
                opponents.append(team)
        return opponents

    def possible_away_opponents(self,week,ls):
        opponents = []
        for game in ls:
            if game.home == self:
                if not game.away.do_they_play_this_week(week,ls) and game.week == 0:
                    opponents.append(game.away)
        return opponents

    def weeks_already_played(self,ls):
        weeks = []
        for game in ls:
            if game.home == self or game.away == self:
                weeks.append(game.week)
        return weeks

    def games_available(self,ls):
        games = []
        for game in ls:
            if game.home == self or game.away == self:
                if game.week == 0:
                    games.append(game)
        return games

    def total_schedule(self,ls):
        sched = []
        for game in ls:
            if game.home == self or game.away == self:
                sched.append(game)
        return sched

class Game:
    def __init__(self,week,home,away):
        self.week = week
        self.home = home
        self.away = away

    def __str__(self):
        return str(self.week) + self.home.name + self.away.name

    def __unicode__(self):
        return str(self.week) + self.home.name + self.away.name

    def __repr__(self):
        return str(self.week) + str(self.home.name)+ str(self.away.name)


from flask import Flask, render_template, request
import datetime
from flask import make_response
import json
import urllib

import feedparser

app =Flask(__name__)

DEFAULTS = {
    "publication" : "bbc",
    "city" : "London, UK",
    "currency_from" : "GBP",
    "currency_to" : "USD"
}

CURRENCY_URL = "https://openexchangerates.org//api/latest.json?app_id=2504504501d94da3aa68e00eec522526"
WEATHER_URL = "http://api.openweathermap.org/data/2.5/weather?q={}&units=metric&appid=49edf448fe3e52ba925bd5549dc6ffc6"

RSS_FEEDS = {
    "bbc": "http://feeds.bbci.co.uk/news/rss.xml",
    'cnn': 'http://rss.cnn.com/rss/edition.rss',
    "time": "http://time.com/feed/",
    "ndtv": "http://feeds.feedburner.com/ndtvnews-latest",
    "fortune" : "http://fortune.com/feed/"

}

@app.route("/")
def home():
    publication = request.args.get("publication")
    if not publication:
        publication = DEFAULTS["publication"]
    articles = get_news(publication)
    city = request.args.get("city")
    if not city:
        city = DEFAULTS["city"]
    weather = get_weather(city)
    currency_from = request.args.get("currency_from")
    if not currency_from:
        currency_from = DEFAULTS["currency_from"]
    currency_to = request.args.get("currency_to")
    if not currency_to:
        currency_to = DEFAULTS["currency_to"]
    rate, currencies = get_rate(currency_from, currency_to)
    return render_template("home.html", articles=articles, weather=weather,currency_from=currency_from, currency_to=currency_to,rate=rate, currencies=sorted(currencies), publication=RSS_FEEDS)

def get_news(query):
    if not query or query.lower() not in RSS_FEEDS:
        publication = DEFAULTS["publication"]
    else:
        publication = query.lower()
    feed = feedparser.parse(RSS_FEEDS[publication])
    return feed["entries"]

def get_weather(query):
    query = urllib.parse.quote(query)
    url = WEATHER_URL.format(query)
    data = urllib.request.urlopen(url).read().decode("utf-8")
    parsed = json.loads(data)
    weather = None
    if parsed.get("weather"):
        weather = {"description":parsed["weather"][0]["description"],"temperature":parsed["main"]["temp"],"city":parsed["name"]
                  }
    return weather

def get_rate(frm, to):
    all_currency = urllib.request.urlopen(CURRENCY_URL).read().decode("utf-8")
    parsed = json.loads(all_currency).get("rates")
    frm_rate = parsed.get(frm.upper())
    to_rate = parsed.get(to.upper())
    return (to_rate / frm_rate, parsed.keys())

if __name__ == "__main__":
    app.run(port=5000, debug=True)


print('**Combustiveis**')
print('A - Alcool (R$1,90/L)')
print('G - Gasolina (R$2,50/L)\n')

combustivel = input('Digite o combustível desejado(A ou G): ')
try:
    litros = float(input('Digite quantos litros você quer abastecer: '))
except:
    print('Valor inválido!')
    quit()
if (0<litros<=20):
    if (combustivel.lower() == 'a'):
        preco = litros*1.9*0.97
        print('Valor total: R$', round(preco, 2))
    elif (combustivel.lower() == 'g'):
        preco = litros*2.5*0.96
        print('Valor total: R$', round(preco, 2))
    else:
        print('Combustível digitado inválido!')
elif (litros>20):
    if (combustivel.lower() == 'a'):
        preco = litros*1.9*0.95
        print('Valor total: R$', round(preco, 2))
    elif(combustivel.lower() == 'g'):
        preco = litros*2.5*0.94
        print('Valor total: R$', round(preco, 2))
    else:
        print('Combustível digitado inválido!')
else:
    print('Não é possível abastecer quantias nulas ou negativas.')


#Display the Info Browser for a search string

import understand
import sys

def sortKeyFunc(ent):
  return str.lower(ent.longname())

def run(db,searchstring):
  list = []
  ents = db.lookup(searchstring,"function,method,procedure")
  for func in sorted(ents,key = sortKeyFunc):
    #If the file is from the Ada Standard library, skip to the next
    if func.library() != "Standard":
      list.append(func)
  for line in list[-1].ib():
    print (line,end="")
  print ("\n",end="")

if __name__ == '__main__':
  # Open Database
  searchstring = ".*Test.*"
  args = sys.argv
  db = understand.open(args[1])
  run(db,searchstring)


from discord.ext import commands
from collections import defaultdict
import random
random.seed()
from discord.ext import commands

class Cog(commands.Cog, name="noplead"):
    def __init__(self ,bot):
        self.bot = bot
        self.times = defaultdict(int)

    @commands.command()
    async def command(self ,ctx):
        self.permload()
        if self.permcheck(ctx ,self.commandid):
            pass

    @commands.Cog.listener()
    async def on_message(self, message):
        content = message.content
        if "ðŸ¥º" in content or ":pleading:" in content or "ðŸ¥¹" in content or ":face_holding_back_tears:" in content:
          await message.channel.send("gtfo pleading user")
          await message.delete()

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


#jiayizha
#Tutorial from GitHub: https://github.com/MiniGirlGeek/pygame-paintme

import pygame
from colorSheet import *
from pygame.locals import *
from dialogueBox import *
from sys import exit
from pygamegame import PygameGame
from dialogueBox import dialogue

class writeScreen(object):
    def __init__(self, width=300, height=200):
        self.width = width
        self.height = height

class drawWindow(PygameGame):
    def __init__(self, x=620, y=410, width=300, height=200):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.charaList = [hito, ki, hana, sora, kawa]
        self.mouse_pos = None
        self.isDrawing = False
        self.step = 0
        self.brushColor = darkMagenta
        self.writing = pygame.Surface((370,200))
        self.writing.fill(white)

    def drawBox(self,screen):
        chara = self.charaList[self.step]
        chara.image = pygame.image.load(chara.path).convert()
        screen.blit(chara.image,(self.x+90,self.y))
        screen.blit(self.writing,(self.x,self.y))

    def fillColor(self):
        self.writing.fill(white)

    def transparent(self):
        self.writing.set_alpha(150) #transparency

    def redrawAll(self,screen):
        self.drawWindow.drawBox(screen)


import click
import poyo
import ruamel.yaml
import runez
import strictyaml
import yaml as pyyaml

from zyaml import load_path, load_string, tokens_from_path, tokens_from_string
from zyaml.marshal import decode, default_marshal, represented_scalar

from . import TestSettings

class ImplementationCollection(object):
    def __init__(self, names, default="zyaml,ruamel"):
        av = [ZyamlImplementation, RuamelImplementation, PyyamlBaseImplementation, PoyoImplementation, StrictImplementation]
        self.available = dict((m.name, m()) for m in av)
        self.unknown = []
        self.selected = []
        if names.startswith("+"):
            names = "%s,%s" % (names[1:], default)

        names = [s.strip() for s in names.split(",")]
        names = [s for s in names if s]
        seen = {}
        for name in names:
            found = 0
            for i in self.available.values():
                if name == "all" or name in i.name:
                    if i.name not in seen:
                        seen[i.name] = True
                        self.selected.append(i)

                    found += 1

            if found == 0:
                self.unknown.append(name)

        self.combinations = None

    def track_result_combination(self, impl, data):
        if isinstance(data, Exception):
            value = runez.stringified(data)

        else:
            value = runez.represented_json(data, stringify=decode, keep_none=True, none_key="-null-")

        name = impl.name
        if self.combinations is None:
            self.combinations = {}
            for i1 in self.selected:
                for i2 in self.selected:
                    if i1.name < i2.name:
                        self.combinations[(i1.name, i2.name)] = set()

        for names, values in self.combinations.items():
            if name in names:
                values.add(value)

    def __repr__(self):
        return ",".join(str(i) for i in self.selected)

    def __len__(self):
        return len(self.selected)

    def __iter__(self):
        for i in self.selected:
            yield i

class Implementation(object):
    """Implementation of loading a yml file"""

    name = None  # type: str

    def __repr__(self):
        return self.name

    @classmethod
    def option(cls, default="zyaml,ruamel", count=None, **kwargs):
        """
        Args:
            default (str | None): Default implementation(s) to use
            count (int | None): Optional: exact number of implementations that have to specified
            **kwargs: Passed-through to click
        """
        kwargs["default"] = default

        def _callback(_ctx, _param, value):
            if not value:
                return None

            impls = ImplementationCollection(value, default=default)
            if impls.unknown:
                raise click.BadParameter("Unknown implementation(s): %s" % ", ".join(impls.unknown))

            if count and len(impls) != count:
                if count == 1:
                    raise click.BadParameter("Need exactly 1 implementation")

                raise click.BadParameter("Need exactly %s" % runez.plural(count, "implementation"))

            if count == 1:
                return impls.selected[0]

            return impls

        metavar = "I1,..."
        hlp = "Implementation(s)"
        if count:
            hlp = runez.plural(count, "implementation")
            metavar = ",".join("I%s" % (i + 1) for i in range(count))

        kwargs.setdefault("help", "%s to use" % hlp)
        kwargs.setdefault("show_default", True)
        kwargs.setdefault("metavar", metavar)
        name = "implementation" if count == 1 else "implementations"
        return click.option(name, "-i", callback=_callback, **kwargs)

    def show_result(self, data, tokens=False):
        rtype = "tokens" if tokens else data.__class__.__name__ if data is not None else "None"
        rep = data
        if not tokens or isinstance(data, Exception):
            rep = TestSettings.represented(data)

        message = "---- %s: %s" % (runez.bold(self.name), runez.dim(rtype))
        if isinstance(data, NotImplementedError):
            print("%s - %s" % (message, rep))
            return

        print(message)
        print(rep)

    def get_outcome(self, content, tokens=False):
        if tokens:
            data = self.tokens(content)
            if isinstance(data, list):
                data = "\n".join(self.represented_token(t) for t in data)

            return data

        return self.deserialized(content)

    def deserialized(self, source):
        value = TestSettings.protected_call(self._deserialized, source)
        return self._simplified(value)

    def tokens(self, source):
        return TestSettings.protected_call(self._tokenize, source)

    def represented_token(self, token):
        return str(token)

    def _deserialized(self, source):
        if hasattr(source, "path"):
            return self._deserialized_from_path(source.path)

        return self._deserialized_from_string(source)

    def _deserialized_from_path(self, path):
        with open(path) as fh:
            return self._deserialized_from_string(fh.read())

    def _deserialized_from_string(self, source):
        raise NotImplementedError()

    def _tokenize(self, source):
        if hasattr(source, "path"):
            return self._tokens_from_path(source.path)

        return self._tokens_from_string(source)

    def _tokens_from_path(self, path):
        with open(path) as fh:
            return TestSettings.unwrapped(self._tokens_from_string(fh.read()))

    def _tokens_from_string(self, source):
        raise NotImplementedError()

    def _simplified(self, value):
        if isinstance(value, list) and len(value) == 1:
            return value[0]

        return value

class ZyamlImplementation(Implementation):
    name = "zyaml"

    def _deserialized_from_path(self, path):
        return load_path(path)

    def _deserialized_from_string(self, source):
        return load_string(source)

    def _tokens_from_path(self, path):
        return tokens_from_path(path)

    def _tokens_from_string(self, source):
        return tokens_from_string(source)

    def _simplified(self, value):
        return value

def ruamel_passthrough_tags(loader, tag, node):
    name = node.__class__.__name__
    if "Seq" in name:
        result = []
        for v in node.value:
            result.append(ruamel_passthrough_tags(loader, tag, v))

        return result

    if "Map" in name:
        result = {}
        for k, v in node.value:
            k = ruamel_passthrough_tags(loader, tag, k)
            v = ruamel_passthrough_tags(loader, tag, v)
            result[k] = v

        return result

    return default_marshal(node.value)

class RuamelImplementation(Implementation):
    name = "ruamel"

    def _deserialized_from_string(self, source):
        y = ruamel.yaml.YAML(typ="safe")
        ruamel.yaml.add_multi_constructor("", ruamel_passthrough_tags, Loader=ruamel.yaml.SafeLoader)
        return y.load_all(source)

    def _tokens_from_string(self, source):
        return ruamel.yaml.main.scan(source)

class PyyamlBaseImplementation(Implementation):
    name = "pyyaml"

    def _deserialized_from_string(self, source):
        return pyyaml.load_all(source, Loader=pyyaml.BaseLoader)

    def _tokens_from_string(self, source):
        yaml_loader = pyyaml.BaseLoader(source)
        curr = yaml_loader.get_token()
        while curr is not None:
            yield curr
            curr = yaml_loader.get_token()

    def represented_token(self, token):
        linenum = token.start_mark.line + 1
        column = token.start_mark.column + 1
        result = "%s[%s,%s]" % (token.__class__.__name__, linenum, column)
        value = getattr(token, "value", None)
        if value is not None:
            if token.id == "<scalar>":
                value = represented_scalar(token.style, value)

            elif token.id == "<anchor>":
                value = "&%s" % value

            elif token.id == "<alias>":
                value = "*%s" % value

            elif token.id == "<tag>":
                assert isinstance(value, tuple)
                value = " ".join(str(s) for s in runez.flattened(value))

            elif token.id == "<directive>":
                result += " %s" % token.name
                value = " ".join(str(s) for s in runez.flattened(value))

            else:
                assert False

            result = "%s %s" % (result, value)

        return result

class PoyoImplementation(Implementation):
    name = "poyo"

    def _deserialized_from_string(self, source):
        return [poyo.parse_string(source)]

class StrictImplementation(Implementation):
    name = "strict"

    def _deserialized_from_string(self, source):
        obj = strictyaml.dirty_load(source, allow_flow_style=True)
        return obj.data


import os

## ==============================================
## GRAPH OPTIONS
## ==============================================

# Font Options
#OPT_FONT_FAMILY = 'serif'
OPT_FONT_NAME = 'Times New Roman'
OPT_FONT_BASE_SIZE = 18

# LEGEND
OPT_LEGEND_FONT_SIZE = OPT_FONT_BASE_SIZE-4
OPT_LEGEND_SHADOW = True

OPT_XLABEL_FONT_SIZE = OPT_FONT_BASE_SIZE+6
OPT_XTICKS_FONT_SIZE = OPT_FONT_BASE_SIZE+2

OPT_YLABEL_FONT_SIZE = OPT_FONT_BASE_SIZE+6
OPT_YTICKS_FONT_SIZE = OPT_FONT_BASE_SIZE+2

# Output Dimensions
OPT_GRAPH_WIDTH = 800
OPT_GRAPH_HEIGHT = 400
OPT_GRAPH_DPI = 200

# GRAPH OPTIONS
OPT_COLORS = ( '#DC3912', '#3366CC', '#5F04B4', '#A1A1A1', '#8D38C9', '##FFFF00')
OPT_PATTERNS = ('//', '\\\\', 'X', '', '+')
OPT_MARKERS = ('s', 'd', 'p', 'o', 'v')

OPT_ERROR_COLOR = "red"
OPT_ERROR_WIDTH = 2.0
OPT_ERROR_CAPSIZE = 5.0

OPT_BENCHMARK_LABEL_XOFFSET = 0.13
OPT_BENCHMARK_LABEL_XSPACER = 0.34
OPT_BENCHMARK_LABEL_YOFFSET = -0.1

# Labels
OPT_BENCHMARK_LABELS = {
    "tm1":      "TATP",
    "tpcc":     "TPC-C",
    "seats":    "SEATS",
    "wiki":     "Wikipedia"
}

# Grid Background
OPT_GRID_LINESTYLE = '-'
OPT_GRID_WHICH = 'major'
OPT_GRID_COLOR = '0.85'

## ==============================================
## LABELS
## ==============================================
OPT_Y_LABEL_THROUGHPUT = 'Transactions per Second'
OPT_Y_LABEL_MEMORY = 'Memory (MB)'
OPT_Y_LABEL_TIME_MS = 'Elapsed Time (ms)'

OPT_X_LABEL_TIME = 'Elapsed Time (seconds)'
OPT_X_LABEL_READ_RATIO = 'Read Ratio'
OPT_X_LABEL_WORKLOAD_SKEW = 'Workload Skew'
OPT_X_LABEL_DATA_SIZE = 'Data Size (data/memory)'
OPT_X_LABEL_TUPLE_SIZE = 'Tuple Size (bytes)'
OPT_X_LABEL_BLOCK_SIZE = 'Eviction Block Size (KB)'
OPT_X_LABEL_BLOCK_SIZE_MB = 'Eviction Block Size (MB)'
OPT_X_LABEL_NUM_INDEXES = 'Number of Indexes'

OPT_LABEL_MYSQL = "MySQL"
OPT_LABEL_HSTORE = "anti-cache\n  (LRU)"
OPT_LABEL_HSTORE_APPROX = "anti-cache\n  (aLRU)"
OPT_LABEL_NO_ANTICACHE = "H-Store"
OPT_LABEL_MEMCACHED = OPT_LABEL_MYSQL + "\n+Memcached"

## ==============================================
## DATA DIRECTORIES
## ==============================================
baseDir = os.path.dirname(__file__)
OPT_DATA_MYSQL = os.path.realpath(os.path.join(baseDir, "../data/mysql"))
OPT_DATA_HSTORE = os.path.realpath(os.path.join(baseDir, "../data/hstore"))
OPT_DATA_HSTORE_ANTICACHE = os.path.realpath(os.path.join(baseDir, "../data/hstore-anticache"))
OPT_DATA_HSTORE_APPROX = os.path.realpath(os.path.join(baseDir, "../data/hstore-approx"))
OPT_DATA_EVICTIONS = os.path.realpath(os.path.join(baseDir, "../data/evictions"))
OPT_DATA_MYSQL_TPCC = os.path.realpath(os.path.join(baseDir, "../data/tpcc/mysql"))
OPT_DATA_MEMCACHED_TPCC = os.path.realpath(os.path.join(baseDir, "../data/tpcc/memcached"))
OPT_DATA_HSTORE_TPCC = os.path.realpath(os.path.join(baseDir, "../data/tpcc/hstore"))
OPT_DATA_HSTORE_TPCC_APPROX = os.path.realpath(os.path.join(baseDir, "../data/tpcc/hstore-approx"))

OPT_DATA_BLOCKSIZE_64 = os.path.realpath(os.path.join(baseDir, "../data/blocksize/64"))
OPT_DATA_BLOCKSIZE_128 = os.path.realpath(os.path.join(baseDir, "../data/blocksize/128"))
OPT_DATA_BLOCKSIZE_256 = os.path.realpath(os.path.join(baseDir, "../data/blocksize/256"))
OPT_DATA_BLOCKSIZE_512 = os.path.realpath(os.path.join(baseDir, "../data/blocksize/512"))
OPT_DATA_BLOCKSIZE_1024 = os.path.realpath(os.path.join(baseDir, "../data/blocksize/1024"))
OPT_DATA_BLOCKSIZE_2048 = os.path.realpath(os.path.join(baseDir, "../data/blocksize/2048"))
OPT_DATA_BLOCKSIZE_4096 = os.path.realpath(os.path.join(baseDir, "../data/blocksize/4096"))

OPT_DATA_INDEX_TREE = os.path.realpath(os.path.join(baseDir, "../data/index/tree/results.csv"))
OPT_DATA_INDEX_HASH = os.path.realpath(os.path.join(baseDir, "../data/index/hash/results.csv"))

OPT_DATA_TUPLESIZE_128 = os.path.realpath(os.path.join(baseDir, "../data/tuplesize/128"))
OPT_DATA_TUPLESIZE_256 = os.path.realpath(os.path.join(baseDir, "../data/tuplesize/256"))
OPT_DATA_TUPLESIZE_512 = os.path.realpath(os.path.join(baseDir, "../data/tuplesize/512"))
OPT_DATA_TUPLESIZE_1024 = os.path.realpath(os.path.join(baseDir, "../data/tuplesize/1024"))

OPT_DATA_MERGE_BLOCK = os.path.realpath(os.path.join(baseDir, "../data/merge/block"))
OPT_DATA_MERGE_TUPLE = os.path.realpath(os.path.join(baseDir, "../data/merge/tuple"))

OPT_DATA_LRU_NONE = os.path.realpath(os.path.join(baseDir, "../data/lru/none/results.csv"))
OPT_DATA_LRU_SINGLE = os.path.realpath(os.path.join(baseDir, "../data/lru/single/results.csv"))
OPT_DATA_LRU_DOUBLE = os.path.realpath(os.path.join(baseDir, "../data/lru/double/results.csv"))

OPT_DATA_EVICT_CONSTRUCT = os.path.realpath(os.path.join(baseDir, "../data/evict/construct/results.csv"))
OPT_DATA_EVICT_WRITE = os.path.realpath(os.path.join(baseDir, "../data/evict/write/results.csv"))
OPT_DATA_EVICT_FETCH = os.path.realpath(os.path.join(baseDir, "../data/evict/fetch/results.csv"))
OPT_DATA_EVICT_BLOCK_MERGE = os.path.realpath(os.path.join(baseDir, "../data/evict/block-merge/results.csv"))
OPT_DATA_EVICT_TUPLE_MERGE = os.path.realpath(os.path.join(baseDir, "../data/evict/tuple-merge/results.csv"))


from __future__ import annotations

from typing import (
    TYPE_CHECKING,
    Callable,
)

import numpy as np

from pandas._typing import Scalar
from pandas.compat._optional import import_optional_dependency

from pandas.core.util.numba_ import (
    NUMBA_FUNC_CACHE,
    get_jit_arguments,
)

def generate_shared_aggregator(
    func: Callable[..., Scalar],
    engine_kwargs: dict[str, bool] | None,
    cache_key_str: str,
):
    """
    Generate a Numba function that loops over the columns 2D object and applies
    a 1D numba kernel over each column.

    Parameters
    ----------
    func : function
        aggregation function to be applied to each column
    engine_kwargs : dict
        dictionary of arguments to be passed into numba.jit
    cache_key_str: str
        string to access the compiled function of the form
        <caller_type>_<aggregation_type> e.g. rolling_mean, groupby_mean

    Returns
    -------
    Numba function
    """
    nopython, nogil, parallel = get_jit_arguments(engine_kwargs, None)

    cache_key = (func, cache_key_str)
    if cache_key in NUMBA_FUNC_CACHE:
        return NUMBA_FUNC_CACHE[cache_key]

    if TYPE_CHECKING:
        import numba
    else:
        numba = import_optional_dependency("numba")

    @numba.jit(nopython=nopython, nogil=nogil, parallel=parallel)
    def column_looper(
        values: np.ndarray,
        start: np.ndarray,
        end: np.ndarray,
        min_periods: int,
        *args,
    ):
        result = np.empty((len(start), values.shape[1]), dtype=np.float64)
        for i in numba.prange(values.shape[1]):
            result[:, i] = func(values[:, i], start, end, min_periods, *args)
        return result

    return column_looper


menor = 99999
maior = -1
totCestas = jog = 0
nome = input()
if nome != 'sair':
    while nome != 'sair':
        cestas = int(input())
        totCestas += cestas
        jog += 1
        if cestas <= menor:
            menor = cestas
            menorJog = nome
        if cestas >= maior:
            maior = cestas
            maiorJog = nome
        nome = input()
    print(menorJog)
    print(maiorJog)
    print('{:.2f}'.format(totCestas/jog))
else:
    print('Nenhum jogador foi registrado.')


from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from django.contrib import messages

from users.models import Influencer
from campaign.models import Contract
from invitation.models import Invitation
from message.models import Message
# Create your views here.
@login_required(login_url='/login')
def dashboard(request):
    if request.user.is_influencer:
        influencer_profiles = Influencer.objects.filter(user=request.user)
        if influencer_profiles.count() == 0:
            messages.warning(request, 'You must fill out the profile form to see home page.')
            return redirect('influencer_profile')
        else:
            if not influencer_profiles.first().instagram_account and not influencer_profiles.first().facebook_account and not influencer_profiles.first().twitter_account:
                return redirect('influencer_profile')

    context = {
        'menu': 'home'
    }

    if request.user.is_influencer:
        context.update(get_num_notification(request))
        return render(request, 'home/home_influencer.html', context)
    elif request.user.is_client:
        context.update(get_num_notification(request))
        return render(request, 'home/home_client.html', context)
    elif request.user.is_staff:
        context.update(get_num_notification(request))
        return render(request, 'home/home_agent.html', context)

def get_num_notification(request):
    context = dict()
    if not request.user.is_client:
        context = {
            'numOfInfluencerMessage': Message.objects.filter(sent_to=request.user,read_status=False,campaign__isnull=True).count()
        }
    if not request.user.is_influencer:
        context.update({'numOfClientMessage': Message.objects.filter(sent_to=request.user,read_status=False,discussion__isnull=True).count()})
    return context


# -*- coding = utf-8 -*-
# @time:2020/9/22 22:55
# Author:TC
# @File:xgb_test.py
# @Software:PyCharm

import numpy as np
import pandas as pd
import xgboost as xgb
import matplotlib.pyplot as plt
plt.style.use('ggplot')

from sklearn.datasets import load_iris
import xgboost as xgb
from xgboost import plot_importance
from matplotlib import pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

iris=load_iris()
X,y=iris.data,iris.target
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,random_state=123456)

# # 定义模型训练参数
# params = {
#     "objective": "binary:logistic",
#     "booster": "gbtree",
#     "max_depth": 3
#          }
# # 训练轮数
# num_round = 5
#
# # 训练过程中实时输出评估结果
# watchlist = [(xgb_train, 'train'), (xgb_test, 'test')]
#
# # 模型训练
# model = xgb.train(params, xgb_train, num_round, watchlist)

# 算法参数
params = {
    'booster': 'gbtree',
    'objective': 'multi:softmax', #多分类问题
    'num_class': 3,               #类别数，与 multisoftmax 并用
    'gamma': 0.1,                 #用于控制是否后剪枝的参数,越大越保守，一般0.1、0.2这样子。
    'max_depth': 6,               #构建树的深度，越大越容易过拟合
    'lambda': 2,                  #控制模型复杂度的权重值的L2正则化项参数，参数越大，模型越不容易过拟合。
    'subsample': 0.7,             #随机采样训练样本
    'colsample_bytree': 0.75,     #生成树时进行的列采样
    'min_child_weight': 3,        #min_child_weight就是叶子上的最小样本数
    'silent': 1,                  #设置成1则没有运行信息输出，最好是设置为0.
    'eta': 0.1,                   #如同学习率
    'seed': 1,
    'nthread': 4,                 #cpu 线程数
}

plst=list(params.items())

#先保存到XGBoost二进制文件中将使加载速度更快，然后再加载进来
dtrain=xgb.DMatrix(X_train,y_train)
num_rounds=500
model=xgb.train(plst,dtrain,num_rounds)

dtest=xgb.DMatrix(X_test)
y_pred=model.predict(dtest)

accuracy=accuracy_score(y_test,y_pred)
print('accuracy:%.2f%%'%(accuracy*100.0))

#显示重要特征
plot_importance(model)
plt.show()


from django.urls import path
from MainApp import views

urlpatterns = [
    path('', views.index,),
    path('about', views.about),
    path('item/<int:id>', views.get_item),
    path('items', views.items_list),
    path('countries', views.countrie_list),
    path('Australia', views.Australia),
    path('Austria', views.Austria),
    path('Bahamas', views.Bahamas),
]


import copy
import torch.nn as nn
from torch import Tensor
import torch._C as _C

class BoundedTensor(Tensor):
    @staticmethod
    # We need to override the __new__ method since Tensor is a C class
    def __new__(cls, x, ptb, *args, **kwargs):
        if isinstance(x, Tensor):
            tensor = super().__new__(cls, [], *args, **kwargs)
            tensor.data = x.data
            tensor.requires_grad = x.requires_grad
            return tensor
        else:
            return super().__new__(cls, x, *args, **kwargs)

    def __init__(self, x, ptb):
        self.ptb = ptb

    def __repr__(self):
        if hasattr(self, 'ptb') and self.ptb is not None:
            return '<BoundedTensor: {}, {}>'.format(super().__repr__(), self.ptb.__repr__())
        else:
            return '<BoundedTensor: {}, no ptb>'.format(super().__repr__())

    def clone(self, *args, **kwargs):
        tensor = BoundedTensor(super().clone(*args, **kwargs), copy.deepcopy(self.ptb))
        return tensor

    def _func(self, func, *args, **kwargs):
        temp = func(*args, **kwargs)
        new_obj = BoundedTensor([], self.ptb)
        new_obj.data = temp.data
        new_obj.requires_grad = temp.requires_grad
        return new_obj

    # Copy to other devices with perturbation
    def to(self, *args, **kwargs):
        # FIXME add a general "to" function in perturbation class, not here.
        if hasattr(self.ptb, 'x_L') and isinstance(self.ptb.x_L, Tensor):
            self.ptb.x_L = self.ptb.x_L.to(*args, **kwargs)
        if hasattr(self.ptb, 'x_U') and isinstance(self.ptb.x_U, Tensor):
            self.ptb.x_U = self.ptb.x_U.to(*args, **kwargs)
        if hasattr(self.ptb, 'eps') and isinstance(self.ptb.eps, Tensor):
            self.ptb.eps = self.ptb.eps.to(*args, **kwargs)
        return self._func(super().to, *args, **kwargs)

    @classmethod
    def _convert(cls, ret):
        if cls is Tensor:
            return ret

        if isinstance(ret, Tensor):
            if True:
                # The current implementation does not seem to need non-leaf BoundedTensor
                return ret
            else:
                # Enable this branch if non-leaf BoundedTensor should be kept
                ret = ret.as_subclass(cls)

        if isinstance(ret, tuple):
            ret = tuple(cls._convert(r) for r in ret)

        return ret

    @classmethod
    def __torch_function__(cls, func, types, args=(), kwargs=None):
        if kwargs is None:
            kwargs = {}

        if not all(issubclass(cls, t) for t in types):
            return NotImplemented

        with _C.DisableTorchFunction():
            ret = func(*args, **kwargs)
            return cls._convert(ret)

class BoundedParameter(nn.Parameter):
    def __new__(cls, data, ptb, requires_grad=True):
        return BoundedTensor._make_subclass(cls, data, requires_grad)

    def __init__(self, data, ptb, requires_grad=True):
        self.ptb = ptb
        self.requires_grad = requires_grad

    def __deepcopy__(self, memo):
        if id(self) in memo:
            return memo[id(self)]
        else:
            result = type(self)(self.data.clone(), self.ptb, self.requires_grad)
            memo[id(self)] = result
            return result

    def __repr__(self):
        return 'BoundedParameter containing:\n{}\n{}'.format(
            self.data.__repr__(), self.ptb.__repr__())

    def __reduce_ex__(self, proto):
        raise NotImplementedError


import numpy as np
import torch
import torchvision.transforms as transforms
from torch.utils.data import Dataset

class RTNetDataset(Dataset):
    def __init__(self, face_data, mask_data, target):
        self.transform = transforms.Compose([transforms.ToTensor()])
        self.face = face_data
        self.mask = mask_data
        self.label = target.reshape(-1, 1)

    def __getitem__(self, index):
        if torch.is_tensor(index):
            index = index.tolist()

        appearance_data = torch.tensor(np.transpose(self.face[index], (2, 0, 1)), dtype=torch.float32)
        motion_data = torch.tensor(np.transpose(self.mask[index], (2, 0, 1)), dtype=torch.float32)
        target = torch.tensor(self.label[index], dtype=torch.float32)

        inputs = torch.stack([appearance_data,motion_data],dim=0)

        if torch.cuda.is_available():
            inputs = inputs.to('cuda')
            target = target.to('cuda')

        return inputs, target

    def __len__(self):
        return len(self.label)


from dal import autocomplete
from django.db.models import F
from django.http import JsonResponse
from django.views.decorators.http import require_GET

from dal_select2_queryset_sequence.views import Select2QuerySetSequenceView
from queryset_sequence import QuerySetSequence

from analysis.models import Symptom
from chemical.models import Chemical, HealthEffect
from experiment.models import Identification
from genome.models import Gene, DiseaseTrait, Snp

class SymptomAutocomplete(autocomplete.Select2QuerySetView):
    def get_queryset(self):
        if not self.request.user.is_authenticated():
            return Symptom.objects.none()

        qs = Symptom.objects.all()

        if self.q:
            qs = qs.filter(name__istartswith=self.q)

        return qs

class SymptomsForeignAutocompleteView(Select2QuerySetSequenceView):
    paginate_by = 60

    def get_queryset(self):
        genes = Gene.objects.all()
        chemicals = Chemical.objects.exclude(categories__id=51).all()
        diseases = DiseaseTrait.objects.all()
        snps = Snp.objects.all()
        healtheffects = HealthEffect.objects.all()
        experiments = Identification.objects.all()

        if self.q:
            genes = genes.filter(name__icontains=self.q)
            chemicals = chemicals.filter(name__icontains=self.q)
            diseases = diseases.filter(name__icontains=self.q)
            snps = snps.filter(rsid__icontains=self.q)
            healtheffects = healtheffects.filter(name__icontains=self.q)
            experiments = experiments.filter(name__icontains=self.q)

        qs = QuerySetSequence(genes, chemicals, diseases, snps, healtheffects, experiments)
        qs = self.mixup_querysets(qs)
        return qs

@require_GET
def symptoms_list_api(request):
    if request.GET.get('data[q]', ''):
        query = request.GET.get('data[q]')
        symptoms = Symptom.objects.filter(name__icontains=query).annotate(text=F('name')).values('id', 'text')
    else:
        symptoms = []
        query = ''
    result = {
        'q': query,
        'results': [{'id': item['id'], 'text': item['text'].title()} for item in list(symptoms)]
    }
    return JsonResponse(result, safe=False)


import fileinput

# Part 1

commands = list(map(lambda x: ((x.rstrip().split(' ')[0], int(x.rstrip().split(' ')[1]))), fileinput.input()))

x_position = sum(command[1] for command in commands if command[0] == 'forward')

y_position = sum(-command[1] if command[0] == 'up' else command[1] for command in commands if command[0] != 'forward')

print(x_position * y_position)

# Part 2

aim = 0
x_position = 0
y_position = 0

for command, value in commands:
    if command == 'forward':
        x_position += value
        y_position += value * aim
    elif command == 'up':
        aim -= value
    else:
        aim += value

print(x_position * y_position)