PKLe&VVdprint/__init__.py"""A printing debugging helper. """ __version__ = '0.1.0' from ._impl import dprint PKuLsCdprint/_impl.pyimport tokenize import inspect import token import sys import re import io import os _NAME_MATCHING_REGEX = re.compile(r'\bdprint\b') def dprint(value): """A simple printing debugging helper. Designed to be used on any expression, to print the value of an expression, without modifying the result of the same. In most cases, the only visible effect of this function is a call to __str__ of the passed value and a call to the builtin print function. """ frame_info = inspect.stack()[1] message = _construct_message(value, frame_info) # The parts that matter. print(message) return value def _construct_message(value, frame_info): """Construct a human readable message for context. """ # Parts filename_str = _format_filename(frame_info.filename) line_str = _format_lineno(frame_info.lineno) function_str = _format_function(frame_info.function) expression_str = _format_expression(frame_info.code_context) val_str = _format_value(value) # Put an arrow on it. context = filename_str + line_str + function_str if context: context += "\n" # Show the expression with the output. if expression_str: main_text = " {} -> {}".format(expression_str, val_str) else: main_text = " -> {}" + val_str return context + main_text def _format_filename(filename): """Format the filename in a nicer manner than given. Try to make the filename shorter when it makes sense to, without losing the clarity of what it means. """ if filename is None: return "" # A tiny helper def in_dir(dirpath, abspath): return dirpath == os.path.commonpath([dirpath, abspath]) abspath = os.path.abspath(filename) cwd = os.getcwd() # If it's in the current directory, return the path, with current directory # removed. if in_dir(cwd, abspath): return abspath[len(cwd) + 1:] # If it's importable, we show the path to it. for location in sys.path: if in_dir(location, abspath): fpath = abspath[len(location) + 1:] if fpath.endswith(".py"): fpath = fpath[:-3] return " " + fpath.replace(os.path.sep, ".") return abspath def _format_lineno(lineno): """Just convert the line number into a better form. """ if lineno is None: return "" return ":" + str(lineno) def _format_function(func_name): """Provide better context for a "function" of the caller. """ if func_name is None: return "" elif func_name == "": return " (top level stmt)" else: return " in " + func_name def _format_expression(code_context): """Provide the expression used to call dprint. Constraints: - Function must be called dprint - A call should span no more than 1 line - No more than 1 call on 1 line If these constraints are violated, the current implementation doesn't manage to extract the expression. """ if not code_context: return "" line = code_context[0] # Tokenize the line token_list = tokenize.tokenize(io.BytesIO(line.encode('utf-8')).readline) # Determine the start and end of expression start = None end = None level = 0 # because nesting for tok in token_list: # Looking for the start of string. if start is None: if tok.type == token.NAME and tok.string == "dprint": start = tok.start[1] # we get the proper value later. continue if end is None: if tok.type != token.OP: continue if tok.string == "(": if level == 0: # opening parens start = tok.end[1] level += 1 elif tok.string == ")": level -= 1 if level == 0: # closing parens end = tok.start[1] # This is fine since we don't need more information break return line[start:end] def _format_value(value): """Convert to a string or be very visibly wrong. """ try: val_str = repr(value) except Exception: val_str = "" return val_str PKL..dprint-0.1.0.dist-info/LICENSEMIT License Copyright (c) 2018, Pradyun Gedam Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.PK!Hp!Qadprint-0.1.0.dist-info/WHEEL HM K-*ϳR03rOK-J,/RH,zd&Y)r$[)T&UD"PK!Hz dprint-0.1.0.dist-info/METADATA}To0 ~_aiHl!4$8natm$ʏ {O|%zpϳhЪdk>`By6{y/Mn,a Auף4hsULqUQtηz(!(SbQך-ﵭ:El0Y3(h3B+gC|AWBrD+rLɡfKcaϗۣ"쿥^xd&ڀp 3v}@L4"oˇ5^asҁDlO"M)c-<މ-.#Pp=V7\ʉ*bv|dmSJ0#L,$Q)`TB6f@k0fˠ #S16$t5؂3|8=Ї]NG+8?c,&~?RQ17Őf:C7RS&_N|=gSfJG8_ \ 2s K0΢c,J DԱaЊ.HJ>..QI =bhǨ}s ;DClaMgd?*O-&֦@iECfM![[zJsz%gTTΛ$VH B(iז@SQ2#Zv)h,ݳ|Qϖ$,oe& PK!Hdv%dprint-0.1.0.dist-info/RECORDuKr0нg BO :,nF*FgieSU.mi5w