|
KICK BAMA KICK posted:Here's the problem. map is the name of a built-in function. Either a) the code you're c/ping must have elsewhere redefined map to refer to a list of lists or something to that effect, whose items you could access with the [x][y] syntax or b) you made a typo -- hopefully this, because the former would mean you're reading some dumb code. __getitem__ is the method that is silently invoked when you access an element of a list by index, or an element of a dictionary by key, etc. Because map is just a function, and functions don't have elements you can access with that syntax, it doesn't have a __getitem__ method and thus Python complains. Well you're getting the error because map is a builtin function and therefore you can't index into it like map[x]. Indexing uses the __getitem__ method under the hood; saying map doesn't have __getitem__ is the same as saying you can't index into it. Without looking at the rest of the code or libtcod, my guess is you want to replace the two appearances of map with fov_map. Unless you have another sort of map as well. E: Holy crap, beaten badly. SurgicalOntologist fucked around with this message at 05:11 on Aug 14, 2014 |
# ? Aug 14, 2014 05:06 |
|
|
# ? May 8, 2024 23:54 |
|
KICK BAMA KICK posted:So either you mistyped map in place of something else (like fov_map perhaps?) or you need to incorporate the code that defines map. In the unlikely event that this code does redefine a very basic and useful built-in function rather than simply coming up with a unique name, consider learning from another source because that's a huge red flag. Also this again, for effect.
|
# ? Aug 14, 2014 05:09 |
|
Confirmed: it's not a typo. The tutorial code is kind of scary.Python code:
SurgicalOntologist fucked around with this message at 05:16 on Aug 14, 2014 |
# ? Aug 14, 2014 05:13 |
|
Alright, I'll go ahead and change everything, and take the tutorial with a grain of salt from here on. Will report back soon with results, or exciting new errors in learning code from people who overwrite global functions.SurgicalOntologist posted:To actually give useful advice: make a class or two, every function that need one or more globals should be a method on one of these classes and every global an attribute. Use the distribution of globals among the function to help decide if there are any natural groupings for making more than one class, if not just make one. I understood precisely none of that. Edit Well, okay, that's not strictly true. I know how to make a class. I just don't know how that applies in the context of this. Let's see what I can MacGyver together, though. Edit2 I'm starting to feel like I might just have more luck starting again from scratch. It's only 350 lines, and better figuring out what and how to make this work properly now than having a horrible messy kludge of code that's 6000 lines long and is completely unmanageable. girl dick energy fucked around with this message at 05:32 on Aug 14, 2014 |
# ? Aug 14, 2014 05:15 |
|
Poison Mushroom posted:I'm going to be frank with you. Python code:
Python code:
quote:Edit2 I'm starting to feel like I might just have more luck starting again from scratch. It's only 350 lines, and better figuring out what and how to make this work properly now than having a horrible messy kludge of code that's 6000 lines long and is completely unmanageable.
|
# ? Aug 14, 2014 05:41 |
|
Yeah I hadn't thought about it any deeper than that. Basically, Poison Mushroom, a common problem is wanting to have access to some repertoire of variables from many functions. There are various ways to achieve this: - Make these variables globals. This is the worst solution. It's easy at first, but leads to strange errors and hard-to-debug code. - Pass these variables around as parameters and/or return them. This is probably the most common solution. But as you get more and more of these variables it gets tedious. - Make a class, put the variables on the class as attributes, and pass the instance around. Remember how when you write methods on a class, you always make the first one self? Methods are sort of just passing around an instance. If you make these functions methods, you don't need to explicitly pass the instance, it will be self. In your example you could make a class like GameState or something, and methods could access self.map or self.player instead of globals.
|
# ? Aug 14, 2014 05:57 |
|
SurgicalOntologist posted:Yeah I hadn't thought about it any deeper than that. KICK BAMA KICK posted:Separating functionality into classes will really help with this; you might want to go a step further and look up the basics of using modules -- separate files of code that you import, just like the libraries you've been using. I've decided to take a compromise between this and the old stuff. Thanks to Notepad++ being loving magic, I was able to CTRL+Z all the back to before I put in the first FOV bits. I'm going to take everything that's left, salvage all that I can into modules or just better code, and then go through the rest of the tutorial as a kind of rough guide, rather than just copying him. It'll be a rougher road with more bumps and bugs and probably all kinds of stupid mistakes, but it's better than either trying to figure all this poo poo out myself, OR just starting from scratch and fixing what it breaks as I go. Edit Really stupid question, how would I get Python to search for modules inside a sub-directory, like a folder to keep them all in? I know it has something to do with sys.path and PYTHONPATH, but I'll be damned if I can figure out what. Edit2 Would it be site.addsitedir(sitedir, known_paths=None)? And if so, would the sitedir be 'c:\whatever\whatever\destination' folder, or would it just be searching straight from the directory that the file's running from? (And thus, just '\destinationfolder'?) I'm going to gently caress around with this and hope I don't break something important. girl dick energy fucked around with this message at 07:13 on Aug 14, 2014 |
# ? Aug 14, 2014 07:00 |
Poison Mushroom posted:Thanks to Notepad++ being loving magic, I was able to CTRL+Z all the back to before I put in the first FOV bits. Now is a great time to learn the basics of git. There's only a few commands you'll need to use at first and it is amazing what it is capable of. I would stick with the command line interface for now (msysgit), no need for a GUI. https://github.com/blog/120-new-to-git
|
|
# ? Aug 14, 2014 09:21 |
|
Poison Mushroom posted:I've decided to take a compromise between this and the old stuff. Thanks to Notepad++ being loving magic, I was able to CTRL+Z all the back to before I put in the first FOV bits. I'm going to take everything that's left, salvage all that I can into modules or just better code, and then go through the rest of the tutorial as a kind of rough guide, rather than just copying him. It'll be a rougher road with more bumps and bugs and probably all kinds of stupid mistakes, but it's better than either trying to figure all this poo poo out myself, OR just starting from scratch and fixing what it breaks as I go. This seems like a reasonable path to take. Feel free to ask any questions in here There are several ways to be able to find modules inside of a sub-directory: 1) Modify your PYTHONPATH environmental variable. In Windows, writing instructions for this is a bit of a pain in the rear end because the names of things change slightly depending on what version you're on, but on Windows 8 you'd right click on This PC (or My Computer), select Properties, select Advanced System Settings, select Environmental Variables. At the top should be a list of your user variables. If PYTHONPATH is defined there, then edit it by adding a semicolon to the end of whatever's there and then typing in (or copy-pasting) the path of whatever sub-directory that you want to use for your python projects. If it's not defined, add a new variable called PYTHONPATH and copy-paste the sub-directory path as the value (you don't need a semicolon anywhere in this case). You should try to do this one, if you can, because if you gently caress something up then nothing breaks, you just won't be able to import your homemade modules until you fix the issue. It sounds difficult, but it's actually pretty easy, and once it's set up you never have to do it again. 2) Use regedit to modify the \SOFTWARE\Python\PythonCore\version\PythonPath in HKEY_CURRENT_USER or HKEY_LOCAL_MACHINE. Like with the environmental variable, entries need to be separated by semicolons. If you look at the PythonPath in HKEY_LOCAL_MACHINE, you'll likely find all of the base Python directories. You can gently caress up your Python installation by messing around in regedit and making the wrong change, so I wouldn't suggest doing this. 3) Create your project in a directory that is already included in your sys.path, such as directories that contain core Python modules (do not actually do this, as it will make your project hard to manage) 4) This is sloppier, but you can just modify the sys.path in whatever script that you're using to launch your game. Python code:
5) You could also use site, but site is just going to extend sys.path anyway. It also does some other stuff that you don't need and is a bit of a coding horror QuarkJets fucked around with this message at 09:57 on Aug 14, 2014 |
# ? Aug 14, 2014 09:46 |
|
fletcher posted:Now is a great time to learn the basics of git. There's only a few commands you'll need to use at first and it is amazing what it is capable of. I would stick with the command line interface for now (msysgit), no need for a GUI. That said if you're replacing "undo" you might as well just be committing the entire file at once every time you run it and get a compilable result.
|
# ? Aug 14, 2014 12:23 |
|
QuarkJets posted:This seems like a reasonable path to take. Feel free to ask any questions in here What you posted isn't what virtualenv sets out to do?
|
# ? Aug 14, 2014 13:19 |
|
I have another dumb question about the conversion from globals to classes. Here's a sample of the old, awful code. code:
code:
girl dick energy fucked around with this message at 18:16 on Aug 14, 2014 |
# ? Aug 14, 2014 18:03 |
|
Yeah, some major problems there, you might want to read up on classes, look at some examples. Here's a basic skeleton to get you started:Python code:
This way, values that you need in a function can come from two places: explicitly passed to the function, like room in create_room, or attributes of the current game state, this is how other methods will have access to self.player and self.map. But note that this second way of getting values is just an indirection on the first way--you're still passing the values to the function, via the self parameter. I added self.player as an example of another attribute. The way I have it set up you would create the player first and then pass it to the GameState. Alternatively you could create the player from scratch within __init__. Also note that having the map be self.map (from inside a method) or state.map (from elsewhere, where state is a GameState instance) means that the previous problem of conflicting with the builtin function is solved. No need to call it gamemap, IMO, as GameState.gamemap is redundant. SurgicalOntologist fucked around with this message at 18:32 on Aug 14, 2014 |
# ? Aug 14, 2014 18:22 |
|
Okay. I probably ended up trying to run before I could crawl, but I'll do what I can. Let me see if I'm groking the concept. A class is a sort of blueprint for making an object or a function, something you'll need to do a lot. Classes are to functions as constants are to magic numbers. If I wanted to create a lot of gamemaps, then it would behoove me to make a Class for them, something to repeat the map-making code over and over. Something like Floor or Level. So I'd put my map-generation code into that class under def __init__, and every time something from the code calls to create something from the Floor class, it'd run all the mapmaking code right then, with room to also include in the class possible variations on the formula? (Larger rooms, fewer rooms, a different tunnel generation process, etc?)
|
# ? Aug 14, 2014 18:52 |
|
Poison Mushroom posted:A class is a sort of blueprint for making an object or a function, something you'll need to do a lot. Classes are to functions as constants are to magic numbers. If I wanted to create a lot of gamemaps, then it would behoove me to make a Class for them, something to repeat the map-making code over and over. Something like Floor or Level. In a nutshell, yes, that is pretty much it.
|
# ? Aug 14, 2014 19:49 |
|
Poison Mushroom posted:A class is a sort of blueprint for making an object or a function, something you'll need to do a lot. Yes and no. Technically correct, I think, but the best way of thinking of classes is as a neatly contained bundle of state and behavior that acts on said state (well, the class is a blueprint for creating that bundle, the bundle is the instance). As opposed to a variable which is only state or a function which is only behavior. It can still be useful to make a class for something like game state that you only plan to instantiate once, because it provides a nice package for giving all the methods (the behavior) access to the attributes (the state). The "do I need a class here" checklist has two items: state and behavior. As is, you're representing the map as a list of lists, which I think is fine. I don't think you should make a map class until you have some behavior that makes more sense to package with the map than with the GameState. Doesn't mean you shouldn't make a create_blank_map function though, if it's something you do more than once. SurgicalOntologist fucked around with this message at 19:55 on Aug 14, 2014 |
# ? Aug 14, 2014 19:51 |
|
HardDisk posted:What you posted isn't what virtualenv sets out to do? It is, but I've never messed with that on Windows
|
# ? Aug 14, 2014 19:59 |
|
Poison Mushroom posted:Okay. I probably ended up trying to run before I could crawl, but I'll do what I can. Let me see if I'm groking the concept. As an example, let's say that you want to be able to create the map more than once, but you definitely want to create a map when the game begins. Here's one way that you could do that Python code:
|
# ? Aug 14, 2014 20:19 |
How do I handle user credentials that are required for running integration tests? I was thinking environment variables might be the way to go. Right now I just have them hard coded in my setUp() method but I'm thinking I probably shouldn't commit that to source control.
|
|
# ? Aug 16, 2014 00:29 |
|
So I am working my way through the "learn python the hard way" course and its great. I think I have hit an issue with my dev enviroment, as I am using Pycharm and not the command line as per the instructions. In exercise 13 it asks us to do this... code:
|
# ? Aug 16, 2014 14:43 |
|
argv is a sequence of the commandline string used to launch the program, split on spaces.Python code:
quote:[thomas@chansey ~]♣ ./test.py one two three I don't know anything about Pycharm, but it's probably failing because the way their code is written it will only work with exactly three arguments passed to it. You'll need to give us the exception it's throwing.
|
# ? Aug 16, 2014 15:02 |
|
thegasman2000 posted:Can i access that from pycharm?
|
# ? Aug 16, 2014 15:28 |
|
fletcher posted:How do I handle user credentials that are required for running integration tests? I was thinking environment variables might be the way to go. Right now I just have them hard coded in my setUp() method but I'm thinking I probably shouldn't commit that to source control. I started making scripts that go across prod and pre-prod with the same code using configparser https://wiki.python.org/moin/ConfigParserExamples For more complicated stuff I created an sqlite database.
|
# ? Aug 16, 2014 16:18 |
|
You can also open a terminal, navigate to the PyCharm project directory, and run the script from the command line there.
|
# ? Aug 16, 2014 16:38 |
|
KernelSlanders posted:You can also open a terminal, navigate to the PyCharm project directory, and run the script from the command line there. Or go to Tools > Open Terminal...
|
# ? Aug 16, 2014 17:07 |
|
Is there a good way to filter on a list of tuples? For example, let's say I want to sum the second values where something about the first value is true. This can be done with a list comprehension, but feels a little clunky.Python code:
Python code:
|
# ? Aug 17, 2014 18:13 |
|
KernelSlanders posted:Is there a good way to filter on a list of tuples? For example, let's say I want to sum the second values where something about the first value is true. This can be done with a list comprehension, but feels a little clunky. Python code:
In general, just think of python's builtins that tend to operate on lists (or stuff that can be iterated) as generally working on generators as well, and that you often actually want to use the generator versions. You were right to be suspicious of constructing an entirely separate list only to immediately traverse it once and then throw it away. ShadowHawk fucked around with this message at 18:40 on Aug 17, 2014 |
# ? Aug 17, 2014 18:33 |
|
Your example is kind of odd, why can't you use a dictionary? Or even something like this:Python code:
|
# ? Aug 17, 2014 19:46 |
|
KernelSlanders posted:Is there a good way to filter on a list of tuples? For example, let's say I want to sum the second values where something about the first value is true. This can be done with a list comprehension, but feels a little clunky. Yeah, that final solution is sort of I think that your first solution looks fine. It looks clunky because the input is clunky.
|
# ? Aug 17, 2014 19:53 |
|
Symbolic Butt posted:Your example is kind of odd, why can't you use a dictionary? Or even something like this: Because my_list is the output of a library function that returns a list of tuples. I have no idea why it does this rather than returning a dictionary. Also, the fact that the second values of my tuples were 1,2,3,4 was just a poor choice in setting up my example and could be anything. I do think your tuple expansion combined with ShadowHawk's point about using the generator leads to something pretty clean. Python code:
KernelSlanders fucked around with this message at 20:17 on Aug 17, 2014 |
# ? Aug 17, 2014 20:14 |
|
e: nm I'm dumb and didn't read the question
|
# ? Aug 17, 2014 20:25 |
|
KernelSlanders posted:Because my_list is the output of a library function that returns a list of tuples. I have no idea why it does this rather than returning a dictionary. Also, the fact that the second values of my tuples were 1,2,3,4 was just a poor choice in setting up my example and could be anything. I do think your tuple expansion combined with ShadowHawk's point about using the generator leads to something pretty clean. From your output example, it looks like the keys are non-unique, so a dictionary isn't really suitable. But yeah, this solution looks pretty great if you're having to deal with tuples in that form
|
# ? Aug 17, 2014 20:37 |
|
Unpacking is great for improving readability. Unpack everything QuarkJets posted:From your output example, it looks like the keys are non-unique, so a dictionary isn't really suitable. But yeah, this solution looks pretty great if you're having to deal with tuples in that form The keys could be the numbers instead of the characters, that's why I thought about just using a list of characters.
|
# ? Aug 17, 2014 22:43 |
|
Symbolic Butt posted:Unpacking is great for improving readability. Unpack everything KernelSlanders posted:
One other possibility, if these tuples are appearing elsewhere in your code, is a named tuple. You can't easily declare them inline during a list creation, but if the API you're using is returning tuples you could namify them as you get them (eg to build your initial list), or perhaps replace the list in place. Python code:
|
# ? Aug 17, 2014 23:23 |
|
Symbolic Butt posted:Unpacking is great for improving readability. Unpack everything Are the numbers necessarily unique, though? I'd assume not
|
# ? Aug 18, 2014 01:59 |
|
Are there any highly recommended books for learning Python? I know the basics and am ready to move to intermediate/advanced topics. A book with problems/exercises followed by solutions and discussion could be great. I've never learned coding from a book before so I'm not sure what to look for.
|
# ? Aug 19, 2014 03:08 |
|
Is it terrible practice to use ast.literal_eval()? Right now I'm faux serializing lists by saving the list itself into a CharField (don't ask, this project is riddled with issues like this.) I'm trying to decide whether it makes sense to serialize to an actual serialization format and then deserialize instead of using literal_eval() or if I can get away with it. Thoughts?
|
# ? Aug 19, 2014 14:42 |
|
So I finally took the leap today and picked up python. I really like it so far and its gone better than any previous attempts I'd made at learning a language. I spent today making a simple button based GUI to launch various scripts. Anyway, my problem so far is that I keep getting errors because all the commands I look up for 2.x. Are there any good sites for 3.x stuff? seance snacks fucked around with this message at 03:49 on Aug 21, 2014 |
# ? Aug 21, 2014 03:43 |
|
Noslo posted:So I finally took the leap today and picked up python. I really like it so far and its gone better than any previous attempts I'd made at learning a language. I spent today making a simple button based GUI to launch various scripts. There's hardly any difference between 2 and 3 that you will likely encounter. Keep plugging away, and in a few days you'll probably learn all the most frequently encountered differences.
|
# ? Aug 21, 2014 04:06 |
|
|
# ? May 8, 2024 23:54 |
|
Noslo posted:So I finally took the leap today and picked up python. I really like it so far and its gone better than any previous attempts I'd made at learning a language. I spent today making a simple button based GUI to launch various scripts. http://www.diveintopython3.net
|
# ? Aug 21, 2014 06:00 |