Tuesday, June 8, 2010

mplayer dump

tried to record a lecture in realmedia format with mplayer, but it keeps dying in the middle. cache seems to help it get farther, but it still chokes. maybe i just need to give it a _big_ cache, or use the -cache-min option:
mplayer -audiofile-cache 8192 -cache 8192 -dumpstream -dumpfile out.rm rtsp://etc

encrypted pdf files

managed to get a pdf file with restricted permissions using pdftk (pdf toolkit). the man page was a bit unclear on this, since if you just put in an owner password it will not actually restrict the file and no passwd is requested (or needed) to open it. this will do the trick, with all restrictions:
pdftk in.pdf output out.pdf user_pw foo
or, to make 2 levels of access:
pdftk in.pdf output out.pdf owner_pw baz user_pw foo
now the restrictions will be in place if you put in foo as the passwd, but they will not if you put in baz.

redirecting stdin, stdout, stderr from python

saw a nice comment in a post on python daemon forks that explains how to redirect std* pipes, even when they are accessed from c. i've been frustrated trying to figure that out before.

More reliable i/o stream redirection. Just reassigning to the sys streams is not 100% effective if you are importing modules that write to stdin and stdout from C code. Perhaps the modules shouldn't do that, but this code will make sure that all stdin and stdout will go where you expect it to.

import os, sys

out_log = file('/out/log/file/name', 'a+')
err_log = file('/err/log/file/name', 'a+', 0)
dev_null = file('/dev/null', 'r')
sys.stdout.flush()
sys.stderr.flush()
os.dup2(out_log.fileno(), sys.stdout.fileno())
os.dup2(err_log.fileno(), sys.stderr.fileno())
os.dup2(dev_null.fileno(), sys.stdin.fileno())
(and another poster suggests closing sys.std* before duping.) cool. i need to remember this next time i wrap somebody's code that thinks it's a good idea to barf on the terminal without a --quiet option.
also, the demon implementation looks pretty clean, with extra tidbits sprinkled into the comments, and the author explains the reasons for doing things. i don't think i will need the double fork for my udev script, but the first fork will be necessary.

printing presentations from evince

sometimes i need to print presentation slides with just big text, and i want to do it n-up so it doesn't waste paper. i've found that the best way to do it with evince is to set 'layout' to two-sided: long edge (standard), pages per side: 6, page ordering: left to right, top to bottom, and 'paper' orientation: landscape. 12 slides per sheet and still very readable.
also, it often helps to concatenate pdfs together so the nup has multiple presentations without a page break:
pdftk in1.pdf in2.pdf in3.pdf cat output out.pdf

Monday, June 7, 2010

ocw

mit's opencourseware has quite a few classes up from course 15 (sloan school of management). worth a look.
15.010, maybe 15.060, 15.062, maybe 15.063, maybe 15.223/15.224, 15.351, 15.352, 15.356, 15.358, 15.369, maybe 15.391, 15.394, 15.402, 15.414, 15.431, 15.433, 15.616, 15.617, 15.628, 15.963, and 15.997 look interesting.
15.501, 15.511, 15.514, 15.515, 15.516, 15.518, 15.521, and 15.535 look like particularly interesting classes on finance.
i should also peek at 15.070, 15.075, 15.081, 15.084J, 15.085J, 15.093, 15.094J, 15.098,15.099, just to make sure i haven't missed any of the math there.

Friday, June 4, 2010

udev rules

finally got a udev rule in /etc/udev/rules.d/ so it will run my script on a hotplug event. first i had to run udevadm info --attribute-walk --name /dev/sdc1 to dump out a list of potential attributes to search. since this spits out info on the whole device chain, i found out that the section to look at was /devices/pci0000:00/0000:00:1d.7/usb1/1-7/1-7:1.0 (i tried matching on a lower level, but my script was executed twice with different env vars.) then i put a rule line into /etc/udev/rules.d/91-myhotplug.rules (91 because i want it to run after everything else) like this:
ACTION=="add", SUBSYSTEM=="usb", ATTRS{modalias}=="usb:v0...50", RUN+="/home/user/svn/bin/hotplugscript"
had to put in a sleep 6 before doing anything in the newly mounted fs because it seems to take about 5 s to settle down. (probably should loop-and-test in production-level code.) oh, and one other handy thing i googled up: use blockdev --flushbufs /dev/sdc1, for example, to flush one block device. sync just flushes them all. i have the whole bash script in brackets with a trailing & to make it fork off and return immediately; that's important because otherwise i found it hangs or dies at the sleep and nothing after that gets executed. and i did find that a umount after business is done in the udev script causes no problems.

Thursday, June 3, 2010

financial accounting info on google finance

google finance has easy access to the balance sheet, cash flow statement, and income statement of publicly traded companies for the last 5 quarters and 4 years, ready to scrape from an html table in a standard format, all in one page. for example, all this info for cisco is at
http://www.google.com/finance?q=NYSE:C&fstype=ii
probably would be a bit more reliable if there were one place i could grab xbrl files, but right now i can only find links to individual companies' websites. not very scriptable.
also, the summary page (eg, http://www.google.com/finance?q=NYSE:C) has a 'key stats and ratios' sidebar. i wonder if any of these are really metrics that the pros use. yahoo has insider and institutional trades at http://finance.yahoo.com/q/it?s=C although dailyfinance shows total holdings for insider trading at http://www.dailyfinance.com/company/citigroup-incorporated/c/nys/insider-transactions
yahoo has convenient pages for analyst estimates, showing average, high, and low earnings estimates from a number of analysts for current and next quarters and years; estimate and actual eps histories; eps estimate recent revisions; and estimates for revenue and growth for the company, industry, sector, and s&p 500 for comparison. for example, dell's page is at http://finance.yahoo.com/q/ae?s=dell pretty good start for fundamentals valuation. the analyst opinion page at http://finance.yahoo.com/q/ao?s=DELL also has price targets and quantitative recommendation poll numbers (in the chart near the bottom; i think the 'mean recommendation' listed above is is basically the dot product of the votes in each category with the arange(0,5) divided by the total votes). not sure i would completely base my decision making on free advice, but it could be worthwhile for a sanity check right before clicking the 'trade' button. looks like it only has these analyst opinions for individual companies; no etfs, etc. but the etfs have holdings info, so i could go from there to get some projections on stocks.
now i need good ways to project near/medium term trends in the bonds, precious metals, commodities, and fx markets.