По-русски Notes

Friday, July 01, 2005

Join-like functionality in Microsoft Shell

Some implementations have been proposed. They either imply constructing new custom objects including properties of both inputs or delegate defining of the join operation to the user. Let's implement another method to join data from several streams...

The task is to produce an object having properties of two or more input objects. The first parallel is SQL JOIN clause. It virtually constructs a new table comprised by selected columns of two source tables. In object-oriented environment this sounds like declaring a class for new objects and constructing its instances. The use of reflection to emit and instantiate the class will not be investigated here, but MshObject class will be used to construct new objects.

First the point of view should be changed slightly. There are two input sequences of objects. Usually the first sequence is delivered via the pipeline while the second was generated earlier and passed as a parameter. Selected set of properties could be added to existing object from pipeline input exploiting the obvious asymmetry. MshNoteProperty class will help to add properties. The following function adds properties according to given hashtable.

function global:add-note{
  param([collections.hashtable] $hash)
  process{
    foreach ($key in $hash.keys) {
      # process each key-value pair
      $note=new-object management.automation.mshNoteProperty 
                                   -arguments $key, $hash[$keys]
      $_.mshObject.properties.add($note)
    }
    $_
  }
}

MSH> gci | add-note @{'SomeProp'='SomeValue'} | ft name, someProperty
Name                                SomeProperty
----                                ------------
foo.txt                             SomeValue
...

Values could be generated by a script block. The scriptblocks use $_ automatic variable to access current object.

      ...
      # process each key-value pair
      if ($hash[$key] -is [scriptblock]) {$v=&$hash[$key]}
      else {$v=$hash[$key]}
      $note=new-object system.management.automation.mshNoteProperty 
                                                 -arguments $key, $v
      $_.mshObject.properties.add($note)
      ...

