Generators can only yield ints?
def letters(): a = xrange(ord('a'), ord('z')+1) B = xrange(ord('A'), ord('Z')+1) while True: yield chr(a) yield chr(B) >>> l = letters() >>> l.next() Traceback (most recent call last): File "", line 1, in l.next() File "", line 5, in letters yield chr(a) TypeError: an integer is required >>> Any way to get around this? -- http://mail.python.org/mailman/listinfo/python-list
>> and << operators?
What does >> and << do? Googling on them and they are just ignored... -- http://mail.python.org/mailman/listinfo/python-list
How can I check nbr of cores of computer?
How can I check how many cores my computer has? Is it possible to do this in a Python-app? -- http://mail.python.org/mailman/listinfo/python-list
Re: isPrime works but UnBoundLocalError when mapping on list
On Jul 15, 7:28 pm, Mensanator <[EMAIL PROTECTED]> wrote: > On Jul 15, 11:26 am, defn noob <[EMAIL PROTECTED]> wrote: > > > > > isPrime works when just calling a nbr but not when iterating on a > > list, why? adding x=1 makes it work though but why do I have to add > > it? > > Is there a cleaner way to do it? > > > def isPrime(nbr): > > for x in range(2, nbr + 1): > > if nbr % x == 0: > > break > > if x == nbr: > > return True > > else: > > return False > > > >>> [isPrime(y) for y in range(11)] > > > Traceback (most recent call last): > > File "", line 1, in > > [isPrime(y) for y in range(11)] > > File "C:\Python25\Progs\blandat\myMath.py", line 9, in isPrime > > if x == nbr: > > UnboundLocalError: local variable 'x' referenced before assignment > > > >>> map(isPrime, range(100)) > > > Traceback (most recent call last): > > File "", line 1, in > > map(isPrime, range(100)) > > File "C:\Python25\Progs\blandat\myMath.py", line 9, in isPrime > > if x == nbr: > > UnboundLocalError: local variable 'x' referenced before assignment>>> > > isPrime(10) > > False > > >>> isPrime(11) > > > True > > > adding x=1 makes it work though: > > > def isPrime(nbr): > > x=1 > > for x in range(2, nbr + 1): > > if nbr % x == 0: > > break > > if x == nbr: > > return True > > else: > > return False > > > >>> [isPrime(y) for y in range(11)] > > > [False, True, True, True, False, True, False, True, False, False, > > False] > > No, it doesn't. You are falsely reporting that 1 is prime. > > And instead of making the fake variable x, shouldn't you > instead test that nbr+1 is greater than 2? Or call it with > range(3,11) instead of range(11)? x isn't initialized > because if nbr+1 is <=2, the for loop has an invalid range > and doesn't even execute. def isPrime(nbr): for x in range(2, nbr + 1): if nbr % x == 0: break if x == nbr: return True else: return False this works for all primes, if i want to not include 1 i just do if nbr<=1 return false you are answering the wrong question. anyway here is a clear one: def isPrime(nbr): if nbr < 2: return False for x in range(2, nbr + 1): if nbr % x == 0: return nbr == x -- http://mail.python.org/mailman/listinfo/python-list
isPrime works but UnBoundLocalError when mapping on list
isPrime works when just calling a nbr but not when iterating on a list, why? adding x=1 makes it work though but why do I have to add it? Is there a cleaner way to do it? def isPrime(nbr): for x in range(2, nbr + 1): if nbr % x == 0: break if x == nbr: return True else: return False >>> [isPrime(y) for y in range(11)] Traceback (most recent call last): File "", line 1, in [isPrime(y) for y in range(11)] File "C:\Python25\Progs\blandat\myMath.py", line 9, in isPrime if x == nbr: UnboundLocalError: local variable 'x' referenced before assignment >>> map(isPrime, range(100)) Traceback (most recent call last): File "", line 1, in map(isPrime, range(100)) File "C:\Python25\Progs\blandat\myMath.py", line 9, in isPrime if x == nbr: UnboundLocalError: local variable 'x' referenced before assignment >>> isPrime(10) False >>> isPrime(11) True adding x=1 makes it work though: def isPrime(nbr): x=1 for x in range(2, nbr + 1): if nbr % x == 0: break if x == nbr: return True else: return False >>> [isPrime(y) for y in range(11)] [False, True, True, True, False, True, False, True, False, False, False] -- http://mail.python.org/mailman/listinfo/python-list
Re: Not necessarily related to python Web Crawlers
just crawling is supereasy. its how to index and search that is hard. just start at yahoo.com, scrape out all the links and then for every site visit every link. i wrote a crawler in 15 lines of code. but then it all it did was visit the sites, not indexing them or anything. you could write a faster one in C++ probably but if you are new to it doing it in python will let you experiment and learn faster. some links: http://infolab.stanford.edu/~backrub/google.html http://www-csli.stanford.edu/~hinrich/information-retrieval-book.html http://www.example-code.com/python/pythonspider.asp http://www.example-code.com/python/spider_simpleCrawler.asp -- http://mail.python.org/mailman/listinfo/python-list
Re: site-packages, unzipepd there but import fails
On Jul 3, 8:06 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > On 3 juil, 18:51, defn noob <[EMAIL PROTECTED]> wrote: > > > well the reason i unzipped and placed it in site-packages was because > > nothign happened when i ran setup.py(unzipped). > > What do you mean, "running setup.py unzipped" ??? > > The correct way to install a package is to: > > - unzip (untar, whatever) the archive somewhere in your own directory > - cd to the unzipped directory > - *from there*, run python setup.py install yes, which i have done. the command row executes but nothing happens, no errors either. -- http://mail.python.org/mailman/listinfo/python-list
Re: manipulating movie files with Python
I would love the same thing. Dont know what you are doing but this might be relevant even if it is not what you are asking for: http://imdbpy.sourceforge.net/ library to take data from imdb. -- http://mail.python.org/mailman/listinfo/python-list
Re: site-packages, unzipepd there but import fails
and yes it is parallel python. and windows vista. and the python GUI shell. -- http://mail.python.org/mailman/listinfo/python-list
Re: site-packages, unzipepd there but import fails
well the reason i unzipped and placed it in site-packages was because nothign happened when i ran setup.py(unzipped). this has worked with other packages before. -- http://mail.python.org/mailman/listinfo/python-list
Re: site-packages, unzipepd there but import fails
On Jul 3, 5:02 pm, Chris <[EMAIL PROTECTED]> wrote: > On Jul 3, 4:11 pm, defn noob <[EMAIL PROTECTED]> wrote: > > > i unzipped and put the folder in site-packages. when i run setup.py > > install nothing happens. > > > when i do import pp from shell it complains it doesnt exist. > > > isnt placing the folder in site-packages enough? > > > these setup.py-files often dont work but normally it still works. > > "python setup.py install" windows...i run setup.py install -- http://mail.python.org/mailman/listinfo/python-list
site-packages, unzipepd there but import fails
i unzipped and put the folder in site-packages. when i run setup.py install nothing happens. when i do import pp from shell it complains it doesnt exist. isnt placing the folder in site-packages enough? these setup.py-files often dont work but normally it still works. -- http://mail.python.org/mailman/listinfo/python-list
PIL(Py Image lib), show() not showing picture...
from PIL import Image import os print os.path.exists('C:/Users/saftarn/Desktop/images/blob.jpg') im2 = Image.open('C:/Users/saftarn/Desktop/images/blob.jpg') im2.show() first the command prompt pops up then the normal window to display pictures but it doenst show a picture. the prin os.path.exists returns true and opening the file by clicking on it works. ive also tried gif-files with no success. -- http://mail.python.org/mailman/listinfo/python-list
Re: Run interpreter in emacs?
thanks but i dont quite understand the load-path, add to the load path. the loadpath has a lot of variables. so where i should i place the python-mode folder and should i then add that directory to the loadpath? -- http://mail.python.org/mailman/listinfo/python-list
Re: Most efficient way to get pixelcolors of an image?
this just shows a GUI that normally shows pictures but it doesnt show any picture... from PIL import Image im = Image.open('C:/Users/saftarn/Desktop/images/giffer.gif') im2 = Image.open('C:/Users/saftarn/Desktop/images/blob.jpg') im.rotate(45).show() im2.show() -- http://mail.python.org/mailman/listinfo/python-list
Run interpreter in emacs?
Can I run the python interpreter from with in Emacs? Do i have to change anything then? or is it by default? it already syntax-highlights python-files. -- http://mail.python.org/mailman/listinfo/python-list
Most efficient way to get pixelcolors of an image?
i want to process a large number of images and store their respective pixels in a matrix. what is the mostt efficient way of opening and checking them? i doubt pygame is made for this purpose :) i guess i could also use tkinter? and why cant i print out matrix after getting the pixels? do i have to deinit() pygame somehow? import pygame import sys import os print os.path.exists('C:/users/saftarn/desktop/images/bloba.bmp') pygame.init() screen = pygame.display.set_mode((800, 600)) image = pygame.image.load('C:/users/saftarn/desktop/images/bloba.bmp') imrect = image.get_rect() imrect = imrect.move(200, 200) matrix = [] while 1: pygame.display.flip() screen.fill((255,255,255)) screen.blit(image, imrect) pygame.event.wait() event = pygame.event.wait() for x in range(1, 301): for y in range(1, 301): matrix.append(screen.get_at((x, y))) #print matrix if event.type == pygame.QUIT: #print matrix sys.exit() -- http://mail.python.org/mailman/listinfo/python-list
tkinter, loading image error, TclError: couldn't recognize data in image file "C:/users/me/desktop/images/blob4.jpg"
from Tkinter import * import os master = Tk() w = Canvas(master, width=800, height=600) print os.path.exists('C:/me/saftarn/desktop/images/blob4.jpg') im = PhotoImage(file = 'C:/users/saftarn/desktop/images/blob4.jpg') #im = file = 'C:/users/me/desktop/images/blob4.jpg' pic = w.create_image(0, 0, image = im, anchor = NW) #image = open('C:/users/saftarn/desktop/images/blob.png') colors = [] for x in range(1, 800): for y in range(1, 600): pic = w.find_closest(x, y)[0] obj = objects[pic] colors.append(obj.get(int(x), int(y))) print colors >>> True Traceback (most recent call last): File "C:/Python25/Progs/ImageVideoSearch/imId.py", line 9, in im = PhotoImage(file = 'C:/users/me/desktop/images/blob4.jpg') File "C:\Python25\lib\lib-tk\Tkinter.py", line 3270, in __init__ Image.__init__(self, 'photo', name, cnf, master, **kw) File "C:\Python25\lib\lib-tk\Tkinter.py", line 3226, in __init__ self.tk.call(('image', 'create', imgtype, name,) + options) TclError: couldn't recognize data in image file "C:/users/me/desktop/ images/blob4.jpg" >>> it has worked before opening and displaying a file like this, anything to do with python 2.52, upgraded from 2.5.1 -- http://mail.python.org/mailman/listinfo/python-list
Re: Pygame, how to show window without loop? no loop=popupand close...
On 28 Juni, 08:32, Carl Banks <[EMAIL PROTECTED]> wrote: > On Jun 27, 10:58 pm, defn noob <[EMAIL PROTECTED]> wrote: > > > right. im an idiot anyway. i can just draw the lines before entering > > the loop, problem solved... > > Do not do that; it'll create a busy loop and use 100% of CPU. Use > pygame.event.wait() instead. It waits for an event to occur, without > using CPU cycles. > > Carl Banks pygame.init() screen = pygame.display.set_mode(size) while 1: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() screen.fill(screencolor) draw((500, 20), 5) pygame.display.flip() pygame.event.wait() running that i cant close the program... what must i do? create an event at mouse click? -- http://mail.python.org/mailman/listinfo/python-list
Re: Pygame, how to show window without loop? no loop=popupand close...
right. im an idiot anyway. i can just draw the lines before entering the loop, problem solved... -- http://mail.python.org/mailman/listinfo/python-list
Pygame, how to show window without loop? no loop=popupand close...
Im using PyGame to draw images of graphs and trees. Howver right now i am looping using: while 1: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() screen.fill(screencolor) pygame.draw.circle(screen, linecolor, (500, 20), 12, 0) draw((500, 20), 3) pygame.display.flip() if i do screen.fill(screencolor) pygame.draw.circle(screen, linecolor, (500, 20), 12, 0) draw((500, 20), 3) pygame.display.flip() it just pops up and closes. how can i make it stay until i close it without using a loop? -- http://mail.python.org/mailman/listinfo/python-list
How to "rebind" py2.5.1 to run from comprompt after uninstalling py3.0?
I installed python30 and so command prompt runs all pythonprograms through that which i didnt want so i uninstalled it. now i cant start any pythonprograms through the commandprompt. how do I "rebind" python25 to luanch when claling .py-files from the command prompt? -- http://mail.python.org/mailman/listinfo/python-list
Re: recursion in Class-methods?
> > > if start == end: > > return path > > if not self.dictionary.has_key(start): > >if start not in self.dictionnary: > > > return None > > for node in self.dictionary[start]: > > if node not in path: > > newpath = find_path(self.dictionary, node, end, path) > >newpath = self.find_path(...) > > (snip remaining code - same problems, same solutions...) it is to incoherent fo follow what you mean here(not meaning to sound rude i appreciate the help). -- http://mail.python.org/mailman/listinfo/python-list
Re: recursion in Class-methods?
class Graph(object): where does anyone write like that? I've seen only examples like i have written. is the object then passed to init? class Graph(object): def __init__(self, dictionary): self.structure = dictionary or class Graph(object): def __init__(self, object): self.structure = object i dont get it. and "mutable containers", do you refer to "path=[]" as a parameter. -- http://mail.python.org/mailman/listinfo/python-list