Blog by Sumana Harihareswara, Changeset founder
Top, Iterators and Generators, and Git, Emacs, and REPL Tips
Hi, reader. I wrote this in 2013 and it's now more than five years old. So it may be very out of date; the world, and I, have changed a lot since I wrote it! I'm keeping this up for historical archive purposes, but the me of today may 100% disagree with what I said then. I rarely edit posts after publishing them, but if I do, I usually leave a note in italics to mark the edit and the reason. If this post is particularly offensive or breaches someone's privacy, please contact me.
Dumping into a post some things I've learned recently, trying to disregard the potential "you didn't know that already?!?!" surprise, feigned or genuine, that people might impose on me.*
>>> a = [3,6,9] >>> a [3, 6, 9] >>> iter(a) <listiterator object at 0x7f5c8c8da490> >>> s = iter(a) >>> type(s) <type 'listiterator'> >>> s.next() 3 >>> s.next() 6 >>> s.next() 9 >>> s.next() Traceback (most recent call last): File "<stdin>", line 1, in <module> StopIteration >>> r = "captain" >>> w = iter(r) >>> w.next() 'c' >>> w.next() 'a' >>> w.next() 'p' >>> w.next() 't'
>>> def foo(): ... yield "first" ... yield "second" ... yield "last" ... >>> b = foo() >>> type(b) <type 'generator'> >>> b.next() 'first' >>> b.next() 'second' >>> b.next() 'last' >>> b.next() Traceback (most recent call last): File "<stdin>", line 1, in <module> StopIteration
I read (bits of) so very very many pages about this, and fellow students tried to help me get this (thank you, Joe and Gideon), and then yesterday I paired with Jessica McKellar and she sealed the deal and I think I get it now! As she explained, you might want to use generators if you want to get an infinite series of values (e.g., all the even numbers up to infinity). Or if you're crunching numbers and it takes hella resources to do this particular part of the crunching on THE WHOLE DATASET ALL AT ONCE, with generators you can just crunch one input at a time and yield it up, then move to the next input in the sequence seamlessly when needed. You can speed up bottlenecks in your assembly line by doing particular computations in a just-in-time way.
Comments
Tikitu
http://ww.logophile.org/
06 Nov 2013, 13:59 p.m.
C. Scott Ananian
http://cscott.net
06 Nov 2013, 14:58 p.m.
For git color options, "auto" is usually a better bet than "true" -- that makes sure that "git grep foo " doesn't get a bunch of random color escape characters in it.
I have the following in my ~/.gitconfig:
[color]
diff = auto
log = auto
commit = auto
branch = auto
[log]
decorate = short
[rebase]
autosquash = true<br/>
Avram
http://grumer.org/
06 Nov 2013, 22:19 p.m.
Usually when I run top, my #1 CPU-occupying process is top.
Avram
http://grumer.org/
06 Nov 2013, 22:21 p.m.
(Nooooo, the comments form ate my <kbd> tags!)
Jed
http://www.kith.org/journals/jed
11 Nov 2013, 12:36 p.m.
Nifty! Thanks for posting these!
I didn't know about most of them. I had encountered integrators in other contexts (in Java, I think), but had had to figure out what they were and how they worked from context; neat to see it laid out so simply and clearly. And I hadn't heard of generators before; super-cool. Can you also use a generator to create an iterator that iterates in nonlinear order, like random order or alphabetical order or something? If I'm understanding right, it sounds related to the concept of implementing your own comparison functions for use by sort functions.
Good stuff! If you like top you might also like htop (same idea but prettier and easier to interact with) and iotop (same idea but for "what is churning my disk all of a sudden?!"). And ack-grep might be useful too: a non-git-specific semi-equivalent to git grep.