#๐Ÿ”’ Really need some help

113 messages ยท Page 1 of 1 (latest)

polar patio
#

Context:
This for my assigment I am on my last task and I don't really know what to do any help is appericate thanks.

old raftBOT
#

@polar patio

Python help channel opened

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.

polar patio
#

This is task 2 and 3 which I have done and works. Just need help with task 4

torpid kiln
#

Have they told you what a unit test is?

polar patio
torpid kiln
#

Sure.

polar patio
torpid kiln
#

Ok. So have you written any doctests?

polar patio
#

Do you need me to share all my code?

torpid kiln
#

Not yet.

#

So, this style::

#

Have you used the Python REPL? The interactive mode with the >>> prompt?

polar patio
#

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*

torpid kiln
#

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.

torpid kiln
#

Pick typical values, empty values, bad values.

polar patio
torpid kiln
#

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
polar patio
#

This is confusing

torpid kiln
#

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.

polar patio
#

I see

torpid kiln
#

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.

polar patio
#

so like this:

#
    ''' Return the first word of the booklist.

        Test 1. Zero.
        >>> word(0)
        0
    '''
    return booklist```
old raftBOT
#

Hey @polar patio!

Please edit your message to use a code block

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!')```
torpid kiln
#

Yes, except that your doctest would probably be running Title_length, not word.

#

i.e. the doctest should exercise that function.

polar patio
torpid kiln
#

Yes. And the argument to something you would pass to Title_length. And make the output what you would expect to get back.

polar patio
torpid kiln
#

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.

polar patio
torpid kiln
#

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.

polar patio
#

So how does this work how would I call this when test?

torpid kiln
#

So, what's the name of the file containing your functions?

torpid kiln
#

I'm assuming the functions your wrote for the tasks are in a .py file?

polar patio
torpid kiln
#

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.

polar patio
#
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?
torpid kiln
#

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()?

polar patio
#

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?

torpid kiln
#

"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.

polar patio
#

alright, i see. how do I get to the >>> part. I have alot of other code

torpid kiln
#

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.

polar patio
#

I see now

#

makes sense

torpid kiln
#

Well, you can run the REPL just by typing python3 at the command line prompt. (Or py on Windows.)

polar patio
torpid kiln
#

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.

polar patio
#

I run the code and it says Enter your booklist:

#

so I don't know how to get out of that

torpid kiln
#

How do you run your code?

polar patio
#

nvm works my bad

#

File "<python-input-0>", line 1
python3 test.py
^^^^
SyntaxError: invalid syntax

#

how is that invaild

torpid kiln
#

You're at the REPL, with the >>> prompt?

torpid kiln
#

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

polar patio
#

I have quit

torpid kiln
#

Now what does it look like?

polar patio
torpid kiln
#

Ok. Type:

py test.py
polar patio
#

can't open file 'C:\Users\User\test.py': [Errno 2] No such file or directory
PS C:\Users\User>

torpid kiln
#

What folder is test.py in? I'm assuming some subfolder.

polar patio
#

is in my data > uni > IT systems

torpid kiln
#

Ok. So first

cd "data\uni\IT systems"
polar patio
torpid kiln
#

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.

polar patio
#

It doesn't work, should I move the file to just inside the data folder? So I can say cd "data"?

torpid kiln
#

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.

polar patio
#

Ok so is just in my data folder now

torpid kiln
#

Ok cd there, then py test.py

polar patio
torpid kiln
#
cd data
py test.py
polar patio
gentle cedar
#

Just right click on folder and copy path

polar patio
#

is in here

polar patio
old raftBOT
#
Python help channel closed for inactivity

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.