It would need to be r[1], which is apparently the correct field of the row.
But also importantly, OP has extracted year, month, and day for a reason. That reason would need to be addressed in a “proper” cleanup. You’ve just compiled it into a date object.
Oh yep, typo. As for the date object, I just don't know a more elegant solution without regex. That said, it sounds like the date format syntax doesn't take a wildcard so that's the biggest caveat I've noticed with this approach. The post just ignores separators so that's a pretty significant oversight on my part.
Oh, I agree it's a better approach from a general standpoint. It's more readable and more maintainable.
However: despite the str(r[1]) littering the code, which should be hoisted, if OP needed only 3 strings, a 4-digit year, a 2-digit month, and a 2-digit day... presuming date format is consistent in the database, the approach OP used is objectively faster than parsing a date string and then reconstituting each field.
There may be a niche case for that processing speed. Unlikely, though.
I once used an API that gave a date back as an integer in the form '20220102' (2022-01-02), but I needed it in Unix time - a pain to convert, but at least node packages made it easier
for r in c.execute('''SELECT * from `entries`'''):
year = str(r[1])[0] + str(r[1])[1] + str(r[1])[2] + str(r[1])[3]
month = str(r[1])[5] + str(r[1])[6]
day = str(r[1])[8] + str(r[1])[9]
65
u/[deleted] Jan 02 '22
Take a drink for every CPU cycle the code wastes! I'm just kidding, that would be incredibly dangerous.
23
u/[deleted] Jan 02 '22 edited Jan 02 '22
Ignoring query optimizations, proper cleanup would look something like this right?:
date = datetime.strptime(r[1], '%Y/%m/%d')edit: fixed typo
10
u/[deleted] Jan 02 '22
I once used an API that gave a date back as an integer in the form '
20220102' (2022-01-02), but I needed it in Unix time - a pain to convert, but at least node packages made it easier6
u/Werzaz Jan 02 '22
Image Transcription: Code
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
14
u/trollsmurf Jan 02 '22
Still, not too bad. The query takes much longer than the array mangling anyway.
3
u/colei_canis Jan 02 '22
SIGKILL more like, no time to wait for a graceful termination with code like this!
2
u/SuitableDragonfly Jan 02 '22
Hope you didn't store any dates prior to the year 1000.
2
u/recycle4science Jan 03 '22
Hopefully they don't add a column to that table...
1
u/yard2010 Jan 02 '22
Commit a SIGTERM
1
u/[deleted] Jan 03 '22
kill -TERM 1Pull the plug.