跳转至

FlatDict

chanfig.FlatDict

Bases: dict

FlatDict with attribute-style access.

FlatDict inherits from built-in dict.

It comes with many easy to use helper methods, such as merge, sort, difference, intersect.

It also has full support for IO operations, such as json and yaml.

Even better, FlatDict has pytorch support built-in. You can directly call FlatDict.cpu() or FlatDict.to("cpu") to move all torch.Tensor objects across devices.

FlatDict works best with Variable objects. Simply call flat_dict.a = Variable(1); flat_dict.b = flat_dict.a, and their values will be synced.

Even better, FlatDict support variable interpolation. Just set the value of one key to another key (surrounded by braces with $ at the begin, like ${xxx}), and calls flat_dict.interpolate(), FlatDict will interpolate their values and create Variable automatically.

FlatDict has many other easy to use helper methods, such as difference, intersect. And has full support for IO operations, such as json and yaml.

FlatDict also has pytorch support built-in. You can directly call flat_dict.cpu() or flat_dict.to("cpu") to move all torch.Tensor objects across devices.

Attributes:

Name Type Description
indent

Indentation level in printing and dumping to json or yaml.

Notes

FlatDict rewrite __getattribute__ and __getattr__ to supports attribute-style access to its members. Therefore, all internal attributes should be set and get through flat_dict.setattr and flat_dict.getattr.

Although it is possible to override other internal methods, it is not recommended to do so.

__class__, __dict__, and getattr are reserved and cannot be overrode in any manner.

Examples:

