Minor cleanup.
This commit is contained in:
parent
59b0241616
commit
be64b90652
|
|
@ -52,7 +52,6 @@ def init_db(conn):
|
||||||
'insert into populations(species, home_star, pop, planet) values (?, ?, ?, ?)',
|
'insert into populations(species, home_star, pop, planet) values (?, ?, ?, ?)',
|
||||||
(people_name, home_star_id, INITIAL_POP, planet_id),
|
(people_name, home_star_id, INITIAL_POP, planet_id),
|
||||||
)
|
)
|
||||||
# print(planet_id, bio, industrial_capacity)
|
|
||||||
c.close()
|
c.close()
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -59,7 +59,7 @@ def life_grows(db_cursor):
|
||||||
else:
|
else:
|
||||||
excess_bio = 0
|
excess_bio = 0
|
||||||
|
|
||||||
bio = bio + new_bio
|
bio += new_bio
|
||||||
assert bio <= bio_capacity
|
assert bio <= bio_capacity
|
||||||
print(
|
print(
|
||||||
f'setting planet {planet_id} bio to {bio} increment of {new_bio}, excess_bio {excess_bio}'
|
f'setting planet {planet_id} bio to {bio} increment of {new_bio}, excess_bio {excess_bio}'
|
||||||
|
|
|
||||||
48
stars.py
48
stars.py
|
|
@ -72,11 +72,7 @@ def init_db(conn):
|
||||||
print('Generating star data.')
|
print('Generating star data.')
|
||||||
c = conn.cursor()
|
c = conn.cursor()
|
||||||
for values in generate_stars(WIDTH, HEIGHT, MINIMUM_DISTANCE_BETWEEN_STARS):
|
for values in generate_stars(WIDTH, HEIGHT, MINIMUM_DISTANCE_BETWEEN_STARS):
|
||||||
try:
|
|
||||||
c.execute('insert into stars(x, y, radius, name) values (?, ?, ?, ?)', values)
|
c.execute('insert into stars(x, y, radius, name) values (?, ?, ?, ?)', values)
|
||||||
except sqlite3.IntegrityError:
|
|
||||||
print(values) # "Rubbpo"
|
|
||||||
continue
|
|
||||||
generate_planets_for_star(c)
|
generate_planets_for_star(c)
|
||||||
c.close()
|
c.close()
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
@ -91,39 +87,21 @@ def iter_stars(conn):
|
||||||
|
|
||||||
def get_planets_for_star_named(conn, name):
|
def get_planets_for_star_named(conn, name):
|
||||||
c = conn.cursor()
|
c = conn.cursor()
|
||||||
c.execute('select id from stars where "name" = ?', (name,))
|
|
||||||
(star_id,) = c.fetchone()
|
|
||||||
c.execute(
|
c.execute(
|
||||||
'select ordo, bio_capacity, industrial_capacity from planets'
|
'''\
|
||||||
' where "star" = ?'
|
select
|
||||||
' order by ordo'
|
planets.ordo,
|
||||||
' limit 29', # Because we only have 29 Roman numbers.
|
planets.bio_capacity,
|
||||||
(star_id,),
|
planets.industrial_capacity
|
||||||
|
from planets join stars
|
||||||
|
on stars.id = planets.star
|
||||||
|
and stars.name = ?
|
||||||
|
order by planets.ordo
|
||||||
|
limit 29
|
||||||
|
''', # Limit because we only have 29 Roman numbers.
|
||||||
|
(name,)
|
||||||
)
|
)
|
||||||
# Note that fetchone() returns a tuple value suitable for passing
|
# TODO: move the display-related code to ui module?
|
||||||
# to execute() as the values tuple. I destructure the result tuple
|
|
||||||
# and build a new one soley because it would be slightly obscure to
|
|
||||||
# do otherwise. I might use a variable named, say, star_id_tuple or
|
|
||||||
# star_id_row and pass it directly to execute(), or even go full
|
|
||||||
# cowboy and put the call to fetchone() in the parameter position in
|
|
||||||
# the call to execute() and eschew a variable altogether. But if
|
|
||||||
# somehow the query is made for a star name that isn't in the DB then
|
|
||||||
# the traceback would be unnecessarily unclear.
|
|
||||||
#
|
|
||||||
# In the event, having gone to this length to explain the situation
|
|
||||||
# I'll probably come back and switch to reusing the result tuple.
|
|
||||||
# I hate the waste. I bet if you looked at the bytecode the work is
|
|
||||||
# there: unpacking and repacking the id int. Bleah. Then again,
|
|
||||||
# this is running in response to a user event and the extra work is
|
|
||||||
# done once and it's not a lot compared to all the other work that's
|
|
||||||
# about to happen (drawing these planet data into the GUI) so it
|
|
||||||
# would be daft to worry about it (after working this all out, that
|
|
||||||
# is.)
|
|
||||||
#
|
|
||||||
# Now that I can do JOIN's I could write a mighty SQL statement that
|
|
||||||
# did all the work in the DB! I could be like the old programmers of
|
|
||||||
# yore!
|
|
||||||
|
|
||||||
for ordo, bio_capacity, industrial_capacity in c.fetchall():
|
for ordo, bio_capacity, industrial_capacity in c.fetchall():
|
||||||
assert bio_capacity >= 0
|
assert bio_capacity >= 0
|
||||||
assert industrial_capacity >= 0
|
assert industrial_capacity >= 0
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue