LP-500 HoTT Telemetry added device definitions
[librepilot.git] / flight / libraries / PyMite / lib / dict.py
blobbf70c6f7d1b7fa276138fed1d26ee25946974ca8
1 # This file is Copyright 2009, 2010 Dean Hall.
3 # This file is part of the Python-on-a-Chip program.
4 # Python-on-a-Chip is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE Version 2.1.
7 # Python-on-a-Chip is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 # A copy of the GNU LESSER GENERAL PUBLIC LICENSE Version 2.1
11 # is seen in the file COPYING in this directory.
13 ## @file
14 # @copybrief dict
16 ## @package dict
17 # @brief Provides PyMite's dict module.
19 def clear(d):
20 """__NATIVE__
21 pPmObj_t pd;
22 PmReturn_t retval = PM_RET_OK;
24 /* Raise TypeError if it's not a dict or wrong number of args, */
25 pd = NATIVE_GET_LOCAL(0);
26 if ((OBJ_GET_TYPE(pd) != OBJ_TYPE_DIC) || (NATIVE_GET_NUM_ARGS() != 1))
28 PM_RAISE(retval, PM_RET_EX_TYPE);
29 return retval;
32 /* Clear the contents of the dict */
33 retval = dict_clear(pd);
34 PM_RETURN_IF_ERROR(retval);
36 NATIVE_SET_TOS(PM_NONE);
38 return retval;
39 """
40 pass
43 def keys(d):
44 """__NATIVE__
45 pPmObj_t pd;
46 pPmObj_t pl;
47 pPmObj_t pk;
48 pSeglist_t psl;
49 uint16_t i;
50 PmReturn_t retval = PM_RET_OK;
51 uint8_t objid;
53 /* Raise TypeError if it's not a dict or wrong number of args, */
54 pd = NATIVE_GET_LOCAL(0);
55 if ((OBJ_GET_TYPE(pd) != OBJ_TYPE_DIC) || (NATIVE_GET_NUM_ARGS() != 1))
57 PM_RAISE(retval, PM_RET_EX_TYPE);
58 return retval;
61 /* Create empty list */
62 retval = list_new(&pl);
63 PM_RETURN_IF_ERROR(retval);
65 /* Iterate through the keys seglist */
66 psl = ((pPmDict_t)pd)->d_keys;
67 for (i = 0; i < ((pPmDict_t)pd)->length; i++)
69 /* Get the key and append it to the list */
70 retval = seglist_getItem(psl, i, &pk);
71 PM_RETURN_IF_ERROR(retval);
72 heap_gcPushTempRoot(pl, &objid);
73 retval = list_append(pl, pk);
74 heap_gcPopTempRoot(objid);
75 PM_RETURN_IF_ERROR(retval);
78 /* Return the list of keys to the caller */
79 NATIVE_SET_TOS(pl);
81 return retval;
82 """
83 pass
86 def has_key(d, k):
87 if k in keys(d):
88 return 1
89 else:
90 return 0
93 def values(d):
94 """__NATIVE__
95 pPmObj_t pd;
96 pPmObj_t pl;
97 pPmObj_t pv;
98 pSeglist_t psl;
99 uint16_t i;
100 PmReturn_t retval = PM_RET_OK;
101 uint8_t objid;
103 /* Raise TypeError if it's not a dict or wrong number of args, */
104 pd = NATIVE_GET_LOCAL(0);
105 if ((OBJ_GET_TYPE(pd) != OBJ_TYPE_DIC) || (NATIVE_GET_NUM_ARGS() != 1))
107 PM_RAISE(retval, PM_RET_EX_TYPE);
108 return retval;
111 /* Create empty list */
112 retval = list_new(&pl);
113 PM_RETURN_IF_ERROR(retval);
115 /* Iterate through the values seglist */
116 psl = ((pPmDict_t)pd)->d_vals;
117 for (i = 0; i < ((pPmDict_t)pd)->length; i++)
119 /* Get the value and append it to the list */
120 retval = seglist_getItem(psl, i, &pv);
121 PM_RETURN_IF_ERROR(retval);
122 heap_gcPushTempRoot(pl, &objid);
123 retval = list_append(pl, pv);
124 heap_gcPopTempRoot(objid);
125 PM_RETURN_IF_ERROR(retval);
128 /* Return the list of values to the caller */
129 NATIVE_SET_TOS(pl);
131 return retval;
133 pass
136 # :mode=c: