Saturday, February 12, 2011

Groovy Tip and How-To (gathering specific files from a directory)

I'm working on a service in Grails that offers all kinds of methods for getting dates or formats of dates with single line calls. For example, dateTimeService.getBracketDates() which will return the first and last dates for the current month (e.g., 2/1/2011 and 2/28/2011). It's not rocket science but I have no desire to re-invent the wheel every time I'm creating a new monthly report for our marketing folks.

Tip: Keep a code snippets (experiment) file

One of the awesome things about groovy is that it's a lot like script programming. That makes it very convenient to try pieces of code and functionality you're not sure about before adding it to your project. Be forewarned. I'm a vi nut. I'm aware that dev tools have snippet support. I just find this works for me, and perhaps for you too.

The nice side-effect of this practice is that you build up a semi-structured collection of groovy and coding knowledge to reference in times of need and senior moments (not you, me!). Guess you could use it for old girl (or boy) friend names too. Another nice side-effect might be "instant book" once you insert a few paragraphs between each snippet!

Put a System.exit(0) at the end of your snippet so that your groovy interpreter doesn't try to execute all the accrued code below. This won't protect you against using variables that have already been defined lower in the file. All I usually do is attach a number to the end of the variable name to get rid of the name collision. Afterall, all I'm trying to do is validate my code. I used this little practice of mine to refinethe How-to below before subjecting you to what might have been buggy code.

How-to: Gathering list of (specifically-named) files from a directory

Here is one of many ways to accomplish this task. I found this old code snippet from my snippet file and thought I'd share it. By the way, a site I love to consult on little basics like this is Pleac Groovy. It's not the most groovy-tized site in the world, but I like it's get-it-done blue collar perspective.

So this bit of code returns a list of log files that use the the naming convention of <directory path>/<year><month><day>.log. In particular, this code winnows the list of files down to the files stored in the month of February (i.e., 201102.*\.log). The regular expression is pretty lame. I'm sure you could improve it.

def baseDir = '/tmp/logs'
def originalFiles = new File(baseDir).listFiles()
println "number of files found:"+originalFiles.size()

// here's our closure, a match expression
def screener2 = { it.name =~ /.*201102.*\.log$/}

def screenedList = originalFiles.findAll(screener2)
println "Number of files matching :"+screenedList.size()
println "screenedList :"+screenedList

Wednesday, December 29, 2010

probability, observer and speculating "existence"


Boy, am I going to get in trouble for a title like this entry's... but this is the kind of stuff that flows through my thoughts when I've been working too much and sleep deprivation denies my brain the energy to cling to my usual collection of standard worries...  and why not end 2010 with a bang!

I'm fascinated by the following aspects of life, according to a number of sources...
  • All substance is composed of mostly nothingness. The ratio of particle to the space it occupies is staggeringly unbalanced.
  • Quantum physics explains the smallest particles as waves of probability.
  • For these things to exist, there must be an observer.
So my thoughts on this have come this far. If you play a game of traveling as an observer of your own life and you speed by the faces of people you've met or walked past, your children as they grew up, the silly arguments you've add, the places you've been to... all blurring as you pass them, what is it that you're really experiencing besides a collection of memories? And how could it be different if you have the ability to re-wind and start over again?

It seems that these are all probabilities that you observed and, in the process of observing, also participated in. It's no wonder why life seems more and more like a dream to me.

But, if you keep digging and thinking about all of this as probability, then life starts to feel less like something tangible and more like a continuous experiment of unconscious messing with probabilities. Combinations of probabilities. Combinations of observations. It's not really like there are a bunch of universes ("multiverses") out there (or in here). It's more like there are all these (floating equations representing) probabilities and you as the observer are making them real. Making them "feel" tangible. So rather than thinking of all of this as an infinite set of what's out there. It's more like everything is being generated as we observe.


All of this brings up the "soul" thing or "consciousness" as one of those fundamental "what is it" questions. Why? Because it's this observer thing that quantum physics says must exist for things to exist. It somehow implies that a soul has special ranking, almost as though it's outside the definition or occurrence of probability. An analogy might be the patterns you see of iron filings when under the influence of a small magnet. The patterns are the assemblages of probabilities. And the magnet is You, the consciousness. (That imagery feels pretty powerful... but it feels more like an influence or steering of probabilities and not just a passive observer roll. Is the observer by definition an actuator?)


I have no authority on this topic. I'm not a mathematician. But I do love how the many topics of physics opens (and throughly energizes) my mind. And as the explanations of things these days gets wilder and wilder, I don't think there's anything wrong with letting a bit of intuition and subconscious rumination have its day. The math helps to illuminate a conundrum. But as our math and its conundrums continue to stack up over time, our ability to go back to our imaginations for speculative interpretation becomes all the more critical to create a sense of things we can conceptually grasp.

As fantastical as the concept of "god" seems, it's actually nothing compared to what this life (you, me, existence) is really all about. My intuition is screaming that.

Tuesday, November 02, 2010

World Series Giants and me...

This is one of those little kid inside me takes over posts.

