Some Bits: Nelson's log

Wanted: simple persistent apps

What’s an easy way to build a Python app that grabs data from somewhere every few minutes, persists it, and then does some analysis of it?

I’m building a new thing that gets some data from Tweets and stores it. The main loop is downloading new tweets for a group of users and storing details of those tweets. It’s not very complicated. Structurally it’s similar to my lolslackbot project.

And I’m building it the same bad way. Persistence via sqlite with hand-rolled SQL statements. This is tedious, error prone, and makes upgrading / changing things difficult. Testing is also a huge PITA.

Is there some modern alternative that’s better?

My first thought is to at least use some sort of ORM. Django or SQLAlchemy, presumably. I’ve done a little Django and it’s hard to love it. Also ORMs cause problems, although maybe not at the small scale I’m working at.

I’m wondering if there’s some other fancier / funkier persistence system I should consider. I kind of want the simplicity and flexibility that an object database or maybe a NoSQL system promises. But those don’t seem to work out well in practice.

Comments

native JSON field in a database that supports it? e.g. postgres or bigquery:

https://www.postgresql.org/docs/9.6/static/datatype-json.html
https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators#json-functions

they support structured query operations to filter, sort, etc. on individual fields inside the JSON. usually comes with a performance hit, but i'm guessing you wouldn't care.

alternatively you could use schema inference to generate tables from the JSON data automatically: https://cloud.google.com/bigquery/docs/schema-detect . i've used it in a few projects (e.g. http://www.indiemap.org/) and it works well. makes it trivial to iterate on the schema, just wipe the table and recreate with new JSON data.

cloud-accessible dbs like bigquery also make BI/analysis/viz pretty easy. http://www.metabase.com/ on heroku is a good cheap option.

Thanks! sqlite's json1 is also pretty good, or one could go a full JSON store like Mongo or TinyDB. But storing the source JSON blobs from Twitter isn't solving the problem, and it's too much data to efficiently do the query. I want to store processed objects and relationships. That's what object databases were supposed to do, but that tech is nearly a dead end.

yup, understood. that was pretty much exactly my use case with indie map: storing and analyzing objects and relationships. i looked around for a simple graph or object db, and found candidates, but nothing that felt reasonable. in the end i just went relational, and used schema inference to iterate quickly.

Just checking: your data is too large/needs more filtering than just using pythons built in `shelve`, right?

https://docs.python.org/3/library/shelve.html

I've used it for very small/janky scripts. Sounds like this might be a tiny bit larger in scope.

How much data is involved here, within some order of N tweets/hour?

A few thousand tweets / hour. But I'm prototyping with a smaller dataset, so right now convenience is better than scale.

I've been playing with SQLAlchemy and trusting the ORM and so far it's kind of going OK.