syspy  0.1
syspy windows system information tool with python
cpu.py
Go to the documentation of this file.
1 __title__ = 'syspy'
2 __version__ = '0.1'
3 __author__ = 'Ali GOREN <goren.ali@yandex.com>'
4 __repo__ = 'https://github.com/aligoren/syspy'
5 __license__ = 'Apache v2.0 License'
6 
7 import psutil
8 
9 class CPU:
10 
11  def __init__(self):
12 
13  self.getCpuTimes
14  self.getCpuPercent
15  self.cpuPercentPerCpu
16  self.cpuTimesPercent
17  self.getCpuCount
18  self.getCpuAffinity
19 
20 
21  def getCpuTimes(self):
22  """
23  START listCpuValues
24 
25  http://stackoverflow.com/a/10058239/3821823
26  dict.items would return an iterable dict view object rather than a list.
27  We need to wrap the call onto a list in order to make the indexing possible
28 
29  END listCpuValues
30 
31  START Test Results
32 
33  >>> import psutil
34  >>> a = psutil.cpu_times()
35  >>> a.items()
36  Traceback (most recent call last):
37  File "<stdin>", line 1, in <module>
38  AttributeError: 'scputimes' object has no attribute 'items'
39  >>> b = a.__dict_
40  Traceback (most recent call last):
41  File "<stdin>", line 1, in <module>
42  AttributeError: 'scputimes' object has no attribute '__dict_'
43  >>> b = a.__dict__
44  >>> b.items()
45  ItemsView(OrderedDict([('user', 822.62451171875), ('system', 373.341796875), ('i
46  dle', 21677.9609375)]))
47  >>> list(b.items())
48  [('user', 822.62451171875), ('system', 373.341796875), ('idle', 21677.9609375)]
49  >>> list(b.items())[0]
50  ('user', 822.62451171875)
51  >>> list(b.items())[0][1]
52  822.62451171875
53  >>> ass = list(b.items())
54  >>> ass[0][1]
55  822.62451171875
56  >>> ass[1][1]
57  373.341796875
58  >>> ass[2][1]
59  21677.9609375
60  >>> psutil.cpu_times()
61  scputimes(user=934.0092163085938, system=431.310546875, idle=23977.853515625)
62  >>>
63 
64  END Test Results
65 
66  START return values
67 
68  https://www.safaribooksonline.com/library/view/python-cookbook-3rd/9781449357337/ch07s04.html
69 
70  END return values
71  """
72  cpuToDict = psutil.cpu_times()
73  cpuDict = cpuToDict.__dict__
74  listCpuValues = list(cpuDict.items())
75 
76  ret_val_user = "{0}".format(listCpuValues[0][1])
77  ret_val_system = "{0}".format(listCpuValues[1][1])
78  ret_val_idle = "{0}".format(listCpuValues[2][1])
79 
80  return ret_val_user, ret_val_system, ret_val_idle
81 
82  def getCpuPercent(self):
83  """
84  START Test Results
85 
86  >>> import psutil
87  >>> psutil.cpu_percent(interval=1)
88  0.7
89  >>> for x in range(6):
90  ... psutil.cpu_percent(interval=1)
91  ...
92  0.0
93  0.9
94  0.0
95  0.0
96  0.8
97  0.0
98 
99  END Test Results
100  """
101  currentCpuPercent = psutil.cpu_percent(interval=1)
102 
103  return currentCpuPercent
104 
105  def cpuPercentPerCpu(self):
106  """
107  START Test Results
108 
109  >>> import psutil
110  >>> for i in range(3):
111  ... psutil.cpu_percent(interval=1,percpu=True)
112  ...
113  [0.0, 1.6]
114  [0.1, 0.0]
115  [0.0, 0.1]
116  >>> a = psutil.cpu_percent(interval=1,percpu=True)
117  >>> a[0]
118  0.0
119  >>> a[0][1]
120  Traceback (most recent call last):
121  File "<stdin>", line 1, in <module>
122  TypeError: 'float' object is not subscriptable
123  >>> a[0][0]
124  Traceback (most recent call last):
125  File "<stdin>", line 1, in <module>
126  TypeError: 'float' object is not subscriptable
127  >>> list(a)
128  [0.0, 0.0]
129  >>> list(a)[0]
130  0.0
131  >>> list(a)[1]
132  0.0
133  >>> for i in range(3):
134  ... a = psutil.cpu_percent(interval=1,percpu=True)
135  ... list(a[i][i])
136  ...
137  Traceback (most recent call last):
138  File "<stdin>", line 3, in <module>
139  TypeError: 'float' object is not subscriptable
140  >>>
141 
142  END Test Results
143  """
144  cpuPerCpuPercent = psutil.cpu_percent(interval=1, percpu=True)
145 
146  return cpuPerCpuPercent
147 
148  def cpuTimesPercent(self):
149  """
150  START Test Results
151 
152  >>> import psutil
153  >>> for x in range(4):
154  ... psutil.cpu_times_percent(interval=1, percpu=False)
155  ...
156  scputimes(user=6.9, system=0.0, idle=93.1)
157  scputimes(user=0.0, system=0.9, idle=99.1)
158  scputimes(user=0.0, system=0.0, idle=100.0)
159  scputimes(user=0.0, system=0.0, idle=100.0)
160  >>> for x in range(4):
161  ... psutil.cpu_times_percent(interval=1, percpu=True)
162  ...
163  [scputimes(user=0.0, system=0.0, idle=100.0), scputimes(user=0.0, system=0.1, id
164  le=99.9)]
165  [scputimes(user=0.0, system=1.4, idle=98.6), scputimes(user=0.0, system=0.0, idl
166  e=100.0)]
167  [scputimes(user=0.0, system=0.1, idle=99.9), scputimes(user=0.0, system=0.0, idl
168  e=100.0)]
169  [scputimes(user=0.0, system=0.0, idle=100.0), scputimes(user=0.0, system=1.5, id
170  le=98.5)]
171  >>> a = psutil.cpu_times_percent(interval=1 percpu=False)
172  File "<stdin>", line 1
173  a = psutil.cpu_times_percent(interval=1 percpu=False)
174  ^
175  SyntaxError: invalid syntax
176  >>> a = psutil.cpu_times_percent(interval=1, percpu=False)
177  >>> list(a)
178  [0.0, 0.0, 100.0]
179  >>> ab = list(a)
180  >>> ab[0]
181  0.0
182  >>> ab[1]
183  0.0
184  >>> ab[2]
185  100.0
186  >>> ab.items()
187  Traceback (most recent call last):
188  File "<stdin>", line 1, in <module>
189  AttributeError: 'list' object has no attribute 'items'
190  >>> a = psutil.cpu_times_percent(interval=1, percpu=True)
191  >>> ab = list(a)
192  >>> ab[0]
193  scputimes(user=0.0, system=0.0, idle=100.0)
194  >>> ab[0][1]
195  0.0
196  >>> ab[0][2]
197  100.0
198  >>> ab[0][0]
199  0.0
200  >>>
201 
202  END Test Results
203  """
204  timesPercent = psutil.cpu_times_percent(interval=1, percpu=False)
205  percentList = list(timesPercent)
206 
207  ret_val_user = "{0}".format(percentList[0])
208  ret_val_system = "{0}".format(percentList[1])
209  ret_val_idle = "{0}".format(percentList[2])
210 
211  return ret_val_user, ret_val_system, ret_val_idle
212 
213  def getCpuCount(self):
214  """
215  START Test Results
216 
217  >>> import psutil
218  >>> psutil.cpu_count()
219  2
220  >>> psutil.cpu_count(logical=False)
221  2
222  >>> psutil.cpu_count(logical=True)
223  2
224  >>>
225 
226  END Test Results
227  """
228  cpuCount = "{0}".format(psutil.cpu_count())
229  cpuCountLogicalFalse = "{0}".format(psutil.cpu_count(logical=False))
230 
231  return cpuCount, cpuCountLogicalFalse
232 
233  def getCpuAffinity(self):
234  """
235  START Test Results
236 
237  >>> import psutil
238  >>> p = psutil.Process()
239  >>> p.cpuaffinity()
240  Traceback (most recent call last):
241  File "<stdin>", line 1, in <module>
242  AttributeError: 'Process' object has no attribute 'cpuaffinity'
243  >>> p.cpu_affinity()
244  [0, 1]
245  >>> p.cpu_affinity()
246  [0, 1]
247  >>> p.cpu_affinity()
248  [0, 1]
249  >>> a = list(range(psutil.cpu_count()))
250  >>> p.cpu_affinity(a)
251  >>> p.cpu_affinity()
252  [0, 1]
253  >>> p.cpu_affinity(a)
254  >>>
255 
256  END Test Results
257  """
258  cAffinity = psutil.Process()
259  c_affinity = "{0}".format(cAffinity.cpu_affinity())
260 
261  return c_affinity
262 
263 
def cpuTimesPercent(self)
Definition: cpu.py:148
def getCpuTimes(self)
Definition: cpu.py:21
def cpuPercentPerCpu(self)
Definition: cpu.py:105
def getCpuAffinity(self)
Definition: cpu.py:233
def __init__(self)
Definition: cpu.py:11
def getCpuPercent(self)
Definition: cpu.py:82
def getCpuCount(self)
Definition: cpu.py:213