import sys
import os
import time
import argparse
import param
import logging
import numpy as np
from threading import Thread
from math import log

logging.basicConfig(format='%(message)s', level=logging.INFO)
num2base = dict(zip((0, 1, 2, 3), "ACGT"))
base2num = dict(zip("ACGT", (0, 1, 2, 3)))
v1Type2Name = dict(zip((0, 1, 2, 3, 4), ('HET', 'HOM', 'INS', 'DEL', 'REF')))
v2Zygosity2Name = dict(zip((0, 1), ('HET', 'HOM')))
v2Type2Name = dict(zip((0, 1, 2, 3), ('REF', 'SNP', 'INS', 'DEL')))
v2Length2Name = dict(zip((0, 1, 2, 3, 4, 5), ('0', '1', '2', '3', '4', '4+')))
maxVarLength = 5
inferIndelLengthMinimumAF = 0.125

def Run(args):
    # create a Clairvoyante
    logging.info("Loading model ...")
    if args.v2 == True:
        import utils_v2 as utils
        utils.SetupEnv()
        if args.slim == True:
            import clairvoyante_v2_slim as cv
        else:
            import clairvoyante_v2 as cv
    elif args.v3 == True:
        import utils_v2 as utils # v3 network is using v2 utils
        utils.SetupEnv()
        if args.slim == True:
            import clairvoyante_v3_slim as cv
        else:
            import clairvoyante_v3 as cv
    if args.threads == None:
        if args.tensor_fn == "PIPE":
            param.NUM_THREADS = 4
    else:
        param.NUM_THREADS = args.threads
    m = cv.Clairvoyante()
    m.init()

    m.restoreParameters(os.path.abspath(args.chkpnt_fn))
    Test(args, m, utils)

def Output(args, call_fh, num, XBatch, posBatch, base, z, t, l):
    if args.v2 == True or args.v3 == True:
        if num != len(base):
          sys.exit("Inconsistent shape between input tensor and output predictions %d/%d" % (num, len(base)))
        #          --------------  ------  ------------    ------------------
        #          Base chng       Zygo.   Var type        Var length
        #          A   C   G   T   HET HOM REF SNP INS DEL 0   1   2   3   4   >=4
        #          0   1   2   3   4   5   6   7   8   9   10  11  12  13  14  15
        for j in range(len(base)):
            if args.showRef == False and np.argmax(t[j]) == 0: continue
            # Get variant type, 0:REF, 1:SNP, 2:INS, 3:DEL
            varType = np.argmax(t[j])
            # Get zygosity, 0:HET, 1:HOM
            varZygosity = np.argmax(z[j])
            # Get Indel Length, 0:0, 1:1, 2:2, 3:3, 4:4, 5:>4
            varLength = np.argmax(l[j])
            # Get chromosome, coordination and reference bases with flanking param.flankingBaseNum flanking bases at coordination
            chromosome, coordination, refSeq = posBatch[j].split(":")
            # Get genotype quality
            sortVarType = np.sort(t[j])[::-1]
            sortZygosity = np.sort(z[j])[::-1]
            sortLength = np.sort(l[j])[::-1]
            qual = int(-4.343 * log((sortVarType[1]*sortZygosity[1]*sortLength[1]  + 1e-300) / (sortVarType[0]*sortZygosity[0]*sortLength[0]  + 1e-300)))
            #if qual > 999: qual = 999
            filt = "."
            if args.qual != None:
                if qual >= args.qual:
                    filt = "PASS"
                else:
                    filt = "LowQual"
            # Get possible alternative bases
            sortBase = base[j].argsort()[::-1]
            base1 = num2base[sortBase[0]]
            base2 = num2base[sortBase[1]]
            # Initialize other variables
            refBase = ""; altBase = ""; inferredIndelLength = 0; dp = 0; af = 0.; info = [];
            dp = sum(XBatch[j,param.flankingBaseNum,:,0]) + sum(XBatch[j,param.flankingBaseNum+1,:,1]) + \
                 sum(XBatch[j,param.flankingBaseNum+1,:,2]) + sum(XBatch[j,param.flankingBaseNum,:,3])
            if dp != 0:
                # For SNP
                if varType == 1 or varType == 0: # SNP or REF
                    coordination = int(coordination)
                    refBase = refSeq[param.flankingBaseNum]
                    if varType == 1: # SNP
                        altBase = base1 if base1 != refBase else base2
                        #altBase = "%s,%s" % (base1, base2)
                    elif varType == 0: # REF
                        altBase = refBase
                    af = XBatch[j,param.flankingBaseNum,base2num[altBase],3] / dp
                elif varType == 2: # INS
                    # infer the insertion length
                    if varLength == 0: varLength = 1
                    af = sum(XBatch[j,param.flankingBaseNum+1,:,1]) / dp
                    if varLength != maxVarLength:
                        for k in range(param.flankingBaseNum+1, param.flankingBaseNum+varLength+1):
                            altBase += num2base[np.argmax(XBatch[j,k,:,1])]
                    else:
                        for k in range(param.flankingBaseNum+1, 2*param.flankingBaseNum+1):
                            referenceTensor = XBatch[j,k,:,0]; insertionTensor = XBatch[j,k,:,1]
                            if k < (param.flankingBaseNum + maxVarLength) or sum(insertionTensor) >= (inferIndelLengthMinimumAF * sum(referenceTensor)):
                                inferredIndelLength += 1
                                altBase += num2base[np.argmax(insertionTensor)]
                            else:
                                break
                    coordination = int(coordination)
                    refBase = refSeq[param.flankingBaseNum]
                    # insertions longer than (param.flankingBaseNum-1) are marked SV
                    if inferredIndelLength >= param.flankingBaseNum:
                        altBase = "<INS>"
                        info.append("SVTYPE=INS")
                    else:
                        altBase = refBase + altBase
                elif varType == 3: # DEL
                    if varLength == 0: varLength = 1
                    af = sum(XBatch[j,param.flankingBaseNum+1,:,2]) / dp
                    # infer the deletion length
                    if varLength == maxVarLength:
                        for k in range(param.flankingBaseNum+1, 2*param.flankingBaseNum+1):
                            if k < (param.flankingBaseNum + maxVarLength) or sum(XBatch[j,k,:,2]) >= (inferIndelLengthMinimumAF * sum(XBatch[j,k,:,0])):
                                inferredIndelLength += 1
                            else:
                               break
                    # deletions longer than (param.flankingBaseNum-1) are marked SV
                    coordination = int(coordination)
                    if inferredIndelLength >= param.flankingBaseNum:
                        refBase = refSeq[param.flankingBaseNum]
                        altBase = "<DEL>"
                        info.append("SVTYPE=DEL")
                    elif varLength != maxVarLength:
                        refBase = refSeq[param.flankingBaseNum:param.flankingBaseNum+varLength+1]
                        altBase = refSeq[param.flankingBaseNum]
                    else:
                        refBase = refSeq[param.flankingBaseNum:param.flankingBaseNum+inferredIndelLength+1]
                        altBase = refSeq[param.flankingBaseNum]
                if inferredIndelLength > 0 and inferredIndelLength < param.flankingBaseNum: info.append("LENGUESS=%d" % inferredIndelLength)
                infoStr = ""
                if len(info) == 0: infoStr = "."
                else: infoStr = ";".join(info)
                gtStr = ""
                if varType == 0: gtStr = "0/0"
                elif varZygosity == 0: gtStr = "0/1"
                elif varZygosity == 1: gtStr = "1/1"

                print >> call_fh, "%s\t%d\t.\t%s\t%s\t%d\t%s\t%s\tGT:GQ:DP:AF\t%s:%d:%d:%.4f" % (chromosome, coordination, refBase, altBase, qual, filt, infoStr, gtStr, qual, dp, af)

def PrintVCFHeader(args, call_fh):
    print >> call_fh, '##fileformat=VCFv4.1'
    print >> call_fh, '##FILTER=<ID=PASS,Description="All filters passed">'
    print >> call_fh, '##FILTER=<ID=LowQual,Description="Confidence in this variant being real is below calling threshold.">'
    print >> call_fh, '##ALT=<ID=DEL,Description="Deletion">'
    print >> call_fh, '##ALT=<ID=INS,Description="Insertion of novel sequence">'
    print >> call_fh, '##INFO=<ID=SVTYPE,Number=1,Type=String,Description="Type of structural variant">'
    print >> call_fh, '##INFO=<ID=LENGUESS,Number=.,Type=Integer,Description="Best guess of the indel length">'
    print >> call_fh, '##FORMAT=<ID=GT,Number=1,Type=String,Description="Genotype">'
    print >> call_fh, '##FORMAT=<ID=GQ,Number=1,Type=Integer,Description="Genotype Quality">'
    print >> call_fh, '##FORMAT=<ID=DP,Number=1,Type=Integer,Description="Read Depth">'
    print >> call_fh, '##FORMAT=<ID=AF,Number=1,Type=Float,Description="Estimated allele frequency in the range (0,1)">'

    if args.ref_fn != None:
      fai_fn = args.ref_fn + ".fai"
      fai_fp = open(fai_fn)
      for line in fai_fp:
          fields = line.strip().split("\t")
          chromName = fields[0]
          chromLength = int(fields[1])
          print >> call_fh, "##contig=<ID=%s,length=%d>" % (chromName, chromLength)

    print >> call_fh, '#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT\t%s' % (args.sampleName)

def Test(args, m, utils):
    call_fh = open(args.call_fn, "w")
    if args.v2 == True or args.v3 == True:
        PrintVCFHeader(args, call_fh)
    tensorGenerator = utils.GetTensor( args.tensor_fn, param.predictBatchSize )
    logging.info("Calling variants ...")
    predictStart = time.time()
    end = 0; end2 = 0; terminate = 0
    end2, num2, XBatch2, posBatch2 = next(tensorGenerator)
    m.predictNoRT(XBatch2)
    base = m.predictBaseRTVal; z = m.predictZygosityRTVal; t = m.predictVarTypeRTVal; l = m.predictIndelLengthRTVal
    if end2 == 0:
        end = end2; num = num2; XBatch = XBatch2; posBatch = posBatch2
        end2, num2, XBatch2, posBatch2 = next(tensorGenerator)
        while True:
            if end == 1:
                terminate = 1
            threadPool = []
            if end == 0:
                threadPool.append(Thread(target=m.predictNoRT, args=(XBatch2, )))
            threadPool.append(Thread(target=Output, args=(args, call_fh, num, XBatch, posBatch, base, z, t, l, )))
            for t in threadPool: t.start()
            if end2 == 0:
                end3, num3, XBatch3, posBatch3 = next(tensorGenerator)
            for t in threadPool: t.join()
            base = m.predictBaseRTVal; z = m.predictZygosityRTVal; t = m.predictVarTypeRTVal; l = m.predictIndelLengthRTVal
            if end == 0:
                end = end2; num = num2; XBatch = XBatch2; posBatch = posBatch2
            if end2 == 0:
                end2 = end3; num2 = num3; XBatch2 = XBatch3; posBatch2 = posBatch3
            #print >> sys.stderr, end, end2, end3, terminate
            if terminate == 1:
                break
    elif end2 == 1:
        Output(args, call_fh, num2, XBatch2, posBatch2, base, z, t, l)

    logging.info("Total time elapsed: %.2f s" % (time.time() - predictStart))