Python Console Session
>>> d = FlatDict()
>>> d.d = 1016
>>> d['d']
1016
>>> d['i'] = 1016
>>> d.i
1016
>>> d.a = Variable(1)
>>> d.b = d.a
>>> d.a, d.b
(1, 1)
>>> d.a += 1
>>> d.a, d.b
(2, 2)
>>> d.a = 3
>>> d.a, d.b
(3, 3)
>>> d.a = Variable('hello')
>>> f"{d.a}, world!"
'hello, world!'
>>> d.a = d.a + ', world!'
>>> d.b
'hello, world!'
Source code in chanfig/flat_dict.py
Python
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
class FlatDict(dict, metaclass=Dict):
    r"""
    `FlatDict` with attribute-style access.

    `FlatDict` inherits from built-in `dict`.

    It comes with many easy to use helper methods, such as `merge`, `sort`, `difference`, `intersect`.

    It also has full support for IO operations, such as `json` and `yaml`.

    Even better, `FlatDict` has pytorch support built-in.
    You can directly call `FlatDict.cpu()` or `FlatDict.to("cpu")` to move all `torch.Tensor` objects across devices.

    `FlatDict` works best with `Variable` objects.
    Simply call `flat_dict.a = Variable(1); flat_dict.b = flat_dict.a`, and their values will be synced.

    Even better, `FlatDict` support variable interpolation.
    Just set the value of one key to another key (surrounded by braces with $ at the begin, like ${xxx}),
    and calls `flat_dict.interpolate()`, `FlatDict` will interpolate their values and create `Variable` automatically.

    `FlatDict` has many other easy to use helper methods, such as `difference`, `intersect`.
    And has full support for IO operations, such as `json` and `yaml`.

    `FlatDict` also has pytorch support built-in.
    You can directly call `flat_dict.cpu()` or `flat_dict.to("cpu")` to move all `torch.Tensor` objects across devices.

    Attributes:
        indent: Indentation level in printing and dumping to json or yaml.

    Notes:
        `FlatDict` rewrite `__getattribute__` and `__getattr__` to supports attribute-style access to its members.
        Therefore, all internal attributes should be set and get through `flat_dict.setattr` and `flat_dict.getattr`.

        Although it is possible to override other internal methods, it is not recommended to do so.

        `__class__`, `__dict__`, and `getattr` are reserved and cannot be overrode in any manner.

    Examples:
        >>> d = FlatDict()
        >>> d.d = 1016
        >>> d['d']
        1016
        >>> d['i'] = 1016
        >>> d.i
        1016
        >>> d.a = Variable(1)
        >>> d.b = d.a
        >>> d.a, d.b
        (1, 1)
        >>> d.a += 1
        >>> d.a, d.b
        (2, 2)
        >>> d.a = 3
        >>> d.a, d.b
        (3, 3)
        >>> d.a = Variable('hello')
        >>> f"{d.a}, world!"
        'hello, world!'
        >>> d.a = d.a + ', world!'
        >>> d.b
        'hello, world!'
    """

    # pylint: disable=R0904

    indent = 2

    def __init__(self, *args: Any, **kwargs: Any) -> None:
        if len(args) == 1:
            arg = args[0]
            if isinstance(arg, (PathLike, str, bytes)):
                arg = self.load(arg)
            elif isinstance(arg, (Namespace,)):
                arg = vars(arg)
            args = (arg,)
        super().__init__(*args, **kwargs)
        self.move_class_attributes()

    def move_class_attributes(self, recursive: bool = True) -> Self:
        r"""
        Move class attributes to instance.

        Args:
            recursive:

        Returns:
            self:
        """

        def move_cls_attributes(cls: type) -> Mapping:
            return {k: cls.__dict__[k] for k in get_annotations(cls).keys() if k in cls.__dict__}

        if recursive:
            for cls in self.__class__.__mro__:
                self.merge(move_cls_attributes(cls), overwrite=False)
        else:
            self.merge(move_cls_attributes(self.__class__), overwrite=False)
        return self

    def __post_init__(self, *args, **kwargs) -> None:
        pass

    def __getattribute__(self, name: Any) -> Any:
        if (name not in ("getattr",) and not (name.startswith("__") and name.endswith("__"))) and name in self:
            if name in dir(self.__class__):
                value = super().__getattribute__(name)
                if isinstance(value, (property, staticmethod, classmethod)) or callable(value):
                    return value
            return self.get(name)
        return super().__getattribute__(name)

    def get(self, name: Any, default: Any = None) -> Any:
        r"""
        Get value from `FlatDict`.

        Args:
            name:
            default:

        Returns:
            value:
                If `FlatDict` does not contain `name`, return `default`.

        Raises:
            KeyError: If `FlatDict` does not contain `name` and `default` is not specified.
            TypeError: If `name` is not hashable.

        Examples:
            >>> d = FlatDict(d=1016)
            >>> d.get('d')
            1016
            >>> d['d']
            1016
            >>> d.d
            1016
            >>> d.get('d', None)
            1016
            >>> d.get('f', 2)
            2
            >>> d.get('f')
            >>> d.get('f', Null)
            Traceback (most recent call last):
            KeyError: 'f'
        """

        if name in self:
            return dict.__getitem__(self, name)
        if default is not Null:
            return default
        return self.__missing__(name)

    def __getitem__(self, name: Any) -> Any:
        return self.get(name, default=Null)

    def __getattr__(self, name: Any) -> Any:
        try:
            return self.get(name, default=Null)
        except KeyError:
            raise AttributeError(f"'{self.__class__.__name__}' object has no attribute '{name}'") from None

    def set(self, name: Any, value: Any) -> None:
        r"""
        Set value of `FlatDict`.

        Args:
            name:
            value:

        Examples:
            >>> d = FlatDict()
            >>> d.set('d', 1016)
            >>> d.get('d')
            1016
            >>> d['n'] = 'chang'
            >>> d.n
            'chang'
            >>> d.n = 'liu'
            >>> d['n']
            'liu'
        """

        if name is Null:
            raise ValueError("name must not be null")
        if name in self and isinstance(self.get(name), Variable):
            self.get(name).set(value)
        else:
            if name in get_annotations(self):
                anno = get_annotations(self)[name]
                if isinstance(anno, type) and not isinstance(value, anno):
                    value = anno(value)
            dict.__setitem__(self, name, value)

    def __setitem__(self, name: Any, value: Any) -> None:
        self.set(name, value)

    def __setattr__(self, name: Any, value: Any) -> None:
        self.set(name, value)

    def delete(self, name: Any) -> None:
        r"""
        Delete value from `FlatDict`.

        Args:
            name:

        Examples:
            >>> d = FlatDict(d=1016, n='chang')
            >>> d.d
            1016
            >>> d.n
            'chang'
            >>> d.delete('d')
            >>> d.d
            Traceback (most recent call last):
            AttributeError: 'FlatDict' object has no attribute 'd'
            >>> del d.n
            >>> d.n
            Traceback (most recent call last):
            AttributeError: 'FlatDict' object has no attribute 'n'
            >>> del d.f
            Traceback (most recent call last):
            AttributeError: 'FlatDict' object has no attribute 'f'
        """

        dict.__delitem__(self, name)

    def __delitem__(self, name: Any) -> None:
        return self.delete(name)

    def __delattr__(self, name: Any) -> None:
        try:
            self.delete(name)
        except KeyError:
            raise AttributeError(f"'{self.__class__.__name__}' object has no attribute '{name}'") from None

    def __missing__(self, name: Any) -> Any:  # pylint: disable=R1710
        raise KeyError(name)

    def validate(self) -> None:
        r"""
        Validate `FlatDict`.

        Raises:
            TypeError: If value is not of the type declared in class annotations.
            TypeError: If `Variable` has invalid type.
            ValueError: If `Variable` has invalid value.

        Examples:
            >>> d = FlatDict(d=Variable(1016, type=int), n=Variable('chang', validator=lambda x: x.islower()))
            >>> d = FlatDict(d=Variable(1016, type=str), n=Variable('chang', validator=lambda x: x.islower()))
            Traceback (most recent call last):
            TypeError: 'd' has invalid type. Value 1016 is not of type <class 'str'>.
            >>> d = FlatDict(d=Variable(1016, type=int), n=Variable('chang', validator=lambda x: x.isupper()))
            Traceback (most recent call last):
            ValueError: 'n' has invalid value. Value chang is not valid.
        """

        self._validate(self)

    @staticmethod
    def _validate(obj) -> None:
        if isinstance(obj, FlatDict):
            annotations = get_annotations(obj)
            for name, value in obj.items():
                if annotations and name in annotations and not isvalid(value, annotations[name]):
                    raise TypeError(f"'{name}' has invalid type. Value {value} is not of type {annotations[name]}.")
                if isinstance(value, Variable):
                    try:
                        value.validate()
                    except TypeError as exc:
                        raise TypeError(f"'{name}' has invalid type. {exc}") from None
                    except ValueError as exc:
                        raise ValueError(f"'{name}' has invalid value. {exc}") from None

    def getattr(self, name: str, default: Any = Null) -> Any:
        r"""
        Get attribute of `FlatDict`.

        Note that it won't retrieve value in `FlatDict`,

        Args:
            name:
            default:

        Returns:
            value: If `FlatDict` does not contain `name`, return `default`.

        Raises:
            AttributeError: If `FlatDict` does not contain `name` and `default` is not specified.

        Examples:
            >>> d = FlatDict(a=1)
            >>> d.get('a')
            1
            >>> d.getattr('a')
            Traceback (most recent call last):
            AttributeError: 'FlatDict' object has no attribute 'a'
            >>> d.getattr('b', 2)
            2
            >>> d.setattr('b', 3)
            >>> d.getattr('b')
            3
        """

        try:
            if name in self.__dict__:
                return self.__dict__[name]
            for cls in self.__class__.__mro__:
                annotations = get_annotations(cls)
                if name in cls.__dict__ and name not in annotations:
                    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

    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', 1016)
            >>> d.setattr('d', 1031)  # RuntimeWarning: d already exists in FlatDict.
            >>> d.get('d')
            1016
            >>> d.d
            1016
            >>> 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

    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]

    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

    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)

    def to_dict(self, flatten: bool = False) -> Mapping | Sequence | Set:
        r"""
        Alias of [`dict`][chanfig.FlatDict.dict].
        """

        return self.dict(flatten)

    @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)}.")

    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

    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

    @staticmethod
    def find_placeholders(key, value, placeholders):
        placeholder = find_placeholders(value)
        if placeholder:
            for index, name in enumerate(placeholder):
                if name.startswith("."):
                    placeholder[index] = key.rsplit(".", 1)[0] + name
                if key == name:
                    raise ValueError(f"Cannot interpolate {key} to itself.")
            placeholders[key] = placeholder

    @staticmethod
    def substitute(placeholder, interpolators, value):
        try:
            if len(value) == 1 and placeholder.startswith("${") and placeholder.endswith("}"):
                return interpolators[value[0]]
            return placeholder.replace("$", "").format(**interpolators)
        except KeyError as exc:
            raise ValueError(f"{exc} is not found in {interpolators}.") from None

    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

    @staticmethod
    def _merge(this: FlatDict, that: Iterable, overwrite: bool = True) -> Mapping:
        if not that:
            return this
        if isinstance(that, Mapping):
            that = that.items()
        for key, value in that:
            if key in this and isinstance(this[key], Mapping):
                if isinstance(value, Mapping):
                    FlatDict._merge(this[key], value)
                elif overwrite:
                    if isinstance(value, FlatDict):
                        this.set(key, value)
                    else:
                        this[key] = value
            elif overwrite or key not in this:
                this.set(key, value)
        return this

    def union(self, *args: Any, **kwargs: Any) -> Self:
        r"""
        Alias of [`merge`][chanfig.FlatDict.merge].
        """
        return self.merge(*args, **kwargs)

    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))

    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

    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)

    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]
        )

    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)

    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}.")

    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"))

    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"))

    def cuda(self) -> Self:  # pragma: no cover
        r"""
        Alias of [`gpu`][chanfig.FlatDict.gpu].
        """
        return self.gpu()

    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"))

    def xla(self) -> Self:  # pragma: no cover
        r"""
        Alias of [`tpu`][chanfig.FlatDict.tpu].
        """
        return self.tpu()

    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)

    def __deepcopy__(self, memo: Mapping | None = None) -> Self:
        # pylint: disable=C0103

        if memo is not None and id(self) in memo:
            return memo[id(self)]
        ret = self.empty()
        ret.__dict__.update(deepcopy(self.__dict__))
        for k, v in self.items():
            if isinstance(v, FlatDict):
                ret[k] = v.deepcopy(memo=memo)
            else:
                ret[k] = deepcopy(v)
        return ret

    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)

    def clone(self, memo: Mapping | None = None) -> Self:
        r"""
        Alias of [`deepcopy`][chanfig.FlatDict.deepcopy].
        """
        return self.deepcopy(memo=memo)

    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}.")

    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)

    @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}.")

    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))

    @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)

    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)

    @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))

    def yaml(self, file: File, *args: Any, **kwargs: Any) -> None:
        r"""
        Dump `FlatDict` to yaml file.

        This method internally calls `self.yamls()` to generate yaml string.
        You may overwrite `yamls` in case something is not yaml serializable.

        Examples:
            >>> d = FlatDict(a=1, b=2, c=3)
            >>> d.yaml("tests/test.yaml")
        """

        with self.open(file, mode="w") as fp:  # pylint: disable=C0103
            self.yamls(fp, *args, **kwargs)

    @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))

    def yamls(self, *args: Any, **kwargs: Any) -> str:
        r"""
        Dump `FlatDict` to yaml string.

        Returns:
            (str):

        Examples:
            >>> FlatDict(a=1, b=2, c=3).yamls()
            'a: 1\nb: 2\nc: 3\n'
        """

        kwargs.setdefault("Dumper", YamlDumper)
        kwargs.setdefault("indent", self.getattr("indent", 2))
        return yaml_dump(self.dict(), *args, **kwargs)

    @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))

    @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__}")

    @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

    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

    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()

    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()

    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()

    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})

    def dropna(self) -> Self:
        r"""
        Alias of [`dropnull`][chanfig.FlatDict.dropnull].
        """
        return self.dropnull()

    @staticmethod
    def extra_repr() -> str:  # pylint: disable=C0116
        return ""

    def __repr__(self) -> str:
        extra_lines = []
        extra_repr = self.extra_repr()
        # empty string will be split into list ['']
        if extra_repr:
            extra_lines = extra_repr.split("\n")
        child_lines = []
        for key, value in self.items():
            key_repr = repr(key)
            value_repr = repr(value)
            value_repr = self._add_indent(value_repr)
            child_lines.append(f"({key_repr}): {value_repr}")
            # child_lines.append(f"{key_repr}: {value_repr}")
        lines = extra_lines + child_lines

        main_repr = self.__class__.__name__ + "("
        if lines:
            # simple one-liner info, which most builtin Modules will use
            if len(extra_lines) == 1 and not child_lines:
                main_repr += extra_lines[0]
            elif len(child_lines) == 1 and not extra_lines and len(child_lines[0]) < 10:
                main_repr += child_lines[0]
            else:
                main_repr += "\n  " + "\n  ".join(lines) + "\n"

        main_repr += ")"
        return main_repr

    def _add_indent(self, text: str) -> str:
        lines = text.split("\n")
        # don't do anything for single-line stuff
        if len(lines) == 1:
            return text
        first = lines.pop(0)
        lines = [(self.getattr("indent", 2) * " ") + line for line in lines]
        text = "\n".join(lines)
        text = first + "\n" + text
        return text

    def __format__(self, format_spec: str) -> str:
        return repr(self.empty({k: v.__format__(format_spec) for k, v in self.all_items()}))

    def __hash__(self):
        return hash(frozenset(self.items()))

    def _ipython_display_(self):  # pragma: no cover
        return repr(self)

    def _ipython_canary_method_should_not_exist_(self):  # pragma: no cover
        return None

    def aihwerij235234ljsdnp34ksodfipwoe234234jlskjdf(self):  # pragma: no cover
        return None

    def __rich__(self):  # pragma: no cover
        return self.__repr__()

