Python Forum
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Standard library code
#1
Hi people,

I was just checking out some code from the standard library from python 3.6.1. This specific piece of code is ftplib.py and is meant to replace some piece of a string. From the function I understand the string should be something like "pass yourpasswordhere". Every letter from "yourpasswordhere" should be replaced with an *, pretty clear. Only thing I wonder about is what's the reason for the s[i:] in the and at the last s = statement? Seems to me it has no function? Thank you for reading :)

    # Internal: "sanitize" a string for printing
    def sanitize(self, s):
        if s[:5] in {'pass ', 'PASS '}:
            i = len(s.rstrip('\r\n'))
            s = s[:5] + '*'*(i-5) + s[i:]
        return repr(s)
Reply
#2
The i variable is the length of the string after terminal line feeds and carriage returns are taken out. So the s assignment on line 5 combines three things: the first five characters of the original s, a number of asterisks (*) equal to the length of the original s (not counting the first five characters or any terminal LF/CR), and any terminal LF or CR from the original s. It's a way of preserving those terminal characters, if they exist.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Just an FYI, you might like Doug Helmann's site: https://pymotw.com/3/ (python 3) or https://pymotw.com/2/ (python 2)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python best library for Excel reports & review of existing code MasterOfDestr 4 496 Feb-14-2024, 03:39 PM
Last Post: MasterOfDestr
  python standard way of importing library mg24 1 872 Nov-15-2022, 01:41 AM
Last Post: deanhystad
  Catching a crash within a library code ebolisa 9 3,060 Nov-22-2021, 11:02 AM
Last Post: bowlofred
  How do I open the Source code of a library? JaneTan 1 2,226 Aug-18-2021, 02:12 AM
Last Post: Larz60+
  Winsorized Mean and Standard Deviation Wheeliam 0 1,791 Jul-11-2020, 05:27 PM
Last Post: Wheeliam
  standard library modules chpyel 4 2,761 May-10-2020, 02:58 PM
Last Post: snippsat
  Is there a standard for autocommit In PEP 249 zatlas1 10 5,104 Feb-06-2019, 04:56 PM
Last Post: buran
  Graphics and standard deviation rocioaraneda 3 2,671 Jan-09-2019, 10:53 PM
Last Post: micseydel
  standard data types rombertus 3 51,423 Dec-23-2018, 08:52 PM
Last Post: rombertus
  Join the Python Standard Library to my project sylas 1 2,164 May-16-2018, 05:59 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020