import numpy as np

"""
Explanation: Strings and Stuff  in Python
End of explanation
"""


s = 'spam'

s,len(s),s[0],s[0:2]

s[::-1]

"""
Explanation: Strings are just arrays of characters
End of explanation
"""


s = 'spam'
e = "eggs"

s + e

s + " " + e

4 * (s + " ") + e

print(4 * (s + " ") + s + " and\n" + e)     # use \n to get a newline with the print function

"""
Explanation: Arithmetic with Strings
End of explanation
"""


"spam" == "good"

"spam" != "good"

"spam" == "spam"

"sp" < "spam"

"spam" < "eggs"

"""
Explanation: You can compare strings
End of explanation
"""


print("This resistor has a value of 100 k\U000003A9")

Ω = 1e3

Ω + np.pi

"""
Explanation: Python supports Unicode characters
You can enter unicode characters directly from the keyboard (depends on your operating system), or you can use the ASCII encoding. 
A list of ASCII encoding can be found here.
For example the ASCII ecoding for the greek capital omega is U+03A9, so you can create the character with \U000003A9
End of explanation
"""


radio_active = "\U00002622"
wink = "\U0001F609"

print(radio_active + wink)

"""
Explanation: Emoji are unicode characters, so you can use them a well (not all OSs will show all characters!)
End of explanation
"""


☢ = 2.345

☢ ** 2    

"""
Explanation: Emoji can not be used as variable names (at least not yet ...)
End of explanation
"""


n = 4

print("I would like " + n + " orders of spam")

print("I would like " + str(n) + " orders of spam")

"""
Explanation: Watch out for variable types!
End of explanation
"""


A = 42
B = 1.23456
C = 1.23456e10
D = 'Forty Two'

"I like the number {0:d}".format(A)

"I like the number {0:s}".format(D)

"The number {0:f} is fine, but not a cool as {1:d}".format(B,A)

"The number {0:.3f} is fine, but not a cool as {1:d}".format(C,A)       # 3 places after decimal

"The number {0:.3e} is fine, but not a cool as {1:d}".format(C,A)       # sci notation

"{0:g} and {1:g} are the same format but different results".format(B,C)

"""
Explanation: Use explicit formatting to avoid these errors
Python string formatting has the form:
{Variable Index: Format Type}  .format(Variable)
End of explanation
"""


"Representation of the number {1:s} - dec: {0:d};  hex: {0:x};  oct: {0:o};  bin: {0:b}".format(A,D)

"""
Explanation: Nice trick to convert number to a different base
End of explanation
"""


import pandas as pd

planet_table = pd.read_csv('Planets.csv')

for idx,val in enumerate(planet_table['Name']):
    
    a = planet_table['a'][idx]
    
    if (a < 3.0):
        Place = "Inner"
        
    else:
        Place = "Outer"
    
    my_string = ("The planet {0:s}, at a distance of {1:.1f} AU, is in the {2:s} solar system"
                .format(val,a,Place))
   
    print(my_string)

"""
Explanation: Formatting is way better than piecing strings together
End of explanation
"""


long_string = (
"""
The planets {0:s} and {1:s} are at a distance
of {2:.1f} AU and {3:.1f} AU from the Sun.
"""
.format(planet_table['Name'][1],planet_table['Name'][4],
        planet_table['a'][1],planet_table['a'][4])
)

print(long_string)

"""
Explanation: Really long strings
End of explanation
"""


import textwrap

lots_of_spam = (s + " ") * 100

print(lots_of_spam)

textwrap.wrap(lots_of_spam, width=70)

"""
Explanation: You can also use the textwrap module
End of explanation
"""


line = "My hovercraft is full of eels"

"""
Explanation: Working with strings
End of explanation
"""


line.replace('eels', 'wheels')

"""
Explanation: Find and Replace
End of explanation
"""


line.center(100)

line.ljust(100)

line.rjust(100, "*")

line2 = "            My hovercraft is full of eels      "

line2.strip()

line3 = "*$*$*$*$*$*$*$*$My hovercraft is full of eels*$*$*$*$"

line3.strip('*$')

line3.lstrip('*$'), line3.rstrip('*$')

"""
Explanation: Justification and Cleaning
End of explanation
"""


line.split()

'_*_'.join(line.split())

' '.join(line.split()[::-1])

"""
Explanation: Splitting and Joining
End of explanation
"""


anotherline = "mY hoVErCRaft iS fUlL oF eEELS"

anotherline.upper()

anotherline.lower()

anotherline.title()

anotherline.capitalize()

anotherline.swapcase()

"""
Explanation: Line Formatting
End of explanation
"""


import re

myline = "This is a test, this in only a test."

print(myline)

"""
Explanation: Regular Expression in Python (re)
End of explanation
"""


regex1 = r"test"

match1 = re.search(regex1, myline)

match1

myline[10:14]

match3 = re.findall(regex1, myline)

match3

"""
Explanation: Raw strings begin with a special prefix (r) and signal Python not to interpret backslashes and other special metacharacters in the string, allowing you to pass them through directly to the regular expression engine.
End of explanation
"""


mynewline = re.sub(regex1, "*TEST*", myline)

mynewline

"""
Explanation: One of the useful tings about regular expressions in Python is using the to search and replace parts of string (re.sub)
End of explanation
"""


golf_file = open("./GOLF/golf_00").read().splitlines()

golf_file

for i in golf_file:
    print(i)

def regex_test_list(mylist, myregex):
    
    for line in mylist:
        
        mytest = re.search(myregex, line)
        
        if (mytest):
            print(line + " YES")
        else:
            print(line + " NOPE")

regex = r"one"

regex_test_list(golf_file, regex)

regex = r"t|n"

regex_test_list(golf_file, regex)

"""
Explanation: RegEx Golf!
End of explanation
"""


import os

os.chdir("./MyData")

my_data_dir = os.listdir()

my_data_dir

for file in my_data_dir:
    
    if file.endswith(".txt"):
        print(file)

for file in my_data_dir:
    
    if file.endswith(".txt"):
        print(os.path.abspath(file))

"""
Explanation: Working with Files and Directories (OS agnostic)

The os package allows you to do operating system stuff without         worrying about what system you are using
End of explanation
"""


import glob

my_files = glob.glob('02_*.fits')

my_files

for file in my_files:
    
    file_size = os.stat(file).st_size
    out_string = "The file {0} as a size of {1} bytes".format(file,file_size)
    
    print(out_string)

"""
Explanation: You can also find files with glob
End of explanation
"""


