Category Archives: Code

Python Snippet: Search for Files Recursively

Currently I’m working on a script to search for duplicate images on my hard drive. To do so, I need to search recursively and go down through all subdirectories.

Recursive searching can be done by importing the commonly used os module. Use the os.walk() function to recursively enumerate file names and paths in the supplied directory. There are then many ways to identify file types, but in this case I’m using fnmatch. Here’s the official fnmatch description:

This module provides support for Unix shell-style wildcards, which are not the same as regular expressions (which are documented in the re module).

We have the modules to use, now the code:

This function, called find_images(), returns an array of full file paths within a given directory that have a file type of ‘.jpg’. While searching, once an image is found we join the image name with root, the current directory being searched by os.walk(). This gives us back a list of full paths instead of just file names.

Example usage:

Hopefully I’ve explained the code clearly, but if not let me know. If you have any questions at all please feel free to ask in the comments.

Whenever you’re reading this, I hope you’re having a great day!

Basic Python Threading

The Python core module Threading provides a really simple way to add concurrency to any program. Concurrency is when a computer executes several instructions simultaneously. If your code waits for a function, but there are other things it could or should be doing in the meantime, try using threading. If threads aren’t quite enough and you feel the need to leverage multiple processors, use the multiprocessing module instead. It is more powerful but also has more overhead to load up.

There are many times where threading is useful, but none so common as a GUI-based program. Python is run sequentially so any little delay will hold up the user interface until the interpreter is done running the other code. For example, a label needs to update to show progress while files are downloaded. If a GUI button calls a function, the whole function has to finish before the interface can regain control and update the window.

Here’s a little example of a timer function that counts up to the specified number of seconds. This is perfect for threading in a GUI so a label can be updated with the current time. After defining the function, a thread is initialized and started up.

Note how quotes are needed around ‘timer_length’ in the keyword args hash in the Thread constructor, but not in the original function definition.

I haven’t tried any advanced threading setups yet, only basics like downloading emails with imaplib and running a timer for an indicator app. Let me know in the comments if you have any questions or if you thought of any cool uses of threading in your own code!

Rainbow Terminal Text With Lolcat

Like seeing rainbows all over the place? Does it make you especially happy to have a colorful terminal? Yeah, me too. There’s a little Ruby program called lolcat and it will help you out (in the happiness department).

lolcat_rainbow_terminal_colorful_pandora_cli_pianobar

Steps to a super colorful terminal:

  1. Install lolcat in one of two ways.
    1. As a Ruby gem using the standard Ruby utility.  gem install lolcat
    2. Through your system’s package manager.  sudo apt-get install lolcat
  2. Pipe your program through lolcat.
    • cat cool_file.txt | lolcat

That’s really it. The only problem I’ve come across is when piping through a program that is still in control of the terminal output.  My main example is with Pianobar, the Pandora CLI app. It doesn’t actually break anything, you just can’t see the last line of the terminal. This means the song progress can’t be seen, and it can be a problem if the user is expected to enter information. I’ve gotten used to it though, it’s really worth it for the colors!

Have fun with rainbows all up in your face!

git_help_lolcat_rainbow_colors_cli_terminal

 

Links: Have Fun Learning Python

This week has been a lot about making jewelry and a little about coding. Even still, I’ve found some great links for learning Python that I’d like to share.

  • Robot Game – This is a really fun way to practice Python once you have the basics down. You use the Robot Game API to write a robot script that competes with other coders’ robots! Upload as many robot ‘AI’ scripts as you’d like and they’ll fight it out in automated battles.
    • “Robot Game is a game for hackers. You write robots in Python, test them with our kit, and submit them to our server. You can challenge other robots yourself, or wait for the server to match you up automatically.”
  • Python Guide to Structuring Your Project – This guide really helped me understand the basics of project layout in Python. I found it by searching for the meaning and use of the __init__.py file I see so often.  What I discovered was a slew of other helpful tips about variable naming, object types, and importing modules. If you’re new to Python and looking to start your own project, this is an excellent guide to get you started.
  • ActiveState.com Popular Python Recipes – This is a great site for seeing how other people solve small problems. A lot of people give the advice of looking at the code for modules you use to basically see how experienced coders get it done. However, I find it’s often just as helpful to see an assortment of smaller solutions. Perhaps one of them will even give you an idea for your next project!
  • Pyvideo.org – Usually I don’t enjoy watching videos all that much as I generally prefer reading. I can set my pace and reread or stop on something to explore it further. However, sometimes I’m doing something that takes a bit more concentration (like making chainmaille!) and it’s great to throw on some content I can learn from. This collection is almost entirely videos from Python Conferences. The talks are often quite entertaining and on a broad variety of subjects. So if you’re not looking for a video course and instead just something to view for extra Python-to-brain exposure, check out this collection.
  • CoderByte – This site offers three difficulties of programming challenges to complete. It’s actually not just Python either. They offer challenges in Javascript, Python, PHP, Ruby, Java, C, and C++. The challenge is all done in the browser and is meant to test your ability to work with the different fundamentals of the language you choose. Easy and Medium challenges are timed and give bonus points for completing faster. Hard problems are not timed. Here I’ll list an example challenge description from each section:
    • [Easy] Palindrome – Determine if a string is written the same way forward and backwards.
    • [Medium] Array Addition – Determine if numbers in an array can add up to a certain  number in the array.
    • [Hard] Noughts Determiner – Determine the winning position of a Tic-tac-toe board.
  • Dungeons and Developers – This one isn’t specifically Python at all, but it’s a really fun concept that lays out the progression needed to become a web developer. The site displays a skill tree that looks like it could have come from any classic RPG game. Each part of the tree is relating to a different aspect of web development (ie Backend, Frontend, Design, Deployment). For each skill, there are links to tutorials that will help you learn the required knowledge to say “I know this stuff, I have this skill now.” It’s just a really fun and interactive way to challenge yourself and push toward a tangible learning goal.

Let me know if you use any of these and what you think of them. I’d also love to know about any similar resources I might like. I hope these can help you in your code-learning adventures!

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