[Tutor] strip and split?

eryksun eryksun at gmail.com
Sun Dec 1 00:14:31 CET 2013


On Sat, Nov 30, 2013 at 1:40 PM, richard kappler <richkappler at gmail.com> wrote:
>
>
> disk usage(total=302264549376, used=73844322304, free=213066088448,
> percent=24.4)
>
> So if I want only the number following percent, I get that I need to convert
> this to a built in type and do some split and strip, but that's where I'm
> floundering. Might I get a little guidance on this please?

disk_usage returns a subclass of tuple, created by
collections.namedtuple. The subclass adds a property for each tuple
item. This helps code to be more self-documenting without sacrificing
the efficiency of tuples.

    usage = collections.namedtuple(
        'usage', 'total used free percent')

    >>> usage.__base__
    <type 'tuple'>

    >>> type(usage.percent)
    <type 'property'>

The property fget for each tuple item is an operator.itemgetter instance:

    >>> type(usage.percent.fget)
    <type 'operator.itemgetter'>

For example:

    u = usage(302264549376, 73844322304, 213066088448, 24.4)
    fget = operator.itemgetter(3)

    >>> u.percent
    24.4
    >>> fget(u)
    24.4


More information about the Tutor mailing list