Python Forum
is it considered good practice to ...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
is it considered good practice to ...
#8
(May-09-2017, 12:36 AM)Skaperen Wrote: here is a snippet example i coded yesterday:

   if time_prefix:
        if sys.version_info.major < 3:
            def xprint(*args,**opts):
                opts['file']=efile
                try:
                    return print(now().strftime(etformat),*args,**opts)
                except (IOError,KeyboardInterrupt):
                    return
        else:
            def xprint(*args,**opts):
                opts['file']=efile
                try:
                    return print(now().strftime(etformat),*args,**opts)
                except (BrokenPipeError,IOError,KeyboardInterrupt):
                    return
    else:
        if sys.version_info.major < 3:
            def xprint(*args,**opts):
                opts['file']=efile
                try:
                    return print(*args,**opts)
                except (IOError,KeyboardInterrupt):
                    return
        else:
            def xprint(*args,**opts):
                opts['file']=efile
                try:
                    return print(*args,**opts)
                except (BrokenPipeError,IOError,KeyboardInterrupt):
                    return

I think I'd write that like:
printer = print
if time_prefix:
    printer = lambda *args, **opts: print(now().strftime(etformat),*args,**opts)
errors = (IOError, KeyboardInterrupt)
if sys.version_info.major >= 3:
    errors += (BrokenPipeError, )

def xprint(*args, **opts):
    opts["file"] = efile
    try:
        return printer(*args, **opts)
    except errors:
        pass
Reply


Messages In This Thread
is it considered good practice to ... - by Skaperen - May-08-2017, 08:48 AM
RE: is it considered good practice to ... - by nilamo - May-09-2017, 01:47 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Is a good practice naming user defined python functions with prefix udf_? meerkat 2 819 Mar-09-2024, 01:40 PM
Last Post: buran
  best practice example data science mostafa705 1 908 Oct-13-2023, 04:44 AM
Last Post: KianLynch
  Practice coding through hackathon!! TheOutcast 0 1,582 Jun-11-2020, 12:03 PM
Last Post: TheOutcast
  Coding Practice BarakTu 2 3,377 Apr-14-2018, 05:31 AM
Last Post: buran
  Is it bad practice to return a variable and not use it? iFunKtion 8 6,672 Apr-04-2017, 04:34 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020