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

Tag: powershell (Page 1 of 7)

SQLite in PowerShell

A while back, I wrote about how I use SQLite to do, among other things, some database modeling. Typically, I will write all my table creation scripts in a single SQL file and then run that script at the command line like so:

C:\sqlite-tools-win32-x86-3300100\sqlite3.exe orders.db < orders_db.sql

This approach works great if you run your command in the Windows command shell or even in Linux, but will it work in PowerShell?

The answer is: No. PowerShell uses the “<” sign differently than the other two shells and won’t honor that command.

Both the Windows shell and Linux treat that “less than” sign as an “input redirection” instruction. You are redirecting your input, the SQL file, into the database you’re generating with the sqlite3 command utility.

So, in PowerShell, how might we accomplish this same sort of input redirection? One answer is to use PowerShell’s Get-Content cmdlet (using its alias “cat”) and pipe the content of the SQL script into the sqlite3 utility:

cat orders_db.sql | & C:\sqlite-tools-win32-x86-3300100\sqlite3.exe orders.db

Note that I’m using the ampersand operator as a “call operator” to launch the sqlite3 utility.

So, now you know how to use SQLite to load scripts in three different command shells!

Maintaining a Positive Sentiment

For various reasons, companies like to know the emotional dispositions of their customers and, sometimes, their employees. One common way to determine these states is through a technique called Sentiment Analysis.

One challenge with sentiment analysis is simply knowing how to score words, one way or another. Our brains know that the word “happy” is usually associated with a positive sentiment because we have a dictionary of sorts in our brains that associates “happy” to a favorable emotional state. Computers don’t immediately know these associations, so you usually have to supply them such mappings, often in the form of simple lists of words.

Now, suppose you want to maintain the appearance of positivity with an organization with whom you interact–maybe through online reviews, email, chat, or some other written medium. Here’s an idea you can try.

Step 1: Downlist a list of positive words

Positive (and negative) word lists are easily available on the Internet. Here’s one I downloaded.

Step 2: Write some PowerShell to extract some random, positive words you can use in your correspondence

If you’re running Microsoft Windows, you’ll likely have PowerShell installed, which is a convenient tool to parse the file you downloaded and provide back a few random words. Here’s a one-liner I wrote:

((cat c:\positive-words.txt|? {!$_.startswith(';') -and $_.length -gt 0}|get-random -count 3) -join '. ') + '.'|set-clipboard

In this code, I load in the word file into memory and then filter out lines that start with semicolons–in the file I downloaded, the author used semicolons to denote comment lines I need to exclude–and empty lines. Next, I select three random lines as each word is on a new line. Next, I put a period between words. Finally, I send the results to the Windows clipboard for easy pasting into the editor in which you are communicating–email, chat window, etc. The result is something like this:

pleasantly. enthralled. idolize.

But wait, there’s more

The above is great, but to get your words, you’ll have to execute the code in a PowerShell command shell. Who has time for that? How about executing your PowerShell from a batch file? Add the following to a new bat file:

set c="((cat c:\positive-words.txt|? {!$_.startswith(';') -and $_.length -gt 0}|get-random -count 3) -join '. ') + '.'|set-clipboard "
powershell -command %c%

Now, place that bat file on your desktop or in a convenient area where you can double-click on it to get your words easily. Even better: make a shortcut to it with a tool like Slickrun.

Now, you can easily leverage positive words in your correspondence and maintain a positive persona to the watchers.

Random numbers with PowerShell

Recently, I was writing some unit tests for a data transformation application I had been developing. I had a sample file of pre-transformed data and decided I wanted my unit tests to just test a few, randomly selected records from the file. My tests would pull in the data file as a list and would iterate through a list of randomly determined indices and test the transformation of each data row. Something like this:

val randomRows = Seq(1, 2, 3, 4, 5) 
for (i <-0 to randomRows.length-1) {
  val randomRow = randomRows(i)
  val dataToTest = dataList(randomRow)
  // transform the data; assert the results
}

But, instead of “1, 2, 3, 4, 5”, I wanted random indices like “432, 260, 397, 175, 98.” How could I quickly achieve this and get different sets of random numbers for the different unit tests I was writing?

Random.org is certainly a good option for picking random numbers. Suppose I had 10 unit tests to write, each needing to test 5 random rows of data. I could generate 50 random numbers like so:

108	62	221	275	342
303	475	234	283	343
184	42	454	102	423
48	348	289	37	493
258	471	461	212	278
175	56	224	405	354
374	124	328	17	171
416	266	415	436	414
93	155	140	382	235
83	382	449	302	170

That’s great, but, annoyingly, I still have to edit these numbers and type commas in between each when I paste them into my code. Is there a way to generate the random numbers I need and automatically format them with commas so that I can easily paste them into my unit test code? PowerShell can do that!

The Get-Random cmdlet

PowerShell has a fantastic cmdlet called Get-Random that allows you easy access to Microsoft’s random number generator features. To use Get-Random to randomly select 5 indices to use in one of my unit tests, I can execute this command at a PowerShell prompt:

0..500 | Get-Random -Count 5

Here, I’m piping a list of numbers–from 0 to 500–to Get-Random and telling the cmdlet to randomly select 5 of them. The result is this:

283
331
212
397
459

The problem is that I’m still no better off that with Random.org: I still must manually comma-delimit these numbers so that they can fit into my code.

Formatting my random numbers

Fortunately, PowerShell includes a handy join operator to make joining my list of random numbers a breeze. All I need to do is surround my original PowerShell command with parentheses and apply a join operation to that result set:

(0..500 | Get-Random -Count 5) -join ", "

And the result:

121, 123, 231, 45, 70

Easy-peasy! I can now drag my mouse over that result, right-click on it to copy the formatted numbers to my clipboard, and then paste the results into my unit test.

But wait, there’s more

That mouse highlighting and right-clicking still seems like a bit of work. Is there anything else I can do to shorten my steps further? Absolutely! PowerShell has another great cmdlet called Set-Clipboard allowing you push PowerShell results right into your clipboard. So, I can just pipe my formatted, random numbers right into the Windows clipboard:

(0..500 | Get-Random -Count 5) -join ", " | Set-Clipboard

Now, once I run the PowerShell command, I can just hop right into my code editor, position my cursor at the appropriate position, and paste in my random numbers. Quite a convenient little command!

« Older posts

© 2024 DadOverflow.com

Theme by Anders NorenUp ↑