Skip to content

Commit 02ed695

Browse files
committed
rozne
1 parent 732f8bc commit 02ed695

File tree

19 files changed

+569
-0
lines changed

19 files changed

+569
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env python
2+
3+
import os
4+
import sys
5+
6+
fullpath = 'images/normal.png'
7+
filename = 'hello_world.png'
8+
9+
# headers
10+
print 'Content-Type: application/octet-stream; name="%s"' % filename
11+
print 'Content-Disposition: attachment; filename="%s"' % filename
12+
print "Content-Length: " + str(os.stat(fullpath).st_size)
13+
print # empty line between headers and body
14+
#sys.stdout.flush() # send header faster
15+
16+
try:
17+
# body
18+
with open(fullpath, 'rb') as fo:
19+
print fo.read()
20+
except Exception as e:
21+
print 'Content-type:text/html'
22+
print # empty line between headers and body
23+
print 'Exception:', e

CGI/simple-server-with-different-languages/server.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
server_address = ("", 8000)
66

7+
print(server_address)
8+
79
httpd = HTTPServer(server_address, CGIHTTPRequestHandler)
810
httpd.serve_forever()
911

requests/proxies/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Dla użytkowników TPSA
2+
217.98.20.20:8080 (w3cache.tpnet.pl)
3+
217.98.20.195:8080 (w3cache.tpnet.pl)
4+
5+
Dla użytkowników NETIA
6+
195.114.161.53:8080
7+
195.114.161.54:8000
8+
9+
Dla użytkowników Dialog
10+
62.87.244.55:8080 (w3cache.dialog.net.pl)

