Python Forum
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
boolean result of loop
#1
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?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
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)
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
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
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
Maybe you can use the for/else construct, the "else" clause is used only if you don't "break" the loop.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#5
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
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)
Reply
#7
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#8
(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.
Reply
#9
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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#10
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Running A Loop Until You See A Particular Result knight2000 6 31,674 Sep-04-2021, 08:55 AM
Last Post: knight2000
  Noob Alert! Wrong result using loop and if statemnent GJG 7 2,856 Dec-19-2020, 05:18 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020