I'm using direnv in my project, and sometimes want to set secrets such as AWS_ACCESS_KEY_ID and friends. I usually have a file like: export AWS_ACCESS_KEY_ID=ABCDEFGHIJK export AWS_SECRET_ACCESS_KEY=ABCDEFGHIJK/lmnopqrstuvwxyz export AWS_DEFAULT_REGION=us-east-1 However I don't want to store these values in the clear. The solution I found was to encrypt the file with gpg and then have the…
I'm writing a lot of Go & Python these days, and both affect the way I write code. One feature I really like in Go is defer . Lately, after writing some complicated pytest fixtures , I tried to make simpler and use a "defer" like fixture. Here's what I came with: Out of the context of pytest, the same idea can become a context manager: Update: I've published pytest-defer to PyPI.
PEP 562 added support for module level __dir__ and __getitem__ . __dir__ is called when the built-in dir function is called on the module __getattr__ is called when an attribute is not found via the regular attribute lookup Let's use this to build an environment based configuration module. Conviruation values has a value, environment key and a function to convert from str to right type I'm going…
Let's say you'd like to check that a string is a valid floating point (e.g. "3.14", ".2", "-2.718" ...). One common techinque to solve these kind of problems is to write a state machine . In our case, the state machine is: Instead of writng the state machine a one big function with a lot of state and a bunch of "if" statements, we're going to write states as functions. To simplify things, the end…
I like quotes and have an extensive list stored in a text file. I was toying with the idea of making a twitter bot that will post a quote per day. Reading that Google cloud is now offering a Secret Manager rekindled this idea. The result is @quotamiki (which you should follow ;) It posts one quote per day (9am Asia/Jerusalem). It even has a simple web site . The code was simple to write, I like…
Typically, you'll read configuration from files (such as YAML) and get them as a dictionary. However in Python you'd like to write config.httpd.port and not config['httpd']['port'] __getattr__ is a hook method that's called by Python when regular attribute lookup fails (not to be confused with the lower level __getattribute__ , which is much harder to work with). You can use it to wrap the…
Go has a nice present tool which let's you present code and run it. I wanted to use it in my lightning talk at GopherCon Israel . The talk is a quiz and I wanted a timer to show on each quiz slide. present syntax supports .html directive which lets you add any HTML code to the slide. From there it easy to add a timer. I wrote timer.html below and added .html timer.html to the first slide. Here's…
Some API's return complex structures. Here's for an example from Stocktwits API . Instead of creating a specific type for every element in the structre, we use use a anonymous structure and define only the fields we're intrested in. In our example we'd like to see the symbols most mentioned for a given symbol (in our case AAPL). And the output: $ go run stocktwits.go SPY 10 TSLA 7 AMZN 5 BTC.X 5…
These days, I write a lot of Go and a lot of Python (and some other languages in the mix). What's great is that I'm getting ideas from both ecosystems and find myself more productive. In the Go world, I like Kelsey Hightower's envconfig for a simple application configuration from environment variables. I couldn't find something similar in the Python world, so I wrote viaenv which uses variable…
Python's dict has a get method . It'll either return an existing value for a given key or return a default value if the key is not in the dict. It's very tempting to write code like val = d.get(key, Object()) , however you need to think about the performance implications. Since function arguments are evaluated before calling the function, this means the a new Object will be created regardless if…
Go's concurrency unit is a goroutine, the Go runtime multiplexes goroutines to operating system (OS) threads. At an upper level, the OS maps threads to CPUs (or cores). To see this goroutine/thread migration, you'll need to use some C code (note that this is Linux specific). If you run this code and sort the output, you'll see the workers moving between cores: $ go run affinity.go | sort -u…
For ages my shell is greeting me with " Let's boogie, Mr Swine ". I had it printed in fixed offset but following a discussion in comp.lang.python I decided to get the screen size and calculate the exact location. I'm using tput to get the size and the fact that printf can get the width of the string as a parameter if you specify a * there (BTW - Python supports that as well ) Here's the code.
353Solutions - 2018 in Review Happy new year! Here’s a summary of 353solution’s 2018. Numbers Total of 242 calendar days where worked for a customer (including partial days) Down 11 days from 2017 Out of 261 work days in 2018 Median consulting work day is 3:36h Workshops are usually a full day Normalized work days (total divided by 8) is 203.3 Up from 171.6 normalized days in 2017 Of these 53.5…
I use the command line a lot. Some projects require different settings, say Python virtual environment, GOPATH for installing go packages and more. I'm using direnv to help with settings per project in the terminal. For every project I have a .envrc file which specifies required settings, this file is automatically loaded once I change directory to the project directory or any of it's sub…
Sometimes you'd like more than one way to serve an API. In my case I'm currently working on serving both gRPC and HTTP. I'd like to have one place where objects are defined and have a nice way to serialize both from protobuf (which is the serialization gRPC uses) and JSON . When producing Go code, protobuf adds JSON struct tags. However since JSON comes from dynamic languages, fields can have any…
Say you have a function that converts text and you'd like to test it. You can write a directory with input and output and use pytest.parameterize to iterate over the cases. The problem is that the input and the output are in different files and it's not obvious to see them next to each other. If the text for testing is not that long, you can place all the cases in a configuration file. In this…
Sometimes you'd like to publish a simple Python script, but it depends on some external packages (e.g. requests ). The usual solution is to create a package, which is a great solution but requires some work and might not be suitable for internal packages. Another solution is to use PEX , which creates an executable virtual environment. The user running the script just needs a Python interpreter…
One of my clients asked me for a printable book to accompany one of my workshops. My teaching style is more fluid and we write a lot of code in class. I wanted a quick way to take all the source code files and some images and make a book out of them. Since I already work with markdown, I looked for a solution that can convert markdown to PDF. The winner was Pandoc (which can be installed with…
After some time with Xubuntu I decided to get back to Arch Linux . Arch have a command line based installer, the installation instructions are pretty good but for a laptop with UEFI I had to do some extra steps. Here's what I came up with, hope you'll find it useful as well. Most of my files are backed on "the cloud", my home directory with all the RC files is on a private git repository. Getting…
Python's built-in iter function is mostly used to extract an iterator from an iterable (if you're confused by this, see Raymond's excellent answer on StackOverflow). iter has a second form where it takes a function with not arguments and a sentinel value. This doesn't see that useful but check out the readline example in the documentation. iter will call the function repeatably until it the…
A little late, but here's a summary of 2017. Numbers Total of 255 calendar days where worked for a customer (including partial days) Up 62 days from 2016 Out of 260 work days in 2016 Median work day is 6:53h Normalized work days (total divided by 8) is 171.6 Up from 158.1 normalized days in 2016 Of these 30 days were in workshops and the rest in consulting 10 workshops Down from 18 last year First…
I'm having fun solving Advent of Code 2017 . Problem 8 reminded the power of Python's eval (and before you start commenting the "eval is evil" may I remind you of this :) You can check The Go implementation that don't have eval and need to work harder.
I'm teaching and consulting in Go a lot lately, and I work a lot with Python as well. There's a big trend of rewriting backend services in Go and to help people coming from the Python world I've created a Go →Python cheatsheet . The code in here , I'd love to hear if you have suggestion (via a PR ;)
In Go , every type has a zero value. Which is the value a variable of this type get if it's not initialized. I had a configuration object of type map[string]interface{} and I needed to check if value exists and is not a zero value. Here's a small piece of code that checks for zero values: