1. 某些.py文件第一行中 #/usr/bin/python3 的作用(针对unix/linux系统)

#!/usr/bin/python3 是告诉操作系统执行这个脚本的时候,调用 /usr/bin 下的 python3解释器。
添加之后,在命令行中可以像运行shell脚本一样运行python文件
python test.py./test.py

2. gitpython与pygithub
最近在工作中与这两个库打交道较多。
gitpython库适合本地与远程仓库之间交互的操作,比如clone, pull, checkout, push等。可以理解成是git工具和python的结合。
pygithub库是用python来调用github的api,更多是一些在github平台上的操作,例如approve,review,获取用户/团队/组织信息等操作。

From github import Github

gh_object = Github(base_url="",login_or_token="")

3.python set
python使用set可以去除所有的重复元素

4. 理解指针
链表反转代码:

def  reverse_list(ListNode: head) -> ListNode:
	# initial the new head
	reverse_head = None
	# initial the node for iteration
	current = head
	while current:
		# restore the next node. Avoid the loss of pointer
		temp_next = current.next
		current.next = reverse_head
		reverse_head = current
		current = temp_next
	return reverse_head

最关键的点: 如何理解current.next?
current.next 在右边,表示的是current.next这个node. (值)
current.next 在左边,表示的是current.next指向一个新的引用 (指针)

5. annotation

class Student(object):
	@property
	def birth(self):
		return self._birth
	
	@birth.setter
	def birth(self, value):
		self._birth = value

6. datetime

import datetime

now = datetime.datetime.now()

print
print "Current date and time using str method of datetime object:"
print str(now)

print
print "Current date and time using instance attributes:"
print "Current year: %d" % now.year
print "Current month: %d" % now.month
print "Current day: %d" % now.day
print "Current hour: %d" % now.hour
print "Current minute: %d" % now.minute
print "Current second: %d" % now.second
print "Current microsecond: %d" % now.microsecond

print
print "Current date and time using strftime:"
print now.strftime("%Y-%m-%d %H:%M")

print
print "Current date and time using isoformat:"
print now.isoformat()

7. logging

1. logging模块的日志级别
logging模块默认定义了以下几个日志等级,它允许开发人员自定义其他日志级别,但是这是不被推荐的,尤其是在开发供别人使用的库时,因为这会导致日志级别的混乱。
日志等级(level)	描述
DEBUG	最详细的日志信息,典型应用场景是 问题诊断
INFO	信息详细程度仅次于DEBUG,通常只记录关键节点信息,用于确认一切都是按照我们预期的那样进行工作
WARNING	当某些不期望的事情发生时记录的信息(如,磁盘可用空间较低),但是此时应用程序还是正常运行的
ERROR	由于一个更严重的问题导致某些功能不能正常运行时记录的信息
CRITICAL	当发生严重错误,导致应用程序不能继续运行时记录的信息
开发应用程序或部署开发环境时,可以使用DEBUG或INFO级别的日志获取尽可能详细的日志信息来进行开发或部署调试;应用上线或部署生产环境时,应该使用WARNING或ERROR或CRITICAL级别的日志来降低机器的I/O压力和提高获取错误日志信息的效率。日志级别的指定通常都是在应用程序的配置文件中进行指定的。

8. exit

1. sys.exit(n) 退出程序引发SystemExit异常, 可以捕获异常执行些清理工作. n默认值为0, 表示正常退出. 其他都是非正常退出. 还可以sys.exit("sorry, goodbye!"); 一般主程序中使用此退出
2. os._exit(n), 直接退出, 不抛异常, 不执行相关清理工作. 常用在子进程的退出.
3. exit()/quit(), 跑出SystemExit异常. 一般在交互式shell中退出时使用.

9. special text
raw string: r’string’, r before string
unicode string: u’string’, u before string

10. python path

print(os.path.dirname(os.path.abspath(__file__)))#return the absolute path of current py file
print(os.getcwd())
#return the absolute path where run the py file
#example:
$python a.py
/home/XXXX/pythonDemo (the location of py file)
/home/XXXX  (where the command running)

11. urlopen problem
urlopen hang problem
description: program crash, no raised error nor resp in urlopen().read()
reason: 1.network disconnected 2. no timeout parameter is set
solution:
1. set timeout when calling urlopen
2. set global socket timeout
3. set timer when calling urlopen

12. pip install

pip install -r requirement.txt
	#install pip packages according to the file
	
	#python run os commands
	a. os.system()
	#easy 
	b. subprocess module
	#more flexible for use