New to Python, and I find this bit rather confusing. I have a config file that is read into a kwarg. with values like:
students||student a|student b|student c
I see this populating properly into the kwarg when I debug, however, when I attempt to read the values, it will always only pull the first letter.
for student in kwargs.get('students'):
--student will come thru as 's'
this seems to be something specific to my machine, as this code is running in prod. I set my libraries the same as the server, but still having the issue. Anyone know why this might be occurring?
I am running on Python 3.6.5
Thanks
Related
I'm relatively new to working with server instances so please excuse my limited knowledge. I have a python script that I run for 5 different people. Each person who I run it for, has a slightly different file (Ie: some names in the script are changed to work with their branding and name), and right now when I get a new client, I have to copy the script, and change each part that needs to be changed by hand, and then run that in my server (I'm running all 5 scripts on the same server). If I make any changes to my codebase, I need to change the script for each of my clients, go to the server and swap out the old versions by hands.
I'm expecting to have this grow to a couple hundred clients so this is just not a feasible method. Are there any products or methods I can use to fix this product, or atleast make it easier to update all the files at once?
Without looking at the specifics of the script it's really hard to say, but here are a couple of solutions that might work:
Pass the values that change as arguments to the script - Use something like argparse to capture the arguments into variables
Modify the script to run on a list of arguments - This is pretty easy to do - literally just add a for-loop around your entire script and iterate over the values for each of your clients.
I am trying to find a way to edit and run a sequence of python scripts in a less manual way.
For context, I am running a series of simulations, which consist in running three codes in order 10 times, making minor changes to each code every time. The problem I am encountering is that this process leads to easy mistakes and chaotic work.
These are the type of edits I have to make to each code.
- Modify input/output file name
- Change value of a parameter
What is the best practice to deal with this? I imagine that the best idea would be to write another python script that does all this. Is there a way to edit other python codes, from within a code, and run them?
I don't intend or want anyone to write a code for me. I just need to be pointed in a general direction. I have searched for ways to 'automatize' codes, but haven't yet been successful in finding a solution to my query (mainly the editing part).
Thanks!
The thing that can change (files or parameter values) should be able to be either passed in or injected. Could be from a command line parameter, configuration file, or method argument. This is the "general direction" I offer.
I've been working on a project lately which requires me to get a value stored in a text file. Simple task, right?
I have tried pretty much every stupid solution that uses readline() at some point, but when it's printed, there's nothing. Debug also tells me that it is empty.
Since I've experienced some inconsistencies with python already, I tried using the same function inside a different python file, and running it does exactly what it's supposed to do.
My current solution (not the best, but currently I just want it to run properly) is
count_file = "count.txt"
filehandle = open(count_file, 'r')
line = filehandle.readline()
print(line)
which works fine when used in a file called "test.py". When used in my "main.py", it returns nothing. And yes, I used it on the same context and temporarily deleted everything else from the main file while testing.
Does anyone have a clue what causes this? I could just paste the other stuff into the test file and rename it, but 1. that's annoying and 2. it'd be useful to know how to avoid this.
EDIT: I am not certain yet, but I think that the problem is caused by my IDE (am using the latest version of Pycharm Professional). I solved the problem by deleting and re-adding the Run/Debug Configuration.
Sometimes you will get return value as None if you do not specify the file path. please check the path/relative path of "count.txt" file with respect to "main.py" script.
First point: your file path is relative, so it will be resolved according to the current working directory, NOT depending on your script's location.
Second point: you read a single line, it can indeed be empty or only contains non-printable characters.
Third point: run your code without pycharm. Pycharm is probably fine when you really understand what it does behind your back, but if you don't you will indeed experiment some unexpected results. Just using the command line and a simple code editor is actually much simpler (though possibly not as productive) and less confusing.
And finally, don't forget you have an interactive Python shell. Quite handy for testing out things...
As the title says, I have an app hosted on Heroku that seems to have more than one instance running. This is a problem because I am trying to keep track of single letter posts on a GroupMe in order to see if they spell anything. I am using python and a global string variable that I can add the characters to. Since there is more than one instance, there is more than 1 global variable so sometimes the character gets added to one or the other which defeats the purpose of the program. Has anyone else run into this problem or found a fix to this? Thanks.
So after thinking about possible ways to fix it, I decided to try to keep my global variables inside a json file and then edit that each time. This worked as I believe that even if there were 2 version of the actual code running, they should both edit the same json file.
I have a test that does the following:
create an index on ES using pyes
insert mapping
insert data
query the data to check if it fits the expected result
However when I run this test, sometimes it would find no result, and sometimes it would find something but some data is missing. The interesting thing is this only happens when I run the test automatically. If I type in the code ling-by-line, everything works as expected. I've tested it 3 times manually and it works without failure.
Sometimes I've even received this message:
NoServerAvailable: list index out of range
It seems like the index was not created at all
I've pinged my ES address and everything looks correct. There was no other error msg found anywhere.
I thought it would be because I was trying to get the data too fast after the insert, as documented here: Elastic Search No server available, list index out of range
(check first comment in the accepted answer)
However, even if I make it delay for 4 seconds or so, this problem still happens. What concerns me is the fact that sometimes only SOME data is missing, which is really strange. I thought it would be either finding it all or missing it all.
Anyone got similar experience that can shine some light on this issue? Thanks.
Elasticsearch is Near Realtime(NRT). It might take upto 1 second to make your recently indexed document be visible/available for search.
To make your recently indexed document available instantly for searching, you can append ?refresh=wait_for at the end of the index document command. For e.g.,
POST index/type?refresh=wait_for
{
"field1":"test",
"field2":2,
"field3":"testing"
}
?refresh=wait_for will forcibly refresh your index to make the recently indexed document available for search. Refer ?refresh.