anthonyw12123 / 4chanwebscraper

A primitive web scraper using Python.
MIT License
0 stars 0 forks source link

Encapsulate code #13

Closed cdsimpkins closed 8 years ago

cdsimpkins commented 8 years ago

For one-off scripts, python is a great option for quick and dirty code. For something you plan on re-using and extending into the distant future, you should use standard practices of code encapsulation to logically organize your methods and make it easier to add functionality as you see fit.

In python, this generally involves defining a "main" method (name is arbitrary), and defining an entry point into your application as follows:

if name == '__main__': main()

the name variable is a reserved variable that gives python some information on how the application was invoked. it allows you to define "startup procedures" when the code is run directly, but also makes it behave more predictably when the code is imported. As it is now, if you were to import your class into another class, all of your code would run on import, which may not be the desired action.

Edit: You should also move related code under functions. i.e. "save a file" function, "download something from the internet" function (this function can be called whether you're returning HTML or binary data), and so on. It allows for more flexible code re-use.