I have the following line of code which reads an image (which is fed into a POST request):
files = {"image": (image_path, open(image_path, "rb"))}
While trying to run this through mypy, it keeps throwing the following error:
Argument 1 to "open" has incompatible type "Optional[str]"; expected "Union[Union[str, bytes, PathLike[str], PathLike[bytes]], int]"
I've tried searching this, but I've not found a solution for similar problems.
Is there a different way to read filepaths in order to avoid these issues?
Not the correct answer but if you want to temporarily make it go away to move ahead:
# type: ignore
at the end of the erroring line should work.
Related
I am trying to run a NEURON simulation via python. I got all the libraries in order and am able to run some simple simulations, but am experiencing some troubles with a more complicated code. If you have any idea how to help I will appreciate it very much
Problem number 1:
Neuron doesn't open part of a .hoc file even though it is compiled. I get the error:
NEURON: Can't open import3d/import3d_sec.hoc
in import3d.hoc near line 1
{xopen("import3d/import3d_sec.hoc")}
^
xopen("import3d/i...")
xopen("import3d.hoc")
execute1("{xopen("im...")
load_file("C:/Users/U...")
Problem number 2:
The simulator doesn't recognize a mechanism I am trying to use. here I am a bit lost and don't know to describe further, but this is the error message:
NEURON: Im is not a MECHANISM
in L5PCbiophys5b.hoc near line 26
insert Im
^
xopen("L5PCbiophy...")
execute1("{xopen("L5...")
load_file("C:/Users/U...")
Problem number 3:
Not recognizing as a template:
NEURON: Import3d_Neurolucida3 is not a template
in L5PCtemplate_2.hoc near line 26
nl = new Import3d_Neurolucida3()
^
xopen("L5PCtempla...")
execute1("{xopen("L5...")
load_file("C:/Users/U...")
You can try to use an absolute path
The name for insert should match with the SUFFIX statement in the file; also make sure that file was compiled in and that the dll is loaded (should be a message when you start nrniv)
Perhaps a result of the file xopen problem? If it is a template-containing file you should use load_file() instead of xopen()
Trying out the queue system for a better user upload experience with Laravel-Excel.
.env was been changed from 'sync' to 'database' and migrations run. All the necessary use statements are in place yet the error above persists.
The exact error happens here:
Illuminate\Queue\Queue.php:97
$payload = json_encode($this->createPayloadArray($job, $queue, $data));
if (JSON_ERROR_NONE !== json_last_error()) {
throw new InvalidPayloadException(
If I drop ShouldQueue, the file imports perfectly in-session (large file so long wait period for user.)
I've read many stackoverflow, github etc comments on this but I don't have the technical skills to deep-dive to fix my particular situation (most of them speak of UTF-8 but I don't if that's an issue here; I changed the excel save format to UTF-8 but it didn't fix it.)
Ps. Whilst running the migration, I got the error:
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `jobs` add index `jobs_queue_index`(`queue`))
I bypassed by dropping the 'add index'; so my jobs table is not indexed on queue but I don't feel this is the cause.
One thing you can do when looking into json_encode() errors is use the json_last_error_msg() function, which will give you a bit more of a readable error message.
In your case you're getting a '5' back, which is the JSON_ERROR_UTF8 error code. The error message back for this is a slightly more informative one:
'Malformed UTF-8 characters, possibly incorrectly encoded'
So we know it's encountering non-UTF-8 characters, even though you're saving the file specifically with UTF-8 encoding. At first glance you might think you need to convert the encoding yourself in code (like this answer), but in this case, I don't think that'll help. For Laravel-Excel, this seems to be a limitation of trying to queue-read .xls files - from the Laravel-Excel docs:
You currently cannot queue xls imports. PhpSpreadsheet's Xls reader contains some non-utf8 characters, which makes it impossible to queue.
In this case you might be stuck with a slow, non-queueable option, or need to convert your spreadsheet into a queueable format e.g. .csv.
The key length error on running the migration is unrelated. It has been around for a while and is a side-effect of using an older version of MySQL/MariaDB. Check out this answer and the Laravel documentation around index lengths - you need to add this to your AppServiceProvider::boot() method:
Schema::defaultStringLength(191);
I am trying to get my program to write over a simple config.txt file.
Which is actually working: it writes the new content over the old one if one condition is met and proceeds to the next block.
However, when the next block calls another function of my program, the content of the file simply dissapears: the .txt file becomes empty.
I have searched SO for similar issues but couldn't find any posts.
I looked into:
How to search and replace text in a file using Python?
Search and replace a line in a file in Python
And and got some of these to work but always ran into the issue above.
Please be gentle as I am a newbie and this is my first "not Hello_world" program and my first post. Thanks:)
>>>Content of the default config.txt file is 'runcount=0'
def alias(confirmation):
###First block###
if confirmation.lower()=='yes':
print("...")
config=open('config.txt','r')
data=config.read()
config.close()
newdata=data.replace('runcount=0','runcount=1')
config=open('config.txt','w')
config.write(newdata)
config.close()
confirmation2=input()
>>>Content of the config.txt file is now 'runcount=1'
###Second block###
if confirmation2.lower()=='yes':
print("...")
return main()
else:
...
>>>The main() function is called... However the config.txt file is now empty
I choose to show you this code because I think it is the most readable.
I tried the "with" method which gives me the same results.
I also tried to write to a new file, remove the default and rename the new to config.txt... same issue.
I don't get why the file becomes empty as I closed it.
No error msg, my program just goes on but the file is empty.
Please note that I used open(config.txt, 'r') in another function of my program, only once yet.
It happens that I coded the following line in main() and did not close config.txt:
config=open("config.txt","w+")
And forgot about it...
Thank you guys for the hint.
I'm going to hide somewhere very dark now.
I'm trying to Create a file and append all the content being calculated into that file, but when I run the script the very last iteration is written inside the file and nothing else.
My code is on pastebin, it's too long, and I feel like you would have to see exactly how the iteration is happening.
Try to summarize it, Go through an array of model numbers, if the model number matches call the function that calculates that MAC_ADDRESS, when done calculating store all the content inside a the file.
I have tried two possible routes and both have failed, giving the same result. There is no error in the code (it runs) but it just doesn't store the content into the file properly there should be 97 different APs and it's storing only 1.
The difference between the first and second attempt,
1 attempt) I open/create file in the beginning of the script and close at the very end.
2 attempt) I open/create file and close per-iteration.
First Attempt:
https://pastebin.com/jCpLGMCK
#Beginning of code
File = open("All_Possibilities.txt", "a+")
#End of code
File.close()
Second Attempt:
https://pastebin.com/cVrXQaAT
#Per function
File = open("All_Possibilities.txt", "a+")
#per function
File.close()
If I'm not suppose to reference other websites, please let me know and I'll just paste the code in his post.
Rather than close(), please use with:
with open('All_Possibilities.txt', 'a') as file_out:
file_out.write('some text\n')
The documentation explains that you don't need + to append writes to a file.
You may want to add some debugging console print() statements, or use a debugger like pdb, to verify that the write() statement actually ran, and that the variable you were writing actually contained the text you thought it did.
You have several loops that could be a one-liner using readlines().
Please do this:
$ pip install flake8
$ flake8 *.py
That is, please run the flake8 lint utility against your source code,
and follow the advice that it offers you.
In particular, it would be much better to name your identifier file than to name it File.
The initial capital letter means something to humans reading your code -- it is
used when naming classes, rather than local variables. Good luck!
In my code I am attempting to read in output files that may or may not have a formatted integer in the first line of the file. To aid backwards compatibility I am attempting to be able to read in both examples as shown below.
head -n 3 infile_new
22
8
98677.966601475651 -35846.869655806520 3523978.2959464169
or
head -n 3 infile_old
8
98677.966601475651 -35846.869655806520 3523978.2959464169
101205.49395364164 -36765.047712555031 3614241.1159234559
The format of the top line of infile_new is '(i5)' and so I can accommodate this in my code with a standard read statement of
read(iunit, '(I5)' ) n
This works fine, but if I attempt to read in infile_old using this, I as expected get an error. I have attempted to get around this by using the following
read(iunit, '(I5)' , iostat=ios, err=110) n
110 if(ios == 0) then
print*, 'error in file, setting n'
naBuffer = na
!rewind(iunit) #not sure whether to rewind or close/open to reset file position
close(iunit)
open (iunit, file=fname, status='unknown')
else
print*, "Something very wrong in particle_inout"
end if
The problem here is that when reading in either the old or new file the code ends up in the error loop. I've not been able to find much documentation on using the read statement in this way, but cannot determine what is going wrong.
My one theory was my use of ios==0 in the if statement, but figured since I shouldn't have an error when reading the new file it shouldn't matter. It would be great to know if anyone knows a way to catch such errors.
From what you've shown us, after the code executes the read statement it executes the statement labelled 110. Then, if there wasn't an error and iostat==0 the true branch of the if construct is executed.
So, if there is an error in the read the code jumps to that statement, if there isn't it walks to the same statement. The code doesn't magically know to not execute the code starting at label 110 if there isn't an error in the read statement. Personally I've never used both iostat and err in the same read statement and here I think it's tripping you up.
Try changing the read statement to
read(iunit, '(I5)' , iostat=ios) n
You'd then need to re-work your if construct a bit, since iostat==0 is not an error condition.
Incidentally, to read a line which is known to contain only one integer I wouldn't use an explicit format, I'd just use
read(iunit, * , iostat=ios) n
and let the run-time worry about how big the integer is and where to find it.