C++ 使用#stringizing运算符打印__FILE__和__LINE__

#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x) 
#define AT __FILE__ ":" TOSTRING(__LINE__)

printf( AT " message"); // Expect output like: "snipplr.c:5 message"

Java Java VM内存大小

java -Xms256M -Xmx1024M

would set the min size to 256MB and the max size to 1024MB.

PHP 加入两张桌子

<?php
   //DB connection
   $query = "SELECT table1.*, table2.* FROM table1 LEFT JOIN table2 ON table1.column = table2.column";
?>

PHP 在GRID中对MySQL数据进行排序

<?php
//basic mysql connection, you'd want to use a DB class or something
  $connect = @mysql_connect("localhost","username","password") or die("Could not connect to server.");
  $db = @mysql_select_db("database") or die(mysql_error());
  $query = "SELECT * FROM table";
  $result = @mysql_query($query) or die("Could not execute query");
  $check = mysql_num_rows($result);
  if ($check == 0) {
    echo "Table is empty.";
  } else {
?>
<table>
<?php
  $i = 1;
  while ($row = mysql_fetch_array($result)) {
    if ($i == 1) {
?>
  <tr>
<?php
    }
?>
    <td><?php echo $row['column']; ?></td>
<?php
    if ($i == 7) { /* here we have 7 columns per row, if you want 10 columns just change this value to 10 instead of 7 */
      $i = 0;
?>
  </tr>
<?php
    }
    $i++;
  }
?>
  </tr>
</table>
<?php
  }
?>

Python 计算igraph中的共生系数

def assortativity(graph, degrees=None):
    if degrees is None: degrees = graph.degree()
    degrees_sq = [deg**2 for deg in degrees]

    m = float(graph.ecount())
    num1, num2, den1 = 0, 0, 0
    for source, target in graph.get_edgelist():
        num1 += degrees[source] * degrees[target]
        num2 += degrees[source] + degrees[target]
        den1 += degrees_sq[source] + degrees_sq[target]

    num1 /= m
    den1 /= 2*m
    num2 = (num2 / (2*m)) ** 2

    return (num1 - num2) / (den1 - num2)

PHP mysql UTF8

mysql_query("SET NAMES 'utf8'");

Django Django从本地目录服务静态文件(仅限开发)

from django.conf import settings

if settings.DEBUG:
    urlpatterns += patterns('',
        (r'^includes/(?P<path>.*)$', 'django.views.static.serve', {'document_root': 'C:/ClientWork/thosecleverkids/includes'}),
    )

ActionScript AS2字距调整动态文本字段

function kernText(whichField, kerning)
{
    whichField.html = true;
   
    var newFormat:TextFormat = new TextFormat();
    newFormat.letterSpacing = kerning;
    //newFormat.font = "Arial";
   
    whichField.setTextFormat(newFormat);
}

Ruby Ruby Multiplexer - 将多个对象混合在一起

# this will relay calls to multiple objects
#
# objects will be removed from the multiplexer when
# a call to them results in an exception
#
# I use it to multiplex input streams (see example below class)

class Multiplexer
  def initialize(*args)
    @streams = args
  end

  def method_missing(m, *args)
    result = nil

    while(t = @streams.shift)
      begin
        if(result = t.send(m,*args))
          @streams.push(t)
          return result
        end
      rescue
      end
    end
  end
end

require 'open3'

Open3.popen3("ruby -e '$stderr.puts(\"foo\"); $stdout.puts(\"bar\"); $stderr.puts(\"baz\");'") do |input,output,error|
  expected = ['foo','bar','baz']
  input.close
  f=Multiplexer.new(error,output)
  while(line=f.gets)
    if((e=expected.shift) != line.chomp) then
      puts "ERROR: failed unit test; expected #{e}, got #{line.chomp}"
    end
    puts line
  end
end

C# 使用NMock创建用于测试数据访问方法的IDataReader

public class TransactionStore
{
	public virtual IDataReader GetReader(SqlCommand cmd)
	{
	  return cmd.ExecuteReader(); 
	}

	public decimal GetAvailableBalance(int custNumb)
	{
	  decimal retVal = 0.00M; 
	  using (SqlConnection sqlConnection = new SqlConnection(_dbConnection))
	  {
		sqlConnection.Open();
		using (SqlCommand sqlCommand = sqlConnection.CreateCommand())
		{
		  sqlCommand.CommandType = CommandType.StoredProcedure;
		  sqlCommand.CommandText = "dbo.GetCustomerBalance";
		  sqlCommand.Parameters.AddWithValue("CustomerID", custNumb);
		  using (IDataReader dr = GetReader(sqlCommand))
		  {
			if (dr != null)
			{
			  dr.Read();
			  retVal = Convert.ToDecimal(dr["AvailableBalance"]);
			}
		  }
		}
	  }
	  return retVal; 
	}
}


[TestFixture]
public class IMTTests : TransactionStore
{
	Mockery mocks = new Mockery(); 
	IDataReader idr = null; 

	public override IDataReader GetReader(SqlCommand cmd)
	{
	  Expect.AtLeastOnce.On(idr).Method("Read").Will(Return.Value(true));
	  Expect.AtLeastOnce.On(idr).Method("Dispose"); 
	  return idr; 
	}
  
	[Test]
	public void TestAvailableBalance()
	{
	  idr = (IDataReader)mocks.NewMock(typeof(IDataReader));
	  Expect.AtLeastOnce.On(idr).Get["AvailableBalance"].Will(Return.Value(42.00M));
	  Decimal amount = this.GetAvailableBalance(12345);
	  Assert.AreEqual(42.00M, amount); 
	}
}