casts.py 5.0 KB

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