Print statement does not concatenate strings and variables in Python 3 - python-3.x

Why the following code returns an exception in the last line?
print(m)
print(b)
print(r)
print(p)
print(se)
print("m: " + m + "\n" + "b: " + b + "\n" + "r: " + r + "\n" + "p " + p + "\n" + "se " + se)
1516.13788561
-5731.63903831
0.858729519032
4.15127287882e-05
250.925294078
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-195-7e2d88a4b5a8> in <module>()
9 print(se)
10
---> 11 print("m: " + m + "\n" + "b: " + b + "\n" + "r: " + r + "\n" + "p " + p + "\n" + "se " + se)
TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('<U32') dtype('<U32') dtype('<U32')

Try casting the variables as strings for the print function.
print("m: " + str(m) + "\n" + "b: " + str(b) + "\n" + "r: " + str(r) + "\n" + "p " + str(p) + "\n" + "se " + str(se))

Related

ESP32 - unable to connect to Google Coud VM running Node-Red httpin node with post/get request

I have a Google Cloud Debian VM with Node Red installed.
I created a flow that is refered as json on the httpin node. When I http://xx.xx.xx.xx:1880/json from chrome, Node Red at the VM responds with a test json string I typed there.
My intention is to post a json from ESP32 and get a response from Node Red.
But I am just unable to even connect with the above URL from ESP32.
At the top of my sketch I have:
String URLWebServer = "http://222.222.111.128";
int PortWebServer = 1880;
String uploadScript = "/json";
Later in the sketch I have the web connection as below:
void postWebServer(){
Serial.print("Connect to " + URLWebServer);
if (webClient.connect(URLWebServer.c_str(), PortWebServer)){
Serial.println(" -> OK");
...
...
...
}else{
Serial.println(" -> Fail");
}
And no matter what I do I always get "-> Fail").
Assistance welcome.
The sketch is divided in several tabs with specific functions.
This is the same sketch I use to connect with another online service and it used to work. Now I am developing my own service using nodered.
When this function is called WiFi is already connected.
Complete code of the function that does the web post:
void postWebServer(){
Serial.print("Connect to " + URLWebServer);
if (webClient.connect(URLWebServer.c_str(), PortWebServer)){
Serial.println(" -> OK");
String startBoundary = "--";
String postTail = startBoundary + boundary + "--" + newLine;
String strBody; // = newLine;
strBody += startBoundary + boundary + newLine;
strBody += "Content-Disposition: form-data; name=\"filename\"" + newLine + newLine;
strBody += ftpFileName + newLine;
strBody += startBoundary + boundary + newLine;
strBody += "Content-Disposition: form-data; name=\"devid\"" + newLine + newLine;
strBody += devid + newLine;
strBody += startBoundary + boundary + newLine;
strBody += "Content-Disposition: form-data; name=\"latitude\"" + newLine + newLine;
strBody += "123" + newLine;
strBody += startBoundary + boundary + newLine;
strBody += "Content-Disposition: form-data; name=\"longitude\"" + newLine + newLine;
strBody += "345" + newLine;
strBody += startBoundary + boundary + newLine;
strBody += "Content-Disposition: form-data; name=\"camera_id\"" + newLine + newLine;
strBody += camera_id + newLine;
strBody += startBoundary + boundary + newLine;
strBody += "Content-Disposition: form-data; name=\"maxSpeed\"" + newLine + newLine;
strBody += "40" + newLine;
strBody += startBoundary + boundary + newLine;
strBody += "Content-Disposition: form-data; name=\"radarSpeed\"" + newLine + newLine;
strBody += "65" + newLine;
strBody += startBoundary + boundary + newLine;
strBody += "Content-Disposition: form-data; name=\"plate\"" + newLine + newLine;
strBody += plate + newLine;
strBody += startBoundary + boundary + newLine;
strBody += "Content-Disposition: form-data; name=\"type\"" + newLine + newLine;
strBody += type + newLine;
strBody += startBoundary + boundary + newLine;
strBody += "Content-Disposition: form-data; name=\"make\"" + newLine + newLine;
strBody += make + newLine;
strBody += startBoundary + boundary + newLine;
strBody += "Content-Disposition: form-data; name=\"model\"" + newLine + newLine;
strBody += model + newLine;
strBody += startBoundary + boundary + newLine;
strBody += "Content-Disposition: form-data; name=\"color\"" + newLine + newLine;
strBody += color + newLine;
strBody += postTail;
Serial.println("Connection to " + URLWebServer + " - OK");
String header = "POST " + uploadScript + " HTTP/1.1" + newLine;
header += "Host: " + URLWebServer + newLine;
header += "Content-Type: multipart/form-data; boundary=" + boundary + newLine;
header += "Content-Length: " + String(strBody.length()) + newLine;
header += newLine;
webClient.print(header);
pDBGln(header);
webClient.print(strBody);
pDBGln(strBody + newLine);
Serial.println("Data sent to " + URLWebServer + "...");
}else{
Serial.println(" -> Fail");
}
}
Best guess since you haven't really provided enough context to your ESP32 code.
But it looks like webClient is actually an ethernet/wifi client since you are building the HTTP request by hand in the function you have provided.
You need to take the http:// off the start of the URLWebServer it should be a hostname/ip address not a URL, since you are passing the port as a separate argument.
You will need to include the http:// in the HTTP Host header (along with the port number)
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Host

