Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Pyhton code embedding in STATA ado program

    Dear All,

    I am trying to generate SHA256 equivalent of a string using python code using the following code:

    Code:
    local string = "AMIT"
    version 18.0
    python:
    import hashlib
    from sfi import Macro
    
    # Get the string from Stata
    fixed_string = r""" `string' """
    fixed_string = fixed_string.strip('"\' ') # Remove any extra quotes
    
    def generate_sha256(input_string):
        input_bytes = input_string.encode('utf-8')  # Convert input to bytes
        hash_obj = hashlib.sha256()  # Create SHA-256 hash object
        hash_obj.update(input_bytes)  # Compute hash
        return hash_obj.hexdigest()  # Return hexadecimal hash
    
    # Generate SHA-256 hash
    sha256_hash = generate_sha256(fixed_string)
    
    # Store result in Stata macro
    Macro.setLocal("sha256_hash", sha256_hash)
    
    print(f"Input String: {fixed_string}")
    print(f"SHA256: {sha256_hash}")
    end
    This code works perfectly fine inside a dofile. But when I try to use this file inside the following ado program, and pass the value of string from stata ado block to python block, this gives an error:

    Code:
    program define genSHA2
        version 18.0
        syntax, STRing(str)
        di "`string'"
    end
    
    
    version 18.0
    python:
    import hashlib
    from sfi import Macro
    
    # Get the string from Stata
    fixed_string = r""" `string' """
    fixed_string = fixed_string.strip('"\' ')  # Remove any extra quotes
    
    def generate_sha256(input_string):
        input_bytes = input_string.encode('utf-8')  # Convert input to bytes
        hash_obj = hashlib.sha256()  # Create SHA-256 hash object
        hash_obj.update(input_bytes)  # Compute hash
        return hash_obj.hexdigest()  # Return hexadecimal hash
    
    # Generate SHA-256 hash
    sha256_hash = generate_sha256(fixed_string)
    
    # Store result in Stata macro
    Macro.setLocal("sha256_hash", sha256_hash)
    
    print(f"Input String: {fixed_string}")
    print(f"SHA256: {sha256_hash}")
    end
    
    di "`sha256_hash'"
    Can anyone please help me understand what is the error in this ado program and how to fix that?

    Thank you.

    Best regards,
    Amit
    Last edited by Amit Narnoli; 01 Apr 2025, 04:22.

  • #2
    There are some misunderstandings regarding the scope of local macros, how ado-files generally work, and how Python code works within ado-files. I won't go through your code in detail. The key idea is that your Python code should be wrapped in one or more functions and your program must explicitly call the Python function(s). Here is a basic layout as a proof of concept:
    Code:
    program define py_sha256
        
        version 18
        
        python : py_sha256("0")
        
        display `"`0'"'
        
    end
    
    
    version 18
    
    python :
    
    import hashlib
    from sfi import Macro
    
    def py_sha256(lmname):
        input_string = Macro.getLocal(lmname).encode("utf-8")
        hash_obj = hashlib.sha256()
        hash_obj.update(input_string)
        Macro.setLocal(lmname,hash_obj.hexdigest())
    
    end
    Save this as an py_sha256.ado where Stata can find it, then call
    Code:
    . py_sha256 Amit
    105a112c81ce60e646c007e98655b7caa27d9e6f36a7ba9584e406dd400e15ff
    Last edited by daniel klein; 03 Apr 2025, 02:58.

    Comment

    Working...
    X