Skip to content

Commit 3ff8f3d

Browse files
committed
Fix formatting and tests
1 parent ca72e2e commit 3ff8f3d

3 files changed

Lines changed: 86 additions & 62 deletions

File tree

chi/container.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import os
1818
import tarfile
1919
import time
20-
from typing import Dictionary, List, Optional, Tuple
20+
from typing import Dict, List, Optional, Tuple
2121

2222
from IPython.display import HTML, display
2323
from packaging.version import Version
@@ -62,7 +62,7 @@ class Container:
6262
id (str): The ID of the container.
6363
created_at (str): The timestamp when the container was created.
6464
status (str): The current status of the container.
65-
environment (Dictionary[str, str]): A dictionary of environment variables for the container.
65+
environment (Dict[str, str]): A dictionary of environment variables for the container.
6666
device_profiles (List[str]): A list of device profiles to be configured on the container.
6767
"""
6868

@@ -77,7 +77,7 @@ def __init__(
7777
runtime: str = None,
7878
command: List[str] = None,
7979
workdir: str = None,
80-
environment: Dictionary[str, str] = {},
80+
environment: Dict[str, str] = {},
8181
device_profiles: List[str] = [],
8282
):
8383
self.name = name

chi/context.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,12 +442,13 @@ def on_change(change):
442442
else:
443443
print("Choose site feature is only available in an ipynb environment.")
444444

445+
445446
def use_lease_id(lease_id: str) -> None:
446447
"""
447448
Sets the current lease ID to use in the global context.
448449
449450
This configures the lease so it can be stored for ease
450-
of restoring suspended sessions. Further lease validation,
451+
of restoring suspended sessions. Further lease validation,
451452
visualizations, and selectors are available in the lease module.
452453
453454
Args:
@@ -456,12 +457,15 @@ def use_lease_id(lease_id: str) -> None:
456457
global _lease_id
457458

458459
if not re.fullmatch(r"[A-Za-z0-9\-]+", lease_id):
459-
raise CHIValueError(f'Lease ID "{lease_id}" is invalid. It must contain only letters, numbers, and hyphens with no spaces or special characters.')
460+
raise CHIValueError(
461+
f'Lease ID "{lease_id}" is invalid. It must contain only letters, numbers, and hyphens with no spaces or special characters.'
462+
)
460463

461464
_lease_id = lease_id
462465

463466
print(f"Now using lease with ID {lease_id}.")
464467

468+
465469
def get_lease_id():
466470
"""
467471
Returns the currently active lease ID, if one has been set.
@@ -473,6 +477,7 @@ def get_lease_id():
473477
print("No lease ID has been set. Use `use_lease_id()` to select one.")
474478
return _lease_id
475479

480+
476481
def get_project_name(project_id: Optional[str] = None) -> str:
477482
"""
478483
Returns the name of a project by ID, or the current project name if no ID is given.

chi/lease.py

Lines changed: 76 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,17 +1210,25 @@ def list_leases() -> List[Lease]:
12101210

12111211
return leases
12121212

