|
Haystack posted:It's not possible using the example you provided Not that you'd ever want to do it, but it actually is possible: code:
|
# ? Mar 17, 2010 03:40 |
|
|
# ? Jun 1, 2024 18:50 |
|
So thanks for the recommendations for Eventlet! It's awesome. Anyway, I'm receiving commands over a socket. For example, one command may be the text "!serveryell". What's a good way to execute the correct function for the !serveryell command? Right now, I have a dict with {"!serveryell": "serveryellfunc()"} and after looking up the command in the dict, I eval("serveryellfunc()"). This feels really lovely to me. I don't really want to code in a bunch of if statements with each command so that it's easier to add new commands.
|
# ? Mar 17, 2010 23:05 |
|
Thermopyle posted:So thanks for the recommendations for Eventlet! It's awesome. code:
|
# ? Mar 17, 2010 23:43 |
|
Avenging Dentist posted:
Oh yeah...duh. Thanks.
|
# ? Mar 18, 2010 00:07 |
|
Avenging Dentist posted:
am I the only one here that would do this instead: code:
|
# ? Mar 18, 2010 00:29 |
|
Yes, you're the only one, and you're a big weirdo too.
|
# ? Mar 18, 2010 00:32 |
|
nbv4 posted:am I the only one Yes, you are terrible for it. All you're doing is obscuring intent, __call__ says to me that you're trying to do something clever, possibly work around the behavior or something (lord knows what), () tells me you want to call a loving function.
|
# ? Mar 18, 2010 01:17 |
|
code:
Is how I've done it before
|
# ? Mar 18, 2010 02:10 |
|
king_kilr posted:Yes, you are terrible for it. All you're doing is obscuring intent, __call__ says to me that you're trying to do something clever, possibly work around the behavior or something (lord knows what), () tells me you want to call a loving function. what males you think callable.__call__() is not the exact same thing as callable()? The reason I do it is because one time I had a line that was something like: code:
code:
|
# ? Mar 18, 2010 06:29 |
|
nbv4 posted:what males you think callable.__call__() is not the exact same thing as callable()? The reason I do it is because one time I had a line that was something like: I know *exactly* what __call__ does, that is not mutually exclusive with also thinking that using it is simply obscuring the intent of the code.
|
# ? Mar 18, 2010 08:13 |
|
king_kilr posted:I know *exactly* what __call__ does, that is not mutually exclusive with also thinking that using it is simply obscuring the intent of the code. It's not very common for a dict to return a function object. It's actually not that common for anything to return a callable. Therefore its easy to assume that in my code example that an operator needs to go between the get(...) and the (var+foo). You said previously that using __call__ implies that you're doing something clever. Thats exactly whats going on here. I'm calling the return value of another callable. To me thats pretty darn clever.
|
# ? Mar 18, 2010 09:21 |
|
nbv4 posted:To me thats pretty darn clever. If you think that's clever, you must constantly be wowed by every little thing in the world. Basically, if you're directly calling Python magic functions, you're probably being dumb and no one else in the world is going to want to look at your code. This would be like typing foo.operator + (bar) every time you wanted to add foo and bar together in C++. (EDIT) More to the point: if you wanted to maximize readability, why would you not use an intermediate variable instead of barfing up a bunch of underscores? Avenging Dentist fucked around with this message at 09:28 on Mar 18, 2010 |
# ? Mar 18, 2010 09:24 |
|
Avenging Dentist posted:If you think that's clever, you must constantly be wowed by every little thing in the world. I only used "clever" in my post because thats the word king_kilr used. "something not obvious at first glance" would work better. And if you read a few post up, I agree using intermediate variables is better than tacking on ()'s after a callable.
|
# ? Mar 18, 2010 09:37 |
|
nbv4 posted:Calling callables __call__ callcallcallcallcallcallcall As someone much less experienced than most here, and who spends a lot of time trying to figure out other people's code, I would definitely find your usage confusing. I'd be thinking "What did I miss here? Why didn't he just use ()? What's going on?!?!"
|
# ? Mar 18, 2010 17:27 |
|
Here's a design question: I have a funky array class that stores 1) a regular (NumPy) array, and 2) an array of elements related to those in (1), pointed to by entries in an index buffer, pointed to by ranges in an offset array. Example: Suppose I have 3 keys (this is (1)): [1, 2, 3]. I also have 6 values: [10, 11, 12, 13, 14, 15]. 1 is related to [10, 11, 12, 13], 2 is related to [11, 12, 13, 14], and 3 is related to [12, 13, 14, 15]. So here's how the data looks: code:
But, and here's the part where my question comes in, sometimes I may want just the values. Or just the indices. Or the keys + the indices. I can do all these with functions, but then you end up with a class where accessing an element sometimes uses [] and sometimes uses (). It also makes it impossible to iterate over just the values. Would it be entirely too "clever" if I wrote a decorator to turn a function getvalues() into a quasi-array with indexing, len, iterators, and all that? EDIT: Ok nevermind on all this. I realized it made more sense to return a tuple containing (keys, MyClass(offsets, indices, values)) and let the user use zip as necessary. Avenging Dentist fucked around with this message at 23:45 on Mar 18, 2010 |
# ? Mar 18, 2010 21:45 |
|
Why doesn't this work:code:
{"Maximum": [[1,2,3],[3,4,5],[6,7,8]...] ...} I get code:
mediocre dad okay fucked around with this message at 03:46 on Mar 19, 2010 |
# ? Mar 19, 2010 03:42 |
|
Kulebri posted:Why doesn't this work: The outermost for is not part of a list comprehension. That syntax is only for list comprehensions.
|
# ? Mar 19, 2010 03:49 |
|
Avenging Dentist posted:The outermost for is not part of a list comprehension. That syntax is only for list comprehensions. How do I make it part of one?
|
# ? Mar 19, 2010 04:20 |
|
Kulebri posted:How do I make it part of one? Just put the outer for on the previous line. Stop trying to be clever. (Do you really want a list of lists of all the lines plotted? I doubt it.)
|
# ? Mar 19, 2010 04:30 |
|
Kulebri posted:How do I make it part of one? List comprehensions are when you want to make lists. If you want a loop, just use a loop. code:
|
# ? Mar 19, 2010 04:33 |
|
Ok so revenge of the offset array thing. Here's the deal: I have two NumPy arrays: an array of data, and an array of offsets into that data. Put these together and you get something that looks like an array of (jagged) arrays. Each consecutive pair of elements in the offsets represents the begin and end of that chunk: [begin, end). If your outer dimension is N, then len(offsets) == N+1. Mostly for fun, I want to make arbitrary slices on this work (just for the outer array). Slices of consecutive elements is easy, but non-consecutive is hard. I can do the offsets in a fairly sane way, but I feel like there's room for improvement on the data array. Here's the code (pretend that chop is part of a __getitem__ function in some class; s is the slice). Any ideas? EDIT: Ok I updated the code a bit but it could still use improvement I think (also I fixed the stupid typos from 1:30 AM) EDITx2: Ok updated again. I think this is as good as I can get without dropping into C. Avenging Dentist fucked around with this message at 20:52 on Mar 19, 2010 |
# ? Mar 19, 2010 07:32 |
|
google have released the complete materials for a two day course in python (video and accompanying text) http://code.google.com/edu/languages/google-python-class/
|
# ? Mar 20, 2010 01:49 |
|
Ok, let's say that I have a generator function that can yield either something or nothing. E.g:code:
code:
Haystack fucked around with this message at 15:26 on Mar 20, 2010 |
# ? Mar 20, 2010 15:24 |
|
It sounds like you've stumbled into the halting problem.
|
# ? Mar 21, 2010 10:14 |
|
Haystack posted:Ok, let's say that I have a generator function that can yield either something or nothing. E.g: I think that it's not 'Pythonic' to do it that way; you're supposed to try code assuming that the generator will yield something, then handle the exception if it throws a StopIteration.
|
# ? Mar 21, 2010 10:32 |
Scaevolus posted:It sounds like you've stumbled into the halting problem. I don't think it's a test to see if a function halts, it's just a function to test whether or not the function is iterable.
|
|
# ? Mar 21, 2010 23:53 |
|
Jo posted:I don't think it's a test to see if a function halts, it's just a function to test whether or not the function is iterable. Rice's Theorem ("any non-trivial statement about the function that is defined by an algorithm is undecidable") is a consequent of the halting problem.
|
# ? Mar 22, 2010 00:12 |
|
Jo posted:I don't think it's a test to see if a function halts, it's just a function to test whether or not the function is iterable. I considered that, but that's not what the given example is.
|
# ? Mar 22, 2010 00:20 |
|
Scaevolus posted:I considered that, but that's not what the given example is. Probably should have been more clear with my examples: I am only interested in whether or not the generator function will iterate, even once. The example was just to demonstrate the try/except logic I was using.
|
# ? Mar 22, 2010 00:36 |
|
Haystack posted:Probably should have been more clear with my examples: I am only interested in whether or not the generator function will iterate, even once. The example was just to demonstrate the try/except logic I was using. It's already been explained but I will be as explicit as possible: it is mathematically impossible to determine the results of arbitrary code without running that code (incidentally, this is why virus scanners still manage to let a lot of sufficiently-clever viruses through). The best you can do is introspect into the code and try to use heuristics to figure it out. I do not recommend this.
|
# ? Mar 22, 2010 00:47 |
|
I've got a question which I'm sure is stupidly easy, but I barely know any Python. I'm trying to take inputs in a for loop. So, I have the loop that's something like this code:
|
# ? Mar 22, 2010 01:07 |
|
(Extremely) explicit:code:
code:
|
# ? Mar 22, 2010 01:09 |
|
Thanks dude, that'll work. Also, naming the variable range helped me think of a way to do the next part of the function really sweetly, so thanks even more.
CRISPYBABY fucked around with this message at 01:22 on Mar 22, 2010 |
# ? Mar 22, 2010 01:18 |
|
attackmole posted:Thanks dude, that'll work. Also, naming the variable range helped me think of a way to do the next part of the function really sweetly, so thanks even more. Naming variables after builtin functions is generally a bad idea, and not recommended.
|
# ? Mar 22, 2010 01:32 |
|
Sailor_Spoon posted:Naming variables after builtin functions is generally a bad idea, and not recommended. Using range is generally a bad idea.
|
# ? Mar 22, 2010 01:33 |
|
Avenging Dentist posted:Using range is generally a bad idea. Py3K
|
# ? Mar 22, 2010 02:39 |
|
king_kilr posted:Py3K Can anyone really be said to "use" Py3k yet?
|
# ? Mar 22, 2010 02:50 |
|
I have a 64bit OS and I downloaded python 3.1 64 bit. When I'm in the shell at the top it says 64bit AMD but I am using an intel processor. Is that normal? Is this optimized for AMD or something?
|
# ? Mar 22, 2010 04:27 |
|
wwqd1123 posted:I have a 64bit OS and I downloaded python 3.1 64 bit. When I'm in the shell at the top it says 64bit AMD but I am using an intel processor. Is that normal? Is this optimized for AMD or something? x86-64 is often referred to as AMD64 since AMD made the spec and was the only implementor for years (while Intel was pushing IA64, which is basically dead from non-server use).
|
# ? Mar 22, 2010 04:33 |
|
|
# ? Jun 1, 2024 18:50 |
|
Otto Skorzeny posted:x86-64 is often referred to as AMD64 since AMD made the spec and was the only implementor for years (while Intel was pushing IA64, which is basically dead from non-server use). Does that mean I should just go with the regular 32bit version on an intel?
|
# ? Mar 22, 2010 05:05 |