Created
December 15, 2011 15:44
-
-
Save jaheba/1481558 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
| from itertools import ifilter | |
| def read(dateiname='passwd.txt', encoding='latin-1'): | |
| with open(dateiname) as fobj: | |
| text = fobj.read().decode(encoding) | |
| return map(lambda s: Account(*s.split(':')), text.splitlines()) | |
| class Account(object): | |
| """docstring for Account""" | |
| def __init__(self, login_name, password, UID, GID, user_name, directory, shell): | |
| self.login_name = login_name | |
| self.password = password | |
| self.UID = int(UID) | |
| self.GID = int(GID) | |
| self.user_name = user_name | |
| self.directory = directory | |
| self.shell = shell | |
| def __repr__(self): | |
| return '<Account %s>' %self.login_name | |
| def find_account(liste, login_name): | |
| try: | |
| return ifilter(lambda account: account.login_name==login_name, liste).next() | |
| except StopIteration: | |
| 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 | |
| from operator import add, sub, mul, div | |
| 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 | |
| # 3*(4+5) | |
| expr = mal(zahl(3), plus(zahl(4), zahl(5))) | |
| #expr: ('*', (3,), ('+', (4,), (5,))) | |
| operators = { | |
| '*':mul, | |
| '/':div, | |
| '+':add, | |
| '-':sub, | |
| } | |
| def calc(expr): | |
| e = expr[0] | |
| if e in operators: | |
| return operators[e](calc(expr[1]), calc(expr[2])) | |
| else: | |
| return e | |
| def infix(expr): | |
| slist = [] | |
| _infix(expr, slist) | |
| return ''.join(slist) | |
| def _infix(expr, slist): | |
| e = expr[0] | |
| if e in operators: | |
| slist.append('(') | |
| _infix(expr[1], slist) | |
| slist.append(e) | |
| _infix(expr[2], slist) | |
| slist.append(')') | |
| else: | |
| return slist.append(str(e)) |
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
| from account import find_account, read | |
| from sys import stderr | |
| class Env(object): pass | |
| tests = [] | |
| def test(setUp=lambda: None): | |
| def decorator(test_func): | |
| def wrapper(): | |
| env = setUp() | |
| try: | |
| test_func(env) | |
| except AssertionError as e: | |
| stderr.write('failed (%s)' %test_func.__doc__) | |
| return False | |
| else: | |
| stderr.write('.') | |
| return True | |
| tests.append(wrapper) | |
| return wrapper | |
| return decorator | |
| def setUp(): | |
| env = Env() | |
| filename='passwd.txt' | |
| env.accounts = read(filename) | |
| return env | |
| @test(setUp) | |
| def test_UID(env): | |
| 'UID of first user should be 5316' | |
| account = env.accounts[0] | |
| assert account.UID == 5316 | |
| @test(setUp) | |
| def test_user_name(env): | |
| 'user_name of last user should be Steffen Kensy' | |
| account = env.accounts[-1] | |
| assert account.user_name == 'Steffen Kensy' | |
| @test(setUp) | |
| def test_unique_UID(env): | |
| 'there must be exactly one user with UID 5131' | |
| accounts = filter(lambda a: a.UID==5131, env.accounts) | |
| assert len(accounts) == 1 | |
| @test(setUp) | |
| def test_find_account_fwesack(env): | |
| 'checking whether the user fwesack has `/home/stud/2005/fwesack` as homedirecory' | |
| fwesack = find_account(env.accounts, 'fwesack') | |
| assert fwesack.directory == '/home/stud/2005/fwesack' | |
| @test(setUp) | |
| def test_find_account_exception(env): | |
| 'find_account must raise an exception looking up an user that does not exists' | |
| try: | |
| find_account(env.accounts, 'billg') | |
| except Exception: | |
| pass | |
| else: | |
| raise AssertionError | |
| if __name__ == '__main__': | |
| failed = 0 | |
| for i, test in enumerate(tests): | |
| failed += test() == False | |
| print 'Ran %i tests. %i failed' %(i, failed) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment