RE: Transferring Python2 to Python3 on This Specific Line - python-3.x

I am attempting to change this line to become acceptable by python3 from a python2 set of source:
Here is the error:
TypeError: unicode strings are not supported, please encode to bytes:
'$PMTK251,9600*17\r\n'
Can anyone tell my why this is this way or how I can change it to suit Python3 methods?
It is a GPS set of source in Python2 that still works but I see that all ideas relating to Python2 will be gone from availability and/or is already pretty much done and gone.
So, my ideas were to update that line and others.
In python3, I receive errors relating to bytes and I have currently read about the idea of (arg, newline='') in source when attempting to make .csv files in Python3.
I am still at a loss w/ how to incorporate Python3 in this specific line.
I can offer more about the line or the rest of the source if necessary. I received this source from toptechboy.com. I do not think that fellow ever updated the source to work w/ Python3.
class GPS:
def __init__(self):
#This sets up variables for useful commands.
#This set is used to set the rate the GPS reports
UPDATE_10_sec = "$PMTK220,10000*2F\r\n" #Update Every 10 Seconds
UPDATE_5_sec = "$PMTK220,5000*1B\r\n" #Update Every 5 Seconds
UPDATE_1_sec = "$PMTK220,1000*1F\r\n" #Update Every One Second
UPDATE_200_msec = "$PMTK220,200*2C\r\n" #Update Every 200 Milliseconds
#This set is used to set the rate the GPS takes measurements
MEAS_10_sec = "$PMTK300,10000,0,0,0,0*2C\r\n" #Measure every 10 seconds
MEAS_5_sec = "$PMTK300,5000,0,0,0,0*18\r\n" #Measure every 5 seconds
MEAS_1_sec = "$PMTK300,1000,0,0,0,0*1C\r\n" #Measure once a second
MEAS_200_msec= "$PMTK300,200,0,0,0,0*2F\r\n" #Meaure 5 times a second
#Set the Baud Rate of GPS
BAUD_57600 = "$PMTK251,57600*2C\r\n" #Set Baud Rate at 57600
BAUD_9600 ="$PMTK251,9600*17\r\n" #Set 9600 Baud Rate
#Commands for which NMEA Sentences are sent
ser.write(BAUD_57600)
sleep(1)
ser.baudrate = 57600
GPRMC_ONLY = "$PMTK314,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*29\r\n" #Send only the GPRMC Sentence
GPRMC_GPGGA = "$PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28\r\n"#Send GPRMC AND GPGGA Sentences
SEND_ALL = "$PMTK314,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0*28\r\n" #Send All Sentences
SEND_NOTHING = "$PMTK314,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28\r\n" #Send Nothing
...
That is the GPS Class Mr. McWhorter wrote for a GPS Module in python2. I am trying to configure this python2 source into a workable python3 class.
I am receiving errors like "needs to be bytes" and/or "cannot use bytes here".
Anyway, if you are handy w/ Python3 and know where I am making mistakes on this source to transfer it over to Python3, please let me know. I have tried changing the source many times to accept bytes and to be read as a utf-string.
Here: Best way to convert string to bytes in Python 3? <<< This seems like the most popular topic on this subject but it does not answer my question so far (I think).

This line simply works when adding a b for bytes in front of the string...like so.
(b'$PMTK251,9600*17\r\n')
That should rid you of that error of TypeError: unicode strings are not supported, please encode to bytes:

Related

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa9 in position 10: invalid start byte

I am trying to read MIDI music files and processing them a bit using the music21 library. I am using the self defined read_midi function, and getting this error "UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa9 in position 10: invalid start byte"
import os
#Array Processing
import numpy as np
#specify the path
path='audio/'
#read all the filenames
files=[i for i in os.listdir(path) if i.endswith(".mid")]
#reading each midi file
notes_array = np.array([read_midi(path+i) for i in files])
here is the read_midi function:
def read_midi(file):
print("Loading Music File:",file)
notes=[]
notes_to_parse = None
#parsing a midi file
midi = converter.parse(file)
#grouping based on different instruments
s2 = instrument.partitionByInstrument(midi)
#Looping over all the instruments
for part in s2.parts:
#select elements of only piano
if 'Piano' in str(part):
notes_to_parse = part.recurse()
#finding whether a particular element is note or a chord
for element in notes_to_parse:
#note
if isinstance(element, note.Note):
notes.append(str(element.pitch))
#chord
elif isinstance(element, chord.Chord):
notes.append('.'.join(str(n) for n in element.normalOrder))
return np.array(notes)
kindly suggest how can I get rid of this error.
An answer I got from the music21 Google Groups and fixed my problem :
HI, and thanks for the report. This is a regression caused by a new feature in 6.1.0 that creates Instrument objects from the text of MIDI track names. It's fixed in the next unreleased version (likely to be 6.2.0), which is available now on GitHub. If that's too cumbersome to install, you can also just edit your own copy of music21 to apply the fix found here: https://github.com/cuthbertLab/music21/pull/607/files
For the curious, the original feature wrongly assumed all MIDI track names would be encoded using utf-8. The files we found to fail each had a copyright symbol in the track name, and they were each created by "www.piano-midi.de". Would you mind sharing what MIDI writer created your file?
Also, I would very much appreciate you sharing this answer on Stack Overflow, since I'm not active there.
Cheers, and happy music21-ing,

