-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstellagama.py
194 lines (171 loc) · 4.36 KB
/
stellagama.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#stellagama.py
# A module with various useful functions by Omer Golan-Joel
# v2.3 - March 31st, 2018
# This is open source code, feel free to use it for any purpose
# contact me at [email protected]
#import modules
import random
import string
import os
import unittest
import platform
def yn():
"""
Simple yes or no prompt filtering invalid results
"""
query = 1
while query == 1:
answer = input("Y/N: ")
if answer in ["y", "Y"]:
return "yes"
break
if answer in ["n", "N"]:
return "no"
break
else:
print ("Invalid Answer")
def random_choice(list): #input list
"""
randomly chooses an element from a list.
"""
element=list[random.randint(0,len(list)-1)]
return element #output randomly-selected element
def dice(n,sides):
"""
dice-roller
"""
die=0
roll=0
while die<n:
roll=roll+random.randint(1,sides)
die+=1
return roll
def pseudo_hex(num): #inputs number
"""
converts numbers to Cepheus Engine "Pseudo-Hex"
now converted to a list.
"""
num=int(num)
code=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "E", "S", "T", "U", "V", "W", "X", "Y", "Z"]
num=code[num]
return num #outputs "pseudo-hex" number
def current_dir():
"""
lists the current directory's contents on Windows or Linux
"""
if platform.system() == "Windows":
directory=os.listdir(".\\")
else:
directory = os.getcwd()
return directory
def check_file_exists(check_file):
"""
checks if a file exists in the directory
"""
if check_file in os.listdir():
file_exists = True
else:
file_exists = False
return file_exists
def savefile(extension): #input extension
"""
file-saving function
"""
filename=str(input("Please enter file name to generate: "))
filecheck=filename+"."+extension #checks in a file with the same name exists in the directory
save = 1
if check_file_exists(filecheck):
print(" ")
print("File already exists. Overwrite?")
overwrite=yn()
if overwrite == "y":
save=0
if overwrite == "n":
filename=input("Please enter new file name to generate: ")
filename=filename+"."+extension
return filename #outpus File name
def clear_screen():
"""
clear screen function
"""
if platform.system() == "Windows":
os.system('cls')
else:
os.system('clear')
def random_line (filename): #input file name
"""
randomly chooses a line from a text file
"""
with open (filename, "r") as line_list:
line=random_choice(line_list.readlines())
line=line.strip()
return line #output randomly-chosen line
class Getch:
"""
Gets a single character from standard input. Does not echo to the
screen
"""
def __init__(self):
try:
self.impl = GetchWindows()
except ImportError:
self.impl = GetchUnix()
def __call__(self):
return self.impl()
class GetchUnix:
def __init__(self):
import tty, sys
def __call__(self):
import sys, tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
class GetchWindows:
def __init__(self):
import msvcrt
def __call__(self):
import msvcrt
return msvcrt.getch()
def getkeypress():
"""
reads a single keypress
"""
if platform.system() == "Windows":
directory=os.listdir(".\\")
key = Getch()
return key().decode() #outputs keypress
else:
key = Getch()
return key() #outputs keypress
def list_stringer(input_list): #input a list
"""
converts a list to a string
"""
output_list=[]
for item in input_list:
output_list.append(str(item))
return ' '.join (output_list) #output a string
#testing area
#class testpseudohex(unittest.TestCase):
# """
# tests for pseudo_hex
# """
# def testhex(self):
# code=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "E", "S", "T", "U", "V", "W", "X", "Y", "Z"]
# for i in range (0, 34):
# self.assertEqual(pseudo_hex(i), code[i])
# class Testdice(unittest.TestCase):
# """
# test the dice function
# """
# def dicetest(self):
# self.assertEqual(dicetest(1, 6), [1, 6])
# if __name__=='__main__':
# unittest.main()
# getch = _Getch()
# print (getkeypress())