Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #50580
| Path | csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'essentially': 0.04; 'exercise': 0.04; 'subject:help': 0.08; 'bash': 0.09; 'lookup': 0.09; 'method,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:into': 0.09; 'url:github': 0.09; 'python': 0.11; 'question.': 0.14; '(1,': 0.16; '(2,': 0.16; '(8,': 0.16; '16)': 0.16; '31.': 0.16; 'hashed': 0.16; 'length)': 0.16; 'length,': 0.16; 'magic': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'selects': 0.16; 'seperate': 0.16; 'tuple': 0.16; 'two,': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'do.': 0.18; 'library': 0.18; 'trying': 0.19; 'pointed': 0.19; 'split': 0.19; 'command': 0.22; 'header:User-Agent:1': 0.23; 'string,': 0.24; 'script': 0.25; 'values': 0.27; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'wondering': 0.29; 'subject:list': 0.30; 'skip:( 20': 0.30; 'code': 0.31; 'lines': 0.31; '"",': 0.31; 'assert': 0.31; 'extract': 0.31; 'keys': 0.31; 'option.': 0.31; 'tuples': 0.31; 'url:python': 0.33; 'could': 0.34; 'knowledge': 0.35; 'subject:with': 0.35; 'skip:s 30': 0.35; 'something': 0.35; 'one,': 0.35; 'google': 0.35; 'there': 0.35; 'located': 0.36; 'url:org': 0.36; 'should': 0.36; 'two': 0.37; 'list': 0.37; 'list.': 0.37; 'to:addr:python-list': 0.38; 'list,': 0.38; 'anything': 0.39; 'expect': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'how': 0.40; 'skip:u 10': 0.60; 'read': 0.60; 'improved': 0.60; 'results.': 0.60; 'new': 0.61; 'first': 0.61; 'making': 0.63; 'such': 0.63; 'happen': 0.63; 'decided': 0.64; 'more': 0.64; 'here': 0.66; 'lack': 0.78; 'email name:peter': 0.84; 'do:': 0.91 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: help with explaining how to split a list of tuples into parts |
| Date | Sat, 13 Jul 2013 09:28:50 +0200 |
| Organization | None |
| References | <fba5dac7-963f-4c1b-b40b-c0a54d681530@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Gmane-NNTP-Posting-Host | p5084b367.dip0.t-ipconnect.de |
| User-Agent | KNode/4.7.3 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.4672.1373700539.3114.python-list@python.org> (permalink) |
| Lines | 79 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1373700539 news.xs4all.nl 15871 [2001:888:2000:d::a6]:45876 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:50580 |
Show key headers only | View raw
peter@ifoley.id.au wrote:
> Hi List,
>
> I am new to Python and wondering if there is a better python way to do
> something. As a learning exercise I decided to create a python bash
> script to wrap around the Python Crypt library (Version 2.7).
>
> My attempt is located here - https://gist.github.com/pjfoley/5989653
>
> I am trying to wrap my head around list comprehensions, I have read the
> docs at
> http://docs.python.org/2/tutorial/datastructures.html#list-comprehensions
> and read various google results. I think my lack of knowledge is making
> it difficult to know what key word to search on.
>
> Essentially I have this list of tuples
>
> # Tuple == (Hash Method, Salt Length, Magic String, Hashed Password
> # Length)
> supported_hashes=[('crypt',2,'',13), ('md5',8,'$1$',22),
> ('sha256',16,'$5$',43), ('sha512',16,'$6$',86)]
>
> This list contains the valid hash methods that the Crypt Library supports
> plus some lookup values I want to use in the code.
>
> I have managed to work out how to extract a list of just the first value
> of each tuple (line 16) which I use as part of the validation against the
> --hash argparse option.
>
> My Question.
>
> Looking at line 27, This line returns the tuple that mataches the hash
> type the user selects from the command line. Which I then split the
> seperate parts over lines 29 to 31.
>
> I am wondering if there is a more efficient way to do this such that I
> could do:
>
> salt_length, hash_type, expected_password_length = [x for x in
> supported_hashes if x[0] == args.hash]
>
> From my limited understanding the first x is the return value from the
> function which meets the criteria. So could I do something like:
>
> ... = [(x[0][1], x[0][2], x[0][3]) for x in supported_hashes if x[0] ==
> args.hash]
>
> I am happy to be pointed to some documentation which might help clarify
> what I need to do.
>
> Also if there is anything else that could be improved on with the code
> happy to be contacted off list.
Every time when you have to look up something you should think 'dict', and I
expect that pretty that will happen automatically.
Also, to split a tuple into its items you can "unpack" it:
triple = (1, 2, 3)
one, two, three = triple
assert one == 1 and two == 2 and three == 3
So:
supported_hashes = {
"crypt": (2, "", 13),
"md5": (8, "$1$", 22),
...
}
...
parser.add_argument(
'--hash', default='sha512',
choices=supported_hashes, # accept the keys
help='Which Hash function to use')
...
salt_length, hash_type, expected_password_length = supported_hashes[args.hash]
...
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
help with explaining how to split a list of tuples into parts peter@ifoley.id.au - 2013-07-12 23:43 -0700
Re: help with explaining how to split a list of tuples into parts Peter Otten <__peter__@web.de> - 2013-07-13 09:28 +0200
Re: help with explaining how to split a list of tuples into parts peter@ifoley.id.au - 2013-07-13 04:47 -0700
Re: help with explaining how to split a list of tuples into parts Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-13 08:11 +0000
Re: help with explaining how to split a list of tuples into parts peter@ifoley.id.au - 2013-07-13 05:05 -0700
Re: help with explaining how to split a list of tuples into parts Roy Smith <roy@panix.com> - 2013-07-13 10:50 -0400
Re: help with explaining how to split a list of tuples into parts Roy Smith <roy@panix.com> - 2013-07-13 10:33 -0400
csiph-web