Skip to content

Commit

Permalink
Merge branch 'master' into newCapModel3
Browse files Browse the repository at this point in the history
  • Loading branch information
MSK61 committed Nov 3, 2024
2 parents 2437b09 + 96c335f commit cc6c9dc
Show file tree
Hide file tree
Showing 23 changed files with 149 additions and 158 deletions.
15 changes: 10 additions & 5 deletions src/container_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#
# author: Mohammed El-Afifi (ME)
#
# environment: Visual Studio Code 1.91.1, python 3.11.9, Fedora release
# environment: Visual Studio Code 1.95.1, python 3.12.7, Fedora release
# 40 (Forty)
#
# notes: This is a private program.
Expand All @@ -48,11 +48,11 @@
from typing import Any, Generic, Optional, TypeVar

from attr import field, frozen
import fastcore.basics
import more_itertools
import pydash

from str_utils import format_obj
import type_checking

_KT = TypeVar("_KT")
_T = TypeVar("_T")
Expand Down Expand Up @@ -186,7 +186,7 @@ def __eq__(self, other: Any) -> Any:
assert type(other) is type(self)
other_items = tuple(other.items())
lst_pairs = (
(type_checking.call(sorted, lst) for lst in [val_lst, self[key]])
(fastcore.basics.Self(lst)(sorted) for lst in [val_lst, self[key]])
for key, val_lst in other_items
)
item_lst_pair: list[collections.abc.Sized] = [self, other_items]
Expand All @@ -201,7 +201,9 @@ def __getitem__(self, key: _KT) -> list[_VT]:
`key` is the key to retrieve whose list.
"""
return self._dict[key]
# Pylance isn't smart enough to figure out that self._dict[key]
# has the same type as list[_VT].
return typing.cast(list[_VT], self._dict[key])

def __len__(self) -> int:
"""Count the number of elements in this dictionary.
Expand Down Expand Up @@ -251,6 +253,9 @@ def _format_elems(self) -> str:
starmap(lambda key, val_lst: f"{key!r}: {val_lst}", elems)
)

# defaultdict isn't strictly required here, but pylance can't
# understand that factory products are passed anyway to the
# converter.
_dict: defaultdict[_KT, list[_VT]] = field(
converter=_val_lst_dict, factory=dict
converter=_val_lst_dict, factory=defaultdict
)
17 changes: 7 additions & 10 deletions src/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
#
# author: Mohammed El-Afifi (ME)
#
# environment: Visual Studio Code 1.86.1, python 3.11.7, Fedora release
# 39 (Thirty Nine)
# environment: Visual Studio Code 1.95.1, python 3.12.7, Fedora release
# 40 (Forty)
#
# notes: This is a private program.
#
Expand All @@ -47,7 +47,9 @@

import attr
from attr import frozen
import fastcore.foundation
import fastcore.basics

import type_checking

# Attrs doesn't honor class variables annotated with typing.Final(albeit
# being mandated by PEP-591) and instead still treats them as instance
Expand Down Expand Up @@ -118,13 +120,8 @@ def _init(
{elem.key: elem.val.displayed for elem in elems}
)
)
val_extractor = fastcore.foundation.Self.val().stored()
# Pylance and pylint can't detect __attrs_init__ as an injected
# method.
# pylint: disable-next=no-member
self.__attrs_init__( # type: ignore[attr-defined]
*(map(val_extractor, elems))
)
val_extractor = fastcore.basics.Self.val().stored()
type_checking.attrs_init(self, *(map(val_extractor, elems)))

