#1
  1. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154

    Organizing Problem


    I'm hav a bit of a problem organizing my lists properly. Can someone show me a better way to make this code work?
    Code:
    if not os.path.isfile('languages.dat'):
        create = open('languages.dat', 'w')
        create.close()
        file = open('languages.dat', 'a')
        file.write('English\n')
        file.write('Spanish\n')
        file.write('French\n')
        file.write('Italian\n')
        file.write('Portuguese\n')
        file.write('German\n')
        file.write('Russian\n')
        file.close()
    else:
        pass
    langs = open ( 'languages.dat' )
    langslist = langs.readlines()
    for item in langslist:
        item = item.replace('\n', '')
        LangList.append(item)
    for item in LangList:
        print item
    language = raw_input('Select a Language: ')
    if language.lower() == LangList[0]:
        E[0] = 'True'
        S[0] = 'False'
        F[0] = 'False'
        I[0] = 'False'
        P[0] = 'False'
        G[0] = 'False'
        R[0] = 'False'
    elif language.lower() == LangList[1]:
        E[0] = 'False'
        S[0] = 'True'
        F[0] = 'False'
        I[0] = 'False'
        P[0] = 'False'
        G[0] = 'False'
        R[0] = 'False'
    elif language.lower() == LangList[2]:
        E[0] = 'False'
        S[0] = 'False'
        F[0] = 'True'
        I[0] = 'False'
        P[0] = 'False'
        G[0] = 'False'
        R[0] = 'False'
    elif language.lower() == LangList[3]:
        E[0] = 'False'
        S[0] = 'False'
        F[0] = 'False'
        I[0] = 'True'
        P[0] = 'False'
        G[0] = 'False'
        R[0] = 'False'
    elif language.lower() == LangList[4]:
        E[0] = 'False'
        S[0] = 'False'
        F[0] = 'False'
        I[0] = 'False'
        P[0] = 'True'
        G[0] = 'False'
        R[0] = 'False'
    elif language.lower() == LangList[5]:
        E[0] = 'False'
        S[0] = 'False'
        F[0] = 'False'
        I[0] = 'False'
        P[0] = 'False'
        G[0] = 'True'
        R[0] = 'False'
    elif language.lower() == LangList[6]:
        E[0] = 'False'
        S[0] = 'False'
        F[0] = 'False'
        I[0] = 'False'
        P[0] = 'False'
        G[0] = 'False'
        R[0] = 'True'
    else:
        pass
    After this is done, the following code will eventually be run, depending on if there are other users in the channel you enter.
    Code:
        if E[0] == 'True':
            print "JOIN: "+username+", "+product
            Log("JOIN: "+username+", "+product)
        elif S[0] == 'True':
            print "ENSAMBLE: "+username+", "+product
            Log("ENSAMBLE: "+username+", "+product)
        elif F[0] == 'True':
            print "JOIGNEZ: "+username+", "+product
            Log("JOIGNEZ: "+username+", "+product)
        elif I[0] == 'True':
            print "UNISCASI: "+username+", "+product
            Log("UNISCASI: "+username+", "+product)
        elif P[0] == 'True':
            print "JUNTE: "+username+", "+product
            Log("JUNTE: "+username+", "+product)
        elif G[0] == 'True':
            print "VERBINDEN Sie: "+username+", "+product
            Log("VERBINDEN Sie: "+username+", "+product)
        elif R[0] == 'True':
            print "СОЕДИНИТЕ: "+username+", "+product
            Log("СОЕДИНИТЕ: "+username+", "+product)
    E is English, S is Spanish, etc. That doesn't matter, but for some reason Python isn't printing out anything. It doesn't know what E[0] equals, S[0] equals, F[0] equals, etc. Can anyone help me out with this?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    Lawrence, Kansas [KU]
    Posts
    1,559
    Rep Power
    15
    why are E, S etc lists? if you're only going to use the first element anyway, and do you ever declare it as a list E=[] etc? and why are you putting True and False in quotes as a string, just use True and False (without quotes) as their normal boolean value.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    78
    Rep Power
    10
    Truly horrible code
    For the first part, the following code is much better:
    Code:
    import os
    if not os.path.isfile('languages.dat'):
        languages = ["English", "Spanish", "French", "Italian", "Portuguese",
                     "German", "Russian"]
        open('languages.dat', 'w').write("\n".join(languages))
        
    langlist = [lang.strip() for lang in open("languages.dat")]
    For the second part, it's not so much a problem with organisation of lists as a problem with design. This is something like how I'd do it:
    Code:
    class Language(object):
        def __init__(self, name, jointxt):
            self.name = name
            self.jointxt = jointxt
    
        def log_product(self, username, product):
            txt = "%s: %s, %s"%(self.jointxt, username, product)
            print txt
            #... insert logging code here - eg LOG.write(txt)
    
    langdict = {
                   "English" : "JOIN",
                   "Spanish" : "ENSAMBLE",
                   "French" : "JOIGNEZ",
                   #... insert other languages here
               }
    
    while True:
        print "\n".join(langdict.keys())
        lang = raw_input('Select a Language: ').strip().title()
        if lang in langdict:
            break
    
    mylang = Language(lang, langdict[lang])
    
    #Then when the code needs to be run
    mylang.log_product("hydroxide", "widgets")
    Understanding is left as an exercise - use lots of print statements on various bits and pieces and the help() function in interactive mode.
    --OH.

    Comments on this post

    • thaminda agrees
  6. #4
  7. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    I think hydroxide understood my question more. jacktasia, I did have E = [], S = [], and so on. I had them equal 'True' or 'False' as the first item in the list so if it wus 'True', it was True, if it was 'False', it was False. You cant do if E[0] == False. That's what the quotes were used for, just to determine if they were being used or not. Thanks hydroxide.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    Don't hard code the languages, it makes your code huge, repetetive and very difficult to expand for more languages if you need to.

    Consider something like:

    A file per language.
    Each filename is the name of the language, e.g. french.lang, english.lang.

    Each piece of text you need in a different language has a way of referring to it, such as "msg_join_channel", "msg_quit_channel", etc.

    Each language file contains the referring code, then the text in that language, so:

    English.lang:
    msg_join_channel, User %s joined channel %s
    msg_quit_channel, User %s quit channel %s

    Latin.lang:
    msg_join_channel, %s is here.
    msg_quit_channel, Exit %s

    Your code reads the chosen language file, looking for codes and builds a dictionary.
    Where you need a message, you go to the language dictionary with that code.

    e.g.
    [code]
    import os

    # get the user to enter a language
    lang = raw_input("Enter a language: ")
    if not os.path.exists(lang_file):
    print "Language file not found"

    # Language dictionary
    messages = {}

    # Read the language text
    for line in file(lang + '.lang'):
    code, text = line.split(', ', 1)
    messages
    Code:
     = text
    
    # ...
    
    # Someone joins the channel
    print messages['msg_join_channel'] % (username, product)
    
    # Someone quits the channel
    print messages['msg_quit_channel'] % (username, product)
    (I have no idea how this would handle unicode or accented/etc characters).


    You cant do if E[0] == False
    Yes you can:

    Code:
    >>> E = [True]
    >>> E
    [True]
    >>> E[0]
    True
    >>> E[0] == True
    True
    >>> E[0] == False
    False
    >>>

IMN logo majestic logo threadwatch logo seochat tools logo