New FAMILUG

The PyMiers

Thursday 29 June 2017

[Python] import sys

sys là thư viện builtin của Python, nó chứa các thông tin liên quan đến chính chương trình python interpreter bạn đang chạy, và cũng cho phép thay đổi một vài thông số. Nó không phải là thư viện chứa các thông tin liên quan đến hệ điều hành như nhiều người lầm tưởng (sys - system - và nghĩ là hệ điều hành).

help(sys) có thông tin đầy đủ về từng giá trị và function, hãy thử xem hệ thống của bạn có gì.

Script
import sys

SKIP = ('path', 'modules', '__doc__', 'path_importer_cache', 'meta_path')

for attr in sorted(sys.__dict__.items()):
    if not callable(attr[1]) and attr[0] not in SKIP:
        print(attr[0], ':', attr[1])

Kết quả khá dài, nhưng một vài thông tin đáng chú ý có thể kể tới:
- version: phiên bản Python đang dùng, bản C đã build chương trình Python interpreter đang chạy 
version : 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609]
- builtin_module_names: tên các module builtin
- platform : linux - nền tảng hệ điều hành đang chạy
- byteorder : little endian
- float_info : các thông số liên quan đến kiểu float

Kết quả



$ python look_sys.py
__name__ : sys
__package__ :
__spec__ : ModuleSpec(name='sys', loader=<class '_frozen_importlib.BuiltinImporter'>)
__stderr__ : <_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>
__stdin__ : <_io.TextIOWrapper name='<stdin>' mode='r' encoding='UTF-8'>
__stdout__ : <_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>
_mercurial : ('CPython', '', '')
_xoptions : {}
abiflags : m
api_version : 1013
argv : ['look_sys.py']
base_exec_prefix : /home/hvn/py35env
base_prefix : /home/hvn/py35env
builtin_module_names : ('_ast', '_bisect', '_codecs', '_collections', '_datetime', '_elementtree', '_functools', '_heapq', '_imp', '_io', '_locale', '_md5', '_operator', '_pickle', '_posixsubprocess', '_random', '_sha1', '_sha256', '_sha512', '_signal', '_socket', '_sre', '_stat', '_string', '_struct', '_symtable', '_thread', '_tracemalloc', '_warnings', '_weakref', 'array', 'atexit', 'binascii', 'builtins', 'errno', 'faulthandler', 'fcntl', 'gc', 'grp', 'itertools', 'marshal', 'math', 'posix', 'pwd', 'pyexpat', 'select', 'spwd', 'sys', 'syslog', 'time', 'unicodedata', 'xxsubtype', 'zipimport', 'zlib')
byteorder : little
copyright : Copyright (c) 2001-2016 Python Software Foundation.
All Rights Reserved.

Copyright (c) 2000 BeOpen.com.
All Rights Reserved.

Copyright (c) 1995-2001 Corporation for National Research Initiatives.
All Rights Reserved.

Copyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam.
All Rights Reserved.
dont_write_bytecode : False
exec_prefix : /home/hvn/py35env
executable : /home/hvn/py35env/bin/python
flags : sys.flags(debug=0, inspect=0, interactive=0, optimize=0, dont_write_bytecode=0, no_user_site=0, no_site=0, ignore_environment=0, verbose=0, bytes_warning=0, quiet=0, hash_randomization=1, isolated=0)
float_info : sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)
float_repr_style : short
hash_info : sys.hash_info(width=64, modulus=2305843009213693951, inf=314159, nan=0, imag=1000003, algorithm='siphash24', hash_bits=64, seed_bits=128, cutoff=0)
hexversion : 50660080
implementation : namespace(_multiarch='x86_64-linux-gnu', cache_tag='cpython-35', hexversion=50660080, name='cpython', version=sys.version_info(major=3, minor=5, micro=2, releaselevel='final', serial=0))
int_info : sys.int_info(bits_per_digit=30, sizeof_digit=4)
maxsize : 9223372036854775807
maxunicode : 1114111
path_hooks : [<class 'zipimport.zipimporter'>, <function FileFinder.path_hook.<locals>.path_hook_for_FileFinder at 0x7f678431f950>]
platform : linux
prefix : /home/hvn/py35env
real_prefix : /usr
stderr : <_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>
stdin : <_io.TextIOWrapper name='<stdin>' mode='r' encoding='UTF-8'>
stdout : <_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>
thread_info : sys.thread_info(name='pthread', lock='semaphore', version='NPTL 2.23')
version : 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609]
version_info : sys.version_info(major=3, minor=5, micro=2, releaselevel='final', serial=0)
warnoptions : []

Một số functions: (từ help(sys))
Functions:

displayhook() -- print an object to the screen, and save it in builtins._
excepthook() -- print an exception and its traceback to sys.stderr
exc_info() -- return thread-safe information about the current exception
exit() -- exit the interpreter by raising SystemExit
getdlopenflags() -- returns flags to be used for dlopen() calls
getprofile() -- get the global profiling function
getrefcount() -- return the reference count for an object (plus one :-)
getrecursionlimit() -- return the max recursion depth for the interpreter
getsizeof() -- return the size of an object in bytes
gettrace() -- get the global debug tracing function
setcheckinterval() -- control how often the interpreter checks for events
setdlopenflags() -- set the flags to be used for dlopen() calls
setprofile() -- set the global profiling function
setrecursionlimit() -- set the max recursion depth for the interpreter
settrace() -- set the global debug tracing function


Xem chi tiết tại: https://docs.python.org/3/library/sys.html

Hết.
HVN at https://pymi.vn

No comments:

Post a Comment