diff mbox

[v2,10/11] tools/kvm_stat: sort '-f help' output

Message ID 20171211112529.33706-11-raspl@linux.vnet.ibm.com (mailing list archive)
State New, archived
Headers show

Commit Message

Stefan Raspl Dec. 11, 2017, 11:25 a.m. UTC
From: Stefan Raspl <stefan.raspl@de.ibm.com>

Sort the fields returned by specifying '-f help' on the command line.
While at it, simplify the code a bit, indent the output and eliminate an
extra blank line at the beginning.

Signed-off-by: Stefan Raspl <raspl@linux.vnet.ibm.com>
---
 tools/kvm/kvm_stat/kvm_stat | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)

Comments

Janosch Frank Dec. 11, 2017, 12:20 p.m. UTC | #1
On 11.12.2017 12:25, Stefan Raspl wrote:
> From: Stefan Raspl <stefan.raspl@de.ibm.com>
> 
> Sort the fields returned by specifying '-f help' on the command line.
> While at it, simplify the code a bit, indent the output and eliminate an
> extra blank line at the beginning.
> 
> Signed-off-by: Stefan Raspl <raspl@linux.vnet.ibm.com>
> ---
>  tools/kvm/kvm_stat/kvm_stat | 18 ++++++++----------
>  1 file changed, 8 insertions(+), 10 deletions(-)
> 
> diff --git a/tools/kvm/kvm_stat/kvm_stat b/tools/kvm/kvm_stat/kvm_stat
> index 42c34b8818f7..929c8379d82a 100755
> --- a/tools/kvm/kvm_stat/kvm_stat
> +++ b/tools/kvm/kvm_stat/kvm_stat
> @@ -33,6 +33,7 @@ import resource
>  import struct
>  import re
>  import subprocess
> +from sets import Set

What's the reason for this import, shouldn't set be built in from at
least 2.7 on? It even seems to be 2.4. The module should be deprecated
from 2.6 on.

>  from collections import defaultdict
> 
>  VMX_EXIT_REASONS = {
> @@ -1572,17 +1573,14 @@ def main():
> 
>      stats = Stats(options)
> 
> -    if options.fields == "help":
> +    if options.fields == 'help':
>          stats.fields_filter = None
> -        event_list = "\n"
> -        s = stats.get()
> -        for key in s.keys():
> -            if key.find('(') != -1:
> -                key = key[0:key.find('(')]
> -            if event_list.find('\n' + key + '\n') == -1:
> -                event_list += key + '\n'
> -        sys.stdout.write(event_list)
> -        return ""
> +        event_list = []
> +        for key in stats.get().keys():
> +            event_list.append(key.split('(', 1)[0])

Definitely better.

> +        event_list.sort()
> +        sys.stdout.write('  ' + '\n  '.join(sorted(Set(event_list))) + '\n')

You just sorted the list, why do you need a set and why do you sort it
again?

> +        sys.exit(0)
> 
>      if options.log:
>          log(stats)
>
Stefan Raspl Dec. 12, 2017, 11:30 a.m. UTC | #2
On 11.12.2017 13:20, Janosch Frank wrote:
> On 11.12.2017 12:25, Stefan Raspl wrote:
>> From: Stefan Raspl <stefan.raspl@de.ibm.com>
>>
>> Sort the fields returned by specifying '-f help' on the command line.
>> While at it, simplify the code a bit, indent the output and eliminate an
>> extra blank line at the beginning.
>>
>> Signed-off-by: Stefan Raspl <raspl@linux.vnet.ibm.com>
>> ---
>>  tools/kvm/kvm_stat/kvm_stat | 18 ++++++++----------
>>  1 file changed, 8 insertions(+), 10 deletions(-)
>>
>> diff --git a/tools/kvm/kvm_stat/kvm_stat b/tools/kvm/kvm_stat/kvm_stat
>> index 42c34b8818f7..929c8379d82a 100755
>> --- a/tools/kvm/kvm_stat/kvm_stat
>> +++ b/tools/kvm/kvm_stat/kvm_stat
>> @@ -33,6 +33,7 @@ import resource
>>  import struct
>>  import re
>>  import subprocess
>> +from sets import Set
> 
> What's the reason for this import, shouldn't set be built in from at
> least 2.7 on? It even seems to be 2.4. The module should be deprecated
> from 2.6 on.

without that line:
$ python --version
Python 2.7.12
$ ./kvm_stat -f help
Traceback (most recent call last):
  File "./kvm_stat", line 1600, in <module>
    main()
  File "./kvm_stat", line 1588, in main
    sys.stdout.write('  ' + '\n  '.join(sorted(Set(event_list))) + '\n')
NameError: global name 'Set' is not defined
>> +        event_list.sort()
>> +        sys.stdout.write('  ' + '\n  '.join(sorted(Set(event_list))) + '\n')
> 
> You just sorted the list, why do you need a set and why do you sort it
> again?

Oooops - my bad, we can drop the first sort.

Thx!
Stefan Raspl Dec. 12, 2017, 1:59 p.m. UTC | #3
On 11.12.2017 13:20, Janosch Frank wrote:
> On 11.12.2017 12:25, Stefan Raspl wrote:
>> diff --git a/tools/kvm/kvm_stat/kvm_stat b/tools/kvm/kvm_stat/kvm_stat
>> index 42c34b8818f7..929c8379d82a 100755
>> --- a/tools/kvm/kvm_stat/kvm_stat
>> +++ b/tools/kvm/kvm_stat/kvm_stat
>> @@ -33,6 +33,7 @@ import resource
>>  import struct
>>  import re
>>  import subprocess
>> +from sets import Set
> 
> What's the reason for this import, shouldn't set be built in from at
> least 2.7 on? It even seems to be 2.4. The module should be deprecated
> from 2.6 on.
> 
>> +        event_list.sort()
>> +        sys.stdout.write('  ' + '\n  '.join(sorted(Set(event_list))) + '\n')

Ah, now I know what mean: Use the built-in 'set' instead of 'Set'?
Yup, good point, will do.

Ciao,
Stefan
diff mbox

Patch

diff --git a/tools/kvm/kvm_stat/kvm_stat b/tools/kvm/kvm_stat/kvm_stat
index 42c34b8818f7..929c8379d82a 100755
--- a/tools/kvm/kvm_stat/kvm_stat
+++ b/tools/kvm/kvm_stat/kvm_stat
@@ -33,6 +33,7 @@  import resource
 import struct
 import re
 import subprocess
+from sets import Set
 from collections import defaultdict
 
 VMX_EXIT_REASONS = {
@@ -1572,17 +1573,14 @@  def main():
 
     stats = Stats(options)
 
-    if options.fields == "help":
+    if options.fields == 'help':
         stats.fields_filter = None
-        event_list = "\n"
-        s = stats.get()
-        for key in s.keys():
-            if key.find('(') != -1:
-                key = key[0:key.find('(')]
-            if event_list.find('\n' + key + '\n') == -1:
-                event_list += key + '\n'
-        sys.stdout.write(event_list)
-        return ""
+        event_list = []
+        for key in stats.get().keys():
+            event_list.append(key.split('(', 1)[0])
+        event_list.sort()
+        sys.stdout.write('  ' + '\n  '.join(sorted(Set(event_list))) + '\n')
+        sys.exit(0)
 
     if options.log:
         log(stats)