move_class_attributes

Python
move_class_attributes(recursive: bool = True) -> Self

Move class attributes to instance.

Parameters:

Name Type Description Default
recursive bool
True

Returns:

Name Type Description
self Self
Source code in chanfig/flat_dict.py
Python
def move_class_attributes(self, recursive: bool = True) -> Self:
    r"""
    Move class attributes to instance.

    Args:
        recursive:

    Returns:
        self:
    """

    def move_cls_attributes(cls: type) -> Mapping:
        return {k: cls.__dict__[k] for k in get_annotations(cls).keys() if k in cls.__dict__}

    if recursive:
        for cls in self.__class__.__mro__:
            self.merge(move_cls_attributes(cls), overwrite=False)
    else:
        self.merge(move_cls_attributes(self.__class__), overwrite=False)
    return self

get

Python
get(name: Any, default: Any = None) -> Any

Get value from FlatDict.

Parameters:

Name Type Description Default
name Any
required
default Any
None

Returns:

Name Type Description
value Any

If FlatDict does not contain name, return default.

Raises:

Type Description
KeyError

If FlatDict does not contain name and default is not specified.

TypeError

If name is not hashable.

Examples:

Python Console Session
>>> d = FlatDict(d=1016)
>>> d.get('d')
1016
>>> d['d']
1016
>>> d.d
1016
>>> d.get('d', None)
1016
>>> d.get('f', 2)
2
>>> d.get('f')
>>> d.get('f', Null)
Traceback (most recent call last):
KeyError: 'f'
Source code in chanfig/flat_dict.py
Python
def get(self, name: Any, default: Any = None) -> Any:
    r"""
    Get value from `FlatDict`.

    Args:
        name:
        default:

    Returns:
        value:
            If `FlatDict` does not contain `name`, return `default`.

    Raises:
        KeyError: If `FlatDict` does not contain `name` and `default` is not specified.
        TypeError: If `name` is not hashable.

    Examples:
        >>> d = FlatDict(d=1016)
        >>> d.get('d')
        1016
        >>> d['d']
        1016
        >>> d.d
        1016
        >>> d.get('d', None)
        1016
        >>> d.get('f', 2)
        2
        >>> d.get('f')
        >>> d.get('f', Null)
        Traceback (most recent call last):
        KeyError: 'f'
    """

    if name in self:
        return dict.__getitem__(self, name)
    if default is not Null:
        return default
    return self.__missing__(name)

