python

Python: best practices

Table of Contents

testing pythontesting

nose is abandoned pythontesting

pytest is the best apparently pythontestingpytest

  • uses the built in assert

    assert 1 == 1, 'This is my error message, which is optional'
    
  • parameterized tests
  • fixtures: These aren't the setup/teardown fixtures (per-se), but more like dependency injection (and are incredibly useful). You can even inject a fixture into a fixture (say you need to log in a user, but you need an HTTP client for it to work, you probably use that client elsewhere so DRY it up):
  • markers: Easy way to categorize tests (and coincidentally run only those groups)

    @pytest.mark.slow
    def test_slow_running_thing():
        time.sleep(500)
    
  • parallel execution via xdist

logging python

simple python

import logging
logging.basicConfig(level=logging.INFO) # apparently callin basicconfig at least one is important
logging.exception('Caught an error' + str(e))

use logzero for nice colored logs python

json pythonjson

json.dump(jobj, stdout, indent=4, sort_keys=True, ensure_ascii=False) pythonjson

TODO asyncio python

[B] [2020-02-12] Guido van Rossum on Twitter: "@jonasrk You missed mypy. Simpler docs use markdown, not ReST (Sphinx). Black is overrated unless your team argues over style a lot. You don't need Pylint if you're using flake8. Never heard of poetry or dependabot. And you should use a CI solution, e.g. Travis-CI, to run your tests." / Twitter https://twitter.com/gvanrossum/status/1227126706089021440 python

You don't need Pylint if you're using flake8

hmm

[C] [2018-05-08] configuration via class variables – not great while testing since configuration has to be reloaded… python

TODO [C] write about setting up elaborate python project + circleci? maybe demonstrate on grasp? pythonci

CREATED: [2019-08-02]
Jump to search, settings & sitemap