Re: [Tutor] Remove a dictionary entry
Thank you, After reading the following documentations http://docs.python.org/tutorial/datastructures.html#looping-techniques http://docs.python.org/tutorial/controlflow.html#for-statements I ended up with this : Version 3 : for i,row in d[:].iteritems() : # BUG : TypeError: unhashable type if len(row) 2 : del d[i] Still buggy... Any lead for this error message? Is a slice unhashable? Am I looking in the right direction for this task? Mr. 427 Le vendredi 17 septembre 2010 à 20:50 -0400, bob gailer a écrit : Please always reply-all so a copy goes to the tutor list. On 9/17/2010 6:20 PM, M. 427 wrote: Thank you very much for your answer. I wanted to know the pythonic way of doing this, so I did not post my buggy trial which was : version 1 : for row in d : if len(row) == 1 : del row # WRONG Version 2 : for i,row in d : if len(row) == 1 : del d(i) # BUG : Syntax error Thank you for posting code. In the future do so initially. It helps us know where to help. It looks like you learned from version 1. When you get a syntax error check the manual. When you look at del in: Python v2.6.4 documentation - The Python Standard Library - 6.8 Mapping Type - dict you will see d[key] - compare that to what you wrote in version 2. -- Bob Gailer 919-636-4239 Chapel Hill NC ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Remove a dictionary entry
M. 427 4...@free.fr wrote I ended up with this : Version 3 : for i,row in d[:].iteritems() : # BUG : TypeError: unhashable type if len(row) 2 : del d[i] You are getting too complicated. You don't need the slice and you don't need iteritems. You have a dictionary. When you iterate over a dictionary what do you get? Don't know? Try it:: for x in {1:'foo',2:'bar'}: print x ... 1 2 So we get the keys. Now how do we use the keys to get the list? Standard dictionary access: print d[1] foo You know how to test the lenth of the list and delete the list so put that together as you did before: for row in d : # row is actually the key if len(row) == 1 :# so use the key to get the real row del row # WRONG #' and delete the row, again using the key HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Remove a dictionary entry
M. 427 wrote: (I am very new to python) I built a dictionary d={} of lists similar to this : d = { 'a': ['apricot', 'apple'], 'b': ['beach', 'bear', 'bottle'], 'c': ['cold', 'cook', 'coleslaw'], 'd': ['deep'], 'e': ['expression', 'elephant'] } Now i want to go through this dictionary and remove all rows containing only one entry. How should I do that? You should never iterate over a list or dictionary and add or remove items to it at the same time. That is a recipe for disaster even if it doesn't fail explicitly. Instead create a new dictionary that contains only the items you are interested in: d = { ... 'a': ['apricot', 'apple'], ... 'b': ['beach', 'bear', 'bottle'], ... 'c': ['cold', 'cook', 'coleslaw'], ... 'd': ['deep'], ... 'e': ['expression', 'elephant'] ... } result = {} for k, v in d.iteritems(): ... if len(v) 1: ... result[k] = v ... import pprint pprint.pprint(result) {'a': ['apricot', 'apple'], 'b': ['beach', 'bear', 'bottle'], 'c': ['cold', 'cook', 'coleslaw'], 'e': ['expression', 'elephant']} Peter PS: Instead of using the pretty print module pprint I could have typed result {'a': ['apricot', 'apple'], 'c': ['cold', 'cook', 'coleslaw'], 'b': ['beach', 'bear', 'bottle'], 'e': ['expression', 'elephant']} The only difference is that it looks messier. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] class problem
Hello, I have this exercise : Create and print a Point object, and then use id to print the object’s unique identifier. Translate the hexadecimal form into decimal and confirm that they match. So I thought that this would solve it: class Point: def __init__(self, x=0, y=0): self.x = x self.y = y P=(Point) a=0 b=0 a=id(P) print a print b print P But now id is a decimal so I don't can't translate it. Did I something wrong ? Roelof ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Help - importing modules
Hi, everyone. I need help when importing a file I created, with the .py extension. I am trying to access its directory in DOS, and after I do it, I type import filename, but it is not working. I tried to do it by writing import filename.py, but it didn't work either. I'm aware that after the first import, I should either call the reload function or restart the session, but I can't even import the file successfully once. I am using Python 2.7. Thanks in advance for your help. F ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help - importing modules
On Sat, Sep 18, 2010 at 10:16 AM, Fernando Karpinski fanger2...@gmail.comwrote: Hi, everyone. I need help when importing a file I created, with the .py extension. I am trying to access its directory in DOS, and after I do it, I type import filename, but it is not working. I tried to do it by writing import filename.py, but it didn't work either. I'm aware that after the first import, I should either call the reload function or restart the session, but I can't even import the file successfully once. I am using Python 2.7. Thanks in advance for your help. F ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor your file needs to be in the same directory as your program or in the sys.path list you don't use the extension. import filename is all you need, not import filename.py -- Joel Goldstick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Remove a dictionary entry
Alan Gauld alan.ga...@btinternet.com wrote I ended up with this : Version 3 : for i,row in d[:].iteritems() : # BUG : TypeError: unhashable type if len(row) 2 : del d[i] You are getting too complicated. You don't need the slice and you don't need iteritems. You have a dictionary. When you iterate over a dictionary what do you get? Don't know? Try it:: for x in {1:'foo',2:'bar'}: print x ... 1 2 So we get the keys. Now how do we use the keys to get the list? Standard dictionary access: print d[1] foo You know how to test the lenth of the list and delete the list so put that together as you did before: for row in d : # row is actually the key if len(row) == 1 :# so use the key to get the real row del row # WRONG #' and delete the row, again using the key Oops, as Peter pointed out that won't work because its changing the iterable while we iterate. (I actually thought it would be OK because the for would use a copy of the keys() list, but I was wrong...) But you can fix that with a list() call: for row in list(d) : # generates a new list of the dict keys if len(d[row]) == 1 :# so use the key to get the real row del d[row] HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] class problem
Roelof Wobben rwob...@hotmail.com wrote Create and print a Point object, and then use id to print the object’s unique identifier. Translate the hexadecimal form into decimal and confirm that they match. I initially had no idea what hexadecimal form the text is talking about. id returns a decimal form... This is a badly worded assignment. class Point: def __init__(self, x=0, y=0): self.x = x self.y = y P=(Point) a=0 b=0 The two a,b assigments are seemingly pointless? a=id(P) print a print b print P Why do you want to print b which will be zero? However your print P gave me a clue to what the assignment is about. When you print the object it gives you a hex value I think they want you to extract that value and convert it to decimal to see if its the same as the value id() gives you. At least that's the only sane thing I can think it means! -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] plotting pixels
On Fri, Sep 17, 2010 at 3:38 AM, Alan Gauld alan.ga...@btinternet.comwrote: For plotting pixels I would not use turtle graphics. That would be a fairly complicated option I'd have thought. A simple canvas would be easier. Alan G. Oh, I see! I did not realize that Tk had a canvas widget. That is nice. I will have to play with that and see if I can get everything done in the code I am working with. What I am doing is just trying to do a simple Mandelbrot set plot. It is another bit of coding that I do when learning a new language to get a handle on some of the graphics capabilities, and I am to that point. -Bill ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] plotting pixels
On Sat, Sep 18, 2010 at 10:44 AM, Bill Allen walle...@gmail.com wrote: On Fri, Sep 17, 2010 at 3:38 AM, Alan Gauld alan.ga...@btinternet.comwrote: For plotting pixels I would not use turtle graphics. That would be a fairly complicated option I'd have thought. A simple canvas would be easier. Alan G. Oh, I see! I did not realize that Tk had a canvas widget. That is nice. I will have to play with that and see if I can get everything done in the code I am working with. What I am doing is just trying to do a simple Mandelbrot set plot. It is another bit of coding that I do when learning a new language to get a handle on some of the graphics capabilities, and I am to that point. -Bill It appears that the Tk canvas widget does not support simply plotting a pixel. However, I can plot a line only one pixel long. I wonder why they do not simply provide the pixel plot primitive? I have seen very many graphics packages that do this and I have always wondered why. The primitive obviously exists in the underlying code, because that is what everything else is built upon. Does Tk actually have a something like a create_pixel method in the canvas widget that I have missed? -Bill ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help - importing modules
On Sun, 19 Sep 2010 12:16:47 am Fernando Karpinski wrote: Hi, everyone. I need help when importing a file I created, with the .py extension. I am trying to access its directory in DOS, and after I do it, I type import filename, but it is not working. Define not working. My crystal ball tells me that you're trying to run import filename at the DOS prompt, rather than in the Python interpreter. Is my crystal ball accurate? -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] class problem
On Sat, 18 Sep 2010 07:14:03 pm Roelof Wobben wrote: P=(Point) This line does not do what you think it does. Brackets in Python are used for two things, grouping and calling functions. To call a function, or a class, you need to have the brackets *after* the function: P = Point() # what about arguments to the function? If you surround it with brackets, as you do above, it does nothing. It's like this: x = (1+1) # exactly the same as x = 1+1 without brackets a=0 b=0 a=id(P) It is a waste of time to initialise variables immediately before initialising them again. print a print b print P But now id is a decimal so I don't can't translate it. id(x) returns an integer. By default, integers always print in decimal, if you want to print them in hex you can do this: hex(id(P)) -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Remove a dictionary entry
On Sat, 18 Sep 2010 07:13:13 pm Peter Otten wrote: You should never iterate over a list or dictionary and add or remove items to it at the same time. That is a recipe for disaster even if it doesn't fail explicitly. That's a bit strong. It's quite possible to modify lists safely and correctly while iterating over them with a little bit of care. You know, for decades people were able to program in languages like C and Pascal and assembly, often on machines with tiny amounts of memory. When your machine has 64K of memory, and the OS and application uses half of it, you don't have the luxury of making a copy of a 20K list before modifying it. Back when I was a lad, we learned how to modify lists in place. It isn't hard. *wink* Even in Python, it is sometimes necessary to modify lists and even dicts in place while iterating over them. 98% of the time, making a copy is faster, simpler and more efficient, but learning how to safely modify data structures in place is a valuable skill to have. But I'm just talking about general principles here. In most cases, stick to Peter's advice to make a copy. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help - importing modules
On Sat, Sep 18, 2010 at 11:46 AM, Steven D'Aprano st...@pearwood.info wrote: On Sun, 19 Sep 2010 12:16:47 am Fernando Karpinski wrote: Hi, everyone. I need help when importing a file I created, with the .py extension. I am trying to access its directory in DOS, and after I do it, I type import filename, but it is not working. Define not working. My crystal ball tells me that you're trying to run import filename at the DOS prompt, rather than in the Python interpreter. Is my crystal ball accurate? My crystal ball says, 'Hey buddy, pal', but after that it says, you have to first assign python in windows, as an environmental variable in windows, with C:\pythonversion. so you can type python in the cmd prompt on windows, and have python interpreter come up. -- Steven D'Aprano ___ Tutor maillist - tu...@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] FW: class problem
Hello , Thanks everyone. I solved it by this : class Point: def __init__(self, x=0, y=0): self.x = x self.y = y P=Point() print P print id(P) and a calculator which can convert hex to decimal. Roelof From: st...@pearwood.info To: tutor@python.org Date: Sun, 19 Sep 2010 01:54:11 +1000 Subject: Re: [Tutor] class problem On Sat, 18 Sep 2010 07:14:03 pm Roelof Wobben wrote: P=(Point) This line does not do what you think it does. Brackets in Python are used for two things, grouping and calling functions. To call a function, or a class, you need to have the brackets *after* the function: P = Point() # what about arguments to the function? If you surround it with brackets, as you do above, it does nothing. It's like this: x = (1+1) # exactly the same as x = 1+1 without brackets a=0 b=0 a=id(P) It is a waste of time to initialise variables immediately before initialising them again. print a print b print P But now id is a decimal so I don't can't translate it. id(x) returns an integer. By default, integers always print in decimal, if you want to print them in hex you can do this: hex(id(P)) -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] What are singletons good for?
Hey all, the usual explanation for the usage of a Singleton goes like this: Use a singleton if you want to make sure, that only one instance of a class exists. But now I ask myself: Why should I call the constructor of a class more than once if I only want one instance? After all, I decide in my code when to create an instance or when to pass an existing instance around. Example in pseudocode: class Session(object): Hold a dictionary of ident_to_data_objects def __init__(self, ident_to_data): self.ident_to_data = ident_to_data Now, that would be a typical singleton use case. I want one instance of this class application-wide. For example in a View class: class View(object): Create fancy views def __init__(self, session): self.session = session In my code I use these classes like this: class MainApp(object): Do some stuff with the data_objects def __init__(self): self.session = Session() self.view = View(self.session) Would a singleton usage in the View class look like that? class View(object): Create fancy views def __init__(self): self.session = Session() What's the point? Is it the spared typing when instanciating a lot of View classes (I wouldn't need to pass the session to the constructor). Or are there some more advantages (instead of passing the same instance aorund)? Also, what would you guys consider as disadvantages? Another question related to this topic is, if I would use a module as a singleton (as suggested by Steve and other), how would I import it the instances of a class? Maybe like this? class View(object): Create fancy views import session def do_something(self, ident): self.certain_data_object = session.ident_to_data[ident] A lot of questions, so thanks in advance for any comments! Cheers, Jan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Remove a dictionary entry
Yet another way is to iterate thru the dict collecting a list of keys of items to be deleted. Then iterate thru that list deleting from the dict. -- Bob Gailer 919-636-4239 Chapel Hill NC ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] next class problem
Hello, I have this exercise : Rewrite the distance function from chapter 5 so that it takes two Points as parameters instead of four numbers. I have this solution : class Point: def __init__(self, x=0, y=0): self.x = x self.y = y def distance(p1,p2): dx = p2.x - p1.x dy = p2.y - p1.y dsquared = dx**2 + dy**2 result = dsquared**0.5 return result P1 = Point() P1.x = 3 P1.y = 3 P2 = Point() P2.x = 6 P2.y = 7 result = distance (P1,P2) print result Is this the correct solution ? Roelof ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] next class problem
On 9/18/2010 1:20 PM, Roelof Wobben wrote: Hello, I have this exercise : Rewrite the distance function from chapter 5 so that it takes two Points as parameters instead of four numbers. I have this solution : class Point: def __init__(self, x=0, y=0): self.x = x self.y = y def distance(p1,p2): dx = p2.x - p1.x dy = p2.y - p1.y dsquared = dx**2 + dy**2 result = dsquared**0.5 return result P1 = Point() P1.x = 3 P1.y = 3 P2 = Point() P2.x = 6 P2.y = 7 result = distance (P1,P2) print result Is this the correct solution ? What is your criteria for correct? There is no one correct solution! You seem to be passing 2 points, as requested. Do you get the correct answer? Then it mus be correct. FWIW Python convention recommends names starting with lower case except for classes and constants. Therefore p1 and p2 are preferred to P1 and P2. Also why not initialize x and y thus: p1 = Point(3,3) That is what the __init__ is for. -- Bob Gailer 919-636-4239 Chapel Hill NC ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] next class problem
Date: Sat, 18 Sep 2010 13:40:55 -0400 From: bgai...@gmail.com To: rwob...@hotmail.com CC: tutor@python.org Subject: Re: [Tutor] next class problem On 9/18/2010 1:20 PM, Roelof Wobben wrote: Hello, I have this exercise : Rewrite the distance function from chapter 5 so that it takes two Points as parameters instead of four numbers. I have this solution : class Point: def __init__(self, x=0, y=0): self.x = x self.y = y def distance(p1,p2): dx = p2.x - p1.x dy = p2.y - p1.y dsquared = dx**2 + dy**2 result = dsquared**0.5 return result P1 = Point() P1.x = 3 P1.y = 3 P2 = Point() P2.x = 6 P2.y = 7 result = distance (P1,P2) print result Is this the correct solution ? What is your criteria for correct? There is no one correct solution! You seem to be passing 2 points, as requested. Do you get the correct answer? Then it mus be correct. FWIW Python convention recommends names starting with lower case except for classes and constants. Therefore p1 and p2 are preferred to P1 and P2. Also why not initialize x and y thus: p1 = Point(3,3) That is what the __init__ is for. -- Bob Gailer 919-636-4239 Chapel Hill NC Hello, Thank you. Learned another thing. Roelof ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] plotting pixels
-Original Message- From: Bill AllenSent: Sep 18, 2010 11:45 AM To: Alan Gauld Cc: tutor@python.org Subject: Re: [Tutor] plotting pixels On Sat, Sep 18, 2010 at 10:44 AM, Bill Allen walle...@gmail.com wrote: On Fri, Sep 17, 2010 at 3:38 AM, Alan Gauld alan.ga...@btinternet.com wrote: For plotting pixels I would not use turtle graphics.That would be a fairly complicated option I'd have thought.A simple canvas would be easier.Alan G. Oh, I see! I did not realize that Tk had a canvas widget. That is nice. I will have to play with that and see if I can get everything done in the code I am working with. What I am doing is just trying to do a simple Mandelbrot set plot. It is another bit of coding that I do when learning a new language to get a handle on some of the graphics capabilities, and I am to that point. -Bill It appears that the Tk canvas widget does not support simply plotting a pixel. However, I can plot a line only one pixel long. I wonder why they do not simply provide the pixel plot primitive? I have seen very many graphics packages that do this and I have always wondered why. The primitive obviously exists in the underlying code, because that is what everything else is built upon. Does Tk actually have a something like a create_pixel method in the canvas widget that I have missed?-Bill Is it naive of me to ask, "Couldn't one write his own plotpixel( ) function using the line() function?" . ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] plotting pixels
It appears that the Tk canvas widget does not support simply plotting a pixel. However, I can plot a line only one pixel long. I wonder why they do not simply provide the pixel plot primitive? I have seen very many graphics packages that do this and I have always wondered why. The primitive obviously exists in the underlying code, because that is what everything else is built upon. Does Tk actually have a something like a create_pixel method in the canvas widget that I have missed? You don't want that. Tkinter's Canvas is a Smart Canvas, each lines and shapes corresponds to a Tcl/Tk object. If you want to plot a 800*600 image pixel-per-pixel in Tkinter's Canvas, then Tkinter would have to create 48 Tcl Objects. If you want to draw pixels and lines directly, Tkinter Canvas isn't suitable for that. Try using a different Canvas, one that uses a Stateless Canvas. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What are singletons good for?
On 09/19/10 02:50, Knacktus wrote: Hey all, the usual explanation for the usage of a Singleton goes like this: Use a singleton if you want to make sure, that only one instance of a class exists. But now I ask myself: Why should I call the constructor of a class more than once if I only want one instance? After all, I decide in my code when to create an instance or when to pass an existing instance around. The guarantee. If you're writing a module that may be used by two or more modules, that may be used by a script. A logger module is a good example; if your script imports two modules, and both modules import the same logger module and instantiate their own version of loggers, then it is difficult to coordinate the logging of those two modules. If instead the logger class is a Singleton, then the user of logger modules doesn't need to care about any other modules using the same logger module, since they will create an instance when needed or get the existing logger when someone else already made one. A configuration module is another good example. It is a common idiom in python to import a .py script for configuration purpose. The benefit of this is that the config file basically becomes a truly global variable (python does not have a true global variable). What's the point? Is it the spared typing when instanciating a lot of View classes (I wouldn't need to pass the session to the constructor). Or are there some more advantages (instead of passing the same instance aorund)? Also, what would you guys consider as disadvantages? Disadvantage? compared to what? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] plotting pixels
On Sat, Sep 18, 2010 at 1:18 PM, Ken Oliver ksterl...@mindspring.comwrote: On Fri, Sep 17, 2010 at 3:38 AM, Alan Gauld alan.ga...@btinternet.comwrote: For plotting pixels I would not use turtle graphics. That would be a fairly complicated option I'd have thought. A simple canvas would be easier. Alan G. Oh, I see! I did not realize that Tk had a canvas widget. That is nice. I will have to play with that and see if I can get everything done in the code I am working with. What I am doing is just trying to do a simple Mandelbrot set plot. It is another bit of coding that I do when learning a new language to get a handle on some of the graphics capabilities, and I am to that point. -Bill It appears that the Tk canvas widget does not support simply plotting a pixel. However, I can plot a line only one pixel long. I wonder why they do not simply provide the pixel plot primitive? I have seen very many graphics packages that do this and I have always wondered why. The primitive obviously exists in the underlying code, because that is what everything else is built upon. Does Tk actually have a something like a create_pixel method in the canvas widget that I have missed? -Bill Is it naive of me to ask, Couldn't one write his own plotpixel( ) function using the line() function? . No, not naive at all. Indeed I could, but that is not the issue in my mind. My point is that it seems strange to me that any graphics package would not make the most basic of the routines, ploting a single pixel, available. I think I actually see a way of doing it with the bitmap class, but that is not the point. While I could do exactly as you have said, I would rather either write my own low-level code to accomplish that or use a package that does provide it than to wrap up a higher end function, such as drawing a line or rectangle, into even more code in order to do less. For the particular use that I am going to put this to, a package such as pygame which does provide the ability to plot pixels directly will be more suitable. -Bill ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] plotting pixels
It appears that the Tk canvas widget does not support simply plotting a pixel. Correct, and I agree it seems odd, but in practice drawing either lines or ovals of one-pixel do the equivalent job - albeit a little more slowly. The primitive obviously exists in the underlying code, It probably exists in the native graphics toolkit (Xlib or Win32 or Aqua) but it doesn't exist at the Tk level which is why Tkinter can't expose it. FWIW wxPython does provide a DrawPoint() method as part of its DeviceContext class. Digging a little deeper it seems the idiomatic way to do this in Python is to use PIL the Python Imaging Library to create a GIF or bitmap image and then insert that into Tkinters cancvas as an image object. The Pil ImageDraw class has a point() ethod I've never tried this but it is described in Grayson's (now out of print?) book on Tkinter where he uses it to draw a Mandelbrot The book may be available online these days... Nowdownloadall.com seems to have it although I've no idea of the legality of it! HTH, Alan G. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] plotting pixels
Digging a little deeper it seems the idiomatic way to do this in Python is to use PIL the Python Imaging Library to create a GIF or bitmap image and then insert that into Tkinters cancvas as an image object. The Pil ImageDraw class has a point() ethod I've never tried this but it is described in Grayson's (now out of print?) book on Tkinter where he uses it to draw a Mandelbrot The book may be available online these days... Nowdownloadall.com seems to have it although I've no idea of the legality of it! HTH, Alan G. Yes, to create a gif or a bmp from the iteration results and then to display that at the end of the run is by far the most efficient way of producing Mandelbrot and related sets. I have actually done it that way before. I just have always had a strange preference to see the set as it is being produced, which is far from efficient. Kind of a very elaborate progress bar! Anyway, I have no real complaints about the Tk canvas methods. It has always just been a pet peeve of mine when something as basic and simple as plotting a pixel is missing. My complaint on this goes way back to the ancient days when I had to figure out how to write a plot_pixel primitive in x86 assembler and then build a graphics library of my own so I could have pixel based graphics on my old monochrome IBM XT clone that had a Hercules graphics card in it. Those were the days! Mandelbrot sets in 4 shades of amber-monochrome!;-) I will check out that book you referenced. I appreciate everybody's feedback on this. -Bill ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] plotting pixels
On Sat, Sep 18, 2010 at 9:26 PM, Bill Allen walle...@gmail.com wrote: Digging a little deeper it seems the idiomatic way to do this in Python is to use PIL the Python Imaging Library to create a GIF or bitmap image and then insert that into Tkinters cancvas as an image object. The Pil ImageDraw class has a point() ethod I've never tried this but it is described in Grayson's (now out of print?) book on Tkinter where he uses it to draw a Mandelbrot The book may be available online these days... Nowdownloadall.com seems to have it although I've no idea of the legality of it! HTH, Alan G. Yes, to create a gif or a bmp from the iteration results and then to display that at the end of the run is by far the most efficient way of producing Mandelbrot and related sets. I have actually done it that way before. I just have always had a strange preference to see the set as it is being produced, which is far from efficient. Kind of a very elaborate progress bar! Anyway, I have no real complaints about the Tk canvas methods. It has always just been a pet peeve of mine when something as basic and simple as plotting a pixel is missing. My complaint on this goes way back to the ancient days when I had to figure out how to write a plot_pixel primitive in x86 assembler and then build a graphics library of my own so I could have pixel based graphics on my old monochrome IBM XT clone that had a Hercules graphics card in it. Those were the days! Mandelbrot sets in 4 shades of amber-monochrome!;-) I will check out that book you referenced. I appreciate everybody's feedback on this. -Bill I found this code on the web. It creates a 100x100 tk.photoimage and fills it with a radom colored pixels then displays it. It seems to me that I should be able to adapt this to what I am trying to acomplish. The only difference in the way I am filling the tk.photoimage object. I ran this under Python 3.1.2 with success. I believe the '#%02x%02x%02x' is the format for an image. It is a color photoimage, but I am presuming that if written directly out to a file this would not actually produce a valid, bmp, gif, pgn, etc. Correct? This does seem to be a reasonable solution that is a pure Tk solution. Also it works in Python 3x, whereas the PIL library has not yet been released for 3x. I have not mentioned it before, but using Python 3x only is also one of my requirement, though self-imposed. Can anyone help me better understand this part of the code below? self.i.put('#%02x%02x%02x' % tuple(color),(row,col)) import tkinter, random class App: def __init__(self, t): self.i = tkinter.PhotoImage(width=100,height=100) colors = [[random.randint(0,255) for i in range(0,3)] for j in range(0,1)] row = 0; col = 0 for color in colors: self.i.put('#%02x%02x%02x' % tuple(color),(row,col)) col += 1 if col == 100: row +=1; col = 0 c = tkinter.Canvas(t, width=100, height=100); c.pack() c.create_image(0, 0, image = self.i, anchor=tkinter.NW) t = tkinter.Tk() a = App(t) t.mainloop() ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor