# -*- coding: utf-8 -*-

import re
import math
from gmisclib import nicknames

JunkPhone = 'junk'

class BadPhonesError(ValueError):
	def __init__(self, *s):
		ValueError.__init__(self, *s)



def make_nicknames(namelist):
	"""Shorten filenames to make unique nicknames.
	This takes a list of names and returns a mapping from a
	nickname to (or from?) the full name of the file.
	"""
	_p = re.compile('[^a-zA-Z0-9_+-]')
	nn = nicknames.nicknames(namelist)[0]
	o = {}
	for (k,v) in nn.items():
		o[k] = _p.sub('_', v)
	return o


def add_to_dict(data, **kv):
	"""Add some keys to each dictionary in a list of dictionaries."""
	o = []
	for datum in data:
		dd = datum.copy()
		dd.update(kv)
		o.append(dd)
	return o


def occasionally(i):
	return round(math.sqrt(i)) != round(math.sqrt(i+1))
