Category Archives: Learning

The Pleasure-Pain

The_Citadel_1024

But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?


You cannot separate the sugar rush from the sugar crash…


But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?

On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure other greater pleasures, or else he endures pains to avoid worse pains.


Sources:

The Pleasure-Pain Syndrome

https://www.lievekamp.nl/resize-test/frame-content.html

Advertisements

Up and running with DNX/DNVM/DNU

PLEASE NOTE THAT THIS CONTENT IS COPY-PASTED FROM : http://www.sblackler.net/2015/05/02/Up-And-Running-With-DNX-DNVM-DNU/

 

There’s been quiet a few announcements throughout Build 2015. One of the many aspects that I am excited about is the new cross platform runtime environment called DNX, or the .NET Execution Environment.

What is DNX?

The .NET Execution Environment (DNX) provides a consistent development and execution environment across multiple platforms (Windows, OS X, Linux), multiple CPU architectures (x86, x64) and different .NET flavors (.NET Framework, .NET Core and Mono).

There has been a lot of talk about how we can run more and more cross-platform during the conference and that’s what DNX is all about. There are several different pieces to DNX, including:

  • DNX (distribution): A NuGet package containing the implementation of the new environment. The .NET Core DNX distribution includes CoreCLR and the base parts of CoreFX. The .NET Framework and Mono DNX distribution only contain the DNX components.
  • DNX (commandline tool): The command line tool controls various app operations, primarily launching.
  • DNVM: This is a tool for acquiring and managing DNX distributions. While it’s not part of DNX itself, it plays an administrator role for DNX, providing version management among other things.
  • DNU: The NuGet client for DNX. NuGet.exe is not used.

What all of this means is that we can run console apps/asp.net 5/entity framework on anything that can run DNX. So think Mac’s, Linux, Raspberry Pi’s etc.

How CoreCLR differs from DNX

CoreCLR contains the .NET Core runtime and the base library called mscorlib. It includes the garbage collector, JIT compiler, base .NET data types and many low-level classes.

DNX contains the CoreCLR and the base parts of CoreFx which includes classes for collections, file systems, console, XML, async and many others.

Installing DNVM/DNX

Before you can install any DNX runtimes you need to install DNVM. There are a few ways to install the DNVM. Personally I prefer running the following powershell command:

C:\Dev> powershell -NoProfile -ExecutionPolicy unrestricted -Command "&{iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/aspnet/Home/dev/dnvminstall.ps1'))}"

Once that command has been executed, you will see similar console output to the following:

Using temporary directory: C:\Users\SBLACK~1\AppData\Local\Temp\dnvminstall
Downloading DNVM.ps1 to
Downloading DNVM.cmd to
Installing DNVM
Installing .NET Version Manager to C:\Users\sblackler\.dnx\bin
Creating destination folder 'C:\Users\sblackler\.dnx\bin' ...
Installing 'dnvm.ps1' to 'C:\Users\sblackler\.dnx\bin' ...
Installing 'dnvm.cmd' to 'C:\Users\sblackler\.dnx\bin' ...
Adding C:\Users\sblackler\.dnx\bin to Process PATH
Adding C:\Users\sblackler\.dnx\bin to User PATH
Adding C:\Users\sblackler\.dnx to Process DNX_HOME
Adding C:\Users\sblackler\.dnx to User DNX_HOME

After that has been completed, restart powershell and you should have the DNVM bits installed. You can verify this using the following command:

dnvm

Installing a version of DNX

To install the latest .NET Core-based DNX, use the dnvm install command:

dnvm install -r coreclr latest

This will install the 32-bit version of .NET Core. If you want the 64-bit version, you can specify processor architecture.

dnvm install -r coreclr -arch x64 latest

Then, you can see the currently installed DNX versions with dnvm list:

dnvm list

Which, after running the above commands, results in the following output from the command above:

Active Version           Runtime Architecture Location                         Alias
------ -------           ------- ------------ --------                         -----
       1.0.0-beta5-11682 coreclr x64          C:\Users\sblackler\.dnx\runtimes
  *    1.0.0-beta5-11682 coreclr x86          C:\Users\sblackler\.dnx\runtimes

Interestingly, when you install each of the runtimes, the installer generates native images for the target machine which improves the start up performance.

Switching between DNX runtimes

If you have the need to switch between different versions of DNX, you can use the dnvm use command which has the following format:

dnvm use -r coreclr -arch <arch> <version>

Where <arch> is, replace with the architecture that you would like to have; eg: x86, x64. With <version> replace with the version number that is shown in dnvm list:

dnvm use -r coreclr -arch x86 1.0.0-beta5-11682

The above is used to switch the current runtime to the x86 version of the 1.0.0-beta5-11682 build of DNX. To switch to the x64 version, simply switch out x86 for x64:

dnvm use -r coreclr -arch x64 1.0.0-beta5-11682

This only makes the DNX runtime apply to the current terminal session. We do have options of adding it to the user profile or the system profile (the system profile is not recommend). Usually, when we run dnvm use we get something along the lines of:

Adding C:\Users\sblackler\.dnx\runtimes\dnx-coreclr-win-x64.1.0.0-beta5-11682\bin to process PATH

Which gives us the following output in dnvm list:

Active Version           Runtime Architecture Location                         Alias
------ -------           ------- ------------ --------                         -----
  *    1.0.0-beta5-11682 coreclr x64          C:\Users\sblackler\.dnx\runtimes
       1.0.0-beta5-11682 coreclr x86          C:\Users\sblackler\.dnx\runtimes

Note the astrix above? That signifies the current running DNX environment. If we wish to persist that environment within our profile we add -p to our dnvm use command:

C:\Dev> dnvm use -r coreclr -arch x64 1.0.0-beta5-11682 -p
Adding C:\Users\sblackler\.dnx\runtimes\dnx-coreclr-win-x64.1.0.0-beta5-11682\bin to process PATH
Adding C:\Users\sblackler\.dnx\runtimes\dnx-coreclr-win-x64.1.0.0-beta5-11682\bin to user PATH

This time you see DNVM has added the run time to the user path, so the next time you start a terminal session, that will be the selected runtime.

Aliasing DNX runtimes

Aliasing is useful so you don’t have to remember those lovely version strings. To see the current aliases setup in your environment use, dnvm alias. This acts the exact same as the dnvm list command. Here is a sample output:

Alias Name
----- ----
Beta5 dnx-coreclr-win-x86.1.0.0-beta5-11682

You can create aliases in a few ways. The first is to specify the name and the version:

dnvm alias Beta5 -Version 1.0.0-beta5-11682 -Runtime coreclr

Using the above command, DNVM will create an alias for version 1.0.0-beta5-11682 with the x86 architecture on coreclr. Given that I have both x86 and x64 runtimes installed, I would suggest that the order of preference for the runtimes is x86, x64, arm, mono, everything else. However, we can specify a specific architecture with the command:

C:\Dev> dnvm alias Beta5 -Version 1.0.0-beta5-11682 -Architecture x64 -Runtime coreclr
Updating alias 'Beta5' to 'dnx-coreclr-win-x64.1.0.0-beta5-11682'

By specifying the architecture and using the same alias name, the DNVM has updated the alias to use the correct runtime. -Version, -Runtime and -Architecture can be shortened to -v, -r and -a respectively. If no runtime is specified, DNVM will default to the clr runtime.

When you have aliasing setup, you can use the alias within the dnvm use command. Note that you have to specify the runtime if you are not selecting the clr option:

C:\Dev> dnvm use Beta5x86 -r coreclr
Adding C:\Users\sblackler\.dnx\runtimes\dnx-coreclr-win-x86.1.0.0-beta5-11682\bin to process PATH
C:\Dev> dnvm list

Active Version           Runtime Architecture Location                         Alias
------ -------           ------- ------------ --------                         -----
        1.0.0-beta5-11682 coreclr x64          C:\Users\sblackler\.dnx\runtimes Beta5
   *    1.0.0-beta5-11682 coreclr x86          C:\Users\sblackler\.dnx\runtimes Beta5x86

Starting a DNX application

Before you can run your program, you need to ensure that your packages are restored. So enter dnu:

dnu restore

As mentioned earlier, dnu is all the other tools. It does builds, package restore and alot more. It can be thought of as a NuGet client plus more. Once the packages are restored, you can run your application:

dnx <path> run

All you need to do is specify a path, or use . as the path. The dot signifies the current working directory. If I build a quick console application and run the commands above, I get the following output:

C:\dev\test\ConsoleApp1> dnu restore
Restoring packages for C:\dev\test\ConsoleApp1\src\ConsoleApp1\project.json
Writing lock file C:\dev\test\ConsoleApp1\src\ConsoleApp1\project.lock.json
Restore complete, 146ms elapsed
C:\dev\test\ConsoleApp1> dnx . run
Hello DNX

And that’s it. That’s the basics of DNX/DNVM/DNU.

ASP.NET 6 Gotchas…

  1. entity framework migrations only work with classname plis a suffix of id as table id, otherwise, an error… (BY DEFAULT…)
  2. do not mix services.AddTransient<AccountService>(); instances with services.AddSingleton<IAccountRepository, AccountRepository>(); services for dependence injection.they result to some funny exception without proper details
  3. package management is easier but with wierd errors upon upgrade or adding a new package, like:

http://stackoverflow.com/questions/33180380/error-iconfigurationprovider-claims-it-is-defined-in-microsoft-framework-conf/33196496

http://openstackwiki.org/wiki/ASP.Net5_Startup.cs_ConfigurationBuilder

  1. there is no bearer tokens, but i suppose they are working on that. you only got Cookies…
  2. wwwroot is not a normal folder, so you cant include it i your scripts or styles reference
  3. how to configure mvc default routing

you have to do the following…

routes.MapRoute(

name: “default”,

template: “{index?}”,

defaults: new { controller = “Home”, action = “Index” }

);

and use the following controller

public class HomeController : Controller

{

public IActionResult Index()

{

return View();

}

 

[Route(“about”)]

public IActionResult About()

{

ViewData[“Message”] = “Your application description page.”;

return View();

}

 

[Route(“contact”)]

public IActionResult Contact()

{

ViewData[“Message”] = “Your contact page.”;

return View();

}

}

}

this will serve the following urls:

http://localhost:3453/

http:localhost:3453/index

http:localhost:3453/about

http:localhost:3453/contact

also, the above will handle unavailable routes without the cost of redirecting, eg, http://localhost:3453/404 will show the page in http://localhost:3453/ given the route http://localhost:3453/404 isnt specified.

  1. Ajax and cookies are not good friends…

Where CSS3 box-shadow property saves…

Creating double border

You can easily create a double border with box-shadow property simply by doing this…

box-shadow:0 0 0 2px #2A0D0D, 0 0 0 4px #cf8a05;

border

 

Stripping HTML elements with all borders

If you know what is stripping something like a button, you realize that setting unequal borders results to an ugly element, so you can use box-shadow property

box-shadow: -5px 0 0 0 #39579b;

loginwith

 

jQuery plugin to dynamically register events on dynamically added items ondemand

 

Durandal With Github Pages

http://mckabue.github.io/#resume

i just finished creating my resume using Durandal and github pages, and men, its awesome. The only down side is durandal isn’t very friendly with mobile devices or low internet connection.

Source code at Github

Capture Capture2

Validating my Campus registration Number using HTML5 validation

i am creating a project application for someone that i need that i need validating the registration number of a student… i wanted to follow the easiest was of doing it instead of using plugins…

i need to validate the following numbers:

  1. IN17/24456/2012

  2. in17/24456/12

  3. 1Np17/24456/2012

  4. inP17/24456/12

AND HEY, these needs to be ultimately strict, as strict as possible…

here is HTML5 magic

<form>
<input pattern="(?:[A-Za-z]{2}[0-9]{2}|[A-Za-z]{2}[Pp]{1}[0-9]{2})\/[0-9]{5}\/([1-2]{1}[90]{1}[0-9]{2}|[901]{1}[0-9]{1})" required/>
<input type="submit" value="Reistration number"/>
</form>

here is the a demo

and the application on apphabour

and the github repo

NB: i havent implemented this yet, but i will very soon…

please vote for my above Appharbour Application

Using Select2.js jQuery plugin

