Musings of a dad with too much time on his hands and not enough to do. Wait. Reverse that.

Month: April 2018 (Page 1 of 2)

Posting code frustrations

Either I’m missing something or posting code snippets on WordPress sites is just not easy–or, at least, I haven’t yet stumbled upon the trick of it.  So far, I’ve posted PowerShell and Python code snippets and I’ve used the CodeColorer plugin to jazz up the snippets a little.  Initially, this worked fine with my PowerShell snippets:

1
2
3
$birth_date = [datetime]"3/29/1916"
$death_date = [datetime]"6/23/1984"
"Tom was {0} years old when he died" -f [math]::floor((new-timespan -start $birth_date -end $death_date).Days / 365.2425)

(I’m wrapping my code with the CodeColorer syntax [ccn lang=”powershell” width=”600″] )

However, Python depends heavily on tabbing to denote if statements, for loops, and so forth and this adds another level of complexity to the snippets you post.  In HTML, to properly preserve tabs and spacing in your code, you generally have to turn the pre tag.  So, for my Python snippets, I turned to both the CodeColorer plugin and the SyntaxHighlighter TinyMCE Button to wrap my snippets in appropriately styled “pre” tags and get this:


1
2
3
4
5
6
7
8
#the old surround-the-string-with-parentheses technique
qry1 = ('SELECT fname ' +
        ',lname ' +
        ',job_title ' +
        'FROM people')

curs.execute(qry1)
curs.fetchall()

(Here, I’m wrapping my snippet with this: <pre class=”brush: python; gutter: true; first-line: 1″>[ccn lang=”python” width=”600″])

What in the world is going on with the large gray box around my code?  It appears to be a background style setting connected to the pre tag, complements of the stylesheet my theme uses. Fortunately, WordPress has a custom CSS feature, so I should be able to overwrite this behavior.

It took me a few attempts, but this is custom CSS I came up with: .preforcode pre {background: transparent}

To implement my custom class, I wrapped my “pre” tag with a div, like so (note that I think all that brush…python stuff that the SyntaxHighlighter TinyMCE Button places in the class attribute of the pre tags is nonsense, so I just removed the attribute altogether):
<div class=”preforcode”><pre>[ccn lang=”python” width=”600″]


1
2
3
4
5
6
7
8
#the old surround-the-string-with-parentheses technique
qry1 = ('SELECT fname ' +
        ',lname ' +
        ',job_title ' +
        'FROM people')

curs.execute(qry1)
curs.fetchall()

Sweet! That gray box is gone! The padding around my snippet is still there: I guess that kind of makes my snippet pop out, but I might now have to mess around with that a little and eventually add any modifications I come up with to my custom CSS. At any rate, at least the snippet looks better so, going forward, hopefully my code snippets will look a little more appealing.

Taking Care of your Family Heirlooms

As a technologist (and nerd), I love going to technology conferences. Over the years, I’ve been to numerous Microsoft gatherings and a variety of other development and security seminars. Fortunately, all those events were paid for by employers past and present. I did attend one day at the Ohio Genealogical Conference a few years ago, but on my own dime and only after burning a day of vacation. In general, it’s hard to justify the high ticket and travel fees, not to mention finite vacation days, to attend genealogy and other non-work related, but interesting events.

All that said, I don’t know why it never occurred to me before, but several months ago, one of the genealogical podcasts I listen to alerted me to the fact that some of these conferences publish their sessions online, sometimes for free. So, while backing up several gigabytes of media files, I decided to take in the session “Taking Care of your Family Heirlooms” from the 2017 National Archives Virtual Genealogy Fair (I guess there are worse things on which the US government can squander tax payer money). If you have an hour, check out the session–although the audio isn’t great; but for a cliff notes version, here are my notes:

Assess your stuff

Over the years, my ancestors have amassed lots of paraphernalia and a lot of that material has filtered down to me. At some point, though, you have to make some hard choices about your artifacts: for every artifact, you should decide whether to keep it, sell it, throw it away, or give it away.

Document your stuff

For all the material you keep, you should document as much as possible. Ask about each object as all those “W” (and “H”) questions: who, what, when, where, how. As in: who owned this artifact? What is it? What makes it so valuable? When was it acquired? Where was it acquired? How was it acquired? Etc. All artifacts should be documented in multiple ways. For example, photographs should be scanned and the electronic file documented in some fashion. Likewise, the physical photograph should be safely labeled. The presenter said that if you must apply a label directly on the artifact, use pencil instead of pen as pencil does less harm.

Store your stuff sensibly

Wet basements and hot attics can do great harm to your heirlooms as can sunlight, ultraviolet light, pests, family pets, and even dust. Ideally, you store your items safely in, say, the first floor of your home where your environment is a little more consistently controlled. What sort of containers should you choose? For photos, documents, and such, the presenter recommended PAT tested, alkaline buffered containers and, even though such containers are expensive, avoid overfilling them as that might compromise the contents. Old books like family Bibles–I have several of these–should be stored flat instead of upright like you’d find in a library, as gravity can be a harsh mistress to these worn tomes.

