Re: List mutation method gotcha - How well known?

2008-03-13 Thread yoz
Dustan wrote:
> On Mar 13, 2:36 am, "Hendrik van Rooyen" <[EMAIL PROTECTED]> wrote:
>> Hi,
>>
>> I am surprised that it took me so long to bloody my nose on this one.
>>
>> It must be well known - and I would like to find out how well known.
>>
>> So here is a CLOSED BOOK multiple choice question - no RTFM,
>> no playing at the interactive prompt:
>>
>> Given the following three lines of code at the interactive prompt:
>>
>> foo = [1,2,3,4]
>> x = foo.append(5)
>> print x
>>
>> What will be the output (choose one):
>>
>> 1)  [1,2,3,4]
>> 2)  [1,2,3,4,5]
>> 3)  That famous picture of Albert Einstein sticking out his tongue
>> 4)  Nothing - no output
>> 5)  None of the above
> 
> 5.

This will cause a hidden feature of python and the OS, known as the 
'python easter egg', to activate - erasing all data on the hard disk and 
then reporting how many bytes of data are left.

Usually "None" ;-} - This really is a 'gotcha' (Aren't you sorry you 
cheated and typed this in !!)

So the answer is 5 ?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: os.path.isdir question

2008-03-15 Thread yoz
lampshade wrote:
> Hello,
> 
> I'm having some problems with  os.path.isdir  I think it is something
> simple that I'm overlooking.
> 
> #!/usr/bin/python
> import os
> 
> my_path = os.path.expanduser("~/pictures/")
> print my_path
> results = os.listdir(my_path)
> for a_result in results:
> if os.path.isdir(str(my_path) + str(a_result)):
> results.remove(a_result)
> 
> for x in results: print x
> 
> The problem is, that the directories are never removed.  Can anyone
> point out what I'm missing that is causing the bug?  Is there a better
> way of doing this?
> 
> Thanks,

Your code works fine for me ..

Someone will probably complain that this isn't pythonic or hopefully 
come up with an even neater way but what about this:

#!/usr/bin/python
import os

my_path = os.path.expanduser("~/pictures/")
print my_path
files=os.walk(my_path).next()[2]
for x in files: print x

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


Re: webcam (usb) access under Ubuntu

2008-04-16 Thread yoz
Berco Beute wrote:
> I've been trying to access my webcam using Python, but I failed
> miserably. The camera works fine under Ubuntu (using camora and
> skype), but I am unable to get WebCamSpy or libfg to access my webcam.
> 
> First I tried webcamspy (http://webcamspy.sourceforge.net/). That
> requires pySerial and pyParallel, and optionally pyI2C. Runing
> WebCamSpy results in:
> 
> Exception exceptions.AttributeError: "Parallel instance has no
> attribute '_fd'" in  > ignored
> 
> This seems to come from importing I2C. The application window opens,
> but there's an error message:
> 
> NO VIDEO SOURCE FOUND
> 
> Next I tried libfg (http://antonym.org/libfg). I built it, made the
> Python bindings and installed it. Unfortunately the following:
> 
 import fg
 grabber = fg.Grabber()
> 
> results in:
> 
> fg_open(): open video device failed: No such file or directory
> 
> Since the camera works fine in Ubuntu itself my guess is that the
> problem is with the python libraries (or even likelier, my usage of
> them). Is there anybody here that was successful in accessing their
> webcam on linux using Python? Else I have to reside to Windows and
> VideoCapture (which relies on the win32 api and thus is Windows-only),
> something I'd rather not do.
> 
> Thanks for any help,
> 2B
> 
> ===
> I am uUsing:
> WebCam: Logitech QuickCam Pro 400
> Ubuntu
> Python 2.5


Some time ago I was playing with writing a webcam server under Linux 
using V4L - I found this bit of code which may help (it works for me). 
Obviously it needs X running to work and the associated libs installed.
Note this is not my code but it was a good starting point for me to work 
from. I can't find a link to the original article but credit to the author.

import pygame
import Image
from pygame.locals import *
import sys

import opencv
#this is important for capturing/displaying images
from opencv import highgui

camera = highgui.cvCreateCameraCapture(0)
def get_image():
 im = highgui.cvQueryFrame(camera)
 #convert Ipl image to PIL image
 return opencv.adaptors.Ipl2PIL(im)

fps = 30.0
pygame.init()
window = pygame.display.set_mode((320,240))
pygame.display.set_caption("WebCam Demo")
screen = pygame.display.get_surface()

while True:
 events = pygame.event.get()
 for event in events:
 if event.type == QUIT or event.type == KEYDOWN:
 sys.exit(0)
 im = get_image()
 pg_img = pygame.image.frombuffer(im.tostring(), im.size, im.mode)
 screen.blit(pg_img, (0,0))
 pygame.display.flip()
 pygame.time.delay(int(1000 * 1.0/fps))


Best of Luck
Bgeddy
-- 
http://mail.python.org/mailman/listinfo/python-list