sábado, 31 de agosto de 2019

Integrating Python in C# .Net (or other language) part 1

Now more than ever, that Python is inevitable. Not my favorite tool to build applications though, it is extremely useful for scripting, prototyping, data analysis, machine learning, and so on.

I decided to write a little bit about my journey with Python as a scripting tool for .Net applications and present how a really simple solution has made me so happy. So I intend to cover IronPython (https://ironpython.net/), Python.net (https://github.com/pythonnet/pythonnet), and finally a pure Python implementation that made incredibly easy to integrate Python with C# .Net (or other language).

Back in the day, as a mostly C# developer, the task was to open up a world of possibilities to the users... TL;DR.

I go straight to the code part. So if you are interested, please leave a comment: I promise to write further when at least someone is interested in.

:D

quarta-feira, 15 de maio de 2013

Implementing partitioned tables in SQL Server 2008 (or superior) Standard Edition

In this examples it's shown how to partition data between tables by Months of Year and at the same time having just one view to select from and to insert into.

First of all, three tables are created with the same structure for the months: Jan/2012, Feb/2012 and Mar/2012.
Constraint check will keep each table with its respectively data on insert. It seems to work on update as well, but further tests are needed to ensure.

CREATE TABLE [dbo].[Data_2012_01]
(
 [DateTime] DATETIME2(3) NOT NULL,
 [TagId] INT NOT NULL,
 CONSTRAINT CK_Data_2012_01 CHECK ([DateTime] >= CAST('2012-01-01' AS DATETIME2) AND [DateTime] < CAST('2012-02-01' AS DATETIME2)),
 [Value] FLOAT NOT NULL
 CONSTRAINT PK_Data_2012_01 PRIMARY KEY ([DateTime], [TagId])
)
CREATE TABLE [dbo].[Data_2012_02]
(
 [DateTime] DATETIME2(3) NOT NULL,
 [TagId] INT NOT NULL,
 CONSTRAINT CK_Data_2012_02 CHECK ([DateTime] >= CAST('2012-02-01' AS DATETIME2) AND [DateTime] < CAST('2012-03-01' AS DATETIME2)),
 [Value] FLOAT NOT NULL
 CONSTRAINT PK_Data_2012_02 PRIMARY KEY ([DateTime], [TagId])
)
CREATE TABLE [dbo].[Data_2012_03]
(
 [DateTime] DATETIME2(3) NOT NULL,
 [TagId] INT NOT NULL,
 CONSTRAINT CK_Data_2012_03 CHECK ([DateTime] >= CAST('2012-03-01' AS DATETIME2) AND [DateTime] < CAST('2012-04-01' AS DATETIME2)),
 [Value] FLOAT NOT NULL
 CONSTRAINT PK_Data_2012_03 PRIMARY KEY ([DateTime], [TagId])
)
Second, a view is created:
CREATE VIEW [dbo].[Data] WITH SCHEMABINDING AS
SELECT [DateTime], [TagId], [Value] FROM [dbo].[Data_2012_01]
UNION ALL
SELECT [DateTime], [TagId], [Value] FROM [dbo].[Data_2012_02]
UNION ALL
SELECT [DateTime], [TagId], [Value] FROM [dbo].[Data_2012_03]
Third (optional), some data are inserted into that view:
INSERT INTO [dbo].[Data] ([DateTime], [TagId], [Value])
VALUES ('2012-01-01', 1, 1)
 
INSERT INTO [dbo].[Data] ([DateTime], [TagId], [Value])
VALUES ('2012-02-01', 1, 1)

INSERT INTO [dbo].[Data] ([DateTime], [TagId], [Value])
VALUES ('2012-03-01', 1, 1)
At last, execution plan is checked to ensure performance gain.
SELECT [DateTime], [TagId], [Value] FROM [dbo].[Data_2012_02] WHERE [DateTime] BETWEEN '2012-02-03' AND '2012-02-04'
SELECT [DateTime], [TagId], [Value] FROM [dbo].[Data] WHERE [DateTime] BETWEEN '2012-02-03' AND '2012-02-04'


Regardless all unions, the view searched just in the right tables.

quarta-feira, 20 de junho de 2012

Logging, Authorizing and Performance analysis on method calls

Intercepting method calls in order to:

  - Analyze the implementation performance
  - Authorize using
  - Log access

With use cases implemented by business class methods, it is a interesting way intercept the calls of these methods and apply these strategies.

How to intercept methods:
The instance of business should be created through a DynamicProxy.
The interceptable methods must be virtual, so that the proxy can override it.
Installs Castle.Core the project, obtained via NuGet, which provides a great API to intercept methods.

This recipe is for properties as well.

Below there is a simple application which implements the idea.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace InterceptingApp
{
    using Castle.DynamicProxy;
    using System.Reflection;

    class Program
    {
        static void Main(string[] args)
        {
            var user = new User { Login = "It's me" };
            var allowedMethods = new List<MemberInfo> 
            {
                typeof(MyBusiness).GetMethod("GetProducts") 
            };

            var performanceInterceptor = new PerformanceInterceptor();
            var authorizeInterceptor = new AuthorizeInterceptor(allowedMethods);
            var logInterceptor = new LogInterceptor(user);

            var generator = new ProxyGenerator(new PersistentProxyBuilder());
            var business = generator.CreateClassProxy<MyBusiness>(performanceInterceptor, authorizeInterceptor, logInterceptor);

            try
            {
                // This call was allowed, so it should works just fine
                Console.WriteLine("Products:");
                foreach (var product in business.GetProducts())
                {
                    Console.WriteLine("Product name: {0} price: {1:c}", product.Name, product.Price);
                }

                
                // This call was not allowed, so it should throw an exception
                Console.WriteLine("BestSellers:");
                foreach (var product in business.GetBestSellers())
                {
                    Console.WriteLine("Product name: {0} price: {1:c}", product.Name, product.Price);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
    }

    public class MyBusiness
    {
        public virtual IEnumerable<Product> GetProducts()
        {
            System.Threading.Thread.Sleep(100);
            return new List<Product> 
            {
                new Product { Name = "Beer", Price = 1.80M },
                new Product { Name = "Soda", Price = 1.50M },
                new Product { Name = "Juice", Price = 2.15M }
            };
        }

        public virtual IEnumerable<Product> GetBestSellers()
        {
            System.Threading.Thread.Sleep(100);
            return new List<Product> 
            {
                new Product { Name = "Juice", Price = 2.15M }
            };
        }
    }

    public class User
    {
        public string Login { get; set; }
    }

    public class Product
    {
        public string Name { get; set; }
        public decimal Price { get; set; }
    }

    class PerformanceInterceptor : IInterceptor
    {
        public void Intercept(IInvocation invocation)
        {
            var start = DateTime.Now;
            try
            {
                invocation.Proceed();
            }
            catch
            {
                throw;
            }
            finally
            {
                var end = DateTime.Now;

                Console.WriteLine("[Performance]: Method {0}.{1} takes {2}",
                    invocation.Method.DeclaringType.Name,
                    invocation.Method.Name,
                    end - start);
            }
        }
    }

    class AuthorizeInterceptor : IInterceptor
    {
        IEnumerable<MemberInfo> _allowedMethods;
        public AuthorizeInterceptor(IEnumerable<MemberInfo> allowedMethods) { _allowedMethods = allowedMethods; }

        public void Intercept(IInvocation invocation)
        {
            if (_allowedMethods.Contains(invocation.Method))
            {
                Console.WriteLine("[Authorize]: Granted! You are allowed to access this method");
                invocation.Proceed();
            }
            else
            {
                throw new Exception("[Authorize]: Denied! You are not allowed to access this method.");
            }
        }
    }

    class LogInterceptor : IInterceptor
    {
        User _user;
        public LogInterceptor(User user) { _user = user; }

        public void Intercept(IInvocation invocation)
        {
            Console.WriteLine("[Log]: Method {0}.{1} was called by '{2}' at {3}",
                 invocation.Method.DeclaringType.Name,
                 invocation.Method.Name,
                 _user.Login,
                 DateTime.Now);

            invocation.Proceed();
        }
    }
}


segunda-feira, 12 de março de 2012

With statement for C# through extension method

Extension methods are really cool feature. I was missing the VB with statement recently, so I thought it will be cool to implement a really simple extension method to do it. Below are a VB.Net, C#, short C# and C# through With extension method: VB.Net
With dataGridView.Columns["customer"]
    .Width = 200
    .HeaderText = "Customer"
    .HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
End With
C#
dataGridView.Columns["customer"].Width = 200;
dataGridView.Columns["customer"].HeaderText = "Customer";
dataGridView.Columns["customer"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
C# short
var column = dataGridView.Columns["customer"];
column.Width = 200;
column.HeaderText = "Customer";
column.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
C# With through extension method
dataGridView.Columns["customer"].With(column => {
    column.Width = 200;
    column.HeaderText = "Customer";
    column.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
});

static public class ObjectExtensions
{
    static public void With<T>(this T o, Action<T> action)
    {
        action(o);
    }
}

sexta-feira, 7 de outubro de 2011

Simple Parallel ForEach in C#.NET 2.0

A simple Parallel ForEach implementation for Microsoft C#.NET 2.0:
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace ParallelTest
{
    public class Parallel
    {
        public delegate void Process();

        public static void ForEach<T>(IEnumerable<T> enumerable, Action<T> action) 
        {
            IEnumerator<T> enumerator = enumerable.GetEnumerator();

            int threadCount = Environment.ProcessorCount;

            Process process = delegate()
            {
                while (true)
                {
                    T item = default(T);

                    lock (enumerator)
                    {
                        if (enumerator.MoveNext())
                            item = enumerator.Current;
                        else
                            return;
                    }

                    action(item);
                }
            };

            IAsyncResult[] asyncResults = new IAsyncResult[threadCount];

            for (int i = 0; i < threadCount; ++i)
                asyncResults[i] = process.BeginInvoke(null, null);

            for (int i = 0; i < threadCount; ++i)
                process.EndInvoke(asyncResults[i]);
        }
    }
}

segunda-feira, 3 de outubro de 2011

How to post code?

Follow the example below:


<pre class="brush: html"><h1>
html code here...</h1>
</pre>


Should render like

html code here...

List of available languages:


  • cpp
  • csharp
  • css
  • html
  • java
  • jscript
  • php
  • python
  • ruby
  • sql
  • vb
  • xml
  • perl

syntax highlighting tutorial

Syntax coloring test

public class Syntax
{
    abstract Language Language { get; set; }
}

Source code:

<pre class="brush: csharp">
public class Syntax
{
    abstract Language Language { get; set; }
}
</pre>