我正在尝试将我用 PHP 编写的东西移植到 Python 中,主要是为了更好地学习这门语言。有问题的代码是一个 SWF 解析器。在 PHP 中,我将所有数据结构都声明为类。我正在尝试在 Python 中做同样的事情,但似乎没有明确的方式来声明类变量。所以我最终得到了许多看起来像这样的类:
class SWFRGBA(object):
red = 0
green = 0
blue = 0
alpha = 0
Pythoners真的会写这样的东西吗?
[编辑]
让我发布一些实际代码来说明这个问题。下面的函数读取 SWF 文件中的矢量形状。readUB(), readSB() 读取一定数量的位,将它们解释为无符号或有符号。有时,给定字段所需的位数本身是从比特流中读取的。可能会出现三种类型的记录:直边、二次曲线或样式变化。样式更改记录可能会移动笔位置、更改线条样式索引、更改两个填充样式索引之一或替换样式数组。
protected function readShapeRecords($numFillBits, $numLineBits, $version, &$bytesAvailable) {
$records = array();
while($bytesAvailable) {
if($this->readUB(1, $bytesAvailable)) {
// edge
if($this->readUB(1, $bytesAvailable)) {
// straight
$line = new SWFStraightEdge;
$line->numBits = $this->readUB(4, $bytesAvailable) + 2;
if($this->readUB(1, $bytesAvailable)) {
// general line
$line->deltaX = $this->readSB($line->numBits, $bytesAvailable);
$line->deltaY = $this->readSB($line->numBits, $bytesAvailable);
} else {
if($this->readUB(1, $bytesAvailable)) {
// vertical
$line->deltaX = 0;
$line->deltaY = $this->readSB($line->numBits, $bytesAvailable);
} else {
// horizontal
$line->deltaX = $this->readSB($line->numBits, $bytesAvailable);
$line->deltaY = 0;
}
}
$records[] = $line;
} else {
// curve
$curve = new SWFQuadraticCurve;
$curve->numBits = $this->readUB(4, $bytesAvailable) + 2;
$curve->controlDeltaX = $this->readSB($curve->numBits, $bytesAvailable);
$curve->controlDeltaY = $this->readSB($curve->numBits, $bytesAvailable);
$curve->anchorDeltaX = $this->readSB($curve->numBits, $bytesAvailable);
$curve->anchorDeltaY = $this->readSB($curve->numBits, $bytesAvailable);
$records[] = $curve;
}
} else {
$flags = $this->readUB(5, $bytesAvailable);
if(!$flags) {
break;
} else {
// style change
$change = new SWFStyleChange;
if($flags & 0x01) {
$change->numMoveBits = $this->readSB(5, $bytesAvailable);
$change->moveDeltaX = $this->readSB($change->numMoveBits, $bytesAvailable);
$change->moveDeltaY = $this->readSB($change->numMoveBits, $bytesAvailable);
}
if($flags & 0x02) {
$change->fillStyle0 = $this->readUB($numFillBits, $bytesAvailable);
}
if($flags & 0x04) {
$change->fillStyle1 = $this->readUB($numFillBits, $bytesAvailable);
}
if($flags & 0x08) {
$change->lineStyle = $this->readUB($numLineBits, $bytesAvailable);
}
if($flags & 0x10) {
$change->newFillStyles = $this->readFillStyles($version, $bytesAvailable);
$change->newLineStyles = $this->readLineStyles($version, $bytesAvailable);
$change->numFillBits = $numFillBits = $this->readUB(4, $bytesAvailable);
$change->numLineBits = $numLineBits = $this->readUB(4, $bytesAvailable);
}
$records[] = $change;
}
}
}
$this->alignToByte();
return $records;
}