Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
dppv.py
#1
this is the code referenced in the "getting the source line number" thread:
#-------#-------#-------#-------#-------#-------#-------#-------#-------#-------#
# dp() does printing and pv() prints a named variable
# to disable these functions at run time, set environment variable 'nodebug'
#-------#-------#-------#-------#-------#-------#-------#-------#-------#-------#
from __future__ import print_function
from os import environ
from sys import stderr, stdout

if bytes == str:
    BrokenPipeError = IOError
#-------#-------#-------#-------#-------#-------#-------#-------#-------#-------#
def dp(*m,**opt): return 0
def pv(*n,**opt): return 0
#-------#-------#-------#-------#-------#-------#-------#-------#-------#-------#
if 'nodebug' not in environ:
#-------#-------#-------#-------#-------#-------#-------#-------#-------#-------#
    def dp(*msg,**opt):
        opt['file'] = opt.get('file',stderr)
        try:
            rc = print(*msg,**opt)
            opt['file'].flush()
        except BrokenPipeError:
            pass
        opt.get('file',stdout).flush()
        return rc
#-------#-------#-------#-------#-------#-------#-------#-------#-------#-------#
    def pv(*names,**opt):
        from inspect import currentframe
        opt['file'], p = opt.get('file',stderr), opt.pop('prefix','')
        opt.get('file',stdout).flush()
        try:
            f = currentframe().f_back
            l, g, x = f.f_locals, f.f_globals, f.f_lineno
            print(p+'line',repr(x)+':',**opt)
            for name in names:
                s = name.split('.')
                n = s.pop(0)
                if n in l:
                    o, v = '... local var:', l[n]
                elif n in g:
                    o, v = '.. global var:', g[n]
                else:
                    print(p+'............. ',
                          str(name),
                          'not assigned in local or global namespace',
                          **opt)
                    opt['file'].flush()
                    continue
                while s:
                    m = s.pop(0)
                    if m in dir(v):
                        v, n = getattr(v,m), n + '.' + m
                    else:
                        break
                print(p+o,repr(n),'=',repr(v),**opt)
                opt['file'].flush()
                continue
        except BrokenPipeError:
            pass
        return
#-------#-------#-------#-------#-------#-------#-------#-------#-------#-------#
#        111111111122222222223333333333444444444455555555556666666666777777777788
#23456789012345678901234567890123456789012345678901234567890123456789012345678901
#=======#=======#=======#=======#=======#=======#=======#=======#=======#=======#
this a debugging aid.  there are 2 functions: dp() operates just like print() but is nullified by setting environment variable "nodebug" (yes, lower case) -and- pv() which is given 1 or more arguments of strings that name variables in the local namespace, the global namespace, or that are not assigned to either. each variable is printed on one line by itself.  the pv() function also prints a line with the line number.  it also supports the named option prefix= which is a string it prefixes on each line of output.  other named options that print() supports, such as file=, are passed along to print().
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

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