Add examples

This commit is contained in:
Mahbub Alam 2023-10-23 00:24:25 -04:00
parent 5989a421ee
commit 53276a299a
9 changed files with 245 additions and 73 deletions

50
examples/average.cpp Normal file
View File

@ -0,0 +1,50 @@
/**
* Calculate average of a list of numbers.
* This example illustrate the process of creating a python module and
* bind a function to it, as well as the procedure for calling the function.
* Additionally, it outlines the creating of a list.
*/
#include "pocketpy.h"
using namespace pkpy;
int main(){
// Create a virtual machine
VM* vm = new VM();
// Create a module
PyObject* math_utils = vm->new_module("math_utils");
// Bind a function named "average" to the module
vm->bind(math_utils, "average(l: List[float]) -> float",
[](VM* vm, ArgsView args){
// Cast the argument to a list
List& l = py_cast<List&>(vm, args[0]);
double sum = 0;
// Calculate the sum of the list by iterating through the list
for(auto& e : l){
sum += py_cast<double>(vm, e);
}
return py_var(vm, sum / l.size());
});
// Create a list of numbers and covert it to a python object
List numbers;
numbers.push_back(py_var(vm, 1.0));
numbers.push_back(py_var(vm, 2.0));
numbers.push_back(py_var(vm, 3.0));
PyObject* list = py_var(vm, std::move(numbers));
// Call the "average" function
PyObject* f_average = math_utils->attr("average");
PyObject* result = vm->call(f_average, list);
std::cout << "Average: " << py_cast<double>(vm, result) << std::endl; // 2
// Dispose the virtual machine
delete vm;
return 0;
}

View File

@ -1,19 +0,0 @@
#include "pocketpy.h"
using namespace pkpy;
int main(){
// Create a virtual machine
VM* vm = new VM();
// cjson loads and dumps!
vm->exec("import cjson");
vm->exec("dict = {'a': 1, 'b': [1, 3, 'Hello World'], 'c': {'a': 4}, 'd': None, 'd': True }");
vm->exec("json_str = cjson.dumps(dict)");
vm->exec("print(json_str)");
vm->exec("loaded_dict = cjson.loads(json_str)");
vm->exec("print(loaded_dict)");
delete vm;
return 0;
}

108
examples/employee.cpp Normal file
View File

@ -0,0 +1,108 @@
/**
* This example illustrate use of Dict and List in PocketPy.
* It creates a python module named "employee" and bind four functions to it.
* It exercises setting and getting elements in a Dict and List.
*/
#include "pocketpy.h"
using namespace pkpy;
int main(){
// Create a virtual machine
VM* vm = new VM();
// Create "employee" module
PyObject* employee_module = vm->new_module("employee");
// Bind a function named "get_first_name" to the module
vm->bind(employee_module, "get_first_name(employee: Dict) -> str",
"Returns first_name of the employee", // docstring
[](VM* vm, ArgsView args){
// Cast the argument to a dictionary
Dict& employee = CAST(Dict&, args[0]);
// Access the first_name field and return it
return employee.try_get(VAR("first_name"));
});
// Bind a function named "get_last_name" to the module
vm->bind(employee_module, "get_last_name(employee: Dict) -> str",
"Returns last_name of the employee", // docstring
[](VM* vm, ArgsView args){
// Cast the argument to a dictionary
Dict& employee = CAST(Dict&, args[0]);
// Access the last_name field and return it
return employee.try_get(VAR("last_name"));
});
// Bind a function named "get_salary" to the module
// It accepts a dictionary as argument and returns a float
vm->bind(employee_module, "get_salary(employee: Dict) -> float",
"Returns salary of the employee", // docstring
[](VM* vm, ArgsView args){
// Cast the argument to a dictionary
Dict& employee = CAST(Dict&, args[0]);
// Access the salary field and return it
return employee.try_get(VAR("salary"));
});
// Bind a function named "love_coding" to the module
// It accepts a dictionary as argument and returns a bool
vm->bind(employee_module, "love_coding(employee: Dict) -> bool",
"Returns Yes if the employee loves coding, No otherwise", // docstring
[](VM* vm, ArgsView args){
// Cast the argument to a dictionary
Dict& employee = CAST(Dict&, args[0]);
// Access the hobbies field and cast it to a list
List& hobbies = CAST(List&, employee.try_get(VAR("hobbies")));
// Iterate through the list and check if the employee loves coding
for(auto& e : hobbies){
if(CAST(Str&, e) == Str("Coding")){
return VAR("Yes");
}
}
return VAR("No");
});
// Create employee dictionary covert it to a python object
Dict employee(vm);
employee.set(VAR("first_name"), VAR("John"));
employee.set(VAR("last_name"), VAR("Doe"));
employee.set(VAR("age"), VAR(30));
employee.set(VAR("salary"), VAR(10000.0));
List hobbies;
hobbies.push_back(VAR("Reading"));
hobbies.push_back(VAR("Walking"));
hobbies.push_back(VAR("Coding"));
employee.set(VAR("hobbies"), VAR(std::move(hobbies)));
PyObject* employee_obj = VAR(std::move(employee));
// Call the "get_first_name" function
PyObject* f_get_first_name = employee_module->attr("get_first_name");
PyObject* first_name = vm->call(f_get_first_name, employee_obj);
std::cout << "First name: " << CAST(Str&, first_name) << std::endl; // First name: John
// Call the "get_last_name" function
PyObject* f_get_last_name = employee_module->attr("get_last_name");
PyObject* last_name = vm->call(f_get_last_name, employee_obj);
std::cout << "Last name: " << CAST(Str&, last_name) << std::endl; // Last name: Doe
// Call the "get_salary" function
PyObject* f_get_salary = employee_module->attr("get_salary");
PyObject* salary = vm->call(f_get_salary, employee_obj);
std::cout << "Salary: "<< CAST(double, salary) << std::endl; // Salary: 10000
// Call the "love_coding" function
PyObject* f_love_coding = employee_module->attr("love_coding");
PyObject* love_coding = vm->call(f_love_coding, employee_obj);
std::cout << "Loves coding: " << CAST(Str&, love_coding) << std::endl; // Loves coding: Yes
// Dispose the virtual machine
delete vm;
return 0;
}

