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

68 lines, 37 significant
1"""Methods on YSH Dict"""
2
3from __future__ import print_function
4
5from _devbuild.gen.syntax_asdl import loc
6from _devbuild.gen.value_asdl import (value, value_t)
7
8from core import error, vm
9from frontend import typed_args
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.PosValue()
60 rd.Done()
61
62 try:
63 del dictionary[key.s]
64 except KeyError:
65 raise error.Expr('Dict entry not found: %r' % key.s,
66 loc.Missing)
67
68 return value.Null