-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.py
125 lines (95 loc) · 3.58 KB
/
generator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/python
# -*- coding: utf-8 -*-
# absolute.py
import sys, os
from PyQt4 import QtGui, QtCore
import random
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
label1 = QtGui.QLabel('About this application:', self)
label1.move(15, 10)
label2 = QtGui.QLabel('Generate random items from the given file.', self)
label2.move(15, 30)
# window
self.setWindowTitle('Random Generate')
self.resize(280, 300)
# Open file
openFile = QtGui.QPushButton('Select file', self)
openFile.setGeometry(10, 10, 100, 30)
openFile.move(50, 80)
self.connect(openFile, QtCore.SIGNAL('clicked()'), self.showDialog)
# total input
total_tip = QtGui.QLabel('Total:', self)
total_tip.move(50, 130)
self.label_total = QtGui.QLineEdit(self)
self.label_total.move(100, 130)
# select number input
select_tip = QtGui.QLabel('Select:', self)
select_tip.move(50, 170)
self.label_select = QtGui.QLineEdit(self)
self.label_select.move(100, 170)
# generate
ok = QtGui.QPushButton('Generate', self)
ok.setGeometry(10, 10, 60 ,30)
ok.move(50, 210)
self.connect(ok, QtCore.SIGNAL('clicked()'),
self.submmit)
# close
quit = QtGui.QPushButton('Close', self)
quit.setGeometry(10, 10, 60 ,30)
quit.move(150,210)
self.connect(quit, QtCore.SIGNAL('clicked()'),
self.close)
# test button, only for debug.
# t = QtGui.QPushButton('Test', self)
# t.setGeometry(10, 10, 60 ,30)
# t.move(50,250)
# self.connect(t, QtCore.SIGNAL('clicked()'),
# self.testfunc)
def submmit(self):
total = self.label_total.text()
select = self.label_select.text()
a = range(1,int(total)+1)
b = int(select)
ran_list = random.sample(a,b)
print ran_list
new_name=0
filepath = os.path.split(str(self.filename))[0]+'/'
while os.path.exists("%s%s.txt"%(filepath, new_name)):
new_name = new_name+1
w = open("%s%s.txt"%(filepath, new_name),"a")
with open(self.filename, 'r') as f:
content = f.read()
new_num = 0
for num in ran_list:
num_tag_a = '%s%s'%('\n', num)
num_tag_b = '%s%s'%('\n', num+1)
if num == 1:
num_content = '%s%s'%('\n', content[:content.index(num_tag_b)])
elif num == int(total):
num_content = content[content.index(num_tag_a):]
else :
num_content = content[content.index(num_tag_a):content.index(num_tag_b)]
new_num = new_num + 1
new_content = '%s%s%s'%('\n', new_num, num_content[len(num_tag_a):])
w.write(new_content)
w.close()
# f = open(self.filename, 'r')
# print ran_list
# e='%s%s'%('\n',d+1)
# c=b[b.index('\n3'):b.index('\n4')]
#
def showDialog(self):
self.filename = QtGui.QFileDialog.getOpenFileName(self, 'Open File',
'/home')
print(self.filename)
# print(os.path.split(str(self.filename))[0])
def testfunc(self):
print(self.filename)
app = QtGui.QApplication(sys.argv)
ex = Example()
ex.show()
sys.exit(app.exec_())