|
Lister_of_smeg posted:Just come across this little snippet of code. code:
|
# ? Mar 1, 2012 15:43 |
|
|
# ? May 9, 2024 06:19 |
|
If execute() can return None (as subprocess.poll() does if the process hasn't completed), then that check also makes sense; otherwise a straight if ret != 0 may catch processes that are still executing
|
# ? Mar 1, 2012 16:02 |
|
the posted:I'm sorry, my programming knowledge is rather limited. For what little it's worth, I followed it. However, I have a physics PhD, so chances are I think in a similiar way you do. (astrophysics rules)
|
# ? Mar 2, 2012 22:30 |
|
Anyone around to answer some fairly simple questions regarding regex? edit: nevermind, got distracted by Achewood Awkward Davies fucked around with this message at 06:56 on Mar 3, 2012 |
# ? Mar 3, 2012 06:04 |
|
I want to write a Python script that, on a Mac or Windows computer, can be double-clicked to run. It would then need to open a cmd or Terminal window to accept some user input. Basically, I am trying to make an ultra-simple text adventure just to practice my Python skills. I can do the text adventure bit, but I don't know how to make my *.py files into executable files that will leave a command prompt running for the user to interact with. How do I do this?
|
# ? Mar 5, 2012 21:59 |
|
Chimp_On_Stilts posted:I want to write a Python script that, on a Mac or Windows computer, can be double-clicked to run. It would then need to open a cmd or Terminal window to accept some user input. py2exe and py2app can be used pretty simply to convert them into an .exe or OSX app that someone can doubleclick on.
|
# ? Mar 5, 2012 22:06 |
|
Trying to remove the scientific notation from this graph I generated with the following code.code:
|
# ? Mar 5, 2012 23:35 |
|
I'm working on some very basic text adventures because I find it's a fun way to learn. Here's the design document () for my second adventure:code:
1. The way I did my previous exercise, I made a player_enters() and a prompt() function for each room. The player entered, which triggered the prompt asking for a command, which allowed the player to enter a new room. Simple stuff. The player could also ask for help and open a door. When the player passed in each command via raw_input(), I had a simple if statement tree determining what to do: code:
2. What's the best way to keep the player's inventory with him or her between rooms? Global variables? Returning a variable value at the end of each room function then passing it to the next variable? Will I be able to use the same method to keep track of whether the keys are still waiting to be picked up or not, should the player return to the area? I'm a beginner, I don't know how to make a class yet. Just this simple adventure will stretch my abilities a little.
|
# ? Mar 6, 2012 07:12 |
|
Hmm. I think your going about this all the wrong way. You really need to think of a more data-driven way to go about it. Lets say we did a big array of rooms that went like this code:
We probably want to set a variable that has your location. location = 'north room' From here, we might define a function that returns the room, and perhaps a function that describes the room. code:
room = getRoom(location) describeRoom(room) Ok. So now we need to think in terms of a game loop. Now, in this loop, we do the following;- * get room * display room * get command * see if there is an exit with the same name as your command. If so change location * EXCEPT if there is a lock on the door! * repeat unless the person typed "quit" There will be more, but I'll get there in a bit;- code:
The first thing we need is an inventory. So something like;- inventory = { } Its just a dictionary that'll contain the long name keyed to the short name of the item. Now for actually picking up stuff, we need to look at how our commands work. we want a command like 'take yellow key'. The command is basically verb<space>noun. So lets split this at the first space. We only want ONE split, not more. We COULD do more, but thats a much more complex parser tokens = cmd.split(' ',1) #This splits the command on space, but only 1 split so token[0] is the verb and token[1] is the noun (or whatever, subject) BUT if your only doing a one word command like "south" , then there will be NO token[1]. So with that in mind, the parser is this code:
So now we can add some more commands in. I'll paste in the full and final code, and you can find the new code using the comments. Note how I'm accessing the noun and verb variables for this. I've added a get, drop, inventory and unlock command . get <item> drop <item> inventory and unlock <direction> code:
pointers to finish it;- Google how to use something like simplejson to write the game data to disk. You could probably write an editor to edit room description. Consider using a key/value store to store command aliases. I just wrote your game for you, and this was a very fun post to write duck monster fucked around with this message at 14:24 on Mar 6, 2012 |
# ? Mar 6, 2012 14:19 |
|
I think he wanted to figure out how to make the game himself in order to learn by doing. =/
|
# ? Mar 6, 2012 19:20 |
|
Thermopyle posted:Trying to remove the scientific notation from this graph I generated with the following code. Firstly, the "easy" solution would be to just edit your plot command to something like code:
It doesn't appear like it will work for this range, but you can also do a semilog plot too. A third solution is a little bit more to do why this isn't working the way it's supposed to to begin with: It's good to be in the habit of doing everything in subplots, even if you only have 1 plot (like you're doing here). I *think* the problem is from the line formy=plot.ScalarFormatter()... I'm not sure though, I've not used that command, and I don't have something to readily test it on. Perhaps try: code:
|
# ? Mar 6, 2012 20:23 |
|
German Joey posted:I think he wanted to figure out how to make the game himself in order to learn by doing. =/ Luckily, I don't know a drat thing about arrays yet, so I barely understand the example and can still learn by doing! I kid of course - I appreciate your help tremendously Duck Monster. I am probably going to stick with my (terrible and inefficient) method on adventure #2, but you preempted my goal of using a non function base method for #3. I just need to learn to interact with arrays. EDIT: I just read up on arrays. Apparently they're like lists, but can only carry a uniform data type. Duck Monster, why did you use an array instead of a list? Chimp_On_Stilts fucked around with this message at 22:34 on Mar 6, 2012 |
# ? Mar 6, 2012 22:10 |
|
Chimp_On_Stilts posted:EDIT: I just read up on arrays. Apparently they're like lists, but can only carry a uniform data type. Duck Monster, why did you use an array instead of a list? He's not actually using the Array class (I assume that's what you were looking at). I also must assume he was just using the word array in a more general sense, meaning "a bunch of stuff". rooms is actually a dictionary
|
# ? Mar 6, 2012 23:25 |
|
I finished "ANOTHER HORRIBLE SPOOKY TEXT ADVENTURE", and boy is it horrible!. It's not supposed to be a masterpiece of interactive fiction, just a way for me to learn by doing. I had fun making it =) Here's the problems I see with it: - It's too long and inefficient. Using an if tree for every player action alone would probably land me in the "coding horrors" thread if I wasn't a total newbie. - The inventory system is totally hamfisted. It uses global variables to determine what the player is holding. Furthermore, I had to write a separate if statement conclusion for every possible combination of inventory items - if there were more than 2 items, this would be insane. - The code is probably not organized in a smart way. How would a better programmer organize this many functions? Would a better programmer avoid using so many functions in the first place? I don't want to drop a long program right here in code tags, so download the .py from here if you want to give me some tips (and reminisce about your own newbie coding terrible mistakes!). My next exercise I hope to move away from the if statement crap. Hopefully I can use Duck Monster's code as an example. I have no experience with libraries, so it'll be fun =) EDIT: I see I forgot to put a reminder to "type 'help' for a list of commands" into the game. If you want to actually play the adventure, this will be helpful to know. Chimp_On_Stilts fucked around with this message at 03:07 on Mar 7, 2012 |
# ? Mar 7, 2012 02:40 |
|
er yeah, dictionary not array. Was a bit drunk doing that post. But my point remaims, think "data driven".
|
# ? Mar 7, 2012 03:04 |
|
<Insert I really don't know anything disclaimer> But wouldn't you just set up a class for rooms and plot their location on an XY axis since you're working from 4 compass points, then move would just check to see if there is a valid exit in that room and either return, "you idiot you walked into wall, go kill yourself" or edited the room location and printed out the descriptions? You write all the checking and moving code once, then you just have to fill in the description, objects and exits and poo poo for each room. Also dictionaries are the best thing ever.
|
# ? Mar 7, 2012 20:16 |
|
So here's a dumb question. I often times deal with huge arrays and I want to run them many, many times quickly for data sets that can change over the course of the program. For example, if I have a small data table like:code:
code:
|
# ? Mar 7, 2012 20:25 |
|
That would work as long as your map layout is a two dimensional grid. For anything beyond that (step into teleporter, fall down giant hole, dennis), you're going to need to add custom code. It's just easier to have a mapping of exits to room IDs for each room, and it's not that much more code.
|
# ? Mar 7, 2012 20:26 |
|
Cast_No_Shadow posted:<Insert I really don't know anything disclaimer> I don't know a whole lot about coding for heavily UI situations, but my intitial reaction to this, from a speed standpoint, is that you're going to have an awfully empty map if you're going to actually grid out an entire "zone" in a mud or something. Suspicious Dish posted:That would work as long as your map layout is a two dimensional grid. For anything beyond that (step into teleporter, fall down giant hole, dennis), you're going to need to add custom code. It's just easier to have a mapping of exits to room IDs for each room, and it's not that much more code. Isn't that how CircleMUD was coded? It's been damned near 15 years since I looked at the code for that, but I was pretty sure that's how all those things worked.
|
# ? Mar 7, 2012 20:27 |
|
JetsGuy posted:So lets say I'm running something where not only the counts but the time bin sizes can change depending on what I'm doing. I want the code to be able to tell me exactly where counts=95 or what's the value at time=5. I can't just index it for one case because it will be different every loop. For cases like this, I just reuse a simple function I wrote years ago that works much like Excel's Lookup: You can use boolean arrays as indexes, so "what's the count at time == 5" can be written as counts[time==5]. To get the indices where counts==95 you can say i, = np.nonzero(counts==95). (The comma on the LHS unpacks the tuple returned by np.nonzero. If counts was n x m you'd do i,j = np.nonzero(counts==95)). Both of these return all the values which match the condition, unlike your version which returns only the first match. Returning all the matches means they immediately generalize to things like counts[np.logical_and(3 <= time, time < 10)].
|
# ? Mar 8, 2012 08:11 |
|
Nippashish posted:You can use boolean arrays as indexes, so "what's the count at time == 5" can be written as counts[time==5]. To get the indices where counts==95 you can say i, = np.nonzero(counts==95). (The comma on the LHS unpacks the tuple returned by np.nonzero. If counts was n x m you'd do i,j = np.nonzero(counts==95)). Interesting, thanks! I originally intended for my version to return the *closest* match, which I think argmin() does, but maybe I have a bug I'm not even aware of. This is much nicer though, and can be written in line. Thanks!
|
# ? Mar 8, 2012 18:40 |
|
Evolution of the Python programmer: https://gist.github.com/289467
|
# ? Mar 8, 2012 19:47 |
|
Thermopyle posted:Evolution of the Python programmer: code:
I would have called it the "java" python programmer, but "enterprise" works haha.
|
# ? Mar 8, 2012 20:37 |
|
Thermopyle posted:Evolution of the Python programmer: Ahahahaha, amazing.
|
# ? Mar 8, 2012 21:08 |
|
Reminded me of this, which I'm sure many of you have seen before. (gently caress Cpp forever) http://www.ariel.com.au/jokes/The_Evolution_of_a_Programmer.html
|
# ? Mar 8, 2012 21:35 |
|
I feel dumb asking this, but can someone explain what all is going on here? (it's from the link above)code:
|
# ? Mar 8, 2012 21:54 |
|
f = lambda x: ... is (in this case) equivalent to def f(x): return .... x and y returns y if x evaluates to true, and false otherwise (which is equivalent to boolean and if they're both bools, and more useful if they aren't). x or y returns x if it's true, and y otherwise. As such, the body of the lambda is equivalent tocode:
|
# ? Mar 8, 2012 22:44 |
|
I've always been mystified by lambda because I've seen it in a few people's python code and I'm just not entirely sure I understand the benefit of using it. People have claimed it helps you in that you can use a function without defining it (and something about memory and speed here). For the purposes of scientific programming, generally the thing that kills me more often that not for "speed" is number of iterations I have to run before I worry about things like memory. I've never had to do high-end simulations like my colleagues down the hall though.
|
# ? Mar 8, 2012 23:48 |
|
There are a few legitimate situations where it's useful to have a short function without a name. The key argument to sorted comes to mind:code:
|
# ? Mar 9, 2012 00:18 |
|
Plorkyeran posted:f = lambda x: ... is (in this case) equivalent to def f(x): return .... x and y returns y if x evaluates to true, and false otherwise (which is equivalent to boolean and if they're both bools, and more useful if they aren't). x or y returns x if it's true, and y otherwise. As such, the body of the lambda is equivalent to Thanks for this!
|
# ? Mar 9, 2012 00:23 |
|
I'm guilty of abusing lambda to feed a callable to a function expecting one with different arguments. For example, with PyQt a button's clicked() signal (call) will only connect to a slot (callable) with no arguments. So you can do:code:
code:
code:
|
# ? Mar 9, 2012 01:46 |
|
I have used lambdas to hand functions to bullet pathways in a small shmup. I could use it as a standard argument for a function call and then just memorise and call it. I guess I could have used an object containing the respective functions too, but it felt fancy just keeping it in a variable. As I understand it, lambdas mainly serve as a kind of syntactic sugar so you don't have to move one-liner throwaway functions away from a potentially larger method, and as such out of direct sight.
|
# ? Mar 9, 2012 01:46 |
|
Harm Barn Gumshoe posted:
code:
|
# ? Mar 9, 2012 02:15 |
|
Suspicious Dish posted:
Huh, I never knew about this! The (better) support for *args/**kwargs and introspection is really nice
|
# ? Mar 9, 2012 02:31 |
|
So in honor of Pycon going on right now, does anyone have recommendations for old Pycon talks that are available online? I've gotten in the habit of watching talks/lectures over the past couple weeks but haven't watched anything Python-specific except for Guido talking about the history of the language to the Dropbox staff: https://www.youtube.com/watch?v=ugqu10JV7dk
|
# ? Mar 9, 2012 06:08 |
|
etcetera08 posted:So in honor of Pycon going on right now, does anyone have recommendations for old Pycon talks that are available online? I've gotten in the habit of watching talks/lectures over the past couple weeks but haven't watched anything Python-specific except for Guido talking about the history of the language to the Dropbox staff: How to Write Obfuscated Python is great for the novelty.
|
# ? Mar 9, 2012 06:31 |
|
Lysidas posted:There are a few legitimate situations where it's useful to have a short function without a name. The key argument to sorted comes to mind: For your example they made the attrgetter function in operator, so you can do things like code:
|
# ? Mar 9, 2012 08:40 |
|
JetsGuy posted:Firstly, the "easy" solution would be to just edit your plot command to something like Unfortunately, this didn't change anything. I can't figure out why. :/
|
# ? Mar 12, 2012 00:44 |
|
Back home from PyCon. For those of you that missed out, it was pretty freakin' awesome. Lots of real good conversations were had to go with the excellent tutorials and amazing talks. m0nk3yz - thank you for doing this every year. I've been to two now, and as long as I'm able to do so, I'll continue attending. Seriously, awesome job.
|
# ? Mar 12, 2012 03:53 |
|
|
# ? May 9, 2024 06:19 |
|
Thermopyle posted:Unfortunately, this didn't change anything. I can't figure out why. :/ It may be that it's interpreted the same way. Is it possible for you to send me your y-axis data (or post it)? If not, I'll just make some poo poo up. I do not have time right now to do it, but I promise I'll look at it sometime (probably the end of the week) and try to give you a much more definitive answer as to what is going wrong.
|
# ? Mar 12, 2012 04:15 |