Showing posts from February, 2017

Playing with monads in Python, 1

There are a number of topics in computer science that I have on my list of `stuff to understand at some point'. One of them is monads, which originally perplexed me when I came across Peter Thatcher's article about a year ago.

My favorite way to structure the code is to start with a base class Monad, which requires any subclass (type of monad we're defining, that is) to define methods bind and unit.
from abc import abstractmethod from types import FunctionType as Function class Monad: @abstractmethod def bind(self, f: Function) -> 'Monad': raise NotImplementedError def __rshift__(self, f: Function) -> 'Monad': return self.bind(f) @classmethod @abstractmethod def unit(cls, *args) -> 'Monad': raise NotImplementedError As Thatcher wrote in his own implementation, I've overloaded the special method __rshift__ to mean bind, which gets us closer to Haskell's syntax (>>=).

I liked…

Professional Developer Tools for Students from JetBrains

If you're a student like me, you should definitely head over to JetBrains' site and sign up for their free professional developer pack. It grants you a year-long subscription to their products.

I've downloaded both PyCharm and CLion, and I look forward to using the latter more frequently. I've enjoyed using Vim, but the benefits of using these IDEs are that they teach you about the language as you're going along. PyCharm doesn't catch every bug in my code, but it catches nearly all minor issues, and many mid-level package issues too. I hope CLion will help as I learn more about C too.

Clean simple FITS data

This won't be a lengthy discussion about methods to clean amateur astronomical FITS data; instead, since I've written about it before (albeit when I didn't know very much about Python), I'd rather post some new code I wrote to do the same process in a much clearer way.

#! /usr/bin/env python3.6 # -*- coding: utf-8 -*- """ Reduce a quick batch of FITS data. Currently run with input file list /home/brandon/Downloads/Astronomy_Data/V1283_Her/fixed_gain_V1283_Her_*.fit /home/brandon/Downloads/Astronomy_Data/V1283_Her/Dark-60s_*.fit /home/brandon/Downloads/Astronomy_Data/V1283_Her/Flat-V_*.fit -o _Finished_Feb_08_2016 """ from typing import Generator, Tuple from import fits as FITS from import Header as HDU from argparse import ArgumentParser from glob import iglob from datetime import datetime import numpy as np _EXT = 'fits' def get_fits_file(name: str, axis: int =0) -> Tuple[np.ndarray, HDU]: &q…