赞
踩
Leave your swimclub.py code open in VS Code (if you like), then open another new notebook, and call it Files.ipynb.
You already know how Python’s import
statement works with the PSL.
It turns out import
can also import your custom modules. And, guess what? The swimclub.py file is a Python module, so you can use import
on it, as shown below:
import swimclub
tips: restart the module cache
Q: A new folder called __pycache__ just appeared inside my Learning folder. What is that and where did it come from?
A: That folder is used internally by the Python interpreter to save cached compiled copies of any modules you create and then import. Although you don’t have to compile your Python code to run it, behind the scenes Python converts your code to an internal bytecode, which is then executed. As this process can sometimes be expensive when importing modules, the interpreter caches a copy of the compiled bytecode in the __pycache__ folder during the import process. The next time you import your module (in a new session), the interpreter checks your module’s timestamp against the timestamp of the cached bytecode and, if they are the same, reuses the bytecode. Otherwise, the code to bytecode process starts all over again. You can safely ignore any files in the __pycache__ folder and leave everything to the interpreter to manage (although you might want to exclude the folder from your Git repo).
- swim_files=os.listdir(swimclub.FOLDER)
- len(swim_files)
output = 61
there are 60 files in your folder, however on your mac, there are 61 items.
So how to debug?
1. print(swim_files)
print dir
print(sorted(swim_files))
The sort method performs it’s reordering “in-place,” which means the new order overwrites (!!) what was previously in the list. The old list ordering is lost forever… and there’s no undo.
Yes, that’s a potential issue.
As the swimdata.zip file was initially created on a Mac, the .DS_Store file was automatically added to the ZIP archive. This type of OS-specific issue is often a concern.
Q: If I unzip swimdata.zip on something other than a Mac, will I still see that Mac-specific .DS_Store?
A: Unfortunately, yes. The ZIP archive was created on an Apple device, so the .DS_Store file is going to be there, unless whomever created the archive instructed their zip tool to exclude the offending file (which hasn’t happened in this case).
An incorrect assumption is the problem.
Your code, as written, assumes every swim time conforms to the mins:secs.hundredths format, but this is clearly not the case with Abi’s 50m swim times, and this is why you’re getting that ValueError
.
Here’s the general structure of a simple if statement:
The def keyword defines a new, bespoke function.
When you put code in it’s own file (with a .py extension), you create a module.
The import statements lets you reuse a module, e.g., import swimclub
.
Use a fully qualified name to invoke a function from a module, e.g., swimclub.read_swim_data
.
The return statement allows a bespoke function to return a result.
If a function tries to return more than one result, the collection of returned values are bundled together as a single tuple. This is due to the fact that Python functions only ever return a single result.
A tuple is an immutable sequence data structure. Once a tuple is assigned values, the tuple cannot change.
Lists are like tuples, expect for the fact that lists are mutable.
The os
module (included as part of the PSL) lets your Python code talk to your underlying operating system.
Although lists come with a handy sort method, be careful using it as the ordering is applied in-place. If you want to keep any list’s current order, use the sorted BIF instead (which always returns a sorted copy of your data).
Lists come built in with lots of methods (not just sort), including the useful remove method.
The in operator is one of our favorites, and should be one of yours, too. It’s great at searching (aka checking for membership).
When you need to make a decision, nothing beats the if else combo.
An often overlooked, but truly wonderful, BIF is enumerate. It can be used to number the iterations of any for loop.
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。