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 (?, ?, ?, ?)',
|
||||
(people_name, home_star_id, INITIAL_POP, planet_id),
|
||||
)
|
||||
# print(planet_id, bio, industrial_capacity)
|
||||
c.close()
|
||||
conn.commit()
|
||||
|
||||
|
|
|
|||
|
|
@ -59,7 +59,7 @@ def life_grows(db_cursor):
|
|||
else:
|
||||
excess_bio = 0
|
||||
|
||||
bio = bio + new_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}'
|
||||
|
|
|
|||
52
stars.py
52
stars.py
|
|
@ -72,11 +72,7 @@ def init_db(conn):
|
|||
print('Generating star data.')
|
||||
c = conn.cursor()
|
||||
for values in generate_stars(WIDTH, HEIGHT, MINIMUM_DISTANCE_BETWEEN_STARS):
|
||||
try:
|
||||
c.execute('insert into stars(x, y, radius, name) values (?, ?, ?, ?)', values)
|
||||
except sqlite3.IntegrityError:
|
||||
print(values) # "Rubbpo"
|
||||
continue
|
||||
c.execute('insert into stars(x, y, radius, name) values (?, ?, ?, ?)', values)
|
||||
generate_planets_for_star(c)
|
||||
c.close()
|
||||
conn.commit()
|
||||
|
|
@ -91,39 +87,21 @@ def iter_stars(conn):
|
|||
|
||||
def get_planets_for_star_named(conn, name):
|
||||
c = conn.cursor()
|
||||
c.execute('select id from stars where "name" = ?', (name,))
|
||||
(star_id,) = c.fetchone()
|
||||
c.execute(
|
||||
'select ordo, bio_capacity, industrial_capacity from planets'
|
||||
' where "star" = ?'
|
||||
' order by ordo'
|
||||
' limit 29', # Because we only have 29 Roman numbers.
|
||||
(star_id,),
|
||||
)
|
||||
# Note that fetchone() returns a tuple value suitable for passing
|
||||
# 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!
|
||||
|
||||
'''\
|
||||
select
|
||||
planets.ordo,
|
||||
planets.bio_capacity,
|
||||
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,)
|
||||
)
|
||||
# TODO: move the display-related code to ui module?
|
||||
for ordo, bio_capacity, industrial_capacity in c.fetchall():
|
||||
assert bio_capacity >= 0
|
||||
assert industrial_capacity >= 0
|
||||
|
|
|
|||
Loading…
Reference in New Issue