requests/stream-chunk-example/main.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/usr/bin/env python3
2+
3+
# example how to use stream
4+
5+
# ---------------------------------------------------------------------
6+
# reading stream in chunks
7+
# ---------------------------------------------------------------------
8+
9+
import requests
10+
11+
url = 'http://httpbin.org/stream/1'
12+
13+
r = requests.get(url, stream=True)
14+
15+
16+
if not r.ok:
17+
print('Status code:', r.status_code)
18+
else:
19+
20+
# empty bytes data
21+
data = b''
22+
23+
for chunk in r.iter_content(32):
24+
if chunk: # filter out keep-alive new chunks
25+
data += chunk
26+
print(chunk.decode('utf-8'), '\n----')
27+
28+
# convert bytes to string and print
29+
print(data.decode('utf-8'))
30+
31+
# ---------------------------------------------------------------------
32+
# writing to file
33+
# ---------------------------------------------------------------------
34+
35+
import requests
36+
37+
url = 'http://httpbin.org/stream/1'
38+
39+
r = requests.get(url, stream=True)
40+
41+
if not r.ok:
42+
print('Status code:', r.status_code)
43+
else:
44+
45+
with open('output.txt', 'wb') as f:
46+
for chunk in r.iter_content(32):
47+
# not sure if `chunk` can be empty
48+
if chunk: # filter out keep-alive new chunks
49+
f.write(chunk)
50+
# flush python buffer and OS buffrer
51+
#f.flush()
52+
#os.fsync(f.fileno())
53+
54+
# ---------------------------------------------------------------------
55+
# using Response.raw and shutil.copyfileobj()
56+
# ---------------------------------------------------------------------
57+
# http://docs.python-requests.org/en/master/api/#requests.Response.raw
58+
# https://docs.python.org/3/library/shutil.html#shutil.copyfileobj
59+
# ---------------------------------------------------------------------
60+
61+
import requests
62+
import shutil
63+
64+
url = 'http://httpbin.org/stream/1'
65+
66+
r = requests.get(url, stream=True)
67+
68+
if not r.ok:
69+
print('Status code:', r.status_code)
70+
else:
71+
72+
with open'output.txt', 'wb') as f:
73+
shutil.copyfileobj(r.raw, f)
74+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"url": "http://httpbin.org/stream/1", "headers": {"Host": "httpbin.org", "Accept-Encoding": "gzip, deflate", "Accept": "*/*", "User-Agent": "python-requests/2.11.1"}, "args": {}, "id": 0, "origin": "83.25.152.4"}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env python3
2+
3+
import tkinter as tk
4+
5+
# --- functions ---
6+
7+
def callback(x):
8+
print(x)
9+
10+
# --- main ---
11+
12+
buttons = [
13+
("1", "2", "3", "+"),
14+
("4", "5", "6", "-"),
15+
("7", "8", "9", "*"),
16+
(".", "0", "=", "/"),
17+
]
18+
19+
root = tk.Tk()
20+
root.title("Calculator")
21+
root['bg'] = 'red'
22+
23+
tk.Entry(root).grid(row=0, columnspan=4, sticky='we')
24+
25+
for r, row in enumerate(buttons, 1):
26+
for c, text in enumerate(row):
27+
b = tk.Button(root, text=text, command=lambda arg=text:callback(arg))
28+
b.grid(row=r, column=c, sticky='news')
29+
30+
for col in range(4):
31+
root.columnconfigure(col, weight=1)
32+
root.rowconfigure(col+1, weight=1)
33+
34+
root.mainloop()
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env python3
2+
3+
import tkinter as tk
4+
5+
# --- functions ---
6+
7+
def callback(x):
8+
print(x)
9+
10+
# --- main ---
11+
12+
buttons = [
13+
("1", "2", "3", "+"),
14+
("4", "5", "6", "-"),
15+
("7", "8", "9", "*"),
16+
(".", "0", "=", "/"),
17+
]
18+
19+
root = tk.Tk()
20+
root.title("Calculator")
21+
22+
tk.Entry(root).pack(fill='x')
23+
24+
frame = tk.Frame(root)
25+
frame.pack(fill='both', expand=True)
26+
frame['bg'] = 'red'
27+
28+
for col in range(4):
29+
frame.columnconfigure(col, weight=1)
30+
frame.rowconfigure(col+1, weight=1)
31+
32+
for r, row in enumerate(buttons, 1):
33+
for c, text in enumerate(row):
34+
b = tk.Button(frame, text=text, command=lambda arg=text:callback(arg))
35+
b.grid(row=r, column=c, sticky='news')
36+
37+
root.mainloop()
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env python3
2+
3+
# http://effbot.org/tkinterbook/canvas.htm
4+
# http://effbot.org/tkinterbook/photoimage.htm
5+
6+
import tkinter as tk
7+
from PIL import Image, ImageTk
8+
9+
# --- constants ---
10+
11+
WIDTH = 800
12+
HEIGHT = 600
13+
14+
# --- functions ---
15+
16+
def on_click():
17+
w = int(c['width'])
18+
h = int(c['height'])
19+
print('minimal size:', w, h)
20+
21+
w = c.winfo_width()
22+
h = c.winfo_height()
23+
print('current size:', w, h)
24+
25+
c.coords(i, w//2, h//2)
26+
c.coords(t, w//2, h//2)
27+
28+
w = c.winfo_reqwidth()
29+
h = c.winfo_reqheight()
30+
print(' req size:', w, h)
31+
32+
# current position + (w//2, h//2)
33+
#c.move(t, w//2, h//2)
34+
35+
def on_resize(event):
36+
# determine the ratio of old width/height to new width/height
37+
w = event.width
38+
h = event.height
39+
print('event size:', w, h)
40+
41+
#w_scale = float(w)/width
42+
#h_scale = float(h)/height
43+
44+
# resize the canvas - doesn't work with `fill` and `expand`
45+
#c.config(width=w, height=h)
46+
47+
c.coords(i, w//2, h//2)
48+
c.coords(t, w//2, h//2)
49+
50+
# rescale all the objects tagged with the "all" tag
51+
#self.scale("all", 0, 0, w_scale,h_scale)
52+
53+
# --- main ---
54+
55+
root = tk.Tk()
56+
57+
c = tk.Canvas(root, width=WIDTH, height=HEIGHT)
58+
c.pack(fill='both', expand=True)
59+
c.bind("<Configure>", on_resize)
60+
61+
# only GIF and PGM/PPM
62+
#photo = tk.PhotoImage(file='test.gif')
63+
64+
# other formats
65+
image = Image.open('test_transparent.png')
66+
photo = ImageTk.PhotoImage(image)
67+
# use in functions - solution for "garbage collector" problem
68+
c.image = photo
69+
70+
# text on image
71+
i = c.create_image((WIDTH//2, HEIGHT//2), image=photo)
72+
t = c.create_text((WIDTH//2, HEIGHT//2), text='Hello World')
73+
74+
# (transparent) image on text
75+
#t = c.create_text((WIDTH//2, HEIGHT//2), text='Hello World')
76+
#i = c.create_image((WIDTH//2, HEIGHT//2), image=photo)
77+
78+
b = tk.Button(root, text='OK', command=on_click)
79+
b.pack()
80+
81+
root.mainloop()
986 Bytes
Loading
1.24 KB
Loading
Loading
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env python3
2+
3+
import tkinter as tk
4+
from PIL import Image, ImageTk
5+
6+
# --- functions ---
7+
8+
def on_click(event=None):
9+
# `command=` calls function without argument (so I set event=None)
10+
# `bind` calls function with one argument
11+
if event:
12+
print("Label clicked (x,y:", event.x, event.y, ')')
13+
else:
14+
print("Button clicked")
15+
16+
# --- main ---
17+
18+
# init
19+
root = tk.Tk()
20+
21+
# load image
22+
image = Image.open("/home/furas/Obrazy/test.png")
23+
photo = ImageTk.PhotoImage(image)
24+
25+
# label with image
26+
l = tk.Label(root, image=photo)
27+
l.pack()
28+
29+
# bind click event to image
30+
l.bind('<Button-1>', on_click)
31+
32+
# button with image binded to the same function
33+
b = tk.Button(root, image=photo, command=on_click)
34+
b.pack()
35+
36+
# button with text closing window
37+
b = tk.Button(root, text="Close", command=root.destroy)
38+
b.pack()
39+
40+
# "start the engine"
41+
root.mainloop()

tkinter/combobox-get-state/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
To get ttk.Combobox state use `c['state'].string` or `str(c['state'])`
2+
3+
See example in `main.py`

tkinter/combobox-get-state/main.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
2+
# http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/ttk-Widget.html
3+
# http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/ttk-state-spec.html
4+
5+
try:
6+
# Python 2
7+
import Tkinter as tk
8+
import ttk
9+
print('Python 2')
10+
except:
11+
# Python 3
12+
import tkinter as tk
13+
import tkinter.ttk as ttk
14+
print('Python 3')
15+
16+
def test():
17+
all_states = ['disabled', 'readonly', 'pressed', 'focus']
18+
19+
print('----------------------------------------------------')
20+
21+
22+
c['state'] = tk.NORMAL
23+
24+
print(' info:', c['state'])
25+
print('compare:', c['state'] == tk.NORMAL, c.cget('state') == tk.NORMAL)
26+
print('instate:', c.instate(all_states), all_states)
27+
print('instate:', c.instate(['disabled']), ['disabled'])
28+
print('instate:', c.instate(['!disabled']), ['!disabled'])
29+
print('state():', c.state())
30+
print(' state:', c.state(all_states))
31+
#print(' state:', c.state([tk.DISABLED]))
32+
#print('state():', c.state())
33+
34+
print('---')
35+
print(' normal:', c['state'].string == tk.NORMAL, str(c['state']) == tk.NORMAL)
36+
print('disabled:', c['state'].string == tk.DISABLED, str(c['state']) == tk.DISABLED)
37+
print('readable:', c['state'].string == tk.READABLE, str(c['state']) == tk.READABLE)
38+
39+
print('----------------------------------------------------')
40+
41+
c['state'] = tk.DISABLED
42+
43+
print(' info:', c['state'])
44+
print('compare:', c['state'] == tk.NORMAL, c.cget('state') == tk.NORMAL)
45+
print('instate:', c.instate(all_states), all_states)
46+
print('instate:', c.instate(['disabled']), ['disabled'])
47+
print('instate:', c.instate(['!disabled']), ['!disabled'])
48+
print('state():', c.state())
49+
print(' state:', c.state(all_states))
50+
print(' state:', c.state([tk.DISABLED]))
51+
print('state():', c.state())
52+
53+
print('---')
54+
print(' normal:', c['state'].string == tk.NORMAL, str(c['state']) == tk.NORMAL)
55+
print('disabled:', c['state'].string == tk.DISABLED, str(c['state']) == tk.DISABLED)
56+
print('readable:', c['state'].string == tk.READABLE, str(c['state']) == tk.READABLE)
57+
58+
59+
root = tk.Tk()
60+
61+
c = ttk.Combobox(root)
62+
c.pack()
63+
64+
print('\n=== start ===\n')
65+
test()
66+
#print('\n=== after ===\n')
67+
#root.after(2000, test)
68+
69+
root.mainloop()

0 commit comments

Comments
 (0)