4_dictionary_operation.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * This example illustrate use of Dict in PocketPy.
  3. * It creates a python module named "employee" and bind four functions to it.
  4. * It exercises setting and getting elements in a Dict.
  5. */
  6. #include "pocketpy.h"
  7. using namespace pkpy;
  8. int main(){
  9. // Create a virtual machine
  10. VM* vm = new VM();
  11. // Create "employee" module
  12. PyObject* employee_module = vm->new_module("employee");
  13. // Bind a function named "get_first_name" to the module
  14. vm->bind(employee_module, "get_first_name(employee: Dict) -> str",
  15. "Returns first_name of the employee", // docstring
  16. [](VM* vm, ArgsView args){
  17. // Cast the argument to a dictionary
  18. Dict& employee = CAST(Dict&, args[0]);
  19. // Access the first_name field and return it
  20. return employee.try_get(VAR("first_name"));
  21. });
  22. // Bind a function named "get_last_name" to the module
  23. vm->bind(employee_module, "get_last_name(employee: Dict) -> str",
  24. "Returns last_name of the employee", // docstring
  25. [](VM* vm, ArgsView args){
  26. // Cast the argument to a dictionary
  27. Dict& employee = CAST(Dict&, args[0]);
  28. // Access the last_name field and return it
  29. return employee.try_get(VAR("last_name"));
  30. });
  31. // Bind a function named "get_salary" to the module
  32. // It accepts a dictionary as argument and returns a float
  33. vm->bind(employee_module, "get_salary(employee: Dict) -> float",
  34. "Returns salary of the employee", // docstring
  35. [](VM* vm, ArgsView args){
  36. // Cast the argument to a dictionary
  37. Dict& employee = CAST(Dict&, args[0]);
  38. // Access the salary field and return it
  39. return employee.try_get(VAR("salary"));
  40. });
  41. // Bind a function named "love_coding" to the module
  42. // It accepts a dictionary as argument and returns a bool
  43. vm->bind(employee_module, "love_coding(employee: Dict) -> bool",
  44. "Returns Yes if the employee loves coding, No otherwise", // docstring
  45. [](VM* vm, ArgsView args){
  46. // Cast the argument to a dictionary
  47. Dict& employee = CAST(Dict&, args[0]);
  48. // Access the hobbies field and cast it to a list
  49. List& hobbies = CAST(List&, employee.try_get(VAR("hobbies")));
  50. // Iterate through the list and check if the employee loves coding
  51. for(auto& e : hobbies){
  52. if(CAST(Str&, e) == Str("Coding")){
  53. return VAR("Yes");
  54. }
  55. }
  56. return VAR("No");
  57. });
  58. // Create employee dictionary covert it to a python object
  59. Dict employee(vm);
  60. employee.set(VAR("first_name"), VAR("John"));
  61. employee.set(VAR("last_name"), VAR("Doe"));
  62. employee.set(VAR("age"), VAR(30));
  63. employee.set(VAR("salary"), VAR(10000.0));
  64. List hobbies;
  65. hobbies.push_back(VAR("Reading"));
  66. hobbies.push_back(VAR("Walking"));
  67. hobbies.push_back(VAR("Coding"));
  68. employee.set(VAR("hobbies"), VAR(std::move(hobbies)));
  69. PyObject* employee_obj = VAR(std::move(employee));
  70. // Call the "get_first_name" function
  71. PyObject* f_get_first_name = employee_module->attr("get_first_name");
  72. PyObject* first_name = vm->call(f_get_first_name, employee_obj);
  73. std::cout << "First name: " << CAST(Str&, first_name) << std::endl; // First name: John
  74. // Call the "get_last_name" function
  75. PyObject* f_get_last_name = employee_module->attr("get_last_name");
  76. PyObject* last_name = vm->call(f_get_last_name, employee_obj);
  77. std::cout << "Last name: " << CAST(Str&, last_name) << std::endl; // Last name: Doe
  78. // Call the "get_salary" function
  79. PyObject* f_get_salary = employee_module->attr("get_salary");
  80. PyObject* salary = vm->call(f_get_salary, employee_obj);
  81. std::cout << "Salary: "<< CAST(double, salary) << std::endl; // Salary: 10000
  82. // Call the "love_coding" function
  83. PyObject* f_love_coding = employee_module->attr("love_coding");
  84. PyObject* love_coding = vm->call(f_love_coding, employee_obj);
  85. std::cout << "Loves coding: " << CAST(Str&, love_coding) << std::endl; // Loves coding: Yes
  86. // Dispose the virtual machine
  87. delete vm;
  88. return 0;
  89. }