|
| 1 | +# coding: utf-8 |
| 2 | +#------------------------------------------------------------------------------------------# |
| 3 | +# This file is part of Pyccel which is released under MIT License. See the LICENSE file or # |
| 4 | +# go to https://github.com/pyccel/pyccel/blob/devel/LICENSE for full license details. # |
| 5 | +#------------------------------------------------------------------------------------------# |
| 6 | +""" |
| 7 | +The dict container has a number of built-in methods that are |
| 8 | +always available. |
| 9 | +
|
| 10 | +This module contains objects which describe these methods within Pyccel's AST. |
| 11 | +""" |
| 12 | + |
| 13 | +from pyccel.ast.internals import PyccelFunction |
| 14 | + |
| 15 | +__all__ = ('DictMethod', |
| 16 | + 'DictPop', |
| 17 | + ) |
| 18 | + |
| 19 | +#============================================================================== |
| 20 | +class DictMethod(PyccelFunction): |
| 21 | + """ |
| 22 | + Abstract class for dict method calls. |
| 23 | +
|
| 24 | + A subclass of this base class represents calls to a specific dict |
| 25 | + method. |
| 26 | +
|
| 27 | + Parameters |
| 28 | + ---------- |
| 29 | + dict_obj : TypedAstNode |
| 30 | + The object which the method is called from. |
| 31 | + |
| 32 | + *args : TypedAstNode |
| 33 | + The arguments passed to dict methods. |
| 34 | + """ |
| 35 | + __slots__ = ("_dict_obj",) |
| 36 | + _attribute_nodes = PyccelFunction._attribute_nodes + ("_dict_obj",) |
| 37 | + |
| 38 | + def __init__(self, dict_obj, *args): |
| 39 | + self._dict_obj = dict_obj |
| 40 | + super().__init__(*args) |
| 41 | + |
| 42 | + @property |
| 43 | + def dict_obj(self): |
| 44 | + """ |
| 45 | + Get the object representing the dict. |
| 46 | +
|
| 47 | + Get the object representing the dict. |
| 48 | + """ |
| 49 | + return self._dict_obj |
| 50 | + |
| 51 | +#============================================================================== |
| 52 | +class DictPop(DictMethod): |
| 53 | + """ |
| 54 | + Represents a call to the .pop() method. |
| 55 | +
|
| 56 | + The pop() method pops an element from the dict. The element is selected |
| 57 | + via a key. If the key is not present in the dictionary then the default |
| 58 | + value is returned. |
| 59 | +
|
| 60 | + Parameters |
| 61 | + ---------- |
| 62 | + dict_obj : TypedAstNode |
| 63 | + The object from which the method is called. |
| 64 | +
|
| 65 | + k : TypedAstNode |
| 66 | + The key which is used to select the value from the dictionary. |
| 67 | +
|
| 68 | + d : TypedAstNode, optional |
| 69 | + The value that should be returned if the key is not present in the |
| 70 | + dictionary. |
| 71 | + """ |
| 72 | + __slots__ = ('_class_type',) |
| 73 | + _shape = None |
| 74 | + name = 'pop' |
| 75 | + |
| 76 | + def __init__(self, dict_obj, k, d = None): |
| 77 | + dict_type = dict_obj.class_type |
| 78 | + self._class_type = dict_type.value_type |
| 79 | + if k.class_type != dict_type.key_type: |
| 80 | + raise TypeError(f"Key passed to pop method has type {k.class_type}. Expected {dict_type.key_type}") |
| 81 | + if d and d.class_type != dict_type.value_type: |
| 82 | + raise TypeError(f"Default value passed to pop method has type {d.class_type}. Expected {dict_type.value_type}") |
| 83 | + super().__init__(dict_obj, k, d) |
| 84 | + |
| 85 | + @property |
| 86 | + def key(self): |
| 87 | + """ |
| 88 | + The key that is used to select the element from the dict. |
| 89 | +
|
| 90 | + The key that is used to select the element from the dict. |
| 91 | + """ |
| 92 | + return self._args[0] |
| 93 | + |
| 94 | + @property |
| 95 | + def default_value(self): |
| 96 | + """ |
| 97 | + The value that should be returned if the key is not present in the dictionary. |
| 98 | +
|
| 99 | + The value that should be returned if the key is not present in the dictionary. |
| 100 | + """ |
| 101 | + return self._args[1] |
0 commit comments