Friday, March 30, 2018

How do you define an Array in Python?

Looks like a trick question.

There is no Array in Python but an object List exists which can be used to the same end.
  • The List may contain different list items or same kind of list items.
  • The List is indexed and zero based. The first list item has an index 0.
  • You can retrieve list items using their index. Even negative index is OK.
As defined here lst is a list .

lst=["a", "b", "c", "e"]

The list can have strings like this as well:

lst2=['a', 'b', 'c', 'e']

They both evaluate to the same: ['a', 'b', 'c', 'e']


List may contain different types of List Items as in this definition.


 lst2=["ab", 8, True, 5.8]

Retrieving list items using index
>>> lst[0]
'a'
>>> lst[-1]
'e'
>>> lst2[-2]
True
>>> lst[5]
Traceback (most recent call last):
  File "", line 1, in
IndexError: list index out of range

Negative Index
>>> lst[-1]
'e'
>>> lst2[-2]
True



These evaluations were carried out using Python 3.6.2


Thursday, March 29, 2018

How does TableSample in FROM clause of a SELECT Query work?

TableSample clause started with SQL Server 2015 limits the number of rows returned from a table to a sample % or sample numbers. In fact, it may not provide the specified rows of %. Also, it is not for tables with too few rows.

Here is how TableSample is defined in the MSDN site:
------------
TABLESAMPLE (10 PERCENT) /*Return a sample 10 percent of the rows of the result set. */
TABLESAMPLE (15 ROWS) /* Return a sample of 15 rows from the result set. */.
--------------
For these scenarios it cannot be applied:

Derived tables
Linked Server Tables
Tables from Table-Valued functions
Row-set functions
Open XML

This here is the syntax for TableSample clause:
----------------
TABLESAMPLE [SYSTEM] (sample_number [ PERCENT | ROWS ] )
[ REPEATABLE (repeat_seed) ]

TableSample in FROM Clause does not behave as defined in the syntax and could provide surprising results:

I queried the Northwind databases Orders table using the following syntax in SQL Server 2016 Developers edition.

SELECT * FROM Orders TableSample(10 Percent)
SELECT * FROM Orders TableSample (10)--surprisingly this does not result in error
SELECT * FROM Orders TableSample (10 ROWS)

These queries were run a number of times and the rows returned were variable from run to run and some times resulted in 0 returned rows.

Wednesday, March 28, 2018

Can you use Interactive C# for Regular Expressions?

Yes, you can. However you have to reference the Regex class (System.Text.RegularExpressions) as shown in this image.


After this you can use its objects and you have intellisense to provide you help in coding.


The Syntax for using RegularExpressions is:

Match match = Regex.Match(InputStr, Pattern, RegexOptions)

You set up a pattern and you test your Pattern against the InputStr you set up to see if there is a match.

