[Tutor] if you're interested in the code thus far...

Clayton Kirkwood crk at godblessthe.us
Mon Oct 27 04:17:34 CET 2014



!-----Original Message-----
!From: Tutor [mailto:tutor-bounces+crk=godblessthe.us at python.org] On
!Behalf Of Alan Gauld
!Sent: Sunday, October 26, 2014 5:34 PM
!To: tutor at python.org
!Subject: Re: [Tutor] if you're interested in the code thus far...
!
!On 26/10/14 22:12, Clayton Kirkwood wrote:
!
!> !On 25/10/14 23:46, Clayton Kirkwood wrote:
!> !> __author__ = 'SYSTEM'
!> !
!> !You are still setting __author__ which is a bit suspect.
!> !Leave double underscores to python.
!>
!> This is something being created when I started with this file.
!
!How are you creating your code? Are you using some kind of IDE?
!

I snarfed it up from somewhere. Right now, it's where it's easy to access
and I don't have to worry about it. The code was instructional and it gives
me something to refer to and be real proud of:<}}

!> !> raw_table = ('''
!> !
!> !> a: Ask    y: Dividend Yield
!> !> b: Bid     d: Dividend per Share
!> !> b2: Ask (Realtime)           r1: Dividend Pay Date
!> !> b3: Bid (Realtime)            q: Ex-Dividend Date
!> !...
!> !> s7: Short Ratio
!> !> ''')
!> !> col_position, code, description = 0, [], [] key_name = !>
!> raw_table.replace('\t','\n') !
!> !I assume this is because you don't control the file format? Since
!> !otherwise you would just use newlines in the file, right?
!>
!> Correct. I cut and pasted the data. Not going to change (probably) no
!> sense in putting it in a file
!
!Are you saying the data will stay with your code? If that's the case
!then reformat the text into a Python data structure and avoid all the
!parsing that makes up about half your code.
!
!> !> for each_line in  key_name.splitlines():
!> !>      if ':' in each_line:
!> !>          c, d = each_line.split(':')
!> !>          code.append(c)
!> !>          description.append(d.strip())
!> !>          print( col_position, code[col_position],
!> !description[col_position])
!> !>          col_position += 1
!> !
!> !You could use enumerate in the for loop and that would set the
!> !col_position value for you:
!> !
!...
!>
!> However, I don't think that this solution will work for me, because
!> there are lines with no :
!
!Ah yes, I didn't notice you only increment the counter inside the if.
!
!> !> output_line_len = 120
!> !> current_output_pos = index = 0
!> !> description_output_string = code_output_string = ''
!> !> for description_position, code_position in zip(description, code):
!> !
!> !Why not just put the codes and descriptions in tuples when you read
!> them !in the loop above? Why use zip? In other words where you do !
!> ! >          c, d = each_line.split(':')
!> ! >          code.append(c)
!> ! >          description.append(d.strip())
!> !
!> !Why not just join the pair there:
!> !
!> ! >          c, d = each_line.split(':')
!> !            values.append((c,d))
!> !
!> !or even just
!> !
!> ! >          values.append(each_line.split(':'))
!> !
!>
!> Uh, because I didn't know about it....:<)))
!>
!> !It seems as if you are splitting the values into two lists only to
!> zip !those lists together again in the next loop?
!> !
!...
!>
!> This has led to a problem, however. How do I determine if a value is
!> in one of the tuples:
!> Key in [a, word], [key, word]
!
!I'm not sure exactly what you mean, I'd probably need to see how you
!implemented it but, using my example above, you can do
!
!key in values[index]
!
!So if values looks like:
!
!values = [(1,2),(2,3),....]
!
!then the test
!
!if 2 in values[0]:  will be true
!
!and
!
!if 2 in values[1]:  will also be true.
!
!If you need to test the specific tuple item you could do:
!
!if 2 == values[0][0]:  will be false because you only test 1st item
!
!and
!
!if 2 == values[0][1]:  will be true coz you test the 2nd item.
!
!If you wanted to extract all the tuples containing 2 you could use a
!comprehension like:
!
!value2 = [tup for tup in values if 2 in tup]
!

get_new_list = True
print(values) 			#[('a', 'Ask'), ('y', 'Dividend Yield')]
while get_new_list:
    key_list = input('Please enter space separated keys in the order you
want: ').split()
    print(key_list)		#['a', 'y']
    for key in key_list:
        print(key)		#a
        if key not in values[0]:	#I am trying to ensure that 'a' is
in the first 'column' of values
            print("Error:", key, "not available, start again")
            get_new_list = True
            break
    else: get_new_list = False

I don't know how to check if 'key' is in the first position of values. Is it
straightforward to check each loop, or should I pull the first part of the
tuple out before the loop?

Clayton

 
!
!HTH
!--
!Alan G
!Author of the Learn to Program web site
!http://www.alan-g.me.uk/
!http://www.flickr.com/photos/alangauldphotos
!
!_______________________________________________
!Tutor maillist  -  Tutor at python.org
!To unsubscribe or change subscription options:
!https://mail.python.org/mailman/listinfo/tutor





More information about the Tutor mailing list