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

Tag: tools (Page 10 of 35)

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!

Pasting without format

Anyone who’s ever tried to copy text from, say, a webpage to an email or document knows the struggle. You’re document might be nicely formatted in a 10 point Arial font, but the text you paste in retains its crazy 20 point Comic Sans font from the source page.

So, I was momentarily excited to read a recent article from Bleeping Computer announcing a potential end to the pasting nightmare. The article discussed upcoming changes to your Windows clipboard functionality allowing you to paste historical clips free of formatting and suggested that the ability to paste format-free current clipboard items may even be available in Windows 10 today:

“Although one can also use the CTRL+SHIFT+V keyboard shortcut to remove all formatting when pasting text content sent to the clipboard, this doesn’t work with clipboard history items.”

Bleeping Computer

Really? You mean, today, Windows contains this feature I have longed for for most of my computing life?

Well, it depends on the application in which you are pasting. If you are pasting text into editors hosted in browsers like Firefox and Chrome, then, yes, Ctrl+Shift+V seems to work. However, if you are pasting text into Microsoft Office products on your desktop–which is where I do the majority of my work–Ctrl+Shift+V doesn’t seem to work at all.

Indeed, Chtrl+Shift+V isn’t even listed as a valid keyboard shortcut on Microsoft’s site, so I’m altogether confused by the statements being made by Bleeping Computer. Perhaps we must still wait for the release of Build 21318 before we can enjoy native format-free pasting, historical or otherwise.

In the meantime, one frustrating way to clear your clipboard text of formatting is to first paste the text into an application that supports no formatting at all, like Microsoft Notepad. Then, copy the text from Notepad to its final destination.

However, what I do is use the application PureText by Steve Miller. (I assume not that Steve Miller.) PureText is a fantastic little utility that does the job Windows should be doing: providing you the ability to paste format-free text in any application running on your Windows machine.

Poor man’s database modeling

What can Donald do if he can’t afford an ERD modeling tool?

Although I’m no database administrator, every-so-often I need to model out table relationships as part of some new project. Most folks would use a tool like Microsoft Visio to model their entity relationships. However, Visio costs money and I don’t have it…er, I don’t have Visio. (I don’t have much money, either, given the exorbitant costs of college these days.)

So, what’s a cheapskate like myself to do? Why, use free tools, that’s what! Lately, I’ve found some joy in the combination of SQLite and DBeaver.

Step 1: Code out your database

In a text editor like Notepad++, start coding out your database. Let’s take the canonical example of an orders database. You might rough out some of those tables with the following script:

DROP TABLE IF EXISTS items;
CREATE TABLE items(
	item_key VARCHAR(64) NOT NULL,
	item_desc VARCHAR(100),
	unit_price DECIMAL(5,2),
	CONSTRAINT item_pk PRIMARY KEY (item_key)
);

DROP TABLE IF EXISTS customers;
CREATE TABLE customers(
	customer_key VARCHAR(64) NOT NULL,
	customer_name VARCHAR(100),
	CONSTRAINT customer_pk PRIMARY KEY (customer_key)
);

DROP TABLE IF EXISTS orders;
CREATE TABLE orders(
	order_key VARCHAR(64) NOT NULL,
	customer_fk VARCHAR(64) NOT NULL,
	order_date DATETIME DEFAULT CURRENT_TIMESTAMP,
	CONSTRAINT order_pk PRIMARY KEY (order_key),
	CONSTRAINT customer_foreign_key FOREIGN KEY (customer_fk) REFERENCES customers(customer_key)
);

DROP TABLE IF EXISTS order_details;
CREATE TABLE order_details(
	order_fk VARCHAR(64) NOT NULL,
	item_fk VARCHAR(64) NOT NULL,
	quantity INTEGER,
	CONSTRAINT order_detail_pk PRIMARY KEY (order_fk, item_fk),
	CONSTRAINT order_foreign_key FOREIGN KEY (order_fk) REFERENCES orders(order_key),
	CONSTRAINT item_foreign_key FOREIGN KEY (item_fk) REFERENCES items(item_key)
);

Save your script to a file like orders_db.sql.

Step 2: Generate a SQLite database

Now, pipe your script into SQLite to generate your orders database. In Windows, you can open a command shell and run a command like the following:

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

Step 3: Open your database in DBeaver

DBeaver supports tens of different types of databases, including SQLite. Creating a connection to your newly created SQLite database is relatively easy. Follow the guide to creating a new connection. When it comes time to select your database type, you can click the “SQL” tab and scroll down until you find the selection for SQLite. All your connection needs is a path to the database file you created in Step 2–in my example, I called the database orders.db.

Step 4: Create an ER diagram from the tables in your database

DBeaver includes a cool feature called Entity Relation Diagrams where it allows you to easily visualize your database. In the application, you can select File > New from the menu and find the ER Diagram selection under the DBeaver folder. From there, you should be presented with a list of available databases including the one you setup in Step 2. Go to your orders database, find your tables, and select all of them. Once you name your ER Diagram, you should be able to click the Finish button. If all goes well, you should see a visual like the following:

My ER Diagram in DBeaver (in dark mode)

Having a visual of your database helps you identify missing columns, bad or missing relationships, etc. You should now be able to identify these problems, go back to Step 1 with the code to fix them, and repeat the steps again until your model starts to look more appropriate. With my tables visualized in an ER diagram, I find it easier to share and discuss my ideas with teammates rather than trying to talk through the code. ER diagrams also make for good images in documentation.

Bonus: your ER Diagrams in Jupyter Notebooks

Recently, I was looking for more information on using markdown in Apache Zeppelin notebooks when I happened upon this discussion of pegdown parsers in Zeppelin. The article mentioned the YUML plugin. YUML seems to be a handy way of creating simple UML diagrams.

So, on my laptop, I opened up a new Jupyter notebook, created a new “markdown” cell, and wrote out the following HTML:

<img src="http://yuml.me/diagram/scruffy/class/[customers]<>1-*>[orders], [orders]<>1-*>[order_details], [items]<>1-*>[order_details], [note: My database model {bg:cornsilk}]" >

This code then rendered this UML diagram:

My ERD created with yuml.me

Certainly not as detailed as my SQLite/DBeaver solution, but quicker to achieve and still helpful with team discussions and documentation. In the comments, feel free to add your “poor person” approach to database modeling.

« Older posts Newer posts »

© 2024 DadOverflow.com

Theme by Anders NorenUp ↑