Python read array from file - python

Pretty new to Python so I'm struggling picking this part up.
I want to put arrays into a text file and have them called with in my .py
Here is what I had:
import json
myfile = open("lists.txt")
myVars = json.load(myfile)
myVars['alist']
and in the lists.txt file:
{
"alist" : [
["1", "2", "3"]
],
"b" : [
["test"]
]
}
I called it in my .py with:
if message.body == "!r alist":
room.message("I recommend: " + choice(alist))
print("did it work?")
(choice is just to get a random one, etc.)

Something is not adding up with the code snippets. Your json loading is fine. You can pass a file pointer to json.load
The problem looks to be here:
if message.body == "!r alist":
room.message("I recommend: " + choice(alist))
print("did it work?")
Is choice a method/function? Where is it defined? Where is alist declared in this code snippet. After loading the json file you need to access alist using:
myAList = myVars['alist']
choice(myAList[0])
Assuming that choice is a valid method that can access myAList. I suspect you are not connecting the pieces of code together properly.

You're only referencing a file pointer... You're not actually loading in the data.
myfile = open('lists.txt').read()
That should give you your string which you can then load into an object using json

Related

I am looking to create an API endpoint route that returns txt in a json format -Python

I'm new to developing and my question(s) involves creating an API endpoint in our route. The api will be used for a POST from a Vuetify UI. Data will come from our MongoDB. We will be getting a .txt file for our shell script but it will have to POST as a JSON. I think these are the steps for converting the text file:
1)create a list for the lines of the .txt
2)add each line to the list
3) join the list elements into a string
4)create a dictionary with the file/file content and convert it to JSON
This is my current code for the steps:
import json
something.txt: an example of the shell script ###
f = open("something.txt")
create a list to put the lines of the file in
file_output = []
add each line of the file to the list
for line in f:
file_output.append(line)
mashes all of the list elements together into one string
fileoutput2 = ''.join(file_output)
print(fileoutput2)
create a dict with file and file content and then convert to JSON
json_object = {"file": fileoutput2}
json_response = json.dumps(json_object)
print(json_response)
{"file": "Hello\n\nSomething\n\nGoodbye"}
I have the following code for my baseline below that I execute on my button press in the UI
#bp_customer.route('/install-setup/<string:customer_id>', methods=['POST'])
def install_setup(customer_id):
cust = Customer()
customer = cust.get_customer(customer_id)
### example of a series of lines with newline character between them.
script_string = "Beginning\nof\nscript\n"
json_object = {"file": script_string}
json_response = json.dumps(json_object)
get the install shell script content
replace the values (somebody has already done this)
attempt to return the below example json_response
return make_response(jsonify(json_response), 200)
my current Vuetify button press code is here: so I just have to ammend it to a POST and the new route once this is established
onClickScript() {
console.log("clicked");
axios
.get("https://sword-gc-eadsusl5rq-uc.a.run.app/install-setup/")
.then((resp) => {
console.log("resp: ", resp.data);
this.scriptData = resp.data;
});
},
I'm having a hard time combining these 2 concepts in the correct way. Any input as to whether I'm on the right path? Insight from anyone who's much more experienced than me?
You're on the right path, but needlessly complicating things a bit. For example, the first bit could be just:
import json
with open("something.txt") as f:
json_response = json.dumps({'file': f.read()})
print(json_response)
And since you're looking to pass everything through jsonify anyway, even this would suffice:
with open("something.txt") as f:
data = {'file': f.read()}
Where you can pass data directly through jsonify. The rest of it isn't sufficiently complete to offer any concrete comments, but the basic idea is OK.
If you have a working whole, you could go to https://codereview.stackexchange.com/ to ask for some reviews, you should limit questions on StackOverflow to actual questions about getting something to work.

How to get a particular key value in a list of dictionaries using python

Hello there this is my first stackoverflow question please bear with me.
my JSON file looks like this:
{"users":
[
{"name": "John Smith",
"phone_num":"+104484932"
},
{"name": "Linda Ray",
"phone_num": "+194387282"
}
]
}
when given an input the script should look for the name in this list of dictionaries and return the phone number, I have tried many resources, it might also look like a possible copy of this question.
Which it isn't, thanks for your help in advance.
Load the json file into a python dict, then iterate over the users list.
def myfunction(target_name):
with open('file.json') as f:
data = json.load(f)
for user in data['users']:
if user['name'] == target_name:
return user['phone_num']
You can do it in a simple way like
json = {"users":
[
{"name": "John Smith",
"phone_num":"+104484932"
},
{"name": "Linda Ray",
"phone_num": "+194387282"
}
]
}
inp = input('Name: ')
print([i['phone_num'] for i in json['users'] if i['name'] == inp][0])
Tell me if its not working...
We don't have access to your code, so not sure what approach you are taking. Gonna presume that this is simple and barebones.
If the file is json, it will need to be read in and that will require that we import the json library.
import json
The question implies we are getting input directly from the user, so we can use the input() function to gather that data. Here we are providing a prompt that will display on the screen when the script is run.
username = input("Please supply a name: ")
From there, we read in the json file which gets read in as a Python dictionary. Since it is a dictionary (Python dict()), we can drill down into it to get to the data we care about querying on specific keys to see the associated values.
When we open() the file and I am choosing to refer to the opened file as fin. Then we pull out the content that is stored under the users key (that value happens to be a list of people).
with open('filename.json') as fin:
book = json.load(fin)
users = data['users']
Then we parse each user in the users list and check for matches to the username that was input earlier. When it finds a match, it prints the corresponding phone number.
for user in users:
if user['name'] == username:
print(user['phone_num'])
The script in total will look something like this:
import json
username = input("Please supply a name: ")
with open('filename.json') as fin:
book = json.load(fin)
users = data['users']
for user in users:
if user['name'] == username:
print(user['phone_num'])

How can I generate Hocon conf file dynamically using pyhocon in Python 3?

I would like to use automation to create the hocon configuration with python 3 scripting. I read that lightbend (https://github.com/lightbend/config) recommends pyhocon (https://github.com/chimpler/pyhocon).
I am having problems figuring out how to create an Hocon object and write the data to a file as hocon. It is important to me that the syntax for the substitution are in the result.
For example I expect the output of the file myconfig.conf to look something like this:
{
Environment: "dev"
JobName: ${Environment}"-hello-bob"
}
So, I assumed that there was a way to do something like this:
config2 = ConfigFactory.parse_string("{}")
config2.put("Environment", "dev")
#Some type of object for references or special syntax for ${Environment}
config2.put("JobName", "${Environment}")
Then after creating the stuffed object there should be a simple way to write out to a file or files:
filename = "myconfig.conf"
print("Write to disk as {}".format(filename))
with open(filename, "w") as fd:
fd.write(config2.to_hocon_str)
Has anyone figured a way to do this? It seems odd that the library can only be used for reading data only.
Probably this example will answer your question
from pyhocon.converter import HOCONConverter
import pyhocon
string = '{"Environment": "Dev","Test": ${Environment}}'
factory = pyhocon.ConfigFactory.parse_string(string, resolve=True)
factory.put('somekey','somevalue')
print(HOCONConverter().to_hocon(factory))
returns
Environment = "Dev"
Test = "Dev"
somekey = "somevalue"
So, I decided to look at documentation for JVM (Java/Scala) library (https://github.com/lightbend/config). After reading the documentation, there was a clear section on hocon examples (https://github.com/lightbend/config#examples-of-hocon). In this documentation, they categorized 7 valid hocon styles. I call these styles because if I was to automate the generation of these files, I would be picking one way to write out and sticking with it.
All of these are valid HOCON.
1.Start with valid JSON:
{
"foo" : {
"bar" : 10,
"baz" : 12
}
}
2.Drop root braces:
"foo" : {
"bar" : 10,
"baz" : 12
}
3.Drop quotes:
foo : {
bar : 10,
baz : 12
}
4.Use = and omit it before {:
foo {
bar = 10,
baz = 12
}
5.Remove commas:
foo {
bar = 10
baz = 12
}
6.Use dotted notation for unquoted keys:
foo.bar=10
foo.baz=12
7.Put the dotted-notation fields on a single line:
foo.bar=10, foo.baz=12
Because I will be using the pyhocon library, I needed to look for write solutions within the library. I found some help from chimpler's git (https://github.com/chimpler/pyhocon). What I found was that they have two hocon styles which can be simply written out. One is json and the other is something that wasn't on the list which was describe above by lightbend.
Style 1: pure JSON, witch can be written out in two ways:
HOCONConverter.to_json
#Using HOCONConverter.to_json
confTree = ConfigFactory.parse_string("{}")
confTree.put("Environment","Dev")
confTree.put("Test","${Environment}")
filename = "./json_coverted.conf"
print("Write to disk as {}".format(filename))
with open(filename, "w") as fd:
fd.write(HOCONConverter.to_json(confTree))
HOCONConverter.to_json Result
{
"Environment": "Dev",
"Test": "${Environment}"
}
OR Using json.dump
#Using json.dump
confTree = ConfigFactory.parse_string("{}")
confTree.put("Environment","Dev")
confTree.put("Test","${Environment}")
filename = "./json_dumped.conf"
print("Write to disk as {}".format(filename))
with open(filename, "w") as fd:
fd.write(json.dumps(confTree,indent=4))
Using json.dump Result
{
"Environment": "Dev",
"Test": "${Environment}"
}
Pyhocon's other Style, not listed by lightbend
# HOCONConverter.to_hocon
confTree = ConfigFactory.parse_string("{}")
confTree.put("Environment","Dev")
confTree.put("Test","${Environment}")
filename = "./hocon_coverted.txt"
print("Write to disk as {}".format(filename))
with open(filename, "w") as fd:
fd.write(HOCONConverter.to_hocon(confTree))
Pyhocon's other Style, not listed by lightbend Result
Environment = "Dev"
Test = "${Environment}"
So, to answer my own question the only dependable way to generate a hocon conf file dynamically using pyhocon in Python 3 is by using one of the json methods (converter or dumps). But this still leaves an open question. The question being, will reading a json to a pyhocon ConfTree object be able dereference the substitutions when they are in the json?
For example if I read the file
{
"Environment": "Dev",
"Test": "${Environment}"
}
Will the ConfTree object get "Dev" as the value for Test?
No, it will not. Here is my test
filename = "json_coverted.conf"
print("Reading file{}".format(filename))
conf = ConfigFactory.parse_file(filename)
key="Test"
value=conf.get(key)
print("Key:{} Value:{}".format(key,value))
Test Result Out to screen
Reading filejson_coverted.conf
Key:Test Value:${Environment}
So, then how does one use pyhocon with substitutions?
It just can't hence, I will not use either library for writing out confs. It has to be a manual process if I want to use substitutions. So, I am only using this library for reading confs.

passing a python object into a casperjs script iterating over the object and returning a result object to python

I'm new to programing in languages more suited to the web, but I have programmed in vba for excel.
What I would like to do is:
pass a list (in python) to a casper.js script.
Inside the casperjs script I would like to iterate over the python object (a list of search terms)
In the casper script I would like to query google for search terms
Once queried I would like to store the results of these queries in an array, which I concatenate together while iterating over the python object.
Then once I have searched for all the search-terms and found results I would like to return the RESULTS array to python, so I can further manipulate the data.
QUESTION --> I'm not sure how to write the python function to pass an object to casper.
QUESTION --> I'm also not sure how to write the casper function to pass an javascript object back to python.
Here is my python code.
import os
import subprocess
scriptType = 'casperScript.js'
APP_ROOT = os.path.dirname(os.path.realpath(__file__))
PHANTOM = '\casperjs\bin\casperjs'
SCRIPT = os.path.join(APP_ROOT, test.js)
params = [PHANTOM, SCRIPT]
subprocess.check_output(params)
js CODE
var casper = require('casper').create();
casper.start('http://google.com/', function() {
this.echo(this.getTitle());
});
casper.run();
Could you use JSON to send the data to the script and then decode it when you get it back?
Python:
json = json.dumps(stuff) //Turn object into string to pass to js
Load a json file into python:
with open(location + '/module_status.json') as data_file:
data = json.load(data_file);
Deserialize a json string to an object in python
Javascript:
arr = JSON.parse(json) //Turn a json string to a js array
json = JSON.stringify(arr) //Turn an array to a json string ready to send to python
You can use two temporary files, one for input and the other for output of the casperjs script. woverton's answer is ok, but lacks a little detail. It is better to explicitly dump your JSON into a file than trying to parse the console messages from casperjs as they can be interleaved with debug strings and such.
In python:
import tempfile
import json
import os
import subprocess
APP_ROOT = os.path.dirname(os.path.realpath(__file__))
PHANTOM = '\casperjs\bin\casperjs'
SCRIPT = os.path.join(APP_ROOT, test.js)
input = tempfile.NamedTemporaryFile(mode="w", delete=False)
output = tempfile.NamedTemporaryFile(mode="r", delete=False)
yourObj = {"someKey": "someData"}
yourJSON = json.dumps(yourObj)
input.file.write(yourJSON)
# you need to close the temporary input and output file because casperjs does operations on them
input.file.close()
input = None
output.file.close()
print "yourJSON", yourJSON
# pass only file names
params = [PHANTOM, SCRIPT, input.name, output.name]
subprocess.check_output(params)
# you need to open the temporary output file again
output = open(output.name, "r")
yourReturnedJSON = json.load(output)
print "returned", yourReturnedJSON
output.close()
output = None
At the end, the temporary files will be automatically deleted when the objects are garbage collected.
In casperjs:
var casper = require('casper').create();
var fs = require("fs");
var input = casper.cli.raw.get(0);
var output = casper.cli.raw.get(1);
input = JSON.parse(fs.read(input));
input.casper = "done"; // add another property
input = JSON.stringify(input);
fs.write(output, input, "w"); // input written to output
casper.exit();
The casperjs script isn't doing anything useful. It just writes the inputfile to the output file with an added property.

How to split Flask Main file in multiple files and share Variable

I have read alot about this but I just don't seem to figure it out... I should use Blueprint for this but the problem I am having right now is that I do not know how to pass my variable from my main file in my second file.
As an example :
/app
/runserver.py
/app
init.py
main.py
second.py
Now I do have a dictionairy in my main that I fill. And I want to use it in my second file to adjust it etc. How will I be able to do this? Since I tried to import the files and tried:
import main
dictMain = main.dictFromMain
I thought this would be enough since I read it on different question on Stack Overflow but it doesn't seem to work!
EDIT: To sketch the problem further
More background : I am making a client - server application, the client is receiving and sending data from the server. But there is a difference is the data the client is sending. On one hand you have files and paramters which I want to 'capture' with my second file with ReST. And on the other hand I got a incomming stream which I 'capture' in my main file.
Example second file:
#app.route('/uploads/', methods = ['GET', 'POST'])
def get_files():
if request.method == 'GET':
sendDict = []
for element in ctxList:
for fileCtx in element['file']:
d = { 'id' : element['id'], 'file': [ {'name': fileCtx['name'], 'uri' : fileCtx['uri'], 'path' : fileCtx['path'] } ] }
sendDict.append(d)
jsonString = jsonify(ctx=sendDict)
return jsonString
But this code uses a dictionairy from my first file (the dict ctxList) I have no idea to get it out of my first file. I used to get a error when I did : ctxList = mainFile.ctxList that the module did not have this variable, but now I am getting a error that the first file does not know the URL structure ( /uploads/ from the second file).

Categories

Resources