如何将不同方法的参数从C#传递给python? [英] How to pass arguments on different method from C# to python?

查看:136
本文介绍了如何将不同方法的参数从C#传递给python?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#表单,有2个按钮。第一个按钮将生成一个矩阵,所以我需要填充一个名为rTb的文本框。 rTb文本框将获得一个矩阵范围的数字。如果rTb是10,那么矩阵将在-10到10的范围内随机生成。但该方法来自python脚本。

过程p =  new  Process();  //  创建过程(即python程序 
p.StartInfo.FileName = C:\\Python27 \\python.exe;
p.StartInfo .RedirectStandardOutput = true ;
p.StartInfo.UseShellExecute = false ; // 确保我们可以读取stdout的输出
p.StartInfo.Arguments = script.py + rTb.Text; // 使用两个参数启动python程序
p.Start(); // 启动进程(python程序)
StreamRea der s = p.StandardOutput;
String output = s.ReadToEnd();
string [] r = output.Split( new char [] {' '}); // 获取参数
string q = r [ 0 ];
string [] words = q.Split(' )。
mTb1.Text =单词[ 0 ];
mTb2.Text =字数[ 1 ];
mTb3.Text =字数[ 2 ];
mTb4.Text =单词[ 3 ];
mTb5.Text =字数[ 4 ];
mTb6.Text =单词[ 5 ];
mTb7.Text =单词[ 6 ];
mTb8.Text =字数[ 7 ];
mTb9.Text =字数[ 8 ];
p.WaitForExit();





和python脚本

从随机导入randrange导入sys 



#检查get_determinant是否为1或-1
def get_determinant(key):
result =(key [0] *((键[4] *键[8]) - (键[5] *键[7]))) - \
(键[1] *((键[3] *键[8]) - (键[5] *键[6])))+ \
(键[2] *((键[3] *键[7]) - (键[4] * key [6])))
返回结果


#生成带键范围的随机矩阵,get_determinant必须为1或-1
#for singularity
key_range = int(sys.argv [1])$ ​​b $ ba = 0
而a == 0:
key_matrix = []
for i in range(9):key_matrix .append(randrange(-key_range,key_range))
如果get_determinant(key_matrix)== 1或get_determinant(key_matrix)== -1:在key_matrix中为$断开
m =''

m = m + str(i)+'。'
print m + str(get_determinant(key_matrix))



C#表单输出没问题。

问题是我用不同的按钮做它,称为RSA按钮,它会随机生成p,q,e和将其设置为每个文本框。但我需要填充digitTb的p,q密钥长度。说2,所以p,q数字将得到ex:12,43,23全部为2位数。但当我通过论证时

过程p = 过程();  //  创建过程(即python程序 
p.StartInfo.FileName = C:\\Python27 \\python.exe;
p.StartInfo .RedirectStandardOutput = true ;
p.StartInfo.UseShellExecute = false ; // 确保我们可以读取stdout的输出
p.StartInfo.Arguments = script2.py + digitTb.text; // 使用两个参数启动python程序
p.Start(); // 启动进程(python程序)
Stream读者s = p.StandardOutput;
String output = s.ReadToEnd();
string [] r = output.Split( new char [] {' '}); // 获取参数
string q = r [ 0 ];
string [] words = q.Split(' )。
pTb.Text =单词[ 0 ];
qTb.Text =字数[ 1 ];
eTb.Text =字数[ 2 ];
p.WaitForExit();





和python script2



  import  sys 
来自 random import randrange
来自分数 import gcd

lehmann primality check method
def lehmann(p):
a = randrange( 1 ,p)
res =(a **( (p - 1 )/ 2))%p
如果 res == 1 res == p - 1 return True
else 返回 错误

使用lehmann检查参数的随机数p givren
将将p作为素数返回,如果p是复合则不返回
def check_prime(p):
error = []
i 范围内( 10 ):
如果 lehmann(p)== False
error.append(< span class =code-digit> 0 )
pass
else :error.append( 1
i = 0
e erro中的class =code-keyword> r:
i + = 1
如果 e!= 1 break
如果 i == len(错误): return p

a = 10 ** str(sys.argv [ 1 ])
生成2个素数唯一p的数字,q

prime = []

len(素数)! = 2
result = check_prime(randrange( 50 ,a))
if result!= :prime.append(result)

RSA Scheme
p = prime [ 0 ]
q = prime [ 1 ]
n = p * q
totient =(p - 1 )*(q - 1

选择e
e = []
i 范围内( 2 ,n):
如果 gcd(i,totient)== 1 :e.append(i)
e = e [ 0 ]
m = ' '
for i prime中的code-keyword>:
m = m + str(i)+ ' 。'
print m + str(e)



错误说指数数组的边界之外。我认为论证不能传递给python脚本。如果有人帮忙,我将不胜感激。

解决方案

在script2.py之后你可能需要空格

 p。 StartInfo.Arguments =   script2.py + digitTb.text;  //  使用两个参数启动python程序 



并添加一些验证

  string  [] words = q.Split(' 。'); 
if (words.Length> 0)
pTb.Text = words [ 0 ];
if (words.Length> 1)
qTb.Text = words [ 1 ];
if (words.Length> 2)
eTb.Text = words [ 2 ];


I have a C# form which has 2 buttons. The first button will generate a matrix , so i need to fill a textbox called rTb. rTb textbox will get a number which be the matrix range. If rTb is 10, so the matrix will randomly generate in range from -10 to 10.But the method is from python script.

Process p = new Process(); // create process (i.e., the python program
                p.StartInfo.FileName = "C:\\Python27\\python.exe";
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.UseShellExecute = false; // make sure we can read the output from stdout
                p.StartInfo.Arguments = "script.py " + rTb.Text; // start the python program with two parameters
                p.Start(); // start the process (the python program)
                StreamReader s = p.StandardOutput;
                String output = s.ReadToEnd();
                string[] r = output.Split(new char[] { ' ' }); // get the parameter
                string q = r[0];
                string[] words = q.Split('.');
                mTb1.Text = words[0];
                mTb2.Text = words[1];
                mTb3.Text = words[2];
                mTb4.Text = words[3];
                mTb5.Text = words[4];
                mTb6.Text = words[5];
                mTb7.Text = words[6];
                mTb8.Text = words[7];
                mTb9.Text = words[8];
                p.WaitForExit();



and the python script

import sys
from random import randrange


# check the get_determinant if 1 or -1
def get_determinant(key):  
    result = (key[0] * ((key[4] * key[8]) - (key[5] * key[7]))) - \
        (key[1] * ((key[3] * key[8]) - (key[5] * key[6]))) + \
        (key[2] * ((key[3] * key[7]) - (key[4] * key[6])))
    return result


# generate random matrix with key range and get_determinant must either 1 or -1 
# for singularity
key_range = int(sys.argv[1])
a = 0
while a == 0:
    key_matrix = []
    for i in range (9): key_matrix.append(randrange(-key_range,key_range))
    if get_determinant(key_matrix) == 1 or get_determinant(key_matrix) == -1: break   
m = ''
for i in key_matrix:
    m = m + str(i) + '.'
print m + str(get_determinant(key_matrix))


No problem with the output in the C# form.
The problem is when i do it with different button, called it RSA button, it'll randomly generate p, q, e, and set it to each textbox. But i need to fill the digitTb for p, q key length. Says it 2, so the p, q digit will get ex: 12,43,23 all in 2 digits. but when i pass the argument

Process p = new Process(); // create process (i.e., the python program
                p.StartInfo.FileName = "C:\\Python27\\python.exe";
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.UseShellExecute = false; // make sure we can read the output from stdout
                p.StartInfo.Arguments = "script2.py" + digitTb.text;// start the python program with two parameters
                p.Start(); // start the process (the python program)
                StreamReader s = p.StandardOutput;
                String output = s.ReadToEnd();
                string[] r = output.Split(new char[] { ' ' }); // get the parameter
                string q = r[0];
                string[] words = q.Split('.');
                pTb.Text = words[0];
                qTb.Text = words[1];
                eTb.Text = words[2];
                p.WaitForExit();



and the python script2

import sys
from random import randrange
from fractions import gcd

# lehmann primality check method
def lehmann(p):
    a = randrange(1, p)
    res = (a ** ((p - 1)/2)) % p
    if res == 1 or res == p - 1: return True
    else: return False

# check a random number p givren on parameter with lehmann
# will return p as prime and none if p is composite
def check_prime(p):  
    error = []    
    for i in range(10):
        if lehmann(p) == False: 
            error.append(0) 
            pass
        else: error.append(1)
    i = 0
    for e in error:
        i += 1
        if e != 1: break
        if i == len(error): return p

a = 10**str(sys.argv[1])
# generate 2 prime number for unique p, q	

prime = []

while len(prime) != 2:
    result = check_prime(randrange(50,a))
    if result != None: prime.append(result)
    
# RSA Scheme	
p = prime[0]
q = prime[1]
n = p * q
totient = ( p - 1 ) * ( q - 1 )

# choose e
e = []
for i in range( 2, n ): 
    if gcd ( i, totient ) == 1: e.append( i )
e = e[0]   
m = ''
for i in prime:
    m = m + str(i) + '.'
print m + str(e)


the error says "Index was outside the bounds of the array". i think the argument cant pass to python script. i will appreciate if anyone help.

解决方案

you may need space after script2.py

p.StartInfo.Arguments = "script2.py " + digitTb.text;// start the python program with two parameters


and also add few validations

string[] words = q.Split('.');
if(words.Length>0) 
  pTb.Text = words[0];
if(words.Length>1) 
   qTb.Text = words[1];
if(words.Length>2) 
  eTb.Text = words[2];


这篇关于如何将不同方法的参数从C#传递给python?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