问:
Cost of calling str() on a string?
What is the cost (if any) of calling the str function on an object that is already a string? The use case here is to normalize an array of objects of different types and convert them into string, naively it can be implemented like so:

```<code>def arr_2_strarr(arr):
    return [str(val) for val in arr]
</code>```

But if the str() causes too much overhead, and my arr contains primarily strings, I may consider using:

```<code>def arr_2_strarr2(arr):
    return [str(val) if not isinstance(val, basestring) else val for val in arr]
</code>```

Any suggestions?

答:
Calling str on a string object is pretty cheap: it just returns the original string object. Calling isinstance explicitly will definitely be slower.

If you want to test this on real data, take a look at the timeit module.

BTW, you should eliminate the not from your 2nd version

```<code>[val if isinstance(val, basestring) else str(val) for val in arr]
</code>```

And you can speed things up slightly by caching str:

```<code>def arr_2_strarr(arr, str=str):
    return [str(val) for val in arr]
</code>```

Happy micro-optimizing. :)

Why cache str? Well, each time you use a name, Python has to look for it. If you're inside a function, first it looks in the local namespace, and if it can't find the name then it looks in the globals. Even though str is built-in, it still "lives" in the global namespace; it would be inefficient to "import" all the built-ins into every function. By doing

```<code>def arr_2_strarr(arr, str=str)
</code>```

we create a local name str that gets bound to the built-in str type, and because it's a default argument that search & bind process happens once, when the function definition is executed, not each time the function is called.

So each time we call arr_2_strarr the interpreter will immediately find that local str, which will save a tiny amount of time.

Here's some timeit code that compares the various strategies. It runs on both Python 2 & Python 3, although on Python 3 it substitutes str for basestr, since basestr doesn't exist in Python 3.

This code runs the functions on lists of various sizes first with integer data, then with string data which is created by converting the integer data to strings.

Each line of output gives the time to perform the given number of loops over 3 repetitions, sorted from fastest to slowest. As the timeit repeat docs mention, the main number to look at in each run is the smallest one.

The results for all functions on a given list size and type are also sorted from fastest to slowest.

```<code>''' Compare the speeds of direct string conversion
    with testing first via isinstance

    See https://stackoverflow.com/q/44439323/4014959

    Written by PM 2Ring 2017.06.09

    Python 2 / 3 compatible
'''

from __future__ import print_function, division
from timeit import Timer
import sys

# Python 3 doesn't have basestring
if sys.version_info[0] > 2:
    basestring = str

# The functions to test
def plain(arr):
    return [str(val) for val in arr]

def cached(arr, str=str):
    return [str(val) for val in arr]

def teststr(arr):
    return [val if isinstance(val, str) else str(val) for val in arr]

def testbase(arr):
    return [val if isinstance(val, basestring) else str(val) for val in arr]

def testbasenot(arr):
    return [str(val) if not isinstance(val, basestring) else val for val in arr]

funcs = (
    plain,
    cached,
    teststr,
    testbase,
    testbasenot,
)

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

def verify(arr):
    results = [func(arr) for func in funcs]
    first, results = results[0], results[1:]
    return all(first == u for u in results)

def time_test(loops, reps):
    ''' Print timing stats for all the functions '''
    timings = []
    for func in funcs:
        fname = func.__name__
        setup = 'from __main__ import arr, ' + fname
        cmd = fname + '(arr)'
        t = Timer(cmd, setup)
        result = t.repeat(reps, loops)
        result.sort()
        timings.append((result, fname))

    timings.sort()
    for result, fname in timings:
        print('{0:12} {1}'.format(fname, result))

# Check that all functions return the same results
if 0:
    print('Testing all functions')
    arr = list(range(10))
    print(arr, verify(arr))
    arr = list('abcdefghij')
    print(arr, verify(arr))

# Do the timing tests
reps = 3
loops = 1 << 16
for i in range(1, 11):
    n = 1 << i
    # Build a data array of integers
    arr = range(n)
    print('\n{0}: Size={1}, Loops={2}'.format(i, n, loops))
    print('* Integer')
    time_test(loops, reps)

    # Convert the data array contents to strings
    arr = cached(arr)
    print('\n* String')
    time_test(loops, reps)
    loops >>= 1
</code>```

typical Python 2 output

```<code>1: Size=2, Loops=65536
* Integer
cached       [0.17268610000610352, 0.19634914398193359, 0.2058720588684082]
plain        [0.17906594276428223, 0.18797492980957031, 0.24009895324707031]
teststr      [0.32513308525085449, 0.33270597457885742, 0.35080599784851074]
testbasenot  [0.32793092727661133, 0.33176803588867188, 0.33498501777648926]
testbase     [0.32964491844177246, 0.33154511451721191, 0.33760714530944824]

* String
cached       [0.1619560718536377, 0.1628870964050293, 0.16448402404785156]
teststr      [0.16335082054138184, 0.16484308242797852, 0.17012500762939453]
plain        [0.16956901550292969, 0.1711430549621582, 0.18457293510437012]
testbase     [0.22378706932067871, 0.2255101203918457, 0.22593879699707031]
testbasenot  [0.22855901718139648, 0.22941207885742188, 0.23271608352661133]

2: Size=4, Loops=32768
* Integer
cached       [0.12796807289123535, 0.12807202339172363, 0.12817001342773438]
plain        [0.13622713088989258, 0.14297294616699219, 0.14868402481079102]
teststr      [0.27701020240783691, 0.27812099456787109, 0.2795259952545166]
testbasenot  [0.27815794944763184, 0.28220701217651367, 0.29373884201049805]
testbase     [0.2804868221282959, 0.28186416625976562, 0.31699705123901367]

* String
cached       [0.12131500244140625, 0.12241697311401367, 0.13379192352294922]
teststr      [0.12839889526367188, 0.1314079761505127, 0.14053797721862793]
plain        [0.13051795959472656, 0.14696002006530762, 0.18504786491394043]
testbase     [0.18404412269592285, 0.1844489574432373, 0.19633579254150391]
testbasenot  [0.18416285514831543, 0.18494606018066406, 0.18553614616394043]

3: Size=8, Loops=16384
* Integer
cached       [0.10957002639770508, 0.11252093315124512, 0.11768913269042969]
plain        [0.11848998069763184, 0.11958003044128418, 0.1292269229888916]
testbase     [0.26231694221496582, 0.26471304893493652, 0.26625895500183105]
teststr      [0.26410102844238281, 0.2641758918762207, 0.26569199562072754]
testbasenot  [0.26910495758056641, 0.26967120170593262, 0.2741539478302002]

* String
cached       [0.102294921875, 0.10357999801635742, 0.1050269603729248]
teststr      [0.10852217674255371, 0.10861611366271973, 0.1127161979675293]
plain        [0.11173510551452637, 0.11183404922485352, 0.12115597724914551]
testbasenot  [0.16488981246948242, 0.16509699821472168, 0.16648602485656738]
testbase     [0.16622614860534668, 0.16688108444213867, 0.16962814331054688]

4: Size=16, Loops=8192
* Integer
cached       [0.10548806190490723, 0.10568594932556152, 0.10611891746520996]
plain        [0.11526799201965332, 0.1160120964050293, 0.12486004829406738]
teststr      [0.25309896469116211, 0.25549888610839844, 0.25838899612426758]
testbasenot  [0.25410699844360352, 0.27252411842346191, 0.32510590553283691]
testbase     [0.25414609909057617, 0.26968812942504883, 0.27393984794616699]

* String
cached       [0.092885017395019531, 0.096045970916748047, 0.10643196105957031]
teststr      [0.098433017730712891, 0.098783016204833984, 0.10051798820495605]
plain        [0.10081005096435547, 0.10222005844116211, 0.12018895149230957]
testbasenot  [0.15373396873474121, 0.15472292900085449, 0.15676999092102051]
testbase     [0.15490198135375977, 0.15572404861450195, 0.15599799156188965]

5: Size=32, Loops=4096
* Integer
cached       [0.10568094253540039, 0.10743498802185059, 0.1115870475769043]
plain        [0.1163330078125, 0.11633419990539551, 0.12796401977539062]
teststr      [0.25122308731079102, 0.26527810096740723, 0.26579189300537109]
testbase     [0.25309586524963379, 0.25563716888427734, 0.25917816162109375]
testbasenot  [0.25465011596679688, 0.25907588005065918, 0.26110982894897461]

* String
cached       [0.085406064987182617, 0.086378097534179688, 0.08651280403137207]
teststr      [0.092473983764648438, 0.09324193000793457, 0.093439817428588867]
plain        [0.096549034118652344, 0.097501993179321289, 0.10462403297424316]
testbase     [0.14794015884399414, 0.14966106414794922, 0.15016818046569824]
testbasenot  [0.14796280860900879, 0.14940309524536133, 0.15308189392089844]

6: Size=64, Loops=2048
* Integer
cached       [0.10838603973388672, 0.1089630126953125, 0.11129999160766602]
plain        [0.11764693260192871, 0.11851096153259277, 0.12583494186401367]
teststr      [0.2550208568572998, 0.25540995597839355, 0.26316595077514648]
testbase     [0.25723910331726074, 0.25930881500244141, 0.26207089424133301]
testbasenot  [0.25864100456237793, 0.25901007652282715, 0.26875495910644531]

* String
cached       [0.086635112762451172, 0.087384939193725586, 0.099885940551757812]
plain        [0.096493959426879883, 0.12469196319580078, 0.13684391975402832]
teststr      [0.096681118011474609, 0.098448991775512695, 0.10569310188293457]
testbase     [0.14573216438293457, 0.14696693420410156, 0.14700508117675781]
testbasenot  [0.14776277542114258, 0.14852094650268555, 0.15462112426757812]

7: Size=128, Loops=1024
* Integer
cached       [0.10915207862854004, 0.11011981964111328, 0.1127631664276123]
plain        [0.11721491813659668, 0.11830401420593262, 0.1254270076751709]
testbase     [0.25789499282836914, 0.26130795478820801, 0.26179313659667969]
teststr      [0.25840306282043457, 0.25889492034912109, 0.26300287246704102]
testbasenot  [0.26443600654602051, 0.26498103141784668, 0.26691412925720215]

* String
cached       [0.083537101745605469, 0.084954023361206055, 0.086431980133056641]
teststr      [0.091158866882324219, 0.09123992919921875, 0.091590166091918945]
plain        [0.091225862503051758, 0.092115163803100586, 0.099261045455932617]
testbase     [0.14569401741027832, 0.14622306823730469, 0.14650607109069824]
testbasenot  [0.14774990081787109, 0.14930200576782227, 0.15020990371704102]

8: Size=256, Loops=512
* Integer
cached       [0.10824894905090332, 0.10865211486816406, 0.10895800590515137]
plain        [0.11750102043151855, 0.12690877914428711, 0.12890195846557617]
teststr      [0.25457501411437988, 0.25542402267456055, 0.25692200660705566]
testbasenot  [0.25513482093811035, 0.25664496421813965, 0.25999689102172852]
testbase     [0.25680398941040039, 0.25924396514892578, 0.26179695129394531]

* String
cached       [0.080662012100219727, 0.081827878952026367, 0.081900119781494141]
teststr      [0.089673995971679688, 0.097939014434814453, 0.15471792221069336]
plain        [0.094327926635742188, 0.095342159271240234, 0.097375154495239258]
testbasenot  [0.14262199401855469, 0.14278602600097656, 0.14302182197570801]
testbase     [0.14464497566223145, 0.14674210548400879, 0.16207790374755859]

9: Size=512, Loops=256
* Integer
cached       [0.10789299011230469, 0.1092069149017334, 0.110015869140625]
plain        [0.11702799797058105, 0.1181950569152832, 0.12698101997375488]
testbase     [0.25504207611083984, 0.25520896911621094, 0.25734806060791016]
testbasenot  [0.25715017318725586, 0.25747489929199219, 0.25850796699523926]
teststr      [0.25783085823059082, 0.25882315635681152, 0.26154208183288574]

* String
cached       [0.078849077224731445, 0.079813003540039062, 0.084489107131958008]
teststr      [0.086745977401733398, 0.087059974670410156, 0.087485074996948242]
plain        [0.088322877883911133, 0.088804960250854492, 0.097378969192504883]
testbasenot  [0.14128994941711426, 0.14266705513000488, 0.1427910327911377]
testbase     [0.14152097702026367, 0.14231991767883301, 0.14392399787902832]

10: Size=1024, Loops=128
* Integer
cached       [0.10892415046691895, 0.11003899574279785, 0.11008000373840332]
plain        [0.1192779541015625, 0.12048506736755371, 0.12956619262695312]
teststr      [0.25335502624511719, 0.25642204284667969, 0.25892996788024902]
testbase     [0.25525593757629395, 0.25550699234008789, 0.25794696807861328]
testbasenot  [0.25932693481445312, 0.25960803031921387, 0.26134610176086426]

* String
cached       [0.078451156616210938, 0.080369949340820312, 0.080511093139648438]
teststr      [0.084844112396240234, 0.085949897766113281, 0.096578836441040039]
plain        [0.086302042007446289, 0.087638139724731445, 0.096364974975585938]
testbase     [0.14068913459777832, 0.14274501800537109, 0.15559101104736328]
testbasenot  [0.14075493812561035, 0.15553092956542969, 0.19578790664672852]
</code>```

typical python3 output

```<code>1: Size=2, Loops=65536
* Integer
plain        [0.2957206170030986, 0.2959696320031071, 0.2991539639988332]
cached       [0.3058611470005417, 0.30598287599787, 0.3073535650000849]
testbase     [0.38803433800057974, 0.39307209699836676, 0.393392562000372]
testbasenot  [0.3888578799997049, 0.3951267439988442, 0.42909636100011994]
teststr      [0.41290506400036975, 0.41541150199918775, 0.4488242949992127]

* String
testbase     [0.23906823500146857, 0.23946705200069118, 0.24624350399972172]
testbasenot  [0.24037985899849446, 0.24200722000023234, 0.2462738950016501]
plain        [0.25742501500280923, 0.2644229819998145, 0.26711930600140477]
teststr      [0.2635171010006161, 0.3559218000009423, 0.3784064870014845]
cached       [0.2687887559986848, 0.2711959320004098, 0.38138879500183975]

2: Size=4, Loops=32768
* Integer
cached       [0.21332427200104576, 0.21363574399947538, 0.21528891600246425]
plain        [0.22395663199858973, 0.22762144099760917, 0.23422862100051134]
testbasenot  [0.31939790100295795, 0.32413787499899627, 0.32422161499926005]
testbase     [0.3209382370005187, 0.3213516770010756, 0.3215230670029996]
teststr      [0.3372085839982901, 0.33786465500088525, 0.33847540900023887]

* String
testbasenot  [0.17031173299983493, 0.17143720199965173, 0.17724975699820789]
testbase     [0.170390128998406, 0.17118954800025676, 0.18865150499914307]
cached       [0.18190538799899514, 0.18262020299880533, 0.183105569001782]
plain        [0.18666503399799694, 0.18781541300268145, 0.1955128590016102]
teststr      [0.18973677000030875, 0.19112570400102413, 0.19168143299975782]

3: Size=8, Loops=16384
* Integer
cached       [0.17012267099926248, 0.18160372200145503, 0.2275817529989581]
plain        [0.1890079689983395, 0.1963043950017891, 0.2016476179996971]
testbasenot  [0.28168991999700665, 0.2821743839995179, 0.286649605997809]
testbase     [0.28295213199817226, 0.28760008400058723, 0.2906435440017958]
teststr      [0.2958552290001535, 0.2989299110013235, 0.31747390199961956]

* String
testbase     [0.13354753000021446, 0.13377505199969164, 0.14039257600234123]
cached       [0.1352838150014577, 0.1353432000032626, 0.13798289999976987]
testbasenot  [0.14252334699995117, 0.14301740500013693, 0.1445914210016781]
plain        [0.15130633899752866, 0.15166569000211894, 0.1616801599993778]
teststr      [0.15267008800219628, 0.1545946529986395, 0.15590016200076207]

4: Size=16, Loops=8192
* Integer
cached       [0.144755126999371, 0.14782401300180936, 0.1484048439997423]
plain        [0.1726092749995587, 0.1740606339990336, 0.1815100200001325]
testbase     [0.26685525399807375, 0.27029573199979495, 0.2716258750006091]
testbasenot  [0.2702714350016322, 0.2723204169997189, 0.27288546099953237]
teststr      [0.28515160999813816, 0.28523068700087606, 0.2878553769987775]

* String
cached       [0.11515368599793874, 0.11579233700103941, 0.11688366999806021]
testbase     [0.12178990400207113, 0.13090817400006927, 0.13304468899877975]
testbasenot  [0.13121789299839293, 0.14976675499929115, 0.1521548589989834]
teststr      [0.13410512400150765, 0.1354981399999815, 0.147247362001508]
plain        [0.13691626099898713, 0.1384456069972657, 0.1426525679999031]

5: Size=32, Loops=4096
* Integer
cached       [0.13246865899782279, 0.13320018100057496, 0.134628559997509]
plain        [0.1636957459995756, 0.16763203899972723, 0.1752369269997871]
testbase     [0.26010187700012466, 0.2606812570011243, 0.2647345440018398]
testbasenot  [0.2620696090016281, 0.26230394700178294, 0.26258907899682526]
teststr      [0.27685887300322065, 0.2787095199964824, 0.28293989099984174]

* String
cached       [0.10246079200078384, 0.10416977099885116, 0.10755630499988911]
testbasenot  [0.10829716499938513, 0.10918466699877172, 0.10935586699997657]
testbase     [0.11739019699962228, 0.11808202800239087, 0.11899654000080773]
plain        [0.12601002500014147, 0.12718953500007046, 0.13454839599944535]
teststr      [0.13366336599938222, 0.13407608800116577, 0.13510101700012456]

6: Size=64, Loops=2048
* Integer
cached       [0.12591946799875586, 0.127094235002005, 0.13223557899982552]
plain        [0.160616523000499, 0.16232994500023779, 0.1691623620026803]
testbase     [0.2534341589998803, 0.2556092949998856, 0.2571690379991196]
testbasenot  [0.2560774869998568, 0.2574564010028553, 0.2606996459981019]
teststr      [0.268248238000524, 0.2702014210008201, 0.27107579600124154]

* String
cached       [0.09791737100022146, 0.09819723300097394, 0.10752435399990645]
testbasenot  [0.1057888709983672, 0.10588572099732119, 0.16173565400094958]
testbase     [0.10636284599968349, 0.1179599219976808, 0.12130766799964476]
plain        [0.12285572399923694, 0.12589510299949325, 0.13114397300159908]
teststr      [0.13122114399811835, 0.13273253399893292, 0.14575592999972287]

7: Size=128, Loops=1024
* Integer
cached       [0.12404713899741182, 0.12496110600113752, 0.12496385000122245]
plain        [0.15980284800025402, 0.16046370399999432, 0.16711239899814245]
testbasenot  [0.25531527800194453, 0.25563639699976193, 0.2586420219995489]
testbase     [0.25544935799916857, 0.2558138679996773, 0.257172014000389]
teststr      [0.2699256220003008, 0.2712909309993847, 0.27702098800000385]

* String
cached       [0.09376715399776003, 0.09393715400074143, 0.09975314399707713]
testbasenot  [0.10510071799944853, 0.10511873200084665, 0.10523289399861824]
testbase     [0.11240010600158712, 0.11325187799957348, 0.11632439300228725]
plain        [0.12139380200096639, 0.12202585699924384, 0.1315958569975919]
teststr      [0.12834531499902369, 0.12949470400053542, 0.12955383699954837]

8: Size=256, Loops=512
* Integer
cached       [0.12225364700134378, 0.12283446399669629, 0.1285843859986926]
plain        [0.15971405900199898, 0.16198832800000673, 0.16777605400056927]
testbase     [0.2507534860014857, 0.2527904779999517, 0.25378678199922433]
testbasenot  [0.25323686200135853, 0.2547167230004561, 0.25919888999851537]
teststr      [0.2652072370001406, 0.2658402630004275, 0.2674206650008273]

* String
cached       [0.0906629850032914, 0.0985801380011253, 0.09929232800277532]
testbase     [0.10155730300175492, 0.1042869699995208, 0.11276149599871133]
testbasenot  [0.10197166099897004, 0.11451221999959671, 0.15595895300066331]
plain        [0.11898361400017166, 0.12018223199993372, 0.12760113599870238]
teststr      [0.12645652200080804, 0.12671815700014122, 0.14095144699967932]

9: Size=512, Loops=256
* Integer
cached       [0.12672984500022721, 0.1462409830019169, 0.2653043659993273]
plain        [0.161721200998727, 0.17296033000093303, 0.19699998799842433]
testbase     [0.25432757399903494, 0.25851125400004094, 0.258548003002943]
testbasenot  [0.25619441399976495, 0.25656893900304567, 0.25998359599907417]
teststr      [0.2719232039999042, 0.2744571339972026, 0.2751794379983039]

* String
cached       [0.08841608199873008, 0.08848714099804056, 0.09124958899701596]
testbasenot  [0.09962382599769626, 0.10016373899998143, 0.10028601600060938]
testbase     [0.10713129000214394, 0.10752918499929365, 0.10952026399900205]
plain        [0.1163020489984774, 0.12190789400119684, 0.1264930679972167]
teststr      [0.1242994140011433, 0.12458201900153654, 0.12523995000083232]

10: Size=1024, Loops=128
* Integer
cached       [0.12827690600170172, 0.1294701549995807, 0.13387694999983069]
plain        [0.16636216699771467, 0.16866590399877168, 0.17549873600000865]
testbasenot  [0.25435296399882645, 0.25515673799964134, 0.2605281959986314]
testbase     [0.26351416900070035, 0.26398584699927596, 0.2651360300005763]
teststr      [0.26816077799958293, 0.26908816800278146, 0.2715630999991845]

* String
cached       [0.08827024300262565, 0.09090095799911069, 0.09729095900183893]
testbase     [0.10063145499952952, 0.1010660120009561, 0.10904535399822635]
testbasenot  [0.10313185999984853, 0.11444468399713514, 0.14796407999892836]
plain        [0.11569941500056302, 0.11579339799936861, 0.12615105800068704]
teststr      [0.12353994099976262, 0.12515813500067452, 0.13752399999793852]
</code>```

These timings were performed on a rather old 32 bit single core 2GHz machine with 2GB of RAM running on a Debian derivative of Linux. I used Python 2.6.6 and Python 3.6.0. Your results may vary. ;) In any case, these results should only be used as a rough guide. timeit does a pretty good job of only timing the stuff we want to time, but it has no control over other processes that also want to use the CPU.

Title: Cost of calling str() on a string?

Body:
What is the cost (if any) of calling the str function on an object that is already a string? The use case here is to normalize an array of objects of different types and convert them into string, naively it can be implemented like so:

def arr_2_strarr(arr):
    return [str(val) for val in arr]

But if the str() causes too much overhead, and my arr contains primarily strings, I may consider using:

