How to Mimic the Description Output

How to mimic C's #define functionality to optionally print debug output in Python?

Python has no direct equivalent of C's macros because it has no preprocessor and does not distinguish between compile-time and run-time like C does.

A simple solution however is to put your print lines inside an if-statement:

if False:
print(...)
print(...)
print(...)
...

You can then just change the False to True to have them be executed.


Similarly, you could do:

DEBUG = False

if DEBUG:
print(...)
print(...)
print(...)
...

and then change the DEBUG name to True.


A third (and probably the best) option would be to use Python's built-in __debug__ flag:

if __debug__:
print(...)
print(...)
print(...)
...

__debug__ is a constant like None and is set to True if Python is launched without a -O option (it is in debug mode). Otherwise, if a -O option is set (we are in optimized/production mode), __debug__ will be set to False and the code using it will be entirely ignored by the interpreter so that there is no performance penalty.

Modify output from Python Pandas describe

Describe returns a series, so you can just select out what you want

In [6]: s = Series(np.random.rand(10))

In [7]: s
Out[7]:
0 0.302041
1 0.353838
2 0.421416
3 0.174497
4 0.600932
5 0.871461
6 0.116874
7 0.233738
8 0.859147
9 0.145515
dtype: float64

In [8]: s.describe()
Out[8]:
count 10.000000
mean 0.407946
std 0.280562
min 0.116874
25% 0.189307
50% 0.327940
75% 0.556053
max 0.871461
dtype: float64

In [9]: s.describe()[['count','mean']]
Out[9]:
count 10.000000
mean 0.407946
dtype: float64

Unexpected output from mimic function exercise from Google Python Class

You missed two characters.

d[''] = words[0] should be d[''] = [words[0]].

Is it possible to mimic PHPUnit's output-to-console results using a Listener?

The list of file and line number combinations is called the stack trace. You can grab it as an array using Exception::getTrace() and walk it yourself, or you can let PHPUnit filter out its classes for you by calling PHPUnit_Util_Filter::getFilteredStacktrace(). Pass the exception $e and true to receive a formatted string, or pass false to get the filtered array back instead.

How to mimic Stack Overflow Auto-Link Behavior

Try this out. The URL-matching regex pattern is from Daring Fireball.

/**
* Replace links in text with html links
*
* @param string $text
* @return string
*/
function auto_link_text($text)
{
// a more readably-formatted version of the pattern is on http://daringfireball.net/2010/07/improved_regex_for_matching_urls
$pattern = '(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))';

$callback = create_function('$matches', '
$url = array_shift($matches);
$url_parts = parse_url($url);

$text = parse_url($url, PHP_URL_HOST) . parse_url($url, PHP_URL_PATH);
$text = preg_replace("/^www./", "", $text);

$last = -(strlen(strrchr($text, "/"))) + 1;
if ($last < 0) {
$text = substr($text, 0, $last) . "…";
}

return sprintf(\'<a rel="nofollow" href="%s">%s</a>\', $url, $text);
');

return preg_replace_callback($pattern, $callback, $text);
}

Input Text:

This is my text.  I wonder if you know about asking questions on StackOverflow:
Check This out http://www.stackoverflow.com/questions/1925455/how-to-mimic-stackoverflow-auto-link-behavior

Also, base_convert php function?
http://pt.php.net/manual/en/function.base-convert.php#52450

http://pt.php.net/manual/en/function.base-convert.php?wtf=hehe#52450

Output Text:

This is my text.  I wonder if you know about asking questions on StackOverflow:
Check This out <a rel="nofollow" href="http://www.stackoverflow.com/questions/1925455/how-to-mimic-stackoverflow-auto-link-behavior">stackoverflow.com/questions/1925455/…</a>

Also, base_convert php function?
<a rel="nofollow" href="http://pt.php.net/manual/en/function.base-convert.php#52450">pt.php.net/manual/en/…</a>

<a rel="nofollow" href="http://pt.php.net/manual/en/function.base-convert.php?wtf=hehe#52450">pt.php.net/manual/en/…</a>


Related Topics



Leave a reply



Submit