-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fd05234
commit 5a371c7
Showing
14 changed files
with
226 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,20 @@ | ||
{ | ||
"python.formatting.provider": "black" | ||
"python.formatting.provider": "black", | ||
"workbench.colorCustomizations": { | ||
"activityBar.activeBackground": "#920320", | ||
"activityBar.activeBorder": "#000000", | ||
"activityBar.background": "#920320", | ||
"activityBar.foreground": "#e7e7e7", | ||
"activityBar.inactiveForeground": "#e7e7e799", | ||
"activityBarBadge.background": "#000000", | ||
"activityBarBadge.foreground": "#e7e7e7", | ||
"statusBar.background": "#600215", | ||
"statusBar.foreground": "#e7e7e7", | ||
"statusBarItem.hoverBackground": "#920320", | ||
"titleBar.activeBackground": "#600215", | ||
"titleBar.activeForeground": "#e7e7e7", | ||
"titleBar.inactiveBackground": "#60021599", | ||
"titleBar.inactiveForeground": "#e7e7e799" | ||
}, | ||
"peacock.color": "#600215" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
é is not ascii |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#%% | ||
""" | ||
read file | ||
write to file | ||
redirect print | ||
encoding | ||
change print separator | ||
""" | ||
#%%[markdown] | ||
# # Read an write text data | ||
# %% | ||
# * Read the entire file as a single string | ||
with open("lorem.txt", "rt") as f: | ||
data = f.read() | ||
# %% | ||
data | ||
# %% | ||
# * Iterate over the lines of a file | ||
with open("lorem.txt","rt") as f: | ||
for line in f: | ||
print(line) | ||
|
||
# %% | ||
# * write chunks of text data | ||
with open("ipsum.txt", "wt") as f: | ||
f.write("Donec placerat sit amet arcu nec consectetur.\n") | ||
f.write("Mauris quis nisi vel ipsum mattis lacinia ac ut leo.\n") | ||
f.write("Maecenas volutpat fringilla sapien in euismod.\n") | ||
# %% | ||
# * or redirect a print statement | ||
with open("ipsum.txt", "wt") as f: | ||
print("Etiam eget posuere lacus, sit amet imperdiet odio.", file=f) | ||
# %% | ||
# fails if file is opened in byte mode | ||
with open("ipsum.txt", "wb") as f: | ||
print("Etiam eget posuere lacus, sit amet imperdiet odio.", file=f) | ||
# %% | ||
# * append at the end of file | ||
with open("ipsum.txt", "at") as f: | ||
print("append1", file=f) | ||
f.write("append2") | ||
# %% | ||
#* encoding | ||
# %% | ||
import sys | ||
sys.getdefaultencoding() | ||
# %% | ||
"""Python understands hundreds of text encodings : most common are ascii, latin-1, utf-8 and utf-16. | ||
utf 8 is usually a safe bet if working with web app | ||
ascii : 7 bit characters in the range U+0000 to U+007F | ||
latin-1 : direct mapping of bytes 0-255 to unicode character U+0000 to U+00FF | ||
latin-1 encoding will never produce a decoding error when reading text of a possibly unknown encoding. Reading a file as latin-1 might not product a completely correct text decoding though but if you later write the data back out, the original input data will be preserved""" | ||
# %% | ||
|
||
with open("lorem.txt", "rt", encoding="latin-1") as f: | ||
print(f.read()) | ||
|
||
# %% | ||
# * newline | ||
""" | ||
recognition on newline on Unix : \n | ||
on windows : \r\n | ||
By default python operates on a "universal newline" mode : all common newlines conventions are recognized and transformed into a single newline character (by default \n on unix and \r\n on windows) | ||
If you dont want this translation supply newline='' to open | ||
""" | ||
|
||
# %% | ||
with open("ascii_error.txt", "rt", encoding="ascii") as f: | ||
data = f.read() | ||
# %% | ||
with open("ascii_error.txt", "rt", encoding="ascii", errors="replace") as f: | ||
data = f.read() | ||
data | ||
# %% | ||
# %% | ||
print('first', 'second', 'third', sep="<(-_-)>") | ||
# %% |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#%% | ||
""" | ||
read and write binary data | ||
byte integer value (indexing and iterating) | ||
encode and decode | ||
no conversion writing | ||
""" | ||
#%% | ||
#* basic read write | ||
with open("lorem.txt", "rb") as f: | ||
data = f.read() | ||
# %% | ||
data | ||
# %% | ||
with open("ipsum2.txt", "wb") as f: | ||
data = f.write(b"Hello world") | ||
# %% | ||
# * iterating | ||
# text | ||
t = "Hello world" | ||
for c in t: | ||
print(c) | ||
# %% | ||
# byte: indexing and iterating return integer byte values instead of byte strings | ||
b = b"Hello world" | ||
for c in b: | ||
print(c) | ||
# %% | ||
# * encode and decode | ||
with open("ipsum2.txt", "wb") as f: | ||
text = "Hello world" | ||
f.write(text.encode("utf-8")) | ||
|
||
# %% | ||
with open("ipsum2.txt", "rb") as f: | ||
data = f.read(4) | ||
print(data.decode("utf-8")) | ||
# %% | ||
#* no conversion | ||
import array | ||
nums = array.array("i", [1,2,3,4]) | ||
with open("data.bin", "wb") as f: | ||
f.write(nums) | ||
# %% | ||
a = array.array('i', [0,0,0,0,0,0,0,0]) | ||
with open("data.bin", "rb") as f: | ||
f.readinto(a) | ||
a | ||
# %% |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#%% | ||
with open("exists.txt", "wt") as f: | ||
f.write("BANZAI") | ||
# %% | ||
# * Instead of | ||
import os | ||
if not os.path.exists("exists.txt"): | ||
with open("exists.txt", "wt") as f: | ||
f.write("Hello") | ||
else: | ||
print("Already exists") | ||
# %% | ||
# * Use : | ||
with open("exists.txt", "xt") as f: | ||
f.write("Hello") | ||
# %% | ||
try: | ||
with open("exists.txt", "xt") as f: | ||
f.write("Hello") | ||
except FileExistsError: | ||
print("Already exists") | ||
# %% |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
BANZAI |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"name": "ff", | ||
"cc": { | ||
"name": "jon" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello world |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
Lorem ipsum dolor sit amet, consectetur adipiscing elit. | ||
Maecenas faucibus, nibh et elementum venenatis, risus lorem consectetur lacus, pulvinar lacinia lacus sem eu felis. | ||
Sed in ligula dui. | ||
Phasellus sagittis id nisl nec sollicitudin. | ||
Phasellus posuere neque ut eros consectetur sagittis. | ||
Nunc auctor sodales arcu, eget consequat nibh interdum sed. | ||
Mauris tempor ante vitae nibh convallis, sit amet varius eros consequat. | ||
Integer id varius neque. | ||
Nam egestas ac diam vitae dapibus. | ||
Sed sit amet justo ut nunc hendrerit ultrices quis id mi. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#%% | ||
from collections import Counter | ||
|
||
|
||
# %% | ||
{} + {'a':5} | ||
# %% | ||
round(55,-2) | ||
# %% | ||
0.1.is_integer() | ||
# %% | ||
1.1.is_integer() | ||
# %% | ||
def manipulate_user_input(): | ||
prompt = "Enter a number" | ||
inp = input(prompt) | ||
try: | ||
nb = float(inp) | ||
rd = round(nb,2) | ||
print(f"{nb} rounded to two decimal places is {rd:.2f}") | ||
except ValueError: | ||
print("A number, dumass") | ||
|
||
# %% | ||
manipulate_user_input() | ||
# %% | ||
f"{3**.125:.3f}" | ||
# %% | ||
f"{150000:,.2f}" | ||
|
||
# %% | ||
f"{2.02/10:.0%}" | ||
# %% | ||
(1+2j).conjugate() | ||
# %% | ||
f"{10:#o}" | ||
|
||
# %% |