def arr_2_strarr2(arr):
    return [str(val) if not isinstance(val, basestring) else val for val in arr]

Any suggestions?

Body:
Calling str on a string object is pretty cheap: it just returns the original string object. Calling isinstance explicitly will definitely be slower.

If you want to test this on real data, take a look at the timeit module.

BTW, you should eliminate the not from your 2nd version

[val if isinstance(val, basestring) else str(val) for val in arr]

And you can speed things up slightly by caching str:

def arr_2_strarr(arr, str=str):
    return [str(val) for val in arr]

Happy micro-optimizing. :)

Why cache str? Well, each time you use a name, Python has to look for it. If you're inside a function, first it looks in the local namespace, and if it can't find the name then it looks in the globals. Even though str is built-in, it still "lives" in the global namespace; it would be inefficient to "import" all the built-ins into every function. By doing

def arr_2_strarr(arr, str=str)

we create a local name str that gets bound to the built-in str type, and because it's a default argument that search & bind process happens once, when the function definition is executed, not each time the function is called.

So each time we call arr_2_strarr the interpreter will immediately find that local str, which will save a tiny amount of time.

Here's some timeit code that compares the various strategies. It runs on both Python 2 & Python 3, although on Python 3 it substitutes str for basestr, since basestr doesn't exist in Python 3.

This code runs the functions on lists of various sizes first with integer data, then with string data which is created by converting the integer data to strings.

Each line of output gives the time to perform the given number of loops over 3 repetitions, sorted from fastest to slowest. As the timeit repeat docs mention, the main number to look at in each run is the smallest one.

The results for all functions on a given list size and type are also sorted from fastest to slowest.

''' Compare the speeds of direct string conversion
    with testing first via isinstance

    See https://stackoverflow.com/q/44439323/4014959

    Written by PM 2Ring 2017.06.09

    Python 2 / 3 compatible
'''

from __future__ import print_function, division
from timeit import Timer
import sys

# Python 3 doesn't have basestring
if sys.version_info[0] > 2:
    basestring = str

# The functions to test
def plain(arr):
    return [str(val) for val in arr]

def cached(arr, str=str):
    return [str(val) for val in arr]

def teststr(arr):
    return [val if isinstance(val, str) else str(val) for val in arr]

def testbase(arr):
    return [val if isinstance(val, basestring) else str(val) for val in arr]

def testbasenot(arr):
    return [str(val) if not isinstance(val, basestring) else val for val in arr]

funcs = (
    plain,
    cached,
    teststr,
    testbase,
    testbasenot,
)

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

def verify(arr):
    results = [func(arr) for func in funcs]
    first, results = results[0], results[1:]
    return all(first == u for u in results)

def time_test(loops, reps):
    ''' Print timing stats for all the functions '''
    timings = []
    for func in funcs:
        fname = func.__name__
        setup = 'from __main__ import arr, ' + fname
        cmd = fname + '(arr)'
        t = Timer(cmd, setup)
        result = t.repeat(reps, loops)
        result.sort()
        timings.append((result, fname))

    timings.sort()
    for result, fname in timings:
        print('{0:12} {1}'.format(fname, result))

# Check that all functions return the same results
if 0:
    print('Testing all functions')
    arr = list(range(10))
    print(arr, verify(arr))
    arr = list('abcdefghij')
    print(arr, verify(arr))

# Do the timing tests
reps = 3
loops = 1 << 16
for i in range(1, 11):
    n = 1 << i
    # Build a data array of integers
    arr = range(n)
    print('\n{0}: Size={1}, Loops={2}'.format(i, n, loops))
    print('* Integer')
    time_test(loops, reps)

    # Convert the data array contents to strings
    arr = cached(arr)
    print('\n* String')
    time_test(loops, reps)
    loops >>= 1

typical Python 2 output

1: Size=2, Loops=65536
* Integer
cached       [0.17268610000610352, 0.19634914398193359, 0.2058720588684082]
plain        [0.17906594276428223, 0.18797492980957031, 0.24009895324707031]
teststr      [0.32513308525085449, 0.33270597457885742, 0.35080599784851074]
testbasenot  [0.32793092727661133, 0.33176803588867188, 0.33498501777648926]
testbase     [0.32964491844177246, 0.33154511451721191, 0.33760714530944824]

* String
cached       [0.1619560718536377, 0.1628870964050293, 0.16448402404785156]
teststr      [0.16335082054138184, 0.16484308242797852, 0.17012500762939453]
plain        [0.16956901550292969, 0.1711430549621582, 0.18457293510437012]
testbase     [0.22378706932067871, 0.2255101203918457, 0.22593879699707031]
testbasenot  [0.22855901718139648, 0.22941207885742188, 0.23271608352661133]

2: Size=4, Loops=32768
* Integer
cached       [0.12796807289123535, 0.12807202339172363, 0.12817001342773438]
plain        [0.13622713088989258, 0.14297294616699219, 0.14868402481079102]
teststr      [0.27701020240783691, 0.27812099456787109, 0.2795259952545166]
testbasenot  [0.27815794944763184, 0.28220701217651367, 0.29373884201049805]
testbase     [0.2804868221282959, 0.28186416625976562, 0.31699705123901367]

* String
cached       [0.12131500244140625, 0.12241697311401367, 0.13379192352294922]
teststr      [0.12839889526367188, 0.1314079761505127, 0.14053797721862793]
plain        [0.13051795959472656, 0.14696002006530762, 0.18504786491394043]
testbase     [0.18404412269592285, 0.1844489574432373, 0.19633579254150391]
testbasenot  [0.18416285514831543, 0.18494606018066406, 0.18553614616394043]

3: Size=8, Loops=16384
* Integer
cached       [0.10957002639770508, 0.11252093315124512, 0.11768913269042969]
plain        [0.11848998069763184, 0.11958003044128418, 0.1292269229888916]
testbase     [0.26231694221496582, 0.26471304893493652, 0.26625895500183105]
teststr      [0.26410102844238281, 0.2641758918762207, 0.26569199562072754]
testbasenot  [0.26910495758056641, 0.26967120170593262, 0.2741539478302002]

* String
cached       [0.102294921875, 0.10357999801635742, 0.1050269603729248]
teststr      [0.10852217674255371, 0.10861611366271973, 0.1127161979675293]
plain        [0.11173510551452637, 0.11183404922485352, 0.12115597724914551]
testbasenot  [0.16488981246948242, 0.16509699821472168, 0.16648602485656738]
testbase     [0.16622614860534668, 0.16688108444213867, 0.16962814331054688]

4: Size=16, Loops=8192
* Integer
cached       [0.10548806190490723, 0.10568594932556152, 0.10611891746520996]
plain        [0.11526799201965332, 0.1160120964050293, 0.12486004829406738]
teststr      [0.25309896469116211, 0.25549888610839844, 0.25838899612426758]
testbasenot  [0.25410699844360352, 0.27252411842346191, 0.32510590553283691]
testbase     [0.25414609909057617, 0.26968812942504883, 0.27393984794616699]

* String
cached       [0.092885017395019531, 0.096045970916748047, 0.10643196105957031]
teststr      [0.098433017730712891, 0.098783016204833984, 0.10051798820495605]
plain        [0.10081005096435547, 0.10222005844116211, 0.12018895149230957]
testbasenot  [0.15373396873474121, 0.15472292900085449, 0.15676999092102051]
testbase     [0.15490198135375977, 0.15572404861450195, 0.15599799156188965]

5: Size=32, Loops=4096
* Integer
cached       [0.10568094253540039, 0.10743498802185059, 0.1115870475769043]
plain        [0.1163330078125, 0.11633419990539551, 0.12796401977539062]
teststr      [0.25122308731079102, 0.26527810096740723, 0.26579189300537109]
testbase     [0.25309586524963379, 0.25563716888427734, 0.25917816162109375]
testbasenot  [0.25465011596679688, 0.25907588005065918, 0.26110982894897461]

* String
cached       [0.085406064987182617, 0.086378097534179688, 0.08651280403137207]
teststr      [0.092473983764648438, 0.09324193000793457, 0.093439817428588867]
plain        [0.096549034118652344, 0.097501993179321289, 0.10462403297424316]
testbase     [0.14794015884399414, 0.14966106414794922, 0.15016818046569824]
testbasenot  [0.14796280860900879, 0.14940309524536133, 0.15308189392089844]

6: Size=64, Loops=2048
* Integer
cached       [0.10838603973388672, 0.1089630126953125, 0.11129999160766602]
plain        [0.11764693260192871, 0.11851096153259277, 0.12583494186401367]
teststr      [0.2550208568572998, 0.25540995597839355, 0.26316595077514648]
testbase     [0.25723910331726074, 0.25930881500244141, 0.26207089424133301]
testbasenot  [0.25864100456237793, 0.25901007652282715, 0.26875495910644531]

* String
cached       [0.086635112762451172, 0.087384939193725586, 0.099885940551757812]
plain        [0.096493959426879883, 0.12469196319580078, 0.13684391975402832]
teststr      [0.096681118011474609, 0.098448991775512695, 0.10569310188293457]
testbase     [0.14573216438293457, 0.14696693420410156, 0.14700508117675781]
testbasenot  [0.14776277542114258, 0.14852094650268555, 0.15462112426757812]

7: Size=128, Loops=1024
* Integer
cached       [0.10915207862854004, 0.11011981964111328, 0.1127631664276123]
plain        [0.11721491813659668, 0.11830401420593262, 0.1254270076751709]
testbase     [0.25789499282836914, 0.26130795478820801, 0.26179313659667969]
teststr      [0.25840306282043457, 0.25889492034912109, 0.26300287246704102]
testbasenot  [0.26443600654602051, 0.26498103141784668, 0.26691412925720215]

* String
cached       [0.083537101745605469, 0.084954023361206055, 0.086431980133056641]
teststr      [0.091158866882324219, 0.09123992919921875, 0.091590166091918945]
plain        [0.091225862503051758, 0.092115163803100586, 0.099261045455932617]
testbase     [0.14569401741027832, 0.14622306823730469, 0.14650607109069824]
testbasenot  [0.14774990081787109, 0.14930200576782227, 0.15020990371704102]

8: Size=256, Loops=512
* Integer
cached       [0.10824894905090332, 0.10865211486816406, 0.10895800590515137]
plain        [0.11750102043151855, 0.12690877914428711, 0.12890195846557617]
teststr      [0.25457501411437988, 0.25542402267456055, 0.25692200660705566]
testbasenot  [0.25513482093811035, 0.25664496421813965, 0.25999689102172852]
testbase     [0.25680398941040039, 0.25924396514892578, 0.26179695129394531]

* String
cached       [0.080662012100219727, 0.081827878952026367, 0.081900119781494141]
teststr      [0.089673995971679688, 0.097939014434814453, 0.15471792221069336]
plain        [0.094327926635742188, 0.095342159271240234, 0.097375154495239258]
testbasenot  [0.14262199401855469, 0.14278602600097656, 0.14302182197570801]
testbase     [0.14464497566223145, 0.14674210548400879, 0.16207790374755859]

9: Size=512, Loops=256
* Integer
cached       [0.10789299011230469, 0.1092069149017334, 0.110015869140625]
plain        [0.11702799797058105, 0.1181950569152832, 0.12698101997375488]
testbase     [0.25504207611083984, 0.25520896911621094, 0.25734806060791016]
testbasenot  [0.25715017318725586, 0.25747489929199219, 0.25850796699523926]
teststr      [0.25783085823059082, 0.25882315635681152, 0.26154208183288574]

