Sinet——Python_AHK 陌诺开源计划

AHK的Array()类型和Map()类型虽然内置了很多函数,但和Python的列表字典相比,仍有较大不足。 这个文档应该会持续更新,我重写了包括List、Dict、Str类型以及一些Python常用库(不完备) 希望能对AHK高效开发产生一定作用,使它更像一门高级编程语言

使用说明:一般来说库前都会有Sinet.的前缀

展示几段代码,以及一些应用

这个是Dict类型重写

Class Dict Extends Map
{
    __Item[Key]
    {
        Get => Super[Key]
        Set => Super[Key] := Value
    }
    
    Clear()
    {
        this.Clear()
    }
    
    Copy()
    {
        Return this.Clone()
    }
    
    Get(Key, Default := "None")
    {
        if this.Has(Key)
            Return this[Key]
        
        Return Default
    }
    
    Has_Key(Key)
    {
        Return this.Has(Key)
    }
    
    Items()
    {
        Lst_Dict := List()
        
        For i, Value in Dict
            Lst_Dict.Append([i, Value])
        
        Return Lst_Dict
    }
    
    Keys()
    {
        Lst_Dict := List()
        
        For i, Value in this
            Lst_Dict.Append(i)
        
        Return Lst_Dict
    }
    
    Pop(Key, Default := "None")
    {
        Try
            Return this.Delete(Key)
        
        Catch
            Return Default
    }
    
    PopItem()
    {
        Last_Key := ""
        
        For i, Value in this
            Last_Key := i
        
        Lst_Last_Key := List()
        Lst_Last_Key.Append([Last_Key, this.Delete(Last_Key)])
        
        Return Lst_Last_Key
    }
    
    SetDefault(Key, Default := 0)
    {
        if this.Has(Key)
            Return this[Key]
        
        else
        {
            this[Key] := Default
            
            Return Default
        }
    }
    
    Str()
    {
        Return Sinet.Print_Return(this)
    }
    
    Subtract(New_Dict)
    {
        Sub_Dict := Dict()
        
        For i, Value in this
        {
            if !New_Dict.Has(i)
                Sub_Dict[i] := Value
        }
        
        Return Sub_Dict
    }
    
    Update(New_Dict)
    {
        For i, Value in New_Dict
            this[i] := Value
    }
    
    Values()
    {
        Lst_Dict := List()
        
        For i, Value in this
            Lst_Dict.Append(Value)
        
        Return Lst_Dict
    }
}

这个是List类型重写

Class List Extends Array
{
    __Item[Index]
    {
        Get => Super[this.Init(Index)]
        Set => Super[this.Init(Index)] := Value
    }
    
    Append(Object*)
    {
        Loop Object.Length
            this.Push(Object[A_Index])
    }
    
    Count(Object)
    {
        Return_Count := 0
        
        For i in this
        {
            if !Sinet.List.Cmp(i, Object)
                Return_Count++
        }
        
        Return Return_Count
    }
    
    Extend(Extend_Lst*)
    {
        Loop Extend_Lst.Length
        {
            if Type(Extend_Lst[A_Index]) !== "Array" && Type(Extend_Lst[A_Index]) !== "List"
                this.Push(Extend_Lst[A_Index])
            
            else
            {
                For i in Extend_Lst[A_Index]
                    this.Push(i)
            }
        }
    }
    
    Index(Object)
    {
        Flag := 0
        Index := 0
        
        For i in this
        {
            if !Sinet.List.Cmp(i, Object)
            {
                Flag := 1
                Break
            }
            
            Index++
        }
        
        if !Flag
            Index := -1
        
        Return Index
    }
    
    Init(Index)
    {
        if Index >= 0
            Index += 1
        
        Return Index
    }
    
    Insert(Index, Object*)
    {
        this.InsertAt(this.Init(Index), Object*)
    }
    
    List(Lst*)
    {
        Return this.Plus(Lst*)
    }
    
    ListMap(Map)
    {
        Lst_Map := List()
    
        if Type(Map) !== "Map" && Type(Map) !== "Dict"
            Return "Illegal Input"
            
        else
        {
            For i, Value in Map
                Lst_Map.Append([i, Value])
            
            Return Lst_Map
        }
    }
    
    Max()
    {
        Return Max(this*)
    }
    
    Min()
    {
        Return Min(this*)
    }
    
    Multiple(Number)
    {
        Lst := List()
        
        Loop Number
        {
            For i in this
            {
                Try
                {
                    Lst.Append(i.Clone())
                    Continue
                }
                
                Catch
                    Error := 1
                
                Lst.Append(i)
            }
        }
        
        this := Lst.Clone()
    }

    Plus(Lst*)
    {
        Loop Lst.Length
        {
            if Type(Lst[A_Index]) !== "Array" && Type(Lst[A_Index]) !== "List"
                this.Append(Lst[A_Index])
            
            else
            {
                For i in Lst[A_Index]
                    this.Append(i)
            }
        }
    }
    
    Pop(Index := -1)
    {
        if Index == -1 || Index >= this.Length
            Return this.Pop()
        
        else
            Return this.RemoveAt(this.Init(Index))
    }
    
    Remove(Object*)
    {
        Loop Object.Length
        {
            Index := this.Index(Object[A_Index])
            
            if Index != -1
                this.Pop(Index)
        }
    }
    
    Reverse()
    {
        Lst := List()
        
        Loop this.Length
            Lst.Append(Lst[Lst.Length - A_Index])
        
        this := Lst.Clone()
    }
    
    SortMap(Map)
    {
        if Type(Map) !== "Map" && Type(Map) !== "Dict"
            Return "Illegal Input"
        
        Lst_Map := this.ListMap(Map)
        
        For i in Range(0, Lst_Map.Length)
        {
            For j in Range(0, Lst_Map.Length)
            {
                if Lst_Map[i][1] > Lst_Map[j][1]
                    Swap(i, j, Lst_Map)
            }
        }
        
        Return Lst_Map
    }
    
    Title()
    {
        Loop this.Length
            if Type(this[A_Index - 1]) == "String"
                this[A_Index - 1] := StrTitle(this[A_Index - 1])
    }
}

