Python: printing bottom up - python

I have a code in python and I need to print 3 results, the first one at the bottom, the third on the top.
The terminal will print in this order:
first
(pause)
second
(pause)
third
I would like to print in this order instead (there will be a pause in between the output):
...third
...second (pause)
first to be printed (then pause)
in order to simulate this I'm using this sequence of print statement:
from time import sleep
print '1'
sleep(1)
print '2'
print '1'
sleep(1)
print '3'
print '2'
print '1'
This bottom up printing should be only used specifically for this 3 line output and not globally for all the output.
Is that possible? Thank you
UPDATE:
It seems that the only library I can effectively use for modifying the newline order of the terminal is curses.
I will give it a try. Thank you.

I think you want to "overwrite" the print outputs for every successive run. If newlines are not a hard constraint, you can do something like this:
import sys
import time
for i in range(3):
sys.stdout.write('\r' + ','.join([str(a) for a in range(i+1, 0, -1)]))
sys.stdout.flush()
time.sleep(1)
sys.stdout.write('\n')
This will first print 1, wait for a second, overwrite the 1 with 2,1, wait for a second and then overwrite the 2,1 with 3,2,1, wait for a second and exit.
The interesting portion is the use of the \r in the output string - this is the Unix carriage return which causes the stdout file pointer in terminals to move to the leftmost position. Now that the pointer is located at the left-most position, we then print the next set of characters in effect overwriting the previous output.
I use sys.stdout.write to avoid the implicit newline or space that print adds. The range is written in this way to generate a reversed list of numbers to print. The flush is needed to force the characters to be flushed to the output.
This only works as long as the output is on a single line - multi-line outputs like in your example will not work with this method. If you must have multi-line outputs, the curses library is your best bet.

Examples :
results = ["1","2","3"]
print "\n".join(results)
1
2
3
print "\n".join(results[::-1])
3
2
1

Yes it is possible. Please post any code that you are stuck with so that people can help you faster. You can use reversed(), a python built-in to reverse any iterable you are trying to print.
For example:
list = ['1', '2', '3']
for item in list:
print item
# You get
# 1
# 2
# 3
Now if you just want to reverse this order of printing you can do something like this:
for item in reversed(list):
print item
# You get
# 3
# 2
# 1
Hope this helps.

Related

Cannot understand how this Python code works

I found this question on HackerRank and I am unable to understand the code(solution) that is displayed in the discussions page.
The question is:
Consider a list (list = []). You can perform the following commands:
insert i e: Insert integer at position .
print: Print the list.
remove e: Delete the first occurrence of integer .
append e: Insert integer at the end of the list.
sort: Sort the list.
pop: Pop the last element from the list.
reverse: Reverse the list.
Even though I have solved the problem using if-else, I do not understand how this code works:
n = input()
slist = []
for _ in range(n):
s = input().split()
cmd = s[0]
args = s[1:]
if cmd !="print":
cmd += "("+ ",".join(args) +")"
eval("slist."+cmd)
else:
print slist
Well, the code takes advantage of Python's eval function. Many languages have this feature: eval, short for "evaluate", takes a piece of text and executes it as if it were part of the program instead of just a piece of data fed to the program. This line:
s = input().split()
reads a line of input from the user and splits it into words based on whitespace, so if you type "insert 1 2", s is set to the list ["insert","1","2"]. That is then transformed by the following lines into "insert(1,2)", which is then appended to "slist." and passed to eval, resulting in the method call slist.insert(1,2) being executed. So basically, this code is taking advantage of the fact that Python already has methods to perform the required functions, that even happen to have the same names used in the problem. All it has to do is take the name and arguments from an input line and transform them into Python syntax. (The print option is special-cased since there is no method slist.print(); for that case it uses the global command: print slist.)
In real-world code, you should almost never use eval; it is a very dangerous feature, since it allows users of your application to potentially cause it to run any code they want. It's certainly one of the easier features for hackers to use to break into things.
It's dirty code that's abusing eval.
Basically, when you enter, for example, "remove 1", it creates some code that looks like sList.remove(1), then gives the created code to eval. This has Python interpret it.
This is probably the worst way you could solve this outside of coding competitions though. The use of eval is entirely unnecessary here.
Actually I Find some error in the code, but I came to an understanding of how this code runs. here is it:
input :
3
1 2 3
cmd = 1 + ( 2 + 3)
then eval(cmd) i.e., eval("1 + (2 + 3)") which gives an output 6
another input:
4
4 5 6 2
cmd = 4 + ( 5 + 6 + 2)
eval(cmd)
if __name__ == '__main__':
N = int(raw_input())
lst=[]
for _ in range(N):
cmd, *line = input().split()
ele= list(map(str,line))
if cmd in dir(lst):
exec('lst.'+cmd+'('+','.join(ele)+')')
elif cmd == 'print':
print(lst)
else:
print('wrong command', cmd)