* String
cached       [0.078849077224731445, 0.079813003540039062, 0.084489107131958008]
teststr      [0.086745977401733398, 0.087059974670410156, 0.087485074996948242]
plain        [0.088322877883911133, 0.088804960250854492, 0.097378969192504883]
testbasenot  [0.14128994941711426, 0.14266705513000488, 0.1427910327911377]
testbase     [0.14152097702026367, 0.14231991767883301, 0.14392399787902832]

10: Size=1024, Loops=128
* Integer
cached       [0.10892415046691895, 0.11003899574279785, 0.11008000373840332]
plain        [0.1192779541015625, 0.12048506736755371, 0.12956619262695312]
teststr      [0.25335502624511719, 0.25642204284667969, 0.25892996788024902]
testbase     [0.25525593757629395, 0.25550699234008789, 0.25794696807861328]
testbasenot  [0.25932693481445312, 0.25960803031921387, 0.26134610176086426]

* String
cached       [0.078451156616210938, 0.080369949340820312, 0.080511093139648438]
teststr      [0.084844112396240234, 0.085949897766113281, 0.096578836441040039]
plain        [0.086302042007446289, 0.087638139724731445, 0.096364974975585938]
testbase     [0.14068913459777832, 0.14274501800537109, 0.15559101104736328]
testbasenot  [0.14075493812561035, 0.15553092956542969, 0.19578790664672852]

typical python3 output

1: Size=2, Loops=65536
* Integer
plain        [0.2957206170030986, 0.2959696320031071, 0.2991539639988332]
cached       [0.3058611470005417, 0.30598287599787, 0.3073535650000849]
testbase     [0.38803433800057974, 0.39307209699836676, 0.393392562000372]
testbasenot  [0.3888578799997049, 0.3951267439988442, 0.42909636100011994]
teststr      [0.41290506400036975, 0.41541150199918775, 0.4488242949992127]

* String
testbase     [0.23906823500146857, 0.23946705200069118, 0.24624350399972172]
testbasenot  [0.24037985899849446, 0.24200722000023234, 0.2462738950016501]
plain        [0.25742501500280923, 0.2644229819998145, 0.26711930600140477]
teststr      [0.2635171010006161, 0.3559218000009423, 0.3784064870014845]
cached       [0.2687887559986848, 0.2711959320004098, 0.38138879500183975]

2: Size=4, Loops=32768
* Integer
cached       [0.21332427200104576, 0.21363574399947538, 0.21528891600246425]
plain        [0.22395663199858973, 0.22762144099760917, 0.23422862100051134]
testbasenot  [0.31939790100295795, 0.32413787499899627, 0.32422161499926005]
testbase     [0.3209382370005187, 0.3213516770010756, 0.3215230670029996]
teststr      [0.3372085839982901, 0.33786465500088525, 0.33847540900023887]

* String
testbasenot  [0.17031173299983493, 0.17143720199965173, 0.17724975699820789]
testbase     [0.170390128998406, 0.17118954800025676, 0.18865150499914307]
cached       [0.18190538799899514, 0.18262020299880533, 0.183105569001782]
plain        [0.18666503399799694, 0.18781541300268145, 0.1955128590016102]
teststr      [0.18973677000030875, 0.19112570400102413, 0.19168143299975782]

3: Size=8, Loops=16384
* Integer
cached       [0.17012267099926248, 0.18160372200145503, 0.2275817529989581]
plain        [0.1890079689983395, 0.1963043950017891, 0.2016476179996971]
testbasenot  [0.28168991999700665, 0.2821743839995179, 0.286649605997809]
testbase     [0.28295213199817226, 0.28760008400058723, 0.2906435440017958]
teststr      [0.2958552290001535, 0.2989299110013235, 0.31747390199961956]

* String
testbase     [0.13354753000021446, 0.13377505199969164, 0.14039257600234123]
cached       [0.1352838150014577, 0.1353432000032626, 0.13798289999976987]
testbasenot  [0.14252334699995117, 0.14301740500013693, 0.1445914210016781]
plain        [0.15130633899752866, 0.15166569000211894, 0.1616801599993778]
teststr      [0.15267008800219628, 0.1545946529986395, 0.15590016200076207]

4: Size=16, Loops=8192
* Integer
cached       [0.144755126999371, 0.14782401300180936, 0.1484048439997423]
plain        [0.1726092749995587, 0.1740606339990336, 0.1815100200001325]
testbase     [0.26685525399807375, 0.27029573199979495, 0.2716258750006091]
testbasenot  [0.2702714350016322, 0.2723204169997189, 0.27288546099953237]
teststr      [0.28515160999813816, 0.28523068700087606, 0.2878553769987775]

* String
cached       [0.11515368599793874, 0.11579233700103941, 0.11688366999806021]
testbase     [0.12178990400207113, 0.13090817400006927, 0.13304468899877975]
testbasenot  [0.13121789299839293, 0.14976675499929115, 0.1521548589989834]
teststr      [0.13410512400150765, 0.1354981399999815, 0.147247362001508]
plain        [0.13691626099898713, 0.1384456069972657, 0.1426525679999031]

5: Size=32, Loops=4096
* Integer
cached       [0.13246865899782279, 0.13320018100057496, 0.134628559997509]
plain        [0.1636957459995756, 0.16763203899972723, 0.1752369269997871]
testbase     [0.26010187700012466, 0.2606812570011243, 0.2647345440018398]
testbasenot  [0.2620696090016281, 0.26230394700178294, 0.26258907899682526]
teststr      [0.27685887300322065, 0.2787095199964824, 0.28293989099984174]

* String
cached       [0.10246079200078384, 0.10416977099885116, 0.10755630499988911]
testbasenot  [0.10829716499938513, 0.10918466699877172, 0.10935586699997657]
testbase     [0.11739019699962228, 0.11808202800239087, 0.11899654000080773]
plain        [0.12601002500014147, 0.12718953500007046, 0.13454839599944535]
teststr      [0.13366336599938222, 0.13407608800116577, 0.13510101700012456]

6: Size=64, Loops=2048
* Integer
cached       [0.12591946799875586, 0.127094235002005, 0.13223557899982552]
plain        [0.160616523000499, 0.16232994500023779, 0.1691623620026803]
testbase     [0.2534341589998803, 0.2556092949998856, 0.2571690379991196]
testbasenot  [0.2560774869998568, 0.2574564010028553, 0.2606996459981019]
teststr      [0.268248238000524, 0.2702014210008201, 0.27107579600124154]

* String
cached       [0.09791737100022146, 0.09819723300097394, 0.10752435399990645]
testbasenot  [0.1057888709983672, 0.10588572099732119, 0.16173565400094958]
testbase     [0.10636284599968349, 0.1179599219976808, 0.12130766799964476]
plain        [0.12285572399923694, 0.12589510299949325, 0.13114397300159908]
teststr      [0.13122114399811835, 0.13273253399893292, 0.14575592999972287]

7: Size=128, Loops=1024
* Integer
cached       [0.12404713899741182, 0.12496110600113752, 0.12496385000122245]
plain        [0.15980284800025402, 0.16046370399999432, 0.16711239899814245]
testbasenot  [0.25531527800194453, 0.25563639699976193, 0.2586420219995489]
testbase     [0.25544935799916857, 0.2558138679996773, 0.257172014000389]
teststr      [0.2699256220003008, 0.2712909309993847, 0.27702098800000385]

* String
cached       [0.09376715399776003, 0.09393715400074143, 0.09975314399707713]
testbasenot  [0.10510071799944853, 0.10511873200084665, 0.10523289399861824]
testbase     [0.11240010600158712, 0.11325187799957348, 0.11632439300228725]
plain        [0.12139380200096639, 0.12202585699924384, 0.1315958569975919]
teststr      [0.12834531499902369, 0.12949470400053542, 0.12955383699954837]

8: Size=256, Loops=512
* Integer
cached       [0.12225364700134378, 0.12283446399669629, 0.1285843859986926]
plain        [0.15971405900199898, 0.16198832800000673, 0.16777605400056927]
testbase     [0.2507534860014857, 0.2527904779999517, 0.25378678199922433]
testbasenot  [0.25323686200135853, 0.2547167230004561, 0.25919888999851537]
teststr      [0.2652072370001406, 0.2658402630004275, 0.2674206650008273]

* String
cached       [0.0906629850032914, 0.0985801380011253, 0.09929232800277532]
testbase     [0.10155730300175492, 0.1042869699995208, 0.11276149599871133]
testbasenot  [0.10197166099897004, 0.11451221999959671, 0.15595895300066331]
plain        [0.11898361400017166, 0.12018223199993372, 0.12760113599870238]
teststr      [0.12645652200080804, 0.12671815700014122, 0.14095144699967932]

9: Size=512, Loops=256
* Integer
cached       [0.12672984500022721, 0.1462409830019169, 0.2653043659993273]
plain        [0.161721200998727, 0.17296033000093303, 0.19699998799842433]
testbase     [0.25432757399903494, 0.25851125400004094, 0.258548003002943]
testbasenot  [0.25619441399976495, 0.25656893900304567, 0.25998359599907417]
teststr      [0.2719232039999042, 0.2744571339972026, 0.2751794379983039]

* String
cached       [0.08841608199873008, 0.08848714099804056, 0.09124958899701596]
testbasenot  [0.09962382599769626, 0.10016373899998143, 0.10028601600060938]
testbase     [0.10713129000214394, 0.10752918499929365, 0.10952026399900205]
plain        [0.1163020489984774, 0.12190789400119684, 0.1264930679972167]
teststr      [0.1242994140011433, 0.12458201900153654, 0.12523995000083232]

10: Size=1024, Loops=128
* Integer
cached       [0.12827690600170172, 0.1294701549995807, 0.13387694999983069]
plain        [0.16636216699771467, 0.16866590399877168, 0.17549873600000865]
testbasenot  [0.25435296399882645, 0.25515673799964134, 0.2605281959986314]
testbase     [0.26351416900070035, 0.26398584699927596, 0.2651360300005763]
teststr      [0.26816077799958293, 0.26908816800278146, 0.2715630999991845]

* String
cached       [0.08827024300262565, 0.09090095799911069, 0.09729095900183893]
testbase     [0.10063145499952952, 0.1010660120009561, 0.10904535399822635]
testbasenot  [0.10313185999984853, 0.11444468399713514, 0.14796407999892836]
plain        [0.11569941500056302, 0.11579339799936861, 0.12615105800068704]
teststr      [0.12353994099976262, 0.12515813500067452, 0.13752399999793852]

These timings were performed on a rather old 32 bit single core 2GHz machine with 2GB of RAM running on a Debian derivative of Linux. I used Python 2.6.6 and Python 3.6.0. Your results may vary. ;) In any case, these results should only be used as a rough guide. timeit does a pretty good job of only timing the stuff we want to time, but it has no control over other processes that also want to use the CPU.


来源: stackexchange

问:
How to represent this table?
I have the following table :

I would like to represent this table in Python so I'll be able to run on each key and search for specific string, if I have a much, return the keyName.

for example:

```<code>str = "c"
if str == [searching all values inside the table]:
    return keyName
</code>```

What will be the best option for me?

答:
Use dict of list.
That way you can iterate over the keys and search for your value:

```<code>for key, values in ket_value_map.iteritems():
    if searched_value in values:
        return key
</code>```

Hope I understood you

ContentLicense: CC BY-SA 3.0

Title: How to represent this table?

Body:
I have the following table :

I would like to represent this table in Python so I'll be able to run on each key and search for specific string, if I have a much, return the keyName.

for example:

str = "c"
if str == [searching all values inside the table]:
    return keyName

What will be the best option for me?

