Skip to content

Instantly share code, notes, and snippets.

@DavidBuchanan314
Last active December 1, 2024 06:50
Show Gist options
  • Save DavidBuchanan314/7cdd3c34fedd0c5175d301a38e6922a1 to your computer and use it in GitHub Desktop.
Save DavidBuchanan314/7cdd3c34fedd0c5175d301a38e6922a1 to your computer and use it in GitHub Desktop.

Revisions

  1. DavidBuchanan314 revised this gist Dec 1, 2024. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion indentless_exceptions.py
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,9 @@
    })()(fn)()
    )),

    (throw := lambda e: (_ for _ in ()).throw(e)),
    (throw := lambda e:
    (_ for _ in ()).throw(e)
    ),

    print(try_except(
    lambda: int("123"),
  2. DavidBuchanan314 revised this gist Dec 1, 2024. 1 changed file with 23 additions and 14 deletions.
    37 changes: 23 additions & 14 deletions indentless_exceptions.py
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,25 @@
    try_except = (lambda fn, exception_callback:
    type("mycontext", (__import__("contextlib").ContextDecorator,), {
    "__enter__": lambda self: self,
    "__exit__": lambda self, a, b, c: (a and exception_callback(a, b, c)) or True
    })()(fn)()
    )
    [
    (try_except := (lambda fn, exception_callback:
    type("mycontext", (__import__("contextlib").ContextDecorator,), {
    "__enter__": lambda self: self,
    "__exit__": lambda self, a, b, c: (a and exception_callback(a, b, c)) or True
    })()(fn)()
    )),

    print(try_except(
    lambda: int("123"),
    lambda a, b, c: print("caught", a, b, c)
    ))
    (throw := lambda e: (_ for _ in ()).throw(e)),

    print(try_except(
    lambda: int("abc"),
    lambda a, b, c: print("caught", a, b, c)
    ))
    print(try_except(
    lambda: int("123"),
    lambda a, b, c: print("caught", a, b, c)
    )),

    print(try_except(
    lambda: int("abc"),
    lambda a, b, c: print("caught", a, b, c)
    )),

    print(try_except(
    lambda: throw(Exception("myexception")),
    lambda a, b, c: print("caught", a, b, c)
    )),
    ]
  3. DavidBuchanan314 revised this gist Dec 1, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion indentless_exceptions.py
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    try_except = (lambda fn, exception_callback:
    type("mycontext", (__import__("contextlib").ContextDecorator,), {
    "__enter__": lambda self: self,
    "__exit__": lambda self, a, b, c: [a and exception_callback(a, b, c), True][-1]
    "__exit__": lambda self, a, b, c: (a and exception_callback(a, b, c)) or True
    })()(fn)()
    )

  4. DavidBuchanan314 revised this gist Dec 1, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion indentless_exceptions.py
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    try_except = (lambda fn, exception_callback:
    type("mycontext", (__import__("contextlib").ContextDecorator,), {
    "__enter__": lambda self: self,
    "__exit__": lambda self, a, b, c: [(not a) or exception_callback(a, b, c), True][-1]
    "__exit__": lambda self, a, b, c: [a and exception_callback(a, b, c), True][-1]
    })()(fn)()
    )

  5. DavidBuchanan314 revised this gist Dec 1, 2024. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions indentless_exceptions.py
    Original file line number Diff line number Diff line change
    @@ -7,10 +7,10 @@

    print(try_except(
    lambda: int("123"),
    lambda a, b, c: print("caught", a)
    lambda a, b, c: print("caught", a, b, c)
    ))

    print(try_except(
    lambda: int("abc"),
    lambda a, b, c: print("caught", a)
    lambda a, b, c: print("caught", a, b, c)
    ))
  6. DavidBuchanan314 created this gist Dec 1, 2024.
    16 changes: 16 additions & 0 deletions indentless_exceptions.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    try_except = (lambda fn, exception_callback:
    type("mycontext", (__import__("contextlib").ContextDecorator,), {
    "__enter__": lambda self: self,
    "__exit__": lambda self, a, b, c: [(not a) or exception_callback(a, b, c), True][-1]
    })()(fn)()
    )

    print(try_except(
    lambda: int("123"),
    lambda a, b, c: print("caught", a)
    ))

    print(try_except(
    lambda: int("abc"),
    lambda a, b, c: print("caught", a)
    ))