1433
-1434
+ Python |
---|
| def all_items(self) -> Generator:
- r"""
- Equivalent to `keys`.
-
- This method is provided solely to make methods work on both `FlatDict` and `NestedDict`.
-
- See Also:
- [`all_items`][chanfig.NestedDict.all_items]
- """
- yield from self.items()
+1442
+1443
| def all_items(self) -> Generator:
+ r"""
+ Equivalent to `keys`.
+
+ This method is provided solely to make methods work on both `FlatDict` and `NestedDict`.
+
+ See Also:
+ [`all_items`][chanfig.NestedDict.all_items]
+ """
+ yield from self.items()
|
@@ -4400,8 +4402,7 @@
Source code in chanfig/flat_dict.py
- Python |
---|
1411
-1412
+ Python |
---|
| def all_keys(self) -> Generator:
- r"""
- Equivalent to `keys`.
-
- This method is provided solely to make methods work on both `FlatDict` and `NestedDict`.
-
- See Also:
- [`all_keys`][chanfig.NestedDict.all_keys]
- """
- yield from self.keys()
+1420
+1421
| def all_keys(self) -> Generator:
+ r"""
+ Equivalent to `keys`.
+
+ This method is provided solely to make methods work on both `FlatDict` and `NestedDict`.
+
+ See Also:
+ [`all_keys`][chanfig.NestedDict.all_keys]
+ """
+ yield from self.keys()
|
@@ -4446,8 +4448,7 @@
Source code in chanfig/flat_dict.py
- Python |
---|
1422
-1423
+ Python |
---|
| def all_values(self) -> Generator:
- r"""
- Equivalent to `keys`.
-
- This method is provided solely to make methods work on both `FlatDict` and `NestedDict`.
-
- See Also:
- [`all_values`][chanfig.NestedDict.all_values]
- """
- yield from self.values()
+1431
+1432
| def all_values(self) -> Generator:
+ r"""
+ Equivalent to `keys`.
+
+ This method is provided solely to make methods work on both `FlatDict` and `NestedDict`.
+
+ See Also:
+ [`all_values`][chanfig.NestedDict.all_values]
+ """
+ yield from self.values()
|
@@ -4486,15 +4488,15 @@
Source code in chanfig/flat_dict.py
- Python |
---|
1065
-1066
+ Python |
---|
| def clone(self, memo: Mapping | None = None) -> Self:
- r"""
- Alias of [`deepcopy`][chanfig.FlatDict.deepcopy].
- """
- return self.deepcopy(memo=memo)
+1069
+1070
| def clone(self, memo: Mapping | None = None) -> Self:
+ r"""
+ Alias of [`deepcopy`][chanfig.FlatDict.deepcopy].
+ """
+ return self.deepcopy(memo=memo)
|
@@ -4553,8 +4555,7 @@
Source code in chanfig/flat_dict.py
- Python |
---|
1001
-1002
+ Python |
---|
| def copy(self) -> Self:
- r"""
- Create a shallow copy of `FlatDict`.
-
- Returns:
- (FlatDict):
-
- Examples:
- >>> d = FlatDict(a=[])
- >>> d.setattr("name", "Chang")
- >>> c = d.copy()
- >>> c.dict()
- {'a': []}
- >>> d.a.append(1)
- >>> c.dict()
- {'a': [1]}
- >>> c.getattr("name")
- 'Chang'
- """
-
- return copy(self)
+1021
+1022
| def copy(self) -> Self:
+ r"""
+ Create a shallow copy of `FlatDict`.
+
+ Returns:
+ (FlatDict):
+
+ Examples:
+ >>> d = FlatDict(a=[])
+ >>> d.setattr("name", "Chang")
+ >>> c = d.copy()
+ >>> c.dict()
+ {'a': []}
+ >>> d.a.append(1)
+ >>> c.dict()
+ {'a': [1]}
+ >>> c.getattr("name")
+ 'Chang'
+ """
+
+ return copy(self)
|
@@ -4646,8 +4648,7 @@
Source code in chanfig/flat_dict.py
- Python |
---|
929
-930
+ Python |
---|
| def cpu(self) -> Self: # pragma: no cover
- r"""
- Move all tensors to cpu.
-
- Returns:
- self:
-
- Examples:
- >>> import torch
- >>> d = FlatDict(a=torch.tensor(1))
- >>> d.cpu().dict() # doctest: +SKIP
- {'a': tensor(1, device='cpu')}
- """
-
- return self.to(TorchDevice("cpu"))
+943
+944
| def cpu(self) -> Self: # pragma: no cover
+ r"""
+ Move all tensors to cpu.
+
+ Returns:
+ self:
+
+ Examples:
+ >>> import torch
+ >>> d = FlatDict(a=torch.tensor(1))
+ >>> d.cpu().dict() # doctest: +SKIP
+ {'a': tensor(1, device='cpu')}
+ """
+
+ return self.to(TorchDevice("cpu"))
|
@@ -4696,15 +4698,15 @@
Source code in chanfig/flat_dict.py
- Python |
---|
967
-968
+ Python |
---|
| def cuda(self) -> Self: # pragma: no cover
- r"""
- Alias of [`gpu`][chanfig.FlatDict.gpu].
- """
- return self.gpu()
+971
+972
| def cuda(self) -> Self: # pragma: no cover
+ r"""
+ Alias of [`gpu`][chanfig.FlatDict.gpu].
+ """
+ return self.gpu()
|
@@ -4769,8 +4771,7 @@
Source code in chanfig/flat_dict.py
- Python |
---|
1037
-1038
+ Python |
---|
| def deepcopy(self, memo: Mapping | None = None) -> Self: # pylint: disable=W0613
- r"""
- Create a deep copy of `FlatDict`.
-
- Returns:
- (FlatDict):
-
- **Alias**:
-
- + `clone`
-
- Examples:
- >>> d = FlatDict(a=[])
- >>> d.setattr("name", "Chang")
- >>> c = d.deepcopy()
- >>> c.dict()
- {'a': []}
- >>> d.a.append(1)
- >>> c.dict()
- {'a': []}
- >>> c.getattr("name")
- 'Chang'
- >>> d == d.clone() # alias
- True
- """
-
- return deepcopy(self)
+1063
+1064
| def deepcopy(self, memo: Mapping | None = None) -> Self: # pylint: disable=W0613
+ r"""
+ Create a deep copy of `FlatDict`.
+
+ Returns:
+ (FlatDict):
+
+ **Alias**:
+
+ + `clone`
+
+ Examples:
+ >>> d = FlatDict(a=[])
+ >>> d.setattr("name", "Chang")
+ >>> c = d.deepcopy()
+ >>> c.dict()
+ {'a': []}
+ >>> d.a.append(1)
+ >>> c.dict()
+ {'a': []}
+ >>> c.getattr("name")
+ 'Chang'
+ >>> d == d.clone() # alias
+ True
+ """
+
+ return deepcopy(self)
|
@@ -4885,8 +4887,7 @@
Source code in chanfig/flat_dict.py
- Python |
---|
444
-445
+ Python |
---|
| def delattr(self, name: str) -> None:
- r"""
- Delete attribute of `FlatDict`.
-
- Note that it won't delete values in `FlatDict`.
-
- Args:
- name:
-
- Examples:
- >>> d = FlatDict()
- >>> d.setattr('name', 'chang')
- >>> d.getattr('name')
- 'chang'
- >>> d.delattr('name')
- >>> d.getattr('name')
- Traceback (most recent call last):
- AttributeError: 'FlatDict' object has no attribute 'name'
- """
-
- del self.__dict__[name]
+464
+465
| def delattr(self, name: str) -> None:
+ r"""
+ Delete attribute of `FlatDict`.
+
+ Note that it won't delete values in `FlatDict`.
+
+ Args:
+ name:
+
+ Examples:
+ >>> d = FlatDict()
+ >>> d.setattr('name', 'chang')
+ >>> d.getattr('name')
+ 'chang'
+ >>> d.delattr('name')
+ >>> d.getattr('name')
+ Traceback (most recent call last):
+ AttributeError: 'FlatDict' object has no attribute 'name'
+ """
+
+ del self.__dict__[name]
|
@@ -5138,8 +5140,7 @@
Source code in chanfig/flat_dict.py
- Python |
---|
493
-494
+ Python |
---|
| def dict(self, flatten: bool = False) -> Mapping | Sequence | Set:
- r"""
- Convert `FlatDict` to other `Mapping`.
-
- Args:
- flatten: Whether to flatten [`NestedDict`][chanfig.NestedDict].
-
- Returns:
- (Mapping):
-
- See Also:
- [`to_dict`][chanfig.flat_dict.to_dict]: Implementation of `dict`.
-
- **Alias**:
-
- + `to_dict`
-
- Examples:
- >>> d = FlatDict(a=1, b=2, c=3)
- >>> d.dict()
- {'a': 1, 'b': 2, 'c': 3}
- """
-
- return to_dict(self, flatten)
+516
+517
| def dict(self, flatten: bool = False) -> Mapping | Sequence | Set:
+ r"""
+ Convert `FlatDict` to other `Mapping`.
+
+ Args:
+ flatten: Whether to flatten [`NestedDict`][chanfig.NestedDict].
+
+ Returns:
+ (Mapping):
+
+ See Also:
+ [`to_dict`][chanfig.flat_dict.to_dict]: Implementation of `dict`.
+
+ **Alias**:
+
+ + `to_dict`
+
+ Examples:
+ >>> d = FlatDict(a=1, b=2, c=3)
+ >>> d.dict()
+ {'a': 1, 'b': 2, 'c': 3}
+ """
+
+ return to_dict(self, flatten)
|
@@ -5206,15 +5208,15 @@
Source code in chanfig/flat_dict.py
- Python |
---|
896
-897
+ Python |
---|
| def diff(self, other: Mapping | Iterable | PathStr, *args: Any, **kwargs: Any) -> Self:
- r"""
- Alias of [`difference`][chanfig.FlatDict.difference].
- """
- return self.difference(other, *args, **kwargs)
+900
+901
| def diff(self, other: Mapping | Iterable | PathStr, *args: Any, **kwargs: Any) -> Self:
+ r"""
+ Alias of [`difference`][chanfig.FlatDict.difference].
+ """
+ return self.difference(other, *args, **kwargs)
|
@@ -5310,8 +5312,7 @@
Source code in chanfig/flat_dict.py
- Python |
---|
855
-856
+ Python |
---|
| def difference(self, other: Mapping | Iterable | PathStr) -> Self:
- r"""
- Difference between `FlatDict` and `other`.
-
- Args:
- other:
-
- Returns:
- (FlatDict):
-
- **Alias**:
-
- + `diff`
-
- Examples:
- >>> d = FlatDict(a=1, b=2, c=3)
- >>> n = {'b': 'b', 'c': 'c', 'd': 'd'}
- >>> d.difference(n).dict()
- {'b': 'b', 'c': 'c', 'd': 'd'}
- >>> l = [('c', 3), ('d', 4)]
- >>> d.difference(l).dict()
- {'d': 4}
- >>> d.merge(l).difference("tests/test.yaml").dict()
- {}
- >>> d.difference(1)
- Traceback (most recent call last):
- TypeError: `other=1` should be of type Mapping, Iterable or PathStr, but got <class 'int'>.
- >>> FlatDict(a=1, b=1, c=1).diff(FlatDict(b='b', c='c', d='d')).dict() # alias
- {'b': 'b', 'c': 'c', 'd': 'd'}
- """
-
- if isinstance(other, (PathLike, str, bytes)):
- other = self.load(other)
- if isinstance(other, (Mapping,)):
- other = self.empty(other).items()
- if not isinstance(other, Iterable):
- raise TypeError(f"`other={other}` should be of type Mapping, Iterable or PathStr, but got {type(other)}.")
- return self.empty(
- **{key: value for key, value in other if key not in self or self[key] != value} # type: ignore[misc]
- )
+894
+895
| def difference(self, other: Mapping | Iterable | PathStr) -> Self:
+ r"""
+ Difference between `FlatDict` and `other`.
+
+ Args:
+ other:
+
+ Returns:
+ (FlatDict):
+
+ **Alias**:
+
+ + `diff`
+
+ Examples:
+ >>> d = FlatDict(a=1, b=2, c=3)
+ >>> n = {'b': 'b', 'c': 'c', 'd': 'd'}
+ >>> d.difference(n).dict()
+ {'b': 'b', 'c': 'c', 'd': 'd'}
+ >>> l = [('c', 3), ('d', 4)]
+ >>> d.difference(l).dict()
+ {'d': 4}
+ >>> d.merge(l).difference("tests/test.yaml").dict()
+ {}
+ >>> d.difference(1)
+ Traceback (most recent call last):
+ TypeError: `other=1` should be of type Mapping, Iterable or PathStr, but got <class 'int'>.
+ >>> FlatDict(a=1, b=1, c=1).diff(FlatDict(b='b', c='c', d='d')).dict() # alias
+ {'b': 'b', 'c': 'c', 'd': 'd'}
+ """
+
+ if isinstance(other, (PathLike, str, bytes)):
+ other = self.load(other)
+ if isinstance(other, (Mapping,)):
+ other = self.empty(other).items()
+ if not isinstance(other, Iterable):
+ raise TypeError(f"`other={other}` should be of type Mapping, Iterable or PathStr, but got {type(other)}.")
+ return self.empty(
+ **{key: value for key, value in other if key not in self or self[key] != value} # type: ignore[misc]
+ )
|
@@ -5410,15 +5412,15 @@
Source code in chanfig/flat_dict.py
- Python |
---|
1467
-1468
+ Python |
---|
| def dropna(self) -> Self:
- r"""
- Alias of [`dropnull`][chanfig.FlatDict.dropnull].
- """
- return self.dropnull()
+1471
+1472
| def dropna(self) -> Self:
+ r"""
+ Alias of [`dropnull`][chanfig.FlatDict.dropnull].
+ """
+ return self.dropnull()
|
@@ -5478,8 +5480,7 @@
Source code in chanfig/flat_dict.py
- Python |
---|
1444
-1445
+ Python |
---|
| def dropnull(self) -> Self:
- r"""
- Drop key-value pairs with `Null` value.
-
- Returns:
- (FlatDict):
-
- **Alias**:
-
- + `dropna`
-
- Examples:
- >>> d = FlatDict(a=Null, b=Null, c=3)
- >>> d.dict()
- {'a': Null, 'b': Null, 'c': 3}
- >>> d.dropnull().dict()
- {'c': 3}
- >>> d.dropna().dict() # alias
- {'c': 3}
- """
-
- return self.empty({k: v for k, v in self.all_items() if v is not Null})
+1465
+1466
| def dropnull(self) -> Self:
+ r"""
+ Drop key-value pairs with `Null` value.
+
+ Returns:
+ (FlatDict):
+
+ **Alias**:
+
+ + `dropna`
+
+ Examples:
+ >>> d = FlatDict(a=Null, b=Null, c=3)
+ >>> d.dict()
+ {'a': Null, 'b': Null, 'c': 3}
+ >>> d.dropnull().dict()
+ {'c': 3}
+ >>> d.dropna().dict() # alias
+ {'c': 3}
+ """
+
+ return self.empty({k: v for k, v in self.all_items() if v is not Null})
|
@@ -5542,19 +5544,19 @@
Source code in chanfig/flat_dict.py
- Python |
---|
1108
-1109
+ Python |
---|
| def dump( # pylint: disable=W1113
- self, file: File, method: str = None, *args: Any, **kwargs: Any # type: ignore[assignment]
-) -> None:
- r"""
- Alias of [`save`][chanfig.FlatDict.save].
- """
- return self.save(file, method, *args, **kwargs)
+1114
+1115
| def dump( # pylint: disable=W1113
+ self, file: File, method: str = None, *args: Any, **kwargs: Any # type: ignore[assignment]
+) -> None:
+ r"""
+ Alias of [`save`][chanfig.FlatDict.save].
+ """
+ return self.save(file, method, *args, **kwargs)
|
@@ -5619,8 +5621,7 @@
Source code in chanfig/flat_dict.py
- Python |
---|
1355
-1356
+ Python |
---|
| @classmethod
-def empty(cls, *args: Any, **kwargs: Any) -> Self:
- r"""
- Initialise an empty `FlatDict`.
-
- This method is helpful when you inheriting `FlatDict` with default values defined in `__init__()`.
- As use `type(self)()` in this case would copy all the default values, which might not be desired.
-
- This method will preserve everything in `FlatDict.__class__.__dict__`.
-
- Returns:
- (FlatDict):
-
- See Also:
- [`empty_like`][chanfig.FlatDict.empty_like]
-
- Examples:
- >>> d = FlatDict(a=[])
- >>> c = d.empty()
- >>> c.dict()
- {}
- """
-
- empty = cls.__new__(cls)
- empty.merge(*args, **kwargs) # pylint: disable=W0212
- return empty
+1380
+1381
| @classmethod
+def empty(cls, *args: Any, **kwargs: Any) -> Self:
+ r"""
+ Initialise an empty `FlatDict`.
+
+ This method is helpful when you inheriting `FlatDict` with default values defined in `__init__()`.
+ As use `type(self)()` in this case would copy all the default values, which might not be desired.
+
+ This method will preserve everything in `FlatDict.__class__.__dict__`.
+
+ Returns:
+ (FlatDict):
+
+ See Also:
+ [`empty_like`][chanfig.FlatDict.empty_like]
+
+ Examples:
+ >>> d = FlatDict(a=[])
+ >>> c = d.empty()
+ >>> c.dict()
+ {}
+ """
+
+ empty = cls.__new__(cls)
+ empty.merge(*args, **kwargs) # pylint: disable=W0212
+ return empty
|
@@ -5733,8 +5735,7 @@
Source code in chanfig/flat_dict.py
- Python |
---|
1382
-1383
+ Python |
---|
| def empty_like(self, *args: Any, **kwargs: Any) -> Self:
- r"""
- Initialise an empty copy of `FlatDict`.
-
- This method will preserve everything in `FlatDict.__class__.__dict__` and `FlatDict.__dict__`.
-
- For example, `property`s are saved in `__dict__`, they will keep their original reference after calling this
- method.
-
- Returns:
- (FlatDict):
-
- See Also:
- [`empty`][chanfig.FlatDict.empty]
-
- Examples:
- >>> d = FlatDict(a=[])
- >>> d.setattr("name", "Chang")
- >>> c = d.empty_like()
- >>> c.dict()
- {}
- >>> c.getattr("name")
- 'Chang'
- """
-
- empty = self.empty(*args, **kwargs)
- empty.__dict__.update(self.__dict__)
- return empty
+1409
+1410
| def empty_like(self, *args: Any, **kwargs: Any) -> Self:
+ r"""
+ Initialise an empty copy of `FlatDict`.
+
+ This method will preserve everything in `FlatDict.__class__.__dict__` and `FlatDict.__dict__`.
+
+ For example, `property`s are saved in `__dict__`, they will keep their original reference after calling this
+ method.
+
+ Returns:
+ (FlatDict):
+
+ See Also:
+ [`empty`][chanfig.FlatDict.empty]
+
+ Examples:
+ >>> d = FlatDict(a=[])
+ >>> d.setattr("name", "Chang")
+ >>> c = d.empty_like()
+ >>> c.dict()
+ {}
+ >>> c.getattr("name")
+ 'Chang'
+ """
+
+ empty = self.empty(*args, **kwargs)
+ empty.__dict__.update(self.__dict__)
+ return empty
|
@@ -5834,8 +5836,7 @@
Source code in chanfig/flat_dict.py
- Python |
---|
525
-526
+ Python |
---|
| @classmethod
-def from_dict(cls, obj: Mapping | Sequence) -> Any: # pylint: disable=R0911
- r"""
- Convert `Mapping` or `Sequence` to `FlatDict`.
-
- Examples:
- >>> FlatDict.from_dict({'a': 1, 'b': 2, 'c': 3})
- FlatDict(
- ('a'): 1
- ('b'): 2
- ('c'): 3
- )
- >>> FlatDict.from_dict([('a', 1), ('b', 2), ('c', 3)])
- FlatDict(
- ('a'): 1
- ('b'): 2
- ('c'): 3
- )
- >>> FlatDict.from_dict([{'a': 1}, {'b': 2}, {'c': 3}])
- [FlatDict(('a'): 1), FlatDict(('b'): 2), FlatDict(('c'): 3)]
- >>> FlatDict.from_dict({1, 2, 3})
- Traceback (most recent call last):
- TypeError: Expected Mapping or Sequence, but got <class 'set'>.
- """
-
- if obj is None:
- return cls()
- if issubclass(cls, FlatDict):
- cls = cls.empty # type: ignore[assignment] # pylint: disable=W0642
- if isinstance(obj, Mapping):
- return cls(obj)
- if isinstance(obj, Sequence):
- try:
- return cls(obj)
- except ValueError:
- return [cls(json) for json in obj]
- raise TypeError(f"Expected Mapping or Sequence, but got {type(obj)}.")
+561
+562
| @classmethod
+def from_dict(cls, obj: Mapping | Sequence) -> Any: # pylint: disable=R0911
+ r"""
+ Convert `Mapping` or `Sequence` to `FlatDict`.
+
+ Examples:
+ >>> FlatDict.from_dict({'a': 1, 'b': 2, 'c': 3})
+ FlatDict(
+ ('a'): 1
+ ('b'): 2
+ ('c'): 3
+ )
+ >>> FlatDict.from_dict([('a', 1), ('b', 2), ('c', 3)])
+ FlatDict(
+ ('a'): 1
+ ('b'): 2
+ ('c'): 3
+ )
+ >>> FlatDict.from_dict([{'a': 1}, {'b': 2}, {'c': 3}])
+ [FlatDict(('a'): 1), FlatDict(('b'): 2), FlatDict(('c'): 3)]
+ >>> FlatDict.from_dict({1, 2, 3})
+ Traceback (most recent call last):
+ TypeError: Expected Mapping or Sequence, but got <class 'set'>.
+ """
+
+ if obj is None:
+ return cls()
+ if issubclass(cls, FlatDict):
+ cls = cls.empty # type: ignore[assignment] # pylint: disable=W0642
+ if isinstance(obj, Mapping):
+ return cls(obj)
+ if isinstance(obj, Sequence):
+ try:
+ return cls(obj)
+ except ValueError:
+ return [cls(json) for json in obj]
+ raise TypeError(f"Expected Mapping or Sequence, but got {type(obj)}.")
|
@@ -5964,8 +5966,7 @@
Source code in chanfig/flat_dict.py
- Python |
---|
1173
-1174
+ Python |
---|
| @classmethod
-def from_json(cls, file: File, *args: Any, **kwargs: Any) -> Self:
- r"""
- Construct `FlatDict` from json file.
-
- This method internally calls `self.from_jsons()` to construct object from json string.
- You may overwrite `from_jsons` in case something is not json serializable.
-
- Returns:
- (FlatDict):
-
- Examples:
- >>> d = FlatDict.from_json('tests/test.json')
- >>> d.dict()
- {'a': 1, 'b': 2, 'c': 3}
- """
-
- with cls.open(file) as fp: # pylint: disable=C0103
- if isinstance(file, (IOBase, IO)):
- return cls.from_jsons(fp.getvalue(), *args, **kwargs) # type: ignore[union-attr]
- return cls.from_jsons(fp.read(), *args, **kwargs)
+1193
+1194
| @classmethod
+def from_json(cls, file: File, *args: Any, **kwargs: Any) -> Self:
+ r"""
+ Construct `FlatDict` from json file.
+
+ This method internally calls `self.from_jsons()` to construct object from json string.
+ You may overwrite `from_jsons` in case something is not json serializable.
+
+ Returns:
+ (FlatDict):
+
+ Examples:
+ >>> d = FlatDict.from_json('tests/test.json')
+ >>> d.dict()
+ {'a': 1, 'b': 2, 'c': 3}
+ """
+
+ with cls.open(file) as fp: # pylint: disable=C0103
+ if isinstance(file, (IOBase, IO)):
+ return cls.from_jsons(fp.getvalue(), *args, **kwargs) # type: ignore[union-attr]
+ return cls.from_jsons(fp.read(), *args, **kwargs)
|
@@ -6063,8 +6065,7 @@
Source code in chanfig/flat_dict.py
- Python |
---|
1212
-1213
+ Python |
---|
| @classmethod
-def from_jsons(cls, string: str, *args: Any, **kwargs: Any) -> Self:
- r"""
- Construct `FlatDict` from json string.
-
- Returns:
- (FlatDict):
-
- Examples:
- >>> FlatDict.from_jsons('{\n "a": 1,\n "b": 2,\n "c": 3\n}').dict()
- {'a': 1, 'b': 2, 'c': 3}
- >>> FlatDict.from_jsons('[["a", 1], ["b", 2], ["c", 3]]').dict()
- {'a': 1, 'b': 2, 'c': 3}
- >>> FlatDict.from_jsons('[{"a": 1}, {"b": 2}, {"c": 3}]')
- [FlatDict(('a'): 1), FlatDict(('b'): 2), FlatDict(('c'): 3)]
- """
-
- return cls.from_dict(json_loads(string, *args, **kwargs))
+1229
+1230
| @classmethod
+def from_jsons(cls, string: str, *args: Any, **kwargs: Any) -> Self:
+ r"""
+ Construct `FlatDict` from json string.
+
+ Returns:
+ (FlatDict):
+
+ Examples:
+ >>> FlatDict.from_jsons('{\n "a": 1,\n "b": 2,\n "c": 3\n}').dict()
+ {'a': 1, 'b': 2, 'c': 3}
+ >>> FlatDict.from_jsons('[["a", 1], ["b", 2], ["c", 3]]').dict()
+ {'a': 1, 'b': 2, 'c': 3}
+ >>> FlatDict.from_jsons('[{"a": 1}, {"b": 2}, {"c": 3}]')
+ [FlatDict(('a'): 1), FlatDict(('b'): 2), FlatDict(('c'): 3)]
+ """
+
+ return cls.from_dict(json_loads(string, *args, **kwargs))
|
@@ -6154,8 +6156,7 @@
Source code in chanfig/flat_dict.py
- Python |
---|
1246
-1247
+ Python |
---|
| @classmethod
-def from_yaml(cls, file: File, *args: Any, **kwargs: Any) -> Self:
- r"""
- Construct `FlatDict` from yaml file.
-
- This method internally calls `self.from_yamls()` to construct object from yaml string.
- You may overwrite `from_yamls` in case something is not yaml serializable.
-
- Returns:
- (FlatDict):
-
- Examples:
- >>> FlatDict.from_yaml('tests/test.yaml').dict()
- {'a': 1, 'b': 2, 'c': 3}
- """
-
- kwargs.setdefault("Loader", YamlLoader)
- with cls.open(file) as fp: # pylint: disable=C0103
- if isinstance(file, (IOBase, IO)):
- return cls.from_yamls(fp.getvalue(), *args, **kwargs) # type: ignore[union-attr]
- return cls.from_dict(yaml_load(fp, *args, **kwargs))
+1266
+1267
| @classmethod
+def from_yaml(cls, file: File, *args: Any, **kwargs: Any) -> Self:
+ r"""
+ Construct `FlatDict` from yaml file.
+
+ This method internally calls `self.from_yamls()` to construct object from yaml string.
+ You may overwrite `from_yamls` in case something is not yaml serializable.
+
+ Returns:
+ (FlatDict):
+
+ Examples:
+ >>> FlatDict.from_yaml('tests/test.yaml').dict()
+ {'a': 1, 'b': 2, 'c': 3}
+ """
+
+ kwargs.setdefault("Loader", YamlLoader)
+ with cls.open(file) as fp: # pylint: disable=C0103
+ if isinstance(file, (IOBase, IO)):
+ return cls.from_yamls(fp.getvalue(), *args, **kwargs) # type: ignore[union-attr]
+ return cls.from_dict(yaml_load(fp, *args, **kwargs))
|
@@ -6253,8 +6255,7 @@
Source code in chanfig/flat_dict.py
- Python |
---|
1284
-1285
+ Python |
---|
| @classmethod
-def from_yamls(cls, string: str, *args: Any, **kwargs: Any) -> Self:
- r"""
- Construct `FlatDict` from yaml string.
-
- Returns:
- (FlatDict):
-
- Examples:
- >>> FlatDict.from_yamls('a: 1\nb: 2\nc: 3\n').dict()
- {'a': 1, 'b': 2, 'c': 3}
- >>> FlatDict.from_yamls('- - a\n - 1\n- - b\n - 2\n- - c\n - 3\n').dict()
- {'a': 1, 'b': 2, 'c': 3}
- >>> FlatDict.from_yamls('- a: 1\n- b: 2\n- c: 3\n')
- [FlatDict(('a'): 1), FlatDict(('b'): 2), FlatDict(('c'): 3)]
- """
-
- kwargs.setdefault("Loader", SafeLoader)
- return cls.from_dict(yaml_load(string, *args, **kwargs))
+1302
+1303
| @classmethod
+def from_yamls(cls, string: str, *args: Any, **kwargs: Any) -> Self:
+ r"""
+ Construct `FlatDict` from yaml string.
+
+ Returns:
+ (FlatDict):
+
+ Examples:
+ >>> FlatDict.from_yamls('a: 1\nb: 2\nc: 3\n').dict()
+ {'a': 1, 'b': 2, 'c': 3}
+ >>> FlatDict.from_yamls('- - a\n - 1\n- - b\n - 2\n- - c\n - 3\n').dict()
+ {'a': 1, 'b': 2, 'c': 3}
+ >>> FlatDict.from_yamls('- a: 1\n- b: 2\n- c: 3\n')
+ [FlatDict(('a'): 1), FlatDict(('b'): 2), FlatDict(('c'): 3)]
+ """
+
+ kwargs.setdefault("Loader", SafeLoader)
+ return cls.from_dict(yaml_load(string, *args, **kwargs))
|
@@ -6671,7 +6673,8 @@ | def getattr(self, name: str, default: Any = Null) -> Any:
+406
+407
| def getattr(self, name: str, default: Any = Null) -> Any:
r"""
Get attribute of `FlatDict`.
@@ -6704,13 +6707,14 @@
try:
if name in self.__dict__:
return self.__dict__[name]
- if name in self.__class__.__dict__:
- return self.__class__.__dict__[name]
- return super().getattr(name, default) # type: ignore[misc]
- except AttributeError:
- if default is not Null:
- return default
- raise AttributeError(f"'{self.__class__.__name__}' object has no attribute '{name}'") from None
+ for cls in self.__class__.__mro__:
+ if name in cls.__dict__:
+ return cls.__dict__[name]
+ return super().getattr(name, default) # type: ignore[misc]
+ except AttributeError:
+ if default is not Null:
+ return default
+ raise AttributeError(f"'{self.__class__.__name__}' object has no attribute '{name}'") from None
|
@@ -6769,8 +6773,7 @@
Source code in chanfig/flat_dict.py
- Python |
---|
945
-946
+ Python |
---|
| def gpu(self) -> Self: # pragma: no cover
- r"""
- Move all tensors to gpu.
-
- Returns:
- self:
-
- **Alias**:
-
- + `cuda`
-
- Examples:
- >>> import torch
- >>> d = FlatDict(a=torch.tensor(1))
- >>> d.gpu().dict() # doctest: +SKIP
- {'a': tensor(1, device='cuda:0')}
- >>> d.cuda().dict() # alias # doctest: +SKIP
- {'a': tensor(1, device='cuda:0')}
- """
-
- return self.to(TorchDevice("cuda"))
+965
+966
| def gpu(self) -> Self: # pragma: no cover
+ r"""
+ Move all tensors to gpu.
+
+ Returns:
+ self:
+
+ **Alias**:
+
+ + `cuda`
+
+ Examples:
+ >>> import torch
+ >>> d = FlatDict(a=torch.tensor(1))
+ >>> d.gpu().dict() # doctest: +SKIP
+ {'a': tensor(1, device='cuda:0')}
+ >>> d.cuda().dict() # alias # doctest: +SKIP
+ {'a': tensor(1, device='cuda:0')}
+ """
+
+ return self.to(TorchDevice("cuda"))
|
@@ -6894,8 +6898,7 @@
Source code in chanfig/flat_dict.py
- Python |
---|
466
-467
+ Python |
---|
| def hasattr(self, name: str) -> bool:
- r"""
- Determine if an attribute exists in `FlatDict`.
-
- Args:
- name:
-
- Returns:
- (bool):
-
- Examples:
- >>> d = FlatDict()
- >>> d.setattr('name', 'chang')
- >>> d.hasattr('name')
- True
- >>> d.delattr('name')
- >>> d.hasattr('name')
- False
- """
-
- try:
- if name in self.__dict__ or name in self.__class__.__dict__:
- return True
- return super().hasattr(name) # type: ignore[misc]
- except AttributeError:
- return False
+491
+492
| def hasattr(self, name: str) -> bool:
+ r"""
+ Determine if an attribute exists in `FlatDict`.
+
+ Args:
+ name:
+
+ Returns:
+ (bool):
+
+ Examples:
+ >>> d = FlatDict()
+ >>> d.setattr('name', 'chang')
+ >>> d.hasattr('name')
+ True
+ >>> d.delattr('name')
+ >>> d.hasattr('name')
+ False
+ """
+
+ try:
+ if name in self.__dict__ or name in self.__class__.__dict__:
+ return True
+ return super().hasattr(name) # type: ignore[misc]
+ except AttributeError:
+ return False
|
@@ -6966,15 +6970,15 @@
Source code in chanfig/flat_dict.py
- Python |
---|
849
-850
+ Python |
---|
| def inter(self, other: Mapping | Iterable | PathStr, *args: Any, **kwargs: Any) -> Self:
- r"""
- Alias of [`intersect`][chanfig.FlatDict.intersect].
- """
- return self.intersect(other, *args, **kwargs)
+853
+854
| def inter(self, other: Mapping | Iterable | PathStr, *args: Any, **kwargs: Any) -> Self:
+ r"""
+ Alias of [`intersect`][chanfig.FlatDict.intersect].
+ """
+ return self.intersect(other, *args, **kwargs)
|
@@ -7151,8 +7155,7 @@
Source code in chanfig/flat_dict.py
- Python |
---|
590
-591
+ Python |
---|
| def interpolate( # pylint: disable=R0912
- self, use_variable: bool = True, interpolators: MutableMapping | None = None, unsafe_eval: bool = False
-) -> Self:
- r"""
- Perform Variable interpolation.
-
- Variable interpolation allows you to set the value of one key to be the value of another key easily.
-
- Args:
- use_variable: Whether to convert values to `Variable` objects.
- interpolators: Mapping contains values for interpolation. Defaults to `self`.
- unsafe_eval: Whether to evaluate interpolated values.
-
- Raises:
- ValueError: If value is not interpolatable.
- ValueError: If reference to itself.
- ValueError: If has circular reference.
-
- See Also:
- [Variable][`chanfig.Variable`]: Mutable wrapper of immutable objects.
-
- Examples:
- >>> d = FlatDict(a=1, b="${a}", c="${a}.${b}")
- >>> d.dict()
- {'a': 1, 'b': '${a}', 'c': '${a}.${b}'}
- >>> d.interpolate(unsafe_eval=True).dict()
- {'a': 1, 'b': 1, 'c': 1.1}
- >>> d = FlatDict(a=1, b="${a}", c="${a}.${b}")
- >>> d.dict()
- {'a': 1, 'b': '${a}', 'c': '${a}.${b}'}
- >>> d.interpolate().dict()
- {'a': 1, 'b': 1, 'c': '1.1'}
- >>> isinstance(d.a, Variable)
- True
- >>> d.a += 1
- >>> d.dict()
- {'a': 2, 'b': 2, 'c': '1.1'}
- >>> d.a is d.b
- True
- >>> d.b is d.c
- False
- >>> d = FlatDict(a=1, b="${a}", c="${b}")
- >>> d.dict()
- {'a': 1, 'b': '${a}', 'c': '${b}'}
- >>> d.interpolate(False).dict()
- {'a': 1, 'b': 1, 'c': 1}
- >>> isinstance(d.a, Variable)
- False
- >>> d.a += 1
- >>> d.dict()
- {'a': 2, 'b': 1, 'c': 1}
- >>> d = FlatDict(a=1, b="${b}", c="${b}")
- >>> d.interpolate().dict()
- Traceback (most recent call last):
- ValueError: Cannot interpolate b to itself.
- >>> d = FlatDict(a="${b}", b="${c}", c="${d}", d="${a}")
- >>> d.interpolate().dict()
- Traceback (most recent call last):
- ValueError: Circular reference found: a->b->c->d->a.
- >>> d = FlatDict(a=1, b="${a}", c="${d}")
- >>> d.interpolate().dict()
- Traceback (most recent call last):
- ValueError: d is not found in FlatDict(
- ('a'): '1'
- ('b'): '${a}'
- ('c'): '${d}'
- ).
- """
- # pylint: disable=C0103
-
- interpolators = interpolators or self
- placeholders: dict[str, list[str]] = {}
- for key, value in self.all_items():
- if isinstance(value, list):
- for v in value:
- self.find_placeholders(key, v, placeholders)
- elif isinstance(value, Mapping):
- for v in value.values():
- self.find_placeholders(key, v, placeholders)
- else:
- self.find_placeholders(key, value, placeholders)
- circular_references = find_circular_reference(placeholders)
- if circular_references:
- raise ValueError(f"Circular reference found: {'->'.join(circular_references)}.")
- if use_variable:
- placeholder_names = {i for j in placeholders.values() for i in j}
- for name in list(placeholder_names.difference(placeholders.keys())):
- if name not in interpolators:
- raise ValueError(f"{name} is not found in {interpolators}.")
- if not isinstance(interpolators[name], Variable):
- interpolators[name] = Variable(interpolators[name])
- for key, value in placeholders.items():
- if isinstance(self[key], list):
- for index, v in enumerate(self[key]):
- self[key][index] = self.substitute(v, interpolators, value)
- elif isinstance(self[key], Mapping):
- for k, v in self[key].items():
- self[key][k] = self.substitute(v, interpolators, value)
- else:
- self[key] = self.substitute(self[key], interpolators, value)
- if unsafe_eval and isinstance(self[key], str):
- with suppress(SyntaxError):
- self[key] = eval(self[key]) # pylint: disable=W0123
- return self
+693
+694
| def interpolate( # pylint: disable=R0912
+ self, use_variable: bool = True, interpolators: MutableMapping | None = None, unsafe_eval: bool = False
+) -> Self:
+ r"""
+ Perform Variable interpolation.
+
+ Variable interpolation allows you to set the value of one key to be the value of another key easily.
+
+ Args:
+ use_variable: Whether to convert values to `Variable` objects.
+ interpolators: Mapping contains values for interpolation. Defaults to `self`.
+ unsafe_eval: Whether to evaluate interpolated values.
+
+ Raises:
+ ValueError: If value is not interpolatable.
+ ValueError: If reference to itself.
+ ValueError: If has circular reference.
+
+ See Also:
+ [Variable][`chanfig.Variable`]: Mutable wrapper of immutable objects.
+
+ Examples:
+ >>> d = FlatDict(a=1, b="${a}", c="${a}.${b}")
+ >>> d.dict()
+ {'a': 1, 'b': '${a}', 'c': '${a}.${b}'}
+ >>> d.interpolate(unsafe_eval=True).dict()
+ {'a': 1, 'b': 1, 'c': 1.1}
+ >>> d = FlatDict(a=1, b="${a}", c="${a}.${b}")
+ >>> d.dict()
+ {'a': 1, 'b': '${a}', 'c': '${a}.${b}'}
+ >>> d.interpolate().dict()
+ {'a': 1, 'b': 1, 'c': '1.1'}
+ >>> isinstance(d.a, Variable)
+ True
+ >>> d.a += 1
+ >>> d.dict()
+ {'a': 2, 'b': 2, 'c': '1.1'}
+ >>> d.a is d.b
+ True
+ >>> d.b is d.c
+ False
+ >>> d = FlatDict(a=1, b="${a}", c="${b}")
+ >>> d.dict()
+ {'a': 1, 'b': '${a}', 'c': '${b}'}
+ >>> d.interpolate(False).dict()
+ {'a': 1, 'b': 1, 'c': 1}
+ >>> isinstance(d.a, Variable)
+ False
+ >>> d.a += 1
+ >>> d.dict()
+ {'a': 2, 'b': 1, 'c': 1}
+ >>> d = FlatDict(a=1, b="${b}", c="${b}")
+ >>> d.interpolate().dict()
+ Traceback (most recent call last):
+ ValueError: Cannot interpolate b to itself.
+ >>> d = FlatDict(a="${b}", b="${c}", c="${d}", d="${a}")
+ >>> d.interpolate().dict()
+ Traceback (most recent call last):
+ ValueError: Circular reference found: a->b->c->d->a.
+ >>> d = FlatDict(a=1, b="${a}", c="${d}")
+ >>> d.interpolate().dict()
+ Traceback (most recent call last):
+ ValueError: d is not found in FlatDict(
+ ('a'): '1'
+ ('b'): '${a}'
+ ('c'): '${d}'
+ ).
+ """
+ # pylint: disable=C0103
+
+ interpolators = interpolators or self
+ placeholders: dict[str, list[str]] = {}
+ for key, value in self.all_items():
+ if isinstance(value, list):
+ for v in value:
+ self.find_placeholders(key, v, placeholders)
+ elif isinstance(value, Mapping):
+ for v in value.values():
+ self.find_placeholders(key, v, placeholders)
+ else:
+ self.find_placeholders(key, value, placeholders)
+ circular_references = find_circular_reference(placeholders)
+ if circular_references:
+ raise ValueError(f"Circular reference found: {'->'.join(circular_references)}.")
+ if use_variable:
+ placeholder_names = {i for j in placeholders.values() for i in j}
+ for name in list(placeholder_names.difference(placeholders.keys())):
+ if name not in interpolators:
+ raise ValueError(f"{name} is not found in {interpolators}.")
+ if not isinstance(interpolators[name], Variable):
+ interpolators[name] = Variable(interpolators[name])
+ for key, value in placeholders.items():
+ if isinstance(self[key], list):
+ for index, v in enumerate(self[key]):
+ self[key][index] = self.substitute(v, interpolators, value)
+ elif isinstance(self[key], Mapping):
+ for k, v in self[key].items():
+ self[key][k] = self.substitute(v, interpolators, value)
+ else:
+ self[key] = self.substitute(self[key], interpolators, value)
+ if unsafe_eval and isinstance(self[key], str):
+ with suppress(SyntaxError):
+ self[key] = eval(self[key]) # pylint: disable=W0123
+ return self
|
@@ -7453,8 +7457,7 @@
Source code in chanfig/flat_dict.py
- Python |
---|
810
-811
+ Python |
---|
| def intersect(self, other: Mapping | Iterable | PathStr) -> Self:
- r"""
- Intersection of `FlatDict` and `other`.
-
- Args:
- other (Mapping | Iterable | PathStr):
-
- Returns:
- (FlatDict):
-
- **Alias**:
-
- + `inter`
-
- Examples:
- >>> d = FlatDict(a=1, b=2, c=3)
- >>> n = {'b': 'b', 'c': 'c', 'd': 'd'}
- >>> d.intersect(n).dict()
- {}
- >>> l = [('c', 3), ('d', 4)]
- >>> d.intersect(l).dict()
- {'c': 3}
- >>> d.merge(l).intersect("tests/test.yaml").dict()
- {'a': 1, 'b': 2, 'c': 3}
- >>> d.intersect(1)
- Traceback (most recent call last):
- TypeError: `other=1` should be of type Mapping, Iterable or PathStr, but got <class 'int'>.
- >>> d.inter(FlatDict(b='b', c='c', d='d')).dict() # alias
- {}
- """
-
- if isinstance(other, (PathLike, str, bytes)):
- other = self.load(other)
- if isinstance(other, (Mapping,)):
- other = self.empty(other).items()
- if not isinstance(other, Iterable):
- raise TypeError(f"`other={other}` should be of type Mapping, Iterable or PathStr, but got {type(other)}.")
- return self.empty(**{key: value for key, value in other if key in self and self[key] == value}) # type: ignore
+847
+848
| def intersect(self, other: Mapping | Iterable | PathStr) -> Self:
+ r"""
+ Intersection of `FlatDict` and `other`.
+
+ Args:
+ other (Mapping | Iterable | PathStr):
+
+ Returns:
+ (FlatDict):
+
+ **Alias**:
+
+ + `inter`
+
+ Examples:
+ >>> d = FlatDict(a=1, b=2, c=3)
+ >>> n = {'b': 'b', 'c': 'c', 'd': 'd'}
+ >>> d.intersect(n).dict()
+ {}
+ >>> l = [('c', 3), ('d', 4)]
+ >>> d.intersect(l).dict()
+ {'c': 3}
+ >>> d.merge(l).intersect("tests/test.yaml").dict()
+ {'a': 1, 'b': 2, 'c': 3}
+ >>> d.intersect(1)
+ Traceback (most recent call last):
+ TypeError: `other=1` should be of type Mapping, Iterable or PathStr, but got <class 'int'>.
+ >>> d.inter(FlatDict(b='b', c='c', d='d')).dict() # alias
+ {}
+ """
+
+ if isinstance(other, (PathLike, str, bytes)):
+ other = self.load(other)
+ if isinstance(other, (Mapping,)):
+ other = self.empty(other).items()
+ if not isinstance(other, Iterable):
+ raise TypeError(f"`other={other}` should be of type Mapping, Iterable or PathStr, but got {type(other)}.")
+ return self.empty(**{key: value for key, value in other if key in self and self[key] == value}) # type: ignore
|
@@ -7557,8 +7561,7 @@
Source code in chanfig/flat_dict.py
- Python |
---|
1158
-1159
+ Python |
---|
| def json(self, file: File, *args: Any, **kwargs: Any) -> None:
- r"""
- Dump `FlatDict` to json file.
-
- This method internally calls `self.jsons()` to generate json string.
- You may overwrite `jsons` in case something is not json serializable.
-
- Examples:
- >>> d = FlatDict(a=1, b=2, c=3)
- >>> d.json("tests/test.json")
- """
-
- with self.open(file, mode="w") as fp: # pylint: disable=C0103
- fp.write(self.jsons(*args, **kwargs))
+1171
+1172
| def json(self, file: File, *args: Any, **kwargs: Any) -> None:
+ r"""
+ Dump `FlatDict` to json file.
+
+ This method internally calls `self.jsons()` to generate json string.
+ You may overwrite `jsons` in case something is not json serializable.
+
+ Examples:
+ >>> d = FlatDict(a=1, b=2, c=3)
+ >>> d.json("tests/test.json")
+ """
+
+ with self.open(file, mode="w") as fp: # pylint: disable=C0103
+ fp.write(self.jsons(*args, **kwargs))
|
@@ -7635,8 +7639,7 @@
Source code in chanfig/flat_dict.py
- Python |
---|
1195
-1196
+ Python |
---|
| def jsons(self, *args: Any, **kwargs: Any) -> str:
- r"""
- Dump `FlatDict` to json string.
-
- Returns:
- (str):
-
- Examples:
- >>> d = FlatDict(a=1, b=2, c=3)
- >>> d.jsons()
- '{\n "a": 1,\n "b": 2,\n "c": 3\n}'
- """
-
- kwargs.setdefault("cls", JsonEncoder)
- kwargs.setdefault("indent", self.getattr("indent", 2))
- return json_dumps(self.dict(), *args, **kwargs)
+1210
+1211
| def jsons(self, *args: Any, **kwargs: Any) -> str:
+ r"""
+ Dump `FlatDict` to json string.
+
+ Returns:
+ (str):
+
+ Examples:
+ >>> d = FlatDict(a=1, b=2, c=3)
+ >>> d.jsons()
+ '{\n "a": 1,\n "b": 2,\n "c": 3\n}'
+ """
+
+ kwargs.setdefault("cls", JsonEncoder)
+ kwargs.setdefault("indent", self.getattr("indent", 2))
+ return json_dumps(self.dict(), *args, **kwargs)
|
@@ -7804,8 +7808,7 @@
Source code in chanfig/flat_dict.py
- Python |
---|
1116
-1117
+ Python |
---|
| @classmethod
-def load( # pylint: disable=W1113
- cls, file: File, method: str = None, *args: Any, **kwargs: Any # type: ignore[assignment]
-) -> Self:
- """
- Load `FlatDict` from file.
-
- Args:
- file: File to load from.
- method: File type, should be in `JSON` or `YAML`.
-
- Returns:
- (FlatDict):
-
- Raises:
- ValueError: If load from `IO` and `method` is not specified.
- TypeError: If dump to unsupported extension.
-
- Examples:
- >>> d = FlatDict.load("tests/test.yaml")
- >>> d.dict()
- {'a': 1, 'b': 2, 'c': 3}
- >>> d.load("tests/test.conf")
- Traceback (most recent call last):
- TypeError: `file='tests/test.conf'` should be in ('json',) or ('yml', 'yaml'), but got conf.
- >>> with open("tests/test.yaml") as f:
- ... d.load(f)
- Traceback (most recent call last):
- ValueError: `method` must be specified when loading from IO.
- """
-
- if method is None:
- if isinstance(file, (IOBase, IO)):
- raise ValueError("`method` must be specified when loading from IO.")
- method = splitext(file)[-1][1:]
- extension = method.lower()
- if extension in JSON:
- return cls.from_json(file, *args, **kwargs)
- if extension in YAML:
- return cls.from_yaml(file, *args, **kwargs)
- raise TypeError(f"`file={file!r}` should be in {JSON} or {YAML}, but got {extension}.")
+1156
+1157
| @classmethod
+def load( # pylint: disable=W1113
+ cls, file: File, method: str = None, *args: Any, **kwargs: Any # type: ignore[assignment]
+) -> Self:
+ """
+ Load `FlatDict` from file.
+
+ Args:
+ file: File to load from.
+ method: File type, should be in `JSON` or `YAML`.
+
+ Returns:
+ (FlatDict):
+
+ Raises:
+ ValueError: If load from `IO` and `method` is not specified.
+ TypeError: If dump to unsupported extension.
+
+ Examples:
+ >>> d = FlatDict.load("tests/test.yaml")
+ >>> d.dict()
+ {'a': 1, 'b': 2, 'c': 3}
+ >>> d.load("tests/test.conf")
+ Traceback (most recent call last):
+ TypeError: `file='tests/test.conf'` should be in ('json',) or ('yml', 'yaml'), but got conf.
+ >>> with open("tests/test.yaml") as f:
+ ... d.load(f)
+ Traceback (most recent call last):
+ ValueError: `method` must be specified when loading from IO.
+ """
+
+ if method is None:
+ if isinstance(file, (IOBase, IO)):
+ raise ValueError("`method` must be specified when loading from IO.")
+ method = splitext(file)[-1][1:]
+ extension = method.lower()
+ if extension in JSON:
+ return cls.from_json(file, *args, **kwargs)
+ if extension in YAML:
+ return cls.from_yaml(file, *args, **kwargs)
+ raise TypeError(f"`file={file!r}` should be in {JSON} or {YAML}, but got {extension}.")
|
@@ -8010,8 +8014,7 @@
Source code in chanfig/flat_dict.py
- Python |
---|
715
-716
+ Python |
---|
| def merge(self, *args: Any, overwrite: bool = True, **kwargs: Any) -> Self:
- r"""
- Merge `other` into `FlatDict`.
-
- Args:
- *args: `Mapping` or `Sequence` to be merged.
- overwrite: Whether to overwrite existing values.
- **kwargs: `Mapping` to be merged.
-
- Returns:
- self:
-
- **Alias**:
-
- + `union`
-
- Examples:
- >>> d = FlatDict(a=1, b=2, c=3)
- >>> n = {'b': 'b', 'c': 'c', 'd': 'd'}
- >>> d.merge(n).dict()
- {'a': 1, 'b': 'b', 'c': 'c', 'd': 'd'}
- >>> l = [('c', 3), ('d', 4)]
- >>> d.merge(l).dict()
- {'a': 1, 'b': 'b', 'c': 3, 'd': 4}
- >>> FlatDict(a=1, b=1, c=1).union(FlatDict(b='b', c='c', d='d')).dict() # alias
- {'a': 1, 'b': 'b', 'c': 'c', 'd': 'd'}
- >>> d = FlatDict()
- >>> d.merge({1: 1, 2: 2, 3:3}).dict()
- {1: 1, 2: 2, 3: 3}
- >>> d.merge(d.clone()).dict()
- {1: 1, 2: 2, 3: 3}
- >>> d.merge({1:3, 2:1, 3: 2, 4: 4, 5: 5}, overwrite=False).dict()
- {1: 1, 2: 2, 3: 3, 4: 4, 5: 5}
- """
-
- if len(args) == 1:
- args = args[0]
- if isinstance(args, (PathLike, str, bytes)):
- args = self.load(args) # type: ignore[assignment]
- warn(
- "merge file is deprecated and maybe removed in a future release. Use `merge_from_file` instead.",
- PendingDeprecationWarning,
- )
- self._merge(self, args, overwrite=overwrite)
- elif len(args) > 1:
- self._merge(self, args, overwrite=overwrite)
- if kwargs:
- self._merge(self, kwargs, overwrite=overwrite)
- return self
+763
+764
| def merge(self, *args: Any, overwrite: bool = True, **kwargs: Any) -> Self:
+ r"""
+ Merge `other` into `FlatDict`.
+
+ Args:
+ *args: `Mapping` or `Sequence` to be merged.
+ overwrite: Whether to overwrite existing values.
+ **kwargs: `Mapping` to be merged.
+
+ Returns:
+ self:
+
+ **Alias**:
+
+ + `union`
+
+ Examples:
+ >>> d = FlatDict(a=1, b=2, c=3)
+ >>> n = {'b': 'b', 'c': 'c', 'd': 'd'}
+ >>> d.merge(n).dict()
+ {'a': 1, 'b': 'b', 'c': 'c', 'd': 'd'}
+ >>> l = [('c', 3), ('d', 4)]
+ >>> d.merge(l).dict()
+ {'a': 1, 'b': 'b', 'c': 3, 'd': 4}
+ >>> FlatDict(a=1, b=1, c=1).union(FlatDict(b='b', c='c', d='d')).dict() # alias
+ {'a': 1, 'b': 'b', 'c': 'c', 'd': 'd'}
+ >>> d = FlatDict()
+ >>> d.merge({1: 1, 2: 2, 3:3}).dict()
+ {1: 1, 2: 2, 3: 3}
+ >>> d.merge(d.clone()).dict()
+ {1: 1, 2: 2, 3: 3}
+ >>> d.merge({1:3, 2:1, 3: 2, 4: 4, 5: 5}, overwrite=False).dict()
+ {1: 1, 2: 2, 3: 3, 4: 4, 5: 5}
+ """
+
+ if len(args) == 1:
+ args = args[0]
+ if isinstance(args, (PathLike, str, bytes)):
+ args = self.load(args) # type: ignore[assignment]
+ warn(
+ "merge file is deprecated and maybe removed in a future release. Use `merge_from_file` instead.",
+ PendingDeprecationWarning,
+ )
+ self._merge(self, args, overwrite=overwrite)
+ elif len(args) > 1:
+ self._merge(self, args, overwrite=overwrite)
+ if kwargs:
+ self._merge(self, kwargs, overwrite=overwrite)
+ return self
|
@@ -8215,8 +8219,7 @@
Source code in chanfig/flat_dict.py
- Python |
---|
790
-791
+ Python |
---|
| def merge_from_file(self, file: File, *args: Any, **kwargs: Any) -> Self:
- r"""
- Merge content of `file` into `FlatDict`.
-
- Args:
- file (File):
- *args: Passed to [`load`][chanfig.FlatDict.load].
- **kwargs: Passed to [`load`][chanfig.FlatDict.load].
-
- Returns:
- self:
-
- Examples:
- >>> d = FlatDict(a=1, b=1)
- >>> d.merge_from_file("tests/test.yaml").dict()
- {'a': 1, 'b': 2, 'c': 3}
- """
-
- return self.merge(self.load(file, *args, **kwargs))
+808
+809
| def merge_from_file(self, file: File, *args: Any, **kwargs: Any) -> Self:
+ r"""
+ Merge content of `file` into `FlatDict`.
+
+ Args:
+ file (File):
+ *args: Passed to [`load`][chanfig.FlatDict.load].
+ **kwargs: Passed to [`load`][chanfig.FlatDict.load].
+
+ Returns:
+ self:
+
+ Examples:
+ >>> d = FlatDict(a=1, b=1)
+ >>> d.merge_from_file("tests/test.yaml").dict()
+ {'a': 1, 'b': 2, 'c': 3}
+ """
+
+ return self.merge(self.load(file, *args, **kwargs))
|
@@ -8382,8 +8386,7 @@
Source code in chanfig/flat_dict.py
- Python |
---|
1304
-1305
+ Python |
---|
| @staticmethod
-@contextmanager
-def open(file: File, *args: Any, encoding: str = "utf-8", **kwargs: Any) -> Generator[IOBase | IO, Any, Any]:
- r"""
- Open file IO from file path or IO.
-
- This methods extends the ability of built-in `open` by allowing it to accept an `IOBase` object.
-
- Args:
- file: File path or IO.
- *args: Additional arguments passed to `open`.
- Defaults to ().
- **kwargs: Any
- Additional keyword arguments passed to `open`.
- Defaults to {}.
-
- Yields:
- (Generator[IOBase | IO, Any, Any]):
-
- Examples:
- >>> with FlatDict.open("tests/test.yaml") as fp:
- ... print(fp.read())
- a: 1
- b: 2
- c: 3
- <BLANKLINE>
- >>> io = open("tests/test.yaml")
- >>> with FlatDict.open(io) as fp:
- ... print(fp.read())
- a: 1
- b: 2
- c: 3
- <BLANKLINE>
- >>> with FlatDict.open(123, mode="w") as fp:
- ... print(fp.read())
- Traceback (most recent call last):
- TypeError: expected str, bytes, os.PathLike, IO or IOBase, not int
- """
-
- if isinstance(file, (IOBase, IO)):
- yield file
- elif isinstance(file, (PathLike, str, bytes)):
- try:
- file = open(file, *args, encoding=encoding, **kwargs) # type: ignore[call-overload] # noqa: SIM115
- yield file # type: ignore[misc]
- finally:
- with suppress(Exception):
- file.close() # type: ignore[union-attr]
- else:
- raise TypeError(f"expected str, bytes, os.PathLike, IO or IOBase, not {type(file).__name__}")
+1353
+1354
| @staticmethod
+@contextmanager
+def open(file: File, *args: Any, encoding: str = "utf-8", **kwargs: Any) -> Generator[IOBase | IO, Any, Any]:
+ r"""
+ Open file IO from file path or IO.
+
+ This methods extends the ability of built-in `open` by allowing it to accept an `IOBase` object.
+
+ Args:
+ file: File path or IO.
+ *args: Additional arguments passed to `open`.
+ Defaults to ().
+ **kwargs: Any
+ Additional keyword arguments passed to `open`.
+ Defaults to {}.
+
+ Yields:
+ (Generator[IOBase | IO, Any, Any]):
+
+ Examples:
+ >>> with FlatDict.open("tests/test.yaml") as fp:
+ ... print(fp.read())
+ a: 1
+ b: 2
+ c: 3
+ <BLANKLINE>
+ >>> io = open("tests/test.yaml")
+ >>> with FlatDict.open(io) as fp:
+ ... print(fp.read())
+ a: 1
+ b: 2
+ c: 3
+ <BLANKLINE>
+ >>> with FlatDict.open(123, mode="w") as fp:
+ ... print(fp.read())
+ Traceback (most recent call last):
+ TypeError: expected str, bytes, os.PathLike, IO or IOBase, not int
+ """
+
+ if isinstance(file, (IOBase, IO)):
+ yield file
+ elif isinstance(file, (PathLike, str, bytes)):
+ try:
+ file = open(file, *args, encoding=encoding, **kwargs) # type: ignore[call-overload] # noqa: SIM115
+ yield file # type: ignore[misc]
+ finally:
+ with suppress(Exception):
+ file.close() # type: ignore[union-attr]
+ else:
+ raise TypeError(f"expected str, bytes, os.PathLike, IO or IOBase, not {type(file).__name__}")
|
@@ -8552,8 +8556,7 @@
Source code in chanfig/flat_dict.py
- Python |
---|
1071
-1072
+ Python |
---|
| def save( # pylint: disable=W1113
- self, file: File, method: str = None, *args: Any, **kwargs: Any # type: ignore[assignment]
-) -> None:
- r"""
- Save `FlatDict` to file.
-
- Raises:
- ValueError: If save to `IO` and `method` is not specified.
- TypeError: If save to unsupported extension.
-
- **Alias**:
-
- + `save`
-
- Examples:
- >>> d = FlatDict(a=1, b=2, c=3)
- >>> d.save("tests/test.yaml")
- >>> d.save("test.conf")
- Traceback (most recent call last):
- TypeError: `file='test.conf'` should be in ('json',) or ('yml', 'yaml'), but got conf.
- >>> with open("test.yaml", "w") as f:
- ... d.save(f)
- Traceback (most recent call last):
- ValueError: `method` must be specified when saving to IO.
- """
-
- if method is None:
- if isinstance(file, (IOBase, IO)):
- raise ValueError("`method` must be specified when saving to IO.")
- method = splitext(file)[-1][1:]
- extension = method.lower()
- if extension in YAML:
- return self.yaml(file=file, *args, **kwargs) # type: ignore[misc] # noqa: B026
- if extension in JSON:
- return self.json(file=file, *args, **kwargs) # type: ignore[misc] # noqa: B026
- raise TypeError(f"`file={file!r}` should be in {JSON} or {YAML}, but got {extension}.")
+1106
+1107
| def save( # pylint: disable=W1113
+ self, file: File, method: str = None, *args: Any, **kwargs: Any # type: ignore[assignment]
+) -> None:
+ r"""
+ Save `FlatDict` to file.
+
+ Raises:
+ ValueError: If save to `IO` and `method` is not specified.
+ TypeError: If save to unsupported extension.
+
+ **Alias**:
+
+ + `save`
+
+ Examples:
+ >>> d = FlatDict(a=1, b=2, c=3)
+ >>> d.save("tests/test.yaml")
+ >>> d.save("test.conf")
+ Traceback (most recent call last):
+ TypeError: `file='test.conf'` should be in ('json',) or ('yml', 'yaml'), but got conf.
+ >>> with open("test.yaml", "w") as f:
+ ... d.save(f)
+ Traceback (most recent call last):
+ ValueError: `method` must be specified when saving to IO.
+ """
+
+ if method is None:
+ if isinstance(file, (IOBase, IO)):
+ raise ValueError("`method` must be specified when saving to IO.")
+ method = splitext(file)[-1][1:]
+ extension = method.lower()
+ if extension in YAML:
+ return self.yaml(file=file, *args, **kwargs) # type: ignore[misc] # noqa: B026
+ if extension in JSON:
+ return self.json(file=file, *args, **kwargs) # type: ignore[misc] # noqa: B026
+ raise TypeError(f"`file={file!r}` should be in {JSON} or {YAML}, but got {extension}.")
|
@@ -8858,8 +8862,7 @@
Source code in chanfig/flat_dict.py
- Python |
---|
408
-409
+ Python |
---|
| def setattr(self, name: str, value: Any) -> None:
- r"""
- Set attribute of `FlatDict`.
-
- Note that it won't alter values in `FlatDict`.
-
- Args:
- name:
- value:
-
- Warns:
- RuntimeWarning: If name already exists in `FlatDict`.
-
- Examples:
- >>> d = FlatDict()
- >>> d.setattr('attr', 'value')
- >>> d.getattr('attr')
- 'value'
- >>> d.set('d', 1013)
- >>> d.setattr('d', 1031) # RuntimeWarning: d already exists in FlatDict.
- >>> d.get('d')
- 1013
- >>> d.d
- 1013
- >>> d.getattr('d')
- 1031
- """
-
- if name in self:
- warn(
- f"{name} already exists in {self.__class__.__name__}.\n"
- f"Users must call `{self.__class__.__name__}.getattr()` to retrieve conflicting attribute value.",
- RuntimeWarning,
- )
- self.__dict__[name] = value
+442
+443
| def setattr(self, name: str, value: Any) -> None:
+ r"""
+ Set attribute of `FlatDict`.
+
+ Note that it won't alter values in `FlatDict`.
+
+ Args:
+ name:
+ value:
+
+ Warns:
+ RuntimeWarning: If name already exists in `FlatDict`.
+
+ Examples:
+ >>> d = FlatDict()
+ >>> d.setattr('attr', 'value')
+ >>> d.getattr('attr')
+ 'value'
+ >>> d.set('d', 1013)
+ >>> d.setattr('d', 1031) # RuntimeWarning: d already exists in FlatDict.
+ >>> d.get('d')
+ 1013
+ >>> d.d
+ 1013
+ >>> d.getattr('d')
+ 1031
+ """
+
+ if name in self:
+ warn(
+ f"{name} already exists in {self.__class__.__name__}.\n"
+ f"Users must call `{self.__class__.__name__}.getattr()` to retrieve conflicting attribute value.",
+ RuntimeWarning,
+ )
+ self.__dict__[name] = value
|
@@ -8986,8 +8990,7 @@
Source code in chanfig/flat_dict.py
- Python |
---|
563
-564
+ Python |
---|
| def sort(self, key: Callable | None = None, reverse: bool = False) -> Self:
- r"""
- Sort `FlatDict`.
-
- Returns:
- (FlatDict):
-
- Examples:
- >>> d = FlatDict(a=1, b=2, c=3)
- >>> d.sort().dict()
- {'a': 1, 'b': 2, 'c': 3}
- >>> d = FlatDict(b=2, c=3, a=1)
- >>> d.sort().dict()
- {'a': 1, 'b': 2, 'c': 3}
- >>> a = [1]
- >>> d = FlatDict(z=0, a=a)
- >>> a.append(2)
- >>> d.sort().dict()
- {'a': [1, 2], 'z': 0}
- """
-
- items = sorted(self.items(), key=key, reverse=reverse)
- self.clear()
- for k, v in items: # pylint: disable=C0103
- self[k] = v
- return self
+588
+589
| def sort(self, key: Callable | None = None, reverse: bool = False) -> Self:
+ r"""
+ Sort `FlatDict`.
+
+ Returns:
+ (FlatDict):
+
+ Examples:
+ >>> d = FlatDict(a=1, b=2, c=3)
+ >>> d.sort().dict()
+ {'a': 1, 'b': 2, 'c': 3}
+ >>> d = FlatDict(b=2, c=3, a=1)
+ >>> d.sort().dict()
+ {'a': 1, 'b': 2, 'c': 3}
+ >>> a = [1]
+ >>> d = FlatDict(z=0, a=a)
+ >>> a.append(2)
+ >>> d.sort().dict()
+ {'a': [1, 2], 'z': 0}
+ """
+
+ items = sorted(self.items(), key=key, reverse=reverse)
+ self.clear()
+ for k, v in items: # pylint: disable=C0103
+ self[k] = v
+ return self
|
@@ -9118,8 +9122,7 @@
Source code in chanfig/flat_dict.py
- Python |
---|
902
-903
+ Python |
---|
| def to(self, cls: str | TorchDevice | TorchDType) -> Self: # pragma: no cover
- r"""
- Convert values of `FlatDict` to target `cls`.
-
- Args:
- cls (str | torch.device | torch.dtype):
-
- Returns:
- self:
-
- Examples:
- >>> d = FlatDict(a=1, b=2, c=3)
- >>> d.to(int)
- Traceback (most recent call last):
- TypeError: to() only support torch.dtype and torch.device, but got <class 'int'>.
- """
-
- # pylint: disable=C0103
-
- if isinstance(cls, (str, TorchDevice, TorchDType)):
- for k, v in self.all_items():
- if hasattr(v, "to"):
- self[k] = v.to(cls)
- return self
-
- raise TypeError(f"to() only support torch.dtype and torch.device, but got {cls}.")
+927
+928
| def to(self, cls: str | TorchDevice | TorchDType) -> Self: # pragma: no cover
+ r"""
+ Convert values of `FlatDict` to target `cls`.
+
+ Args:
+ cls (str | torch.device | torch.dtype):
+
+ Returns:
+ self:
+
+ Examples:
+ >>> d = FlatDict(a=1, b=2, c=3)
+ >>> d.to(int)
+ Traceback (most recent call last):
+ TypeError: to() only support torch.dtype and torch.device, but got <class 'int'>.
+ """
+
+ # pylint: disable=C0103
+
+ if isinstance(cls, (str, TorchDevice, TorchDType)):
+ for k, v in self.all_items():
+ if hasattr(v, "to"):
+ self[k] = v.to(cls)
+ return self
+
+ raise TypeError(f"to() only support torch.dtype and torch.device, but got {cls}.")
|
@@ -9190,17 +9194,17 @@
Source code in chanfig/flat_dict.py
- Python |
---|
518
-519
+ Python |
---|
| def to_dict(self, flatten: bool = False) -> Mapping | Sequence | Set:
- r"""
- Alias of [`dict`][chanfig.FlatDict.dict].
- """
-
- return self.dict(flatten)
+523
+524
| def to_dict(self, flatten: bool = False) -> Mapping | Sequence | Set:
+ r"""
+ Alias of [`dict`][chanfig.FlatDict.dict].
+ """
+
+ return self.dict(flatten)
|
@@ -9259,8 +9263,7 @@
Source code in chanfig/flat_dict.py
- Python |
---|
973
-974
+ Python |
---|
| def tpu(self) -> Self: # pragma: no cover
- r"""
- Move all tensors to tpu.
-
- Returns:
- self:
-
- **Alias**:
-
- + `xla`
-
- Examples:
- >>> import torch
- >>> d = FlatDict(a=torch.tensor(1))
- >>> d.tpu().dict() # doctest: +SKIP
- {'a': tensor(1, device='xla:0')}
- >>> d.xla().dict() # alias # doctest: +SKIP
- {'a': tensor(1, device='xla:0')}
- """
-
- return self.to(TorchDevice("xla"))
+993
+994
| def tpu(self) -> Self: # pragma: no cover
+ r"""
+ Move all tensors to tpu.
+
+ Returns:
+ self:
+
+ **Alias**:
+
+ + `xla`
+
+ Examples:
+ >>> import torch
+ >>> d = FlatDict(a=torch.tensor(1))
+ >>> d.tpu().dict() # doctest: +SKIP
+ {'a': tensor(1, device='xla:0')}
+ >>> d.xla().dict() # alias # doctest: +SKIP
+ {'a': tensor(1, device='xla:0')}
+ """
+
+ return self.to(TorchDevice("xla"))
|
@@ -9321,15 +9325,15 @@
Source code in chanfig/flat_dict.py
- Python |
---|
784
-785
+ Python |
---|
| def union(self, *args: Any, **kwargs: Any) -> Self:
- r"""
- Alias of [`merge`][chanfig.FlatDict.merge].
- """
- return self.merge(*args, **kwargs)
+788
+789
| def union(self, *args: Any, **kwargs: Any) -> Self:
+ r"""
+ Alias of [`merge`][chanfig.FlatDict.merge].
+ """
+ return self.merge(*args, **kwargs)
|
@@ -9465,15 +9469,15 @@
Source code in chanfig/flat_dict.py
- Python |
---|
| def xla(self) -> Self: # pragma: no cover
- r"""
- Alias of [`tpu`][chanfig.FlatDict.tpu].
- """
- return self.tpu()
+ Python |
---|
| def xla(self) -> Self: # pragma: no cover
+ r"""
+ Alias of [`tpu`][chanfig.FlatDict.tpu].
+ """
+ return self.tpu()
|
@@ -9503,8 +9507,7 @@
Source code in chanfig/flat_dict.py
- |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|