Skip to main content

Posts

Showing posts from 2018

Get parent element in jQuery

We often need access of parent elements in the DOM while using jQuery functions.This can be achieved by using element.parent(). Let us take some example: HTML Code: <div id= "div1" > <span> This is span </span> </div> jQuery Code: < script > $($document).ready( function (){ var par_id = ( "span" ).parent().attr( 'id' ); alert(par_id); }); < /script>

How to create a directory if not exists?

#!/usr/bin/python import os file_path = "/home/projects/filename.txt" dir = os . path . dirname(file_path) if not os . path . isdir( dir ) : os . makedirs( dir ) os.path.dirname returns the directory name of pathname path  Ex: os.path.dirname('/home/projects/filename.txt') will returns the directory name projects os.path.isdir returns True if path is an existing directory The method makedirs() is recursive directory creation function. Like mkdir(), but makes all intermediate-level directories needed to contain the leaf directory.

Replace the last occurrence of a string in a string using PHP

Code to replace the last occurrence of a string in a string using PHP <?php function str_lreplace ( $search , $replace , $subject ) { $pos = strrpos ( $subject , $search ); print $pos ; if ( $pos !== false ) { $subject = substr_replace( $subject , $replace , $pos , strlen ( $search )); } echo $subject ; return $subject ; } str_lreplace( 'hello' , 'world' , 'this is hello world for checking hello when hello' ); ?> output :

Replace the last occurrence of an expression in a string

Code to  replace the last occurrence of an expression or string in another string using python. def rreplace (s, old, new, occurrence): li = s . rsplit(old, occurrence) print new . join(li) return new . join(li) rreplace( '123434352232' , '2' , '6' , 1 ) Output :

Reading and Generating QR codes in Python using QRtools

What are QR codes? A Quick Response (QR) code is a 2 dimensional barcode that is used due to its fast readability and relatively large storage capacity.  2 dimensional barcodes are similar to one dimensional barcodes, but can store more information per unit area. Installation and Dependencies Linux:   qrtools can be installed on debian based linux systems with the following commands $sudo apt-get update $sudo apt-get install python-qrtools The following dependencies must be installed as well [sudo] pip install pypng [sudo] pip install zbar [sudo] pip install pillow Windows:   qrtools can be installed on windows by downloading the file from here(https://pypi.python.org/pypi/qrtools/0.0.1). On downloading and extraction, run the following command from inside the folder python setup.py install Generating a qrCode: qrtools contains a class QR (can be viewed in the source code), for which we must initially create an object. The object takes the ...

How to find or get urls in a string

Regex to find urls in string import re url = '<p>Hello World</p><a href="ttp://example.com">More Examples</a><a href="https://example2.com">Even More Examples</a>' urls = re . findall( 'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+' , url) print urls

Parsing a YAML file

YAML is a configuration file format The file itself might look like this: mysql: host: localhost user: root passwd: my secret password db: write-math other: preprocessing_queue: - preprocessing.scale_and_center - preprocessing.dot_reduction - preprocessing.connect_lines use_anonymous: yes You can read it like this: #!/usr/bin/env python import yaml #Import yaml module with open ( "example.yml" , 'r' ) as stream: #open the yaml file in read mode try : myDict = (yaml . load(stream)) #load the file into a dictionary called 'Dict' for section in myDict: print section print myDict[ 'mysql' ] except yaml . YAMLError as exc: print (exc) That's all you need. Now the entire yaml file is in 'MyDict' dictionary. Output: other mysql {'passwd': 'my secret password' , 'host': 'localhost...

GMT to desired date and time format

Converting GMT to desired date and time format First, you need to convert the string to a datetime object using strptime dt = datetime.datetime.strptime('Wed, 14 Mar 2018 07:30:00 GMT','%a, %d %b %Y %H:%M:%S %Z') Then convert back in the desired string format using strftime dt.strftime('%Y/%m/%d') Complete code: import datetime dt = datetime . datetime . strptime( 'Wed, 14 Mar 2018 07:30:00 GMT' , '%a, %d %b %Y %H:%M:%S %Z' ) print dt . strftime( '%Y/%m/ %d ' ) Output : '2018/03/14'

Sending Emails using Mandrill API in python

Mandrill is a great API for sending transactional e-mails. Transactional e-mails are emails such as order confirmations, user signups, forgotten passwords and anything that the user may receive during normal use. Mandrill  is  a simple REST API. By creating an account at Mandrill.com, you can get your API keys, and then you are ready to make calls to Mandrill. Requirements : Python 2.6+, Python 3.0+ Getting the library : The preferred method of installing the Mandrill Python API client is by using pip. $ sudo pip install mandrill Using the library: Now that you have a copy of the library in your project, you're ready to start using it. All uses of the Mandrill API start by importing the library module and instantiating the Mandrill class. import mandrill mandrill_client = mandrill . Mandrill( 'YOUR_API_KEY' ) They are different  api call categories.In that we are using messages calls Messages calls: Send a new transactional message through Mandrill: import m...

Basic Linux commands

Linux has a basic impact in our life. However, getting started with Linux just make you discomfort for the first time. Because on Linux, you usually should use terminal commands instead of just clicking the launcher icon (as you did on Windows).But don't worry with experience of more than 3 years as a Developer i feel  Linux is best as using it.We will give you the mostly used linux commands. So let's get started with the list of 10 Linux Basic commands - 1. sudo ("superuser do") - Allows you to run other commands with administrative privileges. This is useful when, for example, you need to modify files in a directory that your user wouldn't normally have access to. $ sudo su 2.   ls ("list") - Lists all files and folders in your current working directory. You can also specify paths to other directories if you want to view their contents. /home$ ls 3. cd ("change directory") - Changes the directory you are currently working in. You can use ...

jQuery Color Picker

This tutorial is to see how to do simple color picker using jQuery. There are various jQuery plugins providing color picker feature. This tutorial gives a simple example code as a guideline to start writing your own jQuery color picker plugin. In this example, we are using CSS selectors to show color palette. On clicking each color in the palette we store the selected color into a pointer to be applied to the target DIV. We are using jQuery CSS function to pick and apply colors to the target DIV. HTML Code for Color Palette: This is the HTML code for showing color palette to pick colors. It shows four colors with separate DIV tags. <div class= "circular-div" id= "blue" onClick= "pickColor(this.id);" ></div> <div class= "circular-div" id= "red" onClick= "pickColor(this.id);" ></div> <div class= "circular-div" id= "yellow" onClick= "pickColor(this.id);...