19、MicroPython 编程:从字典操作到自定义库构建

MicroPython 编程:从字典操作到自定义库构建

1. 字典操作基础

在 MicroPython 编程中,字典是一种非常实用的数据类型。我们可以使用字典类创建该类型的变量,定义字典的语法是使用一组花括号。以下是一个简单示例:

{'city': 'Anywhere', 'street': '123 Main St', 'zip': 90125}
my_addr = {'street':'201 Cherry Tree Road', 'city':'Gobye', 'zip':12345}
print(my_addr)

运行上述代码,输出结果为:

{'city': 'Gobye', 'street': '201 Cherry Tree Road', 'zip': 12345}

在这个示例中,我们首先定义了一个字典,然后创建了一个新的字典变量 my_addr 并赋值,最后使用 print() 函数输出该字典。

2. 异常处理机制

异常处理是 Python 和 MicroPython 中一个重要的编程技术,它可以帮助我们在错误发生时管理或捕获事件,并针对特定错误执行相应的代码。以下是 MicroPython 中常见的异常类及其描述:
| 异常类 | 描述 |
| — | — |
| AssertionError | assert() 语句失败 |
| Attri

相关推荐

ESP32-S3 MicroPython人脸检测实战:从固件烧录到算法部署全解析

人脸检测是计算机视觉领域的基础技术,其核心原理是通过算法识别图像或视频中的人脸区域。在嵌入式边缘计算场景中,这项技术面临着算力与内存的严格限制。MicroPython作为一种轻量级的Python实现,以其高效的开发体验和硬件操作能力,为在资源受限的微控制器(如ESP32-S3)上快速原型验证AI应用提供了可能。通过结合摄像头驱动、图像采集与轻量级算法,开发者能够在智能门锁、互动装置等物联网设备上实现本地化的人脸感知功能。本文以行空板K10开发板为例,详细阐述了在MicroPython环境下,从固件烧录、摄像

weixin_34265814的博客 654

【ESP32+MicroPython】单位数码管使用

在本教程中,我们将使用ESP32和MicroPython来驱动一个带小数点的单位数码管(7段显示器)。将详细讲解数码管的工作原理,并分步骤完成从基本的段位控制到完整的数字显示,最后封装成可重用的控制类。同时,本文将提供如何判断数码管是**共阳极**还是**共阴极**的方法,并为不同类型的数码管提供兼容代码。

2303_80346267的博客 1468

Raspberry Pi Pico 2开发实战:从RP2350双核架构到MicroPython/C++项目全解析

微控制器作为嵌入式系统的核心,通过集成处理器、内存和丰富外设,实现对物理世界的感知与控制。其工作原理基于硬件与软件的紧密协同,开发者通过编程语言直接或间接操作寄存器,驱动GPIO、ADC、通信接口等模块。这一技术价值在于以极低的成本和功耗,为物联网、智能硬件、工业自动化等领域提供可靠的计算与控制单元。在边缘计算、数据采集、电机控制等应用场景中,微控制器扮演着关键角色。本文聚焦于Raspberry Pi Pico 2这款搭载**RP2350双核** Cortex-M33处理器的新一代微控制器开发板,深入剖析其

hqmln的博客 273

【雕爷学编程】MicroPython手册之数据类型 dictionary 字典

MicroPython的dictionary字典是一种可变的映射类型,可以存储任意类型的键值对,如数字、字符串、列表等。1、dictionary字典中的键必须是不可变的类型,如数字、字符串、元组等,如果用可变的类型,如列表、字典等,会报错TypeError: unhashable type: ‘…2、dictionary字典中的键必须是唯一的,如果有重复的键,后面的值会覆盖前面的值,如{“a”: 1, “a”: 2}等价于{“a”: 2}。字典的键是学生的学号,值是包含学生姓名、年龄和成绩的字典。

雕爷学编程 330

【雕爷学编程】MicroPython手册之文件操作模块 open

由于文件以写入模式打开,如果文件已存在,写入操作将覆盖文件的内容。文件的模式可以是r(只读)、w(写入)、a(追加)、x(创建)、r+(读写)、w+(写入并读取)、a+(追加并读取)或者x+(创建并读取)。修改文件:可以使用open函数来打开一个已存在的文件,然后使用seek和tell方法来定位到指定的位置,然后使用write方法来修改内容。它返回的文件对象有一些方法和属性,如read()、write()、seek()、tell()、close()等方法,以及name、mode、closed等属性。

