|
ashgromnies posted: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. it seems to me your rgb and hsl switch is more like a couple of subclasses waiting to happen. Aren't they just two different types of pixles, each of which should have their own implementation of the getlight method?
|
# ? Jun 4, 2008 07:02 |
|
|
# ? May 11, 2024 13:55 |
|
Okay, I've just spent a few minutes hacking at this to try to get some sort of consistent, clean interface. I think it's just a ridiculous request. You could add a classmethod to the class for operating on tuples, then call Pixel.staticGetLight or whatever and then have getLight call that with itself. It would be easier to either (1) only use a function, instead of a method or (2) have a classmethod and an instance method like how I described above.
|
# ? Jun 4, 2008 07:05 |
|
Bozart posted:it seems to me your rgb and hsl switch is more like a couple of subclasses waiting to happen. Aren't they just two different types of pixles, each of which should have their own implementation of the getlight method? getLight exists to grab a lightness value from an rgb pixel. You may be right unless you're just playing with me, I'm really tired right now. I need to look up how casting works in Python so I can make rgb convertible to hsl and vice versa just by going code:
This is starting to look overengineered. Why the hell am I doing this. I need to get some sleep. Thanks, guys.
|
# ? Jun 4, 2008 07:08 |
|
If you really want to have all the functions be a member of the Pixel class, this works:code:
|
# ? Jun 4, 2008 07:08 |
|
bbq posted:If you really want to have all the functions be a member of the Pixel class, this works: Can you please tell me why this is the wrong way to go about it? What would you recommend instead? It doesn't make sense to me to have to call Pixel.staticGetLight if I don't have an instance and p.getLight if I do have an instance. I could use a function instead of a method but then I have to import it separately from the class or from pixel import * which pollutes my namespace in my opinion. edit: I think I just realized why it's a bad idea - this is going to require me to maintain it in multiple places. I can use an approach like bbq gave to avoid repeating myself. I guess I'm kind of thinking of the class as a namespace in which to store functions, as well as being instantiable. I guess it's acceptable to pollute my global namespace in Python by importing the method when I need it. ashgromnies fucked around with this message at 15:23 on Jun 4, 2008 |
# ? Jun 4, 2008 15:07 |
|
I found that Java-ish static methods are best translated to module top level functions in Python.
|
# ? Jun 4, 2008 17:06 |
|
Python Cookbook.code:
|
# ? Jun 4, 2008 17:12 |
|
How do you refer to "self" inside talk(), though?
|
# ? Jun 4, 2008 17:16 |
|
You would have to pass it in, which kinda breaks the OO-paradigm. You could do: code:
code:
|
# ? Jun 4, 2008 17:24 |
|
I tried something like this:code:
|
# ? Jun 4, 2008 18:03 |
|
ashgromnies posted:
There's nothing wrong with this method - in fact it's a pretty cool hack. The problem is that all the ugliness is up front where you can see it. If you push it down into another module, you could just do this: code:
At least, you could do that if class decorators were allowed, which they're not. (Boo!) So instead you'll have to apply the decorator to __init__ - which actually makes more sense, from a certain point of view, since it makes it explicit that you're messing with the instance that's being initialized, not the class as it's being defined. code:
code:
|
# ? Jun 4, 2008 23:02 |
|
JoeNotCharles posted:There's nothing wrong with this method - in fact it's a pretty cool hack. The problem is that all the ugliness is up front where you can see it. If you push it down into another module, you could just do this: No, you're completely wrong. This is not a cool hack by any means. This is ridiculous, and you've only made it more ridiculous. (Why does it fail silently if the method isn't __init__?) The simplest way to do this is to use a global function. Every other way is either absolutely ridiculous and requires code duplication[1], like this, or requires two separate methods in the class, which is apparently unacceptable. [1] see: http://codepad.org/8LbuQJAc
|
# ? Jun 4, 2008 23:39 |
|
Milde posted: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__. I read this once before, but I can't find it again. What's the difference between old and new-style classes?
|
# ? Jun 4, 2008 23:46 |
|
Scaevolus posted:I read this once before, but I can't find it again. What's the difference between old and new-style classes? New-style classes just implement some things that old-style classes don't have by default, like descriptors. http://docs.python.org/ref/node33.html
|
# ? Jun 4, 2008 23:50 |
|
Habnabit posted:No, you're completely wrong. This is not a cool hack by any means. This is ridiculous, and you've only made it more ridiculous. (Why does it fail silently if the method isn't __init__?) The simplest way to do this is to use a global function. Every other way is either absolutely ridiculous and requires code duplication[1], like this, or requires two separate methods in the class, which is apparently unacceptable. You could assert or make it throw if the method isn't __init__, instead. In fact that's probably better. The point is that this decorator specifically hooks into instantiation, and it doesn't make sense to put it anywhere else. What does the link you pasted have to do with code duplication? What, in fact, is the problem with that link? _test (which gets renamed to "test") is calling itself, which leads to infinite recursion, as expected. If you wanted it to call the classmethod version, use Foo.test. The simplest way to do this is to use a global function, yes. But I'm not going for simplest - I'm going for most elegant API.
|
# ? Jun 5, 2008 00:42 |
|
In Python simplicity is elegance. Nothing is more inelegant than code you have to spend extra time reading and comprehending in order to figure out what it's doing. Jumping back and forth and back and forth through different classes and different modules in order to get a general idea of how a library works is incredibly annoying, and I've had to do that with some Python libraries before. It's even more depressing when you realize there was no reason for the code to do what it does the way it does, and that the entire thing could've been greatly simplified.
|
# ? Jun 5, 2008 00:57 |
|
Milde posted:In Python simplicity is elegance. Nothing is more inelegant than code you have to spend extra time reading and comprehending in order to figure out what it's doing. Jumping back and forth and back and forth through different classes and different modules in order to get a general idea of how a library works is incredibly annoying, and I've had to do that with some Python libraries before. API simplicity != implementation simplicity If you're a user of TestClass, you don't need to care about how the classmethod_override library works, or even that it exists - all you need to know is that you can call the "test" method either as a class or an instance method. If you're an implementor of TestClass, you need to know a little more - you need to know that you can't create two "test" methods directly, because the second one will just replace the first; you need to give the instance version a different name and then rename it using classmethod_override. You don't need to care about how classmethod_override actually works. If you do want to know how classmethod_override actually works, it's not like it's hard to track down where that code is.
|
# ? Jun 5, 2008 01:32 |
|
JoeNotCharles posted:API simplicity != implementation simplicity There are a lot of cases in Python where it isn't this simple. When I wrote that post I was specifically thinking of FormEncode, which does such absolutely mind-bogglingly ridiculous things with metaclasses that you can't even do simple things like instantiate a field without it being inside a form class. And in this case, this doesn't simplify the API at all. I've never ever seen anyone use a pattern like this in Python. I'd probably be scratching my head if I saw it, and honestly I'm still not sure what doing any of this would accomplish. If you find yourself saying, "that's a cool hack," you should probably step back and think for a moment before you decide to soup up that hack even more.
|
# ? Jun 5, 2008 02:50 |
|
Milde posted:There are a lot of cases in Python where it isn't this simple. When I wrote that post I was specifically thinking of FormEncode, which does such absolutely mind-bogglingly ridiculous things with metaclasses that you can't even do simple things like instantiate a field without it being inside a form class. Yes, obviously if you go too far you raise too many barriers to actually coding. I just don't think this particular example is all that complicated. quote:And in this case, this doesn't simplify the API at all. I've never ever seen anyone use a pattern like this in Python. I'd probably be scratching my head if I saw it, and honestly I'm still not sure what doing any of this would accomplish. I think it'd be justified if I was cloning or wrapping a Java interface or something that does this and wanted to follow it as closely as possible, for example.
|
# ? Jun 5, 2008 03:02 |
|
PEP 371 has been accepted/final!
|
# ? Jun 6, 2008 13:02 |
|
AWESOME!!!!!
|
# ? Jun 6, 2008 13:10 |
|
I am having some trouble receiving UDP packets via python script, but I can see that I am getting them through a network monitor called Wireshark. Basically my script looks like this:code:
69.12.53.114 192.168.1.125 UDP Source port: 27015 Destination port: 0 I understand you are supposed to use the destination port, but why is the destination port here specified as 0? How can I capture this data packet? The packets are being sent from a TF2 Steam server, they are log messages.
|
# ? Jun 7, 2008 23:36 |
|
docs posted:recvfrom( bufsize[, flags]) try code:
|
# ? Jun 8, 2008 00:10 |
|
Dr-NiKoN posted:try Thanks for the catch, changed that, regardless of that though, I am not receiving a packet to begin with.
|
# ? Jun 8, 2008 01:14 |
|
Wulfeh posted:The packets are being sent from a TF2 Steam server, they are log messages. Are you talking about the remote logging client in the HL2 server? That isn't enabled by default, and port 27015 is for game traffic, not logging. You have to add your logging server (the recipient) with logaddress_add. If that's what you're actually trying to do, you might find this useful: http://bitheap.org/hg/hl2utils/. It has a very basic remote logging listener.
|
# ? Jun 8, 2008 01:25 |
|
Milde posted:Are you talking about the remote logging client in the HL2 server? That isn't enabled by default, and port 27015 is for game traffic, not logging. You have to add your logging server (the recipient) with logaddress_add. Yea the server has my ip:port setup, and is sending my machine UDP packets, I can see them with Wireshark, and I can decode them and look at the log messages, the problem I am having is with Python picking up these packets as they are incoming edit: I looked at that logging listener, and it's neat, i'll probably use it when I can figure out how to properly grab the packets Wulfeh fucked around with this message at 01:46 on Jun 8, 2008 |
# ? Jun 8, 2008 01:35 |
|
Wulfeh posted:Yea the server has my ip:port setup, and is sending my machine UDP packets, I can see them with Wireshark, and I can decode them and look at the log messages, the problem I am having is with Python picking up these packets as they are incoming According to the docs, an empty host string is equivalent to localhost. You probably meant to listen on 0.0.0.0.
|
# ? Jun 8, 2008 01:50 |
|
I set it to listen to 0.0.0.0 but I am unsure how to handle the UDP packet with a destination port of 0, is it maybe because of my router? the TF2 server has logaddress_add with myip:27800 specified Router is redirecting all data on port 27800 -> mylocalmachine Could that affect the destination port?
|
# ? Jun 8, 2008 02:53 |
|
Wulfeh posted:I set it to listen to 0.0.0.0 but I am unsure how to handle the UDP packet with a destination port of 0, is it maybe because of my router? O hey I answered your question in #python. Make the script listen on 27800.
|
# ? Jun 8, 2008 04:22 |
|
deimos posted:O hey I answered your question in #python. I got it, I am not sure exactly what is going on but the server admin changed the logaddress_add from url:port to my ip:port and it is now properly forwarding packets to 27800. The reason I was receiving packets from port 0 was because I set myself up as the DMZ earlier on and I was able to see all traffic.
|
# ? Jun 8, 2008 19:22 |
|
I'm working a summer internship research dealie at my university and I'm writing a small-to-medium program in Python to study Rubik's cubes and related puzzles (my professor discovered quantum teleportation and I end up working on Rubik's cubes... go figure). I love Python, as it's certainly a huge step up from the JavaScript implementation my prof initially wanted me to do. I'm really enjoying learning a new language, especially one so different from the C/Java stuff my various homework assignments tend to be in, but there's one thing I'm having trouble with: standard library documentation. I'm pretty used to languages like Java, C# on .NET or just plain old standard library C which are rigorously documented. Whatever else you want to say about Java and Sun, you can't deny that the documentation for the Java API is fantastic. Is there any similarly complete docs for the Python standard libraries? I've looked at the official docs and read through Dive into Python, but when I look up, say, lists on either one, I just get a handful of functions and examples of their use. Something like this page is pretty handy, yes, but when I want documentation, I want it to look like this. Sorry if this is a repeated question, but nothing in the OP seemed to do what I need, least of all the official documentation.
|
# ? Jun 10, 2008 16:12 |
|
Try opening an interactive Python interpreter and typing "help(list)" and "help(list.append)".
|
# ? Jun 10, 2008 16:31 |
|
Ah, that's great. Thanks very much!
|
# ? Jun 10, 2008 16:38 |
|
Spazmo posted:Ah, that's great. Thanks very much! If you like printed documentation, Python in a Nutshell is good, although there will probably be another edition out soon, with the release of Py3k.
|
# ? Jun 10, 2008 18:18 |
|
Spazmo posted:Ah, that's great. Thanks very much! Also try http://docs.python.org/lib/lib.html and http://docs.python.org/modindex.html
|
# ? Jun 11, 2008 12:55 |
|
I have just taken up a new position as a (mostly)MS based sys admin. I am looking after about 400 servers (once again, mostly MS based). I dont know scripting at all, and I am not even sure how useful it would be to me, other than hearing/reading lots of folks talk about scripting. I have not ever used a programming language. I did teach myself T-SQL and really enjoyed it, and got reasonably proficient at it, if it matters. I am wondering if Python would be a good starting point to learn scripting and programming in general? From what I have read it seems like it would, are there any useful books you guys would recommend to an absolute beginner at this?
|
# ? Jun 12, 2008 00:48 |
|
Are there any seasoned python programmers out there that wouldn't mind someone asking questions occasionally on AIM or MSN? I use IRC to find solutions sometimes, but it's easier when you can have a more focused conversation with a person. That's not to say IRC isn't helpful, they are pretty awesome and I love #python. Anyway, go ahead and throw me your aim/msn in a pm if you'd be willing
|
# ? Jun 12, 2008 05:25 |
|
StonedogJones posted:I have just taken up a new position as a (mostly)MS based sys admin. I am looking after about 400 servers (once again, mostly MS based). I dont know scripting at all, and I am not even sure how useful it would be to me, other than hearing/reading lots of folks talk about scripting. Learning Python is a pretty good beginner book, and leads right into Programming Python, which has almost everything in it that you would need. However, if it is an MS shop you might also want to look at .NET, particularly C#? I haven't used it at all though.
|
# ? Jun 12, 2008 20:51 |
|
Wulfeh posted:Are there any seasoned python programmers out there that wouldn't mind someone asking questions occasionally on AIM or MSN? I use IRC to find solutions sometimes, but it's easier when you can have a more focused conversation with a person. That's not to say IRC isn't helpful, they are pretty awesome and I love #python. Try #cobol@irc.synirc.net -- the conversation is generally slower than #python, so we can spend more time discussing particulars.
|
# ? Jun 12, 2008 20:57 |
|
|
# ? May 11, 2024 13:55 |
|
What would be the most efficient way to print through a list of tuples? I'm new to python, coming from java and love how simple some things are to do, so I'm thinking there is an elegant way to do this. In my mind it would require something like:code:
|
# ? Jun 12, 2008 23:42 |