Skip to content

Commit

Permalink
type check for bool on param
Browse files Browse the repository at this point in the history
  • Loading branch information
Xinyu Li committed Jan 22, 2024
1 parent 5d75436 commit c72a6a4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
20 changes: 12 additions & 8 deletions python/ionpy/Param.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,24 @@

class Param:
def __init__(self,
key: Optional[str] = None,
val: Any = None,
# -- or
obj_: Optional[c_ion_param_t] = None,
):
key: Optional[str] = None,
val: Any = None,
# -- or
obj_: Optional[c_ion_param_t] = None,
):
if obj_ is None:
obj_ = c_ion_param_t()

ret = ion_param_create(ctypes.byref(obj_), key.encode(), str(val).lower().encode())
if isinstance(val, bool):
if val:
val = "true"
else:
val = "false"
ret = ion_param_create(ctypes.byref(obj_), key.encode(), str(val).encode())
if ret != 0:
raise Exception('Invalid operation')

self.obj = obj_

def __del__(self):
if self.obj: # check not nullptr
if self.obj: # check not nullptr
ion_param_destroy(self.obj)
9 changes: 7 additions & 2 deletions python/test/test_param.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,10 @@


def test_param():
p = Param(key='iamkey', val='iamval')
print(p)

p1 = Param(key='iamkey1', val="IAMKEY") # 'IAMKEY'
p2 = Param(key='iamkey2', val="iamkey") # 'iamkey'
p3 = Param(key='iamkey3', val=1) # '1'
p4 = Param(key='iamkey4', val=0.1) # '0.1'
p5 = Param(key='iamkey5', val=True) # 'true'
p6 = Param(key='iamkey6', val=False) # 'false'

0 comments on commit c72a6a4

Please sign in to comment.