I have an script that collects initial information using npyscreen and then display multiple messages as it progresses. However, these messages are displayed in an unorganized way (see below). Is there a way to populate those messages in the correct way (without tabs)?
Something
Something
Something
This is the code
import npyscreen, time
class Test(npyscreen.NPSApp):
def main(self):
F = npyscreen.Form(name = "Test")
color = F.add(npyscreen.TitleText, name = "Color:")
F.edit()
if(color.value == "Something"):
self.call_function_to_display_messages(color.value)
def call_function_to_display_messages(self, color):
print(color)
print(color)
print(color)
time.sleep(10)
if __name__ == "__main__":
App = Test()
App.run()
The solution is to add the carriage return at the beginning the of print statement.
print("\r"+color)
Related
I have a callback function in a file "color.py" that constantly recieves incoming data but updates a global variable every 3 seconds:
last_color = ""
last_calltime = datetime.strptime("00:00:00","%H:%M:%S")
def callback(data):
global last_calltime, last_color
cube = data.data
t = time.localtime()
tf = time.strftime("%H:%M:%S",t)
current_time = datetime.strptime(tf, "%H:%M:%S")
delta_time = current_time - last_calltime
if abs(delta_time.total_seconds()) >= 3:
if "Red" in cube:
last_color = "Red"
elif "Green" in cube:
last_color = "Green"
else:
last_color = "Blue"
t = time.localtime()
tf = time.strftime("%H:%M:%S",t)
last_calltime = datetime.strptime(tf, "%H:%M:%S")
return last_color
if __name__ == '__main__':
try:
rospy.init_node('color')
rospy.Subscriber ('/topic',String, callback)
rospy.spin()
In another file, I need to reference this "last_color" variable that updates. So far the function I have in this file:
from color import last_color
a = 0
def observe_color(a)
if a == 0:
return last_color
else:
return "None"
I need this to return the updated variable from the "color.py" file but it only returns the initialized value for last_color which is "". How can I do this?
When you import something in Python, you're effectively asking it to execute everything in that module in the same runtime as your current program.
Suppose I have some module color.py with variable last_color='green'
And in a different program observer.py I have from color import last_color
The last_color that exists in color.py and the last_color that exists in observer.py are different places in computer memory - they do not reference the same string value.
A quick (and maybe dirty) solution to this is just to re-import the module every time you want to observe one of its member variables.
I don't think it's gonna work that way. How about save the value of last_color to a file and monitor it ? Because to me you run 2 separated programs, 1 with the ROS and another just to monitor last_color
while True:
with open(filename, 'r') as f:
color = f.readline()
time.sleep(timesteps)
When write to file in the main, you could also append the read value and also the time, to get timeline data.
Since this is all done in ROS it would be easiest to simply have the separate file initialize as a ros node with a subscriber. If for some reason that's not the case you should make color.py a class and have the other file initialize an object. Take the following simplified example:
color.py
class colorPy:
def __init__(self):
self.last_color = None
rospy.init_node('some_node')
tmp = rospy.Subscriber('/some_topic', self.callback)
def callback(self, msg):
self.last_color = msg.data
other file
from color import colorPy
from time import sleep
local_var = colorPy()
sleep(5)
print(local_var.last_color)
I've been searching for an answer for two hours now. What I am trying to accomplish in kivy is this:
import time
clear = "\033[2J\033[1;1f"
print("""
################################
##this should display an image##
################################""")
time.sleep(1)
print(clear)
print("""
#####################################
##this should display another image##
#####################################""")
It is the beginning of a program I already got ready, but not as a real Desktop Application as if it would be with kivy.
The program begins with showing an image, pausing for a second, then not showing an image before initiating the main menu with buttons and so on (not important).
This is what I have so far (not working) :
class Intro(FloatLayout):
connection = Image(source='connection.png')
noconnection = Image(source='noconnection.png')
checkloop = True
def check_connection(self, dt):
url='http://www.google.com/'
timeout=5
connected.stop()
noconnection.stop()
try:
server = smtplib.SMTP('smtp.gmail.com', 587)
_ = requests.get(url, timeout=timeout)
connected.play()
self.add_widget(self.connection)
Clock.schedule_once(self.remove_widget(self.connection), 1)
checkloop = False
except requests.ConnectionError:
noconnection.play()
self.add_widget(self.noconnection)
self.remove_widget(self.noconnection)
txtinput = TextInput(text='ConnectionError. Enter to try again...')
except smtplib.SMTPConnectError:
noconnection.play()
img = Image(source='noconnection.png')
self.add_widget(self.noconnection)
self.remove_widget(self.noconnection)
txtinput = TextInput(text='SMTPConnectError. Enter to try again...')
class I41(App):
def build(self):
self.icon = 'riseicon.png'
self.title = '41'
intro = Intro()
Clock.schedule_once(intro.check_connection)
print("DONE")
What am I doing wrong still?
I hope you guys can help me! First questions asked on stackoverflow!
In your check_connection() method you have many return statements. Note that the documentation. says:
return leaves the current function
So anything after a return is encountered will not be executed, and any other code in the method will be stopped (like a while loop).
A fix is to call the code that you want executed after return using a call to Clock.schedle_once() .
Here is a simple example of how to do that:
from kivy.app import App
from kivy.clock import Clock
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.image import Image
class MyFloatLayout(FloatLayout):
img = Image(source='connection.png')
checkloop = True
def showImage(self, dt):
self.add_widget(self.img)
# other code here, like connecting to Google
if self.checkloop:
# continue loop
Clock.schedule_once(self.deleteImage, 1)
def deleteImage(self, dt):
self.remove_widget(self.img)
if self.checkloop:
# continue loop
Clock.schedule_once(self.showImage, 1)
class I41(App):
def build(self):
self.icon = 'riseicon.png'
self.title = '41'
root = MyFloatLayout()
# start image loop
Clock.schedule_once(root.showImage)
return root
I41().run()
The loop is stopped by setting checkloop to False in MyFloatLayout.
I'm building a large, complicated program, one half of which involves a GUI, which I'm building using Tkinter.
Previous iterations of this GUI seemed to work as intended. However, in the latest version, when I try to run a demonstration (see the demo() function in the code below), my whole computer freezes, and my only option is to carry out a hard reset.
Does anyone have any ideas as to why this might be happening? Some points which might be useful:
If I run the code below with the line self.gui.mainloop() commented out, the desired window appears on the screen for long enough for the toaster message to be displayed, and then closes without any freezing.
The ArrivalsManagerDaughter and DeparturesManagerDaughter objects transmit data wirelessly to another device, but they shouldn't be doing anything, other than being initialised, in the code which is causing the freezing. I don't believe that these are the cause of the problem, although I could well be wrong.
Here's the whole Python file which I'm trying to run. I'm happy to post more code if requested.
"""
This code holds a class which manages transitions between the
"recommendations" and "custom placement" windows, and also oversees their
interactions with Erebus.
"""
# GUI imports.
from tkinter import *
# Non-standard imports.
import ptoaster
# Custom imports.
from erebus.arrivals_manager_daughter import ArrivalsManagerDaughter
from erebus.departures_manager_daughter import DeparturesManagerDaughter
# Local imports.
from charon.custom_placement_window import Custom_Placement_Window
from charon.recommendations_window import Recommendations_Window
# Local constants.
REFRESH_INTERVAL = 1000
# ^^^ in miliseconds ^^^
##############
# MAIN CLASS #
##############
class Comptroller:
""" The class in question. """
def __init__(self, welcome=False, delete_existing_ledger=False,
internal=False, diagnostics=False, path_to_icon=None):
self.close_requested = False
self.path_to_icon = path_to_icon
self.recommendations = dict()
self.arrivals_manager = ArrivalsManagerDaughter(self,
diagnostics=diagnostics)
self.departures_manager = DeparturesManagerDaughter(
delete_existing=delete_existing_ledger, internal=internal,
diagnostics=diagnostics)
self.gui = Tk()
self.top = Frame(self.gui)
self.window = Recommendations_Window(self)
self.is_on_recommendations_window = True
self.arrange()
if welcome:
print_welcome()
def add_recommendation(self, ticket, epc, column, row):
""" Add a recommendation to the dictionary. """
recommendation = dict()
recommendation["ticket"] = ticket
recommendation["epc"] = epc
recommendation["column"] = column
recommendation["row"] = row
self.recommendations[ticket] = recommendation
def remove_recommendation(self, ticket):
""" Delete a recommendation from the dictionary. """
del self.recommendations[ticket]
def get_top(self):
""" Return the top-level GUI object. """
return self.top
def arrange(self):
""" Arrange the widgets. """
self.window.get_top().pack()
self.top.pack()
def switch_to_custom_placement(self, ticket, epc):
""" Switch window from "Recommendations" to "Custom Placement". """
columns = self.arrivals_manager.columns
rows = self.arrivals_manager.rows
self.window.get_top().pack_forget()
self.window = Custom_Placement_Window(self, ticket, epc, columns,
rows)
self.window.get_top().pack()
self.is_on_recommendations_window = False
def switch_to_recommendations(self):
""" Switch window from "Custom Placement" to "Recommendations". """
self.window.get_top().pack_forget()
self.window = Recommendations_Window(self)
self.window.get_top().pack()
self.is_on_recommendations_window = True
def refresh(self):
""" Refresh the "recommendations" window, as necessary. """
if (self.is_on_recommendations_window and
self.arrivals_manager.clear_quanta()):
self.window.refresh_rec_table()
self.departures_manager.clear_backlog()
if self.close_requested:
self.kill_me()
else:
self.gui.after(REFRESH_INTERVAL, self.refresh)
def simulate_recommendation(self, ticket, epc, column, row):
""" Simulate receiving a transmission from the Pi. """
self.add_recommendation(ticket, epc, column, row)
self.window.refresh_rec_table()
def request_close(self):
self.close_requested = True
def run_me(self):
""" Run the "mainloop" method on the GUI object. """
self.gui.after(REFRESH_INTERVAL, self.refresh)
self.gui.title("Charon")
if self.path_to_icon:
self.gui.iconphoto(True, PhotoImage(file=self.path_to_icon))
self.gui.protocol("WM_DELETE_WINDOW", self.request_close)
self.gui.mainloop()
def kill_me(self):
""" Kill the mainloop process, and shut the window. """
self.gui.destroy()
####################
# HELPER FUNCTIONS #
####################
def print_welcome():
""" Print a welcome "toaster" message. """
message = ("Notifications about boxes leaving the coldstore will be "+
"posted here.")
ptoaster.notify("Welcome to Charon", message,
display_duration_in_ms=REFRESH_INTERVAL)
def print_exit(epc):
""" Print an exit "toaster" message. """
message = "Box with EPC "+epc+" has left the coldstore."
ptoaster.notify("Exit", message)
###########
# TESTING #
###########
def demo():
""" Run a demonstration. """
comptroller = Comptroller(welcome=True, delete_existing_ledger=True,
internal=True, diagnostics=True)
comptroller.simulate_recommendation(1, "rumpelstiltskin", 0, 0)
comptroller.simulate_recommendation(2, "beetlejuice", 0, 0)
comptroller.run_me()
###################
# RUN AND WRAP UP #
###################
def run():
demo()
if __name__ == "__main__":
run()
FOUND THE PROBLEM: the culprit was actually calling a ptoaster function from within a Tkinter GUI. Which leads me on to my next question: Is it possible to combine ptoaster and Tkinter in an elegant fashion, and, if so, how?
The problem ocurred when calling print_welcome(), which in turn calls one of the ptoaster functions. It seems that Tkinter and ptoaster do not play together nicely. Removing the reference to print_welcome() from the Comptroller class put a stop to any freezing.
(On a side note: I'd be very grateful to anyone who could suggest an elegant method of combing ptoaster with Tkinter.)
Try changing
def demo():
""" Run a demonstration. """
comptroller = Comptroller(welcome=True, delete_existing_ledger=True,
internal=True, diagnostics=True)
comptroller.simulate_recommendation(1, "rumpelstiltskin", 0, 0)
comptroller.simulate_recommendation(2, "beetlejuice", 0, 0)
comptroller.run_me()
###################
# RUN AND WRAP UP #
###################
def run():
demo()
if __name__ == "__main__":
run()
To simply
if __name__ == "__main__":
""" Run a demonstration. """
comptroller = Comptroller(welcome=True, delete_existing_ledger=True,
internal=True, diagnostics=True)
comptroller.simulate_recommendation(1, "rumpelstiltskin", 0, 0)
comptroller.simulate_recommendation(2, "beetlejuice", 0, 0)
comptroller.run_me()
As to why this might happening, since you're creating an object by instantiating Comptroller inside a regular function demo, the object is not being "retained" after the demo exits.
EDIT
If you would like to still maintain demo and run you could create a class Demo and store the instance globally.
Or maybe a simple global variable inside demo to retain a "reference" to the instance Comptroller.
I'm developing one test bench which runs multiple tests via python gui and prints the output as below.
A Passed
B Passed
C Passed
D Passed
E Passed
Button from gui should be changed to 'Passed' only when A,B,C,D,E all are Passed. If any of these tests fails, it should say failed. What is the way to access this output from gui which is printed on screen.
My code for tests is:
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys, os, time
from PyQt4 import QtGui, QtCore
from progress.bar import Bar
import datetime
import thread
class MyTestBench(QDialog, QtGui.QWidget):
def __init__(self):
super(QDialog, self).__init__()
self.setWindowTitle("Implementation")
self.progressbar = QtGui.QProgressBar()
self.progressbar.setMinimum(0)
self.progressbar.setMaximum(100)
self.run_test_button = QtGui.QPushButton('Run Your Tests')
self.run_test_button.clicked.connect(self.run_test_event)
def run_test_event(self):
thread.start_new_thread(self.run_the_test, ("Thread-1", 0.5))
thread.start_new_thread(self.run_the_progress, ("Thread-2", 0.5))
def run_the_test(self, tname, delay):
os.system("python nxptest.py my_testlist.txt")
self.progressbar.setValue(100)
if self.progressbar.value() == self.progressbar.maximum():
time.sleep(3)
self.run_test_button.setText('Run Your Tests')
def run_the_progress(self, tname, delay):
count = 0
while count < 5:
self.run_test_button.setText('Running.')
time.sleep(0.5)
self.run_test_button.setText('Running..')
time.sleep(0.5)
self.run_test_button.setText('Running...')
value = self.progressbar.value() + 10
self.progressbar.setValue(value)
time.sleep(0.5)
if self.progressbar.value() == self.progressbar.maximum():
self.progressbar.reset()
count = count + 1
app = QApplication(sys.argv)
dialog = MyTestBench()
dialog.setGeometry(100, 100, 200, 50)
dialog.show()
app.exec_()
The main challenge I'm facing here is I'm new to gui programming and I don't know how to access the output that is printed on screen.
If you're trying to get the text output of a program, you can't run that program using os.system. As the docs for that function say:
The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function. See the Replacing Older Functions with the subprocess Module section in the subprocess documentation for some helpful recipes.
If you follow those links, they'll show how to do what you want. But basically, it's something like this:
output = subprocess.check_output(["python", "nxptest.py", "my_testlist.txt"])
If you're using 2.6 or earlier, you won't have check_output; you can read the docs to see how to build it yourself on top of, e.g., communicate, or you can just install the subprocess32 backport from PyPI and use that.
From a comment:
This works but my only concern is there are lot of results for the tests which are printed before it actually prints A Passed B Passed etc.. Im looking for a way to get just this part of string and not the whole output.
That isn't possible. How could your program have any idea which part of the output is "this part of the string" and which part is "a lot of results … which are printed before"?
If you can edit the programs being tested in some way—e.g., make them print their "real" output to stdout, but their "extra" output to stderr, or provide a command-line argument that makes them skip all the extra stuff—that's great. But assuming you can't, there is no alternative but to filter the results.
But this doesn't look very hard. If each line of "real" output is either "X Passed" or "X Failed" and nothing else starts with "X " (where X is any uppercase ASCII letter), that's just:
test_results = {}
for line in output.splitlines():
if line[0] in string.ascii_uppercase and line[1] == ' ':
test_results[line[0]] = line[2:]
Now, at the end, you've got:
{'A': 'Passed', 'B': 'Passed', 'C': 'Passed', 'D': 'Passed', 'E': 'Passed'}
If you want to verify that all of A-E were covered and they all passed:
passed = (set(test_results) == set('ABCDE') and
all(value == 'Passed' for value in test_results.values()))
Of course you could build something nicer that shows which ones were skipped or didn't pass or whatever. But honestly, if you want something more powerful, you should probably be using an existing unit testing framework instead of building one from scratch anyway.
You can mask the printing output through a queue like so:
class FileQ(Queue.Queue):
def __init__(self):
Queue.Queue.__init__(self)
def write(self,data):
self.put(data)
class MyTestBench(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.incoming = FileQ()
self.setWindowTitle("Implementation")
sys.stdout = self.incoming
self.time = QtCore.QTimer()
self.time.timeout.connect(self.refresh)
self.time.start(10)
self.t = QtGui.QTextEdit(self)
self.t.setObjectName('TextOut')
self.box = QtGui.QHBoxLayout(self)
self.setLayout(self.box)
self.box.addWidget(self.t)
self.run_test_event()
self.progressbar = QtGui.QProgressBar(self)
self.progressbar.setObjectName('BarOut')
self.box.addWidget(self.progressbar)
def run_test_event(self):
thread.start_new_thread(self.run_the_test, ("Thread-1", 0.5))
def run_the_test(self,*args):
for i in range(10):
print i
time.sleep(1)
def refresh(self):
try:
data = self.incoming.get_nowait()
except:
return
if data:
self.t.insertPlainText(str(data))
self.progressbar.setValue(int(data)*10)
I'm current writing a short bit of code that will compare an etag for a web server page in a saved document to the etag on the server. If they are different, the code will indicate this. My code is below:-
import httplib
def currentTag():
f = open('C:/Users/ME/Desktop/document.txt')
e = f.readline()
newTag(e)
def newTag(old_etag):
c = httplib.HTTPConnection('standards.ieee.org')
c.request('HEAD', '/develop/regauth/oui/oui.txt')
r = c.getresponse()
current_etag = r.getheader('etag').replace('"', '')
compareTag(old_etag, current_etag)
def compareTag(old_etag, current_etag):
if old_etag == current_etag:
print "the same"
else:
print "different"
if __name__ == '__main__':
currentTag()
Now, reviewing my code, there is actually no reason to pass 'etag' from the currentTag() method to the newTag() method given that the pre-existing etag is not processed in newTag(). Nonetheless, if I don't do this, how can I pass two different values to compareTag(). So for example, when defining compareTag(), how can I pass 'etag' from the currentTag() method and 'current_etag' from the newTag() method?
you shouldn't chain your function calls like that, have a main block of code that calls the functions serially, like so:
if __name__ == '__main__':
currtag = currentTag()
newtag = newTag()
compareTag(currtag,newtag)
adjust your functions to return the relevant data
the basic idea of a function is that it returns data, you usually use functions to do some processing and return a value and not for control flow.
change your main to:
if __name__ == '__main__':
compareTag(currentTag(), newTag())
and then have currentTag() return e and newTag() return current_etag
def checkTags():
c = httplib.HTTPConnection('standards.ieee.org')
c.request('HEAD', '/develop/regauth/oui/oui.txt')
r = c.getresponse()
with open('C:/Users/ME/Desktop/document.txt', 'r') as f:
if f.readline() == r.getheader('etag').replace('"', ''): print "the same"
else: print "different"
you could make the variables (i.e. etag) global