Skip to main content

Posts

Showing posts from March, 2019

Array/List of Popular Global Languages

Array/List of Popular Global Languages [ "Afrikanns", "Albanian", "Arabic", "Armenian", "Basque", "Bengali", "Bulgarian", "Catalan", "Cambodian", "Chinese (Mandarin)", "Croation", "Czech", "Danish", "Dutch", "English", "Estonian", "Fiji", "Finnish", "French", "Georgian", "German", "Greek", "Gujarati", "Hebrew", "Hindi", "Hungarian", "Icelandic", "Indonesian", "Irish", "Italian", "Japanese", "Javanese", "Korean", "Latin", "Latvian", "Lithuanian", "Macedonian", "Malay", "Malayalam", "Maltese", "Maori", "Marathi", "Mongolian", "Nepali", "Norwegian...

Angular 7 Structural Directive - *ngif - Two Ways of Using

Angular 7 Structural Directive - *ngif - Two Ways of Using true vs !true app.component.html < button class = "btn btn-primary" (click) = "onSaving()" > Save </ button > < h3 *ngIf = "displaystatus" > {{ firstname }} {{ lastname }} </ h3 > < h3 *ngIf = "!displaystatus" > Enter something </ h3 > *ngif else app.component.html < h3 *ngIf = "!displaystatus; else nodisplay" > Enter something </ h3 > < ng-template #nodisplay > < h3 > {{ firstname }} {{ lastname }} </ h3 > </ ng-template > app.component.ts onSaving () { this . displaystatus = true ; }

Create Mongo Database From JSON Files

Here is how to create Mongo Database from JSON files. Create JSON files with data. Example, I have created a json file named players.json with following data: { "first_name" : "Virat" , "last_name" : "Kohli" , "type" : "batsman" , "age" : 28 , "team" : "Delhi Daredevils" } { "first_name" : "Mahendra" , "last_name" : "Dhoni" , "type" : "all rounder" , "age" : 38 , "team" : "Big Bengaluru" } { "first_name" : "Rohit" , "last_name" : "Sharma" , "type" : "baller" , "age" : 32 , "team" : "Mohali Wale" } and created teams.json file with the following data: { "name" : "Delhi Daredevils" , ...

Technical Interview Questions and Answers in Python to Test Algorithms

Technical Interview Questions and Answers in Python to Test Algorithms Search an element in a sorted and rotated array -  https://repl.it/@VinitKhandelwal/Search-an-element-in-a-sorted-and-rotated-array Missing element in second list -  https://repl.it/@VinitKhandelwal/missing-element-in-second-list Find missing number from a continuous range - https://repl.it/@VinitKhandelwal/find-missing-number-from-a-continuous-range Find elements in a list that are occurring more than once in Pythonic way -  https://repl.it/@VinitKhandelwal/duplicates-from-a-list

Max Profit with K Transactions in Python

Max Profit with K Transactions in Python Run this code here to see live:  https://repl.it/@VinitKhandelwal/max-profit-calls class Stock : def __init__ ( self , slist , count ): self .slist = slist self .count = count print ( self ._func ( slist )) def _func ( self , passlist ): i = passlist [ 0 ] start = i for index , j in enumerate ( passlist [ 1 :], 1 ): if j < i : end = i self .count -= 1 # repeat for the number of calls to be generated if self .count!= 0 : print ( self ._func ( passlist [ index :])) return ( start , end , end-start ) i=j end = i return ( start , end , end-start ) # passing each day's closing price of stock in list AND number of calls to be generated. print ( "continuous rising - 2 calls" ) obj = Stock ([ 5 , 7 , 11 , 50 , 60 , 90 ], 2 ) print ( "one dip - 2 calls" ) obj1 = ...

Binary Tree Traversal in Python - In Order, Pre Order, Post Order

Binary Tree Traversal in Python - In Order, Pre Order, Post Order Run the code here to see output:  https://repl.it/@VinitKhandelwal/iterative-traversal-of-binary-tree # Node Class to Create Object of Nodes class Node (): def __init__ ( self , value , left= None , right= None ): self .value = value self .left = left self .right = right # Creation of a Binary Tree class BinaryTree (): def __init__ ( self , mylist ): self .mylist = mylist self .root=Node ( None ) for value in self .mylist : self .curr= self .root self ._add ( value ) def _add ( self , value ): if self .curr.value == None : self .curr.value = value else : if self .curr.value > value : if self .curr.left == None : self .curr.left = Node ( value ) else : self .curr = self .curr.left self ._add ( value ) else : if self...

Error Downloading Misaka? Download this

On pip install misaka if you get the following error then install the link given below and try again. Failed building wheel for misaka and error: Microsoft Visual C++ 14.0 is required. Error says to install Microsoft Visual C++, but it still won't work. Follow the following solution instead. Install Anaconda Run the following commands: conda install libpython m2w64-toolchain -c msys2 python -m pip install --upgrade pip pip install misaka

Create Binary Tree from a List of Values in Python

Create Binary Tree from a List of Values in Python Run and Try:  https://repl.it/@VinitKhandelwal/create-binary-tree class Node (): def __init__ ( self , value , left= None , right= None ): self .value = value self .left = left self .right = right class BinaryTree (): def __init__ ( self , mylist ): self .mylist = mylist self .root=Node ( None ) for value in self .mylist : self .curr= self .root self ._add ( value ) def _add ( self , value ): if self .curr.value == None : self .curr.value = value else : if self .curr.value > value : if self .curr.left == None : self .curr.left = Node ( value ) else : self .curr = self .curr.left self ._add ( value ) else : if self .curr.right == None : self .curr.right = Node ( value ) else : self .curr = self .curr.ri...

Create Binary Tree From Numbers As They Are Feeded

Here is a method to create Binary Tree from numbers as they are feeded (which means without sorting first) class Node (): def __init__ ( self , value , left= None , right= None ): self .value = value self .left = left self .right = right class BinaryTree (): def __init__ ( self ): self .root=Node ( None ) def add ( self , value ): self .curr= self .root self ._add ( value ) def _add ( self , value ): if self .curr.value == None : self .curr.value = value else : if self .curr.value > value : if self .curr.left == None : self .curr.left = Node ( value ) else : self .curr = self .curr.left self ._add ( value ) else : if self .curr.right == None : self .curr.right = Node ( value ) else : self .curr = self .curr.right self ._add ( value ) obj = BinaryTree () ...

user.authenticated Not Working in Django Template

Is user.authenticated Not Working in Django Template? The reason is that it is not user.authenticated. It is user.is_authenticated. Saw this mistake very commonly made. So thought of mentioning here in case you have been stuck with this silly mistake. Good luck! {% if user.is_authenticated %} <li class= "nav-item" ><a class= "nav-link" href= "{% url 'accounts:logout' %}" > Log out </a></li> {% else %} <li class= "nav-item" ><a class= "nav-link" href= "{% url 'accounts:signup' %}" > Sign up </a></li> <li class= "nav-item" ><a class= "nav-link" href= "{% url 'accounts:login' %}" > Log in </a></li> {% endif %}

Desktop Site Visible On Mobile Using Bootstrap

If you are experiencing a problem where you are using bootstrap but on mobile the responsiveness is not working great. Then you are missing something. And that is: <meta name="viewport" content="width=device-width, initial-scale=1.0"> Add this in the <head> </head> and your problem will be resolved. If you want desktop site to be visible on all devices you will need to change it to: <meta name="viewport" content="width=1024"> Hope this was helpful.

Text is Overflowing in Jumbotron, Cutting Text In The Middle

If you are also experiencing the problem where text is overflowing while using Jumbotron, that is, Jumbotron is cutting text In the middle then somewhat this is what your code looks like: Problem <div class= "jumbotron" > <div class= "col-md-6 col-md-offset-3" > <h1> PyVinit Blog </h1> </div> </div> Solution <div class= "jumbotron" > <div class= "row" > <div class= "col-md-6 col-md-offset-3" > <h1> PyVinit Blog </h1> </div> </div> </div> col-md-6 is the culprit here. Wrap col-md-6 with another div with class row and your jumbotron will work fine.

007 Views in Django using a Generic View Called TemplateView

TemplateView helps us create Class Based Views (CBV). Let's learn TemplateView with an example. We begin by creating view for index.html page. Under views.py file in your app (Create one if you don't have), add the following: from django.views.generic import View, TemplateView class IndexView(TemplateView):     template_name = 'mainApp/index.html'     def get_context_data(self, **kwargs):         context = super().get_context_data(**kwargs)         context['text'] = "Hello World!"         context['number'] = 100         return context Display these values from views.py in index.html in the following manner: <h2>{{ text }} {{ number|add:"99" }}</h2> This is a short lesson but a useful one.

006 A Guide to Deploy Your Django Application on PythonAnywhere

A step-by-step guide to deploy your django application on PythonAnywhere. Go to pythonanywhere.com Click on Console Tab Click on Bash Link Activate virtual environment with the following command: mkvirtualenv --python=python3.5 myproj use the command pip list to view all the packages that are already installed Use pip install django to install latest version of Django Get the application running in the instance by cloning from Github. Use git clone <HTTPS link>  You will find the HTTPS link by clicking on Clone or download button in your github repository Use command ls to view all files and folders in the virtual environment. go into the project folder using cd <project_folder> Use command  ls  to view all files and folders inside the project folder Keep going inside until you find manage.py file Once you find manage.py, use the following command python manage.py migrate My application uses Pillow library so I get an error. I will in...

005 Inbuilt and Custom Template Tag Filter in Django

Inbuilt and Custom Template Tag Filter in Django Inbuilt Filter One can use in-built filters directly See an example of in-built filters below. data.number is passed from view and 99 is added to the passed value before displaying index.html <!DOCTYPE html > {% extends "mainApp/base.html" %} {% block body_block %} <div class= "container" > <div class= "jumbotron" > <h1> Welcome to a project about users </h1> <h2> {{ data.number|add:"99" }} </h2> </div> </div> {% endblock %} Custom Filter To create a custom filter as per your unique needs follow this process: Inside app folder, create a folder named templatetags Inside templatetags create a blank file named __init__.py. This tells python that templatetags is a package Create another file named my_extras.py. This is where custom filters will ...

004 Reuse block of HTML in Django

Keep parts of html same on all pages in Django. Reuse block of HTML in Django. templates > app_ name > base.html Create base.html under templates > app_name Add the following code wherever other html content will come <!DOCTYPE html > <html> <head> <meta charset= "utf-8" > <title></title> <link rel= "stylesheet" href= "https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" > </head> <body> <nav class= "navbar navbar-expand-lg navbar-light bg-light" > <a class= "navbar-brand" href= "#" > Navbar </a> <button class= "navbar-toggler" type= "button" data-toggle= "collapse" data-target= "#navbarSupportedContent" aria-controls= "navbarSupportedContent" aria-expanded= "false" aria-label= "Toggle n...

003 Create Forms And Update Values Received to Database in Django

Create Forms And Update Values Received to Database in Django Create a file named forms.html in templates > mainApp and add the following to it. <!DOCTYPE html > <html> <head> <meta charset= "utf-8" > <title> Sign Up </title> <link rel= "stylesheet" href= "https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" > </head> <body> <div class= "container" > <h1> Sign Up </h1> <form method= "POST" > {{ form.as_p }} {% csrf_token %} <input type= "submit" class= "btn btn-primary" value= "Submit" > </form> </div> </body> </html> In urls.py in Project Folder add the following line to urlpatterns: url( r'^signup/' ...

002 Create Custom Validation for Django Form Input

Create Custom Validation for Django Form Input Here is the forms.py file to write an example custom validator that ensures input text starts with 'Z' or 'z'. from django import forms from django.core import validators def check_initial_z (value): if value[ 0 ].lower() != 'z' : raise forms.ValidationError( "Name needs to start with Z" ) class FormName(forms.Form): name = forms.CharField( validators =[check_initial_z]) email = forms.EmailField() text = forms.CharField( widget =forms.Textarea)