Show HN: Wat – Python ऑब्जेक्ट्स की गहन जांच
(github.com/igrek51)- WAT Python runtime में अज्ञात objects की पहचान जल्दी समझने के लिए एक inspector है, जिससे type, value, attributes, methods, parent types, signature, documentation और source code तक एक साथ देखा जा सकता है
- इसका मूल उपयोग
wat / objectहै, जोwat(object)जैसा ही काम करता है, औरwat.short / 'foo','foo' | wat.short,wat('foo', short=True)जैसी कई syntaxes को support करता है .short,.dunder,.long,.code,.caller,.public,.all,.ret,.strजैसे modifiers को chain करके output scope, return method, color output और call location display को adjust किया जा सकता है- installation
pip install watके बादimport watसे किया जा सकता है, और quick debugging के लिए Insta-Load snippet को Python session में paste करके उसी session में बिना install किए भी इस्तेमाल किया जा सकता है - Django
User,re.match,pathlib,colorsys.hsv_to_rgb,typing.List[str],str | Noneजैसे examples दिखाते हैं कि WAT debugging, REPL exploration और Python internals सीखने में उपयोगी हो सकता है
WAT क्या करता है
- WAT Python objects को runtime पर explore और inspect करने का tool है
- जब यह समझना मुश्किल हो कि कोई अज्ञात object क्या है, तो Python console में
watinspector से उस object की पहचान की जांच की जा सकती है - किसी भी
objectपरwat / objectचलाने से निम्न जानकारी देखी जा सकती है- object का type
- formatted value
- variables और methods
- parent types
- signature
- documentation
- source code
- वही गहन inspection
wat(object)syntax से भी इस्तेमाल किया जा सकता है Watको Englishwhatका variation बताया गया है, जो confusion या annoyance व्यक्त करने के लिए इस्तेमाल होने वाला शब्द है
मूल उपयोग और syntax
- जल्दी input देने के लिए division operator का उपयोग करता है
wat / foo,wat(foo)के समान है
- समान inspection के लिए कई syntaxes इस्तेमाल की जा सकती हैं
wat.short / 'foo': quick input के लिए syntaxwat.short('foo')wat('foo', short=True): natural Python syntax'foo' | wat.short: Unix pipe style syntax
wat.modifier / fooform से inspection behavior adjust किया जा सकता है- modifiers को chain किया जा सकता है; example
wat.short.str.gray / 'foo'है - Python में objects में सिर्फ data structures ही नहीं, बल्कि functions, classes, modules, built-in types आदि भी शामिल होते हैं, इसलिए
watकिसी भी object को explore कर सकता है - interpreter में
wattype करने परwatobject के बारे में help देखी जा सकती है
Modifiers से inspection scope adjust करना
.shortया.sobject के अंदर variables और methods जैसे attributes छिपाकर केवल value, type, parent types, signature और documentation output करता है.dunder,__से शुरू होने वाले dunder attributes दिखाता है.longabbreviated न किए गए value और docstring दिखाता है.codefunctions, methods और classes का source code दिखाता है.nodocsfunctions और classes की documentation छिपाता है.callerदिखाता है कि inspection कैसे और कहां call हुआ, और यह REPL के बजाय files में काम करता है.publicprivate attributes छिपाकर सिर्फ public attributes दिखाता है.allसभी संभव जानकारी शामिल करता है.retinspection के बाद object को फिर से return करता है.stroutput के बजाय result string return करता है.grayconsole में color output disable करता है.colorconsole में color output force करता हैwat.localslocal variables inspect करता है, औरwat.globalsglobal variables inspect करता है
Installation और Insta-Load
- pip installation flow इस प्रकार है
pip install wat- Python में
import wat
watpackage की external dependencies नहीं हैं- quick debugging के लिए उसी Python session में बिना install किए इस्तेमाल करने योग्य Insta-Load method देता है
- Insta-Load में
base64,zlibimport करने के बाद compressed और encoded code string को restore करकेexec(..., globals())से चलाने वाला Python snippet interpreter में paste किया जाता है - Insta-Load snippet चलाने के बाद
watobject इस्तेमाल किया जा सकता है - snippet चलाने से पहले execute होने वाली चीज़ को verify करने की सिफारिश की जाती है
print(zlib.decompress(base64.b64decode(code)).decode())से extracted code content पहले देखा जा सकता हैinspection.pyकी contents को interpreter में paste करने से भी वही effect मिलता है- pip से package install करके code review करने का तरीका भी बताया गया है
- WAT को single Unicode glyph से load किया जा सकता है
- Unicode string-based loader लंबी emoji और combining character string को
ord(c) & 255से bytes में बदलकरzlib.decompress(...)के बादexec(...)से run करने के form में है
Object type और usage समझना
- Python जैसी dynamically typed language में object type समझना कभी-कभी कठिन हो सकता है, और WAT Inspector type name तथा वह type किस module से आया है, दिखाता है
- type checking examples value, type और length साथ में दिखाते हैं
wat.short / (1,)value(1,), typetuple, length1output करता हैwat.short / {None}value{None}, typeset, length1output करता है
- Django
Userobject example मेंwat.short / user,str: admin,repr: <User: admin>, typedjango.contrib.auth.models.Userऔर parent type list output करता है - actual type की पुष्टि करने के बाद code में type annotations डालकर आगे की confusion कम की जा सकती है
- अज्ञात object का usage समझते समय method list, signatures और docstring output किए जा सकते हैं
- example के रूप में
wat / ['foo']दिया गया है - पूरा docstring देखना हो तो
wat.longइस्तेमाल करें
- example के रूप में
- function usage समझने के लिए function की docstring और signature देखी जा सकती है
- example के रूप में
wat / str.splitदिया गया है
- example के रूप में
Attributes, modules और source code explore करना
- inspect किए जा रहे object के अंदर देखने के लिए attributes और प्रत्येक attribute का type list किया जा सकता है
- example के रूप में
wat / re.match('(\d)_(.*)', '1_title')दिया गया है
- example के रूप में
- modules explore करने के लिए भी इसका उपयोग किया जा सकता है, और चुने गए module के functions, classes और submodules list किए जा सकते हैं
import pathlibके बादwat / pathlibचलाने का example है- इसके बाद
wat / pathlib.fnmatchकी तरह और गहराई में explore किया जा सकता है
- WAT Inspector default रूप से
__से शुरू होने वाले attributes छिपाता हैwat.dunder / {}से dunder attributes देखे जा सकते हैं
- कोई function असल में कैसे काम करता है, यह देखने के लिए source code देखा जा सकता है
import colorsysके बादwat.code / colorsys.hsv_to_rgbचलाने का example है
- nested dict और list indentation वाले readable form में format होते हैं
Debugging session और variable inspection
- Python के
breakpoint()से interactive debugger चलाने के बाद, उसी जगह object inspect किया जा सकता है - Pdb example में
import watया Insta-Load snippet paste करने के बादwat / fooसे local variable inspect किया जाता है, औरcसे execution continue होता है - local variables और global variables क्रमशः
wat.locals,wat.globalsसे देखे जा सकते हैं wat()को बिना arguments call करने पर caller stack के local variablesLocal variablestitle के साथ output होते हैं
Python internals सीखने के examples
- Python internals को समझने के learning-use examples शामिल हैं
reversed([]) == reversed([])Falseहै, औरwat.s / reversed([])दिखाता है कि valuelist_reverseiteratorobject है और typelist_reverseiteratorहैwat / type('ObjectCreator', (), {})dynamically बनाई गई class की value, typetype,signature: class ObjectCreator()दिखाता हैwat / typeखुदtypeकी value, typetype,class type(…)signature,type(object) -> the object's type,type(name, bases, dict, **kwds) -> a new typedocumentation, public attributemroआदि दिखाता हैwat.s / List[str]valuetyping.List[str], typetyping._GenericAlias, parent typestyping._BaseGenericAlias,typing._Final, signaturedef List(*args, **kwargs)दिखाता हैwat(str | None)valuestr | None, typetypes.UnionTypeदिखाता है- Python built-in objects explore करने के examples के रूप में
wat / __builtins__,wat / ...दिए गए हैं - WAT खुद को भी inspect किया जा सकता है
- example के रूप में
wat.dunder / wat,wat.code / wat.__truediv__दिए गए हैं
- example के रूप में
Internal behavior summary
inspect_format(obj, *, short=False, dunder=False, nodocs=False, long=False, code=False, caller=False, public=False, all=False)object inspection result को string के रूप में बनाता हैall=Trueहोने परdunder,long,code,callerसाथ में activate होते हैंpublic=Trueहोने पर private output deactivate होता हैsys.stdout.isatty()true होने पर terminal width ली जाती है और output के ऊपर-नीचे─separators जोड़े जाते हैं
- inspection output object value, string representation, type, parent types, length, signature, documentation, source code, attributes section के क्रम में generate होता है
- attribute inspection
dir(obj)को name order में iterate करता है- dunder attributes,
dundersetting off होने पर exclude होते हैं _से शुरू होने वाले private attributes, private setting off होने पर exclude होते हैंgetattr(obj, key)मेंBaseExceptionआने पर exception object को value के रूप में इस्तेमाल किया जाता है
- dunder attributes,
- callable objects के लिए
inspect.signature(obj)के आधार पर signature format होता है- failure पर
(...)form की fallback signature return करता है - classes में
class, coroutine function मेंasync def, functions, methods, builtins और__name__वाले objects मेंdefprefix जोड़ा जाता है
- failure पर
code=Trueऔर object class या callable होने परinspect.getsource(obj)से source code output होता हैOSError,TypeError,IndentationErrorहोने पर failure message return करता है
- dict और list formatters indentation depth 30 से अधिक होने पर
ERROR: too deeply nestedreturn करते हैं
Color output और theme
- environment variables से color output control किया जा सकता है
WAT_COLOR="false"console का color output disable करता हैWAT_COLOR="true"non-tty environment में भी color output force करता है
WAT_COLORSenvironment variable से color theme customize की जा सकती है- default theme
BAR=0;34,TRAIT=1;34,HEAD=1;37,STR=0;32,NUMBER=0;31,NONE=0;35,TRUE=1;32,FALSE=1;31,DOCS=2;37,KEYWORD=0;34,CALLABLE=1;32,VARIABLE=1;33,CODE=0;33form की ANSI color code mapping है _strip_color(text)regex से ANSI escape sequence हटाता है
प्रेरणा
- WAT को Rich Inspect से inspiration मिली है
1 टिप्पणियां
Hacker News की टिप्पणियां
वाह, सच में अच्छा है। पहले इसी तरह के काम के लिए python-ls[0] इस्तेमाल करता था, लेकिन किसी याद न रहने वाली वजह से कुछ टूट गया और अब वह maintain भी नहीं होता
इसे अपने debugging टूलबॉक्स में जोड़ने वाला हूं, जो मुख्य तौर पर snoop[1] और pdbpp से बना है। wat से मेरी इच्छा बस इतनी है कि Jupyter में object exploration आसान करने वाला कोई ipy widget हो
base64 exec hack भी पसंद आया। Python लंबे समय से इस्तेमाल कर रहा हूं, फिर भी अब तक इसके बारे में न सोचा था न देखा था, इसलिए आगे कुछ उपयोगों में इसे जरूर आजमाऊंगा
[0] https://github.com/gabrielcnr/python-ls
[1] https://pypi.org/project/snoop/
दिलचस्प लग रहा है। Python में dir हमेशा इस्तेमाल करता हूं, और जब docs खास अच्छी नहीं होतीं तो कभी-कभी official docs से भी ज्यादा उपयोगी होता है
interactive shell Python की असली ताकतों में से एक है, इसलिए हैरानी होती है कि उसके आसपास ऐसे नए tools या innovations ज्यादा नहीं हैं
पुराने icecream का ज्यादा चमकदार version लगता है
https://github.com/gruns/icecream
अगर नहीं जानते, तो नीचे दूसरे languages के implementations की list भी देख सकते हैं
https://github.com/gruns/icecream#icecream-in-other-language...
इस तरह के tools उपयोगी होते हैं
20 साल पहले मैंने Zope के लिए object introspector बनाया था
आजकल devtools रोज इस्तेमाल करता हूं, और icecream व q कभी-कभी। wat भी एक बार आजमाऊंगा
from wat import watproject का स्वभाव इतना मजेदार है, फिर भी हैरानी है कि उसी usage syntax के साथ सिर्फ
import watक्यों नहीं दिया। तब जिज्ञासु users wat/wat करके trick खोज सकते थेimport watअच्छा होता, लेकिन Python में module को callable बनाने की सीमा है। इसलिए लंबेfrom wat import watपर जाना पड़ापक्का नहीं, लेकिन
import wat; wat.wat / objectशायद ज्यादा सुविधाजनक हो सकता हैबहुत उपयोगी दिखता है, लेकिन क्या सिर्फ मुझे ही readability के नाम पर बिल्कुल असंबंधित operators—यहां
/operator—को overload करने का हालिया चलन खटकता है?/overload करना अजीब choice है। फिर भी अफसोस है किisको overload नहीं किया जा सकता। असल मेंwat(foo)ही काफी होताझंझट वाले import से बचने के लिए
$PYTHONSTARTUPfile में यह भी जोड़ सकते हैंtry:from wat import watexcept ImportError:passआखिर में मैंने उसका output print करके उसे उस directory में रख दिया, जिसकी ओर
PYTHONPATHpoint करता है, ताकि हमेशा इस्तेमाल कर सकूंदेखना होगा कि लगातार इस्तेमाल करूंगा या नहीं
वाह, Python सीखते समय ऐसा tool होता तो शायद game changer होता। भाषा सीखते समय अंदर क्या हो रहा है यह देखना critical path है, और Python की default debugging, अच्छी से अच्छी हालत में भी, निराशाजनक है
इसके बजाय मैंने pry install किया और Ruby का उत्साही fan बन गया, लेकिन यह tool मुझे Python को फिर से try करने पर मजबूर कर सकता है
लेखक functionality देने के लिए अंदर से standard library का Python inspect module इस्तेमाल कर रहे हैं। बेशक उसके ऊपर काफी added value भी दी है
wat module की
inspection.pyदेखेंदूसरी line में यह है:
import inspect as std_inspect“अगर आप जल्दी से कुछ debug करना चाहते हैं, तो उसी session में कुछ भी install किए बिना इस inspector का इस्तेमाल कर सकते हैं”
“इस snippet को Python interpreter में paste करके तुरंत load करें”
project README में पूरे project की copy को base64-encoded compressed data के रूप में डालने का idea काफी चतुर है
खासकर ऐसे project के लिए यह ठीक बैठता है, जिसे शायद आप पहले से उन environments में डालने के बारे में न सोचें जहां इसकी जरूरत पड़ेगी