本文主要是介绍Langchain编程中常见Python库讲解【1】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
本文罗列部分依赖的python库代码举例
strsimpy, pytz, python-magic-bin, python-decouple, pyreadline3, pyclipper, pathlib, mpmath, jieba, flatbuffers, filetype, faiss-cpu, brotli, zipp, wrapt, win32-setctime, websockets, watchdog, validators, urllib3, typing-extensions, tornado, toolz, toml, tenacity, tabulate, sympy, soupsieve, socksio, sniffio, smmap, six, simplejson
常见用法
依赖全文参考上一篇https://blog.csdn.net/Climbman/article/details/141509558
jieba 库示例(中文分词):
import jiebatext = "我爱自然语言处理"
words = jieba.cut(text)
print(list(words))
validators 库示例(数据验证):
import validatorsemail = "test@example.com"
is_valid_email = validators.email(email)
print(is_valid_email)
urllib3 库示例(发送 HTTP 请求):
import urllib3http = urllib3.PoolManager()
response = http.request('GET', 'https://www.example.com')
print(response.data)
toml 库示例(解析 TOML 格式文件):
import tomldata = """
[tool.poetry]
name = "myproject"
version = "0.1.0"
"""
parsed_data = toml.loads(data)
print(parsed_data)
sympy 库示例(符号计算):
import sympyx = sympy.Symbol('x')
expression = x**2 + 2*x + 1
print(sympy.simplify(expression))
以下是更多库的简单示例代码:
tenacity 库示例(重试机制):
import tenacity
import random@tenacity.retry(wait=tenacity.wait_fixed(2), stop=tenacity.stop_after_attempt(3))
def some_function_with_possible_failure():random_number = random.randint(1, 10)if random_number < 5:raise ValueError("Random number is too small.")else:return "Success!"try:result = some_function_with_possible_failure()print(result)
except Exception as e:print(f"Failed after retries: {e}")
tabulate 库示例(格式化表格输出):
from tabulate import tabulatedata = [["Name", "Age"], ["Alice", 25], ["Bob", 30]]
print(tabulate(data))
socksio 库(假设是 socket 相关库)示例:
import socksiosock = socksio.Socket()
sock.connect(("example.com", 80))
sock.send(b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n")
response = sock.recv(1024)
print(response)
sock.close()
sniffio 库(假设用于网络嗅探)示例:
import sniffioasync def some_async_function():current_async_backend = sniffio.current_async_library()print(f"Current async library: {current_async_backend}")sniffio.run(some_async_function())
smmap 库(假设用于内存映射文件)示例:
import smmapwith open("large_file.txt", "r") as f:mm = smmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)# 可以像处理字节串一样处理这个内存映射对象print(mm.read(10))mm.close()
six 库示例(用于 Python 2 和 3 的兼容性):
from six import add_metaclassclass MyMeta(type):pass@add_metaclass(MyMeta)
class MyClass:passprint(MyClass)
simplejson 库示例(JSON 处理):
import simplejson as jsondata = {"name": "John", "age": 30}
json_data = json.dumps(data)
print(json_data)parsed_data = json.loads(json_data)
print(parsed_data)
flatbuffers 库示例(高效的跨语言序列化库):
假设你有一个定义好的 FlatBuffers schema 文件 MySchema.fbs
,内容如下:
namespace my.namespace;table Person {name: string;age: int;
}
生成 Python 绑定后,可以这样使用:
import flatbuffers
from my_package import Personbuilder = flatbuffers.Builder(0)
name = builder.CreateString('Alice')
Person.PersonStart(builder)
Person.PersonAddName(builder, name)
Person.PersonAddAge(builder, 30)
person = Person.PersonEnd(builder)
builder.Finish(person)
buf = builder.Output()parsed_person = Person.Person.GetRootAsPerson(buf)
print(parsed_person.Name())
print(parsed_person.Age())
filetype 库示例(文件类型检测):
import filetypekind = filetype.guess('image.jpg')
if kind is not None:print(f'File extension: {kind.extension}')print(f'File MIME type: {kind.mime}')
faiss-cpu 库示例(高效的相似性搜索库):
import numpy as np
import faissd = 64 # 向量维度
nb = 1000 # 数据库大小
nq = 10 # 查询数量np.random.seed(1234)
xb = np.random.random((nb, d)).astype('float32')
xq = np.random.random((nq, d)).astype('float32')index = faiss.IndexFlatL2(d)
index.add(xb)D, I = index.search(xq, 5) # 返回每个查询向量的最近 5 个邻居的距离和索引
print(D)
print(I)
brotli 库示例(压缩和解压缩数据):
import brotlidata = b'Hello, world!'
compressed_data = brotli.compress(data)
decompressed_data = brotli.decompress(compressed_data)
print(decompressed_data)
zipp 库示例(操作 ZIP 归档文件):
import zipparchive = zipp.Path('archive.zip')
archive.write_text('file1.txt', 'Content of file 1')
archive.write_text('file2.txt', 'Content of file 2')for file in archive.iterdir():print(file.name)
wrapt 库示例(装饰器相关库):
import wrapt@wrapt.decorator
def my_decorator(wrapped, instance, args, kwargs):print('Before function call')result = wrapped(*args, **kwargs)print('After function call')return result@my_decorator
def my_function():print('Inside function')my_function()
win32-setctime 库示例(在 Windows 上设置文件创建时间):
import win32file
import win32con
import osfile_path = 'test_file.txt'
with open(file_path, 'w') as f:f.write('Some content')creation_time = 1629459200 # 时间戳
handle = win32file.CreateFile(file_path, win32con.GENERIC_WRITE, 0, None, win32con.OPEN_EXISTING, 0, None)
win32file.SetFileTime(handle, creation_time, None, None)
handle.close()
websockets 库示例(WebSockets 通信):
import asyncio
import websocketsasync def echo(websocket, path):async for message in websocket:await websocket.send(message)start_server = websockets.serve(echo, 'localhost', 8765)asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
watchdog 库示例(文件系统监控):
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandlerclass MyHandler(FileSystemEventHandler):def on_created(self, event):print(f'File created: {event.src_path}')def on_modified(self, event):print(f'File modified: {event.src_path}')if __name__ == "__main__":event_handler = MyHandler()observer = Observer()observer.schedule(event_handler, path='.', recursive=False)observer.start()try:while True:time.sleep(1)except KeyboardInterrupt:observer.stop()observer.join()
strsimpy 库(假设是字符串相似度计算库)示例:
# 假设 strsimpy 有一个函数 calculate_similarity 用于计算字符串相似度
from strsimpy import calculate_similaritystr1 = "hello world"
str2 = "hi world"
similarity = calculate_similarity(str1, str2)
print(similarity)
pytz 库示例(时区处理):
import pytz
from datetime import datetimetz = pytz.timezone('America/New_York')
now = datetime.now(tz)
print(now)
python-magic-bin 库示例(文件类型识别):
import magicfile_path = 'test.txt'
file_type = magic.from_file(file_path)
print(file_type)
python-decouple 库示例(配置文件读取):
假设你有一个配置文件 .env
,内容如下:
MY_VARIABLE=some_value
from decouple import configmy_variable = config('MY_VARIABLE')
print(my_variable)
pyreadline3 库示例(增强命令行交互):
import pyreadline3# 假设可以用它来实现更好的命令行输入提示
line = pyreadline3.Completer().complete('print(', 0)
print(line)
pyclipper 库示例(多边形裁剪库):
from pyclipper import PyclipperOffsetcoordinates = [(0, 0), (5, 0), (5, 5), (0, 5)]
clipper_offset = PyclipperOffset()
offset_coordinates = clipper_offset.Execute(coordinates, 2)
print(offset_coordinates)
pathlib 库示例(路径操作):
from pathlib import Pathfile_path = Path('/home/user/documents/test.txt')
if file_path.exists():print(file_path.name)
else:print('File does not exist.')
mpmath 库示例(高精度数学计算):
from mpmath import mpmp.dps = 50
pi = mp.pi
print(pi)
toolz 库示例(函数式编程工具):
from toolz import compose, partialdef add(a, b):return a + badd_five = partial(add, b=5)
double_result = compose(lambda x: x * 2, add_five)print(double_result(3))
tornado 库示例(异步网络框架):
import tornado.ioloop
import tornado.webclass MainHandler(tornado.web.RequestHandler):def get(self):self.write("Hello, Tornado!")def make_app():return tornado.web.Application([(r"/", MainHandler),])if __name__ == "__main__":app = make_app()app.listen(8888)tornado.ioloop.IOLoop.current().start()
这篇关于Langchain编程中常见Python库讲解【1】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!