Skip to content

Commit 996f64d

Browse files
🔵 Mv to resources/table
1 parent 51e8a8c commit 996f64d

File tree

3 files changed

+54
-47
lines changed

3 files changed

+54
-47
lines changed

frictionless/resources/table.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from ..indexer import Indexer
1515
from ..platform import platform
1616
from ..resource import Resource
17-
from ..schema.fields_info import FieldsInfo
1817
from ..system import system
1918
from ..table import Header, Lookup, Row, Table
2019
from ..transformer import Transformer
@@ -700,3 +699,53 @@ def write(
700699
self, target: Optional[Union[Resource, Any]] = None, **options: Any
701700
) -> TableResource:
702701
return self.write_table(target, **options)
702+
703+
704+
class _FieldInfo:
705+
"""Private class to store additional data alongside a field."""
706+
707+
def __init__(self, field: Field, field_number: int):
708+
"""field_number: 1-indexed rank of the field"""
709+
self.field = field
710+
self.field_number = field_number
711+
self.cell_reader = field.create_cell_reader()
712+
self.cell_writer = field.create_cell_writer()
713+
714+
715+
class FieldsInfo:
716+
"""Helper class to store additional data to a collection of fields
717+
718+
This class is not Public API, and should be used only in non-public
719+
interfaces.
720+
"""
721+
722+
def __init__(self, fields: List[Field]):
723+
self._fields: List[_FieldInfo] = [
724+
_FieldInfo(field, i + 1) for i, field in enumerate(fields)
725+
]
726+
727+
def ls(self) -> List[str]:
728+
"""List all field names"""
729+
return [fi.field.name for fi in self._fields]
730+
731+
def get(self, field_name: str) -> _FieldInfo:
732+
"""Get a Field by its name
733+
734+
Raises:
735+
ValueError: Field with name fieldname does not exist
736+
"""
737+
try:
738+
return next(fi for fi in self._fields if fi.field.name == field_name)
739+
except StopIteration:
740+
raise ValueError(f"'{field_name}' is not in fields data")
741+
742+
def get_copies(self) -> List[Field]:
743+
"""Return field copies"""
744+
return [fi.field.to_copy() for fi in self._fields]
745+
746+
def rm(self, field_name: str):
747+
try:
748+
i = self.ls().index(field_name)
749+
del self._fields[i]
750+
except ValueError:
751+
raise ValueError(f"'{field_name}' is not in fields data")

frictionless/schema/fields_info.py

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,3 @@
11
from typing import List
22

33
from frictionless.schema.field import Field
4-
5-
6-
class FieldInfo:
7-
"""Private class to store additional data to a field"""
8-
9-
def __init__(self, field: Field, field_number: int):
10-
"""field_number: 1-indexed rank of the field"""
11-
self.field = field
12-
self.field_number = field_number
13-
self.cell_reader = field.create_cell_reader()
14-
self.cell_writer = field.create_cell_writer()
15-
16-
17-
class FieldsInfo:
18-
def __init__(self, fields: List[Field]):
19-
self._fields: List[FieldInfo] = [
20-
FieldInfo(field, i + 1) for i, field in enumerate(fields)
21-
]
22-
23-
def ls(self) -> List[str]:
24-
"""List all field names"""
25-
return [fi.field.name for fi in self._fields]
26-
27-
def get(self, field_name: str) -> FieldInfo:
28-
"""Get a Field by its name
29-
30-
Raises:
31-
ValueError: Field with name fieldname does not exist
32-
"""
33-
try:
34-
return next(fi for fi in self._fields if fi.field.name == field_name)
35-
except StopIteration:
36-
raise ValueError(f"'{field_name}' is not in fields data")
37-
38-
def get_copies(self) -> List[Field]:
39-
"""Return field copies"""
40-
return [fi.field.to_copy() for fi in self._fields]
41-
42-
def rm(self, field_name: str):
43-
try:
44-
i = self.ls().index(field_name)
45-
del self._fields[i]
46-
except ValueError:
47-
raise ValueError(f"'{field_name}' is not in fields data")

frictionless/table/row.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22

33
from functools import cached_property
44
from itertools import zip_longest
5-
from typing import Any, Dict, List, Optional
5+
from typing import TYPE_CHECKING, Any, Dict, List, Optional
66

77
from .. import errors, helpers
88
from ..platform import platform
9-
from ..schema.fields_info import FieldsInfo
109

1110
# NOTE:
1211
# Currently dict.update/setdefault/pop/popitem/clear is not disabled (can be confusing)
1312
# We can consider adding row.header property to provide more comprehensive API
1413

14+
if TYPE_CHECKING:
15+
from ..resources.table import FieldsInfo
16+
1517

1618
# TODO: add types
1719
class Row(Dict[str, Any]):

0 commit comments

Comments
 (0)