Skip to content

8th December 2021

Coding

Python

GitPython

Python Snippets

  • New: Capture the stdout of a function.

    import io
    from contextlib import redirect_stdout
    
    f = io.StringIO()
    with redirect_stdout(f):
        do_something(my_object)
    out = f.getvalue()
    
  • New: Make temporal directory.

    import tempfile
    
    dirpath = tempfile.mkdtemp()
    
  • New: Change the working directory of a test.

    import unittest
    import os
    
    from src.main import get_cwd
    
    class TestMain(unittest.TestCase):
    
        def test_get_cwd(self):
            os.chdir('src')
            print('testing get_cwd()')
            current_dir = get_cwd()
            self.assertIsNotNone(current_dir)
            self.assertEqual(current_dir, 'src')
    

Poetry

DevOps

Continuous Integration

Dependency managers

  • Correction: Deprecate in favour of Poetry.