3 Replies - 362 Views - Last Post: 21 January 2013 - 04:21 PM Rate Topic: -----

#1 brovall9k  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 20-January 13

Am I using proper code structure

Posted 20 January 2013 - 05:36 PM

#!/usr/bin/env python

import webbrowser

class Tors:
        def getSearch(self):
            #Gets the search that the user is looking for
            u_input = raw_input('Enter search: ')
            return u_input

        def getEngine(self):
            #Gets the engine the user would like to use to search
            engine  = raw_input('Enter engine: ')
            return engine

        def setEngine(self,eng):
            #Converts the users engine input to lowercase; Useful for error prevention. Reduces the number of possibilites for the if statements
            #Does nothing if eng is already lowercase
            eng = eng.lower()
	    #Sets eng to the site specified by the user in getEngine()
            #Resulting eng will be modified to url_eng
            #All sites have a "TEMP", symbolizing the search from getSearch()
            if eng == 'tpb' or eng == 'thepiratebay':
                url_eng = 'http://thepiratebay.se/search/TEMP/0/77/0'
            elif eng == 'kat'or eng == 'kickasstorrents':
                url_eng = 'http://kat.ph/usearch/TEMP/'
            elif eng == 'torrentz' or eng == 'tz':
                url_eng = 'http://torrentz.eu/search?f=TEMP'
            elif eng == 'youtube' or eng == 'ytube' or eng == 'yt':
                url_eng = 'http://www.youtube.com/results?search_query=TEMP'
            elif eng == 'google' or eng == 'goo' or eng == 'goog':
                url_eng = 'http://www.google.com/#hl=en&tbo=d&output=search&sclient=psy-ab&q=TEMP'
            else:
                print(eng+': is not an engine currently supported. To add an engine you will need to modify the script yourself')
            return url_eng

        def setUrl(self,torEng,u_in):
	    #Replaces the "TEMP" with the users input from getSearch()
            #This results in the last modification to the url
            full_url = torEng.replace('TEMP',u_in)
            return full_url

        def navigateUrl(self,complete_url):
            #Displays the complete_url to the screen
            #Uses the webbrowser function to navigate to the fully modified url (complete_url)
            print('Search Url: '+complete_url)
            webbrowser.open_new_tab(complete_url)

#Creates a Tors() object capable of accesing Tors() functions
#This allows the script to access functions inside the Tors() class
newSearch = Tors()
#The Users Input
userIn = newSearch.getSearch()
#The Users Engine Input
tEng = newSearch.getEngine()
#The Temporary Url (http://thepiratebay.se/search/TEMP/0/77/0) to set the engine
tempUrl = newSearch.setEngine(tEng)
#The modified url that replaces TEMP with the users input from getSearch()
modUrl = newSearch.setUrl(tempUrl,userIn)
#Navigates the the "modUrl". Which is the site followed by the serach provided
newSearch.navigateUrl(modUrl)



Is This A Good Question/Topic? 0
  • +

Replies To: Am I using proper code structure

#2 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Am I using proper code structure

Posted 21 January 2013 - 03:56 PM

What is proper code structure? I think everyone will vary on this point, but perhaps I can give you a few pointers that *I* think is proper code structure.

Firstly, all the getX functions aren't really get functions at all, they are get and set in one. If I was writing some code with getters and setters, it would look like this:

class MyClass:
    def __init__(self): # Constructor
      self.varA = "" # Some variables set to default values
      self.varB = 0  
    
    def setA(self, a):
       self.varA = a

    def getA(self):
        return self.varA
    
    def setB(self, B)/>:
        self.varB = b
     
    def getB(self):
        return self.B


See how there are separate methods for getting and setting values? Also, I never ask for raw_input within the class, I don't think you should do it in them methods, if you have to, make a new method to ask the user, or do it externally.

For your setEng method, you really expect a user to type in them URLs exactly? Even if a single forward slash was missed off the end, the equality would fail, even if it was a valid URL. You should either give users a choice which URL to use, or take a look at regular expressions to parse input.

This post has been edited by Simown: 21 January 2013 - 04:02 PM

Was This Post Helpful? 0
  • +
  • -

#3 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,682
  • Joined: 13-March 10

Re: Am I using proper code structure

Posted 21 January 2013 - 04:06 PM

A class should encapsulate a state. In your case, it could be simply the search and engine. Only after you get these values, you instantiate the object and do some stuff to it.
Was This Post Helpful? 0
  • +
  • -

#4 brovall9k  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 20-January 13

Re: Am I using proper code structure

Posted 21 January 2013 - 04:21 PM

Thanks guys. This is really helpful. I knew about constructor functions, but didn't know how to go about scripting one.And this program is merely something for me to build off of. I really want it to be able to recognize websites based off of the name alone. Say the user enters "google" I want it to be able to go out and find the url itself...and add it to a file which saves all the so called search engines for later use...I also want it to be able to return results from the web and have them printed on the screen. With the option to navigate to one or more of the results sites. I really appreciate all the help guys. I am definitely going to change the program and build a constructor function.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1