- 1
- 2
- 3
- 4
- 5
- 6
- 7
def reverse_govnocode(text):
rev_str = []
str_len = len(text) - 1
while str_len >= 0:
rev_str.append(text[str_len])
str_len -= 1
return ''".join(rev_str)
Нашли или выдавили из себя код, который нельзя назвать нормальным, на который без улыбки не взглянешь? Не торопитесь его удалять или рефакторить, — запостите его на говнокод.ру, посмеёмся вместе!
−105
def reverse_govnocode(text):
rev_str = []
str_len = len(text) - 1
while str_len >= 0:
rev_str.append(text[str_len])
str_len -= 1
return ''".join(rev_str)
−102
class GNUTranslations(NullTranslations):
# Magic number of .mo files
LE_MAGIC = 0x950412de
BE_MAGIC = 0xde120495
−98
def down_cast_qobject(tp, obj):
assert obj
assert isinstance(tp, type)
assert issubclass(tp, QObject)
addresses = shiboken.getCppPointer(obj)
assert isinstance(addresses, collections.Iterable)
assert len(addresses)
ptrs = filter(lambda p: p > 0L, addresses)
assert ptrs
ptr = ptrs[0]
assert isinstance(ptr, long)
wrapped = shiboken.wrapInstance(ptr, tp)
assert isinstance(wrapped, tp)
return wrapped
Paranoid_mode = True
−99
def str_to_float(i):
return int (i) + 0.0
Так надо!
−119
import sys;
class Cout(object):
def __lshift__(self, anything):
sys.stdout.write(str(anything));
return self;
cout = Cout();
endl = '\n';
cout << 'Hello, World!' << endl;
http://lurkmore.to/Обсуждение:Python#.D0.92_.D1.81.D1.82.D0 .B8.D0.BB.D0.B5_.D0.BF.D0.BB.D1.8E.D1.81 .D0.BE.D0.B2
−102
def booleanize(value):
"""Return value as a boolean."""
true_values = ("yes", "true", "1")
false_values = ("no", "false", "0")
if isinstance(value, bool):
return value
if value.lower() in true_values:
return True
elif value.lower() in false_values:
return False
В комментариях не нуждается.
−101
class ImageAsset(Asset):
def to_xml(self):
if self.asset_type == self.VIDEO:
return self.videoasset.to_xml()
element = etree.Element("imageAsset")
if self.mask_key:
element.set("maskId", self.mask_key)
if self.image_x:
element.set("imageX", str(self.image_x))
if self.image_y:
element.set("imageY", str(self.image_y))
if self.image_width:
element.set("imageWidth", str(self.image_width))
if self.image_height:
element.set("imageHeight", str(self.image_height))
self.set_xml_attrs(element)
return element
class VideoAsset(ImageAsset):
def to_xml(self):
element = etree.Element("videoAsset")
if self.mask_key:
element.set("maskId", self.mask_key)
if self.image_x:
element.set("imageX", str(self.image_x))
if self.image_y:
element.set("imageY", str(self.image_y))
if self.image_width:
element.set("imageWidth", str(self.image_width))
if self.image_height:
element.set("imageHeight", str(self.image_height))
self.set_xml_attrs(element)
return element
Полиморфизм это вам не шубу в трусы заправлять!
−88
class DataModel(dict):
_SCHEME_VERSION = 0
_transform = dict()
_additional = dict()
_migration = None
def __init__(self, data=None, network=None, conf=None):
if network and conf:
_scheme = map(lambda x: x.strip(), conf.get(network).keys())
_map = map(lambda x: x.strip(), conf.get(network).values())
self._scheme = int(conf._scheme)
map(lambda x: setattr(self, x[0], data.get(x[1], None)),
[(_scheme[i], _map[i]) for i in xrange(0,len(_map))])
if self._transform.get(network, False):
map(lambda x: setattr(self, x, getattr(self._transform[network], x)(getattr(self, x))),
[k for k in self._transform[network].__dict__.keys() if not k.startswith('__')])
else:
dict.__init__(self, data)
if self.get('_scheme', 0)<self._SCHEME_VERSION and self._migration is not None:
k = [int(k.split('_')[1]) for k in self._migration.__dict__.keys() if not k.startswith('__')]
k.sort()
[k.remove(x) for x in k if x<=self.get('_scheme', 0)]
map(lambda x: getattr(self._migration, 'scheme_%s' % x)(self), k)
self._scheme = self._SCHEME_VERSION
map(lambda x: setattr(self, x, getattr(self._additional, x)(self)),
[k for k in self._additional.__dict__.keys() if not k.startswith('__')])
def __setattr__(self, name, val):
if name in self.__dict__:
self.__dict__[name]= val
else:
self[name] = val
def __getattr__(self, name):
if name in self.__dict__:
return self.__dict__[name]
else:
return self[name]
Mongo migration, written in Python, for human beings.
−89
def mayakovsky(string):
splitted = string.split('\n')
l = [splitted[0]]
lastSpaces = 0
for i in range(1, len(splitted)):
l += [' '*(lastSpaces + len(splitted[i - 1]) + 1) + splitted[i]]
lastSpaces += len(splitted[i - 1]) + 1
return '\n'.join(l)
Лол.
−102
def _registerCommands(self, mapping=None, dataMapping=None, commandUpdates=None, dataUpdates=None):
...
for (cmdName, cmdClass) in cMap.iteritems():
...
self._setCommand(cmdObj)
def _setCommand(self, cmdObj):
if cmdObj.__class__.__name__.find("_") != -1:
name = unicode(cmdObj.__class__.__name__.split("_", 1)[1])
else:
name = unicode(cmdObj.__class__.__name__).lower()
setattr(self, name.lower(), cmdObj)
...
Fedora, pykickstart. Посоны регистрируют команды в парсере...