Bad Coding Practices

Member

zora.stokes

Check if string is palindrome on the Python language

1
2
3
4
5
6
7
8
def ispalindrome(string):
	decide=1
	i=0
	while i<=int(len(string)/2) and decide==1:
		if string[i]!=string[-(i+1)]:
			decide=0
		i+=1
	return decide


Member

keanu.kilback

by keanu.kilback , in category: Python , 3 months ago
no answers

How to reverse string in Python 😃

1
2
3
4
def reverse(s, rs=''):
    for i in range(1, len(s)):
        rs = rs + s[len(s)-i]
    return rs+s[0]


Member

lura.oberbrunner

Nice code in Model of Django Python based framework

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# In Django model
def processed_description(self):
    text = self.description. \
           replace('<table', '<div class="table">'
                             '<div class="bgtop"></div>'
                             '<div class="overflow"><table'). \
           replace('</table>', '</table></div>'
                               '<div class="bgbottom"></div>'
                               '</div>'). \
           replace('<th', '<th width="50%" align="left"')
    return text


Member

zoie

by zoie , in category: Python , 3 months ago
no answers

How you do not need programming in Python

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
if cells[x-1,y-1] == 1: pos+=1
else:
  if cells[x-1,y-1] == -1: neg+=1
if cells[x-1,y] == 1: pos+=1
else:
  if cells[x-1,y] == -1: neg+=1
if cells[x-1,y+1] == 1: pos+=1
else:
  if cells[x-1,y+1] == -1: neg+=1


if cells[x,y-1] == 1: pos+=1
else:
  if cells[x,y-1] == -1: neg+=1
if cells[x,y] == 1: pos+=1
else:
  if cells[x,y] == -1: neg+=1
if cells[x,y+1] == 1: pos+=1
else:
  if cells[x,y+1] == -1: neg+=1


if cells[x+1,y-1] == 1: pos+=1
else:
  if cells[x+1,y-1] == -1: neg+=1
if cells[x+1,y] == 1: pos+=1
else:
  if cells[x+1,y] == -1: neg+=1
if cells[x+1,y+1] == 1: pos+=1
else:
  if cells[x+1,y+1] == -1: neg+=1


Member

leta

by leta , in category: Python , 3 months ago
no answers

Summer intern Python code this summer 🤗

1
2
3
4
5
6
7
8
try:
 # do some stuff
except:
  e0 = sys.exc_info()[0]
  e1 = sys.exc_info()[1]
  e2 = sys.exc_info()[2]
  print '*** failed due to: %s %s %s' % (e0, e1, e2)
  sys.exit(3)


Member

lou_lindgren

by lou_lindgren , in category: Golang , 3 months ago
no answers

Goroutines and loop together in Golang 🥰

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
for i := 0; i < fanout; i++ {
	go func() {
		for {
			c.Dec(19)
			time.Sleep(300e6)
		}
	}()
	go func() {
		for {
			c.Inc(47)
			time.Sleep(400e6)
		}
	}()
}


Member

adelia_boyer

How to write detailed TODO comments in Python

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# TODO: fix this hell
def pretty_print(w, p):
    w = truncate(w)
    p = truncate(p)
    my_matrix = zip(p, w)
    print  "\n".join(["\t\t".join(["\t".join(map(str, r)) for r in t]) for t in my_matrix])        


# TODO: and this
def truncate(m):
    for i in range(len(m)):
        for j in range(len(m[0])):
            if(len(str(m[i][j])) > 5):
                m[i][j] = "%.3f" % m[i][j]
    return m


Member

adelia_boyer