def _init_simple(
self,
Expand Down
6 changes: 3 additions & 3 deletions src/processor_sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
#
# author: Mohammed El-Afifi (ME)
#
# environment: Visual Studio Code 1.92.2, python 3.12.4, Fedora release
# environment: Visual Studio Code 1.95.1, python 3.12.7, Fedora release
# 40 (Forty)
#
# notes: This is a private program.
Expand All @@ -57,7 +57,7 @@
from typing import Annotated, Any, IO

from attr import frozen
from fastcore import foundation
from fastcore import basics
import more_itertools
import typer
from typer import FileText
Expand Down Expand Up @@ -114,7 +114,7 @@ def __str__(self) -> str:
return ":".join(
map(
self._get_res_str,
[foundation.Self._stalled(), foundation.Self._unit()],
[basics.Self._stalled(), basics.Self._unit()],
)
)

Expand Down
37 changes: 17 additions & 20 deletions src/processor_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#
# author: Mohammed El-Afifi (ME)
#
# environment: Visual Studio Code 1.94.1, python 3.12.6, Fedora release
# environment: Visual Studio Code 1.95.1, python 3.12.7, Fedora release
# 40 (Forty)
#
# notes: This is a private program.
Expand All @@ -56,8 +56,8 @@
from typing import Any

from attr import field, frozen
from fastcore import foundation
from fastcore.foundation import compose, mapt
from fastcore import basics
from fastcore.basics import compose, mapt
import networkx
from networkx import DiGraph, Graph

Expand All @@ -66,7 +66,6 @@
from errors import UndefElemError
from str_utils import ICaseString
import type_checking
from type_checking import call
from . import _checks, _optimization, _port_defs, units
from .exception import BadEdgeError, BadWidthError, DupElemError
from .units import (
Expand Down Expand Up @@ -299,7 +298,7 @@ def _add_rev_edges(graph: Graph) -> None:
for pred in unit.predecessors
if pred.name in graph
),
call(graph.nodes, _UNIT_KEY),
basics.Self(_UNIT_KEY)(graph.nodes),
)
graph.add_edges_from(chain.from_iterable(edges))

Expand Down Expand Up @@ -337,11 +336,7 @@ def _chk_unit_width(unit: Mapping[object, int]) -> None:
raise BadWidthError(
f"Functional unit ${BadWidthError.UNIT_KEY} has a bad width "
f"${BadWidthError.WIDTH_KEY}.",
*(
foundation.map_ex(
[UNIT_NAME_KEY, UNIT_WIDTH_KEY], unit, gen=True
)
),
*(basics.map_ex([UNIT_NAME_KEY, UNIT_WIDTH_KEY], unit, gen=True)),
)


