def portfolio_holding_lifo_test(): """ test of 'lifo' queuing by adding and removing a position. """ p1 = position.Position(symbol="AAPL", qty=1000, price=185.25, multiplier=1., fee=7.0, total_amt=185250., trans_date=1053605468.54) p2 = position.Position(symbol="AAPL", qty=1500, price=184.00, multiplier=1., fee=7.0, total_amt=276000., trans_date=1054202245.63) p3 = position.Position(symbol="AAPL", qty=-1500, price=186.00, multiplier=1., fee=7.0, total_amt=279000., trans_date=1055902486.22) h = portfolio.Holding() h.add_to(p1) h.add_to(p2) h.remove_from(p3, order='lifo') print "Holding: ", h print "Positions length: ", len(h.positions) print "Positions price: ", h.positions[0].price, h.positions[0].fee assert h.qty==1000 assert len(h.positions)==1 # simple check to make sure the position that we expect is left over... p = h.positions[0] assert p.price==185.25
def test_king_calculate_scope(self): king_fen = ('8/8/2k5/8/4q3/5KQ1/4B1R1/8 w - - 0 1') king_pos = position.Position(king_fen) king_c6 = piece.PieceFactory.create('k', (2, 2)) king_f4 = piece.PieceFactory.create('K', (5, 5)) self.assertCountEqual(king_c6.calculate_scope(king_pos), [(2, 3), (3, 3), (3, 2), (3, 1), (2, 1), (1, 1), (1, 2), (1, 3)]) self.assertCountEqual(king_f4.calculate_scope(king_pos), [(6, 5), (5, 4), (4, 4), (4, 5), (4, 6)]) castling_fen = 'r3kb1r/8/8/8/8/8/8/1R2K2R w KQkq - 0 1' castling_pos = position.Position(castling_fen) king_e1 = piece.PieceFactory.create('K', (7, 4)) king_e8 = piece.PieceFactory.create('k', (0, 4)) self.assertCountEqual(king_e1.calculate_scope(castling_pos), [(7, 5), (7, 6), (6, 5), (6, 4), (6, 3), (7, 3)]) self.assertCountEqual(king_e8.calculate_scope(castling_pos), [(1, 5), (1, 4), (1, 3), (0, 3), (0, 2)])
def position_sort_test(): """ Test to see if I can collect and sort these properly. The objective is to have the objects sort by the trans_date trait. """ p0 = position.Position(symbol="AAPL", id="1223", qty=1000, price=185.25, multiplier=1., fee=7.0, total_amt=185250., trans_date=1045623459.68) p1 = position.Position(symbol="AAPL", id="1224", qty=-1000, price=186.25, multiplier=1., fee=7.0, total_amt=-186250., trans_date=1053605468.54) p2 = position.Position(symbol="AAPL", id="1225", qty=500, price=184.00, multiplier=1., fee=7.0, total_amt=62000., trans_date=1021236990.02) plist = [p0, p1, p2] plist.sort() assert plist[0]==p2 assert plist[1]==p0 assert plist[2]==p1 #### EOF ####################################################################
def portfolio_holding_fifo2_test(): """ test of 'fifo' and 'lifo' queuing by adding and removing a position. Note: there are no protections on 'removing' a position which is in the wrong direction. ## TODO: decide when to make determination of add or remove -- in the class or when deciding to call the appropriate method? """ p1 = position.Position(symbol="AAPL", qty=1000, price=185.25, multiplier=1., fee=7.0, total_amt=185250., trans_date=1053605468.54) p2 = position.Position(symbol="AAPL", qty=1500, price=184.00, multiplier=1., fee=7.0, total_amt=276000., trans_date=1054202245.63) p3 = position.Position(symbol="AAPL", qty=-500, price=186.00, multiplier=1., fee=7.0, total_amt=279000., trans_date=1055902486.22) h = portfolio.Holding() h.add_to(p1) h.add_to(p2) h.remove_from(p3, order='fifo') assert h.qty==2000 assert len(h.positions)==2 # simple check to make sure the positions that we expect are left over... p = h.positions[0] assert p.price==185.25 p = h.positions[1] assert p.price==184.00
def __init__(self, mapType, line): fragments = line.split(":") # [zoom, bottomLeftCorner, topRightCorner] zoom = fragments[0].split("-") self.minZoom = int(zoom[0]) self.maxZoom = int(zoom[1]) southWest = position.Position(fragments[1]) northEast = position.Position(fragments[2]) self.latitudeRange = position.LatitudeRange(southWest, northEast) self.longitudeRange = position.LongitudeRange(southWest, northEast) self.mapType = mapType
def fetch_positions(pages): positions = list() for p in pages: soup = BeautifulSoup(p.text, 'html.parser') title_div = soup.select('div.js_result_container div.jobTitle') date_div = soup.select('div.js_result_container div.postedDate') location_div = soup.select('div.js_result_container div.location') company_div = soup.select('div.js_result_container div.company') for t, d, l, c in zip(title_div, date_div, location_div, company_div): title = t.find('span').text if t.find('span') else '' url = t.find('a').get('href') if t.find('a') else '' date = parse(d.find('time')['datetime']) if d.find('time')['datetime'] else '' city = l.find('meta', {'itemprop': 'addressLocality'}) city = city['content'] if city else '' state = l.find('meta', {'itemprop': 'addressRegion'}) state = state['content'] if state else '' company = c.find('a')['title'] if c.find('a') else '' title = title.encode('unicode-escape').lower() url = url.encode('unicode-escape').lower() company = company.encode('unicode-escape').lower() city = city.encode('unicode-escape').lower() state = state.encode('unicode-escape').lower() p = pos.Position(title, url, date, company, city, state) positions.append(p) return positions
def __init__(self): """the init function""" self.hello_ = "hello world!" # self.grid_ = grid.GridFile("gridFile_1209.data") self.position_ = position.Position() web.header('Content-Type', 'text/html')
def test_fen_infos(self): """Test the info generation from FEN input. Defines a helper class to temporarily replace sys.stdout in order to capture the output of test_position with the expected output. """ class MyOutput: def __init__(self): self.data = [] def write(self, s): self.data.append(s) def clear(self): self.data = [] def __str__(self): return "".join(self.data) stdout_org = sys.stdout # Preserve current state of stdout. my_stdout = MyOutput() for fen, info in self.FEN_INFO: try: sys.stdout = my_stdout test_position = position.Position(fen) test_position.print_info() self.assertEqual(str(my_stdout), info) my_stdout.clear() finally: sys.stdout = stdout_org # Restore stdout state.
def findInitPosition(self): for line in self.laby: for x in line: if x == '8': """the 8 it's Macgyver""" return Position.Position(self.laby.index(line), line.index(x))
def moveActivePiceBack(self, active_piece, passive_piece, at_position, to_position, gameboard): if passive_piece is None: del gameboard.board_state[to_position] else: gameboard.board_state[to_position] = passive_piece gameboard.board_state[at_position] = active_piece active_piece.position = position.Position(at_position)
def test_normal_wifi_config_file(self): """load the normal wifi config file""" wifi_config = position.Position() # print wifi_config.wifi_cfg_ # print len(wifi_config.wifi_cfg_) self.assertEqual(4, len(wifi_config.wifi_cfg_))
def test_knight_calculate_scope(self): knight_fen = 'N3k3/6n1/8/8/4n3/1PN3P1/P1PPPP1P/4K2N w - - 0 1' knight_pos = position.Position(knight_fen) knight_a8 = piece.PieceFactory.create('N', (0, 0)) knight_g7 = piece.PieceFactory.create('n', (1, 6)) knight_e4 = piece.PieceFactory.create('n', (4, 4)) knight_c3 = piece.PieceFactory.create('N', (5, 2)) knight_h1 = piece.PieceFactory.create('N', (7, 7)) self.assertCountEqual(knight_a8.calculate_scope(knight_pos), [(1, 2), (2, 1)]) self.assertCountEqual(knight_g7.calculate_scope(knight_pos), [(3, 7), (3, 5), (2, 4)]) self.assertCountEqual(knight_e4.calculate_scope(knight_pos), [(5, 6), (6, 5), (6, 3), (5, 2), (3, 2), (2, 3), (2, 5), (3, 6)]) self.assertCountEqual(knight_c3.calculate_scope(knight_pos), [(7, 3), (7, 1), (4, 0), (3, 1), (3, 3), (4, 4)]) self.assertCountEqual(knight_h1.calculate_scope(knight_pos), [])
def move_chess(self, str_command): [r1, c1, r2, c2] = str_command.split(',', 4) pass_flag1 = 0 # 检验(r1,c1)是否有属于此player的棋子 for piece_iter in board.Board.piecepool: if piece_iter.position.r == int(r1) and piece_iter.position.c == int(c1) and \ piece_iter.owner == self.player_number: pass_flag1 = 1 break # 检验(r2,c2)是否可以放下此棋子(这一格没有自己的棋子) pass_flag2 = 1 for piece_iter_1 in board.Board.piecepool: if piece_iter_1.position.r == int( r2) and piece_iter_1.position.c == int(c2): if piece_iter_1.owner == self.player_number: pass_flag2 = 0 break # 如果(r1,c1)处有自己的棋子,且(r2,c2)没有自己的棋子则判断(r2,c2)是否有敌方棋子 if pass_flag1 == 1 and pass_flag2 == 1: for piece_iter_1 in board.Board.piecepool: if piece_iter_1.position.r == int( r2) and piece_iter_1.position.c == int(c2): if piece_iter_1.owner != self.player_number: # 当(r2,c2)处有敌方棋子时,吃掉该棋子 board.Board.piecepool.remove(piece_iter_1) break # 将自己移到(r2,c2)处 for piece_iter in board.Board.piecepool: if piece_iter.position.r == int( r1) and piece_iter.position.c == int(c1): piece_iter.position = position.Position(int(r2), int(c2)) break else: print('你并没有在本回合做出有效动作') os.system('pause')
def test_pawn_calculate_scope(self): pawn_fen = ( 'rn1qkb1r/4pppp/2p3B1/pp1pP3/P3b1n1/NP3P1P/2PP2P1/R1BQK1NR ' 'w KQkq d6 0 10') pawn_pos = position.Position(pawn_fen) pawn_a4 = piece.PieceFactory.create('P', (4, 0)) pawn_b3 = piece.PieceFactory.create('P', (5, 1)) pawn_c2 = piece.PieceFactory.create('P', (6, 2)) pawn_e5 = piece.PieceFactory.create('P', (3, 4)) pawn_f3 = piece.PieceFactory.create('P', (5, 5)) pawn_g2 = piece.PieceFactory.create('P', (6, 6)) pawn_h3 = piece.PieceFactory.create('P', (5, 7)) pawn_a5 = piece.PieceFactory.create('p', (3, 0)) pawn_c6 = piece.PieceFactory.create('p', (2, 2)) pawn_d5 = piece.PieceFactory.create('p', (3, 3)) pawn_f7 = piece.PieceFactory.create('p', (1, 5)) self.assertCountEqual(pawn_a4.calculate_scope(pawn_pos), [(3, 1)]) self.assertCountEqual(pawn_b3.calculate_scope(pawn_pos), [(4, 1)]) self.assertCountEqual(pawn_c2.calculate_scope(pawn_pos), [(5, 2), (4, 2)]) self.assertCountEqual(pawn_e5.calculate_scope(pawn_pos), [(2, 4), (2, 3)]) self.assertCountEqual(pawn_f3.calculate_scope(pawn_pos), [(4, 5), (4, 4), (4, 6)]) self.assertCountEqual(pawn_g2.calculate_scope(pawn_pos), [(5, 6)]) self.assertCountEqual(pawn_h3.calculate_scope(pawn_pos), [(4, 7), (4, 6)]) self.assertCountEqual(pawn_a5.calculate_scope(pawn_pos), []) self.assertCountEqual(pawn_c6.calculate_scope(pawn_pos), [(3, 2)]) self.assertCountEqual(pawn_d5.calculate_scope(pawn_pos), [(4, 3)]) self.assertCountEqual(pawn_f7.calculate_scope(pawn_pos), [(2, 5), (3, 5), (2, 6)])
def test_bishop_calculate_scope(self): bishop_fen = 'b7/4kbq1/b7/1n6/2RB4/1n5B/1K4P1/8 w - - 0 1' bishop_pos = position.Position(bishop_fen) bishop_a8 = piece.PieceFactory.create('b', (0, 0)) bishop_a6 = piece.PieceFactory.create('b', (2, 0)) bishop_a4 = piece.PieceFactory.create('b', (4, 0)) bishop_d4 = piece.PieceFactory.create('B', (4, 3)) bishop_h3 = piece.PieceFactory.create('B', (5, 7)) self.assertCountEqual(bishop_a8.calculate_scope(bishop_pos), [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)]) self.assertCountEqual(bishop_a6.calculate_scope(bishop_pos), [(1, 1), (0, 2)]) self.assertCountEqual(bishop_a4.calculate_scope(bishop_pos), []) self.assertCountEqual(bishop_d4.calculate_scope(bishop_pos), [(3, 2), (2, 1), (1, 0), (3, 4), (2, 5), (1, 6), (5, 4), (6, 5), (7, 6), (5, 2)]) self.assertCountEqual(bishop_h3.calculate_scope(bishop_pos), [(4, 6), (3, 5), (2, 4), (1, 3), (0, 2)])
def test_undo_move(self): fen = ('2rq1rk1/1b2bpp1/p2p1n1p/n1p1p1B1/Pp2P3/1NPP1N1P/1PB2PP1/' 'R2QR1K1 w - a3 0 1') moves_position = position.Position(fen) pawn_g2 = piece.PieceFactory.create('P', (6, 6)) bishop_g5 = piece.PieceFactory.create('B', (3, 6)) pawn_b4 = piece.PieceFactory.create('p', (4, 1)) knight_a5 = piece.PieceFactory.create('n', (3, 0)) queen_d8 = piece.PieceFactory.create('q', (0, 3)) move_data = moves_position.make_move(pawn_g2, (4, 6)) moves_position.undo_move(move_data) new_fen = moves_position.generate_fen() self.assertEqual(new_fen, fen) move_data = moves_position.make_move(bishop_g5, (2, 5)) moves_position.undo_move(move_data) new_fen = moves_position.generate_fen() self.assertEqual(new_fen, fen) move_data = moves_position.make_move(pawn_b4, (5, 0)) moves_position.undo_move(move_data) new_fen = moves_position.generate_fen() self.assertEqual(new_fen, fen) move_data = moves_position.make_move(knight_a5, (5, 1)) moves_position.undo_move(move_data) new_fen = moves_position.generate_fen() self.assertEqual(new_fen, fen) move_data = moves_position.make_move(queen_d8, (2, 1)) moves_position.undo_move(move_data) new_fen = moves_position.generate_fen() self.assertEqual(new_fen, fen)
def buildAvailPositionList(): positionList = [] for line in laby: for x in range(0, len(line)): if line[x] == " ": indexOfCurrentLine = laby.index(line) positionList.append(Position.Position(indexOfCurrentLine, x)) return positionList
def position_dates_test(): """ Test to see if I'm handling dates correctly """ p = position.Position(symbol="AAPL", id="1222", qty=1000, price=185.25, multiplier=1., fee=7.0, total_amt=185250., trans_date=1053605468.54) pass
def portfolio_port_test(): """ test of 'lifo' queuing by adding and removing a position. """ p1 = position.Position(symbol="AAPL", id="1110", qty=1000, price=185.25, multiplier=1., fee=7.0, total_amt=185250., trans_date=1053605468.54) p2 = position.Position(symbol="AAPL", id="1111", qty=1500, price=184.00, multiplier=1., fee=7.0, total_amt=276000., trans_date=1054202245.63) p3 = position.Position(symbol="GOOG", id="1112", qty=2000, price=286.00, multiplier=1., fee=7.0, total_amt=572000., trans_date=1055902486.22) h = portfolio.Holding() h.add_to(p1) h.add_to(p2) h2 = portfolio.Holding() h2.add_to(p3) portf = portfolio.Portfolio(name="Test Portfolio", holdings=[h, h2]) print dir(portf) print portf.holdings print portf.pprint assert len(portf.holdings) == 2 assert portf.holdings["GOOG"].symbol == "GOOG" return
def findExitPosition(self): for line in self.laby: for x in line: """the X is the guardian""" if x == 'X': return Position.Position(self.laby.index(line), line.index(x)) """exitPosition here becomes an instance of position (column, line)"""
def validatePawn(self, active_piece, at_position, to_position, unit_direction, gameboard): step = to_position.subtract(at_position) one_step, two_step, capture = self.convertStrPositionToObjPosition('a2', 'a3', 'b2') capture_left = capture.subtract(position.Position('c1')) capture_right = capture.subtract(position.Position('a1')) passive_piece = gameboard.getPieceAtPosition(str(to_position)) if active_piece.hasColor('black'): one_step = one_step.invert() two_step = two_step.invert() capture_left = capture_left.invert() capture_right = capture_right.invert() if (step == capture_left or step == capture_right) and passive_piece is not None: return True elif step == one_step and passive_piece is None: return self.checkSquaresForBlockingPiecesRecursive(at_position, to_position.add(unit_direction), unit_direction, gameboard) elif active_piece.start_position and step == two_step and passive_piece is None: return self.checkSquaresForBlockingPiecesRecursive(at_position, to_position.add(unit_direction), unit_direction, gameboard)
def goUp(self, laby): goingToPos = Position.Position(self.pos.line - 1, self.pos.column) self.willStepOnObject(goingToPos) if lm.charAtPosition(goingToPos) != "*" and self.pos.line > 0: lm.updatePersoPositionInLaby(self.pos, goingToPos) self.pos = goingToPos else: lm.showWarning()
def __init__(self, sock): with open(os.path.dirname(__file__) + '/adjectives') as f1: with open(os.path.dirname(__file__) + '/animals') as f2: adjs = f1.readlines() anims = f2.readlines() self.name = str(random.choice(adjs))[:-1] + ' ' + \ str(random.choice(adjs))[:-1] + ' ' + \ str(random.choice(anims))[:-1] self.level = 1 self.cycles = 0 self.group = [self.name] self.tmp_group = [] self.state = 'FirstIncant' self.broad_queue = deque([]) self.inventory = {'linemate': 0, 'deraumere': 0, 'sibur': 0, 'mendiane': 0, 'phiras': 0, 'thystame': 0, 'food': 10} random.seed(time.time()) self.fiability = 75 self.get_food = False self.look_counter = 0 self.sent_orders = deque([]) self.no_pop_commands = {'dead' : self.callback_die, 'message': self.callback_broadcast, 'eject:' : self.callback_eject, 'Current' : self.callback_level_up, 'Elevation' : self.callback_incant} self.state_dict = {'FirstIncant' : self.get_lv2, 'Hungry' : self.search_food, 'LFG' : self.search_group, 'PrepLead': self.prep_lead, 'PrepWork': self.prep_work, 'Leading' : self.lead_search, 'Gathering' : self.work_search, 'Incanting' : self.incanting, 'GoIncanting' : self.go_incant, 'end incant' : self.end_incant } self.answer_dict = {'Inventory' : self.inventory_answer, 'Look' : self.look_answer, 'Incantation' : self.incant_answer, 'EndIncant' : self.end_incant_answer, 'Forward': self.movement_answer, 'Left': self.movement_answer, 'Right': self.movement_answer, 'Take' : self.take_answer, 'Set' : self.set_answer, 'Connect_nbr' : self.connect_nbr_answer, 'Fork' : self.fork_answer, 'Broadcast': self.do_nothing, 'Eject' : self.do_nothing} self.sock = sock self.sock.start_connection() self.team, self.xMax, self.yMax = sock.team, int(sock.maxX), int(sock.maxY) self.rem_connects = int(sock.rem_connects) self.pos = pos.Position(0, 0, self.xMax, self.yMax, 'NORTH') self.pos._init_dir_dict() self.map = [[tile.Tile(x, y) for x in range(self.xMax)] for y in range(self.yMax)] self.map[0][0].inventory['player'] += 1
def goRight(self, laby): goingToPos = Position.Position(self.pos.line, self.pos.column + 1) self.willStepOnObject(goingToPos) if lm.charAtPosition(goingToPos) != "*" and self.pos.column < len( laby[self.pos.line]) - 1: lm.updatePersoPositionInLaby(self.pos, goingToPos) self.pos = goingToPos else: lm.showWarning()
def goUp(self, labyManager): """idem cf. goLeft for more explnantions""" goingToPos = Position.Position(self.pos.line - 1, self.pos.column) self.willStepOnObject(goingToPos, labyManager) if labyManager.charAtPosition(goingToPos) != "*" and self.pos.line > 0: labyManager.updatePersoPositionInLaby(self.pos, goingToPos) self.pos = goingToPos else: pass
def test_find_should_succeed_term_is_found(self): pos = self.c1.find(['add', 'A', 'B']) pos2 = self.c2.find(['add', 'A', 'B']) subst1 = substitutions.Substitution() subst2 = substitutions.Substitution([('B', 'A')]) subst3 = substitutions.Substitution([('B', 'A'), ('C', 'A')]) expectedPos1 = position.Position(subst1, [0, 1]) expectedPos2 = position.Position(subst2, [0, 2]) expectedPos3 = position.Position(subst3, [0, 1, 2]) expectedPos4 = position.Position(subst1, [0, 2, 1]) self.assertEqual(2, len(pos)) self.assertEqual(expectedPos1, pos[0]) self.assertEqual(expectedPos2, pos[1]) self.assertEqual(2, len(pos2)) self.assertEqual(expectedPos3, pos2[0]) self.assertEqual(expectedPos4, pos2[1])
def open_at_market(self, bar, islong, shares=1): p = position.Position(entry_bar=bar, entry_price=self.bars.Open.values[bar], is_long=islong, shares=shares) self.active_positions[hash(p)] = p return
def position_initialization_test(): """ Test to see if I can handle fields for which I provide no data. """ p = position.Position(symbol="AAPL", id="1221", qty=1000, price=185.25, multiplier=1., fee=7.0, total_amt=185250., trans_date=1053605468.54) assert p.description==""
def position_attribute_test(): """ Dead simple test to see if I can store data in my position object and get it back out """ p = position.Position(symbol="AAPL", id="1220", qty=1000, price=185.25, multiplier=1., fee=7.0, total_amt=185250., trans_date=1053605468.54) assert p.price==185.25
def __init__(self, piece_type, player_number, piece_position=position.Position()): self.piece_type = piece_type self.position = piece_position self.owner = player_number self.symbol = '@=' self.set_symbol(self.owner)