Let me shown a very simple example (the example is run in Interactive C# in Visual Studio Community 2017). Make sure you have already used using System.Text.RegularExpressions before executing these statements:

In the above, the InputStr is 'Jayaram' and the Pattern is shown above. You need to type each line and hit Enter. It is somewhat slow.
In the above case, a match was found for the first lower-case letter in the InputStr.

Here is another example:

The match is for the first upper-case letter in the string.

Here is an example for 2 adjacent numbers in the string:

Only one of the blocks there is a match. Same pattern but different InputStr.




How do you use SplitView for Navigation?

SplitView has two panes and in one of them you place items that allow you to navigate to the details of that item and its contents. You can basically use some event handlers to the items and when the event occurs, you display the content for the item.

Here is the scheme for navigation:


SplViewNav_0

You have three objects in the SplitView pane (Rose, Jasmine, Lily). You attach events to these objects and when the event is fired, you display the related content in the area on the right, the Content.

Before you read this post, please make sure you read the two following posts:

Displaying image in the Assets folder:
http://hodentekhelp.blogspot.com/2018/03/how-do-i-display-using-code-image-from.html

Navigation with SplitView:
http://hodentekhelp.blogspot.com/2018/03/how-do-you-use-splitview-xaml-control.html

Description of App in xaml designer

Start with a UWP Blank project and provide a name. In this it was named JSplitView.
The code for MainPage.xaml is shown here:


SplViewNav_1


I just palced a ListBox control and inside of which I placed three ListBoxItems named itm1, itm2 and itm3.

I also arranged for the event by invoking the KeyDown event. Although I used KeyDown of ListBox, you could have used three Button's and use its Click event.

The Content pane is the Grid element where in I placed a placefolder for an image with the name Img.

In the Design, the app appears as shown in the first image above in the post.

As soon as you place the 'KeyDown' event in the MainPage.xaml, the events show up in the MainPage.xaml.cs page as shown:


SplViewNav_2


Now if you have read my earlier posts, you would see that I wrote code in the Itm_KeyDown events to access the three image files in the Assets folder. Of course, you need to make sure that you add the three images (the images may be your own) to the Assets folder.

This completes the app and all you need to do is to Build, Deploy and Run.
When the app is launched, this is what you see:

This image is when any key is pressed when the cursor is on the item when run on Local Machine.


This one on a 4inch 512 MB emulator.






Tuesday, March 27, 2018

How do you convert Integer to Boolean in C#?


Start Interactive C# in Visual Studio Community 2017. Here are examples of integers (+ and -) and 0 converted to Boolean, using the Convert.ToBoolean(). This functions has 8 overloads.

Read here about running C# in interactive mode.

> bool p = Convert.ToBoolean(6);
> p
true
> bool k = Convert.ToBoolean(0);
> k
false
> bool q = Convert.ToBoolean(-8);
> q
true



Monday, March 26, 2018

How do you use ForEach loop in Windows Power Shell?


'For Each Loop' is used in all programming languages the notation may be slightly different. We already saw how we do it writing the array elements in my previous post here.


The ForEach-Object has other simpler aliases as shown here:



You can also access all Windows Objects if you use WMI as shown here.


These are my logical drives:


How do you work with Array Data structure in Power Shell?

It is the same as a .NET array. In .NET System.Array is based on System.Object. Working with arrays in Power Shell is somewhat easier than in C#.

I am using the Windows PowerShell Integrated Scripting Environment (ISE) (x86). You should be able to find this on your Windows 10 computer.

This is the version I am using for this post:


Assigning as Array. $ stands for a variable.


You assign the array to a variable 'flowers' as shown above. When you call the variable the array will be printed in the console as shown above.

This is same written explicitly as follows:

$flowers=@("rose", "jasmine", "lily") 
Note array elements need not be of the same type.

Writing array content to console
You can also print to the console as shown in the next. $_ is a dynamic variable storing current information.


Write-Host $_:    write current information of each element you find to the console
|                      :   Pipe command. Get the command output and pass it to another

Finding its Type
The next code shows the type of 'flowers' as an Object.


Finding an array element by its index


Add another array item to the array. 'Sun Flower' is added. Adding element using the += operator is slow, using the System.Collections.ArrayList as a New Object is faster for additions. 


Find the last elements(array items) of the array. It is zero based. 
[ .. ] is called a range operator.


Reversing an array (last becomes first and first becomes last). :: stands for static member of a class


Start with an empty array and add elements to it. @ is array operator and @() is an empty array.


Searching with wild cards in an array (in names above)

Note:
*a finds two items
*ar finds only one

Create a 2D array.

Find the second element of the second array in the above


Add a new item to the second array





Friday, March 23, 2018

How do you use a SplitView XAML control in a UWP Project?

You can get a basic start with the SplitView Control here.

You could replace the default 'grid' in a Blank UWP project shown here:

DefaultBlank.png

Use the following in its place:


SplitView sample.png

This is ready for build. Build and run the app in the Local Computer.
This is what you see when the app is run.


There is a Pane (the SplitView. Pane and there is a TextBlock named 'Content' in the Grid.

Now I change the OpenPanelLenth (presently 296) to, say 150 and build and run the app. What do I see?


SplitView 2

Basically you can now see more content.

In the 'Pane' you can place controls and hook up events to them to display related 'Content' in the Content (i.e., inside the 'grid' control).

How do I display using code an image from the Assets in a UWP project?


Let us take the simple example of a button click event displaying an image stored in the project's Asset folder on the page.

At a minimum we create a UWP Blank project. Then we add a StackPanel. In the StackPanel we place two named elements, a button and an image by providing the NAME property for the controls.

Basic Page design MainPage.xaml



Add an external image to Asset folder (you could also use an existing image)

We bring in an external image into the Asset Folder using:


Since we configured a click event in the XAML, this BTN_Click event code will be present in the MainPage.xaml.cs as shown.

Add code to MainPage,xaml.cs


We create an instance of Image as a new image. The source for this image has to change from System.Uri to Windows.Foundation.Uri that UWP requires and hence the conversion. 


However, code needs fixing, and there is a fix with a link. You can safely click this to modify. This includes a new using reference (using Windows.UI.Xaml.Media.Imaging) as shown. 

The this.BaseURI is now referencing the ASSETS folder's content that we added as shown.


Build, Deploy and RUN

This completes the code and the Project builds without errors. When you run it in the Local Machine as you have done with the others you see this page.

App Display

App Display after button click