import io
import re
import math


def mem_per_core(units='kB'):
    """returns memory per core in kb (default) or another desired unit"""
    with io.open('/proc/meminfo') as f:
        lines = [x for x in f.readlines() \
                     if re.search('MemTotal', x) is not None]
    mem = float(lines[0].split()[1]) # memory in kb
    with io.open('/proc/cpuinfo') as f:
        lines = [x for x in f.readlines() \
                     if re.search('processor', x) is not None]
    ncores = len(lines)
    mpc = mem / ncores
    if units.lower() == 'mb':
        mpc *= 1e6 / 1e9 # MB to kB
    return int(math.floor(mpc))