explmod.py

created by andreanx

created on August 27, 2019

1.8 KB

explmod(module) to list the module content.


#platforms:
#0: MicroPython / TI-Nspire
#1: MicroPython / NumWorks
#2: MicroPython / G90+E / G35+E II
#3: MicroPython / G35+E/USB / G75/85/95
#4: CircuitPython / TI-Python / 83PCE / Trinket M0
#5: Xcas / HP Prime
#6: KhiCAS / Graph 90+E
def getplatform():
  id=-1
  try:
    import sys
    try:
      if sys.platform=='nspire':id=0
      if sys.platform.startswith('TI-Python') or sys.platform=='Atmel SAMD21':id=4
    except:id=3
  except:
    try:
      import kandinsky
      id=1
    except:
      try:
        if chr(256)==chr(0):id=5+(not ("HP" in version()))
      except:
        id=2
  return id

platform=getplatform()
#lines shown on screen
plines=[29,12,  7, 9,11,0,0]
#max chars per line
#(error or CR if exceeded)
pcols =[53,99,509,32,32,0,0]

nlines=plines[platform]
ncols=pcols[platform]
curline=0

def mprint(*ls):
  global curline
  st=""
  for s in ls:
    if not(isinstance(s,str)):
      s=str(s)
    st=st+s
  stlines=1+int(len(st)/ncols)
  if curline+stlines>=nlines:
    input("Input to continue:")
    curline=0
  print(st)
  curline+=stlines

def sstr(obj):
  try:
    s=obj.__name__
  except:
    s=str(obj)
    a=s.find("'")
    b=s.rfind("'")
    if a>=0 and b!=a:
      s=s[a+1:b]
  return s

def isExplorable(obj):
  s=str(obj)
  return s.startswith("<module '") or s.startswith("<class '")

def explmod(pitm,pitmsl=[],reset=True):
  global curline
  if(reset):
    curline=0
    pitmsl=[sstr(pitm)]
  hd="."*(len(pitmsl)-1)
  spath=".".join(pitmsl)
  c=0
  for itms in sorted(dir(pitm)):
    c=c+1
    try:
      itm=eval(spath+"."+itms)
      mprint(hd+itms+"="+str(itm))
      if isExplorable(itm):
        pitmsl2=pitmsl.copy()
        pitmsl2.append(itms)
        c=c+explmod(itm,pitmsl2,False)
    except:
      mprint(hd+itms)
  if c>0:
    mprint(hd+"Total: "+str(c)+" item(s)")
  return c