Selfie
Loading...
Searching...
No Matches
selfie_lib.Lens.CompoundLens Class Reference
Inheritance diagram for selfie_lib.Lens.CompoundLens:
selfie_lib.Lens.Lens

Public Member Functions

 __init__ (self)
 
Snapshot __call__ (self, Snapshot snapshot)
 
"CompoundLens" add (self, Lens lens)
 
"CompoundLens" mutate_all_facets (self, Callable[[str], Optional[str]] perString)
 
"CompoundLens" replace_all (self, str toReplace, str replacement)
 
"CompoundLens" replace_all_regex (self, Union[str, re.Pattern[str]] pattern, str replacement)
 
"CompoundLens" set_facet_from (self, str target, str source, Callable[[str], Optional[str]] function)
 
"CompoundLens" mutate_facet (self, str target, Callable[[str], Optional[str]] function)
 

Detailed Description

Definition at line 16 of file Lens.py.

Constructor & Destructor Documentation

◆ __init__()

selfie_lib.Lens.CompoundLens.__init__ (   self)

Definition at line 17 of file Lens.py.

17 def __init__(self):
18 self.lenses: list[Lens] = []
19

Member Function Documentation

◆ __call__()

Snapshot selfie_lib.Lens.CompoundLens.__call__ (   self,
Snapshot  snapshot 
)

Reimplemented from selfie_lib.Lens.Lens.

Definition at line 20 of file Lens.py.

20 def __call__(self, snapshot: Snapshot) -> Snapshot:
21 current = snapshot
22 for lens in self.lenses:
23 current = lens(current)
24 return current
25

◆ add()

"CompoundLens" selfie_lib.Lens.CompoundLens.add (   self,
Lens  lens 
)

Definition at line 26 of file Lens.py.

26 def add(self, lens: Lens) -> "CompoundLens":
27 self.lenses.append(lens)
28 return self
29

◆ mutate_all_facets()

"CompoundLens" selfie_lib.Lens.CompoundLens.mutate_all_facets (   self,
Callable[[str], Optional[str]]   perString 
)

Definition at line 30 of file Lens.py.

32 ) -> "CompoundLens":
33 def _mutate_each(snapshot: Snapshot) -> Iterator[tuple[str, SnapshotValue]]:
34 for entry in snapshot.items():
35 if entry[1].is_binary:
36 yield entry
37 else:
38 mapped = perString(entry[1].value_string())
39 if mapped is not None:
40 yield (entry[0], SnapshotValue.of(mapped))
41
42 return self.add(lambda snapshot: Snapshot.of_items(_mutate_each(snapshot)))
43

◆ mutate_facet()

"CompoundLens" selfie_lib.Lens.CompoundLens.mutate_facet (   self,
str  target,
Callable[[str], Optional[str]]   function 
)

Definition at line 74 of file Lens.py.

76 ) -> "CompoundLens":
77 return self.set_facet_from(target, target, function)
78
79

◆ replace_all()

"CompoundLens" selfie_lib.Lens.CompoundLens.replace_all (   self,
str  toReplace,
str  replacement 
)

Definition at line 44 of file Lens.py.

44 def replace_all(self, toReplace: str, replacement: str) -> "CompoundLens":
45 return self.mutate_all_facets(lambda s: s.replace(toReplace, replacement))
46

◆ replace_all_regex()

"CompoundLens" selfie_lib.Lens.CompoundLens.replace_all_regex (   self,
Union[str, re.Pattern[str]]  pattern,
str   replacement 
)

Definition at line 47 of file Lens.py.

49 ) -> "CompoundLens":
50 return self.mutate_all_facets(lambda s: re.sub(pattern, replacement, s))
51

◆ set_facet_from()

"CompoundLens" selfie_lib.Lens.CompoundLens.set_facet_from (   self,
str  target,
str  source,
Callable[[str], Optional[str]]   function 
)

Definition at line 52 of file Lens.py.

54 ) -> "CompoundLens":
55 def _set_facet_from(snapshot: Snapshot) -> Snapshot:
56 source_value = snapshot.subject_or_facet_maybe(source)
57 if source_value is None:
58 return snapshot
59 else:
60 return self.__set_facet_of(
61 snapshot, target, function(source_value.value_string())
62 )
63
64 return self.add(_set_facet_from)
65

The documentation for this class was generated from the following file: