Python 3 Iterable Unpacking Catch-all

I’m one of those terrible people that still haven’t switched over to using Python3 …just yet. But one thing that pleasantly surprised me recently was learning of the “new” starred expression catch-all that can be used when unpacking iterables. This lets you use up to one starred expression to receive the rest of the items of an iterable during an unpacking assignment.

full_name = "Steven W J Brown"

#python2
names             = full_name.split()                # ['Steven', 'W', 'J', 'Brown']
first, last       = names[0], names[-1]              # 2.A: ignore middle names
first, mids, last = names[0], names[1:-1], names[-1] # 2.B: store middle names

#python3, 
first, *_, last    = full_name.split() # 3.A: ignore middle names
first, *mids, last = full_name.split() # 3.B: store middle names

It’s easy to read, eliminates the need for an intermediary variable and makes the code cleaner. And it can be used in loops. More details here: PEP 3132. Love it.

I really have to start re-configuring my environments for python3….

Org-mode: Indirect Buffer, Narrowing to tree

Here’s a really quick Emacs Org-mode tip that I find extremely useful.

In your config add the following two lines:

(setq org-indirect-buffer-display 'new-frame)
(setq org-src-window-setup 'other-frame)

(Or just run it in your scratch buffer to try it out for your current session.)

The first line sets the org-indirect-buffer-display variable to new-frame. Now when you call org-tree-to-indirect-buffer or org-agenda-tree-to-indirect-buffer (C-c C-x b) from an Org buffer or an Org agenda item, Emacs will create a new frame narrowed to that Org sub-tree. I find this much more useful than the default other-window as I usually operate with one frame vertically split with two windows, and like to maintain manual control over the contents of the windows.

Similarly, I like a frame-focused behaviour when editing literal source blocks, which is what the second line sets. While in an Org source block, calling org-edit-special (C-c ' ) will open a new frame narrowed to the current source block and in the appropriate major mode (python-mode, for example). The variable for this setting is org-src-window-setup.

To see the documentation for either variables, use the Emacs help system! (C-h v variable-name)

See also: Emacs Indirect-Buffers, Emacs Buffer Narrowing, org-narrow-to-subtree

Emacs and Org-mode

Well this is odd. I’ve returned to a text editor I was introduced to in university, but wasn’t really taken with until just recently. The editor is Emacs and the reason is org-mode. Apparently, my story is not all that uncommon and org-mode (with evil mode) has even converted many Vim users.

Emacs has been around since 1976. (Nineteen-seventy-six! And it’s still actively developed! That’s insane!) Org-mode is an Emacs extension (major-mode) that has been in development since 2003 and included in Emacs core since 2006. Org-mode is basically an everything-mode; it allows for really intuitive outlines and section management, source code blocks, inline evaluation of those blocks and literal programming, comprehensive exporting, intra- and inter-document hyperlinking, tables/spreadsheets, presentations, ….the list goes on, and on, and on. And on.

The Boston Emacs Meetup has quite a few good Emacs videos with live demonstrations, and I really enjoyed this one, by Harry Schwartz:

(YouTube: Getting Started With Org Mode)

As I continue to migrate all-the-things over to Emacs and Org-mode, it made me think about why I didn’t put more time into learning Emacs before.

When I was introduced to Emacs in university (somewhere around 2002 or 2003), org-mode was not included in Emacs and relatively young. At that time, Java was being promoted heavily in education, and for programming Java, it makes the most sense to use a Java IDE such as Eclipse or NetBeans which make the verbose language far more tolerable. So that’s generally what I did. But for my C or C++ programming, and general text file editing, I usually chose Emacs over Vi(m) due to it being more similar to what I was used to – but that’s it. I could move about in Emacs without leaving home-row, and I could make a few basic changes in VIM, save, and quit without panicking1. I could have been using Notepad++ and would have been just as happy. I was not taking advantage of either Emacs or Vim.

I never learned about org-mode until roughly 2 years ago (somewhere between June and August, 2016). Since then, I’ve gradually migrated as-much-as-I-fucking-can to these wonderful plain text files. I manage my work projects and todo lists. My agenda. My emacs config file. My blog(s); this post (via org2blog). I write documentation that can be exported into a variety of formats. And I keep learning new things; both for Emacs and for org-mode. Just recently, the org-mode time tracking finally clicked for me. I now schedule my days better and try to track where my time goes. This is extremely useful in evaluating what’s eating up my time, and estimating how long tasks will take so I can plan better.

2018-08-12_emacs.png

The draw of org-mode has also made me appreciate Emacs, of course. I connect to Jupyter notebooks via ein and work on them with conveniences not offered in the browser interface2. I’ve heavily customized my environment, picked up a bit of elisp, and I’ve started tweeking my own theme. I recently moved from primarily using Linux to primarily using Windows, which has presented a few challenges with Emacs’ setup, but it’s helped me develop a consistent environment between work and home.

Both Emacs and org-mode are powerful but take a significant time investment to configure and use effectively. Spacemacs, a distribution of Emacs that comes heavily pre-configured and tries to combine the best of Emacs and Vim, offers a simple way to see what an advanced setup could look like. It’s not how I started, and some consider it too bloated, but it’s very well put together and certainly worth looking at.

Anyway, I’ll probably be reviving this blog with the odd org-mode or Emacs post, as that is what I’m playing with, these days. But in addition to the video above, a good intro would be Carsten Dominik (org-mode’s creator) presentation at Google:

YouTube: Emacs Org-mode – a system for note-taking and project planning

Footnotes:

1

I have a great appreciation for Vim’s modal editing and language. Tempted to use Evil Mode, but I have enough things on my plate, at the moment. ๐Ÿ™‚

2

I want to investigate moving my Jupyter notebook products to org-mode files under Git, only exporting them to notebooks, when needed… but that’s probably a separate post.

Script: Wireless Strength Polling/Logging/Graphing

Initially, I wrote this script to give me frequent feedback on the signal strength. This is useful when adjusting antennas to that sweet spot that give stronger signals; especially if you’re testing some homemade tinfoil parabolic reflectors! ๐Ÿ˜‰ If you have a portable wireless device, like a netbook, you can ssh into your (wireless) desktop and run wireless-strength to get realtime feedback on adjustments to the access point’s antenna… assuming the connection doesn’t break. ๐Ÿ˜› And, of course, you can just walk around running it on your mobile device to create a kind of wireless heatmap.

If you have gnuplot, you can also generate graphs from the data with ws-plot. This is me walking around my house with my notebook:

I started in my room (40% ๐Ÿ™ ), which is where the first peak is – near the window. Left my room, back to 40%, peak near window again, then bathroom… 40%. The climb from 40-80% is me walking towards the TV room (PS3 lives in a solid 80% zone, at least!). Walked upstairs, got 100% in most areas (that’s where the Access Point is) – tried a bedroom, dropped to 40%.

Download the scripts and get more details here: https://github.com/izm/wireless-strength

Nerdy Ramblings

This script originated years back, but I recently tried using it on my laptop and it didn’t work! Unacceptable! The original parsed the output of iwconfig. But what showed up as “Quality=30/70” on my desktop, would show up as “Quality:4” on my laptop. COMPLETELY DIFFERENT. Even the character after “Quality” was different! o.O The output of iwconfig is driver dependent, which is why I looked to network-manager. I figured there’s probably a nice dbus command I can send to network-manager for that purpose – but I got tired of looking and decided to just parse output again. :/ Luckily, network-manager includes nm-tool. It’s certainly not a clean solution, but it works for now.

I also used updating rewriting the script as an excuse to get better acquainted with git – which I’m really liking.

Every time I do a bash script, I vow to do the next script in Python. I like Python and I don’t like Bash… but there’s a certain… nativeness or dependency-free elegance to bash scripts. Still, I hate writing them, and the next script’s in Python! ๐Ÿ˜›

Theme updates

Made some adjustments to my blog theme.

Old

New

  • search box graphic and placeholder text
  • added jetpack subscriptions module and some css to handle it
  • gave page a max width so it doesn’t look ridiculous on wide screen monitors.
  • sidebar: removed dotted borders added white space, toned down colours – reduced noise
  • Removed grey post-meta-content (categories, tags) boxes. Put all post meta data in TOP, with comments repeated at bottom. (Similar to when I started this theme!)
  • Added hover highlight to gallery images (now consistent with image links not in a gallery). Cleaned up some of the code (to not generate HTML comments) and random fixes here and there.
  • Removed lots of black. Too harsh.

You might have to click refresh. And you might not even notice any changes. ๐Ÿ˜›

Feel free to leave a comment or use Markup to make suggestions. Markup’s a pretty cool tool.

It’s interesting to compare the progression of my themes. Well, interesting for me, at least. ๐Ÿ™‚

I think the next time I decide to work on a theme, I will start from scratch. Clean slate. Make it more consistent with my root page.

On a related note, the latest version of WordPress, 3.3.2, is really nice.

Happy Mother’s Day 2012!

It was Mother’s Day! I love cooking, so I made something special for my mom. Actually, me and 2 of my sisters were the kitchen workers today. I made a salad. So I know my mom likes this shrimp and avocado salad that is served at the restaurant where I went to culinary school. Fine. I’ll make that. Again. At home. For her. ๐Ÿ™‚

This is how it turned out:

This was a sample plate I did. I changed the plating a bit… moved the tomatoes face-down on the cucumbers.. and added a full steamed prawn. But it looked something like this.

It was good! If I were to do it again, I’d play around with the plating some more… and I’d make the balsamic reduction thicker (when I tried to spell “Mom” in cursive on my mom’s plate, it gradually expanded until you couldn’t read it…. >.< ). But it turned out pretty good. But the chive oil… oh man… is it ever perdy. Put it in a custard cup in some good light: sexy.

That other thing in the photo is dry whole wheat pasta – another experiment. ^_^

Happy Mother’s Day!

Dessert

After hurting my shoulder, I had to take a 2 month break from culinary school. Hopefully, I’m fit for the kitchen again, because I go back to school tomorrow. Better start doing more cooking!

Last night, I had company coming that I knew liked dessert. There wasn’t going to be many of us, so I decided to make crรจme brรปlรฉe, a classic French dessert.

Turned out good, but I guess I could have caramelized them a bit more.

Then I had the dilemma of what to do with the egg whites. I’ve seen a few Australian cooking shows recently, and pavlova is something that is always mentioned and held in high regard. So I decided to try it.

I guess it turned out okay. No weeping at the top, but the bottom had a bit of syrup weeping out. I’m thinking it may have been slightly undercooked. It deflated a bit as it was cooling, but that’s normal, apparently.

All pavlova recipes I found on the Internet called for an insane amount of sugar, so I found the most reasonable one and cut it back a bit. Even after cutting the sugar in the recipe, I still found it cloyingly sweet. So for a sauce, I made a tart raspberry and mango coulis. The combination was actually quite good. It should also be served with fresh fruit and some kind of creamy sauce… pastry cream or whipped cream or something. Since we were already having crรจme brรปlรฉ, making a creamy accompaniment wasn’t really appealing to me. Was thinking fried bananas might be nice. Next time.

Gedit 3.2 GDP Completions on Ubuntu 11.10 Oneiric Ocelot

Curiosity gets the best of me sometimes. Okay, most of the time. Did you know GNOME’s text editor, gedit, has a plethora of extensions which can basically transform it into an IDE? Something I’ve always wanted is intellisense-style autocompletion. The closest thing I’ve found for gedit is GDP Completions Plugin in the gedit-developer-plugins package in Ubuntu.

sudo aptitude install gedit-developer-plugins

However, there’s a bug in that package and the popup menu doesn’t actually work. Ctrl + Space is supposed to bring it up. So you want to add the Gedit Developer Plugins PPA and upgrade to the more recent version.

sudo add-apt-repository ppa:user/ppa-name
sudo apt-get update
sudo apt-get upgrade gedit-developer-plugins

If you try to run gedit now, you’ll notice it won’t… run, that is. Great. I know, right? The problem is that the bzr plugin (also included in the gedit-developer-plugins package) is trying to use the gtk2 version of bzr-gtk, but that doesn’t work in the gtk3 gedit. Anyway, you can pull a copy of the gtk3 bazaar plugin into your local bzr plugins directory. (I found this info here). Create ~/.bazaar/plugins/ if it doesn’t exist.

mkdir ~/.bazaar/plugins
cd ~/.bazaar/plugins
bzr branch lp:bzr-gtk/gtk3 gtk

The gedit-developer-plugins package and gedit should work after that! An alternative to the above would be to add a PPA that includes bzr-gtk 3. Not sure if one exists at the moment, but that would be a cleaner solution. And you thought it would be simple. I know I did. ๐Ÿ˜›

It’s not as polished or featured as other implementations, but it’s a good start. Here’s a screenshot after I type os. then hit Ctrl + Space:

Further reading

External tools plugin

Rolling your own gedit 3 plugin

Back to School with Cheesecake

Recently, I’ve been busy for a variety of reasons. The most significant reason? I’ve gone back to school! ๐Ÿ˜ฎ

Shocking, I know. But it’s probably not what you expect. Since April 11th, I’ve been going to culinary school in downtown Vancouver. It’s an 11 block program (each block consists of 4 weeks, so… 44 weeks) – I should be done in February 2012. In addition, I just completed a 4-month stage (unpaid internship) at a hotel. I would go there after class (or before class, depending on the class) about 3 times a week. Lots of getting up early, and some getting home late. It can be tough work. I’ve been physically exhausted. Heck, I’m exhausted, right now. And you know what? I love it. There is something really satisfying about cooking. You produce something you can be proud of in relatively short period of time… and the kitchen can be an exhilarating place to be. I’ve always liked a bit of adrenaline in my life and I guess I didn’t find that in an office environment.

Anyway, I haven’t been taking photos at school. And I should be. So today I took some photos. I’m currently in block 7: baking and pastry. Here are some cheesecakes I decorated today. My partner and I had finished pretty much everything, so we had lots of time to play around. ๐Ÿ™‚

whipped cream rosettes with orange segments and strawberry. Probably should have left the orange bits off the rosettes, but oh well.


rosettes on top of kiwi slices, with strawberry on top of orange segments.


The cheescakes turned out really well. No burning, no cracks (thanks to a sour cream topping), and quite level and smooth. Clean!


Spent way too much time on this, but had a lot of fun doing it. Marzipan pumpkin and chocolate "Booo" leaning on rosettes, Messy spider is 2 strawberries covered in chocolate with marzipan eyes and chocolate disc legs. All on chocolate webbing.

The cheesecakes go for $18. It’s a good deal considering how much cream cheese is in them… each about 750g? But whoever gets that halloween one will have a bunch of chocolate shrapnel when they attempt to cut it. Hah! ๐Ÿ™‚

Attacked and almost robbed

Last night, at about midnight, I was attacked while approaching my parked car at a bus exchange in North Vancouver. I was returning from downtown and was on my way home. I had just got off the bus, walked over to my car, put my bags in my trunk and was unlocking the driver side door when I saw a man approaching me in the parking lot. I didn’t think much of it, I thought he wanted to ask me a question or something. So I just stood there and waited for him to approach me. He yelled something, which I didn’t understand until he said it a few more times, “Give me your shit!” I was totally taken aback. Was this really happening? Is he serious? I noticed he was holding something threateningly in his left hand, thought it might be a chain or something. It was dark and hard to see… and I still couldn’t believe this was happening.

My door was open, but I didn’t let on to that. I lowered my key and tried to talk him out of it, saying it couldn’t end well and stepped cautiously towards him. I may have had my hands up and open. Obviously, he wasn’t a rational person. Even if I did pop my trunk and give him all my stuff, I had no insurance he would leave me alone. I suppose that’s when I decided I would have to defend myself as best I could and find help. He swung his weapon at my head a few times, some of which I managed to block with my arm and sweatshirt. I charged him, knocked him back a bit and retreated to the bus exchange looking for help.

I was being pursued, but I didn’t have have to try very hard to keep a safe distance. As I jogged through the bus exchange, there was a young couple there (waiting for their bus, presumably), no buses, and no authorities. I didn’t really want to bring this lunatic into them, and felt a sense of regret. They cast concerned glances at me and asked if I was alright. The assailant was still a safe distance behind me, ranting and stomping on. That’s when I noticed blood on my hands. I was bleeding? I was aware of a different sensation on left side of my face, but it didn’t hurt. I may have actually said that aloud, both shocked and annoyed, “I’m bleeding?!”

Then I actually got a bit of luck. Nearby, I noticed the police had set up a roadblock, so I ran over there and got help. The assailant was surprisingly cooperative, dropping his weapon, lying down on the ground and putting his hands on his head. I later found out it was a studded belt – I guess he was swinging the clasp at me? He was taken away, would be put in jail for the night (minimum) and be charged with assault and robbery. I was interviewed, the young couple were interviewed, and I got a ride home from the officer. After I was home, I was finally able to see what I looked like.

After cleaning up and sleeping, it looks a bit better. Bit of a shiner, though. ๐Ÿ˜›

Aside from being a little shaken, a couple cuts and welts, I’m okay. I was not robbed. Nobody else got hurt. And I’m thankful that things turned out the way they did, as it could have been much worse. ๐Ÿ™‚

UPDATE 2011/09/04:
The shiner keeps developing. It’s so… colorful! ^_^