set

Python
set(name: Any, value: Any) -> None

Set value of FlatDict.

Parameters:

Name Type Description Default
name Any
required
value Any
required

Examples:

Python Console Session
>>> d = FlatDict()
>>> d.set('d', 1016)
>>> d.get('d')
1016
>>> d['n'] = 'chang'
>>> d.n
'chang'
>>> d.n = 'liu'
>>> d['n']
'liu'
Source code in chanfig/flat_dict.py
Python
def set(self, name: Any, value: Any) -> None:
    r"""
    Set value of `FlatDict`.

    Args:
        name:
        value:

    Examples:
        >>> d = FlatDict()
        >>> d.set('d', 1016)
        >>> d.get('d')
        1016
        >>> d['n'] = 'chang'
        >>> d.n
        'chang'
        >>> d.n = 'liu'
        >>> d['n']
        'liu'
    """

    if name is Null:
        raise ValueError("name must not be null")
    if name in self and isinstance(self.get(name), Variable):
        self.get(name).set(value)
    else:
        if name in get_annotations(self):
            anno = get_annotations(self)[name]
            if isinstance(anno, type) and not isinstance(value, anno):
                value = anno(value)
        dict.__setitem__(self, name, value)

delete

Python
delete(name: Any) -> None

Delete value from FlatDict.

