Can't do a multiline assignment!

s0suk3 at gmail.com s0suk3 at gmail.com
Thu Apr 17 12:56:24 EDT 2008


On Apr 17, 11:44 am, Gary Herron <gher... at islandtraining.com> wrote:

> But.  *What's the point* of doing it this way.    I see 14 variables
> being assigned a value, but I don't see the value, they are getting.
> Reading this bit if code provides no useful information unless I'm
> willing to scan down the file until I find the end of this mess.  And in
> that scanning I have to make sure I don't miss the one single line that
> does not end in a backslash.    (Your ellipsis conveniently left out the
> *one* important line needed to understand what this code is doing,  but
> even if you had included it, I'd have to scan *all* lines to understand
> what a single value is being assigned.
>
> There is *no way* you can argue that code is clearer than this:
>
>     # General header fields
>     Cache_Control               = None
>     Connection                  = None
>     Date                        = None
>     Pragma                      = None

class RequestHeadersManager:

    # General header fields
    Cache_Control               = \
    Connection                  = \
    Date                        = \
    Pragma                      = \
    Trailer                     = \
    Transfer_Encoding           = \
    Upgrade                     = \
    Via                         = \
    Warning                     = \

    # Request header fields
    Accept                      = \
    Accept_Charset              = \
    Accept_Encoding             = \
    Accept_Language             = \
    Authorization               = \
    Expect                      = \
    From                        = \
    Host                        = \
    If_Match                    = \
    If_Modified_Since           = \
    If_None_Match               = \
    If_Range                    = \
    If_Unmodified_Since         = \
    Max_Forwards                = \
    Proxy_Authorization         = \
    Range                       = \
    Referer                     = \
    TE                          = \
    User_Agent                  = \

    # Entity header fields
    Allow                       = \
    Content_Encoding            = \
    Content_Language            = \
    Content_Length              = \
    Content_Location            = \
    Content_MD5                 = \
    Content_Range               = \
    Content_Type                = \
    Expires                     = \
    Last_Modified               = \

        None

    def __init__(self, headers, linesep):
        headersDict = parse_headers(headers, linesep)

        for header in headersDict.keys():
            charsLength = len(header)

            if charsLength == 10:
                if header == "connection":
                    self.Connection = headersDict["connection"]
                elif header == "user-agent":
                    self.User_Agent = headersDict["user-agent"]

            elif charsLength == 15:
                if header == "accept-encoding":
                    self.Accept_Encoding = headersDict["accept-
encoding"]
                elif header == "accept-language":
                    self.Accept_Language = headersDict["accept-
language"]

            elif charsLength == 17:
                if header == "if-modified-since":
                    self.If_Modified_Since = headersDict["if-modified-
since"]
                elif header == "transfer-encoding":
                    self.Transfer_Encoding = headersDict["transfer-
encoding"]

            elif charsLength == 2:
                if header == "te":
                    self.TE = headersDict["te"]

            elif charsLength == 3:
                if header == "via":
                    self.Via = headersDict["via"]

            elif charsLength == 4:
                if header == "date":
                    self.Date = headersDict["date"]
                elif header == "host":
                    self.Host = headersDict["host"]
                elif header == "from":
                    self.From = headersDict["from"]

            elif charsLength == 5:
                if header == "allow":
                    self.Allow = headersDict["allow"]
                elif header == "range":
                    self.Range = headersDict["range"]

            elif charsLength == 6:
                if header == "accept":
                    self.Accept = headersDict["accept"]
                elif header == "expect":
                    self.Expect = headersDict["expect"]
                elif header == "pragma":
                    self.Pragma = headersDict["pragma"]

            elif charsLength == 7:
                if header == "expires":
                    self.Expires = headersDict["expires"]
                elif header == "referer":
                    self.Referer = headersDict["referer"]
                elif header == "trailer":
                    self.Trailer = headersDict["trailer"]
                elif header == "upgrade":
                    self.Upgrade = headersDict["upgrade"]
                elif header == "warning":
                    self.Warning = headersDict["warning"]

            elif charsLength == 8:
                if header == "if-match":
                    self.If_Match = headersDict["if-match"]
                elif header == "if-range":
                    self.If_Range = headersDict["if-range"]

            elif charsLength == 11:
                if header == "content-md5":
                    self.Content_MD5 = headersDict["content-md5"]

            elif charsLength == 12:
                if header == "content-type":
                    self.Content_Type = headersDict["content-type"]
                elif header == "max-forwards":
                    self.Max_Forwards = headersDict["max-forwards"]

            elif charsLength == 13:
                if header == "authorization":
                    self.Authorization = headersDict["authorization"]
                elif header == "cache-control":
                    self.Cache_Control = headersDict["cache-control"]
                elif header == "content-range":
                    self.Content_Range = headersDict["content-range"]
                elif header == "if-none-match":
                    self.If_None_Match = headersDict["if-none-match"]
                elif header == "last-modified":
                    self.Last_Modified = headersDict["last-modified"]

            elif charsLength == 14:
                if header == "accept-charset":
                    self.Accept_Charset = headersDict["accept-
charset"]
                elif header == "content-length":
                    self.Content_Length = headersDict["content-
length"]

            elif charsLength == 16:
                if header == "content-encoding":
                    self.Content_Encoding = headersDict["content-
encoding"]
                elif header == "content-language":
                    self.Content_Language = headersDict["content-
language"]
                elif header == "content-location":
                    self.Content_Location = headersDict["content-
location"]

            elif charsLength == 19:
                if header == "if-unmodified-since":
                    self.If_Unmodified_Since = headersDict["if-
unmodified-since"]
                elif header == "proxy-authorization":
                    self.Proxy_Authorization = headersDict["proxy-
authorization"]

There! That's the whole code. I guess the way you suggest is simpler
and a bit more intuitive, but I was figuring that the way I suggested
it is more stylish.



More information about the Python-list mailing list