Knowing exactly where which computation process wrong in intellij - python

I have the following python code inside a large loop
arr_a[indx]*arr_b[arr_c[indx],]
and with one running, an exception occurred and it said index out of range, but there are two possibilities(indx is out of range for predictErr, or arr_c[indx]), how do I know which part goes wrong?
This problem also extend to some general case that when one write several operations in one line and when things goes wrong, it is hard to tell which part causes this, and note that the above mentioned expression is inside a large loop, which means one can not simply start a debug mode and find that out.

Add a print statement for each segment that might be to blame to see which one fails:
print arr_a[indx]
print arr_c[indx]
arr_a[indx]*arr_b[arr_c[indx],]

Related

How do I put all of my code under an infinite while loop?

I have written a code in Python. Now, I want the code to be repeated forever until the receipt of a certain input, using a while loop the condition of which is always true.
The problem is that I receive indentation error. As far as I know, in Python, indentation specifies which part of code is under which. In the situation I described, all of my code should be under this infinite while, but since I am adding the while after writing the code, the indentation is not automatically set. The code is long and I cannot put a tab before every line of my code to take it under the new while. How do I fix this problem?
In C++, I could do this just by adding the while and putting the code in its {}.
Depending on your editor you can indent an entire block of code at once. Pycharm you highlight the code and press tab. On something like VS Code you highlight the block and hold CTRL and press the right square bracket ].
The general pattern I use for "run forever until" is:
keep_running = True
while keep_running:
# code runs forever and returns condition as bool
if condition is True:
keep_running = False # will stop the loop
# or use break
Above is minimal and intended to be easy to grok. There are many ways to go about this type of task, I wanted to present something straight forward. Use of break will also perform in this manner.
As for indentation, mixing tab+space can get weird. Look into flake8 and pylint libraries (there are others) and see what they tell you for code problems.
Edit: Python's indentation can be hard to get used to and setting up your coding environment well is really important. Virtual environments are also VERY IMPORTANT (VENV and PIPENV), do not skip that work if you install any packages. Also important is to learn how to read Python errors (exceptions/tracebacks), they are quite descriptive, but not intuitive at first. If you are coming from C (or PHP, JS, etc) it is important to note that some "slop" you might be used to ignoring will not be tolerated in Python.
Simple:
while True:
print("Hello there")
print("Hello there") is your code running indefinitely.

While Loop Syntax Error in Wingware

I'm using a while loop in some code. This is the third one in this given program, all of them are formatted the same, yet for some reason one specific line is giving me syntax error. I have tried copying and pasting it directly from another portion of my while loop, and nothing seems to be rectifying it. I'm absolutely baffled. Perhaps another set of eyes can see what I'm missing?
close the parenthesis on the previous line

Spyder IDE automatic indentation

Is there any shortcut to automatically indent marked lines in the editor? For example, in MATLAB there is the CTRL+I shortcut.
Matlab's syntax can match the opening closing statements of if,while, for etc. by looking for end statements.
In Python these are ambiguous and defined as the nested indentetation. Hence this cannot be reliably implemented as you cannot decide whether the succeeding if block belongs the the current for loop or it is the next block, if not indented properly.
If indented properly then Forzaa's answer is the answer otherwise the code is useless anyway and needs to be debugged.
First I will just reconfirm what has been said above, that python is ambiguous about what should be the correct indent. Unfortunately as coming from Matlab I also like Ctrl-I.
Though just check on how Tab and Shift-Tab work in practice, they did a little better than I expected. When I ended up having 2 tabs too much after rearranging code, one Shift-tab brought it back to proper position.

How to delete almost duplicate files