def main():
    parser = argparse.ArgumentParser(
            description="Call variants using a trained Clairvoyante model and tensors of candididate variants" )

    parser.add_argument('--tensor_fn', type=str, default = "PIPE",
            help="Tensor input, use PIPE for standard input")

    parser.add_argument('--chkpnt_fn', type=str, default = None,
            help="Input a checkpoint for testing or continue training")

    parser.add_argument('--call_fn', type=str, default = None,
            help="Output variant predictions")

    parser.add_argument('--qual', type=int, default = None,
            help="If set, variant with equal or higher quality will be marked PASS, or LowQual otherwise, optional")

    parser.add_argument('--sampleName', type=str, default = "SAMPLE",
            help="Define the sample name to be shown in the VCF file")

    parser.add_argument('--showRef', type=param.str2bool, nargs='?', const=True, default = False,
            help="Show reference calls, optional")

    parser.add_argument('--ref_fn', type=str, default=None,
                    help="Reference fasta file input, optional, print contig tags in the VCF header if set")

    parser.add_argument('--threads', type=int, default = None,
            help="Number of threads, optional")

    parser.add_argument('--v3', type=param.str2bool, nargs='?', const=True, default = True,
            help="Use Clairvoyante version 3")

    parser.add_argument('--v2', type=param.str2bool, nargs='?', const=True, default = False,
            help="Use Clairvoyante version 2")

    parser.add_argument('--slim', type=param.str2bool, nargs='?', const=True, default = False,
            help="Train using the slim version of Clairvoyante, optional")

    args = parser.parse_args()

    if len(sys.argv[1:]) == 0:
        parser.print_help()
        sys.exit(1)

    Run(args)

if __name__ == "__main__":
    main()


# -*- coding: utf-8 -*-
"""
A terminal based ray-casting engine.

'esc' to exit
't' to turn off textures
'wasdqe' or arrow-keys to move
'space' to jump

Depending on your terminal font, Renderer.ascii_map may need to be adjusted.
If you'd like to make an ascii map more suitable to your terminal's font,
check my Snippets repository for a script that grabs mean brightness of
unicode characters.

Values stored in textures should range from 0-9.  Values below 6 are
subtractive and above 6 are additive.
"""
from collections import defaultdict
import json
import numpy as np
import curses
from pynput import keyboard
from pynput.keyboard import Key, KeyCode

class Map:
    """
    A helper class for easy loading of maps.

    Each sprite is a dict with keys "pos","image","relative" for position,
    sprite image number, and relative position to player (which will be set
    after first call to cast_sprites in the renderer).
    """
    def __init__(self, file_name):
        self.load(file_name)

    def load(self, file_name):
        with open(file_name + ".json", 'r') as file:
            map_dict = json.load(file)
            self.__map = np.array(map_dict["map"]).T
            self.sprites = map_dict["sprites"]
        for sprite in self.sprites: #lists --> numpy arrays
            sprite["pos"] = np.array(sprite["pos"])

    def __getitem__(self, key):
        return self.__map[key]

class Player:
    """
    Player class with methods for moving and updating any effects on the
    player such as falling.
    """
    def __init__(self, game_map, pos=np.array([5., 5.]),
                 initial_angle=0):
        #Settings======================================================
        self.speed = .1
        self.rotate_speed = .05
        self.jump_time = 8
        self.field_of_view = .6 #Somewhere between 0 and 1 is reasonable

        self.game_map = game_map
        self.pos = pos
        self.cam = np.array([[1, 0], [0, self.field_of_view]]) @\
                   np.array([[np.cos(initial_angle), np.sin(initial_angle)],
                             [-np.sin(initial_angle), np.cos(initial_angle)]])
        self.left = np.array([[np.cos(-self.rotate_speed),
                               np.sin(-self.rotate_speed)],
                              [-np.sin(-self.rotate_speed),
                                np.cos(-self.rotate_speed)]])
        self.right = np.array([[np.cos(self.rotate_speed),
                                np.sin(self.rotate_speed)],
                               [-np.sin(self.rotate_speed),
                                np.cos(self.rotate_speed)]])
        self.perp = np.array([[0., -1.],
                              [1., 0.]])
        self.time_in_jump = 0
        self.z = 0.
        self.is_jumping = False

    def update(self):
        #We'll have more to do here eventually.
        self.fall()

    def fall(self):
        if not self.is_jumping:
            return
        if self.time_in_jump >= 2 * self.jump_time:
            self.is_jumping, self.time_in_jump, self.z = False, 0, 0.
            return
        self.z +=\
         (self.jump_time - self.time_in_jump)**2 / (10 * self.jump_time**2)\
          * (1 if self.time_in_jump < self.jump_time else -1)
        self.time_in_jump += 1

    def turn(self, left=True):
        self.cam = self.cam @ (self.left if left else self.right)

    def move(self, speed, strafe=False):
        next_step = self.pos + speed * \
                    (self.cam[0] @ self.perp if strafe else self.cam[0])

        #If we can move both coordinates at once, we should
        if not self.game_map[tuple(next_step.astype(int))]:
            self.pos = next_step

        #Allows 'sliding' on walls
        elif not self.game_map[int(next_step[0])][int(self.pos[1])]:
            self.pos[0] = next_step[0]
        elif not self.game_map[int(self.pos[0])][int(next_step[1])]:
            self.pos[1] = next_step[1]

class Renderer:
    """
    The Renderer class is responsible for everything drawn on the screen --
    including the environment, sprites, menus, items. All textures stored here.
    """
    def __init__(self, screen, player, game_map, *textures):
        #Settings======================================================
        self.max_hops = 60 #How far rays are cast.

        self.screen = screen
        self.height, self.width = self.screen.getmaxyx()
        self.floor_y = self.height // 2
        self.distances = [0] * self.width
        self.player = player
        self.game_map = game_map
        self.load_textures(*textures)
        self.textures_on = True

        #So we have fewer arrays to initialize inside loops============
        self.hght_inv = np.array([0, 1 / self.height])
        self.const = np.array([1, -1])

        #Shading Constants--It's safe to modify ascii_map==============
        self.ascii_map = dict(enumerate(' .,:;<+*LtCa4U80dQM@'))
        self.shades = len(self.ascii_map) - 1
        self.side_shade = (self.shades + 1) // 5
        self.shade_dif = self.shades - self.side_shade

    def load_textures(self, *texture_names):
        self.textures = []
        for name in texture_names:
            with open(name + ".json", 'r') as texture:
                pre_load = json.load(texture)
                self.textures.append(np.array(pre_load).T)

    def cast_ray(self, column):
        """
        TODO: Pass a full numpy array of columns all at once -- we need to
        adjust the for-loop to accommodate, but everything else should stay
        pretty much untouched besides using an einsum to calculate ray_angle.

        #Notes for ray_angle if vectorized columns -- I think this is right for
        #doing element-wise scalar*vector and then element-wise matrix_mul
        column_transform = np.einsum('i,j->ij',columns, self.hght_inv) + self.const
        ray_angles = np.einsum('jk,ij->ij', cam, column_transform)

        A possible solution for the vectorized for-loop is to create an
        array that keeps track of whether a column has hit a wall and then
        use np.where to only do the operations inside the loop for arrays that
        haven't hit a wall yet.
        """
        ray_angle = self.player.cam.T @ (column * self.hght_inv + self.const)
        map_pos = self.player.pos.astype(int)
        with np.errstate(divide="ignore"):
            delta = abs(1 / ray_angle)
        step = 2 * np.heaviside(ray_angle, 1) - 1
        side_dis = step * (map_pos + (step + 1) / 2 - self.player.pos) * delta

        #Cast a ray until we hit a wall or hit max_range
        for hops in range(self.max_hops):
            side = 0 if side_dis[0] < side_dis[1] else 1
            side_dis[side] += delta[side]
            map_pos[side] += step[side]
            if self.game_map[tuple(map_pos)]:
                break
        else:
            #No walls in range
            self.distances[column] = float("inf")
            return float("inf"), side, map_pos, ray_angle

        #Avoiding euclidean distance, to avoid fish-eye effect.
        wall_dis =\
         (map_pos[side] - self.player.pos[side] + (1 - step[side]) / 2)\
         / ray_angle[side]
        #Save distance for sprite calculations.
        self.distances[column] = wall_dis
        return wall_dis, side, map_pos, ray_angle

    def draw_column(self, wall_dis, side, map_pos, ray_angle):
        line_height = int(self.height / wall_dis) if wall_dis else self.height
        if line_height == 0:
            return 0, 0, [] #Draw nothing

        line_start, line_end =\
         [int((i * line_height + self.height) / 2 +
              self.player.z * line_height) for i in [-1, 1]]
        line_start = 0 if line_start < 0 else line_start
        line_end = self.height if line_end > self.height else line_end
        line_height = line_end - line_start #Correct off-by-one errors

        #Shading
        shade = line_height if line_height < self.shade_dif else self.shade_dif
        shade += 0 if side else self.side_shade #One side is brighter

        #A buffer to store shade values
        shade_buffer = [shade] * line_height

        #Texturing
        if self.textures_on:
            tex_num = self.game_map[tuple(map_pos)] - 1
            texture_width, texture_height = self.textures[tex_num].shape

            wall_x =\
             (self.player.pos[1 - side] + wall_dis * ray_angle[1 - side]) % 1
            tex_x = int(wall_x * texture_width)
            if -1**side * ray_angle[side] < 0:
                tex_x = texture_width - tex_x - 1

            #Add or subtract texture values to shade values
            tex_to_wall_ratio = texture_height / line_height
            for i, val in enumerate(shade_buffer):
                tex_y = int(i * tex_to_wall_ratio)
                val += 2 * self.textures[tex_num][tex_x, tex_y] - 12

                #Write to shade_buffer, this clipping logic will be changed
                #in the future.
                if val <= 1:
                    shade_buffer[i] = 1
                elif 1 < val <= self.shades:
                    shade_buffer[i] = val
                else:
                    shade_buffer[i] = self.shades

        #Convert shade values to ascii; convert to array to broadcast to buffer
        column_buffer = [self.ascii_map[val] for val in shade_buffer]
        column_buffer = np.array(column_buffer, dtype=str)
        return line_start, line_end, column_buffer

    def cast_sprites(self):
        #For each sprite, calculate distance (squared) to player
        sprite_distances = {}
        for i, sprite in enumerate(self.game_map.sprites):
            #Relative position of sprite to player
            sprite["relative"] = self.player.pos - sprite["pos"]
            sprite_distances[i] = sprite["relative"] @ sprite["relative"]

        #Sprites sorted by distance from player.
        sorted_sprites = sorted(sprite_distances, key=sprite_distances.get,
                                reverse=True)
        sorted_sprites = [self.game_map.sprites[i] for i in sorted_sprites]

        #Camera Inverse used to calculate transformed position of sprites.
        cam_inv = np.linalg.inv(-self.player.cam[::-1])

        #Draw each sprite from furthest to closest.
        for sprite in sorted_sprites:
            #Transformed position of sprites due to camera's plane and angle
            trans_pos = sprite["relative"] @ cam_inv

            if trans_pos[1] <= 0: #Sprite is behind player, don't draw it.
                continue

            #Sprite x-position on screen
            sprite_x = int(self.height * (1 + trans_pos[0] / trans_pos[1]) - 1)
            #Sprite width and height
            sprite_height = int(self.height / trans_pos[1])
            sprite_width = int(self.width / trans_pos[1] / 2)
            if not all([sprite_height, sprite_width]): #Sprite too small.
                continue

            #Start and end points of vertical lines of the sprite
            start_y, end_y = [int((i * sprite_height + self.height) / 2
                              + self.player.z * sprite_height)
                              for i in [-1, 1]]
            if start_y < 0: start_y = 0
            if end_y >= self.height: end_y = self.height

            #Start and end points of horizontal lines
            start_x, end_x = [(i * sprite_width // 2 + sprite_x)
                              for i in [-1, 1]]
            if start_x < 0: start_x = 0
            if end_x > self.width: end_x = self.width

            tex_width, tex_height = self.textures[sprite["image"]].shape

            #Calculate some constants outside the next loops:
            clip_x = sprite_x - sprite_width / 2
            clip_y = (sprite_height - self.height) / 2\
                      - self.player.z * sprite_height
            width_ratio = tex_width / sprite_width
            height_ratio = tex_height / sprite_height

            #Draw sprite -- outer-loop, left-to-right; inner, top-to-bottom
            for column in range(start_x, end_x):
                #From which column in the texture characters are taken
                tex_x = int((column - clip_x) * width_ratio)

                #Check that column isn't off-screen and that sprite isn't
                #blocked by a wall
                if 0 <= column <= self.width and\
                   trans_pos[1] <= self.distances[column]:

                    vertical_buffer = [0] * (end_y - start_y)

                    for i in range(start_y, end_y):
                        #From which row characters are taken
                        tex_y = int((i + clip_y) * height_ratio)
                        char = self.textures[sprite["image"]][tex_x, tex_y]
                        vertical_buffer[i - start_y] = char\
                            if char != "0" else self.buffer[i, column]

                    #Convert to array to broadcast into buffer
                    vertical_buffer = np.array(vertical_buffer, dtype=str)
                    self.buffer[start_y:end_y, column] = vertical_buffer

    def update(self):
        #Clear buffer
        self.buffer = np.full((self.height, self.width), " ", dtype=str)

        #Draw floor
        self.buffer[self.floor_y:, :] = self.ascii_map[1]

        #Draw walls
        for column in range(self.width - 1):
            start, end, col_buffer = self.draw_column(*self.cast_ray(column))
            self.buffer[start:end, column] = col_buffer

        #Draw sprites
        self.cast_sprites()

        #Push buffer to screen
        self.render()

    def render(self):
        for row_num, row in enumerate(self.buffer):
            self.screen.addstr(row_num, 0, ''.join(row[:-1]))
        self.screen.refresh()

class Controller():
    """
    Controller class handles user input and updates all other objects.
    """
    def __init__(self, player, renderer):
        self.running = True
        self.player = player
        self.renderer = renderer
        self.keys = self.jumping_keys = defaultdict(bool)
        self.player_has_jumped = False
        self.listener = keyboard.Listener(on_press=self.pressed,
                                          on_release=self.released)
        self.listener.start()

    def user_input(self):
        if self.keys[Key.esc]:
            self.running = False
            self.listener.stop()
        if self.keys[KeyCode(char='t')]:
            self.renderer.textures_on = not self.renderer.textures_on
            self.keys[KeyCode(char='t')] = False
        self.movement()

    def pressed(self, key):
        self.keys[key] = True

    def released(self, key):
        self.keys[key] = False

    def movement(self):
        #We stop accepting move inputs (but turning is ok) in the middle of a
        #jump -- the effect is momentum-like movement while in the air.
        keys = self.jumping_keys if self.player.is_jumping else self.keys
        if self.player_has_jumped:
            self.jumping_keys = self.keys.copy()
            self.player_has_jumped = False

        #Constants that make the following conditionals much more readable
        left = self.keys[Key.left] or self.keys[KeyCode(char='a')]
        right = self.keys[Key.right] or self.keys[KeyCode(char='d')]
        up = keys[Key.up] or keys[KeyCode(char='w')]
        down = keys[Key.down] or keys[KeyCode(char='s')]
        strafe_l = keys[KeyCode(char='q')]
        strafe_r = keys[KeyCode(char='e')]

        if left ^ right:
            self.player.turn(left)
        if up ^ down:
            self.player.move((up - down) * self.player.speed)
        if strafe_l ^ strafe_r:
            self.player.move((strafe_l - strafe_r) * self.player.speed, True)
        if self.keys[Key.space]:
            self.player_has_jumped = True
            self.player.is_jumping = True
            self.keys[Key.space] = False

    def update(self):
        self.renderer.update()
        self.user_input()
        self.player.update()

def main(screen):
    init_curses(screen)
    game_map = Map("map1")
    player = Player(game_map)
    #We may mass load textures in the future and pass the list to renderer.
    renderer = Renderer(screen, player, game_map,
                        "texture1", "texture2", "texture3")
    controller = Controller(player, renderer)
    while controller.running:
        controller.update()
    curses.flushinp()
    curses.endwin()

def init_curses(screen):
    curses.noecho()
    curses.curs_set(0)
    curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK)
    screen.attron(curses.color_pair(1))
    screen.clear()

if __name__ == "__main__":
    curses.wrapper(main)


import numpy as np
arr=np.arange(10)
print arr

#saving single array
np.save('saved_array',arr)
#now_file_is_created = saved_array.npy

new_array=np.load('saved_array.npy')
print new_array

#save multiple array

array_1=np.arange(25)
array_2=np.arange(30)

np.savez('saved_archieve.npz',x=array_1,y=array_2)

load_archieve=np.load('saved_archieve.npz')

print 'load_archieve[x] is'
print load_archieve['x']

print 'load_archieve[y] is'
print load_archieve['y']

Section3/L6 Saving loading of file/saving_loading_arrays.py


#!/usr/bin/env python
# coding: utf-8

# # [Import Library dan File Unloading](https://academy.dqlab.id/main/projectcode/212/378/1876)

# In[1]:

#import library yang dibutuhkan
import pandas as pd
import numpy as np

#lakukan pembacaan dataset
movie_df = pd.read_csv('https://dqlab-dataset.s3-ap-southeast-1.amazonaws.com/title.basics.tsv', sep='\t') #untuk menyimpan title_basics.tsv
rating_df = pd.read_csv('https://dqlab-dataset.s3-ap-southeast-1.amazonaws.com/title.ratings.tsv', sep='\t') #untuk menyimpan title.ratings.tsv

# # [5 Data teratas dari table movie](https://academy.dqlab.id/main/projectcode/212/379/1877)

# In[2]:

print(movie_df.head())

# # [Tipe Data dari Setiap Kolom](https://academy.dqlab.id/main/projectcode/212/379/1922)

# In[3]:

print(movie_df.info())

# # [Pengecekan Data dengan Nilai NULL](https://academy.dqlab.id/main/projectcode/212/379/1923)

# In[4]:

print(movie_df.isnull().sum())

# # [Analisis Kolom dengan data bernilai NULL - part 1](https://academy.dqlab.id/main/projectcode/212/379/1931)

# In[5]:

print(movie_df.loc[(movie_df['primaryTitle'].isnull()) | (movie_df['originalTitle'].isnull())])

# # [Membuang Data dengan Nilai NULL - part 1](https://academy.dqlab.id/main/projectcode/212/379/1932)

# In[6]:

#mengupdate movie_df dengan membuang data-data bernilai NULL
movie_df = movie_df.loc[(movie_df['primaryTitle'].notnull()) & (movie_df['originalTitle'].notnull())]

#menampilkan jumlah data setelah data dengan nilai NULL dibuang
print(len(movie_df))

# # [Analisis Kolom dengan data bernilai NULL - part 2](https://academy.dqlab.id/main/projectcode/212/379/1933)

# In[7]:

print(movie_df.loc[movie_df['genres'].isnull()])

# # [Membuang Data dengan Nilai NULL - part 2](https://academy.dqlab.id/main/projectcode/212/379/1934)

# In[8]:

#mengupdate movie_df dengan membuang data-data bernilai NULL
movie_df = movie_df.loc[movie_df['genres'].notnull()]

#menampilkan jumlah data setelah data dengan nilai NULL dibuang
print(len(movie_df))

# # [Mengubah Nilai '\\N'](https://academy.dqlab.id/main/projectcode/212/379/1935)

# In[9]:

#mengubah nilai '\\N' pada startYear menjadi np.nan dan cast kolomnya menjadi float64
movie_df['startYear'] = movie_df['startYear'].replace('\\N', np.nan)
movie_df['startYear'] = movie_df['startYear'].astype('float64')

print(movie_df['startYear'].unique()[:5])
#mengubah nilai '\\N' pada endYear menjadi np.nan dan cast kolomnya menjadi float64
movie_df['endYear'] = movie_df['endYear'].replace('\\N', np.nan)
movie_df['endYear'] = movie_df['endYear'].astype('float64')
print(movie_df['endYear'].unique()[:5])

#mengubah nilai '\\N' pada runtimeMinutes menjadi np.nan dan cast kolomnya menjadi float64
movie_df['runtimeMinutes'] = movie_df['runtimeMinutes'].replace('\\N', np.nan)
movie_df['runtimeMinutes'] = movie_df['runtimeMinutes'].astype('float64')
print(movie_df['runtimeMinutes'].unique()[:5])

# # [Mengubah nilai genres menjadi list](https://academy.dqlab.id/main/projectcode/212/379/1936)

# In[11]:

