Showing posts with label SampleCodes. Show all posts
Showing posts with label SampleCodes. Show all posts

Monday, November 16, 2015

Python: Retrieve files from URL link

file-retrieve-via-url.py
#!/usr/bin/env python
import urllib
from datetime import datetime

saveFileName = "Sydney-Auction-Result-"+datetime.now().strftime("%Y%m%d%H%M%S")+".pdf"
urllib.urlretrieve ("http://domainmastheads.homepriceguide.com.au/saturday_auction_results/Sydney.pdf",saveFileName )
saveFileName = "Adelaide-Auction-Result-"+datetime.now().strftime("%Y%m%d%H%M%S")+".pdf"
urllib.urlretrieve ("http://domainmastheads.homepriceguide.com.au/saturday_auction_results/Adelaide.pdf",saveFileName )
saveFileName = "Melbourne-Auction-Result-"+datetime.now().strftime("%Y%m%d%H%M%S")+".pdf"
urllib.urlretrieve ("http://domainmastheads.homepriceguide.com.au/saturday_auction_results/Melbourne.pdf",saveFileName )
saveFileName = "Brisbane-Auction-Result-"+datetime.now().strftime("%Y%m%d%H%M%S")+".pdf"
urllib.urlretrieve ("http://domainmastheads.homepriceguide.com.au/saturday_auction_results/Brisbane.pdf",saveFileName )
#saveFileName = "Sydney-Domain-Auction-Result-"+datetime.now().strftime("%Y%m%d%H%M%S")+".pdf"
#urllib.urlretrieve ("http://www.domain.com.au/apm/saturday_auction_results/Sydney_Domain.pdf",saveFileName )

Wednesday, November 11, 2015

Bash to synch one particular folder from one SVN repository to another SVN repository


#!/bin/bash

pushd ~/Svn_Local_Proj/Cascade/Firmware/ 

svn up
# Synch two local SVN copies,but ingore .svn etc.
rsync --delete --cvs-exclude --checksum --recursive ~/Svn_Local_Proj/Cascade/Firmware/ ~/Svn_Local_Proj_HomeTrac/Proj_FW/ 

pushd ~/Svn_Local_Proj_HomeTrac/Proj_FW/ 
svn up
#Add new files to repo if there are new files
svn add --force * --auto-props --parents --depth infinity
svn ci -m"Auto Synch SVN copy to Trac from Lubuntu"

popd
popd

Bash to copy files from AWS to local drive

#!/bin/bash

