jeudi 1 mai 2014

Vérifier une chaîne avec python dictionnaire-clés ? -Débordement de pile


I have one string (ope_sys) and I would like to guess what string I have using keys (for now these keys are just example) from one dictionary:


ope_sys = linux-2.6.32-312-ec2-x86_64-with-debian-6.0.8

def check_os_family(ope_sys):

inventory_family = {
"macos": ["macos"],
"linux": ["linux", "with"],
"windows": ["windows"]
}

for key in inventory_family:
for i in inventory_family[key]:
if re.search(i, ope_sys)
name = key
return name

The problem I dont know how exactly finish last if after loop over the lists in the dictionary, is there any way to say:


if all ifs are true name = key


Also is there any other way to do that, im open to change all my code. Thanks!




Regular expressions are a good approach as long as your matching rules are straightforward.


Note that in your code you could get an error when returning name, as it may not have been defined if none of the keywords match.


import re

def check_system(ope_sys):
system_keywords = {
"macos": ["macos"],
"linux": ["linux", "with"],
"windows": ["windows"],
}
for system, keywords in system_keywords.items():
if all(re.search(kw, ope_sys) for kw in keywords):
return system
return None

check_system("linux-2.6.32-312-ec2-x86_64-with-debian-6.0.8")



Instead of pattern-matching, just use what Python gives you: platform.system() will return something like "Windows", "Linux", "Darwin" (for Mac OS), etc.




for key, value in inventory_family.iteritems():
if all(v in ope_sys for v in value):
return key


I have one string (ope_sys) and I would like to guess what string I have using keys (for now these keys are just example) from one dictionary:


ope_sys = linux-2.6.32-312-ec2-x86_64-with-debian-6.0.8

def check_os_family(ope_sys):

inventory_family = {
"macos": ["macos"],
"linux": ["linux", "with"],
"windows": ["windows"]
}

for key in inventory_family:
for i in inventory_family[key]:
if re.search(i, ope_sys)
name = key
return name

The problem I dont know how exactly finish last if after loop over the lists in the dictionary, is there any way to say:


if all ifs are true name = key


Also is there any other way to do that, im open to change all my code. Thanks!



Regular expressions are a good approach as long as your matching rules are straightforward.


Note that in your code you could get an error when returning name, as it may not have been defined if none of the keywords match.


import re

def check_system(ope_sys):
system_keywords = {
"macos": ["macos"],
"linux": ["linux", "with"],
"windows": ["windows"],
}
for system, keywords in system_keywords.items():
if all(re.search(kw, ope_sys) for kw in keywords):
return system
return None

check_system("linux-2.6.32-312-ec2-x86_64-with-debian-6.0.8")


Instead of pattern-matching, just use what Python gives you: platform.system() will return something like "Windows", "Linux", "Darwin" (for Mac OS), etc.



for key, value in inventory_family.iteritems():
if all(v in ope_sys for v in value):
return key

0 commentaires:

Enregistrer un commentaire