1213+
12131214
def _status_color(cell):
1214-
return "#a2d9fe" if cell.value == "2-ACTIVE" else (
1215-
"#ffe599" if cell.value == "1-PENDING" else (
1216-
"#f69084" if cell.value == "3-TERMINATED" else "#e0e0e0"))
1217-
1215+
return (
1216+
"#a2d9fe"
1217+
if cell.value == "2-ACTIVE"
1218+
else (
1219+
"#ffe599"
1220+
if cell.value == "1-PENDING"
1221+
else ("#f69084" if cell.value == "3-TERMINATED" else "#e0e0e0")
1222+
)
1223+
)
1224+
1225+
12181226
def show_leases() -> DataGrid:
12191227
"""
12201228
Displays a table of the user's leases in an interactive, sortable format.
12211229
12221230
Uses an ipydatagrid to present key lease attributes such as ID, name, status,
1223-
duration, and reservation counts. The grid supports sorting, filtering, and
1231+
duration, and reservation counts. The grid supports sorting, filtering, and
12241232
scrolling for easy exploration of lease state.
12251233
12261234
Returns:
@@ -1233,95 +1241,106 @@ def estimate_column_width(df, column, char_px=7, padding=0):
12331241
max_chars = df[column].astype(str).map(len).max()
12341242
return max(max_chars * char_px + padding, 80)
12351243

1236-
12371244
leases = list_leases()
12381245

12391246
rows = []
12401247
for lease in leases:
1241-
12421248
try:
12431249
project_name = get_project_name(lease.project_id)
12441250
except ResourceError:
12451251
project_name = lease.project_id[:8] if lease.project_id else "Unknown"
1246-
1252+
12471253
if lease.user_id == connection().current_user_id:
12481254
user_label = os.getenv("OS_USERNAME")
12491255
else:
12501256
user_label = lease.user_id if lease.user_id else "Unknown"
1251-
1257+
12521258
if lease.start_date and lease.end_date:
1253-
duration_hrs = round((lease.end_date - lease.start_date).total_seconds() / 3600, 1)
1259+
duration_hrs = round(
1260+
(lease.end_date - lease.start_date).total_seconds() / 3600, 1
1261+
)
12541262
else:
12551263
duration_hrs = "N/A"
1256-
1264+
12571265
# Inside your row-building loop:
12581266
if lease.end_date and lease.end_date > datetime.now():
12591267
remaining_td = lease.end_date - datetime.now()
1260-
remaining_str = f"{remaining_td.days:02d}d {(remaining_td.seconds // 3600):02d}h"
1268+
remaining_str = (
1269+
f"{remaining_td.days:02d}d {(remaining_td.seconds // 3600):02d}h"
1270+
)
12611271
elif lease.end_date and lease.end_date <= datetime.now():
12621272
remaining_str = "Expired"
12631273
else:
12641274
remaining_str = "N/A"
12651275

1266-
12671276
# prepending status with numeric makes it possible to character sort
12681277
# since ipydatagrid does not allow custom sort functions
12691278
status_order = {
12701279
"PENDING": "1-PENDING",
12711280
"ACTIVE": "2-ACTIVE",
1272-
"TERMINATED": "3-TERMINATED"
1281+
"TERMINATED": "3-TERMINATED",
12731282
}
1274-
1275-
rows.append({
1276-
"Name": lease.name,
1277-
"Status": status_order.get(lease.status, f"4-{lease.status}"),
1278-
"User": user_label,
1279-
"Project": project_name,
1280-
"Start": lease.start_date.strftime("%Y-%m-%d %H:%M") if lease.start_date else "",
1281-
"End": lease.end_date.strftime("%Y-%m-%d %H:%M") if lease.end_date else "",
1282-
"Remaining": remaining_str,
1283-
"Total Hours": duration_hrs,
1284-
"# Nodes": len(lease.node_reservations),
1285-
"# FIPs": len(lease.fip_reservations),
1286-
"Created": lease.created_at.strftime("%Y-%m-%d %H:%M") if lease.created_at else "",
1287-
"Lease ID": lease.id,
1288-
"_is_user_lease": 0 if lease.user_id == connection().current_user_id else 1
1289-
})
1290-
1283+
1284+
rows.append(
1285+
{
1286+
"Name": lease.name,
1287+
"Status": status_order.get(lease.status, f"4-{lease.status}"),
1288+
"User": user_label,
1289+
"Project": project_name,
1290+
"Start": lease.start_date.strftime("%Y-%m-%d %H:%M")
1291+
if lease.start_date
1292+
else "",
1293+
"End": lease.end_date.strftime("%Y-%m-%d %H:%M")
1294+
if lease.end_date
1295+
else "",
1296+
"Remaining": remaining_str,
1297+
"Total Hours": duration_hrs,
1298+
"# Nodes": len(lease.node_reservations),
1299+
"# FIPs": len(lease.fip_reservations),
1300+
"Created": lease.created_at.strftime("%Y-%m-%d %H:%M")
1301+
if lease.created_at
1302+
else "",
1303+
"Lease ID": lease.id,
1304+
"_is_user_lease": 0
1305+
if lease.user_id == connection().current_user_id
1306+
else 1,
1307+
}
1308+
)
1309+
12911310
df = pandas.DataFrame(rows)
12921311
df = pandas.DataFrame(rows)
12931312
df = df.sort_values(by=["_is_user_lease", "Status", "Created"])
12941313
df = df.drop(columns=["_is_user_lease"])
12951314

12961315
renderers = {
1297-
"Status": TextRenderer(
1298-
background_color=Expr(_status_color),
1299-
text_color="black",
1300-
),
1301-
1316+
"Status": TextRenderer(
1317+
background_color=Expr(_status_color),
1318+
text_color="black",
1319+
),
13021320
}
13031321

1304-
display(DataGrid(
1305-
df,
1306-
layout={"height": "400px", "width": "100%"},
1307-
column_widths={
1308-
"key": 30,
1309-
"Name": int(estimate_column_width(df, "Name")),
1310-
"Status": 120,
1311-
"Remaining": 80,
1312-
"Total Hours": 50,
1313-
"# Nodes": 30,
1314-
"# FIPs": 30,
1315-
"Project": 100,
1316-
"User": 75,
1317-
"Start": 95,
1318-
"End": 95,
1319-
"Created": 95,
1320-
"Lease ID": 30,
1321-
1322-
},
1323-
renderers=renderers
1324-
))
1322+
display(
1323+
DataGrid(
1324+
df,
1325+
layout={"height": "400px", "width": "100%"},
1326+
column_widths={
1327+
"key": 30,
1328+
"Name": int(estimate_column_width(df, "Name")),
1329+
"Status": 120,
1330+
"Remaining": 80,
1331+
"Total Hours": 50,
1332+
"# Nodes": 30,
1333+
"# FIPs": 30,
1334+
"Project": 100,
1335+
"User": 75,
1336+
"Start": 95,
1337+
"End": 95,
1338+
"Created": 95,
1339+
"Lease ID": 30,
1340+
},
1341+
renderers=renderers,
1342+
)
1343+
)
13251344

13261345

13271346
def _get_lease_from_blazar(ref: str):

0 commit comments

Comments
 (0)