Tag Archives: coding

Python Snippet: Get Terminal Width

There is a wonderful command on UNIX systems to return the width of the current terminal. Enter  tput cols and see for yourself! Tput can actually provide a lot of different info by querying the terminfo database. Really all I’ve needed though is ‘cols’ for columns.

I was writing a Python CLI app, and wanted to format and print output in a nice table. To do that (without downloading a table printing module) I had to know how much space was available for printing. Can’t make a nice-looking set of data if every other line is being wrapped!

Here is a simple function that returns the width of whatever terminal is running the script. Subprocess is the most current built-in Python module for running system commands. It contains a selection of different functions each with many optional parameters, depending on the complexity of your needs. Using subprocess.check_output() is the way to go if you are looking to get back the output of a command. The  check_output() function has a whole slew of accepted arguments, but in this simple instance we need only one: a list containing command to run and the arguments to pass to it.

The first exception will be raised if the system returns an error when trying to execute the tput command. The second exception will be raised if the command (in our case tput) has a problem with the given argument ( cols). If nothing goes wrong, the output will be a nice whole number that you can use to determine exactly how many characters can be printed per line!

Have you ever needed to know the width of your terminal? If you have any questions please feel free to ask in the comments below!

Here’s a link to this code snippet on Github Gists: terminal_width.py

From Ruby to Python

For a while I’ve been thinking about switching to Python, especially when I see so many programs I like to use written in it. My favorite terminal Guake comes to mind first and of course my all-time favorite editor Sublime Text. The community around Ruby is so heavily focused on web development that other areas seem to be neglected. When I first started getting into coding, web seemed the way to go. I got caught up in the hype and forgot what my gut told me. I love CLI apps and sysadmin tasks. I’ve also found I really enjoy GUI toolkits.  As a language Ruby is really wonderful, but the Python ecosystem just seems like a better fit for me. In January I decided to take the plunge. I came down with something and was confined to bed for a week so it was the perfect time to spend learning. I’m so happy with the change already. I’m writing more and better code in Python than I ever did in Ruby. First step was a beginner’s tutorial. I started with the free Google Python class. I usually prefer purely reading material, but the videos were really enjoyable. Made me feel like I was back in a classroom environment again.

Python is amazing me every day with how many modules and packages are available. After doing a bit of scripting, I started picking up the Kivy framework. It’s an “Open source Python library for rapid development of applications that make use of innovative user interfaces, such as multi-touch apps.” Python makes me feel empowered to create any coding idea I can dream up. If you’re thinking of learning to code or want to try a new language, I strongly suggest checking out the list of tutorials on the Python wiki. Python is amazingly fun to write and so clean to read. White space as syntax makes for such simple and readable code. I’ve been using it barely over three weeks and already I want to rewrite all of my utility scripts from Ruby to Python.

Let me know in the comments if you have any questions about starting out in either Python or Ruby! And for those interested I have a large list of links to tutorials and useful learning materials on my Pinboard.

Public Code Number One

Today I finally released my first public code into the wild. I’ve been teaching myself programming, and throughout the learning process, my code has been in private repositories with Bitbucket. It was a struggle to feel confident enough to put anything of my own on Github, but I realized I have nothing to be afraid of. As a newcomer to the programming world, I’ve had what many refer to as impostor syndrome. Even though I’ve read style guidelines and paid close attention to how others publish code, there’s been an underlying fear of criticism without any constructive suggestions. What I realized is how silly that sounds. In my time browsing Github, I don’t recall thinking anyone was a total jerk. There have been disagreements, sure, but no trolling and certainly nothing like a lot of other places on the internet. I know my script works, so I added a license, a nice README.md, and pushed it out into the world!

The repo I pushed is a little script that checks whether or not a website can be reached. It then logs the result to a text file and, if provided with an email, sends an alert when the site is down. I have it set up to run as a cronjob on my home server. Every ten minutes it checks my site and that of my wonderful partner in everything, Kenzie. Her web host unfortunately put her site on a server that was having a lot of downtime problems. In order to get an idea of how bad the problem was, I thought it would be perfect to have a log we could refer to. The notification email wasn’t 100% necessary, but it seemed like a neat idea and I really wanted to learn how to send emails from my machine! I was right, it turned out to be a fun feature and I had a great time setting up my first Postfix server.

Overall I feel really good about this first step. It may just be a simple script, but it represents much more than that to me. A metaphorical door in my mind has been opened to feeling comfortable doing this again. There’s another project I’ve been working on that’s getting close to ready. This one, however, is a command-line app. That means it will also be released as a Gem, Ruby’s software packaging system. The code won’t just be hosted on Github, but also on rubygems.org. It would be usable by anyone with Ruby installed, simply using ‘gem install’. Makes me a bit more nervous about it, but I really like how the program is coming out and I’m actually getting pretty excited to share it!

Poetic Ruby Method

Here is a limerick for you.


I Forgot My Newline

This is a friendly reminder to always leave a blank line at the end of your text files. You might be thinking that it doesn’t really matter, and in many cases you’d probably be correct. However, there are also plenty of configuration files for apps that require it to be there. Those that do will most likely not tell you so it would be quite hard to find and fix the problem.

Technically, a text file is just a series of lines ending in a newline character “\n”. On very old systems, a file without the ending newline would not even be considered a text file. Operating systems and applications nowadays can handle this and still read the file, but not without problems. The most likely issue is that the last line will be completely ignored. Due to the missing newline, it’s not even considered a line of text. Hope the last line of your file wasn’t important!

A prime example is cron in Unix-like operating systems. Filling up the last line in a crontab and forgetting to add a newline at the end is one of the most common problems people run into when new to cron. That last command at the end of the crontab will simply not run.

I’ve run into this a few times in the past, so I decided to look it up and find out the deeper reasons behind programs expecting “\n”. I thought it would be interesting, and it was. I hope you’ve learned a useful tip, even if you don’t care about the reasons behind it.

If you want to be sure to never have this problem, check out Sublime Text, my favorite text editor for code and everything else. It has an option to force a newline at the end of a file on saving. Sweet! If you are already using Sublime Text, see my previous post on replacing the default icon with something much cooler.