Copy and paste Python functions in Emacs

I have a program that looks something like (this is a silly example to illustrate my point, what it does is not very important)
count = 0
def average(search_term):
average = 0
page = 0
current = download(search_term, page)
while current:
def add_up(downloaded):
results = downloaded.body.get_results()
count += len(results)
return sum(result.score for result in results)
total = average*count
total += add_up(current)
average = total/count
print('Average so far: {:2f}'.format(average))
page += 1
current = download(search_term, page)
If I have the cursor on any of the lines 8–11 and press a key combination I want Emacs to copy or kill the add_up function, and then I want to move the cursor to line 2 and press a key combination and paste the function there, with the correct level of indentation for the context it is pasted in.
Is this possible, and if so, how would I do that?
With python-mode.el py-kill-def and yank would do the job.
However, there are some restrictions. py-kill-def must be called from inside def in question. So needs to go upward from line 11 first.
Also indenting after insert poses some problems: as indent is syntax, sometimes Emacs can't know which indentation is wanted. In example below have an indent of 4 first and of 8 in add_up probably is not wanted - however it's legal code. After indenting first line in body of add_up, py-indent-and-forward should be convenient for the remaining.
def average(search_term):
average = 0
def add_up(downloaded):
results = downloaded.body.get_results()
count += len(results)
return sum(result.score for result in results)
page = 0
current = download(search_term, page)
while current:
total = average*count
total += add_up(current)
average = total/count
print('Average so far: {:2f}'.format(average))
page += 1
current = download(search_term, page)
For this type of thing I usually use expand-region, which I choose to bind to C-=.
Using your example I can select the add_up() function by pressing C-= once, kill the region normally (C-k), move to line 2, and yank as usual (C-y).
Depending on what else you have configured for Python you may have to clean up some whitespace, or it may get cleaned up for you. For example, aggressive-indent would be helpful.
One manual option would be to reindent the pasted code with something like C-x C-x M-\.
I've been using smart-shift (available in Melpa) for this sort of thing. global-smart-shift-mode to enable (beware, it binds keys). Select the block you want to move (I'd use expand-region like Chris), and the default keybind C-S-c <arrow> starts moving it. Once you're shifting, the arrows (without C-S-c) shift further. Horizontal shifts use the major mode's indent offset (python-indent-offset for python.el).

Within a nested for loop, how can I print tab and carriage return delimited groups of data?