def transform_to_list(x):
    if ',' in x:
    #ubah menjadi list apabila ada data pada kolom genre
        return x.split(',')
    else:
    #jika tidak ada data, ubah menjadi list kosong
        return []

movie_df['genres'] = movie_df['genres'].apply(lambda x: transform_to_list(x))

# In[12]:

movie_df

# # [Menampilkan 5 data teratas](https://academy.dqlab.id/main/projectcode/212/380/1937)

# In[13]:

print(rating_df.head())

# # [Menampilkan tipe data](https://academy.dqlab.id/main/projectcode/212/380/1938)

# In[14]:

print(rating_df.info())

# # [Inner Join table movie dan table rating](https://academy.dqlab.id/main/projectcode/212/381/1939)

# In[15]:

#Lakukan join pada kedua table
movie_rating_df = pd.merge(movie_df, rating_df, on='tconst', how='inner')

#Tampilkan 5 data teratas
print(movie_rating_df.head())

#Tampilkan tipe data dari tiap kolom
print(movie_rating_df.info())

# # [Memperkecil ukuran Table](https://academy.dqlab.id/main/projectcode/212/381/1940)

# In[16]:

movie_rating_df = movie_rating_df.dropna(subset=['startYear','runtimeMinutes'])

#Untuk memastikan bahwa sudah tidak ada lagi nilai NULL
print(movie_rating_df.info())

# # [Pertanyaan 1: Berapa nilai C?](https://academy.dqlab.id/main/projectcode/212/382/1941)

# In[17]:

C = movie_rating_df['averageRating'].mean()
print(C)

# # [Pertanyaan 2: Berapa nilai m?](https://academy.dqlab.id/main/projectcode/212/382/1942)

# In[19]:

m = movie_rating_df['numVotes'].quantile(0.8)
print(m)

# # [Pertanyaan 3: Bagaimana cara membuat fungsi weighted formula?](https://academy.dqlab.id/main/projectcode/212/382/1943)

# In[20]:

def imdb_weighted_rating(df, var=0.8):
    v = df['numVotes']
    R = df['averageRating']
    C = df['averageRating'].mean()
    m = df['numVotes'].quantile(var)
    df['score'] = (v/(m+v))*R + (m/(m+v))*C #Rumus IMDb
    return df['score']

imdb_weighted_rating(movie_rating_df)

#melakukan pengecekan dataframe
print(movie_rating_df.head())

# # [Pertanyaan 4: Bagaimana cara membuat simple recommender system?](https://academy.dqlab.id/main/projectcode/212/382/1944)

# In[21]:

def simple_recommender(df, top=100):
    df = df.loc[df['numVotes'] >= m]
    df = df.sort_values(by='score', ascending=False)

    #Ambil data 100 teratas
    df = df[:top]
    return df

#Ambil data 25 teratas
print(simple_recommender(movie_rating_df, top=25))

# # [Pertanyaan 5: Bagaimana cara membuat simple recommender system dengan user preferences?](https://academy.dqlab.id/main/projectcode/212/382/1945)

# In[22]:

df = movie_rating_df.copy()

def user_prefer_recommender(df, ask_adult, ask_start_year, ask_genre, top=100):
    #ask_adult = yes/no
    if ask_adult.lower() == 'yes':
        df = df.loc[df['isAdult'] == 1]
    elif ask_adult.lower() == 'no':
        df = df.loc[df['isAdult'] == 0]

    #ask_start_year = numeric
    df = df.loc[df['startYear'] >= int(ask_start_year)]

    #ask_genre = 'all' atau yang lain
    if ask_genre.lower() == 'all':
        df = df
    else:
        def filter_genre(x):
            if ask_genre.lower() in str(x).lower():
                return True
            else:
                return False
        df = df.loc[df['genres'].apply(lambda x: filter_genre(x))]

    df = df.loc[df['numVotes'] >= m] #Mengambil film dengan m yang lebih besar dibanding numVotes
    df = df.sort_values(by='score', ascending=False)

    #jika kamu hanya ingin mengambil 100 teratas
    df = df[:top]
    return df

print(user_prefer_recommender(df,
                       ask_adult = 'no',
                        ask_start_year = 2000,
                       ask_genre = 'drama'
                       ))

Project/Python/Project Machine Learning with Python Building Recommender System/Project Machine Learning with Python Building Recommender System.py


r"""
Unique Representation

Abstract classes for cached and unique representation behavior.

.. SEEALSO::

   :class:`sage.structure.factory.UniqueFactory`

AUTHORS:

- Nicolas M. Thiery (2008): Original version.
- Simon A. King (2013-02): Separate cached and unique representation.
- Simon A. King (2013-08): Extended documentation.

What is a cached representation?
================================

Instances of a class have a *cached representation behavior* when several
instances constructed with the same arguments share the same memory
representation. For example, calling twice::

    sage: G = SymmetricGroup(6)
    sage: H = SymmetricGroup(6)

to create the symmetric group on six elements gives back the same
object::

    sage: G is H
    True

This is a standard design pattern. Besides saving memory, it allows for
sharing cached data (say representation theoretical information about a
group). And of course a look-up in the cache is faster than the creation of a
new object.

Implementing a cached representation
------------------------------------

Sage provides two standard ways to create a cached representation:
:class:`CachedRepresentation` and
:class:`~sage.structure.factory.UniqueFactory`. Note that, in spite of its
name, :class:`~sage.structure.factory.UniqueFactory` does not ensure *unique*
representation behaviour, which will be explained below.

Using :class:`CachedRepresentation`
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

It is often very easy to use :class:`CachedRepresentation`: One simply writes
a Python class and adds :class:`CachedRepresentation` to the list of base
classes. If one does so, then the arguments used to create an instance of this
class will by default also be used as keys for the cache::

    sage: from sage.structure.unique_representation import CachedRepresentation
    sage: class C(CachedRepresentation):
    ....:     def __init__(self, a, b=0):
    ....:         self.a = a
    ....:         self.b = b
    ....:     def __repr__(self):
    ....:         return "C(%s, %s)"%(self.a, self.b)
    sage: a = C(1)
    sage: a is C(1)
    True

In addition, pickling just works, provided that Python is able to look up the
class. Hence, in the following two lines, we explicitly put the class into the
``__main__`` module. This is needed in doctests, but not in an interactive
session::

    sage: import __main__
    sage: __main__.C = C
    sage: loads(dumps(a)) is a
    True

Often, this very easy approach is sufficient for applications. However, there
are some pitfalls. Since the arguments are used for caching, all arguments
must be hashable, i.e., must be valid as dictionary keys::

    sage: C((1,2))
    C((1, 2), 0)
    sage: C([1,2])
    Traceback (most recent call last):
    ...
    TypeError: unhashable type: 'list'

In addition, equivalent ways of providing the arguments are *not*
automatically normalised when forming the cache key, and hence different but
equivalent arguments may yield distinct instances::

    sage: C(1) is C(1,0)
    False
    sage: C(1) is C(a=1)
    False
    sage: repr(C(1)) == repr(C(a=1))
    True

It should also be noted that the arguments are compared by equality, not by
identity. This is often desired, but can imply subtle problems. For example,
since ``C(1)`` already is in the cache, and since the unit elements in
different finite fields are all equal to the integer one, we find::

    sage: GF(5)(1) == 1 == GF(3)(1)
    True
    sage: C(1) is C(GF(3)(1)) is C(GF(5)(1))
    True

But ``C(2)`` is not in the cache, and the number two is not equal in different
finite fields (i. e., ``GF(5)(2) == GF(3)(2)`` returns as ``False``), even
though it is equal to the number two in the ring of integers (
``GF(5)(2) == 2 == GF(3)(2)`` returns as ``True``; equality is not transitive
when comparing elements of *distinct* algebraic structures!!). Hence, we
have::

    sage: GF(5)(2) == GF(3)(2)
    False
    sage: C(GF(3)(2)) is C(GF(5)(2))
    False

Normalising the arguments
.........................

:class:`CachedRepresentation` uses the metaclass
:class:`~sage.misc.classcall_metaclass.ClasscallMetaclass`. Its
``__classcall__`` method is a
:class:`~sage.misc.cachefunc.WeakCachedFunction`.  This function creates an
instance of the given class using the given arguments, unless it finds the
result in the cache. This has the following implications:

- The arguments must be valid dictionary keys (i.e., they must be hashable;
  see above).
- It is a weak cache, hence, if the user does not keep a reference to the
  resulting instance, then it may be removed from the cache during garbage
  collection.
- It is possible to preprocess the input arguments by implementing a
  ``__classcall__`` or a ``__classcall_private__`` method, but in order to
  benefit from caching, :meth:`CachedRepresentation.__classcall__` should at
  some point be called.

.. NOTE::

    For technical reasons, it is needed that ``__classcall__`` respectively
    ``__classcall_private__`` are "static methods", i.e., they are callable
    objects that do not bind to an instance or class. For example, a
    :class:`~sage.misc.cachefunc.cached_function` can be used here, because it
    is callable, but does not bind to an instance or class, because it has no
    ``__get__()`` method. A usual Python function, however, has a
    ``__get__()`` method and would thus under normal circumstances bind to an
    instance or class, and thus the instance or class would be passed to the
    function as the first argument. To prevent a callable object from being
    bound to the instance or class, one can prepend the ``@staticmethod``
    decorator to the definition; see :class:`staticmethod`.

    For more on Python's ``__get__()`` method, see:
    https://docs.python.org/2/howto/descriptor.html

.. WARNING::

    If there is preprocessing, then the preprocessed arguments
    passed to :meth:`CachedRepresentation.__classcall__` must be invariant
    under the preprocessing. That is to say, preprocessing the input
    arguments twice must have the same effect as preprocessing the input
    arguments only once. That is to say, the preprocessing must be idempotent.

The reason for this warning lies in the way pickling is implemented. If the
preprocessed arguments are passed to
:meth:`CachedRepresentation.__classcall__`, then the resulting instance will
store the *preprocessed* arguments in some attribute, and will use them for
pickling. If the pickle is unpickled, then preprocessing is applied to the
preprocessed arguments---and this second round of preprocessing must not
change the arguments further, since otherwise a different instance would be
created.

We illustrate the warning by an example. Imagine that one has instances that
are created with an integer-valued argument, but only depend on the *square*
of the argument. It would be a mistake to square the given argument during
preprocessing::

    sage: class WrongUsage(CachedRepresentation):
    ....:     @staticmethod
    ....:     def __classcall__(cls, n):
    ....:         return super(WrongUsage,cls).__classcall__(cls, n^2)
    ....:     def __init__(self, n):
    ....:         self.n = n
    ....:     def __repr__(self):
    ....:         return "Something(%d)"%self.n
    sage: import __main__
    sage: __main__.WrongUsage = WrongUsage # This is only needed in doctests
    sage: w = WrongUsage(3); w
    Something(9)
    sage: w._reduction
    (<class '__main__.WrongUsage'>, (9,), {})

Indeed, the reduction data are obtained from the preprocessed argument. By
consequence, if the resulting instance is pickled and unpickled, the argument
gets squared *again*::

    sage: loads(dumps(w))
    Something(81)

Instead, the preprocessing should only take the absolute value of the given
argument, while the squaring should happen inside of the ``__init__`` method,
where it won't mess with the cache::

    sage: class BetterUsage(CachedRepresentation):
    ....:     @staticmethod
    ....:     def __classcall__(cls, n):
    ....:         return super(BetterUsage, cls).__classcall__(cls, abs(n))
    ....:     def __init__(self, n):
    ....:         self.n = n^2
    ....:     def __repr__(self):
    ....:         return "SomethingElse(%d)"%self.n
    sage: __main__.BetterUsage = BetterUsage # This is only needed in doctests
    sage: b = BetterUsage(3); b
    SomethingElse(9)
    sage: loads(dumps(b)) is b
    True
    sage: b is BetterUsage(-3)
    True

In our next example, we create a cached representation class ``C`` that
returns an instance of a sub-class ``C1`` or ``C2`` depending on the given
arguments. This is implemented in a static ``__classcall_private__`` method of
``C``, letting it choose the sub-class according to the given arguments. Since
a ``__classcall_private__`` method will be ignored on sub-classes, the caching
of :class:`CachedRepresentation` is available to both ``C1`` and ``C2``. But
for illustration, we overload the static ``__classcall__`` method on ``C2``,
doing some argument preprocessing. We also create a sub-class ``C2b`` of
``C2``, demonstrating that the ``__classcall__`` method is used on the
sub-class (in contrast to a ``__classcall_private__`` method!).  ::

    sage: class C(CachedRepresentation):
    ....:     @staticmethod
    ....:     def __classcall_private__(cls, n, implementation=0):
    ....:         if not implementation:
    ....:             return C.__classcall__(cls, n)
    ....:         if implementation==1:
    ....:             return C1(n)
    ....:         if implementation>1:
    ....:             return C2(n,implementation)
    ....:     def __init__(self, n):
    ....:         self.n = n
    ....:     def __repr__(self):
    ....:         return "C(%d, 0)"%self.n
    sage: class C1(C):
    ....:     def __repr__(self):
    ....:         return "C1(%d)"%self.n
    sage: class C2(C):
    ....:     @staticmethod
    ....:     def __classcall__(cls, n, implementation=0):
    ....:         if implementation:
    ....:             return super(C2, cls).__classcall__(cls, (n,)*implementation)
    ....:         return super(C2, cls).__classcall__(cls, n)
    ....:     def __init__(self, t):
    ....:         self.t = t
    ....:     def __repr__(self):
    ....:         return "C2(%s)"%repr(self.t)
    sage: class C2b(C2):
    ....:     def __repr__(self):
    ....:         return "C2b(%s)"%repr(self.t)
    sage: __main__.C2 = C2      # not needed in an interactive session
    sage: __main__.C2b = C2b

In the above example, ``C`` drops the argument ``implementation`` if it
evaluates to ``False``, and since the cached ``__classcall__`` is called in
this case, we have::

    sage: C(1)
    C(1, 0)
    sage: C(1) is C(1,0)
    True
    sage: C(1) is C(1,0) is C(1,None) is C(1,[])
    True

(Note that we were able to bypass the issue of arguments having to be
hashable by catching the empty list ``[]`` during preprocessing in the
``__classcall_private__`` method. Similarly, unhashable arguments can
be made hashable -- e. g., lists normalized to tuples -- in the
``__classcall_private__`` method before they are further delegated to
``__classcall__``. See
:class:`~sage.combinat.crystals.elementary_crystals.TCrystal` for an
example.)

If we call ``C1`` directly or if we provide ``implementation=1`` to ``C``, we
obtain an instance of ``C1``. Since it uses the ``__classcall__`` method
inherited from :class:`CachedRepresentation`, the resulting instances are
cached::

    sage: C1(2)
    C1(2)
    sage: C(2, implementation=1)
    C1(2)
    sage: C(2, implementation=1) is C1(2)
    True

The class ``C2`` preprocesses the input arguments. Instances can, again, be
obtained directly or by calling ``C``::

    sage: C(1, implementation=3)
    C2((1, 1, 1))
    sage: C(1, implementation=3) is C2(1,3)
    True

The argument preprocessing of ``C2`` is inherited by ``C2b``, since
``__classcall__`` and not ``__classcall_private__`` is used. Pickling works,
since the preprocessing of arguments is idempotent::

    sage: c2b = C2b(2,3); c2b
    C2b((2, 2, 2))
    sage: loads(dumps(c2b)) is c2b
    True

Using :class:`~sage.structure.factory.UniqueFactory`
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

For creating a cached representation using a factory, one has to

- create a class *separately* from the factory. This class **must** inherit
  from :class:`object`. Its instances **must** allow attribute assignment.
- write a method ``create_key`` (or ``create_key_and_extra_args``) that
  creates the cache key from the given arguments.
- write a method ``create_object`` that creates an instance of the class
  from a given cache key.
- create an instance of the factory with a name that allows to conclude where
  it is defined.

An example::

    sage: class C(object):
    ....:     def __init__(self, t):
    ....:         self.t = t
    ....:     def __repr__(self):
    ....:         return "C%s"%repr(self.t)
    sage: from sage.structure.factory import UniqueFactory
    sage: class MyFactory(UniqueFactory):
    ....:     def create_key(self, n, m=None):
    ....:         if isinstance(n, (tuple,list)) and m is None:
    ....:             return tuple(n)
    ....:         return (n,)*m
    ....:     def create_object(self, version, key, **extra_args):
    ....:         # We ignore version and extra_args
    ....:         return C(key)

Now, we define an instance of the factory, stating that it can be found under
the name ``"F"`` in the ``__main__`` module. By consequence, pickling works::

    sage: F = MyFactory("__main__.F")
    sage: __main__.F = F                # not needed in an interactive session
    sage: loads(dumps(F)) is F
    True

We can now create *cached* instances of ``C`` by calling the factory. The
cache only takes into account the key computed with the method ``create_key``
that we provided. Hence, different given arguments may result in the same
instance. Note that, again, the cache is weak, hence, the instance might be
removed from the cache during garbage collection, unless an external reference
is preserved.
::

    sage: a = F(1, 2); a
    C(1, 1)
    sage: a is F((1,1))
    True

**If** the class of the returned instances is a sub-class of :class:`object`,
and **if** the resulting instance allows attribute assignment, then pickling
of the resulting instances is automatically provided for, and respects the
cache.  ::

    sage: loads(dumps(a)) is a
    True

This is because an attribute is stored that explains how the instance was
created::

    sage: a._factory_data
    (<__main__.MyFactory object at ...>, (...), (1, 1), {})

.. NOTE::

    If a class is used that does not inherit from :class:`object` then unique
    pickling is *not* provided.

Caching is only available if the factory is called. If an instance of the
class is directly created, then the cache is not used::

    sage: C((1,1))
    C(1, 1)
    sage: C((1,1)) is a
    False

Comparing the two ways of implementing a cached representation
--------------------------------------------------------------

In this sub-section, we discuss advantages and disadvantages of the two ways
of implementing a cached representation, depending on the type of application.

Simplicity and transparency
^^^^^^^^^^^^^^^^^^^^^^^^^^^

In many cases, turning a class into a cached representation requires nothing
more than adding :class:`CachedRepresentation` to the list of base classes of
this class. This is, of course, a very easy and convenient way. Writing a
factory would involve a lot more work.

If preprocessing of the arguments is needed, then we have seen how to do this
by a ``__classcall_private__`` or ``__classcall__`` method. But these are
double underscore methods and hence, for example, invisible in the
automatically created reference manual. Moreover, preprocessing *and* caching
are implemented in the same method, which might be confusing. In a unique
factory, these two tasks are cleanly implemented in two separate methods.
With a factory, it is possible to create the resulting instance by arguments
that are different from the key used for caching. This is significantly
restricted with CachedRepresentation due to the requirement that argument
preprocessing be idempotent.

Hence, if advanced preprocessing is needed, then
:class:`~sage.structure.factory.UniqueFactory` might be easier and more
transparent to use than :class:`CachedRepresentation`.

Class inheritance
^^^^^^^^^^^^^^^^^

Using :class:`CachedRepresentation` has the advantage that one has a class and
creates cached instances of this class by the usual Python syntax::

    sage: G = SymmetricGroup(6)
    sage: issubclass(SymmetricGroup, sage.structure.unique_representation.CachedRepresentation)
    True
    sage: isinstance(G, SymmetricGroup)
    True

In contrast, a factory is just a callable object that returns something that
has absolutely nothing to do with the factory, and may in fact return
instances of quite different classes::

    sage: isinstance(GF, sage.structure.factory.UniqueFactory)
    True
    sage: K5 = GF(5)
    sage: type(K5)
    <class 'sage.rings.finite_rings.finite_field_prime_modn.FiniteField_prime_modn_with_category'>
    sage: K25 = GF(25, 'x')
    sage: type(K25)
    <class 'sage.rings.finite_rings.finite_field_givaro.FiniteField_givaro_with_category'>
    sage: Kp = GF(next_prime_power(1000000)^2, 'x')
    sage: type(Kp)
    <class 'sage.rings.finite_rings.finite_field_pari_ffelt.FiniteField_pari_ffelt_with_category'>

This can be confusing to the user. Namely, the user might determine the class
of an instance and try to create further instances by calling the class rather
than the factory---which is a mistake since it works around the cache (and
also since the class might be more restrictive than the factory -- i. e., the
type of ``K5`` in the above doctest cannot be called on a prime power which
is not a prime). This mistake can more easily be avoided by using
:class:`CachedRepresentation`.

We have seen above that one can easily create new cached-representation
classes by subclassing an existing cached-representation class, even making
use of an existing argument preprocess. This would be much more complicated
with a factory. Namely, one would need to rewrite old factories making them
aware of the new classes, and/or write new factories for the new classes.

Python versus extension classes
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

:class:`CachedRepresentation` uses a metaclass, namely
:class:`~sage.misc.classcall_metaclass.ClasscallMetaclass`. Hence, it can
currently not be a Cython extension class. Moreover, it is supposed to be used
by providing it as a base class. But in typical applications, one also has
another base class, say, :class:`~sage.structure.parent.Parent`. Hence, one
would like to create a class with at least two base classes, which is
currently impossible in Cython extension classes.

In other words, when using :class:`CachedRepresentation`, one must work with
Python classes. These can be defined in Cython code (``.pyx`` files) and can
thus benefit from Cython's speed inside of their methods, but they must not be
``cdef class`` and can thus not use ``cdef`` attributes or methods.

Such restrictions do not exist when using a factory. However, if attribute
assignment does not work, then the automatic pickling provided by
:class:`~sage.structure.factory.UniqueFactory` will not be available.

What is a unique representation?
================================

Instances of a class have a *unique instance behavior* when instances of this
class evaluate equal if and only if they are identical. Sage provides the base
class :class:`~sage.misc.fast_methods.WithEqualityById`, which provides
comparison by identity and a hash that is determined by the memory address of
the instance. Both the equality test and the hash are implemented in Cython
and are very fast, even when one has a Python class inheriting from
:class:`~sage.misc.fast_methods.WithEqualityById`.

In many applications, one wants to combine unique instance and cached
representation behaviour. This is called *unique representation* behaviour.
We have seen above that symmetric groups have a *cached* representation
behaviour. However, they do not show the *unique* representation behaviour,
since they are equal to groups created in a totally different way, namely to
subgroups::

    sage: G = SymmetricGroup(6)
    sage: G3 = G.subgroup([G((1,2,3,4,5,6)),G((1,2))])
    sage: G is G3
    False
    sage: type(G) == type(G3)
    False
    sage: G == G3
    True

The unique representation behaviour can conveniently be implemented with a
class that inherits from :class:`UniqueRepresentation`: By adding
:class:`UniqueRepresentation` to the base classes, the class will
simultaneously inherit from :class:`CachedRepresentation` and from
:class:`~sage.misc.fast_methods.WithEqualityById`.

For example, a symmetric function algebra is uniquely determined by the base
ring. Thus, it is reasonable to use :class:`UniqueRepresentation` in this
case::

    sage: isinstance(SymmetricFunctions(CC), SymmetricFunctions)
    True
    sage: issubclass(SymmetricFunctions, UniqueRepresentation)
    True

:class:`UniqueRepresentation` differs from :class:`CachedRepresentation` only
by adding :class:`~sage.misc.fast_methods.WithEqualityById` as a base
class. Hence, the above examples of argument preprocessing work for
:class:`UniqueRepresentation` as well.

Note that a cached representation created with
:class:`~sage.structure.factory.UniqueFactory` does *not* automatically
provide unique representation behaviour, in spite of its name! Hence, for
unique representation behaviour, one has to implement hash and equality test
accordingly, for example by inheriting from
:class:`~sage.misc.fast_methods.WithEqualityById`.

"""
# ****************************************************************************
#  Copyright (C) 2008 Nicolas M. Thiery <nthiery at users.sf.net>
#  Copyright (C) 2013 Simon A. King <simon.king at uni-jena.de>
#
#  Distributed under the terms of the GNU General Public License (GPL)
#
#    This code is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#    General Public License for more details.
#
#  The full text of the GPL is available at:
#
#                  https://www.gnu.org/licenses/
# *****************************************************************************

