vendredi 26 juillet 2013

Playing with JS and Web API.

One thing really fun is this area, is that more and more data are freely available from/to the Web. Those data can be REALLY free like Weather Real-time information like the service i used below, or NEED APP REGISTRATION.

Note that for a lot of Web API, App Registration is free, it's more for tracking and avoid duplicate App development or that an IP 'get' too much data.

I'm not a Web Developer, but I think that as a developer if you don't know basis of JavaScript and HTML in our 21 century, you would be lost. So my today experiment, I will get Temperature information for my city, and simply display that.

So in the code below I declared 2 HTML label using an ID, temp_celsius, and temp_fraenheit.


<div>
<p> Rennes Today's Temperatures</p>
<ul>
<li><text> Celsius : </text><label id="temp_celsius" ></label></li>
<li><text> Farenheit : </text><label id="temp_farenheit" ></label></li>
<ul>
</div>


The result is here

Rennes Today's Temperatures
  • Celsius :
  • Farenheit :

To feel those 2 HTML label with data, i started using JQUERY (you can live without, but when you become familiar with its notation to access DOM element, you always start with it):


<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>




And now let's call the OpenWeather service . It propose a free Web/REST API to get (or push if you setup your own Weather station) data using JSON format.
My request is "http://api.openweathermap.org/data/2.5/weather?q=Rennes,fr" and in response i received something like:

{"coord":{"lon":-1.67429,"lat":48.11198},"sys":{"country":"FR","sunrise":1374813353,"sunset":1374868234},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"base":"gdps stations","main":{"temp":297.15,"pressure":1012,"humidity":50,"temp_min":297.15,"temp_max":297.15},"wind":{"speed":2.6,"deg":290,"var_beg":250,"var_end":330},"clouds":{"all":36},"dt":1374849000,"id":2983990,"name":"Rennes","cod":200}


One of the advantage of using JQuery is that you can get and parse the JSON data in only 1 call of $.getJSON. The field Temp is express in JKelvin as you can have guess when looking its value (297.15 ...).


<script type="text/javascript">
$(document).ready(function(){
var json_request = $.getJSON("http://api.openweathermap.org/data/2.5/weather?q=Rennes,fr&callback=?", function(data) {
console.log("in cb ????" + data.main.temp);
var celsius = (data.main.temp - 273.15);
$("#temp_celsius").html(celsius);
var farenheit = (celsius * 1.8 ) + 32
$("#temp_farenheit").html(farenheit);


}).done(function() { console.log( "second success" ); })
.fail(function() { console.log( "error" ); });
});
</script>


There is nothing really dynamic after that, but my next step will be nicer, it will be the "Stackoverflow Question Meter" to dynamically show how many question are ask on Stackoverflow and using Google Chart API.

samedi 20 juillet 2013

Counting occurence and histogram

A recurrent question on Stackoverflow [c++] is, hopw can we count word, sentence in string or a text file, or how can we build an histogram showing all unique number in a list and their number of occurence.

In fact every time those question found quite the same answer because in all case, whatever you want count, it can be really easy to do with C++ and STL. If we use the std::map class we can do that in 2 loop.

First, imagine we have a list containing random number.


srand(time(NULL));
std::vector list;
int N = 1 , M = 100;

for (int i = 0; i < M; ++i) { int rnd = M + rand() / (RAND_MAX / (N - M + 1) + 1); list.push_back(rnd); }


Ok, so now we have 100 number with some redundancy, let's count that with a map.


std::map histo;

for (auto rnd : list)
{
histo[rnd] += 1;
}


And it's done !, nothing else .... Just a loop to display the result:


for(auto var = begin(histo) ; var != end(histo) ; ++var)
{
std::cout << var->first << " have " << var->second << " occurence " << std::endl; }


Simple and efficient

Why you really need a "Gravatar" !

At first glance when a web site asked me for a gravatar, I found that annoying and just let my account without avatar or picture of me.

But few week ago I changed my mind, Having a gravatar account let you change your picture once and all the account you linked with, will be automatically update!

At the end, it's just the same thing under Google .... 1 Profile picture for everything, for all the account you have.

So if you want a Gravatar it take only few minutes.

