Ask HN: What are your useful code snippets?

2 points by eamag 3 months ago

When I start a jupyter notebook (especially in a new environment) I almost always paste

  %load_ext autoreload
  %autoreload 2
  %load_ext autotime
  %config Completer.use_jedi = False
  import pandas as pd
  pd.options.display.max_columns = 30
What are your useful code snippets or setup configs you reuse?
mindcrime 3 months ago

It's a little bit bigger than a "snippet" but probably smaller than a typical "library". But I have a little bit of code I reuse quite frequently for server applications. I have server implementations in Java and Python and a Java client.

So what is it? I call it "stoppable". Basically in the main() of the service I wrap everything in a while (!stopped) where stopped is a static boolean variable that defaults to "false". But before entering this loop, I start a thread that just loops, listening on a socket for a message reading "STOP". If it finds that message, it toggles the stopped variable to true.

Then in my systemd unit file for the service, the stop command just becomes something like:

java -jar stoppable-client.jar -p $SOME_PORT

It's simple but works well for my simple use-cases. There's no authentication or anything at the moment, so not really something I would use for production use in an important app; but in my context it's something I use for little POC / prototype services, or random little things I run for my own purposes on my private network.