more scripts file
[archive.git] / Apkawa / Study / pascal / archive / generatejob-python / generatejob.py
bloba54ea86da4754401348cc554cd6c816d5a664713
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 ###
4 #This file is part of <name prog> project
6 #<описание программы>
7 #Copyright (C) <year> <name|nick>
9 #This program is free software; you can redistribute it and/or
10 #modify it under the terms of the GNU General Public License
11 #as published by the Free Software Foundation; either version 2
12 #of the License, or (at your option) any later version.
14 #This program is distributed in the hope that it will be useful,
15 #but WITHOUT ANY WARRANTY; without even the implied warranty of
16 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 #GNU General Public License for more details.
19 #You should have received a copy of the GNU General Public License
20 #along with this program; if not, write to the Free Software
21 #Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #You can contact author by email <my email>
24 ###
26 import os
27 import random
29 dict_folder = os.path.join( os.path.abspath('.'), 'dict' )
30 dicts = {'#fam':open( os.path.join( dict_folder, 'family.txt') ,'r' ),
31 '#name': open( os.path.join( dict_folder, 'name.txt'), 'r')}
33 def parse_argv( argv):
34 result = {}
35 for a in argv:
36 if a.startswith('('):
37 if a.endswith(')'):
38 result.update( {a: a[1:-1].split(',') })
39 if a.startswith('['):
40 if a.endswith(']'):
41 result.update( { a:range( *[ int(i) for i in a[1:-1].split('..')] ) } )
42 if a.startswith('#fam'):
43 result.update( {a:a} )
44 if a.startswith('#name'):
45 result.update( {a:a} )
46 return result
48 def rand_chois_from_dict( tag = '#name'):
49 f = dicts.get(tag)
50 f.seek(0,2)
51 max_pos = f.tell()
52 f.seek( random.randint( 0L, max_pos) )
53 f.readline()
54 selected = f.readline().replace('\r','').replace('\n','')
55 return selected
57 def main( argv=['#name','#fam','(м,ж)','[1500..8900]'], __max = 1024*2, delim=' ', delim_min_count=4,delim_max_count=10):
58 parsed_argv = parse_argv( argv )
59 result_array = []
60 for i in xrange(__max):
61 __temp = []
62 for a in argv:
63 _pa = parsed_argv.get(a)
64 if _pa:
65 if dicts.get(a):
66 __temp.append( rand_chois_from_dict( _pa ) )
67 else:
68 __temp.append( str(random.choice( _pa )) )
69 _delim = delim*random.randint( delim_min_count, delim_max_count)
70 result_array.append( _delim.join( __temp ) )
71 result = '\n'.join( result_array )
72 print result
75 if __name__ == '__main__':
76 main()