update a currently displayed variable - python

I am writing some Python code, which will in the end will display something which looks like:
Volt1: 3.1V
Volt2: 2.6V
Volt3: 5.6V
I am constantly monitoring some register values which will give me the current values of Volt1,Volt2,Volt3. As I read in the new values I want to update the field next to the variable name (i.e 3.1V becomes 3.12V), but without just simply printing a new line, but updated the old value.
I am just not sure how to do it, can someone point me in the right direction?
Thanks

This kind of thing is best handled using a library such as curses.

Related

Enterprise Architect Python Scripting - Add an element to a Diagram

I am trying to write a python script so that I can add an element to a diagram in Sparx Enterprise Architect.
TestDiagEl=TestDiag.DiagramObjects.AddNew("l=10;r=110;t=-20;b=-80","")
TestDiagEl.ElementID=TestEl.ElementID
TestDiagEl.Update
TestDiag.Update
eaRep.ReloadDiagram(TestDiag.DiagramID)
It doesn't seem to work, what am I doing wrong?
EDIT
It seems that #geert was right, additionally i didnt add () after Update.
When writing code you should know what each line of code does, and why you are writing it.
In this case
TestDiagEl=TestDiag.DiagramObjects.AddNew("l=10;r=110;t=-20;b=-80","")
Creates a new diagramObject in memory
TestDiagEl.ElementID=TestEl.ElementID
Sets the elementID of the diagramObject to the elementID of my element
TestDiagEl.Update
Save the diagramObject to the database
TestDiag.Update
Save the current diagram in memory to the database
eaRep.ReloadDiagram(TestDiag.DiagramID)
Get the diagramDetails from the database and show them in the GUI
One problem is the TestDiag.Update. Since your diagram in memory doesn't know about the new DiagramObject yet, you are effectively undoing the addition of the new DiagramObject. Remove that line, and all should be OK.
Another problem is the parameter you pass to the DiagramObjects.AddNew method. The top and bottom values should be positive as you can see in the provided documentation So:
TestDiagEl=TestDiag.DiagramObjects.AddNew("l=10;r=110;t=20;b=80","")

How to keep the same python console on pycharm

I love pycharm as it is very useful for my data analysis.
However there is something that I still can't figure out a big problem.
When I start to save a lot of variables, it is very useful. But sometimes, especially when I want to run a piece of row using seaborn and create a new graph, sometimes, all my variables disseapear and I have to reload them again from scratch.
I'd like to know, do you know a way to keep the data stored and run only a piece of my code without getting this problem ?
Thank you
I am not really sure what issue you are having, but from the sounds of it you just need to store a bunch of data and only use some of it.
If that's the case, I would save files which each set of data and then import the one you want to use at that time.
If you stick to the same data names then your program can just do something like:
import data_435 as data
# now your code can always access data.whatever_var
# even though you have 435 different data sets

Autocomplete / get suggestions only for the function parameters, when inside a function

I'm using Python and I'm on the Jupyter Notebook, although I also use PyCharm so advice for this one would also be appreciated.
Whenever I'm using a function, and I'd like to pass some arguments, I'd like to have suggestions only for the function parameters. Instead, what I get when I press tab, is a long list of irrelevant stuff. Basically, I'd like to press tab, and get a list like this:
data=
kind=
stat_func=
color=
...(etc.)
without having to look at the documentation and finding the right parameter name every time.
Is there a way to get this? If not, what workaround do you suggest?
I think Parameter Info is what you're looking for: https://www.jetbrains.com/help/pycharm/viewing-reference-information.html#view-parameter-info

Is there a way to let the user supply Python code that will run in my program?

I'm quite new to this so apologies in advance if this is a silly question. I'm trying to build a simple module which takes a Pandas dataframe and a set of instructions (in some text format) as an input, and converts this dataframe to a nested JSON representation of the data.
My plan was to essentially leave a section of the code blank and let the user supply the code (instructions) for how to do this conversion. I'm sure this is not a good way to solve the problem, and I'd happily take pointer on how it should be done as well, but is there a way to let the user insert a section of code into the code itself, and then execute the program?
Take a look at the exec function.
If you need some control over the validation of the instructions, maybe it's better to do as #will says. Take a look at this similar question

How to make an action if a line was printed using print?

I’m making a little assistance management on python that prints a message and then writes the assistance data on a file after the message was printed but I can’t make an ‘if print: statement’ code.
I haven’t seen any solution yet, and I’m new to this so I don’t even know how to search it up.
I want to open a file after the message is printed
A few options (given the sketchy description):
Repeat the logic that decided to print to decide whether or not to write to the file (if possible)
Have the code that prints the message change a variable (say, didPrint) from False to True, and check that variable to decide whether to not to write to the file.
If you need to know what was printed, save that to a variable you can use when writing to the file.

Categories

Resources