Filtered by Linux

Page 12

Reset

Grep results expanded

April 23, 2005
5 comments Python, Linux

I find myself often running a grep command with the -n which means that when it shows the filename it also shows the line number it matched it on. Then what I often do is that I open the found file with my editor and move to that line with the gotoline feature. Here's a cute little bin Python script that expands the result a little bit. Try:


$ grep -rin 'def set' *      
frintranet/sequence/tests/testSequence.py:26:    def setUp( self ):
slimmer/tests/testSlimmer.py:37:    def setUp(self):
slimmer/tests/testSlimmer.py~:37:    def setUp(self):
slimmer/tests/testSlimmer.py.bak:42:    def setUp(self):

Truncated! Read the rest by clicking the link below.

pg_class to check if table exists

April 20, 2005
4 comments Linux

I just learnt a better way to check if a PostgreSQL table exists. Before I did this to find out if table mytable is defined:


SELECT * FROM 'mytable' LIMIT 1;

But this had to be wrapped in exception catching application code because I was able to tell if the table existed if the select statement worked. That's the wrong and naive way. The correct way is to use pg_class and look at how many rows were returned:


SELECT relname FROM pg_class 
WHERE relname = 'mytable';

Why have I missed this before? No explanation?

A disadvantage with using pg_class is that it's not ISO standard, meaning that it's specific to PostgreSQL only. The first pattern will work on all relational databases.

Find print statements in Python code

April 12, 2005
6 comments Python, Linux

Do you, like me, use the powerful print function to debug your Python code? Do you, like me, sometimes forget to remove lines with print and ship this out to less development-like environments? Do you, like I did, need a way to check for forgotten print statements?

I've written a dumb-simple script that scans through a directory for print statements. It uses a recursive grep command so this won't work for you hopeless Windows people :)

Download it and put it in your ~/bin (you might want to read it through) then all you have to do is to run it against a directory where there are lots of .py files:


$ find_print_statements.py ~/dev/mypythonmodule

If it doesn't work perhaps you can patch it and let me know. This might not be for the faint-hearted. There might be serious security concerns unless you have full control over your users and su account. I take no responsibility if this script rm -fr / your hd.

Truncated! Read the rest by clicking the link below.

Read in passwords with bash

March 25, 2005
37 comments Linux

This has taken me some time to figure out because I couldn't find anything on Google. I think the problem was that I didn't know what to look for.

If you have a bash script that asks the user to enter their username and password you use the read function in sh. But when you read in the password you don't want it to show on the screen what you're writing. Someone could be leaning over your shoulder. Python has a similar standard library module called getpass which works like this:


>>> from getpass import getpass
>>> p = getpass("Password please: ")
Password please: 
>>> print "Your password is", len(p), "characters long"
Your password is 5 characters long

That's fine if you do this via Python; but I needed to do it in one of my bash scripts. Here's how to do it:


#!/bin/bash
read -p "Username: " uname
stty -echo
read -p "Password: " passw; echo
stty echo

Now, hopefully this will help other people who get stuck with the same problem.

Suspend and resume output in terminal

March 3, 2005
6 comments Linux

To some people it's obvious but I just learnt this now. It's bugged me several times where I've accidently pressed Ctrl-S and the terminal freezes. I had no idea what to do so I had to close the window with Alt-F4. This is extra annoying if you have a ssh session open.

What Ctrl-S does it that it freezes the output on the terminal. Ctrl-Q resumes it again. I can't think of a real usage for me for this but it's good to know if you accidently "freeze" your terminal.

Encrypted files in Emacs

March 1, 2005
0 comments Linux, macOS

UPDATE (Jan 2019): Read this newer blog post instead https://www.peterbe.com/plog/encrypt-with-emacs-on-macos-ccrypt

With the ccrypt program for Linux and some Emacs settings you can achieve the following:

You create or open a file called passwords.cpt. Then Emacs asks you for a password. Once you've entered the password you can word with the file like any other file to for example write down all your passwords to various websites or bank accounts.

When you close Emacs and try to read the file with any other program you get the binary encrypted output which is useless. What's important to Emacs is that the filename ends in .cpt which is a bit clunky. In vi you can get the same effect simply by passing the -x option when opening a file. But I don't like vi.

Make your settings in .Xdefaults come true

November 16, 2004
1 comment Linux

I've been trying to use Xjed recently instead of XEmacs because XEmacs took more than 3 seconds some times to start. If I have to start the editor many times in the day this really matters; and this happens when I use the ExternalEditor for Zope.

Now, to set how big the window should be or how it should look you edit the $HOME/.Xdefaults file. Mine now looks like this:


xjed*Geometry: 90x45+150+50

Truncated! Read the rest by clicking the link below.

Urwid - curses-based UI/widget library for Python

October 30, 2004
0 comments Python, Linux

"Urwid is a curses-based UI/widget library for Python. It features fluid interface resizing, multiple text layout options, simple markup for attributes, powerful scrolling list boxes and flexible edit boxes."

I've been looking for something like this for a long time. See the screenshots to get an idea of what a curses-based UI is.

Truncated! Read the rest by clicking the link below.

Massrenaming with shell and python

October 28, 2004
5 comments Python, Linux

I had the problem today that several files in a directory started with an _ underscore character. (e.g. _red.gif). Instead of manually renaming each file used the power of shell and python to solve it. (and some help from my collegue of course). Fortunately none of the files had an underscore in the middle of the name so I could keep the command quiet simple:


$ find -iname '_*' \
| xargs python -c \
'import sys;print "\n".join(["mv %s %s"%(x, x.replace("_","")) for x in sys.argv[1:]])'\
| sh -s

Truncated! Read the rest by clicking the link below.

Disable Caps Lock in Linux

October 21, 2004
30 comments Linux

I never use the Caps Lock button. Never. The only time I "use" it is when I accidently press it and start typing things in UPPERCASE. To disable it in Linux all you have to do is enter this on the command line:


$ xmodmap -e "remove lock = Caps_Lock"

Truncated! Read the rest by clicking the link below.