Using Squish with python, where the text of a print is printed? - python-2.7

In a python script that is called by the test case, I writed a print.
Where the output of a the function print is printed ?
Note : I use Squish 6.2.0 with python 2.7, but any answer to the question will be welcome.

To see the text printed by the python print, click on 'Show View', then 'Runner/Server Log'.

Related

Python 2.7.15 adds a new line in Windows to the end but not on Linux. How do I fix this?

This is not a typical "how do I strip a new line or spaces" question...
I am new to python in general. But I am aware of
print ("test", end="")
print ("test", end="")
for Python 3 and
print "test",
print "test",
for Python 2
Python 3 implementation will print correctly on both Linux and Windows machines; however the Python 2 implementation will add an extra line at the end of the execution on Windows based machines (but not Linux, there it prints correctly). Is there any way to get rid of this new line?
I have searched around and I cant seem to find anyone talking about this particular issue. Here is a screenshot for demonstration:
So, in accordance with the print documentation
Standard output is defined as the file object named stdout
And we probably assume that python I\O are system dependent, so that's how we could try to guess the explanation of this situation, even thought print documentation states:
A '\n' character is written at the end, unless the print statement
ends with a comma.
OR The reason is that Windows & Linux threat print statement differently (since print is a statement in Python 2, and a function call in Python 3).
Back to the question, how to get rid of this line:
I used future statement for print function:
from __future__ import print_function
print('test', end=' ')
print('test', end='')
If I find any reasonable explanation, I will update the answer (should be somewhere !).
After speaking to a few people about this and doing some research. It appears the most straightforward way around this issue is to directly use:
sys.stdout.write()
instead of print. You can then format your output similar to the way C/C++ and Java work.

Why does termcolor not work in python27 windows?

I just installed termcolor for python 2.7 on windows8.1. When I try to print colored text, I get the strange output.
from termcolor import colored
print colored('Hello world','red')
Here is the result:
[31mHello world[0m
Help to get out from this problem.Thanks,In advance
See this stackOverflow post.
It basically says that in order to get the escape sequences working in Windows, you need to run os.system('color') first.
For example:
import termcolor
import os
os.system('color')
print(termcolor.colored("Stack Overflow", "green")
termcolor or colored works perfectly fine under python 2.7 and I can't replicate your error on my Mac/Linux.
If you looks into the source code of colored, it basically print the string in the format as
\033[%dm%s\033[0m' % (COLORS[color], text)
Somehow your terminal environment does not recognise the non-printing escape sequences that is used in the unix/linux system for setting the foreground color of xterm.

Python Search String One Liner

I'm trying to use Python a little more and I know this isn't the best use case for it but it's bothering me on why I can't get it to work.
I'm currently using Python 2.7.6 and I want to cat a file and then pull specific strings out of it based on regex. The below code works fine for what I want, but only looks at the first line.
cat /tmp/blah.txt | python -c "import re,sys; m = re.search('Host: (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).*OS:(.*) Seq:', sys.stdin.read()); print m.group(1), m.group(2)"
So I assumed I could just use a for loop or fileinput to read the entire file and then put the rest of my code in there but I keep on getting errors with the for loop.
cat /tmp/blah.txt | python -c "import sys; for line in sys.stdin: print line" File "<string>", line 1
import sys; for line in sys.stdin: print line
^
SyntaxError: invalid syntax
I've tried a few variations of this but can't get it to work. It always says invalid syntax at the for portion. I know it has to be something very stupid/obvious I'm doing but any help would be appreciated.
If I created a program called arg.py and put the code below in it and call Python via cat, it works fine. It's just the one liner portion that isn't working.
import sys
for line in sys.stdin:
print line
Unfortunately, constructs that introduce indentation in python like if, for among others are not allowed to be preceded by other statements.
Even in your arg.py file try the following:
import sys; for line in sys.stdin: print line
You will discover that the syntax is also invalid which results to the same error.
So, to answer your question, your problem is not the fact that you ran the program in the terminal but the problem is in python syntax itself. It does not support such syntax
Check out a related question

How to paste multiple lines to an ipdb shell in python?

I am working with python and ipdb debugger.
Let's say is set some breaking point in some line in a python file.
Now, after running the python file, the program stops at the breakpoint.
I want to be able to paste multiple lines to the ipdb shell.
Now i get an error, if trying to paste mutiple lines.
How can i paste mutiple lines?
Thanks.
As far as I know, you cannot simply paste them. You have to use ; to indicate indentation. For example:
for i in range(10): print i; print("hello")
would be equivalent to
for i in range(10):
print(i)
print("hello")
If you want the hello out of the loop, then you need to use ;; instead:
for i in range(10): print i;; print("hello")

Parsing command output in Python

I'm trying to parse out from Popen. Output looks like a table:
SceenShotAttached
How do I print just the col c?
I could do it easily using awk in a shell script but I'm depending on python solely now.
Thank you!