Help w. Nested Class with object values, enumerated into a dictionary Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
sashaatx
Posts: 333
Joined: 27 May 2021, 08:27
Contact:

Help w. Nested Class with object values, enumerated into a dictionary

Post by sashaatx » 25 Mar 2023, 08:44

Im coming into ahkv2 after time away in python, youll see some python references (dictionary in place of associative array)

I need help creating nested class of this.value := "str"
My brain tells me to nest this class with the parent being a dictionary method

this is all in service of setting json values for json.dump

I left a duplication for "this.dictionary:={}" for where I tried placing it, this parent class being the first attempt, right within the nested class after.

I felt it would be easier to describe the workflow via image but raw code is below as well

edit: I have a syntax error in the msgbox section, it should be .= not :=

I think I figured part of it out, I needed to do this:

Code: Select all

myApp := appDic()
appOne := myApp.defineApp("samfisherirl", "Github.ahk")
appOne.connectGithubAPI()
but I still dont know how to reference the parent class. broken code still could use tips

Code: Select all

; Include the JSON library
#Include %A_ScriptDir%\lib\Native.ahk
#Include %A_ScriptDir%\lib\github.ahk



; Example usage
; Create a new instance of the appDictionary class

myApp := appDic.defineApp("samfisherirl", "Github.ahk")
; Call the defineApp method to add a dictionary to the array

myApp.connectGithubAPI()
;set further existing unset values in object


; Serialize the array of dictionaries as JSON and print the result
;//////////json := myApp.SerializeToJson()

myApp.setDic(myApp)
; enumerates set values in nested class into dictionary

myApp.printDic()



; Define a class for building dictionaries of 3 strings
class appDic { 
    __New() {
        this.dictionary := {}
    }
    ; Define a method for adding a dictionary of 3 strings to the array
    class defineApp {
        __New(username, repo) {
                ; Create a new dictionary with the given strings
                this.username := username, 
                this.repo := repo, 
                this.downloadUrl := "",
                this.version := "",
                this.releaseNotes := "",
                this.appPath := "",
                this.downloadPath := ""
                this.dictionary := {}
                
            }
        
        setPath(appPath) {
            this.downloadPath := appPath
        }
        
        connectGithubAPI() {
            ; retieve from github library latest releaseUrl, notes, and version
            git := Github(this.username, this.repo)
            this.downloadUrl := git.releaseUrl()
            this.version := git.version()
            this.releaseNotes := git.details()
        }
            
        setDic() {
            for key, value in appDic.defineApp {
                this.dictionary[key] := value
                }
            }
        printDic() {
            for key, value in this.dictionary {
                msg := key . ": " . value . "`n"
            }
            msgbox(msg)
        }
        ; Add the new dictionary to the array
        ; this.dictionaries.push(defineApp)
         /*
    ; Define a method for serializing the array of dictionaries as JSON
        SerializeToJson() {
            ; Serialize the array of dictionaries as JSON
            jdata := JSON.stringify(this.dictionaries)
            ; Return the serialized JSON string
            return jdata
        }
    ; retreive github library data including url to download and version data
    */
    }
}
Attachments
Untitled-1.png
Untitled-1.png (430.03 KiB) Viewed 529 times
https://github.com/samfisherirl
? /Easy-Auto-GUI-for-AHK-v2 ? /Useful-AHK-v2-Libraries-and-Classes : /Pulovers-Macro-Creator-for-AHKv2 :

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Help w. Nested Class with object values, enumerated into a dictionary

Post by swagfag » 25 Mar 2023, 09:14

first decide what signature the instance method setDic(...) is supposed to have. if ure going to be passing a parameter in, then define one such. if its not gonna be accepting any parameters, dont pass any at the callsite

once uve got that figured out, u can start thinking about what that for-loop inside is actually meant to enumerate: the thing(which may or may not be an Object) that was passed in? this? some hardcoded Class?

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

