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 




Tuesday, July 17, 2018

Exclude endpoint mapping from Swagger documentation in Springfox



 Exclude endpoint mapping from Swagger documentation in Springfox

By default, Swagger ui shows all the endpoint defined via a controller in SwaggerConfig using Docket.
But there are situations in which you dont want those endpoints to be visible in swagger ui.

To achieve this, we can do this following way:

@Bean
    public Docket productApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select().paths(Predicates.not(PathSelectors.regex("/|/error|/swagger.yml"))).apis(RequestHandlerSelectors.basePackage("com.daimler.datalayer.apistreamintegration.controller"))
                .paths(PathSelectors.any())
                .build().apiInfo(apiInfo());


Just provide all regex which you want to exclude.
Here in the example i have excluded all mapping /, /error and /swagger.yml

Cheers!!!

Friday, July 6, 2018

Get me Git

Some useful terms

master: this is the main code branch, equivalent to trunk in Subversion. Branches are generally created off of master.
origin: the default remote repository that all your branches are pull'ed from and push'ed to. This is defined when you execute the initial git clone command.
unpublished vs. published branches: an unpublished branch is a branch that only exists on your local workstation, in your local repository. Nobody but you know that branch exists. A published branch is one that has been push'ed up to github, and is available for other developers to checkout and work on.
fast-forward: the process of bringing a branch up-to-date with another branch, by fast-forwarding the commits in one branch onto the other.
rebase: the process by which you cut off the changes made in your local branch, and graft them onto the end of another branch.
repo, repository: This is your object database were your history and configuration is stored. May contain several branches. Often it contains a worktree too.
a git, "the git": never heard of, sorry. "the git" probably describes the software itself, but I'm not sure
index, staging area: This is a 'cache' between your worktree and your repository. You can add changes to the index and build your next commit step by step. When your index content is to your likes you can create a commit from it. Also used to keep information during failed merges (your side, their side and current state)
clone: A clone of a repository ("just another repository") or the act of doing so ("to clone a repository (creates a new clone)")
commit: A state of your project at a certain time. Contains a pointer to its parent commit (in case of a merge: multiple parents) and a pointer to the directory structure at this point in time.
branch: A different line of development. A branch in git is just a "label" which points to a commit. You can get the full history through the parent pointers. A branch by default is only local to your repository.
tree: Basically speaking a directory. It's just a list of files (blobs) and subdirectories (trees). (The list may also contain commits in case you use submodules, but that's an advanced topic)
upstream: After cloning a repository you often call that "original" repository "upstream". In git it's aliased to origin
HEAD: A symbolic name to describe the currently checked out commit. Often the topmost commit
tag: A descriptive name given to one of your commits (or trees, or blobs). Can also contain a message (eg. changelog). Tags can be cryptographically signed with GPG.
patch: A commit exported to text format. Can be sent by email and applied by other users. Contains the original auther, commit message and file differences
stash: Git allows you to "stash away" changes. This gives you a clean working tree without any changes. Later they can be "popped" to be brought back. This can be a life saver if you need to temporarily work on an unrelated change (eg. time critical bug fix)
object: can be one of committreeblobtag. An object has associated its SHA1 hash by which it is referenced (the commit with id deadbeaf, the tree decaf). The hash is identical between all repositories that share the same object. It also garuantees the integrity of a repository: you cannot change past commits without changing the hashes of all child commits.
(module,) submodule: A repository included in another repository (eg. external library). Advanced stuff.
revspec: A revspec (or revparse expression) describes a certain git object or a set of commits through what is called the extended SHA1 syntax (eg. HEADmaster~4^2origin/master..HEADdeadbeaf^!, …)
refspec: A refspec is pattern describing the mapping to be done between remote and local references during Fetch or Push operations
history: Describes all ancestor commits prior to a commit going back to the first commit.

Note: Use This for more detailed info of git glossary: 

git help glossary


Simple Git Guide:

        Your local repository consists of three "trees" maintained by git.  The first one is your Working Directory which holds the actual files. The second one is the Index which acts as a staging area and finally the HEAD which points to the last commit you've made.

                                                  

ADD and Commit:
     You can propose changes (add it to the Index) using
          git add <filename>
          git add *

This is the first step in the basic git workflow. To actually commit these changes use
         git commit -m "Commit message"

Now the file is committed to the HEAD, but not in your remote repository yet.

Pushing changes to remote repository:
Your changes are now in the HEAD of your local working copy. To send those changes to your remote repository, execute 
        git push origin master
Change master to whatever branch you want to push your changes to.

If you have not cloned an existing repository and want to connect your repository to a remote server, you need to add it with
       git remote add origin <server>   
Now you are able to push your changes to the selected remote server

Branching:
Branches are used to develop features isolated from each other. The master branch is the "default" branch when you create a repository. Use other branches for development and merge them back to the master branch upon completion.
create a new branch named "feature_x" and switch to it using
       git checkout -b feature_x
switch back to master
       git checkout master
and delete the branch again
       git branch -d feature_x
a branch is not available to others unless you push the branch to your remote repository
       git push origin <branch>

Update & Merge
To update your local repository to the newest commit, execute 
       git pull
in your working directory to fetch and merge remote changes.
to merge another branch into your active branch (e.g. master), use
      git merge <branch>
in both cases git tries to auto-merge changes. Unfortunately, this is not always possible and results in conflicts. You are responsible to merge those conflicts manually by editing the files shown by git. After changing, you need to mark them as merged with
      git add <filename>
before merging changes, you can also preview them by using
      git diff <source_branch> <target_branch>

logging
    git log
Replace local changes:
In case you did something wrong, which for sure never happens ;), you can replace local changes using the command
       git checkout -- <filename>
this replaces the changes in your working tree with the last content in HEAD. Changes already added to the index, as well as new files, will be kept.
If you instead want to drop all your local changes and commits, fetch the latest history from the server and point your local master branch at it like this
        git fetch origin
        git reset --hard origin/master