<commit_before>"""
Unit system class.

"""

#-----------------------------------------------------------------------------
# Copyright (c) 2013, yt Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------

from yt.extern.six import string_types
from yt.units import dimensions
from yt.units.unit_object import Unit, unit_system_registry

class UnitSystem(object):
    def __init__(self, name, length_unit, mass_unit, time_unit,
                 temperature_unit, angle_unit, current_mks_unit=None,
                 unit_registry=None):
        self.units_map = {dimensions.length: length_unit,
                          dimensions.mass: mass_unit,
                          dimensions.time: time_unit,
                          dimensions.temperature: temperature_unit,
                          dimensions.angle: angle_unit}
        if current_mks_unit is not None:
            self.units_map[dimensions.current_mks] = current_mks_unit
        self.unit_registry = unit_registry
        self.base_units = self.units_map.copy()
        unit_system_registry[name] = self

    def __getitem__(self, key):
        if isinstance(key, string_types):
            key = getattr(dimensions, key)
        if key not in self.units_map:
            dims = key.expand()
            units = Unit("")
            for factor in dims.as_ordered_factors():
                dim = list(factor.free_symbols)[0]
                u = self.units_map[dim]
                if factor.is_Pow:
                    u = u ** factor.as_base_exp()[1]
                units *= u
            self.units_map[key] = units
        return self.units_map[key]

    def __setitem__(self, key, value):
        if isinstance(key, string_types):
            key = getattr(dimensions, key)
        self.units_map[key] = Unit(value, unit_registry=self.unit_registry)

def create_code_unit_system(ds):
    code_unit_system = UnitSystem(str(ds), "code_length", "code_mass", "code_time",
                                  "code_temperature", "radian")
    code_unit_system[dimensions.velocity] = "code_velocity"

cgs_unit_system = UnitSystem("cgs", "cm", "g", "s", "K", "radian")
cgs_unit_system[dimensions.energy] = "erg"
cgs_unit_system[dimensions.specific_energy] = "erg/g"
cgs_unit_system[dimensions.pressure] = "dyne/cm**2"
cgs_unit_system[dimensions.force] = "dyne"<commit_msg>Fixing bugs. Need to add the unit_system kwarg to all of the frontends (sigh)<commit_after>"""
Unit system class.

"""

#-----------------------------------------------------------------------------
# Copyright (c) 2013, yt Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------

from yt.extern.six import string_types
from yt.units import dimensions
from yt.units.unit_object import Unit, unit_system_registry

class UnitSystem(object):
    def __init__(self, name, length_unit, mass_unit, time_unit,
                 temperature_unit, angle_unit, current_mks_unit=None,
                 registry=None):
        self.units_map = {dimensions.length: length_unit,
                          dimensions.mass: mass_unit,
                          dimensions.time: time_unit,
                          dimensions.temperature: temperature_unit,
                          dimensions.angle: angle_unit}
        if current_mks_unit is not None:
            self.units_map[dimensions.current_mks] = current_mks_unit
        self.registry = registry
        self.base_units = self.units_map.copy()
        unit_system_registry[name] = self

    def __getitem__(self, key):
        if isinstance(key, string_types):
            key = getattr(dimensions, key)
        if key not in self.units_map:
            dims = key.expand()
            units = Unit("")
            for factor in dims.as_ordered_factors():
                dim = list(factor.free_symbols)[0]
                u = self.units_map[dim]
                if factor.is_Pow:
                    u = u ** factor.as_base_exp()[1]
                units *= u
            self.units_map[key] = units
        return self.units_map[key]

    def __setitem__(self, key, value):
        if isinstance(key, string_types):
            key = getattr(dimensions, key)
        self.units_map[key] = Unit(value, registry=self.registry)

def create_code_unit_system(ds):
    code_unit_system = UnitSystem(str(ds), "code_length", "code_mass", "code_time",
                                  "code_temperature", "radian")
    code_unit_system[dimensions.velocity] = "code_velocity"

cgs_unit_system = UnitSystem("cgs", "cm", "g", "s", "K", "radian")
cgs_unit_system[dimensions.energy] = "erg"
cgs_unit_system[dimensions.specific_energy] = "erg/g"
cgs_unit_system[dimensions.pressure] = "dyne/cm**2"
cgs_unit_system[dimensions.force] = "dyne"
{'fraction_non_alphanumeric': 0.1548018142378229, 'fraction_numerical': 0.0027607966870439754, 'mean_word_length': 2.035308198683423, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4391422', 'n_tokens_mistral': 1467, 'n_tokens_neox': 1429, 'n_words': 324}