source: TI02-CSML/trunk/csml/csmllibs/csmlextra.py @ 2037

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI02-CSML/trunk/csml/csmllibs/csmlextra.py@2037
Revision 2037, 2.6 KB checked in by domlowe, 14 years ago (diff)

moving listify function to csmlextra.py

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#Stuff with nowhere better to go
2import random
3import string
4
5def getRandomID():
6    #returns a random 8 character alphanumeric string that can be used as an internal identifier in a CSML document.
7    #the ID only needs to be unique within the document that it occurs, so this is sufficiently random
8    #to make a clash extremely unlikely. The string is alphanumeric but always begins with a letter. 
9    #Note: tested with 50000 ids and all were different
10    randomID=random.choice(string.letters)
11    for i in range(7):
12        randomID=randomID+random.choice(string.letters+string.digits)
13    return randomID
14   
15
16def getSeqRule(nDims):
17        #returns a sequenceRule
18        #*****************very simplified version*****************
19        #This really needs more work to see if the +s and -s are correct. and the x/y/z s in right order
20        #nDims = spatial dimensions + time dimension
21       
22        #TODO CSML2 - rewrite this
23        if nDims == 2:
24                seq = "Linear"
25        elif nDims ==3:
26                seq = "+x+y"
27        elif nDims ==4:
28                seq = "+x+y+z"
29        else:
30                seq="not sure"
31        return seq
32       
33       
34def getMappingRule(nDims):
35        #TODO CSML2 - rewrite this
36        if nDims == 2:
37                mr = "+ gridI + series"
38        elif nDims ==3:
39                mr = "+ gridI + gridJ +series"
40        elif nDims ==4:
41                mr = "+ gridI + gridJ +gridK +series"
42        else:
43                mr="not sure"
44        return mr
45       
46       
47# a couple of string cleaning functions:
48#   don't think these are used now
49def cleanString1(messystring):
50        #removes outer brackets and changes commas to spaces
51        cleanstring = messystring[1:-1]
52        cleanstring = cleanstring.replace(',',' ')
53        #strip off first (time) dimension.
54        #note, this might need rethinking for other datasets
55        cleanstring = cleanstring[3:]
56        return cleanstring
57#       
58def cleanString(messystring):
59   # removes outer brackets and 's, but leaves commas.
60    cleanstring = messystring[1:-1]
61    cleanstring = cleanstring.replace("'",'')
62    #remove any carriage returns
63    cleanstring = cleanstring.replace("\n",'')
64    return cleanstring
65       
66def listify(item):
67    ''' listify checks if an item is a list, if it isn't it puts it inside a list and returns it. Always returns a list object'''
68    if type(item) is list:
69        return item
70    else:
71        return [item]       
72
73def main():
74    #test to check randomness of getRandomID()
75    idlist=[]
76    for j in range (500):
77        for i in range(100):
78            newid= getRandomID()
79            if newid in idlist:
80                print 'ID matched!!!'               
81                sys.exit()
82            else:               
83                idlist.append(newid)
84        print i * j
85    print 'done'
86   
87if __name__=="__main__":
88    main()
Note: See TracBrowser for help on using the repository browser.