Python Forum
Can I replace IF statements with a key lookup ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can I replace IF statements with a key lookup ?
#1
If I have code like the following ( from https://python-forum.io/Thread-Convert-e...VCF-format ):

    name = ""
    if 'cn' in record:
        name = record['cn'][0]
    
    surname = ""
    if 'sn' in record:
        surname = record['sn'][0]
    
    given_name = ""
    if 'givenName' in record:
        given_name = record['givenName'][0]
    
    display_name = ""
    if 'displayName' in record:
        display_name = record['displayName'][0]
    
    email = ""
    if 'mail' in record:
        email = record['mail'][0]
and the input data is from an LDIF formatted file, and the record is coming from a dictionary/class as such

Quote:OrderedDict([('objectClass', ['inetOrgPerson']), ('cn', ['Forrest Gump']), ('sn', ['Gump']), ('displayName', ['Forrest Gump']), ('mail', ['[email protected]'])])

can I use some sort of key lookup as explained at Python: How Key lookup works in Dictionary and how to customize it? ??

simply to replace all the ugly IF statements with a simple key lookup ??
Reply
#2
You could try
name = record.get('cn', ('',))[0]
Or better, make a function
def extract(record, key, default=''):
    return record.get(key, (default,))[0]

name = extract(record, 'cn')
surname = extract(record, 'sn')
nilamo and jehoshua like this post
Reply
#3
(Mar-05-2021, 10:15 PM)Gribouillis Wrote: You could try
name = record.get('cn', ('',))[0]
Or better, make a function
def extract(record, key, default=''):
    return record.get(key, (default,))[0]

name = extract(record, 'cn')
surname = extract(record, 'sn')

Thanks, that's awesome, I just tried the first piece of code and it works just fine. Will try the other part now as a function.
nilamo likes this post
Reply
#4
and using it as a function works perfectly also, thanks a lot @Gribouillis
nilamo likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  2-dataframe, datetime lookup problem Mark17 0 1,214 Jan-27-2022, 01:02 AM
Last Post: Mark17
  Python VLookup? Lookup Table? Nu2Python 3 2,371 Oct-25-2021, 08:47 PM
Last Post: Nu2Python
  python 3 dns lookup private domain didact 1 2,501 Sep-19-2020, 06:01 PM
Last Post: bowlofred
  Partial key lookup in dictionary GaryNR 1 3,383 Jul-16-2020, 06:55 PM
Last Post: Gribouillis
  Encoding and mac-vendor-lookup library tuanjggaa 1 2,663 Mar-27-2020, 03:12 PM
Last Post: deanhystad
  Excel Lookup riteshprakash 0 1,742 Sep-11-2019, 12:43 PM
Last Post: riteshprakash
  fast lookup for array markB 3 4,002 May-13-2019, 12:11 AM
Last Post: scidam
  lookup tables Skaperen 4 3,181 Aug-13-2018, 06:43 AM
Last Post: Gribouillis
  Search & Replace - Newlines Added After Replace dj99 3 3,354 Jul-22-2018, 01:42 PM
Last Post: buran
  Lookup tables parrytoss 0 2,480 Feb-07-2018, 08:45 AM
Last Post: parrytoss

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020