<commit_before># coding: utf-8

""" Read and write potentials to text (YAML) files. """

from __future__ import division, print_function

__author__ = "adrn <<EMAIL>>"

# Standard library
import os

# Third-party
import astropy.units as u
import yaml

# Project
from .. import potential

__all__ = ['read', 'write']

def from_dict(d):
    """
    Convert a dictionary potential specification into a
    :class:`~gary.potential.Potential` object.

    Parameters
    ----------
    d : dict
        Dictionary specification of a potential.

    """

    try:
        class_name = d['class']
    except KeyError:
        raise KeyError("Potential dictionary must contain a key 'class' for "
                       "specifying the name of the Potential class.")

    try:
        unitsys = [u.Unit(unit) for unit in d['units']]
    except KeyError:
        raise KeyError("Potential dictionary must contain a key 'units' with "
                       "a list of strings specifying the unit system.")

    if 'parameters' in d:
        params = d['parameters']
    else:
        params = dict()

    Potential = getattr(potential, class_name)

    return Potential(units=unitsys, **params)

def read(f):
    """
    Read a potential specification file and return a
    :class:`~gary.potential.Potential` object instantiated with parameters
    specified in the spec file.

    Parameters
    ----------
    f : str, file_like<commit_msg>add float parser because pyyaml sux at scientific notation<commit_after># coding: utf-8

""" Read and write potentials to text (YAML) files. """

from __future__ import division, print_function

__author__ = "adrn <<EMAIL>>"

# Standard library
import os

# Third-party
import astropy.units as u
import yaml

# Project
from .. import potential

__all__ = ['read', 'write']

def from_dict(d):
    """
    Convert a dictionary potential specification into a
    :class:`~gary.potential.Potential` object.

    Parameters
    ----------
    d : dict
        Dictionary specification of a potential.

    """

    try:
        class_name = d['class']
    except KeyError:
        raise KeyError("Potential dictionary must contain a key 'class' for "
                       "specifying the name of the Potential class.")

    try:
        unitsys = [u.Unit(unit) for unit in d['units']]
    except KeyError:
        raise KeyError("Potential dictionary must contain a key 'units' with "
                       "a list of strings specifying the unit system.")

    if 'parameters' in d:
        params = d['parameters']
    else:
        params = dict()

    # Hack because PyYAML sux
    for k,v in params.items():
        params[k] = float(v)

    Potential = getattr(potential, class_name)

    return Potential(units=unitsys, **params)

def read(f):
    """
    Read a potential specification file and return a
    :class:`~gary.potential.Potential` object instantiated with parameters
    specified in the spec file.

    Parameters
    ----------
    f : str, file_like
{'fraction_non_alphanumeric': 0.10493004663557629, 'fraction_numerical': 0.0006662225183211193, 'mean_word_length': 2.528789659224442, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4782699', 'n_tokens_mistral': 874, 'n_tokens_neox': 812, 'n_words': 301}