Created
December 12, 2011 14:00
-
-
Save cm2kay/1467291 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Account: | |
| def __init__(self, login_name, password, uid, gid, user_name, directory, shell): | |
| self.login_name = login_name | |
| self.password = password | |
| self.uid = uid | |
| self.gid = gid | |
| self.user_name = user_name | |
| self.directory = directory | |
| self.shell = shell | |
| def read(dateiname): | |
| accountliste = [] | |
| try: | |
| pwdatei = open(dateiname) | |
| for line in pwdatei.readlines(): | |
| content = line.split(":") | |
| login_name = content[0] | |
| password = content[1] | |
| uid = content[2] | |
| gid = content[3] | |
| user_name = content[4] | |
| directory = content[5] | |
| shell = content[6] | |
| new_acc = Account(login_name, password, uid, gid, user_name, directory, shell) | |
| accountliste.append(new_acc) | |
| except Exception: | |
| print "line", content | |
| raise | |
| print "Fehler bei Dateibehandlung" | |
| finally: | |
| pwdatei.close() | |
| return accountliste | |
| def find_account(liste, login_name): | |
| for account in liste: | |
| if login_name == account.login_name: | |
| return account | |
| raise KeyError |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: iso-8859-1 -*- | |
| # Funktionen zur Darstellung von Ausdrücken entsprechend | |
| # der Grammatik: | |
| # epxr = zahl | plus | minus | mal | durch | |
| # plus = expr '+' expr | |
| # minus = expr '-' expr | |
| # mal = expr '*' expr | |
| # durch = expr '/' expr | |
| # Alle Ausdrücke werden durch Tupel repräsentiert: | |
| # Zahlen durch 1-Tupel, binärde Operationen durch 3-Tupel | |
| def zahl(n): | |
| return n, | |
| def plus(a,b): | |
| return '+', a, b | |
| def minus(a,b): | |
| return '-', a, b | |
| def mal(a,b): | |
| return '*', a, b | |
| def durch(a,b): | |
| return '/', a, b | |
| def calc(expression): | |
| if len(expression) == 1: | |
| return expression[0] | |
| else: | |
| if expression[0] == "+": | |
| calculated = calc(expression[1]) + calc(expression[2]) | |
| elif expression[0] == "-": | |
| calculated = calc(expression[1]) - calc(expression[2]) | |
| elif expression[0] == "*": | |
| calculated = calc(expression[1]) * calc(expression[2]) | |
| elif expression[0] == "/": | |
| calculated = float(calc(expression[1])) / float(calc(expression[2])) | |
| return calculated | |
| def infix(expression, first = True): | |
| if len(expression) == 1: | |
| return str(expression[0]) | |
| else: | |
| operation = infix(expression[1], False) + expression[0] + infix(expression[2], False) | |
| if not(first): | |
| operation = "("+ operation + ")" | |
| return operation | |
| # 3*(4+5) | |
| expr = mal(zahl(3), plus(zahl(4), zahl(5))) | |
| print infix(expr),"=", calc(expr) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import account | |
| liste = account.read("passwd") | |
| #Vorbereitung 3. Testfall | |
| c = 0 | |
| for acc in liste: | |
| if acc.uid == "5131": | |
| c += 1 | |
| #Vorbereitung 5. Testfall | |
| try: | |
| account.find_account(liste, "billg") | |
| billgbool = False | |
| except: | |
| billgbool = True | |
| #assert-Block | |
| assert liste[0].uid == "5316", "Erste UID ist nicht 5316." | |
| assert liste[len(liste)-1].user_name == "Steffen Kensy", "Letzter Name ist nicht Steffen Kensy" | |
| assert c == 1, "Es gibt mehr als einen Account mit der UID 5131" | |
| assert account.find_account(liste, "fwesack").directory == "/home/stud/2005/fwesack", "User fwesack hat nicht /home/stud/2005/fwesack als Verzeichnis" | |
| assert billgbool, 'find_account(liste, "billg") loest keine Ausnahme aus' | |
| print "Erfolgreich." | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment