Skip to content

Modeler.set_variable doesn't work in python shell (I have the solution) #61

@AdrienGuimbal

Description

@AdrienGuimbal

In HFSSdrawpy.core.modeler.Modeler.set_variable, the "hack" to auto-name variables assumes the python code lives inside a file, so when the code is tested inside a shell, an error occcures.
The error message is

File "........\modeler.py", line 79, in set_variable
code_line = open(filename).readlines()[f.f_lineno - 1]

Which isn't very usefull

I suggest the following changes for the function Modeler.set_variable

    def set_variable(self, value, name=None):
        """
        name (str): name of the variable in HFSS e.g. 'chip_length', only optionnal if used inside a .py script
        value (str, VarStr, float): value of the variable
                                    if str will try to analyse the unit
        """

        if name is None:
            # this auto-parsing is clearly a hack and not robust
            # but I find it convenient
            f = currentframe().f_back  # .f_back
            filename = getfile(f)
            if filename != '<stdin>':
                code_line = open(filename).readlines()[f.f_lineno - 1]
                name = code_line.split("=")[0].strip()
            else:
                raise ValueError("Please provide a name for the variable, automatic naming only works in .py scripts")

        if self.mode == "hfss":
            self.design.set_variable(name, value)  # for HFSS
        symbol = sympy.symbols(name)
        store_variable(symbol, value)
        return symbol

Adding just an if statement and corecting the documentation

I didn't made tests for .ipynb, I'll leave it to whoever has the time to do it (I dont have a setup for this)

Thanks to whoever take the time to correct this minor issue

Acknowledgement : this is more a Pull Request than an open issue, but I don't have the permission to create a branch and I'm not very fluent with Giuhub (for yet ?)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions