|
What do @classmethod and @property mean?
|
# ? Dec 23, 2014 08:12 |
|
|
# ? May 9, 2024 10:15 |
|
@classmethod @property The @ syntax means they're decorators. Decorators are used when you define a function to 'decorate' it; the decorator is a function that receives your new function as a parameter, and returns what will actually be defined. i.e. this: Python code:
Python code:
And @property is used to simplify defining a very common pattern in OOP: the property. In OO languages without properties, you often end up writing stuff like this: Python code:
Properties give you the best of both worlds: Python code:
|
# ? Dec 23, 2014 10:30 |
|
Moddington posted:@classmethod Python code:
What's the point? It's mostly to get them out of the common namespace so you can put a class-relevant function inside a class even if you don't need to actually reference the class/instance itself.
|
# ? Dec 23, 2014 17:32 |
|
I'm beginning to rethink the earlier comments we had against circular references in classes. I think it might be entirely reasonable in a few cases. For instance, a tree could have each node reference both itself and its parent. Python code:
|
# ? Dec 24, 2014 18:41 |
|
Python also provides weakref's, that won't bump the reference count, which can be useful for object graphs that have cycles. Have you guys really never encountered object graph data structures that have back references? Maybe not the most common things, but it still seems wholly unremarkable to me.
|
# ? Dec 24, 2014 21:59 |
|
BigRedDot posted:Python also provides weakref's, that won't bump the reference count, which can be useful for object graphs that have cycles. Have you guys really never encountered object graph data structures that have back references? Maybe not the most common things, but it still seems wholly unremarkable to me. They are really common in GUI objects. In PySide, nearly everything is based on QObject which has a parent() method.
|
# ? Dec 24, 2014 22:16 |
|
I am trying to use the networkx package. I have successfully installed networkx (I can go through the tutorial examples), but the following code where I try to read in a graph from a GIS shapefile:Python code:
code:
Python code:
Python code:
Anyone know what I might be doing wrong? Jose Cuervo fucked around with this message at 23:24 on Dec 24, 2014 |
# ? Dec 24, 2014 23:22 |
|
Your paths probably aren't the same in each case--try looking at sys.path in IDLE and the same at the top of your script.
|
# ? Dec 24, 2014 23:46 |
|
Jose Cuervo posted:Anyone know what I might be doing wrong? Did you restart IDLE after you modified the environment variables?
|
# ? Dec 24, 2014 23:46 |
|
Hed posted:Your paths probably aren't the same in each case--try looking at sys.path in IDLE and the same at the top of your script. In Idle: Python code:
Python code:
accipter posted:Did you restart IDLE after you modified the environment variables? EDIT: Bah. Restarting seems to have done the trick. Not sure why I needed to do that, but oh well. Jose Cuervo fucked around with this message at 04:37 on Dec 25, 2014 |
# ? Dec 25, 2014 04:18 |
|
Jose Cuervo posted:In Idle: Probably something was being changed to your PYTHONPATH, but only after a restart for some reason
|
# ? Dec 25, 2014 11:15 |
|
Hi again, I'm still doing libtcod + python roguelike stuff. I am trying to access a specific character in this tileset: I'm using it for the graphics and libtcod handles it just fine, however I want to specify one of the really weird characters at the bottom, from the family of stuff that looks like a collection of double pipe segments along three rows. How do I refer to these things? They don't show up in my system's character map utility since this is not a system font, and they don't have ASCII codes either. How do I specify an element in this font matrix? I think I'm using UTF-8 encoding for my program because its the default and I haven't defined anything else.
|
# ? Dec 26, 2014 09:03 |
|
reading posted:Hi again, I'm still doing libtcod + python roguelike stuff. I am trying to access a specific character in this tileset: Unless I'm wrong this seems to be Code page 437. Wikipedia has all the characters in unicode: http://en.wikipedia.org/wiki/Code_page_437
|
# ? Dec 26, 2014 11:44 |
|
Symbolic Butt posted:Unless I'm wrong this seems to be Code page 437. Wikipedia has all the characters in unicode: http://en.wikipedia.org/wiki/Code_page_437 After having a lot of trouble getting this to work it looks like unicode is not supported in the python version of libtcod: http://doryen.eptalys.net/data/libtcod/doc/1.5.1/html2/console_print.html#10 However it says that it's the "python wrapper" that doesn't support it, but it gives examples for C and C++. It's really going to be a bummer if I can't use the whole roguelike tileset, such as Greek lowercase "alpha" for fish, and so on. Do I have any options here, such as using the C/C++ functions in my python code somehow to access and display those characters? This post on the official libtcod forums http://roguecentral.org/doryen/forum/index.php?topic=1491.0 is on this topic but only for C++. They use putChar to display characters from fonts with >255 characters. reading fucked around with this message at 06:56 on Dec 27, 2014 |
# ? Dec 27, 2014 06:51 |
|
reading posted:After having a lot of trouble getting this to work it looks like unicode is not supported in the python version of libtcod: http://doryen.eptalys.net/data/libtcod/doc/1.5.1/html2/console_print.html#10 Yeah, that's what I would suggest. You could write your own wrappers for libtcod that support unicode. It might not even be that much work, if the C++ functions are relatively straightforward to use
|
# ? Dec 27, 2014 08:08 |
|
The Python wrapper is thin as anything - in particular for console_put_char it just takes ord(c) if you pass it str or bytes and passes anything else as-is. All the "ASCII" constants are just defined as numbers between 0 and 255. You don't even need to think about Unicode.
|
# ? Dec 27, 2014 18:19 |
|
Crosscontaminant posted:The Python wrapper is thin as anything - in particular for console_put_char it just takes ord(c) if you pass it str or bytes and passes anything else as-is. All the "ASCII" constants are just defined as numbers between 0 and 255. You don't even need to think about Unicode. This works! I just pass the values as decimal numbers, rather than as u'\u####' unicode characters.
|
# ? Dec 29, 2014 01:31 |
|
I'm writing a small django app thing and I need to set a few GET parameters to None if they're not in the URL, is it better to do:Python code:
Python code:
Literally Elvis fucked around with this message at 17:25 on Dec 31, 2014 |
# ? Dec 31, 2014 17:21 |
|
Those bits of code will produce different results if args["term"] is called and request.GET["t"] is false. The first case will return None, and the second will raise a KeyError. Your second example, changed to match the results of the first: Python code:
Dominoes fucked around with this message at 17:33 on Dec 31, 2014 |
# ? Dec 31, 2014 17:30 |
|
Literally Elvis posted:I'm writing a small django app thing and I need to set a few GET parameters to None if they're not in the URL, is it better to do: I believe you want to do something like so: Python code:
Python code:
Lumpy fucked around with this message at 17:37 on Dec 31, 2014 |
# ? Dec 31, 2014 17:32 |
|
Dominoes posted:Those bits of code will produce different results if args["term"] is called and request.GET["t"] is false. Right, I done goofed, but what you said is what I meant. Lumpy posted:I believe you want to do something like so: That'll do.
|
# ? Dec 31, 2014 17:35 |
|
Lumpy posted:dict.get() Elvis, use the shortcut in your first example whenever it's applicable. It looks cleaner than using multiple lines with indentations. Dominoes fucked around with this message at 18:23 on Dec 31, 2014 |
# ? Dec 31, 2014 17:54 |
|
I have a few 2D arrays, "level[x][y]" specifying all the qualities of map tiles for a big (x, y) grid. But now I want to save these maps and keep a list of arrays. At first I thought about just making it 3D, with the first dimension being the order of the maps, but I also thought about using a dict where the key is a string which describes the map (outdoors, indoors, surface, underground) and the value would be the map itself, but this wouldn't let me do multiple string assignments to each value. Is there a way I can keep a list of arrays and have several strings describing each one? For example, if I want to affect all maps which are indoors, it'd be great if I could just look up everything that fits that description. I need to pass this list around through many functions too and I'm trying to move away from using global to do it.
|
# ? Jan 1, 2015 16:27 |
|
You can use a dictionary as a "value-bag".Python code:
|
# ? Jan 1, 2015 16:45 |
|
Suspicious Dish posted:You can use a dictionary as a "value-bag". Is that a list-of-dicts?
|
# ? Jan 1, 2015 21:22 |
|
reading posted:Is that a list-of-dicts?
|
# ? Jan 1, 2015 22:10 |
|
I think I might just go with multiple space-delimited words in the key and just do string searches/string comprehension on them as needed, so that I can stick with a very basic dict.
|
# ? Jan 1, 2015 23:04 |
|
reading posted:I think I might just go with multiple space-delimited words in the key and just do string searches/string comprehension on them as needed, so that I can stick with a very basic dict. This.... I don't know what to say, really.
|
# ? Jan 2, 2015 00:28 |
|
You're going to do what now?
|
# ? Jan 2, 2015 01:08 |
|
Sorry I mean something like: { "outdoors surface": map1} and then search for all keys that contain the word "outdoors" for example.
|
# ? Jan 2, 2015 02:13 |
|
Why would you want to do that though? Suspicious D's version basically has a set of clear fields you can use for whatever lookups you like, you're just smooshing them all into a string you'll have to take apart and analyse later. Naming complexity and efficiency aside, is this actually making your life any easier? Are you just uneasy about nesting dicts? Think of them as records in a table, if you like e- if you wanted a list of outdoor maps you could just do [map['map'] for map in maps if map['type'] == 'outdoors'] and you're done. I don't think it would be much different for a string, just slower and more prone to issues (like what if you want to tag several attributes with 'large' or 'small'? Do you prefix with the attribute so you know what 'small' in the string actually refers to?) baka kaba fucked around with this message at 02:36 on Jan 2, 2015 |
# ? Jan 2, 2015 02:27 |
|
reading posted:Sorry I mean something like: { "outdoors surface": map1} and then search for all keys that contain the word "outdoors" for example. Python code:
Python code:
|
# ? Jan 2, 2015 02:28 |
|
Not to mention that with a dict key that is just a bunch of keywords you are ensuring that you are never going to use the dict for key-based lookup, thus defeating the purpose of using a dict at all. So if you really wanted to use a keywords approach, instead use (name, keywords, map) tuples. And make keywords a set or list of strings. But really the list-of-dicts suggestion is the obvious answer here. The above is much more explicit than Python code:
SurgicalOntologist fucked around with this message at 03:35 on Jan 2, 2015 |
# ? Jan 2, 2015 03:32 |
|
It's definitely way more efficient to use multiple dictionary entries instead of a single long string that you then have to parse. Don't do the string parsing thing, that's going to be slow as poo poo You could also use a class Python code:
|
# ? Jan 2, 2015 04:52 |
|
QuarkJets posted:It's definitely way more efficient to use multiple dictionary entries instead of a single long string that you then have to parse. Don't do the string parsing thing, that's going to be slow as poo poo I think I will go this route. I was initially confused about how an object might absorb and index all the maps, but making a list of objects seems clear. I was thinking about it backwards. Edit: quick question, is "class MyClass(object): " and "class MyClass: " just a notation thing, or does including (object) matter somewhere? Thanks everyone for your help and code examples. reading fucked around with this message at 07:34 on Jan 2, 2015 |
# ? Jan 2, 2015 07:27 |
|
reading posted:Edit: quick question, is "class MyClass(object): " and "class MyClass: " just a notation thing, or does including (object) matter somewhere? The reasoning is historical. If you proceed rigidly by the above approach, you won't run into problems. You should always use new-style classes in new code since Python 2.3. The basic reason is that Python classes before 2.3 were a lot different and weren't treated like first-class objects. If you use the new-style classes in Python 2.x by inheriting from object, classes can be treated properly as types and objects can be identified as instances of their classes. Python 3.x thankfully got rid of the old-style classes, so everything inherits from object by default. You can read up on new-style vs. old-style classes if you're particularly interested. Here is some quick code from 2.7 that gives hints about how these types of classes differ. Python code:
|
# ? Jan 2, 2015 13:40 |
|
QuarkJets posted:It's definitely way more efficient to use multiple dictionary entries instead of a single long string that you then have to parse. Don't do the string parsing thing, that's going to be slow as poo poo I've run in to a problem with implementing this. Since the maps are 2D arrays (and I obtain information about each square with, for example, "map[x][y].info") I don't know how to create a __getitem__ method in the GameMap class which will allow this kind of iteration. I keep getting TypeError: "GameMap" object does not support indexing.
|
# ? Jan 3, 2015 00:22 |
|
Python code:
Edit: you may want to use self._map instead of self.map. The leading underscore is convention for "outside of the class, you don't need to know this exists". So if you want users of GameMap (i.e., yourself, in other code) to interact with the object directly instead of accessing its map attribute all the time, that would be signified by a leading underscore on the attribute name. SurgicalOntologist fucked around with this message at 00:42 on Jan 3, 2015 |
# ? Jan 3, 2015 00:40 |
|
reading posted:I've run in to a problem with implementing this. Since the maps are 2D arrays (and I obtain information about each square with, for example, "map[x][y].info") I don't know how to create a __getitem__ method in the GameMap class which will allow this kind of iteration. I keep getting TypeError: "GameMap" object does not support indexing. It sounds like you're trying to index into a GameMap object directly instead of accessing its map attribute. Try map.map[x][y].info, or define the __getitem__ method like as was described above
|
# ? Jan 3, 2015 10:35 |
|
|
# ? May 9, 2024 10:15 |
|
Unrelated to your issue, but something to be aware of because things like this can really bite you in the butt later: If you use "map" as a variable name, you are overwriting the built-in Python function "map" with your data. You variable will work, but the function won't, and it won't be obvious why code that uses that function looks like it should be working but isn't. You're probably not using the "map" function in your script and may not need to, but it's a good habit to avoid using Python reserved words as variables. Otherwise, it's really easy to accidentally write things like "list = [1,2,3]" or "type = 'mountains'" or "range = 10". You *can* use reserved words as class attribute names like "self.map[x][y]" in the example from SurgicalOntologist below, because they're attached to the object. PyCharm was invaluable when I was learning because it flagged that kind of stuff.
|
# ? Jan 3, 2015 15:13 |