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
Related
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.
This question already has answers here:
Why does python use two underscores for certain things? [duplicate]
(7 answers)
Closed 9 years ago.
Hi everyone as it obvious from my question I am like a brand new to python.
I am so confused when I am reading the documentation on python or even here in the Stackoverflow forum...
Why do they write like that
from __future__ import division
What does the underscore around the Future word mean ?? And Are we supposed to use it like that with the underscore in the python interpreter ?
This is just one of tons of examples. Any help would be greatly appericated.
According to PEP 236 where this module was proposed, the double underscores make it a reserved name.
[5] This ensures that a future_statement run under a release prior to
the first one in which a given feature is known (but >= 2.1) will
raise a compile-time error rather than silently do a wrong thing.
If transported to a release prior to 2.1, a runtime error will be
raised because of the failure to import __future__ (no such module
existed in the standard distribution before the 2.1 release, and
the double underscores make it a reserved name).
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).
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Enforce “spaces” or “tabs” only in python files?
I got Python code that has mixed tabs and spaces and is very difficult to read or understand the indentation, because lines look like they are at a certain indentation in the IDE but Python parses them as a different indentation than what they look like. Do tabs in Python count for a certain hard-coded number of spaces? Is there a way to canonicalize a Python script that has mixed tabs/spaces, to use consistent spacing?
There is no canonical value for the number of spaces that = 1 tab in python (I like 4, but that's just me).
What you can do is read the file in and search for \t characters, and replace those with however many spaces you need.
EDIT: Something that will probably be useful to you in the future is Python's style guide (aka PEP8)
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is there any working memory profiler for Python3
I have some script where I'd like to find out which objects are using up the memory. Moreover it should be for Python 3. I've found some modules suggested for that, but they are often old and not coded for Python 3. Also trying to install all of them to try out is quite a hassle.
Do you know which memory profiler could give me the following statistics or maybe you know a manual Python hack I can program adhoc to examine the rather short script (not written by me and hence a mess :()?
I'd like to see the object count after the run and moreover in which line this object was created. That's all :)
Any ideas?
EDIT:
One idea is to wrap all object instantiations by a class that counts the initialization in a class variable. It just would require to rewrite all instantiations of dicts, lists, etc.
I'm using objgraph which works with python3 without modifications.