With metal objects, your greatest enemy is rust and you seem to exacerbate this problem if your metal heirlooms touch one another, so keep your metal heirlooms dry, wrapped in acid free paper, and stored so that they’re not touching other metal objects. During the question-and-answer period of the presentation, someone asked about preserving tin photos, of which I personally have a lot. Apparently, “tin” photos aren’t tin at all, but iron. The rule still applies, though: keep the photos from touching one another, keep them dry, stick them in a box, etc.

Display your stuff?

In general, the speaker wasn’t too keen on displaying one’s heirlooms given the damage sunlight and other environmental factors can inflict on your items (reminds me of how the US Declaration of Independence was hung on the wall of a patent office in DC for over 30 years degrading it badly). She recommended only displaying them “on special occasions” or, alternatively, displaying a photocopy of the artifact. Photos could be mounted on PAT approved paper and slipped into polyester sleaving for display. I actually did this with one of my grandmother’s old scrapbooks. I took the further step of printing out labels that I stuck to the photo paper underneath each photo…so I safely stored, displayed, and labeled these precious heirlooms all in one fell swoop!

How to handle your stuff

The basic rule of thumb here is to wash your hands before handling your artifacts. Some experts advocate cotton gloves, others advocate latex free gloves. One disadvantage to gloves, though, is that they dull your sensitivity to the artifact you’re handling possibly allowing you to damage it without realizing. Have stiff boards on hand on which you can lay flat your precious documents and make sure you have clean, de-cluttered surfaces on which to work.

Digitizing your stuff

I strongly recommend digitizing as much of your keepsakes as possible. Certainly photos and slides. I do have several large photos, paintings, and posters for which I struggle to find solutions since I’ve not been able to find a retail scanner for documents larger than letter-size. In the past, I’ve had to settle for scanning these items a section at a time and then using software to stitch the images together. FedEx stores have oversized scanners that I’ve used in the past, but I also want to look into building a rig for these purposes, as well.

The presenter noted that sometimes, you’ll get the best digital product by farming out the work to a capable vendor–you may not even own the equipment necessary to digitize some of your material. Here is a list of questions the speaker recommended asking potential vendors:

  1. Will you perform a pilot test for me on one of my artifacts so I can be sure of the quality of your work?
  2. Do you do the work in-house or do you send the items out to another location?
  3. What type of file will I get back? With audio,for example, WAV audio files are excellent for archival purposes whereas MP3 files are ideal for sharing with other family members.
  4. Do you adjust your equipment to get the best quality product? If you’re digitizing vinyl albums, ask what needle sizes the vendor uses. If you’re digitizing audio tape, ask the vendor if they adjust the asymyth to get a better reproduction. You may not know these terms, but quality vendors should.

Further Resources

For further questions, you can always email the Archives folks at preservation@nara.org or inquire@nara.gov. Other sites the speaker recommended include https://www.archives.gov/ and http://www.conservation-us.org/. For good backup strategies of your digital assets, take a look at this site: https://www.lockss.org/.

Dealing with Multi-line Strings in Python

Often, I’ll have to set a multi-line string in Python, such as a long SQL statement or friendly message of some sort.  It would be easy to write one, long string but that wouldn’t be very readable or maintainable–having to scroll horizontally to read the string–nor would it be compliant with PEP8.  So, what is a coder to do?  Well, fortunately, Python has a number of ways to skin this cat:

Option 1: Concatenate your strings with plus signs

Here, I just concatenate my individual lines together with the good ol’ plus sign:


1
2
3
4
qry1 = ('SELECT fname ' +
        ',lname ' +
        ',job_title ' +
        'FROM people')

Option 2: Concatenate your strings without the plus signs

Well, it turns out you don’t need the plus signs after all to concatenate your lines together:


1
2
3
4
qry2 = ('SELECT fname '
        ',lname '
        ',job_title '
        'FROM people')

Option 3: Use backslashes for line concatenation

Here, you don’t need to surround your strings with parentheses…just use the backslash to do the concatenation work:


1
2
3
4
qry3 = 'SELECT fname ' \
        ',lname ' \
        ',job_title ' \
        'FROM people'

Option 4: (my personal favorite) Surround your multi-line string with three double-quotes

This technique makes it very easy to read the content of your multi-line string:


1
2
3
4
5
6
qry4 = """
SELECT fname
    ,lname
    ,job_title
FROM people
"""

Bonus Option (Jupyter Notebook only): Use the sql_magic keyword

If you have a long SQL statement and are working in Jupyter Notebook, consider using the sql_magic keyword from Pivotal:


1
2
3
4
5
%%read_sql df_result -c conn
SELECT fname
    ,lname
    ,job_title
FROM people

Check out these examples in action on my Jupyter Notebook.

 

« Older posts

© 2024 DadOverflow.com

Theme by Anders NorenUp ↑