Expand All @@ -361,7 +356,7 @@ def _create_graph(
edge_registry = IndexedSet[Collection[str]](
lambda edge: mapt(compose(ICaseString, unit_registry.get), edge)
)
cap_registry = IndexedSet[_CapabilityInfo](foundation.Self.name())
cap_registry = IndexedSet[_CapabilityInfo](basics.Self.name())

for cur_unit in hw_units:
_add_unit(flow_graph, cur_unit, unit_registry, cap_registry)
Expand Down Expand Up @@ -445,7 +440,7 @@ def _get_preds(
The function returns an iterator over predecessor units.
"""
return foundation.map_ex(processor.predecessors(unit), unit_map, gen=True)
return basics.map_ex(processor.predecessors(unit), unit_map, gen=True)


def _get_preds2(
Expand Down Expand Up @@ -474,7 +469,9 @@ def _get_proc_units(graph: DiGraph) -> Generator[FuncUnit, None, None]:
unit: _get_unit_entry(unit, graph.nodes[unit]) for unit in graph
}
return (
call(FuncUnit, unit_map[name], _get_preds2(graph, name, unit_map))
basics.Self(unit_map[name], _get_preds2(graph, name, unit_map))(
FuncUnit
)
for name in graph
)

Expand Down Expand Up @@ -529,7 +526,7 @@ def _get_unit_entry(name: str, attrs: Mapping[str, Any]) -> UnitModel:
The function returns the unit model.
"""
lock_attrs = foundation.map_ex([UNIT_RLOCK_KEY, UNIT_WLOCK_KEY], attrs)
lock_attrs = basics.map_ex([UNIT_RLOCK_KEY, UNIT_WLOCK_KEY], attrs)
return UnitModel(
name,
attrs[UNIT_WIDTH_KEY],
Expand Down Expand Up @@ -659,7 +656,9 @@ def _prep_proc_desc(processor: DiGraph) -> None:
"""
_checks.chk_cycles(processor)
port_info = _port_defs.PortGroup(processor)
port_info = _port_defs.PortGroup( # type: ignore[call-arg]
basics.Self(processor)
)
_optimization.clean_struct(processor)
_optimization.rm_empty_units(processor)
_optimization.chk_terminals(processor, port_info)
Expand All @@ -673,9 +672,7 @@ def _sorted_units(hw_units: Iterable[Any]) -> tuple[Any, ...]:
`hw_units` are the units to sort.
"""
return container_utils.sorted_tuple(
hw_units, key=foundation.Self.model.name()
)
return container_utils.sorted_tuple(hw_units, key=basics.Self.model.name())


@frozen
Expand Down Expand Up @@ -761,8 +758,8 @@ def _make_processor(proc_graph: DiGraph) -> ProcessorDesc:
case _:
in_out_ports.append(unit.model)

return call(
ProcessorDesc, in_ports, out_ports, in_out_ports, internal_units
return basics.Self(in_ports, out_ports, in_out_ports, internal_units)(
ProcessorDesc
)


Expand Down
14 changes: 6 additions & 8 deletions src/processor_utils/_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#
# author: Mohammed El-Afifi (ME)
#
# environment: Visual Studio Code 1.93.1, python 3.12.6, Fedora release
# environment: Visual Studio Code 1.95.1, python 3.12.7, Fedora release
# 40 (Forty)
#
# notes: This is a private program.
Expand All @@ -53,13 +53,12 @@
from typing import Any, TypeVar

from attr import frozen
from fastcore import foundation
from fastcore import basics
import more_itertools
from more_itertools import one
import networkx
from networkx import DiGraph, Graph

import type_checking
from . import cap_anal_utils, exception, _port_defs, units
from .exception import BlockedCapError, ComponentInfo, PathLockError
from .units import ROLE_NAME_KEY, UNIT_ROLES_KEY, UNIT_WIDTH_KEY
Expand Down Expand Up @@ -151,7 +150,7 @@ def make_read_desc(cls, capability: object, start: object) -> typing.Self:
`start` is the path start unit.
"""
return cls(foundation.Self.read_lock(), "read", capability, start)
return cls(basics.Self.read_lock(), "read", capability, start)

@classmethod
def make_write_desc(cls, capability: object, start: object) -> typing.Self:
Expand All @@ -162,7 +161,7 @@ def make_write_desc(cls, capability: object, start: object) -> typing.Self:
`start` is the path start unit.
"""
return cls(foundation.Self.write_lock(), "write", capability, start)
return cls(basics.Self.write_lock(), "write", capability, start)

selector: Callable[[_SatInfo], int]

Expand Down Expand Up @@ -588,9 +587,8 @@ def _get_anal_graph(processor: Graph) -> DiGraph:
for idx, unit in hw_units:
_update_graph(idx, unit, processor, width_graph, new_nodes)

type_checking.call(
width_graph.add_edges_from,
(foundation.map_ex(edge, new_nodes) for edge in processor.edges),
basics.Self(basics.map_ex(edge, new_nodes) for edge in processor.edges)(
width_graph.add_edges_from
)
return width_graph

Expand Down
6 changes: 3 additions & 3 deletions src/processor_utils/_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#
# author: Mohammed El-Afifi (ME)
#
# environment: Visual Studio Code 1.93.1, python 3.12.6, Fedora release
# environment: Visual Studio Code 1.95.1, python 3.12.7, Fedora release
# 40 (Forty)
#
# notes: This is a private program.
Expand All @@ -44,10 +44,10 @@
import typing
from typing import Any

import fastcore.basics
import networkx
from networkx import DiGraph, Graph

import type_checking
from .exception import DeadInputError
from . import _port_defs
from .units import ROLE_NAME_KEY, UNIT_ROLES_KEY
Expand Down Expand Up @@ -105,7 +105,7 @@ def rm_empty_units(processor: Graph) -> None:
The function removes units with no capabilities from the processor.
"""
unit_entries = tuple(type_checking.call(processor.nodes, UNIT_ROLES_KEY))
unit_entries = tuple(fastcore.basics.Self(UNIT_ROLES_KEY)(processor.nodes))

for unit, capabilities in unit_entries:
if not capabilities:
Expand Down
Loading

0 comments on commit cc6c9dc

Please sign in to comment.