Port Matrixify_attributes.php to Python