OILS / builtin / method_dict.py View on Github | oilshell.org

80 lines, 42 significant
1"""Methods on YSH Dict"""
2
3from __future__ import print_function
4
5from _devbuild.gen.value_asdl import (value, value_t)
6
7from core import vm
8from frontend import typed_args
9from mycpp import mylib
10from mycpp.mylib import log
11
12from typing import List
13
14_ = log
15
16
17class Keys(vm._Callable):
18
19 def __init__(self):
20 # type: () -> None
21 pass
22
23 def Call(self, rd):
24 # type: (typed_args.Reader) -> value_t
25
26 dictionary = rd.PosDict()
27 rd.Done()
28
29 keys = [value.Str(k) for k in dictionary.keys()] # type: List[value_t]
30 return value.List(keys)
31
32
33class Values(vm._Callable):
34
35 def __init__(self):
36 # type: () -> None
37 pass
38
39 def Call(self, rd):
40 # type: (typed_args.Reader) -> value_t
41
42 dictionary = rd.PosDict()
43 rd.Done()
44
45 values = dictionary.values() # type: List[value_t]
46 return value.List(values)
47
48
49class Erase(vm._Callable):
50
51 def __init__(self):
52 # type: () -> None
53 pass
54
55 def Call(self, rd):
56 # type: (typed_args.Reader) -> value_t
57
58 dictionary = rd.PosDict()
59 key = rd.PosStr()
60 rd.Done()
61
62 mylib.dict_erase(dictionary, key)
63 return value.Null
64
65
66class Get(vm._Callable):
67
68 def __init__(self):
69 # type: () -> None
70 pass
71
72 def Call(self, rd):
73 # type: (typed_args.Reader) -> value_t
74
75 dictionary = rd.PosDict()
76 key = rd.PosStr()
77 default_value = rd.PosValue()
78 rd.Done()
79
80 return dictionary.get(key, default_value)