casts.pyx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. # cython: profile=True
  4. cdef inline int increment(object iterable, object test_string):
  5. cdef int count = 0
  6. cdef object thing
  7. for thing in iterable:
  8. if thing == test_string:
  9. count += 1
  10. return count
  11. from Naked.toolshed.c.types import NakedObject, XFSet, XDict, XList, XQueue, XSet, XString
  12. from Naked.settings import debug as DEBUG_FLAG
  13. #------------------------------------------------------------------------------
  14. # [ nob ] (NakedObject)
  15. # Cast a dictionary of attributes to a NakedObject with key>attribute mapping
  16. #------------------------------------------------------------------------------
  17. def nob(attributes={}):
  18. try:
  19. return NakedObject(attributes)
  20. except Exception as e:
  21. if DEBUG_FLAG:
  22. print("Naked Framework Error: unable to create a NakedObject with the requested argument using the nobj() function (Naked.toolshed.casts.py).")
  23. raise e
  24. #------------------------------------------------------------------------------
  25. # [ xd function ] (XDict)
  26. # Cast a Python dictionary to a XDict
  27. #------------------------------------------------------------------------------
  28. def xd(dictionary_arg, attributes={}):
  29. try:
  30. return XDict(dictionary_arg, attributes)
  31. except TypeError:
  32. raise TypeError("Attempted to cast to a XDict with an incompatible type")
  33. except Exception as e:
  34. if DEBUG_FLAG:
  35. print("Naked Framework Error: unable to cast object to a XDict with the xd() function (Naked.toolshed.casts.py).")
  36. raise e
  37. #------------------------------------------------------------------------------
  38. # [ xl function ] (XList)
  39. # Cast a Python list, set, or tuple to a XList
  40. #------------------------------------------------------------------------------
  41. def xl(list_arg, attributes={}):
  42. try:
  43. return XList(list_arg, attributes)
  44. except TypeError:
  45. raise TypeError("Attempted to cast to a XList with an incompatible type")
  46. except Exception as e:
  47. if DEBUG_FLAG:
  48. print("Naked Framework Error: unable to cast object to a XList with the xl() function (Naked.toolshed.casts.py).")
  49. raise e
  50. #------------------------------------------------------------------------------
  51. # [ xq function ] (XQueue)
  52. # Cast a Python list, set, tuple to a XQueue
  53. #------------------------------------------------------------------------------
  54. def xq(queue_arg, attributes={}):
  55. try:
  56. return XQueue(queue_arg, attributes)
  57. except TypeError:
  58. raise TypeError("Attempted to cast to a XQueue with an incompatible type")
  59. except Exception as e:
  60. if DEBUG_FLAG:
  61. print("Naked Framework Error: unable to cast object to a XQueue with the xq() function (Naked.toolshed.casts.py).")
  62. raise e
  63. #------------------------------------------------------------------------------
  64. # [ xset function ] (XSet)
  65. # Cast a Python set to a XSet
  66. #------------------------------------------------------------------------------
  67. def xset(set_arg, attributes={}):
  68. try:
  69. return XSet(set_arg, attributes)
  70. except TypeError:
  71. raise TypeError("Attempted to cast to a XSet with an incompatible type")
  72. except Exception as e:
  73. if DEBUG_FLAG:
  74. print("Naked Framework Error: unable to cast object to a XSet with the xset() function (Naked.toolshed.casts.py).")
  75. raise e
  76. #------------------------------------------------------------------------------
  77. # [ xfset function ] (XFSet)
  78. # Cast a Python set to a XFSet
  79. #------------------------------------------------------------------------------
  80. def xfset(set_arg, attributes={}):
  81. try:
  82. return XFSet(set_arg, attributes)
  83. except TypeError:
  84. raise TypeError("Attempted to cast to a XSet with an incompatible type")
  85. except Exception as e:
  86. if DEBUG_FLAG:
  87. print("Naked Framework Error: unable to cast object to a XSet with the xset() function (Naked.toolshed.casts.py).")
  88. raise e
  89. #------------------------------------------------------------------------------
  90. # [ xstr function ] (XString)
  91. # Cast a Python string to a XString
  92. #------------------------------------------------------------------------------
  93. def xstr(string_arg, attributes={}):
  94. try:
  95. return XString(string_arg, attributes)
  96. except TypeError as te:
  97. raise TypeError("Attempted to cast to a XString with an incompatible type")
  98. except Exception as e:
  99. if DEBUG_FLAG:
  100. print("Naked Framework Error: unable to cast object to a XString with the xstr() function (Naked.toolshed.casts.py).")
  101. raise e
  102. if __name__ == '__main__':
  103. test = xstr("A test", {'arg1': '2'})
  104. print(test)
  105. print(test[0])
  106. print(test.arg1)
  107. print(type(test))