MSH> gci | 
>> add-note @{'Tp'={$_.getType()},'Btp'={$_.getType().baseType} |
>> ft name,tp,btp

Name             Tp                        Btp
----             --                        ---
foo.txt          System.IO.FileInfo        System.IO.FileSystemInfo
Coockies         System.IO.DirectoryInfo   System.IO.FileSystemInfo
...

SQL JOIN may return data from the first table more than one time. To mimic this behavior a new object should be constructed. MshObject class seems being suitable.

  process{
    # normalize args
    if ($hash -eq $null) {return}
    elseif ($hash -is [system.collections.hashtable]) {$ha=@($hash)}
    elseif ($hash -is [array]) {$ha=$hash}
    else {
      throw 'Please provide a hashtable or an array of hashtables.'
    }

    # process each hashtable
    foreach ($h in $ha) {
      $res=new-object system.management.automation.mshObject $_
      foreach ($key in $h.keys) {
        # process each key-value pair
        if ($h[$key] -is [scriptblock]) {$v=&$h[$key]}
        else {$v=$h[$key]}
        $note=new-object system.management.automation.mshNoteProperty 
                                                   -arguments $key, $v
        $res.mshObject.properties.add($note)
      }
      $res
    }
  }
  ...

MSH> gci|
>> add-note @(
>>   @{'Tp'={$_.getType()}},
>>   @{'Btp'={$_.getType().baseType}}
>> ) | ft name,tp,btp

Name               Tp                      Btp
----               --                      ---
foo.txt            System.IO.FileInfo      
foo.txt                                    System.IO.FileSystemInfo
...

Finally add support of script block producing hashtables.

      ...
      # normalize args
      if ($hash -is [scriptblock]) {$ht=&$hash}
      else {$ht=$hash}

      if ($ht -eq $null) {return}
      elseif ($ht -is [system.collections.hashtable]) {$ha=@($ht)}
      elseif ($ht -is [array]) {$ha=$ht}
      else {
        throw 'Please provide a hashtable, 
          an array of hashtables, or a scriptblock returning such an array.'
      }
        
      # process each hashtable
      ...

MSH> gci .\dir1 | add-note {
>>  foreach ($f in gci .\dir2) {
>>    if($f.name -eq $_.name){@{dir1=$_;dir2=$f}}
>>  }
>>} | ft dir1,dir2

Note that the scriptblock not only generates values for the properties, but functions as ON part of SQL JOIN clause. In the following example the scriptblock filters the rows like WHERE.

MSH> gci | add-note {
>>  if ($_ -is [io.directoryInfo]) {
>>    $s=0
>>    foreach ($f in gci $_.fullName -rec) 
>>      {$s+=$f.length}
>>    @{TotalLength=$s}
>>  }
>>} | ft name, totalLength

Name                          TotalLength
----                          -----------
Links                                1554
Vision                              12362
...

6 Comments:

At 9/15/2005 9:25 PM, Blogger louisalexander02065556 said...

i thought your blog was cool and i think you may like this cool Website. now just Click Here

 
At 10/14/2005 10:32 AM, Blogger mandyjackson88180307 said...

I really enjoyed your blog. This is a cool Website Check it out now by Clicking Here . I know that you will find this WebSite Very Interesting Every one wants a Free LapTop Computer!

 
At 12/18/2005 3:20 PM, Blogger job opportunitya said...

Fine blog. I found your site suitable for another
visit! And when I'm able to surf the web, I look for
blogs as great as your work.
Want to see top notch work, peep my american board of plastic surgery blog site for the bomb work!

 
At 12/19/2005 5:43 PM, Blogger edingals75849277 said...

Make no mistake: Our mission at Tip Top Equities is to sift through the thousands of underperforming companies out there to find the golden needle in the haystack. A stock worthy of your investment. A stock with the potential for big returns. More often than not, the stocks we profile show a significant increase in stock price, sometimes in days, not months or years. We have come across what we feel is one of those rare deals that the public has not heard about yet. Read on to find out more.

Nano Superlattice Technology Inc. (OTCBB Symbol: NSLT) is a nanotechnology company engaged in the coating of tools and components with nano structured PVD coatings for high-tech industries.

Nano utilizes Arc Bond Sputtering and Superlattice technology to apply multi-layers of super-hard elemental coatings on an array of precision products to achieve a variety of physical properties. The application of the coating on industrial products is designed to change their physical properties, improving a product's durability, resistance, chemical and physical characteristics as well as performance. Nano's super-hard alloy coating materials were especially developed for printed circuit board drills in response to special market requirements

The cutting of circuit boards causes severe wear on the cutting edge of drills and routers. With the increased miniaturization of personal electronics devices the dimensions of holes and cut aways are currently less than 0.2 mm. Nano coats tools with an ultra thin coating (only a few nanometers in thickness) of nitrides which can have a hardness of up to half that of diamond. This has proven to increase tool life by almost ten times. Nano plans to continue research and development into these techniques due to the vast application range for this type of nanotechnology

We believe that Nano is a company on the move. With today�s steady move towards miniaturization we feel that Nano is a company with the right product at the right time. It is our opinion that an investment in Nano will produce great returns for our readers.

Online Stock trading, in the New York Stock Exchange, and Toronto Stock Exchange, or any other stock market requires many hours of stock research. Always consult a stock broker for stock prices of penny stocks, and always seek proper free stock advice, as well as read a stock chart. This is not encouragement to buy stock, but merely a possible hot stock pick. Get a live stock market quote, before making a stock investment or participating in the stock market game or buying or selling a stock option.

 
At 1/21/2006 4:01 PM, Blogger michelprice49425664 said...

I read over your blog, and i found it inquisitive, you may find My Blog interesting. So please Click Here To Read My Blog

http://pennystockinvestment.blogspot.com

 
At 5/06/2006 7:09 AM, Blogger arnoldbencon78941103 said...

Get any Desired College Degree, In less then 2 weeks.

Call this number now 24 hours a day 7 days a week (413) 208-3069

Get these Degrees NOW!!!

"BA", "BSc", "MA", "MSc", "MBA", "PHD",

Get everything within 2 weeks.
100% verifiable, this is a real deal

Act now you owe it to your future.

(413) 208-3069 call now 24 hours a day, 7 days a week.

 

Post a Comment

<< Home