Python Forum
boolean result of loop - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: boolean result of loop (/thread-2648.html)

Pages: 1 2


boolean result of loop - Skaperen - Mar-31-2017

a classic loop structure tests for one of a sequence of possible conditions.  in python the loop iterates over an iterator or equivalent (a sequence).  a variable starts at an initial state and is set by the condition.  it is then tested after the loop.

here is an example:

unsafe = False
for part in filename.split('/'):
    if part == '..':
        unsafe = True
if unsafe:
    raise UnsafeFileError(filename)
what i am looking for is if python has any simpler-code method for doing this kind of test.  since this is a example it needs to be not specific to this example.  any clues?


RE: boolean result of loop - wavic - Mar-31-2017

Like this? 

In [1]: filename = "../kill_all_humans.py"

In [2]: unsave = True and filename.startswith('..')

In [3]: unsave
Out[3]: True
I think it can be done all at once. I presume UnsafeFileError is defined.

filename.startswith('..') and UnsafeFileError(filename)



RE: boolean result of loop - Skaperen - Mar-31-2017

i am looking for something specific to "any iterator" and "any logic test", hopefully that can done on one line.  files and raising an exception was just a believed-to-be-classic example, not what i am actually looking for.  i was hoping for something like how enumerate() gets used and/or how comprehensions get used, etc,

Moderator Kebap: Removed fullquote


RE: boolean result of loop - Ofnuts - Mar-31-2017

Maybe you can use the for/else construct, the "else" clause is used only if you don't "break" the loop.


RE: boolean result of loop - Skaperen - Apr-01-2017

look at the structure of the example i gave.  this is what i wanted to avoid if possible.  this is not very complex.  then i remember how enumerate() simplified some not-so-complex logic into more elegant logic.  i am looking for something like that.


RE: boolean result of loop - buran - Apr-01-2017

I'm really not sure what you want to do (in more general terms), but your code is equivalent to:
if '..' in filename.split('/'):
   raise UnsafeFileError(filename)



RE: boolean result of loop - wavic - Apr-01-2017

When you raise and exception the execution of the code stops. So, how would you check for the other 'parts'? In order to do it, you have to handle the exception with try/except.


RE: boolean result of loop - buran - Apr-01-2017

(Apr-01-2017, 05:02 AM)wavic Wrote: When you raise and exception the execution of the code stops. So, how would you check for the other 'parts'? In order to do it, you have to handle the exception with try/except.

well you don't need to check all parts in order to rise exception, if you have found the specific part you are looking for, e.g. already in the beginning - the result will be the same - an exception :-) It the same what Python does when evaluate or conditions. Lets say that you have 3 parts after the split. So the test is equivalent to if part0 == '..' or part1 == '..' or part2 == '..' rasie Exception. When evaluate this if test with ORs python will stop at first TRUE and not evaluate further, just return TRUE for the test.


RE: boolean result of loop - wavic - Apr-01-2017

Right!  Blush I am sleeping yet.

I was thinking about something like this:

_ = [raise UnsafeFileError(filename) for s in filename.split('/') if s == '..']
But throws me an error.
So, just to raise and error doesn't work this way.

But If you define a function which raises an error...

def error():
    raise UnsafeFileError(filename)

_ = [error() for s in filename.split('/') if s == '..']

Perhaps the same can be done with map()

If raise was a function...
Perhaps lambdas could be used here   Huh


RE: boolean result of loop - ichabod801 - Apr-01-2017

If you want it in one line it sounds like you would be using a list comprehension for it. Why not just use the conditional part of the list comprehension?
if [part for part in filename.split('/') if part == '..']:
    raise UnsafeFileError(filename)
It's kind of inefficient, but it works.