File: UserStack.py

package info (click to toggle)
egenix-mx-base 3.2.1-1.1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 8,496 kB
  • sloc: ansic: 21,976; python: 17,192; sh: 137; makefile: 116
file content (57 lines) | stat: -rw-r--r-- 1,150 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
""" A pure Python Stack implementation modelled after mxStack.

    Copyright (c) 2000, Marc-Andre Lemburg; mailto:mal@lemburg.com
    Copyright (c) 2000-2010, eGenix.com Software GmbH; mailto:info@egenix.com
    See the documentation for further information on copyrights,
    or contact the author. All Rights Reserved.
"""

class UserStack:

    def __init__(self):

        self.stack = ()

    def push(self,x):

        self.stack = (x,self.stack)

    def pop(self):

        x, self.stack = self.stack
        return x

    def not_empty(self):

        return len(self.stack) != 0

    def top(self):

        return self.stack[0]

    def __len__(self):

        i = 0
        s = self.stack
        while len(s) != 0:
            s = s[1]
            i = i + 1
        return i

    def __repr__(self):

        l = []
        s = self.stack
        while len(s) != 0:
            l.append(repr(s[0]))
            s = s[1]
        return '<UserStack [%s]>' % ', '.join(l)

    def __str__(self):

        l = []
        s = self.stack
        while len(s) != 0:
            l.append(s[0])
            s = s[1]
        return 's' + repr(l)