Object oriented path library, mirroring methods of Pathlib.py

Post your working scripts, libraries and tools.
sashaatx
Posts: 351
Joined: 27 May 2021, 08:27
Contact:

Object oriented path library, mirroring methods of Pathlib.py

Post by sashaatx » 12 Nov 2023, 18:29

https://docs.python.org/3/library/pathlib.html


I like pathlib in python, just copying that library for simple path stuff in ahk. Allowing to do simple shortcuts like:

Code: Select all

Msgbox Path(A_ScriptDir).parent()
;prints dir above the script dir 

global __file__ is more for memes and muscle memory 

Msgbox Path(__file__).exists()

Its been years but from memory, emptying a file, or removing a file is best done with a filemove overwrite process. 

Path(A_ScriptDir "\log.txt").remove() ; moves to temp before deleting
Path(A_ScriptDir "\log.txt").empty() ; appends empty str to temp with matching name before overwrite move
 
For part in Path(__file__).parts()
; array of path parts


Read and write quickly. I may remember wrong, but i/o with a fileopen object is faster, and this is just a shortcut.
Msgbox Path(A_ScriptDir "\log.txt").read()


If you need a method list its here https://github.com/samfisherirl/pathlib.ahk/blob/main/README.md

Probably forgetting a lot of stuff, will take feedback. I put this together in an hour, let me know of any errors.

Cheers.

Code: Select all

global __file__ := A_ScriptFullPath

class Path
{
    __New(pathStr)
    {
        this.path := pathStr
        this.cwd := A_WorkingDir
    }
    exists() => FileExist(this.path)
    read() => FileOpen(this.path, "r").Read()
    write(str) => FileOpen(this.path, "w").Write(str)
    parent()
    {
        SplitPath(this.path, , &parentPath)
        return parentPath
    }
    parts()
    {
        if InStr(this.path, "//")
        {
            delim := "//"
        }
        else if InStr(this.path, "/")
        {
            delim := "/"
        }
        else if InStr(this.path, "\\")
        {
            delim := "\\"
        }
        else if InStr(this.path, "\")
        {
            delim := "\"
        }
        return StrSplit(this.path, delim)
    }
    name()
    {
        SplitPath(this.path, &name)
        return name
    }
    ext()
    {
        SplitPath(this.path, , , &ext)
        return ext
    }
    empty()
    {
        if FileExist(this.path)
        {
            temp := A_Temp . "\" . this.name()
            FileOpen(temp, "a").Write("")
            FileMove(temp, this.path, 1)
        }
    }
    remove()
    {
        temp := A_Temp . "\" . this.name()
        if FileExist(this.path)
        {
            FileMove(this.path, temp, 1)
            FileDelete(temp)
        }
        else if DirExist(this.path)
        {
            DirMove(this.path, temp, 1)
            DirDelete(temp)
        }
    }
}
Last edited by sashaatx on 17 Nov 2023, 12:38, edited 2 times in total.
https://github.com/samfisherirl
? /Easy-Auto-GUI-for-AHK-v2 ? /Useful-AHK-v2-Libraries-and-Classes : /Pulovers-Macro-Creator-for-AHKv2 :

neogna2
Posts: 600
Joined: 15 Sep 2016, 15:44

Re: Object oriented path library, mirroring methods of Pathlib.py

Post by neogna2 » 14 Nov 2023, 10:43

Thank you.

A bit related to the SplitPath wish here and SplitPathObj() example function there
viewtopic.php?p=488898

and the .SplitPath() method of Descolada's String.ahk library
viewtopic.php?t=108272
Its been years but from memory, emptying a file, or removing a file is best done with a filemove overwrite process.
Why is that?

sashaatx
Posts: 351
Joined: 27 May 2021, 08:27
Contact:

Re: Object oriented path library, mirroring methods of Pathlib.py

Post by sashaatx » 14 Nov 2023, 20:01

neogna2 wrote:
14 Nov 2023, 10:43
Thank you.

A bit related to the SplitPath wish here and SplitPathObj() example function there
viewtopic.php?p=488898

and the .SplitPath() method of Descolada's String.ahk library
viewtopic.php?t=108272
Its been years but from memory, emptying a file, or removing a file is best done with a filemove overwrite process.
Why is that?
its funny, I wrote a response. Realized it wasnt really accurate half way. Im trying to recall why I have this thought or belief. I started saying "well to empty a file, deleting and creating a new one takes more time" which is accurate but you could just fileOpen.write("") and then after reading into old forums I realized where it stemmed from.

8+ Years ago when I didnt know dot notation or objects and didnt really know how to read ahk code, I needed to replace a line in a file, I had to file append everything.

So I didnt really understand why but all parts of the below code were important so theyre stuck in my mind.

Code: Select all

newfile:=""
loop parse, filread(x), "`n", "`r"
{
	if instr(A_LoopField, "something"){
		StrReplace(something)
	}
	else
	{
		newfile .= A_LoopField
	}
}

FileAppend(newfile, "newfiletemp.txt")
FileMove(oldfile, newfile, 1)
https://github.com/samfisherirl
? /Easy-Auto-GUI-for-AHK-v2 ? /Useful-AHK-v2-Libraries-and-Classes : /Pulovers-Macro-Creator-for-AHKv2 :

User avatar
emmanuel d
Posts: 90
Joined: 17 Nov 2013, 04:45

Re: Object oriented path library, mirroring methods of Pathlib.py

Post by emmanuel d » 15 Nov 2023, 10:07

FYI

Code: Select all

if FileExist(this.path) or DirExist(this.path)
should be:

Code: Select all

if FileExist(this.path)

FileExist works for files and folders, DirExist is for not having to do the extra instr for "D"

User avatar
emmanuel d
Posts: 90
Joined: 17 Nov 2013, 04:45

Re: Object oriented path library, mirroring methods of Pathlib.py

Post by emmanuel d » 15 Nov 2023, 10:10

This whole thing:

Code: Select all

    exists()
    {
        if FileExist(this.path) or DirExist(this.path)
        {
            return true
        } else
        {
            return false
        }
    }
Could be replaced with this:

Code: Select all

exists() => FileExist(this.path)

sashaatx
Posts: 351
Joined: 27 May 2021, 08:27
Contact:

Re: Object oriented path library, mirroring methods of Pathlib.py

Post by sashaatx » 17 Nov 2023, 12:32

emmanuel d wrote:
15 Nov 2023, 10:10
This whole thing:

Code: Select all

    exists()
    {
        if FileExist(this.path) or DirExist(this.path)
        {
            return true
        } else
        {
            return false
        }
    }
Could be replaced with this:

Code: Select all

exists() => FileExist(this.path)
thank you, will adjust. have been quite slow to adopt fat arrows

Code: Select all

    exists() => FileExist(this.path)
    read() => FileOpen(this.path, "r").Read()
    write(str) => FileOpen(this.path, "w").Write(str)
https://github.com/samfisherirl
? /Easy-Auto-GUI-for-AHK-v2 ? /Useful-AHK-v2-Libraries-and-Classes : /Pulovers-Macro-Creator-for-AHKv2 :

Post Reply

Return to “Scripts and Functions (v2)”