詳細を説明すると、複数のワークステーションに関する情報があります。たとえば、名前、インストールされているアプリケーション、ユーザー、部門が異なる100台のワークステーションがあります。複数のインスタンスでいくつかの属性値をチェックし、同じインスタンスの別の属性をPythonで表示する
複数インスタンスで特定の属性をチェックし、同じインスタンスから別の属性を出力したいとしました。たとえば、
"xxxx"というユーザーを検索しています。存在する場合、 "xxxx"ユーザーが属するマシン名または部門と同じような別の属性を表示します。
私はこれをやろうとしましたが、1つのインスタンスに対して行うことができました。オブジェクトをチェックする必要があるクラスのように、class.object条件を使用してこの操作を行うことができます。
しかし、複数のインスタンスがあり、特定の属性をランダムにチェックするのは難しいことです。私はいくつかのヒントを見つけようとしましたが、失敗しました。私はPythonの初心者であり、誰かが答えることができれば助けになるでしょう。
from itertools import count
class Workstation(object):
"""Creates an workstation list-object"""
count = 0
def __init__(self, machine_name, graphics, cad, user, dept):
self.machine_name = machine_name
self.graphics = graphics
self.cad = cad
self.user = user
self.dept = dept
Workstation.count += 1
def ws_list(self):
"""showing the machine name with department"""
print ('Name:', self.machine_name, 'Department:', self.dept)
def total(self):
print ('the total count is %d' %Workstation.count)
def info(self):
print("enter the machine name")
dept = raw_input(" ")
if dept == self.dept:
print ("the department is", self.machine_name)
else:
print ("the machine is not in the list")
ws1 = Workstation("xxx", "nVidia Quadro FX 880M", "cad", 123, "IN")
ws2 = Workstation("yyy", "nVidia Quadro FX 880M", "cae", 456, "US")
ws3 = Workstation("zzz", "nVidia Quadro FX 880M", "IT", 789, "GE")
print ws1.machine_name
print ws1.ws_list()
print total
よろしく、
ジェイ
上記のコードにはどのような問題がありますか?なぜ失敗したのかをもっと説明できますか? –
ありがとうPulkit Goyal、実際にコードの最後の3行を見て、特定の属性を取得するには、ws1、ws2などのインスタンスを言及する必要があります。 – jay