Saturday, August 25, 2018

Python : List all imported modules

>>> print(help('modules'))

or

>>>help()
help>>> os
<.......>
This will list all methods/fuctions inside os module

or
>>import sys
>>print(sys.modules)
>>print(sys.path)
>>dir('__main__')



Python: Listing all functions , methods from a module using dir()

We can list all the methods or functions a python module provides.
You need to import the module first then use the dir() to list everything a module offers.
eg:
1. import os


2. dir(os)
>>> dir(os)
['DirEntry', 'F_OK', 'MutableMapping', 'O_APPEND', 'O_BINARY', 'O_CREAT', 'O_EXCL', 'O_NOINHERIT', 'O_RANDOM', 'O_RDONLY', 'O_RDWR', 'O_SEQUENTIAL', 'O_SHORT_LIVED', 'O_TEMPORARY', 'O_TEXT', 'O_TRUNC', 'O_WRONLY', 'P_DETACH', 'P_NOWAIT', 'P_NOWAITO', 'P_OVERLAY', 'P_WAIT', 'PathLike', 'R_OK', 'SEEK_CUR', 'SEEK_END', 'SEEK_SET', 'TMP_MAX', 'W_OK', 'X_OK', '_Environ', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_execvpe', '_exists', '_exit', '_fspath', '_get_exports_list', '_putenv', '_unsetenv', '_wrap_close', 'abc', 'abort', 'access', 'altsep', 'chdir', 'chmod', 'close', 'closerange', 'cpu_count', 'curdir', 'defpath', 'device_encoding', 'devnull', 'dup', 'dup2', 'environ', 'errno', 'error', 'execl', 'execle', 'execlp', 'execlpe', 'execv', 'execve', 'execvp', 'execvpe', 'extsep', 'fdopen', 'fsdecode', 'fsencode', 'fspath', 'fstat', 'fsync', 'ftruncate', 'get_exec_path', 'get_handle_inheritable', 'get_inheritable', 'get_terminal_size', 'getcwd', 'getcwdb', 'getenv', 'getlogin', 'getpid', 'getppid', 'isatty', 'kill', 'linesep', 'link', 'listdir', 'lseek', 'lstat', 'makedirs', 'mkdir', 'name', 'open', 'pardir', 'path', 'pathsep', 'pipe', 'popen', 'putenv', 'read', 'readlink', 'remove', 'removedirs', 'rename', 'renames', 'replace', 'rmdir', 'scandir', 'sep', 'set_handle_inheritable', 'set_inheritable', 'spawnl', 'spawnle', 'spawnv', 'spawnve', 'st', 'startfile', 'stat', 'stat_float_times', 'stat_result', 'statvfs_result', 'strerror', 'supports_bytes_environ', 'supports_dir_fd', 'supports_effective_ids', 'supports_fd', 'supports_follow_symlinks', 'symlink', 'sys', 'system', 'terminal_size', 'times', 'times_result', 'truncate', 'umask', 'uname_result', 'unlink', 'urandom', 'utime', 'waitpid', 'walk', 'write']


3. What a particular method/function does , we can get this help further by doing dir() as:
>>> dir(os.getcwd)
['__call__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__self__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__text_signature__']
>>>

>>> dir(os.getcwd())
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
>>>

4. get help()
>>> help(os.getcwd)
Help on built-in function getcwd in module nt:

getcwd()
    Return a unicode string representing the current working directory.

>>>

>>> help(os.rename)
Help on built-in function rename in module nt:

rename(src, dst, *, src_dir_fd=None, dst_dir_fd=None)
    Rename a file or directory.
   
    If either src_dir_fd or dst_dir_fd is not None, it should be a file
      descriptor open to a directory, and the respective path string (src or dst)
      should be relative; the path will then be relative to that directory.
    src_dir_fd and dst_dir_fd, may not be implemented on your platform.
      If they are unavailable, using them will raise a NotImplementedError.

>>> 

Thursday, August 16, 2018

Quick learn C to Assembly

I found this link to quickly learn or convert C to Assembly.
1. http://unixwiz.net/techtips/win32-callconv-asm.html

2. https://godbolt.org/





2. If you have source then with GCC convert to Assembly code:
# gcc -S hello.c

3. If you have only executable then use objdump : (uses executable )
# objdump -d hello.out

4. gdb: gdb list command





Signal vs Interrupt

Interrupts are for external events.
When an interrupt occurs , a context switch of already running thread is done to handle that interrupt.
Interrupts are handled by CPU for hardware events.
On receiving interrupt CPU does context switch i.e. saves the state of registers and starts processing the interrupt. (eg: keyboard input).

eg:
Keyboard input
I/O processing
high priority interrupt : NMI


Signals are more of internal mechanism to handle different events.
When a signal is sent to a process then OS interrupts the normal process flow control and handles that signal, if process has its own instructions to handle signal, else OS follows default signal handling. Signals are for process or threads and interrupts are for hardware/external events.

eg: SIGHUP, SIGTERM, 

Process vs Thread

Process states:
Running
Waiting:
Stopped
Zombie :Process died by its entries are still in the process table


Process has a virtual address space which is shared by threads it creates.

A running process has the following thread states :
Thread states:
Init
Ready/Runnable: TS_RUN
Running: TS_ONPROC
Sleeping: TS_SLEEP
Stopped: TS_STOPPED
zombie: TS_ZOMB

Threads inherit address space from parent process.
eg: fork. a child PID returns 0 if it is a thread.

Wednesday, August 15, 2018

stack and heap

Stack:
1. Allocated at compiled time
2. Fast access
3. eg: Arrays
4. LIFO (Last In First Out)
5. Difficult to modify/insert/delete
6. Can be sorted
7. Global variables declared in program, args passed to functions

Heap:
1. Dynamically allocated
2. slow to access
3. eg: malloc allocated memory as heap
4. Easy modify/insert/delete
5. Not Sorted
6. Difficult to traverse
7. Generally pointers used to access memory location

ref:
https://sites.google.com/site/wdhamilttutorials/c/q2/stack_vs_heap

Why use pointers in C/C++

Function return values: normal functions can return only single value. But on the other hand if we call function by reference then you can change multiple values without returning it.

Dynamic memory allocation:

Work directly on memory locations, avoid temporary variable assignment.


Tuesday, August 14, 2018

online compilers

List of online compilers:

https://ide.geeksforgeeks.org/index.php

https://www.tutorialspoint.com/compile_c_online.php

https://www.onlinegdb.com/
( for some reason recursive factorial in C returns 0, while other compilers works fine)

man vs apropos

man keyword shows the man page for given keyword
apropos keyword shows in which man pages this keyword appeared