读书笔记【头先Python】4. List of Files: Functions, Modules & Files

How to create a function in Python

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 

Functions return a tuple when required

  • tuple is an immutable sequence.

THERE ARE NO DUMB QUESTIONS 1

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)

What can you do to lists?

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.

THERE ARE NO DUMB QUESTIONS 2

QIf 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:

Image

 

Did you end up with 60 processed files?

小结 BULLET POINTS

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

  • 17
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值