Archive Page 2

Printing on one line with Python

Ok so I've been using Python for a long time and have written a lot of lines of Python code. But this one is pretty simple and I'm kind of embarrassed that somehow I didn't know about it.

In the past, I've used sys.stdout when I wanted to print multiple things on one line, e.g., you want to print periods to show progress.

  2. import sys
  3. for i in range (10):
  4. sys.stdout.write ( "%d.." % i )

I also knew you could use print with more than one item.

  2. print 1,2,3, "Hi"

But the other day I inadvertently found that you can continue to print on one line if you just leave the trailing comma....

  2. for i in range(10):
  3. print "%d.." % i, # no newline appended
  4. print # add a newline after the loop.

And who cares you say??? Well now I don't have to import sys and use sys.stdout when a simple print can do just fine 🙂

Fun with C++11 Lambdas

So I got a c++11 book. I've been following a few of the language differences and features for a while, but I decided to break down and actually understand it all. I decided to have some fun with lambdas today. Lambdas are a very useful language feature that lots of dynamic languages have (JavaScript, Python, <insert your favorite there is plenty>). For some additional readying you might read about Closures and Functors.

Anyway, so here you go:

Basic syntax

  2. [...] {...} // simplest form
  3. // or showing all the optional arguments
  4. [...] (...) mutable throwSpec -> retType {...}

What does that all mean?

The brackets are a parameter list of values you can pass in for usage in the lambda from the context in which the lambda is being created. You could pass in local variables or this for instance. You can pass by a value or reference. Unlike other languages, if you pass by reference and the value is deleted, your lambda might have unexpected results or crash.

The parenthesis are the parameters you pass in when you call the lambda. Declared like a parameter list for any other function. auto is nice but if you need the type (like to return a lambda from a function) you can use the functional header.


  2. #include <functional>
  3. #include <iostream>
  4. using namespace std;
  6. function<int(int,int)> getTheLambda()
  7. {
  8. int x=7;
  9. // will return an undefined value because x is going to be removed from
  10. // the stack when this function exits.
  11. return [&x] (int a, int b) { return x+a+b; };
  12. }
  14. int main(int argc, char* argv[])
  15. {
  17. auto m = [] (int y) { cout << "lamda value: " << y << endl; };
  18. // call it
  19. m(3);
  21. auto l=getTheLambda();
  22. cout << "Lambda Result: " << l(1,2) << endl;
  24. return 0;
  25. }

For me it printed:

  2. ./test
  3. lamda value: 3
  4. Lambda Result: 32770

If you change the first lamba in the function to "x" instead of "&x" you'll get the expected result of 10.

I can't wait to pass a lambda to a thread and use it as some type of callback!

git diff with color words

I had these files with very very long lines of text on each line. It was paragraphs worth of words but not word wrapped. These files were stored in a git repository. Now and then, someone would change a word. Diff with line based output was difficult to find what actually changed.

I figured there must be a way to see just the words. A little searching around the docs and I found it.

  2. > git diff --word-diff=color

Perfect. I still got ling scrolling lines though. You can override that with your $GIT_PAGER variable or I just piped the output manually for a one time fix:

  2. > git diff --word-diff=color | less -r

Much easier to see the changes!

Storing Lipo batteries for faster charging times and longer lipo life

I just finished a quick flight at the park with my Trex 550e. What a fun bird to fly.

Trex 550e Upside down hover

That picture was taken last year on a nice summer morning. No pictures today but it's the same heli.

Anyway, the point of this post is about lipo batteries. I just brought up the heli fly because I had such a great experience with it's battery. Coincidentally, I was just talking today with someone about charging batteries and they wished they could charge 4 6S batteries in 20 minutes. You need a lot of Watts to do that. A 6S lipo is 22.2V nominal and the batteries for these helis are 5000mAh. So if W=A*V then 4 of these things take ~444W to charge in an hour. Triple that to do it in 20 minutes. The only charger I know of that can do close to that is the Powerlab 8. Given a big enough input power supply it can do 1344W output.

