the good thing is that the cartesian product is indexable, not like that of itertools.product (but the multiplicands must be sequences, not iterators). Second display took 0.109297410704 secondsĪ custom list that when multiplied by other list returns a cartesian product. With my not very fast computer, I got: first display took 4.94626048841 seconds Print '\n'.join(str(i) ' ' repr(line)įor i,line in enumerate(ch.splitlines(1)) ) Print "\n\nIn 3 seconds, I will print the same content, using ' These four "tricks" combined in one snippet very useful to rapidly display the code source of a web page, line after line, each line being numbered, all the special characters like '\t' or newlines being not interpreted, and with the newlines present: import urllibįor i,line in enumerate(ch.splitlines(1)): Splitlines(1) with an argument keeps the newlines I was very satisfied to discover that print '\n'.join(list_of_strings) is displayed much more rapidly with '\n'.join(.) than for ch in list_of_strings: print ch I sang a praise to Python when I discovered repr() that gave me possibility to see precisely the content of strings that I wanted to analyse with a regex Like another person above, I said 'Wooww !!' when I discovered enumerate() Print "\nTotal: %s lines (%s)" %(sum( for x in loclist]), cur_path) Print "d lines in %s" % (linenumbercount, filename) Loclist.append( ( len(open(totalpath, "r").read().splitlines()),įor linenumbercount, filename in loclist: If pyfile.endswith(".py") and pyfile not in ignore_set: Ignore_set = set()įor pydir, _, pyfiles in os.walk(cur_path): I'll start of with a short snippet i use from time to time to count sloc (source lines of code) in python projects: # prints recursive count of lines of python source code from current directory Lines maybe?) and give only one example per post. In spirit of the existing "what's your most useful C/C snippet" - thread:ĭo you guys have short, monofunctional Python snippets that you use (often) and would like to share with the StackOverlow Community? Please keep the entries small (under 25
0 Comments
Leave a Reply. |