22 KiB
layout, status, published, title, author, author_login, author_email, author_url, wordpress_id, wordpress_url, date, categories, tags, comments
| layout | status | published | title | author | author_login | author_email | author_url | wordpress_id | wordpress_url | date | categories | tags | comments | |||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| page | publish | true | Programming Tips | Julien Lengrand-Lambert | jlengrand | julien@lengrand.fr | http://www.lengrand.fr | 431 | http://www.lengrand.fr/?page_id=431 | 2011-12-28 14:24:01.000000000 +01:00 |
|
Here is a list of all simple tips I have been searching for in the past. I put them here to find them easily !
they are classified by language/topic :
Hopefully one of those tips will help someone :)BASH
FLV to AVI conversion :You will need ffmpeg
[bash]
$ sudo apt-get install ffmpeg
$ ffmpeg -o /your/flv/file.flv -vcodec mpeg1-video
-acodec copy -ar 44100 -s 320x240 -y /your/avi/file.avi
[/bash]
OGG to AVI conversion :
You will need mencoder
[bash] $ sudo apt-get install mencoder $ mencoder out.ogv -ovc xvid -oac mp3lame -xvidencopts pass=1 -o Winner.avi [/bash]
Get the size of a file :
Here are two simple ways:
[bash] $ ls -s file $ stat file-c %s [/bash]
Get the processing time of a function/script:
Simply run the same command prepended with time
[bash] $ time previous_command [/bash]
Get the number of arguments in command line:
Use the variable $# to get the number of arguments of previous command.
WARNING: The name of the command stands for one argument!
Get the list of dependencies of a binary:
Simply run the same command prepended with ldd :
[bash] $ ldd previous_command [/bash]
Demonize a process :
The command to search for is start-stop-daemon, with start option.
To avoid any output in stdout, use redirection:
[bash] $ command &> /dev/null& [/bash]
Launch a process at startup:
A link must be placed in /etc/init.d, using ln -s
[bash]
$ mv /etc/init.d
$ ln -s /path/to/file link
[/bash]
WARNING: A printf in a startup process will lead to a crash as it blocks the remaining processes.
INFO: In embedded Linux, startup processes will have to be named using a Sxx name, where xx is a digit between 1 and 99.
The processes will be run in ascending order, so be careful not to block the system (samba after network, . . . )
Block interruptions (CTRL+C) :
Simply add the line
[bash]
trap " 2 3
[/bash]
where you want to 'trap' the command.
End catching interruptions by adding where you want to stop
[bash]
trap 2 3
[/bash]
Use floats in a script, and convert them back to integers:
The operations can be performed with bc :
[bash]
$ VAR=$(echo "2.2/ 10.65" | bc -l)
[/bash]
To switch back to the int value :
[bash]
$ (ceil) INT=${VAR/.*}
[/bash]
Search for pattern in all files of a folder:
[bash]
$ "find ./ -name .svn -prune -o -type f -print | xargs grep -Hi" //pattern//
[/bash]
The .svn part is used to avoid searching in the subversion folder of my projects.
[bash]
$ grep -R //pattern//@@ is also a solution.
[/bash]
It is even possible to create an alias with it (see Add a one line command in terminal).
Add a one line command in terminal:
You may use some one-line commands lots of times a day in terminal.
You can define aliases to gain productivity:
[bash]
$ vim ~/.bash_aliases
[/bash]
Simply add a new line at the end of the file :
[bash]
alias_name = "my_one_line_command"
[/bash]
Restart your terminal, you're done!
Seconds since epoch:
[bash]
$ date -d "$FIRST_TIME" +"%s"
[/bash]
Directly get the correct shebang for you script:
[bash]
$ which //language// > my_script
[/bash]
Example :
[bash]
$ which bash > my_script.sh
[/bash]
Get the encoding of a file:
[bash]
$ file -i //unkown_file_type//
[/bash]
Remove all .svn folders from a repo:
[bash]
$ cd //my_folder///
$ find -name ".svn" -exec rm -rf {} ;
[/bash]
INFO: Can be performed for any folder type, changing the .svn part
Get information about your Linux version:
- Description file:
$ cat /etc/lsb-release
[/bash]
- Kernel version:
$ uname -a
[/bash]
- More information about Kernel (compiler and so on):
$ cat /proc/version
[/bash]
Get information about your processor(s):
[bash]
$ cat /proc/cpuinfo
[/bash]
Change default browser in Linux:
[bash]
$ update-alternatives &&config x-www-browser
[/bash]
You will have to choose between all installed browsers in you distro.
Add a user to the sudoers:
[bash]
$ visudo
[/bash]
Search for the line
[bash]
@@ root ALL=(ALL) ALL@@
[/bash]
Add this new line just below :
[bash]
user_name ALL=(ALL) ALL
[/bash]
Get information about you graphical card:
[bash]
$ lspci | grep VGA
[/bash]
You may try this if first is not working :
[bash]
$ lspci | grep video
[/bash]
Add tab completion for sudo commands:
Add
[bash]
complete -cf sudo
[/bash]
in your .bashrc. It can be done simply in running the following command once:
[bash]
$ echo "complete -cf sudo" > ~/.bashrc
[/bash]
Read information in elf files:
[bash]
$ readelf -h $1 | grep 'Class|File|Machine'
[/bash]
Sniff on serial interface to read output of a device :
[bash]
$ sudo apt-get install jpnevulator
$ jpnevulator --read --ascii --tty /dev/ttyUSB0
[/bash]
Remove trailing / in path in a script:
[bash]
#!/bin/bash
FOLDER=$1
echo ${FOLDER%/}
[/bash]
PYTHON
Get variable name as a string:[python]
blah = 1
blah_name = [ k for k , v in locals (). iteritems () if v is blah ][ 0 ]
[/python]
WARNING : Not very clever however, as variables in Python may have more that one name ! Use with caution!
Replace a part of a table [Numpy]:
[python]
small_BW=where((L[temp[ptr-1]]==ptr),1,0)
a=BW[posi_0:posi_1,posi_2:posi_3]
a[:,:]=small_BW.copy()
[/python]
Reshape table without size information:
Put -1 as second argument, the info will automatically be processed.
[python]
array.reshape(3,-1)
[/python]
Concatenate 2 tables [Numpy]:
[python]
c=numpy.concatenate((a,b),axis=0)
[/python]
Cast data in table to int:
[python]
Ndarray.astype(int)
[/python]
Write in the end of a file :
Use add mode :
[python]
file.open(file_name, 'a')
[/python]
Test if variable exists:
[python] try: print variable except NameError: print "Error!" [/python]
Check if variable is one of several choices:
[python] if a in [b, c,d]: blabla [/python]
Multi instanciation in Python:
[python] a = b = c = 1+1 [/python]
WARNING: Those three variables are the same object!
Print function name :
[python] import sys
def tutut(): """ Dum function displaying its name! """ print sys._getframe().f_code.co_name
if name == 'main': tutut() [/python]
And here is the result
[bash] [airballman@ubuntu:~]$ python tutut.py tutut [/bash]
C/C++
Block interruptions (CTRL+C):Include the signal header :
[c] #include signal.h [/c]
At the beginning of the main, insert
[c] signal(SIGINT, fction) [/c]
with
- fction the function to be started on detection.
- SIGINT the interrupt to be detected.
Some simple basics that helped :
About variables :
- age -> value
- &age -> pointer
- ptr -> ptr value(which is an address)
- *ptr -> variable value placed in the address.
- age is its value
- &age is its pointer
- ptr is the pointer value (which is an address)
- *ptr is the value of the variable contained in at the address.
[c] int *ptr = NULL; //Is just a creation, the * does not stand for anything else than declaring the pointer.
int age = 10;
*ptr=&age; //ptr is already created, the * now means that whe want to assign its value [/c]
Those two are equivalent (used for structures):
- a->b
- *(a.b)
Define a variable in a Makefile :
- Use the -D option in the CFLAGS
- #define in the C code
Git and GitHub
Simple reminders to manage a project with Git and GitHub interface.Clone a project from my personal GitHub account:
[bash] $ git clone git@github.com:jlengrand&/projet.git [/bash]
Clone a project from another source:
[bash] $ git clone git://git.berlios.de/gpsd [/bash]
Push modifications previously locally committed:
Should be performed once in a while to keep remote repository up to date. Do not forget to pull before pushing to avoid conflicts.
[bash] $ git push -origin master [/bash]
More information can be found in git documentation
INFO : No need to create a folder first, as a main folder is automatically created for the project.
Electronics
Some basic tipe for new embedded Eletronics programmers (like me ;) )Use of DDx, PORT and PIN registers:
- DDx: Choice of pins state: Inputs or Outputs
- PORT: Set the outputs state, rules the outputs (ex : set Pin to 0)
- PIN: Reads the inputs states, or toggle values. (ex: Toogle Pin)
Easily set a bit to 0 in a register:
[c] PPR=PPR & 0b01111111 [/c]
Easily set a bit to 1 in a register:
[c] PPR=PPR & 0b01111111 [/c]
Debug hardware errors after optimization:
In case your C code does not work any more after Optimization (-O2 -O3)
- Check if global variables are used in interruptions
- Try to set them as volatile
Hazardous hardware resets:
Check if your interruptions are initialized in your code !
Otherwise, the C code will automatically jump to a non-existing address, causing the reset.
I/O problems:
In case of I/O problems in your code, check your Hardware and especially your resistors
Try to unsolder them and run the code.
Embedded electronics main architecture code:
Generally, embedded architecture software is divided into 3 main layers :
- Drivers: Whose job is to interact with Hardware at a low level (Ex : Read spi data)
- Services/Components: Intermediate layer, interface between drivers and Applications
- Application: Higher layer, which aims at achieving the main purpose of the project (Ex: Calculate movement using an accelerometer)
- Here is a simple drawing to get the idea:
[/caption]
INFO: The main idea behind that when you code is the reuse! The application may not be reusable, but drivers and components must ! If you did good, you should be able to use the same application in two architectures, changing only components and drivers.
OpenCV
Memos and Tips about the OpenCV library.In C/C++:
Place a pointer to the beginning of pixel in an image:
Can be used to naviguate through pixels easily (and more efficiently than some openCV functions).
[c] char *pImage = iplImage->ImageData [/c]
Short review of the IplImage structure:
What's really needed to work with an Image
[c]
- IplImage struct {
depth # Number of channels (gray level, color, . . .)
sizeX
sizeY
- ImageData } #Pixels of the image ...} [/c]
Simple tips for Image Processing optimization in C:
An image is stored in a memory a a long line (n*m size)
Always go trough the buffer way, and not the opposite!
[c] for (i in nb_lines): for (j in nb_cols): //do stuff } } [/c]
and
[c] for (j in nb_cols): for (i in nb_lines): //do stuff } } [/c]
are different !
The second one is 10% faster than the other !
In Python :
Error on compiling OpenCV with Python bindings:
If you get this error when trying to compile OpenCV in Linux
[bash]
Could NOT find PythonLibs (missing: PYTHON_INCLUDE_DIRS) in cmake
[/bash]
Simply try installing python-dev packages.
It should solve the problem.
Subversion
To create a new branch in one of your repositories:- Copy your trunk folder. This will automatically proceed the mkdir
$ svn cp trunk -> new_branch
[/bash]
Update the information
[bash]
$ svn update
[/bash]
INFO: The command is an update and not a commit, as this is the server who performed the operation!
You're done!
Merge your branch back in your trunk:
Move to your trunk folder
[bash]
$ cd path/to/my/trunk
[/bash]
Merge the branch:
[/bash]
$ svn merge --reintegrate ./^branch
[/bash]
INFO: The reintegrate option allows continuing to work in the branch even once reintegrated
Some tips:
- A tag is used to get a precise version, fixed in time (Ex : V1.1)
- The trunk must always stay as the main reference to branches and tags (ie. cd /my/trunk before operations).
- Always use the ^ sign to merge and branch folders. It allows to avoid using absolute path.
- The ^ corresponds to /path/to/my/svn/root/folder
- If you're running Windows, you might want to look at tortoisesvn.
Profiling
Simple tips for software profiling using Linux.Simple steps on using gprof:
gprof is a really efficient prototyping tool designed by Richard Stallman
It is a total graal to exactly know where your bottlenecks are, and which parts of your software have to be optimized first!
- Simply install gprof on your computer (for debian based distros).
$ sudo apt-get install gprof
[/bash]
- Compile your C code with the -pg option
- Execute your program, just as usual
- You will see that a gmon.out file was created during the execution
- You can retrieve the results of profiling by running gprof:
$ gprof your_binary gmon.out >> saved_report
$ vim saved_report
[/bash]
Simple command-line to search for memory leacks:
Using the Valgrind library.
[bash]
$ valgrind --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=20 --track-fds=yes ./test
[/bash]
Wordpress
Some very simple tips and hints for wordpress.Insert an horizontal line in a post :
Simply insert the
tag while in html edition mode .
Insert anchors/links in a section of a page:
This is done in two different steps :
- Define the anchor. You should place the following code where you want the user to be redirected.
- Create a link to the anchor. Depending on what you want, you should insert :
You can see the result everywhere on this page, in the topic listing :).