How to extract the payload of a packet using Pyshark

I am trying to read the payload of all packets in a .pcap file using Pyshark. I am able to open and read the file, access the packets and their other information but I am not able to find the correct attribute/method to use to access the payload of a packet. Any suggestions ? Is there any other way to read packet payloads in .pcap files using python for windows 10 ?
(I tried using Scapy instead of Pyshark, but apparently there is some issue with running Scapy on Windows, it does not work on my system as well)
I found these lines in different code snippets of pyshark projects on the Internet and on StackOverflow. I tried them but none of them work :
import pyshark
cap = pyshark.FileCapture('file.pcap')
pkt = cap[1]
#for other information
print(pkt.tcp.flags_ack) #this works
print(pkt.tcp.flags_syn) #this works
print(pkt.tcp.flags_fin) #this works
#for payload
print(pkt.tcp.data) #does not work, AttributeError
print(pkt.tcp.payload) #does not work, AttributeError
print(pkt.data.data) #does not work, AttributeError
This code will print the value associated with the field name tcp.payload.
capture = pyshark.FileCapture(pcap_file, display_filter='tcp')
for packet in capture:
field_names = packet.tcp._all_fields
field_values = packet.tcp._all_fields.values()
for field_name in field_names:
for field_value in field_values:
if field_name == 'tcp.payload':
print(f'{field_name} -- {field_value}')
# outputs
tcp.payload -- \xc2\xb7\xc2\xb7\xc2\xb7\xc2\xb7\xc2\xb7\xc2\xb7\xc2\xb7AP\xc2\xb7\xc2\xb7\xc2\xb7
tcp.payload -- 0x00001e2c
tcp.payload -- 113977858
...
In order to use that API you have to pass appropriate parameter into constructor of 'FileCapture' class:
import pyshark
cap = pyshark.FileCapture('file.pcap', include_raw=True, use_json=True)
pkt = cap[1]
print(pkt.data.data) # Will work
'include_raw' is the key here, but 'use_json' is needed when when 'include_raw' is used.
dir cap[].
This one will give you all accessible attributes related to your capture., look there if there is the payload option.

Scaling a seaborn heatmap .png output file

I have a program that, in a nutshell, connects to a Cisco wireless controller and gathers data about the number of clients per access point. It runs at 'x' intervals at 'y' time between intervals.
The program works fine.
NOTE Both output files presented below show nine passes at 15 seconds between each pass. All you really care about is that I have 9 columns (one per pass) and the rows are the AP's and their connected clients.
My issue is this: when I run it against a small client (93 access points) the output looks exactly the way I want it to:
But when I run it against another client (1840 access points) the output looks like this:
Here is the relevant portion of my program:
df = pd.DataFrame(e, index=index, columns=cols)
df = df.transpose()
my_dpi = 96
sns.set(font_scale=2)
# plt.figure(figsize=(13, 91))
plt.figure(figsize=(2016 / my_dpi, 9120 / my_dpi), dpi=my_dpi)
sns.heatmap(df, cmap='RdYlGn_r', linewidths=0.5, annot=True, annot_kws={"size": 20})
plt.savefig('d:\\python\\projects\\clients_per_ap\\ac.png')
plt.show()
I tried changing 9120 to 912000, but I get an error stating that the value has to be less than 2^16. I tried 65535, but the program fails with a memory error. I tried 54720 and that works -- 54720 produced the output you see here as the second image, but it is unusable.
How can I scale my output file for the client with 1840 AP's to look like the output file for the client wit 93 AP's? Basically I would like the same (or very close) font and width, just 1840 rows long versus 93.
Have you tried outputting the file in vector format? .PDF works well usually. Sure the text will look big when you zoom in, but that's probably your best bet for working with output that is too big to put into memory. Then you can just use any classic PDF to PNG conversion if you just need that for some reason.