My code is going through frames in a video, and sampling a set of x,y points that I previously selected in each frame. The x,y points are contained in boxes.
For each frame in the video, there are len(boxes) pixel values I want printed on one line with tabs between them, followed by carriage return for the next frame. I'm actually doing some ratioing of pixel values here, but that's not really relevant. I just need to get output with tabs where I want them and not carriage returns.
print("size of boxes: ", len(boxes))
for i in range (0, video.__len__(), 2):
for j in range(0, len(boxes)):
print(str(float(video[i][boxes[j]])/(video[i+1][boxes[j]])) + str('\t'))
print('\n')
I get this instead of the tab delimited groups of 7 that I want:
('size of boxes: ', 7)
0.485893416928
0.602201257862
0.584277620397
0.759312320917
0.663671373556
0.70249017038
0.724576271186
0.496379726468
0.632218844985
0.532608695652
0.699738903394
0.731774415406
0.693527080581
0.772058823529
Use the end argument to print to override the default behavior of printing a newline:
print(video[i][boxes[j]] / video[i+1][boxes[j]], end='\t')
Note that if this is python 3, the float should also be unnecessary. If you're in python 2, you can get all the relevant features with:
from __future__ import print_function, division
If I were writing this, I'd do it:
s = '\n'.join(
'\t'.join(
str(v1[b] / v2[b])
for b in boxes
)
for v1, v2 in zip(video[0::2], video[1::2])
)
print(s)
I accepted Eric's answer but I want to point out that for Python 2 having a comma at the end of the print statement prevented python from automatically adding a carriage return. Also, apparently Python 3.0 uses print() as a function, whereas Python 2 uses print not as a function but rather as a statement. This means that I could optionally also eliminate the parenthesis after print() in my original code, or add a space: print ().
For my scrappy original code, the above meant simply adding a comma after the print statement
print("size of boxes: ", len(boxes))
for i in range (0, video.__len__(), 2):
for j in range(0, len(boxes)):
t0 = time.clock()
print str(float(video[i][boxes[j]])/(video[i+1][boxes[j]])), "\t",
print ('\n')

How to check if variable matches, then do something

I have a script that pulls some data from a network device, strips off some crap and returns a value via a re.search.
the end result is i have two variables that contain a numerical value, say file1 contains one line with '10', file2 contains one line with '20'. i've put these into variables
oldnumber = 10
newnumber = 20
what i need to do is check to see if the numbers are the same value. if the numbers are the same, do nothing. if they aren't the same, then do something else - ie. send a mail to myself (smtplib works for me).
i'm new to python and finding my way, not sure how to code this?
i suppose the simplest way to describe this is if oldnumber = newnumber, then send mail, else do nothing.
If I remember well you're right
just do
if oldnumber!=newnumber;
do what you want
http://www.tutorialspoint.com/python/python_if_else.htm
almost right.
if oldnumber != newnumber:
# do something
# and then proceed..
Or:
if oldnumber == newnumber:
# do this
else:
# do that
# and then proceed..

Use of set_timeout with Sublime Text 3 API

I am trying to use (as the title says) the
set_timeout
function of sublime text 3 in a plugin.
For what I understood, the use of a lambda function is required in many cases. So I tried this simple test :
class SetTimeoutTestCommand(sublime_plugin.WindowCommand):
def run(self):
for x in range(1,10):
sublime.set_timeout(lambda : print(x), 4000)
So I expected that I will have number printed one at a time with a delay of 4 seconds between each. As explained in the Sublime 3 API :
Runs the callback in the main thread after the given delay (in milliseconds). Callbacks with an equal delay will be run in the order they were added.
But instead I have 9 '9' that are printed after 4 seconds. So all '9' are printed at the same time, based on the first iteration of the loop.
Do you have an idea of what I can do to solve this ?
Thanks in advance !
Edit : I found this which work (well, which print '9' 9 times with 1 second delay between each :
class SetTimeoutTestCommand(sublime_plugin.WindowCommand):
def run(self):
for x in range(1,10):
sublime.set_timeout(lambda : print(x), x*1000)
But on problem remains : it only prints out '9' ....
To print different numbers change your plugin script on this
class SetTimeoutTestCommand(sublime_plugin.WindowCommand):
def run(self, edit):
for x in range(1,10):
sublime.set_timeout(lambda x=x: print(x), x*1000)
Because all lambda functions refer to the same x and when it's executed the x value reaches 9.
The first thing to understand is that the set_timeout calls return immediately. That is, you are scheduling all of the print functions to be run in 1 second. Not 1 second from each other. Based on your edit, it seems you figured that out, but just thought I would clarify.
As for always printing 9, all of the print statements are referencing the same value. So, even when the first print is scheduled, it references the same x value that you are incrementing. By the time the print actually runs (1 second later), the value of x is 9. Thus, 9 is printed for every scheduled callback.
Hope that clarifies things some.

Categories