初始化提交
This commit is contained in:
8
common/templates/python/get.py
Normal file
8
common/templates/python/get.py
Normal file
@@ -0,0 +1,8 @@
|
||||
import sys
|
||||
import ubinascii
|
||||
with open('{{&path}}', 'rb') as infile:
|
||||
while True:
|
||||
result = infile.read(32)
|
||||
if result == b'':
|
||||
break
|
||||
len = sys.stdout.write(ubinascii.hexlify(result))
|
||||
22
common/templates/python/ls-long-format.py
Normal file
22
common/templates/python/ls-long-format.py
Normal file
@@ -0,0 +1,22 @@
|
||||
try:
|
||||
import os
|
||||
except ImportError:
|
||||
import uos as os
|
||||
|
||||
def listdir(directory):
|
||||
try:
|
||||
if directory == '/':
|
||||
return sorted([directory + f for f in os.listdir(directory)])
|
||||
else:
|
||||
return sorted([directory + '/' + f for f in os.listdir(directory)])
|
||||
except:
|
||||
return sorted([f for f in os.listdir()])
|
||||
|
||||
r = []
|
||||
for f in listdir('{{&path}}'):
|
||||
try:
|
||||
size = os.stat(f)[6]
|
||||
except:
|
||||
size = os.size(f)
|
||||
r.append([f, size])
|
||||
print(r)
|
||||
35
common/templates/python/ls-recursive.py
Normal file
35
common/templates/python/ls-recursive.py
Normal file
@@ -0,0 +1,35 @@
|
||||
try:
|
||||
import os
|
||||
except ImportError:
|
||||
import uos as os
|
||||
|
||||
def listdir(directory):
|
||||
result = set()
|
||||
|
||||
def _listdir(dir_or_file):
|
||||
try:
|
||||
# if its a directory, then it should provide some children.
|
||||
children = os.listdir(dir_or_file)
|
||||
except OSError:
|
||||
# probably a file. run stat() to confirm.
|
||||
os.stat(dir_or_file)
|
||||
result.add(dir_or_file)
|
||||
else:
|
||||
# probably a directory, add to result if empty.
|
||||
if children:
|
||||
# queue the children to be dealt with in next iteration.
|
||||
for child in children:
|
||||
# create the full path.
|
||||
if dir_or_file == '/':
|
||||
next = dir_or_file + child
|
||||
else:
|
||||
next = dir_or_file + '/' + child
|
||||
|
||||
_listdir(next)
|
||||
else:
|
||||
result.add(dir_or_file)
|
||||
|
||||
_listdir(directory)
|
||||
return sorted(result)
|
||||
|
||||
print(listdir('{{&path}}'))
|
||||
37
common/templates/python/ls.py
Normal file
37
common/templates/python/ls.py
Normal file
@@ -0,0 +1,37 @@
|
||||
try:
|
||||
import os
|
||||
except ImportError:
|
||||
import uos as os
|
||||
|
||||
def check_path(path):
|
||||
try:
|
||||
stat = os.stat(path)
|
||||
# The first element of stat contains the file type and permission information
|
||||
# The mode index of the tuple returned by os.stat() is 0
|
||||
mode = stat[0]
|
||||
# To determine whether it is a directory, check the directory bit in stat mode
|
||||
if mode & 0o170000 == 0o040000:
|
||||
if len(os.listdir(path)):
|
||||
return 'dir'
|
||||
else:
|
||||
return 'empty dir'
|
||||
# To determine whether it is a file, check the file position in stat mode
|
||||
elif mode & 0o170000 == 0o100000:
|
||||
return 'file'
|
||||
else:
|
||||
return 'special file'
|
||||
except OSError:
|
||||
return 'none'
|
||||
|
||||
def listdir(directory):
|
||||
output = []
|
||||
if directory == '/':
|
||||
dirs = sorted([directory + f for f in os.listdir(directory)])
|
||||
else:
|
||||
dirs = sorted([directory + '/' + f for f in os.listdir(directory)])
|
||||
|
||||
for dir in dirs:
|
||||
output.append([dir, check_path(dir)])
|
||||
return output
|
||||
|
||||
print(listdir('{{&path}}'))
|
||||
59
common/templates/python/mixpy.py
Normal file
59
common/templates/python/mixpy.py
Normal file
@@ -0,0 +1,59 @@
|
||||
import math
|
||||
|
||||
def math_map(v, al, ah, bl, bh):
|
||||
return bl + (bh - bl) * (v - al) / (ah - al)
|
||||
|
||||
def math_mean(myList):
|
||||
localList = [e for e in myList if type(e) == int or type(e) == float]
|
||||
if not localList: return
|
||||
return float(sum(localList)) / len(localList)
|
||||
|
||||
def math_median(myList):
|
||||
localList = sorted([e for e in myList if type(e) == int or type(e) == float])
|
||||
if not localList: return
|
||||
if len(localList) % 2 == 0:
|
||||
return (localList[len(localList) // 2 - 1] + localList[len(localList) // 2]) / 2.0
|
||||
else:
|
||||
return localList[(len(localList) - 1) // 2]
|
||||
|
||||
def math_modes(some_list):
|
||||
modes = []
|
||||
# Using a lists of [item, count] to keep count rather than dict
|
||||
# to avoid "unhashable" errors when the counted item is itself a list or dict.
|
||||
counts = []
|
||||
maxCount = 1
|
||||
for item in some_list:
|
||||
found = False
|
||||
for count in counts:
|
||||
if count[0] == item:
|
||||
count[1] += 1
|
||||
maxCount = max(maxCount, count[1])
|
||||
found = True
|
||||
if not found:
|
||||
counts.append([item, 1])
|
||||
for counted_item, item_count in counts:
|
||||
if item_count == maxCount:
|
||||
modes.append(counted_item)
|
||||
return modes
|
||||
|
||||
def math_standard_deviation(numbers):
|
||||
n = len(numbers)
|
||||
if n == 0: return
|
||||
mean = float(sum(numbers)) / n
|
||||
variance = sum((x - mean) ** 2 for x in numbers) / n
|
||||
return math.sqrt(variance)
|
||||
|
||||
def lists_sort(my_list, type, reverse):
|
||||
def try_float(s):
|
||||
try:
|
||||
return float(s)
|
||||
except:
|
||||
return 0
|
||||
key_funcs = {
|
||||
"NUMERIC": try_float,
|
||||
"TEXT": str,
|
||||
"IGNORE_CASE": lambda s: str(s).lower()
|
||||
}
|
||||
key_func = key_funcs[type]
|
||||
list_cpy = list(my_list)
|
||||
return sorted(list_cpy, key=key_func, reverse=reverse)
|
||||
5
common/templates/python/mkdir.py
Normal file
5
common/templates/python/mkdir.py
Normal file
@@ -0,0 +1,5 @@
|
||||
try:
|
||||
import os
|
||||
except ImportError:
|
||||
import uos as os
|
||||
os.mkdir('{{&path}}')
|
||||
10
common/templates/python/mkfile.py
Normal file
10
common/templates/python/mkfile.py
Normal file
@@ -0,0 +1,10 @@
|
||||
try:
|
||||
import os
|
||||
except ImportError:
|
||||
import uos as os
|
||||
|
||||
try:
|
||||
os.stat('{{&path}}')
|
||||
except OSError:
|
||||
f = open('{{&path}}', 'w')
|
||||
f.close()
|
||||
6
common/templates/python/rename.py
Normal file
6
common/templates/python/rename.py
Normal file
@@ -0,0 +1,6 @@
|
||||
try:
|
||||
import os
|
||||
except ImportError:
|
||||
import uos as os
|
||||
|
||||
os.rename('{{&oldPath}}', '{{&newPath}}')
|
||||
5
common/templates/python/rm.py
Normal file
5
common/templates/python/rm.py
Normal file
@@ -0,0 +1,5 @@
|
||||
try:
|
||||
import os
|
||||
except ImportError:
|
||||
import uos as os
|
||||
os.remove('{{&path}}')
|
||||
16
common/templates/python/rmdir.py
Normal file
16
common/templates/python/rmdir.py
Normal file
@@ -0,0 +1,16 @@
|
||||
try:
|
||||
import os
|
||||
except ImportError:
|
||||
import uos as os
|
||||
def rmdir(directory):
|
||||
os.chdir(directory)
|
||||
for f in os.listdir():
|
||||
try:
|
||||
os.remove(f)
|
||||
except OSError:
|
||||
pass
|
||||
for f in os.listdir():
|
||||
rmdir(f)
|
||||
os.chdir('..')
|
||||
os.rmdir(directory)
|
||||
rmdir('{{&path}}')
|
||||
Reference in New Issue
Block a user