Re: Help w. Nested Class with object values, enumerated into a dictionary

Post by sashaatx » 25 Mar 2023, 09:28

swagfag wrote:
25 Mar 2023, 09:14
first decide what signature the instance method setDic(...) is supposed to have. if ure going to be passing a parameter in, then define one such. if its not gonna be accepting any parameters, dont pass any at the callsite

once uve got that figured out, u can start thinking about what that for-loop inside is actually meant to enumerate: the thing(which may or may not be an Object) that was passed in? this? some hardcoded Class?
I'm unsure what you mean by decide what signature is suppose to have.

You're maybe assuming my knowledge of how I want to structure this is set or to be set

I have a descriptive outcome, without knowledge of how to structure.

1. two params are set out of 5 to be set. These 5 potential set:values are all on one set:value class
2. method at same level will set the remaining 3 values
3. Help! I want to enumerate those class items. Should I have the dictionary at the parent, or child level or maybe it does not matter, I need advice.
maybe dictionary object isnt doable.
the for loop is decided. there is only 1 class (app) with 5 params. those params => enumerate to dictionary.

in python it would look like this:

Code: Select all

class Dict:
  def __init__(self):
    self.dic = {}

  class App(Dict): <= inherits class
    def __init__(self, param_one, param_two):
      super().__init__() <= takes dic from parent class
      self.param_one = param_one
      self.param_two = param_two

    def return_key_value_of_child(self):
      return self.__dic__ <= converts params and their values to dic

    def enumerator(self):
      for key, val in return_key_value_of_child().items():  
        self.dic[key] = val <=<=<=<=<=< all i want <= enumerated dic is referenced from parent. 

obj = App(param_one, param_two)
obj.enumerator()

print(obj.dic)
https://github.com/samfisherirl
? /Easy-Auto-GUI-for-AHK-v2 ? /Useful-AHK-v2-Libraries-and-Classes : /Pulovers-Macro-Creator-for-AHKv2 :

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

Re: Help w. Nested Class with object values, enumerated into a dictionary

Post by sashaatx » 25 Mar 2023, 09:55

swagfag wrote:
25 Mar 2023, 09:14
first decide what signature the instance method setDic(...) is supposed to have. if ure going to be passing a parameter in, then define one such. if its not gonna be accepting any parameters, dont pass any at the callsite

once uve got that figured out, u can start thinking about what that for-loop inside is actually meant to enumerate: the thing(which may or may not be an Object) that was passed in? this? some hardcoded Class?
might be OwnProps()
https://github.com/samfisherirl
? /Easy-Auto-GUI-for-AHK-v2 ? /Useful-AHK-v2-Libraries-and-Classes : /Pulovers-Macro-Creator-for-AHKv2 :

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Help w. Nested Class with object values, enumerated into a dictionary

Post by swagfag » 25 Mar 2023, 11:10

