Upgrade to Pro — share decks privately, control downloads, hide ads and more …

One, two, buckle my shoe

One, two, buckle my shoe

Programming is more than just typing, and takes more than talent. Producing software takes time, dedication, and teamwork. This talk gives guiding points for those learning to program and a demonstration of setting up a Flask web app.

Kat Chuang

August 16, 2015
Tweet

More Decks by Kat Chuang

Other Decks in Programming

Transcript

  1. What this talk is about ▪ Compare experience of coding

    in different languages. – Python vs Haskell – Objected oriented vs Functional programming ▪ Share insights to things I’ve learned – Right-brained, visual approaches – It’s important to publish readable code – It’s possible to code with only text editor – no IDEs!
  2. A little bit about me ▪ Pythonista – Research &

    data science – Web development ▪ NYC Python & NYC PyLadies organizer ▪ Now dabbling in Haskell too! – Functional Programming FTW! Want to learn data science? Take my online course! J https://teamtreehouse.com/signup_code/DrKat
  3. Main Points 1. Plumbing is usually boring. 2. There's no

    I in team. 3. A year from now you'll wish you started today. 4. Demo
  4. Python is an Object-Oriented language Object Oriented Programming (OOP) ▪

    This is noun-orientation, objects are classes that have attributes. – Good when you have a fixed set of operations on things Real World Analogy ▪ Sneaker – Material – Size – Brand – calculatePrice – calculateMileage ▪ Sandal – Material – Size – Brand – calculatePrice – calculateMileage
  5. Think more abstractly with functions ▪ Here are the pitfalls

    of OOP: – Adding new operations require editing many classes – Error prone ▪ Advice: write objected oriented code in the style of functions, then compose them As long as your items fit in the bins….
  6. Example def my_greeting(): return ‘Hi’ def reverse_list(lst): lst = lst[::-1]

    def do_my_stuff(lst): print(my_greeting) reverse_list(lst)
  7. Focusing on functions ▪ Write with functions ▪ Functions are

    easier to compose – Functions should not have side effects – same output every time – Functions should be limited in scope and functionality – keep it simple ▪ Example: def reversed(numbers): new_list = list(reversed(numbers)) return new_list
  8. Recap: • Look at the pipe connections • It is

    like a built in unit test for type errors
  9. Unreadable code slows you down… ▪ Handing off code to

    teammate or client and no documents ▪ Leaving a project for someone else taking over ▪ Coming back to code you wrote years ago ▪ Do you really want to spend hours figuring out if a bug is just a typo?
  10. Which is easier to read? def reverse_list(lst): lst = lst[::-1]

    def swap_ends(lst): lst[0], lst[-1] = lst[-1], lst[0] def greeting(): return ‘Hi’ def do_my_stuff(lst): reverse_list(lst) swap_ends(lst) lst.insert(0, greeting)) def mutate(lst): lst = lst[::-1] lst[0], lst[-1] = lst[-1], lst[0] lst.insert(0, ‘Hi’) if isinstance(lst[0], str): lst[0] = lst[0][::-1] elif isinstance(lst[0], (float, int)): lst[0] = lst[0] // 2 return lst
  11. How to write more readable code ▪ PEP 8 style

    guide https://www.python.org/dev/peps/pep-0008/ – Guidelines for indentation, line lengths, commenting ▪ Keep your code simple, consider it poetry ▪ Writing simple functions helps you manage complexity better ▪ Functions should not have side effects – it should give the same output every time ▪ Perform assertions of types
  12. Remember… ▪ Working on any large project is similar to

    gardening. ▪ Aim for done first, not perfect. ▪ You don’t need fancy tools
  13. All you need is a text editor Lots of low

    cost options ▪ Sublime Text http://www.sublimetext.com/ ▪ Vim https://github.com/macvim-dev/macvim ▪ Emacs http://emacsformacosx.com/ ▪ PyCharm https://www.jetbrains.com/pycharm/ Combine a text editor with code searching functionality ▪ Silver Searcher http://geoff.greer.fm/ag/
  14. Where to learn programming? ▪ Books – Learn Python the

    Hard Way http://learnpythonthehardway.org/book/ ▪ Videos – Treehouse http://teamtreehouse.com ▪ Local User Groups – NYC Python Study Groups and Office Hours http://learn.nycpython.org
  15. Recap • Plumbing is usually boring. But when it works,

    we don’t worry. • Coding is not the same as regurgitating document specifications. It’s about building something usable. • It’s important to have the ability to reason about types.
  16. Let’s make a dashboard of the latest Yeezy sneakers sold

    on eBay… Why sneaker analytics? http://fivethirtyeight.com/features/you-see-sneakers-these-guys-see-hundreds-of-millions-in-resale-profit/
  17. Steps 1. Connect the eBay API 2. Get the Image

    URLs 3. Get the prices and calculate average 4. Send these values to the template(s) 5. Ta-Da!
  18. 1. Connect the eBay API ▪ pip install ebaysdk ▪

    Connect the API api = Connection(appid=settings.api_secret, config_file=None) response = api.execute('findItemsAdvanced', {'keywords’: ‘Yeezy’}) items = response.reply.searchResult.item
  19. 2. Get the Image Urls def get_thumb(item): try: assert(type(item.galleryPlusPictureURL)) return

    item.galleryPlusPictureURL except: try: assert(type(item.galleryURL)) return item.galleryURL except: return ””
  20. 3. Get the prices and calculate average def get_price(item): return

    item.sellingStatus.currentPrice.value def avg(my_list): stats = pd.DataFrame(my_list, columns=['Name', 'Img', 'Price’, 'Link']) stats[['Price']] = stats[['Price']].astype(float) avg_shoe = stats[['Price']].mean() total = stats[['Price']].sum()
  21. 4. Send these values to the template(s) shoe = [title=item.title,

    currentPrice=get_price(item), galleryURL=get_thumb(item))] context = {“shoe”: shoe} return render_template("index.html", **context)