calculating average of 3 months profit when input 1 date

I have a table of profit for different vendors with range of date from OCT 2017 to AUG 2018. The intention is to input one single month and return with the average profit of the three previous months and three following months. (eg. input FEB 2018, return with average of DEC 2017, JAN 2019, FEB 2018, also return with average of FEB, MAR, APR 2018). Tried hlookup but seems did not work for this situation. Please advise.
OCT 2017 NOV 2017 DEC 2017 JAN 2018 FEB 2018 MAR 2018 APR 2018 MAY 2018 JUN 2018 JUL 2018 AUG 2018
Autozone " $4,653 " " $5,584 " " $8,277 " " $7,564 " " $7,321 " " $6,420 " " $7,568 " " $3,546 " " $9,954 " " $8,849 " " $2,325 "
Walmart " $9,838 " " $9,979 " " $2,655 " " $4,795 " " $9,811 " " $8,179 " " $7,896 " " $1,689 " " $7,244 " " $8,969 " " $1,189 "
Target " $8,359 " " $7,683 " " $4,607 " " $4,224 " " $6,221 " " $8,519 " " $5,058 " " $9,184 " " $9,699 " " $1,292 " " $1,080 "
Advanced Auto Parts " $3,674 " " $4,619 " " $3,817 " " $8,741 " " $9,118 " " $5,542 " " $1,578 " " $9,853 " " $8,153 " " $4,710 " " $4,519 "
pre:
=AVERAGE(INDEX($2:$5,0,MATCH(H8,1:1,0)):INDEX($2:$5,0,MATCH(H8,1:1,0)-2))
post:
=AVERAGE(INDEX($2:$5,0,MATCH(H8,1:1,0)):INDEX($2:$5,0,MATCH(H8,1:1,0)+2))

perl index skips search value

I am writing a program that is heavily dependent on the perl's index function to take strings. My objective so to break this string into a series of substrings that do not have any spaces.
The problem that I am having is that in the second half of the loop the index function starts skipping spaces.
I am using the test data:
1 12 a 2 5 P Q
I am expecting to get:
1, 12, a, 2, 5, P, Q,
Instead I get:
1, 12, a, 2 5, P Q,
My code follows:
use 5.010;
use strict;
use warnings;
my $ivalue = <stdin>;
chomp($ivalue);
$ivalue = $ivalue . " ";
my $current;
my $space = 0;
my $safespace = 0;
my $lastspace = 0;
my $closestspace = 0;
my $i = 0;
# Test data - 1 12 a 2 5 P Q
while ($space != -1){
$space = index($ivalue, " ", $space + $i++);
$closestspace = $space - $lastspace;
#print $lastspace . " " . $space . " = " . $closestspace . "; ";
$current = substr($ivalue, $lastspace, $closestspace);
#say "substring = " . $current;
$lastspace = $space + 1;
}
Thanks ahead of time! If anyone has any suggestion on how to improve the way I asked my question or on my code in general those are appreciated as well.
The problem is in this line:
$space = index($ivalue, " ", $space + $i++);
instead of using $i, make that $space + 1

Im trying to print the "J" in initial = dotmatrix.dotJ("J")

