Extract all GPU name, model and GPU ram Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)Calculating sum of primes using the CPU and GPUregex to extract version infoExtract Pages from PDF based on search in pythonExtract entities from sentencesAgent-based model performance issue with list comprehensionOptimize GPU usage for real-time object detection from camera with TensorFlow GPU and OpenCVExtract and sort all numbers that appear after ',Extract name, address and phone number from some web pages using multiprocessingRandom name generator from JSON fileHackerrank All Women's Codesprint 2019: Name the Product
Generate an RGB colour grid
Find 108 by using 3,4,6
When a candle burns, why does the top of wick glow if bottom of flame is hottest?
Why aren't air breathing engines used as small first stages?
How were pictures turned from film to a big picture in a picture frame before digital scanning?
How to react to hostile behavior from a senior developer?
Central Vacuuming: Is it worth it, and how does it compare to normal vacuuming?
What does it mean that physics no longer uses mechanical models to describe phenomena?
Project Euler #1 in C++
Chebyshev inequality in terms of RMS
Hangman Game with C++
Performance gap between bool std:vector and array
What is a fractional matching?
Take 2! Is this homebrew Lady of Pain warlock patron balanced?
Why do we need to use the builder design pattern when we can do the same thing with setters?
What was the first language to use conditional keywords?
How to write the following sign?
SF book about people trapped in a series of worlds they imagine
Can a new player join a group only when a new campaign starts?
Trademark violation for app?
Are all finite dimensional hilbert spaces isomorphic to spaces with Euclidean norms?
Did Deadpool rescue all of the X-Force?
What initially awakened the Balrog?
Question about debouncing - delay of state change
Extract all GPU name, model and GPU ram
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)Calculating sum of primes using the CPU and GPUregex to extract version infoExtract Pages from PDF based on search in pythonExtract entities from sentencesAgent-based model performance issue with list comprehensionOptimize GPU usage for real-time object detection from camera with TensorFlow GPU and OpenCVExtract and sort all numbers that appear after ',Extract name, address and phone number from some web pages using multiprocessingRandom name generator from JSON fileHackerrank All Women's Codesprint 2019: Name the Product
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
$begingroup$
I currently use this code to extract the required GPU data. Can you review this method for efficiency or best coding practices?
Code
import wmi
c = wmi.WMI()
def gpu_check():
gpu_ouy =["fu","7"]
gpu_a =""
gpu_b =""
ass = str(c.Win32_VideoController()[0])
asss = str(c.Win32_VideoController()[1])
aq = ass.split()
for x in range(len(c.Win32_VideoController())):
ass = str(c.Win32_VideoController()[x])
saa = ass.split()
saa.remove("instance")
saa.remove("of")
saa.remove("Win32_VideoController")
saa.remove("")
saa.remove("AdapterCompatibility")
saa.remove("AdapterDACType")
while "=" in saa:
saa.remove("=")
saa.remove(";")
saa.remove("SystemName")
saa.remove("Description")
saa.remove("InstalledDisplayDrivers")
saa.remove("DeviceID")
saa.remove("DriverDate")
saa.remove("Name")
saa.remove("Status")
saa.remove("Caption")
saa.remove("InfFilename")
saa.remove("ConfigManagerUserConfig")
saa.remove("ConfigManagerErrorCode")
saa.remove("InfSection")
saa.remove("DriverVersion")
if "CurrentScanMode" in saa:
saa.remove("CurrentScanMode")
saa.remove("SystemCreationClassName")
saa.remove("CreationClassName")
if "CurrentRefreshRate" in saa:
saa.remove("CurrentRefreshRate")
if "CurrentHorizontalResolution" in saa:
saa.remove("CurrentHorizontalResolution")
if "CurrentVerticalResolution" in saa:
saa.remove("CurrentVerticalResolution")
if "CurrentNumberOfColors" in saa:
saa.remove("CurrentNumberOfColors")
if "MinRefreshRate" in saa:
saa.remove("MinRefreshRate")
if 'colors";' in saa:
saa.remove('colors";')
if "CurrentBitsPerPixel" in saa:
saa.remove("CurrentBitsPerPixel")
if "MaxRefreshRate" in saa:
saa.remove("MaxRefreshRate")
saa.remove("VideoProcessor")
saa.remove("PNPDeviceID")
if "CurrentNumberOfColumns" in saa:
saa.remove("CurrentNumberOfColumns")
if "CurrentNumberOfRows" in saa:
saa.remove("CurrentNumberOfRows")
saa.remove("Monochrome")
saa.remove("Availability")
del saa[5]
if '"Integrated' in saa:
saa.remove('"Integrated')
if '"Internal";' in saa:
saa.remove('"Internal";')
if 'VideoModeDescription' in saa:
saa.remove('VideoModeDescription')
saa.remove('AdapterRAM')
if 'RAMDAC";' in saa:
saa.remove('RAMDAC";')
sqa =int(((int(str(saa[1]).replace(";",""))/1024)/1024)/1000)
saa[1] = sqa
else:
sqa =int(((int(str(saa[2]).replace(";",""))/1024)/1024)/1000)
saa[2] = sqa
if 'Corporation";' in saa:
saa.remove('Corporation";')
while "FALSE;" in saa:
saa.remove("FALSE;")
gpu_a =""
gpu_b =""
if saa[6] =="0;":
gpu_a = str(saa[2]).replace('"',"")+" "+saa[3]+" "+saa[4]+" "+str(saa[5]).replace('";',"")+" "+str(saa[1])+" GB"
gpu_ouy[0] = str(gpu_a)
else:
gpu_b = str(saa[2]).replace('"',"")+" "+saa[3]+" "+saa[4]+" "+str(saa[5]).replace('";',"")+" "+str(saa[6]).replace('";',"")+" "+str(saa[1])+" GB"
gpu_ouy[1] = gpu_b
return gpu_ouy
python performance python-3.x
New contributor
tadas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
add a comment |
$begingroup$
I currently use this code to extract the required GPU data. Can you review this method for efficiency or best coding practices?
Code
import wmi
c = wmi.WMI()
def gpu_check():
gpu_ouy =["fu","7"]
gpu_a =""
gpu_b =""
ass = str(c.Win32_VideoController()[0])
asss = str(c.Win32_VideoController()[1])
aq = ass.split()
for x in range(len(c.Win32_VideoController())):
ass = str(c.Win32_VideoController()[x])
saa = ass.split()
saa.remove("instance")
saa.remove("of")
saa.remove("Win32_VideoController")
saa.remove("")
saa.remove("AdapterCompatibility")
saa.remove("AdapterDACType")
while "=" in saa:
saa.remove("=")
saa.remove(";")
saa.remove("SystemName")
saa.remove("Description")
saa.remove("InstalledDisplayDrivers")
saa.remove("DeviceID")
saa.remove("DriverDate")
saa.remove("Name")
saa.remove("Status")
saa.remove("Caption")
saa.remove("InfFilename")
saa.remove("ConfigManagerUserConfig")
saa.remove("ConfigManagerErrorCode")
saa.remove("InfSection")
saa.remove("DriverVersion")
if "CurrentScanMode" in saa:
saa.remove("CurrentScanMode")
saa.remove("SystemCreationClassName")
saa.remove("CreationClassName")
if "CurrentRefreshRate" in saa:
saa.remove("CurrentRefreshRate")
if "CurrentHorizontalResolution" in saa:
saa.remove("CurrentHorizontalResolution")
if "CurrentVerticalResolution" in saa:
saa.remove("CurrentVerticalResolution")
if "CurrentNumberOfColors" in saa:
saa.remove("CurrentNumberOfColors")
if "MinRefreshRate" in saa:
saa.remove("MinRefreshRate")
if 'colors";' in saa:
saa.remove('colors";')
if "CurrentBitsPerPixel" in saa:
saa.remove("CurrentBitsPerPixel")
if "MaxRefreshRate" in saa:
saa.remove("MaxRefreshRate")
saa.remove("VideoProcessor")
saa.remove("PNPDeviceID")
if "CurrentNumberOfColumns" in saa:
saa.remove("CurrentNumberOfColumns")
if "CurrentNumberOfRows" in saa:
saa.remove("CurrentNumberOfRows")
saa.remove("Monochrome")
saa.remove("Availability")
del saa[5]
if '"Integrated' in saa:
saa.remove('"Integrated')
if '"Internal";' in saa:
saa.remove('"Internal";')
if 'VideoModeDescription' in saa:
saa.remove('VideoModeDescription')
saa.remove('AdapterRAM')
if 'RAMDAC";' in saa:
saa.remove('RAMDAC";')
sqa =int(((int(str(saa[1]).replace(";",""))/1024)/1024)/1000)
saa[1] = sqa
else:
sqa =int(((int(str(saa[2]).replace(";",""))/1024)/1024)/1000)
saa[2] = sqa
if 'Corporation";' in saa:
saa.remove('Corporation";')
while "FALSE;" in saa:
saa.remove("FALSE;")
gpu_a =""
gpu_b =""
if saa[6] =="0;":
gpu_a = str(saa[2]).replace('"',"")+" "+saa[3]+" "+saa[4]+" "+str(saa[5]).replace('";',"")+" "+str(saa[1])+" GB"
gpu_ouy[0] = str(gpu_a)
else:
gpu_b = str(saa[2]).replace('"',"")+" "+saa[3]+" "+saa[4]+" "+str(saa[5]).replace('";',"")+" "+str(saa[6]).replace('";',"")+" "+str(saa[1])+" GB"
gpu_ouy[1] = gpu_b
return gpu_ouy
python performance python-3.x
New contributor
tadas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
$begingroup$
What isc? Isc.Win32_VideoController()a simple accessor?
$endgroup$
– greybeard
2 days ago
2
$begingroup$
Welcome to Code Review. Do not comment comments asking for clarification: Edit your question, the code in it unless reviews have started.
$endgroup$
– greybeard
2 days ago
17
$begingroup$
assis a very bad variable name.
$endgroup$
– 422_unprocessable_entity
2 days ago
21
$begingroup$
ass.split()is an even worse statement.
$endgroup$
– Raimund Krämer
2 days ago
3
$begingroup$
I like the Javascript spread operator though,...ass
$endgroup$
– JollyJoker
yesterday
add a comment |
$begingroup$
I currently use this code to extract the required GPU data. Can you review this method for efficiency or best coding practices?
Code
import wmi
c = wmi.WMI()
def gpu_check():
gpu_ouy =["fu","7"]
gpu_a =""
gpu_b =""
ass = str(c.Win32_VideoController()[0])
asss = str(c.Win32_VideoController()[1])
aq = ass.split()
for x in range(len(c.Win32_VideoController())):
ass = str(c.Win32_VideoController()[x])
saa = ass.split()
saa.remove("instance")
saa.remove("of")
saa.remove("Win32_VideoController")
saa.remove("")
saa.remove("AdapterCompatibility")
saa.remove("AdapterDACType")
while "=" in saa:
saa.remove("=")
saa.remove(";")
saa.remove("SystemName")
saa.remove("Description")
saa.remove("InstalledDisplayDrivers")
saa.remove("DeviceID")
saa.remove("DriverDate")
saa.remove("Name")
saa.remove("Status")
saa.remove("Caption")
saa.remove("InfFilename")
saa.remove("ConfigManagerUserConfig")
saa.remove("ConfigManagerErrorCode")
saa.remove("InfSection")
saa.remove("DriverVersion")
if "CurrentScanMode" in saa:
saa.remove("CurrentScanMode")
saa.remove("SystemCreationClassName")
saa.remove("CreationClassName")
if "CurrentRefreshRate" in saa:
saa.remove("CurrentRefreshRate")
if "CurrentHorizontalResolution" in saa:
saa.remove("CurrentHorizontalResolution")
if "CurrentVerticalResolution" in saa:
saa.remove("CurrentVerticalResolution")
if "CurrentNumberOfColors" in saa:
saa.remove("CurrentNumberOfColors")
if "MinRefreshRate" in saa:
saa.remove("MinRefreshRate")
if 'colors";' in saa:
saa.remove('colors";')
if "CurrentBitsPerPixel" in saa:
saa.remove("CurrentBitsPerPixel")
if "MaxRefreshRate" in saa:
saa.remove("MaxRefreshRate")
saa.remove("VideoProcessor")
saa.remove("PNPDeviceID")
if "CurrentNumberOfColumns" in saa:
saa.remove("CurrentNumberOfColumns")
if "CurrentNumberOfRows" in saa:
saa.remove("CurrentNumberOfRows")
saa.remove("Monochrome")
saa.remove("Availability")
del saa[5]
if '"Integrated' in saa:
saa.remove('"Integrated')
if '"Internal";' in saa:
saa.remove('"Internal";')
if 'VideoModeDescription' in saa:
saa.remove('VideoModeDescription')
saa.remove('AdapterRAM')
if 'RAMDAC";' in saa:
saa.remove('RAMDAC";')
sqa =int(((int(str(saa[1]).replace(";",""))/1024)/1024)/1000)
saa[1] = sqa
else:
sqa =int(((int(str(saa[2]).replace(";",""))/1024)/1024)/1000)
saa[2] = sqa
if 'Corporation";' in saa:
saa.remove('Corporation";')
while "FALSE;" in saa:
saa.remove("FALSE;")
gpu_a =""
gpu_b =""
if saa[6] =="0;":
gpu_a = str(saa[2]).replace('"',"")+" "+saa[3]+" "+saa[4]+" "+str(saa[5]).replace('";',"")+" "+str(saa[1])+" GB"
gpu_ouy[0] = str(gpu_a)
else:
gpu_b = str(saa[2]).replace('"',"")+" "+saa[3]+" "+saa[4]+" "+str(saa[5]).replace('";',"")+" "+str(saa[6]).replace('";',"")+" "+str(saa[1])+" GB"
gpu_ouy[1] = gpu_b
return gpu_ouy
python performance python-3.x
New contributor
tadas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
I currently use this code to extract the required GPU data. Can you review this method for efficiency or best coding practices?
Code
import wmi
c = wmi.WMI()
def gpu_check():
gpu_ouy =["fu","7"]
gpu_a =""
gpu_b =""
ass = str(c.Win32_VideoController()[0])
asss = str(c.Win32_VideoController()[1])
aq = ass.split()
for x in range(len(c.Win32_VideoController())):
ass = str(c.Win32_VideoController()[x])
saa = ass.split()
saa.remove("instance")
saa.remove("of")
saa.remove("Win32_VideoController")
saa.remove("")
saa.remove("AdapterCompatibility")
saa.remove("AdapterDACType")
while "=" in saa:
saa.remove("=")
saa.remove(";")
saa.remove("SystemName")
saa.remove("Description")
saa.remove("InstalledDisplayDrivers")
saa.remove("DeviceID")
saa.remove("DriverDate")
saa.remove("Name")
saa.remove("Status")
saa.remove("Caption")
saa.remove("InfFilename")
saa.remove("ConfigManagerUserConfig")
saa.remove("ConfigManagerErrorCode")
saa.remove("InfSection")
saa.remove("DriverVersion")
if "CurrentScanMode" in saa:
saa.remove("CurrentScanMode")
saa.remove("SystemCreationClassName")
saa.remove("CreationClassName")
if "CurrentRefreshRate" in saa:
saa.remove("CurrentRefreshRate")
if "CurrentHorizontalResolution" in saa:
saa.remove("CurrentHorizontalResolution")
if "CurrentVerticalResolution" in saa:
saa.remove("CurrentVerticalResolution")
if "CurrentNumberOfColors" in saa:
saa.remove("CurrentNumberOfColors")
if "MinRefreshRate" in saa:
saa.remove("MinRefreshRate")
if 'colors";' in saa:
saa.remove('colors";')
if "CurrentBitsPerPixel" in saa:
saa.remove("CurrentBitsPerPixel")
if "MaxRefreshRate" in saa:
saa.remove("MaxRefreshRate")
saa.remove("VideoProcessor")
saa.remove("PNPDeviceID")
if "CurrentNumberOfColumns" in saa:
saa.remove("CurrentNumberOfColumns")
if "CurrentNumberOfRows" in saa:
saa.remove("CurrentNumberOfRows")
saa.remove("Monochrome")
saa.remove("Availability")
del saa[5]
if '"Integrated' in saa:
saa.remove('"Integrated')
if '"Internal";' in saa:
saa.remove('"Internal";')
if 'VideoModeDescription' in saa:
saa.remove('VideoModeDescription')
saa.remove('AdapterRAM')
if 'RAMDAC";' in saa:
saa.remove('RAMDAC";')
sqa =int(((int(str(saa[1]).replace(";",""))/1024)/1024)/1000)
saa[1] = sqa
else:
sqa =int(((int(str(saa[2]).replace(";",""))/1024)/1024)/1000)
saa[2] = sqa
if 'Corporation";' in saa:
saa.remove('Corporation";')
while "FALSE;" in saa:
saa.remove("FALSE;")
gpu_a =""
gpu_b =""
if saa[6] =="0;":
gpu_a = str(saa[2]).replace('"',"")+" "+saa[3]+" "+saa[4]+" "+str(saa[5]).replace('";',"")+" "+str(saa[1])+" GB"
gpu_ouy[0] = str(gpu_a)
else:
gpu_b = str(saa[2]).replace('"',"")+" "+saa[3]+" "+saa[4]+" "+str(saa[5]).replace('";',"")+" "+str(saa[6]).replace('";',"")+" "+str(saa[1])+" GB"
gpu_ouy[1] = gpu_b
return gpu_ouy
python performance python-3.x
python performance python-3.x
New contributor
tadas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
tadas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 2 days ago
tadas
New contributor
tadas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 2 days ago
tadastadas
336
336
New contributor
tadas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
tadas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
tadas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$begingroup$
What isc? Isc.Win32_VideoController()a simple accessor?
$endgroup$
– greybeard
2 days ago
2
$begingroup$
Welcome to Code Review. Do not comment comments asking for clarification: Edit your question, the code in it unless reviews have started.
$endgroup$
– greybeard
2 days ago
17
$begingroup$
assis a very bad variable name.
$endgroup$
– 422_unprocessable_entity
2 days ago
21
$begingroup$
ass.split()is an even worse statement.
$endgroup$
– Raimund Krämer
2 days ago
3
$begingroup$
I like the Javascript spread operator though,...ass
$endgroup$
– JollyJoker
yesterday
add a comment |
$begingroup$
What isc? Isc.Win32_VideoController()a simple accessor?
$endgroup$
– greybeard
2 days ago
2
$begingroup$
Welcome to Code Review. Do not comment comments asking for clarification: Edit your question, the code in it unless reviews have started.
$endgroup$
– greybeard
2 days ago
17
$begingroup$
assis a very bad variable name.
$endgroup$
– 422_unprocessable_entity
2 days ago
21
$begingroup$
ass.split()is an even worse statement.
$endgroup$
– Raimund Krämer
2 days ago
3
$begingroup$
I like the Javascript spread operator though,...ass
$endgroup$
– JollyJoker
yesterday
$begingroup$
What is
c? Is c.Win32_VideoController() a simple accessor?$endgroup$
– greybeard
2 days ago
$begingroup$
What is
c? Is c.Win32_VideoController() a simple accessor?$endgroup$
– greybeard
2 days ago
2
2
$begingroup$
Welcome to Code Review. Do not comment comments asking for clarification: Edit your question, the code in it unless reviews have started.
$endgroup$
– greybeard
2 days ago
$begingroup$
Welcome to Code Review. Do not comment comments asking for clarification: Edit your question, the code in it unless reviews have started.
$endgroup$
– greybeard
2 days ago
17
17
$begingroup$
ass is a very bad variable name.$endgroup$
– 422_unprocessable_entity
2 days ago
$begingroup$
ass is a very bad variable name.$endgroup$
– 422_unprocessable_entity
2 days ago
21
21
$begingroup$
ass.split() is an even worse statement.$endgroup$
– Raimund Krämer
2 days ago
$begingroup$
ass.split() is an even worse statement.$endgroup$
– Raimund Krämer
2 days ago
3
3
$begingroup$
I like the Javascript spread operator though,
...ass$endgroup$
– JollyJoker
yesterday
$begingroup$
I like the Javascript spread operator though,
...ass$endgroup$
– JollyJoker
yesterday
add a comment |
3 Answers
3
active
oldest
votes
$begingroup$
I fully agree with the answer by @BaileyParker. I have basically no idea what any of your variables are. Or what your code is actually supposed to achieve. And indeed, there is probably a better way to achieve what you want in this case. However, knowing how to do string and list parsing efficiently is also sometimes necessary, so this is how you could have done that better.
In your code, most of the lines are spent on removing unneeded strings from a list. Instead of manually removing each one of them (and even adding guards for the ones you know might not even exist in the list, hoping you don't miss any of those), just define a blacklist of terms you never want in your list and exclude them. If you choose a set as a data structure for that blacklist, you only need to iterate once over your list and checking if each element is in the blacklist is $mathcalO(1)$, making this algorithm $mathcalO(n)$.
Your code on the other hand is $mathcalO(n)$ for every single term you remove, because the whole list needs to be checked for the item in the worst case, and twice that if you first check if a term exists in the list with in, which is also $mathcalO(n)$. This makes your code $mathcalO(nk)$ with $n$ the list of methods and $k$ the number of terms you want removed.
blacklist = "instance", "of", ...
for methods in c.Win32_VideoController():
methods = [m for m in str(methods).split() if m not in blacklist]
# do something with it
$endgroup$
add a comment |
$begingroup$
Let's ignore efficiency. It took me five minutes to figure out what you were trying to do. In the kindest way possible, this is a mess. But let's fix that!
Your variable names are terrible. I have no idea what an ass is. What about an asss? Surely that's a bad idea, because it's very easy to mistake one for the other. Even if it wasn't, what are they? A variable name should describe what it holds. A bunch of random characters aren't helpful. Same goes for gpu_ouy. Is that a typo? What are gpu_a and gpu_b? What is aq? What is c? What is saa? None of these variables names help someone reading your code understand their function.
Don't use len in a for loop. The idiomatic python approach is to just iterate over the collection (and let it deal with lengths and indices). So instead of:
for x in range(len(c.Win32_VideoController())):
ass = str(c.Win32_VideoController()[x])
Use this:
for controller in c.Win32_VideoController():
# do something with controller...
pass
Some random points before we get to the big issue:
- In
sqa =int(((int(str(saa[1]).replace(";",""))/1024)/1024)/1000), use//instead of callingintso much. This does floor division. Soint(int(x / 1024) / 1024)is the same asx // 1024 // 1024. But then you could just merge them:x // (1024 * 1024). But what are those numbers? I suspect you're converting from bytes to GB (expect 1000 MB = 1 GB isn't correct unless you're dealing with harddrives, which sometimes define the measurements like so). So instead use a constant:BYTES_PER_GB = 1024 * 1024 * 1000. Then do:int(vram_size_bytes) // BYTES_PER_GB. - As @IsmaelMiguel points out, not all cards support more than 1GB of RAM. This math will report 0 for them, which you may not want. One solution is to not use floored division (
int(vram_size_bytes) / BTYES_PER_GB). When printing using the format specifier.02fso you don't get numbers with tons of decimal places (ex."Video RAM: :.02f".format(vram_size_bytes / BYTES_PER_GB)). - Fix your spacing and formatting. PEP8 your code!
- What is
saa[1]above? You have some many magic indices that no one can discern without understanding the value ofstr(controller). Prefer giving things descriptive names (use variables, heck overuse variables to make things clear) to magic indices. removecan fail (itraises aValueErrorif the element isn't found in the list).- You don't need to initialize
gpu_aandgpu_b(ex.gpu_a = ""is unnecessary), because later you assign stuff to them. They will be initialized on first assignment. - Given code this fragile, you definitely want to include tests. I'd imagine some integration tests would help. You can amass a list of outputs of
str(c.Win32_VideoController()[0])s and manually do this string parsing. Then assert in some tests that your code outputs the thing that you expect for each.unittestwill help you with this.
But now the largest issue: you're doing a ton of extra work that you almost certainly don't have to do! I can't seem to find good docs for wmi's wrapping around this, but according to the microsoft docs, this probably returns an object of some type. That means instead of all of this convoluted and fragile string parsing you're attempting, you can probably get at the information you want by just doing controller.AdapterRAM // BYTES_PER_GB or controller.Description (where controller comes from for controller in wmi.WMI().Win32_VideoController()). Open up a Python REPL by running python3 and then run the following:
>>> import wmi
>>> controller = wmi.WMI().Win32_VideoController()[0]
>>> help(controller)
>>> dir(controller)
That should give you an idea of all of the information you can get from it. You don't need to be doing all that string parsing! The information is already available on properties of the object!
$endgroup$
2
$begingroup$
Strictly speaking, one GB is 1000 MB and one MB is 1000 KB, but one GiB is 1024 MiB and one MiB is 1024 KiB. Going by the standards as defined by the IEC, that is.
$endgroup$
– vurp0
yesterday
$begingroup$
Won't this cause issues for GPUs with less than 1GB of vram? (For example, integrated GPUs can have anywhere between 8MB and 2GB. VirtualBox limits the maximum GPU memory to 128MB)
$endgroup$
– Ismael Miguel
yesterday
$begingroup$
@vurp0 Sure, strictly speaking that's right. However, the "ibi" is often dropped (unfortunately) in common parlance. And as I mention, the only place where I've seen GB not mean GiB was in the context of hard drive storage (it's cheaper for manufacturers because 1 GB < 1 GiB, so they exploit this ambiguity).
$endgroup$
– Bailey Parker
yesterday
1
$begingroup$
@IsmaelMiguel Yeah it will. This is a good point to call out. I'll append it to the discussion about the GB math.
$endgroup$
– Bailey Parker
yesterday
add a comment |
$begingroup$
To add on to what @BaileyParker said, instead of phrasing the WMI string directly, you should use the wmi_property command to get the value of the property you are interested in. You can get a list of properties from the Microsoft documentation: Win32_VideoController
For example, you could do something like this:
import json
import wmi
controllers = wmi.WMI().Win32_VideoController()
gpu_data = list()
for controller in controllers:
controller_info =
'Name': controller.wmi_property('Name').value,
'HRes': controller.wmi_property('CurrentHorizontalResolution').value,
'VRes': controller.wmi_property('CurrentVerticalResolution').value,
gpu_data.append(controller_info)
print json.dumps(gpu_data, indent=4)
On my machine it prints the output:
[
"VRes": 1080,
"Name": "NVIDIA GeForce GTX 1050",
"HRes": 1920
]
$endgroup$
1
$begingroup$
I would make thatforloop a list comprehension. Otherwise this seems to be the solution the OP actually needs in this case.
$endgroup$
– Graipher
2 days ago
$begingroup$
I'll add you to your code ram gpu,next gpu ram badly showcontroller.wmi_property('AdapterRAM').value
$endgroup$
– tadas
yesterday
$begingroup$
You can (and should) spelllist()as just[]
$endgroup$
– Bailey Parker
yesterday
$begingroup$
@BaileyParker I disagree and intentionally make empty lists, dicts ect... that way to signal to the reader that they are intentionally created as empty structures differentiating them from structures created with content
$endgroup$
– Mark Omo
19 hours ago
$begingroup$
What? How is an empty list on its own not signal enough that it is empty?[]is empty but if there is anything inside the brackets it’s not.
$endgroup$
– Bailey Parker
19 hours ago
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
tadas is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217532%2fextract-all-gpu-name-model-and-gpu-ram%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
I fully agree with the answer by @BaileyParker. I have basically no idea what any of your variables are. Or what your code is actually supposed to achieve. And indeed, there is probably a better way to achieve what you want in this case. However, knowing how to do string and list parsing efficiently is also sometimes necessary, so this is how you could have done that better.
In your code, most of the lines are spent on removing unneeded strings from a list. Instead of manually removing each one of them (and even adding guards for the ones you know might not even exist in the list, hoping you don't miss any of those), just define a blacklist of terms you never want in your list and exclude them. If you choose a set as a data structure for that blacklist, you only need to iterate once over your list and checking if each element is in the blacklist is $mathcalO(1)$, making this algorithm $mathcalO(n)$.
Your code on the other hand is $mathcalO(n)$ for every single term you remove, because the whole list needs to be checked for the item in the worst case, and twice that if you first check if a term exists in the list with in, which is also $mathcalO(n)$. This makes your code $mathcalO(nk)$ with $n$ the list of methods and $k$ the number of terms you want removed.
blacklist = "instance", "of", ...
for methods in c.Win32_VideoController():
methods = [m for m in str(methods).split() if m not in blacklist]
# do something with it
$endgroup$
add a comment |
$begingroup$
I fully agree with the answer by @BaileyParker. I have basically no idea what any of your variables are. Or what your code is actually supposed to achieve. And indeed, there is probably a better way to achieve what you want in this case. However, knowing how to do string and list parsing efficiently is also sometimes necessary, so this is how you could have done that better.
In your code, most of the lines are spent on removing unneeded strings from a list. Instead of manually removing each one of them (and even adding guards for the ones you know might not even exist in the list, hoping you don't miss any of those), just define a blacklist of terms you never want in your list and exclude them. If you choose a set as a data structure for that blacklist, you only need to iterate once over your list and checking if each element is in the blacklist is $mathcalO(1)$, making this algorithm $mathcalO(n)$.
Your code on the other hand is $mathcalO(n)$ for every single term you remove, because the whole list needs to be checked for the item in the worst case, and twice that if you first check if a term exists in the list with in, which is also $mathcalO(n)$. This makes your code $mathcalO(nk)$ with $n$ the list of methods and $k$ the number of terms you want removed.
blacklist = "instance", "of", ...
for methods in c.Win32_VideoController():
methods = [m for m in str(methods).split() if m not in blacklist]
# do something with it
$endgroup$
add a comment |
$begingroup$
I fully agree with the answer by @BaileyParker. I have basically no idea what any of your variables are. Or what your code is actually supposed to achieve. And indeed, there is probably a better way to achieve what you want in this case. However, knowing how to do string and list parsing efficiently is also sometimes necessary, so this is how you could have done that better.
In your code, most of the lines are spent on removing unneeded strings from a list. Instead of manually removing each one of them (and even adding guards for the ones you know might not even exist in the list, hoping you don't miss any of those), just define a blacklist of terms you never want in your list and exclude them. If you choose a set as a data structure for that blacklist, you only need to iterate once over your list and checking if each element is in the blacklist is $mathcalO(1)$, making this algorithm $mathcalO(n)$.
Your code on the other hand is $mathcalO(n)$ for every single term you remove, because the whole list needs to be checked for the item in the worst case, and twice that if you first check if a term exists in the list with in, which is also $mathcalO(n)$. This makes your code $mathcalO(nk)$ with $n$ the list of methods and $k$ the number of terms you want removed.
blacklist = "instance", "of", ...
for methods in c.Win32_VideoController():
methods = [m for m in str(methods).split() if m not in blacklist]
# do something with it
$endgroup$
I fully agree with the answer by @BaileyParker. I have basically no idea what any of your variables are. Or what your code is actually supposed to achieve. And indeed, there is probably a better way to achieve what you want in this case. However, knowing how to do string and list parsing efficiently is also sometimes necessary, so this is how you could have done that better.
In your code, most of the lines are spent on removing unneeded strings from a list. Instead of manually removing each one of them (and even adding guards for the ones you know might not even exist in the list, hoping you don't miss any of those), just define a blacklist of terms you never want in your list and exclude them. If you choose a set as a data structure for that blacklist, you only need to iterate once over your list and checking if each element is in the blacklist is $mathcalO(1)$, making this algorithm $mathcalO(n)$.
Your code on the other hand is $mathcalO(n)$ for every single term you remove, because the whole list needs to be checked for the item in the worst case, and twice that if you first check if a term exists in the list with in, which is also $mathcalO(n)$. This makes your code $mathcalO(nk)$ with $n$ the list of methods and $k$ the number of terms you want removed.
blacklist = "instance", "of", ...
for methods in c.Win32_VideoController():
methods = [m for m in str(methods).split() if m not in blacklist]
# do something with it
edited 2 days ago
answered 2 days ago
GraipherGraipher
27.6k54499
27.6k54499
add a comment |
add a comment |
$begingroup$
Let's ignore efficiency. It took me five minutes to figure out what you were trying to do. In the kindest way possible, this is a mess. But let's fix that!
Your variable names are terrible. I have no idea what an ass is. What about an asss? Surely that's a bad idea, because it's very easy to mistake one for the other. Even if it wasn't, what are they? A variable name should describe what it holds. A bunch of random characters aren't helpful. Same goes for gpu_ouy. Is that a typo? What are gpu_a and gpu_b? What is aq? What is c? What is saa? None of these variables names help someone reading your code understand their function.
Don't use len in a for loop. The idiomatic python approach is to just iterate over the collection (and let it deal with lengths and indices). So instead of:
for x in range(len(c.Win32_VideoController())):
ass = str(c.Win32_VideoController()[x])
Use this:
for controller in c.Win32_VideoController():
# do something with controller...
pass
Some random points before we get to the big issue:
- In
sqa =int(((int(str(saa[1]).replace(";",""))/1024)/1024)/1000), use//instead of callingintso much. This does floor division. Soint(int(x / 1024) / 1024)is the same asx // 1024 // 1024. But then you could just merge them:x // (1024 * 1024). But what are those numbers? I suspect you're converting from bytes to GB (expect 1000 MB = 1 GB isn't correct unless you're dealing with harddrives, which sometimes define the measurements like so). So instead use a constant:BYTES_PER_GB = 1024 * 1024 * 1000. Then do:int(vram_size_bytes) // BYTES_PER_GB. - As @IsmaelMiguel points out, not all cards support more than 1GB of RAM. This math will report 0 for them, which you may not want. One solution is to not use floored division (
int(vram_size_bytes) / BTYES_PER_GB). When printing using the format specifier.02fso you don't get numbers with tons of decimal places (ex."Video RAM: :.02f".format(vram_size_bytes / BYTES_PER_GB)). - Fix your spacing and formatting. PEP8 your code!
- What is
saa[1]above? You have some many magic indices that no one can discern without understanding the value ofstr(controller). Prefer giving things descriptive names (use variables, heck overuse variables to make things clear) to magic indices. removecan fail (itraises aValueErrorif the element isn't found in the list).- You don't need to initialize
gpu_aandgpu_b(ex.gpu_a = ""is unnecessary), because later you assign stuff to them. They will be initialized on first assignment. - Given code this fragile, you definitely want to include tests. I'd imagine some integration tests would help. You can amass a list of outputs of
str(c.Win32_VideoController()[0])s and manually do this string parsing. Then assert in some tests that your code outputs the thing that you expect for each.unittestwill help you with this.
But now the largest issue: you're doing a ton of extra work that you almost certainly don't have to do! I can't seem to find good docs for wmi's wrapping around this, but according to the microsoft docs, this probably returns an object of some type. That means instead of all of this convoluted and fragile string parsing you're attempting, you can probably get at the information you want by just doing controller.AdapterRAM // BYTES_PER_GB or controller.Description (where controller comes from for controller in wmi.WMI().Win32_VideoController()). Open up a Python REPL by running python3 and then run the following:
>>> import wmi
>>> controller = wmi.WMI().Win32_VideoController()[0]
>>> help(controller)
>>> dir(controller)
That should give you an idea of all of the information you can get from it. You don't need to be doing all that string parsing! The information is already available on properties of the object!
$endgroup$
2
$begingroup$
Strictly speaking, one GB is 1000 MB and one MB is 1000 KB, but one GiB is 1024 MiB and one MiB is 1024 KiB. Going by the standards as defined by the IEC, that is.
$endgroup$
– vurp0
yesterday
$begingroup$
Won't this cause issues for GPUs with less than 1GB of vram? (For example, integrated GPUs can have anywhere between 8MB and 2GB. VirtualBox limits the maximum GPU memory to 128MB)
$endgroup$
– Ismael Miguel
yesterday
$begingroup$
@vurp0 Sure, strictly speaking that's right. However, the "ibi" is often dropped (unfortunately) in common parlance. And as I mention, the only place where I've seen GB not mean GiB was in the context of hard drive storage (it's cheaper for manufacturers because 1 GB < 1 GiB, so they exploit this ambiguity).
$endgroup$
– Bailey Parker
yesterday
1
$begingroup$
@IsmaelMiguel Yeah it will. This is a good point to call out. I'll append it to the discussion about the GB math.
$endgroup$
– Bailey Parker
yesterday
add a comment |
$begingroup$
Let's ignore efficiency. It took me five minutes to figure out what you were trying to do. In the kindest way possible, this is a mess. But let's fix that!
Your variable names are terrible. I have no idea what an ass is. What about an asss? Surely that's a bad idea, because it's very easy to mistake one for the other. Even if it wasn't, what are they? A variable name should describe what it holds. A bunch of random characters aren't helpful. Same goes for gpu_ouy. Is that a typo? What are gpu_a and gpu_b? What is aq? What is c? What is saa? None of these variables names help someone reading your code understand their function.
Don't use len in a for loop. The idiomatic python approach is to just iterate over the collection (and let it deal with lengths and indices). So instead of:
for x in range(len(c.Win32_VideoController())):
ass = str(c.Win32_VideoController()[x])
Use this:
for controller in c.Win32_VideoController():
# do something with controller...
pass
Some random points before we get to the big issue:
- In
sqa =int(((int(str(saa[1]).replace(";",""))/1024)/1024)/1000), use//instead of callingintso much. This does floor division. Soint(int(x / 1024) / 1024)is the same asx // 1024 // 1024. But then you could just merge them:x // (1024 * 1024). But what are those numbers? I suspect you're converting from bytes to GB (expect 1000 MB = 1 GB isn't correct unless you're dealing with harddrives, which sometimes define the measurements like so). So instead use a constant:BYTES_PER_GB = 1024 * 1024 * 1000. Then do:int(vram_size_bytes) // BYTES_PER_GB. - As @IsmaelMiguel points out, not all cards support more than 1GB of RAM. This math will report 0 for them, which you may not want. One solution is to not use floored division (
int(vram_size_bytes) / BTYES_PER_GB). When printing using the format specifier.02fso you don't get numbers with tons of decimal places (ex."Video RAM: :.02f".format(vram_size_bytes / BYTES_PER_GB)). - Fix your spacing and formatting. PEP8 your code!
- What is
saa[1]above? You have some many magic indices that no one can discern without understanding the value ofstr(controller). Prefer giving things descriptive names (use variables, heck overuse variables to make things clear) to magic indices. removecan fail (itraises aValueErrorif the element isn't found in the list).- You don't need to initialize
gpu_aandgpu_b(ex.gpu_a = ""is unnecessary), because later you assign stuff to them. They will be initialized on first assignment. - Given code this fragile, you definitely want to include tests. I'd imagine some integration tests would help. You can amass a list of outputs of
str(c.Win32_VideoController()[0])s and manually do this string parsing. Then assert in some tests that your code outputs the thing that you expect for each.unittestwill help you with this.
But now the largest issue: you're doing a ton of extra work that you almost certainly don't have to do! I can't seem to find good docs for wmi's wrapping around this, but according to the microsoft docs, this probably returns an object of some type. That means instead of all of this convoluted and fragile string parsing you're attempting, you can probably get at the information you want by just doing controller.AdapterRAM // BYTES_PER_GB or controller.Description (where controller comes from for controller in wmi.WMI().Win32_VideoController()). Open up a Python REPL by running python3 and then run the following:
>>> import wmi
>>> controller = wmi.WMI().Win32_VideoController()[0]
>>> help(controller)
>>> dir(controller)
That should give you an idea of all of the information you can get from it. You don't need to be doing all that string parsing! The information is already available on properties of the object!
$endgroup$
2
$begingroup$
Strictly speaking, one GB is 1000 MB and one MB is 1000 KB, but one GiB is 1024 MiB and one MiB is 1024 KiB. Going by the standards as defined by the IEC, that is.
$endgroup$
– vurp0
yesterday
$begingroup$
Won't this cause issues for GPUs with less than 1GB of vram? (For example, integrated GPUs can have anywhere between 8MB and 2GB. VirtualBox limits the maximum GPU memory to 128MB)
$endgroup$
– Ismael Miguel
yesterday
$begingroup$
@vurp0 Sure, strictly speaking that's right. However, the "ibi" is often dropped (unfortunately) in common parlance. And as I mention, the only place where I've seen GB not mean GiB was in the context of hard drive storage (it's cheaper for manufacturers because 1 GB < 1 GiB, so they exploit this ambiguity).
$endgroup$
– Bailey Parker
yesterday
1
$begingroup$
@IsmaelMiguel Yeah it will. This is a good point to call out. I'll append it to the discussion about the GB math.
$endgroup$
– Bailey Parker
yesterday
add a comment |
$begingroup$
Let's ignore efficiency. It took me five minutes to figure out what you were trying to do. In the kindest way possible, this is a mess. But let's fix that!
Your variable names are terrible. I have no idea what an ass is. What about an asss? Surely that's a bad idea, because it's very easy to mistake one for the other. Even if it wasn't, what are they? A variable name should describe what it holds. A bunch of random characters aren't helpful. Same goes for gpu_ouy. Is that a typo? What are gpu_a and gpu_b? What is aq? What is c? What is saa? None of these variables names help someone reading your code understand their function.
Don't use len in a for loop. The idiomatic python approach is to just iterate over the collection (and let it deal with lengths and indices). So instead of:
for x in range(len(c.Win32_VideoController())):
ass = str(c.Win32_VideoController()[x])
Use this:
for controller in c.Win32_VideoController():
# do something with controller...
pass
Some random points before we get to the big issue:
- In
sqa =int(((int(str(saa[1]).replace(";",""))/1024)/1024)/1000), use//instead of callingintso much. This does floor division. Soint(int(x / 1024) / 1024)is the same asx // 1024 // 1024. But then you could just merge them:x // (1024 * 1024). But what are those numbers? I suspect you're converting from bytes to GB (expect 1000 MB = 1 GB isn't correct unless you're dealing with harddrives, which sometimes define the measurements like so). So instead use a constant:BYTES_PER_GB = 1024 * 1024 * 1000. Then do:int(vram_size_bytes) // BYTES_PER_GB. - As @IsmaelMiguel points out, not all cards support more than 1GB of RAM. This math will report 0 for them, which you may not want. One solution is to not use floored division (
int(vram_size_bytes) / BTYES_PER_GB). When printing using the format specifier.02fso you don't get numbers with tons of decimal places (ex."Video RAM: :.02f".format(vram_size_bytes / BYTES_PER_GB)). - Fix your spacing and formatting. PEP8 your code!
- What is
saa[1]above? You have some many magic indices that no one can discern without understanding the value ofstr(controller). Prefer giving things descriptive names (use variables, heck overuse variables to make things clear) to magic indices. removecan fail (itraises aValueErrorif the element isn't found in the list).- You don't need to initialize
gpu_aandgpu_b(ex.gpu_a = ""is unnecessary), because later you assign stuff to them. They will be initialized on first assignment. - Given code this fragile, you definitely want to include tests. I'd imagine some integration tests would help. You can amass a list of outputs of
str(c.Win32_VideoController()[0])s and manually do this string parsing. Then assert in some tests that your code outputs the thing that you expect for each.unittestwill help you with this.
But now the largest issue: you're doing a ton of extra work that you almost certainly don't have to do! I can't seem to find good docs for wmi's wrapping around this, but according to the microsoft docs, this probably returns an object of some type. That means instead of all of this convoluted and fragile string parsing you're attempting, you can probably get at the information you want by just doing controller.AdapterRAM // BYTES_PER_GB or controller.Description (where controller comes from for controller in wmi.WMI().Win32_VideoController()). Open up a Python REPL by running python3 and then run the following:
>>> import wmi
>>> controller = wmi.WMI().Win32_VideoController()[0]
>>> help(controller)
>>> dir(controller)
That should give you an idea of all of the information you can get from it. You don't need to be doing all that string parsing! The information is already available on properties of the object!
$endgroup$
Let's ignore efficiency. It took me five minutes to figure out what you were trying to do. In the kindest way possible, this is a mess. But let's fix that!
Your variable names are terrible. I have no idea what an ass is. What about an asss? Surely that's a bad idea, because it's very easy to mistake one for the other. Even if it wasn't, what are they? A variable name should describe what it holds. A bunch of random characters aren't helpful. Same goes for gpu_ouy. Is that a typo? What are gpu_a and gpu_b? What is aq? What is c? What is saa? None of these variables names help someone reading your code understand their function.
Don't use len in a for loop. The idiomatic python approach is to just iterate over the collection (and let it deal with lengths and indices). So instead of:
for x in range(len(c.Win32_VideoController())):
ass = str(c.Win32_VideoController()[x])
Use this:
for controller in c.Win32_VideoController():
# do something with controller...
pass
Some random points before we get to the big issue:
- In
sqa =int(((int(str(saa[1]).replace(";",""))/1024)/1024)/1000), use//instead of callingintso much. This does floor division. Soint(int(x / 1024) / 1024)is the same asx // 1024 // 1024. But then you could just merge them:x // (1024 * 1024). But what are those numbers? I suspect you're converting from bytes to GB (expect 1000 MB = 1 GB isn't correct unless you're dealing with harddrives, which sometimes define the measurements like so). So instead use a constant:BYTES_PER_GB = 1024 * 1024 * 1000. Then do:int(vram_size_bytes) // BYTES_PER_GB. - As @IsmaelMiguel points out, not all cards support more than 1GB of RAM. This math will report 0 for them, which you may not want. One solution is to not use floored division (
int(vram_size_bytes) / BTYES_PER_GB). When printing using the format specifier.02fso you don't get numbers with tons of decimal places (ex."Video RAM: :.02f".format(vram_size_bytes / BYTES_PER_GB)). - Fix your spacing and formatting. PEP8 your code!
- What is
saa[1]above? You have some many magic indices that no one can discern without understanding the value ofstr(controller). Prefer giving things descriptive names (use variables, heck overuse variables to make things clear) to magic indices. removecan fail (itraises aValueErrorif the element isn't found in the list).- You don't need to initialize
gpu_aandgpu_b(ex.gpu_a = ""is unnecessary), because later you assign stuff to them. They will be initialized on first assignment. - Given code this fragile, you definitely want to include tests. I'd imagine some integration tests would help. You can amass a list of outputs of
str(c.Win32_VideoController()[0])s and manually do this string parsing. Then assert in some tests that your code outputs the thing that you expect for each.unittestwill help you with this.
But now the largest issue: you're doing a ton of extra work that you almost certainly don't have to do! I can't seem to find good docs for wmi's wrapping around this, but according to the microsoft docs, this probably returns an object of some type. That means instead of all of this convoluted and fragile string parsing you're attempting, you can probably get at the information you want by just doing controller.AdapterRAM // BYTES_PER_GB or controller.Description (where controller comes from for controller in wmi.WMI().Win32_VideoController()). Open up a Python REPL by running python3 and then run the following:
>>> import wmi
>>> controller = wmi.WMI().Win32_VideoController()[0]
>>> help(controller)
>>> dir(controller)
That should give you an idea of all of the information you can get from it. You don't need to be doing all that string parsing! The information is already available on properties of the object!
edited yesterday
answered 2 days ago
Bailey ParkerBailey Parker
2,48411115
2,48411115
2
$begingroup$
Strictly speaking, one GB is 1000 MB and one MB is 1000 KB, but one GiB is 1024 MiB and one MiB is 1024 KiB. Going by the standards as defined by the IEC, that is.
$endgroup$
– vurp0
yesterday
$begingroup$
Won't this cause issues for GPUs with less than 1GB of vram? (For example, integrated GPUs can have anywhere between 8MB and 2GB. VirtualBox limits the maximum GPU memory to 128MB)
$endgroup$
– Ismael Miguel
yesterday
$begingroup$
@vurp0 Sure, strictly speaking that's right. However, the "ibi" is often dropped (unfortunately) in common parlance. And as I mention, the only place where I've seen GB not mean GiB was in the context of hard drive storage (it's cheaper for manufacturers because 1 GB < 1 GiB, so they exploit this ambiguity).
$endgroup$
– Bailey Parker
yesterday
1
$begingroup$
@IsmaelMiguel Yeah it will. This is a good point to call out. I'll append it to the discussion about the GB math.
$endgroup$
– Bailey Parker
yesterday
add a comment |
2
$begingroup$
Strictly speaking, one GB is 1000 MB and one MB is 1000 KB, but one GiB is 1024 MiB and one MiB is 1024 KiB. Going by the standards as defined by the IEC, that is.
$endgroup$
– vurp0
yesterday
$begingroup$
Won't this cause issues for GPUs with less than 1GB of vram? (For example, integrated GPUs can have anywhere between 8MB and 2GB. VirtualBox limits the maximum GPU memory to 128MB)
$endgroup$
– Ismael Miguel
yesterday
$begingroup$
@vurp0 Sure, strictly speaking that's right. However, the "ibi" is often dropped (unfortunately) in common parlance. And as I mention, the only place where I've seen GB not mean GiB was in the context of hard drive storage (it's cheaper for manufacturers because 1 GB < 1 GiB, so they exploit this ambiguity).
$endgroup$
– Bailey Parker
yesterday
1
$begingroup$
@IsmaelMiguel Yeah it will. This is a good point to call out. I'll append it to the discussion about the GB math.
$endgroup$
– Bailey Parker
yesterday
2
2
$begingroup$
Strictly speaking, one GB is 1000 MB and one MB is 1000 KB, but one GiB is 1024 MiB and one MiB is 1024 KiB. Going by the standards as defined by the IEC, that is.
$endgroup$
– vurp0
yesterday
$begingroup$
Strictly speaking, one GB is 1000 MB and one MB is 1000 KB, but one GiB is 1024 MiB and one MiB is 1024 KiB. Going by the standards as defined by the IEC, that is.
$endgroup$
– vurp0
yesterday
$begingroup$
Won't this cause issues for GPUs with less than 1GB of vram? (For example, integrated GPUs can have anywhere between 8MB and 2GB. VirtualBox limits the maximum GPU memory to 128MB)
$endgroup$
– Ismael Miguel
yesterday
$begingroup$
Won't this cause issues for GPUs with less than 1GB of vram? (For example, integrated GPUs can have anywhere between 8MB and 2GB. VirtualBox limits the maximum GPU memory to 128MB)
$endgroup$
– Ismael Miguel
yesterday
$begingroup$
@vurp0 Sure, strictly speaking that's right. However, the "ibi" is often dropped (unfortunately) in common parlance. And as I mention, the only place where I've seen GB not mean GiB was in the context of hard drive storage (it's cheaper for manufacturers because 1 GB < 1 GiB, so they exploit this ambiguity).
$endgroup$
– Bailey Parker
yesterday
$begingroup$
@vurp0 Sure, strictly speaking that's right. However, the "ibi" is often dropped (unfortunately) in common parlance. And as I mention, the only place where I've seen GB not mean GiB was in the context of hard drive storage (it's cheaper for manufacturers because 1 GB < 1 GiB, so they exploit this ambiguity).
$endgroup$
– Bailey Parker
yesterday
1
1
$begingroup$
@IsmaelMiguel Yeah it will. This is a good point to call out. I'll append it to the discussion about the GB math.
$endgroup$
– Bailey Parker
yesterday
$begingroup$
@IsmaelMiguel Yeah it will. This is a good point to call out. I'll append it to the discussion about the GB math.
$endgroup$
– Bailey Parker
yesterday
add a comment |
$begingroup$
To add on to what @BaileyParker said, instead of phrasing the WMI string directly, you should use the wmi_property command to get the value of the property you are interested in. You can get a list of properties from the Microsoft documentation: Win32_VideoController
For example, you could do something like this:
import json
import wmi
controllers = wmi.WMI().Win32_VideoController()
gpu_data = list()
for controller in controllers:
controller_info =
'Name': controller.wmi_property('Name').value,
'HRes': controller.wmi_property('CurrentHorizontalResolution').value,
'VRes': controller.wmi_property('CurrentVerticalResolution').value,
gpu_data.append(controller_info)
print json.dumps(gpu_data, indent=4)
On my machine it prints the output:
[
"VRes": 1080,
"Name": "NVIDIA GeForce GTX 1050",
"HRes": 1920
]
$endgroup$
1
$begingroup$
I would make thatforloop a list comprehension. Otherwise this seems to be the solution the OP actually needs in this case.
$endgroup$
– Graipher
2 days ago
$begingroup$
I'll add you to your code ram gpu,next gpu ram badly showcontroller.wmi_property('AdapterRAM').value
$endgroup$
– tadas
yesterday
$begingroup$
You can (and should) spelllist()as just[]
$endgroup$
– Bailey Parker
yesterday
$begingroup$
@BaileyParker I disagree and intentionally make empty lists, dicts ect... that way to signal to the reader that they are intentionally created as empty structures differentiating them from structures created with content
$endgroup$
– Mark Omo
19 hours ago
$begingroup$
What? How is an empty list on its own not signal enough that it is empty?[]is empty but if there is anything inside the brackets it’s not.
$endgroup$
– Bailey Parker
19 hours ago
|
show 1 more comment
$begingroup$
To add on to what @BaileyParker said, instead of phrasing the WMI string directly, you should use the wmi_property command to get the value of the property you are interested in. You can get a list of properties from the Microsoft documentation: Win32_VideoController
For example, you could do something like this:
import json
import wmi
controllers = wmi.WMI().Win32_VideoController()
gpu_data = list()
for controller in controllers:
controller_info =
'Name': controller.wmi_property('Name').value,
'HRes': controller.wmi_property('CurrentHorizontalResolution').value,
'VRes': controller.wmi_property('CurrentVerticalResolution').value,
gpu_data.append(controller_info)
print json.dumps(gpu_data, indent=4)
On my machine it prints the output:
[
"VRes": 1080,
"Name": "NVIDIA GeForce GTX 1050",
"HRes": 1920
]
$endgroup$
1
$begingroup$
I would make thatforloop a list comprehension. Otherwise this seems to be the solution the OP actually needs in this case.
$endgroup$
– Graipher
2 days ago
$begingroup$
I'll add you to your code ram gpu,next gpu ram badly showcontroller.wmi_property('AdapterRAM').value
$endgroup$
– tadas
yesterday
$begingroup$
You can (and should) spelllist()as just[]
$endgroup$
– Bailey Parker
yesterday
$begingroup$
@BaileyParker I disagree and intentionally make empty lists, dicts ect... that way to signal to the reader that they are intentionally created as empty structures differentiating them from structures created with content
$endgroup$
– Mark Omo
19 hours ago
$begingroup$
What? How is an empty list on its own not signal enough that it is empty?[]is empty but if there is anything inside the brackets it’s not.
$endgroup$
– Bailey Parker
19 hours ago
|
show 1 more comment
$begingroup$
To add on to what @BaileyParker said, instead of phrasing the WMI string directly, you should use the wmi_property command to get the value of the property you are interested in. You can get a list of properties from the Microsoft documentation: Win32_VideoController
For example, you could do something like this:
import json
import wmi
controllers = wmi.WMI().Win32_VideoController()
gpu_data = list()
for controller in controllers:
controller_info =
'Name': controller.wmi_property('Name').value,
'HRes': controller.wmi_property('CurrentHorizontalResolution').value,
'VRes': controller.wmi_property('CurrentVerticalResolution').value,
gpu_data.append(controller_info)
print json.dumps(gpu_data, indent=4)
On my machine it prints the output:
[
"VRes": 1080,
"Name": "NVIDIA GeForce GTX 1050",
"HRes": 1920
]
$endgroup$
To add on to what @BaileyParker said, instead of phrasing the WMI string directly, you should use the wmi_property command to get the value of the property you are interested in. You can get a list of properties from the Microsoft documentation: Win32_VideoController
For example, you could do something like this:
import json
import wmi
controllers = wmi.WMI().Win32_VideoController()
gpu_data = list()
for controller in controllers:
controller_info =
'Name': controller.wmi_property('Name').value,
'HRes': controller.wmi_property('CurrentHorizontalResolution').value,
'VRes': controller.wmi_property('CurrentVerticalResolution').value,
gpu_data.append(controller_info)
print json.dumps(gpu_data, indent=4)
On my machine it prints the output:
[
"VRes": 1080,
"Name": "NVIDIA GeForce GTX 1050",
"HRes": 1920
]
edited 2 days ago
answered 2 days ago
Mark OmoMark Omo
239418
239418
1
$begingroup$
I would make thatforloop a list comprehension. Otherwise this seems to be the solution the OP actually needs in this case.
$endgroup$
– Graipher
2 days ago
$begingroup$
I'll add you to your code ram gpu,next gpu ram badly showcontroller.wmi_property('AdapterRAM').value
$endgroup$
– tadas
yesterday
$begingroup$
You can (and should) spelllist()as just[]
$endgroup$
– Bailey Parker
yesterday
$begingroup$
@BaileyParker I disagree and intentionally make empty lists, dicts ect... that way to signal to the reader that they are intentionally created as empty structures differentiating them from structures created with content
$endgroup$
– Mark Omo
19 hours ago
$begingroup$
What? How is an empty list on its own not signal enough that it is empty?[]is empty but if there is anything inside the brackets it’s not.
$endgroup$
– Bailey Parker
19 hours ago
|
show 1 more comment
1
$begingroup$
I would make thatforloop a list comprehension. Otherwise this seems to be the solution the OP actually needs in this case.
$endgroup$
– Graipher
2 days ago
$begingroup$
I'll add you to your code ram gpu,next gpu ram badly showcontroller.wmi_property('AdapterRAM').value
$endgroup$
– tadas
yesterday
$begingroup$
You can (and should) spelllist()as just[]
$endgroup$
– Bailey Parker
yesterday
$begingroup$
@BaileyParker I disagree and intentionally make empty lists, dicts ect... that way to signal to the reader that they are intentionally created as empty structures differentiating them from structures created with content
$endgroup$
– Mark Omo
19 hours ago
$begingroup$
What? How is an empty list on its own not signal enough that it is empty?[]is empty but if there is anything inside the brackets it’s not.
$endgroup$
– Bailey Parker
19 hours ago
1
1
$begingroup$
I would make that
for loop a list comprehension. Otherwise this seems to be the solution the OP actually needs in this case.$endgroup$
– Graipher
2 days ago
$begingroup$
I would make that
for loop a list comprehension. Otherwise this seems to be the solution the OP actually needs in this case.$endgroup$
– Graipher
2 days ago
$begingroup$
I'll add you to your code ram gpu,next gpu ram badly show
controller.wmi_property('AdapterRAM').value$endgroup$
– tadas
yesterday
$begingroup$
I'll add you to your code ram gpu,next gpu ram badly show
controller.wmi_property('AdapterRAM').value$endgroup$
– tadas
yesterday
$begingroup$
You can (and should) spell
list() as just []$endgroup$
– Bailey Parker
yesterday
$begingroup$
You can (and should) spell
list() as just []$endgroup$
– Bailey Parker
yesterday
$begingroup$
@BaileyParker I disagree and intentionally make empty lists, dicts ect... that way to signal to the reader that they are intentionally created as empty structures differentiating them from structures created with content
$endgroup$
– Mark Omo
19 hours ago
$begingroup$
@BaileyParker I disagree and intentionally make empty lists, dicts ect... that way to signal to the reader that they are intentionally created as empty structures differentiating them from structures created with content
$endgroup$
– Mark Omo
19 hours ago
$begingroup$
What? How is an empty list on its own not signal enough that it is empty?
[] is empty but if there is anything inside the brackets it’s not.$endgroup$
– Bailey Parker
19 hours ago
$begingroup$
What? How is an empty list on its own not signal enough that it is empty?
[] is empty but if there is anything inside the brackets it’s not.$endgroup$
– Bailey Parker
19 hours ago
|
show 1 more comment
tadas is a new contributor. Be nice, and check out our Code of Conduct.
tadas is a new contributor. Be nice, and check out our Code of Conduct.
tadas is a new contributor. Be nice, and check out our Code of Conduct.
tadas is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217532%2fextract-all-gpu-name-model-and-gpu-ram%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$begingroup$
What is
c? Isc.Win32_VideoController()a simple accessor?$endgroup$
– greybeard
2 days ago
2
$begingroup$
Welcome to Code Review. Do not comment comments asking for clarification: Edit your question, the code in it unless reviews have started.
$endgroup$
– greybeard
2 days ago
17
$begingroup$
assis a very bad variable name.$endgroup$
– 422_unprocessable_entity
2 days ago
21
$begingroup$
ass.split()is an even worse statement.$endgroup$
– Raimund Krämer
2 days ago
3
$begingroup$
I like the Javascript spread operator though,
...ass$endgroup$
– JollyJoker
yesterday