Why string get from buffer does not equal to JavaScript String - javascript

I am working on a project in which I am using node js and python together. My node js code makes a child process of python code. And I am sending some data from node js to python and also receiving some data back from python to node.
I call my python file like this.
const py = spawn('python', ['file.py', 'some_data']);
and for sending data back from python to node I am using print in python like this.
print("mydata")
and for receiving in nodejs I am using this code.
py.stdout.on('data', async (data) => {
//This data is python string as we can see above
var data_from_python = data.toString();
});
now the problem I am facing is string which come from python does not equal to node js string I have tried this.
data_from_python === "mydata" // false
and also this
data_from_python == "mydata" // false
both returns false.
But when I console the type of both using typeof operator it says string for both.
I want to know what is reason behind and what is the solution if we want to compare them or is there a better way to get data back from python to node. I know this "mydata" string is coming from buffer, python is putting it into the buffer and node is reading it from the buffer, and I think they both are different because one is python string and other is nodejs string, maybe it is because of under the hood both deal or make strings differently.
But what is the exact story behind if anyone one know please share your knowledge.

This is because print in python adds a new line after the data, So instead of getting mydata you will receive mydata\r\n. So you should check for the same in your condition, see below:
const { spawn } = require('child_process')
const py = spawn('python', ['py.py', 'some_data']);
py.stdout.on('data', async (data) => {
console.log(data);
var data_from_python = data.toString();
console.log(data_from_python == "mydata\r\n"); // outputs true
});

Related

POSTMAN - EXPRESS - Modify JSON before writing to file

