Re: builtins.TypeError: catching classes that do not inherit from BaseException is not allowed

2021-12-31 Thread Marco Sulla
It was already done: https://pypi.org/project/tail-recursive/

On Thu, 30 Dec 2021 at 16:00, [email protected]  wrote:
>
> I try to compute the factorial of a large number with tail-recursion 
> optimization decorator in Python3. The following code snippet is converted 
> from the code snippet given here [1] by the following steps:
>
> $ pyenv shell datasci
> $ python --version
> Python 3.9.1
> $ pip install 2to3
> $ 2to3 -w this-script.py
>
> ```
> # This program shows off a python decorator(
> # which implements tail call optimization. It
> # does this by throwing an exception if it is
> # its own grandparent, and catching such
> # exceptions to recall the stack.
>
> import sys
>
> class TailRecurseException:
>   def __init__(self, args, kwargs):
> self.args = args
> self.kwargs = kwargs
>
> def tail_call_optimized(g):
>   """
>   This function decorates a function with tail call
>   optimization. It does this by throwing an exception
>   if it is its own grandparent, and catching such
>   exceptions to fake the tail call optimization.
>
>   This function fails if the decorated
>   function recurses in a non-tail context.
>   """
>   def func(*args, **kwargs):
> f = sys._getframe()
> if f.f_back and f.f_back.f_back \
> and f.f_back.f_back.f_code == f.f_code:
>   raise TailRecurseException(args, kwargs)
> else:
>   while 1:
> try:
>   return g(*args, **kwargs)
> except TailRecurseException as e:
>   args = e.args
>   kwargs = e.kwargs
>   func.__doc__ = g.__doc__
>   return func
>
> @tail_call_optimized
> def factorial(n, acc=1):
>   "calculate a factorial"
>   if n == 0:
> return acc
>   return factorial(n-1, n*acc)
>
> print(factorial(1))
> # prints a big, big number,
> # but doesn't hit the recursion limit.
>
> @tail_call_optimized
> def fib(i, current = 0, next = 1):
>   if i == 0:
> return current
>   else:
> return fib(i - 1, next, current + next)
>
> print(fib(1))
> # also prints a big number,
> # but doesn't hit the recursion limit.
> ```
> However, when I try to test the above script, the following error will be 
> triggered:
> ```
> $ python this-script.py
> Traceback (most recent call last):
>   File "/home/werner/this-script.py", line 32, in func
> return g(*args, **kwargs)
>   File "/home/werner/this-script.py", line 44, in factorial
> return factorial(n-1, n*acc)
>   File "/home/werner/this-script.py", line 28, in func
> raise TailRecurseException(args, kwargs)
> TypeError: exceptions must derive from BaseException
>
> During handling of the above exception, another exception occurred:
>
> Traceback (most recent call last):
>   File "/home/werner/this-script.py", line 46, in 
> print(factorial(1))
>   File "/home/werner/this-script.py", line 33, in func
> except TailRecurseException as e:
> TypeError: catching classes that do not inherit from BaseException is not 
> allowed
> ```
>
> Any hints for fixing this problem will be highly appreciated.
>
> [1]  https://stackoverflow.com/q/27417874
>
> Regards,
> HZ
> --
> https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: builtins.TypeError: catching classes that do not inherit from BaseException is not allowed

2021-12-31 Thread Karsten Hilbert
Am Thu, Dec 30, 2021 at 03:57:25PM -0800 schrieb [email protected]:

> > > Then what cases/scenarios can demonstrate the beauty of recursion?
> > >
> > Walking a tree.
>
> There are many type of trees. Do you mean all of them?

Palm trees don't lend themselves to recursion all that much.

Karsten
--
GPG  40BE 5B0E C98E 1713 AFA6  5BC0 3BEA AC80 7D4F C89B
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: builtins.TypeError: catching classes that do not inherit from BaseException is not allowed

2021-12-31 Thread [email protected]
On Friday, December 31, 2021 at 7:17:18 PM UTC+8, [email protected] wrote:
> On Friday, December 31, 2021 at 4:18:28 PM UTC+8, Marco Sulla wrote: 
> > It was already done: https://pypi.org/project/tail-recursive/
> A few days ago, I also noticed another similar project: 
> https://github.com/baruchel/tco 

And also see the following one:

https://pypi.org/project/tail-recursion/

 
> It seems that they are very similar, even identical. But I'm not sure, so I 
> filed an issue here [1]. 
> 
> [1] https://github.com/0scarB/tail-recursive/issues/1
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: recover pickled data: pickle data was truncated

2021-12-31 Thread iMath
在 2021年12月30日星期四 UTC+8 03:13:21, 写道:
> On Wed, 29 Dec 2021 at 18:33, iMath  wrote: 
> > But I found the size of the file of the shelve data didn't change much, so 
> > I guess the data are still in it , I just wonder any way to recover my data.
> I agree with Barry, Chris and Avi. IMHO your data is lost. Unpickling 
> it by hand is a harsh work and maybe unreliable. 
> 
> Is there any reason you can't simply add a semaphore to avoid writing 
> at the same time and re-run the code and regenerate the data?

Thanks for your replies! I didn't have a sense of adding a semaphore on writing 
to pickle data before, so  corrupted the data.
Since my data was colleted in the daily usage, so cannot re-run the code and 
regenerate the data.
In order to avoid corrupting my data again and the complicity of using  a 
semaphore, now I am using json text to store my data.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: builtins.TypeError: catching classes that do not inherit from BaseException is not allowed

2021-12-31 Thread [email protected]
On Friday, December 31, 2021 at 4:18:28 PM UTC+8, Marco Sulla wrote:
> It was already done: https://pypi.org/project/tail-recursive/

A few days ago, I also noticed another similar project:
https://github.com/baruchel/tco

It seems that they are very similar, even identical. But I'm not sure, so I 
filed an issue here [1].

[1] https://github.com/0scarB/tail-recursive/issues/1
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: builtins.TypeError: catching classes that do not inherit from BaseException is not allowed

2021-12-31 Thread Avi Gross via Python-list
I am sure some people have a sense of humor, but anyone on this forum who
actually does not have some idea of what various "tree" data structures are
in computer science, probably won't get any replies from me when asking such
questions.

But indeed there are things closer to classical trees that are traversed in
various ways including depth-first versus breadth first. Some have special
names like a parse tree or a decision tree and they can even be collected
into a random forest.

But fundamentally, the idea of using a recursion that at each node may take
multiple next steps by calling an instance of itself at the new node, is
fairly common, as are using it to search many kinds of data structures.

The discussion though suggests that no single idea about computer languages
need be used constantly and sometimes recursion is not the only method or
even the best method. You can design tree structures in ways that allow them
to be traversed in an iterative way such as having bi-directional links
along with flags that mark where you entered a node from or already visited.
Tools should be tools, not religions. 

-Original Message-
From: Python-list  On
Behalf Of Karsten Hilbert
Sent: Friday, December 31, 2021 7:09 AM
To: [email protected]
Subject: Re: builtins.TypeError: catching classes that do not inherit from
BaseException is not allowed

Am Thu, Dec 30, 2021 at 03:57:25PM -0800 schrieb [email protected]:

> > > Then what cases/scenarios can demonstrate the beauty of recursion?
> > >
> > Walking a tree.
>
> There are many type of trees. Do you mean all of them?

Palm trees don't lend themselves to recursion all that much.

Karsten
--
GPG  40BE 5B0E C98E 1713 AFA6  5BC0 3BEA AC80 7D4F C89B
-- 
https://mail.python.org/mailman/listinfo/python-list

-- 
https://mail.python.org/mailman/listinfo/python-list