Some Bits: Nelson's log

5 posts in August 2017

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.

Alfred replacements for Windows

I loved Alfred on MacOS. 95% of the time I used it to launch apps with the keyboard, but I also liked its clipboard management, access to Google searches, etc. What about Windows? Turns out there’s lots of options. I cribbed these from this Reddit discussion, also see this article. Many of these are cross-platform and work on Mac and/or Linux.

Hain is pretty slick and what I just chose to switch to. It feels almost exactly like Alfred to me, the only thing it’s missing is number key shortcuts to pick items. It’s not very configurable (no skins for instance) but the defaults are good. I added some plugins: Clipboard (to replace Ditto), Google. The terrible / great thing about Hain is it’s implemented in Electron so there’s a full HTML5 shell running. Or several maybe. I see four hain.exe processes total about 200 MB of RAM. And 0.02% CPU which is small but not zero.

Zazu is newish and open source. It seems OK but not as polished as Hain; configuring it requires editing a JSON file, for instance. It’s also HTML5 based (Chrome?), 240MB in memory and 0.02% CPU.

Wox is popular and pretty solid. The install is strange; it says it requires you also install both everything (a file indexer) and Python3. But I think those are both optional upgrades. It’s a little ugly out of the box, Hain is nicer. Wox is 125 MB of RAM and 0.12% of CPU. It’s built in .NET.

Launchy is what I used to use. It’s old and ugly and doesn’t really do anything but launch apps. And that, not very well. Nothing to love but it does work and is free.

FARR is another option with a long history. I couldn’t figure out how to rebind the hotkey, and it’s kinda old and ugly too, but it seems very capable. Also quite small; 20 MB RAM and 0.00% CPU when idle, like it should be.

 

While I’m here, a shout-out to Seer which gives a space bar quick-preview to the Windows Explorer, like MacOS finder has. It’s about $12. Lightshot also seems like a nice improved screenshot-taker. It’s free, I think they’re trying to drive ad traffic to prntscr.com. (Speaking of which my Apple keyboard has no PrntScrn button. It does have an F13 though which you can bind in Lightshot, even if the UI shows a blank key when you bind it.)

Twitter API: extended tweets

tl;dr: if you’re writing new Twitter API code, be sure you’re requesting extended tweets.

Boy am I out of touch. Twitter did a major API change over a year ago to better support modern tweets that don’t really fit in 140 characters. I stumbled into this while trying to use the API to get the URLs for this tweet. My old code mostly worked but this tweet is long enough to trigger the need for extended tweets. My old code was seeing a URL for the tweet it was in reply to and not the bendbulletin.com URL.

The clue was that the tweet object also had “truncated: true” set on it. What is that? Happily I found this discussion which explained about extended tweets. Twitter also documented this thoroughly last year, it’s explained well in this document.

By default the Twitter API still returns “compatibility tweets” which strictly fit in 140 characters. And in other ways are pretty broken, or at least incomplete. You have to add a tweet_mode=extended to your request URL to get the new extended format. Just to complicate things this new format has some changes, most notably there is no more “text” field. Now it’s called “full_text”. There’s also some funky third format for streaming and Gnip APIs that has both formats in a single message.

I fixed my Tweepy code by adding tweet_mode=’extended’ to the Cursor constructor.

I can’t see any reason any new code shouldn’t immediately switch to using extended tweets forever and always. It’s a shame Twitter didn’t go with a version numbering scheme to change things like this in a neater way, although versions have problems too.

While I’m here, two grumbles about the Twitter API:

Twitter’s own web client no longer is using the JSON API for everything, some stuff is pre-rendered. This means you can’t just use their web app to crib examples of API calls from.

Twitter’s API console for interactive testing of requests is kind of nice. But they don’t format the JSON responses in a useful way! You just get one long-ass line of text you have to paste into a prettifier to make sense of.

Symantec VIP two factor auth

I just set up Symantec VIP two-factor authentication for my bank account, to replace an old VeriSign hardware token. Symantec VIP is a mobile phone app, they also sell a hardware token.

The basic security model is similar to the VeriSign token or TOTP as implemented by Google Authenticator. I type my password to log in, then am prompted to type a six digit second code from the Symantec VIP app. The code generated is valid for 30 seconds. I’m not positive it’s time-based; every time I generate a new token, it shows being valid for a full 30 seconds.

The Symantec VIP app has a very simple model of user identity. The first time I ran the app on my phone it assigned me a “Credential ID”. I then supply that to every single site that I want to use it for 2FA. That’s different from the Google Authenticator model where you scan a QR code to set up a new site. Not sure how Symantec supports having multiple identities or changing your ID. There is an option to scan a QR code, so maybe there’s a second identity model.

There’s no backup of the token; if I lose the phone or something I have to go back to my bank to set up access again, out of band.

Another slight weirdness; my bank says it will take 48 hours to enable the token. No explanation why it’d take that long. In the meantime they gave me a temporary 6 digit code that will work many times as my 2FA code. That’s not ideal but probably harmless, at least in this application.

Symantec VIP is more complex than I’ve seen. There’s a whole VIP Manager product for enterprise management, maybe that provides more flexibility and control for advanced users.

As an end user I prefer Google Authenticator, mostly because I’ve got it set up for several sites already. But Symantec VIP seems OK too. No idea how their backend integration tools compare. TOTP sounds awfully simple to deploy.

Update: one problem with the basic Symantec VIP model; my bank is relying on Symantec to keep the secret safe. They didn’t generate a new secret for me to share with them, they’re using whatever is baked into my default Symantec ID. That may be appropriate for many businesses that want to outsource security, but it’s a form of risk.

Update 2: the python-vipaccess tool works to generate new Symantec VIP tokens that you can then import into Authy.

Python metaprogramming

This Reddit discussion had me looking at a couple of metaprogramming tricks used in Python code.

The standard library’s namedtuple type is generated on the fly at runtime. When a new namedtuple is created the library generates it from a template string. It fills out the template with the necessary details and then passes the class definition to exec(). Exciting! But relatively straightforward and easy to understand. (Except for that extra bit to support pickling; yuck.)

pytest is an alternative unit testing library that has you just using the assert statement everywhere, no library functions. To report test failures in a nice way, it does some dark magic:

Reporting details about a failing assertion is achieved by rewriting assert statements before they are run. ... pytest rewrites test modules on import by using an import hook to write new pyc files

I haven’t read the pytest code but from the way that’s written my impression is they’re inspecting compiled bytecode and rewriting the assert statements to instrument them. That sounds a little scary, in particular if it fails it must be completely confusing. They do warn you that “if you are messing with import yourself, the import hook may interfere”. Update here’s a description of the code rewriting as of 2011.

That second technique reminds me a bit of the way that Protovis rewrote Javascript at load time so you could use more modern Javascript syntax than the browser supported. Mike abandoned that idea in D3, I think because it made things too complicated.