Search K
Appearance
Appearance
扩展技术栈
职业发展
技术互补
对比学习
实践导向
重点把握
# 安装 Python 扩展
# 安装 Jupyter
pip install jupyter
# 检查是否安装成功
jupyter --version
# 启动 Notebook
jupyter notebook
my-python-project/
├── .venv/ # 虚拟环境
├── src/ # 源代码
│ ├── __init__.py
│ └── main.py
├── tests/ # 测试文件
│ ├── __init__.py
│ └── test_main.py
├── requirements.txt # 依赖清单
├── README.md # 项目文档
└── setup.py # 打包配置
特性 | JavaScript | Python |
---|---|---|
类型系统 | 动态弱类型 | 动态强类型 |
编程范式 | 多范式,偏函数式 | 多范式,偏面向对象 |
并发模型 | 事件循环,异步 | 多线程,同步为主 |
包管理 | npm,中心化 | pip,分散式 |
主要应用 | 前端,Node.js后端 | 数据科学,后端,自动化 |
JavaScript 与 Python 的类型对比:
JavaScript | Python | 说明 |
---|---|---|
Number | int, float | Python 区分整数和浮点数 |
String | str | Python 的字符串更强大 |
Boolean | bool | Python 使用 True/False |
null | None | Python 使用 None |
undefined | 不存在 | Python 没有 undefined |
Array | list | Python 的列表更灵活 |
Object | dict | Python 使用字典 |
Set | set | 两种语言都有集合 |
无对应 | tuple | Python 特有的不可变序列 |
JavaScript:
// 数字
const num = 42;
const float = 3.14;
// 字符串
const str = "Hello";
const template = `Value: ${num}`;
// 数组
const arr = [1, 2, 3];
arr.push(4);
// 对象
const obj = {
name: "John",
age: 30
};
Python:
# 数字
num = 42
float_num = 3.14
# 字符串
string = "Hello"
template = f"Value: {num}"
# 列表
lst = [1, 2, 3]
lst.append(4)
# 字典
dict_obj = {
"name": "John",
"age": 30
}
# 元组(JavaScript没有对应类型)
point = (1, 2)
# 集合
unique_items = {1, 2, 3}
# 创建
lst = [1, 2, 3]
# 添加元素
lst.append(4) # [1, 2, 3, 4]
lst.insert(0, 0) # [0, 1, 2, 3, 4]
# 删除元素
lst.pop() # 删除并返回最后一个元素
lst.remove(2) # 删除第一个值为2的元素
# 切片
first_two = lst[:2] # 获取前两个元素
reversed_lst = lst[::-1] # 反转列表
# 创建
dict_obj = {"name": "John", "age": 30}
# 访问
name = dict_obj["name"] # 如果键不存在会抛出异常
age = dict_obj.get("age", 0) # 提供默认值
# 添加/修改
dict_obj["city"] = "New York"
dict_obj.update({"country": "USA", "age": 31})
# 删除
del dict_obj["age"]
city = dict_obj.pop("city") # 删除并返回值
# 创建
set1 = {1, 2, 3}
set2 = {3, 4, 5}
# 集合运算
union = set1 | set2 # 并集
intersection = set1 & set2 # 交集
difference = set1 - set2 # 差集
# 创建
point = (1, 2)
coords = 1, 2, 3 # 括号可选
# 解包
x, y = point
a, *rest = coords # rest = [2, 3]
# 命名元组
from collections import namedtuple
Person = namedtuple('Person', ['name', 'age'])
person = Person('John', 30)
print(person.name) # 'John'
JavaScript:
async function getData() {
try {
const response = await fetch(url);
const data = await response.json();
return data;
} catch (error) {
console.error(error);
}
}
Python:
async def get_data():
try:
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
return await response.json()
except Exception as error:
print(error)
JavaScript:
const user = {
name: 'John',
greet() {
return `Hello, ${this.name}!`;
}
};
Python:
user = {
'name': 'John',
'greet': lambda: f"Hello, {user['name']}!"
}
print(user['greet']()) # 输出:Hello, John!
大部分情况下都是靠使用类class,开实现对象
# 类和方法
class User:
def __init__(self, name):
self.name = name
def greet(self):
return f"Hello, {self.name}!"
user = User('John')
print(user.greet()) # 输出:Hello, John!
JavaScript 与 Python 的类型转换对比:
// 字符串转数字
const num1 = parseInt("42");
const num2 = +"42";
const float = parseFloat("3.14");
// 数字转字符串
const str1 = String(42);
const str2 = 42 + "";
// 数组转字符串
const arr = [1, 2, 3];
const str3 = arr.join(",");
# 字符串转数字
num1 = int("42")
float_num = float("3.14")
# 数字转字符串
str1 = str(42)
# 列表转字符串
lst = [1, 2, 3]
str2 = ",".join(map(str, lst))
# 字符串转列表
str3 = "1,2,3"
lst2 = str3.split(",")
lst3 = list(str3) # ['1', ',', '2', ',', '3']
Python 的字符串操作比 JavaScript 更丰富:
# 格式化
name = "John"
age = 30
# f-string (推荐)
message1 = f"Name: {name}, Age: {age}"
# format 方法
message2 = "Name: {}, Age: {}".format(name, age)
# % 操作符
message3 = "Name: %s, Age: %d" % (name, age)
# 常用方法
text = " Hello, World! "
print(text.strip()) # 删除首尾空白
print(text.lower()) # 转小写
print(text.upper()) # 转大写
print(text.replace(",", "")) # 替换
print(text.split(",")) # 分割
# 多行字符串
multi_line = """
这是一个
多行字符串
"""
这部分内容涵盖了 Python 基础数据类型的主要操作。接下来要继续补充条件与循环部分吗?
// if 语句
if (condition) {
// code
} else if (otherCondition) {
// code
} else {
// code
}
// 三元运算符
const result = condition ? "yes" : "no";
// switch 语句
switch (value) {
case 1:
// code
break;
case 2:
// code
break;
default:
// code
}
# if 语句
if condition:
# code
elif other_condition:
# code
else:
# code
# 三元运算符
result = "yes" if condition else "no"
# match 语句 (Python 3.10+)
match value:
case 1:
# code
case 2:
# code
case _:
# code
# 多条件判断
if 0 <= x <= 10: # Python 支持链式比较
print("在范围内")
JavaScript:
// 遍历数组
const arr = [1, 2, 3];
for (let i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
// for...of
for (const item of arr) {
console.log(item);
}
// for...in(用于对象)
const obj = {a: 1, b: 2};
for (const key in obj) {
console.log(key, obj[key]);
}
// forEach
arr.forEach((item, index) => {
console.log(item, index);
});
Python:
# 遍历列表
lst = [1, 2, 3]
for item in lst:
print(item)
# 带索引的遍历
for index, item in enumerate(lst):
print(index, item)
# 范围遍历
for i in range(5): # 0 到 4
print(i)
# 字典遍历
d = {'a': 1, 'b': 2}
for key in d:
print(key, d[key])
# 同时遍历键值
for key, value in d.items():
print(key, value)
JavaScript:
let i = 0;
while (i < 5) {
console.log(i);
i++;
}
// do...while
let j = 0;
do {
console.log(j);
j++;
} while (j < 5);
Python:
i = 0
while i < 5:
print(i)
i += 1
# Python 特有的 while-else
i = 0
while i < 5:
if i == 3:
break
print(i)
i += 1
else:
print("循环正常完成") # break 时不执行
JavaScript 和 Python 的循环控制语句对比:
// JavaScript
for (let i = 0; i < 5; i++) {
if (i === 2) continue;
if (i === 4) break;
console.log(i);
}
# Python
for i in range(5):
if i == 2:
continue
if i == 4:
break
print(i)
Python 提供了强大的推导式语法,这在 JavaScript 中没有直接对应:
# 列表推导式
squares = [x**2 for x in range(10)]
evens = [x for x in range(10) if x % 2 == 0]
# 字典推导式
square_dict = {x: x**2 for x in range(5)}
# 集合推导式
unique_squares = {x**2 for x in range(10)}
# 生成器表达式
sum_squares = sum(x**2 for x in range(10))
JavaScript 等效实现:
// 使用 map 和 filter
const squares = Array.from({length: 10}, (_, i) => i ** 2);
const evens = Array.from({length: 10}).filter((_, i) => i % 2 === 0);
// 对象字面量
const squareDict = Object.fromEntries(
Array.from({length: 5}, (_, i) => [i, i ** 2])
);
语法结构
特有功能
循环方式
性能考虑
JavaScript:
// 基本函数
function greet(name) {
return `Hello, ${name}!`;
}
// 箭头函数
const greet = (name) => {
return `Hello, ${name}!`;
};
// 默认参数
function greet(name = "Guest") {
return `Hello, ${name}!`;
}
// 剩余参数
function sum(...numbers) {
return numbers.reduce((a, b) => a + b, 0);
}
Python:
# 基本函数
def greet(name):
return f"Hello, {name}!"
# lambda 函数(类似箭头函数): lambda 参数: 表达式
greet = lambda name: f"Hello, {name}!"
# 默认参数
def greet(name="Guest"):
return f"Hello, {name}!"
# 可变参数
def sum(*numbers):
return sum(numbers)
# 关键字参数
def person(**kwargs):
return kwargs
Python:
def user_info(name, age, city="Unknown"):
return f"{name}, {age}, {city}"
# 调用方式
user_info("John", 30) # 位置参数
user_info(age=30, name="John") # 关键字参数
user_info("John", age=30, city="NYC") # 混合使用
# 强制关键字参数
def config(*, host, port): # * 后的参数必须使用关键字
return f"{host}:{port}"
config(host="localhost", port=8000) # 正确
config("localhost", 8000) # 错误
JavaScript:
const numbers = [1, 2, 3];
const config = { host: 'localhost', port: 8000 };
// 数组解包
Math.max(...numbers);
// 对象解包
const server = { ...config, ssl: true };
Python:
numbers = [1, 2, 3]
config = {"host": "localhost", "port": 8000}
# 列表解包
print(*numbers)
# 字典解包
server = {**config, "ssl": True}
# 函数调用中的解包
def setup(host, port):
return f"{host}:{port}"
setup(**config)
JavaScript:
// 单值返回
function getValue() {
return 42;
}
// 多值返回(通过对象或数组)
function getCoords() {
return { x: 10, y: 20 };
// 或
return [10, 20];
}
Python:
# 单值返回
def get_value():
return 42
# 多值返回(自动打包为元组)
def get_coords():
return 10, 20 # 返回元组 (10, 20)
# 解包返回值
x, y = get_coords()
JavaScript:
let globalVar = "global";
function outer() {
let outerVar = "outer";
function inner() {
let innerVar = "inner";
console.log(globalVar, outerVar, innerVar);
}
inner();
}
Python:
global_var = "global"
def outer():
outer_var = "outer"
def inner():
inner_var = "inner"
print(global_var, outer_var, inner_var)
inner()
# 使用全局变量
def modify_global():
global global_var
global_var = "modified"
# 使用外层函数变量
def outer():
count = 0
def inner():
nonlocal count
count += 1
return inner
装饰器本质上是一个函数或类,它接受一个函数或类作为输入,并返回一个经过修改的函数或类。
装饰器基本语法:
@decorator_name
def function_name():
pass
等价于
def function_name():
pass
function_name = decorator_name(function_name)
def class_decorator(cls):
cls.is_decorated = True
return cls
@class_decorator
class MyClass:
pass
print(MyClass.is_decorated) # True
def my_decorator(func):
def wrapper():
print("装饰器执行前")
func()
print("装饰器执行后")
return wrapper
@my_decorator
def say_hello():
print("Hello, World!")
say_hello()
JavaScript:
// 闭包
function counter() {
let count = 0;
return {
increment() { count += 1; },
get() { return count; }
};
}
Python:
# 闭包
def counter():
count = 0
def increment():
nonlocal count
count += 1
return count
return increment
函数定义
def
关键字参数处理
返回值
作用域
global
和 nonlocal
关键字JavaScript:
// 导出
export const PI = 3.14;
export function add(a, b) {
return a + b;
}
export default class Calculator {}
// 导入
import Calculator, { PI, add } from './math';
import * as mathUtils from './math';
Python:
# math.py
PI = 3.14
def add(a, b):
return a + b
class Calculator:
pass
# 导入
from math import PI, add
from math import Calculator
import math
from math import * # 不推荐
JavaScript 项目结构:
my-project/
├── package.json
├── node_modules/
├── src/
│ ├── index.js
│ ├── utils/
│ │ ├── index.js
│ │ └── math.js
│ └── components/
└── dist/
Python 项目结构:
my_project/
├── setup.py
├── requirements.txt
├── my_package/
│ ├── __init__.py
│ ├── module1.py
│ └── subpackage/
│ ├── __init__.py
│ └── module2.py
└── tests/
# my_package/__init__.py
from .module1 import function1
from .subpackage.module2 import function2
__all__ = ['function1', 'function2']
from setuptools import setup, find_packages
setup(
name="my_package",
version="0.1.0",
packages=find_packages(),
install_requires=[
'requests>=2.25.1',
'pandas>=1.2.0',
],
)
JavaScript:
// 动态导入
async function loadModule() {
const module = await import('./module.js');
module.someFunction();
}
// 条件导入
if (condition) {
import('./feature.js').then(module => {
module.feature();
});
}
Python:
# 动态导入
import importlib
math = importlib.import_module('math')
sin = getattr(math, 'sin')
# 条件导入
try:
import numpy as np
except ImportError:
print("NumPy is not installed")
JavaScript:
// 模块作用域
const privateVar = 'private'; // 模块私有
export const publicVar = 'public';
// 命名空间
export namespace Math {
export const PI = 3.14;
export function add(a, b) {
return a + b;
}
}
Python:
# 模块作用域
_private_var = 'private' # 约定私有
public_var = 'public'
# 使用 __all__ 控制导出
__all__ = ['public_var', 'public_function']
# 命名空间包
# math/
# ├── __init__.py
# ├── constants.py
# └── functions.py
JavaScript (package.json):
{
"name": "my-project",
"version": "1.0.0",
"dependencies": {
"express": "^4.17.1",
"lodash": "^4.17.21"
},
"devDependencies": {
"jest": "^27.0.6"
}
}
Python (requirements.txt):
requests==2.26.0
pandas>=1.3.0
numpy==1.21.2
pytest>=6.0.0,<7.0.0
Python 特有的虚拟环境概念:
# 创建虚拟环境
python -m venv myenv
# 激活虚拟环境
# Windows
myenv\Scripts\activate
# Unix
source myenv/bin/activate
# 安装依赖
pip install -r requirements.txt
# 导出依赖
pip freeze > requirements.txt
模块系统
包管理
导入机制
最佳实践
JavaScript:
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
return `Hello, I'm ${this.name}`;
}
static create(name, age) {
return new Person(name, age);
}
}
const person = new Person("John", 30);
Python:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
return f"Hello, I'm {self.name}"
@classmethod
def create(cls, name, age):
return cls(name, age)
person = Person("John", 30)
JavaScript:
class Animal {
constructor(name) {
this.name = name;
}
speak() {
return "Some sound";
}
}
class Dog extends Animal {
speak() {
return "Woof!";
}
}
class Cat extends Animal {
speak() {
return "Meow!";
}
}
Python:
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return "Some sound"
class Dog(Animal):
def speak(self):
return "Woof!"
class Cat(Animal):
def speak(self):
return "Meow!"
# 多重继承(JavaScript不支持)
class Pet(Animal, Playable):
pass
Python 特有的魔术方法:
class Vector:
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self):
return f"Vector({self.x}, {self.y})"
def __repr__(self):
return f"Vector(x={self.x}, y={self.y})"
def __add__(self, other):
return Vector(self.x + other.x, self.y + other.y)
def __len__(self):
return 2
def __getitem__(self, key):
if key == 0:
return self.x
elif key == 1:
return self.y
raise IndexError("Vector index out of range")
v1 = Vector(1, 2)
v2 = Vector(3, 4)
v3 = v1 + v2 # 使用 __add__
print(len(v1)) # 使用 __len__
print(v1[0]) # 使用 __getitem__
Python 的属性访问控制:
class Person:
def __init__(self, name):
self._name = name
@property
def name(self):
return self._name
@name.setter
def name(self, value):
if not value:
raise ValueError("Name cannot be empty")
self._name = value
@name.deleter
def name(self):
del self._name
person = Person("John")
print(person.name) # 使用 getter
person.name = "Jane" # 使用 setter
del person.name # 使用 deleter
JavaScript:
class Utils {
static add(a, b) {
return a + b;
}
static factory() {
return new Utils();
}
}
Python:
class Utils:
@staticmethod
def add(a, b):
return a + b
@classmethod
def factory(cls):
return cls()
@property
def value(self):
return self._value
JavaScript:
class Account {
#balance = 0; // 私有字段
#validateAmount(amount) { // 私有方法
return amount > 0;
}
deposit(amount) {
if (this.#validateAmount(amount)) {
this.#balance += amount;
}
}
}
Python:
class Account:
def __init__(self):
self.__balance = 0 # 名称修饰实现私有
def __validate_amount(self, amount): # 私有方法
return amount > 0
def deposit(self, amount):
if self.__validate_amount(amount):
self.__balance += amount
JavaScript:
// 使用 TypeScript 或注释表示抽象
class AbstractShape {
constructor() {
if (new.target === AbstractShape) {
throw new Error("Cannot instantiate abstract class");
}
}
area() {
throw new Error("Method 'area()' must be implemented");
}
}
Python:
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self):
pass
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14 * self.radius ** 2
类定义
self
显式表示实例访问控制
方法类型
特殊特性
JavaScript (Node.js):
const fs = require('fs');
const csv = require('csv-parser');
// 读取 CSV
fs.createReadStream('data.csv')
.pipe(csv())
.on('data', (row) => {
console.log(row);
})
.on('end', () => {
console.log('CSV file successfully processed');
});
// 写入 CSV
const createCsvWriter = require('csv-writer').createObjectCsvWriter;
const csvWriter = createCsvWriter({
path: 'output.csv',
header: [
{id: 'name', title: 'NAME'},
{id: 'age', title: 'AGE'}
]
});
const records = [
{name: 'John', age: 30},
{name: 'Jane', age: 25}
];
csvWriter.writeRecords(records);
Python:
import pandas as pd
import csv
# 使用 pandas 读取 CSV
df = pd.read_csv('data.csv')
print(df.head()) # 查看前几行
print(df.describe()) # 基本统计信息
# 使用内置 csv 模块
with open('data.csv', 'r') as file:
csv_reader = csv.DictReader(file)
for row in csv_reader:
print(row)
# 写入 CSV
data = [
{'name': 'John', 'age': 30},
{'name': 'Jane', 'age': 25}
]
with open('output.csv', 'w', newline='') as file:
writer = csv.DictWriter(file, fieldnames=['name', 'age'])
writer.writeheader()
writer.writerows(data)
Python 的文件打开模式:
# 读取模式 'r'
with open('file.txt', 'r') as f:
content = f.read() # 读取全部内容
# 或者按行读取
for line in f:
print(line.strip())
# 写入文件
with open('output.txt', 'w', encoding='utf-8') as f:
f.write('Hello World')
# 追加模式 'a'
with open('output.txt', 'a', encoding='utf-8') as f:
f.write('\nNew line')
# 二进制模式 'b'
with open('image.jpg', 'rb') as f:
binary_data = f.read()
# 文本和二进制模式组合
with open('file.bin', 'wb') as f:
f.write(bytes([65, 66, 67])) # 写入 ABC
Python 提供了丰富的文件操作方法:
with open('file.txt', 'r') as f:
# 读取操作
content = f.read() # 读取全部
line = f.readline() # 读取一行
lines = f.readlines() # 读取所有行到列表
# 文件指针操作
f.seek(0) # 移动到文件开头
f.tell() # 获取当前位置
# 迭代读取
for line in f:
print(line.strip())
JavaScript (Node.js):
const path = require('path');
// 路径拼接
const fullPath = path.join(__dirname, 'folder', 'file.txt');
// 获取文件信息
const ext = path.extname('file.txt');
const basename = path.basename('path/to/file.txt');
const dirname = path.dirname('path/to/file.txt');
Python:
from pathlib import Path
# 创建路径对象
path = Path('folder/file.txt')
current_dir = Path.cwd()
home_dir = Path.home()
# 路径操作
full_path = current_dir / 'folder' / 'file.txt'
parent_dir = path.parent
file_name = path.name
stem = path.stem
extension = path.suffix
# 路径检查
path.exists()
path.is_file()
path.is_dir()
JavaScript (Node.js):
const fs = require('fs');
// 创建目录
fs.mkdirSync('new_folder');
// 读取目录内容
const files = fs.readdirSync('folder');
// 删除文件或目录
fs.unlinkSync('file.txt');
fs.rmdirSync('empty_folder');
Python:
from pathlib import Path
import shutil
# 创建目录
Path('new_folder').mkdir(exist_ok=True)
Path('nested/folders').mkdir(parents=True, exist_ok=True)
# 列出目录内容
path = Path('folder')
for item in path.iterdir():
print(item)
# 查找文件
python_files = list(path.glob('**/*.py')) # 递归查找
txt_files = list(path.glob('*.txt')) # 当前目录
# 删除操作
path = Path('file.txt')
path.unlink() # 删除文件
shutil.rmtree('folder') # 递归删除目录
Python 的 shutil 模块提供了高级文件操作:
import shutil
# 复制文件
shutil.copy('source.txt', 'dest.txt')
shutil.copy2('source.txt', 'dest.txt') # 保留元数据
# 复制目录
shutil.copytree('src_dir', 'dst_dir')
# 移动文件或目录
shutil.move('old_path', 'new_path')
# 删除目录及其内容
shutil.rmtree('directory')
# 获取磁盘使用情况
total, used, free = shutil.disk_usage('/')
Python:
import tempfile
# 创建临时文件
with tempfile.NamedTemporaryFile() as tmp:
tmp.write(b'Hello World')
tmp.flush()
# 文件会在退出 with 块时自动删除
# 创建临时目录
with tempfile.TemporaryDirectory() as tmpdir:
path = Path(tmpdir) / 'file.txt'
path.write_text('Hello World')
文件操作模式
路径处理
异常处理
实用功能
JavaScript:
try {
// 可能抛出错误的代码
throw new Error("Something went wrong");
} catch (error) {
console.error(error.message);
} finally {
// 清理代码
console.log("Cleanup");
}
Python:
try:
# 可能抛出异常的代码
raise ValueError("Something went wrong")
except ValueError as e:
print(f"Caught ValueError: {e}")
except Exception as e:
print(f"Caught other exception: {e}")
else:
print("No exception occurred")
finally:
print("Cleanup")
JavaScript 的错误类型:
// 常见内置错误
throw new Error(); // 基础错误
throw new TypeError(); // 类型错误
throw new ReferenceError(); // 引用错误
throw new SyntaxError(); // 语法错误
throw new RangeError(); // 范围错误
// 自定义错误
class CustomError extends Error {
constructor(message) {
super(message);
this.name = 'CustomError';
}
}
Python 的异常类型:
# 常见内置异常
raise Exception() # 基础异常
raise ValueError() # 值错误
raise TypeError() # 类型错误
raise KeyError() # 键错误
raise IndexError() # 索引错误
raise AttributeError() # 属性错误
raise ImportError() # 导入错误
raise FileNotFoundError() # 文件未找到错误
# 自定义异常
class CustomError(Exception):
def __init__(self, message):
self.message = message
super().__init__(self.message)
Python 特有的异常链:
try:
try:
raise ValueError("Original error")
except ValueError as e:
raise RuntimeError("Handling error") from e
except RuntimeError as e:
print(f"Current error: {e}")
print(f"Original error: {e.__cause__}")
JavaScript:
async function fetchData() {
try {
const response = await fetch(url);
const data = await response.json();
return data;
} catch (error) {
if (error instanceof TypeError) {
console.error("Network error:", error);
} else {
console.error("Other error:", error);
}
throw error;
}
}
Python:
import requests
def fetch_data():
try:
response = requests.get(url)
response.raise_for_status()
return response.json()
except requests.ConnectionError as e:
print(f"Network error: {e}")
raise
except requests.HTTPError as e:
print(f"HTTP error: {e}")
raise
except requests.RequestException as e:
print(f"Other error: {e}")
raise
Python 的 with 语句:
class ResourceManager:
def __enter__(self):
print("Acquiring resource")
return self
def __exit__(self, exc_type, exc_value, traceback):
print("Releasing resource")
if exc_type is not None:
print(f"Handled error: {exc_value}")
return True # 抑制异常
# 使用上下文管理器
with ResourceManager() as resource:
# 使用资源
pass
JavaScript:
let resource;
try {
resource = acquireResource();
// 使用资源
} finally {
if (resource) {
resource.release();
}
}
Python:
try:
resource = acquire_resource()
# 使用资源
finally:
if resource:
resource.release()
Python 推荐 EAFP (Easier to Ask for Forgiveness than Permission):
# EAFP 风格
try:
value = dict_data["key"]
except KeyError:
value = default_value
# LBYL 风格 (不推荐)
if "key" in dict_data:
value = dict_data["key"]
else:
value = default_value
Python:
def find_index(lst, value):
try:
return lst.index(value)
except ValueError:
return -1
def process_file(filename):
try:
with open(filename) as f:
return f.read()
except FileNotFoundError:
return None
语法差异
try
/except
而不是 try
/catch
异常类型
异常处理方式
最佳实践
JavaScript 装饰器(目前是提案阶段):
// 类装饰器
function logged(target) {
return class extends target {
constructor(...args) {
super(...args);
console.log(`Created new instance of ${target.name}`);
}
};
}
// 方法装饰器
function log(target, name, descriptor) {
const original = descriptor.value;
descriptor.value = function(...args) {
console.log(`Calling ${name} with:`, args);
return original.apply(this, args);
};
return descriptor;
}
@logged
class Example {
@log
method(x) {
return x * 2;
}
}
Python 装饰器:
# 函数装饰器
def log_decorator(func):
def wrapper(*args, **kwargs):
print(f"Calling {func.__name__} with args: {args}, kwargs: {kwargs}")
result = func(*args, **kwargs)
print(f"Function {func.__name__} returned: {result}")
return result
return wrapper
# 类装饰器
def singleton(cls):
instances = {}
def get_instance(*args, **kwargs):
if cls not in instances:
instances[cls] = cls(*args, **kwargs)
return instances[cls]
return get_instance
# 使用装饰器
@log_decorator
def add(a, b):
return a + b
@singleton
class Database:
def __init__(self):
self.connected = False
Python:
def repeat(times):
def decorator(func):
def wrapper(*args, **kwargs):
for _ in range(times):
result = func(*args, **kwargs)
return result
return wrapper
return decorator
@repeat(3)
def greet(name):
print(f"Hello {name}")
class cached_property:
def __init__(self, func):
self.func = func
self.name = func.__name__
def __get__(self, instance, owner=None):
if instance is None:
return self
result = instance.__dict__[self.name] = self.func(instance)
return result
class User:
def __init__(self, name):
self.name = name
@cached_property
def greeting(self):
return f"Hello, {self.name}!"
JavaScript 生成器:
function* numberGenerator() {
yield 1;
yield 2;
yield 3;
}
function* range(start, end) {
for (let i = start; i < end; i++) {
yield i;
}
}
// 使用生成器
const gen = numberGenerator();
console.log(gen.next().value); // 1
console.log(gen.next().value); // 2
// 迭代生成器
for (const num of range(0, 3)) {
console.log(num);
}
Python 生成器:
def number_generator():
yield 1
yield 2
yield 3
def range_generator(start, end):
for i in range(start, end):
yield i
# 生成器表达式
squares = (x**2 for x in range(10))
# 使用生成器
gen = number_generator()
print(next(gen)) # 1
print(next(gen)) # 2
# 迭代生成器
for num in range_generator(0, 3):
print(num)
Python:
def counter():
i = 0
while True:
val = yield i
if val is not None:
i = val
else:
i += 1
def delegating_generator():
yield from range(3)
yield from "ABC"
# 使用 send
c = counter()
print(next(c)) # 0
print(c.send(10)) # 10
print(next(c)) # 11
# 使用 yield from
for item in delegating_generator():
print(item) # 0, 1, 2, 'A', 'B', 'C'
Python:
async def async_range(start, end):
for i in range(start, end):
await asyncio.sleep(1)
yield i
async def main():
async for i in async_range(0, 3):
print(i)
def memoize(func):
cache = {}
def wrapper(*args):
if args not in cache:
cache[args] = func(*args)
return cache[args]
return wrapper
@memoize
def fibonacci(n):
if n < 2:
return n
return fibonacci(n-1) + fibonacci(n-2)
def read_large_file(file_path, chunk_size=8192):
with open(file_path, 'rb') as f:
while True:
chunk = f.read(chunk_size)
if not chunk:
break
yield chunk
# 使用生成器处理大文件
for chunk in read_large_file('large_file.txt'):
process_chunk(chunk)
装饰器语法
生成器特性
使用场景
性能考虑
JavaScript (Node.js):
const fs = require('fs');
const csv = require('csv-parser');
// 读取 CSV
fs.createReadStream('data.csv')
.pipe(csv())
.on('data', (row) => {
console.log(row);
})
.on('end', () => {
console.log('CSV file successfully processed');
});
// 写入 CSV
const createCsvWriter = require('csv-writer').createObjectCsvWriter;
const csvWriter = createCsvWriter({
path: 'output.csv',
header: [
{id: 'name', title: 'NAME'},
{id: 'age', title: 'AGE'}
]
});
const records = [
{name: 'John', age: 30},
{name: 'Jane', age: 25}
];
csvWriter.writeRecords(records);
Python:
import pandas as pd
import csv
# 使用 pandas 读取 CSV
df = pd.read_csv('data.csv')
print(df.head()) # 查看前几行
print(df.describe()) # 基本统计信息
# 使用内置 csv 模块
with open('data.csv', 'r') as file:
csv_reader = csv.DictReader(file)
for row in csv_reader:
print(row)
# 写入 CSV
data = [
{'name': 'John', 'age': 30},
{'name': 'Jane', 'age': 25}
]
with open('output.csv', 'w', newline='') as file:
writer = csv.DictWriter(file, fieldnames=['name', 'age'])
writer.writeheader()
writer.writerows(data)
JavaScript:
const fs = require('fs');
// 读取 JSON
const data = JSON.parse(fs.readFileSync('data.json', 'utf8'));
// 处理数据
const processed = data.map(item => ({
...item,
fullName: `${item.firstName} ${item.lastName}`
}));
// 写入 JSON
fs.writeFileSync('output.json', JSON.stringify(processed, null, 2));
Python:
import json
# 读取 JSON
with open('data.json', 'r') as file:
data = json.load(file)
# 处理数据
processed = [{
**item,
'full_name': f"{item['first_name']} {item['last_name']}"
} for item in data]
# 写入 JSON
with open('output.json', 'w') as file:
json.dump(processed, file, indent=2)
Python 使用 pandas 进行数据分析:
import pandas as pd
import matplotlib.pyplot as plt
# 读取销售数据
df = pd.read_csv('sales.csv')
# 基本数据清洗
df['date'] = pd.to_datetime(df['date'])
df['revenue'] = df['revenue'].fillna(0)
# 按月份统计销售额
monthly_sales = df.groupby(df['date'].dt.strftime('%Y-%m'))[['revenue']].sum()
# 创建可视化
plt.figure(figsize=(12, 6))
monthly_sales.plot(kind='bar')
plt.title('Monthly Sales Revenue')
plt.xlabel('Month')
plt.ylabel('Revenue')
plt.xticks(rotation=45)
plt.tight_layout()
plt.savefig('sales_chart.png')
# 生成报告
report = f"""
Sales Report
===========
Total Revenue: ${df['revenue'].sum():,.2f}
Average Sale: ${df['revenue'].mean():,.2f}
Number of Transactions: {len(df)}
Best Month: {monthly_sales['revenue'].idxmax()}
"""
with open('report.txt', 'w') as f:
f.write(report)
Python:
import pandas as pd
from openpyxl import Workbook
from openpyxl.styles import Font, PatternFill
# 读取 Excel
df = pd.read_excel('data.xlsx', sheet_name='Sheet1')
# 创建新的 Excel 文件
wb = Workbook()
ws = wb.active
ws.title = "Sales Report"
# 添加标题
headers = ['Product', 'Quantity', 'Revenue']
for col, header in enumerate(headers, 1):
cell = ws.cell(row=1, column=col)
cell.value = header
cell.font = Font(bold=True)
cell.fill = PatternFill(start_color="CCCCCC", end_color="CCCCCC", fill_type="solid")
# 添加数据
for row_idx, row in enumerate(df.values, 2):
for col_idx, value in enumerate(row, 1):
ws.cell(row=row_idx, column=col_idx, value=value)
# 保存文件
wb.save('report.xlsx')
Python:
import pandas as pd
import numpy as np
def clean_data(df):
# 删除重复行
df = df.drop_duplicates()
# 处理缺失值
df['age'] = df['age'].fillna(df['age'].mean())
df['category'] = df['category'].fillna('Unknown')
# 数据类型转换
df['date'] = pd.to_datetime(df['date'])
df['amount'] = pd.to_numeric(df['amount'], errors='coerce')
# 异常值处理
q1 = df['amount'].quantile(0.25)
q3 = df['amount'].quantile(0.75)
iqr = q3 - q1
df = df[~((df['amount'] < (q1 - 1.5 * iqr)) |
(df['amount'] > (q3 + 1.5 * iqr)))]
# 创建新特征
df['month'] = df['date'].dt.month
df['year'] = df['date'].dt.year
return df
# 使用示例
df = pd.read_csv('raw_data.csv')
cleaned_df = clean_data(df)
cleaned_df.to_csv('cleaned_data.csv', index=False)
数据处理工具
文件处理
性能考虑
代码可读性
JavaScript (Express):
const express = require('express');
const app = express();
app.use(express.json());
// 基本路由
app.get('/api/users', (req, res) => {
res.json({ users: [] });
});
app.post('/api/users', (req, res) => {
const { name, email } = req.body;
// 处理用户创建
res.status(201).json({ message: 'User created' });
});
app.listen(3000, () => {
console.log('Server running on port 3000');
});
Python (Flask):
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/api/users', methods=['GET'])
def get_users():
return jsonify({'users': []})
@app.route('/api/users', methods=['POST'])
def create_user():
data = request.get_json()
name = data.get('name')
email = data.get('email')
# 处理用户创建
return jsonify({'message': 'User created'}), 201
if __name__ == '__main__':
app.run(port=3000)
Python (FastAPI):
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import List, Optional
app = FastAPI()
class User(BaseModel):
name: str
email: str
age: Optional[int] = None
class UserResponse(BaseModel):
id: int
name: str
email: str
@app.get("/api/users", response_model=List[UserResponse])
async def get_users():
# 获取用户列表
return [{"id": 1, "name": "John", "email": "john@example.com"}]
@app.post("/api/users", response_model=UserResponse)
async def create_user(user: User):
# 创建用户
if not user.email:
raise HTTPException(status_code=400, detail="Email is required")
return {"id": 1, **user.dict()}
@app.get("/api/users/{user_id}")
async def get_user(user_id: int):
# 获取单个用户
if user_id != 1:
raise HTTPException(status_code=404, detail="User not found")
return {"id": user_id, "name": "John"}
JavaScript (Express):
// 错误处理中间件
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).json({
error: 'Internal Server Error'
});
});
// 认证中间件
const authMiddleware = (req, res, next) => {
const token = req.headers.authorization;
if (!token) {
return res.status(401).json({
error: 'Unauthorized'
});
}
next();
};
app.use('/api/protected', authMiddleware);
Python (FastAPI):
from fastapi import Depends, HTTPException, Security
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
security = HTTPBearer()
async def verify_token(credentials: HTTPAuthorizationCredentials = Security(security)):
if not credentials.credentials:
raise HTTPException(
status_code=401,
detail="Invalid authentication credentials"
)
return credentials.credentials
@app.get("/api/protected")
async def protected_route(token: str = Depends(verify_token)):
return {"message": "Access granted"}
# 全局错误处理
@app.exception_handler(Exception)
async def global_exception_handler(request, exc):
return JSONResponse(
status_code=500,
content={"message": "Internal Server Error"}
)
JavaScript (Express + Sequelize):
const { Sequelize, Model, DataTypes } = require('sequelize');
const sequelize = new Sequelize('database', 'user', 'password', {
host: 'localhost',
dialect: 'postgres'
});
class User extends Model {}
User.init({
name: DataTypes.STRING,
email: DataTypes.STRING
}, { sequelize });
// 使用模型
app.get('/api/users', async (req, res) => {
const users = await User.findAll();
res.json(users);
});
Python (FastAPI + SQLAlchemy):
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from fastapi import Depends
SQLALCHEMY_DATABASE_URL = "postgresql://user:password@localhost/database"
engine = create_engine(SQLALCHEMY_DATABASE_URL)
SessionLocal = sessionmaker(bind=engine)
Base = declarative_base()
class User(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True)
name = Column(String)
email = Column(String)
# 依赖注入
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
@app.get("/api/users")
async def get_users(db: Session = Depends(get_db)):
users = db.query(User).all()
return users
JavaScript (Express + Swagger):
const swaggerJsDoc = require('swagger-jsdoc');
const swaggerUi = require('swagger-ui-express');
const swaggerOptions = {
definition: {
openapi: '3.0.0',
info: {
title: 'API Documentation',
version: '1.0.0'
}
},
apis: ['./routes/*.js']
};
const swaggerDocs = swaggerJsDoc(swaggerOptions);
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocs));
/**
* @swagger
* /api/users:
* get:
* summary: Get all users
* responses:
* 200:
* description: List of users
*/
app.get('/api/users', (req, res) => {
// 处理请求
});
Python (FastAPI):
# FastAPI 自动生成 OpenAPI 文档
from fastapi import FastAPI, Query
from typing import List
app = FastAPI(
title="My API",
description="API Documentation",
version="1.0.0"
)
@app.get("/api/users", response_model=List[User])
async def get_users(
skip: int = Query(0, description="Skip items"),
limit: int = Query(10, description="Limit items")
):
"""
获取用户列表
- **skip**: 跳过的记录数
- **limit**: 返回的最大记录数
"""
return users[skip : skip + limit]
JavaScript (Jest):
const request = require('supertest');
const app = require('../app');
describe('User API', () => {
test('GET /api/users', async () => {
const response = await request(app)
.get('/api/users')
.expect(200);
expect(response.body).toHaveProperty('users');
});
test('POST /api/users', async () => {
const userData = {
name: 'John',
email: 'john@example.com'
};
const response = await request(app)
.post('/api/users')
.send(userData)
.expect(201);
expect(response.body).toHaveProperty('id');
});
});
Python (pytest):
from fastapi.testclient import TestClient
from main import app
client = TestClient(app)
def test_get_users():
response = client.get("/api/users")
assert response.status_code == 200
assert "users" in response.json()
def test_create_user():
user_data = {
"name": "John",
"email": "john@example.com"
}
response = client.post("/api/users", json=user_data)
assert response.status_code == 201
assert "id" in response.json()
框架特性
性能考虑
开发体验
生态系统
Python:
from pathlib import Path
import re
def batch_rename(directory, pattern, replacement):
directory = Path(directory)
for file_path in directory.glob('*'):
if file_path.is_file():
new_name = re.sub(pattern, replacement, file_path.name)
file_path.rename(directory / new_name)
# 使用示例
batch_rename(
'./images',
r'image_(\d+)',
r'photo_\1'
) # image_001.jpg -> photo_001.jpg
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class FileHandler(FileSystemEventHandler):
def on_modified(self, event):
if not event.is_directory:
print(f"File {event.src_path} has been modified")
def on_created(self, event):
if not event.is_directory:
print(f"File {event.src_path} has been created")
def watch_directory(path):
event_handler = FileHandler()
observer = Observer()
observer.schedule(event_handler, path, recursive=False)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
import psutil
import datetime
def monitor_system():
# CPU 信息
cpu_percent = psutil.cpu_percent(interval=1)
memory = psutil.virtual_memory()
# 进程信息
processes = []
for proc in psutil.process_iter(['pid', 'name', 'cpu_percent']):
processes.append(proc.info)
# 生成报告
report = f"""
System Report ({datetime.datetime.now()})
=====================================
CPU Usage: {cpu_percent}%
Memory Usage: {memory.percent}%
Total Processes: {len(processes)}
Top Processes by CPU Usage:
"""
# 排序并显示前5个进程
top_processes = sorted(processes,
key=lambda x: x['cpu_percent'],
reverse=True)[:5]
for proc in top_processes:
report += f"\n{proc['name']}: {proc['cpu_percent']}%"
return report
import schedule
import time
def backup_database():
print("Backing up database...")
# 执行备份操作
def clean_temp_files():
print("Cleaning temporary files...")
# 清理临时文件
# 设置定时任务
schedule.every().day.at("00:00").do(backup_database)
schedule.every().hour.do(clean_temp_files)
# 运行调度器
while True:
schedule.run_pending()
time.sleep(60)
import requests
import smtplib
from email.message import EmailMessage
def monitor_website(url, expected_status=200):
try:
response = requests.get(url)
if response.status_code != expected_status:
send_alert(f"Website {url} returned status {response.status_code}")
except requests.RequestException as e:
send_alert(f"Failed to access {url}: {str(e)}")
def send_alert(message):
msg = EmailMessage()
msg.set_content(message)
msg['Subject'] = 'Website Monitor Alert'
msg['From'] = "monitor@example.com"
msg['To'] = "admin@example.com"
# 发送邮件
with smtplib.SMTP('smtp.gmail.com', 587) as server:
server.starttls()
server.login('username', 'password')
server.send_message(msg)
# 使用示例
sites = ['https://example.com', 'https://api.example.com']
for site in sites:
monitor_website(site)
import requests
import json
from datetime import datetime
class APITester:
def __init__(self, base_url):
self.base_url = base_url
self.session = requests.Session()
self.results = []
def test_endpoint(self, endpoint, method='GET', data=None,
expected_status=200):
url = f"{self.base_url}{endpoint}"
start_time = datetime.now()
try:
if method == 'GET':
response = self.session.get(url)
elif method == 'POST':
response = self.session.post(url, json=data)
duration = (datetime.now() - start_time).total_seconds()
success = response.status_code == expected_status
self.results.append({
'endpoint': endpoint,
'method': method,
'status': response.status_code,
'duration': duration,
'success': success
})
except requests.RequestException as e:
self.results.append({
'endpoint': endpoint,
'method': method,
'error': str(e),
'success': False
})
def generate_report(self):
success_count = sum(1 for r in self.results if r['success'])
total_count = len(self.results)
report = f"""
API Test Report
==============
Success Rate: {success_count}/{total_count}
Detailed Results:
"""
for result in self.results:
report += f"\n{result['method']} {result['endpoint']}: "
report += "Success" if result['success'] else "Failed"
if 'duration' in result:
report += f" ({result['duration']:.2f}s)"
return report
# 使用示例
tester = APITester('https://api.example.com')
tester.test_endpoint('/users')
tester.test_endpoint('/users', method='POST',
data={'name': 'John'})
print(tester.generate_report())
import requests
from bs4 import BeautifulSoup
import pandas as pd
from datetime import datetime
class WebScraper:
def __init__(self):
self.session = requests.Session()
self.session.headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
}
def scrape_page(self, url):
try:
response = self.session.get(url)
response.raise_for_status()
return BeautifulSoup(response.text, 'html.parser')
except requests.RequestException as e:
print(f"Error scraping {url}: {e}")
return None
def extract_data(self, soup, selectors):
data = {}
for key, selector in selectors.items():
element = soup.select_one(selector)
data[key] = element.text.strip() if element else None
return data
def save_to_csv(self, data, filename):
df = pd.DataFrame(data)
df.to_csv(filename, index=False)
print(f"Data saved to {filename}")
# 使用示例
scraper = WebScraper()
selectors = {
'title': 'h1',
'price': '.price',
'description': '.description'
}
products = []
urls = ['https://example.com/product1', 'https://example.com/product2']
for url in urls:
soup = scraper.scrape_page(url)
if soup:
data = scraper.extract_data(soup, selectors)
data['url'] = url
data['timestamp'] = datetime.now()
products.append(data)
scraper.save_to_csv(products, 'products.csv')
语言特性
性能考虑
开发效率
跨平台性
import pandas as pd
import numpy as np
# 读取数据
df = pd.read_csv('data.csv')
# 基本信息查看
print(df.info()) # 数据集信息
print(df.describe()) # 数值列统计摘要
print(df.head()) # 查看前几行
# 基本操作
df['new_column'] = df['price'] * 2 # 添加列
df = df.drop('unused_column', axis=1) # 删除列
df = df[df['price'] > 100] # 过滤数据
df = df.sort_values('price', ascending=False) # 排序
# 处理缺失值
df['price'] = df['price'].fillna(df['price'].mean())
df['category'] = df['category'].fillna('Unknown')
df = df.dropna(subset=['important_column'])
# 删除重复行
df = df.drop_duplicates()
# 数据类型转换
df['date'] = pd.to_datetime(df['date'])
df['price'] = pd.to_numeric(df['price'], errors='coerce')
# 异常值处理
Q1 = df['price'].quantile(0.25)
Q3 = df['price'].quantile(0.75)
IQR = Q3 - Q1
df = df[~((df['price'] < (Q1 - 1.5 * IQR)) |
(df['price'] > (Q3 + 1.5 * IQR)))]
# 基本分组统计
grouped = df.groupby('category').agg({
'price': ['mean', 'min', 'max', 'count'],
'quantity': 'sum'
})
# 时间序列分析
monthly = df.groupby(df['date'].dt.strftime('%Y-%m')).sum()
# 透视表
pivot_table = pd.pivot_table(df,
values='price',
index='category',
columns='region',
aggfunc='mean',
fill_value=0
)
# 相关性分析
correlation = df[['price', 'quantity', 'rating']].corr()
# 描述性统计
stats = df.describe(include='all')
# 频率分析
category_counts = df['category'].value_counts()
category_percentages = df['category'].value_counts(normalize=True)
import matplotlib.pyplot as plt
# 基本折线图
plt.figure(figsize=(10, 6))
plt.plot(df['date'], df['price'])
plt.title('Price Trend Over Time')
plt.xlabel('Date')
plt.ylabel('Price')
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
# 多子图
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 5))
ax1.bar(category_counts.index, category_counts.values)
ax1.set_title('Category Distribution')
ax2.scatter(df['price'], df['quantity'])
ax2.set_title('Price vs Quantity')
plt.tight_layout()
plt.show()
import seaborn as sns
# 设置风格
sns.set_style("whitegrid")
# 分布图
plt.figure(figsize=(10, 6))
sns.histplot(data=df, x='price', bins=30)
plt.title('Price Distribution')
plt.show()
# 箱线图
plt.figure(figsize=(12, 6))
sns.boxplot(data=df, x='category', y='price')
plt.title('Price Distribution by Category')
plt.xticks(rotation=45)
plt.show()
# 热力图
plt.figure(figsize=(8, 6))
sns.heatmap(correlation, annot=True, cmap='coolwarm')
plt.title('Correlation Matrix')
plt.show()
# 重采样
daily = df.resample('D', on='date')['price'].mean()
weekly = df.resample('W', on='date')['price'].mean()
monthly = df.resample('M', on='date')['price'].mean()
# 移动平均
df['MA7'] = df['price'].rolling(window=7).mean()
df['MA30'] = df['price'].rolling(window=30).mean()
# 季节性分解
from statsmodels.tsa.seasonal import seasonal_decompose
decomposition = seasonal_decompose(df['price'],
period=30,
model='additive')
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score
# 准备数据
X = df[['feature1', 'feature2', 'feature3']]
y = df['target']
# 分割训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
# 训练模型
model = LinearRegression()
model.fit(X_train, y_train)
# 预测和评估
y_pred = model.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
def generate_analysis_report(df):
"""生成数据分析报告"""
report = f"""
数据分析报告
===========
1. 基本信息
-----------
记录数: {len(df)}
特征数: {len(df.columns)}
2. 描述性统计
------------
{df.describe().to_string()}
3. 缺失值统计
------------
{df.isnull().sum().to_string()}
4. 分类变量分布
-------------
{df.select_dtypes(include=['object']).nunique().to_string()}
"""
# 保存报告
with open('analysis_report.txt', 'w', encoding='utf-8') as f:
f.write(report)
# 生成可视化
plt.figure(figsize=(15, 10))
# 数值变量分布
numeric_cols = df.select_dtypes(include=[np.number]).columns
for i, col in enumerate(numeric_cols, 1):
plt.subplot(len(numeric_cols), 1, i)
sns.histplot(data=df, x=col)
plt.title(f'{col} Distribution')
plt.tight_layout()
plt.savefig('distributions.png')
数据处理能力
性能优势
分析工具
可视化能力
# 低效
sum = 0
for i in range(1000000):
sum += i
# 高效
sum(range(1000000))
# 低效
squares = []
for i in range(1000):
squares.append(i ** 2)
# 高效
squares = [i ** 2 for i in range(1000)]
# 内存密集
def get_rows(file_path):
rows = []
with open(file_path) as f:
for line in f:
rows.append(line.strip())
return rows
# 内存友好
def get_rows(file_path):
with open(file_path) as f:
for line in f:
yield line.strip()
# 字典查找 O(1)
user_roles = {
'john': 'admin',
'jane': 'user'
}
role = user_roles.get('john', 'guest')
# 集合操作
valid_users = {'john', 'jane', 'bob'}
if user in valid_users: # O(1) 查找
process_user(user)
import threading
import queue
def worker(q):
while True:
item = q.get()
if item is None:
break
process_item(item)
q.task_done()
# 创建工作队列
q = queue.Queue()
threads = []
for i in range(4):
t = threading.Thread(target=worker, args=(q,))
t.start()
threads.append(t)
# 添加任务
for item in items:
q.put(item)
# 等待完成
q.join()
for _ in threads:
q.put(None)
for t in threads:
t.join()
from multiprocessing import Pool
def process_chunk(chunk):
return [item * 2 for item in chunk]
def parallel_processing(data, num_processes=4):
with Pool(num_processes) as pool:
results = pool.map(process_chunk, data)
return results
import asyncio
import aiohttp
async def fetch_url(session, url):
async with session.get(url) as response:
return await response.text()
async def main():
urls = [
'http://example.com',
'http://example.org',
'http://example.net'
]
async with aiohttp.ClientSession() as session:
tasks = [fetch_url(session, url) for url in urls]
results = await asyncio.gather(*tasks)
return results
# 运行异步代码
asyncio.run(main())
import cProfile
import pstats
def profile_code():
profiler = cProfile.Profile()
profiler.enable()
# 要分析的代码
main_function()
profiler.disable()
stats = pstats.Stats(profiler).sort_stats('cumulative')
stats.print_stats()
@profile
def expensive_function():
result = []
for i in range(1000):
result.append(i ** 2)
return result
# 运行: kernprof -l -v script.py
from memory_profiler import profile
@profile
def memory_heavy_function():
big_list = [i for i in range(1000000)]
return sum(big_list)
import pandas as pd
import numpy as np
# 低效
def process_data(df):
result = []
for index, row in df.iterrows():
result.append(row['value'] * 2)
return result
# 高效
def process_data(df):
return df['value'] * 2
# 使用 numpy 优化
def calculate_distance(points):
return np.sqrt(np.sum(points ** 2, axis=1))
# 批量读取
def read_large_file(filename, chunk_size=1024*1024):
with open(filename, 'rb') as f:
while True:
chunk = f.read(chunk_size)
if not chunk:
break
process_chunk(chunk)
# 使用缓冲
from functools import lru_cache
@lru_cache(maxsize=128)
def expensive_computation(n):
# 复杂计算
return result
JavaScript vs Python 性能特点:
# Python - 使用多进程
from multiprocessing import Pool
def cpu_intensive_task(data):
return complex_calculation(data)
with Pool() as pool:
results = pool.map(cpu_intensive_task, data_chunks)
// JavaScript - Worker Threads
const { Worker, isMainThread, parentPort } = require('worker_threads');
if (isMainThread) {
const worker = new Worker(__filename);
worker.on('message', result => console.log(result));
} else {
// Worker 进程中的计算
const result = complexCalculation(data);
parentPort.postMessage(result);
}
# Python - 异步IO
async def fetch_all(urls):
async with aiohttp.ClientSession() as session:
tasks = [fetch_url(session, url) for url in urls]
return await asyncio.gather(*tasks)
// JavaScript - Promise
async function fetchAll(urls) {
return Promise.all(
urls.map(url => fetch(url).then(r => r.json()))
);
}
算法优化
并发优化
内存优化
代码优化
# NumPy - 科学计算基础
import numpy as np
array = np.array([1, 2, 3])
matrix = np.zeros((3, 3))
# Pandas - 数据分析
import pandas as pd
df = pd.read_csv('data.csv')
df.groupby('category').mean()
# SciPy - 科学计算
from scipy import stats
z_scores = stats.zscore(data)
# Scikit-learn - 机器学习
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
# 基本工作流程
X_train, X_test, y_train, y_test = train_test_split(X, y)
model = LogisticRegression()
model.fit(X_train, y_train)
predictions = model.predict(X_test)
# TensorFlow/Keras
import tensorflow as tf
from tensorflow.keras import layers
model = tf.keras.Sequential([
layers.Dense(128, activation='relu'),
layers.Dropout(0.2),
layers.Dense(10, activation='softmax')
])
# PyTorch
import torch
import torch.nn as nn
class Net(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(784, 128)
self.fc2 = nn.Linear(128, 10)
# Django - 全功能框架
from django.db import models
from django.urls import path
class Article(models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
# FastAPI - 现代API框架
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
@app.get("/items/{item_id}")
async def read_item(item_id: int):
return {"item_id": item_id}
# aiohttp - 异步HTTP
import aiohttp
import asyncio
async def fetch_data(url):
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
return await response.text()
# Starlette - 轻量级异步框架
from starlette.applications import Starlette
from starlette.responses import JSONResponse
from starlette.routing import Route
async def homepage(request):
return JSONResponse({'hello': 'world'})
# Pytest - 测试框架
import pytest
def test_addition():
assert 1 + 1 == 2
# Selenium - 浏览器自动化
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.example.com")
element = driver.find_element_by_id("search")
# Fabric - 自动化部署
from fabric import Connection
with Connection('host') as c:
c.run('uname -s')
# Ansible - 自动化配置
import ansible.runner
runner = ansible.runner.Runner(
pattern='web*',
module_name='command',
module_args='uptime'
)
# Matplotlib - 基础绘图
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [1, 2, 3])
plt.title('Simple Plot')
plt.show()
# Seaborn - 统计可视化
import seaborn as sns
sns.scatterplot(data=df, x='x', y='y', hue='category')
# Plotly - 交互式图表
import plotly.express as px
fig = px.scatter(df, x='x', y='y', color='category')
fig.show()
# Bokeh - Web可视化
from bokeh.plotting import figure, show
p = figure(title='Interactive Plot')
p.line([1, 2, 3], [1, 2, 3])
show(p)
# Poetry - 依赖管理
# pyproject.toml
[tool.poetry]
name = "my-project"
version = "0.1.0"
dependencies = {
"requests": "^2.25.1",
"pandas": "^1.2.0"
}
# Pipenv - 虚拟环境管理
# Pipfile
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
[packages]
requests = "*"
pandas = "*"
# Black - 代码格式化
# pyproject.toml
[tool.black]
line-length = 88
target-version = ['py37']
# Pylint - 代码分析
# .pylintrc
[MESSAGES CONTROL]
disable=C0111,C0103
# MyPy - 类型检查
# mypy.ini
[mypy]
python_version = 3.7
warn_return_any = True
数据科学和机器学习
Web 开发
自动化工具
开发工具
包管理
Web 框架
数据处理
测试框架
构建工具
# 1. Python 核心概念练习
def practice_core_concepts():
# 数据结构
list_comprehension = [x**2 for x in range(10)]
dict_comprehension = {x: x**2 for x in range(5)}
# 函数式编程
from functools import reduce
sum_squares = reduce(lambda x, y: x + y, list_comprehension)
# 面向对象
class Shape:
def __init__(self, name):
self.name = name
@property
def description(self):
return f"This is a {self.name}"
# 2. 标准库探索
from collections import defaultdict, Counter
from itertools import combinations, permutations
from functools import partial, lru_cache
# 1. 元编程
class MetaExample(type):
def __new__(cls, name, bases, attrs):
# 修改类的创建过程
return super().__new__(cls, name, bases, attrs)
# 2. 并发编程
import asyncio
async def async_example():
await asyncio.sleep(1)
return "Done"
# 3. 设计模式
class Singleton:
_instance = None
def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance
# FastAPI 项目结构
project/
├── app/
│ ├── __init__.py
│ ├── main.py
│ ├── models/
│ ├── routes/
│ └── services/
├── tests/
└── docker-compose.yml
# 示例代码
from fastapi import FastAPI, Depends
from sqlalchemy.orm import Session
app = FastAPI()
@app.get("/items/{item_id}")
async def read_item(
item_id: int,
db: Session = Depends(get_db)
):
return {"item_id": item_id}
# 数据分析项目结构
project/
├── data/
├── notebooks/
├── src/
│ ├── data_processing.py
│ ├── feature_engineering.py
│ └── modeling.py
└── reports/
# 示例代码
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
def analyze_data(data_path):
df = pd.read_csv(data_path)
# 数据清洗和分析
return analysis_results
# 推荐资源列表
resources = {
"课程平台": [
"Coursera - Python for Everybody",
"edX - CS50's Web Programming with Python",
"Real Python - 高质量教程网站"
],
"文档": [
"Python 官方文档",
"FastAPI 文档",
"Pandas 用户指南"
],
"书籍": [
"Fluent Python",
"Python Cookbook",
"Clean Code in Python"
]
}
# 开源项目参与
projects = {
"初级": [
"https://github.com/topics/good-first-issue",
"https://goodfirstissue.dev/"
],
"中级": [
"Django",
"FastAPI",
"Requests"
],
"高级": [
"CPython",
"Pandas",
"Numpy"
]
}
skill_path = {
"第一阶段": {
"目标": "掌握基础语法和标准库",
"时间": "1-2个月",
"项目": "命令行工具开发"
},
"第二阶段": {
"目标": "Web开发和数据处理",
"时间": "2-3个月",
"项目": "REST API 开发"
},
"第三阶段": {
"目标": "高级特性和性能优化",
"时间": "3-4个月",
"项目": "大规模数据处理系统"
}
}
career_paths = {
"后端开发": [
"Web框架(Django/FastAPI)",
"数据库设计",
"系统架构"
],
"数据科学": [
"数据分析(Pandas)",
"机器学习(Scikit-learn)",
"深度学习(PyTorch/TensorFlow)"
],
"自动化": [
"脚本开发",
"测试自动化",
"运维自动化"
]
}
# 代码风格指南
style_guide = {
"工具": [
"black - 代码格式化",
"pylint - 代码分析",
"mypy - 类型检查"
],
"原则": [
"PEP 8 规范",
"编写文档字符串",
"类型注解"
]
}
# 示例
from typing import List, Optional
def process_data(data: List[str], config: Optional[dict] = None) -> List[str]:
"""
处理字符串列表数据。
Args:
data: 输入的字符串列表
config: 可选的配置字典
Returns:
处理后的字符串列表
"""
return [item.strip().lower() for item in data]
# 项目结构最佳实践
project_structure = {
"src/": "源代码目录",
"tests/": "测试文件",
"docs/": "文档",
"requirements/": {
"base.txt": "基础依赖",
"dev.txt": "开发依赖",
"prod.txt": "生产依赖"
},
"Makefile": "自动化任务",
"README.md": "项目文档"
}
learning_strategies = {
"日常实践": [
"每日编码练习",
"阅读优质开源代码",
"参与开源项目"
],
"知识更新": [
"关注 Python 官方博客",
"参与技术社区",
"订阅技术简讯"
],
"实战提升": [
"解决实际问题",
"编写技术博客",
"参与代码审查"
]
}
knowledge_system = {
"基础知识": {
"语言特性": ["语法", "数据类型", "面向对象"],
"标准库": ["内置函数", "常用模块"],
"设计模式": ["创建型", "结构型", "行为型"]
},
"专业技能": {
"Web开发": ["框架", "数据库", "API设计"],
"数据处理": ["数据分析", "可视化", "机器学习"],
"工程实践": ["测试", "部署", "监控"]
},
"软技能": {
"问题解决": ["调试能力", "性能优化"],
"团队协作": ["代码审查", "文档编写"],
"项目管理": ["需求分析", "任务分解"]
}
}
这些内容为 JavaScript 工程师提供了一个清晰的 Python 学习路径,帮助他们系统地提升 Python 技能。通过结合理论学习和实践项目,可以更快地掌握 Python 编程,并在实际工作中应用这些技能。
# JavaScript 思维
js_way = {
"异步思维": "回调和 Promise",
"原型继承": "基于原型链",
"事件驱动": "响应式编程"
}
# Python 思维
python_way = {
"同步思维": "清晰的程序流",
"类继承": "基于类的继承",
"数据处理": "批处理和转换"
}
combined_skills = {
"前端开发": {
"JavaScript": "用户界面和交互",
"Python": "数据处理和后端服务"
},
"全栈开发": {
"JavaScript": "Node.js API 和实时功能",
"Python": "数据密集型处理和机器学习"
},
"开发工具": {
"JavaScript": "构建工具和自动化",
"Python": "数据分析和脚本工具"
}
}
future_trends = {
"人工智能": [
"机器学习应用开发",
"深度学习模型部署",
"AI 辅助开发工具"
],
"数据科学": [
"大数据处理",
"数据可视化",
"预测分析"
],
"全栈开发": [
"JavaScript + Python 混合架构",
"微服务和云原生开发",
"跨平台应用开发"
]
}
career_growth = {
"技术路线": {
"全栈工程师": "掌握前后端技术栈",
"AI 工程师": "专注机器学习应用",
"数据工程师": "专注数据处理和分析"
},
"能力提升": {
"技术广度": "掌握多语言开发",
"技术深度": "专注特定领域",
"架构能力": "系统设计和优化"
}
}
final_tips = {
"学习建议": [
"保持开放的学习心态",
"关注实际问题解决",
"持续学习和实践",
"参与开源社区"
],
"实践方向": [
"从小项目开始",
"逐步增加复杂度",
"结合实际工作需求",
"建立个人项目集"
],
"长期发展": [
"建立技术影响力",
"分享学习经验",
"保持技术敏感度",
"关注行业动态"
]
}
def conclusion():
"""
JavaScript 工程师学习 Python 的旅程是一次宝贵的经历。
通过掌握 Python,不仅扩展了技术视野,还打开了新的职业发展方向。
记住:编程语言是工具,解决问题才是目的。
祝你在 Python 的学习之旅中收获满满!
"""
pass
这本指南到此结束。希望它能够帮助 JavaScript 工程师们更好地理解和掌握 Python,在技术发展的道路上走得更远。记住,学习新的编程语言不仅是掌握语法和工具,更重要的是理解和运用其背后的思维方式和设计理念。让我们继续在编程的世界中探索和成长!