sexta-feira, 20 de dezembro de 2019

Python3 - String data as csv (file or list) objects

I spent the last three days reading Python guides, manuals, blogs, tutorials, e-books, etc. just to be able to start working with data in text format I gathered from sensors and text format tables.

I got stuck because I was incorrectly trying to use csv methods to manipulate a file that I read into a string, and wrongly though I could just format it as a standard csv file layout.  This just does not work!

The official doc for csv.reader() at is very helpful, which stats this:

    "file objects and list objects are both suitable"

The solution is to convert your formatted string to a "file or list object", (what ?!?)

The side effect of not doing this is that all csv methods don't fetch the items correctly and ignores your delimiters (ie. like the comma)

So you have two ways of doing this:

First:  you can import StringIO to perform this conversion:

   from IO import StringIO
   import csv

After that you need to code the conversion, and for this example lets assume your string is named: target

   new_csv = StringIO(target)

But wait!!! Hold your horses!!! Keep on reading.

Second: Or choose a easier way, that is: make a list from your string:

   new_csv = target.splitlines()

You can check the type of objects with:

   print('type of target  = '+str(type(target)))
   print('type of new_csv = '+str(type(new_csv)))

Now you are ready to use all the csv methods to analise/manipulate your tables/data


quinta-feira, 5 de dezembro de 2019

Check your link speed to all servers in location

So I had same problems with my local xDSL provider.

To be able to test the speed of connection we can use the speedtest site or a bash script from a GitHub developer, but I created the following bash script which will use the speedtest script to test the speed on all the servers in my city and save it to a log file.

# speedtest all my city servers
# Author: 05/DEZ/2019
# ref: 

# curl -s | python -
# dependencies: 

#  1) wget

#  2) ubuntu 18.04

#  2.1) GNU bash, version 4.4.20(1)-release (x86_64-pc-linux-gnu)

#  3)   # for external IP fetch


# if target city name has spaces change it for grep \s like 
# this: "Salt\sLake\sCity"
gtarget=$( echo "$targetcity" | sed "s/ /\\\s/g )
servers=$( python --list | grep $gtarget )
# debug:
# echo -e "I found these servers:\n$servers"
servnum=$( echo -e "$servers" | cut -d ')' -f 1 )

# debug:
# echo -e "These are the server numbers:\n$servnum"
tot=$( echo -e "$servnum" | wc -l )
echo -e "\nRunning, please wait $tot servers to go:\n"

echo -e "Speedtest "$( wget -qO - )" "$( date +%F )"\n" > speedtest_$( date +%F ).log


for serv in $servnum
     echo -e "$i of $tot - ($serv): "
     python --server $serv 2>&1 | grep ':' |\
     sed "s/$targetcity/$serv/g" |\
     sed "s/.*ERROR.*/Server is not responding/g" |\
     tee -a speedtest_$( date +%F ).log
     echo -e "" | tee -a speedtest_$( date +%F ).log
     sleep 1
echo ""