Skip to content

Commit

Permalink
Remove OrderedDict/odict (conda#12254)
Browse files Browse the repository at this point in the history
  • Loading branch information
kenodegard authored Jan 20, 2023
1 parent 4df3f78 commit 81acff3
Show file tree
Hide file tree
Showing 28 changed files with 539 additions and 538 deletions.
83 changes: 38 additions & 45 deletions conda/base/context.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# Copyright (C) 2012 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause

from collections import OrderedDict

from errno import ENOENT
from functools import lru_cache
from itertools import chain
Expand Down Expand Up @@ -49,7 +46,7 @@
from ..auxlib.ish import dals
from .._vendor.boltons.setutils import IndexedSet
from .._vendor.frozendict import frozendict
from ..common.compat import NoneType, odict, on_win
from ..common.compat import NoneType, on_win
from ..common.configuration import (Configuration, ConfigurationLoadError, MapParameter,
ParameterLoader, PrimitiveParameter, SequenceParameter,
ValidationError)
Expand Down Expand Up @@ -668,35 +665,33 @@ def signing_metadata_url_base(self):

@property
def conda_exe_vars_dict(self):
'''
An OrderedDict so the vars can refer to each other if necessary.
"""
The vars can refer to each other if necessary since the dict is ordered.
None means unset it.
'''
"""

if context.dev:
return OrderedDict(
[
("CONDA_EXE", sys.executable),
(
"PYTHONPATH",
# [warning] Do not confuse with os.path.join, we are joining paths
# with ; or : delimiters.
os.pathsep.join((CONDA_SOURCE_ROOT, os.environ.get("PYTHONPATH", ""))),
),
("_CE_M", "-m"),
("_CE_CONDA", "conda"),
("CONDA_PYTHON_EXE", sys.executable),
]
)
return {
"CONDA_EXE": sys.executable,
# do not confuse with os.path.join, we are joining paths with ; or : delimiters
"PYTHONPATH": os.pathsep.join(
(CONDA_SOURCE_ROOT, os.environ.get("PYTHONPATH", ""))
),
"_CE_M": "-m",
"_CE_CONDA": "conda",
"CONDA_PYTHON_EXE": sys.executable,
}
else:
bin_dir = 'Scripts' if on_win else 'bin'
exe = 'conda.exe' if on_win else 'conda'
# I was going to use None to indicate a variable to unset, but that gets tricky with
# error-on-undefined.
return OrderedDict([('CONDA_EXE', os.path.join(sys.prefix, bin_dir, exe)),
('_CE_M', ''),
('_CE_CONDA', ''),
('CONDA_PYTHON_EXE', sys.executable)])
return {
"CONDA_EXE": os.path.join(sys.prefix, bin_dir, exe),
"_CE_M": "",
"_CE_CONDA": "",
"CONDA_PYTHON_EXE": sys.executable,
}

@memoizedproperty
def channel_alias(self):
Expand Down Expand Up @@ -731,42 +726,40 @@ def custom_multichannels(self):
if self.restore_free_channel:
default_channels.insert(1, 'https://repo.anaconda.com/pkgs/free')

reserved_multichannel_urls = odict((
(DEFAULTS_CHANNEL_NAME, default_channels),
('local', self.conda_build_local_urls),
))
reserved_multichannels = odict(
(name, tuple(
Channel.make_simple_channel(self.channel_alias, url) for url in urls)
) for name, urls in reserved_multichannel_urls.items()
)
custom_multichannels = odict(
(name, tuple(
Channel.make_simple_channel(self.channel_alias, url) for url in urls)
) for name, urls in self._custom_multichannels.items()
)
return odict(
(name, channels)
reserved_multichannel_urls = {
DEFAULTS_CHANNEL_NAME: default_channels,
"local": self.conda_build_local_urls,
}
reserved_multichannels = {
name: tuple(Channel.make_simple_channel(self.channel_alias, url) for url in urls)
for name, urls in reserved_multichannel_urls.items()
}
custom_multichannels = {
name: tuple(Channel.make_simple_channel(self.channel_alias, url) for url in urls)
for name, urls in self._custom_multichannels.items()
}
return {
name: channels
for name, channels in (
*custom_multichannels.items(),
*reserved_multichannels.items(), # order maters, reserved overrides custom
)
)
}

@memoizedproperty
def custom_channels(self):
from ..models.channel import Channel

return odict(
(channel.name, channel)
return {
channel.name: channel
for channel in (
*chain.from_iterable(channel for channel in self.custom_multichannels.values()),
*(
Channel.make_simple_channel(self.channel_alias, url, name)
for name, url in self._custom_channels.items()
),
)
)
}

@property
def channels(self):
Expand Down
20 changes: 9 additions & 11 deletions conda/cli/main_info.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# Copyright (C) 2012 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause

from collections import OrderedDict
import json
from logging import getLogger
import os
Expand Down Expand Up @@ -57,15 +55,15 @@ def dump_record(pkg):
def pretty_package(prec):

pkg = dump_record(prec)
d = OrderedDict([
('file name', prec.fn),
('name', pkg['name']),
('version', pkg['version']),
('build string', pkg['build']),
('build number', pkg['build_number']),
('channel', str(prec.channel)),
('size', human_bytes(pkg['size'])),
])
d = {
"file name": prec.fn,
"name": pkg["name"],
"version": pkg["version"],
"build string": pkg["build"],
"build number": pkg["build_number"],
"channel": str(prec.channel),
"size": human_bytes(pkg["size"]),
}
for key in sorted(set(pkg.keys()) - SKIP_FIELDS):
d[key] = pkg[key]

Expand Down
8 changes: 4 additions & 4 deletions conda/common/_os/linux.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# Copyright (C) 2012 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause

from collections import OrderedDict
from functools import lru_cache
from genericpath import exists
from logging import getLogger
Expand All @@ -26,8 +24,10 @@ def linux_get_libc_version():
# Python 2.7 does not have either of these keys in confstr_names, so provide
# hard-coded defaults and assert if the key is in confstr_names but differs.
# These are defined by POSIX anyway so should never change.
confstr_names_fallback = OrderedDict([('CS_GNU_LIBC_VERSION', 2),
('CS_GNU_LIBPTHREAD_VERSION', 3)])
confstr_names_fallback = {
"CS_GNU_LIBC_VERSION": 2,
"CS_GNU_LIBPTHREAD_VERSION": 3,
}

val = None
for k, v in confstr_names_fallback.items():
Expand Down
2 changes: 1 addition & 1 deletion conda/common/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def isiterable(obj):
# other
# #############################

from collections import OrderedDict as odict # NOQA
from collections import OrderedDict as odict # noqa: F401

from io import open as io_open # NOQA

Expand Down
12 changes: 6 additions & 6 deletions conda/common/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from conda._vendor.toolz.itertoolz import unique
from conda._vendor.toolz.dicttoolz import merge, merge_with

from .compat import isiterable, odict, primitive_types
from .compat import isiterable, primitive_types
from .constants import NULL
from .path import expand
from .serialize import yaml_round_trip_load
Expand Down Expand Up @@ -485,7 +485,7 @@ def _get_st_mode(path):
load_paths = (_loader[st_mode](path)
for path, st_mode in zip(expanded_paths, stat_paths)
if st_mode is not None)
raw_data = odict(kv for kv in chain.from_iterable(load_paths))
raw_data = dict(kv for kv in chain.from_iterable(load_paths))
return raw_data


Expand Down Expand Up @@ -1286,7 +1286,7 @@ class Configuration(metaclass=ConfigurationType):
def __init__(self, search_path=(), app_name=None, argparse_args=None):
# Currently, __init__ does a **full** disk reload of all files.
# A future improvement would be to cache files that are already loaded.
self.raw_data = odict()
self.raw_data = {}
self._cache_ = {}
self._reset_callbacks = IndexedSet()
self._validation_errors = defaultdict(list)
Expand Down Expand Up @@ -1406,12 +1406,12 @@ def post_build_validation(self):
return ()

def collect_all(self):
typed_values = odict()
validation_errors = odict()
typed_values = {}
validation_errors = {}
for source in self.raw_data:
typed_values[source], validation_errors[source] = self.check_source(source)
raise_errors(tuple(chain.from_iterable(validation_errors.values())))
return odict((k, v) for k, v in typed_values.items() if v)
return {k: v for k, v in typed_values.items() if v}

def describe_parameter(self, parameter_name):
# TODO, in Parameter base class, rename element_type to value_type
Expand Down
12 changes: 6 additions & 6 deletions conda/common/pkg_formats/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from conda.common.iterators import groupby_to_dict as groupby

from ... import CondaError
from ..compat import odict, open
from ..compat import open
from ..path import (
get_python_site_packages_short_path, pyc_path, win_path_ok, get_major_minor_version,
)
Expand Down Expand Up @@ -129,7 +129,7 @@ def _parse_requires_file_data(data, global_section='__global__'):
"""
https://setuptools.readthedocs.io/en/latest/formats.html#requires-txt
"""
requires = odict()
requires = {}
lines = [line.strip() for line in data.split('\n') if line]

if lines and not (lines[0].startswith('[') and lines[0].endswith(']')):
Expand Down Expand Up @@ -174,7 +174,7 @@ def _parse_entries_file_data(data):
https://setuptools.readthedocs.io/en/latest/formats.html#entry-points-txt-entry-point-plugin-metadata
"""
# FIXME: Use pkg_resources which provides API for this?
entries_data = odict()
entries_data = {}
config = ConfigParser()
config.optionxform = lambda x: x # Avoid lowercasing keys
try:
Expand All @@ -183,7 +183,7 @@ def _parse_entries_file_data(data):
do_read = config.readfp
do_read(StringIO(data))
for section in config.sections():
entries_data[section] = odict(config.items(section))
entries_data[section] = dict(config.items(section))

return entries_data

Expand Down Expand Up @@ -541,7 +541,7 @@ def _message_to_dict(cls, message):
description key.
- The result should be stored as a string-keyed dictionary.
"""
new_data = odict()
new_data = {}

if message:
for key, value in message.items():
Expand All @@ -566,7 +566,7 @@ def _read_metadata(cls, fpath):
"""
Read the original format which is stored as RFC-822 headers.
"""
data = odict()
data = {}
if fpath and isfile(fpath):
parser = HeaderParser()

Expand Down
19 changes: 2 additions & 17 deletions conda/common/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import json
from logging import getLogger

from .compat import odict, ensure_text_type
from .compat import ensure_text_type
from ..auxlib.entity import EntityEncoder

try:
Expand All @@ -20,22 +20,6 @@
log = getLogger(__name__)


def represent_ordereddict(dumper, data):
value = []

for item_key, item_value in data.items():
node_key = dumper.represent_data(item_key)
node_value = dumper.represent_data(item_value)

value.append((node_key, node_value))

return yaml.nodes.MappingNode("tag:yaml.org,2002:map", value)


yaml.representer.RoundTripRepresenter.add_representer(odict, represent_ordereddict)
yaml.representer.SafeRepresenter.add_representer(odict, represent_ordereddict)


# FUTURE: Python 3.9+, replace with functools.cache
@functools.lru_cache(maxsize=None)
def _yaml_round_trip():
Expand All @@ -50,6 +34,7 @@ def _yaml_safe():
parser = yaml.YAML(typ="safe", pure=True)
parser.indent(mapping=2, offset=2, sequence=4)
parser.default_flow_style = False
parser.sort_base_mapping_type_on_output = False
return parser


Expand Down
6 changes: 3 additions & 3 deletions conda/core/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from ..base.constants import DEFAULTS_CHANNEL_NAME, PREFIX_MAGIC_FILE, SafetyChecks
from ..base.context import context
from ..cli.common import confirm_yn
from ..common.compat import ensure_text_type, odict, on_win
from ..common.compat import ensure_text_type, on_win
from ..common.io import Spinner, dashlist, time_recorder
from ..common.io import DummyExecutor, ThreadLimitedThreadPoolExecutor
from ..common.path import (explode_directories, get_all_directories, get_major_minor_version,
Expand Down Expand Up @@ -166,8 +166,8 @@ def match_specs_to_dists(packages_info_to_link, specs):
class UnlinkLinkTransaction:

def __init__(self, *setups):
self.prefix_setups = odict((stp.target_prefix, stp) for stp in setups)
self.prefix_action_groups = odict()
self.prefix_setups = {stp.target_prefix: stp for stp in setups}
self.prefix_action_groups = {}

for stp in self.prefix_setups.values():
log.info("initializing UnlinkLinkTransaction with\n"
Expand Down
Loading

0 comments on commit 81acff3

Please sign in to comment.