Python

Administrator
发布于 2023-12-15 / 34 阅读
0
0

Python

Python

配置源

	https://pypi.tuna.tsinghua.edu.cn/simple

虚拟环境

	python3.8 -m venv balsa-env
	source balsa-env/bin/activate

Python调试

vscode python 调试

vscode 只支持python 3.7以上的版本,低于该版本需要升级,通过右下脚查看python是否满足条件

1. 安装python 插件

在扩展中搜索python安装即可

2. 配置调试文件

python 调试有三种文件

2.1 调试当前文件

点击调试当前文件,基本上不需要改动,如果需要添加参数,可添加args

	{
		"version":"0.2.0",
		"configurations":[
			{
				"name":"Python: 当前文件",
				...,
				"args":["--run", "Balsa_JOBRandomSplit", "--local"]
			}
		]
	}
2.2 调试模块

点击左边的调试,选择模块调试,在默认模版的基础上添加module,args和env等

	{
		"version":"0.2.0",
		"configurations":[
			{
				"name":"Python: 模块",
				...,
				"module":"mathtranslate.translate_tex",
				"args":["--debug", "--nocache"],
				"env":{
					"http_proxy": "http://127.0.0.1:8118",
					"https_proxy": "https://127.0.0.1:8118",
				},
				"justMyCode": true
			}
		]
	}
2.3 调试正在运行的python进程
	{
		"version":"0.2.0",
		"configurations":[
			{
				"name":"Python: Attach using Process Id",
				"type": "python",
				"request":"attach",
				"processId":"${command:piickProcess}",
				"justMyCode": true
			}
		]
	}

pdb 调试

安装pdb

	yum install python-epdb

使用pdb

    # 非侵入式
    python3 -m pdb xxx.py
    # 侵入式
    import pdb; pdb.set_trace()

Python 基础

列表推导式

	[表达式 for 迭代变量 in 可迭代对象 [if 条件表达式]]
    [if 条件表达式] 不是必须的,可以使用,也可以省略
    
    #  支持多层循环
    src_a = [30, 12, 66, 34, 39, 78, 36, 57, 121]
    src_b = [3, 5, 7, 11]
    # 只要y能整除x,就将它们配对在一起
    result = [(x, y) for x in src_b for y in src_a if y % x == 0]
    print(result)
    => [(3, 30), (3, 12), (3, 66), (3, 39), (3, 78), (3, 36), (3, 57), (5, 30), (11, 66), (11, 121)]

Fancier Output Formatting

	year = 2016
	event = 'Referendum'
	f'Results of the {year} {event}'
	=> 'Results of the 2016 Referendum'


	yes_votes = 42_572_654
	no_votes = 43_132_495
	percentage = yes_votes / (yes_votes + no_votes)
	'{:-9} YES votes  {:2.2%}'.format(yes_votes, percentage)
	=> ' 42572654 YES votes  49.67%'

Formatted String Literals

also called f-strings for short

	import math
	print(f'The value of pi is approximately {math.pi:.3f}.')
	=> The value of pi is approximately 3.142.


	table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678}
	for name, phone in table.items():
	    print(f'{name:10} ==> {phone:10d}')

	
	=> Sjoerd     ==>       4127
	=> Jack       ==>       4098
	=> Dcab       ==>       7678


	animals = 'eels'
	print(f'My hovercraft is full of {animals}.')
	=> My hovercraft is full of eels.

	print(f'My hovercraft is full of {animals!r}.')
	=> My hovercraft is full of 'eels'.

	bugs = 'roaches'
	count = 13
	area = 'living room'
	print(f'Debugging {bugs=} {count=} {area=}')
	=> Debugging bugs='roaches' count=13 area='living room'

The String format() Method

	print('We are the {} who say "{}!"'.format('knights', 'Ni'))
	=> We are the knights who say "Ni!"
	
	print('{0} and {1}'.format('spam', 'eggs'))
	=> spam and eggs
	print('{1} and {0}'.format('spam', 'eggs'))
	=> eggs and spam
	
	print('This {food} is {adjective}.'.format(
	      food='spam', adjective='absolutely horrible'))
	=> This spam is absolutely horrible.
	
	print('The story of {0}, {1}, and {other}.'.format('Bill', 'Manfred',
	                                                   other='Georg'))
	=> The story of Bill, Manfred, and Georg.
	
	table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
	print('Jack: {0[Jack]:d}; Sjoerd: {0[Sjoerd]:d}; '
	      'Dcab: {0[Dcab]:d}'.format(table))
	=> Jack: 4098; Sjoerd: 4127; Dcab: 8637678
	
	table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
	print('Jack: {Jack:d}; Sjoerd: {Sjoerd:d}; Dcab: {Dcab:d}'.format(**table))
	=> Jack: 4098; Sjoerd: 4127; Dcab: 8637678
	
	for x in range(1, 11):
	    print('{0:2d} {1:3d} {2:4d}'.format(x, x*x, x*x*x))
	
	=> 1   1    1
	=> 2   4    8
	=> 3   9   27
	=> 4  16   64
	=> 5  25  125
	=> 6  36  216
	=> 7  49  343
	=> 8  64  512
	=> 9  81  729
	=> 10 100 1000

Python 导入

同目录导入

A_F1.py 导入 A_F2.py

	import A_F2

上级目录导入

A/A_F1.py 导入 F1.py

	import sys 
	sys.path.append("..") 
	import F1

下级目录导入

在下级目录下建一个空的 __init__.py 文件

在 test/F1.py 导入A中的A_F1.py

	from A import A_F1

相邻目录导入

在引入的相邻目录建立空的 __init__.py 文件

A/A_F1.py 导入 B/B_F1.py

	import sys 
	sys.path.append("..") 
	from B import B_F1

引用

Python-import导入上级目录文件


评论