Parameters:

Name Type Description Default
name Any
required

Examples:

Python Console Session
>>> d = FlatDict(d=1016, n='chang')
>>> d.d
1016
>>> d.n
'chang'
>>> d.delete('d')
>>> d.d
Traceback (most recent call last):
AttributeError: 'FlatDict' object has no attribute 'd'
>>> del d.n
>>> d.n
Traceback (most recent call last):
AttributeError: 'FlatDict' object has no attribute 'n'
>>> del d.f
Traceback (most recent call last):
AttributeError: 'FlatDict' object has no attribute 'f'
Source code in chanfig/flat_dict.py
Python
def delete(self, name: Any) -> None:
    r"""
    Delete value from `FlatDict`.

    Args:
        name:

    Examples:
        >>> d = FlatDict(d=1016, n='chang')
        >>> d.d
        1016
        >>> d.n
        'chang'
        >>> d.delete('d')
        >>> d.d
        Traceback (most recent call last):
        AttributeError: 'FlatDict' object has no attribute 'd'
        >>> del d.n
        >>> d.n
        Traceback (most recent call last):
        AttributeError: 'FlatDict' object has no attribute 'n'
        >>> del d.f
        Traceback (most recent call last):
        AttributeError: 'FlatDict' object has no attribute 'f'
    """

    dict.__delitem__(self, name)

validate

Python
validate() -> None

Validate FlatDict.

Raises:

Type Description
TypeError

If value is not of the type declared in class annotations.

TypeError

If Variable has invalid type.

ValueError

If Variable has invalid value.

Examples:

Python Console Session
>>> d = FlatDict(d=Variable(1016, type=int), n=Variable('chang', validator=lambda x: x.islower()))
>>> d = FlatDict(d=Variable(1016, type=str), n=Variable('chang', validator=lambda x: x.islower()))
Traceback (most recent call last):
TypeError: 'd' has invalid type. Value 1016 is not of type <class 'str'>.
>>> d = FlatDict(d=Variable(1016, type=int), n=Variable('chang', validator=lambda x: x.isupper()))
Traceback (most recent call last):
ValueError: 'n' has invalid value. Value chang is not valid.
Source code in chanfig/flat_dict.py
Python
def validate(self) -> None:
    r"""
    Validate `FlatDict`.

    Raises:
        TypeError: If value is not of the type declared in class annotations.
        TypeError: If `Variable` has invalid type.
        ValueError: If `Variable` has invalid value.

    Examples:
        >>> d = FlatDict(d=Variable(1016, type=int), n=Variable('chang', validator=lambda x: x.islower()))
        >>> d = FlatDict(d=Variable(1016, type=str), n=Variable('chang', validator=lambda x: x.islower()))
        Traceback (most recent call last):
        TypeError: 'd' has invalid type. Value 1016 is not of type <class 'str'>.
        >>> d = FlatDict(d=Variable(1016, type=int), n=Variable('chang', validator=lambda x: x.isupper()))
        Traceback (most recent call last):
        ValueError: 'n' has invalid value. Value chang is not valid.
    """

    self._validate(self)

getattr

Python
getattr(name: str, default: Any = Null) -> Any

Get attribute of FlatDict.

Note that it won’t retrieve value in FlatDict,

Parameters:

Name Type Description Default
name str
required
default Any
Null

Returns:

Name Type Description
value Any

If FlatDict does not contain name, return default.

Raises:

Type Description
AttributeError

If FlatDict does not contain name and default is not specified.

Examples:

Python Console Session
>>> d = FlatDict(a=1)
>>> d.get('a')
1
>>> d.getattr('a')
Traceback (most recent call last):
AttributeError: 'FlatDict' object has no attribute 'a'
>>> d.getattr('b', 2)
2
>>> d.setattr('b', 3)
>>> d.getattr('b')
3
Source code in chanfig/flat_dict.py
Python
def getattr(self, name: str, default: Any = Null) -> Any:
    r"""
    Get attribute of `FlatDict`.

    Note that it won't retrieve value in `FlatDict`,

    Args:
        name:
        default:

    Returns:
        value: If `FlatDict` does not contain `name`, return `default`.

    Raises:
        AttributeError: If `FlatDict` does not contain `name` and `default` is not specified.

    Examples:
        >>> d = FlatDict(a=1)
        >>> d.get('a')
        1
        >>> d.getattr('a')
        Traceback (most recent call last):
        AttributeError: 'FlatDict' object has no attribute 'a'
        >>> d.getattr('b', 2)
        2
        >>> d.setattr('b', 3)
        >>> d.getattr('b')
        3
    """

    try:
        if name in self.__dict__:
            return self.__dict__[name]
        for cls in self.__class__.__mro__:
            annotations = get_annotations(cls)
            if name in cls.__dict__ and name not in annotations:
                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

setattr

Python
setattr(name: str, value: Any) -> None

Set attribute of FlatDict.

Note that it won’t alter values in FlatDict.

Parameters:

Name Type Description Default
name str
required
value Any
required

Warns:

Type Description
RuntimeWarning

If name already exists in FlatDict.

Examples:

Python Console Session
>>> d = FlatDict()
>>> d.setattr('attr', 'value')
>>> d.getattr('attr')
'value'
>>> d.set('d', 1016)
>>> d.setattr('d', 1031)  # RuntimeWarning: d already exists in FlatDict.
>>> d.get('d')
1016
>>> d.d
1016
>>> d.getattr('d')
1031
Source code in chanfig/flat_dict.py
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', 1016)
        >>> d.setattr('d', 1031)  # RuntimeWarning: d already exists in FlatDict.
        >>> d.get('d')
        1016
        >>> d.d
        1016
        >>> 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

