UPDATE as of 03/07/2021 15:36 MSK
To obtain the answer for particular question consider version of procedure catchList( ) with loop from 10 to 100 instead of 100 to 1000.
std::vector<int> catchList(int m,int pw1,int pw2) {
std::vector<int> result;
int j,z;
for( j=10; j < m; ++j)
{
z = digitSum(j);
if (pow(double(z),pw1) == pow(double(j),pw2))
result.push_back(j);
}
return result;
}
(.env) [boris@fedora33server YANDEXQ]$ python setup.py install
Next step is addressing exactly question has been asked
(.env) [boris@fedora33server YANDEXQ]$ cat MyProg1.py
import myModule
p1 = int(input("input power for digitSum :" ))
p2 = int(input("input power for Number to detect: " ))
print(myModule.caughtList(100,p1,p2))
(.env) [boris@fedora33server YANDEXQ]$ python MyProg1.py
input power for digitSum :3
input power for Number to detect: 2
[27]
END UPDATE
(.env) [boris@fedora33server YANDEXQ]$ cat test.h
#pragma once
#include <cstddef>
#include <vector>
namespace abc {
std::vector<int> catchList(int m,int pw1,int pw2) ;
}
(.env) [boris@fedora33server YANDEXQ]$ cat test.cpp
#include <Python.h>
#include <iostream>
#include <vector>
#include <cmath>
#include "test.h"
#pragma GCC diagnostic ignored "-Wsign-compare"
namespace abc {
int digitSum(int n)
{
int sum=0,m;
while(n>0)
{
m=n%10;
sum=sum+m;
n=n/10;
}
return sum;
}
std::vector<int> catchList(int m,int pw1,int pw2) {
std::vector<int> result;
int j,z;
for( j=100; j < m; ++j)
{
z = digitSum(j);
if (pow(double(z),pw1) == pow(double(j),pw2))
result.push_back(j);
}
return result;
}
}
extern "C"{}
namespace {
// myModule.caughtList(1000,power1,power2)
// is supposed to be invoked from Python script
static PyObject *caughtList(PyObject* self, PyObject* args)
{
int n;
int pwr1,pwr2;
if(!PyArg_ParseTuple(args,"iii",&n,&pwr1,&pwr2))
return NULL;
std::vector<int>numbers = abc::catchList(n,pwr1,pwr2);
PyObject* result = PyList_New(numbers.size());
for(int i = 0; i < numbers.size(); i++) {
PyList_SetItem(result, i, PyLong_FromLong(numbers[i]));
}
return result;
};
// Our Module's Function Definition struct
// We require this `NULL` to signal the end of our method
// definition
static PyMethodDef myMethods[] = {
{ "caughtList", caughtList, METH_VARARGS, "Calculate prime numbers" },
{ NULL, NULL, 0, NULL }
};
// Our Module Definition struct
static struct PyModuleDef myModule = {
PyModuleDef_HEAD_INIT,
"myModule",
"Test Module",
-1,
myMethods
};
// Initializes our module using our above struct
PyMODINIT_FUNC PyInit_myModule(void)
{
return PyModule_Create(&myModule);
};
} // namespace
(.env) [boris@fedora33server YANDEXQ]$ cat setup.py
from distutils.core import setup, Extension
import sysconfig
language = 'c++'
std = 'c++20'
# GCC flags to compile C++ Code
default_compile_args = sysconfig.get_config_var('CFLAGS').split()
extra_compile_args = [f"-std={std}", "-Wall", "-Wextra", "-Werror", "-DNDEBUG", "-O3"]
setup(name = 'myModule', version = '1.0', \
ext_modules = [Extension('myModule', ['test.cpp'])])
Now we are going to build shared library which would contain required procedure. Notice we are in Python Virtual Environment to avoid problems with "install".
(.env) [boris@fedora33server YANDEXQ]$ python setup.py install
running install
running build
running build_ext
building 'myModule' extension
creating build
creating build/temp.linux-x86_64-3.9
gcc -pthread -Wno-unused-result -Wsign-compare -DDYNAMIC_ANNOTATIONS_ENABLED=1 -DNDEBUG -O2 -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fstack-protector-strong -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE -fPIC -fwrapv -O2 -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fstack-protector-strong -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE -fPIC -fwrapv -O2 -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fstack-protector-strong -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE -fPIC -fwrapv -fPIC -I/home/boris/YANDEXQ/.env/include -I/usr/include/python3.9 -c test.cpp -o build/temp.linux-x86_64-3.9/test.o
creating build/lib.linux-x86_64-3.9
g++ -pthread -shared -Wl,-z,relro -Wl,--as-needed -Wl,-z,now -g -Wl,-z,relro -Wl,--as-needed -Wl,-z,now -g build/temp.linux-x86_64-3.9/test.o -L/usr/lib64 -o build/lib.linux-x86_64-3.9/myModule.cpython-39-x86_64-linux-gnu.so
running install_lib
copying build/lib.linux-x86_64-3.9/myModule.cpython-39-x86_64-linux-gnu.so -> /home/boris/YANDEXQ/.env/lib64/python3.9/site-packages
running install_egg_info
Removing /home/boris/YANDEXQ/.env/lib64/python3.9/site-packages/myModule-1.0-py3.9.egg-info
Writing /home/boris/YANDEXQ/.env/lib64/python3.9/site-packages/myModule-1.0-py3.9.egg-info
******************
Make sure we did it
******************
(.env) [boris@fedora33server YANDEXQ]$ cd build
(.env) [boris@fedora33server build]$ pwd
/home/boris/YANDEXQ/build
(.env) [boris@fedora33server build]$ ls -CRl
.:
total 0
drwxrwxr-x. 2 boris boris 53 Jul 2 20:42 lib.linux-x86_64-3.9
drwxrwxr-x. 2 boris boris 20 Jul 2 20:42 temp.linux-x86_64-3.9
./lib.linux-x86_64-3.9:
total 84
-rwxrwxr-x. 1 boris boris 82696 Jul 2 20:42 myModule.cpython-39-x86_64-linux-gnu.so
./temp.linux-x86_64-3.9:
total 128
-rw-rw-r--. 1 boris boris 127024 Jul 2 20:42 test.o
(.env) [boris@fedora33server YANDEXQ]$ cat MyProg1.py
import myModule
p1 = int(input("input power for digitSum :" ))
p2 = int(input("input power for Number to detect: " ))
print(myModule.caughtList(1000,p1,p2))
No comments:
Post a Comment