PPL 的 DataFrame 库
什么是 PPL,生成代码的 DataFrame 库,DataFrame 库的内容,几个方法的使用示例和结果
引言
PPL 是 **P**arenthesis **P**rogramming **L**anguage(括号编程语言),其中所有元素(语句、参数、块)都包含在括号中。PPL 包含一个预处理器,以简化程序编写并减少括号的数量。PPL 软件包括控制台解释器、WPPF 图形解释器、用于创建用户库 C# 代码的工具、图形助手、用于访问现有 C# 功能的库、教程和示例。PPL 软件可以连接到任何用户应用程序。
DataFrame
库是 PPL 软件的一部分,包含用于具有命名列的表格的不同数据处理方法。
类似 DataFrame
库的软件在其他语言中也有使用,例如 Python 的 Pandas。
DataFrame 库的内容
DataFrame
库包含以下方法
Create
SetWidthForAll
SetRow
SetColumn
Write
ClearColumns
保存
读取
InsertRows
AddRows
InsertColumns
AddColumns
RemoveRows
RemoveColumns
Sort
Reverse
SelectRows
UnSelectRows
可以在解释器 CPPL.exe 或 WPPL.exe 中使用命令 DataFrame.help(method_name)
,或在 Assistant.exe 中获取简短的帮助。有关详细说明和示例,请参阅 TutorialPPL。
创建 DataFrame 代码
PPL 工具 CreateULC.exe 生成 Dataframe
项目的 C# 骨架代码,以便后续填充。库方法名称在文件 config.cfg 中定义
// config for createulc.exe
name = DataFrame
// fullpath where to save generated file or empty
path =
methods = Create, SetRow, SetColumn, Write,
Save, Read,
InsertRows, AddRows,
InsertColumns, AddColumns,
RemoveRows, RemoveColumns,
ClearColumns, SetWidthForAll,
Sort, Reverse,
SelectRows, UnSelectRows
生成的代码 DataFrame.cs
/***************************************************************
*This code generated with Application CreateUserLibCode.exe *
*for creation PPL User Library *
*Add Project Reference PPL.Lib *
*Author: Oscar Kogosov, email: ok21@hotmail.com *
*You must not remove this notice from this software. *
***************************************************************/
using System;
using System.Collections.Generic;
namespace PPLNS
{
public class DataFrame : AbstractClass
{
// ppl & help_dict in Abstract Class
//public PPL ppl;
//Dictionary<string, string> help_dict = new Dictionary<string, string>();
public DataFrame(PPL ppl)
{
this.ppl = ppl;
}
//==========================================================
public void AddToKeywordDictionary()
{
keyword_dict = new Dictionary<string, PPL.OperatorDelegate>();
keyword_dict.Add("help", FuncHelp);
keyword_dict.Add("Create",FuncCreate);
keyword_dict.Add("SetRow",FuncSetRow);
keyword_dict.Add("SetColumn",FuncSetColumn);
keyword_dict.Add("Write",FuncWrite);
keyword_dict.Add("Save",FuncSave);
keyword_dict.Add("Read",FuncRead);
keyword_dict.Add("InsertRows",FuncInsertRows);
keyword_dict.Add("AddRows",FuncAddRows);
keyword_dict.Add("InsertColumns",FuncInsertColumns);
keyword_dict.Add("AddColumns",FuncAddColumns);
keyword_dict.Add("RemoveRows",FuncRemoveRows);
keyword_dict.Add("RemoveColumns",FuncRemoveColumns);
keyword_dict.Add("ClearColumns",FuncClearColumns);
keyword_dict.Add("SetWidthForAll",FuncSetWidthForAll);
keyword_dict.Add("Sort",FuncSort);
keyword_dict.Add("Reverse",FuncReverse);
keyword_dict.Add("SelectRows",FuncSelectRows);
keyword_dict.Add("UnSelectRows",FuncUnSelectRows);
help_dict.Add("help","DataFrame.help([name])");
help_dict.Add("Create","");
help_dict.Add("SetRow","");
help_dict.Add("SetColumn","");
help_dict.Add("Write","");
help_dict.Add("Save","");
help_dict.Add("Read","");
help_dict.Add("InsertRows","");
help_dict.Add("AddRows","");
help_dict.Add("InsertColumns","");
help_dict.Add("AddColumns","");
help_dict.Add("RemoveRows","");
help_dict.Add("RemoveColumns","");
help_dict.Add("ClearColumns","");
help_dict.Add("SetWidthForAll","");
help_dict.Add("Sort","");
help_dict.Add("Reverse","");
help_dict.Add("SelectRows","");
help_dict.Add("UnSelectRows","");
try
{
if (ppl.ImportList.ContainsKey("DataFrame") == false)
{
foreach (KeyValuePair<string, PPL.OperatorDelegate> pair in keyword_dict)
{
ppl.processing.keyword_dict.Add("DataFrame." + pair.Key, pair.Value);
}
ppl.ImportList.Add("DataFrame", this);
}
}
catch (Exception io)
{ }
}
//==========================================================
public bool FuncCreate(List<string> parameters, ref string result,
Composite node = null)
{
try
{
}
catch (Exception ex)
{
ppl.print("Error: ...");
return false;
}
return true;
}
//==========================================================
public bool FuncSetRow(List<string> parameters, ref string result,
Composite node = null)
{
// same code as in FuncCreate
return true;
}
//==========================================================
public bool FuncSetColumn(List<string> parameters, ref string result,
Composite node = null)
{
// same code as in FuncCreate
return true;
}
//==========================================================
public bool FuncWrite(List<string> parameters, ref string result,
Composite node = null)
{
// same code as in FuncCreate
return true;
}
//==========================================================
public bool FuncSave(List<string> parameters, ref string result,
Composite node = null)
{
// same code as in FuncCreate
return true;
}
//==========================================================
public bool FuncRead(List<string> parameters, ref string result,
Composite node = null)
{
// same code as in FuncCreate
return true;
}
//==========================================================
public bool FuncInsertRows(List<string> parameters, ref string result,
Composite node = null)
{
// same code as in FuncCreate
return true;
}
//==========================================================
public bool FuncAddRows(List<string> parameters, ref string result,
Composite node = null)
{
// same code as in FuncCreate
return true;
}
//==========================================================
public bool FuncInsertColumns(List<string> parameters, ref string result,
Composite node = null)
{
// same code as in FuncCreate
return true;
}
//==========================================================
public bool FuncAddColumns(List<string> parameters, ref string result,
Composite node = null)
{
// same code as in FuncCreate
return true;
}
//==========================================================
public bool FuncRemoveRows(List<string> parameters, ref string result,
Composite node = null)
{
// same code as in FuncCreate
return true;
}
//==========================================================
public bool FuncRemoveColumns(List<string> parameters, ref string result,
Composite node = null)
{
// same code as in FuncCreate
return true;
}
//==========================================================
public bool FuncClearColumns(List<string> parameters, ref string result,
Composite node = null)
{
// same code as in FuncCreate
return true;
}
//==========================================================
public bool FuncSetWidthForAll(List<string> parameters, ref string result,
Composite node = null)
{
// same code as in FuncCreate
return true;
}
//==========================================================
public bool FuncSort(List<string> parameters, ref string result,
Composite node = null)
{
// same code as in FuncCreate
return true;
}
//==========================================================
public bool FuncReverse(List<string> parameters, ref string result,
Composite node = null)
{
// same code as in FuncCreate
return true;
}
//==========================================================
public bool FuncSelectRows(List<string> parameters, ref string result,
Composite node = null)
{
// same code as in FuncCreate
return true;
}
//==========================================================
public bool FuncUnSelectRows(List<string> parameters, ref string result,
Composite node = null)
{
// same code as in FuncCreate
return true;
}
}
}
这些方法的每个主体都使用 PPL API,代码对于理解来说并不难,但对于在本页上展示来说太长了,你可以在 DataFrame 源代码 中找到它。
此外,工具 Create ULC.exe 会创建文件 DataFrame.json,供工具 Assistent.exe 使用,你可以在此处查看该文件的一部分
[
{
"cmd": "help",
"ppl": ["DataFrame.help([name])"]
},
{
"cmd": "Create",
"ppl": ["DataFrame.Create([df_name])[(number rows)(column1)(column2)(column3)…]"]
},
{
"cmd": "SetRow",
"ppl": ["DataFrame.SetRow(df_name)(row index)(value column1)( value column2)…"]
},
.............................
]
代码示例
示例 1
创建第一个 DataFrame
默认情况下
DataFrame
名称为 DF
,
行数为 10
,
列名 - AB
、C
、...X
有几种方法可以填充 DataFrame
单元格
调用 DataFrame.SetColumn(dataframe 名称, 列名称, value row1, value row2,...)
调用 DataFrame.SetRow(dataframe 名称, 行索引, value column1, value column2,...)
- 直接使用 PPL 运算符
设置 dataframe_name.column_name[行索引] = value
例如
对于上述示例,set DF.B[0] = PPL
- 从 csv 格式或 data(内部 PPL 格式)的文件中读取数据
调用 DataFrame.Read(dataframe_name,filename)
示例 2
File: examples\df\products.csv
Bagel,140,310,Medium
Buiscuit ,86,480,High
Jaffa cake,48,370,Med-High
Bread white,96,240,Medium
Bread wholemeal,88,220,LowMed
Chapatis,250,240,Medium
Cornflakes,130,300,Med-High
以下 PPL 程序创建 DataFrame
,从 csv 文件读取数据,显示 dataframe
,按 column per100gr 升序排序并再次显示它
Program: examples\df\df8.scr
import DataFrame;
DataFrame.Create(Products)(0)(Bread&Cereals)(Size)(per100grams)(energy);
set Products.Settings.Bread&CerealsWidth = 20;
DataFrame.Read(Products)(examples\df\products.csv);
DataFrame.Write(Products);
DataFrame.Sort(Products)(ascend)(per100grams);
DataFrame.Write(Products);
结论
DataFrame
库的内容将会扩展,并且任何用户添加的建议都将被接受。同样,我也愿意帮助创建新的 PPL 用户库。
历史
- 2022 年 6 月 4 日:初始版本