delattr

Python
delattr(name: str) -> None

Delete attribute of FlatDict.

Note that it won’t delete values in FlatDict.

Parameters:

Name Type Description Default
name str
required

Examples:

Python Console Session
>>> 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'
Source code in chanfig/flat_dict.py
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]

hasattr

Python
hasattr(name: str) -> bool

Determine if an attribute exists in FlatDict.

Parameters:

Name Type Description Default
name str
required

Returns:

Type Description
bool

Examples:

Python Console Session
>>> d = FlatDict()
>>> d.setattr('name', 'chang')
>>> d.hasattr('name')
True
>>> d.delattr('name')
>>> d.hasattr('name')
False
Source code in chanfig/flat_dict.py
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

dict

Python
dict(flatten: bool = False) -> Mapping | Sequence | Set

Convert FlatDict to other Mapping.

Parameters:

Name Type Description Default
flatten bool

Whether to flatten NestedDict.

False

Returns:

Type Description
Mapping
See Also

to_dict: Implementation of dict.

Alias:

  • to_dict

Examples:

Python Console Session
>>> d = FlatDict(a=1, b=2, c=3)
>>> d.dict()
{'a': 1, 'b': 2, 'c': 3}
Source code in chanfig/flat_dict.py
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)

to_dict

Python
to_dict(flatten: bool = False) -> Mapping | Sequence | Set

Alias of dict.

Source code in chanfig/flat_dict.py
Python
def to_dict(self, flatten: bool = False) -> Mapping | Sequence | Set:
    r"""
    Alias of [`dict`][chanfig.FlatDict.dict].
    """

    return self.dict(flatten)

from_dict classmethod

Python
from_dict(obj: Mapping | Sequence) -> Any

Convert Mapping or Sequence to FlatDict.

Examples:

Python Console Session
>>> 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'>.
Source code in chanfig/flat_dict.py
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)}.")

sort

Python
1
2
3
sort(
    key: Callable | None = None, reverse: bool = False
) -> Self

Sort FlatDict.

Returns:

Type Description
FlatDict

Examples:

Python Console Session
>>> 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}
Source code in chanfig/flat_dict.py
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

interpolate

Python
1
2
3
4
5
interpolate(
    use_variable: bool = True,
    interpolators: MutableMapping | None = None,
    unsafe_eval: bool = False,
) -> Self

Perform Variable interpolation.

Variable interpolation allows you to set the value of one key to be the value of another key easily.

Parameters:

Name Type Description Default
use_variable bool

Whether to convert values to Variable objects.

True
interpolators MutableMapping | None

Mapping contains values for interpolation. Defaults to self.

None
unsafe_eval bool

Whether to evaluate interpolated values.

False

Raises:

Type Description
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:

Python Console Session
>>> 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}'
).
Source code in chanfig/flat_dict.py
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

merge

Python
1
2
3
merge(
    *args: Any, overwrite: bool = True, **kwargs: Any
) -> Self

Merge other into FlatDict.

Parameters:

Name Type Description Default
*args Any

Mapping or Sequence to be merged.

()
overwrite bool

Whether to overwrite existing values.

True
**kwargs Any

Mapping to be merged.

{}

Returns:

Name Type Description
self Self

Alias:

  • union

Examples:

Python Console Session
>>> 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}
Source code in chanfig/flat_dict.py
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

union

Python
union(*args: Any, **kwargs: Any) -> Self

Alias of merge.

Source code in chanfig/flat_dict.py
Python
def union(self, *args: Any, **kwargs: Any) -> Self:
    r"""
    Alias of [`merge`][chanfig.FlatDict.merge].
    """
    return self.merge(*args, **kwargs)

merge_from_file

Python
1
2
3
merge_from_file(
    file: File, *args: Any, **kwargs: Any
) -> Self

Merge content of file into FlatDict.

Parameters:

Name Type Description Default
file File
required
*args Any

Passed to load.

()
**kwargs Any

Passed to load.

{}

Returns:

Name Type Description
self Self

Examples:

Python Console Session
>>> d = FlatDict(a=1, b=1)
>>> d.merge_from_file("tests/test.yaml").dict()
{'a': 1, 'b': 2, 'c': 3}
Source code in chanfig/flat_dict.py
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))

intersect

Python
intersect(other: Mapping | Iterable | PathStr) -> Self

Intersection of FlatDict and other.

Parameters:

Name Type Description Default
other Mapping | Iterable | PathStr
required

Returns:

Type Description
FlatDict

Alias:

  • inter

Examples:

Python Console Session
>>> 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
{}
Source code in chanfig/flat_dict.py
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

inter

Python
1
2
3
4
5
inter(
    other: Mapping | Iterable | PathStr,
    *args: Any,
    **kwargs: Any
) -> Self

Alias of intersect.

Source code in chanfig/flat_dict.py
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)

difference

Python
difference(other: Mapping | Iterable | PathStr) -> Self

Difference between FlatDict and other.

Parameters:

Name Type Description Default
other Mapping | Iterable | PathStr
required

Returns:

Type Description
FlatDict

Alias:

  • diff

Examples:

Python Console Session
>>> 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'}
Source code in chanfig/flat_dict.py
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]
    )

