本文整理匯總了Python中common.print_utils.Table類的典型用法代碼示例。如果您正苦於以下問題:Python Table類的具體用法?Python Table怎麽用?Python Table使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Table類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __report_system_threats
def __report_system_threats(self):
"""Print a table with system threats to the logfile."""
current_turn = fo.currentTurn()
if current_turn >= 100:
return
threat_table = Table([
Text('System'), Text('Vis.'), Float('Total'), Float('by Monsters'), Float('by Fleets'),
Float('by Planets'), Float('1 jump away'), Float('2 jumps'), Float('3 jumps')],
table_name="System Threat Turn %d" % current_turn
)
universe = fo.getUniverse()
for sys_id in universe.systemIDs:
sys_status = self.systemStatus.get(sys_id, {})
system = universe.getSystem(sys_id)
threat_table.add_row([
system,
"Yes" if sys_status.get('currently_visible', False) else "No",
sys_status.get('totalThreat', 0),
sys_status.get('monsterThreat', 0),
sys_status.get('fleetThreat', 0),
sys_status.get('planetThreat', 0),
sys_status.get('neighborThreat', 0.0),
sys_status.get('jump2_threat', 0.0),
sys_status.get('jump3_threat', 0.0),
])
info(threat_table)
示例2: log_planet_count_dist
def log_planet_count_dist(sys_list):
planet_count_dist = {}
planet_size_dist = {size: 0 for size in planets.planet_sizes}
for system in sys_list:
planet_count = 0
for planet in fo.sys_get_planets(system):
this_size = fo.planet_get_size(planet)
if this_size in planets.planet_sizes:
planet_count += 1
planet_size_dist[this_size] += 1
planet_count_dist.setdefault(planet_count, [0])[0] += 1
planet_tally = sum(planet_size_dist.values())
count_distribution_table = Table(
[Text('planets in system'), Text('num of systems'), Float('% of systems', precession=1)],
table_name='Planet Count Distribution'
)
for planet_count, sys_count in planet_count_dist.items():
count_distribution_table.add_row((planet_count, sys_count[0], 100.0 * sys_count[0] / len(sys_list)))
count_distribution_table.print_table()
print
size_distribution = Table(
[Text('size'), Text('count'), Float('% of planets', precession=1)],
table_name='Planet Size Distribution'
)
for planet_size, planet_count in sorted(planet_size_dist.items()):
size_distribution.add_row((planet_size, planet_count, 100.0 * planet_count / planet_tally))
size_distribution.print_table()
print
示例3: log_planets
def log_planets():
universe = fo.get_universe()
planets_table = Table(
[Text('id'), Text('name'), Text('system'), Text('type'),
Sequence('specials'), Text('species'), Sequence('buildings')],
table_name='Planets summary')
# group planets by system
for sid in fo.get_systems():
for pid in fo.sys_get_planets(sid):
planet = universe.getPlanet(pid)
planet_type = fo.planet_get_type(pid).name
planet_size = fo.planet_get_size(pid).name
if planet_type != planet_size:
planet_type = '%s %s' % (planet_type, planet_size)
buildings = [universe.getBuilding(x).name for x in planet.buildingIDs]
planets_table.add_row([
pid, planet.name, planet.systemID, planet_type, list(planet.specials), planet.speciesName, buildings
])
# Printing too much info at once will lead to truncation of text
for line in planets_table.get_table().split('\n'):
print line
示例4: test_empty_table
def test_empty_table():
empty = Table(
[
Text("name", description="Name for first column"),
Float("value", description="VValue"),
Sequence("zzz"),
Sequence("zzzzzzzzzzzzzzzzzz"),
],
table_name="Wooho",
)
assert empty.get_table() == EXPECTED_EMPTY_TABLE
示例5: print_resources_priority
def print_resources_priority():
"""Calculate top resource priority."""
universe = fo.getUniverse()
empire = fo.getEmpire()
empire_planet_ids = PlanetUtilsAI.get_owned_planets_by_empire(universe.planetIDs)
print "Resource Priorities:"
resource_priorities = {}
aistate = get_aistate()
for priority_type in get_priority_resource_types():
resource_priorities[priority_type] = aistate.get_priority(priority_type)
sorted_priorities = resource_priorities.items()
sorted_priorities.sort(lambda x, y: cmp(x[1], y[1]), reverse=True)
top_priority = -1
for evaluation_priority, evaluation_score in sorted_priorities:
if top_priority < 0:
top_priority = evaluation_priority
print " %s: %.2f" % (evaluation_priority, evaluation_score)
# what is the focus of available resource centers?
print
warnings = {}
foci_table = Table([
Text('Planet'),
Text('Size'),
Text('Type'),
Text('Focus'),
Text('Species'),
Text('Pop')
], table_name="Planetary Foci Overview Turn %d" % fo.currentTurn())
for pid in empire_planet_ids:
planet = universe.getPlanet(pid)
population = planet.currentMeterValue(fo.meterType.population)
max_population = planet.currentMeterValue(fo.meterType.targetPopulation)
if max_population < 1 and population > 0:
warnings[planet.name] = (population, max_population)
foci_table.add_row([
planet,
planet.size,
planet.type,
"_".join(str(planet.focus).split("_")[1:])[:8],
planet.speciesName,
"%.1f/%.1f" % (population, max_population)
])
info(foci_table)
print "Empire Totals:\nPopulation: %5d \nProduction: %5d\nResearch: %5d\n" % (
empire.population(), empire.productionPoints, empire.resourceProduction(fo.resourceType.research))
for name, (cp, mp) in warnings.iteritems():
print "Population Warning! -- %s has unsustainable current pop %d -- target %d" % (name, cp, mp)
示例6: log_systems
def log_systems():
universe = fo.get_universe()
systems_table = Table(
[Text('id'), Text('name'), Sequence('planets'), Sequence('connections'), Text('star')],
table_name='System summary')
for sid in fo.get_systems():
system = universe.getSystem(sid)
systems_table.add_row([
sid, system.name, fo.sys_get_planets(sid), fo.sys_get_starlanes(sid), system.starType.name
])
# Printing too much info at once will lead to truncation of text
for line in systems_table.get_table().split('\n'):
print line
示例7: log_planet_type_summary
def log_planet_type_summary(sys_list):
planet_type_summary_table = {k: 0 for k in planets.planet_types}
for system in sys_list:
for planet in fo.sys_get_planets(system):
planet_type_summary_table[fo.planet_get_type(planet)] += 1
planet_total = sum(planet_type_summary_table.values())
type_summary_table = Table(
[Text('planet type', align='<'), Float('% of planets', precession=1)],
table_name='Planet Type Summary for a total of %d placed planets' % planet_total
)
for planet_type, planet_count in sorted(planet_type_summary_table.items()):
type_summary_table.add_row((planet_type.name, 100.0 * planet_count / planet_total))
print type_summary_table
print
示例8: log_specials_summary
def log_specials_summary():
special_placement_count_table = Table(
[Text('special'), Text('times placed')],
table_name="Special Placement Summary"
)
for special in sorted(specials_summary):
special_placement_count_table.add_row([special, specials_summary[special]])
special_placement_count_table.print_table()
print
special_placement = Table(
[Text('count'), Text('tally'), Float('% of objects', precession=1)],
table_name="Specials Count(Repeat) Distribution"
)
objects_tally = sum(specials_repeat_dist.values())
for number, tally in specials_repeat_dist.items():
special_placement.add_row((number, tally, 100.0 * tally / (1E-10 + objects_tally)))
special_placement.print_table()
print
示例9: report_system_threats
def report_system_threats(self):
if fo.currentTurn() >= 100:
return
universe = fo.getUniverse()
sys_id_list = sorted(universe.systemIDs) # will normally look at this, the list of all known systems
current_turn = fo.currentTurn()
# assess fleet and planet threats
threat_table = Table([
Text('System'), Text('Vis.'), Float('Total'), Float('by Monsters'), Float('by Fleets'),
Float('by Planets'), Float('1 jump away'), Float('2 jumps'), Float('3 jumps')],
table_name="System Threat Turn %d" % current_turn
)
defense_table = Table([
Text('System Defenses'), Float('Total'), Float('by Planets'), Float('by Fleets'),
Float('Fleets 1 jump away'), Float('2 jumps'), Float('3 jumps')],
table_name="System Defenses Turn %d" % current_turn
)
for sys_id in sys_id_list:
sys_status = self.systemStatus.get(sys_id, {})
system = universe.getSystem(sys_id)
threat_table.add_row([
system,
"Yes" if sys_status.get('currently_visible', False) else "No",
sys_status.get('totalThreat', 0),
sys_status.get('monsterThreat', 0),
sys_status.get('fleetThreat', 0),
sys_status.get('planetThreat', 0),
sys_status.get('neighborThreat', 0.0),
sys_status.get('jump2_threat', 0.0),
sys_status.get('jump3_threat', 0.0),
])
defense_table.add_row([
system,
sys_status.get('all_local_defenses', 0.0),
sys_status.get('mydefenses', {}).get('overall', 0.0),
sys_status.get('myFleetRating', 0.0),
sys_status.get('my_neighbor_rating', 0.0),
sys_status.get('my_jump2_rating', 0.0),
sys_status.get('my_jump3_rating', 0.0),
])
threat_table.print_table()
defense_table.print_table()
示例10: __report_last_turn_fleet_missions
def __report_last_turn_fleet_missions(self):
"""Print a table reviewing last turn fleet missions to the log file."""
universe = fo.getUniverse()
mission_table = Table(
[Text('Fleet'), Text('Mission'), Text('Ships'), Float('Rating'), Float('Troops'), Text('Target')],
table_name="Turn %d: Fleet Mission Review from Last Turn" % fo.currentTurn())
for fleet_id, mission in self.get_fleet_missions_map().items():
fleet = universe.getFleet(fleet_id)
if not fleet:
continue
if not mission:
mission_table.add_row([fleet])
else:
mission_table.add_row([
fleet,
mission.type or "None",
len(fleet.shipIDs),
CombatRatingsAI.get_fleet_rating(fleet_id),
FleetUtilsAI.count_troops_in_fleet(fleet_id),
mission.target or "-"
])
info(mission_table)
示例11: log_monsters_summary
def log_monsters_summary(monster_freq):
monster_place_table = Table([Text('monster'), Text('count')], table_name='Monster placement')
for monster, counter in sorted(monsters_summary):
monster_place_table.add_row([monster, counter])
monster_place_table.print_table()
print
monster_chance = universe_tables.MONSTER_FREQUENCY[monster_freq]
monster_table = Table(
[Text('monster'), Float('chance'), Text('attempts'),
Text('number placed'), Text('number valid sys locs'), Text('number valid nest planet locs')],
table_name=("Space Monsters Placement Summary\n"
"Tracked Monster and Nest Summary (base monster freq: %4.1f%%)" % (100 * monster_chance))
)
for monster in sorted(tracked_monsters_summary):
monster_table.add_row(
(monster, tracked_monsters_chance[monster],
tracked_monsters_tries[monster], tracked_monsters_summary[monster],
tracked_monsters_location_summary[monster], tracked_nest_location_summary[monster])
)
monster_table.print_table()
print
示例12: __report_system_defenses
def __report_system_defenses(self):
"""Print a table with system defenses to the logfile."""
current_turn = fo.currentTurn()
if current_turn >= 100:
return
defense_table = Table([
Text('System Defenses'), Float('Total'), Float('by Planets'), Float('by Fleets'),
Float('Fleets 1 jump away'), Float('2 jumps'), Float('3 jumps')],
table_name="System Defenses Turn %d" % current_turn
)
universe = fo.getUniverse()
for sys_id in universe.systemIDs:
sys_status = self.systemStatus.get(sys_id, {})
system = universe.getSystem(sys_id)
defense_table.add_row([
system,
sys_status.get('all_local_defenses', 0.0),
sys_status.get('mydefenses', {}).get('overall', 0.0),
sys_status.get('myFleetRating', 0.0),
sys_status.get('my_neighbor_rating', 0.0),
sys_status.get('my_jump2_rating', 0.0),
sys_status.get('my_jump3_rating', 0.0),
])
info(defense_table)
示例13: split_new_fleets
def split_new_fleets(self):
"""Split any new fleets (at new game creation, can have unplanned mix of ship roles)."""
universe = fo.getUniverse()
mission_table = Table([Text('Fleet'), Text('Mission'), Text('Ships'), Float('Rating'), Float('Troops'), Text('Target')],
table_name="Turn %d: Fleet Mission Review from Last Turn" % fo.currentTurn())
for fleet_id, mission in self.get_fleet_missions_map().items():
fleet = universe.getFleet(fleet_id)
if not fleet:
continue
if not mission:
mission_table.add_row([fleet])
else:
mission_table.add_row([
fleet,
mission.type or "None",
len(fleet.shipIDs),
CombatRatingsAI.get_fleet_rating(fleet_id),
FleetUtilsAI.count_troops_in_fleet(fleet_id),
mission.target or "-"
])
mission_table.print_table()
# TODO: check length of fleets for losses or do in AIstat.__cleanRoles
known_fleets = self.get_fleet_roles_map()
self.newlySplitFleets.clear()
fleets_to_split = [fleet_id for fleet_id in FleetUtilsAI.get_empire_fleet_ids() if fleet_id not in known_fleets]
if fleets_to_split:
print "Splitting new fleets"
for fleet_id in fleets_to_split:
fleet = universe.getFleet(fleet_id)
if not fleet:
print >> sys.stderr, "After splitting fleet: resulting fleet ID %d appears to not exist" % fleet_id
continue
fleet_len = len(list(fleet.shipIDs))
if fleet_len == 1:
continue
new_fleets = FleetUtilsAI.split_fleet(fleet_id) # try splitting fleet
print "\t from splitting fleet ID %4d with %d ships, got %d new fleets:" % (fleet_id, fleet_len, len(new_fleets))
示例14: __clean_fleet_roles
def __clean_fleet_roles(self, just_resumed=False):
"""Removes fleetRoles if a fleet has been lost, and update fleet Ratings."""
universe = fo.getUniverse()
current_empire_fleets = FleetUtilsAI.get_empire_fleet_ids()
self.shipCount = 0
destroyed_object_ids = universe.destroyedObjectIDs(fo.empireID())
fleet_table = Table([
Text('Fleet'), Float('Rating'), Float('Troops'),
Text('Location'), Text('Destination')],
table_name="Fleet Summary Turn %d" % fo.currentTurn()
)
# need to loop over a copy as entries are deleted in loop
for fleet_id in list(self.__fleetRoleByID):
fleet_status = self.fleetStatus.setdefault(fleet_id, {})
rating = CombatRatingsAI.get_fleet_rating(fleet_id, self.get_standard_enemy())
old_sys_id = fleet_status.get('sysID', -2) # TODO: Introduce helper function instead
fleet = universe.getFleet(fleet_id)
if fleet:
sys_id = fleet.systemID
if old_sys_id in [-2, -1]:
old_sys_id = sys_id
fleet_status['nships'] = len(fleet.shipIDs) # TODO: Introduce helper function instead
self.shipCount += fleet_status['nships']
else:
# can still retrieve a fleet object even if fleet was just destroyed, so shouldn't get here
# however,this has been observed happening, and is the reason a fleet check was added a few lines below.
# Not at all sure how this came about, but was throwing off threat assessments
sys_id = old_sys_id
# check if fleet is destroyed and if so, delete stored information
if fleet_id not in current_empire_fleets: # or fleet.empty:
# TODO(Morlic): Is this condition really correct? Seems like should actually be in destroyed object ids
if (fleet and self.__fleetRoleByID.get(fleet_id, -1) != -1 and
fleet_id not in destroyed_object_ids and
any(ship_id not in destroyed_object_ids for ship_id in fleet.shipIDs)):
if not just_resumed:
fleetsLostBySystem.setdefault(old_sys_id, []).append(max(rating, MilitaryAI.MinThreat))
self.delete_fleet_info(fleet_id)
continue
# if reached here, the fleet does still exist
this_sys = universe.getSystem(sys_id)
next_sys = universe.getSystem(fleet.nextSystemID)
fleet_table.add_row([
fleet,
rating,
FleetUtilsAI.count_troops_in_fleet(fleet_id),
this_sys or 'starlane',
next_sys or '-',
])
fleet_status['rating'] = rating
if next_sys:
fleet_status['sysID'] = next_sys.id
elif this_sys:
fleet_status['sysID'] = this_sys.id
else:
error("Fleet %s has no valid system." % fleet)
info(fleet_table)
# Next string used in charts. Don't modify it!
print "Empire Ship Count: ", self.shipCount
print "Empire standard fighter summary: ", CombatRatingsAI.get_empire_standard_fighter().get_stats()
print "------------------------"
示例15: test_simple_table
def test_simple_table():
t = Table(
[
Text("name", description="Name for first column"),
Float("value", description="VValue"),
Sequence("zzz"),
Sequence("zzzzzzzzzzzzzzzzzz"),
],
table_name="Wooho",
)
t.add_row(["hello", 144444, "abcffff", "a"])
t.add_row([u"Plato aa\u03b2 III", 21, "de", "a"])
t.add_row([u"Plato \u03b2 III", 21, "de", "a"])
t.add_row(["Plato B III", 21, "d", "a"])
t.add_row(["Plato Bddddd III", 21, "d", "a"])
t.add_row(["Plato III", 21, "d", "a"])
assert t.get_table() == EXPECTED_SIMPLE_TABLE