pycharm's python_stubs problem

When writing the udp socket program recently, when I left-clicked several functions (such as sendto) command+, I found that I jumped to this python file. (The default should be to the function source code)

/PyCharm2018.1/python_stubs/160944109/_socket.py

The following pseudo functions are defined

 def send(self, data, flags=None): # real signature unknown; restored from __doc__
 """
 send(data[, flags])-  count

 Send a data string to the socket. For the optional flags
 argument, see the Unix manual. Return the number of bytes
 sent;this may be less than len(data)if the network is busy."""
 pass

 def sendall(self, data, flags=None): # real signature unknown; restored from __doc__
 """
 sendall(data[, flags])

 Send a data string to the socket. For the optional flags
 argument, see the Unix manual. This calls send() repeatedly
 until all data is sent. If an error occurs, it's impossible
 to tell how much data has been sent."""
 pass

I googled it and found that someone had already asked related questions on stackoverflow. I will briefly refine it here.

https://stackoverflow.com/questions/24266114/pycharm-what-is-python-stubs

Under what circumstances will there be a jump to python_stubs?

When the function we call is a built-in function or a function that only exists in binary (no py file, only pyc, etc.), pycharm will hard-code a certain version to generate a pseudo function (actually not called this, but it is just convenient for us to do Development).

Think about pycharm's painstaking effort for our convenience.

Supplementary knowledge: Python3 custom log class mylog

Let's look at the code directly!

# encoding=utf-8import os, sys
import datetime
import time
classMylog(object):
# Root folder
root_dir = sys.path[0]
# Root directory
root_path = sys.path[0]+ os.path.sep
# System directory dividing line
sys_sep = os.path.sep
# Configuration
option ={
# Log level: 0: all, 1: debugging, 2: warning, 3: error
' level':0,
# Whether to open, if closed, neither output nor log
' is_open': True,
# Whether to print output
' is_print': True,
# Whether to log to the log file
' is_write': True,
# Whether to prefix each log content
' is_prefix': True,
# If you enable the prefix before each log, set the prefix of log level to 1
' level_1_prefix':'Test: ',
# If you enable the prefix before each log, set the prefix of log level to 2
' level_2_prefix':'Warning: ',
# If you enable prefixing each log, set the prefix of log level to 3
' level_3_prefix':'Error: ',
# The name of the root folder where log files are stored
' root_dir_name':'mylog',
# Customize the file name for storing log files, this folder is in root_dir_name folder
' dir_name':''}
def __init__(self, config=None):if config is not None:
self.option.update(dict(config))
# Folder where logs are saved (full path)
save_dir = self.root_path + self.option['root_dir_name']
# Create folder
if os.path.isdir(save_dir) is not True:
os.mkdir(save_dir)iflen(self.option['dir_name'])0:
save_dir += self.sys_sep + self.option['dir_name']if os.path.isdir(save_dir) is not True:
os.mkdir(save_dir)
self.save_dir = save_dir
self.save_path = save_dir + self.sys_sep
'''
Input log / record log
'''
def log(self, content='', level=0):
self._print_log(content, level)
self._write_log(content, level)'''
Input log
'''
def _print_log(self, content, level):if self.option['is_open'] is True and self.option['is_print'] is True:if self.option['level']==0 or self.option['level']== level:if level   0:
content = self.option['level_'+str(level)+'_prefix']+ content
print(content)'''
Log
'''
def _write_log(self, content, level):if self.option['is_open'] is True and self.option['is_print'] is True:if self.option['level']==0 or self.option['level']== level:if self.option['is_prefix'] is True:
today = datetime.date.today()
file_name =str(today)+'.log'
now = time.strftime("%H:%M:%S")
log_file = self.save_path + file_name
if level   0:
content = self.option['level_'+str(level)+'_prefix']+ content
if os.path.isfile(log_file):
save_file =open(log_file,'a')else:
save_file =open(log_file,'w')
save_file.write(str(now)+"\r\n"+ content +"\r\n")
save_file.close()**Important content
#! /usr/bin/env python
#- *- coding: GBK -*-
__ author__ ='DiaoHuabin'import logging
import getpass
import sys
# Define the MyLog class
classMyLog(object):'''This class is used to create a log for personal use'''
def __init__(self): #Constructor of class MyLog
user = getpass.getuser() #Returns the user's login name
self.logger = logging.getLogger(user) #Return a log with a specific name
self.logger.setLevel(logging.DEBUG) #Set a threshold value lower than DEBUG level for the displayed log information to not display
logFile ='./'+sys.argv[1][0:-3]+'.log' #Log file name
formatter = logging.Formatter('%(asctime)-12s $(levelname)-8s %(name)-10s %(message)-12s')'''The log is displayed on the screen and output to the log file'''
logHand = logging.FileHandler(logFile) #Output log file, the file name is logFile
logHand.setFormatter(formatter) #Format logHand in formatter
logHand.setLevel(logging.ERROR) #Only errors are recorded in the logfile
logHandSt = logging.StreamHandler() #classlogging.StreamHandler(stream=None)
# Return an instance of the StreamHandler class, if the stream is determined, use the stream as the log output, otherwise, use
# sys.stderr
logHandSt.setFormatter(formatter) #Format logHandSt in formatter
self.logger.addHandler(logHand) #Add a specific handler logHand to the log file logger
self.logger.addHandler(logHandSt) #Add a specific handler logHandSt to the log file logger
''' The 5 levels of the log correspond to the following five functions'''
def debug(self,msg):
self.logger.debug(msg) #Logs a message with level DEBUG on this logger.
# The msg is the message format string
def info(self,msg):
self.logger.info(msg)
def warn(self,msg):
self.logger.warn(msg)
def error(self,msg):
self.logger.error(msg)
def critical(self,msg):
self.logger.critical(msg)if __name__ =='__main__':
mylogw =MyLog()
mylogw.debug("I'm debug")
mylogw.info("I'm info")
mylogw.warn("I'm warn")
mylogw.error("I'm error")
mylogw.critical("I'm critical")

The above pycharm python_stubs problem is all the content shared by the editor, I hope to give you a reference.

Recommended Posts

pycharm's python_stubs problem