Gentoo Archives: gentoo-portage-dev

From: Tom Wijsman <TomWij@g.o>
To: wking@×××××××.us
Cc: gentoo-portage-dev@l.g.o
Subject: Re: [gentoo-portage-dev] [PATCH 2/3] pym/portage/package/ebuild/fetch.py: Factor out _get_fetch_resume_size
Date: Mon, 20 Jan 2014 01:42:49
Message-Id: 20140120024141.258009e4@TOMWIJ-GENTOO
In Reply to: [gentoo-portage-dev] [PATCH 2/3] pym/portage/package/ebuild/fetch.py: Factor out _get_fetch_resume_size by "W. Trevor King"
1 On Sat, 18 Jan 2014 19:07:46 -0800
2 "W. Trevor King" <wking@×××××××.us> wrote:
3
4 > +def _get_fetch_resume_size(settings, default='350K'):
5 > + v = settings.get("PORTAGE_FETCH_RESUME_MIN_SIZE")
6 > + if v is not None:
7 > + v = "".join(v.split())
8 > + if not v:
9 > + # If it's empty, silently use the default.
10 > + v = default
11 > + match = _fetch_resume_size_re.match(v)
12 > + if (match is None or
13 > + match.group(2).upper() not in
14 > _size_suffix_map):
15 > + writemsg(_("!!! Variable
16 > PORTAGE_FETCH_RESUME_MIN_SIZE"
17 > + " contains an unrecognized format:
18 > '%s'\n") % \
19 > +
20 > settings["PORTAGE_FETCH_RESUME_MIN_SIZE"],
21 > + noiselevel=-1)
22 > + writemsg(_("!!! Using
23 > PORTAGE_FETCH_RESUME_MIN_SIZE "
24 > + "default value: %s\n") % default,
25 > + noiselevel=-1)
26 > + v = None
27 > + if v is None:
28 > + v = default
29 > + match = _fetch_resume_size_re.match(v)
30 > + v = int(match.group(1)) * \
31 > + 2 ** _size_suffix_map[match.group(2).upper()]
32 > + return v
33
34 There is some duplicate code here, I think the conditions can be
35 rewritten in such way that the duplicate code doesn't take place.
36
37 Move everything from the first 'if' except for the first line out
38 of that 'if', that way get a bare:
39
40 if v is not None:
41 v = "".join(v.split())
42
43 Outside that, you now have 'if not v:' whereas you later have
44 'if v is None:'; you can optimize this to 'if not v or v is None:'
45 (maybe that can be optimized further, dunno) and just have one
46 condition set the default here. This gives you:
47
48 if not v or v is None:
49 # If it's empty, silently use the default.
50 v = default
51
52 Afterwards, you have 'match = _fetch_resume_size_re.match(v)' in both
53 places, you can again just have this once.
54
55 Can the two remaining code blocks just be placed after that?
56
57 --
58 With kind regards,
59
60 Tom Wijsman (TomWij)
61 Gentoo Developer
62
63 E-mail address : TomWij@g.o
64 GPG Public Key : 6D34E57D
65 GPG Fingerprint : C165 AF18 AB4C 400B C3D2 ABF0 95B2 1FCD 6D34 E57D

Attachments

File name MIME type
signature.asc application/pgp-signature

Replies