Friday, October 30, 2015

Feynman Diagrams in Tikz

There is a package for making Feynman diagrams in LaTeX. Unfortunately, it is old and dvi latex only. If you are using pdflatex or lualatex, as you should be, it does not work. Even in regular LaTeX, it's a bit of a pain. Why is there not a new package for pdflatex? Turns out, you don't need one. Due to the powerful drawing library Tikz, you can create any diagram easily, and can customize it completely. For example:

Monday, October 19, 2015

Including CRY cosmic ray generator in CMake

I realized that CRY did not have a CMake based install option, so including it in a GEANT4 cmake project might not be obvious. This is how you would do it in your CMakeLists.txt:

Wednesday, October 7, 2015

GTest Submodule

Note: There is a better way to do this described here.

If you've ever tried apt-get or brew to try to install gtest, you arer probably familiar with the fact that gtest is not "recommend" for global install on your system. As an alternitive, the recommendation is that you make it part of your project. The process for making gtest part of your project, however, is not well documented, at least for modern git projects. What follows is the procedure I used to do so.

Thursday, August 6, 2015

Slots in Python

Slots seem to be poorly documented. What they do is simple, but whether they are used is tricky. This is a little mini-post on slots.

Basics of Metaclasses

This is a quick tutorial over the basics of what metaclasses do.

The Metaclass

Metaclasses, while seemingly a complex topic, really just do something very simple. They control what happens when you have code that turns into a class object. The normal place they are executed is right after the class statement. Let's see that in action by using print as our metaclass.

Note: this post uses Python 3 metaclass notation. Python 2 uses assignment to a special __metaclass__ attribute to set the metaclass. Also, Python 2 requires explicit, 2 argument super() calls.

In [13]:
class WillNotBeAClass(object, metaclass=print):
    x=1
WillNotBeAClass (<class 'object'>,) {'x': 1, '__module__': '__main__', '__qualname__': 'WillNotBeAClass'}

Here, we have replaced the metaclass (type) with print, just to investigate how it works. This is quite useless, of course, but does show that the metaclass gets called with three arguments when a class is created. The first is the name of the class to be created, the second is a tuple of base classes, and the third is a dictionary that has the namespace of the body of a class, with a few extra special values added.

Wednesday, July 29, 2015

Factory Classmethods in Python

I haven't seen a great deal of practical documentation about using classmethods as factories in Python (which is arguably the most important use of a classmethod, IMO). This post hopes to fill in that gap.

Friday, July 24, 2015

Making a Plumbum Autoload Extension for IPython

I recently decided to try my hand at making an auto-load extension for Python and plumbum. I was planning to suggest it as a new feature, then I thought it might be an experimental feature, and now it's just a blog post. But it was an interesting idea and didn't seem to be well documented process on the web. So, here it is.

The plan was to make commands like this:

>>> from plumbum.cmd import echo
>>> echo("This is echoed!")

or

>>> from plumbum import local
>>> echo = local['echo']
>>> echo("This is echoed!")

into this:

>>> echo("This is echoed!")

Thereby making Plumbum even more like Bash for fast scripting.