1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<TITLE>mxTools - A Collection of New Builtins for Python</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<STYLE TYPE="text/css">
p { text-align: justify; }
ul.indent { }
body { }
</STYLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#FF0000">
<HR SIZE=2 NOSHADE WIDTH="100%">
<H2>mxTools - A Collection of New Builtins for Python</H2>
<HR SIZE=1 NOSHADE WIDTH="100%">
<TABLE WIDTH="100%">
<TR>
<TD>
<SMALL>
<A HREF="#BuiltinFunctions">Builtin Functions</A> :
<A HREF="#BuiltinObjects">Builtin Objects</A> :
<A HREF="#SysFunctions">sys-Module Functions</A> :
<A HREF="#Functions">mx.Tools Functions</A> :
<A HREF="#Examples">Examples</A> :
<A HREF="#Structure">Structure</A> :
<A HREF="#Support">Support</A> :
<A HREF="http://www.egenix.com/files/python/eGenix-mx-Extensions.html#Download-mxBASE"><B>Download</B></A> :
<A HREF="#Copyright">Copyright & License</A> :
<A HREF="#History">History</A> :
<A HREF="" TARGET="_top">Home</A>
</SMALL>
</TD>
<TD ALIGN=RIGHT>
<SMALL>
<FONT COLOR="#FF0000">Version 2.0.3</FONT>
</SMALL>
</TD>
</TABLE>
<HR SIZE=1 NOSHADE WIDTH="100%">
<H3>Introduction</H3>
<UL CLASS="indent">
<P>
As time passes there have often been situations where I
thought "Hey, why not have this as builtin". In most cases
the functions were easily coded in Python. But I started to
use them quite heavily and since performance is always an
issue (at least for me: hits/second pay my bills), I decided
to code them in C. Well, that's how it started and here we
are now with an ever growing number of goodies...
<P>
The functions defined by the C extensions are installed by
the package at import time in different places of the Python
interpreter. They work as fast add-ons to the existing set
of functions and objects.
<P>
</UL><!--CLASS="indent"-->
<A NAME="BuiltinFunctions">
<H3>New Builtin Functions</H3>
<UL CLASS="indent">
<P>
The following functions are installed as Python builtin
functions at package import time. They are then available as
normal builtin functions in every module without explicit
import in each module using them (though it is good practice
to still put a '<CODE>import mx.Tools.NewBuiltins</CODE>' at
the top of each module relying on these add-ons).
<P><DL>
<DT><CODE><FONT COLOR="#000099">
indices(object)
</FONT></CODE></DT>
<DD>
Returns the same as <CODE>tuple(range(len(object)))</CODE>
-- a tad faster and a lot easier to type.<P></DD>
<DT><CODE><FONT COLOR="#000099">
trange([start=0,]stop[,step=1])
</FONT></CODE></DT>
<DD>
This works like the builtin function <CODE>range()</CODE>
but returns a tuple instead of a list. Since
<CODE>range()</CODE> is most often used in for-loops there
really is no need for a mutable data type and construction
of tuples is somewhat (20%) faster than that of lists. So
changing the usage of <CODE>range()</CODE> in for-loops to
<CODE>trange()</CODE> pays off in the long run.<P></DD>
<DT><CODE><FONT COLOR="#000099">
range_len(object)
</FONT></CODE></DT>
<DD>
Returns the same as
<CODE>range(len(object))</CODE>.<P></DD>
<DT><CODE><FONT COLOR="#000099">
tuples(sequence)
</FONT></CODE></DT>
<DD>
Returns much the same as
<CODE>apply(map,(None,)+tuple(sequence))</CODE> does,
except that the resulting list will always have the length
of the first sub-sequence in sequence. The function
returns a list of tuples <CODE>(a[0], b[0], c[0],...),
(a[1], b[1], c[1],...), ...</CODE> with missing elements
being filled in with <CODE>None</CODE>.
<P>
Note that the function is of the single argument type
meaning that calling <CODE>tuples(a,b,c)</CODE> is the
same as calling <CODE>tuples((a,b,c))</CODE>. tuples()
can be used as inverse to lists().<P></DD>
<DT><CODE><FONT COLOR="#000099">
lists(sequence)
</FONT></CODE></DT>
<DD>
Same as tuples(sequence), except that a tuple of lists is
returned. Can be used as inverse to tuples().<P></DD>
<DT><CODE><FONT COLOR="#000099">
reverse(sequence)
</FONT></CODE></DT>
<DD>
Returns a tuple or list with the elements from
<CODE>sequence</CODE> in reverse order. A tuple is
returned, if the sequence itself is a tuple. In all other
cases a list is returned. <P></DD>
<DT><CODE><FONT COLOR="#000099">
dict(items)
</FONT></CODE></DT>
<DD>
Constructs a dictionary from the given items sequence. The
sequence items must contain sequence entries with at least
two values. The first one is interpreted as key, the
second one as associated object. Remaining values are
ignored. <P></DD>
<DT><CODE><FONT COLOR="#000099">
setdict(sequence,value=None)
</FONT></CODE></DT>
<DD>
Constructs a dictionary from the given sequence. The
sequence must contain hashable objects which are used as
keys. The values are all set to value. Multiple keys are
silently ignored. The function comes in handy whenever you
need to work with a sequence in a set based context
(e.g. to determine the set of used values). <P></DD>
<DT><CODE><FONT COLOR="#000099">
invdict(dictionary)
</FONT></CODE></DT>
<DD>
Constructs a new dictionary from the given one with
inverted mappings. Keys become values and vice versa. Note
that no exception is raised if the values are not
unique. The result is undefined in this case (there is a
value:key entry, but it is not defined which key gets
used). <P></DD>
<DT><CODE><FONT COLOR="#000099">
irange(object[,indices])
</FONT></CODE></DT>
<DD>
Builds a tuple of tuples
<CODE>(index,object[index])</CODE>. If a sequence
<CODE>indices</CODE> is given, the indices are read from
it. If not, then the index sequence defaults to
<CODE>trange(len(object))</CODE>.
<P>
Note that <CODE>object</CODE> can be any object that can
handle <CODE>object[index]</CODE>, e.g. lists, tuples,
string, dictionaries, even your own objects, if they
provide a __getitem__-method. This makes very nifty
constructions possible and extracting items from another
sequence becomes a piece of cake. Give it a try ! You'll
soon love this little function. <P></DD>
<DT><CODE><FONT COLOR="#000099">
ifilter(condition,object[,indices])
</FONT></CODE></DT>
<DD>
Builds a list of tuples <CODE>(index,object[index])</CODE>
such that <CODE>condition(object[index])</CODE> is true
and index is found in the sequence indices (defaulting to
<CODE>trange(len(object))</CODE>). Order is
preserved. condition must be a callable object.<P></DD>
<DT><CODE><FONT COLOR="#000099">
get(object,index[,default])
</FONT></CODE></DT>
<DD>
Returns <CODE>object[index]</CODE>, or, if that fails,
<CODE>default</CODE>. If <CODE>default</CODE> is not given
or the singleton <CODE>NotGiven</CODE> an error is raised
(the error produced by the object). <P></DD>
<DT><CODE><FONT COLOR="#000099">
extract(object,indices[,defaults])
</FONT></CODE></DT>
<DD>
Builds a list with entries <CODE>object[index]</CODE> for
each index in the sequence <CODE>indices</CODE>.
<P>
If a lookup fails and the sequence <CODE>defaults</CODE>
is given, then <CODE>defaults[nth_index]</CODE> is used,
where <CODE>nth_index</CODE> is the index of
<CODE>index</CODE> in <CODE>indices</CODE> (confused ?
it works as expected !). <CODE>defaults</CODE> should
have the same length as <CODE>indices</CODE>.
<P>
If you need the indices as well, try the
<CODE>irange</CODE> function. The function raises an
<CODE>IndexError</CODE> in case it can't find an entry
in indices or defaults. <P></DD>
<DT><CODE><FONT COLOR="#000099">
iremove(object,indices)
</FONT></CODE></DT>
<DD>
Removes the items indexed by indices from object.
<P>
This changes the object in place and thus is only
possible for mutable types.
<P>
For sequences the index list must be sorted ascending;
an <CODE>IndexError</CODE> will be raised otherwise (and
the object left in an undefined state). <P></DD>
<DT><CODE><FONT COLOR="#000099">
findattr(object_list,attrname)
</FONT></CODE></DT>
<DD>
Returns the first attribute with name
<CODE>attrname</CODE> found among the objects in the
list. Raises an <CODE>AttributeError</CODE> if the
attribute is not found. <P></DD>
<DT><CODE><FONT COLOR="#000099">
attrlist(object_list,attrname)
</FONT></CODE></DT>
<DD>
Returns a list of all attributes with name
<CODE>attrname</CODE> found among the objects in the
list. <P></DD>
<DT><CODE><FONT COLOR="#000099">
napply(number_of_calls,function[,args=(),kw={}])
</FONT></CODE></DT>
<DD>
Calls the given function <CODE>number_of_calls</CODE>
times with the same arguments and returns a tuple with the
return values. This is roughly equivalent to a for-loop
that repeatedly calls <CODE>apply(function,args,kw)</CODE>
and stores the return values in a tuple. Example: create
a tuple of 10 random integers... <CODE>l =
napply(10,whrandom.randint,(0,10)).</CODE><P></DD>
<DT><CODE><FONT COLOR="#000099">
mapply(callable_objects[,args=(),kw={}])
</FONT></CODE></DT>
<DD>
Creates a tuple of values by applying the given arguments
to each object in the sequence
<CODE>callable_objects</CODE>.
<P>
This function has a functionality dual to that of
<CODE>map()</CODE>. While <CODE>map()</CODE> applies
many different arguments to one callable object, this
function applies one set of arguments to many different
callable objects. <P></DD>
<DT><CODE><FONT COLOR="#000099">
method_mapply(objects,methodname[,args=(),kw={}])
</FONT></CODE></DT>
<DD>
Creates a tuple of values by applying the given arguments
to each object's <methodname> method. The objects
are processed as given in the sequence
<CODE>objects</CODE>.
<P>
A simple application is
e.g. <CODE>method_mapply([a,b,c],'method', (x,y))</CODE>
resulting in a tuple <CODE>(a.method(x,y),
b.method(x,y), c.method(x,y))</CODE>. Thanks to Aaron
Waters for suggesting this function. <P></DD>
<DT><CODE><FONT COLOR="#000099">
count(condition,sequence)
</FONT></CODE></DT>
<DD>
Counts the number of objects in sequence for which
condition returns true and returns the result as
integer. condition must be a callable object.<P></DD>
<DT><CODE><FONT COLOR="#000099">
exists(condition,sequence)
</FONT></CODE></DT>
<DD>
Return 1 if and only if condition is true for at least one
of the items in sequence and 0 otherwise. condition must
be a callable object. <P></DD>
<DT><CODE><FONT COLOR="#000099">
forall(condition,sequence)
</FONT></CODE></DT>
<DD>
Return 1 if and only if condition is true for all of the
items in sequence and 0 otherwise. condition must be a
callable object. <P></DD>
<DT><CODE><FONT COLOR="#000099">
index(condition,sequence)
</FONT></CODE></DT>
<DD>
Return the index of the first item for which condition is
true. A <CODE>ValueError</CODE> is raised in case no item
is found. condition must be a callable object. <P></DD>
<DT><CODE><FONT COLOR="#000099">
sizeof(object)
</FONT></CODE></DT>
<DD>
Returns the number of bytes allocated for the given Python
object. Additional space allocated by the object and
stored in pointers is not taken into account (though the
pointer itself is). If the object defines tp_itemsize in
its type object then it is assumed to be a variable size
object and the size is adjusted accordingly. <P></DD>
<DT><CODE><FONT COLOR="#000099">
acquire(object,name)
</FONT></CODE></DT>
<DD>
Looks up the attribute name in object.baseobj and returns
the result. If object does not have an attribute 'baseobj'
or that attribute is None or the attribute name starts
with an underscore, an AttributeError is raised.
<P>
This function can be used as __getattr__ hook in Python
classes to enable implicit acquisition along a
predefined lookup chain (object.baseobj provides a way
to set up this chain). See Examples/Acquistion.py for
some sample code. <P></DD>
<DT><CODE><FONT COLOR="#000099">
defined(name)
</FONT></CODE></DT>
<DD>
Returns true iff a symbol name is defined in the current
namespace.
<P>
The function has intimate knowledge about how symbol
resolution works in Python: it first looks in locals(),
then in globals() and if that fails in
__builtins__. <P></DD>
<DT><CODE><FONT COLOR="#000099">
reval(codestring[,locals={}])
</FONT></CODE></DT>
<DD>
Evaluates the given codestring in a restricted environment
that only allows access to operators and basic type
constructors like (), [] and {}.
<P>
No builtins are available for the evaluation. locals
can be given as local namespace to use when evaluating
the codestring.
<P>
After a suggestion by Tim Peters on
comp.lang.python.
<P></DD>
<DT><CODE><FONT COLOR="#000099">
truth(object)
</FONT></CODE></DT>
<DD>
Returns the truth value of object as truth singleton (True or
False). Note that the singletons are ordinary Python integers
1 and 0, so you can also use them in calculations.
<P>
This function is different from the one in the
<CODE>operator</CODE> module: the function does not
return truth singletons but integers.
<P></DD>
<DT><CODE><FONT COLOR="#000099">
sign(object)
</FONT></CODE></DT>
<DD>
Returns the signum of object interpreted as number,
i.e. -1 for negative numbers, +1 for positive ones and 0
in case it is equal to 0. The method used is equivalent to
cmp(object,-object).
<P></DD>
</DL>
<P>
A note on the naming scheme used:
<UL>
<LI><CODE>i</CODE> stands for indexed, meaning that you have
access to indices
<LI><CODE>m</CODE> stands for multi, meaning that processing
involves multiple objects
<LI><CODE>n</CODE> stands for n-times, e.g. a function is
executed a certain number of times
<LI><CODE>t</CODE> stands for tuple
<LI><CODE>x</CODE> stands for lazy evaluation
</UL>
<P>
Since this is (and will always be) work-in-progress, more
functions will eventually turn up in this module, so
stopping by every now and then is not a bad idea
<TT>:-)</TT>.
<P>
</UL><!--CLASS="indent"-->
<A NAME="BuiltinObjects">
<H3>New Builtin Objects</H3>
<UL CLASS="indent">
<P>
These objects are available after importing the package:
<P><DL>
<DT><CODE><FONT COLOR="#000099">
xmap(func, seq, [seq, seq, ...])
</FONT></CODE></DT>
<DD>
Constructs a new xmap object emulating <CODE>map(func,
seq, [seq, seq, ...])</CODE>.
<P>
The object behaves like a list, but evaluation of the
function is postponed until a specific element from the list
is requested. Unlike map, xmap can handle sequences not
having a __len__ method defined (due to the
evaluation-on-demand feature).
<P>The <CODE>xmap</CODE> objects define one method:
<P><DL>
<DT><CODE><FONT COLOR="#000099">
tolist()
</FONT></CODE></DT>
<DD>Return the whole list giving the same result as the
emulated map()-construct. <P></DD>
</DL>
<P>
This object is a contribution by Christopher Tavares (see
xmap.c for his email address). I am providing this extension
AS-IS, since I haven't had time to adapt it to my coding
style.
<P></DD>
<DT><CODE><FONT COLOR="#000099">
NotGiven
</FONT></CODE></DT>
<DD>
This is a singleton similar to <CODE>None</CODE>. Its main
purpose is providing a way to indicate that a keyword was not
given in a call to a keyword capable function, e.g.
<PRE><FONT COLOR="#000066">import mx.Tools.NewBuiltins
def f(a,b=4,c=NotGiven,d=''):
if c is NotGiven:
return a / b, d
else:
return a*b + c, d
</FONT></PRE>
<P>
It is also considered false in <CODE>if</CODE>-statements, e.g.
<PRE><FONT COLOR="#000066">import mx.Tools.NewBuiltins
a = NotGiven
# ...init a conditionally...
if not a:
print 'a was not given as value'
</FONT></PRE>
<P></DD>
<DT><CODE><FONT COLOR="#000099">
True, False
</FONT></CODE></DT>
<DD>
These two singletons are used by Python internally to
express the boolean values true and false. They represent
Python integer objects for 1 and 0 resp. All explicit
comparisons return these singletons, e.g. <CODE>(1==1) is
True</CODE> and <CODE>(1==0) is False</CODE>.
<P></DD>
</DL>
</UL><!--CLASS="indent"-->
<A NAME="SysFunctions">
<H3>New sys-Module Functions</H3>
<UL CLASS="indent">
<P>
The following functions are installed as add-ons to the
builtin <TT>sys</TT> module.
<P><DL>
<DT><CODE><FONT COLOR="#003399">
sys.verbosity([level])
</FONT></CODE></DT>
<DD>
If level is given, the value of the interpreter's
verbosity flag is set to level and the previous value of
that flag is returned. Otherwise, the current value is
returned.
<P>
You can use this function to e.g. enable verborse lookup
output to stderr for import statements even when the
interpreter was not invoked with '-v' or '-vv' switch or
to force verbosity to be switched off. <P></DD>
<DT><CODE><FONT COLOR="#003399">
sys.debugging([level])
</FONT></CODE></DT>
<DD>
If level is given, the value of the interpreter's
debugging flag is set to level and the previous value of
that flag is returned. Otherwise, the current value is
returned.
<P>
You can use this function to check whether the
interpreter was called with '-d' flag or not. Some
extensions use this flag to enable/disable debugging log
output (e.g. all the <A
HREF="http://www.egenix.com/files/python/eGenix-mx-Extensions.html">eGenix.com
mx Extensions</A>).<P></DD>
<DT><CODE><FONT COLOR="#000099">
sys.optimization([level])
</FONT></CODE></DT>
<DD>
If level is given, the value of the interpreter's
optimization flag is set to level and the previous value
of that flag is returned. Otherwise, the current value is
returned.
<P>
You can use this function to e.g. compile Python scripts
in optimized mode even though the interpreter was not
started with -O. <P></DD>
<DT><CODE><FONT COLOR="#000099">
sys.cur_frame([offset=0])
</FONT></CODE></DT>
<DD>
Return the current execution frame. If level is given,
the returned frame is taken from offset levels up the
execution stack. None is returned in case the frame is
not found, i.e. there are not enough frames on the stack.
<P>
Note: Storing the execution frame in a local variable
introduces a circular reference, since the locals and
globals are referenced in the execution frame, so use
the return value with caution. <P></DD>
<DT><CODE><FONT COLOR="#000099">
sys.makeref(id)
</FONT></CODE></DT>
<DD>
Provided that id is a valid address of a Python object
(<TT>id(object)</TT> returns this address), this function
returns a new reference to it. Only objects that are
"alive" can be referenced this way, ones with zero
reference count cause an exception to be raised.
<P>
You can use this function to reaccess objects lost
during garbage collection.
<P>
<I>USE WITH CARE:</I> this is an expert-only function
since it can cause instant core dumps and many other
strange things -- even ruin your system if you don't
know what you're doing !
<P>
<I>SECURITY WARNING:</I> This function can provide you
with access to objects that are otherwise not visible,
e.g. in restricted mode, and thus be a potential
security hole.
<P></DD>
</DL>
<P>
</UL><!--CLASS="indent"-->
<A NAME="Functions">
<H3>mx.Tools Functions</H3>
<UL CLASS="indent">
<P>
The following functions are not installed in any builtin
module. Instead, you have to reference them via the
<TT>mx.Tools</TT> module.
<P><DL>
<DT><CODE><FONT COLOR="#003399">
mx.Tools.verscmp(a,b)
</FONT></CODE></DT>
<DD>
Compares two version strings and returns a cmp() function
compatible value (<,==,> 0). The function is useful
for sorting lists containing version strings.
<P>
The logic used is as follows: the strings are compared
at each level, empty levels defaulting to '0', numbers
with attached strings (e.g. '1a1') compare less than
numbers without attachement (e.g. '1a1' < '1).
<P></DD>
<DT><CODE><FONT COLOR="#003399">
mx.Tools.dictscan(dictobj[,prevposition=0])
</FONT></CODE></DT>
<DD>
Dictionary scanner.
<P>
Returns a tuple (key,value,position) containing the
key,value pair and slot position of the next item found
in the dictionaries hash table after slot
prevposition.
<P>
Raises an IndexError when the end of the table is
reached or the prevposition index is out of range.
<P>
Note that the dictionary scanner does <I>not</I> produce
an items list. It provides a very memory efficient way
of iterating over large dictionaries. <P></DD>
<DT><CODE><FONT COLOR="#003399">
mx.Tools.srange(string)
</FONT></CODE></DT>
<DD>
Converts a textual representation of integer numbers and
ranges to a Python list.
<P>
Supported formats: "2,3,4,2-10,-1 - -3, 5 - -2"
<P>
Values are appended to the created list in the order
specified in the string. <P></DD>
<DT><CODE><FONT COLOR="#003399">
mx.Tools.fqhostname(hostname=None, ip=None)
</FONT></CODE></DT>
<DD>
Tries to return the fully qualified (hostname, ip) for the
given hostname.
<P>
If hostname is None, the default name of the local host
is chosen. ip then defaults to '127.0.0.1' if not given.
<P>
The function modifies the input data according to what
it finds using the socket module. If that doesn't work
the input data is returned unchanged.<P></DD>
<DT><CODE><FONT COLOR="#003399">
mx.Tools.username(default='')
</FONT></CODE></DT>
<DD>
Return the user name of the user running the current process.
<P>
If no user name can be determined, default is returned.
<P></DD>
<DT><CODE><FONT COLOR="#003399">
mx.Tools.scanfiles(files, dir=None, levels=0, filefilter=None)
</FONT></CODE></DT>
<DD>
Build a list of filenames starting with the filenames and
directories given in files.
<P>
The filenames in are made absolute relative to dir. dir
defaults to the current working directory if not given.
<P>
If levels is greater than 0, directories in the files
list are recursed into up the given number of levels.
<P>
If filefilter is given, as re match object, then all
filenames (the absolute names) are matched against
it. Filenames which do not match the criteria are
removed from the list.
<P>
Note that directories are not included in the resulting
list. All filenames are non-directories.
<P>
If no user name can be determined, default is returned.
<P></DD>
</DL>
<P>
</UL><!--CLASS="indent"-->
<A NAME="Objects">
<H3>mx.Tools Objects</H3>
<UL CLASS="indent">
<P>
The following objects are not installed in any builtin
module. Instead, you have to reference them via the
<TT>mx.Tools</TT> module.
<P><DL>
<DT><CODE><FONT COLOR="#003399">
mx.Tools.DictScan(dictionary)
</FONT></CODE></DT>
<DD>
Creates a forward iterator for the given dictionary. It is
based on mx.Tools.dictscan().
<P>
The dictionary scanner does <I>not</I> produce an items
list. It provides a very memory efficient way of
iterating over large dictionaries.
<P>
Note that no precaution is taken to insure that the
dictionary is not modified in-between calls to the
__getitem__ method. It is the user's responsibility to
ensure that the dictionary is neither modified, nor
changed in size, since this would result in skipping
entries or double occurance of items in the scan.
<P>
The iterator inherits all methods from the underlying
dictionary for convenience.
<P>
The returned object inherits all methods from the
underlying dictionary and additionally provides the
following methods:
<P><DL>
<DT><CODE><FONT COLOR="#000099">
reset()
</FONT></CODE></DT>
<DD>Resets the iterator to its initial position.
<P></DD>
</DL>
<P></DD>
<DT><CODE><FONT COLOR="#003399">
mx.Tools.DictItems(dictionary)
</FONT></CODE></DT>
<DD>
Is an alias for mx.Tools.DictScan.
<P></DD>
</DL>
<P>
</UL><!--CLASS="indent"-->
<A NAME="Examples">
<H3>Examples of Use</H3>
<UL CLASS="indent">
<P>
A few simple examples:<PRE><FONT COLOR="#000066">import mx.Tools.NewBuiltins
sequence = range(100)
# In place calculations:
for i,item in irange(sequence):
sequence[i] = 2*item
# Get all odd-indexed items from a sequence:
odds = extract(sequence,trange(0,len(sequence),2))
# Turn a tuple of lists into a list of tuples:
chars = 'abcdefghji'
ords = map(ord,chars)
table = tuples(chars,ords)
# The same as dictionary:
chr2ord = dict(table)
# Inverse mapping:
ord2chr = invdict(chr2ord)
# Range checking:
if exists( lambda x: x > 10, sequence ):
print 'Warning: Big sequence elements!'
# Handle special cases:
if forall( lambda x: x > 0, sequence ):
print 'Positive sequence'
else:
print 'Index %i loses' % (index( lambda x: x <= 0, sequence ),)
# dict.get functionality for e.g. lists:
print get(sequence,101,"Don't have an element with index 101")
# Filtering away false entries of a list:
print filter(truth,[1,2,3,0,'',None,NotGiven,4,5,6])
</FONT></PRE>
<P>
More elaborate examples can be found in the Examples/
subdirectory of the package.
</UL><!--CLASS="indent"-->
<A NAME="Structure">
<H3>Package Structure</H3>
<UL CLASS="indent">
<PRE>
[Tools]
Doc/
[Examples]
Acquisition.py
[mxTools]
vc5/
bench1.py
bench2.py
hack.py
test.py
NewBuiltins.py
Tools.py
</PRE>
<P>
Entries enclosed in brackets are packages (i.e. they are
directories that include a <TT>__init__.py</TT> file) or
submodules. Ones with slashes are just ordinary
subdirectories that are not accessible via
<CODE>import</CODE>.
<P>
<U>Note</U>: Importing <CODE>mx.Tools</CODE> will
automatically install the functions and objects defined in
this package as builtins. They are then available in all
other modules without having to import then again every
time. If you don't want this feature, you can turn it off in
<TT>mx/Tools/__init__.py</TT>.
<P>
</UL><!--CLASS="indent"-->
<A NAME="Support">
<H3>Support</H3>
<UL CLASS="indent">
<P>
eGenix.com is providing commercial support for this
package. If you are interested in receiving information
about this service please see the <A
HREF="http://www.egenix.com/files/python/eGenix-mx-Extensions.html#Support">eGenix.com
Support Conditions</A>.
</UL><!--CLASS="indent"-->
<A NAME="Copyright">
<H3>Copyright & License</H3>
<UL CLASS="indent">
<P>
© 1997-2000, Copyright by Marc-André Lemburg;
All Rights Reserved. mailto: <A
HREF="mailto:mal@lemburg.com">mal@lemburg.com</A>
<P>
© 2000-2001, Copyright by eGenix.com Software GmbH,
Langenfeld, Germany; All Rights Reserved. mailto: <A
HREF="mailto:info@egenix.com">info@egenix.com</A>
<P>
This software is covered by the <A
HREF="mxLicense.html#Public"><B>eGenix.com Public
License Agreement</B></A>. The text of the license is also
included as file "LICENSE" in the package's main directory.
<P>
<B> By downloading, copying, installing or otherwise using
the software, you agree to be bound by the terms and
conditions of the eGenix.com Public License
Agreement. </B>
</UL><!--CLASS="indent"-->
<A NAME="History">
<H3>History & Future</H3>
<UL CLASS="indent">
<P>Things that still need to be done:
<P><UL>
<LI> Implement a generic join() builtin: <BR>
join((a,b,c),sep) := (((a + sep) + b) + sep) + c<BR> with
optimizations for sequences of strings, unicode objects,
lists and tuples (e.g. join(((1,2),(3,4),(0,))) gives
(1,2,0,3,4)).
<P>
<LI> Provide some more examples.
<LI> Add Neil S.'s repeat module (see private dir).
<LI> Make dict(dict) return a copy of the dictionary
just like list(list) returns a copy of the list.
</UL>
<P>Changes from 2.0.0 to 2.0.3:
<P>
<UL>
<LI> Removed config.h include from xmap.c -- this was never
really needed and causes problems with Python 2.2. Thanks to
Gerhard Hring for finding this one.
</UL>
<P>Changes from <A HREF="mxTools-1.0.0.zip">1.0.0</A> to 2.0.0:
<P>
<UL>
<LI>Added truth(;-).
<P><LI>Added VC5 project files donated by Darrell Gallion.
<P><LI>Added sys.debugging() and sign().
<P><LI><B>Moved</B> the package under a new top-level
package 'mx' and <B>renamed</B> it to mx.Tools. It is part
of the <I>eGenix.com mx BASE distribution</I>.
<P><LI>Added mx.Tools.verscmp().
<P><LI>Added mx.Tools.dictscan() and mx.Tools.DictScan class.
<P><LI>Added mx.Tools.scanfiles().
</UL>
<P>Changes from <A HREF="mxTools-0.9.1.zip">0.9.1</A> to 1.0.0:
<P>
<UL>
<LI>Added defined().
<P><LI><B>Moved</B> the two functions verbosity() and
optimization() to the sys module and added enhanced
custimization code to NewBuiltins/NewBuiltins.py. You can
now define where the functions and objets are installed by
editing that file.
<P><LI><B>Important change:</B> Cleaned up the packages
offerings in that all old function names are now disabled
by default. You can edit NewBuiltins/NewBuiltins.py to
reenable them without the need to recompile.
<P><LI>Added sys.cur_frame() and iremove().
<P><LI>Added Macintosh precompiled binaries for PowerMacs
donated by Joseph Strout. They are included in a StuffIt
file in the mxTools subdir.
<P><LI>Added True and False singletons.
</UL>
<P>Changes from <A HREF="mxTools-0.9.0.zip">0.9.0</A> to 0.9.1:
<P>
<UL>
<LI>Added optimization() (XXX should probably go into sys
rather than __builtins__ just as verbosity()).
<P><LI>Added new PYD files provided by David
Ascher. Thanks Dave :-)
<P><LI>Added NotGiven singleton.
</UL>
<P>Changes from <A HREF="mxTools-0.8.zip">0.8.1</A> to 0.9.0:
<P>
<UL>
<LI>Added acquire() and a some sample code for its usage in
Examples/Acquisition.py.
<P><LI><B>Renamed mget()</B> to extract(). The old
function is aliased to mget, so this should not break any
existing code.
<P><LI><B>tuples()</B> now is a single argument function,
just as lists(): passing tuples() a single tuple will
cause it to interpret the tuple as argument tuple,
e.g. tuples((a,b)) gets interpreted as tuples(a,b). Note
that this causes the semantics of tuples(a) (called with
only one sequence) to change !!!
<P><LI>Added setdict() and attrlist().
<P><LI>Added verbosity().
</UL>
<P>Changes from <A HREF="mxTools-0.7.zip">0.7</A> to 0.8:
<P>
<UL>
<LI>Version 0.8.1: Fixed a bug that caused sizeof(),
reverse() and invdict() to dump core when called without
argument.
<P><LI>Fixed a bug in forall() that caused it to fail. Found by
Henk Jansen.
<P><LI>Added index(). Contributed by Henk Jansen (TU Delft).
<P><LI>Added tests for forall(), exists(), count() and index()
to the test script.
</UL>
<P>Changes from <A HREF="mxTools-0.6.zip">0.6</A> to 0.7:
<P>
<UL>
<LI><B>Renamed mgetattr()</B> to findattr().
The old function name still works, but it will either be
removed in the near future or replaced with another
functionality.
<P><LI>Changed the way list creation works. This should speed
up all functions from the package which create and return
lists.
<P><LI>Fixed a serious memory leakage in dict().
<P><LI>Fixed a bug in get().
<P><LI>Added lists().
<P><LI>Made many functions taking only one argument use a simpler
calling mechanism. Note that passing more than one argument
to these functions results in the "multiple" arguments being
seen as a tuple, e.g. sizeof(1,2) is the same as calling
sizeof((1,2)). Note that there are some other methods in core
Python that work in the same way, e.g. list.append(1,2) really
does a list.append((1,2)). I find this convenient at times.
<P><LI><B>Renamed trange_len()</B> to indices() (easier to
write and intuitive enough to use, e.g. in 'for i in
indices(obj):...'). The old function name still works, but
it will be removed in the near future.
</UL>
<P>Changes from <A HREF="mxTools-0.5.zip">0.5</A> to 0.6:
<P>
<UL>
<LI>Added dict().
<P><LI>Added invdict().
<P><LI>Added tuples().
<P><LI>Added reverse().
</UL>
<P>Changes from <A HREF="mxTools-0.4.zip">0.4</A> to 0.5:
<P>
<UL>
<LI>Added get() and mget().
<P><LI>Added sizeof().
<P><LI>Added mgetattr().
</UL>
<P>Changes from <A HREF="mxTools-0.3.zip">0.3</A> to 0.4:
<P>
<UL>
<LI>Converted the module into a package called
'NewBuiltins'. Importing it installs all of the functions
defined in the C extension modules as builtins.
<P><LI>Fixed a few memory leaks.
<P><LI>Added method_mapply().
<P><LI>Added Christopher Tavares' xmap module.
</UL>
<P>
</UL><!--CLASS="indent"-->
<HR WIDTH="100%">
<CENTER><FONT SIZE=-1>
<P>
© 1997-2000, Copyright by Marc-André Lemburg;
All Rights Reserved. mailto: <A
HREF="mailto:mal@lemburg.com">mal@lemburg.com</A>
<P>
© 2000-2001, Copyright by eGenix.com Software GmbH;
All Rights Reserved. mailto: <A
HREF="mailto:info@egenix.com">info@egenix.com</A>
</FONT></CENTER>
</FONT></CENTER>
</BODY>
</HTML>
|