go-uci库使用示例及注意事项

17 篇文章 2 订阅

go-uci库使用示例及注意事项


1. 前言

之前介绍过openwrt的uci相关内容,这里就go uci库使用及注意事项做下简单总结,注意点可能会引起一些不易发觉的bug。

2. 示例

使用的go-uci库:https://github.com/digineo/go-uci

import "github.com/digineo/go-uci"

func main() {
    //强制读取,否则由于内存中缓存的内容导致配置文件中内容在shell或者原生配置web中修改后这里读取的内容还是未修改前的
	err = uci.LoadConfig("network", true)
	if err != nil {
		fmt.Println(err)
	}
	err = uci.Commit()
	if err != nil {
		fmt.Println(err)
	}
    
    // use the default tree (/etc/config)
    if values, ok := uci.Get("system", "@system[0]", "hostname"); ok {
        fmt.Println("hostanme", values)
        //=> hostname [OpenWrt]
    }

    // use a custom tree
    u := uci.NewTree("/path/to/config")
    if values, ok := u.Get("network", "lan", "ifname"); ok {
        fmt.Println("network.lan.ifname", values)
        //=> network.lan.ifname [eth0.2]
    }
    if sectionExists := u.Set("network", "lan", "ipaddr", "192.168.7.1"); !sectionExists {
        _ = u.AddSection("network", "lan", "interface")
        _ = u.Set("network", "lan", "ipaddr", "192.168.7.1")
    }
    u.Commit() // or uci.Revert()
}

3. 注意点

默认的示例程序没有加载配置文件,但是当从shell或者openwrt的web界面上修改配置文件后程序无法察觉(程序不重新执行配置文件是加载一次一直存在内存中的,这导致外界更改文件后没有影响到加载到内存中的数据),这会导致从这些地方修改内容和程序中修改配置文件不统一,比如初始ip为192.168.1.1,而张三通过shell临时修改ip为192.168.1.3而忘记修改回去了,而李四通过一直运行的程序查看到的ip还是未修改前的ip,觉得没问题,实际上ip已经被修改为1.3了而不是李四认为的没问题的1.1的ip。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是使用UCI乳腺癌数据集的Python代码示例: ```python import pandas as pd import numpy as np from sklearn.datasets import load_breast_cancer # 加载UCI乳腺癌数据集 data = load_breast_cancer() # 将数据集转换为pandas DataFrame格式 df = pd.DataFrame(np.c_[data['data'], data['target']], columns=np.append(data['feature_names'], ['target'])) # 打印数据集信息 print(df.info()) # 打印数据集前5行 print(df.head()) ``` 输出结果如下: ``` <class 'pandas.core.frame.DataFrame'> RangeIndex: 569 entries, 0 to 568 Data columns (total 31 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 mean radius 569 non-null float64 1 mean texture 569 non-null float64 2 mean perimeter 569 non-null float64 3 mean area 569 non-null float64 4 mean smoothness 569 non-null float64 5 mean compactness 569 non-null float64 6 mean concavity 569 non-null float64 7 mean concave points 569 non-null float64 8 mean symmetry 569 non-null float64 9 mean fractal dimension 569 non-null float64 10 radius error 569 non-null float64 11 texture error 569 non-null float64 12 perimeter error 569 non-null float64 13 area error 569 non-null float64 14 smoothness error 569 non-null float64 15 compactness error 569 non-null float64 16 concavity error 569 non-null float64 17 concave points error 569 non-null float64 18 symmetry error 569 non-null float64 19 fractal dimension error 569 non-null float64 20 worst radius 569 non-null float64 21 worst texture 569 non-null float64 22 worst perimeter 569 non-null float64 23 worst area 569 non-null float64 24 worst smoothness 569 non-null float64 25 worst compactness 569 non-null float64 26 worst concavity 569 non-null float64 27 worst concave points 569 non-null float64 28 worst symmetry 569 non-null float64 29 worst fractal dimension 569 non-null float64 30 target 569 non-null float64 dtypes: float64(31) memory usage: 137.9 KB None mean radius mean texture mean perimeter mean area mean smoothness mean compactness mean concavity mean concave points mean symmetry mean fractal dimension radius error texture error perimeter error area error smoothness error compac... 0 17.99 10.38 122.80 1001.0 0.1184 0.2776 0.3001 0.14710 0.2419 0.07871 1.0950 0.9053 8.589 153.40000 0.006399 ... 1 20.57 17.77 132.90 1326.0 0.0847 0.0786 0.0869 0.07017 0.1812 0.05667 0.5435 0.7339 3.398 74.08000 0.005225 ... 2 19.69 21.25 130.00 1203.0 0.1096 0.1599 0.1974 0.12790 0.2069 0.05999 0.7456 0.7869 4.585 94.03000 0.006150 ... 3 11.42 20.38 77.58 386.1 0.1425 0.2839 0.2414 0.10520 0.2597 0.09744 0.4956 1.1560 3.445 27.23000 0.009110 ... 4 20.29 14.34 135.10 1297.0 0.1003 0.1328 0.1980 0.10430 0.1809 0.05883 0.7572 0.7813 5.438 94.44000 0.011490 ... ``` 这里使用了sklearn的load_breast_cancer函数来加载乳腺癌数据集,然后将其转换为pandas DataFrame格式,方便数据分析和处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

昵称系统有问题

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值