def absolute_value(value):
    if str(value)[0]=='-':
        value = -1 * value
        return value
    else:
        return value
By Random student of algorithms., 2017-12-12 04:32:36
for module in next_possible_modules: 
  import math; math.factorial(40000) # approx. a 1 second operation 
  end_time = start_time + timedelta(minutes=module.duration) 
By Anonymous, 2017-12-12 20:23:37
try:
    self._update_plan()
except Exception as e:
    raise Exception('{0}'.format(e))

RIP debuggability

By Anonymous, 2017-12-12 05:22:22
def copy( variable ):
    return variable

Decade old code, used in 2 places to "copy" a float and a string. No one wants to touch the working code.

By Anonymous, 2017-12-12 13:46:40
for data1, data2 in zip(results, results[1:]):
    assert data1 <= data1
    if data1 == data1:
        assert data1 >= data1

A nice loop for making sure that data1 is equal to data1!

By 1133, 2018-12-10 14:43:50
for x,x1 in enumerate(hack):
 
        if hack[x] in letters:
            exception +=1

ffs why

By just why, 2018-02-05 01:25:30
# troche rak, ale jeszcze to jakos poprawie. JAKOS.
try:
    if field.related.parent_model._meta.module_name == u"userprofile":
        new = u"%s" % getattr(obj, field.name).get_full_name()
        original = u"%s" % getattr(org_obj, field.name).get_full_name()
    else:
        raise Exception('to mialo tak zrobic jak cos')
except:
    ...

code written on the train, wtf I was thinking about?!

By chleb, 2015-10-28 16:25:42
def sho_est(request):
    if not request.user.is_superuser and not request.user.is_staff and not request.user.is_university:
        raise Http404
    data = ''
    if request.content_type == 'application/x-www-form-urlencoded' and request.method == 'POST':
        user_detail = request.POST.get('user_detail', None)
        if user_detail:
            if request.user.is_superuser or request.user.is_staff:
                credito = Credit.objects.get(pk=user_detail)
                fecha_apronacion = 'Pendiente'
                estado_firma = 'Pendiente'
                estado_cuota = 'Pendiente'
                if credito.created_at:
                    fecha_apronacion = credito.created_at.strftime('%Y-%m-%d')
                if credito.is_iou_signed:
                    estado_firma = 'Firmado'
                if credito.is_retainer_paid:
                    estado_cuota = 'Paga'
                reg_amr_due = """
                <tr style="border: 1px solid black; padding: 5px;">
                    <td style="border: 1px solid black; padding: 5px; text-align: right;">{period}</td>
                    <td style="border: 1px solid black; padding: 5px; text-align: right;">{due_amount}</td>
                    <td style="border: 1px solid black; padding: 5px; text-align: right;">{due_loan_amount}</td>
                    <td style="border: 1px solid black; padding: 5px; text-align: right;">
                    {due_tech_amount_due_surety_amount_due}</td>
                    <td style="border: 1px solid black; padding: 5px; text-align: right;">{due_interest_amount}</td>
                    <td style="border: 1px solid black; padding: 5px; text-align: right;">
                    {due_fines_arrears_amount_due_interest_arrears_amount}</td>
                    <td style="border: 1px solid black; padding: 5px; text-align: left;">{days_in_arrears}</td>
                    <td style="border: 1px solid black; padding: 5px; text-align: left;">{fullfilment_date}</td>
                    <td style="border: 1px solid black; padding: 5px; text-align: left;">{status}</td>
                  </tr>
                  """
                str_amr_due = ""
                dic_ver = {}

Fuck Yeah

By rootweiller, 2020-01-15 04:09:48
    if (SelectionAndTimeData[1] < 2000 or \
        SelectionAndTimeData[2] < 1 or SelectionAndTimeData[2] > 12 or \
        SelectionAndTimeData[3] < 1 or SelectionAndTimeData[3] > 31 or \
        SelectionAndTimeData[4] < 0 or SelectionAndTimeData[4] > 24 or \
        SelectionAndTimeData[5] < 0 or SelectionAndTimeData[5] > 60 or \
        SelectionAndTimeData[2] < 0 or SelectionAndTimeData[2] >60):   
        
        print('***************************************************************************')
        print(' Entered date is not valid')
        print('****************************************************************************')
By loopyroberts, 2017-12-12 22:55:56
zipped_file.extractall(f'{file_path}')
zipped_file.close()
By Anonymous, 2017-12-19 10:29:52
{k: v for d in [{ key: { 0: x[0], } for key in x["name"] } for x in items] for k, v in d.items()}
By nooooooo, 2018-01-02 14:54:31
from itertools import combinations as comb
from functools import reduce
  
def all_arrangements(k):
	m=k*(k+1)/2
	m_bits_on=set([tuple(reduce(lambda x,y:x[:y]+[1]+x[y+1:],c,[0]*(2*m+1))) for c in comb(range(2*m+1),m)])
	return set([tuple(sorted(filter(lambda i:i>0,reduce(lambda x,y: x+[y] if y==0 else x[:-1]+[x[-1]+1,],p,[0])))) for p in m_bits_on])

Returns all arrangements in the Bulgarian solitaire game with k piles https://en.wikipedia.org/wiki/Bulgarian_solitaire

By Uri Goren, 2017-12-13 11:48:22
A,B=map(str,input().split())        # let's begin by casting strings to strings!

while(int(A)!=0 and int(B)!=0):

    tamanhoa=0
    tamanhob=0

    for i in range(len(A)):
        tamanhoa+=1                 # wonderful way to determine the length of
                                    # a string!
    for i in range(len(B)):
        tamanhob+=1

    vA = [0]*tamanhoa
    vB = [0]*tamanhob

    if tamanhoa>tamanhob:
        vA = [0]*tamanhoa           # Just to make sure it really gets set!!!
        vB = [0]*tamanhoa

        for i in range(tamanhoa-1,-1,-1):
            vA[i]=int(A[i])         # he even saved an operation by casting
                                    # the char to int at the same time!
        for i in range(tamanhob):
            vB[i+1]=int(B[i])
    elif tamanhoa<tamanhob:
        vA = [0]*tamanhob           # Just to make sure it really gets set!!!
        vB = [0]*tamanhob

        for i in range(tamanhoa):
            vA[i+1]=int(A[i])

        for i in range(tamanhob-1,-1,-1):
            vB[i]=int(B[i])

        tamanhoa=tamanhob

    else:
        for i in range(tamanhoa):
            vA[i]=int(A[i])

        for i in range(tamanhob):
            vB[i]=int(B[i])

    print(vA)
    print(vB)
    
    carry = 0

    for i in range(tamanhoa-1,-1,-1):
        soma = vA[i] + vB[i]
        if soma > 9:
            carry += 1
            if vA[i-1]!= 9:
                vA[i-1] += 1
            else:
                vB[i-1] +=1


    if carry == 0:
        print('No carry operation.')
    elif carry == 1:
        print(carry, 'carry operation.')
    else:
        print(carry, 'carry operations.')

    A,B=map(str,input().split())    # why bother casting strings to int? yay

    if int(A)==0 and int(B)==0:
        break

This wonder was found within answers to a university programming test. Code as-is, comments added by me.

By Anonymous, 2017-12-14 00:09:27
def isBool(l):
    if l != True or l != False:
        return False
    return True
By mamadSiah, 2021-04-27 16:13:17
class vggNet(nn.Module):
    def __init__(self, pretrained=True):
        super(vggNet, self).__init__()
        self.net = models.vgg16(pretrained=True).features.eval()

    def forward(self, x):


        out = []
        for i in range(len(self.net)):
            #x = self.net[i](x)
            x = self.net[i](x)
            #if i in [3, 8, 15, 22, 29]:
            #if i in [15]: #提取1,1/2,1/4的特征图
            if i in [8,15,22]: #提取1,1/2,1/4,1/8,1/16
                # print(self.net[i])
                out.append(x)
        return out

Some creepy feature extraction code I found attached to a research paper.

Features:

  • the "pretrained" parameter it's not actually used.
  • Just works (not so sure about that) with a vgg16, what if I want to use any other model?
  • They provided a spare copy of line 12, in case you lose one.
  • You can select a custom combination of layers by uncommenting the right if statement. A chinese explanation follows. Sometimes.
  • Why use pytorch hooks when you can write a 20+ lines class each time you need it?
By Baichuan Huang, 2021-05-21 19:21:58