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

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")

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.

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

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'.

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

Why i cant get the process that exist on print?

import os
import time
os.system('WMIC /OUTPUT:C:\Users\PRO\Desktop\ProcessList.txt PROCESS get Caption')
with open('C:\Users\PRO\Desktop\ProcessList.txt', 'r') as f:
if str('System') in f:
print "Yes"
else:
print "No"
I have tried your code, and it was needed some little modification. I did that, but didn't get desirable result. It write successfully in a mentioned file. So, I though it would be better if I print out each line in my python IDLE. I got such weird result.
At first, it looked fine, when open in any text editor. But later, after finding this weird space between each character, I opened this file in Sublime editor, run over there, then found something look like this
After googling a little bit, I found that this is a NUL terminator used to teminate the string in C\C++. Maybe someone with more knowledge about this can explain it better.
I have no idea, how and why it is adding it after every character. But if you remove that and run again the code, you'll get the desirable result.
Kindly find the below updated code:
import os
import time
os.system('WMIC /OUTPUT:C:\Users\sohan.tirpude\Documents\LogInLog.txt PROCESS get Caption')
searchfile = open("C:\Users\sohan.tirpude\Documents\LogInLog.txt", "r")
for line in searchfile:
line = line.replace('\0', '')
#print line
if 'System' in line:
print "Yes"
break
else:
print "No"
Kindly give it a try.

How can I handle a file in python 2.7 to separate them line-by-line to insert into database?

I am very novice in python 2.7 who is unable to handle a large file( Data-sets for thesis). suppose my data-set(mmd.txt) has 3 attributes.
1,2,3
0.2,2.3,4
5.4,2,3
1.3,2.4,3
9.2,2.6,7.22
5.4,2,3
5.66,4.25,7.6
45.2,52.6,7.22
5.4,20.2,3.6
5.66,4.25,7.6
here is a the data-set (mmd.txt). you can see that every line has three attributes separated by comma and each line defines a row.So how can I handle them/separate them to insert into database?please put your suggestion so that I can understand. Thanks for reading my post.
Hello and welcome to the wonderful world of Python.
What are you trying to do is easily accomplishable. First you need to open the file using the open() function. Than you can split('\n') it into an array containing your lines. Lastly you can for loop through and split(',') the lines, separating the comma-separated values into an array. This is just the basic framework you'd have to follow and needs to be managed accordingly to the size of your input. In practice, it would look like this:
FILE_NAME = "mmd.txt"
with open(FILE_NAME, 'r') as f:
lines = f.read().split('\n');
for line in lines:
print line.split(',')
This code will print your parameters nicely separated inside an array. I think you can go on with that.
If you want to learn more about the language, I'd recommend Codecademy's Python Track.
Happy coding