-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.py
More file actions
78 lines (60 loc) · 2.19 KB
/
Copy pathdemo.py
File metadata and controls
78 lines (60 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from iris_global import GlobalReference
# Create a reference to a global
team = GlobalReference("^demo")
team.kill() # Clear any existing data
# Different ways to set values
team.set((), "Baseball") # Set root node
team["name"] = "Boston Red Sox" # Dictionary-style assignment
team.set(("players", "1"), "Babe Ruth") # Tuple subscript
team.set(["players", "2"], "Cy Young") # List subscript
team["players", "3"] = "Ted Williams" # Multiple subscripts
# Different ways to get values
print(team.get(())) # Get root value: "Baseball"
print(team["name"]) # Dictionary-style access
print(team.get(("players", "1"))) # Using get() method
print(team["players"]["2"]) # Nested dictionary-style
# Check if nodes exist
print(("players", "1") in team) # True
print("nonexistent" in team) # False
# Delete nodes
del team["players", "3"] # Delete using del
team.kill(("players", "2")) # Delete using kill()
# Iteration Examples
# 1. Iterate through all nodes and values
for key, value in team.items():
print(f"Node {key}: {value}") # Shows all nodes with values
# Output :
# Node (): Baseball
# Node ('name',): Boston Red Sox
# Node ('players', '1'): Babe Ruth
# 2. Iterate through direct children only
for key in team.keys(children_only=True):
print(f"Direct child: {key}") # Shows only root level nodes
# Output:
# Direct child: ()
# Direct child: ('name',)
# 3. Custom iteration with subscripts
for sub in team.subscripts(("players",), children_only=True):
print(f"Player: {team.get(sub)}") # Shows only players
# Output:
# Player: Babe Ruth
# Count direct children
print(len(team)) # Number of root level nodes
# Output:
# 3
# Display global structure
print(team.zw()) # Show ZWRITE format
# Output:
# ^demo="Baseball"
# ^demo("name")="Boston Red Sox"
# ^demo("players","1")="Babe Ruth"
# Display this global in a dictionary format
print(team.to_dict())
# Output:
# {
# None: 'Baseball',
# 'name': 'Boston Red Sox',
# 'players': {
# '1': 'Babe Ruth'
# }
# }