Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8766081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:27:34+00:00 2026-06-13T16:27:34+00:00

I am trying to make arguments easier to manage in a script I am

  • 0

I am trying to make arguments easier to manage in a script I am putting together, I figured I’d wrap a bunch of related items into a dictionary, and pass the dictionary out of the func. pulling out the objects as I need them.

One of these items is a regEx, and I’m struggling to figure out how to structure things properly so I can make it work.

In my initial code (no dictionary). I am ‘hard’ coding the regex into the parser:

def TopnTail(self,line):
    topRegEx = re.compile(r'(<!--make_database header end-->)') 
    tailRegEx = re.compile(r'(<!--make_database footer start-->)') 
    searchdumpTopOfPage = topRegEx.search(line)
    searchdumpBottomOfPage = tailRegEx.search(line)
    if searchdumpTopOfPage:
        self.__useLine=1
    if searchdumpBottomOfPage:
        self.__useLine=0  
    if self.__useLine == 1:
        self.trimmedLines = self.trimmedLines + line + "\n"
    return (self.trimmedLines)

in the ‘dictionaried’ version I want to set the variables in a setter:

def siteDetails():
    baseDict = {'topRegex':'''re.compile(r'(<!--make_database header end-->)')''', 'tailRegex':'''re.compile(r'(<!--make_database footer start-->)')'''}
    return baseDict

and get to the compiled regex:

def TopnTail(self,line):
    topRegEx = baseDict['topRegex'] 
    tailRegEx = baseDict['tailRegex'] 
    searchdumpTopOfPage = topRegEx.search(line)
    searchdumpBottomOfPage = tailRegEx.search(line)
    if searchdumpTopOfPage:
        self.__useLine=1
    if searchdumpBottomOfPage:
        self.__useLine=0  
    if self.__useLine == 1:
        self.trimmedLines = self.trimmedLines + line + "\n"
    return (self.trimmedLines) 

but this throws an error:

line 35, in TopnTail
searchdumpTopOfPage = topRegEx.search(line)
AttributeError: 'str' object has no attribute 'search'

Which I am guessing means that its not actually made the regex object, but is still passing the string.

I appreciate that I am probably breaking about 3 cardinal rules here… but any suggestions about how to make it work would be fantastic… (also, first time playing with both classes and dictionaries… so please go easy if I’ve really messed up!)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T16:27:36+00:00Added an answer on June 13, 2026 at 4:27 pm

    How about this?

    baseDict = {
      'topRegex': r'(<!--make_database header end-->)'
    }
    

    And in your TopnTail method

    topRegEx = re.compile(baseDict['topRegex'])
    

    The problem with what you have, is that you’re assigning a string to topRegEx that contains '''re.compile(r'(<!--make_database header end-->)')'''. Since str has no method search, you get an error.

    It makes sense to still compile your regex, and use the returned object. Splitting the contents of the regex into a dict will help if you ever need to changes the regex pattern, or you want to define it dynamically.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make a shell script that takes arguments and then performs
I'm trying to make a simple HTTP post a endpoint with ONLY url arguments.
I'm trying to use C++11 features to make custom stream manipulators easier to create.
I am trying to make Apache send: http://mydomain.com/these/are/arguments/mmmk to: http://mydomain.com/?pages=these/are/arguments/mmmk Such that $_GET['pages'] will
I'm trying to make a parser for a Java Program's command line arguments, as
I am trying to make my code NOT to accept keyword arguments just like
Here I'm trying to make an array of functions with arguments to Async.js. The
I'm trying to make a function that takes 2 arguments from the user_controller and
I am trying to make a new command which runs a shell script that
I'm trying to make argparse ignore the fact that two normally required positional arguments

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.