• from my lab 5

    From Stefan Ram@21:1/5 to All on Tue Feb 14 22:10:00 2023
    main.py

    import contextlib
    import io

    def output( *args, **kwargs ):
    string = io.StringIO()
    with contextlib.redirect_stdout( string ): print( *args, **kwargs )
    s = string.getvalue()
    print( end=s )
    return len( s )

    print( output( 'Hi!' ))

    output

    Hi!
    4

    previous "from my lab" posts

    Adding "start" and "end" methods to a logger
    1 How a function can get its own name
    2 Logging function entry and exit
    3 Logging function entry and exit with indentation
    4 Tracing executed statements

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Stefan Ram on Wed Feb 15 11:26:55 2023
    ram@zedat.fu-berlin.de (Stefan Ram) writes:
    print( output( 'Hi!' ))
    output
    Hi!
    4

    def output( *args, **kwargs ):
    string = io.StringIO()
    if 'file' in kwargs:
    file = kwargs[ 'file' ]
    del kwargs[ 'file' ]
    else:
    file = None
    with contextlib.redirect_stdout( string ): print( *args, **kwargs )
    s = string.getvalue()
    print( end=s, file=file )if file else print( end=s )
    return len( s )

    print( output( 'Hi!' ))
    print( output( 'Hi!', file=sys.stderr ))

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