Lately, I have been on a project that required a heavy use of HTML select. I choose select2 after a period of comparison which other plugins like choose.js and selectize.js. it was very easy to get along, thanks to the almost enough documentation. i am writing this post to answer my own question which I asked at stackoverflow, since I never got even a single relevant response. Its really scaring how some stackoverflow boys want to cause trouble when you mean no harm. I mean, if you see someone asking a SLIGHTLY AMBIGUOUS question, it means that most likely, they only have the idea of what is to be done. Sometimes you don’t even know what to Google, so you feel inclined to ask a question so at least you get your search terms right, or should stackExchange have another site called “search-terms”??

Anyway, here was my question…

Autofilling inputs and selects with select2

and here is my answer…

<form style="width: 350px;" id="" class="navbar-form form" role="form" data-bind="">
<dl class="dl-horizontal">
<dt>President</dt>
<dd><select data-placeholder='President' id="president" class="select11" style="width: 300px;">
<option></option>
<option value="AK">Mickel</option>
<option value="HI">James</option>
<option value="CA">Sandney</option>
</select>
</dd>
<dd>
<input id="runningMate" placeholder="running mate" class="form-control" type="" name="" style="width: 300px;" disabled>
</dd>
</dl>

<dl class=”dl-horizontal”>
<dt>Secretary General</dt>
<dd><select data-placeholder=’Secretary General’ class=”select11″ style=”width: 300px;”>
<option></option>
<option value=”AK”>Alaska</option>
<option value=”HI”>Hawaii</option>
<option value=”CA”>California</option>
<option value=”NV”>Nevada</option>
<option value=”OR”>Oregon</option>
</select>
</dd>
</dl>

<dl class=”dl-horizontal”>
<dt>Financial Controller</dt>
<dd><select data-placeholder=’Financial Controller’ class=”select11″ style=”width: 300px;”>
<option></option>
<option value=”AK”>Alaska</option>
<option value=”HI”>Hawaii</option>
<option value=”CA”>California</option>
<option value=”NV”>Nevada</option>
<option value=”OR”>Oregon</option>
</select>
</dd>
</dl>

<dl class=”dl-horizontal”>
<dt>Faculty representative</dt>
<dd><select data-placeholder=’Choose Faculty’ id=”faculty” class=”select11″ style=”width: 300px;”>
<option></option>
<option value=”fist”>fist</option>
<option value=”commerce”>commerce</option>
<option value=”spas”>spas</option>
</select>
</dd>
<dd><select data-placeholder=’Faculty representative’ id=’representative’ class=”select11″ style=”width: 300px;”>
<option></option>
</select>
</dd>
</dl>
</form>

