I'm looking for a more dynamic way to write the following statement.
data = [r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7], r[8], r[9], r[10], r[11], r[12], r[13]]
r
represents my response data coming in, to which I take that and enumerate the items into a dictionary. This works, but its too static. If data is added or removed, the API code then needs to be adjusted to account for it.
What is an intuitive way to handle this? Below is a sample of the full class for reference.
class Pots(Resource):
def get(self, store):
print('USAGE: Received a request at POTS for Store ' + store )
conn = sqlite3.connect('store-db.db')
cur = conn.cursor()
cur.execute('SELECT * FROM Pots WHERE StoreNumber like ' + store)
res = cur.fetchall()
if not res:
print("RESPONSE: No data was found for this request.")
return('No data was found', 404)
else:
for r in res:
column_names = ["StoreNumber", "ForwardOnFail", "HuntLine","FirePrimary","FireSecondary", "BurglarPrimary", "BurglarSecondary","BurglarTertiary", "DNR", "PassengerElevator", "FreightElevator", "Escalator1", "Escalator2","ShopperTrak"]
data = [r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7], r[8], r[9], r[10], r[11], r[12], r[13]]
datadict = {column_names[itemindex]:item for itemindex, item in enumerate(data)}
return(datadict, 200)
Edit: Please ignore any SQL inefficiencies. While I appreciate the advice, I am aware and do not intend to use this in production. Just for testing. :)