Порядок вложенных структур в таблице PyTable

Предположим, у меня есть следующий дескриптор столбца PyTable:

import numpy as np
import tables as pt

class Date_t(pt.IsDescription):
    year    = pt.Int32Col(shape=(), dflt=2013, pos=0)
    month   = pt.Int32Col(shape=(), dflt=1,    pos=1)
    day     = pt.Int32Col(shape=(), dflt=1,    pos=2)

class Info(pt.IsDescription):
    col1       = pt.Int32Col(shape=(), dflt=0, pos=0)
    startdate  = Date_t() 
    birthdate  = Date_t() 
    col2       = pt.Int32Col(shape=(), dflt=0, pos=3)
    enddate    = Date_t() 
    col3       = pt.Int32Col(shape=(), dflt=0, pos=5)
    col4       = pt.Int32Col(shape=(), dflt=0, pos=6) 

Как указать положение «даты начала», «даты рождения» и «даты окончания»?
Я подумал, что могу сделать что-то вроде:

startdate = Date_t(pos=1)
birthdate = Date_t(pos=2)

и переопределить класс Date_t как:

class Date_t(pt.IsDescription):
    def __init__(self, pos):
        self._v_pos = pos
    year    = pt.Int32Col(shape=(), dflt=2013, pos=0)
    month   = pt.Int32Col(shape=(), dflt=1,    pos=1)
    day     = pt.Int32Col(shape=(), dflt=1,    pos=2)

но это дает мне ошибку:
TypeError: object.new() не принимает параметров

Любые идеи?


person Joel Vroom    schedule 08.05.2013    source источник


Ответы (1)


Несколько хакерским способом вы можете изменить атрибуты класса Info после его определения.

class Info(pt.IsDescription):
    col1       = pt.Int32Col(shape=(), dflt=0, pos=0)
    startdate  = Date_t() 
    birthdate  = Date_t() 
    col2       = pt.Int32Col(shape=(), dflt=0, pos=3)
    enddate    = Date_t() 
    col3       = pt.Int32Col(shape=(), dflt=0, pos=5)
    col4       = pt.Int32Col(shape=(), dflt=0, pos=6) 

Info.columns['startdate']._v_pos = 1
Info.columns['birthdate']._v_pos = 2
Info.columns['enddate']._v_pos   = 3

В pytables есть функция под названием descr_from_dtype(), которая создает описание таблицы из (возможно, вложенный) numpy dtype. Он не импортируется автоматически при запуске from tables import *, поэтому вы должны импортировать его явно. Эта функция может пригодиться, когда у вас возникают проблемы с синтаксисом вложенных таблиц.

person streeto    schedule 31.05.2013