Hugo’s watching web server is great for creating pages. Tweak a little and see the browser refresh. (Unless you break things and then it is still just a manual browser refresh away from back to the good state, once your page is unbroken.) As I was developing local pages, I happened to have my realtime Google Anaytics open. I noticed I was hitting it. When I look back at yesterday’s stats, I have 124 visits to a page I was working on, but hadn’t published yet.
Note: This is the first method I used to create a series with Hugo. Later in the Hugo series, I changed to using taxonomies, which is a better way of accomplishing this. This post is left up for reference. Goal In converting my Pelican blog over to Hugo, I needed to learn a new templating system. I miss a few things that Pelican did, like categories based on folders and not front matter.
I’ve used Python as my main programming language for a few years. When my Word Press was hacked and defaced yet again, I finally decided to rebuild my website with a static generator and host on GitHub. I leaned towards Pelican, due to the Python base that I thought I could modify if I needed. And I hoped that I could get it to work easily. I did get everything converted over to .
One of the best ways to become efficient with Python is to really learn the standard libraries. However, do this with understanding that some are included for base functionality or compatibility, but have been superseded by better packages in PyPi. Requests over urllib2 is a great example of this. By being outside of Python, these packages can iterate faster and not be dependent on Python releases. Collections is not one that needed replacement.
You may have seen if __name__ == '__main__': at the bottom of a script. Why is this there and what does it mean? First, a python file is a module. They can be combined into a directory module and this is what is being done when you see an __init__.py file in the folder. We will get there in a little while, but first we will cover a little more than just __name__.
Iterators and Generators are two mechanisms for providing data to cycle through in Python. The difference is if the data is already in memory and we iterate along it, or if we are generating it as we go. The names actually make sense. These turn out to be very similar and just differ in execution. One is better if you already have a class. The other is a little simpler.
Context managers were added to Python with PEP 343. This allows proper handling of resources without worrying about missing something in your try/finally code. If you are not opening files using a context manager, you are most likely doing it wrong. Looking at opening a file and what could go wrong, will show how context managers make programming both more robust and simpler. The operation can be broken into 3 sections of code.
There are a whole bunch of underscores in Python. If you are new to Python, this may seem weird. There is a reason behind the flatness. There are even special names for them, under when you have a single underscore and dunder when two underscores are used (for double underscore). Unders There are many uses of single underscores in Python. We will hit a few common and easy to explain right away and get them over with.
If you came directly to this article, you might want to read my previous on in this series with the link at the bottom. We discussed the dangers of using mutable values for defaults in a function parameter. In this article I’ll discuss two things: using mutable defaults for good and decorators. In the end, combining these together. Using the default mutable for good In developing a Python workflow for a custom system, I’m interfacing with custom hardware to measure current and temperature.
Default arguments in Python allow you to make complex functions very easy to use. They can be called with mostly defaults or called with as much configuration as needed. This comes with a gotcha for those getting started in this. If you use mutable data structures as defaults, things don’t behave exactly as you would expect. However, before we start, lets cover some things you may not know that I will use in the code.