0

I have the following code where I try to set "build_info_match" to True only when all the build locations present in "engg_buildinfo" match with "prod_buildinfo",currently I believe I am hitting the error because prod_buildinfo is a string instead of dictionary,how do get the corresponding build location from prod_buildinfo?

CODE:-

prod_buildinfo = """|h6. {color:blue} Meta Build{color}|{color:blue}MSM1234.LA.1.9{color}|{color:blue}\\flower\location721\INTEGRATION\CI_MSM1234.LA.1.9-116391-STD.INT-2{color}|
|h6. {color:green}MSM1234{color}|
|h6. GLUE|GLUE.MSM1234.2.0|\\bigelow\zipbuild261\INTEGRATION\GLUE.MSM1234.2.0-00010.1-NOOP_TEST-1|
|h6. PASS|LA.HB.1.3.9|\\flower\location674\INTEGRATION\CI_LA.HB.1.3.9-19602-8x96.1-5|
|h6. MOD|MPSS.TH.2.0.2|\\flower\location678\INTEGRATION\MPSS.TH.2.0.2-00089-M1234FAAAANAAM-1|
|h6. BOOT|BOOT.XF.1.0|\\flower\location675\INTEGRATION\BOOT.XF.1.0-00297-M1234LAB-1|
|h6. TZ|TZ.BF.4.0.1|\\flower\location781\INTEGRATION\TZ.BF.4.0.1-00235-M1234AAAAANAAT-1|
|h6. ADSP|ADSP.1234.2.7.1|\\flower\location714\PROD\ADSP.1234.2.7.1-00121-00355-1|
|h6. SLPI|SLPI.HB.1.0|\\flower\location692\INTEGRATION\SLPI.HB.1.0-00277-M1234AAL-1|
|h6. RPM|RPM.BF.1.6|\\flower\location673\TEST\RPM.BF.1.6-00148-M1234AAAAANAAR-1|
|h6. WIR|WIR.RM.4.3|\\flower\location781\INTEGRATION\WIR.RM.4.3-00092-QCARMSWP-1|
|h6. BF|BF.RM.4.3|\\flower\location698\INTEGRATION\BF.RM.4.3-00022-QCABFSWP-1|
|h6. VIDEO|VIDEO.VE.4.2|\\flower\location682\PROD\VIDEO.VE.4.2-00018-PROD-1|
|h6. CPE_V2|CPE.TSF.2.0|\\flower\location670\INTEGRATION\CPE.TSF.2.0-00003-W9335AAAAAAAAQ-1|
|h6. GI|GI.SPR.1.0|\\nickle\zipbuild165\INTEGRATION\GI.SPR.1.0-10011-GISW-1|
|h6. UNKNOWN|WIR.ADDON_RM.3.0|\\nickle\zipbuild193\PROD\WIR.ADDON_RM.3.0-00013-CNSS_RMZ_WAPI-1|"""

engg_buildinfo = {'TZ':'\\gromit\location1\fw.861','PASS':'\\gromit\apps\pass.61'}

build_info_match = False

for key in engg_buildinfo :
  engg_buildinfo['key'] = prod_buildinfo['key']
  #all the build locations in dict match with buildinfo locations
  build_info_match = True


print build_info_match

OUTPUT:-

Traceback (most recent call last):
  File "buildinfo.py", line 27, in <module>
    engg_buildinfo['key'] = prod_buildinfo['key']
TypeError: string indices must be integers, not str
  • First error is you're checking egg_buildinfo['key'] while it has to be egg_buildinfo[key] without single quotes, next you have not yet parsed any dictioaries - prod_buildinfo is a string, not a dictionary that contains key same to keys in egg_buildinfo. First use regular expressions to extract the dictionary from string, then you can start comparing if there's something legitimate under key in the resulting dictionary. – dmitryro Jun 21 '16 at 23:16
  • thanks dmitryro - can you provide some guidance on how to use regular expressions to extract the dictionary from string –  Jun 21 '16 at 23:19
  • On how to transform string (certain format considerations apply) into dictionary - see some examples at http://stackoverflow.com/questions/13675942/converting-string-to-dict and http://codereview.stackexchange.com/questions/106719/converting-string-to-dict-using-python-ast-library . On how to evaluate regular expressions and form them using literals, metacharacters etc... look at https://docs.python.org/2/library/re.html, https://docs.python.org/2/howto/regex.html – dmitryro Jun 21 '16 at 23:28
  • is there any other approach other an regex?regex seems very complex for this case –  Jun 21 '16 at 23:43
  • If you know which string you want to get rid of, you can replace it with an empty string like in http://www.tutorialspoint.com/python/string_replace.htm – dmitryro Jun 21 '16 at 23:46
  • Would it be easier to get a match of (key)|.*|.* ,split based on `|` and then get the build location –  Jun 22 '16 at 00:01
  • You probably know better which character separates substrings that match your criteria - you can split it and get a list of strings and then create a dictionary if you need to refer by key – dmitryro Jun 22 '16 at 00:03

1 Answers1

0

From what I understood about your code I come up with this, I think you could work from here, I could add more code but I don't get what you mean by "all the build locations in dict match with buildinfo locations"

