pycharm代码模板
时间: 2023-09-18 16:05:42 浏览: 104
以下是一些常用的 PyCharm 代码模板:
1. Python 文件头部注释
```python
# -*- coding:utf-8 -*-
"""
@Author: [Your Name]
@Date: [Date]
@Last Modified by: [Your Name]
@Last Modified time: [Date]
@Description: [Description]
"""
```
2. 类的定义
```python
class [ClassName]:
def __init__(self, [Arguments]):
[Constructor Code]
def [Method Name](self, [Arguments]):
[Method Code]
```
3. 函数的定义
```python
def [Function Name]([Arguments]):
[Function Code]
```
4. if 语句
```python
if [Condition]:
[Code]
else:
[Code]
```
5. for 循环
```python
for [Variable] in [List]:
[Code]
```
6. try-except 语句
```python
try:
[Code]
except [Exception Type]:
[Code]
```
7. with 语句
```python
with [Resource] as [Variable]:
[Code]
```
8. assert 语句
```python
assert [Condition], [Error Message]
```
9. logging 日志
```python
import logging
logging.basicConfig(
filename='[Log File Name]',
level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s'
)
logging.debug('[Debug Message]')
logging.info('[Info Message]')
logging.warning('[Warning Message]')
logging.error('[Error Message]')
logging.critical('[Critical Message]')
```
10. unittest 测试
```python
import unittest
class Test[ClassName](unittest.TestCase):
def setUp(self):
[Setup Code]
def tearDown(self):
[Teardown Code]
def test_[Method Name](self):
[Test Code]
if __name__ == '__main__':
unittest.main()
```
阅读全文