Step back a bit. When I first started flying RC airplanes and helis I wanted to fly all the time. Well, I still do but I was pretty gung ho from the beginning. I'd do like 3 or 4 flights per day if the weather wasn't bad. Morning, evening, it was all good. We live by a park big enough to fly my small trainer so I'd just run out for a couple quick flights and then go back to doing whatever. I didn't have time to charge my batteries before every flight so at the time I just charged them after the flights and left them charged all the time. It worked out pretty well except after a while I started learning that it might be best to not store them charged all the time. Apparently it breaks down the chemistry faster when they have a full charge. A day before you're probably fine but charged all the time is going to wear them out much faster.

Ok, side note again. All my batteries that I did that with are still fine. I use them in planes and even an EDF still and as far as I can tell that early storing stuff didn't do much harm to them. BUT... you know, the experts must have something they know so I better heed advice.

Fast forward a bit. I decide I should probably not store my batteries charged. I think this is the point I started flying less often. If I wanted to go fly something I had to take a moment and charge some batteries. You can see why someone would want to be able to charge their batteries FAST.

I got a better charger.

OK, so I have this charger that can charge faster than the original one I had but I have a lot of batteries by this time too. I hook them all up and charge most of them at the same time. It can take over an hour still. In the mean time, I keep learning and reading things and I find out that the experts are advocating not storing the batteries all the way discharged too. Apparently storing them discharged has a similar effect to when you store them charged. How much? I have no idea. My philosophy is that I'll probably destroy the battery before I find out if the way I stored it has much effect. However, this is good news because 1) My fancy pants charger has "store" mode and 2) I get to fly faster if they are already charged a bit.

Store mode on these chargers puts the charge on the pack back to about 70% capacity. Somewhere around 3.85-3.9V/cell. So now I get to feel all dandy because I'm storing my batteries in such a way that the experts won't complain AND, my charging time is cut in half. I just charge the packs before a flight, go fly, then stick the packs back on the charger for their storage charge and I'm good to go for next time.

Back to the heli flight today. I was smiling because I just stuck that one 6S 5000mAh pack on for a quick charge and 20 minutes later I was ready to go.

Quick convert raw g711 uLaw audio to a .au file

I had reason to playback some raw g.711 audio data. I made the following script to convert the data to a .au file that is playable.

From the AU file spec.. which is way simple by the way:
You just need to add 6 32-bit header flags.

  1. import struct
  3. header = [ 0x2e736e64, 24, 0xffffffff, 1, 8000, 1 ]
  4. o=open('','wb')
  5. o.write ( struct.pack ( ">IIIIII", *header ) )
  7. raw = open('in.raw','rb').read()
  8. o.write(raw)
  9. o.close()

You can probably see that this would be extremely simple to use with other audio formats too. Modify as you need.
Header Bytes:

  1. Const AU identifier: 0x2e736e64
  2. Size of Header: 24.
  3. Size of File: use 0xffffffff for unknown, or you could replace with the actual size.
  4. Audio format. 1 is uLaw. From the spec choose whatever you need if it isn't uLaw.
  5. Sample rate. uLaw is always 8000.
  6. Channels. 1 channel in this case.

How to correctly calculate break even time on a refinance – For Math Geeks

After my last post on refinancing, I found a refinance calculator on that compared loans the way I suggested. It has a shortfall though, it calculates break even time with the difference in your new payment compared to the difference in your old payment. This is actually quite incorrect. That number is good to show you how much money you'll have extra in your bank account each month if you are making only the minimum payment. What if you pay extra on the mortgage though? In order to make almost any refinance the most beneficial, you need to pay extra on the new mortgage so that it doesn't take as long as the new term of the loan to actually pay off the mortgage. i.e., you should at least pay what you were paying on your old mortgage.

Continue reading 'How to correctly calculate break even time on a refinance – For Math Geeks'