Body:
Use dict of list.
That way you can iterate over the keys and search for your value:

for key, values in ket_value_map.iteritems():
    if searched_value in values:
        return key

Hope I understood you


问:
how to force break words that exceed width of parent div
i am running into an issue that i do not think is possible with css. i have a text output dynamically showing in a label that only has the following html

label {
width:94px;
}

i am trying to come up with a solution that will dynamically take any mutliple word that exceeds the width of the div, and force break that word. if a word is to be split but could fit on its own line without breaking within the text, then wrap it to the next line

an example of this working fine as is would be the following screenshot:

here is the current scenario where a letter inside a word is pushed to the next line:

here is what i would like to happen in this scenario:

any help would be greatly appreciated. i am very curious if this is even possible with css without a lot of hacks and cross-browser issues...

答:
By default, it should wrap (example 1)

use word-break: keep-all; (example 2)

But when a word is too long you need to use overflow-wrap: break-word; (example 3)

p {
  width: 140px;
  border: 1px solid #000000;
}

p.test1 {
  word-break: keep-all;
}

p.test2 {
  overflow-wrap: break-word;
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><p class="test">This paragraph contains some text. This line will-break-at-hyphens. verylongtextnonstoplolstillnotdonesorry</p>

<p class="test1">This paragraph contains some text. This line will-break-at-hyphens. verylongtextnonstoplolstillnotdonesorry</p>

<p class="test2">This paragraph contains some text. The lines will break at any character. verylongtextnonstoplolstillnotdonesorry</p></code>```

Body:
By default, it should wrap (example 1)

use word-break: keep-all; (example 2)

But when a word is too long you need to use overflow-wrap: break-word; (example 3)

p {
  width: 140px;
  border: 1px solid #000000;
}

p.test1 {
  word-break: keep-all;
}

p.test2 {
  overflow-wrap: break-word;
}

<p class="test">This paragraph contains some text. This line will-break-at-hyphens. verylongtextnonstoplolstillnotdonesorry</p>

<p class="test1">This paragraph contains some text. This line will-break-at-hyphens. verylongtextnonstoplolstillnotdonesorry</p>

<p class="test2">This paragraph contains some text. The lines will break at any character. verylongtextnonstoplolstillnotdonesorry</p>


问:
how to figure image size to fit below header and above footer
I want to center an image in the available space below the page header and above the page footer.
It needs to be responsive, so I want the picture to be as large as possible but not overlap the header and footer.

The ID of the image is #nMapImg.

I have these values:

```<code>var dh=$(document).height();
var dw=$(document).width();
var hh=$("#header").height();
var fh=$("#footer").height();
var w=$("#nMapImg").width();
var availHeight=h-hh-fh;
</code>```

I think this is just math to figure the max size I can have and then I'll center it. I just can't wrap my head around it.

答:
using flexbox, you can achieve this with pure css

body {
  margin: 0;
  padding: 0;
}
html, body, .app {
  height: 100%;
}
header {
  background: red;
}
footer {
  background: blue;
}
footer, header {
  height: 400px;
}

img {
  max-width: 100%;
  max-height: 100%;
}
.app {
  display: flex;
  flex-direction: column;
}
.content {
  flex: 1 1 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><div class="app">
  <header>header</header>
  <div class="content">
    <img src="http://placehold.it/500" alt="" />
  </div>
  <footer>footer</footer>
</div></code>```

Body:
I want to center an image in the available space below the page header and above the page footer.
It needs to be responsive, so I want the picture to be as large as possible but not overlap the header and footer.

The ID of the image is #nMapImg.

I have these values:

var dh=$(document).height();
var dw=$(document).width();
var hh=$("#header").height();
var fh=$("#footer").height();
var w=$("#nMapImg").width();
var availHeight=h-hh-fh;

I think this is just math to figure the max size I can have and then I'll center it. I just can't wrap my head around it.

Body:
using flexbox, you can achieve this with pure css

body {
  margin: 0;
  padding: 0;
}
html, body, .app {
  height: 100%;
}
header {
  background: red;
}
footer {
  background: blue;
}
footer, header {
  height: 400px;
}

img {
  max-width: 100%;
  max-height: 100%;
}
.app {
  display: flex;
  flex-direction: column;
}
.content {
  flex: 1 1 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

<div class="app">
  <header>header</header>
  <div class="content">
    <img src="http://placehold.it/500" alt="" />
  </div>
  <footer>footer</footer>
</div>


问:
Why is return used instead of console.log?
I'm trying to learn JS from Codecademy. I'm doing this excercise.

Can someone please tell me why return is used instead of console.log()?
I don't fully understand how return works.

答:
console.log is useful for debugging purposes, but actually can't accomplish anything as far as affecting the surrounding code or application.

return is a way of "returning" a value generated by a function to the place it was called from. Think of it as giving a value to that function.

example:

```<code> function retFive(){
   return 5;
 }

 var x = 37 + retFive();
console.log(x); //prints out 42
</code>```

hope that helps!

Body:
console.log is useful for debugging purposes, but actually can't accomplish anything as far as affecting the surrounding code or application.

return is a way of "returning" a value generated by a function to the place it was called from. Think of it as giving a value to that function.

example:

 function retFive(){
   return 5;
 }

 var x = 37 + retFive();
console.log(x); //prints out 42

hope that helps!


问:
Getting individual windows application current volume output level as visualized in audio Mixer
I am trying to write a C# code that outputs the current audio output level from each of the windows application accessing the sound output (as shown with constantly changing green bars of the Volume mixer).

The program will check every 10 ms, and outputs sth like this: Windows Media Player: 30, Mozilla Firefox: 0, Adobe Flash Player: 35 (as per the figure)

I am using Windows 7, and trying it in C# (as Java cannot achieve this).

I have found ways to get and set the Master Volume (the handle bar which shows 65% for Windows Media Player) for a running application, is there a way to get the green fluctuating level data?

Thank you!

答:
Here is a sample application which displays the audio levels from running applications in a graph. There are two versions, one in WPF and one in Windows.Forms. They use the method from Florian's answer to get the audio levels.

https://github.com/jeske/SoundLevelMonitor


问:
Angular 2: [hidden] and *NgIf not working?
So i have this table showing a green circle if data is available. in gegevensAanwezig you have a schoolname a schoolcode and some booleans(Entreetoets, Eindtoets etc) to show if data is available or not.

```<code>        <tr *ngFor="let g of gegegevensAanwezig">
            <td>{{ g.brinCode }}</td>
            <td>{{ g.schoolNaam }}</td>
            <td></td>
            <td></td>
            <td><div class="circle" [hidden]="!g.Entreetoets">  </div> </td>
            <td><div class="circle" [hidden]="!g.Eindtoets">  </div></td>
            <td><div class="circle" [hidden]="!g.BLez">  </div></td>
            <td><div class="circle" [hidden]="!g.BLui">  </div></td>
            <td><div class="circle" [hidden]="!g.DMT">  </div></td>
            <td><div class="circle" [hidden]="!g.ORD">  </div></td>
            <td><div class="circle" [hidden]="!g.RW">  </div></td>
            <td><div class="circle" [hidden]="!g.SP">  </div></td>
            <td><div class="circle" [hidden]="!g.SV">  </div></td>
            <td><div class="circle" [hidden]="!g.TL">  </div></td>
            <td><div class="circle" [hidden]="!g.TV">  </div></td>
            <td><div class="circle" [hidden]="!g.TVK">  </div></td>
            <td><div class="circle" [hidden]="!g.WS">  </div></td>
        </tr>
</code>```

The g.brinCode & g.schoolNaam are working they show the schoolname and schoolcode just fine. But [hidden] or *NgIf is not working. Either all circles are shown or nothing is shown.

Is this because of Angular-universal rendering the view on the backend while data is being loaded after it is served by the browser?

I used if (isBrowser) to load data after the view is served because i needed the localStorage.

screenshot:
The white space should be filled with some green circles based on if the boolean is true or false

console.log(gegevensAanwezig):

答:
As it turned out somehow angular does not recognize capital variables..
I changed everything to lowercase and it worked..</p>

Body:
So i have this table showing a green circle if data is available. in gegevensAanwezig you have a schoolname a schoolcode and some booleans(Entreetoets, Eindtoets etc) to show if data is available or not.

            {{ g.brinCode }}
            {{ g.schoolNaam }}

The g.brinCode & g.schoolNaam are working they show the schoolname and schoolcode just fine. But [hidden] or *NgIf is not working. Either all circles are shown or nothing is shown.

Is this because of Angular-universal rendering the view on the backend while data is being loaded after it is served by the browser?

I used if (isBrowser) to load data after the view is served because i needed the localStorage.

screenshot:
The white space should be filled with some green circles based on if the boolean is true or false

console.log(gegevensAanwezig):


问:
Split testing transactional emails
I'm trying to figure out a solution to manage our transaction emails (such as the welcome email, you've got a bid, etc...)

We would like to be able to allow marketing to manage the content of the emails, and create split tests to test content / subject lines / etc...

Ideally we could invent our own success metrics to report back to the email management system (such as user completed registration, accepted bid, etc...).

Right now we have our emails in templates using stringtemplate.  The code replaces tokens with the correct content for that email.

Strongmail is a potential solution, but it is pricey - anybody have experience with alternatives?

答:
Have you taken a look at PostageApp?

Currently, it's a layer between your web app and your SMTP server which has additional features for your transactional emails.

With PostageApp, you are able to create two different templates and have them triggered alternately with different content and subject lines. However, the metrics that you would want to use for A/B testing aren't built into the system yet, so I'm not sure if it would be a good fit for you.

Full Disclosure: I work for The Working Group, the company that created PostageApp.

But if you do have questions about what we can help you with and what we can't, definitely let me know and I can answer plenty of questions for you!

ContentLicense: CC BY-SA 2.5

Title: Split testing transactional emails


问:
How to mock method chaining using Mockito
I have a method which has below line of code.

```<code>getRequestedCapabilities(){
        ObjectMapper mapper = new ObjectMapper();

Capabilities capabilities = mapper.readValue(new
 File(getClass().getClassLoader().getResource(jsonFilePath).getFile()), Capabilities.class);

}
</code>```

In my test method I have prepared a DTO(capabilities) which  mapper.readValue() needs to return when it is invoked.

```<code>Mockito.when(mapper.readValue(Mockito.mock(File.class),Mockito.eq(Capabilities.class))).thenReturn(capabilities);
</code>```

But I am getting an exception saying invalid use of matchers.
Can you please suggest how can I do this? Here the issue is how should I mock the parameters that is passed to mapper.readValue method which has method invocation chaining.

答:
```<code>Mockito.when(mapper.readValue(Mockito.mock(File.class),Mockito.eq(Capabilities.class))).thenReturn(capabilities);
</code>```

It's coming from Mockito.mock, when using parameters matchers all parameters must be matchers. You should then replace Mockito.mock(File.class) with Mockito.any(File.class) or Mockito.eq(a properly initialized mock)

Body:
I have a method which has below line of code.

getRequestedCapabilities(){
        ObjectMapper mapper = new ObjectMapper();

Capabilities capabilities = mapper.readValue(new
 File(getClass().getClassLoader().getResource(jsonFilePath).getFile()), Capabilities.class);

}

In my test method I have prepared a DTO(capabilities) which  mapper.readValue() needs to return when it is invoked.

Mockito.when(mapper.readValue(Mockito.mock(File.class),Mockito.eq(Capabilities.class))).thenReturn(capabilities);

But I am getting an exception saying invalid use of matchers.
Can you please suggest how can I do this? Here the issue is how should I mock the parameters that is passed to mapper.readValue method which has method invocation chaining.

Body:
Mockito.when(mapper.readValue(Mockito.mock(File.class),Mockito.eq(Capabilities.class))).thenReturn(capabilities);

It's coming from Mockito.mock, when using parameters matchers all parameters must be matchers. You should then replace Mockito.mock(File.class) with Mockito.any(File.class) or Mockito.eq(a properly initialized mock)


问:
Web API: different way of attribute routing
I am learning web api. just gone through few articles and found attribute routing can be done different way.

https://www.codeproject.com/Articles/774807/Attribute-Routing-in-ASP-NET-MVC-WebAPI

http://www.c-sharpcorner.com/UploadFile/b1df45/web-api-route-and-route-prefix-part-2/

https://www.codeproject.com/Articles/999691/RESTful-Day-sharp-Custom-URL-Re-Writing-Routing-us

See the code used:

```<code>[RoutePrefix("Movie")]
public class HomeController : Controller
{
      //Route: Movie/Index
      [Route]
      public ActionResult Index()
      {
           ViewBag.Message = "You are in Home Index";
           return View();
      }

      //Route: NewRoute/About
       [Route("~/NewRoute/About")]
       public ActionResult About()
       {
           ViewBag.Message = "You successfully reached NEWRoute/About route";
           return View();
       }
}
</code>```

in above example the author use Route attribute to define route for action.

see this one again

```<code>[GET("productid/{id?}")]
public HttpResponseMessage Get(int id)
{
    var product = _productServices.GetProductById(id);
    if (product != null)
        return Request.CreateResponse(HttpStatusCode.OK, product);
    return Request.CreateErrorResponse(HttpStatusCode.NotFound, "No product found for this id");
}
</code>```

Here the author do not use route attribute rather use http verbs to define route.

So tell me which approach is right?

Another question that by attribute routine we can give different name to action then when one should use action name attribute to give different name to action?

When we can change action name by attribute routing then why should one use action name attribute to give different name to action?

答:
[Route] exposes an action to the outside world. It takes the action name (method name) as the name for the route. It can use the RoutePrefix or controller name to construct the URL.

If you provide a value to [Route(template)], that (relative) URL is used to construct the URL the action is available.

Since routes are by default only accessible through GET, you can use HttpGet(template) and Route the same way. If you use Route together with HttpGet and HttpPost for example, that route will be available through those verbs.

So you can mix around a little, depending on your needs and preferences.

Body:
I am learning web api. just gone through few articles and found attribute routing can be done different way.

https://www.codeproject.com/Articles/774807/Attribute-Routing-in-ASP-NET-MVC-WebAPI

http://www.c-sharpcorner.com/UploadFile/b1df45/web-api-route-and-route-prefix-part-2/

https://www.codeproject.com/Articles/999691/RESTful-Day-sharp-Custom-URL-Re-Writing-Routing-us

See the code used:

[RoutePrefix("Movie")]
public class HomeController : Controller
{
      //Route: Movie/Index
      [Route]
      public ActionResult Index()
      {
           ViewBag.Message = "You are in Home Index";
           return View();
      }

      //Route: NewRoute/About
       [Route("~/NewRoute/About")]
       public ActionResult About()
       {
           ViewBag.Message = "You successfully reached NEWRoute/About route";
           return View();
       }
}

in above example the author use Route attribute to define route for action.

see this one again

[GET("productid/{id?}")]
public HttpResponseMessage Get(int id)
{
    var product = _productServices.GetProductById(id);
    if (product != null)
        return Request.CreateResponse(HttpStatusCode.OK, product);
    return Request.CreateErrorResponse(HttpStatusCode.NotFound, "No product found for this id");
}

Here the author do not use route attribute rather use http verbs to define route.

So tell me which approach is right?

Another question that by attribute routine we can give different name to action then when one should use action name attribute to give different name to action?

When we can change action name by attribute routing then why should one use action name attribute to give different name to action?


问:
Roundtrip Unicode conversion returns different Byte[] array
I'm tinkering with RSA signing of data.

I'm using a plaintext string, which i convert to byte array. i then generate private certificate, sign the byte array and then generate public key.

next i'm using the same byte array to verify the signature.

but i want to convert signature, in between steps, to the string - idea is to append it later on to the file that's being signed.

```<code>static void TestSigning(string privateKey)
    {
        string data = "TEST_TEST-TEST+test+TEst";
        Console.WriteLine("==MESSAGE==");
        Console.WriteLine(data);
        byte[] dataByte = Encoding.Unicode.GetBytes(data);
        using (var rsa = new RSACryptoServiceProvider())
        {
            rsa.FromXmlString(privateKey);
            var publicKey = rsa.ToXmlString(false);
            byte[] signature = rsa.SignData(dataByte, CryptoConfig.MapNameToOID("SHA512"));
            string signatureString = Encoding.Unicode.GetString(signature);
            byte[] roundtripSignature = Encoding.Unicode.GetBytes(signatureString);
            Console.WriteLine("==TEST==");
            Console.WriteLine(signature.Length.ToString());
            Console.WriteLine(roundtripSignature.Length.ToString());
            using (var checkRSA = new RSACryptoServiceProvider())
            {
                checkRSA.FromXmlString(publicKey);
                bool verification = checkRSA.VerifyData(
                    dataByte,
                    CryptoConfig.MapNameToOID("SHA512"),
                    roundtripSignature);
                Console.WriteLine("==Verification==");
                Console.WriteLine(verification.ToString());
                Console.ReadKey();
            }
        }
    }
</code>```

now here's the fun part
if i use UTF8 encoding i get byte arrays of different length

256 is the original size

484 is the roundtrip

UTF7 returns different sizes too
256 vs 679

both ASCII and Unicode return proper sizes 256 vs 256.

i've tried using

```<code>var sb = new StringBuilder();
        for (int i = 0; i < signature.Length; i++)
        {
            sb.Append(signature[i].ToString("x2"));
        }
</code>```

to get the string. I'm then using Encoding.UTF8.GetBytes() method

this time i get the sizes of:
256 vs 512
if i remove the format from toString() i get:
256 vs 670

signature verification alwayas failed.
it works fine if i use 'signature' instead of roundtripSignature.

my question: Why, despite using same encoding type i get different byte arrays and strings? shouldn't this conversion be lossless?

答:
Unicode isn't a good choice because, at minimum, \0, CR, LF, <delete>, <backspace> (and the rest of the control codes) can mess things up. (See an answer about this for Encrypt/Decrypt for more).

As @JamesKPolk said, you need to use a suitable binary-to-text encoding.  Base64 and hex/Base16 are the most common, but there are plenty of other viable choices.

Body:
I'm tinkering with RSA signing of data.

I'm using a plaintext string, which i convert to byte array. i then generate private certificate, sign the byte array and then generate public key.

next i'm using the same byte array to verify the signature.

but i want to convert signature, in between steps, to the string - idea is to append it later on to the file that's being signed.

static void TestSigning(string privateKey)
    {
        string data = "TEST_TEST-TEST+test+TEst";
        Console.WriteLine("==MESSAGE==");
        Console.WriteLine(data);
        byte[] dataByte = Encoding.Unicode.GetBytes(data);
        using (var rsa = new RSACryptoServiceProvider())
        {
            rsa.FromXmlString(privateKey);
            var publicKey = rsa.ToXmlString(false);
            byte[] signature = rsa.SignData(dataByte, CryptoConfig.MapNameToOID("SHA512"));
            string signatureString = Encoding.Unicode.GetString(signature);
            byte[] roundtripSignature = Encoding.Unicode.GetBytes(signatureString);
            Console.WriteLine("==TEST==");
            Console.WriteLine(signature.Length.ToString());
            Console.WriteLine(roundtripSignature.Length.ToString());
            using (var checkRSA = new RSACryptoServiceProvider())
            {
                checkRSA.FromXmlString(publicKey);
                bool verification = checkRSA.VerifyData(
                    dataByte,
                    CryptoConfig.MapNameToOID("SHA512"),
                    roundtripSignature);
                Console.WriteLine("==Verification==");
                Console.WriteLine(verification.ToString());
                Console.ReadKey();
            }
        }
    }

now here's the fun part
if i use UTF8 encoding i get byte arrays of different length

256 is the original size

484 is the roundtrip

UTF7 returns different sizes too
256 vs 679

both ASCII and Unicode return proper sizes 256 vs 256.

i've tried using

var sb = new StringBuilder();
        for (int i = 0; i < signature.Length; i++)
        {
            sb.Append(signature[i].ToString("x2"));
        }

to get the string. I'm then using Encoding.UTF8.GetBytes() method

this time i get the sizes of:
256 vs 512
if i remove the format from toString() i get:
256 vs 670

signature verification alwayas failed.
it works fine if i use 'signature' instead of roundtripSignature.

my question: Why, despite using same encoding type i get different byte arrays and strings? shouldn't this conversion be lossless?


问:
WooCommerce inquiry if no price available
I'm helping a good friend setting up a WooCommerce shop. Since the shop is going to be bigger and the products are pretty variable and customizable we are not able to provide/configure all prizes from the beginning.

However we would like all products to be in the shop and ad an inquiry lead form in case no price is available.

Since I never programmed with WooCommerce I was wondering that is the right hook to implement such an functionality?

答:
Maybe too late, but I have had same issue currently.

Here is there code, Woocommerce uses to check if a product can be purchased:
https://github.com/woocommerce/woocommerce/blob/master/includes/abstracts/abstract-wc-product.php#L1404

Notice about: && '' !== $this->get_price()

```<code>/**
 * Returns false if the product cannot be bought.
 *
 * @return bool
 */
public function is_purchasable() {
    return apply_filters( 'woocommerce_is_purchasable', $this->exists() && ( 'publish' === $this->get_status() || current_user_can( 'edit_post', $this->get_id() ) ) && '' !== $this->get_price(), $this );
}
</code>```

So you need to write a filter like this to override default:

```<code>add_filter( 'woocommerce_is_purchasable', function ( $is_purchasable, $product ) {
    return $product->exists() && ( 'publish' === $product->get_status() || current_user_can( 'edit_post', $product->get_id() ) );
}, 10, 2 );
</code>```

Body:
Maybe too late, but I have had same issue currently.

Here is there code, Woocommerce uses to check if a product can be purchased:
https://github.com/woocommerce/woocommerce/blob/master/includes/abstracts/abstract-wc-product.php#L1404

Notice about: && '' !== $this->get_price()

/**
 * Returns false if the product cannot be bought.
 *
 * @return bool
 */
public function is_purchasable() {
    return apply_filters( 'woocommerce_is_purchasable', $this->exists() && ( 'publish' === $this->get_status() || current_user_can( 'edit_post', $this->get_id() ) ) && '' !== $this->get_price(), $this );
}

So you need to write a filter like this to override default:

add_filter( 'woocommerce_is_purchasable', function ( $is_purchasable, $product ) {
    return $product->exists() && ( 'publish' === $product->get_status() || current_user_can( 'edit_post', $product->get_id() ) );
}, 10, 2 );


问:
How to avoid deadlocks in Postgres?
I run multiple servers and each of them are running multi-update statements such as these

```<code>UPDATE user SET ... WHERE user_id = 2;
UPDATE user SET ... WHERE user_id = 1;
UPDATE user SET ... WHERE user_id = 3;
</code>```

If there is a concurrenct update like:

```<code>UPDATE user SET ... WHERE user_id = 1;
UPDATE user SET ... WHERE user_id = 2;
</code>```

Then I will run into error deadlock detected

Right now, my fix is to order the update statement on client-side and always guarantee that the ids are in the same order. i.e. I always sort the statement on the client-side ASC by user_id

This has seemed to fix the issue so far, but I still have questions:

Is this (order the statements) a good solution for fixing deadlock errors?

If I start doing multi-table updates, do I have to order the statements across tables as well?

答:
Ordering statements at the application level is a good solution in that it avoids database overhead.  The statements would need to keep their order per-table.  If this is easily workable in the application, it's worthwhile.

There is also a solution at the database level: serializable isolation.

The Serializable isolation level provides the strictest transaction isolation. This level emulates serial transaction execution for all committed transactions; as if transactions had been executed one after another, serially, rather than concurrently. However, like the Repeatable Read level, applications using this level must be prepared to retry transactions due to serialization failures. In fact, this isolation level works exactly the same as Repeatable Read except that it monitors for conditions which could make execution of a concurrent set of serializable transactions behave in a manner inconsistent with all possible serial (one at a time) executions of those transactions.

You can set this isolation level when you start your transactions.  This does add some database overhead, but more importantly the application must be ready to catch serialization failures and retry the transaction.

Title: How to avoid deadlocks in Postgres?

Body:
I run multiple servers and each of them are running multi-update statements such as these

UPDATE user SET ... WHERE user_id = 2;
UPDATE user SET ... WHERE user_id = 1;
UPDATE user SET ... WHERE user_id = 3;

If there is a concurrenct update like:

UPDATE user SET ... WHERE user_id = 1;
UPDATE user SET ... WHERE user_id = 2;

Then I will run into error deadlock detected

Right now, my fix is to order the update statement on client-side and always guarantee that the ids are in the same order. i.e. I always sort the statement on the client-side ASC by user_id

This has seemed to fix the issue so far, but I still have questions:

Is this (order the statements) a good solution for fixing deadlock errors?

If I start doing multi-table updates, do I have to order the statements across tables as well?


问:
Java array initialization within argument list
How come the first call to someMethod doesn't compile without being explicit that it's String[]?

It's fine to use an array initializer to create a String[] array but you can't use it to pass an argument.  Are the curly braces used in some other fashion for passing arguments that derails how I'd expect this to behave?

```<code>public void someMethod(String[] arr){
    //do some magic
}

public void makeSomeMagic(){

    String[] arr = {"cat", "fish", "cow"};

    //Does not compile!
    someMethod({"cat", "fish", "cow"});

    //This compiles!
    someMethod(new String[]{"cat", "fish", "cow"});

    //This compiles!
    someMethod(arr);
}
</code>```

The compiler error is the following:

The method someMethod(String[]) in the type Moo is not applicable for the arguments (String, String, String)

答:
Or you can use varargs:

```<code>public void someMethod(String... arr){
    //do some magic
}

public void makeSomeMagic(){
    someMethod("cat", "fish", "cow");
}
</code>```

It's basically a fancy syntax for an array parameter (vararg must be the last parameter in method signature).

Body:
How come the first call to someMethod doesn't compile without being explicit that it's String[]?

It's fine to use an array initializer to create a String[] array but you can't use it to pass an argument.  Are the curly braces used in some other fashion for passing arguments that derails how I'd expect this to behave?

public void someMethod(String[] arr){
    //do some magic
}

public void makeSomeMagic(){

    String[] arr = {"cat", "fish", "cow"};

    //Does not compile!
    someMethod({"cat", "fish", "cow"});

    //This compiles!
    someMethod(new String[]{"cat", "fish", "cow"});

    //This compiles!
    someMethod(arr);
}

The compiler error is the following:

The method someMethod(String[]) in the type Moo is not applicable for the arguments (String, String, String)

Body:
Or you can use varargs:

public void someMethod(String... arr){
    //do some magic
}

public void makeSomeMagic(){
    someMethod("cat", "fish", "cow");
}

It's basically a fancy syntax for an array parameter (vararg must be the last parameter in method signature).


问:
hide an element using angularjs
I using visjs, i want to hide the first image of the first div, how i can do that, i used ng-show but it doesnt work??</p>

```<code>storyboard.drawTimeLine = function (start, end, color, marg,km) {
        var elm = angular.element(document.querySelector('#vv'));
            elm.html("");
            item = {};
            item.id = start + '@' + end;
            item.start = start;
            item.end = end;
            item.title = elm.append($compile(
                '<div style="padding-left:0px;padding-bottom: 9px "><img src="../../../../assets/img/fuel.png"> -</div>'+
                '<div style="padding-left:0px;padding-bottom: 9px "><img src="../../../../assets/img/oil.png">  -</div>')($scope)).html();
                ;
            item.style ="top:"+ marg +"px;height:14px;background-color:" + color + ";border-color:" + color;
        };
</code>```

答:
You probably shouldn't be building your HTML like that.

You should use AngularJS properly to bind your HTML to your controller.

About hiding:

Checkout more: https://docs.angularjs.org/api/ng/directive/ngHide

Title: hide an element using angularjs

Body:
I using visjs, i want to hide the first image of the first div, how i can do that, i used ng-show but it doesnt work??

storyboard.drawTimeLine = function (start, end, color, marg,km) {
        var elm = angular.element(document.querySelector('#vv'));
            elm.html("");
            item = {};
            item.id = start + '@' + end;
            item.start = start;
            item.end = end;
            item.title = elm.append($compile(
                '
 -
'+
                '
  -
')($scope)).html();
                ;
            item.style ="top:"+ marg +"px;height:14px;background-color:" + color + ";border-color:" + color;
        };

Body:
You probably shouldn't be building your HTML like that.

You should use AngularJS properly to bind your HTML to your controller.

About hiding: <div ng-hide="myValue"></div>

Checkout more: https://docs.angularjs.org/api/ng/directive/ngHide


问:
How to set/change Active Directory user password across domains using C# .NET?
I have been searching around for quite some time now how to set/change a password and revoke/restore a user but have yet to find a solution that actually works for me.

I am beginning to lean towards the fact that I am crossing domains as the problem, even though I can programmatically create/delete/update and even connect/disconnect users from groups.

Basically, I've tried the following ways:

```<code>DirectoryEntry account = new DirectoryEntry("LDAP://" + adHostname + "/" + dn, adUserName, adPassword);

account.Invoke("SetPassword", "Password1");
account.Properties["LockOutTime"].Value = 0;
account.CommitChanges();
</code>```

And also

```<code>account.Invoke("SetPassword", new object[] { "Password1" });
</code>```

They both ultimately throw the error "One or more input parameters are invalid\r\n"

I then have tried to use the .NET 3.5 approach using principal context.

```<code>using (var context = new PrincipalContext(ContextType.Domain, adHostname, myContainer, ContextOptions.SimpleBind, adUserName, adPassword))
    {
        using (var user = UserPrincipal.FindByIdentity(context, account.Properties["sAMAccountName"].Value.ToString()))
        {
             user.SetPassword(password);
        }
    }
</code>```

This approach is also throwing the same error as above. If I switch some things around (I can't seem to remember all the combinations I've tried), it will sometimes throw a "Local error has occurred" COM Exception.

Any help is much appreciated.

## EDIT WITH WORKING SOLUTION ##

```<code>using System.DirectoryServices.Protocols;

LdapDirectoryIdentifier identifier = new LdapDirectoryIdentifier(_adHostname, 636);
NetworkCredential credential = new NetworkCredential(_adUserName, _adPassword);

string password = "MyRandomComplexPassword";

using (LdapConnection connection = new LdapConnection(identifier, credential))
{
    connection.SessionOptions.SecureSocketLayer = true;
    connection.SessionOptions.VerifyServerCertificate += delegate { return true; };
    connection.AuthType = AuthType.Basic;
    connection.Bind(credential);

    DirectoryAttributeModification modPwd = new DirectoryAttributeModification();
    modPwd.Operation = DirectoryAttributeOperation.Replace;
    modPwd.Name = "unicodePwd";
    modPwd.Add(Encoding.Unicode.GetBytes("\"" + password + "\""));

    DirectoryAttributeModification[] dMods = new DirectoryAttributeModification[1];
    dMods[0] = modPwd;

    ModifyRequest modReq = new ModifyRequest(accountDN, dMods);

    DirectoryResponse pwdModResponse;
    pwdModResponse = connection.SendRequest(modReq);
}
</code>```

答:
This one always works for me. Hope it helps.
First add reference to System.DirectoryServices.AccountManagement

```<code>    string hostName = "myDomain";
    string adminName = "admin";
    string adminPassword = "password";

    public static void ResetPassword(string username, string password)
            {
        using (PrincipalContext pContext = new PrincipalContext(ContextType.Domain, hostName, adminName, adminPassword))
                                {
                                    UserPrincipal up = UserPrincipal.FindByIdentity(pContext, username);
                                    if (up != null)
                                    {
                                        up.SetPassword(password);
                                        up.Save();
                                    }
                                }
                        }
</code>```

You can also do all sort of things to UserPrincipal like lock account, expire password, get last logon, etc.

ContentLicense: CC BY-SA 4.0

Body:
I have been searching around for quite some time now how to set/change a password and revoke/restore a user but have yet to find a solution that actually works for me.

I am beginning to lean towards the fact that I am crossing domains as the problem, even though I can programmatically create/delete/update and even connect/disconnect users from groups.

Basically, I've tried the following ways:

DirectoryEntry account = new DirectoryEntry("LDAP://" + adHostname + "/" + dn, adUserName, adPassword);

account.Invoke("SetPassword", "Password1");
account.Properties["LockOutTime"].Value = 0;
account.CommitChanges();

And also

account.Invoke("SetPassword", new object[] { "Password1" });

They both ultimately throw the error "One or more input parameters are invalid\r\n"

I then have tried to use the .NET 3.5 approach using principal context.

using (var context = new PrincipalContext(ContextType.Domain, adHostname, myContainer, ContextOptions.SimpleBind, adUserName, adPassword))
    {
        using (var user = UserPrincipal.FindByIdentity(context, account.Properties["sAMAccountName"].Value.ToString()))
        {
             user.SetPassword(password);
        }
    }

This approach is also throwing the same error as above. If I switch some things around (I can't seem to remember all the combinations I've tried), it will sometimes throw a "Local error has occurred" COM Exception.

Any help is much appreciated.

## EDIT WITH WORKING SOLUTION ##

using System.DirectoryServices.Protocols;

LdapDirectoryIdentifier identifier = new LdapDirectoryIdentifier(_adHostname, 636);
NetworkCredential credential = new NetworkCredential(_adUserName, _adPassword);

string password = "MyRandomComplexPassword";

using (LdapConnection connection = new LdapConnection(identifier, credential))
{
    connection.SessionOptions.SecureSocketLayer = true;
    connection.SessionOptions.VerifyServerCertificate += delegate { return true; };
    connection.AuthType = AuthType.Basic;
    connection.Bind(credential);

    DirectoryAttributeModification modPwd = new DirectoryAttributeModification();
    modPwd.Operation = DirectoryAttributeOperation.Replace;
    modPwd.Name = "unicodePwd";
    modPwd.Add(Encoding.Unicode.GetBytes("\"" + password + "\""));

    DirectoryAttributeModification[] dMods = new DirectoryAttributeModification[1];
    dMods[0] = modPwd;

    ModifyRequest modReq = new ModifyRequest(accountDN, dMods);

    DirectoryResponse pwdModResponse;
    pwdModResponse = connection.SendRequest(modReq);
}

Body:
This one always works for me. Hope it helps.
First add reference to System.DirectoryServices.AccountManagement

    string hostName = "myDomain";
    string adminName = "admin";
    string adminPassword = "password";

    public static void ResetPassword(string username, string password)
            {
        using (PrincipalContext pContext = new PrincipalContext(ContextType.Domain, hostName, adminName, adminPassword))
                                {
                                    UserPrincipal up = UserPrincipal.FindByIdentity(pContext, username);
                                    if (up != null)
                                    {
                                        up.SetPassword(password);
                                        up.Save();
                                    }
                                }
                        }

You can also do all sort of things to UserPrincipal like lock account, expire password, get last logon, etc.


答:
Without answering about that exercise specifically, the purpose of a return statement is to pass information from one function call to another. This information can be anything - an object, a string, a number, and can have any purpose, whether that be to return information about whether the function was successful or not, or to do a calculation and return the answer so another function can use it.

Console.log() simply prints out whatever you pass in as a parameter to the console, which can change depending on your context. It doesn't have anything to do with relaying information from one part of your program to another.

Title: Why is return used instead of console.log?

Body:
I'm trying to learn JS from Codecademy. I'm doing this excercise.

Can someone please tell me why return is used instead of console.log()?
I don't fully understand how return works.


问:
Mail sending error in codeigniter
I am trying to send email through Codeigniter email library form localhost. Below is my code.

```<code>$config = Array(
    'protocol' => 'smtp',
    'smtp_host' => 'ssl://smtp.googlemail.com',
    'smtp_port' => 465,
    'smtp_user' => 'shreesaipratik1@gmail.com',
    'smtp_pass' => 'xxxxxx',
    );
    $this->load->library('email', $config);
    $this->email->set_newline("\r\n");
    $this->email->from('shreesaipratik1@gmail.com');
    $this->email->to('shreesaipratik@gmail.com');
    $this->email->subject('Subject');
    $this->email->message('Sample message');
    if (!$this->email->send())
    {
        show_error($this->email->print_debugger());
    }
    else
    {
        echo 'Your e-mail has been sent!';
    }
</code>```

I am getting following errors:-

Unable to send email using PHP mail(). Your server might not be configured to send mail using this method.  &

Severity: Warning

Message: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set()

Filename: libraries/Email.php

Line Number: 1896

Backtrace:

File: D:\xampp\htdocs\ajax\application\controllers\angular_http.php
Line: 29
Function: send

File: D:\xampp\htdocs\ajax\index.php
Line: 315
Function: require_once

答:
I think you are not configured mail server yet. if you are using XAMPP then you can easily send mail from localhost.

for example you can configure (considering 'C:\xampp' as installation directory) C:\xampp\php\php.ini and c:\xampp\sendmail\sendmail.ini for gmail to send mail.

in C:\xampp\php\php.ini find extension=php_openssl.dll and remove the semicolon from the beginning of that line to make SSL working for gmail for localhost.

in php.ini file find [mail function] and change

```<code>SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = my-gmail-id@gmail.com
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
</code>```

Now Open C:\xampp\sendmail\sendmail.ini. Replace all the existing code in sendmail.ini with following code

```<code>[sendmail]

smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
auth_username=my-gmail-id@gmail.com
auth_password=my-gmail-password
force_sender=my-gmail-id@gmail.com
</code>```

First, make sure you PHP installation has SSL support (look for an "openssl" section in the output from phpinfo()).

You can set the following settings in your PHP.ini:

```<code>ini_set("SMTP","ssl://smtp.gmail.com");
ini_set("smtp_port","465");
</code>```

Please come up with stack you are using on localhost, If you are not using XAMPP or WAMP.

Title: Mail sending error in codeigniter

Body:
I am trying to send email through Codeigniter email library form localhost. Below is my code.

$config = Array(
    'protocol' => 'smtp',
    'smtp_host' => 'ssl://smtp.googlemail.com',
    'smtp_port' => 465,
    'smtp_user' => 'shreesaipratik1@gmail.com',
    'smtp_pass' => 'xxxxxx',
    );
    $this->load->library('email', $config);
    $this->email->set_newline("\r\n");
    $this->email->from('shreesaipratik1@gmail.com');
    $this->email->to('shreesaipratik@gmail.com');
    $this->email->subject('Subject');
    $this->email->message('Sample message');
    if (!$this->email->send())
    {
        show_error($this->email->print_debugger());
    }
    else
    {
        echo 'Your e-mail has been sent!';
    }

I am getting following errors:-

Unable to send email using PHP mail(). Your server might not be configured to send mail using this method.  &

Severity: Warning

Message: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set()

Filename: libraries/Email.php

Line Number: 1896

Backtrace:

File: D:\xampp\htdocs\ajax\application\controllers\angular_http.php
Line: 29
Function: send

File: D:\xampp\htdocs\ajax\index.php
Line: 315
Function: require_once

Body:
I think you are not configured mail server yet. if you are using XAMPP then you can easily send mail from localhost.

for example you can configure (considering 'C:\xampp' as installation directory) C:\xampp\php\php.ini and c:\xampp\sendmail\sendmail.ini for gmail to send mail.

in C:\xampp\php\php.ini find extension=php_openssl.dll and remove the semicolon from the beginning of that line to make SSL working for gmail for localhost.

in php.ini file find [mail function] and change

SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = my-gmail-id@gmail.com
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"

Now Open C:\xampp\sendmail\sendmail.ini. Replace all the existing code in sendmail.ini with following code

[sendmail]

smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
auth_username=my-gmail-id@gmail.com
auth_password=my-gmail-password
force_sender=my-gmail-id@gmail.com

First, make sure you PHP installation has SSL support (look for an "openssl" section in the output from phpinfo()).

You can set the following settings in your PHP.ini:

ini_set("SMTP","ssl://smtp.gmail.com");
ini_set("smtp_port","465");

Please come up with stack you are using on localhost, If you are not using XAMPP or WAMP.


问:
Apache mod_python with django issue
While running a django application on top of apache2 mod_python, I am getting this error message in my apache error log.

```<code>[Tue Dec 14 14:26:45 2010] [error] [client SOME_IP] IOError: Write failed, client closed connection., referer: http://example.com/
Traceback (most recent call last):
  File "/usr/lib/python2.6/dist-packages/mod_python/importer.py", line 1931, in ReportError
    req.write(text)
IOError: Write failed, client closed connection.
[Tue Dec 14 14:26:45 2010] [error] [client SOME_IP] python_handler: Dispatch() returned non-integer., referer: http://example.com/
</code>```

Can anyone please suggest some solution on this?

答: Indicates that the user HTTP client connection was dropped before the complete response could be written back. Nothing one can do about it. Your application should handle it gracefully.</p>

Title: Apache mod_python with django issue

Body:
While running a django application on top of apache2 mod_python, I am getting this error message in my apache error log.

[Tue Dec 14 14:26:45 2010] [error] [client SOME_IP] IOError: Write failed, client closed connection., referer: http://example.com/
Traceback (most recent call last):
  File "/usr/lib/python2.6/dist-packages/mod_python/importer.py", line 1931, in ReportError
    req.write(text)
IOError: Write failed, client closed connection.
[Tue Dec 14 14:26:45 2010] [error] [client SOME_IP] python_handler: Dispatch() returned non-integer., referer: http://example.com/

Can anyone please suggest some solution on this?


答:
Try the below css, your line break will now depend on the width of span

span{
  width:20%;
  display:flex;
  background-color:yellow;
}

div{
  width:100%;
  border:1px solid red;
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><div><span>how to force break words</span></div></code>```

Title: how to force break words that exceed width of parent div

Body:
i am running into an issue that i do not think is possible with css. i have a text output dynamically showing in a label that only has the following html

label {
width:94px;
}

i am trying to come up with a solution that will dynamically take any mutliple word that exceeds the width of the div, and force break that word. if a word is to be split but could fit on its own line without breaking within the text, then wrap it to the next line

an example of this working fine as is would be the following screenshot:

here is the current scenario where a letter inside a word is pushed to the next line:

here is what i would like to happen in this scenario:

any help would be greatly appreciated. i am very curious if this is even possible with css without a lot of hacks and cross-browser issues...

Body:
Try the below css, your line break will now depend on the width of span

span{
  width:20%;
  display:flex;
  background-color:yellow;
}

div{
  width:100%;
  border:1px solid red;
}

<div><span>how to force break words</span></div>


问:
How to represent this table?
I have the following table :

I would like to represent this table in Python so I'll be able to run on each key and search for specific string, if I have a much, return the keyName.

for example:

```<code>str = "c"
if str == [searching all values inside the table]:
    return keyName
</code>```

What will be the best option for me?

答:
you can try with dictionary in python:

```<code>dictionary_table = {'test1':['a','b','c','d'],'test2':['e','f','g','h'],'test3':['i','j','k','l']}
search_str = "c"
key = [k for k,v in dictionary_table.items() if search_str in v]
print key
</code>```

search "c" in the dictionary values, and get its key:

```<code>['test1']
</code>```

Title: How to represent this table?

Body:
I have the following table :

I would like to represent this table in Python so I'll be able to run on each key and search for specific string, if I have a much, return the keyName.

for example:

str = "c"
if str == [searching all values inside the table]:
    return keyName

What will be the best option for me?

Body:
you can try with dictionary in python:

dictionary_table = {'test1':['a','b','c','d'],'test2':['e','f','g','h'],'test3':['i','j','k','l']}
search_str = "c"
key = [k for k,v in dictionary_table.items() if search_str in v]
print key

search "c" in the dictionary values, and get its key:

['test1']


答:
A function in code is really no different from a math function. Think about it this way:

f(x) = x+1

Which would "return" something based on the input of x.

So f(1) = 2 because f(1) returns the value of 1+1.

Putting that into Javascript:

```<code>function f(x){
    return x+1;
}
</code>```

allows us to do this:

```<code>var y = f(1);
</code>```

Now the value of y is 2.

Without a return, we wouldn't be able to assign the value to y.

console.log() really does nothing. It just logs something to the console to view or debug values.

Body:
A function in code is really no different from a math function. Think about it this way:

f(x) = x+1

Which would "return" something based on the input of x.

So f(1) = 2 because f(1) returns the value of 1+1.

Putting that into Javascript:

function f(x){
    return x+1;
}

allows us to do this:

var y = f(1);

Now the value of y is 2.

Without a return, we wouldn't be able to assign the value to y.

console.log() really does nothing. It just logs something to the console to view or debug values.


答:
return has pretty much the same behavior as it does in other languages.

From the return MDN

When a return statement is called in a function, the execution of this function is stopped. If specified, a given value is returned to the function caller. If the expression is omitted, undefined is returned instead.

What this basically means is that when you call a function foo() that has a statement like return 1; it will have the value of 1 so: var bar = foo(); // bar = 1

```<code>function foo() {
    return 1;
}
var bar = foo(); // bar = 1
</code>```

console.log() can be confusing if you're running the program in the console, it prints the value to the console so if our function foo from before was function foo() { console.log(1); } our var bar = foo(); would actually set bar to undefined.

```<code>function foo() {
    console.log(1);
}
var bar = foo(); // bar = undefined
</code>```

console.log() MDN

Body:
return has pretty much the same behavior as it does in other languages.

From the return MDN

When a return statement is called in a function, the execution of this function is stopped. If specified, a given value is returned to the function caller. If the expression is omitted, undefined is returned instead.

What this basically means is that when you call a function foo() that has a statement like return 1; it will have the value of 1 so: var bar = foo(); // bar = 1

function foo() {
    return 1;
}
var bar = foo(); // bar = 1

console.log() can be confusing if you're running the program in the console, it prints the value to the console so if our function foo from before was function foo() { console.log(1); } our var bar = foo(); would actually set bar to undefined.

function foo() {
    console.log(1);
}
var bar = foo(); // bar = undefined

console.log() MDN