dict.py

Go to the documentation of this file.
00001 # This file is Copyright 2009, 2010 Dean Hall.
00002 #
00003 # This file is part of the Python-on-a-Chip program.
00004 # Python-on-a-Chip is free software: you can redistribute it and/or modify
00005 # it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE Version 2.1.
00006 #
00007 # Python-on-a-Chip is distributed in the hope that it will be useful,
00008 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00009 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00010 # A copy of the GNU LESSER GENERAL PUBLIC LICENSE Version 2.1
00011 # is seen in the file COPYING in this directory.
00012 
00013 ## @file
00014 #  @copybrief dict
00015 
00016 ## @package dict
00017 #  @brief Provides PyMite's dict module.
00018 
00019 def clear(d):
00020     """__NATIVE__
00021     pPmObj_t pd;
00022     PmReturn_t retval = PM_RET_OK;
00023 
00024     /* Raise TypeError if it's not a dict or wrong number of args, */
00025     pd = NATIVE_GET_LOCAL(0);
00026     if ((OBJ_GET_TYPE(pd) != OBJ_TYPE_DIC) || (NATIVE_GET_NUM_ARGS() != 1))
00027     {
00028         PM_RAISE(retval, PM_RET_EX_TYPE);
00029         return retval;
00030     }
00031 
00032     /* Clear the contents of the dict */
00033     retval = dict_clear(pd);
00034     PM_RETURN_IF_ERROR(retval);
00035 
00036     NATIVE_SET_TOS(PM_NONE);
00037 
00038     return retval;
00039     """
00040     pass
00041 
00042 
00043 def keys(d):
00044     """__NATIVE__
00045     pPmObj_t pd;
00046     pPmObj_t pl;
00047     pPmObj_t pk;
00048     pSeglist_t psl;
00049     uint16_t i;
00050     PmReturn_t retval = PM_RET_OK;
00051     uint8_t objid;
00052 
00053     /* Raise TypeError if it's not a dict or wrong number of args, */
00054     pd = NATIVE_GET_LOCAL(0);
00055     if ((OBJ_GET_TYPE(pd) != OBJ_TYPE_DIC) || (NATIVE_GET_NUM_ARGS() != 1))
00056     {
00057         PM_RAISE(retval, PM_RET_EX_TYPE);
00058         return retval;
00059     }
00060 
00061     /* Create empty list */
00062     retval = list_new(&pl);
00063     PM_RETURN_IF_ERROR(retval);
00064 
00065     /* Iterate through the keys seglist */
00066     psl = ((pPmDict_t)pd)->d_keys;
00067     for (i = 0; i < ((pPmDict_t)pd)->length; i++)
00068     {
00069         /* Get the key and append it to the list */
00070         retval = seglist_getItem(psl, i, &pk);
00071         PM_RETURN_IF_ERROR(retval);
00072         heap_gcPushTempRoot(pl, &objid);
00073         retval = list_append(pl, pk);
00074         heap_gcPopTempRoot(objid);
00075         PM_RETURN_IF_ERROR(retval);
00076     }
00077 
00078     /* Return the list of keys to the caller */
00079     NATIVE_SET_TOS(pl);
00080 
00081     return retval;
00082     """
00083     pass
00084 
00085 
00086 def has_key(d, k):
00087     if k in keys(d):
00088         return 1
00089     else:
00090         return 0
00091 
00092 
00093 def values(d):
00094     """__NATIVE__
00095     pPmObj_t pd;
00096     pPmObj_t pl;
00097     pPmObj_t pv;
00098     pSeglist_t psl;
00099     uint16_t i;
00100     PmReturn_t retval = PM_RET_OK;
00101     uint8_t objid;
00102 
00103     /* Raise TypeError if it's not a dict or wrong number of args, */
00104     pd = NATIVE_GET_LOCAL(0);
00105     if ((OBJ_GET_TYPE(pd) != OBJ_TYPE_DIC) || (NATIVE_GET_NUM_ARGS() != 1))
00106     {
00107         PM_RAISE(retval, PM_RET_EX_TYPE);
00108         return retval;
00109     }
00110 
00111     /* Create empty list */
00112     retval = list_new(&pl);
00113     PM_RETURN_IF_ERROR(retval);
00114 
00115     /* Iterate through the values seglist */
00116     psl = ((pPmDict_t)pd)->d_vals;
00117     for (i = 0; i < ((pPmDict_t)pd)->length; i++)
00118     {
00119         /* Get the value and append it to the list */
00120         retval = seglist_getItem(psl, i, &pv);
00121         PM_RETURN_IF_ERROR(retval);
00122         heap_gcPushTempRoot(pl, &objid);
00123         retval = list_append(pl, pv);
00124         heap_gcPopTempRoot(objid);
00125         PM_RETURN_IF_ERROR(retval);
00126     }
00127 
00128     /* Return the list of values to the caller */
00129     NATIVE_SET_TOS(pl);
00130 
00131     return retval;
00132     """
00133     pass
00134 
00135 
00136 # :mode=c:

Generated on Mon Oct 18 07:40:46 2010 for Python-on-a-chip by  doxygen 1.5.9