· 6 years ago · Feb 17, 2020, 09:08 PM
1# I decided to use Python because this is a cross-platform language: a Python program written on a Macintosh computer will run on a Linux system and
2# vice versa. Python programs can run on a Windows computer, as long as the Windows machine has the Python interpreter installed (most other operating systems come with Python pre-installed)
3
4# JSON module is mainly used to convert the python dictionary above
5# into a JSON string that can be written into a file. While the JSON module will convert strings to Python datatypes,
6# normally the JSON functions are used to read and write directly from JSON files
7import json
8
9#Requests momdule will allow us to send HTTP/1.1 requests using Python. With it, we can add content like headers, form data,
10# multipart files, and parameters via simple Python libraries. It also allows us to access the response data of Python in the same way.
11#We are using it to send the data to Insights
12import requests
13
14#An OrderedDict is a dictionary subclass that remembers the order that keys were first inserted.
15#OrderedDict preserves the order in which the keys are inserted. We are using this when we counting to see how many time
16#does a word appers on the data.txt file.
17from collections import OrderedDict
18
19#A regular expression (or RE) specifies a set of strings that matches it; the functions in this module let you check if a
20# particular string matches a given regular expression (or if a given regular expression matches a particular string,
21# which comes down to the same thing). We are using this module because we want to remove any special characters and number from the list like (. , " :)
22import re
23
24#The subprocess module present is used to run new applications or programs through Python code by creating new processes.
25# It also helps to obtain the input/output/error pipes as well as the exit codes of various commands. We are using this to be able obtain clock time info.
26import subprocess
27
28#The platform module in Python is used to access the underlying platform's data, such as, hardware, operating system, and interpreter version information
29#On our use case we want to know what platform system is accessing out script.
30import platform
31
32#The OS module in Python provides a way of using operating system dependent functionality. The functions that the OS module provides allows you to interface
33#The name of the operating system dependent module imported. The following names have currently been registered: 'posix', 'nt', 'java'.
34import os
35
36#Global variables
37#In Python, a variable declared outside of the function or in global scope is known as global variable.
38# This means, global variable can be accessed inside or outside of the function.
39INSERT_KEY = 'NRII-h8JZoE13GDuJe0QoocsiArnZ8Uu_dM0d' #Insights Key to let us insert events into New Relic account with just a simple HTTPS request.
40filename = "data.txt" #The main source of data where the script will read from the unique file
41cmd = "date"
42numLines = 0
43numWords = 0
44numChars = 0
45wordFreq = []
46wordListGlobal = []
47
48#we are going to use for loop to read every single line of the data.txt file
49with open(filename, 'r') as file:
50 for line in file:
51 wordsList = line.split() # creating a list to hold the words from the data.txt file
52 # we need to remove special characters and number from the word list like (. , " :)
53 cleanString = re.sub(r"[^a-zA-Z]", ' ', line) # we want only words from 'a to z' (upper and lower case)
54 wordListGlobal += cleanString.split() # adding the new sanitized words to a new unique list
55 numLines += 1 # adding +1 for each like on the file
56 numWords += len(wordsList) # just get lenght of wordList list
57 numChars += len(line) # getting lenght of line on each interaction will be our number of characters
58
59print ('\n------------------------------------------------------------')
60cstr = " WELCOME TO MY WORD COUNT PYTHON APPLICATION "
61print (cstr.center(50, '#')) # this function center aligns the string according to the width specified and fills remaining space of line with blank space if ‘ fillchr ‘ argument is not passed.
62print ('------------------------------------------------------------')
63
64# for loop to be able to calculate how many times the same word is used
65for w in wordListGlobal:
66 wordFreq.append(wordListGlobal.count(w))
67
68print ('\n*************************************************************')
69print ('How many "Lines, Words and Characters" are in data.txt file?')
70print ('*************************************************************')
71
72# printing the final result of number of Line, Words and Characters
73print("Lines: %i\nWords: %i\nCharacters: %i" % (numLines, numWords, numChars)) # %i is a placeholder for integers
74
75print ('\n*************************************************************')
76print ('How many times does a WORD appears in the data.txt file? separatig by upper and lower case.')
77print ('*************************************************************')
78
79# ????????????????
80for key, value in OrderedDict().fromkeys(zip(wordListGlobal, wordFreq)):
81 print("%s: %s " % (key, value)) # %s is a placeholder for string
82
83# we are collecting the current date and time from the OS
84returned_output = subprocess.check_output(cmd) # returns output as byte string
85
86print ('\n*************************************************************')
87print ('OPERATING SYSTEM INFORMATION')
88print ('*************************************************************')
89print("OS Name: %s\nSystem: %s\nVersion: %s" % (os.name, platform.system(), platform.release())) # printing the OS name, System name and Version
90
91# sending custom attribute event to Insights so we can see how many times the scrip was used and create Dashboard for better visualization
92headers = { "Content-Type": "application/json", "X-Insert-Key": INSERT_KEY } # passing the Header information specially the API key on the POST request to be able to connect to New Relic
93url = 'https://insights-collector.newrelic.com/v1/accounts/2227431/events' # Reaching Insights endpoint
94
95content = json.dumps({ #insight expected a JSON format
96 'eventType': 'wordcount', 'message': 'Successful', 'System': platform.system(), 'Version': platform.release(), 'Time': returned_output.decode("utf-8")
97})
98r = requests.post(url, data = content, headers = headers)# serforming the POST request with the above content
99
100print ('\n*************************************************************')
101print ('CONFIRMING POST REQUEST RESULT')
102print ('*************************************************************')
103print ('HTTP POST request response:',r)
104
105# using decode() function to convert byte string to string
106print('\nScript successfully runned on', returned_output.decode("utf-8"))