Tkinter库使用样例(这个库还在重写进程中)

#Include <Sinet>
index := 0
root := Sinet.Tkinter.Tk()
 
; Adding widgets :
 
; Add button :
btn := Sinet.Tkinter.Button(root, "text=PRESS ME", "func=press")
; Place button in window : 
btn.grid()
 
; Define the function :
press(*)
{
    Global index
    lbl := Sinet.Tkinter.Label(root,"text=You Pressed The Button")
    lbl.grid("x=200","y=" . 20 * index)
    index++
}
 
; Enter the main Loop : 

root.mainloop()
#Include <Sinet>

MyWin := Sinet.Tkinter.Tk()
MyWin.Geometry("600x450+374+182")
MyWin.Title("你好")
p := Sinet.Tkinter.Label(MyWin, "x=100", "text=hello", "fontsize=30", "fg=blue")
q := Sinet.Tkinter.Entry(MyWin, "x=100", "text=hello", "fontsize=30", "fg=blue", "r=2")
r := Sinet.Tkinter.Button(MyWin, "x=300", "text=hello", "fontsize=30", "fg=blue")
p.Grid("x=200", "y=200")
MyWin.Mainloop()

当然要更像Python,还需要一个Print函数,其中引用了Sinet.ToStringPrint函数(可以区分数字与字符串——通过引号,目前测试下打印准确率在95%以上)

Print(Text*)
{
    Print_Text := ""

    Loop Text.Length
    {
        String_Text := Sinet.ToStringPrint(Text[A_Index])
        
        if SubStr(String_Text, -1) == "," && (Type(Text[A_Index]) == "Array" || Type(Text[A_Index]) == "List" || Type(Text[A_Index]) == "Map" || Type(Text[A_Index]) == "Dict" || Type(Text[A_Index]) == "Object")
            String_Text := SubStr(String_Text, 1, StrLen(String_Text) - 1)
        
        Print_Text .= String_Text
    }
    
    MsgBox Print_Text
}