Edit 2:
Solved, see my answer waaaaaaay below.
Edit:
After banging my head a few times, I almost did it.
Here's my (not cleaned up, you can tell I was troubleshooting a bunch of stuff) code:
http://pastebin.com/ve4Qkj2K
And here's the problem: It works sometimes and other times not so much. For example, it will work perfectly with some files, then leave one of the longest codes instead of the shortest one, and for others it will delete maybe 2 out of 5 duplicates, leaving 3 behind. If it just performed reliably, I might be able to fix it, but I don't understand the seemingly random behavior. Any ideas?
Original Post:
Just so you know, I'm just beginning with python, and I'm using python 3.3
So here's my problem:
Let's say I have a folder with about 5,000 files in it. Some of these files have very similar names, but different contents and possible different extensions. After a readable name, there is a code, always with a "(" or a "[" (no quotes) before it. The name and code are separated by a space. For example:
something (TZA).blah
something [TZZ].another
hello (YTYRRFEW).extension
something (YJTR).another_ext
I'm trying to only get one of the something's.something, and delete the others. Another fact which may be important is that there are usually more than one code, such as "something (THTG) (FTGRR) [GTGEES!#!].yet_another_random_extension", all separated by spaces. Although it doesn't matter 100%, it would be best to save the one with the least codes.
I made some (very very short) code to get a list of all files:
import glob
files=[]
files=glob.glob("*")
but after this I'm pretty much lost. Any help would be appreciated, even if it's just pointing me in the right direction!
I would suggest creating separate array of bare file names and check the condition if any element exists in any other place by taking array with all indices excluding the current checked in loop iteration.
The
if str_fragment in name
condition simply finds any string fragment in any string-type name. It can be useful as well.
I got it! The version I ended up with works (99%) perfectly. Although it needs to make multiply passes, reading,analyzing, and deleting over 2 thousand files took about 2 seconds on my pitiful, slow notebook. My final version is here:
http://pastebin.com/i7SE1mh6
The only tiny bug is that if the final item in the list has a duplicate, it will leave it there (and no more than 2). That's very simple to manually correct so I didn't bother to fix it (ain't nobody got time fo that and all).
Hope sometime in the future this could actually help somebody other than me.
I didn't get too many answers here, but it WAS a pretty unusual problem, so thanks anyway. See ya.

Is it possible to end a python module import with something like a return?

I would like to know if there is a way of writing the below module code without having to add another indentation level the whole module code.
# module code
if not condition:
# rest of the module code (big)
I am looking for something like this:
# module code
if condition:
# here I need something like a `return`
# rest of the module code (big)
Note, I do not want to throw an Exception, the import should pass normally.
I don't know of any solution to that, but I guess you could put all your code in an internal module and import that if the condition is not met.
I know of no way to do this. The only thing I could imagine that would work would be return but that needs to be inside a function.
It's super hard to say without knowing what your higher-level goal is. (For instance, what is the condition? Why does it matter? Are you DEAD SURE you're not having an X-Y problem here? Can't you just tell us what your overall goal is?) It's also really hard to say without knowing how the module is going to be called. (As a script from the command line? By being imported by another module?) And it would help a lot to know (a) why you're trying to avoid indentation (WWII is over, and we don't need to ration spaces any more; or, to put it more kindly, Python is a language that uses indentation as a SYNTACTIC FEATURE, so saying "I can't use this syntactic feature" strikes many people as a weird constraint. It's like giving up if-then tests: you might theoretically be able to work around that constraint, possibly, sometimes, but why are you going into the boxing ring with your hands tied behind your back?), and (b) why you can't throw an exception (no, really: are you TOTALLY SURE you ABSOLUTELY CANNOT THROW ANY EXCEPTIONS AT ALL?).
As it is, all you've really done is ask a "how do I do X, given conditions A, B, and C?" question, without indicating why you want to do X, or why conditions A, B, and C exist, or even whether you're 100% sure they exist and cannot be worked around.
If what you're really saying is "I don't want to hit {TAB} 40 times while writing a function," then the real problem is that you need a better text editor. If what you're really saying is "I happen to find indentation to be aesthetically unpleasant," then you should think about (a) what the other side of the argument is; that is, why people Python's use of indentation as syntax to be useful; (b) whether your own aesthetic preferences in this regard are more important than the reasons you've come up with in (a); and (c) whether, given these things, Python is the right tool for you personally to be using to accomplish whatever your own larger-scale goal is. (It's OK to not like indentation as a syntactic feature; but this is so basic to Python that being philosophically opposed to it to an extent that rules it out is a strong indication that maybe Python is not the ideal language for you to accomplish your programming goals in.) If what you're really saying is that you would benefit from factoring code that needs to be run under two different sets of circumstances into two modules, then it would benefit you to refactor. If what you're saying is that you've got spaghetti code that winds up being totally impossible to refactor, then that's really the first problem to be addressed, before you try to abort module imports.

Categories