-1

I would like to summarize the input parameters for a python calculation using text strings. I can summarize the input parameters using print() but I don't know how to also save these as text strings.

Sample Code

First, define the scan of input parameters:

import numpy as np

gapsize = np.arange(0.5,0.8,0.1)
ringwidth = np.arange(1.25,5,0.75)
outputangle = [28.43]

Now define a function that prints a parameter-specific summary of these values

def printSCANvalues(parameter):
    ## Do cases for text descriptions
    if parameter is ringwidth:
        paramTEXT = "RW"
    elif parameter is gapsize:
        paramTEXT = "Gap"
    elif parameter is outputangle:
        paramTEXT = "Facet Angle"

    if parameter is ringwidth or parameter is gapsize:
        unit = 'um'
    elif parameter is outputangle:
        unit = 'deg.'


    ## only calculate the gradient for arrays with more than one value
    if len(parameter) > 1:
        step = round(1000*np.gradient(parameter)[0])/1000
    elif len(parameter) == 1:
        step = 0

    stopVAL = round(1000*parameter[-1])/1000


    print("")
    print(paramTEXT, "Scan: qty =", len(parameter),", start = ", parameter[0],
          unit, ", stop = ", stopVAL, unit, ", step = ", step, unit)
    print(paramTEXT, "values: ", parameter, unit)

Now use function to generate the text that summarizes the input parameter scan:

print("")
print("====== Input Parameter Scan Values ======")
print("")

printSCANvalues(ringwidth)
printSCANvalues(gapsize)
printSCANvalues(outputangle)

Screenshot of output:

screenshot of output

The problem

I would also like to save this output as a text string so that it can be written to a text file. Is there a way to save the associated string for each parameter type before print() is executed so that I can use it later?

1
  • The documentation for the print() function indicates it accepts an optional file keyword argument — which implies that anything you can print can be sent to an (open) file. You can also do it using the contextlib.redirect_stdout() function.
    – martineau
    Commented Jan 4, 2022 at 1:19

2 Answers 2

0

As a very general rule, in a situation like this, you shouldn't have the function do the printing. Have the function return a string, and let the caller decide what to DO with the string. Like this. You can see how to take the results and copy them to a file later.

def SCANvalues(parameter):
    ## Do cases for text descriptions
    if parameter is ringwidth:
        paramTEXT = "RW"
    elif parameter is gapsize:
        paramTEXT = "Gap"
    elif parameter is outputangle:
        paramTEXT = "Facet Angle"

    if parameter is ringwidth or parameter is gapsize:
        unit = 'um'
    elif parameter is outputangle:
        unit = 'deg.'

    ## only calculate the gradient for arrays with more than one value
    step = 0
    if len(parameter) > 1:
        step = round(1000*np.gradient(parameter)[0])/1000

    stopVAL = round(1000*parameter[-1])/1000

    s =  f"{paramTEXT} Scan: qty = {len(parameter)}, start = {parameter[0]}, stop = {stopVal} {unit}, step = {step} {unit}\n"
    s += f"{paramTEXT} Values: {parameter} {unit}"
    return s

print("")
print("====== Input Parameter Scan Values ======")
print("")

rw = SCANvalues(ringwidth)
gs = SCANvalues(gapsize)
oa = SCANvalues(outputangle)
print(rw)
print(gs)
print(oa)
4
  • Thanks for the reply! This doesn't appear to generate a string variable as far as I can tell. 's' never shows up in the variable explorer. I may be missing something obvious since I'm new to python. Also, maybe there's a way to append "RW" etc to "s" for each variable type?
    – Drotar
    Commented Jan 4, 2022 at 18:30
  • ??? The last three lines of the function generate a string s and return that string. The mainline code just prints the string, but of course you can store it in your own variable and do what you like. I don't know what you mean by "RW". Commented Jan 4, 2022 at 18:38
  • Hi Tim, I guess the question was "how do I store it as a variable"? I realize it's generating a string I just don't know how to use it afterward. "RW" is my designation for one of the parameters. I was just trying to distinguish the different copies of 's' that would be generated.
    – Drotar
    Commented Jan 4, 2022 at 18:53
  • You're not thinking about what you're asking. If you call "abc".upper(), how do you store its result in a variable? It's the same thing here. They're just functions. You store it in a variable by assigning its result to a variable. Commented Jan 4, 2022 at 18:59
0

I think I figured it out using the tip from @martineau and the modification from @Tim Roberts.

def SCANvalues(parameter):
    ## Do cases for text descriptions
    if parameter is ringwidth:
        paramTEXT = "RW"
    elif parameter is gapsize:
        paramTEXT = "Gap"
    elif parameter is outputangle:
        paramTEXT = "Facet Angle"

    if parameter is ringwidth or parameter is gapsize:
        unit = 'um'
    elif parameter is outputangle:
        unit = 'deg.'

    ## only calculate the gradient for arrays with more than one value
    step = 0
    if len(parameter) > 1:
        step = round(1000*np.gradient(parameter)[0])/1000

    stopVAL = round(1000*parameter[-1])/1000

    
    s =  f"{paramTEXT} Scan: qty = {len(parameter)}, start = {parameter[0]}, stop = {stopVAL} {unit}, step = {step} {unit}\n"
    s += f"{paramTEXT} Values: {parameter} {unit}\n"
    return s

Code for printing to a file

I didn't see if there was an option to print to a file as well as the console so I just included two instances

log = open('log.txt', 'w')
geos = [ringwidth, gapsize, outputangle]

def geoPRINT(geoPARAMS):
    print("\n ====== Input Parameter Scan Values ======\n")
    print("\n ====== Input Parameter Scan Values ======\n",file = log)
    for i in range(len(geoPARAMS)):
        print(SCANvalues(geoPARAMS[i]))
        print(SCANvalues(geoPARAMS[i]),file = log)

    
geoPRINT(geos)
log.close()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.