2013-06-28 53 views
1

我正在研究一组函数,这些函数可能更容易在模块中自行管理。但是,除了我的薪酬等级外,还有其他原因想要考虑将其变成一个班级。但是,由于它是从一个多项式输入作为字符串转换而来的,正则表达式对一个字符串起作用,但是一旦输入是一个类实例,它就决定在这之后退出。Python - class vs module

所以我的问题是如何把它变成一个类,并仍然保持功能(如何初始化它等)。或者如果这种情况更适合作为一个模块,那么我可以争辩说,但我需要充分理解。我检查了这些了,但我真的不知道我得到了足够他们来解决这个问题:

http://docs.python.org/2/tutorial/classes.html

organising classes and modules in python

import re 

def id(lst): #returns modulus 2 (1,0,0,1,1,....) for input lists 
    return [int(lst[i])%2 for i in range(len(lst))] 

def listToInt(lst): #converts list to integer for later use 
    result = id(lst) 
    return int(''.join(map(str,result))) 

def parsePolyToListInput(poly): 
    c = [int(i.group(0)) for i in re.finditer(r'\d+', poly)] #re.finditer returns an iterator 
    return [1 if x in c else 0 for x in xrange(max(c), -1, -1)] 

def prepBinary(x,y): #converts to base 2 and orders min and max for use 
    x = parsePolyToListInput(x); y = parsePolyToListInput(y) 
    a = listToInt(x); b = listToInt(y) 
    bina = int(str(a),2); binb = int(str(b),2) 
    #a = min(bina,binb); b = max(bina,binb); 
    return bina,binb #bina,binb are binary values like 110100101100..... 

def add(a,b): # a,b are GF(2) polynomials like x**7 + x**3 + x**0 .... 
    bina,binb = prepBinary(a,b) 
    return outFormat(bina^binb) #returns binary string 

def subtract(x,y): # same as addition in GF(2) 
    return add(x,y) 

def multiply(a,b): # a,b are GF(2) polynomials like x**7 + x**3 + x**0 .... 
    a,b = prepBinary(a,b) 
    return outFormat(a*b) #returns product of 2 polynomials in gf2 

def divide(a,b): #a,b are GF(2) polynomials like x**7 + x**3 + x**0 .... 
    a,b = prepBinary(a,b) 
    #bitsa = "{0:b}".format(a); bitsb = "{0:b}".format(b) 
    return outFormat(a/b),outFormat(a%b) #returns remainder and quotient formatted as polynomials 

def quotient(a,b): #separate quotient function for clarity when calling 
    return divide(a,b)[1] 

def remainder(a,b): #separate remainder function for clarity when calling 
    return divide(a,b)[0] 

def outFormat(raw): # process resulting values into polynomial format 
    raw = "{0:b}".format(raw); raw = str(raw[::-1]); g = [] #reverse binary string for enumeration 
    g = [i for i,c in enumerate(raw) if c == '1'] 
    processed = "x**"+" + x**".join(map(str, g[::-1])) 
    if len(g) == 0: return 0 #return 0 if list empty 
    return processed #returns result in gf(2) polynomial form 

def extendedEuclideanGF2(a,b): # extended euclidean. a,b are values 10110011... in integer form 
    inita,initb=a,b; x,prevx=0,1; y,prevy = 1,0 
    while b != 0: 
     q = int("{0:b}".format(a//b),2) 
     a,b = b,int("{0:b}".format(a%b),2); 
     x,prevx = (int("{0:b}".format(prevx-q*x)), int("{0:b}".format(x,2))); y,prevy=(prevy-q*y, y) 
    #print("%d * %d + %d * %d = %d" % (inita,prevx,initb,prevy,a)) 
    return a,prevx,prevy # returns gcd of (a,b), and factors s and t 

def modular_inverse(a,mod): # a,mod are GF(2) polynomials like x**7 + x**3 + x**0 .... 
    a,mod = prepBinary(a,mod) 
    bitsa = int("{0:b}".format(a),2); bitsb = int("{0:b}".format(mod),2) 
    #return bitsa,bitsb,type(bitsa),type(bitsb),a,mod,type(a),type(mod) 
    gcd,s,t = extendedEuclideanGF2(a,mod); s = int("{0:b}".format(s)) 
    initmi = s%mod; mi = int("{0:b}".format(initmi)) 
    print ("%d * %d mod %d = 1"%(a,initmi,mod)) 
    if gcd !=1: return outFormat(mi),False 
    return outFormat(mi) # returns modular inverse of a,mod 


a = "x**14 + x**1 + x**0"; b = "x**6 + x**2 + x**1" 
c = "x**2 + x**1 + x**0"; d = "x**3 + x**1 + x**0" 
e = "x**3 + x**2 + x**1 + x**0"; f = "x**2"; g = "x**1 + x**0" 
p = "x**13 + x**1 + x**0"; q = "x**12 + x**1" 
print "add: [%s] + [%s] = %s "%(a,b,add(a,b)) 
print "add: [%s] + [%s] = %s "%(c,d,add(c,d)) 
print "multiply: [%s] * [%s] = %s "%(a,b,multiply(a,b)) 
print "multiply: [%s] * [%s] = %s "%(c,d,multiply(c,d)) 
print "multiply: [%s] * [%s] = %s "%(f,g,multiply(f,g)) 
print "quotient (max(a,b)/min(a,b): [%s]/[%s] = %s "%(a,b,quotient(a,b)) 
print "quotient (max(a,b)/min(a,b): [%s]/[%s] = %s "%(c,d,quotient(c,d)) 
print "remainder (max(a,b) mod min(a,b)): [%s] mod [%s] = %s "%(a,b,remainder(a,b)) 
print "remainder (max(a,b) mod min(a,b): [%s] mod [%s] = %s "%(c,d,remainder(c,d)) 
valuemi1 = modular_inverse(a,b) 
print "modular_inverse: [%s] * [%s] mod [%s] = 1 [%s]"%(a,valuemi1[0],b,valuemi1[1]) 
valuemi2 = modular_inverse(p,q) 
print "modular_inverse: [%s] * [%s] mod [%s] = 1 [%s]"%(p,valuemi2[0],q,valuemi2[1]) 

每个算术运算只需要在GF(2)字符串格式的多项式然后将其解析为相应的二进制值,然后将其发送给outFormat()以转换回多项式。现在工作正常,所以我倾向于不修复它,如果它没有损坏。

回答

1

我只是个新手Python开发的,但如果我理解你的问题正确,那么你需要的代码是这样的:

import re 
class MathOperations: 
     def id(_self, lst): #returns modulus 2 (1,0,0,1,1,....) for input lists 
      return [int(lst[i])%2 for i in range(len(lst))] 

,然后你可以使用类:

obj = MathOperations() 
obj.id([1, 2]) 
+0

感谢我正在寻找的东西。只是2件事:1.我看到的大部分类都使用'class Math(object):'而不是'class Math:'... 2.然后当你创建一个实例并使用它时,我不知道为什么它正在运行到正则表达式的问题,但现在这个实例似乎处理输入OK ... – stackuser

+0

@Rami:它可能会更好地使用标准的''self''变量 - 提高可读性。 @stackuser:看来你所要做的就是将一些函数打包到一个类中。我没有看到你的功能交换数据。所以为什么不使用静态函数。例如: ''类MathOperations: @staticmethod 高清ID(LST): 回报[INT(VAL)%2我,VAL在枚举(LST) '' 几件事情要注意: 你可以调用如: MathOperations.id([1,2]) – goofd

+0

对不起,格式搞砸了,但不想添加新评论 – goofd