Skip to content

Commit 70aadac

Browse files
🔵 Mv to resources/table
1 parent 39aa0f2 commit 70aadac

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
@@ -677,3 +676,53 @@ def write(
677676
self, target: Optional[Union[Resource, Any]] = None, **options: Any
678677
) -> TableResource:
679678
return self.write_table(target, **options)
679+
680+
681+
class _FieldInfo:
682+
"""Private class to store additional data alongside a field."""
683+
684+
def __init__(self, field: Field, field_number: int):
685+
"""field_number: 1-indexed rank of the field"""
686+
self.field = field
687+
self.field_number = field_number
688+
self.cell_reader = field.create_cell_reader()
689+
self.cell_writer = field.create_cell_writer()
690+
691+
692+
class FieldsInfo:
693+
"""Helper class to store additional data to a collection of fields
694+
695+
This class is not Public API, and should be used only in non-public
696+
interfaces.
697+
"""
698+
699+
def __init__(self, fields: List[Field]):
700+
self._fields: List[_FieldInfo] = [
701+
_FieldInfo(field, i + 1) for i, field in enumerate(fields)
702+
]
703+
704+
def ls(self) -> List[str]:
705+
"""List all field names"""
706+
return [fi.field.name for fi in self._fields]
707+
708+
def get(self, field_name: str) -> _FieldInfo:
709+
"""Get a Field by its name
710+
711+
Raises:
712+
ValueError: Field with name fieldname does not exist
713+
"""
714+
try:
715+
return next(fi for fi in self._fields if fi.field.name == field_name)
716+
except StopIteration:
717+
raise ValueError(f"'{field_name}' is not in fields data")
718+
719+
def get_copies(self) -> List[Field]:
720+
"""Return field copies"""
721+
return [fi.field.to_copy() for fi in self._fields]
722+
723+
def rm(self, field_name: str):
724+
try:
725+
i = self.ls().index(field_name)
726+
del self._fields[i]
727+
except ValueError:
728+
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)