Compare commits
1 Commits
29bb38ed2f
...
0e8e8a9803
Author | SHA1 | Date | |
---|---|---|---|
|
0e8e8a9803 |
120
Code/DataStructureManipulations.py
Normal file
120
Code/DataStructureManipulations.py
Normal file
@ -0,0 +1,120 @@
|
||||
from sys import platform as OperatingSystem
|
||||
|
||||
Quotes = '"\''
|
||||
|
||||
if OperatingSystem == "win32":
|
||||
PathSlash = '\\'
|
||||
else:
|
||||
PathSlash = '/'
|
||||
FileProtocol = "file:" + 2*PathSlash
|
||||
|
||||
|
||||
def ConvertToStandardPathFormat(Path):
|
||||
""" Example,
|
||||
Input: '"file:///some/path/somefile.extension"
|
||||
Output: /some/path/somefile.extension
|
||||
"""
|
||||
Path = Path.strip(Quotes)
|
||||
if Path.startswith(FileProtocol):
|
||||
Path = Path[len(FileProtocol):]
|
||||
return Path
|
||||
|
||||
|
||||
def GetTextAfter(Text, ReadlinesTextFile):
|
||||
for Lines in range(len(ReadlinesTextFile)):
|
||||
Line = ReadlinesTextFile[Lines].strip('\n')
|
||||
if Line.startswith(Text):
|
||||
return Line[len(Text):]
|
||||
return ''
|
||||
|
||||
|
||||
def SingleQuoteString(String):
|
||||
if len(String) > 0:
|
||||
if String[0] != '\'' or String[-1] != '\'':
|
||||
String = '\'' + String + '\''
|
||||
return String
|
||||
|
||||
|
||||
def DoubleQuoteString(String):
|
||||
if len(String) > 0:
|
||||
if String[0] != '"' or String[-1] != '"':
|
||||
String = '"' + String + '"'
|
||||
return String
|
||||
|
||||
|
||||
def ListIntoString(List, QuoteItems=0, Seprator=' '):
|
||||
if QuoteItems == 2:
|
||||
for i in range(len(List)):
|
||||
Quoteditem = DoubleQuoteString(List[i])
|
||||
List[i] = Quoteditem
|
||||
elif QuoteItems == 1:
|
||||
for i in range(len(List)):
|
||||
Quoteditem = SingleQuoteString(List[i])
|
||||
List[i] = Quoteditem
|
||||
Stringoflist = (Seprator).join(List)
|
||||
return Stringoflist
|
||||
|
||||
|
||||
# strip=0 => remove both ' & ", 1 => remove ', 2 => remove "
|
||||
def UnquoteString(String, strip=0):
|
||||
while True:
|
||||
if (
|
||||
strip != 2 and
|
||||
String.startswith('"') and
|
||||
String.endswith('"')
|
||||
):
|
||||
String = String.strip('"')
|
||||
elif (
|
||||
strip != 1
|
||||
and String.startswith("'")
|
||||
and String.endswith("'")
|
||||
):
|
||||
String = String.strip("'")
|
||||
else:
|
||||
break
|
||||
return String
|
||||
|
||||
|
||||
def StandardVariableName(Variable):
|
||||
Variable = Variable.casefold()
|
||||
Variable = Variable.replace('_', '').replace(' ', '')
|
||||
return Variable
|
||||
|
||||
"""
|
||||
def DictionaryToJsonStr(Dict, BaseIndentation=0):
|
||||
BI = '\t'*BaseIndentation
|
||||
JsonStr = BI+'{\n'
|
||||
for k, v in Dict.items():
|
||||
JsonStr += BI+'\t"'+k+'" : "'+v+'",\n'
|
||||
JsonStr = JsonStr[:-2]
|
||||
JsonStr += '\n'+BI+'}'
|
||||
return JsonStr
|
||||
"""
|
||||
|
||||
def StringToKeyValuePair(String, Seprator):
|
||||
SepratorAt = String.find(Seprator)
|
||||
if SepratorAt >= 0:
|
||||
Key = String[:SepratorAt]
|
||||
Value = String[SepratorAt+1:]
|
||||
return Key, Value
|
||||
else:
|
||||
return "", String
|
||||
|
||||
|
||||
def FormatStrForDictinary(String):
|
||||
String = String.strip(" \n\r")
|
||||
return UnquoteString(String)
|
||||
|
||||
|
||||
def StrListToDictionary(
|
||||
List,
|
||||
Seprator='=',
|
||||
FormatFunction=FormatStrForDictinary
|
||||
):
|
||||
Dictionary = {}
|
||||
for i in List:
|
||||
k, v = StringToKeyValuePair(i, Seprator)
|
||||
k, v = FormatFunction(k), FormatFunction(v)
|
||||
if len(k) > 0:
|
||||
Dictionary[k] = v
|
||||
return Dictionary
|
33
Code/InputOutput.py
Normal file
33
Code/InputOutput.py
Normal file
@ -0,0 +1,33 @@
|
||||
from os.path import isfile as DoesFileExist
|
||||
|
||||
from .DataStructureManipulations import ConvertToStandardPathFormat
|
||||
|
||||
|
||||
def ReadTextFile(FilePath):
|
||||
FilePath = ConvertToStandardPathFormat(FilePath)
|
||||
if DoesFileExist(FilePath) is True:
|
||||
File = open(FilePath)
|
||||
ReadFile = File.read()
|
||||
File.close()
|
||||
return ReadFile
|
||||
else:
|
||||
return ''
|
||||
|
||||
|
||||
def ReadlinesTextFile(FilePath):
|
||||
String = ReadTextFile(FilePath)
|
||||
return String.split('\n')
|
||||
|
||||
|
||||
def WriteTextFiles(FilePath, Text):
|
||||
if type(Text) != str:
|
||||
Text = '\n'.join(Text)
|
||||
File = open(FilePath, 'w')
|
||||
File.write(Text)
|
||||
File.close()
|
||||
|
||||
|
||||
def WriteBinaryToFile(Filepath, Data):
|
||||
File = open(Filepath, 'wb')
|
||||
File.write(Data)
|
||||
File.close()
|
8
Code/Network.py
Normal file
8
Code/Network.py
Normal file
@ -0,0 +1,8 @@
|
||||
import ssl
|
||||
from urllib.request import urlopen
|
||||
|
||||
HttpsContext = ssl.create_default_context()
|
||||
|
||||
|
||||
def Download(Url):
|
||||
return urlopen(Url, context=HttpsContext).read()
|
36
Code/OS.py
Normal file
36
Code/OS.py
Normal file
@ -0,0 +1,36 @@
|
||||
import os
|
||||
|
||||
|
||||
def CallFuncInDir(Directory, Function, *args, **kwArgs):
|
||||
CurrentDir = os.getcwd()
|
||||
os.chdir(Directory)
|
||||
Function(*args, **kwArgs)
|
||||
os.chdir(CurrentDir)
|
||||
|
||||
|
||||
def CreateDir(Directory):
|
||||
""" return True if operation succesful and False if failed """
|
||||
if not os.path.isfile(Directory):
|
||||
if not os.path.isdir(Directory):
|
||||
os.mkdir(Directory)
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
def CopyFile(Source, Destination):
|
||||
""" raises exception if fails.
|
||||
Implement exception handling else program will abort.
|
||||
"""
|
||||
try:
|
||||
# hard link if on same partition
|
||||
os.link(Source, Destination)
|
||||
except FileExistsError:
|
||||
# FIXME: create a function to compare if two files
|
||||
# are same and use it here
|
||||
pass
|
||||
except OSError:
|
||||
# copy if on different partition
|
||||
shutil.copy2(Source, Destination)
|
||||
except Exception as unknownError:
|
||||
raise unknownError
|
0
Code/__init__.py
Normal file
0
Code/__init__.py
Normal file
@ -1,3 +1,3 @@
|
||||
# portablecode
|
||||
# Python Portable Code
|
||||
|
||||
Python portable code library which can be used in any program without modification.
|
||||
Python portable code library which can be used in any program without modification. Many are simplified, self-explanatory interface for python standard library features.
|
||||
|
Loading…
Reference in New Issue
Block a user