lunedì 30 novembre 2020

Counting greps

 #!/bin/bash
 
if [ $# -eq 1 ]
  then
   echo "file log: $1 - num of occurrences: "
   grep -w "string to grep" $1 | wc -l
else
 echo "no args specified: insert a file name"
fi

lunedì 23 novembre 2020

Monitoring directory to handle excel files in python

 My last post was about how to remove columns from a excel in python. I needed to monitor a directory so I did it directly in python, here is the result: handle columns in a excel file in every file added to a directory.



import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import shutil
import pandas as pd
import sys
 
class Watcher:
        DIRECTORY_TO_WATCH =  "Test"
 
        def __init__(self):
            self.observer = Observer()
 
        def run(self):
            event_handler = Handler()
            self.observer.schedule(event_handler, self.DIRECTORY_TO_WATCH, recursive=True)
            self.observer.start()
            try:
                while True:
                    time.sleep(5)
            except:
                    self.observer.stop()
                    print("Error")
 
            self.observer.join()
 
class Handler(FileSystemEventHandler):
 
    @staticmethod
    def on_any_event(event):
        if event.is_directory:
            return None
 
        elif event.event_type == 'created':
            print( "Received %s." % event.src_path)
            fileToHandle = event.src_path
            print(fileToHandle)
            df = pd.read_excel(fileToHandle)
            print(df)
            df = df.drop(
                ['IDCONTRATTO', 'IDHARDWARE', 'DATAEVENTO'], axis=1)
            print(df)
 
            filename = fileToHandle.split("/")
            outputFile = filename[1]
            df.to_excel(outputFile)
            shutil.move(outputFile, "OutputTest")  # todo sostituire con il nome della dir.
 
 
if __name__ == '__main__':
    w = Watcher()
    w.run()

venerdì 20 novembre 2020

Remove multiple columns from exel with Pandas in Python


import pandas as pd
import xlwt 

df = pd.read_excel('input.xls')
print(df)
 
df = df.drop(['IDCONTRATTO', 'IDHARDWARE', 'DATAEVENTO'], axis=1)
 
print(df)
 
df.to_excel('output.xls')
 

 

 

-----------------------------

 # Then I edited the script to get param from a Input folder and move it to a "OutDir" folder. (just because I needed this, it sucks but it is the way I had to do it.)


import shutil 
import pandas as pd
import sys
import xlwt
 
inputFile = str(sys.argv[1])
print(inputFile)
 
df = pd.read_excel(inputFile)
 
print(df)
 
df = df.drop(['IDCONTRATTO', 'IDHARDWARE', 'DATAEVENTO'], axis=1)
 
print(df)
 
filename = inputFile.split("/")
outputFile = filename[1]
df.to_excel(outputFile)
 
shutil.move(outputFile,"OutDir")

Run minikube with podman on Fedora

After install minikube as described in the documentation , set rootless property to true to use Podman without sudo: minikube config set roo...