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()以轉換回多項式。現在工作正常,所以我傾向於不修復它,如果它沒有損壞。
感謝我正在尋找的東西。只是2件事:1.我看到的大部分類都使用'class Math(object):'而不是'class Math:'... 2.然後當你創建一個實例並使用它時,我不知道爲什麼它正在運行到正則表達式的問題,但現在這個實例似乎處理輸入OK ... – stackuser
@Rami:它可能會更好地使用標準的''self''變量 - 提高可讀性。 @stackuser:看來你所要做的就是將一些函數打包到一個類中。我沒有看到你的功能交換數據。所以爲什麼不使用靜態函數。例如: ''類MathOperations: @staticmethod 高清ID(LST): 回報[INT(VAL)%2我,VAL在枚舉(LST) '' 幾件事情要注意: 你可以調用如: MathOperations.id([1,2]) – goofd
對不起,格式搞砸了,但不想添加新評論 – goofd