I am outputting the stderr to a wx.TextCtrl, after 10 lines I want to delete the first line so there is only ever a maximum of 10 lines in my wx.TextCtrl window.
I have a python script which is using multiple threads and classes. I just can't for the life of me get the below bit of code to work, can someone give me a few hints please?
a = 1
while True:
line = self.process1.stderr.readline().decode('utf-8')
wx.CallAfter(self.frame.running_log1.AppendText, line)
if a >= 10:
s = wx.CallAfter(self.frame.running_log1.GetLineLength, 0) +1
wx.CallAfter(self.frame.running_log1.Remove, 0, s)
print s
a +=1
When run s = None, so fails. I am using wx.CallAfter as I am using threads.
The reason wx.CallAfter returns None is because there isn't anything to return at that point. It can't return the length, because all it has done is made a note that at some point soon it needs to call the function. It hasn't actually called the function, and won't wait until the function has been called.
In this situation I would write a method that would append a line and remove the first line as necessary. This might look something like:
def appendAndTrim(self, line):
self.frame.running_log1.AppendText(line)
self.line_count += 1
if self.line_count > 10:
first_line_length = self.frame.running_log1.GetLineLength(0) + 1
self.frame.running_log1.Remove(0, first_line_length)
I would then pass this single method to wx.CallAfter, rather than making three separate calls to wx.CallAfter:
self.line_count = 0
while True:
line = self.process1.stderr.readline().decode('utf-8')
wx.CallAfter(self.appendAndTrim, line)
Related
I'm making a method that takes a string, and it outputs parts of the strings on separate line according to a window.
For example:
I want to output every 3 letters of my string on separate line.
Input : "Advantage"
Output:
Adv
ant
age
Input2: "23141515"
Output:
231
141
515
My code:
def print_method(input):
mywindow = 3
start_index = input[0]
if(start_index == input[len(input)-1]):
exit()
print(input[1:mywindow])
printmethod(input[mywindow:])
However I get a runtime error.... Can someone help?
I think this is what you're trying to get. Here's what I changed:
Renamed input to input_str. input is a keyword in Python, so it's not good to use for a variable name.
Added the missing _ in the recursive call to print_method
Print from 0:mywindow instead of 1:mywindow (which would skip the first character). When you start at 0, you can also just say :mywindow to get the same result.
Change the exit statement (was that sys.exit?) to be a return instead (probably what is wanted) and change the if condition to be to return once an empty string is given as the input. The last string printed might not be of length 3; if you want this, you could use instead if len(input_str) < 3: return
def print_method(input_str):
mywindow = 3
if not input_str: # or you could do if len(input_str) == 0
return
print(input_str[:mywindow])
print_method(input_str[mywindow:])
edit sry missed the title: if that is not a learning example for recursion you shouldn't use recursion cause it is less efficient and slices the list more often.
def chunked_print (string,window=3):
for i in range(0,len(string) // window + 1): print(string[i*window:(i+1)*window])
This will work if the window size doesn't divide the string length, but print an empty line if it does. You can modify that according to your needs
I have written an instance method which uses recursion to find a certain solution. It works perfectly fine except the time when I'm exiting the if-elif block. I call the function itself inside IF block. Also, I have only one return statement. The output from the method is weird for me to understand. Here is the code and the output:
def create_schedule(self):
"""
Creates the day scedule for the crew based on the crew_dict passed.
"""
sched_output = ScheduleOutput()
assigned_assignements = []
for i in self.crew_list:
assigned_assignements.extend(i.list_of_patients)
rest_of_items = []
for item in self.job.list_of_patients:
if item not in assigned_assignements:
rest_of_items.append(item)
print("Rest of the items are:", len(rest_of_items))
if len(rest_of_items) != 0:
assignment = sorted(rest_of_items, key=lambda x: x.window_open)[0]
# print("\nNext assignment to be taken ", assignment)
output = self.next_task_eligibility(assignment, self.crew_list)
if len(output) != 0:
output_sorted = sorted(output, key=itemgetter(2))
crew_to_assign = output_sorted[0][1]
assignment.eta = output_sorted[0][4]
assignment.etd = int(assignment.eta) + int(assignment.care_duration)
crew = next((x for x in self.crew_list if x.crew_number == crew_to_assign), None)
self.crew_list.remove(crew)
crew.list_of_patients.append(assignment)
crew.time_spent = assignment.etd
self.crew_list.append(crew)
self.create_schedule()
else:
print("*" * 80, "\n", "*" * 80, "\nWe were not able to assign a task so stopped.\n", "*" * 80, "\n", "*" * 80)
sched_output.crew_output = self.crew_list
sched_output.patients_left = len(rest_of_items)
elif not rest_of_items:
print("Fully solved.")
sched_output.crew_output = self.crew_list
sched_output.patients_left = 0
print("After completely solving coming here.")
return sched_output
This was the output:
Rest of the items are: 10
Rest of the items are: 9
Rest of the items are: 8
Rest of the items are: 7
Rest of the items are: 6
Rest of the items are: 5
Rest of the items are: 4
Rest of the items are: 3
Rest of the items are: 2
Rest of the items are: 1
Rest of the items are: 0
Fully solved.
After completely solving coming here.
After completely solving coming here.
After completely solving coming here.
After completely solving coming here.
After completely solving coming here.
After completely solving coming here.
After completely solving coming here.
After completely solving coming here.
After completely solving coming here.
After completely solving coming here.
After completely solving coming here.
What I don't understand is that as soon as my list rest_of_items is empty, I assign data to sched_output and return it. However, print statement is being executed for the same number of time as recursion was done. How can I avoid this?
My output is perfectly fine. All I want to do is understand the cause of this behaviour and how to avoid it.
The reason it's printing out 11 times is that you always call print at the end of the function, and you're calling the function 11 times. (It's really the same reason you get Rest of the items are: … 11 times, which should be a lot more obvious.)
Often, the best solution is to redesign things so instead of doing "side effects" like print inside the function, you just return a value, and the caller can then do whatever side effects it wants with the result. In that case, it doesn't matter that you're calling print 11 times; the print will only happen once, in the caller.
If that isn't possible, you can change this so that you only print something when you're at the top of the stack. But in many recursive functions, there's no obvious way to figure that out without passing down more information:
def create_schedule(self, depth=0):
# etc.
self.create_schedule(depth+1)
# etc.
if not depth:
print('After completely solving come here.')
returns sched_output
The last resort is to just wrap the recursive function, like this:
def _create_schedule(self):
# etc.
self._create_schedule()
# etc.
# don't call print
return sched_output
def create_schedule(self):
result = self._create_schedule()
print('After completely solving come here.')
return result
That's usually only necessary when you need to do some one-time setup for the recursive process, but here you want to do some one-time post-processing instead, which is basically the same problem, so it can be solved the same way.
(Of course this is really just the first solution in disguise, but it's hidden inside the implementation of create_schedule, so you don't need to change the interface that the callers see.)
As you call your create_schedule function within itself before the function finishes, once it has gotten to the end and doesn't need to call itself again, each function ends, and hits the "After completely solving coming here.", at the end of the function.
This means that each function, after calling itself, is still running - just stuck at the line where it calls itself - until they have all completed, which is when the paused functions can finish their task, printing out your statement.
You have print("After completely solving coming here.") at the end of your recursive function. That line will be executed once for each recursion.
Consider this simple example, which recreates your issue:
def foo(x):
print("x = {x}".format(x=x))
if x > 1:
foo(x-1)
print("Done.")
Now call the function:
>>> foo(5)
x = 5
x = 4
x = 3
x = 2
x = 1
Done.
Done.
Done.
Done.
Done.
As you can see, on the final call to foo(x=0), it will print "Done.". At that point, the function will return to the previous call, which will also print "Done." and so on.
Hello im trying to make an mp3 player. At the moment Im just trying to get it to print the found mp3 file to the console but its stuck in an infinate loop and Im not sure how to break it as im still new to python
def mp3_finder():
times_done = 5
starting = 0
for file in os.listdir(full_dir):
if file.endswith(".mp3"):
while starting < times_done:
starting = starting + 1
print(file)
return mp3_finder()
EDIT:
Sorry i wasnt very clear but what im trying to do is find the mp3 file and print the name to the console 5 times but because it keeps finding the file it keeps printing it to the console until python stops it because it printed hundreds of it
You're calling the function again in the return statement; since you're printing within the function, you can just remove the return entirely.
def mp3_finder():
times_done = 5
starting = 0
for file in os.listdir(full_dir):
if file.endswith(".mp3"):
while starting < times_done:
starting = starting + 1
print(file)
That answers your question about breaking a loop, but perhaps you should ask another one about your code, because I don't think it's going to give you the output you want.
First of all you should probably not call mp3_finder at the end of the function - it will recurse infinitly. Also you probably don't want the inner loop, it wil just print the first file five times. Combined the result will be that the function prints the first file five times, then it calls itself which again prints the first file five times and so on until you reach maximum recursion depth.
What you want to return isn't clear maybe it's OK to just return None (ie skip the return statement entirely). Second you'll need to break out of the loop when you're done.
def mp3_finder():
times_done = 5
starting = 0
for file in os.listdir(full_dir):
if file.endswith(".mp3"):
if starting < times_done:
starting = starting + 1
print(file)
else:
break
def mp3_finder():
times_done = 5
starting = 0
for file in os.listdir(full_dir):
if file.endswith(".mp3"):
while starting < times_done:
starting = starting + 1
print(file)
mp3_finder()
Watch your indent otherwise looks good
I have a game that I've written for my first project and I'd like to have a system where I can play and pause the game. When you click the unpause button, I want it to call a function every 1 second that advances the date. Time.sleep stops the whole program so it's not useful to me and I cant seem to restart threads after I've started one. Here's the advancing day function.
def time():
global timer
timer = threading.Timer(1.0, time)
timer.start()
thirtyonemonths = [1, 3, 5, 7, 8, 10, 12]
thirtymonths = [4, 6, 9, 11]
globalvars.day = globalvars.day + 1
for self in thirtyonemonths:
if self == globalvars.month:
print "hi"
if globalvars.day == 32:
globalvars.day = 1
globalvars.month = globalvars.month + 1
for self in thirtymonths:
if self == globalvars.month:
print "hi"
if globalvars.day == 31:
globalvars.day = 1
globalvars.month = globalvars.month + 1
if globalvars.month == 2:
print "hi"
if globalvars.day == 29:
globalvars.day = 1
globalvars.month = globalvars.month + 1
if globalvars.month == 13:
globalvars.month = 1
globalvars.year = globalvars.year + 1
threading.Thread.time(self)
timer = threading.Timer(1.0, time)
Later I have the code for when the button is clicked that checks if it's paused or not
if b.collidepoint(pos):
if globalvars.ispaused == 1:
globalvars.ispaused = 0
timer.start()
break
if globalvars.ispaused == 0:
globalvars.ispaused = 1
timer.cancel()
break
Everything works perfectly up until I press the button a third time. Does anyone know a way I can restart the thread or maybe use a different method to do what I want?
Without seeing the rest of your code, it's hard to be sure where the problem is, but my guess would be that sometimes, when you click the button, ispaused is 1, but timer is an expired timer rather than a paused one. Calling start on an expired timer has no effect.
While that could be fixed, there are easier ways to do this.
For one thing, it looks like you're using some kind of GUI or game framework here. I have no idea which one you're using, but pretty much every one of them has a function to do timers (in the main event loop, as opposed to in a separate thread, but that's not the key thing here) that are more powerful than threading.Thread—in particular, that can automatically recur every second until canceled. That would obviously make your life easier.
If not, it's pretty easy to write your own repeating Timer, or to just find one on PyPI. Notice that the threading docs start with a link to the source code. That's because, like many modules in the stdlib, threading is written to be simple and easy to understand, to serve as sample code on top of being useful in its own right. In particular, Timer is dead simple, and it should be pretty obvious how to extend it: Just put a loop around the run method.
At the start of your function you've set up a new global each time and a timer:
global timer
timer = threading.Timer(1.0, time)
timer.start()
Then at the end of the function you have threading.Thread.time(self) which isn't needed and needs to be removed. Then after the function declaration you have timer = threading.Timer(1.0, time) which may be an error because when it is first called, the global timer may not have been created yet. Replace that last line of code with time() to just call the function immediately the first time. Changing these two lines will probably fix your code good enough.
Also, you have your for loops like this:
for self in thirtyonemonths:
and the problem with this would be the use of the keyword self . If this function is defined inside a class, then this use of self may be interpreted as a reference to the object. It is usually better not to use keywords such as self as iterators. Replace all uses of self with something else, like m to improve your code.
In the following code i am trying to make a "more" command (unix) using python script by reading the file into a list and printing 10 lines at a time and then asking user do you want to print next 10 lines (Print More..).
Problem is that raw_input is asking again and again input if i give 'y' or 'Y' as input and do not continue with the while loop and if i give any other input the while loop brakes.
My code may not be best as am learning python.
import sys
import string
lines = open('/Users/abc/testfile.txt').readlines()
chunk = 10
start = 0
while 1:
block = lines[start:chunk]
for i in block:
print i
if raw_input('Print More..') not in ['y', 'Y']:
break
start = start + chunk
Output i am getting for this code is:-
--
10 lines from file
Print More..y
Print More..y
Print More..y
Print More..a
You're constructing your slices wrong: The second parameter in a slice gives the stop position, not the chunk size:
chunk = 10
start = 0
stop = chunk
end = len(lines)
while True:
block = lines[start:stop] # use stop, not chunk!
for i in block:
print i
if raw_input('Print More..') not in ['y', 'Y'] or stop >= end:
break
start += chunk
stop += chunk
Instead of explaining why your code doesn't work and how to fix it (because Tim Pietzcker already did an admirable job of that), I'm going to explain how to write code so that issues like this don't come up in the first place.
Trying to write your own explicit loops, checks, and index variables is difficult and error-prone. That's why Python gives you nice tools that almost always make it unnecessary to do so. And that's why you're using Python instead of C.
For example, look at the following version of your program:
count = 10
with open('/Users/abc/testfile.txt', 'r') as testfile:
for i, line in enumerate(testfile):
print line
if (i + 1) % count == 0:
if raw_input('Print More..') not in ['y', 'Y']:
break
This is shorter than the original code, and it's also much more efficient (no need to read the whole file in and then build a huge list in advance), but those aren't very good reasons to use it.
One good reason is that it's much more robust. There's very little explicit loop logic here to get wrong. You don't even need to remember how slices work (sure, it's easy to learn that they're [start:stop] rather than [start:length]… but if you program in another language much more frequently than Python, and you're always writing s.sub(start, length), you're going to forget…). It also automatically takes care of ending when you get to the end of the file instead of continuing forever, closing the file for you (even on exceptions, which is painful to get right manually), and other stuff that you haven't written yet.
The other good reason is that it's much easier to read, because, as much as possible, the code tells you what it's doing, rather than the details of how it's doing it.
But it's still not perfect, because there's still one thing you could easily get wrong: that (i + 1) % count == 0 bit. In fact, I got it wrong in my first attempt (I forgot the +1, so it gave me a "More" prompt after lines 0, 10, 20, … instead of 9, 19, 29, …). If you have a grouper function, you can rewrite it even more simply and robustly:
with open('/Users/abc/testfile.txt', 'r') as testfile:
for group in grouper(testfile, 10):
for line in group:
print line
if raw_input('Print More..') not in ['y', 'Y']:
break
Or, even better:
with open('/Users/abc/testfile.txt', 'r') as testfile:
for group in grouper(testfile, 10):
print '\n'.join(group)
if raw_input('Print More..') not in ['y', 'Y']:
break
Unfortunately, there's no such grouper function built into, say, the itertools module, but you can write one very easily:
def grouper(iterator, size):
return itertools.izip(*[iterator]*size)
(If efficiency matters, search around this site—there are a few questions where people do in-depth comparisons of different ways to achieve the same effect. But usually it doesn't matter. For that matter, if you want to understand why this groups things, search this site, because it's been explained at least twice.)
As #Tim Pietzcker pointed out, there's no need of updating chunk here, just use start+10 instead of chunk.
block = lines[start:start+10]
and update start using start += 10.
Another alternative solution using itertools.islice():
with open("data1.txt") as f:
slc=islice(f,5) #replace 5 by 10 in your case
for x in slc:
print x.strip()
while raw_input("wanna see more : ") in("y","Y"):
slc=islice(f,5) #replace 5 by 10 in your case
for x in slc:
print x.strip()
this outputs:
1
2
3
4
5
wanna see more : y
6
7
8
9
10
wanna see more : n