from sage.misc.cachefunc import weak_cached_function
from sage.misc.classcall_metaclass import ClasscallMetaclass, typecall
from sage.misc.fast_methods import WithEqualityById

class CachedRepresentation(metaclass=ClasscallMetaclass):
    """
    Classes derived from CachedRepresentation inherit a weak cache for their
    instances.

    .. NOTE::

        If this class is used as a base class, then instances are (weakly)
        cached, according to the arguments used to create the instance.
        Pickling is provided, of course by using the cache.

    .. NOTE::

        Using this class, one can have arbitrary hash and comparison.
        Hence, *unique* representation behaviour is *not* provided.

    .. SEEALSO::

        :class:`UniqueRepresentation`, :mod:`~sage.structure.unique_representation`

    EXAMPLES:

    Providing a class with a weak cache for the instances is easy: Just
    inherit from :class:`CachedRepresentation`::

        sage: from sage.structure.unique_representation import CachedRepresentation
        sage: class MyClass(CachedRepresentation):
        ....:     # all the rest as usual
        ....:     pass

    We start with a simple class whose constructor takes a single
    value as argument (TODO: find a more meaningful example)::

        sage: class MyClass(CachedRepresentation):
        ....:     def __init__(self, value):
        ....:         self.value = value
        ....:     def __eq__(self, other):
        ....:         if type(self) != type(other):
        ....:             return False
        ....:         return self.value == other.value

    Two coexisting instances of ``MyClass`` created with the same argument data
    are guaranteed to share the same identity. Since :trac:`12215`, this is
    only the case if there is some strong reference to the returned instance,
    since otherwise it may be garbage collected::

        sage: x = MyClass(1)
        sage: y = MyClass(1)
        sage: x is y               # There is a strong reference
        True
        sage: z = MyClass(2)
        sage: x is z
        False

    In particular, modifying any one of them modifies the other
    (reference effect)::

        sage: x.value = 3
        sage: x.value, y.value
        (3, 3)
        sage: y.value = 1
        sage: x.value, y.value
        (1, 1)

    The arguments can consist of any combination of positional or keyword
    arguments, as taken by a usual :meth:`__init__ <object.__init__>`
    function. However, all values passed in should be hashable::

        sage: MyClass(value = [1,2,3])
        Traceback (most recent call last):
        ...
        TypeError: unhashable type: 'list'

    .. rubric:: Argument preprocessing

    Sometimes, one wants to do some preprocessing on the arguments, to
    put them in some canonical form. The following example illustrates
    how to achieve this; it takes as argument any iterable, and
    canonicalizes it into a tuple (which is hashable!)::

        sage: class MyClass2(CachedRepresentation):
        ....:     @staticmethod
        ....:     def __classcall__(cls, iterable):
        ....:         t = tuple(iterable)
        ....:         return super(MyClass2, cls).__classcall__(cls, t)
        ....:
        ....:     def __init__(self, value):
        ....:         self.value = value
        sage: x = MyClass2([1,2,3])
        sage: y = MyClass2(tuple([1,2,3]))
        sage: z = MyClass2(i for i in [1,2,3])
        sage: x.value
        (1, 2, 3)
        sage: x is y, y is z
        (True, True)

    A similar situation arises when the constructor accepts default
    values for some of its parameters. Alas, the obvious
    implementation does not work::

        sage: class MyClass3(CachedRepresentation):
        ....:     def __init__(self, value = 3):
        ....:         self.value = value
        sage: MyClass3(3) is MyClass3()
        False

    Instead, one should do::

        sage: class MyClass3(UniqueRepresentation):
        ....:     @staticmethod
        ....:     def __classcall__(cls, value = 3):
        ....:         return super(MyClass3, cls).__classcall__(cls, value)
        ....:
        ....:     def __init__(self, value):
        ....:         self.value = value
        sage: MyClass3(3) is MyClass3()
        True

    A bit of explanation is in order. First, the call ``MyClass2([1,2,3])``
    triggers a call to ``MyClass2.__classcall__(MyClass2, [1,2,3])``. This is
    an extension of the standard Python behavior, needed by
    :class:`CachedRepresentation`, and implemented by the
    :class:`~sage.misc.classcall_metaclass.ClasscallMetaclass`. Then,
    ``MyClass2.__classcall__`` does the desired transformations on the
    arguments. Finally, it uses ``super`` to call the default implementation
    of ``__classcall__`` provided by :class:`CachedRepresentation`. This one
    in turn handles the caching and, if needed, constructs and initializes a
    new object in the class using :meth:`__new__<object.__new__>` and
    :meth:`__init__<object.__init__>` as usual.

    Constraints:

    - :meth:`__classcall__` is a staticmethod (like, implicitly,
      :meth:`__new__<object.__new__>`)
    - the preprocessing on the arguments should be idempotent. That is, if
      ``MyClass2.__classcall__(<arguments>)`` calls
      ``CachedRepresentation.__classcall__(<preprocessed_arguments>)``, then
      ``MyClass2.__classcall__(<preprocessed_arguments>)`` should also result
      in a call to ``CachedRepresentation.__classcall__(<preprocessed_arguments>)``.
    - ``MyClass2.__classcall__`` should return the result of
      :meth:`CachedRepresentation.__classcall__` without modifying it.

    Other than that ``MyClass2.__classcall__`` may play any tricks, like
    acting as a factory and returning objects from other classes.

    .. WARNING::

        It is possible, but strongly discouraged, to let the ``__classcall__``
        method of a class ``C`` return objects that are not instances of
        ``C``. Of course, instances of a *subclass* of ``C`` are fine. Compare
        the examples in :mod:`~sage.structure.unique_representation`.

    We illustrate what is meant by an "idempotent" preprocessing. Imagine
    that one has instances that are created with an integer-valued argument,
    but only depend on the *square* of the argument. It would be a mistake to
    square the given argument during preprocessing::

        sage: class WrongUsage(CachedRepresentation):
        ....:     @staticmethod
        ....:     def __classcall__(cls, n):
        ....:         return super(WrongUsage,cls).__classcall__(cls, n^2)
        ....:     def __init__(self, n):
        ....:         self.n = n
        ....:     def __repr__(self):
        ....:         return "Something(%d)"%self.n
        sage: import __main__
        sage: __main__.WrongUsage = WrongUsage # This is only needed in doctests
        sage: w = WrongUsage(3); w
        Something(9)
        sage: w._reduction
        (<class '__main__.WrongUsage'>, (9,), {})

    Indeed, the reduction data are obtained from the preprocessed
    arguments. By consequence, if the resulting instance is pickled and
    unpickled, the argument gets squared *again*::

        sage: loads(dumps(w))
        Something(81)

    Instead, the preprocessing should only take the absolute value of the
    given argument, while the squaring should happen inside of the
    ``__init__`` method, where it won't mess with the cache::

        sage: class BetterUsage(CachedRepresentation):
        ....:     @staticmethod
        ....:     def __classcall__(cls, n):
        ....:         return super(BetterUsage, cls).__classcall__(cls, abs(n))
        ....:     def __init__(self, n):
        ....:         self.n = n^2
        ....:     def __repr__(self):
        ....:         return "SomethingElse(%d)"%self.n
        sage: __main__.BetterUsage = BetterUsage # This is only needed in doctests
        sage: b = BetterUsage(3); b
        SomethingElse(9)
        sage: loads(dumps(b)) is b
        True
        sage: b is BetterUsage(-3)
        True

    .. rubric:: Cached representation and mutability

    :class:`CachedRepresentation` is primarily intended for implementing
    objects which are (at least semantically) immutable. This is in
    particular assumed by the default implementations of ``copy`` and
    ``deepcopy``::

        sage: copy(x) is x
        True
        sage: from copy import deepcopy
        sage: deepcopy(x) is x
        True

    However, in contrast to :class:`UniqueRepresentation`, using
    :class:`CachedRepresentation` allows for a comparison that is not by
    identity::

        sage: t = MyClass(3)
        sage: z = MyClass(2)
        sage: t.value = 2

    Now ``t`` and ``z`` are non-identical, but equal::

        sage: t.value == z.value
        True
        sage: t == z
        True
        sage: t is z
        False

    .. rubric:: More on cached representation and identity

    :class:`CachedRepresentation` is implemented by means of a cache.
    This cache uses weak references in general, but strong references to
    the most recently created objects. Hence, when all other references
    to, say, ``MyClass(1)`` have been deleted, the instance is
    eventually deleted from memory (after enough other objects have been
    created to remove the strong reference to ``MyClass(1)``). A later
    call to ``MyClass(1)`` reconstructs the instance from scratch::

        sage: class SomeClass(UniqueRepresentation):
        ....:     def __init__(self, i):
        ....:         print("creating new instance for argument %s" % i)
        ....:         self.i = i
        ....:     def __del__(self):
        ....:         print("deleting instance for argument %s" % self.i)
        sage: class OtherClass(UniqueRepresentation):
        ....:     def __init__(self, i):
        ....:         pass
        sage: O = SomeClass(1)
        creating new instance for argument 1
        sage: O is SomeClass(1)
        True
        sage: O is SomeClass(2)
        creating new instance for argument 2
        False
        sage: L = [OtherClass(i) for i in range(200)]
        deleting instance for argument 2
        sage: del O
        deleting instance for argument 1
        sage: O = SomeClass(1)
        creating new instance for argument 1
        sage: del O
        sage: del L
        sage: L = [OtherClass(i) for i in range(200)]
        deleting instance for argument 1

    .. rubric:: Cached representation and pickling

    The default Python pickling implementation (by reconstructing an object
    from its class and dictionary, see "The pickle protocol" in the Python
    Library Reference) does not preserve cached representation, as Python has
    no chance to know whether and where the same object already exists.

    :class:`CachedRepresentation` tries to ensure appropriate pickling by
    implementing a :meth:`__reduce__ <object.__reduce__>` method returning the
    arguments passed to the constructor::

        sage: import __main__             # Fake MyClass being defined in a python module
        sage: __main__.MyClass = MyClass
        sage: x = MyClass(1)
        sage: loads(dumps(x)) is x
        True

    :class:`CachedRepresentation` uses the :meth:`__reduce__
    <object.__reduce__>` pickle protocol rather than :meth:`__getnewargs__
    <object.__getnewargs__>` because the latter does not handle keyword
    arguments::

        sage: x = MyClass(value = 1)
        sage: x.__reduce__()
        (<function unreduce at ...>, (<class '__main__.MyClass'>, (), {'value': 1}))
        sage: x is loads(dumps(x))
        True

    .. NOTE::

        The default implementation of :meth:`__reduce__ <object.__reduce__>`
        in :class:`CachedRepresentation` requires to store the constructor's
        arguments in the instance dictionary upon construction::

            sage: x.__dict__
            {'_reduction': (<class '__main__.MyClass'>, (), {'value': 1}), 'value': 1}

        It is often easy in a derived subclass to reconstruct the constructor's
        arguments from the instance data structure. When this is the case,
        :meth:`__reduce__ <object.__reduce__>` should be overridden; automagically
        the arguments won't be stored anymore::

            sage: class MyClass3(UniqueRepresentation):
            ....:     def __init__(self, value):
            ....:         self.value = value
            ....:
            ....:     def __reduce__(self):
            ....:         return (MyClass3, (self.value,))
            sage: import __main__; __main__.MyClass3 = MyClass3  # Fake MyClass3 being defined in a python module
            sage: x = MyClass3(1)
            sage: loads(dumps(x)) is x
            True
            sage: x.__dict__
            {'value': 1}

    .. rubric:: Migrating classes to ``CachedRepresentation`` and unpickling

    We check that, when migrating a class to :class:`CachedRepresentation`,
    older pickles can still be reasonably unpickled. Let us create a
    (new style) class, and pickle one of its instances::

        sage: class MyClass4(object):
        ....:     def __init__(self, value):
        ....:         self.value = value
        sage: import __main__; __main__.MyClass4 = MyClass4  # Fake MyClass4 being defined in a python module
        sage: pickle = dumps(MyClass4(1))

    It can be unpickled::

        sage: y = loads(pickle)
        sage: y.value
        1

    Now, we upgrade the class to derive from :class:`UniqueRepresentation`,
    which inherits from :class:`CachedRepresentation`::

        sage: class MyClass4(UniqueRepresentation, object):
        ....:     def __init__(self, value):
        ....:         self.value = value
        sage: import __main__; __main__.MyClass4 = MyClass4  # Fake MyClass4 being defined in a python module
        sage: __main__.MyClass4 = MyClass4

    The pickle can still be unpickled::

        sage: y = loads(pickle)
        sage: y.value
        1

    Note however that, for the reasons explained above, unique
    representation is not guaranteed in this case::

        sage: y is MyClass4(1)
        False

    .. TODO::

        Illustrate how this can be fixed on a case by case basis.

    Now, we redo the same test for a class deriving from SageObject::

        sage: class MyClass4(SageObject):
        ....:     def __init__(self, value):
        ....:         self.value = value
        sage: import __main__; __main__.MyClass4 = MyClass4  # Fake MyClass4 being defined in a python module
        sage: pickle = dumps(MyClass4(1))

        sage: class MyClass4(UniqueRepresentation, SageObject):
        ....:     def __init__(self, value):
        ....:         self.value = value
        sage: __main__.MyClass4 = MyClass4
        sage: y = loads(pickle)
        sage: y.value
        1

    Caveat: unpickling instances of a formerly old-style class is not supported yet by default::

        sage: class MyClass4:
        ....:     def __init__(self, value):
        ....:         self.value = value
        sage: import __main__; __main__.MyClass4 = MyClass4  # Fake MyClass4 being defined in a python module
        sage: pickle = dumps(MyClass4(1))

        sage: class MyClass4(UniqueRepresentation, SageObject):
        ....:     def __init__(self, value):
        ....:         self.value = value
        sage: __main__.MyClass4 = MyClass4
        sage: y = loads(pickle)  # todo: not implemented
        sage: y.value            # todo: not implemented
        1

    .. rubric:: Rationale for the current implementation

    :class:`CachedRepresentation` and derived classes use the
    :class:`~sage.misc.classcall_metaclass.ClasscallMetaclass`
    of the standard Python type. The following example explains why.

    We define a variant of ``MyClass`` where the calls to
    :meth:`__init__<object.__init__>` are traced::

        sage: class MyClass(CachedRepresentation):
        ....:     def __init__(self, value):
        ....:         print("initializing object")
        ....:         self.value = value

    Let us create an object twice::

        sage: x = MyClass(1)
        initializing object
        sage: z = MyClass(1)

    As desired the :meth:`__init__<object.__init__>` method was only called
    the first time, which is an important feature.

    As far as we can tell, this is not achievable while just using
    :meth:`__new__<object.__new__>` and :meth:`__init__<object.__init__>` (as
    defined by type; see Section :python:`Basic Customization
    <reference/datamodel.html#basic-customization>` in the Python Reference
    Manual). Indeed, :meth:`__init__<object.__init__>` is called
    systematically on the result of :meth:`__new__<object.__new__>` whenever
    the result is an instance of the class.

    Another difficulty is that argument preprocessing (as in the example
    above) cannot be handled by :meth:`__new__<object.__new__>`, since the
    unprocessed arguments will be passed down to
    :meth:`__init__<object.__init__>`.
    """

    @weak_cached_function(cache=128)  # automatically a staticmethod
    def __classcall__(cls, *args, **options):
        """
        Construct a new object of this class or reuse an existing one.

        See also :class:`CachedRepresentation` and
        :class:`UniqueRepresentation` for a discussion.

        EXAMPLES::

            sage: x = UniqueRepresentation()
            sage: y = UniqueRepresentation()
            sage: x is y   # indirect doctest
            True
        """
        instance = typecall(cls, *args, **options)
        assert isinstance( instance, cls )
        if instance.__class__.__reduce__ == CachedRepresentation.__reduce__:
            instance._reduction = (cls, args, options)
        return instance

    @classmethod
    def _clear_cache_(cls):
        """
        Remove all instances of this class from the cache.

        EXAMPLES:

        If ``cls`` overloads :meth:`~sage.structure.unique_representation.CachedRepresentation.__classcall__`,
        clearing the cache still works, because ``cls.mro()``
        is searched until a ``__classcall__`` with an attribute
        ``cache`` is found::

            sage: class A(UniqueRepresentation):
            ....:     def __init__(self, x):
            ....:         pass
            sage: class B(A):
            ....:     @staticmethod
            ....:     def __classcall__(cls, *args, **kwds):
            ....:          return super(B,cls).__classcall__(cls,*args,**kwds)
            sage: class C(B): pass
            sage: a = A(1)
            sage: b = B(2)
            sage: c = C(3)
            sage: a is A(1)
            True
            sage: b is B(2)
            True
            sage: c is C(3)
            True
            sage: B._clear_cache_()

        Now, all instances of (sub-classes of) ``B`` have disappeared
        from the cache::

            sage: a is A(1)
            True
            sage: b is B(2)
            False
            sage: c is C(3)
            False

        Here is a similar example, using a private classcall in the class
        ``B``, which is not inherited by ``C``::

            sage: class A(UniqueRepresentation):
            ....:     def __init__(self, x):
            ....:         pass
            sage: class B(A):
            ....:     @staticmethod
            ....:     def __classcall_private__(cls, *args, **kwds):
            ....:         print("Private B")
            ....:         return super(B,cls).__classcall__(cls,*args,**kwds)
            sage: class C(B): pass
            sage: a = A(1)
            sage: b = B(2)
            Private B
            sage: c = C(3)
            sage: a is A(1)
            True
            sage: b is B(2)
            Private B
            True
            sage: c is C(3)
            True
            sage: B._clear_cache_()

        Again, all instances of (sub-classes of) ``B`` have disappeared
        from the cache::

            sage: a is A(1)
            True
            sage: b is B(2)
            Private B
            False
            sage: c is C(3)
            False
        """
        del_list = []
        cache = None
        for C in cls.mro():
            try:
                cache = C.__classcall__.cache
            except AttributeError:
                pass
        for k in cache:
            if issubclass(k[0][0],cls):
                del_list.append(k)
        for k in del_list:
            del cache[k]

    def __reduce__(self):
        """
        Return the arguments that have been passed to
        :meth:`__new__<object.__new__>` to construct this object,
        as per the pickle protocol.

        See also :class:`CachedRepresentation` and
        :class:`UniqueRepresentation` for a discussion.

        EXAMPLES::

            sage: x = UniqueRepresentation()
            sage: x.__reduce__()          # indirect doctest
            (<function unreduce at ...>, (<class 'sage.structure.unique_representation.UniqueRepresentation'>, (), {}))
        """
        return (unreduce, self._reduction)

    def __copy__(self):
        """
        Return ``self``, as a semantic copy of ``self``.

        This assumes that the object is semantically immutable.

        EXAMPLES::

            sage: x = UniqueRepresentation()
            sage: x is copy(x)    # indirect doctest
            True
        """
        return self

    def __deepcopy__(self, memo):
        """
        Return ``self``, as a semantic deep copy of ``self``.

        This assumes that the object is semantically immutable.

        EXAMPLES::

            sage: from copy import deepcopy
            sage: x = UniqueRepresentation()
            sage: x is deepcopy(x)      # indirect doctest
            True
        """
        return self

def unreduce(cls, args, keywords):
    """
    Calls a class on the given arguments::

        sage: sage.structure.unique_representation.unreduce(Integer, (1,), {})
        1

    .. TODO::

        should reuse something preexisting ...

    """
    return cls(*args, **keywords)

class UniqueRepresentation(CachedRepresentation, WithEqualityById):
    r"""
    Classes derived from UniqueRepresentation inherit a unique
    representation behavior for their instances.

    .. SEEALSO::

        :mod:`~sage.structure.unique_representation`

    EXAMPLES:

    The short story: to construct a class whose instances have a
    unique representation behavior one just has to do::

        sage: class MyClass(UniqueRepresentation):
        ....:     # all the rest as usual
        ....:     pass

    Everything below is for the curious or for advanced usage.

    .. rubric:: What is unique representation?

    Instances of a class have a *unique representation behavior* when
    instances evaluate equal if and only if they are identical (i.e., share
    the same memory representation), if and only if they were created using
    equal arguments. For example, calling twice::

        sage: f = SymmetricFunctions(QQ)
        sage: g = SymmetricFunctions(QQ)

    to create the symmetric function algebra over `\QQ` actually gives back the
    same object::

        sage: f == g
        True
        sage: f is g
        True

    This is a standard design pattern. It allows for sharing cached data (say
    representation theoretical information about a group) as well as for very
    fast hashing and equality testing. This behaviour is typically desirable
    for parents and categories. It can also be useful for intensive
    computations where one wants to cache all the operations on a small set of
    elements (say the multiplication table of a small group), and access this
    cache as quickly as possible.

    :class:`UniqueRepresentation` is very easy to use: a class just needs to
    derive from it, or make sure some of its super classes does. Also, it
    groups together the class and the factory in a single gadget::

        sage: isinstance(SymmetricFunctions(CC), SymmetricFunctions)
        True
        sage: issubclass(SymmetricFunctions, UniqueRepresentation)
        True

    This nice behaviour is not available when one just uses a factory::

        sage: isinstance(GF(7), GF)  # py2
        Traceback (most recent call last):
        ...
        TypeError: isinstance() arg 2 must be a class, type, or tuple of classes and types
        sage: isinstance(GF(7), GF)  # py3
        Traceback (most recent call last):
        ...
        TypeError: isinstance() arg 2 must be a type or tuple of types

        sage: isinstance(GF, sage.structure.factory.UniqueFactory)
        True

    In addition, :class:`~sage.structure.factory.UniqueFactory` only provides
    the *cached* representation behaviour, but not the *unique* representation
    behaviour---the examples in :mod:`~sage.structure.unique_representation`
    explain this difference.

    On the other hand, the :class:`UniqueRepresentation` class is more
    intrusive, as it imposes a behavior (and a metaclass) on all the
    subclasses. In particular, the unique representation behaviour is imposed
    on *all* subclasses (unless the ``__classcall__`` method is overloaded and
    not called in the subclass, which is not recommended). Its implementation
    is also more technical, which leads to some subtleties.

    EXAMPLES:

    We start with a simple class whose constructor takes a single value as
    argument. This pattern is similar to what is done in
    :class:`sage.combinat.sf.sf.SymmetricFunctions`::

        sage: class MyClass(UniqueRepresentation):
        ....:     def __init__(self, value):
        ....:         self.value = value

    Two coexisting instances of ``MyClass`` created with the same argument
    data are guaranteed to share the same identity. Since :trac:`12215`, this
    is only the case if there is some strong reference to the returned
    instance, since otherwise it may be garbage collected::

        sage: x = MyClass(1)
        sage: y = MyClass(1)
        sage: x is y               # There is a strong reference
        True
        sage: z = MyClass(2)
        sage: x is z
        False

    In particular, modifying any one of them modifies the other
    (reference effect)::

        sage: x.value = 3
        sage: x.value, y.value
        (3, 3)
        sage: y.value = 1
        sage: x.value, y.value
        (1, 1)

    When comparing two instances of a unique representation with ``==``
    or ``!=`` comparison by identity is used::

        sage: x == y
        True
        sage: x is y
        True
        sage: z = MyClass(2)
        sage: x == z
        False
        sage: x is z
        False
        sage: x != y
        False
        sage: x != z
        True

    A hash function equivalent to :meth:`object.__hash__` is used, which is
    compatible with comparison by identity. However this means that the hash
    function may change in between Sage sessions, or even within the same Sage
    session.
    ::

        sage: hash(x) == object.__hash__(x)
        True

    .. WARNING::

        It is possible to inherit from
        :class:`~sage.structure.unique_representation.UniqueRepresentation`
        and then overload comparison in a way that destroys the unique
        representation property. We strongly recommend against it!  You should
        use :class:`~sage.structure.unique_representation.CachedRepresentation`
        instead.

    .. rubric:: Mixing super types and super classes

    TESTS:

    For the record, this test did fail with previous implementation
    attempts::

        sage: class bla(UniqueRepresentation, SageObject):
        ....:     pass
        sage: b = bla()
    """


from cmath import pi
from math import sqrt
from turtle import Screen
import pygame
import sys
import random
from pyparsing import Or

from scipy import rand

from soupsieve import match
from sqlalchemy import case, false

pygame.init()

SCREEN_WIDTH = 1400
SCREEN_HEIGHT = 800
COLOR_BLACK = (0, 0, 0)
COLOR_WHITE = (255, 255, 255)

#Create surface
surface = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

#Create title
pygame.display.set_caption("(^_ ^)")

#sounds

infectedsound = pygame.mixer.Sound('BallVeggBounce.wav')

#antall virus og murlocs for "score"
AntM = 0
AntV = 0
font = pygame.font.Font('freesansbold.ttf', 32)
fontPosx, fontPosy = 20, 20

def show_score(x, y):
    scoreV = font.render("Virus: " + str(AntV), True, (255, 255, 255))

pos_x = SCREEN_WIDTH/2 + random.randrange(-200, 200)
pos_y = SCREEN_HEIGHT/2 + random.randrange(-200, 200)

running = True

murloclist = []
viruslist = []

def move_ball():
    global pos_x
    global pos_y

    pos_x += 1
    pos_y += 1

class Murloc():
    def __init__(self):
        self.x = SCREEN_WIDTH/2 + random.randrange(-200, 200)
        self.y = SCREEN_HEIGHT/2 + random.randrange(-200, 200)
        self.infected = False
        self.size = random.randrange(35, 45)
        self.color = (255, 0, random.randrange(0, 100))
        self.spx = random.randrange(-5, 5, 2)
        self.spy = random.randrange(-5, 5, 2)
        self.infectedTime = 0

class Virus():
    def __init__(self):
        self.x = SCREEN_WIDTH/2 + random.randrange(-200, 200)
        self.y = SCREEN_HEIGHT/2 + random.randrange(-200, 200)
        self.spx = random.randrange(-7, 7, 2)
        self.spy = random.randrange(-7, 7, 2)
        self.size = random.randrange(4, 6)
        self.infectiousR = random.randrange(0, 95)
        self.color = (175, self.infectiousR+87, self.infectiousR+100)
        self.copy = random.randrange(1, 3)

def create_murloc():
    return Murloc()
  #  pygame.draw.circle(surface, m.color, (m.x, m.y), m.size)

def create_virus():
    return Virus()

def move(m: Murloc):

    Alive = True
    i = 1000

    while i > 0:
        move_murlocs()

        i -= 10

        pygame.display.update()

        surface.fill(COLOR_BLACK)

def clone_virus(m: Murloc):
    for i in range (1, random.randrange(0, 6)):
        v = create_virus()
        v.x = m.x
        v.y = m.y
        viruslist.append(v)

def move_murlocs():

    for m in murloclist:
        if m.x + m.size >= SCREEN_WIDTH or m.x - m.size <= 0:
            m.spx *= -1
        if m.y + m.size >= SCREEN_HEIGHT or m.y - m.size <= 0:
            m.spy *= -1
        if m.infected == True and m.infectedTime+5000<pygame.time.get_ticks():
            clone_virus(m)
            murloclist.remove(m)

        pygame.draw.circle(surface, m.color, (m.x, m.y), m.size)
        m.x += m.spx
        m.y += m.spy

def move_virus():

    for m in viruslist:
        if m.x + m.size >= SCREEN_WIDTH or m.x - m.size <= 0:
            m.spx *= -1
        if m.y + m.size >= SCREEN_HEIGHT or m.y - m.size <= 0:
            m.spy *= -1

        m.x += m.spx
        m.y += m.spy
        pygame.draw.circle(surface, m.color, (m.x, m.y), m.size)
        # if m.x + m.size >= SCREEN_WIDTH:
        #     Alive = False
        # elif m.x - m.size <= SCREEN_WIDTH:
        #     Alive = False
        # elif m.y + m.size >= SCREEN_HEIGHT:
        #     Alive = False
        # elif m.y - m.size <= SCREEN_HEIGHT:
        #     Alive = False

def infections(murloclist, viruslist):
    for m in murloclist:
        for v in viruslist:
            if m.infected == False and (sqrt(((v.x - m.x)**2)+((v.y - m.y)**2)) <= (m.size + v.size)):
                m.infected = True
                infectedsound.play()
                m.infectedTime = pygame.time.get_ticks()
                print(m.infectedTime)
                m.color = (39,134,39)
                viruslist.remove(v)

while running:
    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            running = False
            sys.exit()

        if event.type == pygame.KEYDOWN:

            if event.key == pygame.K_m:
                m = create_murloc()
                murloclist.append(m)
            if event.key == pygame.K_c:
                surface.fill(COLOR_BLACK)
            if event.key == pygame.K_v:
                v = create_virus()
                viruslist.append(v)
            if event.key == pygame.K_d:
                swch = 4

    move_murlocs()
    move_virus()
    infections(murloclist, viruslist)

    pygame.display.update()
    surface.fill(COLOR_BLACK)