Python Forum

Full Version: rewrite_title
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
"""
Create a function called rewrite_title, taking two arguments:
  - String, representing HTML body (full or partial)
  - String, representing a new TITLE that must be applied into the HTML

The function must:
  1. Return a new string, where the content found between <title> and
  </title> (*case insensitive*) are replaced with the function's TITLE argument.
  All other text should remain unchanged, including the <title> opening and
  closing tags. All occurances of <title> tags should be rewritten, if there is
  more than 1.
  2. The function documentation should read:
    Replace the HTML title contents with the given TITLE

HINT: Look into the re.IGNORECASE flag for case insensitivity.

rewrite_title('<html><head><TiTLe>Foo title</title></html>', 'Bar title') ->
    '<html><head><TiTLe>Bar title</title></html>'
"""

import re

def rewrite_title(*args):
    """ Replace the HTML title contents with the given TITLE """
    htmlbody = args[0]
    newtitle = args[1]
    regex = '<title>(.+?)</title>'
    pattern = re.compile(regex,re.IGNORECASE)
    title = re.findall (pattern,htmlbody)
    rehtmlbody = re.sub (title, newtitle, htmlbody)
    print rehtmlbody

hb = '<html><head><TiTLe>Foo title</title></html>'
nt = 'Bar title'
rewrite_title(hb,nt)
what am i doing wrong,why is it not working
Start by making your function legible, so we can help better :) Making us do a bunch of work just to help you, isn't a good way to get people wanting to help lol

Maybe like this:
# old
def rewrite_title(*args):
    """ Replace the HTML title contents with the given TITLE """
    htmlbody = args[0]
    newtitle = args[1]

# new
def rewrite_title(htmlbody, newtitle):
    """ Replace the HTML title contents with the given TITLE """
Don't use *args like this.

(Dec-05-2016, 08:26 PM)roadrage Wrote: [ -> ]what am i doing wrong,why is it not working
You've been told before: be more specific.
I don't think me using *args is a blocker to this solution....
all i hear from you is bad practice (but no specific reasoning...why it is bad practice )
there are a million ways of writing programs, as long as it is working I do not care  (unless there is some added benefit to using it a particular way!!; this only comes through experience, if you have experience of this problem then explain why it is bad way to use it, i'd be happy to listen analyze and learn)

replace the above lines with this....

rehtmlbody = htmlbody.replace (title[0], newtitle)

    print ("'" + rehtmlbody + "'")
There is a specific reason: it's hard for other people to understand what you're trying to do.

Writing something that just works is easy, but if nobody can understand it, then you may as well have never written it. Instead of simply writing what the meaning behind your program is, you're adding a layer of indirection that serves no purpose at all aside from making it difficult to read. There is no benefit, so why do it?
(Dec-05-2016, 09:30 PM)roadrage Wrote: [ -> ]all i hear from you is bad practice (but no specific reasoning...why it is bad practice )
It's been mentioned several times, and a week ago I said that you should ask questions if something isn't clear (rather than ignoring it). To many of us, including when we were new users, it seemed obvious - a caller is able to accidentally call with too many or too few arguments, and the error messages will be more confusing (if they're even present), someone who wants to call your function has to read the body instead of just the parameter list to have any idea how to call it, and it's more lines of code, all with zero benefit.

I understand that we were taking these for granted, but when you were told repeatedly that this wasn't ok and then that you should ask questions, you can't act like we're just being annoying. This is on top of nilamo's very valid criticism, that you should be making it easier for us to help you. And finally, even if you reject all that, I mentioned the following in a previous post as well...

(Dec-05-2016, 09:30 PM)roadrage Wrote: [ -> ]I don't think me using *args is a blocker to this solution....
(Dec-05-2016, 08:26 PM)roadrage Wrote: [ -> ]Create a function called rewrite_title, taking two arguments:
While it's true that your function takes two arguments, it also takes more and less. Your assignment isn't to take in varargs, it's two args. If I were your teacher, you would not get full credit for this, if any at all. Even if your current teacher is more lenient, it's just luck that they are not taking their own specifications more seriously. It is entirely reasonable for us to consider this a blocking issue.

In the spirit of helping us to help you, you're still not doing a good job making it dead simple what's wrong vs. expected.
(Dec-05-2016, 09:30 PM)roadrage Wrote: [ -> ]replace the above lines with this....

rehtmlbody = htmlbody.replace (title[0], newtitle)

    print ("'" + rehtmlbody + "'")
What does this mean? All of them ("the above lines")? Some of them? Why replace them? To just have updated code, to fix a problem, to reproduce a problem, or what?

However annoying this seems, we're all putting a lot more effort into trying to help you than you are in either your assignment or to asking questions. It doesn't inspire our altruistic tendencies when people whine either. Our expectations are really not so hard, and following them will help you even after you're done here.
well... my focus is the"meat and potatoes" of the problem;not the "soups n salads";
your repeated comments about the same problem is nothing but a distraction to my focus of getting things done!

my post replace the above lines is about finding the solution (just by replacing the lines in my actual program, they have a solution), if someone else is looking through the posts for a similar problem, I want them to a find a solution.
Looks like what you have does work, though. It's just ugly as hell lol.

>>> re.sub(r'<title>(.+?)</title>', '<title>new title</title>', x, 0, re.IGNORECASE)
'<html><head><title>new title</title></head></html>'
What's the actual problem? Maybe the reason we're complaining about the style of your code, is because you never said what the issue is.
I totally agree... my code is the "ugly duckling" at this point...
hope "someday" it becomes the "beautiful duck" Smile
(Dec-07-2016, 07:31 PM)roadrage Wrote: [ -> ]I totally agree... my code is the "ugly duckling" at this point...
hope "someday" it becomes the "beautiful duck" Smile

This won't happen because you'll have taken bad habits... Ugly code is hard to read (in the sense that it needs more brainpower), and hard to read code is more likely to have unnoticed bugs, because you have diverted your brainpower to futile task instead of concentrating on the code logic. You are just making your life difficult. When you write something like this, it is totally inefficient for you:
def rewrite_title(*args):
    """ Replace the HTML title contents with the given TITLE """
    htmlbody = args[0]
    newtitle = args[1]
because you have to remember the arguments order, and to call the whole function you have to look at 4 lines of code to make sure you have the arguments at the right places, while when you write the function like this:
def rewrite_title(htmlbody,newtitle):
    """ Replace the HTML title contents with the given TITLE """
everything needed is in the def line... and the more arguments you have, the more difficult you code style makes it.

Now, if you want to remain a code poseur, you can save some typing and vertical space like this:
def rewrite_title(*args):
    """ Replace the HTML title contents with the given TITLE """
    htmlbody,newtitle = args
but then it becomes quite silly....

PS: of course, until you start coding to universally accepted standards, you'll be coding alone
Pages: 1 2