|
Is there a better way to do this? I snipped the code but pretend there are dozens of these. It may be that there is a better way to handle this in django too, but I'll look into that later -- I'm more curious if there is a better way to go about doing this pattern in python.Python code:
The March Hare fucked around with this message at 17:39 on Sep 17, 2013 |
# ? Sep 17, 2013 17:05 |
|
|
# ? Jun 12, 2024 15:35 |
|
I posted earlier in the thread that I had used pexpect to create an ssh program, someone suggested I use paramiko, so I finally rewrote it using paramiko and used some tips from the feedback I got to make it a little better.Python code:
|
# ? Sep 17, 2013 17:17 |
|
DSA_Key posted:Well I suppose I can look it up on the web. Do that and then ask questions.
|
# ? Sep 17, 2013 17:49 |
|
I'm trying to throw up an error and a prompt upon getting an exception. Like so:Python code:
Python code:
code:
And yes, I imported urllib2...
|
# ? Sep 17, 2013 17:55 |
|
"urrlib". Two r's.
|
# ? Sep 17, 2013 18:00 |
|
oh my god
|
# ? Sep 17, 2013 18:02 |
|
The March Hare posted:Is there a better way to do this? I snipped the code but pretend there are dozens of these. It may be that there is a better way to handle this in django too, but I'll look into that later -- I'm more curious if there is a better way to go about doing this pattern in python. Stick all the race dictionaries into another dictionary. Then use setattr(hero, key, value) inside a loop of hero_dictionary[hero.race].iteritems() (iteritems() doesn't work in 3, so if you're using 3, it looks like you just use .items()) FoiledAgain fucked around with this message at 18:12 on Sep 17, 2013 |
# ? Sep 17, 2013 18:08 |
|
FoiledAgain posted:Stick all the race dictionaries into another dictionary. Then use setattr(hero, key, value) inside a loop of hero_dictionary[hero.race].iteritems() I'm using 2 and I totally forgot you could nest dicts, thank you for helping my frazzled brain.
|
# ? Sep 17, 2013 18:12 |
|
The March Hare posted:Is there a better way to do this? I snipped the code but pretend there are dozens of these. It may be that there is a better way to handle this in django too, but I'll look into that later -- I'm more curious if there is a better way to go about doing this pattern in python. I don't know Django but this is a pretty textbook use-case for classes and inheritance. Python code:
|
# ? Sep 17, 2013 18:33 |
|
Stuck. Again. So, I'm trying to create a beginning date and an end date. Basically, I'm using datetime.timedelta() to return one year, three months, or one day ago. Doing a timeframe of one year ago works, but the rest don't. I get this message instead: code:
|
# ? Sep 17, 2013 20:19 |
|
Pollyanna posted:Stuck. Again. This works for me: Python code:
Dominoes fucked around with this message at 21:10 on Sep 17, 2013 |
# ? Sep 17, 2013 21:03 |
|
Dominoes posted:Post your code that caused this. This is the snippet of code in question: Python code:
edit: Hahahaha gently caress. It was the weeks thing that stopped it from working. Sorry about that. I mean, well, option 'c' still doesn't work - but I think I know why, and it's unavoidable. Pollyanna fucked around with this message at 21:16 on Sep 17, 2013 |
# ? Sep 17, 2013 21:14 |
|
Given a list of 10 players:Python code:
Python code:
Is there a better way of doing this? NtotheTC fucked around with this message at 22:15 on Sep 17, 2013 |
# ? Sep 17, 2013 22:04 |
|
NtotheTC posted:
Already give you all unique combinations. Whats your question again?
|
# ? Sep 17, 2013 23:01 |
|
Sorry I wasn't clear, that gives me the unique team combinations, but I want the unique MATCHUP combinations (team1 vs team2). For any given 5 man team combination in that list, it has it's counterpart (the team combination containing the other 5 players). VVV Excellent, missed that in the docs. Thanks! NtotheTC fucked around with this message at 10:10 on Sep 18, 2013 |
# ? Sep 17, 2013 23:03 |
|
NtotheTC posted:This solution relies on the fact that the ordering of the combinations() object is consistent (splitting the combo list in half and reversing the second half means that I get the unique matchups lined up). From the itertools documentation: quote:Combinations are emitted in lexicographic sort order. So, if the input iterable is sorted, the combination tuples will be produced in sorted order. Since the description of the function's behaviour specifies that the combinations will come out in the observed order, you are fine. (If the behaviour was merely observed, not explicitly specified, it would not be safe to rely upon it.)
|
# ? Sep 17, 2013 23:14 |
|
Is there a good resource out there for working with winsock in python? There don't seem to be any other options for working with Bluetooth devices using Python 3 (on Windows)
|
# ? Sep 19, 2013 14:31 |
|
Why winsock? Surely if you need sockets the socket module is a better choice? It has native Bluetooth support on Python3 http://docs.python.org/2/library/socket.html
|
# ? Sep 19, 2013 15:26 |
|
the wizards beard posted:Why winsock? Surely if you need sockets the socket module is a better choice? It has native Bluetooth support on Python3 http://docs.python.org/2/library/socket.html Not on Windows code:
Ireland Sucks fucked around with this message at 15:32 on Sep 19, 2013 |
# ? Sep 19, 2013 15:29 |
|
Ireland Sucks posted:Not on Windows I get the same result Googling says that Bluetooth isn't supported on Windows, that sucks. I guess you're stuck with talking to winsock with ctypes?
|
# ? Sep 19, 2013 17:44 |
|
the wizards beard posted:I get the same result Googling says that Bluetooth isn't supported on Windows, that sucks. I guess you're stuck with talking to winsock with ctypes? Yep, or turning everything into 2 and using the old pybluez module
|
# ? Sep 19, 2013 17:47 |
|
Ireland Sucks posted:Yep, or turning everything into 2 and using the old pybluez module Have you looked through the pybluez source? At some point it must wrap around the native Windows calls, you could just rip out some of the code?
|
# ? Sep 19, 2013 17:57 |
|
More of a docutils / restructured text / Sphinx question but maybe the solution involves Python: So, I'm writing a book using Sphinx. That's all working well except that I would like to group the chapters into sections or parts: "Part 1 - The Basics" will include a number of chapters, "Part 2 - Advanced Use" will include the next batch of chapters and so on. But how to do this in Sphinx? One webpage I saw makes an oblique reference to document part headings, but I haven't tracked that down to specific markup. Any ideas? Is there something you can do with the table-of-contents? (It strikes me that actually many books have a structure like this, except its reflected in the frontmatter and backmatter, i.e. the foreword and appendices etc.)
|
# ? Sep 19, 2013 18:24 |
|
I'm starting to learn Python in my free time after some exposure to C/C++ in college, and I have a question about if something would be feasible. I'm going to write a program at work that would need to communicate with some old hardware via serial port. Basically all I'll need would be to send and receive data on that, and to have a GUI my coworkers would understand. Is this within reach for a Python3 novice with some experimentation? Also would the code need to be changed at all if we switched to a different version of Windows? The only reason I ask that last question is because our current program is written in Visual Basic and won't run on anything newer than Windows XP, which is one of the many reasons I'm replacing it.
|
# ? Sep 20, 2013 14:54 |
|
Nolanar posted:I'm starting to learn Python in my free time after some exposure to C/C++ in college, and I have a question about if something would be feasible. That's all pretty easy, and it shouldn't require any modification to run on different versions of Windows. Without too much extra work the same code will even work in Linux and OS X. Check out PySerial, it's super easy to use.
|
# ? Sep 20, 2013 15:48 |
|
It shouldn't be a problem. Break it up into two parts. First, write a command line app to do the serial interface control. Second, write the GUI. I suspect writing the GUI is going to be the hard part.
|
# ? Sep 20, 2013 16:03 |
|
So I said I was working on learning OOP, right? Well, I think I get the hang of classes and inheritance now. I coded a list of classes for that theoretical game I was talking about, and I was wondering if you guys could look at my code and see if I made any mistakes or if there's anything I could do better?Python code:
What's bugging me is that doing Python code:
Pollyanna fucked around with this message at 06:33 on Sep 21, 2013 |
# ? Sep 21, 2013 05:18 |
|
I'm more of a beginner than you are so sorry if I'm off base here: You define the Civilian init function without using super to also call the parent class' init (where name is established).
|
# ? Sep 21, 2013 06:31 |
|
Yup, that was part of the problem. I just updated to reflect that. But I still have an issue where I can't do sally = Civilian(name="Sally",line="Hi! I\'m Sally!") without it bitching that name is an unknown argument. Why?
|
# ? Sep 21, 2013 06:34 |
|
http://rhettinger.wordpress.com/2011/05/26/super-considered-super/ check the code example under "Practical Advice" to see how you can use **kwargs to pass your extra arguments up to the super.__init__. Sorry for badposting, I've only got my phone at the moment. Hope this helps.
|
# ? Sep 21, 2013 06:56 |
|
Kumquat posted:http://rhettinger.wordpress.com/2011/05/26/super-considered-super/ check the code example under "Practical Advice" to see how you can use **kwargs to pass your extra arguments up to the super.__init__. Sorry for badposting, I've only got my phone at the moment. Hope this helps. Yayyyyy that fixed it Thankee!!!! God, this is complicated as gently caress. edit: I've actually just thought of something: Is there a way to just write a list of monsters and their attributes in like a .txt file, and just have mobs.py bring in the stuff from there? Or does that not make sense? Seems like kind of a pain in the rear end to have to add a new class every time I want to add a type of monster. Pollyanna fucked around with this message at 07:58 on Sep 21, 2013 |
# ? Sep 21, 2013 07:18 |
|
Pollyanna posted:Yayyyyy that fixed it Thankee!!!! I don't see how that would save time; you'd be loading the information from a text file (expensive) and then doing the same stuff with it as you already are. A better alternative might be to just have a Monster class with a type attribute that can be set to "Orc" or "Goblin" or whatever. If all monsters behave fundamentally the same and only differ by name, then this is fine. If you want Orcs and Goblins to have different abilities, then it may make more sense to make them unique classes and then spawn several instances of them (goblin1, orc1, orc2, etc). Your example code would be great for a village where you want to have several bakers, several blacksmiths, etc. If you just want one of each then it might not be necessary to have a class for each; you could just have a "profession" attribute. So let's say that you want all villagers to pop up some sort of dialog box when the player interacts with them. You'd put that code into the Villager class. But you also want some villagers to have a merchant interaction. You'd create a new class called Merchant, have it inherit from Villager, and then code up the merchant interactions inside of the Merchant class. Now you have villagers that can create dialog boxes and merchants that can create dialog boxes or merchant windows.
|
# ? Sep 21, 2013 09:34 |
|
You'll quickly find out that inheritance for behavior like that is going to become unusable for many reasons, but this can really only be understood once you do it wrong first, so I'll let you do that. Don't be afraid of being messy and putting numbers like "orcs have 20 HP" in the code for now; getting your game up and running is much more important than designing the best code in the world. Code is code; you can always change it later, and you can always change it to read from an external file later so that different orcs have different HPs.
|
# ? Sep 21, 2013 17:22 |
|
Suspicious Dish posted:You'll quickly find out that inheritance for behavior like that is going to become unusable for many reasons, but this can really only be understood once you do it wrong first, so I'll let you do that. Why is that? I've never built any kind of large game or whatever in an OO language.
|
# ? Sep 21, 2013 17:36 |
|
After reading these posts on my phone, I was going to post basically what Dish said when I got home. I don't know what he had in mind, but one thing that comes to mind when doing OO in a game is that it becomes kind of hard to reason about. A good google search is "composition versus inheritance".
|
# ? Sep 21, 2013 17:41 |
|
I remember a blog from a while ago written by an ex-Blizzard dev talking about Starcraft development and the problems they faced using inheritance. I think he talked about composition over inheritance too. Correct me I'm wrong though but I thought the main problem with this sort of thing came from the multiple inheritance/diamond problem? Does Python's Method Resolution Order not take care of that in this instance? E: Found the blog NtotheTC fucked around with this message at 18:01 on Sep 21, 2013 |
# ? Sep 21, 2013 17:55 |
|
yaoi prophet posted:Why is that? I've never built any kind of large game or whatever in an OO language. The big reason is that inheritance is hard to change once built. You can easily change ORC_HP to self.hp if you want to load that from somewhere else, but it's not so easy to unwind all the various little pieces of code you're reusing from a subclass and split that out. This is a problem with all things using OOP, but it's even worse in games because the inheritance tends to model intuition rather than code reuse and the Liskov substitution principle. As an example, "Orc extends Monster" sounds fairly sane, but if Orc is the class that does the animating of the Orc sprite, and Monster is the class that makes enemies fightable, it might be hard to make an Orc NPC that you talk to and roams around town. Try splitting out the Orc animation code into a separate class and unwinding everything that has to do with that without breaking your game.
|
# ? Sep 21, 2013 18:38 |
|
QuarkJets posted:I don't see how that would save time; you'd be loading the information from a text file (expensive) and then doing the same stuff with it as you already are. A better alternative might be to just have a Monster class with a type attribute that can be set to "Orc" or "Goblin" or whatever. If all monsters behave fundamentally the same and only differ by name, then this is fine. If you want Orcs and Goblins to have different abilities, then it may make more sense to make them unique classes and then spawn several instances of them (goblin1, orc1, orc2, etc). Suspicious Dish posted:You'll quickly find out that inheritance for behavior like that is going to become unusable for many reasons, but this can really only be understood once you do it wrong first, so I'll let you do that. Oh Yeah, you might be right. I somehow got confused and made the different types of monsters their own subclasses, when being objects is more obvious. Hmm...I'll try rewriting the code. Thanks! And yeah, this probably isn't a huge big ol' project like I'm actually making a game. I'm trying to see if I "get" OOP. Although, maybe I will make something more of it... Suspicious Dish posted:The big reason is that inheritance is hard to change once built. Er...so, how would you say I should structure my code? Here's a cleaned up version of my items: Python code:
Python code:
code:
Pollyanna fucked around with this message at 21:36 on Sep 21, 2013 |
# ? Sep 21, 2013 21:22 |
|
Pollyanna posted:Er...so, how would you say I should structure my code? Well, let me get back to you once I clean my code up a bit... There's many different ways to do it, and even AAA games haven't found something that works 100%. What I would do highly depends on the kind of game and the scale of the project. If I was making an action RPG-style game a la Zelda or Ys, I'd probably try a component-based style -- split out the idea of "animation" from "behavior". So an enemy orc you can fight is a combination of an "Orc animation" and an "Orc behavior". This is just some basic thoughts, though. If I actually sat down and made an ARPG-type game, I'd probably find that there are lots of problems with it, especially when movement and animation are tied together highly, and that fighting style and movement go hand in hand. Perhaps I'd write a generic state-based animation system that still keeps the split, but all the behavior does is say "display the 'running left' state please" and the animation system paints the frames of the Orc running left. As I said before, the important thing is to get your game up and running, and when you find it's too fragile, sit down and think about what the issues with your codebase are, and work towards fixing them.
|
# ? Sep 21, 2013 21:43 |
|
|
# ? Jun 12, 2024 15:35 |
|
Pollyanna posted:Er...so, how would you say I should structure my code? Here's a cleaned up version of my items: (sorry; didn't see the edit) I'm not exactly sure what the "typing" attribute is. I wouldn't have a amount associated with each item, but instead, naturally have an "amount" by representing each instance of an item with... its own instance: Python code:
|
# ? Sep 21, 2013 21:49 |