| Conditions | 3 |
| Total Lines | 27 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | """Context module.""" |
||
| 55 | @classmethod |
||
| 56 | def from_dict(cls, d: dict) -> 'Context': |
||
| 57 | """Create a new context instance from a dictionary. |
||
| 58 | |||
| 59 | Use ``context.to_dict()`` to create a corresponding dictionary. |
||
| 60 | |||
| 61 | Args: |
||
| 62 | d (dict): A corresponding dictionary. |
||
| 63 | |||
| 64 | Example: |
||
| 65 | >>> context = Context.from_dict({ |
||
| 66 | ... 'text': 'Hello', |
||
| 67 | ... 'caret_locus': 3, |
||
| 68 | ... }) |
||
| 69 | >>> context.text |
||
| 70 | 'Hello' |
||
| 71 | >>> context.caret_locus |
||
| 72 | 3 |
||
| 73 | |||
| 74 | Returns: |
||
| 75 | Context: A context instance. |
||
| 76 | """ |
||
| 77 | context = cls.__new__(cls) |
||
| 78 | for k, v in d.items(): |
||
| 79 | if k in cls.__slots__: |
||
| 80 | setattr(context, k, v) |
||
| 81 | return context |
||
| 82 |