Python package function does not match C++ signature - python

I am trying to read a configuration file with the Pyrfa Python package. This is my code:
import pyrfa
p = pyrfa.Pyrfa
p.createConfigDb("./pyrfa.cfg")
p.acquireSession("Session1")
p.createOMMConsumer()
This code has been copied from https://github.com/devcartel/pyrfa.
When running this code, I get the following error message.
Boost.Python.ArgumentError: Python argument types in
Pyrfa.createConfigDb(str)
did not match C++ signature:
createConfigDb(class Pyrfa {lvalue}, class boost::python::api::object)
It somehow suspect that this has not so much to do with the Pyrfa package. Does anybody have an idea?
Thanks so much!

I think you're only missing ()
p = pyrfa.Pyrfa()

Related

How to use a Python class file in another script (in Matlab)

I am new to python and mainly use Matlab. I found some Python code on Github (https://github.com/simonmb/fragmentation_algorithm) that I want to run in Matlab, but I can't even manage to write a function in Python to run the code with any input. When I try to run the function I wrote (called 'RunFrag.py') in the Console in PyCharm, I only get the following error: NameError: name 'RunFrag' is not defined, even though the script is in the same folder. The file from Github ('fragmenter.py') defines a class and has the line __name__ == '__main__' and I have no idea how to deal with this, in Matlab classes occur rather rarely.
Can anyone help me with creating a function where I can just enter a SMILES molecule as input and get the fragmentation out?
Thanks in advance.
Here is the Code from the RunFrag function I wrote (the additionally needed fragmenter.py file can be found at the github link):
def RunFrag(smiles):
from .fragmenter import fragmenter
fragmentation_scheme = {
'CH2' : '[CH2]',
'OH' : '[OH]',
'CH3' : '[CH3]',
'CH2-CH2' : '[CH2][CH2]'
}
fragmentation_scheme_order1 = ['CH2-CH2', 'CH3', 'CH2', 'OH']
frg = fragmenter(fragmentation_scheme, fragmentation_scheme_order=fragmentation_scheme_order1, algorithm='simple')
fragmentation, success, fragmentation_matches = frg.fragment(smiles)
return smiles, fragmentation, success

kubeflow AttributeError: 'ComponentStore' object has no attribute 'uri_search_template'

I am trying to load some prebuit gcp kubeflow components using kfp.components.ComponentStore. However I am getting this error:
line 180, in _load_component_spec_in_component_ref
if self.uri_search_template:
AttributeError: 'ComponentStore' object has no attribute 'uri_search_template'
when at this line of code:
mlengine_train_op = component_store.load_component('ml_engine/train')
KFP version:
kfp 1.8.10
kfp-pipeline-spec 0.1.13
kfp-server-api 1.7.1
Steps to reproduce
import kfp
from kfp.components import func_to_container_op
COMPONENT_URL_SEARCH_PREFIX = "https://raw.githubusercontent.com/kubeflow/pipelines/1.7.1/components/gcp/"
component_store = kfp.components.ComponentStore(
local_search_paths=None, url_search_prefixes=[COMPONENT_URL_SEARCH_PREFIX])
mlengine_train_op = component_store.load_component('ml_engine/train')
mlengine_deploy_op = component_store.load_component('ml_engine/deploy')
I got this code from an example that was running on kfp 0.2.5. This is probably some version problem but does anyone knows how to update this code to the newer version?
I solved the issue. It is something that #Kabilan Mohanraj also mentioned is his comment. It looks like there is a bug if you don't set up uri_search_template when creating the ComponentStore.
I solved it by providing it as parameter, it doesn't really matter what you pass to the constructor really, but it is required because in the code there is the following
if self.url_search_prefixes:
but if you don't pass something that attribute doesn't get created in the constructor.
I fixed it like this:
component_store = kfp.components.ComponentStore(
local_search_paths=["local_search_path"], url_search_prefixes=[COMPONENT_URL_SEARCH_PREFIX]), uri_search_template="{name}/"
I hope it will help someone in the future.
I suggested in the github issue to address this by either make the error more clear or to have better check on uri_search_template attribute.

Unable to use pip modules with PyO3

Recently I have been working on a project that involves generating docx files. Since Rust's docx support is still quite immature, I've decided to use Python's python-docx module via PyO3.
Here's my code so far:
extern crate pyo3;
use pyo3::prelude::*;
(...)
// Initialize some Python
let gil = Python::acquire_gil();
let py = gil.python();
let docx = PyModule::import(py, "docx")?;
let document = docx.Document();
Unfortunately, I'm running into two pretty serious errors.
Error #1:
let docx = PyModule::import(py, "docx")?;
^ cannot use the `?` operator in a function that returns `std::string::String
Error #2:
let document = docx.Document();
^^^^^^^^ method not found in `&pyo3::prelude::PyModule`
How do I solve these errors?
N.B. Yes, I have made sure that python-docx is installed. It's located in /home/<my username>/.local/lib/python3.8/site-packages

Python Scripting for Abaqus

I am new to Abaqus Python Scripting. The following line of code in my script is throwing a keyword error (Type Error: Keyword error on mergeWire).
myPart.WirePolyLine(points = myPoints, mergeWire = OFF, meshable = ON)
where
myPoints = ((datum_points[crossPart_datums_keys[0]],datum_points[crossPart_datums_keys[1]]),datum_points[crossPart_datums_keys[2]],datum_points[crossPart_datums_keys[3](datum_points[crossPart_datums_keys[4]],datum_points[crossPart_datums_keys[5]]),(datum_points[crossPart_datums_keys[6]],datum_points[crossPart_datums_keys[7]]),(datum_points[crossPart_datums_keys[8]],datum_points[crossPart_datums_keys[9]]),(datum_points[crossPart_datums_keys[10]],datum_points[crossPart_datums_keys[11]]),(datum_points[crossPart_datums_keys[12]],datum_points[crossPart_datums_keys[13]]),(datum_points[crossPart_datums_keys[14]],datum_points[crossPart_datums_keys[15]]))
Can someone help me to fix this?
Looking at Abaqus 6.14 Scripting Reference Guide, WirePolyLine has two arguments, points and mergeType. mergeWire and meshable are not listed as arguments. Perhaps you should use:
from abaqusConstants import SEPARATE
myPart.WirePolyLine(points = myPoints, mergeType = SEPARATE)
I strongly suggest you consult section 37.2.56 of Abaqus 6.14 Scripting Reference Guide.
dear friend, I am also a beginner in abacus, but I think the contents of this link can help you in this matter, the good thing is that it is free.
Caeassistant

Wrapping c++ functions in python with ctypes on windows : function not found

I need to run a series of python scripts calculating various scripts, that are working fine, but one of them runs very slowly and has to be done in C++.
The C++ code is ready, but I need to find a way to call the C++ function from the python and get the return value.
I found information about SWIG, but didn't get it to work on Windows with Visual Studio (I have to do it in VS on Windows, because of other constraints). I found ctypes much easier for my very simple function with standart input and output values.
So I did many tests with ctypes, tried all the examples I could find online, and each and every time, what happens is that I can load the dll (built with visual studio 2012 compiler) but when I try calling the function in python, it just goes :
Traceback (most recent call last):
File "test.py", line 3, in <module>
print maLib.plop()
File "C:\Python27\lib\ctypes\__init__.py", line 378, in __getattr__
func = self.__getitem__(name)
File "C:\Python27\lib\ctypes\__init__.py", line 383, in __getitem__
func = self._FuncPtr((name_or_ordinal, self))
AttributeError: function 'plop' not found
The python code is (test.py) :
from ctypes import cdll
lib = cdll.LoadLibrary('test')
print lib.plop()
The C++ code is (test.cpp, compiled as test.dll, Visual Studio set to build as dll) :
extern "C"
{
int plop();
}
int plop() { return 4; }
As you can see, I tried to make it as simple as possible, to avoid details making it fail. I have read the python ctypes help, and tutorials on how to use ctypes, trying exactly the same codes as them, but I had to adapt a bit because I am using Visual Studio / windows and most of the other users are using linux.
All of the files are in the same folder.
I have tried multiple ways of loading the library : LoadLibrary('./name.dll'), WinDLL('name.dll'), giving the function a different name, void return type, etc...
Do you think I should use SWIG ? Is my problem an obvious beginner mistake ? I am a student, and new to most of what I'm using, but I put a lot of effort in this, even if I just need it for a particular single task.
When using SWIG, I had to make a wrapper with function pointers, or references, so I thought this was the problem, which made me want to try a simpler solution.
I am using : Python 2.7.7, Visual Studio 2012, Windows 8.1
Many thanks in advance
The error reported by your Python is very clear.
function 'plop' not found
This means that the DLL does not export a function of that name. So, you need to export the function. Either with a .def file, or using __declspec(dllexport):
extern "C"
{
__declspec(dllexport) int plop();
}
To inspect your DLL to debug issues like this, use dumpbin or Dependency Walker.
Note that since you do not specify calling convention, the default of __cdecl is used. That means that cdll is correct on the ctypes side.
You need to give your function "C" linkage to avoid name mangling. Declare it like this:
extern "C"
{
int plop();
}
and it will then properly be called "plop" rather than being name-mangled.

Categories