郁闷 !多模块全局变量调用问题
appletian520
|
1#
appletian520 发表于 2008-04-15 15:12
郁闷 !多模块全局变量调用问题
#####################################################
#test1.py # -*- coding: cp936 -*- import encode s=Rand() s.ranseqs() s.encode("host='.',user='sa',password='',databse='THIS4_08'",'c:\\test.ini') host,user,password,database=s.decode('c:\\test.ini') print host,user,password,database Traceback (most recent call last): File "<pyshell#3>", line 1, in <module> s.decode("c:\\dbconnect.ini" File "D:\test\db_connect\encode.py", line 117, in decode t=s.arry1[t][n] NameError: global name 's' is not defined 不知道怎末申明全局变量 这样可以在别的模块里面可以调用! ##################################################### #encode.py # -*- coding: cp936 -*- class Rand(): #global s def __init__(self, x=None): self.SS=256 self.seed(x) def seed(self,x=None): if x==None=[1168766482222222,251315447555555,313164684666666] #if x==None=[116876648,251315447,313164684] self.x,self.y,self.z=x import zlib self.first= zlib.crc32(str(x[0])+str(x[1])+str(x[2]))%self.SS def rand(self): t=int('100000000000000',16) #t=int('100000000',16) self.x,self.y=self.y,((self.x+self.y)^self.z)%t return self.y def ranseq(self): t=[] while len(t)!=self.SS: k=0 while len(t)!=self.SS: k+=1 if k>self.SS*3:break m=self.rand() if m not in t: t+=[m] self.x+=3 return t def ranseqs(self): #global s t=range(self.SS) k=[] m=[] for i in t: z=self.ranseq() b=map(lambda x:list(x),zip(t,z)) b=sorted(b,cmp=lambda x,y: cmp(x[1], y[1])) tn=[i[0] for i in b] k+=[tn] mm=[1]*self.SS for j in t: mm[tn[j]]=j m+=[mm] self.arry=k[:] self.arry1=m[:] self.test() def test(self): import random tt=range(self.SS) t=tt[:] random.shuffle(t) dt=t[:] dd=[1]*self.SS dg=dd[:] m=123 for i in tt: dd=self.arry[m][dt] m=dd m=123 for i in tt: dg=self.arry1[m][dd] m=dd assert dg==dt,"数组无效" ## def encode(self,s1,s2): ## t=self.first ## f1=open(s1,"rb" ## f2=open(s2,"wb" ## a1=f1.read() ## m='' ## for i in list(a1): ## t=s.arry[t][ord(i)] ## m+=chr(t) ## f2.write(m) ## f1.close() ## f2.close() def encode(self,s1,s2): t=self.first #f1=open(s1,"rb" f2=open(s2,"wb" a1=s1 m='' for i in list(a1): t=s.arry[t][ord(i)] m+=chr(t) f2.write(m) #f1.close() f2.close() ## def decode(self,s1,s2): ## t=self.first ## f1=open(s1,"rb" ## f2=open(s2,"wb" ## a1=f1.read() ## m='' ## for i in list(a1): ## n=ord(i) ## t=s.arry1[t][n] ## m+=chr(t) ## t=n ## f2.write(m) ## f1.close() ## f2.close() def decode(self,s1): #global s t=self.first f1=open(s1,"rb" #f2=open(s2,"wb" a1=f1.read() m='' c=[] for i in list(a1): n=ord(i) t=s.arry1[t][n] m+=chr(t) t=n #以下改进过取返回字符串里面的=后右边的字符 vaule=map(str,m.split(',')) left0,step0,right0=vaule[0].partition("=" left1,step1,right1=vaule[1].partition("=") left2,step2,right2=vaule[2].partition("=") left3,step3,right3=vaule[3].partition("=") return right0,right1,right2,right3 f1.close() #f2.close() #if __name__ == '__main__': # s=Rand() # s.ranseqs() # s.encode("host='.',user='sa',password='',databse='THIS4_08'",'c:\\test.ini') # # host,user,password,database=s.decode('c:\\test.ini') #print host,user,password,database |