Untitled

By: a guest on Sep 20th, 2012  |  syntax: Python  |  size: 1.15 KB  |  hits: 40  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. #!/usr/bin/python
2. # -*- coding: UTF-8 -*-
3. import math
4.
5. def check(a):
6. #    if a == "":return False,0
7.     if a[-1:] =="p" and a[0].isdigit(): return True,len(a)
8.     if a.replace('/','').replace('\\','').isdigit(): return True,len(a)
9.     if a[0] =="s" and a[-1:].isdigit() : return True,len(a)
10.     else:return False,0
11.
12.
13. def short(string,length):
14.     return string[:length]
15.
16. def xer(d):
17.         splt=d.split('-')
18.         while 1:
19.            try: splt.remove('')
20.            except(ValueError): break
21.
22.         l=[0,0,0]
23.         for c in xrange(len(splt)):
24.              if check(splt[c])[0]==False:l[0]=l[0]+1
25.              elif check(splt[c])[0]==True:l[1]=l[0]+check(splt[c])[1]
26.              l[2]=l[2]+1
27.
28.         length=int(math.floor((19-l[1]-l[2])/l[0]))
29.         g=[]
30.         for m in xrange(len(splt)):
31.              if check(splt[m])[0]==False:g.append(short(splt[m],length))
32.              else: g.append(splt[m])
33.         return "-".join(map(str, g))
34.
35. def classs(d):
36.     if len(d)>20:return xer(d).replace('/','_').replace('\\','_')
37.     else: return d.replace('/','_').replace('\\','_')
38.
39.
40. print classs('Geroev-Revolyutsii-32/1-3p--sw2')