Skip to content

Commit

Permalink
rw
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel2409 committed Apr 2, 2021
1 parent fd05234 commit 5a371c7
Show file tree
Hide file tree
Showing 14 changed files with 226 additions and 30 deletions.
19 changes: 18 additions & 1 deletion .vscode/settings.json
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"
}
1 change: 1 addition & 0 deletions ascii_error.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
é is not ascii
77 changes: 77 additions & 0 deletions c5_01_03_rw_text_print.py
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="<(-_-)>")
# %%
49 changes: 49 additions & 0 deletions c5_04_rw_binary_data.py
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
# %%
22 changes: 22 additions & 0 deletions c5_05_writing_to_file_if_not_exist.py
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")
# %%
29 changes: 1 addition & 28 deletions c9_09_class_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class decorator : __get__
"""

#%% [markdown]
# # Class decorators for functions
# # Class decorators
#%%
import types
from functools import wraps
Expand Down Expand Up @@ -131,33 +131,6 @@ def baz():
"""
# %%




























# %%
def profiled2(func):
ncalls = 0
Expand Down
3 changes: 2 additions & 1 deletion c9_09_descriptor_property_class_static.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class and static methods
#%% [markdown]
# # Classic accessor : BAD
# Note : only do this if you want to add extra funcs to get, set and delete
#%% [markdown]

#%%
class Tester:
def __init__(self, val):
self.val = val
Expand Down
Binary file added data.bin
Binary file not shown.
1 change: 1 addition & 0 deletions exists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BANZAI
6 changes: 6 additions & 0 deletions gg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "ff",
"cc": {
"name": "jon"
}
}
Empty file added ipsum.txt
Empty file.
1 change: 1 addition & 0 deletions ipsum2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello world
10 changes: 10 additions & 0 deletions lorem.txt
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.
38 changes: 38 additions & 0 deletions sandbox.py
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}"

# %%

0 comments on commit 5a371c7

Please sign in to comment.