Giter Club home page Giter Club logo

python3-coding-exercise's Introduction

python3-coding-exercise

I was given the following exercise: Write a ~10 lines long function in Python 3 that you would consider as ideal code, and explain why it is ideal?

Note that I went straight to docs.python.org for tutorials as I have never coded in Python before. I am a quick study, and have looked it over in the past and found it to borrow elements from many other languages that I have written in, including Perl, PHP, and CoffeeScript.

Please find herein a simple function that is exactly ten lines as my submission of "ideal". This can be executed simply with python3 api_client.py. I chose to use a mini-representation of something that would be useful in the world of back-end systems automation and integration: a script that may receive some data from one source and pass it to another destination by way of a RESTful HTTP API interface.

I used this opportunity to explore python3 beyond simply writing a function, but also to understand code structure, imports, CLI invocation, Class structures/methods, possible approaches to dependency injection, iteration, http request/response handling, regular expression string processing, and formatting output for print/logging.

To me, the "ideal" aspects of the send_events() function are:

  • Comments that express intent rather than simply reword the code statements
  • Concise code that eliminates redundant statements without being too clever or difficult to read
  • A single-purpose function that declares what it is going to do and does nothing more
  • Scope-contained configuration (apihost) that eliminates the use of any sort of globals
  • Error checking/handling/logging for observability so that problems will not go unnoticed
  • Compiled regular expression that is reused for each event processed to reduce overhead

Had this been a real application, there would have been much more to this including dependency injection, configuration management, indirect output through some log/result/event stream, setup and teardown, unit test coverage, and packaging as a reusable module ready for an application to use the ApiClient.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.