How to delete multiple lines in a python console window? [duplicate] - python

This question already has answers here:
Rewrite multiple lines in the console
(7 answers)
Closed 9 years ago.
I am currently working on a text-adventure, which should have a more or less complex fight system. To create such, I decided to generate a 16x16 tile battlefield, which will be represented by ASCII-characters. But, because a fight may take more than turn, I don't want to reprint the battlefield multiple times, but delete the "old" one and print the new situation to the same place. But I suppose that it won't work with sys.stdout.write() and sys.stdout.flush() since there have to be removed multiple lines. So my question is: how do I accomplish my goal? At the moment I open a new console window, where everything is reprinted, which is ahem... not very elegant.
Another problem would be the cross-platform use of the programm.

Check out the curses module (http://docs.python.org/2/library/curses.html).

Related

Where to find python .difference() source code? [duplicate]

This question already has answers here:
How to read Python source code directly from IDE
(3 answers)
Finding the source code for built-in Python functions?
(8 answers)
Closed 1 year ago.
I am using macOS, conda python 3.7 with PyCharm CE IDE.
When clicking into the function, the function didn't show any source code.
Therefore, where can I find the .difference() code?
It's written in C to improve performance (pycharm doesn't have access to the cpython source code, so it can't jump to the definition), you find it here: https://github.com/python/cpython/blob/main/Objects/setobject.c#L1481
The main details of the algorithm are from line 1531 and reasonably easy to follow. It basically iterates the first set, checking if each item is in the other set, if it is, add it to a result set, then returns the result set.
The code you're looking for starts here.
First thing it does is check that the two parameters are the same length. Then it goes through the first and checks for elements that are not present in the second, building up the result as it goes. Finally, it returns said result.

Using quit or exit in Python? [duplicate]

This question already has answers here:
How do I terminate a script?
(14 answers)
Closed 2 years ago.
I was told in another language that it is bad practice to call the equivalent to quit() or exit() in Python.
Rather then calling a similar function to quit() or exit() in Python that it is better to let the program end "naturally". In that the main document ends after completing any function/method calls or any condition checking, etc...
Is this also true in Python or is it fine to use these functions for ending the program?
Edit: I will include a link to my actual code to hopefully add some clarification, it is the bottom two function that I'm having issues with
https://github.com/itwasntzak/delivery_tracking-python/blob/test/menus.py
I have never come across where there is a need for a hard stop like that. You are right that it is common practice to let the program execute in full. If you need to break out of a loop then you can use break. Is there a specific example where you think you need to use a quit() tag?

20,000 letter long dictionary in one line in Python [duplicate]

This question already has answers here:
What is the max length of a Python string?
(3 answers)
Closed 4 years ago.
do you have any experience with such a long dictionaries in one line 20.000? I am working on a module that has such. First I imported this dictionary with json, but I feel like it's better to import as least as possible. The code is not meant to be ever opened by the users and is working well for now. Do you have any experience with line that has 20.000 characters? can it cause any problems in the future?
Thanks
One practical problem you could run into is LINE_MAX. On *nix systems, that's the maximum length of a line which is guaranteed to be supported by all the usual utilities like grep and so on. The value promised by POSIX is 2048 bytes, but some systems have a larger value like 4096.
So, if you do decide to have 20,000 characters on a single line, you can't expect the usual utility programs to be able to operate on that file.
Ref: https://www.gnu.org/software/libc/manual/html_node/Utility-Minimums.html

How to solve the error on this code?SyntaxError [duplicate]

This question already has answers here:
SyntaxError: multiple statements found while compiling a single statement
(6 answers)
Closed 5 years ago.
my English is not very good,so if I couldn't explain the question clearly,please forgive me.
I downloaded the Geany directly and used it without any setting(because there are always something wrong and I can't solve the problem.
I input code in geany,then copy and paste the in the python.
then……
Lines in the interpreter run one at a time.
Write a command, press Enter to execute it, and then write the next.
It seems that you are posting the two instructions at once instead the firs instruction (the assignment) and the the second one (the print) but I'm not sure how you can do this. I'd suggest putting them copying and pasting first one, then the other.

Python: profiling blocks of code [duplicate]

This question already has answers here:
How can I profile Python code line-by-line?
(5 answers)
Closed 7 years ago.
I have a Python function in which I want to find most slowing-down places. Just not I'm using cProfile, but I have an additional functionality.
I don't want to split my function into a dozen of sub-functions: it looks a bit bulky and annoying.
Isn't there instead a way to profile a function line-by-line? Or add something like timer_start(timer_id) and timer_stop(timer_id) before and after each block of code I want to profile execution time?
If you are not using IPython already, you should give it a look. It has magic functions like %lprun which make line-by-line profiling easy. Take a look at Timing and Profiling in IPython

Categories