You use the '...' magic:
Cached here:
>>> x
Traceback (most recent call last):
...
NameError: name 'x' is not defined
Let us curl, milady. Let us throw and sweep atwain until the heavens themselves drop their jaws in wonder and envy. And afterwards there'll be beer and cocoa with marshmallows floating in the foam. And if, from now till the end of time, someone should ask what we were doing on the eve of the seventeenth of November, we shall proclaim that we were curling!
If you want to wrap some code in a try/except block in Python, but you want it to be tried number of times before finally raising an exception, you can do something like this:
maxtries = 5
trynumber = 1
while 1:
try:
# your code here
except WhateverException:
if trynumber >= maxtries:
# report your error
raise SomeException
# maybe sleep here
trynumber += 1
This can be used for transient errors like network stuff...
This implements Ruby's begin/rescue/retry block (more messily of course.)
If, like me, you have your vim setup to do 4-space tabs, you will have a problem when you are editing Makefiles. Makefiles need real \t tabs. There are two ways to deal with this.
The first is manual: instead of pressing the tab key, press ctrl-v ctrl-i
The second way is using a vim comment hint like so:
# vim: noexpandtabs
... somewhere in your file. With this second method you can now simply press the tab key to get real tabs.
When you try to test your new HTTP server installation on a Centos box, you will find that you get no response. That's because port 80 is firewalled by default. Add this new rule to your tables:
# iptables -I RH-Firewall-1-INPUT 6 -p tcp -m tcp --dport 80 -j ACCEPT
^ ^ ^ ^
| at position 6 the HTTP port |
insert into RH-Firewall-1-INPUT chain allow :-)
Then after you've tested this change, make it permanent (otherwise it won't survive a reboot):
# service iptables save
You can check the rulesets at any time with:
# service iptables status
... and remember to do something similar if you have port 443 open for HTTPS...
Sometimes you will have to convert a public key generated by PuttyGen for use on *nix. Just use ssh-keygen:
ssh-keygen -i -f sshv2.pub > id_dsa.pub
If you need vim to interpret a file in a specific way (or set some values for a specific file only), you can put comment hints in the file.
For example, if you have a Python file that doesn't have a .py extension and also doesn't have a #! line indicating that it is indeed Python, then vim will not turn on the correct syntax highlighting for your file (because it thinks it is just plain text.) To force Python syntax highlighting, place a comment hint at the bottom of your file like this:
def eggs():
pass
# vim: syntax=python
Let's try to implement something like Python's with statement in Perl.
First, let's look at Python. Python lets you do something like this:
class controlled_execution:
def __enter__(self):
set things up
return thing
def __exit__(self, type, value, traceback):
tear things down
with controlled_execution() as thing:
some code
(code example stolen from effbot)
The idea is that when the with block exits, the __exit__ cleanup is automatically called. This is nice for dealing with files, for example:
with f = open("somefile"):
f.read()
... where the implicit __exit__ code closes the file for us.
Can we do this in Perl? Sure. We just don't have the luxury of using nicely named __enter__ and __exit__ functions. Here is an example:
package Dir;
use strict;
use warnings;
use Cwd;
sub cd {
my $dir = shift;
my $code = shift;
my $origdir = getcwd();
chdir($dir);
$code->();
chdir($origdir);
}
1;
Now the script that utilises it:
#!/usr/bin/env perl
use strict;
use warnings;
use Dir;
Dir::cd "..", sub {
my @files = glob("*");
print join("\n", @files);
};
This context manager is for "cd", which chages directory, runs all the code in the block, and as it exits it simply changes back to the original directory it was in. There you go! Simple context managers in Perl.
James over at Prog21 has an article on calculating the shortest rotation (in degrees) for a satellite dish - his point being that there is a case or two that he didn't immediately think about (i.e. that it's harder than it sounds.)
Here is my Python version of his function (with doc tests):
def angle_diff(begin, end):
"""
>>> angle_diff(0, 50)
50
>>> angle_diff(37, 38)
1
>>> angle_diff(200, 10)
170
>>> angle_diff(50, 20)
-30
>>> angle_diff(0, 270)
-90
>>> angle_diff(270, 0)
90
>>> angle_diff(100, 100)
0
>>> angle_diff(0, 0)
0
"""
easyway = end - begin
if abs(easyway) > 180:
return (360 - abs(easyway)) * cmp(0, easyway)
# get the shorter way ^
# in the opposite direction ^
return easyway
Testing:
$ nosetests --with-doctest ad.py
.
----------------------------------------------------------------------
Ran 1 test in 0.003s
OK
If you just committed a change as the incorrect user, as long as you haven't pushed your branch you can do this:
$ git config user.name "Correct name here"
$ git config user.email "Correct email here"
$ git commit --amend --reset-user
Django Python 960.gs Git Vim NetBSD Nginx
This is the blog of Brad Willis, a software engineer living in Brisbane.
Help
Latest entries
*BSD Agile Apache Apple apt Athletics Best-Practice Censorship Comedy Cool Crosswords Deployment Django English Exim Firefox Git Hardcore Health irssi Javascript Jira Languages Linux Makefile Mathematics Mobile Broadband Mutt MySQL NetBSD nginx Nokia OpenVZ OSX Perl Privacy Python Rant Requirements rsync Ruby Shell Slackware SQL SQLite SSH Standards Subversion Television Testing ThisBlog Vim VMWare (Fusion) VPN X zsh
Checking for exceptions in doctests
Homer's Curling Speech
retry in Python
Vim Makefile tabs
Centos (or RH) IPTables
Converting ssh2 public keys to openssh
Vim comment hints
Context managers in Perl
Dish rotation
Git - fixing commit user
apt stuff
Using shell variables in AWK
Linux - Too many open files
Tell gvim to save and quit... remotely
Vim - automatically remove whitespace at EOL
Python - relative paths from within modules
TV Aspect Ratios
Git - Which commits are in your branch only?
Subversion setup cheat sheet
Force detach a screen session
Modify sudo's use of environment variables
Install all Perl modules
Mutt - delete old messages
OpenVZ VPS and swap space
fail2ban on NetBSD for ssh
NetBSD - Using sup
Python - testing for a sys.exit
Python Best Practice Link Dump
Python script names
Perl - Using an expensive module
Speed of git clone
Perl Modules with Custom Prefix
Perl: tr vs. s
Brilliant sysadmin Reference
Why is GRUB better than LILO?
Why is swap space important?
Perldoc Output
Git's Index
Jira Project Keys
Git GUI