Hello, welcome to my blog, a stream of random ramblings, mostly about technical stuff, including (but not limited too): programming, Linux, OS X, Drupal, ...
Happy clicking around,
Stefaan
Hello, welcome to my blog, a stream of random ramblings, mostly about technical stuff, including (but not limited too): programming, Linux, OS X, Drupal, ...
Happy clicking around,
Stefaan
Even if you are in an automated context, not using a typical browser, fetching the data/webpage from an URL is easy. There are command line tools like wget and curl, and every programming library has its share of libraries to build on.
But what if you need the HTML markup of page after it has been manipulated with JavaScript?
A colleague of mine at work has set up his IDE to clean up all trailing whitespace on save. Nothing wrong with that, except that he is the only one, which very often means in practice that his commits are sprinkled with fixes of other people's whitespace mixups. Because of these unrelated line changes, you easily get in conflict situations when cherry-picking or merging.
Here is a trick I use to cherry-pick this kind of commits, without the whitespace parts.
For example, say his commit SHA1 is "abc123".
So you have some Python code in a try-catch, and you want the typical Python stacktrace (aka traceback, aka backtrace) in a way you can manipulate?
Here are some clues:
import traceback import logging try: stuff() except Exception: # Just print traceback print "something went wrong, here is some info:" traceback.print_exc() # Get traceback as a string and do something with it error = traceback.format_exc() print error.upper() # Log it through logging channel logging.error('Something went wrong', exc_info=True)
Music files come, music files go. And after some time your iTunes library is sprinkled with cases of The song “Jajazazalala” could not be used because the original file could not be found. Would you like to locate it?
Here is some Python powered unicode fun (command line oneliner version):
python -c 'for c in range(0x1F410, 0x1F4f0): print (r"\U%08x"%c).decode("unicode-escape"),'result:

In git, when you do git stash pop of unfinished code after upstream merges, you might get a conflict between the stash and the upstream changes you pulled in.
You git clone now has "unmerged paths" aka is in conflict state. For example, git status shows:
# Unmerged paths: # (use "git reset HEAD <file>..." to unstage) # (use "git add/rm <file>..." as appropriate to mark resolution) # # both modified: src/some/file.txt # both modified: src/some/other/file.txt
After you fix the conflicts manually in the files, you normally want to tell git that the conflict has been resolved. If you google for how to do this, you typically find solutions that suggest to git add the files and commit. But what if your (stashed) work is not ready for commit/staging yet?
Here's a simple bash oneliner to determine the installation prefix (and more) of a python interpreter:
python -c 'import sys;print("\n".join("{0:16s}: {1!r}".format(x, getattr(sys, x)) for x in ["executable", "version", "prefix", "exec_prefix"]))'
(Replace "python" with the actual python executable you want to investigate of course)
As its name suggests, the Python subprocess allows you to spawn a child/sub process and keep an eye on its standard output through a pipe for example. Very handy to glue together external systems/processes. A tricky issue with having parallel "flows" this way (your python process and the child process in this case), is the risk on deadlocks if you are not careful with the order things are done.