I'm wondering if i can make this shorter:

if not link.startswith("javascript") and not link.startswith("mailto") and not link.endswith("pdf") and not link.endswith("ppt"):
    #do something

Cause this is getting VERY lengthy, very soon

Edited 6 Years Ago by Aeronobe: n/a

I guess you can assign them to temporary var?

temp1 = not link.startswith("javascript")
temp2 = not link.startswith("mailto")
temp3 = not link.endswith("pdf")
temp4 = not link.endswith("ppt")
if temp1 and temp2 and temp3 and temp4:
    #do something

del temp1, temp2, temp3, temp4

Don't see why it would be better though.

Edited 6 Years Ago by ultimatebuster: n/a

Here is some ways, first is to overcome too long lines, second to save typing.

The second is not so useful maybe as you think, because Python is using byte code internally and then for speed for example the length of text does not matter so much. I only give it to show how functions are equal position with other values in Python.

for link in ['javascript:asdfajfd.js','mailto:fasf@dsaf.saf', 'sdfaf.doc','flasdjf.html','afjdalfj.ppt','fasdfa.xls']:
    if not link.startswith("javascript") and not link.startswith("mailto") and not link.endswith("pdf") and not link.endswith("ppt"):
        #do something
        print 'Here we are',link

## splitting condition to multiple lines with using of extra parenthesis
    if (
        not(
             link.startswith("javascript") or link.startswith("mailto")
             )
        and not (
            link.endswith("pdf") or link.endswith("ppt")
            )
        ):
        #do something
        print 'Here we are again',link
    ls = link.startswith
    le = link.endswith
    if not ls("javascript") and not ls("mailto") and not le("pdf") and not le("ppt"):
         #do something
        print 'And even here',link

Well, if you are going to add more to the list of things it can't start or end with, you can do something like this:

nostart = ("javascript","mailto")  #you can add more
noend = ("pdf","ppt")

if not ([item for item in nostart
        if link.startswith(item)] +
            [item for item in noend
                if link.endswith(item)]):
    print "Link passed the test."
Comments
First once that answers the question with a valid answer.

startswitch/endswith you can use tuple.

if not link.startswith(('javascript','mailto')) and not link.endswith(('pdf','ppt')):
#do something

Depends on how input text is,but a regex can bee used.
Just an untestet regex for this could look something like this. ^javascript|^mailto|pdf$|ppt$ .

Or make variables with tuple as jca0219 suggest.

Edited 6 Years Ago by snippsat: n/a

Didn't expect as many answers!

Time for me to start experimenting with them, i don't doubt they all work :)

Thanks for the help everyone !

Here would be another approach ...

# change start and ending to test 
link = "mailto ---- pdf"

# startswith conditions
start = ('javascript','mailto')
# endswith conditions
end = ('pdf','ppt')
if not all( (link.startswith(start), link.endswith(end)) ):
    # do something
    print("link = %s did not pass" % link)
else:
    print("link = '%s' did pass" % link)
This question has already been answered. Start a new discussion instead.