View File

View File

View File

@ -1,54 +0,0 @@
#include <iostream>
#include "pocketpy.h"
using namespace pkpy;
int main(){
// Create a virtual machine
VM* vm = new VM();
//prints: Hello, World!
vm->exec("print(\"Hello, {}!\".format(\"World\"))");
//prints: I love Python
vm->exec("print('{} {} {}'.format('I', 'love', 'Python'))");
//prints: I love Python
vm->exec("print('{0} {1} {2}'.format('I', 'love', 'Python'))");
//prints: Python love I
vm->exec("print('{2} {1} {0}'.format('I', 'love', 'Python'))");
//prints: pythonlovepython
vm->exec("print('{0}{1}{0}'.format('python', 'love'))");
//prints
vm->exec("print('{k}={v}'.format(k='key', v='value'))");
vm->exec("print('{k}={k}'.format(k='key'))");
vm->exec("print('{0}={1}'.format('{0}', '{1}'))");
vm->exec("print('{{{0}}}'.format(1))");
vm->exec("print('{0}{1}{1}'.format(1, 2, 3))");
// vm->exec("\
// try: \
// print('{0}={1}}'.format(1, 2)) \
// except ValueError: \
// print('ValueError')"
// );
vm->exec("try:\n");
vm->exec(" print('{0}={1}}'.format(1, 2))\n");
vm->exec(" exit(1)\n");
vm->exec("except ValueError:\n");
vm->exec(" print('ValueError')\n");
vm->exec("print('{{{}xxx{}x}}'.format(1, 2))");
vm->exec("print('{{abc}}'.format())");
delete vm;
return 0;
}

87
examples/type_checker.cpp Normal file
View File

@ -0,0 +1,87 @@
/**
* This example demonstrates how to create different types of objects and
* how to check their types.
*/
#include "pocketpy.h"
using namespace pkpy;
int main(){
// Create a virtual machine
VM* vm = new VM();
// Create a module
PyObject* type_checker = vm->new_module("type_checker");
// Bind a function named "get_type" to the module
vm->bind(type_checker, "get_type(obj: object) -> str",
"Returns type of a python object", // docstring
[](VM* vm, ArgsView args){
PyObject* obj = args[0];
if(is_type(obj, vm->tp_int)){
return py_var(vm, "int");
}
else if(is_type(obj, vm->tp_str)){
return py_var(vm, "str");
}
else if(is_type(obj, vm->tp_float)){
return py_var(vm, "float");
}
else if(is_type(obj, vm->tp_bool)){
return py_var(vm, "bool");
}
else if(is_type(obj, vm->tp_dict)){
return py_var(vm, "dict");
}
else if(is_type(obj, vm->tp_list)){
return py_var(vm, "list");
}
else if(is_type(obj, vm->tp_tuple)){
return py_var(vm, "tuple");
}
else{
return py_var(vm, "unknown");
}
});
// Get the "get_type" function
PyObject* f_get_type = type_checker->attr("get_type");
// Create a dictionary
Dict d(vm);
d.set(py_var(vm, "key"), py_var(vm, "value"));
// Create a list
List l;
l.push_back(py_var(vm, "list_element"));
// Declare a two-element tuple
Tuple t(2);
t[0] = py_var(vm, "tuple_element_0");
t[1] = py_var(vm, "tuple_element_1");
// Create different types of objects
PyObject* int_obj = py_var(vm, 1);
PyObject* str_obj = py_var(vm, "hello");
PyObject* float_obj = py_var(vm, 1.0);
PyObject* bool_obj = py_var(vm, true);
PyObject* dict_obj = py_var(vm, std::move(d));
PyObject* list_obj = py_var(vm, std::move(l));
PyObject* tuple_obj = py_var(vm, std::move(t));
// Call the "get_type" function and print type of different objects
std::cout << "Type of 1: " << CAST(Str&, vm->call(f_get_type, int_obj)) << std::endl;
std::cout << "Type of \"hello\": " << CAST(Str&, vm->call(f_get_type, str_obj)) << std::endl;
std::cout << "Type of 1.0: " << CAST(Str&, vm->call(f_get_type, float_obj)) << std::endl;
std::cout << "Type of true: " << CAST(Str&, vm->call(f_get_type, bool_obj)) << std::endl;
std::cout << "Type of {\"key\": \"value\" }: " << CAST(Str&, vm->call(f_get_type, dict_obj)) << std::endl;
std::cout << "Type of [\"list_element\"]: " << CAST(Str&, vm->call(f_get_type, list_obj)) << std::endl;
std::cout << "Type of (\"tuple_element_0\", \"tuple_element_1\"): " << CAST(Str&, vm->call(f_get_type, tuple_obj)) << std::endl;
// Dispose the virtual machine
delete vm;
return 0;
}