Email: bknpk@hotmail.com Phone: +972-54-7649119
V

 

ASIC/FPGA Design and Verification Out Source Services

My first python script.



  1. Recently I started to study python . In this site I present many scripts. For simple tasks, I use bash and for more complex ones perl. Since my daughter starts to study python , so do I.

  2. This script extracts the current time from a web site, translates it to IL local time and prints the results.

  3. First I needed to check if the library request is installed on my debian machine. So I fired the interactive python and gave the import command:

    >>> import requests
    Traceback (most recent call last):
    File "", line 1, in
    ImportError: No module named requests

    To install the library, I used:

    apt-cache search python-requests
    python-requests - elegant and simple HTTP library for Python, built for human beings

    apt-get install python-requests


  4. The script is simple and is built of the following:
    1. library import,
    2. read web page into memory,
    3. extract time using regular expression,
    4. print results

    The code is listed below:

    #!/usr/bin/python

    #process web pages
    import requests
    #handle regular expression
    import re

    #Get the time from the web
    #Look for the following string:... 07:14:02 UTC ...
    response = requests.get('http://tycho.usno.navy.mil/cgi-bin/timer.pl')
    m = re.search('[0-9][0-9]*:[0-9][0-9]*:[0-9][0-9]* UTC', response.content[:500])
    print "found", m.group(0)

    str = m.group(0).replace(" UTC", "")

    Lname=[]
    Lname.append("hour")
    Lname.append("min ")
    Lname.append("sec ")


    array=str.split(':')
    ix=0
    for ti in array:
      if(ix == 0):
        #adjust to IL time
        ti_i = int(ti) + 3
        print Lname[ix], ti_i
      else:
        print Lname[ix], ti
      ix += 1





Search This Site


Feedback This Site




new pages on this site