C#: reflection-like mechanism into python file (get functions, parameters etc) [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
From a C# program, I want to examine a given python file for the functions it offers. If possible, I also want to get metainformation about each function and parameter, like a description.
I can control both the C# side and the Python side.
How can I achieve that?
(As for the use case: I am going to write a dispatcher function in Python that the C# program can call inside the python script to execute a specific function, given the required arguments.)

Never tried this. But think you could use a combination of the module optparse (Taken from How do I get list of methods in a Python class?):
from optparse import OptionParser
import inspect
inspect.getmembers(OptionParser, predicate=inspect.ismethod)
With function.__doc__ to get the documentation from the method like
def factorial(x):
'''int f(int x); returns the factorial of an integer number'''
#code here
>>> factorial.__doc__
'int f(int x); returns the factorial of an integer number'
Use the first part (int f(int x);) as the description of what parameters it needs and returns (You could take this part just taking the string until first semicolon)
And use getattr to call the function (https://docs.python.org/2/library/functions.html#getattr):
getattr(myObject, 'factorial', '3')()
Hope this helps you

Related

c# automatic memoization of method? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
Improve this question
Is there an attribute or capability in C# to automatically memoize static function results, based on input parameters, without writing all the logic for it myself?
For example in Python you can depend on functools:
import functools
#functools.lru_cache()
def my_function(param1):
...
Is there some similar package or built-in for C#?
[Memoizable]
public static MyFunction(MyType param1)
{
...
Results should be cached to avoid re-calculating for the same inputs.
Nope, does not exist. There are external tools that do so, but there is nothing automatic in C# for this. You will have to program it, if you need it, or use some external tool that does code rewrite.
public class SampleClass
{
private readonly IDictionary<int,int> _cache =
new Dictionary<int, int>();
public int Calculate(int input)
{
if (!_cache.ContainsKey(input))
{
int result = input*2;
_cache.Add(input,result);
}
return _cache[input];
}
}

Calling python script from C# (object-oriented approach) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
I want to run python script from C#(Visual Studio). I don't want to use C# process to do it, because I must have access to all of the python's modules, classes, and methods (I'd like to treat it as python object). I'm looking for sth like this:
"Python code"
"PyModule"
class PyClass:
def method:
print("Hello world!")
C# code
using PyModule.PyClass
PyClass.method()
I found python.net http://pythonnet.github.io/ , but they say that their solution is unverifiable.
I have to write it in Python 3, so IronPython isn't solution for me.
Do you know any solution that is similar to this?
Well, do have a look into IronPython as suggested in the comments. You can write your python script, save it to, say, greet.py
def greet(name):
return 'Hello ' + name + '!'
Now from C# you can do:
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
static void RunPythonScript()
{
var engine = Python.CreateEngine();
dynamic scope = engine.CreateScope();
engine.ExecuteFile("greeting.py", scope);
var greeting = scope.greet("John");
Console.WriteLine(greeting);
}
Result as you might expect is Hello John!

How to structure program that calculates math expression [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 7 years ago.
Improve this question
I plan to write a program or rather function which will be able to analyze a string parameter which in turn will be math expression. Only the 4 basic operations are allowed(addition, subtraction, multiplication and division) and the numbers are all whole numbers from -100 to 100. The result is allowed to be float. I know the registries work in the same way I.e calculate result of two numbers and store it, than calculate result of stored value and the next operant and store. And so forth until there are no operands left. The number of operands will usually be 2 but I will have a need of 3 or even more so yes, more operands is a requirement.
I was wondering how would you structure this in C#? What tools helper functions you would use in this scenario?
Note: I am working on Unity 5.1.4 project and I want to use a math parser in it. Unity is .NET 2.0
Note: This seems most promising: http://mono.1490590.n4.nabble.com/Javascript-eval-function-in-c-td1490783.html
It uses a variant of eval() function.
In .NET there are no some high level helper functions to help you with this. You would have to parse and tokenize the string in your code. There are however third party libraries that do what you need, for instance Expression Compiler, Simple Math Parser, Mathos Parser, and many other. Search for math expression parser.
If you want to make one from scratch you could look the code of existing ones.
Hans Passant mentions a simple solution, maybe just what you need. You get the result of the expression, so if you need just that, and not the actual expression tokens, then .NET got you covered.
This tool finished the job with no adding external references, dlls or what not: http://mono.1490590.n4.nabble.com/Javascript-eval-function-in-c-td1490783.html

C# VS 2013 how to call programs in DLL [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a Delphi created DLL that has functions I need to call from VS 2013 C3 app.
Not exactly sure how or where to start to accomplish this.
Do I have to include the dll as a reference or import it somehow or both?
And how do I call the program?
The dll is MSA.dll and the method I need to call looks like this:
GetXML(txtPath.Text, txtCabFile.Text, False);
Any 101 basic suggestion appreciated.
Check out something called "P/Invoke." It allows you to call into "native" (i.e. Delphi, C, etc.) DLLs using simple "extern" function definitions.
Here's a website I use as a resource for P/Invoke calls to the Windows API:
http://www.pinvoke.net/
EDIT: Make sure your target on the .NET side is the same as the one you compiled your Delphi library in. When in doubt, its probably x86 if you're on a PC. Thanks to the commenter below who brought this point to my attention. Don't use AnyCPU.
EDIT 2: The extern declaration you would use:
[DllImport("MSA.dll", CharSet = Ansi)]
public extern string GetXML(string firstParam, string secondParam, bool thirdParam);
You can name the parameters whatever you want. I didn't know what to call them so I just gave them names.

Use non-managed code (C) in c# [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm working on a project that combines a very large open source project (OSP) written in C and attempting to build a front end in C#. I'm currently compiling the OSP with Visual Studio 2012 Express and generating all the .exe's, .dll's,etc from this project.
How should I perform the integration? (Remember I have full access to the .h/.c files) I attempted using the IJW (It Just Works) method, but it didn't appear to allow me to import the references for my freshly compiled .dll's. Apart from that, I'm not sure how to execute the various functions within C# (apart from sending command line commands to the .exe's, which I would prefer not to do....)
Do I need to compile the OSP with special options/parameters for IJW, or would it require code rewrite?
For DLL written in C, you need to write the equivalent declarations in C# instead of adding reference to that DLL.
This is called PInvoke.
class ABC
{
[DllImport("abc.dll")]
public static extern int FuncX(int x, int y);
}

Categories