Re: Repository - file scanner

2007-06-11 Thread jigloo
On 6 9 ,   3 33 , HMS Surprise <[EMAIL PROTECTED]> wrote:
> Greetings,
>
> Could someone point my muddled head at a/the python repository. I know
> that one exists but cannot find it again. In particular I am looking
> for a standalone search tool that given a path searches files for a
> text string.
>
> Thanks,
>
> jvh

grep maybe the best choice.
if you want a pure python script
here it is.

#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# File: grep.py
# Author: phus

try:
import psyco
psyco.full()
except ImportError:
print 'no mod psyco'

import os, re

def grep_r(rx, path):
root = path
dirstack = [root]
while len(dirstack) > 0:
dir = dirstack.pop()
try:
dirs = os.listdir(dir)
except:
print "os.listdir('%s') error: %s" % (dir, os.error)
continue
for name in dirs:
fullname = os.path.join(dir, name)
if os.path.isdir(fullname):
dirstack.append(fullname)
else:
grep(rx, fullname)

def grep(rx, file):
try:
f = open(file, "r")
for line in f:
if rx.findall(line):
print file, ":", line.strip()
f.close()
except:
print "grep error in %s" % file

if __name__ == "__main__":
patern = "blah blah"
rx = re.compile(patern, re.I)
grep_r(rx, '.')

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: retrieve / find out an image's dimensions

2007-06-16 Thread jigloo
http://www.pycode.com/modules/?id=32

On 6 16 ,   6 27 , Adam Teale <[EMAIL PROTECTED]> wrote:
> hey guys
>
> Is there a builtin/standard install method in python for retrieving or
> finding out an image's dimensions?
>
> A quick google found me 
> this:http://www.pythonware.com/library/pil/handbook/introduction.htm
>
> but it looks like it is something I will need to install - I'd like to
> be able to pass my script around to people without them needing any
> additional modules
>
> Any help would be fantastic!
>
> Cheers
>
> Adam
> python 2.3.5
> osx 10.4.9


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: retrieve / find out an image's dimensions

2007-06-16 Thread jigloo
On 6 16 ,   6 27 , Adam Teale <[EMAIL PROTECTED]> wrote:
> hey guys
>
> Is there a builtin/standard install method in python for retrieving or
> finding out an image's dimensions?
Sorry, after i review these code in http://www.pycode.com/modules/?id=32,
i found some(not just a few) *BUGS* in it.
I must apologize to you for this.
I have rewrite the moudle.
please have a try.


"""Recognize image file formats and size based on their first few
bytes."""
# Perl Image::Size module clone
# see more http://search.cpan.org/author/RJRAY/Image-Size-3.01/lib/Image/Size.pm
# rewrited by jigloo([EMAIL PROTECTED])
# GPL-2 license

__all__ = ["what", "imgsz", "size"]

import os  # for os.path os.error sys.stderr
import StringIO# for StringIO.StringIO
import struct  # for unpack
import re  # for regex

# jpegsize: gets the width and height (in pixels) of a jpeg file
#
def jpegsize(stream):
(x, y, error) = (None, None, "could not determine JPEG size")

# Dummy read to skip header ID
stream.read(2)
while True:
# Extract the segment header.
(marker, code, length) = struct.unpack("!BBH", stream.read(4))

# Verify that it's a valid segment.
if marker != 0xFF:
# Was it there?
error = "JPEG marker not found"
break
elif code >= 0xC0 and code <= 0xC3:
# Segments that contain size info
(y, x) = struct.unpack("!xHH", stream.read(5))
error = "no error"
break
else:
# Dummy read to skip over data
stream.read(length - 2)

return ("JPEG", x, y, error)


# bmpsize: size a Windows-ish BitMaP image
#
def bmpsize(stream):
(x, y, error) = (None, None, "Unable to determine size of BMP data")

# Dummy read to skip header data
stream.read(18)
(x, y) = struct.unpack(" 0 and y > 0:
error = "no error"

return ("BMP", x, y, error)


# pngsize : gets the width & height (in pixels) of a png file
# cor this program is on the cutting edge of technology! (pity it's
blunt!)
#
def pngsize(stream):
(x, y, error) = (None, None, "could not determine PNG size")

# Dummy read to skip header data
stream.read(12)
if stream.read(4) == "IHDR":
(x, y) = struct.unpack("!LL", stream.read(8))
error = "no error"

return ("PNG", x, y, error)

# gifsize : Subroutine gets the size of the specified GIF
#
# Default behavior for GIFs is to return the "screen" size
GIF_BEHAVIOR = 0
#
def gifsize(stream):

if GIF_BEHAVIOR > 2:
return ("GIF", 0, 0, "Out-of-range value for GIF_BEHAVIOR: %d" %
GIF_BEHAVIOR)

# Skip over the identifying string, since we already know this is a
GIF
type = stream.read(6)
buf = stream.read(5)
if len(buf) != 5:
return ("GIF", 0, 0, "Invalid/Corrupted GIF (bad header)")
(sw, sh, x) = struct.unpack(" num_dirent:
break
tag = struct.unpack(be+"H", ifd[:2])[0] # ...and decode its tag
type = struct.unpack(be+"H", ifd[2:2+2])[0] # ...and the data 
type
# Check the type for sanity.
if type > len(packspec) or packspec[type] is None:
continue
if tag == 0x0100: # ImageWidth (x)
# Decode the value
x = struct.unpack(packspec[type], ifd[8:4+8])[0]
elif tag == 0x0101: # ImageLength (y)
# Decode the value
y = struct.unpack(packspec[type], ifd[8:4+8])[0]

# Decide if we were successful or not
if x and y:
error = "no error"
else:
error = ""
if x is None:
error = "ImageWidth "
if y is None:
if error != "":
error = error + "and "
error = error + "ImageWidth "
error = error + "tag(s) could not be found"

return ("TIFF", x, y, error)

# psdsize : determine the size of a PhotoShop save-file (*.PSD)
#
def psdsize(stream):
(x, y, error) = (None, None, "could not determine PSD size")

stream.read(14)
(y, x)

Re: Fastest way to convert a byte of integer into a list

2007-07-14 Thread jigloo
On 7 13 ,   6 34 , Godzilla <[EMAIL PROTECTED]> wrote:
> Hello,
>
> I'm trying to find a way to convert an integer (8-bits long for
> starters) and converting them to a list, e.g.:
>
> num = 255
> numList = [1,1,1,1,1,1,1,1]
>
> with the first element of the list being the least significant, so
> that i can keep appending to that list without having to worry about
> the size of the integer. I need to do this because some of the
> function call can return a 2 lots of 32-bit numbers. I have to find a
> way to transport this in a list... or is there a better way?

my clone *bin* function from python3000
def _bin(n, count=32):
"""returns the binary of integer n, using count number of digits"""
return ''.join([str((n >> i) & 1) for i in range(count-1, -1, -1)])

-- 
http://mail.python.org/mailman/listinfo/python-list