- Strength to Increase Rep
- +10
- Strength to Decrease Rep
- -2
- Upvotes Received
- 31
- Posts with Upvotes
- 22
- Upvoting Members
- 16
- Downvotes Received
- 1
- Posts with Downvotes
- 1
- Downvoting Members
- 1
404 Posted Topics
Re: The info on the links is not encouraging: "very little code was written in 2005, and 2006 doesn't look much better." Hmm... My mildly informed $0.02, for the purpose of generating lively discussion: I don't think a true OS can be written in Python, for the following reason: Python requires … | |
Hi all, The continuing quest to master Tkinter has led to this implementation of the dots and boxes game. The rules are well-known: each player gets to create a new line between two dots (single-click near the line you wish to create). If a player creates a box, he gets … | |
Re: Right. Notice the subtle difference and be careful about it: the original code is legitimate Python, but it doesn't mean what one might think it means. [code] if var1 == "image0.GIF" or "image1.GIF" or "image2.GIF": varValue = 10 [/code] will take the value of 'var1 == "image0.GIF"' (which might be … | |
Re: Or, [code="Python"] if math.sqrt(n) % 1 == 0: print "integer square root!" else: print "not." [/code] | |
Re: That's a good start. I might do something like [code=Python] ... for ch in message: code_val = ord(ch) + key if code_val > ord('z'): code_val -= 26 # better: ord('z') - ord('a'), just in case len(alphabet) != 26 codedMessage = codedMessage+chr(code_val) ... [/code] Jeff | |
Re: [quote=sneekula;274491]Are there any guidelines when to use and not to use a class?[/quote] (1) Well, one of the most important prerequisites for having clean code is to use the right data structure. A class can help you construct the data structure that you need instead of having to force something … | |
Re: Here's one that I actually wrote for home use cataloging several thousand pictures: Write a program to scan a user-supplied directory for .jpg files. Then, output the list of files, sorted by date picture was taken, as an HTML file with hyperlinks to each pic. Bonus points: make the scan … | |
Re: Do you have a link for this particular algorithm? Thanks, Jeff | |
Re: It's a good question, and my own private peace with it is that .destroy() appears to be more general: widgets and frames can be destroyed without destroying the parent, but if you .quit() anywhere within the widget tree, it appears to kill the whole toplevel. Here's some sample code. The … | |
Re: This is a partial solution, but I want to try to figure it out, 'cause it's a cool problem. If you use an Entry() widget for the password and then set show='*' in the Entry widget, the Control-c will only copy the ******s rather than the underlying characters. That would … | |
Re: So in the manual, it says [quote]Of course, Python doesn't use 8-bit numbers. It USED to use however many bits were native to your machine, but since that was non-portable, it has recently switched to using an INFINITE number of bits. Thus the number -5 is treated by bitwise operators … | |
Generators are essentially dynamic sequences, making their official debut in Python 2.5. That got me to thinking: can we make a circular list with them? Why yes, we can... | |
Re: In terms of putting items into a list to be sorted, you want this: [code=Python] f = open("mytextfile","r") mylist = [] for line in f: mylist.append(f.readline()) f.close() [/code] or even more concisely, [code=Python] f = open("mytextfile","r") lines = f.readlines() f.close() [/code] I agree with woooee, but he's not refusing help: … | |
Re: Good job! Some testing results: 1) Starting in the foyer and going n, e, w leads to an endless loop of "invalid command" -- no commands, even help, are recognized. 2) Starting in the foyer and going e, followed by "look", gives this: There is an open wardrobe in the … | |
Re: I'm not sure you'll get meaningful results from anything mouse-and-keyboard driven *because* the mouse and keyboard events can sometimes take unpredictable amounts of time to be passed along to your program. I'm not a hardware expert, so I could be wrong about that ... but at least keep the possibility … | |
Re: Well, just to give you a bit of confidence ... the prof has given you good advice. Now for the details ... ! First, you need to plan your code. Make a plan (sometimes called [I]pseudocode[/I]) that doesn't go into the details, but clearly and generally spells out exactly how … | |
Re: Does win.getMouse() wait for the mouse to be clicked before it returns? If not, then you may have a problem ... because it will fire 5 times instantaneously, and get the same mouse coordinates for p1 through p5. Jeff | |
Re: It almost seems like what you need is a simple C function to write the char to the port, and then link it in to your Python program. Jeff | |
Re: Check the documentation for tkFileDialog with help(tkFileDialog) and help(tkFileDialog.askopenfile). That will tell you what the dialog accepts as parameters and returns as values. Jeff | |
Re: If you're looking to test really large numbers, here's an implementation of the Rabin-Miller probabilistic test: [code=Python] def primes(n): """ primes(n) --> primes Return list of primes from 2 up to but not including n. Uses Sieve of Erasth. """ if n < 2: return [] nums = range(2,int(n)) p … | |
Re: Preliminary tip: encase your code in (code="Python) and (/code) tags (no spaces on the first tag) so that it'll look pretty. This is a classic case of recursion: fib(n) = fib(n-1) + fib(n-2) The best way to compute it is therefore recursively: >>> def fib(n): if n <= 0: return … | |
Re: reinstall! Wow. I've gotten that message before, but rebooting is the most extreme solution to date for me. It may be the case that your firewall is blocking connections to IP address 127.0.0.1, the internal loopback that Python uses. If so, you can go into Symantec/Norton/McCaffee/whatever and enable Python on … | |
Re: Here's what your prof meant: let s = "refer" then s[0] == s[-1], so we check s = "efe" and s[0] == s[-1], so we check s = "f" and s[0] == s[-1], so we check "" which is empty, so we're done, and we return True. So what you … | |
Re: The super() function allows you to access methods in the parent x that are shadowed by methods of the same name in the child y. In this case, y.__init__ wants to call x.__init__. But the problem is that y.__init__ clobbers __init__ in the namespace; that is, x.__init__ doesn't get inherited. … | |
Re: I'm confused. I agree with the change; == is almost always better than is. Still and all... [code="Python"] >>> a = 2 >>> a is 1 False >>> a is 2 True >>> if a is 1: print "a is 1!" else: print "a is not 1!" a is not … | |
Re: Apparently so. You can check it: [code="Python"] >>> a = ((11L,), (12L,), (13L,), (14L,), (15L,), (16L,), (17L,), (18L,), (19L,), (20L,)) >>> print a[0] (11L,) >>> print a[0][0] 11 >>> print type(a[0][0]) <type 'long'> >>> [/code] Jeff | |
So I was thinking that there would be a simple Pythonic way to delete a directory structure. My app creates and deletes user accounts, which may contain any number of subdirectories and files. Hence, I wanted to do something like: [code] os.rmdir(user_directory) [/code] BUT...that throws an error when user_directory is … | |
Re: Or if regular expressions are too painful, some kind of compromise: [code] def count_caps(s): d = {} caps = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" for i in caps: d[i] = s.count(i) return d def print_caps(s): d = count_caps(s) # not 'for i in d' becuase dictionaries don't have guaranteed order for i in "ABCDEFGHIJKLMNOPQRSTUVWXYZ": … | |
Re: well, one approach could be to have the button attached to a single callback that checks state and calls one of three functions based on the state: [code] def callback(self): commands = [self.myfunc1, self.myfunc2, self.myfunc3] commands[self.mydata]() [/code] commands is then a jump table that gets you to the right function. … | |
Re: Yeah, I posted about this, too. Here's the deal: Because lists are mutable, multiple references to the same list can lead to side effects. This line: [code]mlist = [[0]*3]*3[/code] creates a list with three references to the single list [0,0,0]. So when you change that list, all of the references … | |
![]() | Re: Let's assume that you're implementing the queue with a simple list. In the specific case of a limit break, it would be easy to implement without a double queue: [code="pseudo"] if action == limit_break: action_queue = [character] + action_queue else: action_queue.append(character) [/code] That is, if the character casts the limit … |
Re: Can't speak to IPython, but a simple file copy will solve the first problem. | |
Inspired by Bumsfeld's color tester, and wanting to give my students some practice at interpreting color strings, I wrote a little Tkinter program that asks the user to chose one of six buttons whose color most closely matches the color string printed at top. Comments welcome, and does anyone know … | |
So I needed a way to represent a line in Python. My first stab was to create a function dynamically. But classes are so much ... classier, so here's a simple implementation of a line object, with plenty of room for additional methods. The line is created as l = … | |
Re: I like the middle one the best, in terms of flexibility and conceptual cleanness. The function version is nice, except that it would require a different function for each nested loop (unless your loops were similar enough that you could polymorph them...). The flag version is fastest, I would guess, … | |
Re: A good framework is to play against yourself and pay attention to what you do. Then write a plan that describes your steps in general terms. Then refine those steps until you have code. Jeff | |
Re: No, it makes sense. Try this: [code="Python"] def perms(s): if len(s) == 0: return [] elif len(s) == 1: return [s] retval = [] for x in s: base = s[:] base.remove(x) for tmp in perms(base): tmp = [x] + tmp retval.append(tmp) return retval print perms([]) print perms([1]) print perms(range(1,3)) … | |
Re: You mean the center of mass? Well, do it like the physicists do: Sum(moments) / Sum(masses): [code="Python"] >>> def findCOM(grid): Mx = 0 My = 0 mass = 0 for i in range(len(grid)): for j in range(len(grid[i])): if grid[i][j]: Mx += j My += i mass += 1 COM = … | |
Re: Heh. On WinXP I got "Skipped unknown compression method." I thought it was just me. :lol: I can see the file names, but I can't unzip them. Or is this an NSA challenge? :p Jeff | |
Re: Well, Python and I were both confused by this line: [code="Python"] newsubsets.copy(subsets) [/code] Did you intend for newsubsets to become a copy of subsets? If so, then you want [code="Python"] newsubsets = subsets[:] [/code] But I question the wisdom of clobbering newsubsets every time you go through the inner loop. … | |
Re: Interesting concept. Two potential problems: (1) What if the images are not all the same size? (2) How does the code decide what framerate to use? Jeff | |
Re: I think I understand what you're trying to do. It still can be done with instances like so: [code="Python"] class Cube(object): def __init__(self, x, y): self.x = x self.y = y self.image = pygame.image.load("Data\Cube_one.bmp") ... def isColliding(self, player): rect = self.get_rect() player_rect = player.get_rect() if ... # inequalities go here … | |
Re: The phrase [code="Python"] for line in data: ... [/code] assigns the variable "line" to each interated string terminated by '\n' within the data. So if you inserted the statement [code="Python"] data = open ("files/" + data + ".txt", 'r') for line in data: print line line = line[34:38] file_list = … | |
Re: Hm. The only thing I can think of is that your LocalFileName is not valid ASCII. Jeff | |
Re: Search this forum for Vega's decimal to binary converter. Your code is reading the data correctly; now you just have to find the right way to display it. Jeff | |
Re: That's cool. I didn't even know there was a PySerial module. First, the error messages are confusing. Are those the result of the print motor.read(motor.InWaiting()) command? Second, just to back up Bear's point: Since the motor is known good, and since hyperterminal works, it therefore follows that PySerial isn't sending … | |
Re: The standard way of repeating code is to use a [b]while[/b] loop. There are two styles: the 'test first' and 'test last' methods. [code="Python"] # a test-first loop: set the sentinel and repeat until sentinel is false repeat = True while repeat: print "You lose!" if raw_input("Play again? (y/n) ") … | |
Re: [QUOTE=karenmaye;810937]Hi, I found a code that displays multiple images but you have to supply its filename first before it can access it. Is there a way to display an image as soon as it is captured by the camera? The GUI that I am creating is going to be interfaced … |
The End.