|
ashgromnies posted:
list.append runs in constant time. In the first case, you're completely rebuilding the tuple every time you're "appending" to it. Tuples aren't lists, and shouldn't be used like lists. As has been mentioned, tuples are immutable and hashable, which means they can be used as dictionary keys and in a myriad of other places where lists can't. (Also, both run very slow because you're building a list and then iterating over it. The second case is redundant; you can just type 'array = range(1000000)' though, as has been mentioned, it's really a list. If you want to iterate over a sequence of numbers like that, use xrange.)
|
# ? May 30, 2008 13:17 |
|
|
# ? May 10, 2024 03:39 |
|
Habnabit posted:As has been mentioned, tuples are immutable and hashable, which means they can be used as dictionary keys and in a myriad of other places where lists can't. It was my understanding that they are only hashable if they include only hashable elements, although I could be completely wrong.
|
# ? May 30, 2008 15:55 |
|
After learning that you can use tuples as hash keys it took another tenth of a second off my program execution time on average! Woo! So I feel REALLY embarrassed for asking this one, but how can you put variables inline? I'm writing some CSS out from a Python script(eventually I will do this the right way with templating, this is just for a really quick prototype though so it's not necessary at the moment) and I need to embed variables and it looks really ugly: code:
Suggestions?
|
# ? May 30, 2008 18:47 |
|
ashgromnies posted:After learning that you can use tuples as hash keys it took another tenth of a second off my program execution time on average! Woo! You really do want Templating. Or at least the basic String Formatting Operations. code:
code:
Scaevolus fucked around with this message at 19:06 on May 30, 2008 |
# ? May 30, 2008 19:02 |
|
Scaevolus posted:You really do want Templating. Yeah, I know I want templating but this is just a one-off for now to test my algorithm and will go away so I don't want to put too much effort into it - just enough to be readable. This is eventually going to be called from a Django application. I didn't know Python had an equivalent of sprintf! Thanks!
|
# ? May 30, 2008 19:53 |
|
Also, it might do you good to use the named string formatting for more readability.code:
|
# ? May 30, 2008 19:58 |
|
ashgromnies posted:Yeah, I know I want templating but this is just a one-off for now to test my algorithm and will go away so I don't want to put too much effort into it - just enough to be readable. This is eventually going to be called from a Django application. If this is for use in a Django app, you should probably take a look at the Django templating language.
|
# ? May 30, 2008 20:35 |
|
king_kilr posted:If this is for use in a Django app, you should probably take a look at the Django templating language. Yeah, I know the Django templating language. It's pretty easy. Believe me, I know alllll about templating and what it gives you, this is just for a one-off for prototyping and I'd never really do this in an application
|
# ? May 30, 2008 20:36 |
|
Bozart posted:It was my understanding that they are only hashable if they include only hashable elements, although I could be completely wrong. That's correct. Tuples hash the hashes of their contents.
|
# ? May 31, 2008 21:53 |
|
How do I get the indexes of every instance of a given character in a given string? string.find seems to only give the index of the first instance. Flea110 fucked around with this message at 21:03 on Jun 2, 2008 |
# ? Jun 2, 2008 21:01 |
|
Flea110 posted:How do I get the indexes of every instance of a given character in a given string? code:
|
# ? Jun 2, 2008 21:08 |
|
code:
|
# ? Jun 2, 2008 23:47 |
|
And if you want the count of every character in a string:code:
code:
Habnabit posted:Fixed in bold. Ahh crap I did the wrong import meant to from collections import defaultdict. And you're entirely right about __getitem__, my brain farted. deimos fucked around with this message at 02:19 on Jun 3, 2008 |
# ? Jun 2, 2008 23:52 |
|
deimos posted:And if you want the count of every character in a string: Fixed in bold.
|
# ? Jun 3, 2008 02:11 |
|
Benji the Blade posted:I am by NO means a PyPy expert, though I do think it's pretty cool. One of the things the pypy people mentioned was improving the performance and portability of the interpreter to the point where it would be faster and more compatible than ironpython/cpython/jython; specifically they mentioned picking up where psyco left off in terms of speeding up cpu-bound code. I realize one of the stated goals of pypy over psyco is that its not as dependant on architecture, but will pypy fully support 64-bit processors/code, and how easily will it mesh with parallel python or pyprocessing? I never even bothered to download and compile python in 32-bit compatibility mode to take advantage of psyco, and if I did I suspect it wouldn't play nice with parallel python, although I haven't tried it yet. In a nutshell, does anyone know if the devs of pypy going to make it work out of the box on multicore systems on a 64-bit os?
|
# ? Jun 3, 2008 03:44 |
|
tripwire posted:This is a really cool post; I have a somewhat dumb followup question that you or someone else might be able to answer for me. I think the problem right now for PyPy is turning itself from little more than a science project into something more concrete. Right now they're still fiddling around with a lot of stuff (see: http://morepypy.blogspot.com/) and either they're lacking resources or focus. Of course, maybe I'm too focused on shipping stuff. As for how well it would mesh with PP and PyProcessing: We'll see. It looks like my Pep (371) is getting accepted, and some of the ramifications of that may be some work on my part (or others) to port the module to jython/ironpython/pypy - but given the lack of GIL on the first two, it may not need to be ported.
|
# ? Jun 3, 2008 14:21 |
|
tripwire posted:I'm modifying the source of neat-python, which is a pretty decent implementation of NEAT to use. I know this was a ways back, but thanks for this -- I've been going through the papers on NEAT and reading some of the python code and it is exactly what I have been looking for.
|
# ? Jun 3, 2008 15:33 |
|
I haven't done much Python before, so I was playing around with reimplementing some PHP experiment code I'd made. What the PHP code did was use reflection to take a method of a class, discover its arguments, and then allow a http GET to call that method, using the GET arguments as arguments for the function. I worked out how to discover the number of arguments of a method/function in Python, but it doesn't seem to have a way to discover the names of the arguments - anyone know if there would be a way to do this? I realize that in many languages, this probably doesn't make sense, since the names of arguments are just labels local to the definition of the function. If anyone has a nice guide on reflection in Python, it'd be appreciated - I could only find some rather obtuse references that led me to find im_func and so on, which I have to admit is slightly easier than PHP5's reflection methods.
|
# ? Jun 3, 2008 16:03 |
|
chips posted:I worked out how to discover the number of arguments of a method/function in Python, but it doesn't seem to have a way to discover the names of the arguments - anyone know if there would be a way to do this? I realize that in many languages, this probably doesn't make sense, since the names of arguments are just labels local to the definition of the function. If anyone has a nice guide on reflection in Python, it'd be appreciated - I could only find some rather obtuse references that led me to find im_func and so on, which I have to admit is slightly easier than PHP5's reflection methods. You can do it, but it's not for the faint of heart. code:
I've never really messed with it, but I am marginally familiar with tooling around with func_code stuff because of this little tutorial/link on exploring Python bytecode: http://thermalnoise.wordpress.com/2007/12/30/exploring-python-bytecode/ edit: haha, and right after I made the post I noticed this: code:
No Safe Word fucked around with this message at 16:28 on Jun 3, 2008 |
# ? Jun 3, 2008 16:16 |
|
That's great, thanks a lot.
|
# ? Jun 3, 2008 17:20 |
|
Oh, so this is what Python is all about.
|
# ? Jun 3, 2008 23:01 |
|
Bozart posted:I know this was a ways back, but thanks for this -- I've been going through the papers on NEAT and reading some of the python code and it is exactly what I have been looking for. OK so I'm a total newbie and an idiot and basically I do everything wrong all the time. I downloaded the neat-python code from the google code cvs, but I can't get any of the examples to run. I'm running python 2.5.2. When I do import neat in IDLE, from the directory above where I have the neat folder, it runs without a problem, but it doesn't do anything - the __init__.py file is empty. And when I try to run the setup.py function that comes with it, it just errors out. And when I try to run the examples it can't import anything from neat. Normally I would (and tried) to sit down and go through what could be going wrong, but for a new language with a new package, with no documentation, I am at a loss. I don't know if I maybe got the wrong files from google, or what?
|
# ? Jun 4, 2008 00:06 |
|
From the commandline, try navigating to the code:
code:
|
# ? Jun 4, 2008 01:03 |
|
So I'm writing a method in a class that I want to be able to be accessed without instantiating the class, or if it's called from an instance of the class to use the variables inside the class. Here's what I'm trying to do(and it's not working): code:
code:
|
# ? Jun 4, 2008 04:09 |
|
ashgromnies posted:I'm not sure whether this is it, but there's at least one thing to try: code:
Habnabit fucked around with this message at 04:21 on Jun 4, 2008 |
# ? Jun 4, 2008 04:19 |
|
Bonus posted:From the commandline, try navigating to the Thanks for the pointer. I am now somewhat closer to my goal, however I wasn't kidding when I said I basically screw everything up. It gives an error about having been built in VS2003 (the extensions for neat-python are written in c++) and that extensions have to be built using the same compiler. I don't own VS2003, but it also said that you can build it with mingw32. OK, sweet, I downloaded and installed mingw32, and then tried again with the additional option code:
So I installed cygwin and then installed python under it, and installed gcc as well. Things compiled fine but now I can't use eclipse. (this is making me think I am doing things very wrong) Also, when I tried to run the example/single_pole.py it tries to import genome2.py, I think, and that file or directory just doesn't exist. Help me please!
|
# ? Jun 4, 2008 04:45 |
|
ashgromnies posted:
In Python, 0 evaluates to False. (What you want is colorhash.has_key('r'))
|
# ? Jun 4, 2008 04:52 |
|
Bozart posted:Thanks for the pointer. I am now somewhat closer to my goal, however I wasn't kidding when I said I basically screw everything up. You can run the setup.py file which will throw all the modules into your python base installation; this can sometimes mess with eclipse if the pythonhome and pythonpath environment variables get changed (try resetting or unsetting them if eclipse complains). The way I usually run fitness tests is with Breve, (a simulator with its own prepackaged python interpreter). Since breve won't let me load my own python modules due to wanting 32-bit python and me having 64-bit python, to get around that I throw my source code (the entire "neat" directory) into the directory where my simulation file is. That way a call to "import neat" in my simulation file will cause python to check locally before looking for neat in your python home directory. You can do this as well if you like. If you are patient I'll show you my changes to the code and go over any questions you have about getting it to work; usually the only problem is the pythonpath and pythonhome variables. I'm just going to clean it up a bit and I'll upload it later.
|
# ? Jun 4, 2008 04:56 |
|
Habnabit posted:I'm not sure whether this is it, but there's at least one thing to try: But that doesn't let me call that without instantiating an instance of the class. Something similar to this is what I need, I think, I'm just having trouble figuring out where to take it: code:
Thanks for telling me about has_key, I didn't know that! I would have been scratching my head if a bug due to that came up. Chewing through the docs, I found another very important fact that I will need to watch out for because I'm treading on dangerous water in regards to it now: http://docs.python.org/ref/function.html posted:Default parameter values are evaluated when the function definition is executed. This means that the expression is evaluated once, when the function is defined, and that that same ``pre-computed'' value is used for each call. This is especially important to understand when a default parameter is a mutable object, such as a list or a dictionary: if the function modifies the object (e.g. by appending an item to a list), the default value is in effect modified. This is generally not what was intended. A way around this is to use None as the default, and explicitly test for it in the body of the function I really need to check out Python unit testing. It looks like you can just write the expected output from calling it in the interactive interpreter, which is really cool. Anyone have experience? ashgromnies fucked around with this message at 06:04 on Jun 4, 2008 |
# ? Jun 4, 2008 05:44 |
|
A tuple is immutable, it isn't an issue there. And you want @classmethod:code:
nose is nice too. With nose you can just write functions starting with test_ and use assert.
|
# ? Jun 4, 2008 06:03 |
|
Milde posted:A tuple is immutable, it isn't an issue there. And you want @classmethod: I keep forgetting tuples are immutable, oops. Thanks. The class method lets me run it on the class, yeah, but then I can't run it based on the values inside of an instance. I'd have to make two methods(one for class, one for instance) with different names and I don't want to do that because I'm a lazy programmer with a poor memory and two methods is too many to remember(or I just want to do this to figure out how). I had a crackpot scheme, this is probably the wrong way to do it. What if I defined a "getLight" class method on Pixel that expected a tuple and did the work required on it and returned the results. Then in the __init__ method I could dynamically override the getLight method to be an instance method that would calculate it based on the values in self. Is that correct or is there a better way? ashgromnies fucked around with this message at 06:22 on Jun 4, 2008 |
# ? Jun 4, 2008 06:06 |
|
What are you trying to accomplish? Why can't you have an instance method that returns a new instance? You can't have a class method that accesses an instance if it doesn't have an instance to access. I think you're trying to make whatever you're doing more complicated than it needs to be. Maybe you mean something like this? code:
|
# ? Jun 4, 2008 06:13 |
|
Scaevolus posted:In Python, 0 evaluates to False. (What you want is colorhash.has_key('r')) Is there really ever a reason to use has_key now that you can just use in? (Honest question, not snarky contrary response) code:
code:
|
# ? Jun 4, 2008 06:17 |
|
has_key is apparently technically slower than in, but I personally don't use it for style reasons. It's easier to read the operator than the method call, at least in my opinion, and it matches up with membership tests for sequences. It's also slated for removal in Python 3.0.
|
# ? Jun 4, 2008 06:23 |
|
I'm unsure of where you were going with your last example, but I got an example of the behavior I wanted working:code:
|
# ? Jun 4, 2008 06:25 |
|
I still have no idea what you're trying to accomplish, but it looks like you're making things more complicated than they need to be. And while we're talking about style, you should be using new-style classes (e.g. class TestClass(object):) and you should just assign the attribute directly, instead of using __dict__.
|
# ? Jun 4, 2008 06:31 |
|
ashgromnies posted:I'm unsure of where you were going with your last example, but I got an example of the behavior I wanted working: That's a little messy. Why don't you just do it like this? code:
|
# ? Jun 4, 2008 06:34 |
|
Habnabit posted:That's a little messy. Why don't you just do it like this? I don't really like that because I can't call getLight on the Pixel class so it makes it harder to encapsulate. I would need to import that method from the file Pixel is stored in as well. I'd rather have it all in one class if I could. I will check out types and try to replace new with it. Milde posted:I still have no idea what you're trying to accomplish, but it looks like you're making things more complicated than they need to be. Ahh, thanks. I don't see how I'm making things more complicated - it makes them easier and more reusable in the end, in my opinion. The Pixel class contains methods that act on Pixels. Sometimes I don't actually have an instance of a Pixel and don't need to make one because I won't be using it any more but would like to perform certain operations on an arbitrary (r,g,b) tuplet. ashgromnies fucked around with this message at 06:43 on Jun 4, 2008 |
# ? Jun 4, 2008 06:41 |
|
ashgromnies posted:I don't really like that because I can't call getLight on the Pixel class so it makes it harder to encapsulate. I would need to import that method from the file Pixel is stored in as well. I'd rather have it all in one class if I could. No you wouldn't. Everything in a module, including functions and classes, is created aware of its surroundings. If it was in a separate module, it wouldn't check the caller's global namespace; that wouldn't make sense. It would check the global namespace in which it was defined. Besides, it's rather weird to do it the way you want. Why not then just make one function and call that? code:
e: stupid textmate, mixing tabs and spaces. ashgromnies posted:I don't see how I'm making things more complicated - it makes them easier and more reusable in the end, in my opinion. The Pixel class contains methods that act on Pixels. Sometimes I don't actually have an instance of a Pixel and don't need to make one because I won't be using it any more but would like to perform certain operations on an arbitrary (r,g,b) tuplet. Habnabit fucked around with this message at 06:48 on Jun 4, 2008 |
# ? Jun 4, 2008 06:43 |
|
|
# ? May 10, 2024 03:39 |
|
Habnabit posted:No you wouldn't. Everything in a module, including functions and classes, is created aware of its surroundings. If it was in a separate module, it wouldn't check the caller's global namespace; that wouldn't make sense. It would check the global namespace in which it was defined. I don't want to do that because getting lightness is a function associated with pixels so I want it to be in their class. This is what I meant by "I would need to import that method": code:
Habnabit posted:What might seem easier or more intuitive can sometimes make more fragile code. I've seen some pretty broken APIs that rely on isinstance or type calls to try to determine what to do with the arguments passed to a method, effectively limiting themselves in the name of "convenience." Maybe I'm not going about this in the Python way. I'm not really sure how people normally solve this problem in Python but I like being able to call methods on the class since it performs logic that only makes sense in the context of that class. ashgromnies fucked around with this message at 06:54 on Jun 4, 2008 |
# ? Jun 4, 2008 06:50 |