Pewarta Proxy

📋 Server Request & Proxy Logs

Loading active proxy settings...
Developed by Khairul Adha (adhexa.id)
Mengambil log server...

⚙️ Pengaturan Proxy Target

Skip to content

Commit a16f17b

Browse files
committed
ds_store
1 parent 99f79a8 commit a16f17b

5 files changed

Lines changed: 130 additions & 0 deletions

File tree

‎python/intermediate/23_open.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
f = open('photo.jpg', 'r+')
2+
jpgdata = f.read()
3+
f.close()
4+
5+
'''
6+
3가지 에러 가능성
7+
1. To make sure that the file gets closed whether an exception occurs or not, pack it into a with statement
8+
If you want to read the file, pass in r
9+
If you want to read and write the file, pass in r+
10+
If you want to overwrite the file, pass in w
11+
If you want to append to the file, pass in a
12+
13+
2.open does not allow explicit encoding specification in Python 2.x. However, the function io.open is available in both Python 2.x and 3.x (where it is an alias of open), and does the right thing.
14+
15+
3. you must rely on metadata (for example, in HTTP headers) to know the encoding. Increasingly, formats just define the encoding to be UTF-8.
16+
'''
17+
18+
import io
19+
with open('photo.jpg', 'rb') as inf:
20+
jpgdata = inf.read()
21+
22+
if jpgdata.startswith(b'\xff\xd8'):
23+
text = u'This is a JPEG file (%d bytes long)\n'
24+
else:
25+
text = u'This is a random file (%d bytes long)\n'
26+
27+
with io.open('summary.txt', 'w', encoding='utf-8') as outf:
28+
outf.write(text % len(jpgdata))
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'''
2+
In that case you have two choices. The first one is to distribute 2 modules, one for Python 2 and the other for Python 3. The other choice is to modify your current code and make it compatible with both Python 2 and 3.
3+
'''
4+
5+
# The first and most important method is to use __future__ imports. It allows you to import Python 3 functionality in Python 2.
6+
print
7+
# Output:
8+
9+
from __future__ import print_function
10+
print(print)
11+
# Output: <built-in function print>
12+
13+
try:
14+
import urllib.request as urllib_request # for Python 3
15+
except ImportError:
16+
import urllib2 as urllib_request # for Python 2
17+
18+
'''
19+
Now whenever you try to use the modules which are abandoned in Python 3, it raises a NameError like this:
20+
21+
from future.builtins.disabled import *
22+
23+
apply()
24+
# Output: NameError: obsolete Python 2 builtin apply is disabled
25+
External standard-library backports
26+
27+
There are a few packages in the wild which provide Python 3 functionality in Python 2. For instance, we have:
28+
29+
enum pip install enum34
30+
singledispatch pip install singledispatch
31+
pathlib pip install pathlib
32+
'''
33+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'''
2+
generators are data producers
3+
coroutines are data consumers
4+
'''
5+
6+
7+
'''
8+
파이썬 generator, yield
9+
함수로부터 만들어진 제너레이터 객체가 for 루프를 통해 처음 실행될 때 Python은 함수 내에 있는 코드를 yield 키워드를 만나기 전까지 실행하고 첫 번째 루프의 값을 반환하게 됩니다. 다음 루프 때에는 yield 키워드 뒤에 있는 코드를 실행하고 다시 루프를 돌면서 반환할 값이 아예 없을 때까지 계속 같은 과정을 반복하게 됩니다
10+
https://tech.ssut.me/what-does-the-yield-keyword-do-in-python/
11+
12+
Coroutines consume values which are sent to it. A very basic example would be a grep alternative in Python:
13+
14+
http://www.dabeaz.com/coroutines/Coroutines.pdf
15+
'''
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Function caching allows us to cache the return values of a function depending on the arguments. It can save time when an I/O bound function is periodically called with the same arguments.
2+
3+
4+
from functools import lru_cache
5+
6+
@lru_cache(maxsize=32)
7+
def fib(n):
8+
if n < 2:
9+
return n
10+
return fib(n-1) + fib(n-2)
11+
12+
print([fib(n) for n in range(10)])
13+
# Output: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
14+
15+
fib.cache_clear()
16+
17+
from functools import wraps
18+
19+
def memoize(function):
20+
memo = {}
21+
@wraps(function)
22+
def wrapper(*args):
23+
try:
24+
return memo[args]
25+
except KeyError:
26+
rv = function(*args)
27+
memo[args] = rv
28+
return rv
29+
return wrapper
30+
31+
@memoize
32+
def fibonacci(n):
33+
if n < 2: return n
34+
return fibonacci(n - 1) + fibonacci(n - 2)
35+
36+
fibonacci(25)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'''
2+
Context managers allow you to allocate and release resources precisely when you want to. The most widely used example of context managers is the with statement. Suppose you have two related operations which you’d like to execute as a pair, with a block of code in between. Context managers allow you to do specifically that.
3+
'''
4+
5+
'''While comparing it to the first example we can see that a lot of boilerplate code is eliminated just by using with. The main advantage of using a with statement is that it makes sure our file is closed without paying attention to how the nested block exits.'''
6+
7+
from contextlib import contextmanager
8+
9+
@contextmanager
10+
def open_file(name):
11+
f = open(name, 'w')
12+
try:
13+
yield f
14+
finally:
15+
f.close()
16+
17+
with open_file('some_file') as f:
18+
f.write('hola!')

0 commit comments

Comments
 (0)