I use mine for Stackoverflow, CoderBits and some other and finally I found that more convenient than uploading a picture for each!

lundi 1 juillet 2013

Don't forget to 'EncodeURL'

In  http://tinyurl.com/qa3nmtv I presented a small script/exe I made to quickly convert selected path (folder or file) from Explorer to a string in the clipboardwhich can be 'paste' in a browser editor as an URL to a local resssource.

In fact I forgot something important in my script. You may hate or love it but Windows allow 'spaces' in path. So I have to convert SPACE to %20, to build a correct URL.

I updated my script this morning:

https://github.com/alexbuisson/Project/tree/master/UsefullScript

Just pull Path2URL.exe, and configure your StExBar like in the following Snapshot.





Visual Studio 2008Randomly crashing.


If you are using VS'08 and a lot of plugin you may experience some random crash when using "Intellisense" functionality (performing a right click in the source code) or when switching from Debug to Release configuration.

 I may be really difficult  for some to figure from where and for why the crash/freeze occur.

In fact I meet that issue and I finally attached another Visual Studio instance with a debugger and realize that VS was hanging somewhere in the FEACP.DLL

That DLL is the engine of Intellisense.

If you have that kind of problem and that you want double check or experiment VS'08 without Intellisense running behind, you just have to rename that dll in your VS'08 setup folder and restart VS.

It's work fine but without Intellisense, you really need additional plugin to provide, some source browsing capabilities ("Find all references", etc...)


Build a script/tool to make your life easier.

Hi,
Just wanted to share some work done to simplify the everyday life of my teammates.

One week ago, one of my colleague had a lot of file path to share through our CRM system and in fact he found that was very boring because he had to copy all network path and apply a transformation on every string. To transform network file or folder path from their UNC or Drive path (mounted drive) representation to a file/folder URL style that can be use to access local system storage through a browser.

To do that he had to 'manually' switch all '\' (windows path separator) to '/' and concatenate 'file://'. I understood it could become a pain to do that kind of boring task, so I developed a simple script/executable to do that for one or multiple selected file or folder with only 1 click from Windows Explorer.

My Teammates and I already used StExBar to improve our Windows Explorer experience. It's a highly customizable tools in which you can add your own custom command, with button and/or hot-key.


As the function we need has only to run under windows, I choose a windows only tool called AutoHotKey to build a script performing multiple path selection, conversion to URL style and injection into the clipboard.

AutoHotKey provide every things I needed to develop the script:
  • File parsing 
    • StExBar selection (single or multiple) are store in a temp text file that can be pass to any cmd or application through "%selafile"
  • Clipboard
    • From AHK (AutoHotKey) point of view the winows clipboard is just a variable.
  • String parsing, Regex Matching etc...
    • To detect Drive letter
    • To detect UNC Path
    • To replace '\' and concatenate the 'file://' prefix.


link := "file:///"
pathrep := ""

CR := Chr(13)
LF := Chr(10)

GetUNCPath(letter)
{
  localName = %letter%
  length := 1000
  VarSetCapacity(remoteName, length)
  result := DllCall("Mpr\WNetGetConnection"
      , "Str", localName
      , "Str", remoteName
      , "UInt *", length)
  if (ErrorLevel <> 0 || result != 0)
  {
     return ""
  }
  else
  {
;     MsgBox, %remoteName% (%length%) -- result: %result%
    return remotename ; end function
  }
}

Loop Read, %1%
{
 p := A_LoopReadLine
 ;MsgBox %p%
  if ( RegExMatch( p , "[A-Za-z][:]") ) ; match a regex for drive letter "^:"
  {
    unc := GetUNCPath( SubStr( p, 1, 2) )
    if ( unc <> "" )
    {
      withoutDriveLetter := SubStr( p, 3, strLen(p)-2 )
      pathrep = %link%%unc%%withoutDriveLetter%%CR%%LF%%pathrep%
    }
  }
  else ; should already be an unc (check to be sure)
  {
    if ( RegExMatch( p , "\\\\") ) 
    {
      pathrep = %link%%p%%CR%%LF%%pathrep%
    }
    else
    {
      ; Msgbox "ignored " %p%
    }
  }
   
}

