Example #1
0
def get_basic_process_info_windows():
    pipe = os.popen("tasklist /FO CSV")
    content = StringIO(pipe.read())
    reader = csv.reader(content, delimiter=',', quotechar='"')
    content = []
    for row in reader:
        content.append(list(row))
    processes = {}
    for line in content[1:]:
        name, pid, _, _, _ = line
        processes[pid] = {
            "user": None,
            "command": name
        }
    return processes
Example #2
0
def main(pgn_file, out_path):
    '''
    Takes in one long/concatenated PGN file and spits out the end king squares to an output file.
    '''
    
    def end_board(parsed_pgn_file):
        '''Moves the py-chess board obj to the final move.'''
        current_board = parsed_pgn_file.board()
        for move in parsed_pgn_file.main_line():
            current_board.push(move)    
        return current_board
    
    def does_q_checkmate_k(board, winner, loser):
        #obv, only accounts for when a queen is DIRECTLY attacking a checkmated king,
        #not accounting for the lines of attack which PREVENT the king from moving
        try:
            #uses .attackers() to find which squares are attacking out checkmated king?
            board_string = str(board.attackers(winner, board.king(loser))).replace(" ","")[::-1] #removes whitespace and reverses str
            board_string = board_string.split("\n") #split in ranks (horizontally)
            board_string = [b[::-1] for b in board_string] #reverse each rank because h8 is the final sq
            board_string = "".join(board_string) #rejoin list
            board_string = list(enumerate(board_string)) #enumerate list as this is the way squares are counted (but in reverse)

            #is a queen on one of those squares?
            queen_num = board.queen(winner)
            does_q_checkmate_k = board_string[queen_num][1] is not '.' #is there a piece at the index which the queen is in
            return does_q_checkmate_k
        except:
            return False
    
    with open(pgn_file) as bigfile:
        pgn = []
        then = time.time()
        for line in bigfile:
            
            #Every 20 minutes, sleep for 2 minutes
            elapsed = time.time() - then
            if elapsed > 1200:
                print("Sleeping for 2 minutes")
                sleep(120)
                then = time.time()
        
            if line.startswith("[Event"):
                pgn.append(line)
            elif line.startswith("1. "):
                pgn.append(line)
                pgn = "".join(pgn)
                pgn = StringIO(pgn)
                pgn = read_game(pgn)
                result = pgn.headers["Result"]
                board = end_board(pgn)
                 
                if result in ["1-0","0-1"] and board.is_checkmate():
                    if result == "1-0":
                        winner = 1 # WHITE wins
                        loser = 0
                        king_num = board.king(loser) # BLACK loses
                        queen_num = 99
                    if result == "0-1":
                        winner = 0 # BLACK wins
                        loser = 1
                        king_num = board.king(loser) # WHITE loses
                        queen_num = 99
                    if does_q_checkmate_k(board,winner,loser):
                        queen_num = board.queen(winner)
                    with open(out_path, "a") as results:
                        results.write("%i,%i,%i\n" % (winner,king_num,queen_num))     
                else:
                    winner = 2 # DRAW
                    with open(out_path, "a") as results:
                        results.write("%i,%i-%i,\n" % (winner,board.king(1),board.king(0)))
                        
                pgn = []
                
            else:
                pgn.append(line)
Example #3
0
#  Mastering Python High Performance
#  http://proquest.safaribooksonline.com.ezproxy.spl.org:2048/book/programming/python/9781783989300/pypy/ch06lvl2sec51_html?uicode=spl

from io import StringIO

TIMES = 100000

#  Normal Python style string concatenation
init = time.process_time()
value = ''
for i in range(TIMES):
    value += str(i)
print("Concatenation: %s" % (time.process_time() - init))

#  cStringIO concatenation
init = time.process_time()
value = StringIO()
for i in range(TIMES):
    value.write(str(i))
print("StringIO: %s" % (time.process_time() - init))

#  List concatenation
init = time.process_time()
value = []
for i in range(TIMES):
    value.append(str(i))
finalValue = ''.join(value)
print("List: %s" % (time.process_time() - init))
Example #4
0
File: blp.py Project: shuge/pilgrim
    def __decode_blp1(self):
        header = StringIO(self.fp.read(28 + 16 * 4 + 16 * 4))

        magic, compression = unpack(b"<4si", header.read(8))
        encoding, alphaDepth, alphaEncoding, hasMips = unpack(b"<4b", header.read(4))
        self.size = unpack(b"<II", header.read(8))
        encoding, subtype = unpack(b"<ii", header.read(8))
        offsets = unpack(b"<16I", header.read(16 * 4))
        lengths = unpack(b"<16I", header.read(16 * 4))

        if compression == 0:
            jpegHeaderSize, = unpack(b"<I", self.fp.read(4))
            jpegHeader = self.fp.read(jpegHeaderSize)
            extraData = self.fp.read(offsets[0] - self.fp.tell())  # What IS this?
            data = self.fp.read(lengths[0])
            data = jpegHeader + data
            data = StringIO(data)
            image = JPEG(data)
            self.tile = image.tile  # PIL is terrible
            self.fp = image.fp
            self.mode = image.mode
            return

        if compression == 1:
            if encoding in (3, 4):
                data = []
                palette_data = self.fp.read(256 * 4)
                palette = getpalette(palette_data)
                length = self.size[0] * self.size[1]  # lengths[0] is the total length of data + alpha data
                _data = StringIO(self.fp.read(length))
                alpha_data = StringIO(self.fp.read(length))
                self.mode = "RGBA"
                self.tile = []

                while True:
                    try:
                        offset, = unpack(b"<B", _data.read(1))
                        a, = unpack(b"<B", alpha_data.read(1))
                    except StructError:
                        break
                    b, g, r, _ = palette[offset]
                    data.append(pack(b"<BBBB", r, g, b, a))

                data = "".join(data)
                self.im = Image.core.new(self.mode, self.size)
                self.fromstring(data)
                return

            elif encoding == 5:
                data = []
                palette_data = self.fp.read(256 * 4)
                palette = getpalette(palette_data)
                _data = StringIO(self.fp.read(lengths[0]))
                self.mode = "RGB"
                self.tile = []
                while True:
                    try:
                        offset, = unpack(b"<B", _data.read(1))
                    except StructError:
                        break
                    b, g, r, a = palette[offset]
                    data.append(pack(b"<BBB", r, g, b))

                data = b"".join(data)
                self.im = Image.core.new(self.mode, self.size)
                self.fromstring(data)
                return

            raise ValueError("Expected encoding 3, 4 or 5, got %i instead" % (encoding))

        raise ValueError("Expected compression 0 or 1, got %i instead" % (compression))