Click to See Complete Forum and Search --> : python snippets! (and a useful tool for you debian users)


jemfinch
12-14-2000, 08:59 PM
Yeah. We don't need no stinkin' parentheses around the s! I've got more than one JUST BY MYSELF!!

http://www.linuxnewbie.org/ubb/smile.gif

Anyway, here are a few things I've been whipping up.

md5sum.py:

import struct, string, md5

def md5sum(filename):
m = md5.new()
fd = open(filename, 'r')
m.update(fd.read())
return string.join(map(lambda x: '%02x' % x, struct.unpack('B' * 16, m.digest())), '')

if __name__ == '__main__':
import sys
if len(sys.argv) < 2:
print '\n' + md5sum('/dev/fd/0')
sys.exit(0)

for filename in sys.argv[1:]:
try:
sum = md5sum(filename)
except Exception, e:
sys.stderr.write('%s\n' % e)
print '%s %s' % (sum, filename)
sys.exit(0)


shasum.py:

import struct, string, sha

def shasum(filename):
s = sha.new()
fd = open(filename, 'r')
s.update(fd.read())
return string.join(map(lambda x: '%02x' % x, struct.unpack('B' * 20, s.digest())), '')

if __name__ == '__main__':
import sys
if len(sys.argv) < 2:
print '\n' + shasum('/dev/fd/0')
sys.exit(0)

for filename in sys.argv[1:]:
try:
sum = shasum(filename)
except Exception, e:
sys.stderr.write('%s\n' % e)
print '%s %s' % (sum, filename)
sys.exit(0)


check_debian_package_sums.py:

import glob, sys, string, md5sum # (md5sum from above)

if len(sys.argv) < 2:
sys.stderr.write('Usage: %s <logfile>\n' % sys.argv[0])
sys.exit(-1)

try:
log = open(sys.argv[1], 'w+')
except Exception, e:
sys.stderr.write('%s\n' % e)
sys.exit(-1)

for sums_file in glob.glob('/var/lib/dpkg/info/*.md5sums'):
for line in open(sums_file, 'r').readlines():
expected_sum, filename = string.split(line)
filename = '/' + filename
try:
real_sum = md5sum.md5sum(filename)
except Exception, e:
log.write('%s\n' % e)
continue
if real_sum != expected_sum:
log.write('%s did not match expected sum\n' % filename)


Now, I didn't do an exact copy/paste since that code is on a different computer than the one I'm posting this from. I hope I didn't mess up any of the typing, but if someone goes to all the trouble to run this and does get an error, please, let me know so everyone doesn't get the same error.

I've got a few other tools I've whipped up; a "find" program that finds based on regular expressions rather than shell globs, and a small, robust, spoofing identd server. If anyone's interested, I can post those, but it's tough to roll back and forth between these two not-networked computers, so only if someone asks http://www.linuxnewbie.org/ubb/smile.gif

Anyone else have snippets? As opposed to some other languages, reading other people's code in python is actually enjoyable http://www.linuxnewbie.org/ubb/smile.gif

Jeremy

YaRness
12-14-2000, 10:47 PM
<edit> don't take this too TOO seriously. i'm not really all that offendable :P
1) don't be an ***. every language has a use. they had a nice article on /. today about the pointlessness of language adv.. advo... well, something.

2) any language can be written so it's readable if you take the extra seconds to do so.

3) <insert flame about using whitespace to delimit blocks here>

4) http://www.linuxnewbie.org/ubb/cool.gif Perl http://www.linuxnewbie.org/ubb/cool.gif (well it was expected)

in all honesty i haven't looked at python at all. from your snippets, it looks like there might be a feel of java, or even ada, to it (that sys.whatever prolly). whitespace to delimit blocks still give me the willies, though i can't always put a finger on why.

anyway, to each his own i guess. i'd say it's below a programmer to be stuck up about his language(s), but as Larry Wall said, hubris is definitely a quality of a programmer http://www.linuxnewbie.org/ubb/biggrin.gif

------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/
------------------

[This message has been edited by YaRness (edited 14 December 2000).]

jemfinch
12-14-2000, 11:34 PM
Huh? Wha?

I just thought your "perl snippets" idea was nice so I copied it with some python snippets I'd been working on.

Of course, as a former (and to a lesser degree, present) perl user, I had to stick in the obligatory insult about perl's readability http://www.linuxnewbie.org/ubb/smile.gif

Don't take offense, it was all in good humor. http://www.linuxnewbie.org/ubb/smile.gif

Jeremy

(ps. don't knock whitespace delimited blocks until you try it -- I tried it, and it's better. Much better http://www.linuxnewbie.org/ubb/smile.gif)

YaRness
12-15-2000, 08:43 AM
</flames>
s'all cool and whatnot.

whitespace would drive me absolutely nuts. i'll explain why:

i'm WAY too used to having some kind of block delimiter. when i write perl, i do something like this


if


if (/<some pattern/)


if (/<some pattern/)
{
}
#or obviously i could do
#if (/<some pattern/){
#}


if (/<some pattern/)
{
#code
}


if (/<some pattern/)
{
#code
#more code
#etc
}


it makes me a little more comfortable knowing EXACTLY where the end of a block is before i even finish writing the block.

post some python links, maybe i'll give it the college try this weekend or during xmas break (or maybe even today if i get some time to kill). but i won't promise to like it.
http://www.linuxnewbie.org/ubb/mad.gif perl http://www.linuxnewbie.org/ubb/mad.gif

http://www.linuxnewbie.org/ubb/biggrin.gif

------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/
------------------