# Copy from AWS
# Password based ssh access, but downside is disclose plain text password. Better use ssh key pair based access
# sshpass -p "Password" scp -pv root@qqec2.tklapp.com:~/AuctionResults/*.pdf ./
rsync --checksum qqec2:~/AuctionResults/*.pdf ./
echo "Done synch with AWS"
ssh qqec2 "if [ ! -d 'AuctionResults-Bkup' ]; then mkdir -v AuctionResults-Bkup; echo 'Created folders that doesnot exist';fi;
#ssh qqec2 "if [ ! -d "AuctionResults-Bkup" ]; "
#"then mkdir -v AuctionResults-Bkup; "
#"echo "Created folders that doesnot exist";"
#"fi;"
#"cp -vp AuctionResults/*.pdf AuctionResults-Bkup/""
echo "Done moving all download pdf files to bkup folder on AWS"

Friday, November 6, 2015

Shared Libraries Demo *.so file.



Ref 1 cmd line to generate *.so on stackoverflow
Ref 2 stackflow post ld --verbose -llibxxx

testsomain is smaller when not compiled with testso.c 
[q.yang@fedora20 Trash]$ gcc testsomain.c -o testsomain -L./ -ltestso
It's bigger when compiled with testso.c. It's one of the benefit of using dynamic library.
[q.yang@fedora20 Trash]$ gcc testsomain.c testso.c -o testsomain
testsomain.c
#include 
#include "testso.h"

int main(void)
{
  printf("Testing calling function in *.so \n");
  Ext_PrintHello();
}
testso.c
#include 

void Ext_PrintHello(void)
{
  printf("%s Invoked from external module.\n",__func__);
}
testso.h
//#error "this file has been included here"

void Ext_PrintHello(void);
Compile to generate dynamic library:
Note: MUST use -fPIC option PositionIndependantCode(PIC)
*.so file name must be libxxnamexx.so, cannot be xxnamexx.so
[q.yang@fedora20 Trash]$ gcc -shared -fPIC -o libtestso.so testso.c
Compile main program to load dynamic library *.so
[q.yang@fedora20 Trash]$ gcc testsomain.c -o testsomain -L./ -ltestso
Set load library search path
[q.yang@fedora20 Trash]$ LD_LIBRARY_PATH=./
[q.yang@fedora20 Trash]$ export LD_LIBRARY_PATH
Run program
[q.yang@fedora20 Trash]$ ./testsomain 
Testing calling function in *.so 
Ext_PrintHello Invoked from external module.
Checking so file dependencies $ldd and file info $file. (Running for i686 or ARM)
duser@10.1.1.8:~/Trash$ file libtestso.so 
libtestso.so: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), dynamically linked, BuildID[sha1]=0xf54c8aad49c8dfd0220885eab40fe703c0e5495b, not stripped

duser@10.1.1.8:~/Trash$ ldd libtestso.so 
 libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0xb754b000)
 /lib/ld-linux.so.2 (0xb770f000)

We can also use linker $ld to check where system default search path for *.so shared library files. Given libtestso.so [Ref 2]
duser@10.1.1.8:~/Trash$ld -L./ -ltestso --verbose

..................
..................
==================================================
attempt to open /usr/i686-linux-gnu/lib32/libtestso.so failed
attempt to open /usr/i686-linux-gnu/lib32/libtestso.a failed
attempt to open /usr/local/lib32/libtestso.so failed
attempt to open /usr/local/lib32/libtestso.a failed
attempt to open /lib32/libtestso.so failed
attempt to open /lib32/libtestso.a failed
attempt to open /usr/lib32/libtestso.so failed
attempt to open /usr/lib32/libtestso.a failed
attempt to open /usr/local/lib/i386-linux-gnu/libtestso.so failed
attempt to open /usr/local/lib/i386-linux-gnu/libtestso.a failed
attempt to open /usr/local/lib/libtestso.so failed
attempt to open /usr/local/lib/libtestso.a failed
attempt to open /lib/i386-linux-gnu/libtestso.so failed
attempt to open /lib/i386-linux-gnu/libtestso.a failed
attempt to open /lib/libtestso.so failed
attempt to open /lib/libtestso.a failed
attempt to open /usr/lib/i386-linux-gnu/libtestso.so failed
attempt to open /usr/lib/i386-linux-gnu/libtestso.a failed
attempt to open /usr/lib/libtestso.so failed
attempt to open /usr/lib/libtestso.a failed
/usr/bin/ld.bfd.real: cannot find -ltestso
If we tell the load library path via -L option. Linker will find it.
duser@10.1.1.8:~/Trash$ ld -L./ -ltestso --verbose
....................
....................
==================================================
attempt to open .//libtestso.so succeeded
-ltestso (.//libtestso.so)
libc.so.6 needed by .//libtestso.so
found libc.so.6 at /lib/i386-linux-gnu/libc.so.6
ld-linux.so.2 needed by /lib/i386-linux-gnu/libc.so.6
found ld-linux.so.2 at /lib/i386-linux-gnu/ld-linux.so.2
/usr/bin/ld.bfd.real: warning: cannot find entry symbol _start; not setting start address

Wednesday, November 4, 2015

Sample code of recursive function in C/C++ (ToDo.....)

[1] www.cplusplus.com/Recursion

What needs to pay attention when creating safe recursive function?


Sample Codes of Thread Safe functions and variables (ToDo....)

The simplest method is to make your variables private (but you do that already, right?) and to use synchronized accessor methods. Accessor methods allow access to private member variables, but in a controlled manner. Take the following accessor methods, which provide a safe way to change the value of a counter.

 Here is how to make variable thread safe in java. Make it private and accessible via synchronized public function call.
public class MyCounter
{
private int count = 0; // count starts at zero

public synchronized void setCount(int amount)
{ 
count = amount;
}

public synchronized int getCount()
{
return count;
}
}


In C/C++, mutex, would be necessary.

Function Pointers in C/C++



#include <iostream>
#include <cstring>
#include <vector>

using namespace std;
namespace DemoFunPt {
typedef void (*CallBackFunc)(int i_input);
// Register callback function
vector<CallBackFunc> Callbackfuncs;

void Func_A(int i_input)
{
  cout<< "Hello from Func_A " << i_input << endl;
}

void Func_B(int i_input)
{
  cout<< "Hello from Func_B " << i_input << endl;
}

void RegisterCallbackFunc(CallBackFunc i_FuncPt)
{
  Callbackfuncs.push_back(i_FuncPt);
}

}

int  main()
{
   DemoFunPt::RegisterCallbackFunc(DemoFunPt::Func_A);
   DemoFunPt::RegisterCallbackFunc(DemoFunPt::Func_B);

   for(int i=0; i< DemoFunPt::Callbackfuncs.size(); i++){
      DemoFunPt::Callbackfuncs[i](i);
   }
   cout<<"Done demo" << endl;
   return 0;
}



root@trac ~# g++ funcpointer.cpp -o test
root@trac ~# ./test
Hello from Func_A 0
Hello from Func_B 1
Done demo

Thursday, October 29, 2015

Sample Python Codes plot value with date time axis in matplotlib



[1] stackoverflow.com/ plotting-time-in-python-with-matplotlib



#!/usr/bin/env python

import matplotlib.pyplot as plt
import datetime



ax = tuple([datetime.datetime(2015, 10, 27, 17, 17, sec) for sec in range(1,10)])
print ax

ay = tuple(range(1,10))

plt.plot(ax,ay)
# Beautify the display format
plt.gcf().autofmt_xdate()
plt.show()


When doing enough zoom in, it will display in macro seconds resolution. When zooming out enough,it will display dates.

Wednesday, October 28, 2015

Static Variable Inside C Function.

#include <stdio.h>

#define TEST_3


void FuncA ()
{
#ifdef TEST_0
  // 0 value initialization only happen once if defined 'static'.
  static int StaticVar=0;
#endif
#ifdef TEST_1
  // 0 value initialization happen every time when FuncA is called.
  int StaticVar=0;
#endif
#ifdef TEST_2
  // When not give inital value, system set StaticVar to '0'
  // Changed value will stay next time when FuncA is called as StaticVar
  // is defined with keyward 'static'.
  static int StaticVar;
  printf("StaticVar is : %d\n",StaticVar);
  StaticVar = 2;
#endif
#ifdef TEST_3
  // When no 'static', system set StaticVar to a random value.
  // (I think it depends on what value in stack.)
  // But in test it seems, StaticVar keep same value as it's retrieving 
  // value from same postion in stack.
  int StaticVar;
  printf("StaticVar is : %d\n",StaticVar);
  StaticVar = 3;
#endif

  printf("StaticVar is : %d\n",StaticVar);
  StaticVar++;

}


void main()
{
  FuncA();
  FuncA();
  FuncA();
  FuncA();
  FuncA();
  FuncA();
  printf ("====Done====\n"); 

}
The results after defining different test cases:

Test_0
qyang@lubuntu-laptop:~$ ./Learn_static 
StaticVar is : 0
StaticVar is : 1
StaticVar is : 2
StaticVar is : 3
StaticVar is : 4
StaticVar is : 5
====Done====
Test_1
qyang@lubuntu-laptop:~$ ./Learn_static 
StaticVar is : 0
StaticVar is : 0
StaticVar is : 0
StaticVar is : 0
StaticVar is : 0
StaticVar is : 0
====Done====
Test_2
qyang@lubuntu-laptop:~$ ./Learn_static 
StaticVar is : 0
StaticVar is : 2
StaticVar is : 3
StaticVar is : 2
StaticVar is : 3
StaticVar is : 2
StaticVar is : 3
StaticVar is : 2
StaticVar is : 3
StaticVar is : 2
StaticVar is : 3
StaticVar is : 2
====Done====
Test_3
qyang@lubuntu-laptop:~$ ./Learn_static 
StaticVar is : 134513947
StaticVar is : 3
StaticVar is : 4
StaticVar is : 3
StaticVar is : 4
StaticVar is : 3
StaticVar is : 4
StaticVar is : 3
StaticVar is : 4
StaticVar is : 3
StaticVar is : 4
StaticVar is : 3
====Done====