Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/unit/unit.py
blob: cf067159b8b552fbaa4ebbb04d5c39685cc9c2c1 (plain)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/usr/bin/python
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#


import sys
sys.path.append('..')

#import json
import cPickle

from persistence.joke import Joke
from persistence.jokebook import Jokebook
from persistence.jokemachinestate import JokeMachineState

from util.decorators import Property

# ##############################################################################
# test Property decorator
#

#class Foo(object):
  
  #@Property
  #def all():
    #def get(self): return self.__all
    #def set(self, value): self.__all = value
    #def default(self): return 'this is a default value'
    
  #@Property
  #def onlyget():
    #def get(self): return self.__onlyget
      
  #@Property
  #def nodefault():
    #def get(self): return self.__nodefault
    #def set(self, value): self.__nodefault = value
      
  
#help(Property)
#foo = Foo()

#print foo.all
#Foo.all = 'changed'
#print foo.all

#print foo.nodefault
#foo.nodefault = 'set now'
#print foo.nodefault

#print foo.onlyget
#foo.onlyget = 'cannot set this'


def f(x):
  ret = 0
  counter = x
  while (counter != 0):
    ret = ret + x
    counter = counter - 1
  return ret


buf = []
def g(x):
  if len(buf) <= x:
    curpos = len(buf)
    for i in range(curpos, x + 1):
      buf.insert(i, i * i)
  return buf[x]
  
  

print g(4)
print g(1)
#print g(2)
#print g(3)
print g(8)
print g(6)

sys.exit()

class Plink(object):
  def __init__(self):
    self.b = 'a'

class Plonk(Plink):
  def __init__(self):
    self.b = 'a'

class Foo(Plonk):
  def __init__(self):
    
    self.a = 'a'

class Bar(Foo):
  def plonk(self, value):
    value = 'b'
    
bar = Bar()

def list_bases(c):
  bases = []
  print 'looking at: ', c
  if not hasattr(c, '__bases__'):
    print 'quitting at', c
    print dir(c)
    return bases
  
  for base in c.__bases__:
    print 'Has base: ', base
    bases.append(base)
    if len(base.__bases__) > 0:
      bases.append(list_bases(base.__bases__))
      
  return bases
  
print list_bases(bar.__class__)

sys.exit()


# ##############################################################################
# test PersistentProperty decorator
#

# dump properties on persistent objects
def dump(obj, indent = '  '):
  print indent + str(obj)
  for name, prop in obj.__properties__:
    value = prop.fget(obj)
    print indent + name, '=', value #, ' "' + prop.__doc__ + '"'
    if value.__class__ is list:
      for item in value:
        dump(item, indent + '  ')

  print indent + 'is_dirty =', obj.__dirty__




state = JokeMachineState()
state = state.test_data()
dump(state)
print "\n========================================================================\n"
pickle = state.dumps()
j = JokeMachineState.loads(pickle)
jokebook = j.jokebooks[0]
jokebook.owner = 'new owner'
dump(j)



sys.exit()


joke = Joke()

print 'Joke.id.doc: ', Joke.id.__doc__
print 'Joke.joke_text.doc: ', Joke.text.__doc__
print

print 'joke.id default should be 0 = ', joke.id
print 'joke is dirty = ', joke.__dirty__
print

joke.id = 66
print 'joke.id set to 66 = ', joke.id
print 'joke is dirty = ', joke.__dirty__
print

print 'joke.joke_text has no default:', joke.text
joke.text = 'joke text'
print 'joke.joke_text has been set to \'joke text\' = ', joke.text
print

print 'joke.id is still 66 = ', joke.id
joke.id = 23
print

print 'joke.id set to 23 = ', joke.id
print


jokebook = Jokebook()
dump(jokebook)
print

jokebook.jokes.append(joke)
dump(jokebook)
print 

j = jokebook.jokes[0]
j.id = 991

dump(jokebook)

print "========================================================================\n"

# ##############################################################################
# test Pickling
#
#jokebook.__dirty__ = False
#jokebook.jokes[0].__dirty__ = False

p = cPickle.dumps(jokebook)
#print p
#o = cPickle.loads(p)

#dump(o)
#dump(o.jokes[0])

print 'Persisting...'
print
jokebook.id = 0
dump(jokebook)
print
pickle = jokebook.dumps()
j = Jokebook.loads(pickle)
dump(j)

#print j.jokes.__class__

#print joke
#print joke.__dirty__
#print joke.dirty
#print joke.pdirty


# json
#p = json.write(j.__dict__)
#print p
#d = json.read(p)
#o = Joke()
#o.__dict__ = d
#print o.id

# pickle
#p = cPickle.dumps(j)
#print p
#o = cPickle.loads(p)
#print o.id
#print o.some_prop
#print o.fn()


#meta
#class meta(type):
  #def __init__(cls, name, bases, dct):
    #print 'Init is called: ' + str(cls) + str(name) + str(bases) + str(dct)

    #method_list = []
    #for func in dct.values():
      #try:
        #method_name = func._dbus_method_name()
        #print func, method_name
        #method_list.append(method_name)
      #except:
        #pass 

    #print method_list
    #super(meta, cls).__init__(name, bases, dct)

#def method(func):
  #def decorator(self, *args):
    #func(self, *args)

  #def _dbus_method_name():
    #return 'dbus_' + func.__name__

  #decorator._dbus_method_name = _dbus_method_name
  #return decorator


#class bar:
  #__metaclass__ = meta

  #@method
  #def my_method(self):
    #print 'my_first_method'
    
  #@method
  #def another_method(self):
    #print 'my_other_method'

#x = bar()
#x.my_method()

#print x.my_method._dbus_method_name