Skip to content

Instantly share code, notes, and snippets.

@kinchungwong
Last active March 1, 2024 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kinchungwong/24443565a75a73556d465ec0e50308bf to your computer and use it in GitHub Desktop.
Save kinchungwong/24443565a75a73556d465ec0e50308bf to your computer and use it in GitHub Desktop.
Python object type explorer
Credits, Acknowledgements and Similar Projects:
https://github.com/marcheiligers/python_type_explorer
class Oxr:
"""Oxr: object explorer.
"""
_name: str
_o: Any
_ids: set[int]
_oid_tid: dict[int, int]
_tid_name: dict[int, str]
_fqn_tid: dict[str, int]
_fqn_tdesc: dict[str, str]
def __init__(self, o: Any, name: str = None) -> None:
self._name = name or "_"
self._o = o
self._ids = set()
self._oid_tid = dict()
self._tid_name = dict()
self._fqn_tid = dict()
self._fqn_tdesc = dict()
self._index(self._name, o)
def _index(self, name: str, o: Any) -> None:
if name in self._fqn_tid:
return
# print(name)
if o is None:
return
oid = id(o)
if oid in self._ids:
return
t = type(o)
tid = id(t)
self._add_type(t)
self._ids.add(oid)
self._oid_tid[oid] = tid
self._fqn_tid[name] = tid
### https://stackoverflow.com/questions/1322068/determine-if-python-variable-is-an-instance-of-a-built-in-type
# print(name, self._tid_name[tid], o.__class__.__module__)
if isinstance(o, Mapping):
if name not in self._fqn_tdesc.keys():
ktset = set()
vtset = set()
for item_k, item_v in o.items():
if item_k is not None:
ktset.add(type(item_k))
if item_v is not None:
vtset.add(type(item_v))
self._fqn_tdesc[name] = "Mapping[%s, %s]" % (
", ".join(self._add_type(kt) for kt in ktset),
", ".join(self._add_type(vt) for vt in vtset),
)
print(name, self._fqn_tdesc[tid])
# for item_k, item_v in o.items():
# kfq = name + ".{k}."
# vfq = name + ".{v}."
# self._index(kfq, item_k)
# self._index(vfq, item_v)
elif isinstance(o, Sequence) and not isinstance(o, (str, bytes,)):
if name not in self._fqn_tdesc.keys():
itset = set()
for item in o:
if item is None:
continue
itset.add(type(item))
seqdesc = "".join((
"Sequence[",
", ".join(
self._add_type(it)
for it in itset
),
"]",
))
self._fqn_tdesc[name] = seqdesc
for item in o:
if item is None:
continue
self._index(name + "[]", item)
elif o.__class__.__module__ == "builtins":
return
else:
for aname in o.__dict__:
a = getattr(o, aname)
afq = name + "." + aname
self._index(afq, a)
def _add_type(self, t) -> str:
tid = id(t)
if tid in self._tid_name:
return self._tid_name[tid]
tname = str(t).replace("<class \'", "").replace("'>", "")
self._tid_name[tid] = tname
return tname
def __str__(self) -> str:
for fqn, tid in self._fqn_tid.items():
tname = self._tid_name[tid]
tdesc = self._fqn_tdesc.get(fqn)
print(fqn, ":", (tdesc or tname))
_ : openai.types.chat.chat_completion.ChatCompletion
_.id : str
_.choices : Sequence[openai.types.chat.chat_completion.Choice]
_.choices[] : openai.types.chat.chat_completion.Choice
_.choices[].finish_reason : str
_.choices[].index : int
_.choices[].logprobs : openai.types.chat.chat_completion.ChoiceLogprobs
_.choices[].logprobs.content : Sequence[openai.types.chat.chat_completion_token_logprob.ChatCompletionTokenLogprob]
_.choices[].logprobs.content[] : openai.types.chat.chat_completion_token_logprob.ChatCompletionTokenLogprob
_.choices[].logprobs.content[].token : str
_.choices[].logprobs.content[].bytes : Sequence[int]
_.choices[].logprobs.content[].bytes[] : int
_.choices[].logprobs.content[].logprob : float
_.choices[].logprobs.content[].top_logprobs : Sequence[openai.types.chat.chat_completion_token_logprob.TopLogprob]
_.choices[].logprobs.content[].top_logprobs[] : openai.types.chat.chat_completion_token_logprob.TopLogprob
_.choices[].logprobs.content[].top_logprobs[].token : str
_.choices[].logprobs.content[].top_logprobs[].bytes : Sequence[int]
_.choices[].logprobs.content[].top_logprobs[].bytes[] : int
_.choices[].logprobs.content[].top_logprobs[].logprob : float
_.choices[].message : openai.types.chat.chat_completion_message.ChatCompletionMessage
_.choices[].message.content : str
_.choices[].message.role : str
_.created : int
_.model : str
_.object : str
_.usage : openai.types.completion_usage.CompletionUsage
_.usage.completion_tokens : int
_.usage.prompt_tokens : int
_.usage.total_tokens : int
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment