Monday, January 13, 2020

Run python program from notepad++

Open Notepad++ > Run... > Enter the following command and create a shortcut.


#cmd /k python.exe "$(CURRENT_DIRECTORY)\$(FILE_NAME)"
#cmd /k python.exe "$(FULL_CURRENT_PATH)"

Other variables that may be useful:
$(CURRENT_DIRECTORY)
$(FULL_CURRENT_PATH)
$(FILE_NAME)
$(NAME_PART)

Saturday, January 12, 2019

Python : Check memory address of a variable

Python has a built in function to check memory location of a variable.
The variable "id" can be used for this purpose.


Using this special built in function you will get amazing insight on how assignment and other operations works in python.

eg: 
  1. >>>A=1
  2. >>> id(A)
  3. 1403307696
  4. >>> A=A+4
  5. >>> id(A)
  6. 1403307824
  7. >>>

As you can see in python the memory address of a variable changes when we add integers. As integer variables are immutable similar to string. In this case when we add integer python allocated a new memory to variable and discarded older location.

On the other hand let's see what happens to a list:
  1. >>> mylist=[1,2,3,4]
  2. >>> id(mylist)
  3. 841655129352
  4. >>> mylist.append(5)
  5. >>> id(mylist)
  6. 841655129352
  7. >>>

We can clearly see lists in python are mutable as the address of mylist variable remains same.

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.