StringReplace, pathrep, pathrep, \, /, All
; MsgBox %pathrep% 
clipboard =  %pathrep% 

The main advantage of using AHK script is that it can be build and provide as a binary file without any dependency. And the integration of the resulting executable as an StExBar Command is really simple like you can see in the following snapshot:


That script is publicly available on my GitHub repo



mardi 18 juin 2013

About the importance of unit-test defintion

Last week, a friend and I submitted some piece of code to solve an on-line coding challenge. It was a 'french' challenge but I will try to explain the problem and present the data vector used to test submitted function. We will show that their data vector definition is incorrect and doesn't detect if a function can really solve to problem !

First, the problem:

As developer and team member of a High-Tech Company, your manager ask you to register and join the maximum of development and tech-event you can.
So, you list all the incoming event and some are in the same time.
But you still need to be in the "maximum" of event.

So nothing magic... you cannot be in 2 different place the same day.
For each event, you only know when it start and its duration (days).

Second, the test data they use:

They propose 3 different test to check the function output:



Simple

2013-01-12;5
2013-01-01;13
2013-01-16;20

function should return 3





Complex

2014-04-28;4
2014-05-01;7
2014-05-03;7
2014-05-09;5
2014-05-11;11
2014-05-16;3
2014-05-20;1

function should return 4




With a leap year

2012-02-20;10
2012-03-01;2
2012-03-12;2
2012-03-14;2
2012-03-15;2
2012-03-20;13

function should return 5


And now where the defined test failed ...

My co-worker and I had 2 different favorite language (Python for him and C++ for me) and 2 different solution.
In fact he use a real exhaustive exploration using the 'permutation' function to find the 'maximum number of event', but it lead to a high complexity if the number of event grows. On my side, I preferred a linear complexity and I used a common 'greedy' solver. But in fact case, it's well known that it can not find the 'optimal' solution.

My simple C++ implementation use an t_event class define by:

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class event_t 
{
public:
 event_t(const std::string& _date)
 {
  memset(&_start_date, 0, sizeof(struct tm));

  sscanf(_date.c_str(), "%d-%d-%d;%d", &_start_date.tm_year, &_start_date.tm_mon, &_start_date.tm_mday, &_duration);
  _start_date.tm_year -= 1900;
  _start_date.tm_mon -= 1;

  _start_pt = mktime(&_start_date);
  _end_pt = _start_pt + _duration * 24 * 60 * 60;

  memset(&_end_date, 0, sizeof(struct tm));
  memcpy(&_end_date, gmtime(&_end_pt), sizeof(struct tm));
 }

 event_t(const event_t& e)
 {
  _duration = e._duration;
  _start_date = e._start_date;
  _start_pt = e._start_pt;
  _end_pt = e._end_pt;
  _end_date = e._end_date;
 }

 bool operator < (const event_t& r)
 {
  return this->_end_pt < r._end_pt;
 }

 friend std::ostream& operator<< (std::ostream& stream, const event_t& evt);

public:
  struct tm _start_date;
  unsigned int _duration;
  struct tm _end_date;

  time_t _start_pt;
  time_t  _end_pt;
} ;

And use the following function to build a planning:

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void CreatePlanning( std::vector<event_t> &_list )
{
 std::ostream_iterator< event_t > dump_events( std::cout, "" );

 std::sort(_list.begin(), _list.end()); 
 std::cout << " Start \t End \t Duration \n", 
  std::copy(_list.begin(),_list.end(), dump_events);

 std::vector<event_t> _planning;
 _planning.push_back(_list[0]);
 unsigned int j = 0;
 for (unsigned int i = 1 ; i < _list.size() ; ++i)
 {
  if(_list[i]._start_pt >= _list[j]._end_pt)
  { 
   _planning.push_back(_list[i]);
   j = i;
  }
 }

 std::cout << "Nb event planned : " << _planning.size() << std::endl;
 std::cout << " Start \t End \t Duration \n", 
  std::copy(_planning.begin(),_planning.end(), dump_events);
}

That code is ok regarding the expected result of the unit-test, but as i said before the price for the lower complexity is that it can lead to a no optimal solution.
The heuristic I use can be summarize, start ASAP and restart ASAP again and again ... Simple and efficient, I'm a greedy person !!!