-
Notifications
You must be signed in to change notification settings - Fork 0
/
python
134 lines (93 loc) · 3.93 KB
/
python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
Python:
- split returns a splited dict/ list
- append adds to ans empty array, list
- print "%.nf" ; n spefies precision upto n decimal spaces
- map() =>
>>> squares = []
>>> for x in range(10):
... squares.append(x**2)
>>> squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
same as
squares = [x**2 for x in range(10)]
or
squares = map(lambda x: x**2, range(10))
- map(function f, sequence s,....):
it returns the sequence of type s(default list) modified by the function f.
[IMP! IT CAN TAAKE MORE THAN 1 SEQUENCES]
eg: map(int, "1 3 5") returns a list [1, 3, 5] etc..
- tuple(list) makes a tuple from the list
- x.strip([char]) strips of the end charcters, char, from the list/string x, (default whitespaces)211
- filter(function, sequence) returns a sequence consisting of those items from the sequence for which function(item) is true.
[imp! IT TAKES ONLY 2 ARGS,.. INSTEAD USE LIST COMPREHENSION, see line 49]
- reduce(function, sequence) returns a single value constructed by calling the binary function function on the first two items of the sequence, then on the result and the next item (similar to recursion)
- in module __future__ ,
print(item, sep = , end = )
also for int div: a//b and for decimal div a/b
- lamda x : similar to #define macros
- map() consumes more time than for loop.
- sorted(data_type) returns the same data_type sorted.
- print(time.time()) : prints the time the prom took from its begin to run.
- lists can be:
index and slice , append , delete from , sort and reverse .
- List Comprehensions: derived from map:
sq = [a**2 for a in range(5)]
op = [[x,y,z] for x in range(a+1) for y in range(b+1) for z in range(c+1) if x+y+z!=n]
(ref: https://docs.python.org/2/tutorial/datastructures.html; 5.1.3)
- sorted(list_to_sort, key=itemgetter(2,0,1))
import: from operator import itemgetter
also:
l.sort(key=lambda x: x[2])
- join(): The method join() returns a string in which the string elements of sequence have been joined by str separator.
str.join(seq/list)
STRINGS:
- str.swapcase() : swaps the case of each letter in a str
GTK :
ELements/ Wigdets
- window
- button
- First add padding to you window to make it clean
window.set_border_width(px)
- add(elm) to add the elem
- signals in Gtk : (use connect to connect them to functions)
windows => delete-event,
button => clicked ,
- Box : Gtk.Box(orentation="eg. Gtk.Orentation.HORIZONTAL", spacing=" eg. 100 ")
->
- Grid layout : Gtk.Grid()
-> can be refered by rows and cols
-> grid.attach(elmenent, column, row, width/col_span, height/row_span )
-> grid atatch_next_to( elem_to_add, ref_elem, position , width, height )
Gtk.Postion.BOTTOM
Gtk.Postion.RIGHT
Gtk.Postion.LEFT
- ListBox: Gtk.ListBox() [a box f very helpful for forms]
-> listbox.set_selection_mode(Gtk.Seelction.NONE) ==> SO as to allow user to selct nothing (also can use .MULTIPLE)
-> create rows in ListBox by using Gtk.ListBoxRow(), use .add() to add elem to it
-> CheckBox: Gtk.CheckButton() Adds a mcq waala check box
-> ToggleSwitch : Gtk.Switch() Adds 1/0 switch
- Stack + StackSwitcher : Gtk.Stack() and Gtk.StackSwitcher()
-> Initiale the stack:
stack.set_transition_type(Gtk.StackTranstionType.SLIDE_LEFT_RIGHT)
stack.set_transiton_duration(in ms)
-> to add elem to a stack var in a new stack
stack.add_titled(elem, "id", "Stack Name")
-> to add stck switcher
switcher = Gtk.StackSwitcher()
- Odd stuff :
New-style classes inherit from object, or from another new-style class.
class NewStyleClass(object):
pass
class AnotherNewStyleClass(NewStyleClass):
pass
Old-style classes don't.
class OldStyleClass():
pass
super() doesn't work with old style classes. As :
>>instance = OldStyleClass()
>>issubclass(instance.__class__, object)
False
instead use :
class OldStyleClass(AnotherOldClass, object):
so as to have NewStyleClass functionality/.
Or use C++ style inheritance.