0

I need advice in attacking this problem, I am stumped and don't really know where to start. I don't want the code i just need advice

The question is as follows:

use regular expressions to extract all of the variable names which get assigned to dictionary or set literals from a Python program located in the file code.py. Python variables match the regular expression \w+ in Python (see the documentation about \w). Dictionary and set literals start with a open squiggly brace ({).

For example, given this Python program:

code.py

# Here is a Python file.
names = {}
foo = []
names[0] = 12
christmas ={
  'tree': 'green',
  'candy cane': 'red and white'
}

def yes():
  return "yes"

your program should produce this output:

names
christmas

The variable names should be printed out in the order that they appear in the file. The Python code will be syntactically valid, but not necessarily executable without error.

So I have this code.py consisting of the above code and my new file called program.py where I have to read the contents of code.py and output the variable names for all the dictionaries. That means if code.py is to change and a lot more dictionaries were added in it would still output all of them in order.

Explanations as to what does what would help greatly!

2
  • This is obviously a homework assignment. But either way - it's all about the regular expression you need to create. program.py will read in code.py (see the python function open), then will need to find things that look like dictionaries (the bits with {}). See the python re module docs. Commented Aug 12, 2015 at 11:36
  • i got it mostly working, when i print it now it will output. ('names',) ('christmas',) instead of just the pure output. is there a way of doing this differently so it just outputs 'names' and 'christmas' Commented Aug 12, 2015 at 11:50

1 Answer 1

1

You can use this regex

(\w*)\s*=\s*{(?:.|\n)*?}

How

(\w*) #match the dict name (any alphanumeric character and _ ) and capture it
\s*=\s* #match '=' with any amount of space around it
{ # match '{' 
(?:.|\n)*? # match any dict defintion over couple of lines
} # match '}' 

You can check it here

Sign up to request clarification or add additional context in comments.

1 Comment

Could you explain what each one does? like why is there a space after the =, im a bit confused with all these regex's. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.