PROLOG - check if file exists - prolog

In my program I want to first check if a certain file exists. If it does I want to read it, if not I want to create it (using tell) and work with it. I can't get Prolog to check if a file exists.
I've tried
exists_file('testfile.pl')
exists('testfile.pl')
access_file('testfile.pl',exists)
For all three I get the message "existence error ... does not exist"
What's wrong here?
What I want is a predicate that returns true or false, depending on whether the file exists or not.
Thanks

Related

Cypress file upload for negative test

I have a form that accepts a file and using cypress-file-upload, I made it to work correctly. But I also have a scenario where I need to verify an error when no file is uploaded. The package itself is validating the file is exist or not. How we can test in such case?
cy.get('input[type="file"]').attachFile();// Cannot read property 'filePath' of undefined
cy.get('input[type="file"]').attachFile(""); //missing "filePath" or "fileName".
Please make sure you are passing either "filePath" or "fileName"
cy.get('input[type="file"]').attachFile("expense1.json")//Works for the positive case
cy.get(".alert").should('be.visible').and('contain', "You poor guy did not upload a file...")
It's not clear from the question how a user would do the wrong thing and cause the alert, this is what you'd want to replicate in test.
Maybe there is no such user path, if it's just for checking some internal error occurred.
You should find out exactly what triggers the alert and test accordingly.
In this case, here's some ideas
option allowEmpty: true and cy.get('input[type="file"]').attachFile("")
make expense1.json a binary file, or an empty file, or a badly formed json (remove a bracket)
give an invalid encoding in options, encoding: 'binary' and use valid expense1.json`
give an invalid mimeType in options

Power Automate - Condition result is always true

My flow checks if there is a file in a folder. If there is, it appends a string to the filename to avoid duplicate file names. It then copies the file to another folder and deletes it.
The problem is, the majority of the time there are no files in the folder. When this happens, the flow fails. I am trying to resolve this with a conditional statement. The statement I am using must not be correct since even when there are no files in my folder, it is returning true.
In the Condition step it's checking Body is not null, which it isn't, it's just an empty array (again it's not null even if it's empty), to check for an empty body (which is an empty array) this should be the condition expression:
empty(outputs('List_files_in_folder')?['body']) is equal to true

A "display" that always displays in prolog

I have a predicate check(Data,Res) that checksDats according to some rules and returns Res (a function result on Data, assuming Data answers to several criteria).
I have another function generate(N,Data) which generates a N-size Data.
My main program begins with generating many 1-size Data, then if none answered the criteria, we go on to 2-size Data and so on until we reach a certain M upper limit.
main(M):- next_number(N,M), generate(N,Data), check(Data,Res).
However, the program runs for very long time. I wanted to make sure it does not get stuck. For this, I wanted to print the generated Data each time before its being checked. But adding display did not assist, because it only actually displayed if the entire statement was true.
That's not what I want.
I want to keep track of the progran using display, similarly to System.out.println in Java.
Is there any other function that displays anyway? Or an idea how to use display in a way that will always display, regardless if the Data answered the criteria or not?
I thought to do:
(check(Data,Res) -> display(Data);display(Data)).
But I am not sure. Any ideas?
Your long process is likely to be within check - or more precisely, that check fails for most data, causing the system to backtrack repeatedly.
If you display a result in check, you'll have line upon line of tracing. Instead, you could add a write statement to generate, or even to your number generation:
main(M):-
next_number_and_tick(N,M),
generate(N,Data),
check(Data,Res).
next_number_and_tick(N,M) :-
next_number(N,M),
write('Tick - now doing '),
writeln(N).
Upon backtracking, the program will signal the data size it is now working on, giving you an idea of the volume of work it is doing.
The problem in the way you use display is that the entire statement must be true for it to display. Your idea of using "if-then" is good but not accurate. If you want to use it, you should "trick" prolog the following way:
new_check(Data,Res) :- (check(Data,Res) -> display('Victory!'),!; display('Failed Data: '), display(Data), nl, fail).
This way, if the check fails, you will get a report on which Data failed, and if it succeeded everything stops (assuming you want only 1 solution. If you want more, remoce the ! predicate).

Psychopy: Call a conditions file from the outer loop's conditions file

I am a bit new to PsychoPy and Python coding, so please excuse my question if it is basic. In my task, I have a number of files that dictate the position of stimuli. My outer loop has a variable, ExcelList, which has the previously mentioned file names listed under it. The inner loop, which dictates each trial, attempts to call these files at random by entering $ExcelList into the space asking for a conditions file. As I understand it, the command for $ExcelList should access the conditions file in the outer loop and pull one of the files containing stimuli positions for that trial. However, I am instead presented with the following error:
File "/Users/bencline/Desktop/Psychexp/NegPriming2080_lastrun.py",
line 247, in module>
trialList=data.importConditions(ExcelList), File "/Applications/PsychoPy2.app/Contents/Resources/lib/python2.7/psychopy/data.py",
line 1366, in importConditions
raise ImportError('Conditions file not found: %s' %os.path.abspath(fileName)) ImportError: Conditions file not found:
/Users/bencline/Desktop/Psychexp/Trials_20_95.xlsx
It would appear that the inner loop is not finding the condition in the outer loop (or not reading the outer loop altogether). If I try instead writing $eval(ExcelList) I am presented with the following error:
File "/Users/bencline/Desktop/Psychexp/NegPriming2080_lastrun.py",
line 247, in
trialList=data.importConditions(eval(ExcelList)), File "", line 1, in NameError: name 'Trials_20_95' is not
defined
This seems more indicative of the underlying problem, but I'm still not sure how to proceed from here. Do you have any suggestions for why this is happening and how I could potentially fix it?
Thank you,
-Ben
Your strategy is right. It reads your ExcelList in the outer loop but fails to find the filenames in ExcelList on your filesystem. In particular, in your first error message, it fails to find /Users/bencline/Desktop/Psychexp/Trials_20_95.xlsx. So check whether it actually exists. I strongly suspect that it does not because of one or both of these:
It is in a different folder, e.g. a subfolder. The solution is to write out the path (relative or absolute) in the ExcelList file.
It is spelled differently, e.g. with a small T or capital postfix (XLSX), a spacebar or the like. The solution is of course to make the filenames in the ExcelList and the actual filenames match.

How can I check if a string is a valid file name for windows using R?

I've been writing a program in R that outputs randomization schemes for a research project I'm working on with a few other people this summer, and I'm done with the majority of it, except for one feature. Part of what I've been doing is making it really user friendly, so that the program will prompt the user for certain pieces of information, and therefore know what needs to be randomized. I have it set up to check every piece of user input to make sure it's a valid input, and give an error message/prompt the user again if it's not. The only thing I can't quite figure out is how to get it to check whether or not the file name for the .csv output is valid. Does anyone know if there is a way to get R to check if a string makes a valid windows file name? Thanks!
These characters aren't allowed: /\:*?"<>|. So warn the user if it contains any of those.
Some other names are also disallowed: COM, AUX, NUL, COM1 to COM9, LPT1 to LPT9.
You probably want to check that the filename is valid using a regular expression. See this other answer for a Java example that should take minimal tweaking to work in R.
https://stackoverflow.com/a/6804755/134830
You may also want to check the filename length (260 characters for maximum portability, though longer names are allowed on some systems).
Finally, in R, if you try to create a file in a directory that doesn't exist, it will still fail, so you need to split the name up into the filename and directory name (using basename and dirname) and try to create the directory first, if necessary.
That said, David Heffernan gives good advice in his comment to let Windows do the wok in deciding whether or not it can create the file: you don't want to erroneously tell the user that a filename is invalid.
You want something a little like this:
nice_file_create <- function(filename)
{
directory_name <- dirname(filename)
if(!file.exists(directory_name))
{
ok <- dir.create(directory_name)
if(!ok)
{
warning("The directory of that path could not be created.")
return(invisible())
}
}
tryCatch(
file.create(filename),
error = function(e)
{
warning("The file could not be created.")
}
)
}
But test it thoroughly first! There are all sorts of edge cases where things can fall over: try UNC network path names, "~", and paths with "." and ".." in them.
I'd suggest that the easiest way to make sure a filename is valid is to use fs::path_sanitize().
It removes control characters, reserved characters, and Windows-reserved filenames, truncating the string at 255 bytes in length.

Resources