3.4 - Diving Into enum & pathlib
00:00
The previous lesson went from Python 3.1 through 3.3 and talked about the yield from
statement as well as the unittest.mock
module.
00:09
This lesson covers Python 3.4, the Enum
class, and the pathlib
module.
00:16
So Python 3.4. This is 2014. Again, that five-year cycle, asyncio
is beginning to be added to Python here, and along with that, Enum
, which we’ll have some demonstrations of here shortly, and a lot of people’s favorite feature I think added at this point was pathlib
, and I definitely have to agree with that.
00:38
Okay, let’s head into the REPL and play with both Enum
and pathlib
. Sometimes when you declare a constant, it needs to be part of a group of things.
00:47
This often happens when you want to give the user one of several choices. A good solution for that is the Enum
class.
00:56
To create an enum
, you inherit from Enum
in Enum
, yeah. Without the visual on the screen here, I could see how that sentence might get confusing.
01:07
Say I want to allow my user to choose from one of three different animals. I declare a class and inherit from capital E Enum
. Then inside of it, I create class-level attributes for the constants that I want.
01:21 By convention, constants are in all caps, another animal and a third. That last one’s a questionable choice for a house pet. But hey, maybe you’re after your own Netflix special.
01:34 Once you’ve got it, you instantiate it like any other object.
01:39
If you pass in the constant for DOG
, then you get an instance of the dog. Each instance object has a .value
attribute, so you can access the constant itself
01:51
and a .name
attribute to get at the name of the constant. In fact, you can create instances with those very same names. If I directly reference the class attribute, I get a new instance object representing the cat.
02:07 This means you can go back and forth from one form to the other depending on your need in the code. So this is a nice addition for having constants be like a collection of constants and being able to kind of work through them.
02:20 This is something that’s included in lots of other languages, so this was nice for Python to add it. Yeah, this actually took a very long time considering. Python tends to be that language that makes it a little easier to do these kinds of common things.
02:34
It was actually kind of surprising that we got all the way to 3.4 before enum
popped up to be. Yeah, there were third-party libraries out there to do these things, so I think that was why it sort of took a while.
02:45
They sort of went, oh, well, if you needed to use a library, yeah, but this is a fundamental type in some languages, right? So even how Python did it, it’s a very Pythonic way, but even how they did it, other languages don’t put dictionaries a first-class citizen, but do put Enum
.
02:59
So it’s kind of, yeah, pick what you want. Yeah. Alright. Onto pathlib
. Pathlib
library is for handling file paths and can be used instead of the older os.path
module.
03:12
I found this module late myself. It had been around for three or four versions before I found out about it, but once I did, I’ve never gone back. The main class in pathlib
is Path
.
03:25
You can instantiate a Path
object by passing a string for a file name, numbers.txt
being my file here. There are a bunch of useful methods on it.
03:39 Is it a file? Paths don’t have to correspond to a file. You might use a path to create a file. So this method tells you whether or not your path object corresponds to a thing on the disk as well.
03:54 And in contrast to is this a file? This method tells you if the path is a directory. If you’re iterating over a bunch of things from the file system, you might take different actions based on whether it’s a file or a directory.
04:05
For example, read the file but descend into the directory to keep searching for more files. I won’t cover them here, but there are actually helper methods in pathlib
to do this as well.
04:17
The .resolve()
method returns a new path object containing a fully qualified file name. Up until now, I’ve been using a locally referenced file in my current directory.
04:26
That can sometimes be problematic if you’re passing the objects around as the current directory can change. Calling .resolve()
fixes that problem.
04:37
The .parent
attribute on a path gives you the directory above this object.
04:43
So it shouldn’t come as a surprise that the directory holding numbers.txt
is in fact a directory. Why did I do that? Well, to show you one of my favorite features of this library:
04:57
pathlib
overloads the division operator and uses it to join paths. Normally, I’m not a big fan of operator overloading, but this means the code reads just like a Unix file path.
05:08 Sorry to those Windows folks out there; blame Microsoft’s original DOS for trying to look different from the OS they were copying.
05:16
Seeing as this second
is the parent of numbers.txt
, when you join it with numbers.txt
, it makes sense that it’s equal to the original path object.
05:26
All in all, I find the pathlib
module easier to read and deal with than os.path
, and it has all the same methods. If you’re switching over to it, the Python documentation page even has a handy chart at the bottom telling you how to map the old ways to the new ones.
05:40
Spoiler alert: this and f-strings would be the two things that cause me to go, I must move to 3. Yeah, I would say, yeah, it cleaned up some of the code and I’m still finding with some of the older libraries that I maintain, I was like, oh, there’s still an os.path
in here.
05:54
I should fix that. So yeah, yeah. Seeing some early tutorials at Real Python trying to explain os.path
and then seeing ones that moved to pathlib
, I was like, oh, there’s no reason I should learn this older method.
06:09
I mean, I could look it up if I needed to, you know, again, maintaining older code or something like that. But pathlib
is a rather elegant way of dealing with paths.
06:17
Yeah, yeah. Particularly with joining them, right? So there’s a Path
method called .join()
. For each argument you pass in, it tries to join them all together.
06:25 So if you’re creating a long path with a bunch of different parts in it, you end up having like five or six arguments to it, and it just becomes this long line of text on your code that’s hard to read, whereas I find this is much cleaner.
06:40
If you’re interested in enum
, you can see all of the features in this core tutorial. Well, this set is the one I wish I’d come across earlier.
06:47
To learn more about pathlib
, concurrency, and async features are a really big topic. This tutorial can get you started if you want to get into parallel code.
06:59 Next up, Python 3.6, which contains f-strings and the ability to distinguish thousands separators in numerics through underscores.
Become a Member to join the conversation.