Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
AP-Dahmane-lynda
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Lynda Dahmane
AP-Dahmane-lynda
Commits
b438e009
Commit
b438e009
authored
1 year ago
by
Dahmane Lynda
Browse files
Options
Downloads
Patches
Plain Diff
Tp3
parent
6ca6fcef
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
Tp3/Recursion/fractales/ap_decorators.py
+135
-0
135 additions, 0 deletions
Tp3/Recursion/fractales/ap_decorators.py
Tp3/recursion.zip
+0
-0
0 additions, 0 deletions
Tp3/recursion.zip
with
135 additions
and
0 deletions
Tp3/Recursion/fractales/ap_decorators.py
0 → 100644
+
135
−
0
View file @
b438e009
#!/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
)
This diff is collapsed.
Click to expand it.
Tp3/recursion.zip
0 → 100644
+
0
−
0
View file @
b438e009
File added
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment