Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #50578
| Newsgroups | comp.lang.python |
|---|---|
| Date | 2013-07-12 23:43 -0700 |
| Message-ID | <fba5dac7-963f-4c1b-b40b-c0a54d681530@googlegroups.com> (permalink) |
| Subject | help with explaining how to split a list of tuples into parts |
| From | peter@ifoley.id.au |
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.
Thanks,
Peter.
Back to comp.lang.python | Previous | Next — 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