Home > Mobile >  Is there a way to access what help() prints and set it to a variable?
Is there a way to access what help() prints and set it to a variable?

Time:11-10

For example I am using pymeasure and would like to create a way for a user to check weather the device they want to use is accessible in pymeasure. rather than accessing the docstring i want to access the list of packages in the module that help prints. to do this i want do:

print(help("pymeasure.instruments"))

which outputs:



Help on package pymeasure.instruments in pymeasure:

NAME
    pymeasure.instruments

DESCRIPTION
    # This file is part of the PyMeasure package.
    #
    # Copyright (c) 2013-2022 PyMeasure Developers......

PACKAGE CONTENTS
    advantest (package)
    agilent (package)
    ametek (package)
    ami (package)
    anaheimautomation (package)
    anapico (package)
    andeenhagerling (package)
    anritsu (package)
    attocube (package)
    bkprecision (package)
    comedi
    danfysik (package)
    deltaelektronika (package)
    edwards (package)
    fakes
    fluke (package)
    fwbell (package)
    heidenhain (package)
    hp (package)
    instrument
    keithley (package)
    keysight (package)
    lakeshore (package)
    newport (package)
    ni (package)
    oxfordinstruments (package)
    parker (package)
    pendulum (package)
    razorbill (package)
    resources
    rohdeschwarz (package)
    signalrecovery (package)
    srs (package)
    tektronix (package)
    thorlabs (package)
    toptica (package)
    validators
    yokogawa (package)

FILE
    c:\users\mnowshad\appdata\local\programs\python\python39\lib\site-packages\pymeasure\instruments\__init__.py


None

I want to take every item under PACKAGE CONTENTS and make it a list so a user can check if a manufacturer is in that list.

I tried setting a variable to print(help("")), and print(str(help(""))) and help("") none of those work as the variable is always type none

CodePudding user response:

Reading through the source code of the help() function, it looks like it uses pkgutil.iter_modules() to find the modules which are part of pymeasure.instruments, and generate that help file.

Here's how to get all of the submodules of a module.

import pkgutil
module = pymeasure.instruments
print([pkg.name for pkg in pkgutil.iter_modules(module.__path__)])

Which prints:

['advantest', 'agilent', 'ametek', 'ami', 'anaheimautomation', 'anapico', 'andeenhagerling', 'anritsu', 'attocube', 'bkprecision', 'comedi', 'danfysik', 'deltaelektronika', 'edwards', 'fakes', 'fluke', 'fwbell', 'heidenhain', 'hp', 'instrument', 'keithley', 'keysight', 'lakeshore', 'newport', 'ni', 'oxfordinstruments', 'parker', 'pendulum', 'razorbill', 'resources', 'rohdeschwarz', 'signalrecovery', 'srs', 'tektronix', 'thorlabs', 'toptica', 'validators', 'yokogawa']

CodePudding user response:

Good question, try following any of these steps, I did not try myself, but I guess you will find something here:

https://www.folkstalk.com/2022/10/python-export-console-output-to-file-with-code-examples.html

  • Related