SPSS Statistics

SPSS Statistics

Your hub for statistical analysis, data management, and data documentation. Connect, learn, and share with your peers! 

 View Only
  • 1.  Custom command development: "TypeError: 'dict_keys' object is not subscriptable'

    Posted Wed May 18, 2022 10:39 AM
    I'm writing my own extension command in Python 3. After successful development in external mode only, I included Run function as it's needed according to this documentation. Later, I created .xml syntax description and installed it using the solution below:
    extension action = add
    /specification command =
    "C:\<SPSS installation directory>\extensions\MY_OWN_COMMAND.xml".

    The command was installed without any issues. Moreover, when I typed this syntax in SPSS – the command was coloured. Sadly, during execution, I constantly get very weird error:

    Traceback (most recent call last): 
      File "<string>", line 13, in <module> 
      File "C:\\<SPSS installation directory>\\extensions\MY_OWN_COMMAND.py", line 23, in Run 
     
    TypeError: 'dict_keys' object is not subscriptable

    Line 23 in my code is empty. No matter what I place in that line (code, comment, nothing) I always get this error in this exact line. These are first 24 lines of the code (I cannot post everything due to my company rights):

    import spss
    import spssaux
    import SpssClient
    from extension import Template, Syntax, processcmd
    from itertools import chain
    
    from typing import Tuple
    
    def Run(
        args
    ) -> None:
        
        oobj = Syntax([
            Template("...", subc="...", ktype="str", var="...", vallist=["...", "..."]),
            Template("...", subc="...", ktype="int", var="..."),
            Template("...", subc="...", ktype="int", var="..."),
            Template("...", subc="...", ktype="int", var="...", islist=True),
            Template("...", subc="...", ktype="existingvarlist", var="...", islist=True),
            Template("...", subc="...", ktype="str", var="...", vallist=["...", "..."]),
            Template("...", subc="...", ktype="existingvarlist", var="...", islist=False)])
        
        args = args[args.keys()[0]]
        
        processcmd(oobj, args, my_own_command)

    This is exactly the same code (except Templates) as in the documentation above. Why is this happening? Any advice will be appreciated



    ------------------------------
    Konrad Gałuszko
    ------------------------------


    #SPSSStatistics


  • 2.  RE: Custom command development: "TypeError: 'dict_keys' object is not subscriptable'

    Posted Wed May 18, 2022 01:36 PM
    That document has apparently not been updated for Python 3.  In Python 3, keys() returns a memoryView object for efficiency, and so operations don't work with that.
    Replace
     args = args[args.keys()[0]]
    with 
    args = args[list(args.keys())[0]]
    and you should be fine.

    Which document did you find that obsolete information in and where did you find it?  I'll try to get it updated.

    --





  • 3.  RE: Custom command development: "TypeError: 'dict_keys' object is not subscriptable'

    Posted Wed May 18, 2022 05:35 PM
    Hi Jon. The document is here: Writing IBM SPSS Statistics Extension Commands.

    Concerning code, I corrected this line. Sadly, this generated another error:
    ValueError: Function has keyword-only parameters or annotations, use inspect.signature() API which can support them​


    ------------------------------
    Konrad Gałuszko
    ------------------------------



  • 4.  RE: Custom command development: "TypeError: 'dict_keys' object is not subscriptable'

    Posted Wed May 18, 2022 05:51 PM
    I need a little more context for this.  Does it refer to the processcmd call?

    Could you send me the code in the import spssimport block by email so that the formatting would be correct (jkpeck@gmail.com)?

    It might be that the my_own_commands function has some parameters without defaults, and you are not supplying those.  Which line generated that error message?

    Do you know about the environment variable that enables tracebacks?  If you set this environment variable
    SPSS_EXTENSIONS_RAISE=true
    then error messages will also have a traceback.  Otherwise, the extensions module suppresses them, because to end users, they would be meaningless and confusing.  But for developers, you would usually want this.

    --