A Cheatsheet for Python Built-in Functions (Part 2: C-D)
GRREF
Friday, June 18, 2021
A Cheatsheet for Python Built-in Functions (Part 2: C-D)
# *** callable() ***
def func0():
pass
print(callable(func0)) # Output: True
# *** chr() ***
print(chr(65)) # Output: A
# *** classmethod() ***
# Note: The new @classmethod decorator should be used instead of
# the built-in function classmethod().
class MyClass1:
myProperty = 1234
def myFunc(self):
print(self.myProperty)
obj0 = MyClass1()
obj0.myFunc()
MyClass1.myFunc = classmethod(MyClass1.myFunc)
MyClass1.myFunc() # myFunc is used as a class method (vs. instance method)
# *** compile() ***
myCode = '_a = 10 \n_b = 20 \nmySum = _a + _b \nprint("mySum =", mySum)'
myCodeObject = compile(myCode, 'myCodeString', 'exec')
exec(myCodeObject) # Output: 30
# *** complex() ***
print(complex(1, 2)) # Output: (1+2j)
# *** delattr() ***
class MyClass2:
myProperty1 = 1234
myProperty2 = 'ABC'
myObj2 = MyClass2()
print(myObj2.myProperty2)
delattr(MyClass2, 'myProperty2')
#print(myObj2.myProperty2) # Error: AttributeError: 'MyClass2' object has no attribute 'myProperty2'
# *** dict() ***
myDic1 = dict(key1 = "val1", key2 = "val2", key3 = "val3")
print(myDic1) # Output: {'key1': 'val1', 'key2': 'val2', 'key3': 'val3'}
# *** dir() ***
class MyClass3:
myProperty1 = 1234
def myMethod1():
pass
print(dir(MyClass3)) # Output:
"""
'__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__',
'__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__',
'__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
'__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'myMethod1', 'myProperty1']
"""
# *** divmod() ***
print(divmod(7, 3)) # Output: (2, 1)
# Note: The output is a tuple:
# divmod(dividend, divisor) => (quotient, reminder)
Wednesday, June 16, 2021
A Cheatsheet for Python Built-in Functions (Part 1: A-B)
A Cheatsheet for Python Built-in Functions (Part 1: A-B)
x1 = -1.5
x2 = 1.5
x3 = 255
b1 = x1 == x2
b2 = x1 == -x2
# *** abs() ***
print(abs(x1)) # Output: 1.5
# *** all() ***
print(all([not b1, b2])) # Output: True
# *** any() ***
print(any([b1, b2])) # Output: True
# *** ascii() ***
print(ascii("This is R in Farsi: ر")) # Output: 'This is R in Farsi: \u0631'
print(ascii(set([b1, b2]))) # Output: {False, True}
# *** bin() ***
print(bin(x3)) # Output: 0b11111111
# *** bool() ***
print(bool(x3), bool(x2), bool(x1)) # Output: True True True
print(bool(0), bool(None), bool([]), bool(()), bool({})) # Output: False False False False False
# *** bytearray() ***
ba1 = bytearray([b1, b2])
print(ba1) # Output: bytearray(b'\x00\x01')
ba2 = bytearray("TEST", encoding='utf8')
print(ba2) # Output: bytearray(b'TEST')
ba3 = bytearray(8)
print(ba3) # Output: bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
# *** bytes() ***
ba1 = bytes([b1, b2])
print(ba1) # Output: b'\x00\x01'
ba2 = bytes("TEST", encoding='utf8')
print(ba2) # Output: b'TEST'
ba3 = bytes(8)
print(ba3) # Output: '\x00\x00\x00\x00\x00\x00\x00\x00'
Declare Arrays in Python
Declare Arrays in Python
A Python List can be considered as a Dynamic Array. The following examples present how to declare an Array in Python.
arr1 = [0] * 8
print(arr1)
# Output: [0, 0, 0, 0, 0, 0, 0, 0]
arr2 = [""] * 8
print(arr2)
# Output: ['', '', '', '', '', '', '', '']
arr3 = [None] * 8
print(arr3)
# Output: [None, None, None, None, None, None, None, None]
arr4 = [None for i in range(0, 8)]
print(arr4)
# Output: [None, None, None, None, None, None, None, None]
A Cheatsheet for Python Lists
A Cheatsheet for Python Lists
# *** Creating a list ***
list1 = []
list2 = [1, "item1", 2.2]
list3 = [list1, list2, "a text", 1234]
print(list3[1][1])
# Output: item1
print(list3[-1]) # negative index are counted from the end.
# Output: 1234
#
# *** Length of a list ***
len3 = len(list3)
print(len3)
# Output: 4
#
# *** Appending new list items ***
for i in range(10, 14):
list3.append(i)
list3.append([t for t in range(0, 10, 3)])
print(list3)
# Output: [[], [1, 'item1', 2.2], 'a text', 1234, 10, 11, 12, 13, [0, 3, 6, 9]]
print(list3[-1]) # negative index are counted from the end.
# Output: [0, 3, 6, 9]
#
# Inserting new items into a list.
print(list2)
# Output: [1, 'item1', 2.2]
list2.insert(2, 222)
print(list2)
# Output: [1, 'item1', 222, 2.2]
#
# Extending a list.
list2.extend([t for t in range(0, 10, 3)])
print(list2)
# Output: [1, 'item1', 222, 2.2, 0, 3, 6, 9]
#
# Removing an items from a list.
list2.remove("item1")
print(list2)
# Output: [1, 222, 2.2, 0, 3, 6, 9]
#
# Using pop method to get and remove an item.
val1 = list2.pop()
print(val1)
# Output: 9
val2 = list2.pop(2) # index.
print(val2)
# Output: 2.2
#
# Slicing a list.
list4 = ["A", "B", "C", "D", "E", "F", "G"]
print(list4[:2])
# Output: ['A', 'B']
print(list4[:-2])
# Output: ['A', 'B', 'C', 'D', 'E']
print(list4[2:])
# Output: ['C', 'D', 'E', 'F', 'G']
print(list4[2:4])
# Output: ['C', 'D']
print(list4[::-1])
# Output: [['G', 'F', 'E', 'D', 'C', 'B', 'A']
Thursday, December 31, 2020
Multiton
Multiton
Design Patterns
Creational Design Patterns
Design Patterns
Creational Design Patterns
In computer programming, the Multiton Pattern is a design pattern which generalizes the singleton pattern in two different ways:
1) On copy of the corresponding Singleton for each data type,
2) On copy of the corresponding Singleton for each value of a parameter.
Where
the Singleton Pattern permits only one instance of a class to be
instantiated, the Multiton Pattern allows multiple instances of a class
to be instantiated.
Functional Programming
Functional Programming is a computer programming paradigm in which the constituents of a computer program are functions. In the workflow of the program, you would find functions calling functions in a way that in one stack of calls you might observe tens or even hundreds of nested function calls. Functional Programming implementations make many complex algorithms be programmed easier than many other paradigms, however, its performance (speed of computer program executing) may be lower than those paradigms.
Tuesday, May 7, 2019
GP :: Purchase Order Number
Purchase Order Number
SELECT PORDNMBR [Order ID], * FROM PM10000 WITH(nolock) WHERE DEX_ROW_TS > '2019-05-01';
SELECT PORDNMBR [Order ID], * FROM PM20000 WITH(nolock) WHERE DEX_ROW_TS > '2019-05-01';
SELECT PORDNMBR [Order ID], * FROM PM30200 WITH(nolock) WHERE DEX_ROW_TS > '2019-05-01';
SELECT PORDNMBR [Order ID], * FROM PM10000 WITH(nolock) WHERE DEX_ROW_TS > '2019-05-01';
SELECT PORDNMBR [Order ID], * FROM PM20000 WITH(nolock) WHERE DEX_ROW_TS > '2019-05-01';
SELECT PORDNMBR [Order ID], * FROM PM30200 WITH(nolock) WHERE DEX_ROW_TS > '2019-05-01';
Subscribe to:
Posts (Atom)