Module nafparserpy.layers.tunits

Expand source code
from dataclasses import dataclass, field
from typing import List

from nafparserpy.layers.utils import AttributeGetter, create_node


@dataclass
class Tunit(AttributeGetter):
    """Represents a text unit"""
    id: str
    offset: str
    length: str
    attrs: dict = field(default_factory=dict)
    """optional attributes ('type', 'xpath')"""

    def __post_init__(self):
        """Copy compulsory attributes to `attrs` field"""
        self.attrs.update({'id': self.id, 'offset': self.offset, 'length': self.length})

    def node(self):
        """Create etree node from object"""
        return create_node('tunit', None, [], self.attrs)

    @staticmethod
    def object(node):
        """Create object from etree node"""
        return Tunit(node.get('id'), node.get('offset'), node.get('length'), node.attrib)


@dataclass
class Tunits:
    """Tunits layer class"""
    items: List[Tunit]
    """list of text units"""

    def node(self):
        """Create etree node from object"""
        return create_node('tunits', None, self.items, {})

    @staticmethod
    def object(node):
        """Create list of `Tunit` objects from etree node"""
        return [Tunit.object(n) for n in node]

Classes

class Tunit (id: str, offset: str, length: str, attrs: dict = <factory>)

Represents a text unit

Expand source code
@dataclass
class Tunit(AttributeGetter):
    """Represents a text unit"""
    id: str
    offset: str
    length: str
    attrs: dict = field(default_factory=dict)
    """optional attributes ('type', 'xpath')"""

    def __post_init__(self):
        """Copy compulsory attributes to `attrs` field"""
        self.attrs.update({'id': self.id, 'offset': self.offset, 'length': self.length})

    def node(self):
        """Create etree node from object"""
        return create_node('tunit', None, [], self.attrs)

    @staticmethod
    def object(node):
        """Create object from etree node"""
        return Tunit(node.get('id'), node.get('offset'), node.get('length'), node.attrib)

Ancestors

Class variables

var attrs : dict

optional attributes ('type', 'xpath')

var id : str
var length : str
var offset : str

Static methods

def object(node)

Create object from etree node

Expand source code
@staticmethod
def object(node):
    """Create object from etree node"""
    return Tunit(node.get('id'), node.get('offset'), node.get('length'), node.attrib)

Methods

def node(self)

Create etree node from object

Expand source code
def node(self):
    """Create etree node from object"""
    return create_node('tunit', None, [], self.attrs)

Inherited members

class Tunits (items: List[Tunit])

Tunits layer class

Expand source code
@dataclass
class Tunits:
    """Tunits layer class"""
    items: List[Tunit]
    """list of text units"""

    def node(self):
        """Create etree node from object"""
        return create_node('tunits', None, self.items, {})

    @staticmethod
    def object(node):
        """Create list of `Tunit` objects from etree node"""
        return [Tunit.object(n) for n in node]

Class variables

var items : List[Tunit]

list of text units

Static methods

def object(node)

Create list of Tunit objects from etree node

Expand source code
@staticmethod
def object(node):
    """Create list of `Tunit` objects from etree node"""
    return [Tunit.object(n) for n in node]

Methods

def node(self)

Create etree node from object

Expand source code
def node(self):
    """Create etree node from object"""
    return create_node('tunits', None, self.items, {})