|
Well, i'll be learning it out of hobby-like interest. I won't be doing anything professional for a long while (I haven't even gone to school, which I plan on). I'm just thinking that 3.0 is my best bet since I won't be really working with it professionally for a long while. Sorry for being vague in the first post, hah.
|
# ? Jan 7, 2009 01:15 |
|
|
# ? May 11, 2024 11:23 |
|
jstirrell posted:The problem is that it doesn't compile currently which I think is due to some syntax error at the end of the code which I can't figure out, but what worries me more is that I still feel like I have no clue if I even have a decent structure for the program I'm trying to write, or where I'm really heading with this. Anyone have any suggestions? What am I doing right? What is wrong? Any general tips for struggling self-teaching noobs like myself? Also, I'm trying to get the general structure of the code down so I realize some of the non-critical details (ie elifs) are missing. There is one syntax error. def newSubject(name): should have self as the first argument. It runs for me after fixing that. If you didn't catch that then it sounds like you're not using an IDE that inspects your code and points out syntax errors, which can be incredibly helpful if you're just starting out. Are you just typing things into IDLE? If you plan to be doing python for a while (or any programming) you should set up an IDE and use it. Everyone has their favorite but a pretty well-established python IDE is Eclipse using the pydev plugin. Go to eclipse.org and download the Classic Eclipse package down at the bottom then google pydev and follow the instructions to get the python plugin installed. The pydev site has a howto for creating projects and things. It might take a while to set up and get the hang of but the code completion and inspection will probably make it worth it.
|
# ? Jan 7, 2009 02:13 |
|
Can someone help me get this stupid mona lisa approximator thing working? I've given up on trying anything remotely fancy and just mutate a list of floats which are the values used to draw triangles on a cairo canvas. I'm using numpy to add up the squared difference between color values on each pixel and square rooting that to get the root mean square error. For some reason my picture ALWAYS converges to some ridiculous garbage in a short amount of time which is frustrating, because it could mean either that my genetic algorithm is hosed up somehow (maybe I made a mistake with passing by reference vs passing by value somewhere?) or that the logic for my root mean square error code is hosed up. Can anyone help me out? code:
|
# ? Jan 7, 2009 07:52 |
|
tripwire posted:Can someone help me get this stupid mona lisa approximator thing working? I've given up on trying anything remotely fancy and just mutate a list of floats which are the values used to draw triangles on a cairo canvas. I'm way too tired to try to read this, but the original didn't use RMS, it used a simple sum of differences. Perhaps that causes garbage convergence? Also it seems like after a few generations every genome has the exact same fitness, which seems to be an error. I doubt the effectiveness of crossover between genomes in this problem domain-- stair-climbing seems better. Scaevolus fucked around with this message at 09:16 on Jan 7, 2009 |
# ? Jan 7, 2009 09:08 |
|
Scaevolus posted:I'm way too tired to try to read this, but the original didn't use RMS, it used a simple sum of differences. Perhaps that causes garbage convergence? Yeah I commented out crossover and now each generation is produced by mutating the best 60 percent of the previous generation and then randomly choosing among those 60 percent other genomes to mutate and add into the genepool. The fact that the fitness gets identical after a few generations is whats wrong but I don't know why, it has to be because of some stupid mistake I made somewhere. There was some discussion in a previous thread here where someone was using particle swarm optimization to try accomplishing the same thing. People in that thread were saying that squaring the differences is likely to give better results because it makes larger differences count more than small differences which makes sense in the context of evolving a better approximation of a picture.
|
# ? Jan 7, 2009 09:38 |
|
I am new to Python and Pygame (and coding). In what module/function should I be looking in order to draw a line at exactly a 45 degree angle from a point?
|
# ? Jan 7, 2009 09:45 |
|
tripwire posted:Can someone help me get this stupid mona lisa approximator thing working? I've given up on trying anything remotely fancy and just mutate a list of floats which are the values used to draw triangles on a cairo canvas. I too tried to implement it(using Pyglet+OpenGL) and I had the same problem, here's my code: http://github.com/alex/evolves/tree/master Perhaps we both have the same error in our code?
|
# ? Jan 7, 2009 10:03 |
|
Swanson Broth posted:I am new to Python and Pygame (and coding). In what module/function should I be looking in order to draw a line at exactly a 45 degree angle from a point? Well what are you you drawing to, a display? Check this out http://www.pygame.org/project/997/?release_id=1737
|
# ? Jan 7, 2009 10:21 |
tripwire posted:
It could be me, but I thought when inheriting genes, one would select a random splice point and then take all material up to said splice from one parent and all material from the splice from the other parent. Here, it looks as if you're alternating your selection of chunks of genes. I.E. Instead of... P1: [ABCDEFG] P2: [HIJKLMN] CH: [ABCKLMN] You're doing... P1: [ABCDEFG] P2: [HIJKLMN] CH: [ABJKLFG]
|
|
# ? Jan 7, 2009 18:11 |
|
Jo posted:It could be me, but I thought when inheriting genes, one would select a random splice point and then take all material up to said splice from one parent and all material from the splice from the other parent. Here, it looks as if you're alternating your selection of chunks of genes. Hmm good point. I think crossover in general is not really applicable to a genome as specified here, I want to eventually replace this with the pyGEP implementation someone has on google code but I was trying to get this working as proof of concept, thats why I have the crossover functionality effectively cancelled out either with comments or just ensuring its not called. There has to be something wrong with the way I extend the parents genes or something that ends up making all the genomes identical inspite of the constant mutations. Can you try the code and see if you can get it to work? I'm sure this is like a stupid mistake where a list is made by reference rather than by value or something, theres no reason for the genepool to end up identical in perpetuity in the face of constant mutations.
|
# ? Jan 7, 2009 18:22 |
|
I'm trying to use Tkinter to get a single line of input, then turn that line into a tuple, parsed by the commas that the user enters.code:
|
# ? Jan 7, 2009 22:13 |
|
If anyone cares I think I just about got it, although it seems to be very slow. Heres a couple of minutes: and a little further along: tripwire fucked around with this message at 09:07 on Jan 8, 2009 |
# ? Jan 8, 2009 07:50 |
|
Kire posted:I'm trying to use Tkinter to get a single line of input, then turn that line into a tuple, parsed by the commas that the user enters. portfolio = [(self.e.get().split(','))] Although it's a lot clearer if you just do: code:
whoknew fucked around with this message at 08:33 on Jan 8, 2009 |
# ? Jan 8, 2009 08:20 |
|
I've been doing the Euler problems after I read about them (here in fact) the other day. Here is the code I used on number 47 (basically it involves calculating prime factors):code:
In the answer forum someone posted this: code:
Ninja edit: My question, of course, is why is his code so much faster when they seem identical?
|
# ? Jan 8, 2009 18:53 |
|
UnNethertrash posted:Ninja edit: My question, of course, is why is his code so much faster when they seem identical? I read his code. After the second time through it was clear to me what he was doing. Very straightforward, simply accomplished. I've tried to read through your code four or five times now. Each time I give up because the spaghetti logic is simply too much. I have a question for you and a recommendation. Question: If your code is running so much slower than this person's code, are your if statements really improvements? If you determine that they're not improvements of the most important type, perhaps you could go back to before you added them in, and optimize there. Recommendation: If you were truly interested in improving your ability, rather than continue on solving each of these Euler problems, none of which will pose any real challenge to you, you would start over at the beginning and play golf with each of the problems. What is the shortest amount of code you can solve each of the problems in? I think this exercise will do you a lot more good. functional fucked around with this message at 19:42 on Jan 8, 2009 |
# ? Jan 8, 2009 19:25 |
|
functional posted:I read his code. After the second time through it was clear to me what he was doing. Very straightforward, simply accomplished. I've tried to read through your code four or five times now. Each time I give up because the spaghetti logic is simply too much. I have a question for you and a recommendation. Thanks, I will try redoing the if blocks and see what happens. I didn't look at them before because I assumed that the processing of if statements would be trivial compared to the time necessary to factor numbers. Perhaps I was wrong about that. I realized the same thing as your recommendation, which is why I started looking at other peoples code to begin with. Thanks.
|
# ? Jan 8, 2009 21:20 |
|
tripwire posted:Note that a leading __ does not make private attributes. It invokes name mangling, which is most definitely not what you want. It's only useful if you're trying to prevent naming conflicts in subclasses. If you're trying to annotate an attribute as private, use a better lexical convention: a leading _. Everyone knows what you mean and it doesn't mangle names.
|
# ? Jan 9, 2009 06:24 |
|
Habnabit posted:Note that a leading __ does not make private attributes. It invokes name mangling, which is most definitely not what you want. It's only useful if you're trying to prevent naming conflicts in subclasses. If you're trying to annotate an attribute as private, use a better lexical convention: a leading _. Everyone knows what you mean and it doesn't mangle names. Thanks for the heads up. I knew I read before that you were supposed to use just one underscore for private names but apparently I'm an idiot and forgot. Using it on the functions i want to override is ok though right? I based most of this off of a neuroevolution package I used to use because I had no idea where to start so theres still some boneheaded artifacts of my unthinking copy+pasting in there. My population class has its own iteration and comparator methods though, in that case double underscores are correct right? Like if you are trying to override a builtin function?
|
# ? Jan 9, 2009 06:33 |
|
tripwire posted:Thanks for the heads up. I knew I read before that you were supposed to use just one underscore for private names but apparently I'm an idiot and forgot. Using it on the functions i want to override is ok though right? I based most of this off of a neuroevolution package I used to use because I had no idea where to start so theres still some boneheaded artifacts of my unthinking copy+pasting in there. Yes, name mangling only takes effect if there's a leading __ with no trailing __. So, __iter__ et al. are safe. Regarding your code itself, I haven't used (or even installed) cairo myself, so I unfortunately can't be much help there. e: Oh, I missed a part of your question. No, you don't want to use name mangling if you want to override the methods. In fact, name mangling is intended to prevent things from being (implicitly) overridden. Habnabit fucked around with this message at 09:54 on Jan 9, 2009 |
# ? Jan 9, 2009 09:49 |
|
Habnabit posted:Yes, name mangling only takes effect if there's a leading __ with no trailing __. So, __iter__ et al. are safe. Hrm, guess I've misunderstood. I'm a little confused, can you help me to understand the reasons why its used in this code? http://code.google.com/p/neat-python/source/browse/trunk/neat/population.py Thats the thing I'm ripping off. The writer specifically prefixes function definitions and private variables with double underscores, in some cases with trailing double underscores as well, but only for functions which I assume are inherited from object? Right here: code:
|
# ? Jan 9, 2009 14:42 |
|
It could just be overzealous use. I've seen developers do that in other projects, perhaps thinking __foo is like "private" in other languages (it isn't). I haven't delved into the code there deep enough to see if there are complex inheritance issues that really warrant it. For what it's worth, I almost never use name mangling.
|
# ? Jan 9, 2009 15:27 |
|
tripwire posted:Whats the idea of that? I thought it was so then when you call len() on a population object it would use the __len__ definition provided above. Yes, that's what it is. The convention is:
An example of this would be: code:
code:
However, that certainly doesn't mean that you shouldn't use __ as private. That's exactly what it's for. The real lesson here is that with the duck typing that Python uses, a "real" private member isn't nearly as useful at it seems when you're working in a static language like Java or something. So just use _private unless you find that you truly need __private. Regarding __magic__. Things that start with double underscore and end with double underscore are lovingly called "magic" attributes. They do things like you noticed, interacting with built in functions. Generally speaking, you shouldn't ever name your own stuff __magic__, instead, you should just use those whenever you want to do something "magic" that you found in the Python documentation. tbradshaw fucked around with this message at 20:00 on Jan 9, 2009 |
# ? Jan 9, 2009 19:46 |
|
I've got a small problem with Python 2.6 on my Vista computer. I have a Python script 'foo.py' containing the following code:code:
On my laptop running XP and Python 2.6 the latter prints "['F:\\Code\\Python\\foo.py', bar]", which again is what one would expect. Why the strange behavior on my Vista computer? Edit: This was caused by some major problems with the .py file associations, fixed by manually editing the relevant registry keys. Id4ever fucked around with this message at 00:10 on Jan 10, 2009 |
# ? Jan 9, 2009 23:07 |
|
tbradshaw posted:Using __dict__ isn't necessary. __foo in class Bar turns into _Bar__foo, and since that doesn't have two leading underscores, subclasses can use self._Bar__foo to access the attribute just fine. Overzealous use is definitely the problem here, because it just makes it a pain in the rear end for people who have to use the library later on. Another caveat: this is done by the compiler, so getattr(self, '__foo') sidesteps the name mangling.
|
# ? Jan 10, 2009 03:29 |
|
Thanks for the help guys, I think I understand now. I tidied up my code a bit but now I've run into another problem as I started adding in things- rather than steadily ratcheting up in similarity over time, the fitness seems to fluctuate wildly now with no clear indication of progress. Is there some obvious error anyone can see? I screwed up before because I stupidly didn't realize that doing code:
http://pastebin.com/m109364c Its frustrating trying to figure out where I screwed up, I'm sure there has to be a much more elegant way of accomplishing what I'm doing in python than what I've got.
|
# ? Jan 10, 2009 06:30 |
|
code:
|
# ? Jan 10, 2009 07:20 |
|
dorkanoid posted:
I can get as far as code:
Using a lambda and map I guess?
|
# ? Jan 10, 2009 08:23 |
|
tripwire posted:How do you build a list comprehension that would mimic the functionality of going through every gene in the genome and conditionally applying a mutation if random.random() is greater or less than than some threshold value? code:
|
# ? Jan 10, 2009 08:49 |
|
I don't see what's wrong with this:code:
code:
Also, just doing map(foo, self.genome) won't do a thing because map doesn't modify the list, it just returns a new copy, so you have to do self.genome = map(foo, self.genome) hey mom its 420 fucked around with this message at 16:39 on Jan 10, 2009 |
# ? Jan 10, 2009 16:37 |
|
Honestly I forgot you could do the if clause there and not just on the for portion of the list comprehension. That should be fine
|
# ? Jan 10, 2009 21:16 |
|
Bonus posted:I don't see what's wrong with this:
|
# ? Jan 10, 2009 22:23 |
|
Ok, I tried to tidy up my code some more and use list comprehensions and generators where applicable, but theres still some dumb mistake somewhere that is preventing it from working. Can anyone take another look and see if anything looks off? http://codepad.org/qIW3uWEp I'm stumped. It should generally be going UP in fitness over time because its always removing the worst of the population and the majority of mutations should have negligible effect on fitness, yet the fitness always seems to fluctuate wildly with no apparant direction. I think my code for mutation must be screwed up somewhere but I can't figure out where. Aw what the hell... I just checked generation by generation to see what was going on and it looks like every member of the new generation is a clone, or they all receive the same mutations. How can this be? Garghfghdfj (each row is a new generation) --------- update: Hurray!! I realized that code:
What this ended up doing was making 10 children whose genomes all pointed to genes that existed in someone elses genomes. Obviously I needed to copy them rather then just refer to them so I gave my genes a copy class which simply returns a new instance with the same members. I feel so good about finally getting this to a workable state! tripwire fucked around with this message at 08:30 on Jan 11, 2009 |
# ? Jan 11, 2009 00:52 |
|
I am attempting to develop a portion of a GUI using Tkinter. I have a listbox where I select an image file, the file is loaded and then displayed on a canvas. I am having a difficult time getting the image created within the subfunction to display in the canvas. I initialize the canvas with a logo for the program, but I can't get my callback function for the listbox to update the image on the canvas. It just remains blank. code:
|
# ? Jan 11, 2009 19:52 |
|
I have a bunch of hostnames that I need to check if they exist in DNS. I'm not connecting to them (that is done later by passing it on to ssh), I just need to verify that they're there. I'm using plain socket.gethostbyname() which works well, but the problem is that hostnames that do not exist take forever to time out and throw the "not found" exception. Well, not forever, but a few seconds which add up to a whole lot when doing lots of hosts. All valid names resolve very quickly (usually <10ms, since it's all nearby dns zones) and non-existend ones return NXDOMAIN just as fast (checked using dig). I would like to set a short timeout (of, say, 100ms), and if it does not get a response in that time just say that it isn't there, but I'm not sure how to control that timeout. socket.setdefaulttimeout() seemingly has no effect on socket.gethostbyname() or socket.getaddrinfo(). I'm not very skilled with threads in Python, but would it work to start a thread to do the gethostbyname() call, and if it doesn't respond in time just kill it off? Would it be possible to run several such calls in parallel, or could there be something thread-unsafe about that (especially when killing them off)? I've read something about the C function for gethostbyname() being thread-unsafe in some way, but I have no idea about how that turns out in Python. ionn fucked around with this message at 00:01 on Jan 12, 2009 |
# ? Jan 11, 2009 23:59 |
|
twisted has a lot of non blocking type stuff and has a dns specific implementations if i recall correctly.
|
# ? Jan 12, 2009 01:13 |
|
Seconding this. Don't bother with threads when twisted provides an async DNS resolver already.
|
# ? Jan 12, 2009 01:28 |
|
WickedMetalHead posted:twisted has a lot of non blocking type stuff and has a dns specific implementations if i recall correctly. Habnabit posted:Seconding this. Don't bother with threads when twisted provides an async DNS resolver already. Twisted looks like the way to go for lots of network-related stuff, there are many other cases where I could imagine it being very useful to me. I'm building the dns-resolution thingy, it still takes some thread-awareness (a thread-safe place to keep track of outstanding requests) but shouldn't be too difficult.
|
# ? Jan 12, 2009 12:39 |
|
Has anyone used the multiprocessing module before? I'm trying to use a pool to split up the work in my mona lisa triangle mover thing but it doesn't look like you can call it from an instance of a class and point to an instance method (it raises a pickle error, 'can't pickle instancemethod' or something like that. Do I have to make it use a global function? Is there some idiom or pattern for doing this?
|
# ? Jan 13, 2009 02:19 |
|
tripwire posted:Has anyone used the multiprocessing module before? I'm trying to use a pool to split up the work in my mona lisa triangle mover thing but it doesn't look like you can call it from an instance of a class and point to an instance method (it raises a pickle error, 'can't pickle instancemethod' or something like that. Do I have to make it use a global function? Is there some idiom or pattern for doing this? I haven't, but I know one poster here has it's m0nk3yz, he was like the main guy who edit: fixed No Safe Word fucked around with this message at 10:02 on Jan 13, 2009 |
# ? Jan 13, 2009 02:48 |
|
|
# ? May 11, 2024 11:23 |
|
tripwire posted:Has anyone used the multiprocessing module before? I'm trying to use a pool to split up the work in my mona lisa triangle mover thing but it doesn't look like you can call it from an instance of a class and point to an instance method (it raises a pickle error, 'can't pickle instancemethod' or something like that. Do I have to make it use a global function? Is there some idiom or pattern for doing this? Yes, it has to be a pickle-able function/class instance/etc. Make it a function, and you're golden. choo choo! I'm not good with the GUI stuff, so I will help where I can. Also, I didn't write it, I just got it into python.
|
# ? Jan 13, 2009 03:25 |