I have written a POSTMAN call to a server that responds with a list of items in JSON like below:-
{
"count": 6909,
"setIds": [
"1/7842/0889#001",
"2/4259/0166#001",
"ENT0730/0009",
"9D/11181#002",
"1/9676/0001#001",
"2/4718/0001#004",
"2/1783/0044#001",
"1/4501/0001#001",
"1/2028/0002#002",
"2/3120/0079#001",
"2/1672/0024#001",
"2/3398/0064#001"
}
I want to make calls to another server using the value of the setID each time and iterate through all of these so that I end up calling the server thousands of times to verify the response from that server. The problem I have is that the second server expects the set id to be in a form where the forward slashes are converted to underscores and the hashes to dots, so
"1/7842/0889#001"
becomes
"1_7842_0889.001"
I have code that converts one to the other in POSTMAN
var jsonData = pm.response.json()
for (var i=0; i<jsonData.setIds.length; i++)
{
var new_j = jsonData.setIds[i].replace (/#/g, ".");
var new_i = new_j.replace (/\//g, "_");
}
})
This works fine line by line it creates the right thing in the console of POSTMAN but obviously what I really need to do is save the entire JSON in the right form to a file and then read from that file line by line using the corrected data. I don't seem to be able to save the data in a file in the right form using my code and I suspect I am missing something simple. Is there a way to write a file line by line from in side postman or in a script and manipulate the data as I'm creating it?
Alternatively I guess I could read from the JSON I have saved i.e. the full response and iterate through that manipulating the data as a pre-request script?
I have tried to do something like this using environmental variables - so in my first call I do:
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable('setIds', JSON.stringify(jsonData));
and then in my second call to the express server where I want to send my payload I run a pre-request script that I thought would work using the env variable but this fails as it doesn't seem to like the {...
SyntaxError: Unexpected token {
I think there are probably some neat ways of solving this either doing all of this outside of POSTMAN in javascript but I'm a little lost where to start. Any help appreciated
Would tell you are plaing with content, but not setting it back to JSON object ??
jsonData.setIds[i] = new_i;
can help or you can use 2x replace it in a string and convert back to make it easier (in case there are no / or # somewhere else).
var src = {
"count": 6909,
"setIds": [
"1/7842/0889#001",
"2/4259/0166#001",
"ENT0730/0009",
"9D/11181#002",
"1/9676/0001#001",
"2/4718/0001#004",
"2/1783/0044#001",
"1/4501/0001#001",
"1/2028/0002#002",
"2/3120/0079#001",
"2/1672/0024#001",
"2/3398/0064#001"
//...
]
}
var str = JSON.stringify(src, null, 4);
str = str.replace(/\//g,'_').replace(/#/g,'.');
console.log(str);
var res = JSON.parse(str);
console.log(res);

Get output from muterun_js to python

I am using Naked.toolshed.shell to execute javascript with python.
https://naked.readthedocs.io/toolshed_shell.html#javascript-node-js-execution-functions
The python code is like this.
def push_transaction(self, ...):
"""
node PushContractTransaction.js [...]
"""
arguments = [...]
)
response = muterun_js(self.current_dir + '/js/PushContractTransaction.js', arguments=arguments)
if response.exitcode == 0:
print(f"This is the response: {response}")
return
else:
raise PushContractTransactionException(response.stderr)
The javascript code that is called returns a string.
Now i want this string in python. I am getting this object as a reponse, but I don't know how to extract the return value from it.
Naked.toolshed.types.NakedObject object at 0x105a84390
I could log something to console and then use response.stderr but doesn't seem like a good solution and I think there is a much easier one.
Making it into an answer:
muterun_js returns a Naked object which has an stdout attribute for standard output stream data, a stderr attribute for standard error stream data, and an exitcode attribute for the exit code.
Getting output of javascript script:
naked_object = muterun_js('script.js')
print("Output", naked_object.stdout.decode("utf-8"))

NativeScript: is there a way to retrieve the binary data and process it after being read from file?

This question is NativeScript related.
I want to process the binary data after being read from file.
I followed the official doc here:
file-system#readingwriting-binary-data-fromto-a-file
with this, I can successfully read and write (in other word: "copy") the binary files. What I'm trying to accomplish now is to process the binary data ( it could be any sort of binary data, eg. image, database, etc... ) and then save it in file. But I can't figure out how to retrieve the raw binary data from a variable object returned by readSync, please have a look at the code below (btw currently testing on ios environment):
var source = sourceFile.readSync(e=> { error = e; });
console.log(typeof source.bytes);
console.log(typeof source.length);
console.log(source.bytes);
console.log(source.length);
When I execute this code, I see the these messages in console log:
CONSOLE LOG file:///app/views/login/login.js:99:12: object
CONSOLE LOG file:///app/views/login/login.js:100:12: number
CONSOLE LOG file:///app/views/login/login.js:101:12: <Pointer: 0x11da02000>
CONSOLE LOG file:///app/views/login/login.js:102:12: 1720728
From this output I surmise that source.bytes is a pointer object and not an array object... My question is: is there a way to store the data into array, possibily in Uint8Array type?
I finally found a solution to make it work on ios environment. It was written in the source code:
NativeScript: objc!Foundation.d.ts
So, use getBytes() to retrieve the data into array abject.
Example code:
var arr = new ArrayBuffer(source.length);
source.getBytes(arr);
var uint8arr = new Uint8Array(arr);

Using JSON to convert an array from C# to Javascript

I have been struggling with consuming c# code converted to the javascript angular navigation tree structure. The code is correctly converted because when I hard code the returned json value my app works. However, when I try to set a variable equal to the returned get value the app does not work. Is there some kind of conversion I am missing between the get command and entering it into my treeContents?
$scope.deviceNavCollection = DeviceSvc.DeviceNavCollection.get({ deviceId:$scope.currentDeviceID });
var treeData = $scope.deviceNavCollection;
$scope.treeContents = treeData.data;
The format of the data (which matches abn-tree)
{"data":[{"label":"Connection","id":1,"src":"../Device_Common/partials/Connection.html","children":null},............}]}]}
HTML
<abn-tree tree-data="treeContents" on-select="onTreeItemSelected(branch)" select-id="treeSelectId" icon-leaf="icon-file" expand-level="1"></abn-tree>
c#
string strJSON = new JavaScriptSerializer().Serialize(navCollection);
return json;
System.Console.WriteLine("C#: Leaving GetDeviceNavCollection method");
Thanks
The request is asynchronous, you have to wait until it's finished otherwise it will be undefined:
DeviceSvc.DeviceNavCollection.get({ deviceId:$scope.currentDeviceID }).then(function(response) {
$scope.deviceNavCollection = response;
var treeData = $scope.deviceNavCollection;
$scope.treeContents = treeData.data;
});
This is, of course, assuming your DeviceNavCollection service is returning a promise.

Convert Javascript dictionary to python dictionary

Somewhere in my Django app, I make an Ajax call to my view
$.post("/metrics", {
'program': 'AWebsite',
'marketplace': 'Japan',
'metrics': {'pageLoadTime': '1024'}
});
in my python code, I have got
#require_POST
def metrics(request):
program = request.POST.get('program', '')
marketplace = request.POST.get('marketplace', '')
metrics = request.POST.get('metrics', '')
reportMetrics(metrics, program, marketplace)
the metrics() function in python is supposed to call reportMetrics() with these parameters which are then supposed to go in a log file. But In my log file, I do not see the 'pageLoadTime' value - probably because it is being passed in as a dictionary. In future I need to add more items to this, so it needs to remain a dictionary (and not a string like first two).
Whats the easiest way to convert this incoming javascript dictionary to python dictionary?
Send the javascript dictionary as json and import the python json module to pull it back out. You'll use json.loads(jsonString).
Edit - Added example
$.post("/metrics", {
data: JSON.stringify({
'program': 'AWebsite',
'marketplace': 'Japan',
'metrics': {'pageLoadTime': '1024'}
})
});
Then on the python side
import json
def metrics(request):
data = json.loads(request.POST.get('data'))
program = data.get('program','')
marketplace = data.get('marketplace','')
metrics = data.get('metrics','')
I don't really have a good way of testing this right now, but I believe it should work. You may also have to do some checking if a field is blank, but I believe .get() will handle that for you.

Categories