|
That shouldn't have been that drat difficult to find. Also, can you set the default path to something other than the default? I keep my small programs on the desktop, and I'd like IDLE to go there first when looking for programs. edit: another interesting thing: If I run this on the command line (or in the command prompt), even if the word is correct, it won't show it as correct, but it works fine when I run it in IDLE. Gothmog1065 fucked around with this message at 18:50 on Mar 24, 2011 |
# ? Mar 24, 2011 18:42 |
|
|
# ? Jun 11, 2024 15:31 |
|
pre:class Base: def __init__(self): iam = 0 class Derivedfrom(Base): def __init__(self): thisis = 1 fish = Derivedfrom()
|
# ? Mar 24, 2011 21:11 |
|
clearly not a horse posted:I am having trouble finding any info on how I can declare a subclass of Bas WITHOUT having to declare every stinking variable from the Base class inside the derived class. ANy tips? It works in C++, but Python is forcing me to write self all the time. You actually have to call the base class constructor. code:
|
# ? Mar 24, 2011 21:17 |
|
To provite a slightly different alternative, that uses new-style classes and better supports MRO for multiple inheritance:code:
|
# ? Mar 24, 2011 21:27 |
|
Alternately, you can use class variables.code:
|
# ? Mar 24, 2011 21:29 |
|
Thanks for the replies. I have yet another question, this one regarding file processing. Let's say I have a txt-file which looks like this:pre:apple_001 type = apple weight = 4 value = 2 taste = spiffin apple_002 type = apple weight = 5 value = 3 taste = horrible pre:class Fruit: def __init__(self, fruitid): self.type = None self.weight = 0 self.value = 0 self.taste = None self.getfruit(fruitid) def getfruit(fruitid): #read file, find the corresponding fruitid and assign the corresponding values to the type, weight, value and taste variables in the object.
|
# ? Mar 24, 2011 22:18 |
|
Can someone briefly indicate to me why Canvas on Tkinter is so slow when doing moving objects/rectangles? I have a while True: loop which sleeps for 30ms (so it should be 33.3 FPS) and then moves ten rectangles around the canvas, bumping off the walls screensaver-style. I don't have any memory/data racking up (like, all I do is clear the canvas, loops through the objects and move their x/y position, then redraw them), so I cannot really comprehend what would cause them to slow down in the canvas over time (after like ten seconds they slow down a ton). I imagine it's some sort of memory issue that my code should be handling that it isn't, because when I make the same program in Javascript/HTML5 canvas, it never slows down (because the browser is handling something).
|
# ? Mar 25, 2011 01:30 |
|
Well first of all, you should never have fixed delays like that in a gui draw loop. You should keep track of the timestamp of the previous frame and only sleep how ever many ms won't put you over schedule for the next draw. Post your code.
|
# ? Mar 25, 2011 01:40 |
|
tripwire posted:Well first of all, you should never have fixed delays like that in a gui draw loop. You should keep track of the timestamp of the previous frame and only sleep how ever many ms won't put you over schedule for the next draw. Interesting. My CS teacher did say it was a terrible way to do animations (though that was in Java but there's no reason Python would handle it any differently I suppose). disclaimer: this code is terrible, I only use Python for data-mining/little stuff and I'm learning blah blah code:
|
# ? Mar 25, 2011 01:52 |
|
clearly not a horse posted:Thanks for the replies. I have yet another question, this one regarding file processing. Let's say I have a txt-file which looks like this: There are several ways to do this, but a line-by-line parser is probably the simplest. File pointers, when used as an iterator, give you lines. So we first can for the right fruit ID, then read & parse line by line to get the details. Regular expressions, as always, are awesome. code:
|
# ? Mar 25, 2011 02:35 |
|
Stabby McDamage posted:There are several ways to do this, but a line-by-line parser is probably the simplest. File pointers, when used as an iterator, give you lines. So we first can for the right fruit ID, then read & parse line by line to get the details. Regular expressions, as always, are awesome. Thanks a lot for show reminding me that I have a pretty long way left - a lot here I need to read up on. * with X as Y * match() * group() * setattr() If it isn't too redundant for this thread, I would be glad if someone could clarify the concepts for me. re.match('(\w+)\s*=\s*(.*)' seems kind of mystic to me, as I can't see where "re" was declared.
|
# ? Mar 25, 2011 02:51 |
|
I was doing a phone screen today with someone for a decidedly non-technical position, but on both his resume and his cover letter, he mentioned python, so I was curious about his level of aptitude. Now, we're doing the interview over Skype since he was currently traveling and in a hotel in Thailand on some shaky internet. The following conversation took place: Me: "Can you describe what a list comprehension is and when you'd use one?" Him: "It's, well, a list of elements and you could use them whenever you had a series of items you'd want to keep track of." Me: "Pardon? I was asking about list comprehensions. Do you know what they are?" Him: "Yeah, I comprehend what a list is." I laughed and we moved back to non-technical topics.
|
# ? Mar 25, 2011 02:54 |
|
quote:If it isn't too redundant for this thread, I would be glad if someone could clarify the concepts for me. re is the Regular Expression module. Basically this is being used to parse: variable = value
|
# ? Mar 25, 2011 03:13 |
|
clearly not a horse posted:Thanks a lot for show reminding me that I have a pretty long way left - a lot here I need to read up on. re was imported on line 1.
|
# ? Mar 25, 2011 03:13 |
|
windwaker posted:Interesting. My CS teacher did say it was a terrible way to do animations (though that was in Java but there's no reason Python would handle it any differently I suppose). I do know that everytime you call the clear method, you are creating a new rectangle. All the previous ones are still there! The call to create_rectangle returns an ID number which you can use with other TK functions, so you only need to call it once and save the result. I think the raise function in canvas might be what you want, but then again theres probably an actual way of clearing the screen that youre supposed to use, I'm just not familiar with tkinter/tk Heres some fixes i made: code:
Wilcard imports will bite you in the rear end! tripwire fucked around with this message at 07:32 on Mar 25, 2011 |
# ? Mar 25, 2011 07:30 |
|
Ahhhhhhhhhhh, that is how to properly "tick" an animation loop like this, I see. Very helpful, thank you! I found out that in canvas, you can point to the rectangle objects created by create_rectangle, and later delete them with canvas.delete(id), which removes the need to clear the canvas (which is not how the <canvas> attribute in HTML5 works). You were correct and the slowdown was because those objects kept being created, not sure how I missed that. Thanks!
|
# ? Mar 25, 2011 08:10 |
|
I just realized I forgot to update the value of the Window._last_update in that tick method; don't forget to do that
|
# ? Mar 25, 2011 13:45 |
|
My last set of questions may have been missed. One, is there a way to get python to change it's default directory (Where it looks for saved files)? Also, this code will not work in the console, but it works fine when run within IDLE. Is there something that would cause that?
|
# ? Mar 25, 2011 14:13 |
|
One, what do you mean by "saved files"? You mean, where open() looks? Two, make sure you're actually running the same version of Python both under IDLE and from the command-line; the change in behavior of input() from 2.x to 3.x may be screwing things up. While I have you here: 1. There's a function called random.shuffle() 2. code:
Sneftel fucked around with this message at 19:10 on Mar 25, 2011 |
# ? Mar 25, 2011 19:07 |
|
Sneftel posted:One, what do you mean by "saved files"? You mean, where open() looks? Two, make sure you're actually running the same version of Python both under IDLE and from the command-line; the change in behavior of input() from 2.x to 3.x may be screwing things up. Also, this is a fresh install of windows, and I only installed 3.2. The program works up until it checks to see if it's 'correct'. In IDLE it works just fine, but when running it on the command line, it will never see it as the correct answer. quote:While I have you here: edit: With random.shuffle(), simply put the word to be shuffled into the () rather than rebuilding it with the while loop (I'm pretty sure the book is trying to hammer in immutable list modifications)? Gothmog1065 fucked around with this message at 19:35 on Mar 25, 2011 |
# ? Mar 25, 2011 19:28 |
|
Gothmog1065 posted:Sorry, I mean where the .py files are stored. I'm still working on small stuff as I learn, and all my smaller programs are stored on my desktop in a /python folder. Is there a way to set it so the IDLE client looks there first (instead of having to locate it manually)? I'm assuming you are starting your program from the start menu. Firstly, programs have something called a working directory. If you launch your program from the command line the working directory will be whatever directory you were in when you launched the program. If you launch your program from the start menu the working directory will be whatever it is set to in the shortcut properties. In the case of IDLE, with Python26 (it's what I have installed), the working directory is set to "C:\Python26". Incidentally, it is not called a working directory in the shortcut properties. Instead it is labeled "Start in:". Anyway, programs generally open to their working directory when you do file->open. You can probably set the working directory to whatever folder your crap is in to get IDLE to default to your folder. Alternatively, and this is what I would do, you could leave an explorer window open to your crap and right-click edit with IDLE whenever you want to open a file. Or you could use an IDE with a built-in file explorer. Gothmog1065 posted:With random.shuffle(), simply put the word to be shuffled into the () rather than rebuilding it with the while loop (I'm pretty sure the book is trying to hammer in immutable list modifications)? The random.shuffle doc string: code:
Observe code:
code:
|
# ? Mar 25, 2011 20:05 |
|
Dren posted:Alternatively, and this is what I would do, you could leave an explorer window open to your crap and right-click edit with IDLE whenever you want to open a file. quote:Oh, and ಠ_ಠ I... I know Sorry if I'm retarding this thread up, but I've always found people to be my best resource for learning.
|
# ? Mar 25, 2011 20:20 |
|
To elaborate on the ways strings, tuples and lists are the same: the only commonality is they are all sequence types, they support subscript access and you can iterate through them. You use them for very different reasons. A tuple is an immutable heterogeneous sequence: you declare it once, you can't assign to elements of it, nor can you append or remove elements. They support subscripting and random access to elements, and they work together with pythons packing/unpacking syntax so you can do things like code:
A string is also immutable but it is a homogenous sequence- every element has to be a one character string. As with tuples, you should never be building up strings with concatenation, and you are not permitted to assign to elements of a string. A list IS mutable, and you'll find yourself using them all over. Lists support fast appending, fast length checking, fast popping (from the right) and fast random access (and assignment!). They have a sort method for sorting in place which is very useful.
|
# ? Mar 25, 2011 20:55 |
|
clearly not a horse posted:Thanks a lot for show reminding me that I have a pretty long way left - a lot here I need to read up on. with X as Y: Some parts of 'with' get complicated, but here's the short version: for functions that support it, you use it to make Y=X for the body of the 'with' block, and when you leave that block, Y gets cleaned up. Files support the 'with' interface, so I used it so work with that file without worrying about closing it -- that lets me simply return instead of breaking out of the loop and hitting a close() at the end. Just some brevity. match() and group(): part of the regular expression system. Basically, regular expressions are a language to let you match parts of strings, which makes for easy parsing. See the 're' documentation for details. Here's a good quick reference PDF (it's for Perl, but the syntax is the same). Regular expressions are amazingly stupendously powerful tools -- you should definitely learn them. setattr(): just a global function in python that lets you set object attributes by string instead of by identifier. So thing.speed = 5 is the same as setattr(thing,'speed',5). Why use the latter? Because you can specify the attribute as a string, i.e. the name of the attribute can itself be in a variable. This was a shortcut I took because your text file field names were the same as your attribute names. I could just as easily have done: code:
|
# ? Mar 26, 2011 01:05 |
|
tripwire posted:A tuple is an immutable heterogeneous sequence: you declare it once, you can't assign to elements of it, nor can you append or remove elements. You do have to be careful though when using mutable types inside of a tuple, i've seen more then one person think that tuple means that the data inside the tuple cannot change ever. code:
|
# ? Mar 27, 2011 02:57 |
|
Fun times:code:
|
# ? Mar 27, 2011 06:32 |
|
Quick question. First, I'm a novice programmer, doing this as a hobby and sucking at it. With python, how do you stop people from looking at your code? I want to write a small simple program that will encrypt a text file, and then decrypt it when the user wants to. Is there way to stop people from looking at my script's code? *A quick google search says no. Any differing opinions? Hughmoris fucked around with this message at 06:00 on Mar 28, 2011 |
# ? Mar 28, 2011 05:57 |
|
I'm trying to pull some numbers out of a bunch of files and then use them to print some figures. But I'm getting this error IOError: [Errno 13] Permission denied: 'C:\\Python27\\Figures' code:
double edit: just learned that pylab.plot does not do quite what I thought it did FoiledAgain fucked around with this message at 08:12 on Mar 28, 2011 |
# ? Mar 28, 2011 07:11 |
|
Hughmoris posted:Quick question. First, I'm a novice programmer, doing this as a hobby and sucking at it. You can obfuscate your code to make it hard to read, and I'm pretty sure there are tools that will bundle the interpreter and your script up into one executable (which will be hard to read as well) but ultimately any encryption method that depends on nobody being able to read your code is a Bad Idea.
|
# ? Mar 28, 2011 08:10 |
|
yaoi prophet posted:You can obfuscate your code to make it hard to read, and I'm pretty sure there are tools that will bundle the interpreter and your script up into one executable (which will be hard to read as well) but ultimately any encryption method that depends on nobody being able to read your code is a Bad Idea. Even the bundling isn't particularly secure against anyone who knows what to look for.
|
# ? Mar 28, 2011 16:37 |
|
I pretty much us Python as a scripting language to replace bash/csh, so quite often I want to run a command, maybe get what it printed to stdout, and maybe get the return value. The subprocess module does all that, but just seems really complicated. Apparently everything is deprecated in 3.x though, so am I stuck with subprocess? Is there another easier way to run unix commands from within Python?
|
# ? Mar 28, 2011 17:31 |
|
at the bottom of the subprocess's 2.7 documentation, there's: http://docs.python.org/library/subprocess.html#replacing-older-functions-with-the-subprocess-module which is helpful for this kind of thing, maybe a few extra characters compared to the old stuff, but not terrible. but honestly, if you're on 2.7 (or I assume 3.x), then check_output does basically everything I'd ever want.
|
# ? Mar 28, 2011 18:24 |
|
Hughmoris posted:Quick question. First, I'm a novice programmer, doing this as a hobby and sucking at it. Make it a web service. People upload the text file and you send it back. That's pretty much the only way.
|
# ? Mar 28, 2011 18:30 |
|
Hughmoris posted:Quick question. First, I'm a novice programmer, doing this as a hobby and sucking at it. Your problem isn't hiding what your code is doing. Your problem is the method of encryption you have chosen.
|
# ? Mar 28, 2011 19:35 |
|
How do I check if an int is less than the length of 2 separate list lengths? Here is what I have now, but it dosnt seem to work.code:
code:
|
# ? Mar 28, 2011 20:06 |
|
zarg posted:How do I check if an int is less than the length of 2 separate list lengths? Here is what I have now, but it dosnt seem to work. You need to check the variable against both lengths. code:
|
# ? Mar 28, 2011 20:12 |
|
zarg posted:How do I check if an int is less than the length of 2 separate list lengths? Here is what I have now, but it dosnt seem to work. You're checking if counter is greater than, not less than.
|
# ? Mar 28, 2011 20:13 |
|
In python as in most other languages, the "or" operator just operates on two boolean expressions. Let's take a look at your original if statement with some clarifying parentheses to make it clear why this matters to you. When you tell pythoncode:
code:
code:
code:
|
# ? Mar 28, 2011 20:15 |
|
Dunno if you've ever used the 'all' or 'any' built-ins, but they're convenient for this:code:
http://docs.python.org/library/functions.html#all Lurchington fucked around with this message at 21:27 on Mar 28, 2011 |
# ? Mar 28, 2011 21:18 |
|
|
# ? Jun 11, 2024 15:31 |
|
king_kilr posted:Fun times:
|
# ? Mar 28, 2011 22:08 |