x
права), что на уровне веб-сервера.items = [1, 2, 3, 4]
def get_item_as_method():
return items.__getitem__(1)
def get_item_as_operator():
return items[1]
LOAD_GLOBAL 0 (items)
LOAD_ATTR 1 (__getitem__)
LOAD_CONST 1 (1)
CALL_FUNCTION 1 (1 positional, 0 keyword pair)
RETURN_VALUE
LOAD_GLOBAL 0 (items)
LOAD_CONST 1 (1)
BINARY_SUBSCR
RETURN_VALUE
import random
import struct
>>> my_set={random.randint(1,1000) for _ in xrange(10)}
>>> my_set
set([246, 898, 918, 113, 11, 677, 82, 374, 184, 101])
data = struct.pack("I%dH" % len(my_set), len(my_set), *list(my_set))
>>> data
'\n\x00\x00\x00\xf6\x00\x82\x03\x96\x03q\x00\x0b\x00\xa5\x02R\x00v\x01\xb8\x00e\x00'
(data_len,)=struct.unpack("I", data[:struct.calcsize("I")])
>>> data_len
10
>>> reconstructed_set=set(struct.unpack("%dH" % data_len,data[struct.calcsize("I"):]))
>>> reconstructed_set
set([898, 246, 11, 113, 82, 918, 184, 374, 101, 677])
>>> reconstructed_set==my_set
True
In [1]: import inspect
In [2]: def get_user_attributes(cls):
...: boring = dir(type('dummy', (object,), {}))
...: return [item for item in inspect.getmembers(cls)
...: if item[0] not in boring]
...:
In [3]: class C(object):
...: pass
...:
In [4]: get_user_attributes(C)
Out[4]: []
In [5]: class C(object):
...: first = 1
...: second = 2
...:
In [6]: get_user_attributes(C)
Out[6]: [('first', 1), ('second', 2)]
In [7]:
def get_params(*args):
def my_decorator(method):
def wrapped(**kwargs):
items = {"params":{"power":{"basevalue":5}, "damage":{"basevalue":5}}}
test = {item: items["params"].get(item)["basevalue"]
for item in args if item in items["params"]}
kwargs.update(test)
return method(**kwargs)
return wrapped
return my_decorator
@get_params("power","damagea")
def params_from(power=None, damage=None,**kwargs):
return power, damage
print params_from()