def move( templatePath ,skip=False): """ スクリーンキャプチャからテンプレート画像の位置にマウスを移動する @param templatePath {String} テンプレート画像。クリックする対象の画像 @param skip=False {Boolean} テンプレート画像が見つからなかった時に例外にするかどうか. Trueだと例外は投げられない @return {Boolean} 移動をスキップしたかどうか. skipしてればTrue = moveしたらFalse """ point = getPoint( templatePath, skip ) if not point: return True core.move( point ) return False
def move(templatePath, skip=False): """ スクリーンキャプチャからテンプレート画像の位置にマウスを移動する @param templatePath {String} テンプレート画像。クリックする対象の画像 @param skip=False {Boolean} テンプレート画像が見つからなかった時に例外にするかどうか. Trueだと例外は投げられない @return {Boolean} 移動をスキップしたかどうか. skipしてればTrue = moveしたらFalse """ point = getPoint(templatePath, skip) if not point: return True core.move(point) return False
def move(self, state, choice): unlocked_exits = [ exit_name for (exit_name, (req_key, dest)) in state.location.exits.items() if req_key is None ] direc = choice(unlocked_exits) st = move(state, direc) assert st.location_name == state.location.exits[direc][1] return st
def click(templatePath, skip=False): """ スクリーンキャプチャからテンプレート画像の位置をクリックします @param templatePath {String} テンプレート画像。クリックする対象の画像 @param skip=False {Boolean} テンプレート画像が見つからなかった時に例外にするかどうか. Trueだと例外は投げられない @return {Boolean} クリックをスキップしたかどうか. skipしてればTrue = clickしたらFalse """ point = getPoint(templatePath, skip) if not point: return True core.click(point) #マウスの位置がそのままだと、画面が変化しマウスオーバーが発生したりするので逃がす core.move((5, 5)) #気休め #これを入れとくと精度が上がる気がする time.sleep(0.2) return False
def click( templatePath ,skip=False): """ スクリーンキャプチャからテンプレート画像の位置をクリックします @param templatePath {String} テンプレート画像。クリックする対象の画像 @param skip=False {Boolean} テンプレート画像が見つからなかった時に例外にするかどうか. Trueだと例外は投げられない @return {Boolean} クリックをスキップしたかどうか. skipしてればTrue = clickしたらFalse """ point = getPoint( templatePath, skip ) if not point: return True core.click( point ) #マウスの位置がそのままだと、画面が変化しマウスオーバーが発生したりするので逃がす core.move( (5,5) ) #気休め #これを入れとくと精度が上がる気がする time.sleep( 0.2 ) return False
def getSolution(board): moves = [] rows = len(board) cols = len(board[0]) while(1): madeMove = False for x in range(cols): for y in range(rows): pts, newBoard = floodfill(board,board[y][x],x,y) if pts > 1: moves.append((x,y)) board = move(gravity(newBoard)) madeMove = True if not madeMove: break return moves
def sync(args): starttime = time.time() excludes = [fnmatch.translate('.pysync')] if args.excludes: excludes += map(fnmatch.translate, args.excludes) source_archive = get_archive(args.source) source_index = core.create_index(args.source, excludes=excludes, archive=source_archive) elapsedtime = datetime.timedelta(seconds=time.time() - starttime) print 'Loaded index with {} files in {}.'.format(len(source_index.files), elapsedtime) target_archive = get_archive(args.target) target_index = core.create_index(args.target, excludes=excludes, archive=target_archive) elapsedtime = datetime.timedelta(seconds=time.time() - starttime) print 'Loaded index with {} files in {}.'.format(len(target_index.files), elapsedtime) changeset = source_index.compare(target_index) print 'Enter command:' apply_regex = re.compile( 'apply (?P<location>source|target) (?P<pattern>.*)(?:[\s\n\r]*)') revert_regex = re.compile( 'revert (?P<location>source|target) (?P<pattern>.*)(?:[\s\n\r]*)') ignore_regex = re.compile( 'ignore (?P<location>source|target) (?P<pattern>.*)(?:[\s\n\r]*)') while True: try: line = raw_input('> ') except EOFError: line = '' if not line or line == 'exit': break # Command: show if line == 'show': print '{} new files:'.format(len(changeset.new_files)) for desc in changeset.new_files: print '\t{}'.format(desc.relpath) print '{} deleted files:'.format(len(changeset.removed_files)) for desc in changeset.removed_files: print '\t{}'.format(desc.relpath) print '{} changed files:'.format(len(changeset.file_changes)) for new, old in changeset.file_changes: print '\t{}'.format(old.relpath) print '{} moved files:'.format(len(changeset.file_moves)) for new, old in changeset.file_moves: print '\t{} -> {}'.format(old.relpath, new.relpath) continue # Command: apply match = apply_regex.match(line) if match: location = match.group('location') pattern = match.group('pattern') # Apply new files. if location == 'source': copy_fds = [ fd for fd in changeset.new_files if fnmatch.fnmatch(fd.relpath, pattern) ] for fd in copy_fds: source_path = os.path.join(args.source, fd.relpath) target_path = os.path.join(args.target, fd.relpath) print 'Copying {} to {}...'.format(source_path, target_path) changeset.new_files.remove(fd) if not args.dryrun: try: core.copy(source_path, target_path, overwrite=False) stat = os.stat(target_path) new_fd = core.FileDescriptor(relpath=fd.relpath, mtime=stat.st_mtime, size=stat.st_size, sha256=fd.sha256) target_archive.insert(new_fd) except EnvironmentError as e: sys.stderr.write('Could not copy {} to {}: {}\n', source_path, target_path, e) sys.stderr.flush() # Apply deleted files. if location == 'target': del_fds = [ fd for fd in changeset.removed_files if fnmatch.fnmatch(fd.relpath, pattern) ] for fd in del_fds: path = os.path.join(args.target, fd.relpath) print 'Deleting {}...'.format(path) changeset.removed_files.remove(fd) if not args.dryrun: try: os.remove(path) target_archive.delete(fd) except EnvironmentError as e: sys.stderr.write('Could not delete: {}\n', path, e) sys.stderr.flush() # Apply file changes. if location == 'source': match_index = 0 elif location == 'target': match_index = 1 changes = [ fds for fds in changeset.file_changes if fnmatch.fnmatch(fds[match_index].relpath, pattern) ] for source_fd, target_fd in changes: source_path = os.path.join(args.source, source_fd.relpath) target_path = os.path.join(args.target, target_fd.relpath) print 'Copying {} to {}...'.format(source_path, target_path) changeset.file_changes.remove((source_fd, target_fd)) if not args.dryrun: try: core.copy(source_path, target_path, overwrite=True) stat = os.stat(target_path) new_fd = core.FileDescriptor(relpath=target_fd.relpath, mtime=stat.st_mtime, size=stat.st_size, sha256=source_fd.sha256) target_archive.update(new_fd) except EnvironmentError as e: sys.stderr.write('Could not copy {} to {}: {}\n', source_path, target_path, e) sys.stderr.flush() # Apply file moves. if location == 'source': match_index = 0 elif location == 'target': match_index = 1 changes = [ fds for fds in changeset.file_moves if fnmatch.fnmatch(fds[match_index].relpath, pattern) ] for source_fd, target_fd in changes: from_path = os.path.join(args.target, target_fd.relpath) to_path = os.path.join(args.target, source_fd.relpath) print 'Moving {} to {}...'.format(from_path, to_path) changeset.file_moves.remove((source_fd, target_fd)) if not args.dryrun: try: core.move(from_path, to_path, overwrite=False) stat = os.stat(to_path) new_fd = core.FileDescriptor(relpath=source_fd.relpath, mtime=stat.st_mtime, size=stat.st_size, sha256=target_fd.sha256) target_archive.delete(target_fd) target_archive.insert(new_fd) except EnvironmentError as e: sys.stderr.write('Could not move {} to {}: {}\n', from_path, to_path, e) sys.stderr.flush() continue # Command: revert match = revert_regex.match(line) if match: location = match.group('location') pattern = match.group('pattern') # Revert deleted files. if location == 'target': copy_fds = [ fd for fd in changeset.removed_files if fnmatch.fnmatch(fd.relpath, pattern) ] for fd in copy_fds: source_path = os.path.join(args.source, fd.relpath) target_path = os.path.join(args.target, fd.relpath) print 'Copying {} to {}...'.format(target_path, source_path) changeset.removed_files.remove(fd) if not args.dryrun: try: core.copy(target_path, source_path, overwrite=False) stat = os.stat(source_path) new_fd = core.FileDescriptor(relpath=fd.relpath, mtime=stat.st_mtime, size=stat.st_size, sha256=fd.sha256) source_archive.insert(new_fd) except EnvironmentError as e: sys.stderr.write('Could not copy {} to {}: {}\n', target_path, source_path, e) sys.stderr.flush() # Revert new files. if location == 'source': del_fds = [ fd for fd in changeset.new_files if fnmatch.fnmatch(fd.relpath, pattern) ] for fd in del_fds: path = os.path.join(args.source, fd.relpath) print 'Deleting {}...'.format(path) changeset.new_files.remove(fd) if not args.dryrun: try: os.remove(path) source_archive.delete(fd) except EnvironmentError as e: sys.stderr.write('Could not delete {}: {}\n', path, e) sys.stderr.flush() # Apply file changes. if location == 'source': match_index = 0 elif location == 'target': match_index = 1 changes = [ fds for fds in changeset.file_changes if fnmatch.fnmatch(fds[match_index].relpath, pattern) ] for source_fd, target_fd in changes: source_path = os.path.join(args.source, source_fd.relpath) target_path = os.path.join(args.target, target_fd.relpath) print 'Copying {} to {}...'.format(target_path, source_path) changeset.file_changes.remove((source_fd, target_fd)) if not args.dryrun: try: core.copy(target_path, source_path, overwrite=True) stat = os.stat(source_path) new_fd = core.FileDescriptor(relpath=source_fd.relpath, mtime=stat.st_mtime, size=stat.st_size, sha256=target_fd.sha256) source_archive.update(new_fd) except EnvironmentError as e: sys.stderr.write('Could not copy {} to {}: {}\n', target_path, source_path, e) sys.stderr.flush() # Apply file moves. if location == 'source': match_index = 0 elif location == 'target': match_index = 1 changes = [ fds for fds in changeset.file_moves if fnmatch.fnmatch(fds[match_index].relpath, pattern) ] for source_fd, target_fd in changes: from_path = os.path.join(args.source, source_fd.relpath) to_path = os.path.join(args.source, target_fd.relpath) print 'Moving {} to {}...'.format(from_path, to_path) changeset.file_moves.remove((source_fd, target_fd)) if not args.dryrun: try: core.move(from_path, to_path, overwrite=False) stat = os.stat(to_path) new_fd = core.FileDescriptor(relpath=target_fd.relpath, mtime=stat.st_mtime, size=stat.st_size, sha256=source_fd.sha256) source_archive.delete(source_fd) source_archive.insert(new_fd) except EnvironmentError as e: sys.stderr.write('Could not move {} to {}: {}\n', from_path, to_path, e) sys.stderr.flush() continue print 'Unknown command.'
def move_through_locked_door(self, state, choice): direc = choice(eligible_locked_exits(state)) st = move(state, direc) assert st.location_name == state.location.exits[direc][1] return st
def test_move_with_key(): in_home_with_key = in_street_with_key.set(location_name="Home") expected_state = in_home_with_key.set(location_name="Basement") assert move(in_home_with_key, "down") == expected_state
def test_move(): assert move(initial_state, "east") == in_street
def test_move_without_key(): assert move(initial_state, "down") is None
def test_move_invalid(): assert move(initial_state, "inward") is None