Mostly Linux & Python syntax notes and hyperlinks.
Tuesday, September 10, 2013
Sunday, July 14, 2013
Linux: finding file (quoting Alex Gaynor Tweet)
To find a file by name: `find . -name “*dns*”`. To find a file by contents: `ag “dns”`. Unix.
— Alex Gaynor (@alex_gaynor) July 14, 2013
Wednesday, June 19, 2013
Veracode Infographic - History of Programming Languages
from a Tweet, to a website, to an offer to Add this Infographic to Your Website for FREE!
Infographic by Veracode Application Security
Tuesday, June 11, 2013
python: os.path pathname split examples
From http://docs.python.org/2/library/os.path.html,
And then to get-last-directory-name-in-a-path
>>> import os
>>> pth="/abeja/blanca/zumbas/ebria/de/miel/en/mi/alma/y.te.tuerces"
>>> os.path.split(pth)
('/abeja/blanca/zumbas/ebria/de/miel/en/mi/alma', 'y.te.tuerces')
>>> os.path.dirname(pth)
'/abeja/blanca/zumbas/ebria/de/miel/en/mi/alma'
>>> os.path.basename(pth)
'y.te.tuerces'
>>> os.path.splitext(os.path.basename(pth))
('y.te', '.tuerces')
>>> os.path.splitext(pth)
('/abeja/blanca/zumbas/ebria/de/miel/en/mi/alma/y.te', '.tuerces')
And then to get-last-directory-name-in-a-path
Tuesday, June 4, 2013
python: checking whether link exists os.path.exists(), os.path.lexists(),os.path.islink()
We were checking whether a link existed before trying to create it.
We were using os.path.exists(). This failed when there was a broken link.
It is better to use os.path.lexists() to check whether something exists before you use that name again for linking to another file. (See also may2011)
Useful list of os.path methods here.
We were using os.path.exists(). This failed when there was a broken link.
It is better to use os.path.lexists() to check whether something exists before you use that name again for linking to another file. (See also may2011)
$ l foo
-rw-rw-r-- 1 usr grp 8 Jul 11 2012 foo
$ ln -s foo fiBad
create symbolic link `fiBad' to `foo'
$ mv foo foooo
`foo' -> `foooo'
$ ln -s foooo fiGood
create symbolic link `fiGood' to `foooo'
$ l f*
-rw-rw-r-- 1
usr grp
8 Jul 11 2012 foooo
lrwxrwxrwx 1
usr grp
3 Jun 4 18:23 fiBad -> foo
lrwxrwxrwx 1
usr grp
5 Jun 4 18:24 fiGood -> foooo
$ python
Python 2.4.1 (#2, Jul 24 2007, 12:14:31)
[GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-34)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> if os.path.exists("foooo"):
... print "T"
...
T
>>> if os.path.exists("fiGood"):
... print "T"
...
T
>>> if os.path.exists("fiBad"):
... print "T"
... else:
... print "F"
...
F
>>> if os.path.lexists("fiBad"):
... print "T"
... else:
... print "F"
...
T
>>> if os.path.lexists("foooo"):
... print "T"
...
T
>>> if os.path.lexists("fiGood"):
... print "T"
...
T
>>> if os.path.islink("fiGood"):
... print "T"
...
T
>>> if os.path.islink("fiBad"):
... print "T"
... else:
... print "F"
...
T
Useful list of os.path methods here.
Thursday, May 23, 2013
Python: given two lists of strings, create 2-d dictionary to hold int values, initialized to 0
>>> L1=['A','B','C']
>>> L2=['y','z']
>>> dd=dict()
>>> for x in L2:
... d=dict()
... for q in L1:
... d[q]=0
... dd[x]=d
...
>>> dd
{'y': {'A': 0, 'C': 0, 'B': 0}, 'z': {'A': 0, 'C': 0, 'B': 0}}
Then to use it to count stuff:
>>> L3=['Ax','Bz','Cy','By','Cz']
>>> for x in L2:
... for q in L1:
... for s in L3:
... if x in s and q in s:
... dd[x][q]+=1
...
>>> dd
{'y': {'A': 0, 'C': 1, 'B': 1}, 'z': {'A': 0, 'C': 1, 'B': 1}}
>>> L2=['y','z']
>>> dd=dict()
>>> for x in L2:
... d=dict()
... for q in L1:
... d[q]=0
... dd[x]=d
...
>>> dd
{'y': {'A': 0, 'C': 0, 'B': 0}, 'z': {'A': 0, 'C': 0, 'B': 0}}
Then to use it to count stuff:
>>> L3=['Ax','Bz','Cy','By','Cz']
>>> for x in L2:
... for q in L1:
... for s in L3:
... if x in s and q in s:
... dd[x][q]+=1
...
>>> dd
{'y': {'A': 0, 'C': 1, 'B': 1}, 'z': {'A': 0, 'C': 1, 'B': 1}}
Thursday, May 16, 2013
Python: don't catch an exception if you don't know what to do with it
Big complicated tangle of code. Lots of utility methods called from different places. We got in the habit of every method enclosing a try... Except block.
But sometimes it's better to propagate an exception up to the calling method. It will know what to do about it and be closer to the error logging needed.
We missed catching some error conditions soon enough because they were lost inside a utility that trapped the exception but didn't handle it right.
linux: a tenth of a second from ksh
We needed a time stamp of the format HHMMSST
where HH = hour, MM=minutes SS=seconds and T=one tenth of a second.
If you type "man time" you can see different format strings for days to seconds to nanoseconds:
To get the nanoseconds, you can do
To get the tenth of a second in the bash shell, it was simple to do:
I needed to generate the timestamp within an already-written ksh script.
I found two methods that worked in my korn shell script: using "expr" or using "typeset"
I used typeset:
where HH = hour, MM=minutes SS=seconds and T=one tenth of a second.
If you type "man time" you can see different format strings for days to seconds to nanoseconds:
%j day of year (001..366)
%k hour (0..23)
%M minute (00..59)
%S second (00..60); the 60 is
necessary to accommodate a leap second
%N nanoseconds (000000000..999999999)
To get the nanoseconds, you can do
N=$(date +%N)
This worked great in bash or korn shell.To get the tenth of a second in the bash shell, it was simple to do:
$ N=$(date +%N)
$ echo $N
538595000
$ t=${N:0:1}
$ echo $t
5
In the korn shell, I got this:
$ t=${N:0:1}
ksh: : bad substitution
I needed to generate the timestamp within an already-written ksh script.
I found two methods that worked in my korn shell script: using "expr" or using "typeset"
Using expr:
$ expr $N : '\(.\{3\}\)'
115
$ expr $N : '\(.\{1\}\)'
1
using typeset
$ echo $N
655762000
$ typeset -L1 t=$N
$ echo $t
6
I used typeset:
function set_timestamp
{
TOSEC=$(date +%H%M%S)
NANO=$(date +%N)
typeset -L1 TENTH=$NANO
TIMESTAMP=${TOSEC}${TENTH}
}
Subscribe to:
Posts (Atom)