本文整理匯總了Python中database.memcached_database.MemcachedDatabase.set_world_size方法的典型用法代碼示例。如果您正苦於以下問題:Python MemcachedDatabase.set_world_size方法的具體用法?Python MemcachedDatabase.set_world_size怎麽用?Python MemcachedDatabase.set_world_size使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類database.memcached_database.MemcachedDatabase
的用法示例。
在下文中一共展示了MemcachedDatabase.set_world_size方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: World
# 需要導入模塊: from database.memcached_database import MemcachedDatabase [as 別名]
# 或者: from database.memcached_database.MemcachedDatabase import set_world_size [as 別名]
#.........這裏部分代碼省略.........
@param location: Location to try to add the robot. (x, y)
'''
for square_x, square_y in SquareInterator(location, self._size):
try:
square_object = self.get_square((square_x, square_y), for_update=True)
# Checking if something blocked this square.
if not square_object.is_blocking():
robot.set_location((square_x, square_y))
self._database.add_robot(robot, (square_x, square_y))
# Done.
return
except LockAlreadyAquiredError:
# If this square was locked, go to the next one.
continue
raise exceptions.WorldIsFullError("No free location is remained in the world!") # pragma: no cover
def move_robot(self, robot, destination):
'''Moves a robot to the specified location.
@param destination: A tuple of (x, y)
'''
# Locking both origin and destination.
origin = robot.get_location()
origin_square = self.get_square(origin, for_update=True)
destination_square = self.get_square(destination, for_update=True)
if destination_square.is_blocking():
raise exceptions.LocationIsBlockedError("Destination location {0} is blocked.".format(destination))
origin_square.set_robot_id(None)
destination_square.set_robot_id(robot.get_id())
robot.set_location(destination)
def plant(self, plant, location):
'''Plant a plant on the specified location.'''
square = self.get_square(location, for_update=True)
if square.get_type() != MapSquareTypes.SOIL:
raise exceptions.CannotPlantHereError("Cannot plant on {0} square type.".format(square.get_type()))
if square.get_plant() is not None:
raise exceptions.AlreadyPlantError("There's already a plant on {0}".format(location))
square.set_plant(plant)
def get_square(self, location, for_update=False):
'''Gets the square object of the specified location.
@param location: Location to get.
@param for_update: If you want to update this square (store its
changes back to the database) you should set this flag.
Note that it automatically updates the changes if transaction commits.
'''
return self._database.get_square(location, for_update=for_update)
def load_from_file(self, file_path):
'''Loads a world from the specified file.'''
with open(file_path, 'r') as world_file:
file_lines = world_file.readlines()
row_length = None
line_number = 0
for line in file_lines:
line = line.replace('\r', '').replace('\n', '')
if line.startswith("#") or line.isspace():
continue
line_number += 1
# Keeping length of the first line, so we can validate that all
# the lines have same length.
if row_length is None:
row_length = len(line)
if len(line) != row_length:
raise exceptions.InvalidWorldFileError("Length of line {0} is invalid.".format(line_number))
row = []
column_number = 0
for square_type in line:
column_number += 1
square_type = int(square_type)
if square_type not in (0, 1, 2, 3):
raise exceptions.InvalidWorldFileError(
"Found invalid square in line {0} column {1}.".format(line_number, column_number))
row.append(MapSquare(square_type, (column_number - 1, line_number - 1)))
self._database.add_square_row(row)
self._size = (row_length, line_number)
self._database.set_world_size(self._size)