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

26
data.py
View File

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

View File

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

View File

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

View File

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