import re
prod_buildinfo = """|h6. {color:blue} Meta Build{color}|{color:blue}MSM1234.LA.1.9{color}|{color:blue}\\flower\location721\INTEGRATION\CI_MSM1234.LA.1.9-116391-STD.INT-2{color}|
    |h6. {color:green}MSM1234{color}|
    |h6. GLUE|GLUE.MSM1234.2.0|\\bigelow\zipbuild261\INTEGRATION\GLUE.MSM1234.2.0-00010.1-NOOP_TEST-1|
    |h6. PASS|LA.HB.1.3.9|\\flower\location674\INTEGRATION\CI_LA.HB.1.3.9-19602-8x96.1-5|
    |h6. MOD|MPSS.TH.2.0.2|\\flower\location678\INTEGRATION\MPSS.TH.2.0.2-00089-M1234FAAAANAAM-1|
    |h6. BOOT|BOOT.XF.1.0|\\flower\location675\INTEGRATION\BOOT.XF.1.0-00297-M1234LAB-1|
    |h6. TZ|TZ.BF.4.0.1|\\flower\location781\INTEGRATION\TZ.BF.4.0.1-00235-M1234AAAAANAAT-1|
    |h6. ADSP|ADSP.1234.2.7.1|\\flower\location714\PROD\ADSP.1234.2.7.1-00121-00355-1|
    |h6. SLPI|SLPI.HB.1.0|\\flower\location692\INTEGRATION\SLPI.HB.1.0-00277-M1234AAL-1|
    |h6. RPM|RPM.BF.1.6|\\flower\location673\TEST\RPM.BF.1.6-00148-M1234AAAAANAAR-1|
    |h6. WIR|WIR.RM.4.3|\\flower\location781\INTEGRATION\WIR.RM.4.3-00092-QCARMSWP-1|
    |h6. BF|BF.RM.4.3|\\flower\location698\INTEGRATION\BF.RM.4.3-00022-QCABFSWP-1|
    |h6. VIDEO|VIDEO.VE.4.2|\\flower\location682\PROD\VIDEO.VE.4.2-00018-PROD-1|
    |h6. CPE_V2|CPE.TSF.2.0|\\flower\location670\INTEGRATION\CPE.TSF.2.0-00003-W9335AAAAAAAAQ-1|
    |h6. GI|GI.SPR.1.0|\\nickle\zipbuild165\INTEGRATION\GI.SPR.1.0-10011-GISW-1|
    |h6. UNKNOWN|WIR.ADDON_RM.3.0|\\nickle\zipbuild193\PROD\WIR.ADDON_RM.3.0-00013-CNSS_RMZ_WAPI-1|"""

engg_buildinfo = {'TZ':'\\gromit\location1\fw.861','PASS':'\\gromit\apps\pass.61'}
prod_buildinfo_dict = {}
# Cleanup unwanted color data like {color:blue} or {color}
prod_buildinfo = re.sub(r'{color.{0,6}}', '', prod_buildinfo)

# Split your file by this string "|h6. "
rows = prod_buildinfo.split("|h6. ")
# Remove the first row because it is empty
rows = rows[1::]

# Create a dictionary from your data
for row in rows:
    #split your rows in columns
    columns = row.split("|")

    #Some data would not have location  
    if len(columns) > 2:
        location = columns[2]
    else:
        location = ""

    # Creating the dictionary
    prod_buildinfo_dict[columns[0]] = location
  • Ideally `prod_buildinfo_dict`should look like `{'GLUE':'\\bigelow\zipbuild261\INTEGRATION\GLUE.MSM1234.2.0-00010.1-NOOP_TEST-1' , 'PASS':'flower\location674\INTEGRATION\CI_LA.HB.1.3.9-19602-8x96.1-5' , 'MOD':'\\flower\location678\INTEGRATION\MPSS.TH.2.0.2-00089-M1234FAAAANAAM-1' ............, 'UNKNOWN':'\\nickle\zipbuild193\PROD\WIR.ADDON_RM.3.0-00013-CNSS_RMZ_WAPI-1'}` ,there is redundant data with above code –  Jun 22 '16 at 00:23
  • To clarify ,we see unncessary data `{color:green}MSM1234{color} \nickle\zipbuild193\PROD\WIR.ADDON_RM.3.0-00013-CNSS_RMZ_WAPI-1` and `{color:blue} Meta Build{color} \nickle\zipbuild193\PROD\WIR.ADDON_RM.3.0-00013-CNSS_RMZ_WAPI-1` –  Jun 22 '16 at 00:29
  • The "{color:color}" data on the row or the full row? – Cristopher Chacon Jun 22 '16 at 01:14
  • So you can play with regexps in an interface like http://regexr.com/ to construct a regexp that will extract the valid data for you - otherwise it would be hard to locate the substring with all the special characters. – dmitryro Jun 22 '16 at 14:37
  • @Cristopher - the full row,`|h6. {color:blue} Meta Build{color}|{color:blue}MSM1234.LA.1.9{color}|{color:blue}\\flower\location721\INTEGRATION\CI_MSM1234.LA.1.9-116391-STD.INT-2{color}| |h6. {color:green}MSM1234{color}| ` be removed from the rows –  Jun 22 '16 at 18:40