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

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.

Related

why ast.literal_eval('1+1') is wrong in python2.7 [duplicate]

This question already has answers here:
Why does ast.literal_eval('5 * 7') fail?
(2 answers)
Closed 5 years ago.
When I execute code ast.literal_eval('1+1') in python2.7, the result as follows:
And I try it in python3.6, it works correctly. So what's the reason?
Both the 2.7 and 3.6 docs say the following:
This can be used for safely evaluating strings containing Python values from untrusted sources without the need to parse the values oneself. It is not capable of evaluating arbitrarily complex expressions, for example involving operators or indexing.
Addition is an operator, so this is documented not to work. The fact that it does work in Python 3.6 is surprising to me. Searching the bug tracker, this discrepancy is listed as Python bug #31778.

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

How do I block python RuntimeWarning from printing to the terminal? [duplicate]

This question already has answers here:
How to disable Python warnings?
(12 answers)
Closed 8 years ago.
I have some python code that, at some point, takes the axis means of a reasonably sparse array with nans in. Every time I run the code it raises a RuntimeWarning.
I know what's causing the warning, it's intentional, and it doesn't affect the output. It is, however, quite irritating to be presented with the warning every time I run the program - so, is there a cheap and nasty way to prevent them from being printed to the terminal?
This may be useful to you, I think the issue has been pretty solved in this question: How to disable python warnings
All what you need is suppress it exactly as described in official documentation: https://docs.python.org/2/library/warnings.html#temporarily-suppressing-warnings

Python 3 memory profiler or simple alternative [duplicate]

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.

Categories