Monday, September 16, 2019

docker-commands-cheat-sheet

Docker Commands
Description
docker search searchterm
Search Docker Hub for images.
docker pull user/image
Downloads an image from Docker Hub.
docker login
Authenticate to Docker Hub
(or other Docker registry).
docker push user/image
Uploads an image to Docker Hub.
You must be authenticated to run this command.
docker ps
List all running containers.
docker ps -a
List all container instances, with their ID
and status.
docker images
Lists all images on the local machine.
docker history user/image
Lists the history of an image.
docker logs [container name or ID]
Displays the logs from a running container.
docker port [container name or ID]
Displays the exposed port of a running container.
docker diff [container name or ID]
Lists the changes made to a container.
docker run -it user/image
Runs an image, creating a container and
changing the terminal
to the terminal within the container.
docker run -p $HOSTPORT:$CONTAINERPORT -d user/image
Run an image in detached mode
with port forwarding.
ctrl+p then ctrl+q
From within the container’s command prompt,
detach and return to the host’s prompt.
docker attach [container name or ID]
Changes the command prompt
from the host to a running container.
docker start [container name or ID]
Start a container.
docker stop [container name or ID]
Stop a container.
docker rm -f [container name or ID]
Delete a container.
docker rmi
Delete an image.
docker tag user/image:tag user/image:newtag
Add a new tag to an image.
docker exec [container name or ID] shell command
Executes a command within a running container.
docker commit user/image
Save a container as an image.
docker save user/image
Save an image to a tar archive.
docker build -t sampleuser/ubuntu .
Builds a Docker image from a Dockerfile
in the current directory.
docker load
Loads an image from file.
docker info
Get information about the docker infra.
docker images
Get all the images in local docker repo.
docker rm $(docker ps -a -q)
Delete all containers
docker rmi $(docker images -q)
Delete all images---- use -f is needed for force



Sunday, September 8, 2019

python-cheat-sheet

Python-Cheat-Sheet


Modularity

- Python code is placed in *.py files called modules

- Modules can be executed directly with :
py module_name.py
- Modules can be imported in the REPL or in other module using:
import module_name
- Named Functions are defined with the def keyword, eg:
def function_name(arg1,argn):
- Return statement from function is optional.

- Default return is None.

- Use __name__ to determine how the module is being executed.(directly py or by importing)

- if __name__=='__main__' then the module is being executed.

- Module code is executed exactly once, on first import

- Command line arguments are accessible through sys.argv

- sys.argv[0] is the filename.

- Docstrings are the standalone literal string as the first statement of a function or module.

- Docstring are delimited by triple quotes.

- Docstring provide help(module_name)

- Comments begin with #.

- A special comment on the first line beginning #! controls module execution by the program loader/interpreter.



Objects in Python


- Named reference to objects rather than variables
* Assignment attaches a name to an object
* Assigning from one reference to another puts two name tags on the same object.

- Garbage collector reclaims unreachable objects.

- 'id()' returns a unique and constant identifier

- 'is' operator determines equality of identity (same id)

- '==' is used for equality check

- Function arguments are passed by object reference.

- Reference is lost if function argument is reassigned to other object.

- 'retun' also passes by object reference.

- Function arguments can be specified with defaults.

- Default argument expressions evaluated once when def is executed.

- Python uses dynamic and strong typing.

- Names are looked  up in four nested scopes
LEGB rule: Local , Enclosing , Global and Built-ins
- Use global to assign to global references from a local scope.

- 'type' can be used to determine the type of an object.

- 'dir()' is used to get details of an object and its attributes.

- '__name__' attribute gives name of function or module object.

- '__doc__' attribute gives docstring for a funtion or module object

- 'len()' is used to measure the length of a string.

- 'repetition' operation-> multiply a string by an integer.


Collections in python

-- Tuple
- hetrogeneous immutable sequence.
- delimited by parantheses.
- Items seperated by commas.
- Element access with square brackets and zero based index t[index].
- len(t) for number of elements.
- Iteration with for loop.
- Concatination with + operator.
- Repetition with * operator.
- nested tuples possible. ((3,2),(5,2),(5,1))
- Can't use one object in parantheses as single element tuple. t = (3), is allowed, but its not tuple, its int.
- For a single element tuple include a trailing comma. eg : t=(3,).
- The empty tuple is simply empty parentheses. eg: t = ().
- Delimiting parantheses are optional for one or more elements. t = 3,4 2,6,9
- Tuples are useful for multiple return values.
- Tuple unpacking allows us to destructure directly into named references.
- Tuple unpacking works with arbitrarily nested tuples(although not with other data structure)
eg: (a,(b,(c,d))) = (3,(4,(8,3)))
- a,b = b,a is the idiomatic swap.
- use the tuple(iterable) constructor to create tuples from other iterable series of objects.
eg: tuple([2,43,5,3,23])
tuple("its is tuple") -- > ('i', 't', 's', ' ', 'i', 's', ' ', 't', 'u', 'p', 'l', 'e')
- The 'in' and 'not in' operators can be used  with  tuples and other collection types for membership testing.
eg: 5 in (4,3,3,4,5,2,1) or 8 not in (4,3,3,4,5,2,1)

-- String (str)
- homogenous immutable sequence of Unicode codepoints (Characters)
- len(s) gives length
- '+' or '+=' can be used for concatination
- join(s1,s2,....sn) is best way to concatination.
eg : "".join(["ye","ah"])--> 'yeah'
";".join(["ye","ah"])--> 'ye;ah'
 
- split(sperator)  is used to split.
eg: 'ye;ah'.split(';') --> ['ye','ah']
- without arguments, split divides on whitespaces.
eg: 'asfgafsdgafg dfga afsg asfg asfg'.split() --> ['asfgafsdgafg', 'dfga', 'afsg', 'asfg', 'asfg']
- The partition() method divides a string into 3 around a separator: prefix, separator,suffix
eg: "unpunishable".partition('punish') --> ('un', 'punish', 'able')
- String unpacking can be used in partition method.
eg : hour, colon, minute = "06:45".partition(':')
- '_' use underscore as a dummy name for separator
eg: hour, _, minute = "06:45".partition(':')
- use format() to insert values into string.
eg: "My name is {0}. I am an {1}. I am proud to be {1}.".format('Sadique','Engineer')
- Field names in {} can be omited if used in sequence.
eg: "This is {} and that is {}".format('good','bad')
- field naming can be used in {} for formating.
eg: "Current position: {long} {lat}".format(long='60N',lat='5E')
- tuple can be used to index values in format:
eg: "This is an example for sequence: {pos[0]},{pos[1]},{pos[2]},{pos[3]}".format(pos=t) --> 'This is an example for sequence: 1,2,3,4'
- Access attributes using dot in the replacement filed in format()
eg: "Math constants: pi = {m.pi}, e = {m.e}".format(m = math) --> 'Math constants: pi = 3.141592653589793, e = 2.718281828459045'

-- Range
- arthemetic progression of integers. range(start,stop,step)
- stop value is 1 more than it.
eg: range(5) --> range(0,5) ie: 0,1,2,3,4
- range values can be used to convert to list.
eg: list(range(2,6)) --> [2, 3, 4, 5]
- optional 3rd step value
eg: list(range(2,8,2)) --> [2,4,6]
- use enumerate to get index and its value.
eg: t = [2,5,8,2,2,4,6]
for p in enumerate(t):
print(p)
Ans: (0, 2)
(1, 5)
(2, 8)
(3, 2)
(4, 2)
(5, 4)
(6, 6)

-- list
- hetrogeneous mutable sequence
- Negative integers index from end. Last element is at index -1
- Slicing extracts part of list. slice = s[start:end]. start is included,while end not.
- Slicing works with negative indexes.
- start and end index is optional, provide any one. eg: s[1:], s[:3]
- A full slice = Omitting start and end index slices form begining to end. s[:]
- Good way to copy list. eg : new_lst = s[:]
- List copying ways: Full slice, copy method, list constructor.
- These all copying techniques are shallow copying
- Repeat list using * operator. Repetition is shallow.
-  index(item) returns the integer index of the first equivalent  element. if not found gives ValueError.
- count(item) returns the number of matching elements.
- 'in' and 'not in' is used for membership.
- 'del s[index]' to remove by index.
- s.remove(item) to remove by value. throws 'ValueError' if not found.
- Insert the items with 's.insert(index,item)'
- concatinate list with + operator.
- In place extension can be done using += or extend method.
- list can be reversed using reverse() method. list.reverse()
- list can be sorted using sort(). list.sort(), it can take an argument called reverse, if True, returns decending order.
- sort method takes 'key' as argument, which expects a function for defining the sorting logic. this function should be in item object
e.g: list.sort(key=len) --> sorts element depending on the length of the items. This would give error for int list as len method is not there in int.
- sorted() is a built-in function which sorts any iterable series and returns a list.
eg: y = sorted(x)
- reversed is a built-in function which reverses the iterable items.
eg: y = reversed(x)

-- dict
- unordered mapping from unique, immutable keys to mutable values.
- dict is delimited by { and }. comma seperated 'key:value' pairs. key should be unique.
- key in dict must be immutable and values can be mutable. Ordering is not relaiable.
- dict() constructor accepts:
* iterable series of key-value 2-tuples.
eg: name_ages = [("jack", 10),("Rony", 45),("bala",34),("Huge",39)]
d = dict(name_ages)
output = {'jack': 10, 'Rony': 45, 'bala': 34, 'Huge': 39}
* keyword arguments - eg: val = dict(a="one", b = "two", c ="three") outputs - > {'a': 'one', 'b': 'two', 'c': 'three'}
- dict copying
* d.copy()
* dict(d)
- update() function can be used to update an existing dict. eg: f.update(g)
- Iteration is over keys. to get value: v[key]. Order is arbitrary.
- Use 'values()' for iterable view onto the series of values.
- No efficient way to get they key from the corresponding value.
- Use 'items()' for iterable view onto the series of key-value tuples.
- The 'in' and 'not in' operators work on keys.
- Use 'del' keyword to remove by key. eg : del d[key]. KeyError if key not found.

-- set
- unordered collection of unique , immutable objects
- delimted by { and }, single comma seperated items.
- empty {} makes a dict, so for empty set use set constructor. eg d = set().
- set constructor accepts:
* iterable series of values. eg s= set([1,3,4,45,5])
* duplicates are discarded.
* no order preserving.
- sets are iterable, order is arbitrary.
- 'in' and 'not in' works in sets.
- add(item) inserts a single element.
- for multiple elements to be added , use update(items). items is any iterable series.
- 'remove(item)' to remove item, gives KeyError if not found.
- 'discard(item)' also removes item, but no side effects if item is not found.
- 's.copy()' and constructor set(s) to create a copy of set.
- 's.union(t)' to combine to set.
- 's.intersection(t)' to get common fields.
- 's.difference(t)' to get if one set doesnot have given set.
- 's.symmetric_difference(t)' to get all which in not common
- 's.issubset(t)' to get if all are contained in given set.
- 's.issuperset(t)' just opposite of issubset.
- 's.isdisjoint(t)' to check if nothing is in common.

Wednesday, May 29, 2019

Day today findings in work.

Day today findings in work.

I thought of placing small findings which bugged while working in day to day life. I think it may help me and others in future, so that i don't repeat same  mistake again.
  1.  As per Javadoc, System.lineSeparator() doesn't exists in java < 7. Use System.getProperty("line.separator") of java <= 6 .
  2. For SOAP request/response to have namespace prefix , WSDL should be designed properly
            a) Use elementFormDefault="qualified" in <schema> tag in WSDL
            b) Donot use anonymous types in schema definition in 
               WSDL(avoid-anonymous-types)

            c) To make custom prefix: update generated package-info.java
               
    XmlSchema(elementFormDefault=XmlNsForm.QUALIFIED,
    namespace="http://www.example.com/FOO",
    xmlns={@XmlNs(prefix="bar",
                  namespaceURI="http://www.example.com/BAR")}
     )

    3. If you want to change Environment variable without admin rights, the use this       
       command:


rundll32 sysdm.cpl,EditEnvironmentVariables
 
        4. Install python without admin rights : Execute this way:

      C:\development\apps>msiexec /a python-2.7.12.msi /qb                        
                                            TARGETDIR=C:\Development\apps\python_2.7.12


       5. Set proxy for npm command: use %40 for @
           $ npm config set proxy http://username:password@hostname:port

       6. Set proxy for git command:
           $ git config --global http.proxy http://proxyuser:proxypwd@proxy.server.com:port
      $ git config --global https.proxy https://proxyuser:proxypwd@proxy.server.com:port

    7. Remote debug using eclipse Tomcat.
       a) create/update file setenv.bat in {TOMCAT_HOME}/bin.
       b) add CATALINA_OPTS="-Xdebug -        Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n" in setenv.bat file.
       c)save the file and close.
       d)run the server from command prompt using this command : catalina.bat jpda start
          e.g, : C:\software\apache-tomcat-7.0.53-windows-x64\apache-tomcat-7.0.53\bin>catalina.bat jpda start
       e) Go to Eclipse and open debug configuration and create remote java application.             use the hostname and port which used in setenv.bat file.
       f)click on debug to start in debug mode.


        8. Opening windows command prompt from Windows Explorer.
           a) Open the Windows explorer with your directory location. Go to address bar and type cmd and hit enter. It will open up the CMD for you.
           b) Open the Windows explorer with your directory location. select Shift and right click , select the option 'Open command window here.'. It will open uo the CMD for you.


Using PatriciaTrie 

