User Controls

Twitpy, my first python script, a twitter bot.

  1. #1
    First time using python, so the structure of my code may see weird, anyway. It's not fully finishing, I've not added the loop, and some error management, but anyway:

    https://github.com/filtration/Twitpy/blob/master/twitpy.py
  2. #2
    Sophie Pedophile Tech Support
    Not bad, not bad at all. This hurts my eyeballs so please don't do it though.


    multiple = str(raw_input("Perform multiple actions, or just like tweet.. I.e: favourite, retweet. [y/n]"))


    Refractor to:


    multiple = raw_input("Question: Y/n: ")


    For option parsing you don't have to do all this string comparing magic, that's over thinking it. Instead do this:


    import sys

    choice = raw_input("Retry? Y/n: ")

    if "y" in choice:
    print "\nRetrying"
    Do_Something()
    elif "n" in choice:
    print "\nQuitting"
    sys.exit(0) # This can be whatever.
    else:
    print "\nUnhandled option, quitting"
    sys.exit(0) # Again just adding to fill space


    Reason my print statement is different than your is because i write Python 2.7 but the principles carry over. 3.x is cancer IMO but whatever.

    Also what does `try/except KeyError: 'text'` do?


    Oh wut.

    You ahve a print statements like: print("Cannot be empty") and later on one in your exception handling like: print 'liked '.

    Can't have it both ways nigga'.


    I would also advise you to always print strings with double quotes.

    Is your program cross compatible over 2.7 and 3.x? If not please use a shebang line for us Linux users.

    #!/usr/bin/env python2.7

    Or your version.

    But i am one to talk for i sin against the Gods of Linux on a regular basis, it's just best practice, same as with asterisk imports. I know you don't have them so good on you for that.

    All in all for your first python program pretty decent.
    The following users say it would be alright if the author of this post didn't die in a fire!
  3. #3
    Originally posted by Sophie Not bad, not bad at all. This hurts my eyeballs so please don't do it though.


    multiple = str(raw_input("Perform multiple actions, or just like tweet.. I.e: favourite, retweet. [y/n]"))


    Refractor to:


    multiple = raw_input("Question: Y/n: ")


    For option parsing you don't have to do all this string comparing magic, that's over thinking it. Instead do this:


    import sys

    choice = raw_input("Retry? Y/n: ")

    if "y" in choice:
    print "\nRetrying"
    Do_Something()
    elif "n" in choice:
    print "\nQuitting"
    sys.exit(0) # This can be whatever.
    else:
    print "\nUnhandled option, quitting"
    sys.exit(0) # Again just adding to fill space


    Reason my print statement is different than your is because i write Python 2.7 but the principles carry over. 3.x is cancer IMO but whatever.

    Also what does `try/except KeyError: 'text'` do?


    Oh wut.

    You ahve a print statements like: print("Cannot be empty") and later on one in your exception handling like: print 'liked '.

    Can't have it both ways nigga'.


    I would also advise you to always print strings with double quotes.

    Is your program cross compatible over 2.7 and 3.x? If not please use a shebang line for us Linux users.

    #!/usr/bin/env python2.7

    Or your version.

    But i am one to talk for i sin against the Gods of Linux on a regular basis, it's just best practice, same as with asterisk imports. I know you don't have them so good on you for that.

    All in all for your first python program pretty decent.





    multiple = str(raw_input("Perform multiple actions, or just like tweet.. I.e: favourite, retweet. [y/n]"))


    I was trying to stop them putting in an integer, but I already had a if multiple in... so I agree, that wasn't needed.


    Also what does `try/except KeyError: 'text'` do?

    Handles the tweepy/twitter error.



    You ahve a print statements like: print("Cannot be empty") and later on one in your exception handling like: print 'liked '.

    Can't have it both ways nigga'.

    lol. First one is to say their input cannot be empty when selecting if they want to favorite and retweet [y], and the print later on is to print out what status they have liked on twitter


    I would also advise you to always print strings with double quotes.

    Don't tell me what to do nigger lol


    Is your program cross compatible over 2.7 and 3.x? If not please use a shebang line for us Linux users.

    I dunno lol I use sublime and run ctrl+b to build, dunno how to test it in 3x


    Thanks for the tips.
    The following users say it would be alright if the author of this post didn't die in a fire!
  4. #4
    Sophie Pedophile Tech Support
    Originally posted by Iam

    multiple = str(raw_input("Perform multiple actions, or just like tweet.. I.e: favourite, retweet. [y/n]"))


    I was trying to stop them putting in an integer, but I already had a if multiple in… so I agree, that wasn't needed.




    Handles the tweepy/twitter error.





    lol. First one is to say their input cannot be empty when selecting if they want to favorite and retweet [y], and the print later on is to print out what status they have liked on twitter




    Don't tell me what to do nigger lol




    I dunno lol I use sublime and run ctrl+b to build, dunno how to test it in 3x


    Thanks for the tips.

    You did a pretty good job. You should see how C programmers write Python. Python always looks flowing to me, but if a C nigga writes python it becomes almost robotic, it's hard to explain.
  5. #5
    Sophie Pedophile Tech Support
    Also there is no need to build a python program, it's an interpreted language after all. You can just run your script from the command line as long as you have the interpreter.
  6. #6
    Originally posted by Sophie Also there is no need to build a python program, it's an interpreted language after all. You can just run your script from the command line as long as you have the interpreter.

    I mean it just builds it to run in the console of sublime. Instead of opening cmd all the time.
  7. #7
    Sophie Pedophile Tech Support
    Originally posted by Iam I mean it just builds it to run in the console of sublime. Instead of opening cmd all the time.

    Oh nice, i don't use sublime but that's a pretty cool feature. Geany on Linux kind of has the same where you have a terminal window and you can easily compile your program or run your script from there.
  8. #8
    I like pythons. It's logic really tickles my autism.
  9. #9
    Sophie Pedophile Tech Support
    Originally posted by yum I like pythons. It's logic really tickles my autism.

    I didn't know you could code python?
  10. #10
    Lanny Bird of Courage
    I don't think this does what you think it does:

     if not input:


    `input` is a builtin function so "not input" will never evaluate true. You might want `if not times:` instead.

    except KeyError: 'text'


    It's generally considered a bad practice to catch an exception and do nothing with it, but if you do have some code branch where you just need a statement to satisfy the parser python has the `pass` statement which is a noop.
    The following users say it would be alright if the author of this post didn't die in a fire!
  11. #11
    Sophie Pedophile Tech Support
    Originally posted by Lanny I don't think this does what you think it does:

     if not input:


    `input` is a builtin function so "not input" will never evaluate true. You might want `if not times:` instead.

    except KeyError: 'text'


    It's generally considered a bad practice to catch an exception and do nothing with it, but if you do have some code branch where you just need a statement to satisfy the parser python has the `pass` statement which is a noop.

    Now i know what this does.


    except KeyError: 'text'


    Nuffin'. Lol.
  12. #12
    Originally posted by Lanny I don't think this does what you think it does:

     if not input:


    `input` is a builtin function so "not input" will never evaluate true. You might want `if not times:` instead.

    except KeyError: 'text'


    It's generally considered a bad practice to catch an exception and do nothing with it, but if you do have some code branch where you just need a statement to satisfy the parser python has the `pass` statement which is a noop.


    Yeah, when I wasn't catching it broke my code, so has to catch it, and I didn't wanna stop the loop on an exception so just left it empty :/
Jump to Top