Skip to content

Commit 7e3961e

Browse files
committed
02_11_2025: added more line methods
1 parent b6bb803 commit 7e3961e

File tree

1 file changed

+201
-51
lines changed

1 file changed

+201
-51
lines changed

model/library/line.py

Lines changed: 201 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -6,57 +6,63 @@
66
77
Methods and properties
88
9-
__init__ : line instance initialization
10-
serialize : (property) serialize line
11-
inverse : inverse line
12-
data : generate default deviation data for all unique elements
13-
scan : scan line and yeild (with duplicates) all elements with given attribute
14-
select : (static) select elements
15-
get : get given attribute from selected elements
16-
set : set value to a given attribute for selected elements
17-
name : (property) get/set name of the line
18-
sequence : (property) get/set sequence
19-
flatten : flatten line (all levels)
20-
rename : rename first level element
21-
append : append element
22-
extend : extend line
23-
insert : insert element
24-
remove : remove first occurrance of element with given name
25-
replace : replace first occurrance of element with given name
26-
names : (property) get list of first level element names
27-
layout : generate data for layout plotting
28-
locations : first level element/line entrance frame locations
29-
position : get element position in sequence
30-
positions : get all element position in sequence
31-
start : (property) set/get the first element
32-
roll : roll first level sequence
33-
unique : (property) get unique elements
34-
index : return list of unique element names and their indices from first level sequence
35-
duplicate : (property) get duplicate elements
36-
itemize : get list of all elements with matching kind
37-
describe : (property) return number of elements (with unique names) for each kind
38-
split : split elements
39-
clean : clean first level sequence
40-
mangle : mangle elements
41-
merge : merge drift elements
42-
splice : splice line
43-
group : replace sequence part (first level) from probe to other with a line
44-
dp : (property) get/set momentum deviation
45-
exact : (property) get/set exact flag
46-
length : (property) get line length
47-
angle : (property) get line angle
48-
flag : (property) get layout flag
49-
ns : (property) get/set number of integration steps
50-
order : (property) get/set integration order
51-
output : (property) get/set output flag
52-
matrix : (property) get/set matrix flag
53-
query : query line
54-
__call__ : transform state
55-
__len__ : get number of elements (first level)
56-
__getitem__ : get (first level) element by key
57-
__setitem__ : set (first level) element by key
58-
__delitem__ : del (first level) element by key
59-
__repr__ : print line
9+
__init__ : line instance initialization
10+
serialize : (property) serialize line
11+
inverse : inverse line
12+
data : generate default deviation data for all unique elements
13+
scan : scan line and yeild (with duplicates) all elements with given attribute
14+
select : (static) select elements
15+
get : get given attribute from selected elements
16+
set : set value to a given attribute for selected elements
17+
name : (property) get/set name of the line
18+
sequence : (property) get/set sequence
19+
flatten : flatten line (all levels)
20+
rename : rename first level element
21+
append : append element
22+
extend : extend line
23+
insert : insert element
24+
remove : remove first occurrance of element with given name
25+
replace : replace first occurrance of element with given name
26+
names : (property) get list of first level element names
27+
layout : generate data for layout plotting
28+
locations : first level element/line entrance frame locations
29+
position : get element position in sequence
30+
positions : get all element position in sequence
31+
start : (property) set/get the first element
32+
roll : roll first level sequence
33+
unique : (property) get unique elements
34+
index : return list of unique element names and their indices from first level sequence
35+
duplicate : (property) get duplicate elements
36+
itemize : get list of all elements with matching kind
37+
describe : (property) return number of elements (with unique names) for each kind
38+
split : split elements
39+
clean : clean first level sequence
40+
mangle : mangle elements
41+
merge : merge drift elements
42+
splice : splice line
43+
group : replace sequence part (first level) from probe to other with a line
44+
dp : (property) get/set momentum deviation
45+
exact : (property) get/set exact flag
46+
length : (property) get line length
47+
angle : (property) get line angle
48+
flag : (property) get layout flag
49+
ns : (property) get/set number of integration steps
50+
order : (property) get/set integration order
51+
output : (property) get/set output flag
52+
matrix : (property) get/set matrix flag
53+
query : query line
54+
apply : apply function for specific elements
55+
before : insert element before
56+
after : insert element after
57+
swap : swap elements
58+
rename_group : rename element group
59+
replace_group : replace element group
60+
__call__ : transform state
61+
__len__ : get number of elements (first level)
62+
__getitem__ : get (first level) element by key
63+
__setitem__ : set (first level) element by key
64+
__delitem__ : del (first level) element by key
65+
__repr__ : print line
6066
6167
6268
"""
@@ -1461,6 +1467,150 @@ def query(self, *,
14611467
return elements
14621468

14631469

1470+
def apply(self,
1471+
funcion:Callable[[Element], None], *,
1472+
kinds: Optional[list[str]]=None,
1473+
patterns: Optional[list[str]]=None,
1474+
checks: Optional[list[Callable[[Element], bool]]]=None) -> None:
1475+
"""
1476+
Apply function for specific elements
1477+
1478+
Parameters
1479+
----------
1480+
kinds: Optional[list[str]]
1481+
list of kinds to select
1482+
patterns: Optional[list[str]]
1483+
list of patterns to select
1484+
checks: checks:Optional[;ist[Callable[[Element], bool]]]
1485+
list of check functions
1486+
1487+
Returns
1488+
-------
1489+
None
1490+
1491+
"""
1492+
for element in self.query(kinds=kinds, patterns=patterns, checks=checks or []):
1493+
funcion(element)
1494+
1495+
1496+
def before(self,
1497+
name:str,
1498+
element:Element) -> None:
1499+
"""
1500+
Insert element before name
1501+
1502+
Parameters
1503+
----------
1504+
name: str
1505+
element name
1506+
element: Element
1507+
element to insert
1508+
1509+
Returns
1510+
-------
1511+
None
1512+
1513+
"""
1514+
self.sequence.insert(self.position(name), element)
1515+
1516+
1517+
def after(self,
1518+
name:str,
1519+
element:Element) -> None:
1520+
"""
1521+
Insert element after name
1522+
1523+
Parameters
1524+
----------
1525+
name: str
1526+
element name
1527+
element: Element
1528+
element to insert
1529+
1530+
Returns
1531+
-------
1532+
None
1533+
1534+
"""
1535+
self.sequence.insert(self.position(name) + 1, element)
1536+
1537+
1538+
def swap(self,
1539+
first:str|int,
1540+
second:str|int) -> None:
1541+
"""
1542+
Swap elements
1543+
1544+
Parameters
1545+
----------
1546+
first: str|int
1547+
first element
1548+
second: str|int
1549+
second element
1550+
1551+
Returns
1552+
-------
1553+
None
1554+
1555+
"""
1556+
first = self.position(first) if isinstance(first, str) else first
1557+
second = self.position(second) if isinstance(second, str) else second
1558+
self.sequence[first], self.sequence[second] = self.sequence[second], self.sequence[first]
1559+
1560+
1561+
def rename_group(self,
1562+
pattern:str,
1563+
replace:str,
1564+
kinds:Optional[list[str]]=None) -> None:
1565+
"""
1566+
Rename group
1567+
1568+
Parameters
1569+
----------
1570+
pattern: str
1571+
regex pattern
1572+
replace: str
1573+
replacement string
1574+
kinds: Optional[list[str]]
1575+
list of kinds to select
1576+
1577+
Returns
1578+
-------
1579+
None
1580+
1581+
"""
1582+
for element in self.query(kinds=kinds):
1583+
name = re.compile(pattern).sub(replace, element.name)
1584+
if name != element.name:
1585+
element.name = name
1586+
1587+
1588+
def replace_group(self,
1589+
pattern:str,
1590+
factory:Callable[[Element], Element], *,
1591+
kinds:Optional[list[str]]=None) -> None:
1592+
"""
1593+
Replace group
1594+
1595+
Parameters
1596+
----------
1597+
pattern: str
1598+
regex pattern
1599+
factory: Callable[[Element], Element]
1600+
factory function
1601+
kinds: Optional[list[str]]
1602+
list of kinds to select
1603+
1604+
Returns
1605+
-------
1606+
None
1607+
1608+
"""
1609+
for i, element in enumerate(self.sequence):
1610+
if (not kinds or element.__class__.__name__ in kinds) and re.compile(pattern).search(element.name):
1611+
self.sequence[i] = factory(element)
1612+
1613+
14641614
def __call__(self,
14651615
state:State, *,
14661616
data:Optional[dict[str, Tensor | dict[str, Tensor]]]=None,

0 commit comments

Comments
 (0)