Range(Stop, Start := 0, Step := 1, Swap := -1)
{
    if Swap == -1
    {
        Temp := Stop
        Stop := Start
        Start := Temp
    }
    
    Lst_Range := List()
    Loop_Times := ((Stop - Start) // Abs(Step))
    
    Loop Loop_Times
    {
        if Step = -1
            Lst_Range.Append(Stop - A_Index)
        
        else
            Lst_Range.Append(Start + (A_Index - 1) * Step)
    }
    
    Return Lst_Range
}

Swap(ValueX, ValueY, Object := -1)
{
    if Type(Object) == "Array" || Type(Object) == "List" || Type(Object) == "Map" || Type(Object) == "Dict"
    {
        Temp := Object[ValueX]
        Object[ValueX] := Object[ValueY]
        Object[ValueY] := Temp
    }
    
    else
    {
        Try
        {
            Temp := %ValueX%
            %ValueX% := %ValueY%
            %ValueY% := Temp
        }
        
        Catch
            MsgBox("Swap Error`nTry to Input Object")
    }
}
Class Sinet
{
    Static ToString(Text)
    {
        if Type(Text) == "Array"
        {
            if Text.Length < 1
                Text.InsertAt(1, "")
            
            String_Plus := ""
            String_Text := "[" . Sinet.ToString(Text[1])
            
            Loop Text.Length - 1
                String_Plus .= "," . Sinet.ToString(Text[A_Index + 1])
            
            String_Text .= String_Plus
            String_Text .= "]"
            
            Return String_Text
        }
        
        else if Type(Text) == "List"
        {
            if Text.Length < 1
                Text.InsertAt(1, "")
            
            String_Plus := ""
            String_Text := "[" . Sinet.ToString(Text[0])
            
            Loop Text.Length - 1
                String_Plus .= "," . Sinet.ToString(Text[A_Index])
            
            String_Text .= String_Plus
            String_Text .= "]"
            
            Return String_Text
        }
        
        else if Type(Text) == "Map" || Type(Text) == "Dict"
        {
            String_Text := "{"
            
            For i, Value in Text
                String_Text .= Sinet.ToString(i) . ":" . Sinet.ToString(Value) . ","
            
            if SubStr(String_Text, -1) !== "{"
                String_Text := SubStr(String_Text, 1, StrLen(String_Text) - 1)
            
            String_Text .= "}"
            
            Return String_Text
        }
        
        else if Type(Text) == "Integer" || Type(Text) == "Float" || Type(Text) == "String"
            Return String(Text)
        
        else if Type(Text) == "Object"
        {
            String_Text := "{"
            
            For i, Value in Text.OwnProps()
                String_Text .= Sinet.ToString(i) . ":" . Sinet.ToString(Value) . ","
            
            if SubStr(String_Text, -1) !== "{"
                String_Text := SubStr(String_Text, 1, StrLen(String_Text) - 1)
            
            String_Text .= "}"
            
            Return String_Text
        }
        
        else
            Return "#Null#"
    }
    
    Static ToStringPrint(Text)
    {
        if Type(Text) == "Array"
        {
            if Text.Length < 1
                Text.InsertAt(1, "")
            
            String_Plus := ""
            String_Text := "[" . Sinet.ToStringPrint(Text[1])
            
            Loop Text.Length - 1
                String_Plus .= "," . Sinet.ToStringPrint(Text[A_Index + 1])
            
            String_Text .= String_Plus
            String_Text .= "]"
            
            Return String_Text
        }
        
        else if Type(Text) == "List"
        {
            if Text.Length < 1
                Text.InsertAt(1, "")
            
            String_Plus := ""
            String_Text := "[" . Sinet.ToStringPrint(Text[0])
            
            Loop Text.Length - 1
                String_Plus .= "," . Sinet.ToStringPrint(Text[A_Index])
            
            String_Text .= String_Plus
            String_Text .= "]"
            
            Return String_Text
        }
        
        else if Type(Text) == "Map" || Type(Text) == "Dict"
        {
            String_Text := "{"
            
            For i, Value in Text
                String_Text .= Sinet.ToStringPrint(i) . ":" . Sinet.ToStringPrint(Value) . ","
            
            if SubStr(String_Text, -1) !== "{"
                String_Text := SubStr(String_Text, 1, StrLen(String_Text) - 1)
            
            String_Text .= "}"
            
            Return String_Text
        }
        
        else if Type(Text) == "Integer" || Type(Text) == "Float"
            Return String(Text)
        
        else if Type(Text) == "String"
        {
            Lst_Text := StrSplit(Text, "")
            
            Try
            {
                if Lst_Text[1] !== '"' || Lst_Text[-1] !== '"'
                    Return '"' . Text . '"'
                    
                else
                    Return Text
            }
            
            Catch
                Return Text
        }
        
        else if Type(Text) == "Object"
        {
            String_Text := "{"
            
            For i, Value in Text.OwnProps()
                String_Text .= Sinet.ToStringPrint(i) . ":" . Sinet.ToStringPrint(Value) . ","
            
            if SubStr(String_Text, -1) !== "{"
                String_Text := SubStr(String_Text, 1, StrLen(String_Text) - 1)
            
            String_Text .= "},"
            
            Return String_Text
        }
        
        else
            Return "#Null#"
    }
}

当然作为一个三千多行代码的库包,还提供了各种各样的功能,如之前某个大佬的YAML修订版解析(即修复含\uxxxx的中文字符解析错误的问题,改名为Json库),再比如Pinyin库则是改写了之前V1版本的某个大佬脚本(时间太久了,翻不到大佬名字,如果可以私信发我,我一定贴上——感谢)

再比如之后会将以前写的一个聚合搜索的Api嵌入到目前的Online库(目前只有Network_Work(url, mode := “GET”)和Translate(Text)两个函数)

说了这么多还是希望各位可以在使用我重写的这个大类中找到AHK V2的美妙之处,不过请一定尊重我个人的劳动成果,不得用于商业用途,至于转载,带上我的名字就成(😀

Tip:Autohotkey v2 beta4已发布,AHK2exe也可以使用了,我一并附在了文件中

Mono 2022.6.16

链接: https://pan.baidu.com/s/1pFxwoC07SjuQhuzs1PnJLA?pwd=mgec 提取码: mgec 复制这段内容后打开百度网盘手机App,操作更方便哦
–来自百度网盘超级会员v5的分享

 

给TA捐赠
共{{data.count}}人
人已捐赠
AHKV2游戏社区推荐

AHK游戏系列:俄罗斯方块

2022-6-17 6:53:54

办公工具社区推荐

ahk悬浮粘贴板内容在桌面,实现复制

2022-6-17 19:43:15

6 条回复 A文章作者 M管理员
  1. jly

    您好,请问 这个json库在哪里下载?

    “如之前某个大佬的YAML修订版解析(即修复含uxxxx的中文字符解析错误的问题,改名为Json库)”

    • 陌诺Mono

      都封装在我的库里了,用Sinet.Json.Parse和Sinet.Json.Stringify调用,压缩包lib文件夹里应该有源文件YAML.ahk

  2. dbgba
    dbgba给您捐赠了¥2
  3. 1河许人
    1河许人给您捐赠了¥2
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索