Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 是否将sage工作表作为脚本运行?_Python_Input_Console_Worksheet_Sage - Fatal编程技术网

Python 是否将sage工作表作为脚本运行?

Python 是否将sage工作表作为脚本运行?,python,input,console,worksheet,sage,Python,Input,Console,Worksheet,Sage,大家好,我已经用sageworksheet格式编写了一个代码,它在sagecloud上运行得很好,很流畅,但我有一个问题:我想让它更具交互性,这样用户可以自己输入参数,所以我愿意将其转换成sage脚本或pyhton脚本,我已经在我的ubuntu机器上安装了sage,同样,代码在笔记本上运行,但在控制台上不运行。它在P.x上给了我一些语法错误。同样,如果我尝试将其作为python脚本运行,我只想将其作为python脚本或Sage脚本运行,这样我就可以使用输入函数要求用户输入参数!代码如下:

大家好,我已经用sageworksheet格式编写了一个代码,它在sagecloud上运行得很好,很流畅,但我有一个问题:我想让它更具交互性,这样用户可以自己输入参数,所以我愿意将其转换成sage脚本或pyhton脚本,我已经在我的ubuntu机器上安装了sage,同样,代码在笔记本上运行,但在控制台上不运行。它在P.x上给了我一些语法错误。同样,如果我尝试将其作为python脚本运行,我只想将其作为python脚本或Sage脚本运行,这样我就可以使用输入函数要求用户输入参数!代码如下:

    P.<x> = PolynomialRing(ZZ); 

def bezout(f,g):    
    P.<x> = PolynomialRing(QQ)
    f = f+x-x
    g = g+x-x
    e=xgcd(f,g)
    gcd=e[0]
    u=e[1]
    v=e[2]
    return (u,v,gcd)

def polymod(f,q):  
    P.<x> = PolynomialRing(QQ)
    f = f
    c = f.coefficients(sparse=False)
    N = len(c)
    for i in range(N):
        c[i] = Rational(c[i]).mod_ui(q);
    p = sum(c[i]*(x^(i)) for i in range(N));
    return p

def center(f,q): 
    u = q/2
    v = -u
    c = f.coefficients(sparse=False)
    N = len(c)
    for i in range(N):
        if c[i]<v:
            c[i] = c[i] + q;
        elif c[i]>u:
            c[i] = c[i] - q;
        else:
            c[i] = c[i];
    p = sum(c[i]*(x^(i)) for i in range(N));
    return p


class Ntru:           
    N = None
    p = None
    q = None
    d = None
    f = None
    g = None
    h = None
    fp = None
    fq = None
    Phi = None

    def __init__(self,N,p,q,d):  
        self.N = N
        self.p = p
        self.q = q
        self.d = d
        v = self.N
        self.Phi = x^v -1


    def test(self): 
        if not is_prime(self.N):
            print "N n est pas premier, pensez a changer ce parametre"
            return False
        if gcd(self.N,self.p) != 1:
            print "N et p ne sont pas premiers entre eux, pensez a changer ces parametres"
            return False
        if gcd(self.N,self.q) != 1:
            print "N et q ne sont pas premiers entre eux, pensez a changer ces parameres"
            return False
        if self.q <= (6*self.d+1)*self.p :
            print "q doit etre superieur a (6d+1)*p "
            return False
        return True


    def genPublicKey(self,f_new,g_new):   

        self.f = f_new
        self.g = g_new
        (b_f,b_phi,bz) = bezout(self.f,self.Phi)
        self.fp = polymod(b_f,self.p)
        self.fq = polymod(b_f,self.q)
        self.h = polymod((self.fq*self.g).quo_rem(self.Phi)[1],self.q)
        if not self.test():
            print "le cryptage ne peut s effectuer avec ces parametres !"
            quit()

    def encrypt(self,message,rand): 
        if self.h!=None:
            temp=(self.p*rand*self.h + message).quo_rem(self.Phi)[1]
            e=polymod(temp,self.q)
            return e
        else:
            print "Impossible de faire le cryptage : la cle n a pas encore ete generee"
            print "Veuillez en generer une avec la fonction genPublicKey"

    def decrypt(self,encryptedMsg): 
        a = (self.f*encryptedMsg).quo_rem(self.Phi)[1]
        a = polymod(a,self.q)
        a = center(a,self.q)
        tmp = (self.fp*a).quo_rem(self.Phi)[1]
        m=polymod(tmp,self.p)
        return m



NTRU=Ntru(167,3,128,3) 
f = 1+x-x^2-x^3-x^4+x^5+x^6 
g = -1+x^2+x^3+x^4-x^5-x^6

NTRU.f = f 
NTRU.g = g
NTRU.genPublicKey(f,g)
print "La cle publique est : ",NTRU.h 

msg = 1+x+x^4+x^5+x^6 
rand = -1 -x + x^2 + x^3 - x^4 + x^6 

enc = NTRU.encrypt(msg,rand)
print "Le Message crypte est : ",enc   
dec = NTRU.decrypt(enc) 
print "Le Message dechiffre est : ",dec 
print "Le Message en clair etait : ",msg

谢谢大家!

对于python脚本,您可以从底部的代码开始,并定义一个函数作为主要入口点,如:

def main():
    NTRU=Ntru(167,3,128,3) 
    f = 1+x-x^2-x^3-x^4+x^5+x^6 
    g = -1+x^2+x^3+x^4-x^5-x^6

    NTRU.f = f 
    NTRU.g = g
    NTRU.genPublicKey(f,g)
    print "La cle publique est : ",NTRU.h 

    msg = 1+x+x^4+x^5+x^6 
    rand = -1 -x + x^2 + x^3 - x^4 + x^6 

    enc = NTRU.encrypt(msg,rand)
    print "Le Message crypte est : ",enc   
    dec = NTRU.decrypt(enc) 
    print "Le Message dechiffre est : ",dec 
    print "Le Message en clair etait : ",msg
然后在这个代码块之后,您需要告诉python您希望它运行什么代码,就像它是一个脚本一样:

if __name__ == "__main__":
    main()
如果您想从ubuntu命令行将其作为可执行文件运行,我建议在顶部添加一个shebang,如:

#!/bin/python  //this should be the path to your python executable
如果您对向脚本添加命令行参数以供用户输入感兴趣,我建议查看python附带的argParse库:

对于python 3 对于python 2.7

不幸的是,我不熟悉圣人;我希望这能让你从正确的方向开始