Register
Email: Password:
Forum » Python. How do you do things with it?

Python. How do you do things with it?

Dorten 15 years ago
Subject: Notrium Mod Viewer

"Amarth" said:
PS: And about this different direction: is there anyone good with Python?
Yes.

I'm just starting to learn it. Currently translating this:[attachment=0]<!-- ia0 -->TheProject.rar<!-- ia0 -->[/attachment] to Python, and then will be expanding it further.

Here's the question.

If I write something like this:
Creature.py:

from Player import *

class Creature:
...
if isinstance(this,Player):
...


Player.py:

from Creature import *

class Player(Creature):
...


It gives a error in Creature.py, saying: "Wassafuck is Player?"

if I replace "from Player import *" with "from Player import Player", it says: "Cannot import Player"

but if I write "import Player" and "if isinstance(this,Player.Player):", THEN it works.

I guess no workaround here?
#
Amarth 15 years ago
Sounds like a name clash between module names (files) and classes. You can basically see Python as having a giant string-to-object map where about everything is an object, so Python wouldn't know what to do with the "Player" string: is it the module or the class?

Easiest (and standard) way to solve it is by making your files lowercase, then "import player.Player" (or any variant) should work fine. Otherwise you'll have to just "import Player" (the module), and you can refer to the class as Player.Player, but that's a bit verbose and doesn't read really well. It *might* be possible to do an explicit assignment like "Player = Player.Player" but just looking at it should make clear that is madness.
#
Dorten 15 years ago
Another thingie:

I'm using libtcod to do the console, and there is problem with output of strings, which do not contain any symbols from '\x00' to '\x7f'.

I'm using some symbols from '\x80' to '\xff' to represent walls, for example.

It goes like: libtcod.console_print_left(0, x, y, libtcod.BKGND_NONE, Text)

If text is some old good string like 'Hello!' all is OK, if it's something like '└───┘.' it's OK, but if I remove last point, so that there's only pseudo-graph symbols in the string BAM! It just does not print.

I dunno if it's because of libtcod problems, or because of the way Python handles such symbols. Any ideas?
#
Amarth 15 years ago
Not entirely sure. Strings can be complex beasts, and I think in theory codepoints above \x80 don't make sense specifying without an encoding. It might be a problem in either the library or your code. Strange it works when including one other character though, but that might again be pointing to an encoding problem. I don't know, I don't trust exotic strings and I don't think any language ever got them right (mostly because operating systems don't even get them right all of the time).
#
MageKing17 15 years ago
You've got a circular import going on. Your Creature file is importing Player, which is importing Creature. This causes it to go "What the fuck is going on?"

Unfortunately, Python being Python, it doesn't just say "You've got a circular import", but instead says it can't find the module.
#
Dorten 15 years ago
It still works if I just make it "import Player" and "import Creature" respectively, so no big problem here

Also: is there any way to force garbage collection? My code is not very clean, and apparently it makes a lot of garbage: you play for some time and it starts to slow down. Leave it be for a couple of mins, and garbage collection apparently kicks in, making it fast again...
#
Amarth 15 years ago
Theoretically, having garbage shouldn't doesn't slow you down, it's cleaning the garbage that is slow. Well, unless you memory gets swapped to disk but then you either use an awful lot of memory or you don't have enough RAM.

Garbage is just memory that is allocated but not reachable. Since all memory accesses are done in basically constant time it doesn't matter how much garbage you have to use the memory that is reachable. However, determining whether a given allocated memory place is reachable does take a while since the garbage collector potentially needs to dereference all existing variables.

So in short, I believe it's either the GC running that slows you down (though I think you should see short pauses then, not just slowdown), or something in your code (like a list you search linearly that keeps growing).

To answer your actual question though, I think it's possible yes, not sure though. Google it?
#
Dorten 15 years ago
I found psyco!

That's what I call speedup!
#
Forum » Python. How do you do things with it?

Post Reply


Your email:
Your name: