How to use Fabric in Python

How to use Fabric in Python

What is Fabric? Fabric is a Python library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks. Typical usage involves creating a Python module containing one or more functions, then executing them via the fab command-line tool. You can execute shell commands over SSH, so you only need to have SSH running on the remote machine. It interacts with the remote machines that you specify as if they were local. Installation Fabric requires Python version 2.5 or 2.6 (Fabric has not yet been tested on Python 3.x) The most common ways of installing Fabric is via, pip, easy_install or via the operating system’s package manager: pip install fabric sudo easy_install fabric sudo apt-get install fabric # (the package is typically called fabric or python-fabric.) Please read the official documentation for more information (dependancies etc..) How to use Pip and PyPI How to use Virtualenv in Python How to use Python virtualenv Fabric Usage On their website they write: “it provides a basic suite of operations for executing local or remote shell commands (normally or via sudo) and uploading/downloading files, as well as auxiliary functionality such as prompting the running user for input, or…

How to use Pillow, a fork of PIL

How to use Pillow, a fork of PIL

Overview In the last post, I was writing about PIL, also known as Python Imaging Library, this library can be used to manipulate images quite easily. PIL hasn’t seen any development since 2009. Therefore, the kind users of this site suggested taking a look at Pillow. This article will tell you how to use Pillow. What is Pillow? Pillow is a fork of PIL (Python Image Library), started and maintained by Alex Clark and Contributors. It was based on the PIL code and then evolved to a better, modern, and more friendly version of PIL. It adds support for opening, manipulating, and saving many different image file formats. A lot of things work the same way as the original PIL. Python – Quick Guide Resize an Image with Python 2.x (and in Batch) Make your life easier with Virtualenvwrapper Download and Installing Pillow Before we start to use the Pillow, we must first download and install it. Pillow is available for Windows, Mac OS X and Linux. The most recent version is “2.2.1” and is supported by python 2.6 and above. To install Pillow on Windows machines you can use easy_install: easy_install Pillow To install Pillow on Linux machines simply…

Using Feedparser in Python

Using Feedparser in Python

Overview In this post we will take a look on how we can download and parse syndicated feeds with Python. The Python module we will use for that is “Feedparser”. The complete documentation can be found here. What is RSS? RSS stands for Rich Site Summary and uses standard web feed formats to publish frequently updated information: blog entries, news headlines, audio, video. An RSS document (called “feed”, “web feed”, or “channel”) includes full or summarized text, and metadata, like publishing date and author’s name. [source] What is Feedparser? Feedparser is a Python library that parses feeds in all known formats, including Atom, RSS, and RDF. It runs on Python 2.4 all the way up to 3.3. [source] RSS Elements Before we install the feedparser module and start to code, let’s take a look at some of the available RSS elements. The most commonly used elements in RSS feeds are “title”, “link”, “description”, “publication date”, and “entry ID”. The less commonnly used elements are “image”, “categories”, “enclosures” and “cloud”. HTML Parser: How to scrape HTML content | Parsing HTML in Python with BeautifulSoup Python Beautiful Soup Example: Yahoo Finance Scraper Beautiful Soup 4 Python Install Feedparser To install feedparser on your…

Using the CSV module in Python

Using the CSV module in Python

If you want to import or export spreadsheets and databases for use in the Python interpreter, you must rely on the CSV module, or Comma Separated Values format. What is a CSV File? CSV files are used to store a large number of variables – or data. They are incredibly simplified spreadsheets – think Excel – only the content is stored in plaintext. And the CSV module is a built-in function that allows Python to parse these types of files. It’s worth noting that when you work with a CSV file, you are dabbling in JSON development. JSON – which stands for JavaScript Object Notation – is a format that is used to store information as JavaScript code in plaintext files. You don’t need to know JavaScript to work with these files, nor is the practice confined to that language. Obviously, since we’re working with Python here. The text inside a CSV file is laid out in rows, and each of those has columns, all separated by commas. Every line in the file is a row in the spreadsheet, while the commas are used to define and separate cells. How to Validate an Email Address Using Python | Check if…

When to Use trycatch Instead of ifelse

When to Use try/catch Instead of if/else

While programming, we have to deal with many constraints which are imposed on variables so that program can be executed in proper way. To enforce the constraints on the variables, we use if else block as well as try catch blocks. In this article we will see how both the constructs work and we will look into the conditions where if-else blocks can be used and where try-except blocks can be used. Python Programming – Decision Making Conditional statements in Python Comment out a block of code in Python Working of if-else If-else blocks are used in python or any other programming language to control the execution of statements in a program based on a conditional statement.If The condition mentioned in the if block is True then the statements written in if block are executed. If the condition evaluates to False, the statements written in else block of the program are executed. If-else block is mainly used for flow control of the program based on the conditions which are known in advance to us. For Example, suppose we are performing a division operation in our program. In this case the variable in the divisor cannot be zero. If the divisor…

How to use For and While Loops in Python

How to use For and While Loops in Python

Why Loops? All programming languages need ways of doing similar things many times, this is called iteration. For Loop The for statement is used to iterate over the elements of a sequence. It’s traditionally used when you have a piece of code which you want to repeat n number of time. The for loop is often distinguished by an explicit loop counter or loop variable. Loops in Python Python Programming – Python Loops Break and Continue Statements For Loop Examples Let’s see how the for loop is working with some examples. for counter in range(1, 6): print counter #can also be written like this: numbers = range(1,6) for count in numbers: print (count) Output >>output 1 2 3 4 5 Loop through words Here we use the for loop to loop through the word computer word = “computer” for letter in word: print letter Output c o m p u t e r While Loop The while loop tells the computer to do something as long as the condition is met. Its construct consists of a block of code and a condition. The condition is evaluated, and if the condition is true, the code within the block is executed. This…

String Concatenation and Formatting in Python

String Concatenation and Formatting in Python

One common task you’ll need to accomplish with any language involves merging or combining strings. This process is referred to as concatenation. The best way to describe it is when you take two separate strings – stored by the interpreter – and merge them so that they become one. For instance, one string would be “hello” and the other would be “world.” When you use concatenation to combine them it becomes one string, or “hello world”. This post will describe how to concatenate strings in Python. There are different ways to do that, and we will discuss the most common methods. After, we will explore formatting, and how it works. The Basics: Concatenating Strings Python – Quick Guide Python Programming – Special String Operators Concatenation In Python, there are a few ways to concatenate – or combine – strings. The new string that is created is referred to as a string object. Obviously, this is because everything in Python is an object – which is why Python is an objected-oriented language. In order to merge two strings into a single object, you may use the “+” operator. When writing code, that would look like this: str1 = “Hello” str2 = “World”…

Why we need Comments in Python

Why we need Comments in Python

Comments are the statements that are included in the source code but don’t contribute to program logic. A python comment is not executed by the interpreter and it can only be accessed when we have access to the source code. Now the question is that despite the fact that they do not contribute to the program logic, why are the comments included in the source code. In this article, we will try to see why we need comments in python. Let’s dive into it then. How to write comments in Python Comment out a block of code in Python Python Docstrings We use comments to specify the metadata of the source code. When we write any computer program for commercial purposes, We generally include the name of the programmer, the date and time at which the source code was created. Generally, when a program is developed under contract, the conditions for use of the source code and the license and copyright of the source code, and a general description of the program is also included in the source file. To include this information in the source code, we use python comments. Metadata is included in the header comment at the…

What is a good commentcode ratio

What is a good comment/code ratio?

A comment is a piece of code that isn’t executed by the compiler or interpreter when the program is executed. Comments can only be read when we have access to the source code. To answer the question about a  good comment/code ratio, we need to first understand the needs that require us to include comments in our source code. Why are comments needed? It is very well known that comments don’t improve the quality of the program. They improve the readability and understanding of the source code. Following are some of the specific reasons for using comments in our source codes. 1. We use comments to specify the license or copyright of the source code. When we write any source code for a commercial purpose, it is customary to specify the name of the author, date and time at which the source code was created. We also specify if the source code is copyrighted or licensed and whether the source code can be used and modified without the permission of authors of the code. Comment out a block of code in Python How to write comments in Python How to create Multiline Comments in Python 2. We use comments to…

Single Line and Multi Line Comments in Python

Single Line and Multi Line Comments in Python

A comment is a piece of code that isn’t executed by the compiler or interpreter when the program is executed. Comments can only be read when we have access to the source code. Comments are used to explain the source code and to make the code more readable and understandable. In this article, we will see how to write single line and multi line comments using different methods in python. What is a single line comment in python? Single line comments are those comments which are written without giving a line break or newline in python. A python comment is written by initializing the text of comment with a # and terminates when the end of line is encountered. The following example shows a single line comment in a program where a function is defined to add a number and its square to a python dictionary as key value pair. #This is a single line comment in python def add_square_to_dict(x,mydict): a=x*x mydict[str(x)]=a return mydict We can also add a single line comment after another statement. #This is a single line comment in python print(“Pythonforbeginners.com”) #This is also a python comment How to write comments in Python Comment out a block of code in Python How to…