I was lucky enough and old enough to see the Beatles from their (U.S.) beginnings, but I consider myself even more fortunate to have seen Willie Mays, Willie McCovey, Orlando Cepeda, Jimmy Davenport, Juan Marichal and Jack Sanford all through the 60's. Those Candlestick memories have dominated my thoughts the past few weeks and re-introduced my Dad in my day-to-day thoughts as well. I'm a pretty private person and don't intend to blog much about that, but I will say that I absolutely love my past as a young kid. I was pretty damn lucky.

The even better part of the whole Giants-Rangers-Phillies-Braves experience was hanging out with my New York daughter Amanda over the phone. She kicked things off by calling me from a downtown pub in Philadelphia the evening the Giants won that last game... wearing her Giants hat! Brave young woman and a TRUE Giants fan. Will Clark, Kevin Mitchell and Rod Beck were the guys during Amanda's early days. It was wonderful to feel so connected between PA, NY and ABQ! Thanks Amanda!

This is from the San Francisco Chronicle website @ http://www.sfgate.com :

Sunday, October 10, 2010

The little things in vi that make folks watch and go "wow"

My guess is that I'll add more hints & tips to this posting, so be sure to bookmark it and revisit every now and then.

-- David 10Oct2010

I don't know if all of your colleagues will go "wow", but I know you'll be a lot more productive than you would with a standard Eclipse editor or MS Word.

Task: You want to enter command mode to try all of the commands in the rest of this page:

# press Esc key
<ESC>

Task: You want to find whole words spelled "the", excluding words like "them".

<ESC>
/\<the\>

Task: You have a text file of numbers representing some of your customer account ID numbers. You're creating an SQL query statement and you want to corral those numbers into an IN clause like "... where clientId in (333, 444, 555...). But the 10 numbers are listed, one by one, on ten lines. How do you get them collected into your IN claus?

First thing, you want to append commas after each number, with the exception of the 10th number.

# replace the endline of each line with a comma.
# to avoid putting a comma after the 10th number, specify the line range as 1 through 9.
:1,9s/$/,/g

Now it's time to bring everything onto a single line. Do this using "J" for join.

# Join the 10 lines onto a single line
<ESC>
:10J

You should see all 10 numbers on the same line now. Fill out the rest of your SQL query.

Task: You have a bunch of concatenated strings that each should occupy a single line. Each string is separated by the damn ^M characters that do nothing to create a visual line break.

<ESC>
:1,$s/<cntr-v>M/\\r/g

Task: You want to insert "</span>" between "Task" and ":", like I did when I decided to wrap a span tag around each Task word in this blog posting.

<ESC>
:1,$s/Task:/Task<\/span>:/g

Task: You want to jump to the 13th line in your text file.

<ESC>
:13

Task: Come back on occassion to see additional vi editor tasks here.

# At your browser's URL:
http://discretepassions.blogspot.com/2010/10/little-things-in-vi-that-may-folks.html

Thursday, October 07, 2010

The miracle called "Khan Academy" -- Web-Ed the maximized way

It took me 5 minutes to realize I was witnessing a miracle. What grabbed me about Sal and his www.khanacademy.org?

  • A charismatic, humorous voice.
  • 1,800 12 to 15 minutes lectures - physics, math, business, chemistry, statistics and more.
  • Explained at a pace and in a style that makes comprehension possible.
  • Makes you feel like you can learn things you were never able to nail down back in college.
  • And I sensed immediately that my 10 year old would relate to him, thanks to his trusting humor and kind voice.

So where does Khan Academy fit in? The answer: where doesn't it fit in?

  • My daughter loves Migaland, a 2nd Life for kids. The agreement we have is that she must watch one Khan Academy math class before logging in to Migaland.
  • This is the perfect supplemental homework pill for school teachers to give to their kids. Kids get distracted in class, kids have poor listening habits. Khan gives them a resource at home that 1) the kid can trust, 2) the teacher can trust and 3) the parents can trust.

So, why does this work? Why am I so convinced that this IS the greatest gift of the Internet?

  • It's teaching using common sense... a person who has the gift is able to share that gift in a way that only the Internet can make possible.
  • 15 minute lessons. How perfect is that? Life is fast. Afternoons are short. Evenings are just about impossible. But 15 minutes is a reasonable commitment with the option of doing another 15 minutes.
  • 15 minutes is the perfect amount of time to except hyper focus. Yes, we do, can and should go longer than 15 minutes when we need to. But 15 minutes is also a way of limiting us from gorging, so that we can develop the long term memory of the lesson in a high quality, quickening manner.
  • It's a huge opportunity for folks to diversify their knowledge. Go to the Khan Bank of Knowledge and check out "credit default swaps" or "La Place transforms." It's almost analogous to take a little pill and you have instant knowledge.
  • It's been awhile since college for me. I've had a great career as a software developer and business executive. Khan is a gold mine of possibilities for me for "the next phase" of my life. It's all there.

Enough cheerleading for now. I don't know if Khan's courses will prevent Alzheimer's, but it sure will make life feel more unlimited than it already does. And for parents, it's a true answer to the on-going challenging of keep that kid learning...