sashaatx wrote:
25 Mar 2023, 09:28
swagfag wrote:
25 Mar 2023, 09:14
first decide what signature the instance method setDic(...) is supposed to have. if ure going to be passing a parameter in, then define one such. if its not gonna be accepting any parameters, dont pass any at the callsite
I'm unsure what you mean by decide what signature is suppose to have.
what i mean by this is:
  • in ur code, u wrote: myApp.setDic(myApp), which in plain english says: "invoke the method setDic on the Object myApp(or the first Prototype thereof that defines such a method), and pass it the argument myApp"
  • whereas in ur class definition, u wrote: setDic() {, which in plain english says: "any instantiated objects of this Class will have access to a method called setDic, which is a method accepting no arguments"
obviously i cant read ur mind, so its up to u to decide, which one of those 2 definitions is the correct one for ur use-case
i dont do python code, much less so broken, uncompilable python code, so i cant comment on what the code is supposed to be doing or what its meant to achieve

from the tidbits revealed so far, i dont see what apparent need there is for having 2 classes, nesting one into the other and also inheriting from it. just write 1 class:

Code: Select all

class App
{
	........
}
and done

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

Re: Help w. Nested Class with object values, enumerated into a dictionary

Post by sashaatx » 25 Mar 2023, 11:58

swagfag wrote:
25 Mar 2023, 11:10
sashaatx wrote:
25 Mar 2023, 09:28
swagfag wrote:
25 Mar 2023, 09:14
first decide what signature the instance method setDic(...) is supposed to have. if ure going to be passing a parameter in, then define one such. if its not gonna be accepting any parameters, dont pass any at the callsite
I'm unsure what you mean by decide what signature is suppose to have.
what i mean by this is:
  • in ur code, u wrote: myApp.setDic(myApp), which in plain english says: "invoke the method setDic on the Object myApp(or the first Prototype thereof that defines such a method), and pass it the argument myApp"
  • whereas in ur class definition, u wrote: setDic() {, which in plain english says: "any instantiated objects of this Class will have access to a method called setDic, which is a method accepting no arguments"
obviously i cant read ur mind, so its up to u to decide, which one of those 2 definitions is the correct one for ur use-case
i dont do python code, much less so broken, uncompilable python code, so i cant comment on what the code is supposed to be doing or what its meant to achieve

from the tidbits revealed so far, i dont see what apparent need there is for having 2 classes, nesting one into the other and also inheriting from it. just write 1 class:

Code: Select all

class App
{
	........
}
and done
you're right. I ended up using one class. I want to use a nested class for future app expansion. I'm going to be explicit because it seems the more Information I provide, you respond with more questions. I must not be clear because you're responding with rhetorical questions describing how I should ponder something but that means you're not understanding or Im not being clear, Ill take the L that Im being too descriptive.

I mentioned in my post Im returning to ahk, my syntax for v2 is being learned, which means Im having issues.

If you're asking me to decide, that presupposes I have the answers. But I dont. You're asking me why I did (x) that contradicts (y) but I dont know the answer and fixing either doesnt get me to (z) fixing my problem and I've described what Im trying to do and how I tried to do it. The why is because Im learning. The what is colored below.

Im saying I was descriptive to the outcome, throw it all in the trash if you want.

Ive used my ahk code, my python code and plain english and a flow chart. It baffles me that you feel you need to mind read because we need more to solve the problem. Either way, Ill blame my over inclusion of information.

what I need, plain and simple. how to properly talk to parent child class object values, from either
-in the parents "__new" section, or instantiation, how do i reference in the method and or "__new" section of the child, the parent
-OR in the childs "__new" section, or instantiation, how do i reference in the method and or "__new" section of the parent, the child.

I've figured out the for loop, OwnProps {dic.%key% := value}
https://github.com/samfisherirl
? /Easy-Auto-GUI-for-AHK-v2 ? /Useful-AHK-v2-Libraries-and-Classes : /Pulovers-Macro-Creator-for-AHKv2 :

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Help w. Nested Class with object values, enumerated into a dictionary  Topic is solved

Post by swagfag » 25 Mar 2023, 16:04

but you do have the answers! who else would have them besides u? u have a particular class in mind. u want the class to used in a particular way. its only a matter of defining its interface:
  • step 1, ok, u instantiate this object passing it these parameters
  • step 2, ok, u call this method on the instance u just constructed passing it no parameters
  • step 3, ok, now u call this other method on the instance u just constructed passing it an associative array of key-value pairs as its first and only parameter
  • step 4, ok now u do whatever
  • step 5, yadda yadda...
  • step N, ure done

Ive used my ahk code, my python code and plain english and a flow chart. It baffles me that you feel you need to mind
the problem with all that is ur ahk code, ur python code, ur plain english explanation and ur graphic all contradict each other at every intersection
  • ur ahk code... can be written either way: passing 0 parameters to a .setDict() method and passing 1 parameter to a .setDict(....) method
  • ur python code... wants to pass 0 parameters (see def enumerator(self): and obj.enumerator())
  • ur plain english explanation... doesnt even state where the data is supposed to be coming from
  • ur graphic... has the same ambiguity as ur ahk code, since its based on it
what am i to make of all this? at this point, assuming i was capable of reading minds, im starting to doubt whether reading urs would even help at all or whether it would just leave me even more confused than ever
what I need, plain and simple. how to properly talk to parent child class object values, from either
-in the parents "__new" section, or instantiation, how do i reference in the method and or "__new" section of the child, the parent
-OR in the childs "__new" section, or instantiation, how do i reference in the method and or "__new" section of the parent, the child.
:crazy: :crazy: :crazy: plain and simple, huh. if u had told me joe biden asked this, i would have believed u outright
anyway, heres some commented ahk code based on what I think u probably intended to write:

Code: Select all

#Requires AutoHotkey v2.0.2

; construct an instance of the Class `defineApp`, 
; which happens to reside nested inside another Class called `appDic`
; and also by happenstance inherits from it
myApp := appDic.defineApp("samfisherirl", "Github.ahk")

; some key-value pairs u happen to have laying around that u want to add
MyKeyValuePairs := {abc: 'xyz', oneTwoThree: 123}

myApp.setDic(MyKeyValuePairs)

myApp.printDic()

class appDic { 
    dictionary := {}

    class defineApp extends appDic{
        __New(username, repo) {
                this.username := username, 
                this.repo := repo, 
                this.downloadUrl := "",
                this.version := "",
                this.releaseNotes := "",
                this.appPath := "",
                this.downloadPath := ""

                ; store all these member variables u see above inside ur "dictionary"
                ; (with whatever values they CURRENTLY have assigned to them)
                this.setDic(this)
            }
        
        setDic(AnObjectToCopyKeyValuePairsFrom) {
            ; assuming u want to copy only `Value Properties` (look up in the docs what this means)
            ; a single variable for-loop must be used
            for propName in AnObjectToCopyKeyValuePairsFrom.OwnProps() {

                ; in case TheObjectToCopyKeyValuePairsFrom that got passed in is our own instance
                ; or otherwise a different Object that just so happens to have a property called `dictionary` in it, 
                ; skip it, since we dont want to overwrite our own `dictionary` property (that presumably already
                ; contains some valuable information in it)
                if (propName = 'dictionary')
                    continue

                ; retrieve the property descriptor for this particular property
                PropDesc := AnObjectToCopyKeyValuePairsFrom.GetOwnPropDesc(propName)

                ; if its a `Value Property` (again, the docs)
                if PropDesc.HasProp('Value')
                    this.dictionary.%propName% := PropDesc.Value ; store it
                }

                ; if its a `Dynamic Property` (getter/setter) or a `Method` (call)
                ; dont do anything with it
            }
        printDic() {
            msg := ''

            ; since we've ensured the "dictionary" is only filled with `Value Properties`
            ; at this point, the 2 argument for-loop is safe to use
            for key, value in this.dictionary.OwnProps() {
                msg .= key . ": " . value . "`n"
            }
            msgbox(msg)
        }
    }
}

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

Re: Help w. Nested Class with object values, enumerated into a dictionary

Post by sashaatx » 25 Mar 2023, 18:19

swagfag wrote:
25 Mar 2023, 16:04
but you do have the answers! who else would have them besides u? u have a particular class in mind. u want the class to used in a particular way. its only a matter of defining its interface:
  • step 1, ok, u instantiate this object passing it these parameters
  • step 2, ok, u call this method on the instance u just constructed passing it no parameters
  • step 3, ok, now u call this other method on the instance u just constructed passing it an associative array of key-value pairs as its first and only parameter
  • step 4, ok now u do whatever
  • step 5, yadda yadda...
  • step N, ure done

Ive used my ahk code, my python code and plain english and a flow chart. It baffles me that you feel you need to mind
the problem with all that is ur ahk code, ur python code, ur plain english explanation and ur graphic all contradict each other at every intersection
  • ur ahk code... can be written either way: passing 0 parameters to a .setDict() method and passing 1 parameter to a .setDict(....) method
  • ur python code... wants to pass 0 parameters (see def enumerator(self): and obj.enumerator())
  • ur plain english explanation... doesnt even state where the data is supposed to be coming from
  • ur graphic... has the same ambiguity as ur ahk code, since its based on it
what am i to make of all this? at this point, assuming i was capable of reading minds, im starting to doubt whether reading urs would even help at all or whether it would just leave me even more confused than ever
what I need, plain and simple. how to properly talk to parent child class object values, from either
-in the parents "__new" section, or instantiation, how do i reference in the method and or "__new" section of the child, the parent
-OR in the childs "__new" section, or instantiation, how do i reference in the method and or "__new" section of the parent, the child.
:crazy: :crazy: :crazy: plain and simple, huh. if u had told me joe biden asked this, i would have believed u outright
anyway, heres some commented ahk code based on what I think u probably intended to write:

Code: Select all

#Requires AutoHotkey v2.0.2

; construct an instance of the Class `defineApp`, 
; which happens to reside nested inside another Class called `appDic`
; and also by happenstance inherits from it
myApp := appDic.defineApp("samfisherirl", "Github.ahk")

; some key-value pairs u happen to have laying around that u want to add
MyKeyValuePairs := {abc: 'xyz', oneTwoThree: 123}

myApp.setDic(MyKeyValuePairs)

myApp.printDic()

class appDic { 
    dictionary := {}

    class defineApp extends appDic{
        __New(username, repo) {
                this.username := username, 
                this.repo := repo, 
                this.downloadUrl := "",
                this.version := "",
                this.releaseNotes := "",
                this.appPath := "",
                this.downloadPath := ""

                ; store all these member variables u see above inside ur "dictionary"
                ; (with whatever values they CURRENTLY have assigned to them)
                this.setDic(this)
            }
        
        setDic(AnObjectToCopyKeyValuePairsFrom) {
            ; assuming u want to copy only `Value Properties` (look up in the docs what this means)
            ; a single variable for-loop must be used
            for propName in AnObjectToCopyKeyValuePairsFrom.OwnProps() {

                ; in case TheObjectToCopyKeyValuePairsFrom that got passed in is our own instance
                ; or otherwise a different Object that just so happens to have a property called `dictionary` in it, 
                ; skip it, since we dont want to overwrite our own `dictionary` property (that presumably already
                ; contains some valuable information in it)
                if (propName = 'dictionary')
                    continue

                ; retrieve the property descriptor for this particular property
                PropDesc := AnObjectToCopyKeyValuePairsFrom.GetOwnPropDesc(propName)

                ; if its a `Value Property` (again, the docs)
                if PropDesc.HasProp('Value')
                    this.dictionary.%propName% := PropDesc.Value ; store it
                }

                ; if its a `Dynamic Property` (getter/setter) or a `Method` (call)
                ; dont do anything with it
            }
        printDic() {
            msg := ''

            ; since we've ensured the "dictionary" is only filled with `Value Properties`
            ; at this point, the 2 argument for-loop is safe to use
            for key, value in this.dictionary.OwnProps() {
                msg .= key . ": " . value . "`n"
            }
            msgbox(msg)
        }
    }
}
Ill let your brainless comments on biden whos stuttered since hes a child go since you posted teh code. To be truthful, I was sure you were messing with me so I copied the question already answered in another thread, answered by someone with your level of posts and it was answered clearly. You didnt have a chance. But you provided the code and I appreciate your time and sorry for pulling your leg.
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 “Ask for Help (v2)”