A decorator provides a simplified way to pass one callable object to another (i.e a class or function). The main benefit of decorators is that it allows you to clearly state where and what objects are being passed to each other. Typically this can be achieved by the following. Without Decorators As you can see, … Read more
Within this article we will show you how to import a python module that is located within a different folder. This example will be based upon your folder structure including a folder named ‘module’ and the path ‘/opt/django/myproject/’ already being within your python system path. Example Folder Structure Below shows you a quick example of … Read more
Below is a python function that I created. The purpose of this function is to print correctly aligned columns using variable length items. The result is that you can pass the function a list (or list of lists), the same list is returned with each item padded to the maximum item length within the relating … Read more
To print the path of python module the command print [module].__file__. An example is shown below: [root@webserver1 ~]# pythonPython 2.4.3 (#1, Sep 21 2011, 19:55:41)[GCC 4.1.2 20080704 (Red Hat 4.1.2-51)] on linux2Type “help”, “copyright”, “credits” or “license” for more information.>>> import pexpect>>> print pexpect.__file__/usr/lib/python2.4/site-packages/pexpect.pyc
Below is a small script to convert temperatures between celcuis and fahrenheit (and vice versa). This article / script is meant as reference point rather than a full tutorial. #!/usr/bin/python import sys def convert(t,fc): if t == “2f”: print (fc * 9) / 5 + 32,”Degress Fahrenheit” elif t == “2c”: print (fc – 32) … Read more
Below is a short script designed to output the addressable IP addresses within a given subnet. Note : This script requires the netaddr python library. #!/usr/bin/pythonimport sysfrom netaddr import IPNetworkIPADDRESS = raw_input(‘Enter Subnet:’)try: for ip in IPNetwork(IPADDRESS).iter_hosts(): print ‘%s’ % ipexcept: print “Usage: “,sys.argv[0],”[SUBNET ID]/[NETMASK]” print “Output addressable ip addresses within a given subnet.” … Read more
Below is a small example to show some of the various way in which print can be called within Python. quote = “A person who never made a mistake never tried anything new.”print(“ORIGINAL”)print(quote)print(“\nUPPERCASE”)print(quote.upper())print(“\nLOWERCASE”)print(quote.lower())print(“\nTITLE”)print(quote.title())print(“\nREPLACE”)print(quote.replace(“person”, “banana”)) Output [root@linux]# python print_example.py ORIGINALA person who never made a mistake never tried anything new.UPPERCASEA PERSON WHO NEVER MADE A MISTAKE … Read more
This python script will connect to a designated FTP server. Obtain a list of all files, then sort all files with a .jpa extension and then download the latest one. In order to do this regular expressions are used to build a list of files only containing .jpa file extensions. This list is then sorted … Read more