雕爷学编程 364

【雕爷学编程】MicroPython手册之文件操作模块 read 读取文件

然后,我们使用 for 循环逐行读取文件中的数据,并对每行数据进行处理(例如,使用自定义的 process_line() 函数)。然后,使用read()方法并指定字节数为20,读取文件的前20个字节内容,并将其存储在变量content中。它会根据文件对象的当前位置来读取内容,并且每次读取后,会将文件对象的位置向后移动相应的字节数。它接受一个可选的参数,表示要读取的字节数。MicroPython的read方法是用于从一个文件对象中读取内容的方法,它返回一个字符串或一个字节对象,根据文件的类型和模式而定。

雕爷学编程 906

泛微OA表单设计实战:如何快速实现自定义多选浏览框(附完整配置步骤)

本文详细介绍了在泛微OA中快速实现自定义多选浏览框的完整配置步骤。针对招聘提报、活动审批等需要从海量数据中规范选择多个条目的业务场景,文章从理解需求、构建数据源、设计查询页面到创建浏览框控件,提供了清晰的实战指南,帮助开发者高效解决复杂数据选择难题,提升表单操作体验。

yog99的博客 1052

软件工程期末知识点总结【超详细】

生命周期模型(过程模型):瀑布模型、快速原型模型、增量模型、螺旋模型、喷泉模型。2、实体联系图:1、数据对象2、属性3、联系(1:1、1:N、M:N)3、语句覆盖 就是设计足够的调试用例,使得程序中的每个语句至少执行一次。 5、判定覆盖就是设计足够的测试用例,使得程序中每个判定的取“真”分支和取“假”分支至少都执行一次,判定覆盖又称分支覆盖 6、条件覆盖就是设计足够的测试用例,使得程序判定中的每个条件能获得各种可能的结果。7、软件开发时期通常分成4个阶段:总体设计、详细设计、编码和单元测试、综合测试。

这个人好有趣....hh 1万+

使用GEOquery包下载原始数据

使用GEOquery包下载原始数据

m0_75172651的博客 1901

【项目实战】小智AI MCP协议在IoT设备控制中的深度应用解析

本文深度解析小智AI MCP协议在IoT设备控制中的应用实践。通过MCP协议作为AI与物联网设备的翻译桥梁,实现大语言模型与ESP32等硬件的无缝通信。文章详细介绍了环境搭建、工具注册、消息路由机制,并以语音控制RGB灯光系统为例展示完整开发流程,为AIoT开发者提供实用指南。

oauth7security的博客 826

基于Freertos与lwip移植FreeModbusTcp协议

基于FreeRTOS的N32G457VEL7之LWIP网络协议栈移植-优快云博客。

Dd_456456的博客 3239

告别UI字体臃肿!详解TextMeshPro字体AssetBundle打包优化技巧(Unity 2022实测)

本文详细解析了Unity 2022中使用TextMeshPro时字体资源AssetBundle打包的优化技巧,特别针对中日文等多语言场景下的字体臃肿问题。通过公共资源标记、依赖关系分析和实战案例,帮助开发者有效减少包体体积和内存占用,提升游戏性能。

weixin_33725722的博客 420

用Python爬取大连商品交易所期货实时行情:从入门到实战

python复制编辑# utils.py"豆粕": "M0","铁矿石": "I0","聚乙烯": "L0","玉米": "C0","聚丙烯": "PP0","棕榈油": "P0"本项目展示了:✅ 如何从新浪财经 API获取主力合约实时数据✅ 使用Python异步爬虫技术) 提升效率✅ 利用pandas和matplotlib进行结构化分析与可视化✅ 具备良好的扩展性,可对接金融、农业、能源等数据需求场景✅ 可作为期货量化策略数据源✅ 可嵌入自动化报告系统。

2201_76125261的博客 1248

【HAL库】STM32CubeMX开发----STM32F407----ETH+LWIP----TCPclient客户端

STM32F407开发板 为TCPclient客户端,PC端 为TCPserver服务端,相互之间的发送和接收实验。

MQ0522的博客 3051

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

上一篇: 20、区块链技术与国际数据空间融合的探索与应用
下一篇: 10、深入探索Azure Web应用与SQL数据库:从部署到管理的全方位指南
pluto
博客等级 码龄25年 46粉丝 1562原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值