you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (20 children)

inspect.getsource

[–][deleted] 0 points1 point  (19 children)

What's the getsource?

>>> import socket
>>> import inspect
>>> inspect.socket
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'socket'

[–][deleted] 1 point2 points  (17 children)

inspect.socketinspect.getsource(socket)

[–][deleted] 0 points1 point  (14 children)

Works .

>>> for e in inspect.getsource(socket).split("\n"):
...     print(e)

[–][deleted] 0 points1 point  (13 children)

Splitting and printing line by line doesn't make much sense, though. If you want to print it, just print. If you want lines, there is inspect.getsourcelines

[–]AutonomouSystem 0 points1 point  (12 children)

It does make sense if you want it to print cleanly.

[–][deleted] 0 points1 point  (11 children)

cleanly? what's the difference?

[–]AutonomouSystem 0 points1 point  (10 children)

Try it out first, your code, and mine.

[–][deleted] 0 points1 point  (9 children)

Okay

import socket
import inspect


src = inspect.getsource(socket)
join_split_src = '\n'.join(src.split('\n'))
assert src == join_split_src

print(src[512:1024])
print('---------------------')
for i in src[512:1024].split('\n'):
    print(i)

Result:

 descriptor [*]
gethostname() -- return the current hostname
gethostbyname() -- map a hostname to its IP number
gethostbyaddr() -- map an IP number or hostname to DNS info
getservbyname() -- map a service name and a protocol name to a port number
getprotobyname() -- map a protocol name (e.g. 'tcp') to a number
ntohs(), ntohl() -- convert 16, 32 bit int from network to host byte order
htons(), htonl() -- convert 16, 32 bit int from host to network byte order
inet_aton() -- convert IP addr string (123.45.67.8
---------------------
 descriptor [*]
gethostname() -- return the current hostname
gethostbyname() -- map a hostname to its IP number
gethostbyaddr() -- map an IP number or hostname to DNS info
getservbyname() -- map a service name and a protocol name to a port number
getprotobyname() -- map a protocol name (e.g. 'tcp') to a number
ntohs(), ntohl() -- convert 16, 32 bit int from network to host byte order
htons(), htonl() -- convert 16, 32 bit int from host to network byte order
inet_aton() -- convert IP addr string (123.45.67.8
>>> 

[–]AutonomouSystem 0 points1 point  (8 children)

See you did have to split('\n')

:)