https://docs.python.org/2/library/functions.html#property
http://www.python-course.eu/python3_properties.php
Thursday, November 5, 2015
Wednesday, November 4, 2015
negate the set of characters in python regular expression: [^\/ab]
https://github.com/tartley/python-regex-cheatsheet/blob/master/cheatsheet.rst
After '[', enclose a set, the only special chars are:
] End the set, if not the 1st char
- A range, eg. a-c matches a, b or c
^ Negate the set only if it is the 1st char
After '[', enclose a set, the only special chars are:
] End the set, if not the 1st char
- A range, eg. a-c matches a, b or c
^ Negate the set only if it is the 1st char
start and end anchor in python regular expression
http://www.regular-expressions.info/anchors.html
^ and $ are different breed. They are start and end anchors. They do not match any character at all. They just ensure that the following ir preceding character starts from the beginning or the end of a line.
^ and $ are different breed. They are start and end anchors. They do not match any character at all. They just ensure that the following ir preceding character starts from the beginning or the end of a line.
endswith in python
if filename.endswith(".pdf"):
http://stackoverflow.com/questions/12187799/regular-expression-in-python-wont-match-end-of-a-string
http://stackoverflow.com/questions/12187799/regular-expression-in-python-wont-match-end-of-a-string
types library in python
https://docs.python.org/2/library/types.html
mport re
import types
str = 'you are'
#str = 'you are my are not they are'
RE = re.compile(r'are')
match = RE.findall(str)
if match:
if type(match) is list:
print match
else:
print match.group()
else:
print "No match found"
mport re
import types
str = 'you are'
#str = 'you are my are not they are'
RE = re.compile(r'are')
match = RE.findall(str)
if match:
if type(match) is list:
print match
else:
print match.group()
else:
print "No match found"
private variable in python: comvention to use: _var_name ( _name, _age)
“Private” instance variables that cannot be accessed except from inside an object don’t exist in Python.
Generally private variable is accessible only from inside the object.
Since there is a valid use-case for class-private members (namely to avoid name clashes of names with names defined by subclasses), there is limited support for such a mechanism, called name mangling. Any identifier of the form __spam (at least two leading underscores, at most one trailing underscore) is textually replaced with _classname__spam, where classname is the current class name with leading underscore(s) stripped. This mangling is done without regard to the syntactic position of the identifier, as long as it occurs within the definition of a class.
Generally private variable is accessible only from inside the object.
Since there is a valid use-case for class-private members (namely to avoid name clashes of names with names defined by subclasses), there is limited support for such a mechanism, called name mangling. Any identifier of the form __spam (at least two leading underscores, at most one trailing underscore) is textually replaced with _classname__spam, where classname is the current class name with leading underscore(s) stripped. This mangling is done without regard to the syntactic position of the identifier, as long as it occurs within the definition of a class.
file operation in python
http://stackoverflow.com/questions/125703/how-do-i-modify-a-text-file-in-python
#Unfortunately there is no way to insert into the middle of a file without re-writing it.
import sys
import fileinput
# replace all occurrences of 'sit' with 'SIT' and insert a line after the 5th
for i, line in enumerate(fileinput.input('C:/Users/test/Desktop/test.txt', inplace=1)):
sys.stdout.write(line.replace('sit', 'SIT')) # replace 'sit' and write
# if i == 4: sys.stdout.write('\n') # write a blank line after the 5th line
#Unfortunately there is no way to insert into the middle of a file without re-writing it.
import sys
import fileinput
# replace all occurrences of 'sit' with 'SIT' and insert a line after the 5th
for i, line in enumerate(fileinput.input('C:/Users/test/Desktop/test.txt', inplace=1)):
sys.stdout.write(line.replace('sit', 'SIT')) # replace 'sit' and write
# if i == 4: sys.stdout.write('\n') # write a blank line after the 5th line
Tuesday, November 3, 2015
named group and non-capturing group in python
https://docs.python.org/2/howto/regex.html#regex-howto
named group is just an extension of regular expression group by adding the prefix ?P<group-name> in the beginning of the left parenthesis: example:
str = my name is hello !
pattern = ^my\s(?P<group-name>\w\w\w\w)\s.+
match.group('group-name') = name
named group is just an extension of regular expression group by adding the prefix ?P<group-name> in the beginning of the left parenthesis: example:
str = my name is hello !
pattern = ^my\s(?P<group-name>\w\w\w\w)\s.+
match.group('group-name') = name
optional argument passing convention: inside parenthesis: [-h]
usage: bug-update.py [-h]
changelist branch_path [action_flag [action_flag ...]]
bug-update.py: error: too few arguments
changelist branch_path [action_flag [action_flag ...]]
bug-update.py: error: too few arguments
sample argument parsing without library
#!/usr/bin/env python
import sys, os
import subprocess
import argparse
# Descriptive argument parsing using argparse
parser = argparse.ArgumentParser()
parser.add_argument('file_name')
args, unknown = parser.parse_known_args()
#print args
#print unknown
help_string = """
Purpose of the script:
1. make one or more file executable
Usage: python change_user <file_name or wild card for file names>
Note:
Arguments:
file_name: file_name = arg1
Help: make_exe.py help
"""
# example command = chmod +x *.py
CMD = ['chmod']
if ((len(sys.argv) > 1) and (sys.argv[1]== "help")):
print help_string
quit()
# 1 arguments required
if ((len(sys.argv)) < 2):
print "Not enough arguments provided."
exit(2)
arg1 = sys.argv[1].decode('string-escape')
print sys.argv[1]
print "HERE IS THE PRINT: " + arg1
status_success = 'operation completed successfully'
status_error = 'operation failed'
class CommandError(Exception):
def __init__(self, cmd, retcode, out):
self.cmd = cmd
self.retcode = retcode
self.out = out
Exception.__init__(
self,
'Command "%s" failed with retcode %s (%s)' % (' '.join(cmd), retcode, out,)
)
def read_cmd_output(args, input=None, keepends=False, **kw):
"""Read the output of a the given command."""
return read_output(CMD + args, input=input, keepends=keepends, **kw)
def read_cmd_lines(args, keepends=False, **kw):
"""Return the lines output by p4 command.
Return as single lines, with newlines stripped off."""
return read_cmd_output(args, keepends=True, **kw).splitlines(keepends)
def read_output(cmd, input=None, keepends=False, **kw):
if input:
stdin = subprocess.PIPE
else:
stdin = None
p = subprocess.Popen(
cmd, stdin=stdin, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kw
)
(out, err) = p.communicate(input)
retcode = p.wait()
if retcode:
raise CommandError(cmd, retcode, out)
if not keepends:
out = out.rstrip('\n\r')
return out + err
def base_function():
validate = os.path.exists(arg1)
if validate:
out = read_cmd_output(
['+x', arg1],
)
# display output
if out != "":
print out
print status_success
else:
print status_error
def main(args):
base_function()
if __name__ == '__main__':
main(sys.argv[1:])
import sys, os
import subprocess
import argparse
# Descriptive argument parsing using argparse
parser = argparse.ArgumentParser()
parser.add_argument('file_name')
args, unknown = parser.parse_known_args()
#print args
#print unknown
help_string = """
Purpose of the script:
1. make one or more file executable
Usage: python change_user <file_name or wild card for file names>
Note:
Arguments:
file_name: file_name = arg1
Help: make_exe.py help
"""
# example command = chmod +x *.py
CMD = ['chmod']
if ((len(sys.argv) > 1) and (sys.argv[1]== "help")):
print help_string
quit()
# 1 arguments required
if ((len(sys.argv)) < 2):
print "Not enough arguments provided."
exit(2)
arg1 = sys.argv[1].decode('string-escape')
print sys.argv[1]
print "HERE IS THE PRINT: " + arg1
status_success = 'operation completed successfully'
status_error = 'operation failed'
class CommandError(Exception):
def __init__(self, cmd, retcode, out):
self.cmd = cmd
self.retcode = retcode
self.out = out
Exception.__init__(
self,
'Command "%s" failed with retcode %s (%s)' % (' '.join(cmd), retcode, out,)
)
def read_cmd_output(args, input=None, keepends=False, **kw):
"""Read the output of a the given command."""
return read_output(CMD + args, input=input, keepends=keepends, **kw)
def read_cmd_lines(args, keepends=False, **kw):
"""Return the lines output by p4 command.
Return as single lines, with newlines stripped off."""
return read_cmd_output(args, keepends=True, **kw).splitlines(keepends)
def read_output(cmd, input=None, keepends=False, **kw):
if input:
stdin = subprocess.PIPE
else:
stdin = None
p = subprocess.Popen(
cmd, stdin=stdin, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kw
)
(out, err) = p.communicate(input)
retcode = p.wait()
if retcode:
raise CommandError(cmd, retcode, out)
if not keepends:
out = out.rstrip('\n\r')
return out + err
def base_function():
validate = os.path.exists(arg1)
if validate:
out = read_cmd_output(
['+x', arg1],
)
# display output
if out != "":
print out
print status_success
else:
print status_error
def main(args):
base_function()
if __name__ == '__main__':
main(sys.argv[1:])
argparse documentation
https://docs.python.org/2/library/argparse.html#action
https://docs.python.org/3/library/argparse.html
http://stackoverflow.com/questions/7427101/dead-simple-argparse-example-wanted-1-argument-3-results
http://stackoverflow.com/questions/12818146/python-argparse-ignore-unrecognised-arguments
https://docs.python.org/2/howto/argparse.html
#description and epilog (additional message such as script objective):
https://docs.python.org/2/library/argparse.html#epilog
when -h or --help is used: description shows at the top and epilog show at the bottop of the usual message.
text without description overrides the script file name:
parser = argparse.ArgumentParser('My first argparse attempt')
C:\Windows\System32>C:\Users\test\Desktop\a.py
usage: My first argparse attempt [-h] [--SeeMan SEEMAN] sdsd
My first argparse attempt: error: too few arguments
#parse undefined args from command line:
parser = argparse.ArgumentParser()
args, unknown = parser.parse_known_args()
in place of:
args = = parser.parse_args()
#argument type check in argparse:
https://docs.python.org/2/library/argparse.html#type
https://docs.python.org/3/library/argparse.html
http://stackoverflow.com/questions/7427101/dead-simple-argparse-example-wanted-1-argument-3-results
http://stackoverflow.com/questions/12818146/python-argparse-ignore-unrecognised-arguments
https://docs.python.org/2/howto/argparse.html
add_mutually_exclusive_group:
http://www.frattv.com/python-argparse-command-line-argument-that-can-be-either-named-or-positional/#description and epilog (additional message such as script objective):
https://docs.python.org/2/library/argparse.html#epilog
when -h or --help is used: description shows at the top and epilog show at the bottop of the usual message.
text without description overrides the script file name:
parser = argparse.ArgumentParser('My first argparse attempt')
C:\Windows\System32>C:\Users\test\Desktop\a.py
usage: My first argparse attempt [-h] [--SeeMan SEEMAN] sdsd
My first argparse attempt: error: too few arguments
#parse undefined args from command line:
parser = argparse.ArgumentParser()
args, unknown = parser.parse_known_args()
in place of:
args = = parser.parse_args()
#argument type check in argparse:
https://docs.python.org/2/library/argparse.html#type
comparison operators in python
http://www.tutorialspoint.com/python/python_basic_operators.htm
Operators are the constructs which can manipulate the value of operands.
Consider the expression 4 + 5 = 9. Here, 4 and 5 are called operands and + is called operator.
[ Show Example ]
Assume variable a holds 10 and variable b holds 20, then −
[ Show Example ]
[ Show Example ]
a = 0011 1100
b = 0000 1101
-----------------
a&b = 0000 1100
a|b = 0011 1101
a^b = 0011 0001
~a = 1100 0011
There are following Bitwise operators supported by Python language
[ Show Example ]
[ Show Example ]
Used to reverse the logical state of its operand.
[ Show Example ]
[ Show Example ]
[ Show Example ]
Operators are the constructs which can manipulate the value of operands.
Consider the expression 4 + 5 = 9. Here, 4 and 5 are called operands and + is called operator.
Types of Operator
Python language supports the following types of operators.- Arithmetic Operators
- Comparison (Relational) Operators
- Assignment Operators
- Logical Operators
- Bitwise Operators
- Membership Operators
- Identity Operators
Python Arithmetic Operators
Assume variable a holds 10 and variable b holds 20, then −[ Show Example ]
Operator | Description | Example |
---|---|---|
+ Addition | Adds values on either side of the operator. | a + b = 30 |
- Subtraction | Subtracts right hand operand from left hand operand. | a – b = -10 |
* Multiplication | Multiplies values on either side of the operator | a * b = 200 |
/ Division | Divides left hand operand by right hand operand | b / a = 2 |
% Modulus | Divides left hand operand by right hand operand and returns remainder | b % a = 0 |
** Exponent | Performs exponential (power) calculation on operators | a**b =10 to the power 20 |
// | Floor Division - The division of operands where the result is the quotient in which the digits after the decimal point are removed. | 9//2 = 4 and 9.0//2.0 = 4.0 |
Python Comparison Operators
These operators compare the values on either sides of them and decide the relation among them. They are also called Relational operators.Assume variable a holds 10 and variable b holds 20, then −
[ Show Example ]
Operator | Description | Example |
---|---|---|
== | If the values of two operands are equal, then the condition becomes true. | (a == b) is not true. |
!= | If values of two operands are not equal, then condition becomes true. | |
<> | If values of two operands are not equal, then condition becomes true. | (a <> b) is true. This is similar to != operator. |
> | If the value of left operand is greater than the value of right operand, then condition becomes true. | (a > b) is not true. |
< | If the value of left operand is less than the value of right operand, then condition becomes true. | (a < b) is true. |
>= | If the value of left operand is greater than or equal to the value of right operand, then condition becomes true. | (a >= b) is not true. |
<= | If the value of left operand is less than or equal to the value of right operand, then condition becomes true. | (a <= b) is true. |
Python Assignment Operators
Assume variable a holds 10 and variable b holds 20, then −[ Show Example ]
Operator | Description | Example |
---|---|---|
= | Assigns values from right side operands to left side operand | c = a + b assigns value of a + b into c |
+= Add AND | It adds right operand to the left operand and assign the result to left operand | c += a is equivalent to c = c + a |
-= Subtract AND | It subtracts right operand from the left operand and assign the result to left operand | c -= a is equivalent to c = c - a |
*= Multiply AND | It multiplies right operand with the left operand and assign the result to left operand | c *= a is equivalent to c = c * a |
/= Divide AND | It divides left operand with the right operand and assign the result to left operand | c /= a is equivalent to c = c / ac /= a is equivalent to c = c / a |
%= Modulus AND | It takes modulus using two operands and assign the result to left operand | c %= a is equivalent to c = c % a |
**= Exponent AND | Performs exponential (power) calculation on operators and assign value to the left operand | c **= a is equivalent to c = c ** a |
//= Floor Division | It performs floor division on operators and assign value to the left operand | c //= a is equivalent to c = c // a |
Python Bitwise Operators
Bitwise operator works on bits and performs bit by bit operation. Assume if a = 60; and b = 13; Now in binary format they will be as follows −a = 0011 1100
b = 0000 1101
-----------------
a&b = 0000 1100
a|b = 0011 1101
a^b = 0011 0001
~a = 1100 0011
There are following Bitwise operators supported by Python language
[ Show Example ]
Operator | Description | Example |
---|---|---|
& Binary AND | Operator copies a bit to the result if it exists in both operands | (a & b) (means 0000 1100) |
| Binary OR | It copies a bit if it exists in either operand. | (a | b) = 61 (means 0011 1101) |
^ Binary XOR | It copies the bit if it is set in one operand but not both. | (a ^ b) = 49 (means 0011 0001) |
~ Binary Ones Complement | It is unary and has the effect of 'flipping' bits. | (~a ) = -61 (means 1100 0011 in 2's complement form due to a signed binary number. |
<< Binary Left Shift | The left operands value is moved left by the number of bits specified by the right operand. | a << = 240 (means 1111 0000) |
>> Binary Right Shift | The left operands value is moved right by the number of bits specified by the right operand. | a >> = 15 (means 0000 1111) |
Python Logical Operators
There are following logical operators supported by Python language. Assume variable a holds 10 and variable b holds 20 then[ Show Example ]
Used to reverse the logical state of its operand.
Python Membership Operators
Python’s membership operators test for membership in a sequence, such as strings, lists, or tuples. There are two membership operators as explained below[ Show Example ]
Operator | Description | Example |
---|---|---|
in | Evaluates to true if it finds a variable in the specified sequence and false otherwise. | x in y, here in results in a 1 if x is a member of sequence y. |
not in | Evaluates to true if it does not finds a variable in the specified sequence and false otherwise. | x not in y, here not in results in a 1 if x is not a member of sequence y. |
Python Identity Operators
Identity operators compare the memory locations of two objects. There are two Identity operators explained below:[ Show Example ]
Operator | Description | Example |
---|---|---|
is | Evaluates to true if the variables on either side of the operator point to the same object and false otherwise. | x is y, here is results in 1 if id(x) equals id(y). |
is not | Evaluates to false if the variables on either side of the operator point to the same object and true otherwise. | x is not y, here is not results in 1 if id(x) is not equal to id(y). |
Python Operators Precedence
The following table lists all operators from highest precedence to lowest.[ Show Example ]
Operator | Description | |
---|---|---|
** | Exponentiation (raise to the power) | |
~ + - | Ccomplement, unary plus and minus (method names for the last two are +@ and -@) | |
* / % // | Multiply, divide, modulo and floor division | |
+ - | Addition and subtraction | |
>> << | Right and left bitwise shift | |
& | Bitwise 'AND' | |
^ | | Bitwise exclusive `OR' and regular `OR' | |
<= < > >= | Comparison operators | |
<> == != | Equality operators | |
= %= /= //= -= += *= **= | Assignment operators | |
is is not | Identity operators | |
in not in | Membership operators | |
not or and | Logical operators |
no private variable in python
https://docs.python.org/2/tutorial/classes.html#tut-private
“Private” instance variables that cannot be accessed except from inside an object don’t exist in Python.
However, there is a convention that is followed by most Python code: a name prefixed with an underscore (e.g. _spam) should be treated as a non-public part of the API (whether it is a function, a method or a data member). It should be considered an implementation detail and subject to change without notice.
Since there is a valid use-case for class-private members (namely to avoid name clashes of names with names defined by subclasses), there is limited support for such a mechanism, called name mangling. Any identifier of the form __spam (at least two leading underscores, at most one trailing underscore) is textually replaced with _classname__spam, where classname is the current class name with leading underscore(s) stripped. This mangling is done without regard to the syntactic position of the identifier, as long as it occurs within the definition of a class.
Virtual class:
The virtual function provides the ability to define a function in a base class and have a function of the same name and type in a derived class called when a user calls the base class function.
In C++ terminology, normally class members (including the data members) are public (except see below Private Variables and Class-local References), and all member functions are virtual.
“Private” instance variables that cannot be accessed except from inside an object don’t exist in Python.
However, there is a convention that is followed by most Python code: a name prefixed with an underscore (e.g. _spam) should be treated as a non-public part of the API (whether it is a function, a method or a data member). It should be considered an implementation detail and subject to change without notice.
Since there is a valid use-case for class-private members (namely to avoid name clashes of names with names defined by subclasses), there is limited support for such a mechanism, called name mangling. Any identifier of the form __spam (at least two leading underscores, at most one trailing underscore) is textually replaced with _classname__spam, where classname is the current class name with leading underscore(s) stripped. This mangling is done without regard to the syntactic position of the identifier, as long as it occurs within the definition of a class.
Virtual class:
The virtual function provides the ability to define a function in a base class and have a function of the same name and type in a derived class called when a user calls the base class function.
In C++ terminology, normally class members (including the data members) are public (except see below Private Variables and Class-local References), and all member functions are virtual.
Monday, November 2, 2015
try false in python: Vald words are "True" and "False":
Return value May be: true and false
Subscribe to:
Posts (Atom)