Monthly Archives: December 2012

Python function to update a var in a list of tuple

As part of my new ssh-fail script, now written in python I found myself needing to update a var in a list of a list, but you can’t just do list_a[3][3]= ‘new thing’ :(

so i wrote this function:

def tuple_update(touple, varloc, newval):
    temp = []
    for a in range(len(tuple)):
        if varloc != a:
            temp.append(tuple[a])
        else:
            temp.append(newval)
    return temp

and you can use it like this:

>>>ip_test=[('8.8.8.8', 423, None, 0), ('4.2.2.2', 64, None, 3), ('42.42.42.42', 23, None, 10)]
 
>>> ip_test[1]
('4.2.2.2', 64, None, 3)
>>> ip_test[1]=tuple_update(ip_test[1],1,38)
>>> ip_test[1]
['4.2.2.2', 38, None, 3]
>>> ip_test[2]=tuple_update(ip_test[2],0,'100.100.2.3')
>>> ip_test[2]
['100.100.2.3', 23, None, 10]
>>> ip_test
[['9.9.9.9', 423, None, 0], ['4.2.2.2', 38, None, 3], ['100.100.2.3', 23, None, 10]]

that’s: tuple_update(list,location,newval)

SourceForge direct download with clean filename

If you ever wget files from source forge you’ll know how the file name will be annoying with a bunch of extra crap

so…:

 

#!/bin/bash
file=`echo $1 | sed 's,.*/,,g; s/?.*//'`
echo $file
wget "$1" -O ./$file

for:

% ~/sf.sh "http://downloads.sourceforge.net/project/gns-3/GNS3/0.8.3.1/GNS3-0.8.3.1-src.tar.bz2?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fgns-3%2F%3Fsource%3Ddirectory&ts=1354547913&use_mirror=voxel"
GNS3-0.8.3.1-src.tar.bz2
--2012-12-03 10:18:49--  http://downloads.sourceforge.net/project/gns-3/GNS3/0.8.3.1/GNS3-0.8.3.1-src.tar.bz2?r=http%3A%2F%
<>

Length: 2611269 (2.5M) [application/x-bzip2]
Saving to: `./GNS3-0.8.3.1-src.tar.bz2'

100%[=======================================================================================================================================================================>] 2,611,269   2.37M/s   in 1.1s    

2012-12-03 10:19:16 (2.37 MB/s) - `./GNS3-0.8.3.1-src.tar.bz2' saved [2611269/2611269]
 
( ~/del/sftest )% ls
GNS3-0.8.3.1-src.tar.bz2

yay!