newbie help ?

John Hunter jdhunter at ace.bsd.uchicago.edu
Fri May 16 00:37:18 EDT 2003


>>>>> "Beanie" == Beanie  <nospam at diving.ukdiver.com> writes:

    Beanie> I have the code below which is supposed to run in plone to

What is plone?  python?  Really, I am lost here.

    Beanie> pull a code out of a text file and mail it to a person, it
    Beanie> dosen't work and i don't know why. It dosn't help that I
    Beanie> don't really no how to cofe in python i'm more of a
    Beanie> c/vb/asp/ada person. i've been loking throught the net for

OK, cofe=code, loking=looking.   The former mistake looks like a typo
and not dyslexia, since the 'f' is next to the 'd'.  That indicates
carelessness.  Which is a problem in coding.  Proofread your code;
proofread your posts.

( ... stepping off high horse ... )

    Beanie> any online tutorials / resources and ahven't found a lot
    Beanie> that was helpful. anybody sugest a) what is wrong witht
    Beanie> hescript - got to be something simple most of it is hacked
    Beanie> from examples b) where to get good resources on python
    Beanie> scripting

You need to tell us what is wrong from your end; you say it isn't
working, but don't say how/why.  No error messages are provided.

    # Import a standard function, and get the HTML request and response objects.
    from Products.PythonScripts.standard import html_quote
    request = container.REQUEST
    RESPONSE =  request.RESPONSE

Looks like zope.  That's good information you should provide in your
post.

    referer = request.came_from

    if request.from_addr == '':
       RESPONSE.redirect( '%s?portal_status_message=%s' % ( referer, "You have
    not entered your E-Mail address" ) )
       return

    file=open("codes.txt",'r+') #open the file

Danger.  'file' is a built-in name; don't use it as a variable name.
And what is 'r+'?  I know 'w+' is open for writing in append mode.
Does r+ mean something?  Try

inFile = open("codes.txt",'r')
 
    strCode=file.readlines() # read all of the file into a list
    strDiscountCode = strCode.pop(0) #get one code
    if strDiscountCode =="": #if its null we run out so mail me
     mail_text = context.comments_template(
        from_name = " Discount Codes",
        from_addr = "codes at domain",
        who = "me at me",
        comments =  request.from_addr , "tried for a code but we run out.")

     host = context.MailHost
     host.send( mail_text )

     RESPONSE.redirect( '%s?portal_status_message=%s' % ( referer, "Sorry we
    have temporarily run out of codes one will be sent as soon as
    possible!" ) );
    else: # else send them the code

Your single line indentation makes this code almost unreadable.
Python does away with curly brackets and end delimiters and uses
whitespace instead.  For most humans, you need enough whitespace to
make the space delimters clear by eye.  The consensus is 3 or 4
spaces.  Definitely not 1.  The python interpreter is happy as a pig
in mud with one space, but your average python-list reader gets lost.

     mail_text = context.comments_template(
        from_name = "Discount Codes",
        from_addr = "codes at domain",
        who = request.from_addr,
        comments =  "Your Discount code is:" & strDiscountCode & ".")

     host = context.MailHost
     host.send( mail_text )

     RESPONSE.redirect( '%s?portal_status_message=%s' % ( referer, "Your
    discount code has been sent. Thank you." ) );

            file.writelines(strCodes) #rewrite the file with missing code

    file.close()# close the file

Again, file is *a very bad name*.

The comments I made are minor and syntactical, and I doubt they go to
the core of your problem.  Repost with some more specific info about
what is going wrong, and someone here will be able to help.

JDH





More information about the Python-list mailing list