• Re: Logging

    From Stefan Ram@21:1/5 to baran200167@gmail.com on Sat Nov 19 11:08:57 2022
    <baran200167@gmail.com> writes:
    You are expected to implement logging feature to an existing
    code which uses the function below.
    def my_ugly_debug(s, level=0):
    pre_text = [ "INFO",
    "WARNING",
    "ERROR"
    ]
    print(f"{pre_text[level]}: {s}")
    You are not allowed to make changes in my_ugly_debug, so find another way.

    If found a solution that is even more ugly than your
    function. I was just about to post it here, but then
    remembered about the "no homework" rule. Bummer!

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From =?UTF-8?B?QmFyYW4gR8O2a8OnZWtsaQ==?@21:1/5 to All on Sat Nov 19 02:44:21 2022
    How can I solve this question?

    There is a module called ‘logging’ to
    employ logging facility in Python.

    import logging

    logging. info('Just a normal message' )

    Logging.warning('Not fatal but still noted')

    logging.error('There is something wrong')

    You are expected to implement logging
    feature to an existing code which uses the
    function below.

    def my_ugly_debug(s, level=0):

    pre_text = [ "INFO",
    "WARNING",
    "ERROR"
    ]

    print(f"{pre_text[level]}: {s}")
    You are not allowed to make changes in my_ugly_debug, so find another way.




    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Stefan Ram on Sat Nov 19 11:46:39 2022
    ram@zedat.fu-berlin.de (Stefan Ram) writes:
    If found a solution that is even more ugly than your
    function. I was just about to post it here, but then
    remembered about the "no homework" rule. Bummer!

    I tampered with "sys.stdout" to intercept the "print" calls
    and redirect them to logging when they start with a tag,
    otherwise they would be printed as usual. So then

    print( "normal printing" )
    my_ugly_debug( "A", level=0 )
    my_ugly_debug( "A", level=1 )
    my_ugly_debug( "A", level=2 )

    would print

    normal printing
    WARNING:root:WARNING: A
    ERROR:root:ERROR: A

    plus some additional empty lines (one would need a bit
    more work to clean that up). Another possibility would
    be to redefine "print". If decorators, functions, or
    classes around the definition of "my_ugly_debug" were
    allowed, things could be handled a bit more cleanly.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Cameron Simpson@21:1/5 to Stefan Ram on Sun Nov 20 09:27:58 2022
    On 19Nov2022 11:08, Stefan Ram <ram@zedat.fu-berlin.de> wrote: ><baran200167@gmail.com> writes:
    You are expected to implement logging feature to an existing
    code which uses the function below. [...]
    You are not allowed to make changes in my_ugly_debug, so find another
    way.

    If found a solution that is even more ugly than your
    function. I was just about to post it here, but then
    remembered about the "no homework" rule. Bummer!

    I suspect that the OP is just being asked to modify existing code which
    calls my_ugly_debug to use more conventional logging calls.

    Baran, in addition to the various info(), warning() etc logging calls
    there is a log() logging call which accepts a log level (the warning()
    etc calls basicly call this with their own logging level).

    I would be inclined to write a my_better_debug(s,level=0) function which
    took those existing levels (0,1,2) and mapped them to the official
    logging levels logging.INFO, logging.WARNING etc, and then called
    logging.log() with the official level.

    Then adjust the calling code to call your new function.

    The alternative is to just replace every calling function which uses my_ugly_debug() to directly call a logging.whatever() call.

    Cheers,
    Cameron Simpson <cs@cskk.id.au>

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Thomas Passin@21:1/5 to Cameron Simpson on Sat Nov 19 18:26:25 2022
    On 11/19/2022 5:27 PM, Cameron Simpson wrote:
    On 19Nov2022 11:08, Stefan Ram <ram@zedat.fu-berlin.de> wrote:
    <baran200167@gmail.com> writes:
    You are expected to implement logging feature to an existing
    code which uses the function below. [...]
    You are not allowed to make changes in my_ugly_debug, so find another
    way.

     If found a solution that is even more ugly than your
     function. I was just about to post it here, but then
     remembered about the "no homework" rule. Bummer!

    I suspect that the OP is just being asked to modify existing code which
    calls my_ugly_debug to use more conventional logging calls.

    Baran, in addition to the various info(), warning() etc logging calls
    there is a log() logging call which accepts a log level (the warning()
    etc calls basicly call this with their own logging level).

    I would be inclined to write a my_better_debug(s,level=0) function which
    took those existing levels (0,1,2) and mapped them to the official
    logging levels logging.INFO, logging.WARNING etc, and then called logging.log() with the official level.

    Then adjust the calling code to call your new function.

    The alternative is to just replace every calling function which uses my_ugly_debug() to directly call a logging.whatever() call.

    Maybe a place for a decorator...


    Cheers,
    Cameron Simpson <cs@cskk.id.au>

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Cameron Simpson@21:1/5 to Thomas Passin on Sun Nov 20 12:26:19 2022
    On 19Nov2022 18:26, Thomas Passin <list1@tompassin.net> wrote:
    The alternative is to just replace every calling function which uses >>my_ugly_debug() to directly call a logging.whatever() call.

    Maybe a place for a decorator...

    Indeed, though I don't think the OP is up to decorators yet.

    But really, is there any problem which cannot be solved with a
    decorator? I've even got a @decorator decorator for my decorators.

    Cheers,
    Cameron Simpson <cs@cskk.id.au>

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris Angelico@21:1/5 to Cameron Simpson on Sun Nov 20 12:36:29 2022
    On Sun, 20 Nov 2022 at 12:27, Cameron Simpson <cs@cskk.id.au> wrote:

    On 19Nov2022 18:26, Thomas Passin <list1@tompassin.net> wrote:
    The alternative is to just replace every calling function which uses >>my_ugly_debug() to directly call a logging.whatever() call.

    Maybe a place for a decorator...

    Indeed, though I don't think the OP is up to decorators yet.

    But really, is there any problem which cannot be solved with a
    decorator? I've even got a @decorator decorator for my decorators.


    Do you have a @toomanydecorators decorator to reduce the number of
    decorators you need to decorate your decorators?

    ChrisA

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Cameron Simpson@21:1/5 to Chris Angelico on Sun Nov 20 14:00:58 2022
    On 20Nov2022 12:36, Chris Angelico <rosuav@gmail.com> wrote:
    On Sun, 20 Nov 2022 at 12:27, Cameron Simpson <cs@cskk.id.au> wrote:
    But really, is there any problem which cannot be solved with a
    decorator? I've even got a @decorator decorator for my decorators.

    Do you have a @toomanydecorators decorator to reduce the number of
    decorators you need to decorate your decorators?

    Apparently not :-)

    @classmethod
    @pfx_method
    @promote
    @typechecked
    def promote(cls, policy, epoch: Optional[Epoch] = None, **policy_kw):

    and there aren't even any @require decorators on that one (from the
    excellent icontract package).

    Cheers,
    Cameron Simpson <cs@cskk.id.au>

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)