Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Gelencsér Szabolcs
/
cloud
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
912ccc1b
authored
Mar 19, 2014
by
Őry Máté
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
common: cleanup HumanSortField.get_normalized_value
parent
0c795347
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
18 additions
and
5 deletions
+18
-5
circle/common/models.py
+18
-5
No files found.
circle/common/models.py
View file @
912ccc1b
...
@@ -174,9 +174,19 @@ class HumanSortField(CharField):
...
@@ -174,9 +174,19 @@ class HumanSortField(CharField):
def
get_monitored_value
(
self
,
instance
):
def
get_monitored_value
(
self
,
instance
):
return
getattr
(
instance
,
self
.
monitor
)
return
getattr
(
instance
,
self
.
monitor
)
def
get_normalized_value
(
self
,
val
):
@staticmethod
def
_partition
(
s
,
pred
):
def
partition
(
s
,
pred
):
"""Partition a deque of chars to a tuple of a
- string of the longest prefix matching pred,
- string of the longest prefix after the former one not matching,
- deque of the remaining characters.
>>> HumanSortField._partition(deque("1234abc567"),
... lambda s: s.isdigit())
('1234', 'abc', deque(['5', '6', '7']))
>>> HumanSortField._partition(deque("12ab"), lambda s: s.isalpha())
('', '12', deque(['a', 'b']))
"""
match
,
notmatch
=
deque
(),
deque
()
match
,
notmatch
=
deque
(),
deque
()
while
s
and
pred
(
s
[
0
]):
while
s
and
pred
(
s
[
0
]):
match
.
append
(
s
.
popleft
())
match
.
append
(
s
.
popleft
())
...
@@ -184,12 +194,15 @@ class HumanSortField(CharField):
...
@@ -184,12 +194,15 @@ class HumanSortField(CharField):
notmatch
.
append
(
s
.
popleft
())
notmatch
.
append
(
s
.
popleft
())
return
(
''
.
join
(
match
),
''
.
join
(
notmatch
),
s
)
return
(
''
.
join
(
match
),
''
.
join
(
notmatch
),
s
)
def
get_normalized_value
(
self
,
val
):
logger
.
debug
(
'Normalizing value:
%
s'
,
val
)
logger
.
debug
(
'Normalizing value:
%
s'
,
val
)
norm
=
""
norm
=
""
val
=
deque
(
val
)
val
=
deque
(
val
)
while
val
:
while
val
:
numbers
,
letters
,
val
=
partition
(
val
,
lambda
s
:
s
[
0
]
.
isdigit
())
numbers
,
letters
,
val
=
self
.
_partition
(
val
,
norm
+=
numbers
and
numbers
.
rjust
(
self
.
maximum_number_length
,
'0'
)
lambda
s
:
s
[
0
]
.
isdigit
())
if
numbers
:
norm
+=
numbers
.
rjust
(
self
.
maximum_number_length
,
'0'
)
norm
+=
letters
norm
+=
letters
logger
.
debug
(
'Normalized value:
%
s'
,
norm
)
logger
.
debug
(
'Normalized value:
%
s'
,
norm
)
return
norm
return
norm
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment