#๐ Really need some help
113 messages ยท Page 1 of 1 (latest)
@polar patio
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Have they told you what a unit test is?
Yes I have some photos should I send them?
Sure.
Ok. So have you written any doctests?
No
Do you need me to share all my code?
Not yet.
So, this style::
Have you used the Python REPL? The interactive mode with the >>> prompt?
Yes, but I don
don't understand the question do I have to write some code in my main program? Then the marker goes in there?
in the repl*
These doctests are snippets from the REPL. You can make them by running code in the REPL, then copy/paste the bits you want into the docstring. Usually the docstring of the function you're testing, sometimes elsewhere.
Doctests are tests embedded in the function documentation.
For example, like this:
def square(n):
''' Return the square of n.
Test 1. Zero.
>>> square(0)
0
'''
return n * n
That long triple quoted string under the function header is a "docstring". The >>> square(0) and the 0 underneath it is a doctest, which you're using as a unit test.
So suppose your code's in a file called exercise1.py. Open the repl and go:
>>> from exercise1 import func1
where func1 is the function you want to make some tests for. Adjust exercise1 to match your filename and func1 to match one of your functions from the tasks.
Then test your function with some value:
>> func1("foo")
That should produce some output.
The >>> func1("foo") line and the output can then be copy/pastded into your function doc string like the example above.
Call func1 (well, your task function) with various values.
So I put this in my code?
Pick typical values, empty values, bad values.
and then do this in the tester?
Yes.
Here's an example of one of my own functions with a docstring contaoining a doctest:
def cutprefix(s, prefix):
''' Strip a `prefix` from the front of `s`.
Return the suffix if `s.startswith(prefix)`, else `s`.
Example:
>>> abc_def = 'abc.def'
>>> cutprefix(abc_def, 'abc.')
'def'
>>> cutprefix(abc_def, 'zzz.')
'abc.def'
>>> cutprefix(abc_def, '.zzz') is abc_def
True
'''
if prefix and s.startswith(prefix):
return s[len(prefix):]
return s
This is confusing
I literally copy/pasted these out of the REPL, exercising that function.
We give functions docstrings to describe them. The Python help() function knows about these. Look:
>>> from cs.lex import cutprefix
>>> help(cutprefix)
Help on function cutprefix in module cs.lex:
cutprefix(s, prefix)
Strip a `prefix` from the front of `s`.
Return the suffix if `s.startswith(prefix)`, else `s`.
Example:
>>> abc_def = 'abc.def'
>>> cutprefix(abc_def, 'abc.')
'def'
>>> cutprefix(abc_def, 'zzz.')
'abc.def'
>>> cutprefix(abc_def, '.zzz') is abc_def
True
That help information literally comes from the docstring.
I see
The doctest module knows how to read the docstrings from a module (your script). It looks for little bits of stuff looking like the REPL (the >>> prompt) and runs the statement after the >>> prompt and compares the result with what's in the docstring.
In the example above, It will run abc_def = 'abc.def' and expect no output (it's just an assignment statement).
Then it will run cutprefix(abc_def, 'abc.') and expect the output:
'def'
and so on.
so like this:
''' Return the first word of the booklist.
Test 1. Zero.
>>> word(0)
0
'''
return booklist```
Hey @polar patio!
Make sure you put your code on a new line following py. There must not be any spaces after py.
Here is an example of how it should look:
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
Yes, except that your doctest would probably be running Title_length, not word.
i.e. the doctest should exercise that function.
So i should change word to title_length?
Yes. And the argument to something you would pass to Title_length. And make the output what you would expect to get back.
I am so confused though, cause idk what I will get back as the user puts in there booklist.
What are you supposed to get back. Remember, you get to supply what "the user" would give.
So it looks like you're meant to get back the first word of what you're given. So you might expect this:
>>> Title_length("one two three")
'one'
So you could put that as a doctest.
like so? ```py
def Title_length(booklist):
''' Return the first word of the booklist.
Test 1. Zero.
Title_length("one two three")
'one'
'''
return booklist```
Yes, but as:
def Title_length(booklist):
''' Return the first word of the booklist.
Test 1. Zero.
>>> Title_length("one two three")
'one'
'''
return booklist
So the function to run after a >>> prompt. And the output you expect underneath.
And of course you'd say Test 1. First of three words. or some other suitable description of the test.
The >>> in the docstring is what doctest uses to fund each test.
So how does this work how would I call this when test?
File?
I'm assuming the functions your wrote for the tasks are in a .py file?
oh like on the computer
is called test.py
Ok.
When you run that file, Python defined a variable called __name__ which is the name of the module. Your file is a module.
If you're in the REPL and you import your file:
>>> import test
then inside test.py the variable __name__ would be the string "test".
However, when you run your file as the main programme, like this at the command line prompt:
python3 test.py
Python sets __file__ to the special string "__main__" instead.
So you can tell if you're being "imported" or if you're the main program.
By putting the boilerplate stuff above at the bottom of your file:
if __name__ == '__main__':
from doctest import testmod
testmod(verbose=True)
then when you import it the if fails, and does not run the tests.
def Title_length(booklist):
''' Return the first word of the booklist.
Test 1. Zero.
>>> Title_length("one two three")
'one'
'''
return booklist
if __test__ == "__file__":
interact()
#-----Main Program to Run Student's Solution-------------------------#
#You must NOT change any of the code in this section.
if __name__ == "__main__":
interact()
``` like so?
But if you run it from the command line:
python3 test.py
the stuff in the if does run. Running the doctests.
Why call interact()?
I confused, just for more context should have said but this is like my 6 week into python and don't know tons of the words you saying. Like what is a boiler pot?
"boiler plate" is a term meaning a standard incantation we all use. The if __name__ == '__main__': test is what we all use to decide if our script/mpdule is being run as the main programme or being imported by something else.
alright, i see. how do I get to the >>> part. I have alot of other code
See :Boilerplate code" here: https://en.wikipedia.org/wiki/Boilerplate_text
Boilerplate text, or simply boilerplate, is any written text (copy) that can be reused in new contexts or applications without significant changes to the original. The term is used about statements, contracts, and source code, and is often used pejoratively to refer to clichรฉd or unoriginal writing.
Well, you can run the REPL just by typing python3 at the command line prompt. (Or py on Windows.)
but the promot asks me other questions before that
Like this:
[~]fleet2*> python3
Python 3.12.7 (v3.12.7:0b05ead877f, Sep 30 2024, 23:18:00) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>>
[~]fleet2*> is the command prompt on my machine here. Yours will be different.
I run the code and it says Enter your booklist:
so I don't know how to get out of that
How do you run your code?
nvm works my bad
So now i say python3 test.py
File "<python-input-0>", line 1
python3 test.py
^^^^
SyntaxError: invalid syntax
how is that invaild
You're at the REPL, with the >>> prompt?
python3 test.py is for the command line, not the REPL. The REPL expects Python code, hence the error.
Exit the REPL by typing quit
I have quit
Now what does it look like?
Ok. Type:
py test.py
can't open file 'C:\Users\User\test.py': [Errno 2] No such file or directory
PS C:\Users\User>
It is saved as test.py
What folder is test.py in? I'm assuming some subfolder.
yea
is in my data > uni > IT systems
Ok. So first
cd "data\uni\IT systems"
what does that do?
It changes your "working directory".
When you open a file with a relative name like test.py it is looked for in the working directory.
It doesn't work, should I move the file to just inside the data folder? So I can say cd "data"?
Maybe rename IT systems to not contain a space. Or change VSCode's working directory to be where your code is (the IT systems folder).
You can type a name with space to windows, but I'm not sure how. I'm not a windows person.
Ok so is just in my data folder now
Ok cd there, then py test.py
cd data
py test.py
Is the data folder on your desk
Just right click on folder and copy path
is in here
no folders attached to it
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.