diff

Python
1
2
3
4
5
diff(
    other: Mapping | Iterable | PathStr,
    *args: Any,
    **kwargs: Any
) -> Self

Alias of difference.

Source code in chanfig/flat_dict.py
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)

to

Python
to(cls: str | device | dtype) -> Self

Convert values of FlatDict to target cls.

Parameters:

Name Type Description Default
cls str | device | dtype
required

Returns:

Name Type Description
self Self

Examples:

Python Console Session
>>> 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'>.
Source code in chanfig/flat_dict.py
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}.")

cpu

Python
cpu() -> Self

Move all tensors to cpu.

Returns:

Name Type Description
self Self

Examples:

Python Console Session
>>> import torch
>>> d = FlatDict(a=torch.tensor(1))
>>> d.cpu().dict()
{'a': tensor(1, device='cpu')}
Source code in chanfig/flat_dict.py
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"))

gpu

Python
gpu() -> Self

Move all tensors to gpu.

Returns:

Name Type Description
self Self

Alias:

  • cuda

Examples:

Python Console Session
>>> import torch
>>> d = FlatDict(a=torch.tensor(1))
>>> d.gpu().dict()
{'a': tensor(1, device='cuda:0')}
>>> d.cuda().dict()  # alias
{'a': tensor(1, device='cuda:0')}
Source code in chanfig/flat_dict.py
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"))

cuda

Python
cuda() -> Self

Alias of gpu.

Source code in chanfig/flat_dict.py
Python
def cuda(self) -> Self:  # pragma: no cover
    r"""
    Alias of [`gpu`][chanfig.FlatDict.gpu].
    """
    return self.gpu()

tpu

Python
tpu() -> Self

Move all tensors to tpu.

Returns:

Name Type Description
self Self

Alias:

  • xla

Examples:

Python Console Session
>>> import torch
>>> d = FlatDict(a=torch.tensor(1))
>>> d.tpu().dict()
{'a': tensor(1, device='xla:0')}
>>> d.xla().dict()  # alias
{'a': tensor(1, device='xla:0')}
Source code in chanfig/flat_dict.py
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"))

xla

Python
xla() -> Self

Alias of tpu.

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()

copy

Python
copy() -> Self

Create a shallow copy of FlatDict.

Returns:

Type Description
FlatDict

Examples:

Python Console Session
>>> 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'
Source code in chanfig/flat_dict.py
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)

deepcopy

Python
deepcopy(memo: Mapping | None = None) -> Self

Create a deep copy of FlatDict.

Returns:

Type Description
FlatDict

Alias:

  • clone

Examples:

Python Console Session
>>> 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
Source code in chanfig/flat_dict.py
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)

clone

Python
clone(memo: Mapping | None = None) -> Self

Alias of deepcopy.

Source code in chanfig/flat_dict.py
Python
def clone(self, memo: Mapping | None = None) -> Self:
    r"""
    Alias of [`deepcopy`][chanfig.FlatDict.deepcopy].
    """
    return self.deepcopy(memo=memo)

save

Python
1
2
3
4
5
6
save(
    file: File,
    method: str = None,
    *args: Any,
    **kwargs: Any
) -> None

Save FlatDict to file.

Raises:

Type Description
ValueError

If save to IO and method is not specified.

TypeError

If save to unsupported extension.

Alias:

  • save

Examples:

Python Console Session
>>> 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.
Source code in chanfig/flat_dict.py
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}.")

dump

Python
1
2
3
4
5
6
dump(
    file: File,
    method: str = None,
    *args: Any,
    **kwargs: Any
) -> None

Alias of save.

Source code in chanfig/flat_dict.py
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)

load classmethod

Python
1
2
3
4
5
6
load(
    file: File,
    method: str = None,
    *args: Any,
    **kwargs: Any
) -> Self

Load FlatDict from file.

Parameters:

Name Type Description Default
file File

File to load from.

required
method str

File type, should be in JSON or YAML.

None

Returns:

Type Description
FlatDict

Raises:

Type Description
ValueError

If load from IO and method is not specified.

TypeError

If dump to unsupported extension.

Examples:

Python Console Session
>>> 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.
Source code in chanfig/flat_dict.py
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}.")

json

Python
json(file: File, *args: Any, **kwargs: Any) -> None

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:

Python Console Session
>>> d = FlatDict(a=1, b=2, c=3)
>>> d.json("tests/test.json")
Source code in chanfig/flat_dict.py
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))

from_json classmethod

Python
from_json(file: File, *args: Any, **kwargs: Any) -> Self

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:

Type Description
FlatDict

Examples:

Python Console Session
>>> d = FlatDict.from_json('tests/test.json')
>>> d.dict()
{'a': 1, 'b': 2, 'c': 3}
Source code in chanfig/flat_dict.py
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)

jsons

Python
jsons(*args: Any, **kwargs: Any) -> str

Dump FlatDict to json string.

Returns:

Type Description
str

Examples:

Python Console Session
>>> d = FlatDict(a=1, b=2, c=3)
>>> d.jsons()
'{\n  "a": 1,\n  "b": 2,\n  "c": 3\n}'
Source code in chanfig/flat_dict.py
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)

from_jsons classmethod

Python
from_jsons(string: str, *args: Any, **kwargs: Any) -> Self

Construct FlatDict from json string.

Returns:

Type Description
FlatDict

Examples:

