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 https://docs.python.org/2/library/csv.html 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

Cheers!

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.

#!/bin/bash
#
# speedtest all my city servers
#
# Author: braselectron.com 05/DEZ/2019
#
#
# ref: 

# curl -s https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py | python -
#
# dependencies: 

#
#  1) wget https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py
#

#  2) ubuntu 18.04
#

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

#  3) http://ipinfo.io/ip   # for external IP fetch
#

#
targetcity="Miami"

# 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 speedtest.py --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 - http://ipinfo.io/ip )" "$( date +%F )"\n" > speedtest_$( date +%F ).log
#

i=1
#

for serv in $servnum
   do
     echo -e "$i of $tot - ($serv): "
     python speedtest.py --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
     ((i++))
     #
   done
echo ""
Cheers!