and here is the script that is doing the magic…
<script>
$(function(){
$('#president').select2({
allowClear: true
})
.on("select2-selecting", function(e) {
if (e.choice.text == "Mickel"){ $('#runningMate').val('Mercy is ' + e.choice.text + "'s Running Mate"); }
else if (e.choice.text == "James"){ $('#runningMate').val('Mukami is ' + e.choice.text + "'s Running Mate"); }
else if (e.choice.text == "Sandney"){ $('#runningMate').val('Moses is ' + e.choice.text + "'s Running Mate"); }
})

$(‘#representative’).select2({
allowClear: true
});

$(‘#faculty’).select2({
allowClear: true
})
.on(“select2-open”, function() {
fist=new Array(“jamo”,”sam”,”kaka”,”jane”);
commerce=new Array(‘chalo’,’mark’,’timo’);
spas=new Array(‘ali’,’Elke’,’Fred’,’Bobby’,’Frits’);

$(‘#faculty’).change(function(){ populateSelect(); });
function populateSelect(){
var representative = $(‘#faculty’).val();
$(‘#representative’).select2(“data”,””);
$(‘#representative’).html(‘<option></option>’);

eval(representative).forEach(function(t) {
//$(‘#representative’).select2(“val”:””);
$(‘#representative’).append(‘<option>’+t+'</option>’);
});
}
})
});
</script>

and this is what i got…

select1

select2

My Best Assignment: “Pure Knockoutjs”

Demo

The following are the topics I will discuss:

  1.   What is Knockoutjs?
  2.   What was my project? And how was it born?
  3.   Why did I choose knockoutjs for my project?
  4.    Screenshots of the project
  5.   How you, not me…how you will extend this project.
  6.   Creating a 100% static blog with Knockoutjs.
  7.   The future of Knockoutjs.
  8.   Let’s learn Knockoutjs.
  9.   Help me with Knockoutjs View-Models [seriously please…]

What is Knockoutjs?
Knockoutjs is a JavaScript library. It’s a framework created meant for use just like a pure JavaScript code, but has a different implementation, a better implementation, which is MVVM. For example, what would you do if you wanted to produce the HTML code dynamically, like when you want to create a customizable user interface? Using Knockoutjs, this a task of simply implementing a binding element; like for foreach. Checkout a simple implementation Here.
What was my project? And how was it born?
Our lecturer gave us an assignment. The requirements of the project were to create a primary school application with any programming language; be C++, C, Java, C#, VB, Delphi, or any other existing language, including Lisp if you wish. So, here is how I ended up using Knockoutjs: I am doing a personal project with SignalR, Knockoutjs, ASP.NET MVC, and some other technologies. And since Knockoutjs remains to be a subject to learn to me, I thought I would exploit my chances and risk doing the project in JavaScript, Knockoutjs to be precise…. The application had to do the following:

  1.  Capture student registration details
  2. Capture fee payment records
  3. Teachers on duty
  4. Subject offered in high school
  5. Class teachers
  6. Rank students in examinations
  7. Produce order of merit list per class
  8. Display the most improved student in each class and the whole school
  9. Print report cards in PDF
  10.  Generate trial balance for the school
  11.  Generate income statement

NB: This was not a final year, or end of course project. In fact, we only had two weeks to do a prototype that could demonstrate all that, because he wanted to explain a point in class using those projects [Software Development].

Why did I choose knockoutjs for my project?
⦁    Knockoutjs was part of a personal project that am currently doing
⦁    Knockoutjs has nice templates [Templating with KO observables]
⦁    I wanted to do a simple static site that could do complex tasks without the need of server side code
⦁    Is very easy to reuse already existing examples online, as it is simple to learn

Screenshots of the project

image1

image2

image3

How you, not me…how you will extend this project.
I will give the code of the whole project at the bottom, and youwill also find a link to a running demo. If you are curious to see all those functionalities running and working, you will be disappointed to find that I didn’t implement some. This is all due to time constrains. We are starting our exams very soon, and for someone like me who is always concentrating on one thing at a time, I have to stop everything, including my project so I can at least start learning for the exams, yeah, I said reading to pass exams! This is what we do in Kenya, or better said, this is what they encourage in Kenya, “PASSING EXAMS-IF YOU PASS, YOU ARE BRIGHT :: THEORY EXAMS, NO PRACTICALS”
So, that being constant, I left some things un-accomplished, like PDF functionalities, and not because they are so hard to do, it all boils from time, and priorities. Wee, if you are willing to pay me so I can finish that specifically for you, well then, you are very welcome (money and college have always been dependent on each other!!). So, if you want to return HTML pages as PDF files from the browser, completely with client side JavaScript, there is a library called jsPDF. And because I am not willing to work an example for you, I will show you screenshots how jsPDF works. I have downloaded the samples from their github repository, and its working 100%, so there is no need of re-implementing it again.

image4image5image6  image7

Creating a 100% static blog with Knockoutjs.
From my little experience with knockoutjs, am vividly convinced a question that was asked at stackoverflow can be answered. The owner of the question wanted a blog engine created with pure JavaScript (and isn’t Knockoutjs JavaScript? Of course it is…)
I don’t know why something tells me that it would be easier to edit a Knockoutjs template for github pages than using Jekyll. All you have to do, is wrap a HTML list-element with a foreach loop and everything in the Knockoutjs template gets posted on the HTML page, with the order you want. You can also implement something like a search, and many more features like sorting.
I know this is, or might sound stupid if you are an established programmer, but I know people like me, people like us, we value and respect simplicity.

Demo

Question 2

Create a sample algorithm for solving “Factorial“, “Anagram“, “Combination“, and “Permutation