Partition Functions and Invariant States in Two-dimensional and Nearly Two-dimensional Turbulence