Python: Generators and decorators with for a dictionary -


I created a problem that uses the ability to use generators, decorators and dictionaries in Python.

However, I can not solve this practice myself, and wondering if it is possible to solve it.

Is it possible to cache the output output as a dictionary using a decorator function, which generator?

Exercise:

Write Decorator for Cash Function Greetings Results Store Combination Arg: A result in a dictionary of function objects in a dictionary. Generate these results using the generator function Test your code on the Fibonacci function.

I tried to apply it like this:

  def cachefunc (func): def cover (* args, ** quargas): result = Throw (* args, ** quarj) returns {throw .__ name__ + '(' + str (list) ([args]) [1: -1] + ')': str (result)} wrapping .__ name__ = fan .__ name__ wrapper___ doctor__ = fancy.__ doc__ return wrapper @cachefunc def fibonacci (n): n & gt; = 0 if n & lt; 2: returns not end: returns (Fnonacci (N-1) + Fibonacci (N -2) def allfib (): n = 0 while true: yield Fibonacci (N) N + = 1 result = [] generator = allfib) while LAN (results) & lt; 10: x = next (generator) result. Ampad (x) print result   

However, I get the following error:

  python dg.py traceback (Most recent call final): File "dg.py", line 32, & lt; Module & gt; File = "dg.py", line 10, results in the wrapper = "function" (* args, ** kwargs) file "DG.", In the file "dg.py", line 26, orphib yield Fibonacci (N) Fibonacci Returns (Fibonacci (N-1) + Fibonacci (N-2)) Type: Error: Type of unsupported Operation Type: for 'Dict' and 'Dict' and 'Dict',   

Does anyone know the alternate solution to such a question?

Specific error

The error is here:

  return {func .__ name__ + '(' + str (list (arg)) [1: -1] + ') ': Str (result)}   

When you want to return results, you are returning a dictionary, I believe. I do not know enough that you are inside caching caching

General Comments

This pattern of caching is also commonly called memonization.

Try it Fibonacci About the part of Scroll down the following codes here:

  Class memory: def __init __ (self, function): self.function = function self.func_name = function .__name__ self.memoized = {} Def __call __ (self, * args): Try: "Memo Solution" for "+ self.func_name +" Use "+ str (Args) Returns self" except.momozed [Args] except. Keyteeter: Print "Computing Solution" "+ self.func_name +" at "+ str (args)" itself. Memoized [args] = self.function (* Args) Returns self.memoised [Args]   

Then just do:

  @ mmoizf df fibonacci (n): n & gt; = 0 if n & lt; 2: return no more: returns (fibonacci (n-1) Memonization still works on your generator, for example, note that the print statements show you that Missed results .  
  Remember class: def __init __ (self, function): self.function = function self.func_name = function .__ name__ self Memoized = {} def __call __ (Self, * args): Try: Using "+ str (args) return self" memo solution for print "" + self.func_name + " Excluding KeyError: [Argus]: Print "computing solution now automatically for" + str.func_name + "for" + str (args) ". Memoized [args] = self.function (* Args) Returns self-MOVIZED [ARGS] @EmMoise DF Fanonachi (N): N zardari = 0 if n & lt; 2: returns not end: returns (F1) + Fibonacci (N -2) def allfib (): n = 0 while true: yield Fibonacci (N) N + = 1 result = [] generator = allfib) while LAN (result) <10: x = next (generator) result. Ampended (x) print result    

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -