Write text to file function python - python-3.x

so I would like to ask you for help.
I was scooping and sweeping the internet so long for that and I did not find how to do it. What I just produced is totally wrong and my professor did bad time with formulating the task. it is entry level task on university I have already finished the hardest ones, but this one I just dont get what he wants
Translation is made by me so it might be little bit off, but bear with me, please.
"Create file "linewriter.py". This file will have function writeTextToFile, that will accept one parameter and on end is also one parameter. Function will take the parameter on input and chain it with variable STATIC_TEXT (stated bellow). Queue of chaining will be as follows: 1st static text and then argument of function. This way chained input will write to file of any given name and a the name of this file will be returned as input parameter of function.
STATIC_TEXT: “This is my static text which must be added to file. It is very long text and I do not know what they want to do with this terrible text. ”
...
What? I really don't get what he want's from me. Can you help? Thank you all :)

STATICKY_TEXT = "This is my static text which must be added to file. It is very long text and I do not know what they want to do with this terrible text.\nNew Line! "
saveFile = open ("exampleFile.txt","w")
saveFile.write(STATICKY_TEXT)
saveFile.close()
solved it :) but I COULD not figure it out for days haha ... silly me

Related

Executing functions stored in a string

Lets say that there is a function in my Delphi app:
MsgBox
and there is a string which has MsgBox in it.
I know what most of you are going to say is that its possible, but I think it is possible because I opened the compiled exe(compiled using delphi XE2) using a Resource Editor, and that resource editor was built for Delphi. In that, I could see most of the code I wrote, as I wrote it. So since the variables names, function names etc aren't changed during compile, there should a way to execute the functions from a string, but how? Any help will be appreciated.
EDIT:
What I want to do is to create a simple interpreter/scripting engine. And this is how its supposed to work:
There are two files, scr.txt and arg.txt
scr.txt contains:
msg_show
0
arg.txt contains:
"Message"
And now let me explain what that 0 is:
First, scr.txt's first line is function name
second line tells that at which line its arguments are in the arg.txt, i.e 0 tells that "Message" is the argument for msg_show.
I hope my question is now clear.
I want to make a simple scripting engine.
In order to execute arbitrary code stored as text, you need a compiler or an interpreter. Either you need to write one yourself, or embed one that already exists. Realistically, the latter option is your best option. There are a number available but in my view it's hard to look past dwscript.
I think I've already solved my problem! The answer is in this question's first answer.
EDIT:
But with that, as for a workaround of the problem mentioned in first comment, I have a very easy solution.
You don't need to pass all the arguments/parameters to it. Just take my example:
You have two files, as mentioned in the question. Now you need to execute the files. It is as simple as that:
read the first line of scr.txt
check if it's a function. If not, skip the line
If yes, read the next line which tells the index where it's arguments are in arg.txt
pass on the index(an integer) to the "Call" function.
Now to the function which has to be executed, it should know how many arguments it needs. i.e 2
Lets say that the function is "Sum(a,b : integer)".It needs 2 arguments
Now let the function read the two arguments from arg.txt.
And its done!
I hope it will help you all.
And I can get some rep :)

Same for loop, giving out two different results using .write()

this is my first time asking a question so let me know if I am doing something wrong (post wise)
I am trying to create a function that writes into a .txt but i seem to get two very different results between calling it from within a module, and writing the same loop in the shell directly. The code is as follows:
def function(para1, para2): #para1 is a string that i am searching for within para2. para2 is a list of strings
with open("str" + para1 +".txt", 'a'. encoding = 'utf-8') as file:
#opens a file with certain naming convention
n = 0
for word in para2:
if word == para1:
file.write(para2[n-1]+'\n')
print(para2[n-1]) #intentionally included as part of debugging
n+=1
function("targetstr". targettext)
#target str is the phrase I am looking for, targettext is the tokenized text I am
#looking through. this is in the form of a list of strings, that is the output of
#another function, and has already been 'declared' as a variable
when I define this function in the shell, I get the correct words appearing. However, when i call this same function through a module(in the shell), nothing appears in the shell, and the text file shows a bunch of numbers (eg: 's93161), and no new lines.
I have even gone to the extent of including a print statement right after declaration of the function in the module, and commented everything but the print statement, and yet nothing appears in the shell when I call it. However, the numbers still appear in the text file.
I am guessing that there is a problem with how I have defined the parameters or how i cam inputting the parameters when I call the function.
As a reference, here is the desired output:
‘She
Ashley
there
Kitty
Coates
‘Let
let
that
PS: Sorry if this is not very clear as I have very limited knowledge on speaking python
I have found the solution to issue. Turns out that I need to close the shell and restart everything before the compiler recognizes the changes made to the function in the module. Thanks to those who took a look at the issue, and those who tried to help.

TextIO.outputSubstr() does not write anything

I have a really annoying problem.
This function:
fun writeAFile() =
let
val outstream = TextIO.openOut "look_at_me_im_a_file.txt"
in
TextIO.outputSubstr(outstream,Substring.full("I'm so sad right now :("))
end;
Just creates the file look_at_me_im_a_file.txt but it's empty.
I get no errors and it does not work with either SML/NJ or PolyML.
I have no problems reading from files.
First of all, Substring.full isn't needed - it doesn't really do anything other than give you something of the substring type. Instead, you can do:
TextIO.output (outstream, "I'm so sad right now :(");
Now, the reason it doesn't work:
When you tell sml to write something to a file (using TextIO.output or TextIO.outputSubstr) it doesn't actually write it in the file right away. It writes to a buffer. Well, sometimes it writes to the file right away, but not often enough that you can depend on it.
Now, this seems terribly impractical, but it's more efficient - if you tell it to write several small pieces of data after each other, it can just lump it all together in one write operation.
The way to get around it is to tell sml "Hey, I really want that write to happen right now." There's a function just for that, which is called TextIO.flushOut. Alternatively, closing the stream will also cause everything to be written.
Actually, you should always remember to close your streams. Leaving open file handles lying around is messy - how will the filesystem know that you're done with it, and that it can let other programs write to the file?
Being an rookie i didn't check our lecture notes :/
a functioning version of the code is
fun writeAFile() =
let
val outstream = TextIO.openOut "look_at_me_im_a_file.txt"
in
(
TextIO.output(outstream,"I'm so glad right now :)");
TextIO.closeOut(outstream)
)
end;
Although its noteworthy that the online documentation at http://www.standardml.org/Basis/text-io.html only gives a vague reference to the output function.
And looking at the documentation for the IMPERATIVE_IO says val output : outstream * vector -> unit which is confusing since it is non apparent that string is actually of type CharVector.vector and is thus a valid argument for the output function.
I hope this will be of help to some other rookie out there.

VB6 Read String From Resource

I have text file in VB6 Application resources, and I am trying to read the text in it.
How to do that? I have been searching for hours without a proper solution. Somebody please help me.
My code is:
Private Sub Command1_Click()
Dim URL As String
URL = LoadResString(101)
MsgBox URL
End Sub
This maybe explains it more: http://i.imgur.com/wGnWCBb.jpg
Is this even possible? Somebody please spoonfeed me, I would appreciate that a lot.
I am trying to read the string from resource to a variable(string) and then prompt it with messagebox.
Some simple solution would be great. Also, if this is possible with FindResource API, please tell me how or point me to the right direction.
I had to do something like this many years ago.
I used s = StrConv(LoadResData(resId, resType), vbUnicode). The resource was an ANSI (non-unicode) file.
resType was a custom type I just made up when I saved the resource.
I had an issue with a double null that got appended at the end of the text, and which had to be removed. I can't remember the exact reason why that happens, but I presume it has to do with the resource being stored as a double-null-terminated list of C-strings.
If I had to guess, you'll have better luck with LoadResData(). Make sure to use both parameters (the id and type ones).

How to get the filename and line number of a particular JetBrains.ReSharper.Psi.IDeclaredElement?

I want to write a test framework extension for resharper. The docs for this are here: http://confluence.jetbrains.net/display/ReSharper/Test+Framework+Support
One aspect of this is indicating if a particular piece of code is part of a test. The piece of code is represented as a IDeclaredElement.
Is it possible to get the filename and line number of a piece of code represented by a particular IDeclaredElement?
Following up to the response below:
#Evgeny, thanks for the answer, I wonder if you can clarify one point for me.
Suppose the user has this test open in visual studio: https://github.com/fschwiet/DreamNJasmine/blob/master/NJasmine.Tests/SampleTest.cs
Suppose the user right clicks on line 48, the "player.Resume()" expression.
Will the IDeclaredElement tell me specifically they want to run at line 48? Or is it going to give me a IDeclaredElement corresponding to the entire class, and a filename/line number range for the entire class?
I should play with this myself, but I appreciate tapping into what you already know.
Yes.
The "IDeclaredElement" entity is the code symbol (class, method, variable, etc.). It could be loaded from assembly metadata, it could be declared in source code, it could come from source code implicitly.
You can use
var declarations = declaredElement.GetDeclarations()
to get all AST elements which declares it (this could return multiple declarations for partial class, for example)
Then, for any IDeclaration, you can use
var documentRange = declaration.GetDocumentRange()
if (documentRange.IsValid())
Console.WriteLine ("File: {0} Line:{1}",
DocumentManager.GetInstance(declaration.GetSolution()).GetProjectFile(documentRange.Document).Name,
documentRange.Document.GetCoordsByOffset(documentRange.TextRange.StartOffset).Line
);
By the way, which test framework extension are you developing?
Will the IDeclaredElement tell me specifically they want to run at
line 48?
Once more: IDeclaredElement has no positions in the file. Instead, it's declaration have them.
For every declaration (IDeclaration is a regular AST node) there is range in document which covers it. In my previous example, I used TextRange.StartOffset, though you can use TextRange.EndOffset.
If you need more prcise position in the file, please traverse AST tree, and check the coordinates in the document for specific expression/statement

Resources