Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,280
    Rep Power
    13

    Question '-' not in ( name and month and year )


    Code:
    
            if '-' not in ( name and month and year ):
                cur.execute( '''SELECT * FROM works WHERE clientsID = (SELECT id FROM clients WHERE name = %s) and MONTH(lastvisit) = %s and YEAR(lastvisit) = %s ORDER BY lastvisit ASC''', (name, month, year) )
            elif '-' not in ( name and year ):
                cur.execute( '''SELECT * FROM works WHERE clientsID = (SELECT id FROM clients WHERE name = %s) and YEAR(lastvisit) = %s ORDER BY lastvisit ASC''', (name, year) )
            elif '-' not in ( month and year ):
                cur.execute( '''SELECT * FROM works WHERE MONTH(lastvisit) = %s and YEAR(lastvisit) = %s ORDER BY lastvisit ASC''', (month, year) )
            elif '-' not in year:
                cur.execute( '''SELECT * FROM works WHERE YEAR(lastvisit) = %s ORDER BY lastvisit ASC''', year )
    ======================

    i just want 4 cases to examine so correct execute to be run:

    i'm reading and reading and reading this all over:

    if '-' not in ( name and month and year ):

    and i cant comprehend it.

    While it seems so beutifull saying:

    if character '-' aint contained in string name , neither in string month neither in string year.

    But it just doesn't work like this.

    Since ( name and month and year ) are all truthy values, what is returned by this expression to be checked if it cotnains '=' within it?

    How can i write ti correctly?
    Please tell em and do explain.
    What is now proved was once only imagined!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    163
    Rep Power
    19
    You may do this (doesn't need an explanation. It's pretty much English-like):
    Code:
    if '-' not in name and '-' not in month and '-' not in year:
        <statements>
    elif '-' not in name and '-' not in year:
        <statements>
    <and so on>
  4. #3
  5. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,280
    Rep Power
    13
    tahn k you, but i forgot to mentioned that this way i used to had it before i tried to make thigns more compact by using the non in (string1 string2 string3) method.

    Can you write it in a more compact way?
    What is now proved was once only imagined!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    2
    Well you could do this, but it isn't much shorter than the alternative:
    python Code:
    if not any('-' in x for x in [name,month,year]):

    -Mek
  8. #5
  9. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,280
    Rep Power
    13
    It works, ill try it but can you plz explain this too?

    if '-' not in ( name and month and year ):

    why doesnt it work as expected?
    What is now proved was once only imagined!
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    2
    Code:
    (name and month and year)
    is like a Boolean expression. It will evaluate to True if all three are True and False otherwise (well technically it will evaluate to the last value if they are all Boolean True, or the first value that is Boolean False; but it makes no difference here). You cannot use it the way you desired regardless of how logical you think it looks.

    Basically you are writing:
    Code:
    if "-" in True:
    or
    Code:
    if "-" in False:
    Obviously when you think of it this way you can see it makes no sense.

    -Mek
    Last edited by Mekire; June 13th, 2013 at 09:50 AM.
  12. #7
  13. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,280
    Rep Power
    13
    Originally Posted by Mekire
    Code:
    (name and month and year)
    is a Boolean expression. It will return True if all three are True and False otherwise (well technically it will return the last argument if they are all Boolean True, or the first argument that is Boolean False; but it makes no difference here). You cannot use it the way you desired regardless of how logical you think it looks.

    Basically you are writing:
    Code:
    if "-" in True:
    or
    Code:
    if "-" in False:
    Obviously when you think of it this way you can see it makes no sense.

    -Mek
    and for it to return true all string must have values, right?


    But (name or month or year) is also a Boolean expression.

    It will return the first of the three depending which value comes first as truthy.
    it will return False if none of the three are False.

    So how am i going to write it that

    if '-' not in name and '-' not in month and '-' not in year: ????
    What is now proved was once only imagined!
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    2
    Ok lets be technical:
    Code:
    (a and b and c)
    evaluates to c if all values are Boolean True; or evaluate to the first Boolean False value.
    Code:
    (a or b or c)
    evaluates to c if all values are Boolean False; or evaluate to the first Boolean True value.


    So lets take the first. If we have the three values:
    Code:
    a,b,c ="one","t-wo","three"
    all three values are Boolean True; however the expression evaluates to the last term:
    Code:
    >>> ("one" and "t-wo" and "three")
    'three'
    >>>
    So now if you say:
    Code:
    >>> "-" not in ("one" and "t-wo" and "three")
    it is the same as writing:
    Code:
    "-" not in "three"
    So your expression evaluates to True when this isn't actually what you wanted.

    -Mek
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    431
    Rep Power
    67
    I would do

    Code:
    if all('-' not in x for x in [name, month, year]):
    even though as Mekire said it is not much shorter, because I still think it’s more readable. Of course you could also use concatenation if you’re sure all the variables are of the same type (i.e., strings):

    Code:
    if '-' not in name + month + year:

    Comments on this post

    • Mekire agrees : I tend to favor [any] over [all] where possible because it is slightly faster but of course here that is a non-issue.
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (mini laptop, work laptop), Android 4.2.1 (tablet)
  18. #10
  19. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,280
    Rep Power
    13
    Originally Posted by SuperOscar
    I would do

    Code:
    if all('-' not in x for x in [name, month, year]):
    even though as Mekire said it is not much shorter, because I still think it’s more readable. Of course you could also use concatenation if you’re sure all the variables are of the same type (i.e., strings):

    Code:
    if '-' not in name + month + year:
    this looks very nice and staightforward, and yes all 3 vars are strings.
    whats the difference between:

    if '-' not in name + month + year:

    and this

    if '-' not in ( name and month and year):

    can you explain please?
    What is now proved was once only imagined!
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    431
    Rep Power
    67
    Originally Posted by Nik
    whats the difference between:

    if '-' not in name + month + year:

    and this

    if '-' not in ( name and month and year):

    can you explain please?
    Well, with strings, “+” concatenates the values, “and” tests whether the values are different from the empty string. As Mekire has patiently tried to explain, “name and month and year” evaluates to True if the variables name, month and year are non-empty strings and False if any of them is the empty string.
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (mini laptop, work laptop), Android 4.2.1 (tablet)
  22. #12
  23. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,280
    Rep Power
    13
    Tahnk you all ill decide to use this: if '-' not in name + month + year:

    So all 3 strings become one nig string and then the '-' is checked for existance in this big string right?

    can you please also explain

    if '-' not in ( name or month or year) in plain english?
    What is now proved was once only imagined!
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    3
    Originally Posted by Nik
    if '-' not in ( name or month or year) in plain english?
    (name or month or year) is evaluated, it will result in one of the three strings (the first non-empty one, or the last one if they're all empty). Then it will check if "-" is in that string.
  26. #14
  27. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,280
    Rep Power
    13
    Code:
    >>> name="abcd"
    >>> month="efgh"
    >>> year="ijkl"
    
    >>> print(name or month or year)
    abcd
    
    Can understand that, it takes the first string out of the 3 strings that has a truthy value.
    
    >>> print("k" in (name and month and year))
    True
    
    No clue. since the expression in parenthesis returns 'abcd' how can 'k' contained within 'abcd' ?
    
    >>> print(name and month and year)
    ijkl
    
    Seems here is returning the last string out of 3 strings, but have no clue why Python doing this.
    
    >>> print("k" in (name and month and year))
    True
    >>>
    
    yes, since expression returns 'ijkl', then the in operator can detect the 'k' character within the returned string.
    Please care to epxlain to me.
    What is now proved was once only imagined!
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    2
    Originally Posted by Nik
    Code:
    >>> print(name or month or year)
    abcd
    
    Can understand that, it takes the first string out of the 3 strings that has a truthy value.
    
    >>> print("k" in (name and month and year))
    True
    
    No clue. since the expression in parenthesis returns 'abcd' how can 'k' contained within 'abcd' ?
    You aren't being careful. You typed or the first time and and the second time. Of course your results weren't consistent.

    Honestly this is just how and and or work in python. At your level you don't need to think about why they return certain values. Think of it as returning True or False. There are practical uses to it returning the value it does and not a Boolean, but they aren't going to help you here.

    -Mek
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo