desc
stringlengths
3
26.7k
decl
stringlengths
11
7.89k
bodies
stringlengths
8
553k
'A CSV representation of the :class:`Dataset` object. The top row will contain headers, if they have been set. Otherwise, the top row will contain the first row of the dataset. A dataset object can also be imported by setting the :class:`Dataset.csv` attribute. :: data = tablib.Dataset() data.csv = \'age, first_name, l...
@property def csv():
pass
'A TSV representation of the :class:`Dataset` object. The top row will contain headers, if they have been set. Otherwise, the top row will contain the first row of the dataset. A dataset object can also be imported by setting the :class:`Dataset.tsv` attribute. :: data = tablib.Dataset() data.tsv = \'age first_name...
@property def tsv():
pass
'A YAML representation of the :class:`Dataset` object. If headers have been set, a YAML list of objects will be returned. If no headers have been set, a YAML list of lists (rows) will be returned instead. A dataset object can also be imported by setting the :class:`Dataset.yaml` attribute: :: data = tablib.Dataset() da...
@property def yaml():
pass
'A JSON representation of the :class:`Dataset` object. If headers have been set, a JSON list of objects will be returned. If no headers have been set, a JSON list of lists (rows) will be returned instead. A dataset object can also be imported by setting the :class:`Dataset.json` attribute: :: data = tablib.Dataset() da...
@property def json():
pass
'A HTML table representation of the :class:`Dataset` object. If headers have been set, they will be used as table headers. ..notice:: This method can be used for export only.'
@property def html():
pass
'A dBASE representation of the :class:`Dataset` object. A dataset object can also be imported by setting the :class:`Dataset.dbf` attribute. :: # To import data from an existing DBF file: data = tablib.Dataset() data.dbf = open(\'existing_table.dbf\').read() # to import data from an ASCII-encoded bytestring: data = tab...
@property def dbf():
pass
'A LaTeX booktabs representation of the :class:`Dataset` object. If a title has been set, it will be exported as the table caption. .. note:: This method can be used for export only.'
@property def latex():
pass
'Inserts a row to the :class:`Dataset` at the given index. Rows inserted must be the correct size (height or width). The default behaviour is to insert the given row to the :class:`Dataset` object at the given index.'
def insert(self, index, row, tags=list()):
self._validate(row) self._data.insert(index, Row(row, tags=tags))
'Adds a row to the end of the :class:`Dataset`. See :class:`Dataset.insert` for additional documentation.'
def rpush(self, row, tags=list()):
self.insert(self.height, row=row, tags=tags)
'Adds a row to the top of the :class:`Dataset`. See :class:`Dataset.insert` for additional documentation.'
def lpush(self, row, tags=list()):
self.insert(0, row=row, tags=tags)
'Adds a row to the :class:`Dataset`. See :class:`Dataset.insert` for additional documentation.'
def append(self, row, tags=list()):
self.rpush(row, tags)
'Adds a list of rows to the :class:`Dataset` using :class:`Dataset.append`'
def extend(self, rows, tags=list()):
for row in rows: self.append(row, tags)
'Removes and returns the first row of the :class:`Dataset`.'
def lpop(self):
cache = self[0] del self[0] return cache
'Removes and returns the last row of the :class:`Dataset`.'
def rpop(self):
cache = self[(-1)] del self[(-1)] return cache
'Removes and returns the last row of the :class:`Dataset`.'
def pop(self):
return self.rpop()
'Inserts a column to the :class:`Dataset` at the given index. Columns inserted must be the correct height. You can also insert a column of a single callable object, which will add a new column with the return values of the callable each as an item in the column. :: data.append_col(col=random.randint) If inserting a col...
def insert_col(self, index, col=None, header=None):
if (col is None): col = [] if hasattr(col, '__call__'): col = list(map(col, self._data)) col = self._clean_col(col) self._validate(col=col) if self.headers: if (not header): raise HeadersNeeded() elif (header and (self.height == 0) and len(col)): ...
'Adds a column to the end of the :class:`Dataset`. See :class:`Dataset.insert` for additional documentation.'
def rpush_col(self, col, header=None):
self.insert_col(self.width, col, header=header)
'Adds a column to the top of the :class:`Dataset`. See :class:`Dataset.insert` for additional documentation.'
def lpush_col(self, col, header=None):
self.insert_col(0, col, header=header)
'Adds a separator to :class:`Dataset` at given index.'
def insert_separator(self, index, text='-'):
sep = (index, text) self._separators.append(sep)
'Adds a :ref:`separator <separators>` to the :class:`Dataset`.'
def append_separator(self, text='-'):
if (not self.headers): index = (self.height if self.height else 0) else: index = ((self.height + 1) if self.height else 1) self.insert_separator(index, text)
'Adds a column to the :class:`Dataset`. See :class:`Dataset.insert_col` for additional documentation.'
def append_col(self, col, header=None):
self.rpush_col(col, header)
'Returns the column from the :class:`Dataset` at the given index.'
def get_col(self, index):
return [row[index] for row in self._data]
'Adds a :ref:`formatter` to the :class:`Dataset`. .. versionadded:: 0.9.5 :param col: column to. Accepts index int or header str. :param handler: reference to callback function to execute against each cell value.'
def add_formatter(self, col, handler):
if isinstance(col, str): if (col in self.headers): col = self.headers.index(col) else: raise KeyError if (not (col > self.width)): self._formatters.append((col, handler)) else: raise InvalidDatasetIndex return True
'Returns a new instance of the :class:`Dataset`, excluding any rows that do not contain the given :ref:`tags <tags>`.'
def filter(self, tag):
_dset = copy(self) _dset._data = [row for row in _dset._data if row.has_tag(tag)] return _dset
'Sort a :class:`Dataset` by a specific column, given string (for header) or integer (for column index). The order can be reversed by setting ``reverse`` to ``True``. Returns a new :class:`Dataset` instance where columns have been sorted.'
def sort(self, col, reverse=False):
if (isinstance(col, str) or isinstance(col, unicode)): if (not self.headers): raise HeadersNeeded _sorted = sorted(self.dict, key=itemgetter(col), reverse=reverse) _dset = Dataset(headers=self.headers, title=self.title) for item in _sorted: row = [item[key] fo...
'Transpose a :class:`Dataset`, turning rows into columns and vice versa, returning a new ``Dataset`` instance. The first row of the original instance becomes the new header row.'
def transpose(self):
if (not self): return _dset = Dataset() new_headers = ([self.headers[0]] + self[self.headers[0]]) _dset.headers = new_headers for (index, column) in enumerate(self.headers): if (column == self.headers[0]): continue row_data = ([column] + self.get_col(index)) ...
'Stack two :class:`Dataset` instances together by joining at the row level, and return new combined ``Dataset`` instance.'
def stack(self, other):
if (not isinstance(other, Dataset)): return if (self.width != other.width): raise InvalidDimensions _dset = copy(self) rows_to_stack = [row for row in _dset._data] other_rows = [row for row in other._data] rows_to_stack.extend(other_rows) _dset._data = rows_to_stack retur...
'Stack two :class:`Dataset` instances together by joining at the column level, and return a new combined ``Dataset`` instance. If either ``Dataset`` has headers set, than the other must as well.'
def stack_cols(self, other):
if (not isinstance(other, Dataset)): return if (self.headers or other.headers): if ((not self.headers) or (not other.headers)): raise HeadersNeeded if (self.height != other.height): raise InvalidDimensions try: new_headers = (self.headers + other.headers) ...
'Removes all duplicate rows from the :class:`Dataset` object while maintaining the original order.'
def remove_duplicates(self):
seen = set() self._data[:] = [row for row in self._data if (not ((tuple(row) in seen) or seen.add(tuple(row))))]
'Removes all content and headers from the :class:`Dataset` object.'
def wipe(self):
self._data = list() self.__headers = None
'Returns a new instance of the :class:`Dataset`, including only specified rows and columns.'
def subset(self, rows=None, cols=None):
if (not self): return if (rows is None): rows = list(range(self.height)) if (cols is None): cols = list(self.headers) rows = [row for row in rows if (row in range(self.height))] cols = [header for header in cols if (header in self.headers)] _dset = Dataset() _dset.hea...
'Removes all :class:`Dataset` objects from the :class:`Databook`.'
def wipe(self):
self._datasets = []
'Adds format properties.'
@classmethod def _register_formats(cls):
for fmt in formats.available: try: try: setattr(cls, fmt.title, property(fmt.export_book, fmt.import_book)) cls._formats[fmt.title] = (fmt.export_book, fmt.import_book) except AttributeError: setattr(cls, fmt.title, property(fmt.export_...
'Adds given :class:`Dataset` to the :class:`Databook`.'
def add_sheet(self, dataset):
if isinstance(dataset, Dataset): self._datasets.append(dataset) else: raise InvalidDatasetType
'Packages :class:`Databook` for delivery.'
def _package(self, ordered=True):
collector = [] if ordered: dict_pack = OrderedDict else: dict_pack = dict for dset in self._datasets: collector.append(dict_pack(title=dset.title, data=dset._package(ordered=ordered))) return collector
'The number of the :class:`Dataset` objects within :class:`Databook`.'
@property def size(self):
return len(self._datasets)
'Import `in_stream` to the :class:`Databook` object using the `format`. :param \*\*kwargs: (optional) custom configuration to the format `import_book`.'
def load(self, format, in_stream, **kwargs):
if (not format): format = detect_format(in_stream) (export_book, import_book) = self._formats.get(format, (None, None)) if (not import_book): raise UnsupportedFormat('Format {0} cannot be loaded.'.format(format)) import_book(self, in_stream, **kwargs) return self
'Export :class:`Databook` object to `format`. :param \*\*kwargs: (optional) custom configuration to the format `export_book`.'
def export(self, format, **kwargs):
(export_book, import_book) = self._formats.get(format, (None, None)) if (not export_book): raise UnsupportedFormat('Format {0} cannot be exported.'.format(format)) return export_book(self, **kwargs)
'Append the actual tags to content.'
def render(self, tag, single, between, kwargs):
out = ('<%s' % tag) for (key, value) in kwargs.items(): if (value is not None): key = key.strip('_') if (key == 'http_equiv'): key = 'http-equiv' elif (key == 'accept_charset'): key = 'accept-charset' out = ('%s %s="%s"' ...
'Append a closing tag unless element has only opening tag.'
def close(self):
if (self.tag in self.parent.twotags): self.parent.content.append(('</%s>' % self.tag)) elif (self.tag in self.parent.onetags): raise ClosingError(self.tag) elif ((self.parent.mode == 'strict_html') and (self.tag in self.parent.deptags)): raise DeprecationError(self.tag)
'Append an opening tag.'
def open(self, **kwargs):
if ((self.tag in self.parent.twotags) or (self.tag in self.parent.onetags)): self.render(self.tag, False, None, kwargs) elif ((self.mode == 'strict_html') and (self.tag in self.parent.deptags)): raise DeprecationError(self.tag)
'Stuff that effects the whole document. mode -- \'strict_html\' for HTML 4.01 (default) \'html\' alias for \'strict_html\' \'loose_html\' to allow some deprecated elements \'xml\' to allow arbitrary elements case -- \'lower\' element names will be printed in lower case (default) \'upper\...
def __init__(self, mode='strict_html', case='lower', onetags=None, twotags=None, separator='\n', class_=None):
valid_onetags = ['AREA', 'BASE', 'BR', 'COL', 'FRAME', 'HR', 'IMG', 'INPUT', 'LINK', 'META', 'PARAM'] valid_twotags = ['A', 'ABBR', 'ACRONYM', 'ADDRESS', 'B', 'BDO', 'BIG', 'BLOCKQUOTE', 'BODY', 'BUTTON', 'CAPTION', 'CITE', 'CODE', 'COLGROUP', 'DD', 'DEL', 'DFN', 'DIV', 'DL', 'DT', 'EM', 'FIELDSET', 'FORM', 'FR...
'Return the document as a string. escape -- False print normally True replace < and > by &lt; and &gt; the default escape sequences in most browsers'
def __call__(self, escape=False):
if escape: return _escape(self.__str__()) else: return self.__str__()
'This is an alias to addcontent.'
def add(self, text):
self.addcontent(text)
'Add some text to the bottom of the document'
def addfooter(self, text):
self.footer.append(text)
'Add some text to the top of the document'
def addheader(self, text):
self.header.append(text)
'Add some text to the main part of the document'
def addcontent(self, text):
self.content.append(text)
'This method is used for complete documents with appropriate doctype, encoding, title, etc information. For an HTML/XML snippet omit this method. lang -- language, usually a two character string, will appear as <html lang=\'en\'> in html mode (ignored in xml mode) css -- Cascading Style Sheet filename as a str...
def init(self, lang='en', css=None, metainfo=None, title=None, header=None, footer=None, charset=None, encoding=None, doctype=None, bodyattrs=None, script=None):
self._full = True if ((self.mode == 'strict_html') or (self.mode == 'loose_html')): if (doctype is None): doctype = "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>" self.header.append(doctype) self.html(lang=lang) self.head() ...
'This convenience function is only useful for html. It adds css stylesheet(s) to the document via the <link> element.'
def css(self, filelist):
if isinstance(filelist, str): self.link(href=filelist, rel='stylesheet', type='text/css', media='all') else: for file in filelist: self.link(href=file, rel='stylesheet', type='text/css', media='all')
'This convenience function is only useful for html. It adds meta information via the <meta> element, the argument is a dictionary of the form { \'name\':\'content\' }.'
def metainfo(self, mydict):
if isinstance(mydict, dict): for (name, content) in mydict.items(): self.meta(name=name, content=content) else: raise TypeError('Metainfo should be called with a dictionary argument of name:content pairs.')
'Only useful in html, mydict is dictionary of src:type pairs will be rendered as <script type=\'text/type\' src=src></script>'
def scripts(self, mydict):
if isinstance(mydict, dict): for (src, type) in mydict.items(): self.script('', src=src, type=('text/%s' % type)) else: raise TypeError('Script should be given a dictionary of src:type pairs.')
'Return `DbfFieldDef` instance from the current definition.'
def getDbfField(self):
return self.cls(self.name, self.len, self.dec)
'Create a `DbfFieldDef` instance and append it to the dbf header. Arguments: dbfh: `DbfHeader` instance.'
def appendToHeader(self, dbfh):
_dbff = self.getDbfField() dbfh.addField(_dbff)
'Add field definition. Arguments: name: field name (str object). field name must not contain ASCII NULs and it\'s length shouldn\'t exceed 10 characters. typ: type of the field. this must be a single character from the "CNLMDT" set meaning character, numeric, logical, memo, date and date/time respectively. len: length ...
def add_field(self, name, typ, len, dec=0):
self.fields.append(self.FieldDefinitionClass(name, typ, len, dec))
'Create empty .DBF file using current structure.'
def write(self, filename):
_dbfh = DbfHeader() _dbfh.setCurrentDate() for _fldDef in self.fields: _fldDef.appendToHeader(_dbfh) _dbfStream = open(filename, 'wb') _dbfh.write(_dbfStream) _dbfStream.close()
'Initialize instance.'
def __init__(self, name, length=None, decimalCount=None, start=None, stop=None, ignoreErrors=False):
assert (self.typeCode is not None), 'Type code must be overriden' assert (self.defaultValue is not None), 'Default value must be overriden' if (len(name) > 10): raise ValueError(('Field name "%s" is too long' % name)) name = str(name).upper() if (self._...
'Decode dbf field definition from the string data. Arguments: string: a string, dbf definition is decoded from. length of the string must be 32 bytes. start: position in the database file. ignoreErrors: initial error processing mode for the new field (boolean)'
def fromString(cls, string, start, ignoreErrors=False):
assert (len(string) == 32) _length = string[16] return cls(utils.unzfill(string)[:11].decode('utf-8'), _length, string[17], start, (start + _length), ignoreErrors=ignoreErrors)
'Return encoded field definition. Return: Return value is a string object containing encoded definition of this field.'
def toString(self):
if (sys.version_info < (2, 4)): _name = (self.name[:11] + ('\x00' * (11 - len(self.name)))) else: _name = self.name.ljust(11, '\x00') return (((((_name + self.typeCode) + (chr(0) * 4)) + chr(self.length)) + chr(self.decimalCount)) + (chr(0) * 14))
'Return field information. Return: Return value is a (name, type, length, decimals) tuple.'
def fieldInfo(self):
return (self.name, self.typeCode, self.length, self.decimalCount)
'Return a "raw" field value from the record string.'
def rawFromRecord(self, record):
return record[self.start:self.end]
'Return decoded field value from the record string.'
def decodeFromRecord(self, record):
try: return self.decodeValue(self.rawFromRecord(record)) except: if self.ignoreErrors: return utils.INVALID_VALUE else: raise
'Return decoded value from string value. This method shouldn\'t be used publicly. It\'s called from the `decodeFromRecord` method. This is an abstract method and it must be overridden in child classes.'
def decodeValue(self, value):
raise NotImplementedError
'Return str object containing encoded field value. This is an abstract method and it must be overriden in child classes.'
def encodeValue(self, value):
raise NotImplementedError
'Return string object. Return value is a ``value`` argument with stripped right spaces.'
def decodeValue(self, value):
return value.rstrip(' ').decode('utf-8')
'Return raw data string encoded from a ``value``.'
def encodeValue(self, value):
return str(value)[:self.length].ljust(self.length)
'Return a number decoded from ``value``. If decimals is zero, value will be decoded as an integer; or as a float otherwise. Return: Return value is a int (long) or float instance.'
def decodeValue(self, value):
value = value.strip(' \x00') if ('.' in value): return float(value) elif value: return int(value) else: return 0
'Return string containing encoded ``value``.'
def encodeValue(self, value):
_rv = ('%*.*f' % (self.length, self.decimalCount, value)) if (len(_rv) > self.length): _ppos = _rv.find('.') if (0 <= _ppos <= self.length): _rv = _rv[:self.length] else: raise ValueError(('[%s] Numeric overflow: %s (field width: %i)' % (self.nam...
'Return an integer number decoded from ``value``.'
def decodeValue(self, value):
return struct.unpack('<i', value)[0]
'Return string containing encoded ``value``.'
def encodeValue(self, value):
return struct.pack('<i', int(value))
'Return float number decoded from ``value``.'
def decodeValue(self, value):
return (struct.unpack('<q', value)[0] / 10000.0)
'Return string containing encoded ``value``.'
def encodeValue(self, value):
return struct.pack('<q', round((value * 10000)))
'Return True, False or -1 decoded from ``value``.'
def decodeValue(self, value):
if (value == '?'): return (-1) if (value in 'NnFf '): return False if (value in 'YyTt'): return True raise ValueError(('[%s] Invalid logical value %r' % (self.name, value)))
'Return a character from the "TF?" set. Return: Return value is "T" if ``value`` is True "?" if value is -1 or False otherwise.'
def encodeValue(self, value):
if (value is True): return 'T' if (value == (-1)): return '?' return 'F'
'Return int .dbt block number decoded from the string object.'
def decodeValue(self, value):
raise NotImplementedError
'Return raw data string encoded from a ``value``. Note: this is an internal method.'
def encodeValue(self, value):
raise NotImplementedError
'Return a ``datetime.date`` instance decoded from ``value``.'
def decodeValue(self, value):
if value.strip(): return utils.getDate(value) else: return None
'Return a string-encoded value. ``value`` argument should be a value suitable for the `utils.getDate` call. Return: Return value is a string in format "yyyymmdd".'
def encodeValue(self, value):
if value: return utils.getDate(value).strftime('%Y%m%d') else: return (' ' * self.length)
'Return a `datetime.datetime` instance.'
def decodeValue(self, value):
assert (len(value) == self.length) (_jdn, _msecs) = struct.unpack('<2I', value) if (_jdn >= 1): _rv = datetime.datetime.fromordinal((_jdn - self.JDN_GDN_DIFF)) _rv += datetime.timedelta(0, (_msecs / 1000.0)) else: _rv = None return _rv
'Return a string-encoded ``value``.'
def encodeValue(self, value):
if value: value = utils.getDateTime(value) _rv = struct.pack('<2I', (value.toordinal() + self.JDN_GDN_DIFF), ((((value.hour * 3600) + (value.minute * 60)) + value.second) * 1000)) else: _rv = ('\x00' * self.length) assert (len(_rv) == self.length) return _rv
'Initialize instance. Arguments: fields: a list of field definitions; recordLength: size of the records; headerLength: size of the header; recordCount: number of records stored in DBF; signature: version number (aka signature). using 0x03 as a default meaning "File without DBT". for more information about this field vi...
def __init__(self, fields=None, headerLength=0, recordLength=0, recordCount=0, signature=3, lastUpdate=None, ignoreErrors=False):
self.signature = signature if (fields is None): self.fields = [] else: self.fields = list(fields) self.lastUpdate = getDate(lastUpdate) self.recordLength = recordLength self.headerLength = headerLength self.recordCount = recordCount self.ignoreErrors = ignoreErrors se...
'Return header instance from the string object.'
def fromString(cls, string):
return cls.fromStream(io.StringIO(str(string)))
'Return header object from the stream.'
def fromStream(cls, stream):
stream.seek(0) first_32 = stream.read(32) if (type(first_32) != bytes): _data = bytes(first_32, sys.getfilesystemencoding()) _data = first_32 (_cnt, _hdrLen, _recLen) = struct.unpack('<I2H', _data[4:12]) _year = _data[1] if (_year < 80): _year += 2000 else: _year ...
'Update `ignoreErrors` flag on self and all fields'
def ignoreErrors(self, value):
self._ignore_errors = value = bool(value) for _field in self.fields: _field.ignoreErrors = value
'Internal variant of the `addField` method. This method doesn\'t set `self.changed` field to True. Return value is a length of the appended records. Note: this method doesn\'t modify ``recordLength`` and ``headerLength`` fields. Use `addField` instead of this method if you don\'t exactly know what you\'re doing.'
def _addField(self, *defs):
_defs = [] _recordLength = 0 for _def in defs: if isinstance(_def, fields.DbfFieldDef): _obj = _def else: (_name, _type, _len, _dec) = (tuple(_def) + ((None,) * 4))[:4] _cls = fields.lookupFor(_type) _obj = _cls(_name, _len, _dec, ignoreErrors=...
'Add field definition to the header. Examples: dbfh.addField( ("name", "C", 20), dbf.DbfCharacterFieldDef("surname", 20), dbf.DbfDateFieldDef("birthdate"), ("member", "L"), dbfh.addField(("price", "N", 5, 2)) dbfh.addField(dbf.DbfNumericFieldDef("origprice", 5, 2))'
def addField(self, *defs):
_oldLen = self.recordLength self.recordLength += self._addField(*defs) if (not _oldLen): self.recordLength += 1 self.headerLength = ((32 + (32 * len(self.fields))) + 1) self.changed = True
'Encode and write header to the stream.'
def write(self, stream):
stream.seek(0) stream.write(self.toString()) fields = [_fld.toString() for _fld in self.fields] stream.write(''.join(fields).encode(sys.getfilesystemencoding())) stream.write('\r') self.changed = False
'Returned 32 chars length string with encoded header.'
def toString(self):
return (struct.pack('<4BI2H', self.signature, (self.year - 1900), self.month, self.day, self.recordCount, self.headerLength, self.recordLength) + ('\x00' * 20))
'Update ``self.lastUpdate`` field with current date value.'
def setCurrentDate(self):
self.lastUpdate = datetime.date.today()
'Return a field definition by numeric index or name string'
def __getitem__(self, item):
if isinstance(item, str): _name = item.upper() for _field in self.fields: if (_field.name == _name): return _field else: raise KeyError(item) else: return self.fields[item]
'Initialize instance. Arguments: f: Filename or file-like object. new: True if new data table must be created. Assume data table exists if this argument is False. readOnly: if ``f`` argument is a string file will be opend in read-only mode; in other cases this argument is ignored. This argument is ignored even if ``new...
def __init__(self, f, readOnly=False, new=False, ignoreErrors=False):
if isinstance(f, str): self.name = f if new: self.stream = open(f, 'w+b') else: self.stream = open(f, ('r+b', 'rb')[bool(readOnly)]) else: self.name = getattr(f, 'name', '') self.stream = f if new: self.header = self.HeaderClass() e...
'Update `ignoreErrors` flag on the header object and self'
def ignoreErrors(self, value):
self.header.ignoreErrors = self._ignore_errors = bool(value)
'Return fixed index. This method fails if index isn\'t a numeric object (long or int). Or index isn\'t in a valid range (less or equal to the number of records in the db). If ``index`` is a negative number, it will be treated as a negative indexes for list objects. Return: Return value is numeric object maning valid in...
def _fixIndex(self, index):
if (not isinstance(index, int)): raise TypeError('Index must be a numeric object') if (index < 0): index += (len(self) + 1) if (index >= len(self)): raise IndexError('Record index out of range') return index
'Flush data to the associated stream.'
def flush(self):
if self.changed: self.header.setCurrentDate() self.header.write(self.stream) self.stream.flush() self._changed = False
'Index of field named ``name``.'
def indexOfFieldName(self, name):
names = [f.name for f in self.header.fields] return names.index(name.upper())
'Return new record, which belong to this table.'
def newRecord(self):
return self.RecordClass(self)
'Append ``record`` to the database.'
def append(self, record):
record.index = self.header.recordCount record._write() self.header.recordCount += 1 self._changed = True self._new = False
'Add field definitions. For more information see `header.DbfHeader.addField`.'
def addField(self, *defs):
if self._new: self.header.addField(*defs) else: raise TypeError("At least one record was added, structure can't be changed")
'Return number of records.'
def __len__(self):
return self.recordCount
'Return `DbfRecord` instance.'
def __getitem__(self, index):
return self.RecordClass.fromStream(self, self._fixIndex(index))
'Write `DbfRecord` instance to the stream.'
def __setitem__(self, index, record):
record.index = self._fixIndex(index) record._write() self._changed = True self._new = False