text stringlengths 0 828 |
|---|
return [(Point(x, v), Point(x + 1 + v, y - 1 - v)) for v in range(y)]" |
4416,"def put(self, x, y, rules): |
# type: (int, int, List[PlaceItem]) -> None |
"""""" |
Set possible rules at specific position. |
:param x: X coordinate. |
:param y: Y coordinate. |
:param rules: Value to set. |
"""""" |
self._field[y][x] = rules" |
4417,"def froze_it(cls): |
"""""" |
Decorator to prevent from creating attributes in the object ouside __init__(). |
This decorator must be applied to the final class (doesn't work if a |
decorated class is inherited). |
Yoann's answer at http://stackoverflow.com/questions/3603502 |
"""""" |
cls._frozen = False |
def frozensetattr(self, key, value): |
if self._frozen and not hasattr(self, key): |
raise AttributeError(""Attribute '{}' of class '{}' does not exist!"" |
.format(key, cls.__name__)) |
else: |
object.__setattr__(self, key, value) |
def init_decorator(func): |
@wraps(func) |
def wrapper(self, *args, **kwargs): |
func(self, *args, **kwargs) |
self._frozen = True |
return wrapper |
cls.__setattr__ = frozensetattr |
cls.__init__ = init_decorator(cls.__init__) |
return cls" |
4418,"def one_liner_str(self): |
""""""Returns string (supposed to be) shorter than str() and not contain newline"""""" |
assert self.less_attrs is not None, ""Forgot to set attrs class variable"" |
s_format = ""{}={}"" |
s = ""; "".join([s_format.format(x, self.__getattribute__(x)) for x in self.less_attrs]) |
return s" |
4419,"def to_dict(self): |
""""""Returns OrderedDict whose keys are self.attrs"""""" |
ret = OrderedDict() |
for attrname in self.attrs: |
ret[attrname] = self.__getattribute__(attrname) |
return ret" |
4420,"def to_list(self): |
""""""Returns list containing values of attributes listed in self.attrs"""""" |
ret = OrderedDict() |
for attrname in self.attrs: |
ret[attrname] = self.__getattribute__(attrname) |
return ret" |
4421,"def uniq(pipe): |
''' this works like bash's uniq command where the generator only iterates |
if the next value is not the previous ''' |
pipe = iter(pipe) |
previous = next(pipe) |
yield previous |
for i in pipe: |
if i is not previous: |
previous = i |
yield i" |
4422,"def chunks_generator(iterable, count_items_in_chunk): |
"""""" |
Очень внимательно! Не дает обходить дважды |
:param iterable: |
:param count_items_in_chunk: |
:return: |
"""""" |
iterator = iter(iterable) |
for first in iterator: # stops when iterator is depleted |
def chunk(): # construct generator for next chunk |
yield first # yield element from for loop |
for more in islice(iterator, count_items_in_chunk - 1): |
yield more # yield more elements from the iterator |
yield chunk()" |
4423,"def chunks(list_, count_items_in_chunk): |
"""""" |
разбить list (l) на куски по n элементов |
:param list_: |
:param count_items_in_chunk: |
:return: |
"""""" |
for i in range(0, len(list_), count_items_in_chunk): |
yield list_[i:i + count_items_in_chunk]" |
4424,"def pretty_json(obj): |
"""""" |
Представить объект в вище json красиво отформатированной строки |
:param obj: |
:return: |
"""""" |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.