Flutter插件packhorse的使用方法
Flutter插件packhorse的使用方法
欢迎来到packhorse
库,它支持从小型到中型的数据处理和分析项目,无论是客户端还是服务器端。
packhorse
提供了几个类,使我们能够轻松地定义新的表格和列,提取常用的统计信息,执行结构化操作如连接,并将数据导出为多种格式,如CSV、HTML和JSON。
示例
final rectPart1 = """
id,length,color
0,12,red
1,15,blue
2,6,green
3,10,blue
4,5,green
""".parseAsCsv(),
rectPart2 = {
"id": [0, 1, 2, 3, 4],
"width": [4, 9, 3, 2, 10],
}.toDataFrame();
print(
rectPart1
.withInnerJoinOn(
rectPart2,
pivot: "id",
)
.withNumericColumnFromFormula(
name: "area",
formula: "length * width",
)
.withCategoricColumnFromRowValues(
name: "desc",
generator: (numeric, categoric) =>
(numeric["area"]! > 25 ? "large-" : "small-") +
categoric["color"]!,
),
);
输出结果如下:
.--.------.--------.-----.----.-------.-------------.
|id|length|right_id|width|area|color |desc |
:--+------+--------+-----+----+-------+-------------:
|0 |12 |0 |4 |48 |red |large-red |
|1 |15 |1 |9 |135 |blue |large-blue |
|2 |6 |2 |3 |18 |green |small-green |
|3 |10 |3 |2 |20 |blue |small-blue |
|4 |5 |4 |10 |50 |green |large-green |
'--'------'--------'-----'----'-------'-------------'
查看example/example.md
文件中的示例,可以快速了解该库的使用方法。
感谢您对本库的兴趣。请在这里提交问题和请求:[问题提交链接]。
实例化
列实例化
NumericColumn
和CategoricColumn
实例通常直接从迭代器中实例化:
final numeric = NumericColumn([1, 2, 3]),
categoric = CategoricColumn(["a", "b", "c"]);
print(numeric);
print(categoric);
输出结果如下:
NumericColumn [1, 2, 3]
CategoricColumn [a, b, c]
[618 μs]
另外,我们也可以直接从列表中实例化这些实例:
final numeric = [1, 2, 3].toNumericColumn(),
categoric = ["a", "b", "c"].toCategoricColumn();
print(numeric);
print(categoric);
输出结果如下:
NumericColumn [1, 2, 3]
CategoricColumn [a, b, c]
[610 μs]
数据帧实例化
DataFrame
实例可以从字符串(CSV或JSON表示)、映射或列表中实例化。例如:
final df = """
id,petal_length,petal_width,species
1,1.4,0.2,setosa
2,1.4,0.2,setosa
3,1.3,0.2,setosa
4,1.5,0.2,setosa
51,4.7,1.4,versicolor
52,4.5,1.5,versicolor
53,4.9,1.5,versicolor
54,4.0,1.3,versicolor
101,6.0,2.5,virginica
102,5.1,1.9,virginica
103,5.9,2.1,virginica
104,5.6,1.8,virginica
""".parseAsCsv();
print(df);
输出结果如下:
.---.------------.-----------.------------.
|id |petal_length|petal_width|species |
:---+------------+-----------+------------:
|1 |1.4 |0.2 |setosa |
|2 |1.4 |0.2 |setosa |
|3 |1.3 |0.2 |setosa |
|4 |1.5 |0.2 |setosa |
|51 |4.7 |1.4 |versicolor |
|52 |4.5 |1.5 |versicolor |
|53 |4.9 |1.5 |versicolor |
|54 |4.0 |1.3 |versicolor |
|101|6.0 |2.5 |virginica |
|102|5.1 |1.9 |virginica |
|103|5.9 |2.1 |virginica |
|104|5.6 |1.8 |virginica |
'---'------------'-----------'------------'
[3729 μs]
数据帧实例化(其他方式)
final data = """
{
"a": [1, 2, 3],
"b": ["red", "blue", "blue"]
}
""".parseAsMapOfLists();
print(data);
输出结果如下:
.-.------.
|a|b |
:-+------:
|1|red |
|2|blue |
|3|blue |
'-'------'
[5097 μs]
final data = """
[
{"a": 1, "b": "red"},
{"a": 2, "b": "blue"},
{"a": 3, "b": "blue"}
]
""".parseAsListOfMaps();
print(data);
输出结果如下:
.-.------.
|a|b |
:-+------:
|1|red |
|2|blue |
|3|blue |
'-'------'
[4695 μs]
final data = {
"a": [1, 2, 3],
"b": ["red", "blue", "blue"]
}.toDataFrame();
print(data);
输出结果如下:
.-.------.
|a|b |
:-+------:
|1|red |
|2|blue |
|3|blue |
'-'------'
[3297 μs]
final data = [
{"a": 1, "b": "red"},
{"a": 2, "b": "blue"},
{"a": 3, "b": "blue"},
].toDataFrame();
print(data);
输出结果如下:
.-.------.
|a|b |
:-+------:
|1|red |
|2|blue |
|3|blue |
'-'------'
[3067 μs]
操作
在以下示例中,数据帧iris
、petals
和sepals
已从著名的iris
数据集中创建。
使用with...
方法
以withDataAdded
、withColumns
、withLeftJoin
和withNumericFromFormula
等方法为例,它们返回新的数据帧,因此可以方便地进行链式调用:
final focus = iris
.withColumnsDropped([
"sepal_length",
"petal_length",
])
.withNumeric(
name: "sepal_width_z_scores",
column: NumericColumn(
iris.numericColumns["sepal_width"]!.zScores,
),
)
.withRowsSampled(
10,
withReplacement: true,
seed: 0,
);
print(focus);
输出结果如下:
.---.-----------.-----------.--------------------.------------.
|id |sepal_width|petal_width|sepal_width_z_scores|species |
:---+-----------+-----------+--------------------+------------:
|106|3.0 |2.1 |-0.1319794793216258 |virginica |
|60 |2.7 |1.4 |-0.8225697780975647 |versicolor |
|65 |2.9 |1.3 |-0.36217624558027245|versicolor |
|50 |3.3 |0.2 |0.5586108194543131 |setosa |
|7 |3.4 |0.3 |0.7888075857129598 |setosa |
|142|3.1 |2.3 |0.09821728693702086 |virginica |
|45 |3.8 |0.4 |1.7095946507475455 |setosa |
|32 |3.4 |0.4 |0.7888075857129598 |setosa |
|92 |3.0 |1.4 |-0.1319794793216258 |versicolor |
|120|2.2 |1.5 |-1.973553609390797 |virginica |
'---'-----------'-----------'--------------------'------------'
[5571 μs]
变更
通过模板、公式和行变量访问来创建新列。
使用模板
模板是一个带有占位符的字符串,标记为列名。例如:
print(petals.withCategoricColumnFromTemplate(
name: "id_code",
template: "{species}-{id}"));
输出结果如下:
.---.------------.-----------.------------.---------------.
|id |petal_length|petal_width|species |id_code |
:---+------------+-----------+------------+---------------:
|1 |1.4 |0.2 |setosa |setosa-1 |
|2 |1.4 |0.2 |setosa |setosa-2 |
|3 |1.3 |0.2 |setosa |setosa-3 |
|4 |1.5 |0.2 |setosa |setosa-4 |
|51 |4.7 |1.4 |versicolor |versicolor-51 |
|52 |4.5 |1.5 |versicolor |versicolor-52 |
|53 |4.9 |1.5 |versicolor |versicolor-53 |
|54 |4.0 |1.3 |versicolor |versicolor-54 |
|101|6.0 |2.5 |virginica |virginica-101 |
|102|5.1 |1.9 |virginica |virginica-102 |
|103|5.9 |2.1 |virginica |virginica-103 |
|104|5.6 |1.8 |virginica |virginica-104 |
'---'------------'-----------'------------'---------------'
[4609 μs]
我们还可以从模板创建数值列:
print(petals.withNumericColumnFromTemplate(
name: "species_letters",
template: "{species}",
generator: (result) => result.length,
));
输出结果如下:
.---.------------.-----------.---------------.------------.
|id |petal_length|petal_width|species_letters|species |
:---+------------+-----------+---------------+------------:
|1 |1.4 |0.2 |6 |setosa |
|2 |1.4 |0.2 |6 |setosa |
|3 |1.3 |0.2 |6 |setosa |
|4 |1.5 |0.2 |6 |setosa |
|51 |4.7 |1.4 |10 |versicolor |
|52 |4.5 |1.5 |10 |versicolor |
|53 |4.9 |1.5 |10 |versicolor |
|54 |4.0 |1.3 |10 |versicolor |
|101|6.0 |2.5 |9 |virginica |
|102|5.1 |1.9 |9 |virginica |
|103|5.9 |2.1 |9 |virginica |
|104|5.6 |1.8 |9 |virginica |
'---'------------'-----------'---------------'------------'
[4542 μs]
使用公式
公式是一个数学表达式,使用数值列名称作为变量。(更多关于支持的数学表达式的详细信息,请参阅function-tree库。)
例如:
print(petals.withNumericColumnFromFormula(
name: "log_petal_area",
formula: "log(petal_length * petal_width)",
));
输出结果如下:
.---.------------.-----------.-------------------.------------.
|id |petal_length|petal_width|log_petal_area |species |
:---+------------+-----------+-------------------+------------:
|1 |1.4 |0.2 |-1.2729656758128876|setosa |
|2 |1.4 |0.2 |-1.2729656758128876|setosa |
|3 |1.3 |0.2 |-1.3470736479666092|setosa |
|4 |1.5 |0.2 |-1.203972804325936 |setosa |
|51 |4.7 |1.4 |1.884034745337226 |versicolor |
|52 |4.5 |1.5 |1.9095425048844386 |versicolor |
|53 |4.9 |1.5 |1.9947003132247454 |versicolor |
|54 |4.0 |1.3 |1.6486586255873816 |versicolor |
|101|6.0 |2.5 |2.70805020110221 |virginica |
|102|5.1 |1.9 |2.2710944259026746 |virginica |
|103|5.9 |2.1 |2.516889695641051 |virginica |
|104|5.6 |1.8 |2.3105532626432224 |virginica |
'---'------------'-----------'-------------------'------------'
[10118 μs]
我们还可以使用公式创建分类列:
print(petals.withCategoricColumnFromFormula(
name: "description",
formula: "petal_width / petal_length",
generator: (result) => result < 0.3 ? "narrow" : "wide",
));
输出结果如下:
.---.------------.-----------.------------.-----------.
|id |petal_length|petal_width|species |description|
:---+------------+-----------+------------+-----------:
|1 |1.4 |0.2 |setosa |narrow |
|2 |1.4 |0.2 |setosa |narrow |
|3 |1.3 |0.2 |setosa |narrow |
|4 |1.5 |0.2 |setosa |narrow |
|51 |4.7 |1.4 |versicolor |narrow |
|52 |4.5 |1.5 |versicolor |wide |
|53 |4.9 |1.5 |versicolor |wide |
|54 |4.0 |1.3 |versicolor |wide |
|101|6.0 |2.5 |virginica |wide |
|102|5.1 |1.9 |virginica |wide |
|103|5.9 |2.1 |virginica |wide |
|104|5.6 |1.8 |virginica |wide |
'---'------------'-----------'------------'-----------'
[5802 μs]
使用行变量
print(petals.withCategoricColumnFromRowValues(
name: "code",
generator: (numeric, categoric) {
final pre = categoric["species"]!.substring(0, 3),
area = (numeric["petal_length"]! * numeric["petal_width"]!)
.toStringAsFixed(2)
.padLeft(5, "0");
return "$pre-$area";
},
));
输出结果如下:
.---.------------.-----------.------------.-----------.
|id |petal_length|petal_width|species |code |
:---+------------+-----------+------------+-----------:
|1 |1.4 |0.2 |setosa |set-00.28 |
|2 |1.4 |0.2 |setosa |set-00.28 |
|3 |1.3 |0.2 |setosa |set-00.26 |
|4 |1.5 |0.2 |setosa |set-00.30 |
|51 |4.7 |1.4 |versicolor |ver-06.58 |
|52 |4.5 |1.5 |versicolor |ver-06.75 |
|53 |4.9 |1.5 |versicolor |ver-07.35 |
|54 |4.0 |1.3 |versicolor |ver-05.20 |
|101|6.0 |2.5 |virginica |vir-15.00 |
|102|5.1 |1.9 |virginica |vir-09.69 |
|103|5.9 |2.1 |virginica |vir-12.39 |
|104|5.6 |1.8 |virginica |vir-10.08 |
'---'------------'-----------'------------'-----------'
[3781 μs]
产品
我们可以将数据帧输出为几种文本表示形式,如Markdown、CSV和HTML:
print(petals.toMarkdown());
输出结果如下:
|id|petal_length|petal_width|species|
|:--|:--|:--|:--|
|1|1.4|0.2|setosa|
|2|1.4|0.2|setosa|
|3|1.3|0.2|setosa|
|4|1.5|0.2|setosa|
|51|4.7|1.4|versicolor|
|52|4.5|1.5|versicolor|
|53|4.9|1.5|versicolor|
|54|4.0|1.3|versicolor|
|101|6.0|2.5|virginica|
|102|5.1|1.9|virginica|
|103|5.9|2.1|virginica|
|104|5.6|1.8|virginica|
[2850 μs]
print(petals.toCsv());
输出结果如下:
id,petal_length,petal_width,species
1,1.4,0.2,"setosa"
2,1.4,0.2,"setosa"
3,1.3,0.2,"setosa"
4,1.5,0.2,"setosa"
51,4.7,1.4,"versicolor"
52,4.5,1.5,"versicolor"
53,4.9,1.5,"versicolor"
54,4.0,1.3,"versicolor"
101,6.0,2.5,"virginica"
102,5.1,1.9,"virginica"
103,5.9,2.1,"virginica"
104,5.6,1.8,"virginica"
[2052 μs]
print(petals.toHtml());
输出结果如下:
<div class="packhorse">
<table>
<thead>
<tr><th>id</th><th>petal_length</th><th>petal_width</th><th>species</th></tr>
</thead>
<tbody>
<tr><td>1</td><td>1.4</td><td>0.2</td><td>setosa</td></tr>
<tr><td>2</td><td>1.4</td><td>0.2</td><td>setosa</td></tr>
<tr><td>3</td><td>1.3</td><td>0.2</td><td>setosa</td></tr>
<tr><td>4</td><td>1.5</td><td>0.2</td><td>setosa</td></tr>
<tr><td>51</td><td>4.7</td><td>1.4</td><td>versicolor</td></tr>
<tr><td>52</td><td>4.5</td><td>1.5</td><td>versicolor</td></tr>
<tr><td>53</td><td>4.9</td><td>1.5</td><td>versicolor</td></tr>
<tr><td>54</td><td>4.0</td><td>1.3</td><td>versicolor</td></tr>
<tr><td>101</td><td>6.0</td><td>2.5</td><td>virginica</td></tr>
<tr><td>102</td><td>5.1</td><td>1.9</td><td>virginica</td></tr>
<tr><td>103</td><td>5.9</td><td>2.1</td><td>virginica</td></tr>
<tr><td>104</td><td>5.6</td><td>1.8</td><td>virginica</td></tr>
</tbody>
</table>
</div>
[2702 μs]
print(sepals.withHead(2).toJsonAsMapOfLists());
输出结果如下:
{"id":["3","4"],"sepal_length":["4.7","4.6"],"sepal_width":["3.2","3.1"]}
[2779 μs]
添加数据
我们可以垂直合并两个具有相同列的数据帧。
final headAndTail = iris.withHead(5).withRowsAdded(iris.withTail(5));
print(headAndTail);
输出结果如下:
.---.------------.-----------.------------.-----------.-----------.
|id |sepal_length|sepal_width|petal_length|petal_width|species |
:---+------------+-----------+------------+-----------+-----------:
|1 |5.1 |3.5 |1.4 |0.2 |setosa |
|2 |4.9 |3.0 |1.4 |0.2 |setosa |
|3 |4.7 |3.2 |1.3 |0.2 |setosa |
|4 |4.6 |3.1 |1.5 |0.2 |setosa |
|5 |5.0 |3.6 |1.4 |0.3 |setosa |
|146|6.7 |3.0 |5.2 |2.3 |virginica |
|147|6.3 |2.5 |5.0 |1.9 |virginica |
|148|6.5 |3.0 |5.2 |2.0 |virginica |
|149|6.2 |3.4 |5.4 |2.3 |virginica |
|150|5.9 |3.0 |5.1 |1.8 |virginica |
'---'------------'-----------'------------'-----------'-----------'
[4059 μs]
连接
数据帧支持连接操作:
print("Left (petals sample):");
print(petals);
print("\nRight (sepals sample):");
print(sepals);
print("\nLeft-join:");
print(petals.withLeftJoinOn(sepals, pivot: "id"));
print("\nLeft-outer-join:");
print(petals.withLeftOuterJoinOn(sepals, pivot: "id"));
print("\nRight-join:");
print(petals.withRightJoinOn(sepals, pivot: "id"));
print("\nRight-outer-join:");
print(petals.withRightOuterJoinOn(sepals, pivot: "id"));
print("\nFull-join:");
print(petals.withFullJoinOn(sepals, pivot: "id"));
print("\nInner-join:");
print(petals.withInnerJoinOn(sepals, pivot: "id"));
print("\nOuter-join:");
print(petals.withOuterJoinOn(sepals, pivot: "id"));
输出结果如下:
更多关于Flutter插件packhorse的使用方法的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter插件packhorse的使用方法的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
packhorse
是一个 Flutter 插件,尽管它不是 Flutter 官方核心库的一部分,但它的名称暗示了它可能与“打包”或“负载处理”有关。以下是关于 packhorse
插件的一些潜在使用场景和功能的推测,以及如何在 Flutter 项目中利用它的建议。
1. 数据打包与解包
- 用途:
packhorse
可能用于将复杂的数据结构(如 JSON、Protobuf 或其他二进制格式)打包成更小的、网络传输友好的格式,或者在接收端解包数据。 - 示例:
// 假设 packhorse 提供数据打包功能 var packedData = Packhorse.pack(jsonData); var unpackedData = Packhorse.unpack(packedData);
- 场景: 在需要高效传输数据的应用中,例如聊天应用、实时数据同步等。
2. 文件压缩与解压缩
- 用途:
packhorse
可能提供文件压缩功能,支持将多个文件或文件夹打包成单个压缩文件(如 ZIP 或 TAR),或者在需要时解压缩。 - 示例:
// 压缩文件 Packhorse.compressFiles(['file1.txt', 'file2.jpg'], 'archive.zip'); // 解压缩文件 Packhorse.decompressFile('archive.zip', 'output_directory');
- 场景: 在需要处理大量文件的应用中,例如文件管理器、备份工具等。
3. 资源管理与优化
- 用途:
packhorse
可能用于优化应用资源(如图片、音频、视频)的加载和传输,减少包体积或网络带宽消耗。 - 示例:
// 优化图片资源 var optimizedImage = Packhorse.optimizeImage('large_image.png');
- 场景: 在资源密集型应用中,例如图片编辑器、视频播放器等。
4. 网络请求优化
- 用途:
packhorse
可能用于优化网络请求,例如将多个小请求合并为一个请求,或者将大请求分割为多个小请求。 - 示例:
// 合并多个网络请求 var combinedResponse = Packhorse.combineRequests([request1, request2]);
- 场景: 在需要高效处理网络请求的应用中,例如电商、社交应用等。
5. 代码与资源加密
- 用途:
packhorse
可能提供加密功能,用于保护敏感数据或应用资源。 - 示例:
// 加密数据 var encryptedData = Packhorse.encryptData('sensitive_data', 'password'); // 解密数据 var decryptedData = Packhorse.decryptData(encryptedData, 'password');
- 场景: 在需要保护用户隐私或应用安全的应用中,例如金融、健康类应用。
6. 插件与依赖管理
- 用途:
packhorse
可能用于管理 Flutter 插件或依赖项,帮助开发者更轻松地集成和维护第三方库。 - 示例:
// 安装插件 Packhorse.installPlugin('flutter_plugin_name'); // 卸载插件 Packhorse.uninstallPlugin('flutter_plugin_name');
- 场景: 在需要频繁更新或管理插件的复杂项目中。
7. 跨平台支持
- 用途:
packhorse
可能提供跨平台支持,简化在不同平台(如 iOS、Android、Web)上的开发和部署。 - 示例:
// 检查当前平台 var platform = Packhorse.getPlatform(); // 执行平台特定逻辑 if (platform == 'iOS') { // iOS 代码 } else if (platform == 'Android') { // Android 代码 }
- 场景: 在需要支持多平台的应用中。
如何开始使用 packhorse
?
- 安装插件:
在
pubspec.yaml
中添加依赖:dependencies: packhorse: ^latest_version