Python Console Session
>>> 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)]
Source code in chanfig/flat_dict.py
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))

yaml

Python
yaml(file: File, *args: Any, **kwargs: Any) -> None

Dump FlatDict to yaml file.

This method internally calls self.yamls() to generate yaml string. You may overwrite yamls in case something is not yaml serializable.

Examples:

Python Console Session
>>> d = FlatDict(a=1, b=2, c=3)
>>> d.yaml("tests/test.yaml")
Source code in chanfig/flat_dict.py
Python
def yaml(self, file: File, *args: Any, **kwargs: Any) -> None:
    r"""
    Dump `FlatDict` to yaml file.

    This method internally calls `self.yamls()` to generate yaml string.
    You may overwrite `yamls` in case something is not yaml serializable.

    Examples:
        >>> d = FlatDict(a=1, b=2, c=3)
        >>> d.yaml("tests/test.yaml")
    """

    with self.open(file, mode="w") as fp:  # pylint: disable=C0103
        self.yamls(fp, *args, **kwargs)

from_yaml classmethod

Python
from_yaml(file: File, *args: Any, **kwargs: Any) -> Self

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:

Type Description
FlatDict

Examples:

Python Console Session
>>> FlatDict.from_yaml('tests/test.yaml').dict()
{'a': 1, 'b': 2, 'c': 3}
Source code in chanfig/flat_dict.py
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))

yamls

Python
yamls(*args: Any, **kwargs: Any) -> str

Dump FlatDict to yaml string.

Returns:

Type Description
str

Examples:

Python Console Session
>>> FlatDict(a=1, b=2, c=3).yamls()
'a: 1\nb: 2\nc: 3\n'
Source code in chanfig/flat_dict.py
Python
def yamls(self, *args: Any, **kwargs: Any) -> str:
    r"""
    Dump `FlatDict` to yaml string.

    Returns:
        (str):

    Examples:
        >>> FlatDict(a=1, b=2, c=3).yamls()
        'a: 1\nb: 2\nc: 3\n'
    """

    kwargs.setdefault("Dumper", YamlDumper)
    kwargs.setdefault("indent", self.getattr("indent", 2))
    return yaml_dump(self.dict(), *args, **kwargs)

from_yamls classmethod

Python
from_yamls(string: str, *args: Any, **kwargs: Any) -> Self

Construct FlatDict from yaml string.

Returns:

Type Description
FlatDict

Examples:

Python Console Session
>>> 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)]
Source code in chanfig/flat_dict.py
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))

open staticmethod

Python
1
2
3
4
5
6
open(
    file: File,
    *args: Any,
    encoding: str = "utf-8",
    **kwargs: Any
) -> Generator[IOBase | IO, Any, Any]

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.

Parameters:

Name Type Description Default
file File

File path or IO.

required
*args Any

Additional arguments passed to open. Defaults to ().

()
**kwargs Any

Any Additional keyword arguments passed to open. Defaults to {}.

{}

Yields:

Type Description
Generator[IOBase | IO, Any, Any]

Examples:

Python Console Session
>>> with FlatDict.open("tests/test.yaml") as fp:
...     print(fp.read())
a: 1
b: 2
c: 3

>>> io = open("tests/test.yaml")
>>> with FlatDict.open(io) as fp:
...     print(fp.read())
a: 1
b: 2
c: 3

>>> 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
Source code in chanfig/flat_dict.py
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__}")

empty classmethod

Python
empty(*args: Any, **kwargs: Any) -> Self

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:

Type Description
FlatDict
See Also

empty_like

Examples:

Python Console Session
>>> d = FlatDict(a=[])
>>> c = d.empty()
>>> c.dict()
{}
Source code in chanfig/flat_dict.py
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

empty_like

Python
empty_like(*args: Any, **kwargs: Any) -> Self

Initialise an empty copy of FlatDict.

This method will preserve everything in FlatDict.__class__.__dict__ and FlatDict.__dict__.

For example, propertys are saved in __dict__, they will keep their original reference after calling this method.

Returns:

Type Description
FlatDict
See Also

empty

Examples:

Python Console Session
>>> d = FlatDict(a=[])
>>> d.setattr("name", "Chang")
>>> c = d.empty_like()
>>> c.dict()
{}
>>> c.getattr("name")
'Chang'
Source code in chanfig/flat_dict.py
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

all_keys

Python
all_keys() -> Generator

Equivalent to keys.

This method is provided solely to make methods work on both FlatDict and NestedDict.

See Also

all_keys

Source code in chanfig/flat_dict.py
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()

all_values

Python
all_values() -> Generator

Equivalent to keys.

This method is provided solely to make methods work on both FlatDict and NestedDict.

See Also

all_values

Source code in chanfig/flat_dict.py
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()

all_items

Python
all_items() -> Generator

Equivalent to keys.

This method is provided solely to make methods work on both FlatDict and NestedDict.

See Also

all_items

Source code in chanfig/flat_dict.py
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()

dropnull

Python
dropnull() -> Self

Drop key-value pairs with Null value.

Returns:

Type Description
FlatDict

Alias:

  • dropna

Examples:

Python Console Session
>>> 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}
Source code in chanfig/flat_dict.py
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})

dropna

Python
dropna() -> Self

Alias of dropnull.

Source code in chanfig/flat_dict.py
Python
def dropna(self) -> Self:
    r"""
    Alias of [`dropnull`][chanfig.FlatDict.dropnull].
    """
    return self.dropnull()