Sunday, September 29, 2013
Free Web Hosting using Google Doc/Drive
I found that Google Drive not only allows you to store information on the web, it also allows you to publish materials on the web for free! If you tried to visit the helps sites here and here,you will realize that there are many information that will confuse you as it had confused me. Here is what you need to do to publish your site.
Step 1.
I believe most would know how to do this since you are reading this.
Create a public folder accessible to the Public and upload all your web files (HTML, ...) to the folder you just created.
Step 2.
Find out the folder ID by going to https://drive.google.com/#folders.
Navigate to the public folder you created in Step 1.
Noticed that the address now looks something like this:
https://drive.google.com/#folders/0BzarOaYG8nb6QlJCRTl5MjJp (this link is invalid)
Copy "0BzarOaYG8nb6QlJCRTl5MjJp" and that is your Folder ID.
Step 3.
Now you need to identify your website address. Mine looks like this:
https://googledrive.com/host/0BzarOaYG8nb6UzBrT1NzUzJuSEU/
Have you already figured it out already?
Your address to your website is https://googledrive.com/host/<folder id>, hence in this case it will be:
https://googledrive.com/host/0BzarOaYG8nb6QlJCRTl5MjJp
Some other useful sites you can visit:
http://www.labnol.org/internet/host-website-on-google-drive/28178/
http://www.youtube.com/watch?v=fwyZqyGEPNk
Thursday, September 12, 2013
Python On Android
Step 1.
You will need to install both Python for Android and SLA4 onto your Android phone. However, they are considered "unknown application" so you have to allow unknown application to install on your phone by checking the option at setting->security.
I shall not go through the installation as it is rather straight forward.
Step 2.
Next you are ready to code in Python!! Just start SLA4A application and you will see a list of sample scripts there.
The API for Python on Android can be found here and you can find some tutorial here. Also do note that similar to Python on PC, you will also be able to install packages and use it when it is available.
A few interesting things you will be able to do with this...
- Take photo/video
- Location discovery
- Bluetooth connectivity
- SMS
- Whatsapp (I have not tried this myself)
A sample example I did here is to use location discovery to get the coordinate of the current location. Using this coordinates, I launch Google Map and it will display your location.
Organize/Sort large data Part 2 - Extracting Data at Specific day of the Week.
Step 1.
Choose the symbol of your liking, in this case "FRI" is used.
Copy and paste this filtered data obtained, into a new worksheet, in this case, "Sheet3".
Step 2.
Next, noticed that the format of the date represented here is YYYYMMDD. In this format, Excel is not able to make out the date. Hence, you will need to translate the date into Excel readable date format. This steps would vary depending on the format used and you might need some creativity to translate it to excel readable format. In this example, we do truncation using "LEFT" and "RIGHT" function of Excel. Which obtain part of a string starting from the right or left. The following formula will enable us to do the translation:
DATE(LEFT(A2,4),LEFT(RIGHT(A2,4),2),RIGHT(A2,2))
Step 3.
Now Excel is able to read the date properly however, you need to extract the weekdays information and this can be done using:
WEEKDAY(B4,1)
The first parameter correspond to the Date cell we converted in Step 2 and the second parameter of allows you to choose different representation of weekdays. In this case, we chose 1, which means the start of the week as Sunday represented by 1 and the end of the week as Saturday represented by 7; Wednesday is represented by 4.
Explanation of what I will be doing, you may choose to skip if you just want to get to the results:
In the case where we are certain that we have data on every Wednesday, we will be able to use a simple method by using FILTERS or a simple IF statement "IF(C5=3,D5,)".
But the problem here is we could have an holiday on Wednesday and we will not be able to get any data for that week. I need to get the closest data to Wednesday. What I am doing here is to create a convention where I can identify the closest day to Wednesday in the event of a Holiday. By making Wednesday the start of the week, it will have the smallest digit which can be identified by comparing with the previous day.
You might like to notice that none of the option in the WEEKDAY() second parameter allows us to let us start the week on Wednesday. Hence, we need to use another method to remap the weekdays.
Step 4.
We need to remap the weekday as per my previous explanation. To do this I created a Lookup Table in "Sheet2!$C$3:$D$7" and it looks like this:
This table translate the week such that it will start on Wednesday.
By using this table and the formula:
VLOOKUP(C2,Sheet2!$C$3:$D$7,2)
Create a new column using this formula and you would have recreated the new Weekday field by starting the week from Wednesday which is represented by the smallest number of the week, 1.
Step 5.
Noticed that the start of the week is the smallest number. From this Wednesday to the coming Tuesday, the new Weekday number will be in increasing order and it is only when there is a transition to the next week, then new weekday number will suddenly be smaller. By using this pattern, use a IF statement in a new column to filter out the starting day of the week:
IF(D6<D7,E6,"")
Hence, when there is a holiday on Wednesday, it will automatically take the next working day of the week as the start of the week. Now you just need to filter out the BLANKS.
VIOLA! You got the data you need!
The finished product can be obtained here.
Tuesday, September 10, 2013
.dll? That's .so Microsoft. (C)
I was thinking about DLLs from the previous blog by Ken and decided to look a little at how shared objects are made and used in Linux. Based on this answer to a StackOverflow question, I came up with this example in C.
Preparing the shared object
Say you want to write a function sumSquare(int, int)
which takes two integers and returns the sum of their squares. Then you could code it simply as follows:
int sumSquare(int first, int second) {
return ((first * first) + (second * second));
}
Save this file as, say, sumSquare.c
. Then you could build the shared object with gcc like this:
$ gcc -fPIC -Wall -shared -o sumSquare.so sumSquare.c
where -fPIC
instructs gcc to produce position-independent code which is necessary for code that can be in different locations, -shared
compiles the code into a shared object instead of an executable, and -o filename
specifies the preferred output filename. If this last argument is not given, then gcc prefers to use a.out
as the output filename. -Wall
is just my personal preference to show all Warnings.
Now you have the shared object which you can include in your code. Yeah... how?
Including the shared object
Now I write a tiny piece of code that takes the sum of squares of two numbers - say, 3 and 4 - and prints them on screen.
#include
#include
int main(int argc, char** argv) {
void *handle;
int (*ss)(int, int);
char *error;
handle = dlopen("./sumSquare.so", RTLD_LAZY);
if (!handle) {
fprintf(stderr, "%s\n", dlerror());
return 1;
}
dlerror();
ss = dlsym(handle, "sumSquare");
if ((error = dlerror()) != NULL) {
fprintf(stderr, "%s\n", error);
return 2;
}
printf("3^2 + 4^2 = %d\n", (*ss)(3, 4));
dlclose(handle);
return 0;
}
OK, that wasn't so tiny after all. But there is good reason for it. Let's look at it segment by segment: first, the includes. <stdio.h>
is responsible for input and output, and you might have seen it before. Then there is <dlfcn.h>
. What does this library do? It handles the communication with the shared object, providing calls like dlsym()
, dlopen()
and dlerror()
.
Next, the variables are declared. A void *handle
, seemingly a pointer to nothing, is followed by a strange int (*ss)
which takes two arguments and a char* error
. The last is easy enough to understand, it's a 'string', a sequence of characters.
The next block provides a, yes, handle
on the shared object. ./sumSquare.so
means that the shared object should be in the same directory as the code. The if-block prints an error to STDERR
and returns 1 if nothing is in handle
. The final dlerror()
silently drops any stray errors. Not really the best approach, as you might guess... but I'll leave it to you to improve on :)
Then ss is assigned the symbol "sumSquare", which as we saw before, is the name of the function we want. Here again is an if-block in case an error pops up. Here, dlerror()
will be NULL unless something goes wrong, so we check for that.
All that was just making sure the function could be accessed. Finally, the call to ss
is made with the requisite (here hard-coded) arguments as part of a printf
statement.
At the end of the function, the handle for sumSquare.so
is closed.
Here is the call to the compiler:
$ gcc -Wall -o trivial -ldl code.c
-ldl
is necessary for the library in <dlfcn.h>
to be linked in. This makes all the dynamic loading possible.
That's it! You can take a look at sumSquare.c and code.c if you wish. I would recommend reading about the dlopen() calls though.
Monday, September 9, 2013
Organize/Sort large data Part 1 - Using Pivot Table in Excel
Step 1.
Insert a Pivot Table by going to Insert -> Pivot Table.
Select the range of data in the spreadsheet you would like to organize/sort and choose if you would like to create this table in a new worksheet or the existing one. Normally I would choose to create a new worksheet to make things neater. Of course if you keep creating pivot table, your workbook will become messy so do take note of how you want to create your pivot table. In this case, I chose to create a new worksheet with the data from "Sheet1!$A$1:$G$493468". Upon pressing OK, you will get an empty pivot table which you have to customize to your needs.
- "dates" to be at the row
- Trading symbols or in this case it is also called "Ticker" at the column
For those who did not managed to download the completed product, it can be downloaded here.
LibreOffice addons in C++... OK, OK, Basic
There are times when the built-in functions you have in your spreadsheet program just won't do. You feel you need to write your own special function for convenience or to stroke your ego. This entails adding functionality to an existing program.... how would you go about doing that?
This was really inspired by Ken's post about developing a DLL for Excel with C++. I thought I'd cover the open-source side somewhat less competently by sharing how to put together the same thing for OpenOffice/LibreOffice Calc. Just so you could compare the different approaches and I get to learn something about C++ and LibreOffice suites.
Step 1: Download Excel...
OK, here goes.
Step 1: Look up the C++ examples in the LibreOffice API. Pick a simple example, like Counter.
Step 2: Recoil in horror at the length of a "very simple" example. Even if you take into account the padding that the BSD licence gives, there are still 120-odd lines in counter.cxx and 50-ish in countermain.cxx. Then there's the .idl file ...
Step 3: Realise that I am doing a silly thing, trying to use a Windows convention outside Windows. In LibreOffice, it does not seem possible to compile shared objects (what Windows knows as DLLs) and bundle them for use with VBA. What I should have been doing is to instead define a function in LibreOffice Basic.
So here's the real step 1: Open the Basic-IDE using the menu sequence below.
Step 2Drill down to Standard.Module1 in My Macros and click New:
Step 3: Write the following function into the editor:
Function addition(a, b)
addition = a + b
End Function
Be sure to remove the two existing procedures, Sub Main and Sub Macro1; writing functions inside procedures is not acceptable in StarBasic. Then Save the code and close the editor.
Step 4: Use the function as you would an inbuilt function: For instance, write =addition(A1, B1)
in cell C1. You can check by changing the values of A1 and B1 that you have a new function you can use with any new spreadsheet on your computer.
Step 5: Optionally, if you want to share the file with others, it should be located in ~/.config/libreoffice/3/user/basic/Standard/Module1.xba
. Copy that XML file as, say, Addition.xba
and update script.xlb
with the following line:
<library:element library:name="Addition"/>
LibreOffice should detect it upon loading.
As with Excel, changes to the code outside of LibreOffice (even deleting the script) will not be detected until you restart.
Yes, yes, it's a bit of a cop-out. I will look more into the C++ analogue or talk about writing a macro in Python soon.
Using C++ and VBA in Excel
Step 1.
Create a empty Win32 Visual C++ project and you will get to the Wizard page.
Resolve any syntax error on your code and compile your code.