Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

8.14.2016

Using py.test with Django and Hypothesis

If you're looking to use py.test as your test runner, but still want Hypothesis to correctly work with Django (i.e. make sure the database is in a clean state before each example is tested), use something like this:

class PytestHypothesisBase:
    def start_atomic_example(self):
        self.atomic = transaction.atomic(savepoint=True)
        self.atomic.__enter__()

    def setup_example(self):
        self.start_atomic_example()

    def finish_atomic_example(self):
        transaction.set_rollback(True)
        self.atomic.__exit__(None, None, None)

    def teardown_example(self, ex):
        self.finish_atomic_example()

Hypothesis will run setup_example and teardown_example even when using py.test. This is nice because while inheriting from hypothesis.extra.django.TestCase works with py.test, it causes dependency injection to break, meaning you can't use py.test's nice fixture behaviors. This approach lets you use the @given decorator with py.test fixtures.

9.22.2011

IPython dependencies

IPython has some nifty features, but since IPython is still in development, they are awfully hard to get running, at least on OS X. Compounding this is the dependency on Qt for the cool new features to work. Qt isn't the most seamless package to get installed and running. I'm not complaining at all. I just want to list all the steps that I went through to get the Qt console working.

  1. Try starting the IPython qtconsole:
    python qtconsole
    and learn that I'm missing all kinds of dependencies.
  2. Try to install pyqt using pip:
    pip install pyqt
    and learn that the egg for pyqt is broken.
  3. Try to build pyqt using the files that pip installed
    python configure.py
    and learn that pyqt depends on sip.
  4. Try to install sip using pip(!):
    pip install sip
    and learn that the sip egg is broken in the same way as pyqt
  5. Install sip using the files that pip downloaded:
    python configure.py
    make
    make install

    This actually works.
  6. Try to build pyqt again:
    python configure.py
    and learn that I need the Qt SDK. This one is my fault.
  7. Install Qt from the Nokia website
  8. Repeat step 6, find that for some reason qmake was not installed.
  9. Uninstall Qt
  10. Uninstall sip
  11. Install Homebrew
  12. Try to install pyqt:
    brew install pyqt
    which successfully installs Qt, but fails because it can't link to the right version of Python, which I have somehow accumulated 4 different installations of
  13. Install python through homebrew:
    brew install python
    This fails sip has hard coded the location of the old, broken framework.
  14. Uninstall sip.
    brew remove sip
  15. Try to install pyqt:
    brew install pyqt
    This still fails. No idea why.

8.28.2011

MATLAB may not be completely terrible

I use MATLAB a lot, but I wish I could use it less. I have a few reasons for not liking it:
  • I prefer to use VIM and the command line for everything. The MATLAB IDE isn't my favorite.
  • It's closed source (not optimal, but not necessarily a deal-breaker) and expensive (unless you're a student).
  • Lots of useful code is sold as addons. The parallel computing toolbox, for example, is not included in the standard version of MATLAB.
  • The language itself isn't as nice as Python is, at least for me. Language choice is a highly personal matter, however.
For these reasons, I am trying to install Numeric Python as a replacement for MATLAB. So far, it's a huge struggle. The value in having a working product out of the box is not insignificant. Python support on OS X isn't as strong as could be hoped, and the recent upgrade to Lion has also broken some dependencies. I am eschewing pre-built binaries for locally compiled versions to get around these issues. I'll update this post with resources.