how to use enum in swig with python?

2024/10/7 4:35:12

I have a enum declaration as follows:

typedef enum mail_ {Out = 0,Int = 1,Spam = 2
} mail;

Function:

mail status;
int fill_mail_data(int i, &status);

In the function above, status gets filled up and will send.

When I am trying this through swig I am facing the following issues:

  1. It is not showing details of mail. When I try to print mail.__doc__ or help(mail), it is throwing an error saying there is no such Attribute, though though i am able to use those values (Spam, In, and Out).
  2. As shown above, the Swig does not know what main is, so it is not accepting any function arguments for that mail.
Answer

To SWIG, an enum is just an integer. To use it as an output parameter as in your example, you also can declare the parameter as an output parameter like so:

%module x// Declare "mail* status" as an output parameter.
// It will be returned along with the return value of a function
// as a tuple if necessary, and will not be required as a function
// parameter.
%include <typemaps.i>
%apply int *OUTPUT {mail* status};%inline %{typedef enum mail_ {Out  = 0,Int  = 1,Spam = 2
} mail;int fill_mail_data(int i, mail* status)
{*status = Spam;return i+1;
}%}

Use:

>>> import x
>>> dir(x)    # Note no "mail" object, just Int, Out, Spam which are ints.
['Int', 'Out', 'Spam', '__builtins__', '__cached__', '__doc__', '__file__', '__initializing__', '__loader__', '__name__', '__package__', '_newclass', '_object', '_swig_getattr', '_swig_property', '_swig_repr', '_swig_setattr', '_swig_setattr_nondynamic', '_x', 'fill_mail_data']
>>> x.fill_mail_data(5)
[6, 2]
>>> ret,mail = x.fill_mail_data(5)
>>> mail == x.Spam
True
https://en.xdnf.cn/q/70281.html

Related Q&A

What does except Exception as e mean in python? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 4…

Numpy efficient big matrix multiplication

To store big matrix on disk I use numpy.memmap.Here is a sample code to test big matrix multiplication:import numpy as np import timerows= 10000 # it can be large for example 1kk cols= 1000#create some…

Basic parallel python program freezes on Windows

This is the basic Python example from https://docs.python.org/2/library/multiprocessing.html#module-multiprocessing.pool on parallel processingfrom multiprocessing import Pooldef f(x):return x*xif __na…

Formatting multiple worksheets using xlsxwriter

How to copy the same formatting to different sheets of the same Excel file using the xlsxwriter library in Python?The code I tried is: import xlsxwriterimport pandas as pd import numpy as npfrom xlsxw…

Good python library for generating audio files? [closed]

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines. It is not currently accepting answers.We don’t allow questi…

Keras ConvLSTM2D: ValueError on output layer

I am trying to train a 2D convolutional LSTM to make categorical predictions based on video data. However, my output layer seems to be running into a problem:"ValueError: Error when checking targe…

debugging argpars in python

May I know what is the best practice to debug an argpars function.Say I have a py file test_file.py with the following lines# Script start import argparse import os parser = argparse.ArgumentParser() p…

Non-blocking server in Twisted

I am building an application that needs to run a TCP server on a thread other than the main. When trying to run the following code:reactor.listenTCP(ServerConfiguration.tcpport, TcpCommandFactory()) re…

Reading changing file in Python 3 and Python 2

I was trying to read a changing file in Python, where a script can process newly appended lines. I have the script below which prints out the lines in a file and does not terminate.with open(tmp.txt,r)…

How to remove timestamps from celery pprint output?

When running the celery worker then each line of the output of the pprint is always prefixed by the timestamp and also is being stripped. This makes it quite unreadable:[2015-11-05 16:01:12,122: WARNIN…