From b438e009efd8aa0a75c8a9adf27350dd43087357 Mon Sep 17 00:00:00 2001 From: Dahmane Lynda <lynda.dahmane.etu@118p28.fil.univ-lille.fr> Date: Wed, 31 Jan 2024 08:41:48 +0100 Subject: [PATCH] Tp3 --- Tp3/Recursion/fractales/ap_decorators.py | 135 +++++++++++++++++++++++ Tp3/recursion.zip | Bin 0 -> 1018 bytes 2 files changed, 135 insertions(+) create mode 100644 Tp3/Recursion/fractales/ap_decorators.py create mode 100644 Tp3/recursion.zip diff --git a/Tp3/Recursion/fractales/ap_decorators.py b/Tp3/Recursion/fractales/ap_decorators.py new file mode 100644 index 0000000..5f4adc9 --- /dev/null +++ b/Tp3/Recursion/fractales/ap_decorators.py @@ -0,0 +1,135 @@ +#!/usr/bin/python3 +# -*- coding: utf-8 -*- + +""" +:module: ap_decorators +:author: FIL - Faculté des Sciences et Technologies - Univ. Lille <http://portail.fil.univ-lille1.fr>_ +:date: 2018, september + +""" + +from functools import wraps + + +def trace(fct): + ''' + Decorator for tracing every call to fct. + Recursive calls are indented. + + :Example: + + >>> @trace + ... def fact(n): + ... if n == 0: + ... return 1 + ... else: + ... return n * fact(n - 1) + + >>> fact(5) + -> fact((5,), {}) + ... -> fact((4,), {}) + ...... -> fact((3,), {}) + ......... -> fact((2,), {}) + ............ -> fact((1,), {}) + ............... -> fact((0,), {}) + ............... <- 1 + ............ <- 1 + ......... <- 2 + ...... <- 6 + ... <- 24 + <- 120 + 120 + ''' + @wraps(fct) + def wrapper(*args, **kwargs): + dots = '...' * wrapper.__depth + print('{:s} -> {:s}{:s}'.format(dots, wrapper.__name__, repr((args, kwargs)))) + wrapper.__depth += 1 + y = fct(*args, **kwargs) + wrapper.__depth -= 1 + print('{:s} <- {:s}'.format(dots, repr(y))) + return y + wrapper.__depth = 0 + return wrapper + +def count(fct): + ''' + decorator for counting calls to function fct + + :Example: + + >>> @count + ... def fact(n): + ... if n == 0: + ... return 1 + ... else: + ... return n * fact(n - 1) + + >>> fact.counter + 0 + >>> fact(5) + 120 + >>> fact.counter + 6 + ''' + @wraps(fct) + def wrapper(*args, **kwargs): + y = fct(*args, **kwargs) + wrapper.counter += 1 + return y + wrapper.counter = 0 + return wrapper + + +def memoize(fct): + ''' + decorator for memoizing computed values of function fct + + :Example: + + >>> @count + ... @memoize + ... def fact(n): + ... if n == 0: + ... return 1 + ... else: + ... return n * fact(n - 1) + + >>> fact.counter + 0 + >>> fact(5) + 120 + >>> fact.counter + 6 + >>> fact.counter = 0 + >>> fact(5) + 120 + >>> fact.counter + 1 + ''' + cache = dict() + @wraps(fct) + def wrapper(*args, **kwargs): + key = repr((args, kwargs)) + if key in cache: + return cache[key] + else: + y = fct(*args, **kwargs) + cache[key] = y + return y + return wrapper + + + +if __name__ == '__main__': + import doctest + doctest.testmod(optionflags=doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS, verbose=False) + + + + + + + + + diff --git a/Tp3/recursion.zip b/Tp3/recursion.zip new file mode 100644 index 0000000000000000000000000000000000000000..0f7fe41fb6ded95993fb2f398e1e8ee56bd0af88 GIT binary patch literal 1018 zcmWIWW@Zs#U|`^2I5^Eb;<vTtV=ZO|20<<c23ZCfhLQsPw4%i1lEj?UV*SK|_>|P- z{G!B?{GwvLg38bkP6lSHzw1-a9^8>yTEWf0$nuhzfdNe14ZE9n*+699`CyrC-Ak5w z-u|l~axs&`Dsbh2Exd}20Z%N?Wlg*>@o~<SjqHysD-8SQnq+SiG2G!8;g$Tm=J@mX z)_L3Hc~3j>p5H0)%zN*Ir0X}?_P=MIYA7zQIK}GY{8_TcZ{E69bjase>M19MN)cl% z(ThQ%41OM7A>oQLXP-SdEVwXrReeqS4ABBFOJNappTm-ixfO#e4;H><-Xr|SsKL@d znC-k+)boc0GrX5_`Zu%87kXNkb>y)^Ap1n&V=jyT&b|2KB?H&A>}hk2Lo1)Fc%@C4 z(v+onnW-YJuSwhW)1ksSGgKFdE<EGC;$17>gM!%W`{LI%HL}PzANtT3YqXWc>ypS8 zmMdI3KUyvZ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