0

I need to call a python script from C# Blazor WASM using IronPython 2.7. The reason I need to call python is that this is a legacy application that was originally a PHP server app that would call the python script from the filesystem to produce a pseudo-random string based upon a string seed.

I haven't found any good examples on how to do this. Here is the PY script:

#! /usr/bin/env python
# answerkey.py

""" generate 'random' answer keys """

import sys
from random import random, seed

SHUFFLE = 1000

ANS    = ('A','B','C','D')


ANS_CT = 50

def makeKeys(group, key_ct, ans_ct=ANS_CT, ans=ANS):
    """
    create tuple(key_ct) of tuple(ans_ct) of 'randomized' answers.
    
    group  - group name of answers (actually random 'seed')
    ans    - sequence of possible answers (default ('A','B','C','D'))
    ans_ct - number of answers in key (default 50)
    key_ct - number of different keys to generate
    """
    
    l = []
    while len(l) < ans_ct:
        l.extend(list(ans))

    # difference between 32-bit and 62-bit random seed
    seed(hash(group) & 0xffffffff)
    ct = len(l)

    r =[]    
    for i in range(key_ct):
        i = SHUFFLE
        while i > 0:
            e1 = int(random() * ct)
            e2 = int(random() * ct)
            if e1 != e2:
                l[e1],l[e2]=l[e2],l[e1]
                i -= 1
        # r.append(tuple(l[:ans_ct]))
        r.append(''.join(l[:ans_ct]))
    return r

if __name__ == '__main__':
    x = makeKeys(sys.argv[1], int(sys.argv[2]))
    for i in x:
        print (I)

Suggesions?

Darryl Wagoner WA1GON
  • 967
  • 1
  • 10
  • 31
  • 1
    Should be the same mechanism as any other C# context. See [here](https://stackoverflow.com/questions/7053172/how-can-i-call-ironpython-code-from-a-c-sharp-app). – Robert Harvey Jul 21 '21 at 15:45
  • I looks like that IronPython doesn't work on WASM. Got a platform not supported. – Darryl Wagoner WA1GON Jul 25 '21 at 01:07
  • What you're trying to do is a bit extreme. Wouldn't it be easier to just convert the Python script to C#? IronPython requires the DLR (Dynamic Language Runtime), and I'm not sure that's supported in a Blazor context. – Robert Harvey Jul 25 '21 at 17:52
  • If it was just a script, I will agree and that is the first thing I tried. However, the script relies on the Python pRNG to create a known set of pRND based upon a seed. Even python 3.x doesn't produce the correct output. The output has to match a pre-built template overlay. I have recreated a standard pRNG based upon SHA512 so the next person to rewrite this will have a language-independent method of creating pRandom numbers. This of course doesn't help with the people with templates using the old pRND. – Darryl Wagoner WA1GON Jul 26 '21 at 00:21
  • 1
    That sounds brittle enough where you need to replace it anyway. – Robert Harvey Jul 26 '21 at 00:38

0 Answers0