Python warnings- how to not print the source line? [duplicate] - python

This question already has answers here:
Print only the message on warnings
(4 answers)
Closed 8 years ago.
When I issue a warnings.warn() warning in python, the output to stderr includes not just my warning method, but also the offending line from the source code. I would like to suppress this line from the source code, as it really detracts from expressing the warning clearly. Does anyone know how to do this?

You can monkey-patch the formatwarning function. If you look at the source, it shouldn't be too hard to write a replacement that doesn't have a line number there.

Related

Python zero as file parameter in open function [duplicate]

This question already has answers here:
Integer File Descriptor "0" in open()
(3 answers)
Closed 1 year ago.
I'm currently puzzling around this piece of code.
What exactly does
open(0)
do?
I already looked up the docs or tried to find something in the internet but no clues.
The codesnippet where this code is used:
map(abs,map(int,open(0).read().split()))
Thanks ^
0 is the file descriptor associated with stdin (1 corresponds to stdout, 2 to stderr). open takes int file descriptors as an argument, not just paths, so passing 0 is legal. This is just a somewhat obscure way to create a file object bound to stdin without needing to import sys.
The flaw with it is that, when it is closed, it will close the file descriptor 0 (since closefd=False was not passed), so sys.stdin will be closed without realizing it (though it'll probably figure it out when if someone tries to use it).

How to call various strings without using %s? [duplicate]

This question already has answers here:
Type Error: Format Requires Mapping
(2 answers)
Closed 3 years ago.
text1="Python"
text2="with me"
print("Study %(language)s" %{'language':text1})
This works. But I am wondering whether it is using dictionary to call string?
print("Study %(language)s %(with whom)" %({'language':text1},{'with whom':text2}))
But it doesn't work. How can I fix it?
The error says 'format requires a mapping'
It would have worked, if you noticed that:
you've forgotten to put an s after %(with whom) --> %(with whom)s,
and instead of this %({'language':text1},{'with whom':text2}) --> %{'language':text1,'with whom':text2}
so the line would be like this:
print("Study %(language)s %(with whom)s" %{'language':text1,'with whom':text2})

How to read a line in the terminal with python [duplicate]

This question already has answers here:
Capture stdout from a script?
(11 answers)
Closed 3 years ago.
I'm trying to get a already written line in python 3, but I haven't found any function that can read a line from the terminal. It should work something like sys.stdout.read(), or sys.stdout.readline() but this function just throws an error.
If you mean to read from the user/a pipe, then simply use input.
However, from your comments it seems like you want to be able to read from what has already been printed.
To do this, you have a few options. If you don't actually want it to display on the terminal, and you only care about certain part of the output, then you can use contextlib.redirect_stdout and contextlib.redirect_stderr. You can combine this with io.StringIO to capture the output of your application to a string. This has been discussed in the question Capture stdout from a script in Python
However, if you want to have something which provides you both a means of printing to the terminal and giving you the lines, then you will need to implement your own type which inherits from io.TextIOBase or uses io.TextIOWrapper.
Do you mean something like this?
name = input("Enter a name: ")
print(name)

Python Formatting - Break up long line caused by indexing [duplicate]

This question already has answers here:
pep8 compliant deep dictionary access
(2 answers)
Closed 4 years ago.
I have a line accessing a very nested attribute in a dictionary that is now failing a PEP8 checker that was recently added to our release pipeline.
For example:
nested_attr = d['long_attr_name_1']['long_attr_name_2']['long_attr_name_3']['you_get_the_point']
What is the pythonic way to break up a line like this so it does not exceed the style guide line limit check?
nested_attr = (d['long_attr_name_1']['long_attr_name_2']
['long_attr_name_3']['you_get_the_point'])
Wrapping in brackets if a PEP8 saviour!

Why can't I print outside loop [duplicate]

This question already has answers here:
Python indentation mystery
(3 answers)
Closed 6 years ago.
I am a code beginner.Can anyone tell me what happend if I get a syntaxerror when putting the "print" outside the loop
Seems like you are using an interactive interpreter shell. You should hit enter after the last line of the loop before you try to print.
In future questions please write your code in the question body instead of attaching a screenshot.

Categories