Débutant en python
Le
DAIREAUX Jean-Baptiste
Bonjour je suis encore novice en python (pas en dev)
j'ai un module qui fonctionne mais que je trouve pas trés propre.
pourai-je avoir quelque conseille pour le néttoyer ? (si c'est possible)
J.B.D.
le code -
lazy.py
# -*- coding: cp1252 -*-
import sys
import types
TRACEúlse #affiche ou pas des info de traçage
if TRACE :
def iprint(st):
print(st)
else :
def iprint(st):
pass
def run(k,uv):
u=uv[0]
v=uv[1]
st=["u["+str(x)+"]" for x in xrange(len(u))]
st2=[ str(sk)+"="+repr(sv) for sk,sv in v.iteritems() ]
s=",".join(st+st2)
sf="lambda(z): k("+s+")"
dic1=globals().copy()
dic1.update({'k':k ,'u':u,'v':v})
f=eval(sf,dic1,None)
return f('&')
def wait(k,*a,**b):
yield run(k,[a,b])
###############################
#Gen ne sert pas. Ce fut une fonction ou j'ai cherché des solutions a
diverse pbm
def gen(k):
a=k.func_code.co_argcount
if k.func_code.co_flags & 0x04 :
b=a+1
else :
b=a
if k.func_code.co_flags & 0x08 :
c=b+1
else :
c=b
print "nb argument : "+str(a)
print "nom des argument : "+str(k.func_code.co_varnames[0:a])
####################################"
class continuation(object):
"""
Une instance de continuation est crée par l'appel a la fonction décorer
par @lazy
l'objet attend une quelconque utilisation pour s'évaluer et renvoyer la
réponse adéquate.
"""
st=["_oo_","_calc_","_resu_"]
def __init__(self,o):
#self._oo_=o
object.__setattr__(self,'_oo_',o)
#self._calc_úlse
object.__setattr__(self,'_calc_',False)
#self._resu_=None
object.__setattr__(self,'_resu_',None)
def _calcule(self) :
if not self._calc_ :
iprint("Calcule de la valeur")
a=self._oo_.next()
#self._resu_=a
object.__setattr__(self,'_resu_',a)
#self._calc_=True
object.__setattr__(self,'_calc_',True)
iprint( "fin calcule valeur")
def __repr__(self):
iprint( "repr")
self._calcule()
return repr(self._resu_)
def __getattr__(self,name):
iprint ("getattr : "+str(name))
self._calcule()
st="lambda (u): u."+name
#a=eval(st,globals(),locals())
a=eval(st,None,None)
return a(self._resu_)
def __setattr__( self, name, value) :
#if name in continuation.st :
# object.__setattr__(self, name, value)
#else :
iprint( "setattr : "+str(name)+"="+str(value))
self._calcule()
self._resu_.__setattr__(name, value)
def __nonzero__(self):
iprint ("nonzero")
self._calcule()
return self._resu_.__nonzero__()
def __call__(self,*a,**b) :
iprint( "call")
self._calcule()
return run(self._resu_,[a,b])
def __str__(self) :
iprint( "str")
self._calcule()
return str(self._resu_)
def __lt__( self, other) :
iprint ("<")
self._calcule()
return self._resu_<other
def __le__( self, other) :
iprint ("<=")
self._calcule()
return self._resu_<=other
def __eq__( self, other) :
iprint ("==")
self._calcule()
return self._resu_==other
def __ne__( self, other) :
iprint ("!= ou <>")
self._calcule()
return self._resu_!=other
def __gt__( self, other) :
iprint (">")
self._calcule()
return self._resu_>other
def __ge__( self, other) :
iprint (">=")
self._calcule()
return self._resu_>=other
def __hash__( self) :
iprint ("hash")
self._calcule()
return self._resu_.__hash__()
def __unicode__( self) :
iprint ("unicode")
self._calcule()
return self._resu_.__unicode__()
def __iter__( self ) :
iprint ("iter")
self._calcule()
return self._resu_.__iter__()
def __getitem__( self, key) :
iprint ("getitem "+str(key))
self._calcule()
return self._resu_[key]
def __setitem__( self, key, value) :
iprint ("setitem")
self._calcule()
self._resu_[key]=value
def __delitem__( self, key) :
iprint ("delitem")
self._calcule()
self._resu_.__delitem__(key)
def __contains__( self, item) :
iprint ("contains")
self._calcule()
return self._resu_.__contains__(item)
def __len__(self) :
iprint ("len")
self._calcule()
return self._resu_.__len__()
if sys.version_info < (2, 0):
# They won't be defined if version is at least 2.0 final
def __getslice__(self, i, j):
iprint ("getslice")
self._calcule()
return self._resu_.__getslice__(i,j)
def __setslice__(self, i, j, seq):
iprint ("setslice")
self._calcule()
self._resu_.__setslice__(i,j,seq)
def __delslice__(self, i, j):
iprint ("delslice")
self._calcule()
self._resu_.__delslice__(i,j,seq)
def __add__( self, other) :
iprint ("add")
self._calcule()
return self._resu_ + other
def __sub__( self, other) :
iprint ("sub")
self._calcule()
return self._resu_ - other
def __mul__( self, other) :
iprint ("mul")
self._calcule()
return self._resu_ * other
def __floordiv__( self, other) :
iprint ("floordiv")
self._calcule()
return self._resu_ // other
def __mod__( self, other) :
iprint ("mod")
self._calcule()
return self._resu_ % other
def __divmod__( self, other) :
iprint ("divmod")
self._calcule()
return divmod(self._resu_ , other)
def __pow__( self, other, modulo=None) :
iprint ("pow")
self._calcule()
return pow(self._resu_ , other ,modulo)
def __lshift__( self, other) :
iprint ("lshift")
self._calcule()
return self._resu_ << other
def __rshift__( self, other) :
iprint ("rshift")
self._calcule()
return self._resu_ >> other
def __and__( self, other) :
iprint ("and")
self._calcule()
return self._resu_ & other
def __xor__( self, other) :
iprint ("xor")
self._calcule()
return self._resu_ ^ other
def __or__( self, other) :
iprint ("or")
self._calcule()
return self._resu_ | other
def __div__( self, other) :
iprint ("div")
self._calcule()
return self._resu_ / other
def __truediv__( self, other) :
iprint ("truediv")
self._calcule()
return self._resu_ / other
def __radd__( self, other) :
iprint("radd")
self._calcule()
return other + self._resu_
def __rsub__( self, other) :
iprint ("rsub")
self._calcule()
return other - self._resu_
def __rmul__( self, other) :
iprint ("rmul")
self._calcule()
return other * self._resu_
def __rdiv__( self, other) :
iprint ("rdiv")
self._calcule()
return other / self._resu_
def __rtruediv__( self, other) :
iprint ("rtruediv")
self._calcule()
return other / self._resu_
def __rfloordiv__( self, other) :
iprint( "rfloordiv")
self._calcule()
return other // self._resu_
def __rmod__( self, other) :
iprint ("rmod")
self._calcule()
return other % self._resu_
def __rdivmod__( self, other) :
iprint ("rdivmod")
self._calcule()
return divmod(other , self._resu_)
def __rpow__( self, other) :
iprint ("rpow")
self._calcule()
return other ** self._resu_
def __rlshift__( self, other) :
iprint ("rlshift")
self._calcule()
return other << self._resu_
def __rrshift__( self, other) :
iprint ("rrshift")
self._calcule()
return other >> self._resu_
def __rand__( self, other) :
iprint ("rand")
self._calcule()
return other & self._resu_
def __rxor__( self, other) :
iprint ("rxor")
self._calcule()
return other ^ self._resu_
def __ror__( self, other) :
iprint ("ror")
self._calcule()
return other | self._resu_
def __iadd__( self, other) :
iprint ("iadd")
self._calcule()
a=self._resu_
a+= other
return a
def __isub__( self, other) :
iprint ("isub")
self._calcule()
a=self._resu_
a-= other
return a
def __imul__( self, other) :
iprint ("imul")
self._calcule()
a=self._resu_
a*= other
return a
def __idiv__( self, other) :
iprint ("idiv")
self._calcule()
a=self._resu_
a/= other
return a
def __itruediv__( self, other) :
iprint ("itruediv")
self._calcule()
a=self._resu_
a/= other
return a
def __ifloordiv__( self, other) :
iprint ("ifloordiv")
self._calcule()
a=self._resu_
a//= other
return a
def __imod__( self, other) :
iprint ("imod")
self._calcule()
a=self._resu_
a%= other
return a
def __ipow__( self, other, modulo=None) :
iprint ("ipow")
self._calcule()
a=self._resu_
a**= other
return a
def __ilshift__( self, other) :
iprint ("ilshift")
self._calcule()
a=self._resu_
a<<= other
return a
def __irshift__( self, other) :
iprint ("irshift")
self._calcule()
a=self._resu_
a>>= other
return a
def __iand__( self, other) :
iprint ("iand")
self._calcule()
a=self._resu_
a&= other
return a
def __ixor__( self, other) :
iprint ("ixor")
self._calcule()
a=self._resu_
a^= other
return a
def __ior__( self, other) :
iprint ("ior")
self._calcule()
a=self._resu_
a|= other
return a
def __neg__( self) :
iprint ("neg")
self._calcule()
return - self._resu_
def __pos__( self) :
iprint ("pos")
self._calcule()
return + self._resu_
def __abs__( self) :
iprint ("abs")
self._calcule()
return abs( self._resu_ )
def __invert__( self) :
iprint ("invert")
self._calcule()
return ~ self._resu_
def __complex__( self) :
iprint ("complex")
self._calcule()
return complex( self._resu_)
def __int__( self) :
iprint ("int")
self._calcule()
return int(self._resu_)
def __long__( self) :
iprint ("long")
self._calcule()
return long(self._resu_)
def __float__( self) :
iprint ("float")
self._calcule()
return float(self._resu_)
def __oct__( self) :
iprint ("oct")
self._calcule()
return oct(self._resu_)
def __hex__( self) :
iprint ("hex")
self._calcule()
return hex(self._resu_)
def __coerce__( self, other) :
iprint ("coerce")
self._calcule()
return coerce(self._resu_,other)
def lazy(f):
iprint ("appel de la lazy fonction")
a=None
c="""def m"""+str(hash(f))+"""(*a,**b) :
return continuation(wait(m"""+str(hash(f))+"""._k,*a,**b))
a=m"""+str(hash(f))+"""
"""
iprint(c)
d=compile(c,'<string>','exec')
exec(d)
a._k=f
globals().update({a.func_name:a})
return a
#####################################################################
#Exemple de test :
class toto :
def __init__(self,a):
self.v=a
#self.getaddval=lazy(self.getaddval)
def getv(self):
return self.a
@lazy
def getaddval(self,t,y):
iprint("traitement de getaddval("+str(t)+","+str(y)+")")
return self.v + t + y
@lazy
def donne_toto(y) :
iprint("execution de donne_toto")
return toto(y)
@lazy
def fn(x,y,larg=None):
r=x*2
print "execution de fn("+str(x)+","+str(y)+","+str(larg)+") --> "+str(r)
return r
@lazy
def f():
print "execution de f"
return complex(5,8)
@lazy
def f2() :
print "execution de f2"
return [1,2,3]
def nonlazy():
print "execution de non lazy"
return 2
print "fin de la déclaration des éléments du test"
d=fn(nonlazy(),'g')
print "d est affecté"
u=f()
print "u est affecté"
print "u="+str(u)
print "d="+str(d)
print "fin test 1"
gen(fn)
print "fin test 2"
t=donne_toto(5)
print "t est affecté"
print str(t.getaddval(1,2))
print "fin test 3"
j'ai un module qui fonctionne mais que je trouve pas trés propre.
pourai-je avoir quelque conseille pour le néttoyer ? (si c'est possible)
J.B.D.
le code -
lazy.py
# -*- coding: cp1252 -*-
import sys
import types
TRACEúlse #affiche ou pas des info de traçage
if TRACE :
def iprint(st):
print(st)
else :
def iprint(st):
pass
def run(k,uv):
u=uv[0]
v=uv[1]
st=["u["+str(x)+"]" for x in xrange(len(u))]
st2=[ str(sk)+"="+repr(sv) for sk,sv in v.iteritems() ]
s=",".join(st+st2)
sf="lambda(z): k("+s+")"
dic1=globals().copy()
dic1.update({'k':k ,'u':u,'v':v})
f=eval(sf,dic1,None)
return f('&')
def wait(k,*a,**b):
yield run(k,[a,b])
###############################
#Gen ne sert pas. Ce fut une fonction ou j'ai cherché des solutions a
diverse pbm
def gen(k):
a=k.func_code.co_argcount
if k.func_code.co_flags & 0x04 :
b=a+1
else :
b=a
if k.func_code.co_flags & 0x08 :
c=b+1
else :
c=b
print "nb argument : "+str(a)
print "nom des argument : "+str(k.func_code.co_varnames[0:a])
####################################"
class continuation(object):
"""
Une instance de continuation est crée par l'appel a la fonction décorer
par @lazy
l'objet attend une quelconque utilisation pour s'évaluer et renvoyer la
réponse adéquate.
"""
st=["_oo_","_calc_","_resu_"]
def __init__(self,o):
#self._oo_=o
object.__setattr__(self,'_oo_',o)
#self._calc_úlse
object.__setattr__(self,'_calc_',False)
#self._resu_=None
object.__setattr__(self,'_resu_',None)
def _calcule(self) :
if not self._calc_ :
iprint("Calcule de la valeur")
a=self._oo_.next()
#self._resu_=a
object.__setattr__(self,'_resu_',a)
#self._calc_=True
object.__setattr__(self,'_calc_',True)
iprint( "fin calcule valeur")
def __repr__(self):
iprint( "repr")
self._calcule()
return repr(self._resu_)
def __getattr__(self,name):
iprint ("getattr : "+str(name))
self._calcule()
st="lambda (u): u."+name
#a=eval(st,globals(),locals())
a=eval(st,None,None)
return a(self._resu_)
def __setattr__( self, name, value) :
#if name in continuation.st :
# object.__setattr__(self, name, value)
#else :
iprint( "setattr : "+str(name)+"="+str(value))
self._calcule()
self._resu_.__setattr__(name, value)
def __nonzero__(self):
iprint ("nonzero")
self._calcule()
return self._resu_.__nonzero__()
def __call__(self,*a,**b) :
iprint( "call")
self._calcule()
return run(self._resu_,[a,b])
def __str__(self) :
iprint( "str")
self._calcule()
return str(self._resu_)
def __lt__( self, other) :
iprint ("<")
self._calcule()
return self._resu_<other
def __le__( self, other) :
iprint ("<=")
self._calcule()
return self._resu_<=other
def __eq__( self, other) :
iprint ("==")
self._calcule()
return self._resu_==other
def __ne__( self, other) :
iprint ("!= ou <>")
self._calcule()
return self._resu_!=other
def __gt__( self, other) :
iprint (">")
self._calcule()
return self._resu_>other
def __ge__( self, other) :
iprint (">=")
self._calcule()
return self._resu_>=other
def __hash__( self) :
iprint ("hash")
self._calcule()
return self._resu_.__hash__()
def __unicode__( self) :
iprint ("unicode")
self._calcule()
return self._resu_.__unicode__()
def __iter__( self ) :
iprint ("iter")
self._calcule()
return self._resu_.__iter__()
def __getitem__( self, key) :
iprint ("getitem "+str(key))
self._calcule()
return self._resu_[key]
def __setitem__( self, key, value) :
iprint ("setitem")
self._calcule()
self._resu_[key]=value
def __delitem__( self, key) :
iprint ("delitem")
self._calcule()
self._resu_.__delitem__(key)
def __contains__( self, item) :
iprint ("contains")
self._calcule()
return self._resu_.__contains__(item)
def __len__(self) :
iprint ("len")
self._calcule()
return self._resu_.__len__()
if sys.version_info < (2, 0):
# They won't be defined if version is at least 2.0 final
def __getslice__(self, i, j):
iprint ("getslice")
self._calcule()
return self._resu_.__getslice__(i,j)
def __setslice__(self, i, j, seq):
iprint ("setslice")
self._calcule()
self._resu_.__setslice__(i,j,seq)
def __delslice__(self, i, j):
iprint ("delslice")
self._calcule()
self._resu_.__delslice__(i,j,seq)
def __add__( self, other) :
iprint ("add")
self._calcule()
return self._resu_ + other
def __sub__( self, other) :
iprint ("sub")
self._calcule()
return self._resu_ - other
def __mul__( self, other) :
iprint ("mul")
self._calcule()
return self._resu_ * other
def __floordiv__( self, other) :
iprint ("floordiv")
self._calcule()
return self._resu_ // other
def __mod__( self, other) :
iprint ("mod")
self._calcule()
return self._resu_ % other
def __divmod__( self, other) :
iprint ("divmod")
self._calcule()
return divmod(self._resu_ , other)
def __pow__( self, other, modulo=None) :
iprint ("pow")
self._calcule()
return pow(self._resu_ , other ,modulo)
def __lshift__( self, other) :
iprint ("lshift")
self._calcule()
return self._resu_ << other
def __rshift__( self, other) :
iprint ("rshift")
self._calcule()
return self._resu_ >> other
def __and__( self, other) :
iprint ("and")
self._calcule()
return self._resu_ & other
def __xor__( self, other) :
iprint ("xor")
self._calcule()
return self._resu_ ^ other
def __or__( self, other) :
iprint ("or")
self._calcule()
return self._resu_ | other
def __div__( self, other) :
iprint ("div")
self._calcule()
return self._resu_ / other
def __truediv__( self, other) :
iprint ("truediv")
self._calcule()
return self._resu_ / other
def __radd__( self, other) :
iprint("radd")
self._calcule()
return other + self._resu_
def __rsub__( self, other) :
iprint ("rsub")
self._calcule()
return other - self._resu_
def __rmul__( self, other) :
iprint ("rmul")
self._calcule()
return other * self._resu_
def __rdiv__( self, other) :
iprint ("rdiv")
self._calcule()
return other / self._resu_
def __rtruediv__( self, other) :
iprint ("rtruediv")
self._calcule()
return other / self._resu_
def __rfloordiv__( self, other) :
iprint( "rfloordiv")
self._calcule()
return other // self._resu_
def __rmod__( self, other) :
iprint ("rmod")
self._calcule()
return other % self._resu_
def __rdivmod__( self, other) :
iprint ("rdivmod")
self._calcule()
return divmod(other , self._resu_)
def __rpow__( self, other) :
iprint ("rpow")
self._calcule()
return other ** self._resu_
def __rlshift__( self, other) :
iprint ("rlshift")
self._calcule()
return other << self._resu_
def __rrshift__( self, other) :
iprint ("rrshift")
self._calcule()
return other >> self._resu_
def __rand__( self, other) :
iprint ("rand")
self._calcule()
return other & self._resu_
def __rxor__( self, other) :
iprint ("rxor")
self._calcule()
return other ^ self._resu_
def __ror__( self, other) :
iprint ("ror")
self._calcule()
return other | self._resu_
def __iadd__( self, other) :
iprint ("iadd")
self._calcule()
a=self._resu_
a+= other
return a
def __isub__( self, other) :
iprint ("isub")
self._calcule()
a=self._resu_
a-= other
return a
def __imul__( self, other) :
iprint ("imul")
self._calcule()
a=self._resu_
a*= other
return a
def __idiv__( self, other) :
iprint ("idiv")
self._calcule()
a=self._resu_
a/= other
return a
def __itruediv__( self, other) :
iprint ("itruediv")
self._calcule()
a=self._resu_
a/= other
return a
def __ifloordiv__( self, other) :
iprint ("ifloordiv")
self._calcule()
a=self._resu_
a//= other
return a
def __imod__( self, other) :
iprint ("imod")
self._calcule()
a=self._resu_
a%= other
return a
def __ipow__( self, other, modulo=None) :
iprint ("ipow")
self._calcule()
a=self._resu_
a**= other
return a
def __ilshift__( self, other) :
iprint ("ilshift")
self._calcule()
a=self._resu_
a<<= other
return a
def __irshift__( self, other) :
iprint ("irshift")
self._calcule()
a=self._resu_
a>>= other
return a
def __iand__( self, other) :
iprint ("iand")
self._calcule()
a=self._resu_
a&= other
return a
def __ixor__( self, other) :
iprint ("ixor")
self._calcule()
a=self._resu_
a^= other
return a
def __ior__( self, other) :
iprint ("ior")
self._calcule()
a=self._resu_
a|= other
return a
def __neg__( self) :
iprint ("neg")
self._calcule()
return - self._resu_
def __pos__( self) :
iprint ("pos")
self._calcule()
return + self._resu_
def __abs__( self) :
iprint ("abs")
self._calcule()
return abs( self._resu_ )
def __invert__( self) :
iprint ("invert")
self._calcule()
return ~ self._resu_
def __complex__( self) :
iprint ("complex")
self._calcule()
return complex( self._resu_)
def __int__( self) :
iprint ("int")
self._calcule()
return int(self._resu_)
def __long__( self) :
iprint ("long")
self._calcule()
return long(self._resu_)
def __float__( self) :
iprint ("float")
self._calcule()
return float(self._resu_)
def __oct__( self) :
iprint ("oct")
self._calcule()
return oct(self._resu_)
def __hex__( self) :
iprint ("hex")
self._calcule()
return hex(self._resu_)
def __coerce__( self, other) :
iprint ("coerce")
self._calcule()
return coerce(self._resu_,other)
def lazy(f):
iprint ("appel de la lazy fonction")
a=None
c="""def m"""+str(hash(f))+"""(*a,**b) :
return continuation(wait(m"""+str(hash(f))+"""._k,*a,**b))
a=m"""+str(hash(f))+"""
"""
iprint(c)
d=compile(c,'<string>','exec')
exec(d)
a._k=f
globals().update({a.func_name:a})
return a
#####################################################################
#Exemple de test :
class toto :
def __init__(self,a):
self.v=a
#self.getaddval=lazy(self.getaddval)
def getv(self):
return self.a
@lazy
def getaddval(self,t,y):
iprint("traitement de getaddval("+str(t)+","+str(y)+")")
return self.v + t + y
@lazy
def donne_toto(y) :
iprint("execution de donne_toto")
return toto(y)
@lazy
def fn(x,y,larg=None):
r=x*2
print "execution de fn("+str(x)+","+str(y)+","+str(larg)+") --> "+str(r)
return r
@lazy
def f():
print "execution de f"
return complex(5,8)
@lazy
def f2() :
print "execution de f2"
return [1,2,3]
def nonlazy():
print "execution de non lazy"
return 2
print "fin de la déclaration des éléments du test"
d=fn(nonlazy(),'g')
print "d est affecté"
u=f()
print "u est affecté"
print "u="+str(u)
print "d="+str(d)
print "fin test 1"
gen(fn)
print "fin test 2"
t=donne_toto(5)
print "t est affecté"
print str(t.getaddval(1,2))
print "fin test 3"

Poser une question


schéma UML de classes ...
En effet, la classe continuation me parraît "un poil" chargée.
Pour que cela soit plus clair, je pense qu'il faudrait que tu essaies
de redécouper cette classe ...
Je sais pas... peut être regrouper les fonctions par types ... les
opérations booléennes dans un type, arithmétiques dans un autre, etc
...
Ha oui aussi rien ne vaut une bonne "commentation" du code ...
Sinon à quoi sert ce module ... enfin concrètement ?
Juste un détail ; remplacer les
print "xxx="+str(xxx)
par des
print "xxx="+repr(xxx)
L'avantage de repr(, sur str( c'est que l'on obtiens la même chose qu'un
print
MCI
Je serais curieux de savoir si UML est capable de représenter toutes les
subtilités de Python.
@-salutations
Michel Claveau
http://www.objectsbydesign.com/proj...ample.html
convertit alors que l'autre affiche simplement tout dépend de ce que
l'on veut faire. Dans ce cas il est préférable d'utiliser repr() car il
s'agit d'afficher et non de convertir une donnée en une chaîne de
caractère. Cependant, il faut noter que ceci existe aussi:
a = "Hello World"
b = "Salut Monde"
separation = " -- "
print "Vos phrases: %s %s %s" % (a, separation, b)
Cette notation permet d'être très clair car le lecteur de la source
saura de quel type est une variable.
YL