[code sharing] descriptive keyword arguments

asked 2024-11-25 16:30:15 -0600

likethevegetable gravatar image

updated 2024-11-25 16:32:55 -0600

I'm stuck using PSSE 34, and have a strong dislike for passing arguments using intgar and realar. So I made a little helper function.

def pssf(fun, *args, **kwargs):
    """
    pssf = pss function caller
    Call a psspy function with descriptive kw-args
    Eg. for machine_chng_2, we normally use intgar1=1 to changes the machine status, or realar1=100.0 to change the power (pg), like so:
        psspy.machine_chng_2(101, '1', intgar1=1, realar1=100.0)
    This function lets you do the following:
        pssf('machine_chng_2', 101, '1', stat=1, pg=100.0) 
    which I think is more readable.
    """
    fun = getattr(psspy, fun)
    doc = fun.__doc__
    kwargs_new = kwargs.copy()
    for k, v in kwargs.items():
        for t in 'INTGAR', 'REALAR', 'CHARAR':
            matches = re.search('^' + t + '\((\d+)\) ' + k.upper() + ',', s, re.MULTILINE)
            if matches:
                kwargs_new[t.lower() + matches.group(1)] = v
                kwargs_new.pop(k)
                break
    return fun(*args, **kwargs_new)

Do newer versions of PSSE solve this problem? Did I waste my time making this?

edit retag flag offensive close merge delete