(http://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/trie/PatriciaTrie.html)



public static void main(String[] args) {
              Map<String, Student> stdMap = new LinkedHashMap<String, Student>();
              /* Load your regualr map with data */
              Student std1 = new Student();
              std1.setName("Raj");
              std1.setRoll("23");
              stdMap.put("Raj ", std1);
             
              Student std2 = new Student();
              Std2.setName("Rak");
              Std2.setRoll("22");
              stdMap.put("Ra ", std2);
             
              Student std3 = new Student();
              Std3.setName("Raop");
              Std3.setRoll("2");
              stdMap.put("Raop", std3);
             
              Student std4 = new Student();
              Std4.setName("Pat");
              Std4.setRoll("4");
              stdMap.put("Pat ", std4);
             
              Student std5 = new Student();
              Std5.setName("kalo");
              Std5.setRoll("34");
              stdMap.put("kalo", std5);
             
              /* PatriciaTrie map*/
              PatriciaTrie<Student> stdMapTrie = new PatriciaTrie<Student>(stdMap);
             
              /*SortedMap<String, Student> entries1 = stdMapTrie.prefixMap("Ra");
              System.out.println(entries1);
             
              SortedMap<String, Student> entries2 = stdMapTrie.headMap("ak");
              System.out.println(entries2);
             
              SortedMap<String, Student> entries3 = stdMapTrie.subMap("", "47");
              System.out.println(entries3);
             
              SortedMap<String, Student> entries4 = stdMapTrie.tailMap("00");
              System.out.println(entries4);*/
             
              //Entry<String, Student> entries5 = stdMapTrie.select("5600");
              //System.out.println(entries5.getValue().getSTD());
              //
             
       }

https://github.com/sanjar/Day_Today/tree/master/apiinpractice

9.   Whenever you are using Angular Bootstrap Module.
       --> npm install --save @ng-bootstrap/ng-bootstrap
               Don't forget to include  NgbModule  in app.module.ts imports section.

10. To know proxy setting using Chrome:
       ---> chrome://net-internals/#proxy

11. Setting global proxy in CentOS for YUM downloads:
      ---> Add following in /etc/yum.conf file
              # The proxy server - proxy server:port number
           proxy=http://mycache.mydomain.com:3128
        # The account details for yum connections
           proxy_username=yum-user
           proxy_password=qwerty

12. I solved the issue of proxy setting in Windows docker tool box by modifying config.json
    C:\Users\<username>\.docker\machine\machines\default\config.json
    Put the proxy setting in HostOptions -> EngineOptions

    "Env": [
                "HTTP_PROXY=http://APAC\\\\username:password@proxy-url:port/",
                "HTTPS_PROXY=https://APAC\\\\username:password@proxy-url:port/"
            ],

Then run this command to get it updated:
docker-machine provision


13. Sparse checkout git.
          There are multiple ways to checkout only a portion of repository from git. Here one i find easy one. Run this in git bash.

        git init 
    git remote add origin <url>
    git config core.sparsecheckout true
    echo "inner_folder/*" >> .git/info/sparse-checkout
    git pull --depth=1 origin master

14. In Java , if i do a split on a string using some delimiter then, it does not consider the empty portion at the end after delimiter.

  Eg:
         String[] s = "hello;world;".split(";");
         System.out.println(s);

Developer Expected output: [hello, world, ]
Actual output: [hello, world]

To get the actual developer expected output, split method has to be overloaded.

     String[] s = "hello;world;".split(";",-1);
     System.out.println(s);



15. How to know the Browser details using javascript?
    Ans: In Javascript there is an object called navigator, which has got some variable which gives information about browser you are in. Keep in mind that every new version of browser may have different pattern of informations.

  • navigator.userAgent
  • navigator.appVersion
  • navigator.appName
  • navigator.appCodeName
  • navigator.platform
16. In Github, if credential prompt is not coming, run this command in command line in windows.
  •     git config --system credential.helper store
17.  Setting proxy for Pip (python) :

  • use --proxy http://<proxy_url>:<port> along with pip install command
  • set http_proxy and https_proxy in env or command prompt. Format would be : http://username:password@proxyAddress:port
18. If you want to update/reset your git credentials or any other credentials in windows 10, here is the quick way.
        Control Panel --> User Accounts --> Credential Manager --> Windows Credentials.

19. Kill Process associated with a Port No in windows
       1. Run this in cmd : netstat -ano | findstr :<PORT_NUMBER> jlkjlk
       2. Execute this with your pid which you get from step 1: taskkill /PID <processid> /F ;

20. Reverting pushed code in git remote repo:
        # To revert the changes pushed in remote repo with history
            >git reset --hard <commit-id>
            >git push -f
 
       # To revert the changes pushed in remote repo keeping the history:
            >git revert <commit-id> or git revert -m 1 <commit-id> (if its merge commit)

    Good reference:
        https://christoph.ruegg.name/blog/git-howto-revert-a-commit-already-pushed-to-a-remote-reposit.html