Dynamic Time Warp with Speech Signal Processing Toolkit (SPTK) output

I'm an IT student and got an assignment to do about Dynamic Time Warping(DTW) using the Speech Signal Processing Toolkit (SPTK) and comparing some words spoken by 2 speakers and finding the similarities.
I managed to get the SPTK working and everything, collected 8 people(4 female, 4 male) who recorded 8 words each for me(same words for every person) and saved them as files with a .wav extension.
My .wav files are: RIFF (little-endian) data, WAVE audio, mono 16000 Hz.
I transfered every .wav file into .short data files.
I transfered every .short file to a .mcep file with this line of code:
x2x +sf < source_maleA.short | frame -l 400 -p 80 | window -l 400 -L 512 | mcep -l 512 -m 20 -a 0.42 > source_maleA.mcep
After that, I went to compare the .mcep files with this line of code:
dtw -m 24 target_maleB.mcep < source_maleA.mcep > source_maleA_target_maleB.dtw
The output of that command line should be a numeric value(probably a float/double/int value) or a few values. The problem is that I'm not sure how to open that .dtw files and in the documentation I get there isn't any good info about that. When I try to open it in any editor or cat it in the terminal, I get some strange letters as an output [picture 1].
In the documentation however it says that with the parameter -s [Score] I can get the score of the DTW process. So I tried it with this command line:
dtw -m 24 -s Scorefile target_maleB.mcep < source_maleA.mcep > source_maleA_target_maleB.dtw
I get a value, but in strange format.
I searched online and in many documentations about the .dtw file and couldn't find anything. I tried to convert the result into another format, but not any luck with that.
Tried to contact my mentor about it, but no answers so far and it's been a while already.
Anyone could give me any suggestion on what to do or anything else?
The documentation can be found on this site : http://sp-tk.sourceforge.net/ (sorry for not link, but still not enough reputation - will remove if I have to), but I don't think it's needed that much, since I think I pretty much understood the DTW process and think I've done it ok, it's just that the output is causing me problems.
Thanks in advance,
Marco.
picture 1
The score file is in float so you have to convert it to asci with the x2x command from SPTK:
x2x +fa scorefile.bin > scorefile.txt

audio file isn't being parsed with Google Speech

This question is a followup to a previous question.
The snippet of code below almost works...it runs without error yet gives back a None value for results_list. This means it is accessing the file (I think) but just can't extract anything from it.
I have a file, sample.wav, living publicly here: https://storage.googleapis.com/speech_proj_files/sample.wav
I am trying to access it by specifying source_uri='gs://speech_proj_files/sample.wav'.
I don't understand why this isn't working. I don't think it's a permissions problem. My session is instantiated fine. The code chugs for a second, yet always comes up with no result. How can I debug this?? Any advice is much appreciated.
from google.cloud import speech
speech_client = speech.Client()
audio_sample = speech_client.sample(
content=None,
source_uri='gs://speech_proj_files/sample.wav',
encoding='LINEAR16',
sample_rate_hertz= 44100)
results_list = audio_sample.async_recognize(language_code='en-US')
Ah, that's my fault from the last question. That's the async_recognize command, not the sync_recognize command.
That library has three recognize commands. sync_recognize reads the whole file and returns the results. That's probably the one you want. Remove the letter "a" and try again.
Here's an example Python program that does this: https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/speech/cloud-client/transcribe.py
FYI, here's a summary of the other types:
async_recognize starts a long-running, server-side operation to translate the whole file. You can make further calls to the server to see whether it's finished with the operation.poll() method and, when complete, can get the results via operation.results.
The third type is streaming_recognize, which sends you results continually as they are processed. This can be useful for long files where you want some results immediately, or if you're continuously uploading live audio.
I finally got something to work:
import time
from google.cloud import speech
speech_client = speech.Client()
sample = speech_client.sample(
content = None
, 'gs://speech_proj_files/sample.wav'
, encoding='LINEAR16'
, sample_rate= 44100
, 'languageCode': 'en-US'
)
retry_count = 100
operation = sample.async_recognize(language_code='en-US')
while retry_count > 0 and not operation.complete:
retry_count -= 1
time.sleep(10)
operation.poll() # API call
print(operation.complete)
print(operation.results[0].transcript)
print(operation.results[0].confidence)
for op in operation.results:
print op.transcript
Then something like
for op in operation.results:
print op.transcript

Resources