I have a project where I build a dotmatrix module and then import it into another program that will print out my initials. I have it done to the point where it will print out my initials, but it won't print the "J" in the
initial = dotmatrix.dotJ("J")
It will just print the "*". I have defined in my module:
def dotJ(char):
"""Creates a capital J in 7 x 7 dots"""
dotJ = " * \n"
dotJ += " * \n"
dotJ += " * \n"
dotJ += " * \n"
dotJ += " * * \n"
dotJ += " * * \n"
dotJ += " *** \n"
return dotJ
where I have the *, I want it to print out whatever is called for in the
initial = dotmatrix.dotJ("J")
A simple way would be to define your dotJ variable with a star and then replace the star with the character:
def dotJ(char):
"""Creates a capital J in 7 x 7 dots"""
dotJ = " * \n"
dotJ += " * \n"
dotJ += " * \n"
dotJ += " * \n"
dotJ += " * * \n"
dotJ += " * * \n"
dotJ += " *** \n"
dotJ = dotJ.replace("*",char)
return dotJ
You could also use a format string. But I suspect this would be less easy to read.

Use awk to check a specific combination in other files

I have 3 files
base.txt
12345 6 78
13579 2 46
24680 1 35
123451 266 78
135792 6572 46
246803 12587 35
1stcheck.txt
Some odded stuffs
AB 12345/6/78 Fx00
BC 13579/2/47 0xFF
CD 24680/1/35 5x88
AB 123451/266_10/78 Fx00 #10 is mod(266,256)
BC 135792/6572_172/46 0xFF #172 is mod(6572,256)
CD 246803/12587_43/35 5x88 #43 is mod(12587,256)
There may be some other odded stuffs
2ndcheck.txt
12345u_6_78.dat
13579u_2_46.dat
24680u_0_35.dat
123451u_10_78.dat #10 is mod(266,256)
135792u_172_46.dat #172 is mod(6572,256)
246803u_43_35.dat #43 is mod(12587,256)
The info in 1stcheck.txt and 2ndcheck.txt is just combination of base.txt in applied some template/format
I'd like to have
report.txt
12345 6 78 passed passed
| |
(12345/6/78) (12345u_6_78)
13579 2 46 failed passed
24680 1 35 passed failed
123451 266 78 passed passed
135792 6572 46 passed passed
246803 12587 35 passed passed
Please help to consider about performance since
base.txt,2ndcheck.txt ~ 8MB-12MB
1stcheck.txt ~ 70MB
Many thanks
You'll have to decide if this is memory efficient: it does have to store data from all files in arrays before printing the table.
Required GNU awk
gawk '
# base file: store keys (and line numbers for output ordering)
FILENAME == ARGV[1] {key[$0] = FNR; next}
# 1st check: if key appears in base, store result as pass
FILENAME == ARGV[2] {
k = $2
gsub(/\//, " ", k)
if (k in key) pass1[k] = 1
}
# 2nd check: if key appears in base, store result as pass
FILENAME == ARGV[3] {
if ( match($0, /([0-9]+)._([0-9]+)_([0-9]+)\.dat/, m) ) {
k = m[1] " " m[2] " " m[3]
if (k in key) pass2[k] = 1
}
next
}
# print the result table
END {
PROCINFO["sorted_in"] = "#val_num_asc" # traverse array by line number
for (k in key) {
printf "%s\t%s\t%s\n", k \
, (k in pass1 ? "passed" : "failed") \
, (k in pass2 ? "passed" : "failed")
}
}
' base.txt 1stcheck.txt 2ndcheck.txt
12345 6 78 passed passed
13579 2 46 failed passed
24680 1 35 passed failed
Based on #glenn jackman's suggestion, I could solve my problem
gawk '
# Store key for 1st check
FILENAME == ARGV[1] {
k = $2
gsub(/\//, " ", k)
key_first[k];next
}
# Store key for 2nd check
FILENAME == ARGV[2] {
if ( match($0, /([0-9]+)._([0-9]+)_([0-9]+)\.dat/, m) ) {
k = m[1] " " m[2] " " m[3]
key_second[k];
}
next
}
# base file: do check on both 1st and 2nd check
FILENAME == ARGV[3] {
if($2>256) {
first=$1 " " $2 "_" ($2%256) " " $3
}
else {
first=$1 " " $2 " " $3
}
second=$1 " " $2%256 " " $3
if (first in key_first) pass1[$0] = 1
if (second in key_second) pass2[$0] = 1
key[$0]= FNR; next
}
# print the result table
END {
PROCINFO["sorted_in"] = "#val_num_asc" # traverse array by line number
for (k in key) {
printf "%s\t%s\t%s\n", k \
, (k in pass1 ? "sic_passed" : "sic_failed") \
, (k in pass2 ? "gd_passed" : "gd_failed")
}
}
' 1stcheck.txt 2ndcheck.txt base.txt

Resources