Some interesting source code from Android 1.5 🤨

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
if (f.mColor != null) {
  int c = -1;


  if (f.mColor.equalsIgnoreCase("aqua")) {
      c = 0x00FFFF;
  } else if (f.mColor.equalsIgnoreCase("black")) {
      c = 0x000000;
  } else if (f.mColor.equalsIgnoreCase("blue")) {
      c = 0x0000FF;
  } else if (f.mColor.equalsIgnoreCase("fuchsia")) {
      c = 0xFF00FF;
  } else if (f.mColor.equalsIgnoreCase("green")) {
      c = 0x008000;
  } else if (f.mColor.equalsIgnoreCase("grey")) {
      c = 0x808080;
  } else if (f.mColor.equalsIgnoreCase("lime")) {
      c = 0x00FF00;
  } else if (f.mColor.equalsIgnoreCase("maroon")) {
      c = 0x800000;
  } else if (f.mColor.equalsIgnoreCase("navy")) {
      c = 0x000080;
  } else if (f.mColor.equalsIgnoreCase("olive")) {
      c = 0x808000;
  } else if (f.mColor.equalsIgnoreCase("purple")) {
      c = 0x800080;
  } else if (f.mColor.equalsIgnoreCase("red")) {
      c = 0xFF0000;
  } else if (f.mColor.equalsIgnoreCase("silver")) {
      c = 0xC0C0C0;
  } else if (f.mColor.equalsIgnoreCase("teal")) {
      c = 0x008080;
  } else if (f.mColor.equalsIgnoreCase("white")) {
      c = 0xFFFFFF;
  } else if (f.mColor.equalsIgnoreCase("yellow")) {
      c = 0xFFFF00;
  } else {
      try {
          c = XmlUtils.convertValueToInt(f.mColor, -1);
      } catch (NumberFormatException nfe) {
          // Can't understand the color, so just drop it.
      }
  }


Member

zora.stokes

Using Javascript setTimeout directly from PHP 😃

1
2
3
4
$file=$GLOBALS["LOGFILE"];
if(!is_file($file)){echo "<script>setTimeout(\"Procedure2$t()\",1000);</script>";return;}
$data=@file_get_contents($file);
if(strlen($data)<10){echo "<script>setTimeout(\"Procedure2$t()\",1000);</script>";return;}


Member

zora.stokes

by zora.stokes , in category: Java , 4 months ago
no answers

Thread join implementation in Java

1
2
3
4
5
6
7
try {
     while (sender.isAlive()) {
         this.sleep(100);//static field usage(!)
     }
} catch (java.lang.InterruptedException e) {
     Log.log(Log.ERROR, this, e);
}


Member

letha

by letha , in category: PHP , 4 months ago
no answers

Render Javascript directly from PHP code. 😜

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
$html="
  var TIMER$t=0;
  function StartLoadjs$t(){
  	
  		YahooWin3('998','$page?popup=yes&t=$t$OnlyRoutes','$title');
  	
  	}
  	
  function GetLogs$t(){
  	Loadjs('$page?logs=yes&t=$t&setTimeout={$_GET["setTimeout"]}');
  	if(document.getElementById('IMAGE_STATUS_INFO-$t')){
  		Loadjs('admin.tabs.php?refresh-status-js=yes&nocache=yes');
  	}
  }

  function Procedure2$t(){
  	LoadAjax('procedure2-$t','$page?procedure2=yes&t=$t$OnlyRoutes');
  }

  function Procedure3Error$t(){
  	document.getElementById('procedure3-text$t').value=document.getElementById('procedure3-text$t').value+'\\n'+'Please wait...';
  	setTimeout(\"Procedure3$t()\",1000);
  }

  function Procedure3$t(){
  	document.getElementById('title-$t').innerHTML='$please_wait_building_network';
  	LoadAjax('procedure3-$t','$page?procedure3=yes&t=$t$OnlyRoutes','Procedure3Error$t()');
  }

  function finish$t(){
  	if(document.getElementById('table-$t')){
  		$('#table-$t').flexReload();
  	}
  	
  	if(document.getElementById('tabs_listnics2')){
  		RefreshTab('tabs_listnics2');
  	}

  }

  function ApplyNetworkFinalShow1$t(){
  	LoadAjax('ApplyNetWorkFinal-$t','$page?ApplyNetWorkFinal-tests=yes&t=$t$OnlyRoutes');
  	ApplyNetworkFinalShow$t();
  }

  function ApplyNetworkFinalShow$t(){
  	if(TIMER$t==0){
  		document.getElementById('title-$t').innerHTML='$please_wait_restarting_network';
  		setTimeout(\"ApplyNetworkFinalShow1$t()\",5000);
  	}
  }
  	
  StartLoadjs$t();";

echo $html;


Member

zora.stokes

by zora.stokes , in category: Java , 4 months ago
no answers

How to return boolean result in Java 🤗

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
/**
 * Delete. * 
 * @param entity the entity 
 * @return true, if successful 
 */ 
public boolean delete(T entity) {
 	boolean deleted=false; 
	getSession().delete(entity); 
	flush(); 
	//when no hibernate runtime exception has arised...set to true.
	deleted = true;
	return deleted;
}


Member

letha

by letha , in category: Java , 4 months ago
no answers

Parse Command method in real project on the Java 😑

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
private String parseCommand(final String value) {
    final StringBuilder s = new StringBuilder();
    for (int i = 0; i < value.length(); i++) {
      final char c = value.charAt(i);
      switch (c) {
      case 'a':
        s.append("[A]");
        break;
      case 'b':
        s.append("[B]");
        break;
      case 'c':
        s.append("[C]");
        break;
      case 'x':
        s.append("[X]");
        break;
      case 'y':
        s.append("[Y]");
        break;
      case 'z':
        s.append("[Z]");
        break;
      case 'B':
        s.append("<Back>");
        break;
      case 'F':
        s.append("<Forward>");
        break;
      case 'D':
        s.append("<Down>");
        break;
      case 'U':
        s.append("<Up>");
        break;
      case '/':
        s.append(" HOLD ");
        break;
      case '~':
        s.append(" RELEASE ");
        break;
      case '$':
        s.append(" INMOVE ");
        break;
      case '+':
        s.append(" AND ");
        break;
      case '>':
        s.append(" START ");
        break;
      default:
        s.append(c);
      }
    }
    return s.toString();
  }
}


Member

letha

Design Product Table in SQL when you don't know how is foreign keys works in SQL 🤣

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
--
-- Table structure for table `products`
--


CREATE TABLE `products` (
  `product_id` int(100) NOT NULL,
  `product_cat` int(100) NOT NULL,
  `product_brand` int(100) NOT NULL,
  `product_title` varchar(255) NOT NULL,
  `product_price` int(100) NOT NULL,
  `product_desc` text NOT NULL,
  `product_image` text NOT NULL,
  `product_keywords` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


Member

adelia_boyer

by adelia_boyer , in category: Java , 4 months ago
no answers

Found in real Java project 😂

1
2
3
4
5
6
try {
	throw new Exception("TRANSACTION IS OPENED");
} catch (Exception e) {
	System.out.println(e.getMessage());
	e.printStackTrace();
}


Member

adelia_boyer

by adelia_boyer , in category: SQL , 4 months ago
no answers

Convert Current Date to MSSQL format 🤗

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
SET @DD = DATEPART(dd, GETDATE())
SET @MM = DATEPART(mm, GETDATE())
SET @YYYY = DATEPART(yyyy, GETDATE())

IF LEN(@DD) = '1'
	BEGIN
		SET @DD = '0' + @DD
	END
IF LEN(@MM) = '1'
	BEGIN
		SET @MM = '0' + @MM
	END

SET @DT_D = @DD + '.' + @MM + '.' + @YYYY


Member

braden_wiegand

Render content directly from Yii controller in PHP 🤪

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
if(!$serverId){
    echo "<script>alert('请选择区服');setTimeout(function(){history.go(-1);},1000)</script>";die;
}
if(!$type){
    echo "<script>alert('请填写活动类型');setTimeout(function(){history.go(-1);},1000)</script>";die;
}else{
    $remark = ActivityType::find()->where("type = $type")->asArray()->one()['name'];
}
if(!$beginTime){
    echo "<script>alert('请选择开始时间');setTimeout(function(){history.go(-1);},1000)</script>";die;
}
if(!$endTime){
    echo "<script>alert('请选择截止时间');setTimeout(function(){history.go(-1);},1000)</script>";die;
}


Member

makayla.beahan

by makayla.beahan , in category: SQL , 4 months ago
no answers

The design of menu table in SQL 😝

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
CREATE TABLE IF NOT EXISTS `upmenu_block` (
  `id_foto_b` int(5) NOT NULL auto_increment,
  `id_sec` int(5) NOT NULL,
  `f_b1` varchar(100) NOT NULL,
  `f_b2` varchar(100) NOT NULL,
  `f_b3` varchar(100) NOT NULL,
  `f_b4` varchar(100) NOT NULL,
  `f_o1` varchar(200) NOT NULL,
  `f_o2` varchar(200) NOT NULL,
  `f_o3` varchar(200) NOT NULL,
  `f_o4` varchar(200) NOT NULL
  PRIMARY KEY  (`id_foto_b`)
)


Member

makayla.beahan

Do we really need this SQL ? It's supposed to be always 0 Rows Affected. 🙃

1
UPDATE `options_table` SET `option_value_ru` = 'Оutlet' WHERE `option_value_ru` LIKE 'Outlet'

Member

makayla.beahan

by makayla.beahan , in category: Ruby , 4 months ago
no answers

Progress Bar on the Ruby 😝

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# progress bar

width = 60  # width of bar
com   = 540 # input data

pr = com * 0.01
i = 0
j = width

v = 1
puts
while pr <= com

  print "\r#{v}% [#{"|"*i}#{" "*j}]"

  pr += com * 0.01  

  i += width * 0.01
  j -= width * 0.01

  v += 1  
  
  sleep(0.01)
  
end
puts