Slackware – check installed packages with Python

Querying installed package is something I do every now and then. It’s usually needed to find out if all the necessary dependencies of a package have been met. On Slackware it is usually accomplished with a simple:

ls /var/log/packages | grep -i package

For example:

$ ls /var/log/packages | grep -i gconf
GConf-2.32.1-x86_64-1
perl-extutils-pkgconfig-1.12-x86_64-1_SBo

As a way of practising programming, I decided to simulate the above command in Python 3. Here’s what I’ve coded:


#!/usr/bin/env python3

import glob, os, sys

if len(sys.argv) != 2:
        '''Check if the user provided the required argument'''
        print('The {0} command takes exactly 1 argument!'.format(sys.argv[0]))
else:
        packages_directory = '/var/log/packages/'
        package = sys.argv[1].lower()
        for files in glob.glob(packages_directory + '*' + package + '*'):
            print(os.path.basename(files))

It might be slightly slower than using Bash but it does the job. As I’m not a seasoned Python programmer, there’ll probably be ways of optimising the code, which I obviously welcome. Feel free to provide feedback.

Python 3 shell in Emacs

If you’d like to run a Python 3 shell within Emacs you could issue the following command:

C-u M-x run-python

It’ll then prompt you to confirm which Python version you’d want to run. You can just type 3 and click Enter again. If you use it a lot, however, it’s not going to be the most convenient of the solutions. You could automate the whole process by adding the following 2 lines to your ~/.emacs file:

(defun run-python3 () (interactive) (run-python "python3"))
(global-set-key [f5] 'run-python3)

The second line assigns a keybinding to the command. In the example above it is the F5 key.