Commit 466049fe authored by Grant Paton-Simpson's avatar Grant Paton-Simpson

Remove need for file_path in superhelp.this; allow r as var

parent b5af851c
Pipeline #580 failed with stages
# https://git.nzoss.org.nz/pyGrant/superhelp
version number: 0.9.18
version number: 0.9.19
author: Grant Paton-Simpson
## Overview
......@@ -21,7 +21,7 @@ in e.g. get advice on a snippet or line of Python
or put the following at the top of your Python script and run the script:
import superhelp
superhelp.this(__file__)
superhelp.this()
## Installation
......@@ -102,22 +102,22 @@ The notebook has more detailed instructions at the top.
Put the following at the top of your script and then run the script (note - there are two underscores on either side of file):
import superhelp
superhelp.this(__file__)
superhelp.this()
If you don't want the default web output you can specify another displayer such as 'cli' (command line interface) or 'md' (markdown):
import superhelp
superhelp.this(__file__, displayer='md')
superhelp.this(displayer='md')
If you don't want the default 'Extra' level of messages you can specify a different message_level ('Brief' or 'Main') e.g.
import superhelp
superhelp.this(__file__, displayer='md', message_level='Brief')
superhelp.this(displayer='md', message_level='Brief')
or:
import superhelp
superhelp.this(__file__, message_level='Main')
superhelp.this(message_level='Main')
### From the command line (terminal / console)
......
......@@ -2,7 +2,7 @@ from setuptools import setup, find_packages # @UnresolvedImport
from codecs import open
from os import path
__version__ = '0.9.18'
__version__ = '0.9.19'
here = path.abspath(path.dirname(__file__))
......
......@@ -257,8 +257,8 @@ def short_name_check(block_dets, *, repeat=False):
longer names should be used when they aid readability (i.e. usually
;-)).
`k` and `v` are idiomatic in Python when iterating through dictionary
items e.g.
`k` and `v` are idiomatic in Python when iterating through
dictionary items e.g.
""")
+
layout("""\
......@@ -271,6 +271,16 @@ def short_name_check(block_dets, *, repeat=False):
But even they should probably be replaced with something more
descriptive.
And there are other cases specific to certain contexts e.g. using r
for the result of a request e.g.
""")
+
layout("""\
r = requests.get('https://pypi.org/project/superhelp/')
""", is_code=True)
+
layout("""\
The main goal is readability, readability, readability. That is what
should drive variable naming above all else. Only a modest weight
should be given to speed of typing in any code that is going to be
......
import argparse
import inspect
import logging
try:
......@@ -99,11 +100,39 @@ def get_advice(snippet=None, *, file_path=None, displayer='html',
if in_notebook:
return res
def this(file_path, *, displayer='html', message_level=conf.EXTRA):
def _get_introspected_file_path():
"""
Yes - if you import this later I've wrecked it but it is more important to
allow superhelp.this() :-).
The actual call we are interested in isn't necessarily the second one (e.g.
console first then actual script) so we have to explicitly filter for it. In
pydev, for example, it was the third item.
https://stackoverflow.com/questions/13699283/how-to-get-the-callers-filename-method-name-in-python
wasn't correct but gave some hints that I could build upon
"""
for item in inspect.stack():
has_superhelp_this = (
item.code_context is not None
and 'superhelp.this' in ''.join(item.code_context)) ## seems to be a list of one item in each case
if has_superhelp_this:
calling_item = item
break
else: ## didn't break for-loop
raise Exception('Unable to identify calling script through '
'introspection so file_path=__file__ will need to be explicitly '
'supplied e.g. superhelp.this(file_path=__file__)')
file_path = calling_item.filename
return file_path
def this(*, displayer='html', message_level=conf.EXTRA, file_path=None):
"""
Get SuperHELP output on the file_path Python script.
Yes - if you "import this" later I've shaded it in this namespace by calling
this function, err, this. But I thought it more important to expose the
simple superhelp.this() interface than namespace purity :-).
"""
if not file_path:
file_path = _get_introspected_file_path()
get_advice(snippet=None, file_path=file_path, displayer=displayer,
message_level=message_level, in_notebook=False)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment