1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Introduce array_to_string() utility function; add port_list property to Service

This commit is contained in:
Jeremy Stretch
2020-09-21 13:31:38 -04:00
parent f97d8963f2
commit e77f1bdd85
5 changed files with 24 additions and 18 deletions

View File

@@ -1,6 +1,7 @@
import datetime
import json
from collections import OrderedDict
from itertools import count, groupby
from django.core.serializers import serialize
from django.db.models import Count, OuterRef, Subquery
@@ -282,6 +283,16 @@ def curry(_curried_func, *args, **kwargs):
return _curried
def array_to_string(array):
"""
Generate an efficient, human-friendly string from a set of integers. Intended for use with ArrayField.
For example:
[0, 1, 2, 10, 14, 15, 16] => "0-2, 10, 14-16"
"""
group = (list(x) for _, x in groupby(sorted(array), lambda x, c=count(): next(c) - x))
return ', '.join('-'.join(map(str, (g[0], g[-1])[:len(g)])) for g in group)
#
# Fake request object
#