@@ -5,7 +5,6 @@ import json
55from libcpp cimport bool
66from libcpp.string cimport string
77from libcpp.vector cimport vector
8- from libcpp.optional cimport optional
98
109cdef extern from " common/params.h" :
1110 cpdef enum ParamKeyFlag:
@@ -37,7 +36,7 @@ cdef extern from "common/params.h":
3736 int putBool(string, bool ) nogil
3837 bool checkKey(string) nogil
3938 ParamKeyType getKeyType(string) nogil
40- optional[ string] getKeyDefaultValue(string) nogil
39+ string getKeyDefaultValue(string) nogil
4140 string getParamPath(string) nogil
4241 void clearAll(ParamKeyFlag)
4342 vector[string] allKeys()
@@ -74,45 +73,40 @@ cdef class Params:
7473 raise UnknownKeyName(key)
7574 return key
7675
77- def cast (self , t , value , default ):
78- if value is None :
79- return None
80- try :
81- if t == STRING:
82- return value.decode(" utf-8" )
83- elif t == BOOL:
84- return value == b" 1"
85- elif t == INT:
86- return int (value)
87- elif t == FLOAT:
88- return float (value)
89- elif t == TIME:
90- return datetime.datetime.fromisoformat(value.decode(" utf-8" ))
91- elif t == JSON:
92- return json.loads(value)
93- elif t == BYTES:
94- return value
95- else :
96- raise TypeError ()
97- except (TypeError , ValueError ):
98- return self .cast(t, default, None )
99-
100- def get (self , key , bool block = False ):
76+ def get (self , key , bool block = False , default = None ):
10177 cdef string k = self .check_key(key)
102- cdef ParamKeyType t = self .p.getKeyType(k )
78+ cdef ParamKeyType t = self .p.getKeyType(ensure_bytes(key) )
10379 cdef string val
10480 with nogil:
10581 val = self .p.get(k, block)
10682
107- default = self .get_default_value(k)
10883 if val == b" " :
10984 if block:
11085 # If we got no value while running in blocked mode
11186 # it means we got an interrupt while waiting
11287 raise KeyboardInterrupt
11388 else :
114- return self .cast(t, default, None )
115- return self .cast(t, val, default)
89+ return default
90+
91+ try :
92+ if t == STRING:
93+ return val.decode(" utf-8" )
94+ elif t == BOOL:
95+ return val == b" 1"
96+ elif t == INT:
97+ return int (val)
98+ elif t == FLOAT:
99+ return float (val)
100+ elif t == TIME:
101+ return datetime.datetime.fromisoformat(val.decode(" utf-8" ))
102+ elif t == JSON:
103+ return json.loads(val)
104+ elif t == BYTES:
105+ return val
106+ else :
107+ return default
108+ except (TypeError , ValueError ):
109+ return default
116110
117111 def get_bool (self , key , bool block = False ):
118112 cdef string k = self .check_key(key)
@@ -162,5 +156,4 @@ cdef class Params:
162156 return self .p.allKeys()
163157
164158 def get_default_value (self , key ):
165- cdef optional[string] default = self .p.getKeyDefaultValue(self .check_key(key))
166- return default.value() if default.has_value() else None
159+ return self .p.getKeyDefaultValue(self .check_key(key))
0 commit comments