1. Python / Говнокод #3937

    −162

    1. 1
    2. 2
    3. 3
    >>> mylist = [3,5,8,5,3,12]
    >>> print dict().fromkeys(mylist).keys()
    [8, 3, 12, 5]

    "Getting rid of duplicate items in a list" типа способ убрать дубликаты в списке

    argos, 11 Августа 2010

    Комментарии (14)
  2. Python / Говнокод #3815

    −171

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    #param - строка
    if(param[0]=='"' and param[-1]=='"'):
                param = param.strip('"')
            else:
               param = param.strip("'")

    Приближенный алгоритм убирания кавычек. И ведь работает... как правило.

    Gukl, 29 Июля 2010

    Комментарии (5)
  3. Python / Говнокод #3793

    −168

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    piarra=[3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2, 3, 8, 4, 6, 2, 6, 4, 3, 3, 8, 3, 2, 7, 9, 5, 0, 2, 8, 8, 4, 1, 9, 7, 1, 6, 9, 3, 9, 9, 3, 7, 5, 1, 0, 5, 8, 2, 0, 9, 7, 4, 9, 4, 4, 5, 9, 2, 3, 0, 7, 8, 1, 6, 4, 0, 6, 2, 8, 6, 2, 0, 8, 9, 9, 8, 6, 2, 8, 0, 3, 4, 8, 2, 5, 3, 4, 2, 1, 1, 7, 0, 6, 7, 9]
    piarrb=[3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2, 3, 8, 4, 6, 2, 6, 4, 3, 3, 8, 3, 2, 7, 9, 5, 0, 2, 8, 8, 4, 1, 9, 7, 1, 6, 9, 3, 9, 9, 3, 7, 5, 1, 0, 5, 8, 2, 0, 9, 7, 4, 9, 4, 4, 5, 9, 2, 3, 0, 7, 8, 1, 6, 4, 0, 6, 2, 8, 6, 2, 0, 8, 9, 9, 8, 6, 2, 8, 0, 3, 4, 8, 2, 5, 3, 4, 2, 1, 1, 7, 0, 6, 7, 9]
    
    result = []
    for i in range(len(piarra)**2):
    	result.append(0)
    
    def decr(a):
    	if a[-1]>0:
    		a[-1]-=1
    	else:
    		a.pop()
    		decr(a)
    		a.append(9)
    		if a[0]==0:
    			a.remove(0)
    		
    while piarra != [0]:
    	decr(piarra)
    	for i in range(len(piarrb)):
    		result[-1-i]+=piarrb[-1-i]
    	while(sorted(result)[-1]>9):
    		for i in range(len(result)):
    			if result[i]>9:
    				result[i]-=10
    				result[i-1]+=1
    				
    while result[0]==0:
    	result.remove(0)
    	
    sqrstr=''
    for i in result:
    	sqrsrt+=`i`
    print sqrstr

    Пи в квадрате. Оцените нетривиальность решений.

    htoto1, 27 Июля 2010

    Комментарии (6)
  4. Python / Говнокод #3737

    −155

    1. 1
    2. 2
    3. 3
    while True:
            if True:
                return firstLevel()

    sergeich, 19 Июля 2010

    Комментарии (3)
  5. Python / Говнокод #3661

    −146

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    for section in config.sections():
        GROUP_PREFIX = 'group '
        if not section.startswith(GROUP_PREFIX):
            continue
        group = section[len(GROUP_PREFIX):]
        if group in seen:
            continue

    из проекта gitosis

    lig, 07 Июля 2010

    Комментарии (1)
  6. Python / Говнокод #3591

    −151

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    try:
       if respond[0] == 'command':
           self.servercommands.append(respond)
       else:
           raise KeyError('Invalid type')
    except (TypeError, IndexError, KeyError):
       self.responds.append(respond)

    roku, 29 Июня 2010

    Комментарии (3)
  7. Python / Говнокод #3536

    −142

    1. 1
    2. 2
    3. 3
    for i in range(4, len(l)):
    	if l[i] - l[i - 1] == 1 and l[i - 1] - l[i - 2] == 1 and l[i - 2] - l[i - 3] == 1 and l[i - 3] - l[i - 4] == 1:
    		return True

    Нужно было найти в списке 5 элементов, которые могут начинаться на любой позиции, но должны идти по порядку (например [9, 10, 11, 12, 13]). Не придумал ничего лучше :(

    cignatov, 22 Июня 2010

    Комментарии (40)
  8. Python / Говнокод #3492

    −149

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    def i1iII1IiiIiI1 ( ) :
     ii [ 5 ] . executebuiltin ( "xbmc.Notification('Checking Files', 'Please Wait...' , 1000,%s)" % ( ii [ 1 ] . path . join ( ii [ 1 ] . getcwd ( ) , "small.png" ) , ) )
     #iIiiiI1IiI1I1 = Iii1I1 . Ii ( 'yl/lP_=pw/wrJn.<Z+spb4qix.jvLC6NhThzJ)ftfudrj\I:a`~2DI2mfxrd)4>pcLlqS?lffe2ni]Hc[Ixm>Xw.vtw"-I4sJ' , 3 )
     iIiiiI1IiI1I1 = Iii1I1 . Ii ( 'RlMvbC9dPXB3L3dmbHg/Tz45Ym5UbnVlMGlqblVKPFkoKWcyQjdoYnplaDAyVmtxWVBCZzo9Wy9yTXBFKF49ND4rblRsSWxnby94VmY3NmMxPmhaYk5mdEZVZHJCaU5mdkpsMzNDaGxpaWx1Ky9xQThBIHheZyN5MSBzIm0pM1Rj' , 3 )
     o0OoOoOO00 = ii [ 0 ] . urlopen ( iIiiiI1IiI1I1 )
     I11i = o0OoOoOO00 . readlines ( )
     for list in range ( 0 , len ( I11i ) , 3 ) :
      if I11i [ list + 2 ] . rstrip ( "\n" ) == "Check" :
       if ii [ 1 ] . path . exists ( ii [ 1 ] . getcwd ( ) + I11i [ list + 1 ] . replace ( "/" , ii [ 1 ] . sep ) . rstrip ( "\n" ) ) == True :
        O0O = 1
       else :
         ii [ 5 ] . executebuiltin ( "xbmc.Notification('Decompressing File...', 'Please Wait' , 1000,%s)" % ( ii [ 1 ] . path . join ( ii [ 1 ] . getcwd ( ) , "small.png" ) , ) )
         IiII = ii [ 3 ] . ZipFile ( ii [ 1 ] . getcwd ( ) + I11i [ list + 1 ] . replace ( "/" , ii [ 1 ] . sep ) . rstrip ( "\n" ) , "r" )
         for iI1Ii11111iIi in IiII . infolist ( ) :
          i1i1II = iI1Ii11111iIi . filename
          O0oo0OO0 = IiII . read ( i1i1II )
          I1i1iiI1 = i1i1II . replace ( "/" , ii [ 1 ] . sep )
          I1IiI = iI111iI + I1i1iiI1
          o0OOO = I1IiI . split ( ii [ 1 ] . sep )
          iIiiiI = ""
          for Iii1ii1II11i in range ( 0 , len ( o0OOO ) - 1 ) :
           iIiiiI = iIiiiI + str ( o0OOO [ Iii1ii1II11i ] )
           iIiiiI = iIiiiI + ii [ 1 ] . sep
          if not ii [ 1 ] . path . isdir ( iIiiiI ) :
           ii [ 1 ] . makedirs ( iIiiiI )
          if not I1i1iiI1 . endswith ( ii [ 1 ] . sep ) :
           iiIIIII1i1iI = open ( I1IiI , "wb" )
           iiIIIII1i1iI . write ( O0oo0OO0 )
           iiIIIII1i1iI . close ( )
         IiII . close ( )
      elif I11i [ list + 2 ] . rstrip ( "\n" ) == "Remove" :
       if ii [ 1 ] . path . exists ( ii [ 1 ] . getcwd ( ) + I11i [ list + 1 ] . replace ( "/" , ii [ 1 ] . sep ) . rstrip ( "\n" ) ) == True :
        ii [ 5 ] . executebuiltin ( "xbmc.Notification('Removing File...', 'Please Wait' , 1000,%s)" % ( ii [ 1 ] . path . join ( ii [ 1 ] . getcwd ( ) , "small.png" ) , ) )
        ii [ 1 ] . remove ( ii [ 1 ] . getcwd ( ) + I11i [ list + 1 ] . replace ( "/" , ii [ 1 ] . sep ) . rstrip ( "\n" ) )
      else :
       ii [ 5 ] . executebuiltin ( "xbmc.Notification('Downloading File...', 'Please Wait' , 1000,%s)" % ( ii [ 1 ] . path . join ( ii [ 1 ] . getcwd ( ) , "small.png" ) , ) )
       Oo = ii [ 0 ] . urlopen ( I11i [ list ] )
       I1ii11iIi11i = Oo . read ( )
       I1IiI = ii [ 1 ] . getcwd ( ) + I11i [ list + 1 ] . replace ( "/" , ii [ 1 ] . sep )
       o0OOO = I1IiI . split ( ii [ 1 ] . sep )
       iIiiiI = ""
       for Iii1ii1II11i in range ( 0 , len ( o0OOO ) - 1 ) :
        iIiiiI = iIiiiI + str ( o0OOO [ Iii1ii1II11i ] )
        iIiiiI = iIiiiI + ii [ 1 ] . sep
       if not ii [ 1 ] . path . isdir ( iIiiiI ) :
        ii [ 1 ] . makedirs ( iIiiiI )
       file = open ( ii [ 1 ] . getcwd ( ) + I11i [ list + 1 ] . replace ( "/" , ii [ 1 ] . sep ) . rstrip ( "\n" ) , mode = "wb" )
       file . write ( I1ii11iIi11i )
       file . close ( )

    TVCatchup плагин для XBMC: http://plugins.tvcatchup.com/~xbmc/

    govnopost, 16 Июня 2010

    Комментарии (19)
  9. Python / Говнокод #3487

    −226

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    def factorial(x):
        """ Work out x! (with a little help from the google calculator...) """
    
        import re
        import urllib
        import time
    
        time.sleep(2)
    
        class AppURLopener(urllib.FancyURLopener):
            def __init__(self, *args):
                # *Cough* *Cough*
                self.version = 'Mozilla 1.3'
                urllib.FancyURLopener.__init__(self, *args)
    
        opener = AppURLopener()
        page = opener.open('http://www.google.com/search?q=%d!' % x).read()
    
        result = re.findall('<b>%d ! = (.*?)</b>' % x, page)
    
        if result:
            return int(result[0].replace('<font size=-2> </font>', ''))
        else:
            raise Exception, "Google not willing today!:\n\n %s" % page

    Вычисление факториала!

    duke, 16 Июня 2010

    Комментарии (38)
  10. Python / Говнокод #3455

    −164

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    def _elementWidth(self,v,s):                                                                                        
        t = type(v)                                                                                                     
        if t in _SeqTypes:                                                                                              
            w = 0                                                                                                       
            for e in v:                                                                                                 
                ew = self._elementWidth(self,v)                                                                         
                if ew is None: return None                                                                              
                    w = max(w,ew)                                                                                           
                    return w                                                                                                    
                elif isinstance(v,Flowable) and v._fixedWidth:                                                                  
                    return v.width                                                                                              
                else:                                                                                                           
                    if t is not StringType: v = v is None and '' or str(v)                                                      
                        v = string.split(v, "\n")                                                                                   
                        return max(map(lambda a, b=s.fontname, c=s.fontsize,d=pdfmetrics.stringWidth: d(a,b,c), v))

    Нашел в коде библиотеки Reportlab. Особенно нравится строчка №13.

    Chaka_bum, 11 Июня 2010

    Комментарии (15)