<div dir="ltr">2.7 is for bug fixes only. Unless there is a bug to be fixed, I would leave the code as is.<div><br clear="all"><div><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div>Mariatta</div></div></div></div><br></div></div><br><div class="gmail_quote"><div dir="ltr">On Tue, Aug 7, 2018 at 8:14 AM 蔡銘峯 <<a href="mailto:parktasi@gmail.com">parktasi@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hello everybody, <br>I am Park Tsai. I want to refactor __get_builtin_constructor on hasklib.py of python 2.7 (<a href="https://github.com/python/cpython/blob/2.7/Lib/hashlib.py#L72" target="_blank">https://github.com/python/cpython/blob/2.7/Lib/hashlib.py#L72</a>). <br>This is the first time that I try to refactor code of CPython on GitHub, so I am very excited. <br><br>This is __get_builtin_constructor code on hasklib.py ,as follows. <br><br>def __get_builtin_constructor(name):<br> try:<br> if name in ('SHA1', 'sha1'):<br> import _sha<br> return _sha.new<br> elif name in ('MD5', 'md5'):<br> import _md5<br> return _md5.new<br> elif name in ('SHA256', 'sha256', 'SHA224', 'sha224'):<br> import _sha256<br> bs = name[3:]<br> if bs == '256':<br> return _sha256.sha256<br> elif bs == '224':<br> return _sha256.sha224<br> elif name in ('SHA512', 'sha512', 'SHA384', 'sha384'):<br> import _sha512<br> bs = name[3:]<br> if bs == '512':<br> return _sha512.sha512<br> elif bs == '384':<br> return _sha512.sha384<br> except ImportError:<br> pass # no extension module, this hash is unsupported.<br><br> raise ValueError('unsupported hash type ' + name)<br><br><br>When I read this code, it looks messy, so I want to refactor it and make it become more clearly . <br><br>Then, it will be like this <br><br>def get_builtin_constructor(name):<br> try:<br> if name[:3] in ('SHA','sha'):<br> if(name[3:]=='1'):<br> import _sha<br> return _sha.new<br><br> elif (name[3:] == '224'):<br> import _sha256<br> return _sha256.sha224<br><br> elif (name[3:] == '256'):<br> import _sha256<br> return _sha256.sha256<br><br> elif (name[3:] == '384'):<br> import _sha512<br> return _sha512.sha384<br><br> elif (name[3:] == '512'):<br> import _sha512<br> return _sha512.sha512<br> elif name in ('MD5', 'md5'):<br> import _md5<br> return _md5.new<br><br> except ImportError:<br> pass # no extension module, this hash is unsupported.<br><br> raise ValueError('unsupported hash type ' + name)<br><br>I will be grateful for any help you can provide. I really appreciate any feedback you can offer! <br><br>Best regards, <br>Park Tsai !!<br></div>
_______________________________________________<br>
Python-Dev mailing list<br>
<a href="mailto:Python-Dev@python.org" target="_blank">Python-Dev@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-dev" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-dev</a><br>
Unsubscribe: <a href="https://mail.python.org/mailman/options/python-dev/mariatta.wijaya%40gmail.com" rel="noreferrer" target="_blank">https://mail.python.org/mailman/options/python-dev/mariatta.wijaya%40gmail.com</a><br>
</blockquote></div>