Blacken some code.

This commit is contained in:
Simon Forman 2024-04-15 15:32:16 -07:00
parent 8dd8ed9bca
commit 54bfd924b8
4 changed files with 61 additions and 44 deletions

28
data.py
View File

@ -29,14 +29,6 @@ conn = None
TABLES = [
'''\
create table populations (
id INTEGER PRIMARY KEY AUTOINCREMENT,
species TEXT,
pop INTEGER,
planet INTEGER,
FOREIGN KEY(planet) REFERENCES planets(id)
)''',
'''\
create table stars (
id INTEGER PRIMARY KEY AUTOINCREMENT,
@ -45,13 +37,13 @@ TABLES = [
radius INTEGER,
name TEXT UNIQUE
)''',
('''\
(
'''\
create table planets (
id INTEGER PRIMARY KEY,
ordo INTEGER,''' # The order from the star, counting from 1.
# I use "ordo" instead of "order" because "order" is a keyword
# in SQL (or at least it caused a syntax error in SQLite.)
# These capacities are fixed and reflect the conditions on the
# planet in a very abstract way. Bio and Industry should be
# low/high near and far from the star and high/low in the mid-
@ -60,11 +52,19 @@ TABLES = [
bio INTEGER DEFAULT 0,
industrial_capacity INTEGER,
industry INTEGER DEFAULT 0,
'''
'''star INTEGER,
star INTEGER,
FOREIGN KEY(star) REFERENCES stars(id)
)'''),
]
)'''
),
'''\
create table populations (
id INTEGER PRIMARY KEY AUTOINCREMENT,
species TEXT,
pop INTEGER,
planet INTEGER,
FOREIGN KEY(planet) REFERENCES planets(id)
)''',
]
def initialize_db_tables(filename):

View File

@ -29,11 +29,13 @@ def init_db(conn):
# Find the not-too-crappy planets.
c = conn.cursor()
c.execute('''\
c.execute(
'''\
select id, bio, industrial_capacity
from planets
where bio > 10000 and industrial_capacity > 10000
''')
'''
)
# Note that bio is initialized to full capacity whereas industry is 0.
# Select some and set initial population and industry.
@ -42,8 +44,14 @@ def init_db(conn):
bio -= INITIAL_POP # We know bio is above 10000 from the query above.
ind = min(INITIAL_POP, industrial_capacity)
people_name = get_name_of_planets_star(c, planet_id) + 'ians'
c.execute('update planets set bio = ?, industry = ? where id = ?', (bio, ind, planet_id))
c.execute('insert into populations(species, pop, planet) values (?, ?, ?)', (people_name, INITIAL_POP, planet_id))
c.execute(
'update planets set bio = ?, industry = ? where id = ?',
(bio, ind, planet_id),
)
c.execute(
'insert into populations(species, pop, planet) values (?, ?, ?)',
(people_name, INITIAL_POP, planet_id),
)
print(planet_id, bio, industrial_capacity)
c.close()
conn.commit()
@ -51,12 +59,13 @@ def init_db(conn):
def get_name_of_planets_star(db_cursor, planet_id):
# My first ever JOIN statement! Squeeeee!
db_cursor.execute('''\
db_cursor.execute(
'''\
select stars.name from stars
join planets
on stars.id = planets.star
and planets.id = ?
''',
(planet_id,)
(planet_id,),
)
return db_cursor.fetchone()[0]

View File

@ -34,12 +34,15 @@ def turn():
BIO_GROWTH = 0.03
POP_GROWTH = 0.0125
def life_grows(db_cursor):
print('life_grows')
db_cursor.execute('''
db_cursor.execute(
'''
select id, bio, bio_capacity from planets
where bio > 0 and bio < bio_capacity
''')
'''
)
planets = list(db_cursor.fetchall())
# Get all the results before updating some in this loop.
for planet_id, bio, bio_capacity in planets:
@ -58,17 +61,21 @@ def life_grows(db_cursor):
bio = bio + new_bio
assert bio <= bio_capacity
print(f'setting planet {planet_id} bio to {bio} increment of {new_bio}, excess_bio {excess_bio}')
print(
f'setting planet {planet_id} bio to {bio} increment of {new_bio}, excess_bio {excess_bio}'
)
db_cursor.execute('update planets set bio = ? where id = ?', (bio, planet_id))
def population_grows(db_cursor):
print('population_grows')
db_cursor.execute('''
db_cursor.execute(
'''
select populations.id, populations.pop, planets.id, planets.bio
from populations join planets
on populations.planet = planets.id
''')
'''
)
# and populations.home_star = planets.star
# People are only legally allowed to reproduce in their home
# stellar systems.
@ -91,6 +98,7 @@ def population_grows(db_cursor):
def industry_produces():
print('industry_produces')
def vessels_move():
print('vessels_move')

View File

@ -98,7 +98,7 @@ def get_planets_for_star_named(conn, name):
' where "star" = ?'
' order by ordo'
' limit 29', # Because we only have 29 Roman numbers.
(star_id,)
(star_id,),
)
# Note that fetchone() returns a tuple value suitable for passing
# to execute() as the values tuple. I destructure the result tuple
@ -138,7 +138,7 @@ def get_planets_for_star_named(conn, name):
def generate_stars(width, height, minimum_distance_between_stars):
unique_names = set()
for (x, y) in poisson(width, height, minimum_distance_between_stars):
for x, y in poisson(width, height, minimum_distance_between_stars):
name = generate_name()
while name in unique_names:
name = generate_name()
@ -168,7 +168,8 @@ def generate_planets_for_star(c):
for ordo in range(1, how_many_planets() + 1):
bio_capacity = round(lognormvariate(10, 5))
bio = bio_capacity if has_life() else 0
c.execute('insert into planets'
c.execute(
'insert into planets'
'(ordo, star, bio_capacity, bio, industrial_capacity)'
' values (?, ?, ?, ?, ?)',
(
@ -177,6 +178,5 @@ def generate_planets_for_star(c):
bio_capacity,
bio,
round(lognormvariate(10, 5)),
),
)
)