http://www.python-course.eu/pipes.php
Friday, October 30, 2015
stdin, stdout, and stderr in python
these are part of standard operating system design. When a process starts, it initializes three essential handles to read write data: stdin, stdout, and stderror. if a program want to use these handles, they can use it.
capitalize function: capitalizes first character of a word: Not very reliable function
Use .title() for the same purpose
using strip()
.strip() removes leading and trailing spaces.
a = " name here "
a.strip() => "name here"
a = " name there ".strip() => a = "name there"
a = " name here "
a.strip() => "name here"
a = " name there ".strip() => a = "name there"
swap variable in python
a,b = 5,6
a,b = b,a
=> a = 6, b = 5
a,b = b,a
=> a = 6, b = 5
variable name does not start with a number
1a = 5 is not allowed.
rounding in python
Rounding in python is different than school math where .5 or higher adds 1.
round(8.5) = 8 rather than 9.
round(8.5) = 8 rather than 9.
multi line command or function on command line
if a command is not complete on command line. enter causes "..."(three dots) on the console. It means type remaining part of the command.
>>>def f():
... print "Hello"
...
>>>f()
... => continued command
>>>def f():
... print "Hello"
...
>>>f()
... => continued command
r in regular expression
the 'r' means the the following is a "raw string".
ie. backslash characters are treated literally instead of signifying special treatment of the following character.
so
and
another way to write it would be
'\n'
is a single newlineand
r'\n'
is two characters - a backslash and the letter 'n'another way to write it would be
'\\n'
because the first backslash escapes the second
an equivalent way of writing this
print (re.sub(r'(\b\w+)(\s+\1\b)+', r'\1', 'hello there there'))
is
print (re.sub('(\\b\\w+)(\\s+\\1\\b)+', '\\1', 'hello there there'))
Because of the way Python treats characters that are not valid escape characters, not all of those double backslashes are necessary - eg
'\s'=='\\s'
however the same is not true for '\b'
and '\\b'
. My preference is to be explicit and double all the backslashes.regular expression in python
Note:
1. meaning of re keywords are very unusual than their regular meaning.
2. in general, matching text= match.group()
3. Regular expression: deals with text search in a string
\w = any word character match
+ = unlimited repeat of the preceding pattern-type (such as . \w \d ...) if one instance is found .
* = unlimited repeat of the preceding pattern-type (such as . \w \d ...) even if no instance is found .
https://docs.python.org/2/library/re.html
https://docs.python.org/2/howto/regex.html#regex-howto
https://developers.google.com/edu/python/regular-expressions?hl=en
Regular expressions use the backslash character ('\') to indicate special forms or to allow special characters to be used without invoking their special meaning.
#Square bracket: => group of characters to match: [ ]: so [abc] matches 'a' or 'b' or 'c
#Group extraction: add parenthesis ( )
=====
=====
Regular expression in programming: Elaborate feature on: Find a substring in a given string
1. It is related with finding something in a string.
2. Modern regular expression has heavy extended capabilities beyond find a substring.
3. There are specific mechanism to use regular expression capabilities of different langulages and operating systems.
Regular expression (definition): an expression to define criteria to find match
The special characters are for regular expression (RE):
'.' (Dot.) In the default mode, this matches any character except a newline. If the DOTALL flag has been specified, this matches any character including a newline.
'^' (Caret.) Matches the start of the string, and in MULTILINE mode also matches immediately after each newline.
'$' Matches the end of the string or just before the newline at the end of the string, and in MULTILINE mode also matches before a newline. foo matches both ‘foo’ and ‘foobar’, while the regular expression foo$ matches only ‘foo’. More interestingly, searching for foo.$ in 'foo1\nfoo2\n' matches ‘foo2’ normally, but ‘foo1’ in MULTILINE mode; searching for a single $ in 'foo\n' will find two (empty) matches: one just before the newline, and one at the end of the string.
'*' Causes the resulting RE to match 0 or more repetitions of the preceding RE, as many repetitions as are possible. ab* will match ‘a’, ‘ab’, or ‘a’ followed by any number of ‘b’s.
'+' Causes the resulting RE to match 1 or more repetitions of the preceding RE. ab+ will match ‘a’ followed by any non-zero number of ‘b’s; it will not match just ‘a’.
'?' Causes the resulting RE to match 0 or 1 repetitions of the preceding RE. ab? will match either ‘a’ or ‘ab’.
*?, +?, ??
The '*', '+', and '?' qualifiers are all greedy; they match as much text as possible. Sometimes this behaviour isn’t desired; if the RE <.*> is matched against '<H1>title</H1>', it will match the entire string, and not just '<H1>'. Adding '?' after the qualifier makes it perform the match in non-greedy or minimal fashion; as few characters as possible will be matched. Using .*? in the previous expression will match only '<H1>'.
{m}
Specifies that exactly m copies of the previous RE should be matched; fewer matches cause the entire RE not to match. For example, a{6} will match exactly six 'a' characters, but not five.
{m,n}
Causes the resulting RE to match from m to n repetitions of the preceding RE, attempting to match as many repetitions as possible. For example, a{3,5} will match from 3 to 5 'a' characters. Omitting m specifies a lower bound of zero, and omitting n specifies an infinite upper bound. As an example, a{4,}b will match aaaab or a thousand 'a' characters followed by a b, but not aaab. The comma may not be omitted or the modifier would be confused with the previously described form.
{m,n}?
Causes the resulting RE to match from m to n repetitions of the preceding RE, attempting to match as few repetitions as possible. This is the non-greedy version of the previous qualifier. For example, on the 6-character string 'aaaaaa', a{3,5} will match 5 'a' characters, while a{3,5}? will only match 3 characters.
'\'
Either escapes special characters (permitting you to match characters like '*', '?', and so forth), or signals a special sequence; special sequences are discussed below.
If you’re not using a raw string to express the pattern, remember that Python also uses the backslash as an escape sequence in string literals; if the escape sequence isn’t recognized by Python’s parser, the backslash and subsequent character are included in the resulting string. However, if Python would recognize the resulting sequence, the backslash should be repeated twice. This is complicated and hard to understand, so it’s highly recommended that you use raw strings for all but the simplest expressions.
[]
Used to indicate a set of characters. In a set:
Characters can be listed individually, e.g. [amk] will match 'a', 'm', or 'k'.
Ranges of characters can be indicated by giving two characters and separating them by a '-', for example [a-z] will match any lowercase ASCII letter, [0-5][0-9] will match all the two-digits numbers from 00 to 59, and [0-9A-Fa-f] will match any hexadecimal digit. If - is escaped (e.g. [a\-z]) or if it’s placed as the first or last character (e.g. [a-]), it will match a literal '-'.
Special characters lose their special meaning inside sets. For example, [(+*)] will match any of the literal characters '(', '+', '*', or ')'.
Character classes such as \w or \S (defined below) are also accepted inside a set, although the characters they match depends on whether LOCALE or UNICODE mode is in force.
Characters that are not within a range can be matched by complementing the set. If the first character of the set is '^', all the characters that are not in the set will be matched. For example, [^5] will match any character except '5', and [^^] will match any character except '^'. ^ has no special meaning if it’s not the first character in the set.
To match a literal ']' inside a set, precede it with a backslash, or place it at the beginning of the set. For example, both [()[\]{}] and []()[{}] will both match a parenthesis.
'|'
A|B, where A and B can be arbitrary REs, creates a regular expression that will match either A or B. An arbitrary number of REs can be separated by the '|' in this way. This can be used inside groups (see below) as well. As the target string is scanned, REs separated by '|' are tried from left to right. When one pattern completely matches, that branch is accepted. This means that once A matches, B will not be tested further, even if it would produce a longer overall match. In other words, the '|' operator is never greedy. To match a literal '|', use \|, or enclose it inside a character class, as in [|].
(...)
Matches whatever regular expression is inside the parentheses, and indicates the start and end of a group; the contents of a group can be retrieved after a match has been performed, and can be matched later in the string with the \number special sequence, described below. To match the literals '(' or ')', use \( or \), or enclose them inside a character class: [(] [)].
(?...)
This is an extension notation (a '?' following a '(' is not meaningful otherwise). The first character after the '?' determines what the meaning and further syntax of the construct is. Extensions usually do not create a new group; (?P<name>...) is the only exception to this rule. Following are the currently supported extensions.
(?iLmsux)
(One or more letters from the set 'i', 'L', 'm', 's', 'u', 'x'.) The group matches the empty string; the letters set the corresponding flags: re.I (ignore case), re.L (locale dependent), re.M (multi-line), re.S (dot matches all), re.U (Unicode dependent), and re.X (verbose), for the entire regular expression. (The flags are described in Module Contents.) This is useful if you wish to include the flags as part of the regular expression, instead of passing a flag argument to the re.compile() function.
Note that the (?x) flag changes how the expression is parsed. It should be used first in the expression string, or after one or more whitespace characters. If there are non-whitespace characters before the flag, the results are undefined.
(?:...)
A non-capturing version of regular parentheses. Matches whatever regular expression is inside the parentheses, but the substring matched by the group cannot be retrieved after performing a match or referenced later in the pattern.
(?P<name>...)
Similar to regular parentheses, but the substring matched by the group is accessible via the symbolic group name name. Group names must be valid Python identifiers, and each group name must be defined only once within a regular expression. A symbolic group is also a numbered group, just as if the group were not named.
Named groups can be referenced in three contexts. If the pattern is (?P<quote>['"]).*?(?P=quote) (i.e. matching a string quoted with either single or double quotes):
Context of reference to group “quote” Ways to reference it
in the same pattern itself
(?P=quote) (as shown)
\1
when processing match object m
m.group('quote')
m.end('quote') (etc.)
in a string passed to the repl argument of re.sub()
\g<quote>
\g<1>
\1
(?P=name)
A backreference to a named group; it matches whatever text was matched by the earlier group named name.
(?#...)
A comment; the contents of the parentheses are simply ignored.
(?=...)
Matches if ... matches next, but doesn’t consume any of the string. This is called a lookahead assertion. For example, Isaac (?=Asimov) will match 'Isaac ' only if it’s followed by 'Asimov'.
(?!...)
Matches if ... doesn’t match next. This is a negative lookahead assertion. For example, Isaac (?!Asimov) will match 'Isaac ' only if it’s not followed by 'Asimov'.
(?<=...)
Matches if the current position in the string is preceded by a match for ... that ends at the current position. This is called a positive lookbehind assertion. (?<=abc)def will find a match in abcdef, since the lookbehind will back up 3 characters and check if the contained pattern matches. The contained pattern must only match strings of some fixed length, meaning that abc or a|b are allowed, but a* and a{3,4} are not. Group references are not supported even if they match strings of some fixed length. Note that patterns which start with positive lookbehind assertions will not match at the beginning of the string being searched; you will most likely want to use the search() function rather than the match() function:
Features in regular expression libraries:
1.
1. meaning of re keywords are very unusual than their regular meaning.
2. in general, matching text= match.group()
3. Regular expression: deals with text search in a string
\w = any word character match
+ = unlimited repeat of the preceding pattern-type (such as . \w \d ...) if one instance is found .
* = unlimited repeat of the preceding pattern-type (such as . \w \d ...) even if no instance is found .
https://docs.python.org/2/library/re.html
https://docs.python.org/2/howto/regex.html#regex-howto
https://developers.google.com/edu/python/regular-expressions?hl=en
Regular expressions use the backslash character ('\') to indicate special forms or to allow special characters to be used without invoking their special meaning.
The basic rules of regular expression search for a pattern within a string are:
- The search proceeds through the string from start to end, stopping at the first match found
- All of the pattern must be matched, but not all of the string
- If
match = re.search(pat, str)
is successful, match is not None and in particular match.group() is the matching text
#Square bracket: => group of characters to match: [ ]: so [abc] matches 'a' or 'b' or 'c
#Group extraction: add parenthesis ( )
=====
Python 2.7 Regular Expressions
Non-special chars match themselves. Exceptions are special characters:
\ Escape special char or start a sequence. . Match any char except newline, see re.DOTALL ^ Match start of the string, see re.MULTILINE $ Match end of the string, see re.MULTILINE [] Enclose a set of matchable chars R|S Match either regex R or regex S. () Create capture group, & indicate precedence
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
Quantifiers (append '
?
' for non-greedy):{m} Exactly m repetitions {m,n} From m (default 0) to n (default infinity) * 0 or more. Same as {,} + 1 or more. Same as {1,} ? 0 or 1. Same as {,1}
Special sequences:
\A Start of string \b Match empty string at word (\w+) boundary \B Match empty string not at word boundary \d Digit \D Non-digit \s Whitespace [ \t\n\r\f\v], see LOCALE,UNICODE \S Non-whitespace \w Alphanumeric: [0-9a-zA-Z_], see LOCALE \W Non-alphanumeric \Z End of string \g<id> Match prev named or numbered group, '<' & '>' are literal, e.g. \g<0> or \g<name> (not \g0 or \gname)
Special character escapes are much like those already escaped in Python string literals. Hence regex '
\n
' is same as regex '\\n
':\a ASCII Bell (BEL) \f ASCII Formfeed \n ASCII Linefeed \r ASCII Carriage return \t ASCII Tab \v ASCII Vertical tab \\ A single backslash \xHH Two digit hexadecimal character goes here \OOO Three digit octal char (or just use an initial zero, e.g. \0, \09) \DD Decimal number 1 to 99, match previous numbered group
Extensions. Do not cause grouping, except '
P<name>
':(?iLmsux) Match empty string, sets re.X flags (?:...) Non-capturing version of regular parens (?P<name>...) Create a named capturing group. (?P=name) Match whatever matched prev named group (?#...) A comment; ignored. (?=...) Lookahead assertion, match without consuming (?!...) Negative lookahead assertion (?<=...) Lookbehind assertion, match if preceded (?<!...) Negative lookbehind assertion (?(id)y|n) Match 'y' if group 'id' matched, else 'n'
Flags for re.compile(), etc. Combine with
'|'
:re.I == re.IGNORECASE Ignore case re.L == re.LOCALE Make \w, \b, and \s locale dependent re.M == re.MULTILINE Multiline re.S == re.DOTALL Dot matches all (including newline) re.U == re.UNICODE Make \w, \b, \d, and \s unicode dependent re.X == re.VERBOSE Verbose (unescaped whitespace in pattern is ignored, and '#' marks comment lines)
Module level functions:
compile(pattern[, flags]) -> RegexObject match(pattern, string[, flags]) -> MatchObject search(pattner, string[, flags]) -> MatchObject findall(pattern, string[, flags]) -> list of strings finditer(pattern, string[, flags]) -> iter of MatchObjects split(pattern, string[, maxsplit, flags]) -> list of strings sub(pattern, repl, string[, count, flags]) -> string subn(pattern, repl, string[, count, flags]) -> (string, int) escape(string) -> string purge() # the re cache
RegexObjects (returned from
compile()
):.match(string[, pos, endpos]) -> MatchObject .search(string[, pos, endpos]) -> MatchObject .findall(string[, pos, endpos]) -> list of strings .finditer(string[, pos, endpos]) -> iter of MatchObjects .split(string[, maxsplit]) -> list of strings .sub(repl, string[, count]) -> string .subn(repl, string[, count]) -> (string, int) .flags # int, Passed to compile() .groups # int, Number of capturing groups .groupindex # {}, Maps group names to ints .pattern # string, Passed to compile()
MatchObjects (returned from
match()
and search()
):.expand(template) -> string, Backslash & group expansion .group([group1...]) -> string or tuple of strings, 1 per arg .groups([default]) -> tuple of all groups, non-matching=default .groupdict([default]) -> {}, Named groups, non-matching=default .start([group]) -> int, Start/end of substring match by group .end([group]) -> int, Group defaults to 0, the whole match .span([group]) -> tuple (match.start(group), match.end(group)) .pos int, Passed to search() or match() .endpos int, " .lastindex int, Index of last matched capturing group .lastgroup string, Name of last matched capturing group .re regex, As passed to search() or match() .string string, "
=====
Regular expression in programming: Elaborate feature on: Find a substring in a given string
1. It is related with finding something in a string.
2. Modern regular expression has heavy extended capabilities beyond find a substring.
3. There are specific mechanism to use regular expression capabilities of different langulages and operating systems.
Regular expression (definition): an expression to define criteria to find match
The special characters are for regular expression (RE):
'.' (Dot.) In the default mode, this matches any character except a newline. If the DOTALL flag has been specified, this matches any character including a newline.
'^' (Caret.) Matches the start of the string, and in MULTILINE mode also matches immediately after each newline.
'$' Matches the end of the string or just before the newline at the end of the string, and in MULTILINE mode also matches before a newline. foo matches both ‘foo’ and ‘foobar’, while the regular expression foo$ matches only ‘foo’. More interestingly, searching for foo.$ in 'foo1\nfoo2\n' matches ‘foo2’ normally, but ‘foo1’ in MULTILINE mode; searching for a single $ in 'foo\n' will find two (empty) matches: one just before the newline, and one at the end of the string.
'*' Causes the resulting RE to match 0 or more repetitions of the preceding RE, as many repetitions as are possible. ab* will match ‘a’, ‘ab’, or ‘a’ followed by any number of ‘b’s.
'+' Causes the resulting RE to match 1 or more repetitions of the preceding RE. ab+ will match ‘a’ followed by any non-zero number of ‘b’s; it will not match just ‘a’.
'?' Causes the resulting RE to match 0 or 1 repetitions of the preceding RE. ab? will match either ‘a’ or ‘ab’.
*?, +?, ??
The '*', '+', and '?' qualifiers are all greedy; they match as much text as possible. Sometimes this behaviour isn’t desired; if the RE <.*> is matched against '<H1>title</H1>', it will match the entire string, and not just '<H1>'. Adding '?' after the qualifier makes it perform the match in non-greedy or minimal fashion; as few characters as possible will be matched. Using .*? in the previous expression will match only '<H1>'.
{m}
Specifies that exactly m copies of the previous RE should be matched; fewer matches cause the entire RE not to match. For example, a{6} will match exactly six 'a' characters, but not five.
{m,n}
Causes the resulting RE to match from m to n repetitions of the preceding RE, attempting to match as many repetitions as possible. For example, a{3,5} will match from 3 to 5 'a' characters. Omitting m specifies a lower bound of zero, and omitting n specifies an infinite upper bound. As an example, a{4,}b will match aaaab or a thousand 'a' characters followed by a b, but not aaab. The comma may not be omitted or the modifier would be confused with the previously described form.
{m,n}?
Causes the resulting RE to match from m to n repetitions of the preceding RE, attempting to match as few repetitions as possible. This is the non-greedy version of the previous qualifier. For example, on the 6-character string 'aaaaaa', a{3,5} will match 5 'a' characters, while a{3,5}? will only match 3 characters.
'\'
Either escapes special characters (permitting you to match characters like '*', '?', and so forth), or signals a special sequence; special sequences are discussed below.
If you’re not using a raw string to express the pattern, remember that Python also uses the backslash as an escape sequence in string literals; if the escape sequence isn’t recognized by Python’s parser, the backslash and subsequent character are included in the resulting string. However, if Python would recognize the resulting sequence, the backslash should be repeated twice. This is complicated and hard to understand, so it’s highly recommended that you use raw strings for all but the simplest expressions.
[]
Used to indicate a set of characters. In a set:
Characters can be listed individually, e.g. [amk] will match 'a', 'm', or 'k'.
Ranges of characters can be indicated by giving two characters and separating them by a '-', for example [a-z] will match any lowercase ASCII letter, [0-5][0-9] will match all the two-digits numbers from 00 to 59, and [0-9A-Fa-f] will match any hexadecimal digit. If - is escaped (e.g. [a\-z]) or if it’s placed as the first or last character (e.g. [a-]), it will match a literal '-'.
Special characters lose their special meaning inside sets. For example, [(+*)] will match any of the literal characters '(', '+', '*', or ')'.
Character classes such as \w or \S (defined below) are also accepted inside a set, although the characters they match depends on whether LOCALE or UNICODE mode is in force.
Characters that are not within a range can be matched by complementing the set. If the first character of the set is '^', all the characters that are not in the set will be matched. For example, [^5] will match any character except '5', and [^^] will match any character except '^'. ^ has no special meaning if it’s not the first character in the set.
To match a literal ']' inside a set, precede it with a backslash, or place it at the beginning of the set. For example, both [()[\]{}] and []()[{}] will both match a parenthesis.
'|'
A|B, where A and B can be arbitrary REs, creates a regular expression that will match either A or B. An arbitrary number of REs can be separated by the '|' in this way. This can be used inside groups (see below) as well. As the target string is scanned, REs separated by '|' are tried from left to right. When one pattern completely matches, that branch is accepted. This means that once A matches, B will not be tested further, even if it would produce a longer overall match. In other words, the '|' operator is never greedy. To match a literal '|', use \|, or enclose it inside a character class, as in [|].
(...)
Matches whatever regular expression is inside the parentheses, and indicates the start and end of a group; the contents of a group can be retrieved after a match has been performed, and can be matched later in the string with the \number special sequence, described below. To match the literals '(' or ')', use \( or \), or enclose them inside a character class: [(] [)].
(?...)
This is an extension notation (a '?' following a '(' is not meaningful otherwise). The first character after the '?' determines what the meaning and further syntax of the construct is. Extensions usually do not create a new group; (?P<name>...) is the only exception to this rule. Following are the currently supported extensions.
(?iLmsux)
(One or more letters from the set 'i', 'L', 'm', 's', 'u', 'x'.) The group matches the empty string; the letters set the corresponding flags: re.I (ignore case), re.L (locale dependent), re.M (multi-line), re.S (dot matches all), re.U (Unicode dependent), and re.X (verbose), for the entire regular expression. (The flags are described in Module Contents.) This is useful if you wish to include the flags as part of the regular expression, instead of passing a flag argument to the re.compile() function.
Note that the (?x) flag changes how the expression is parsed. It should be used first in the expression string, or after one or more whitespace characters. If there are non-whitespace characters before the flag, the results are undefined.
(?:...)
A non-capturing version of regular parentheses. Matches whatever regular expression is inside the parentheses, but the substring matched by the group cannot be retrieved after performing a match or referenced later in the pattern.
(?P<name>...)
Similar to regular parentheses, but the substring matched by the group is accessible via the symbolic group name name. Group names must be valid Python identifiers, and each group name must be defined only once within a regular expression. A symbolic group is also a numbered group, just as if the group were not named.
Named groups can be referenced in three contexts. If the pattern is (?P<quote>['"]).*?(?P=quote) (i.e. matching a string quoted with either single or double quotes):
Context of reference to group “quote” Ways to reference it
in the same pattern itself
(?P=quote) (as shown)
\1
when processing match object m
m.group('quote')
m.end('quote') (etc.)
in a string passed to the repl argument of re.sub()
\g<quote>
\g<1>
\1
(?P=name)
A backreference to a named group; it matches whatever text was matched by the earlier group named name.
(?#...)
A comment; the contents of the parentheses are simply ignored.
(?=...)
Matches if ... matches next, but doesn’t consume any of the string. This is called a lookahead assertion. For example, Isaac (?=Asimov) will match 'Isaac ' only if it’s followed by 'Asimov'.
(?!...)
Matches if ... doesn’t match next. This is a negative lookahead assertion. For example, Isaac (?!Asimov) will match 'Isaac ' only if it’s not followed by 'Asimov'.
(?<=...)
Matches if the current position in the string is preceded by a match for ... that ends at the current position. This is called a positive lookbehind assertion. (?<=abc)def will find a match in abcdef, since the lookbehind will back up 3 characters and check if the contained pattern matches. The contained pattern must only match strings of some fixed length, meaning that abc or a|b are allowed, but a* and a{3,4} are not. Group references are not supported even if they match strings of some fixed length. Note that patterns which start with positive lookbehind assertions will not match at the beginning of the string being searched; you will most likely want to use the search() function rather than the match() function:
Features in regular expression libraries:
1.
Thursday, October 29, 2015
self in python class
self:
1. self is just a very powerful convention and it's not really a reserved keyword in Python. Java and C# have this as a keyword
2. "self" is same as "this" in java and C#
3. "self" is reference to a generic, anonymous, or un-named instance of the class inside which it is used.
4. python has more use of an instance reference for the class in the class structure than Java and C#.
5. "self" description is very confusing on the internet articles: "self" is simple thing - this is the way python class is structured - self is just a reference to an instance of the same class where it is getting used.
1. self is just a very powerful convention and it's not really a reserved keyword in Python. Java and C# have this as a keyword
2. "self" is same as "this" in java and C#
3. "self" is reference to a generic, anonymous, or un-named instance of the class inside which it is used.
4. python has more use of an instance reference for the class in the class structure than Java and C#.
5. "self" description is very confusing on the internet articles: "self" is simple thing - this is the way python class is structured - self is just a reference to an instance of the same class where it is getting used.
inheritance in python
http://www.dotnetperls.com/class-python
class A: def width(self): print("a, width called") class B(A): def size(self): print("b, size called") # Create new class instance. b = B() # Call method on B. b.size() # Call method from base class. b.width()
class declaration with arguments
Argument to a class declaration is "super class" or a parent class. a class can be derived without any parent such as: class MyClass: . From a parent a class is derived as: class Myclass(parentClass). A class can be and is usually derived from default class "object".
A class in instantiated with argument only when its constructor accepts arguments.
A class in instantiated with argument only when its constructor accepts arguments.
default class inheritance in python
http://stackoverflow.com/questions/4906014/python-default-inheritance
class Config: vs class Config(object)
------------------------------------
If you define a class and don't declare any specific parent, the class becomes a "classic class", which behaves a bit differently than "new-style classes" inherited from object. See here for more details: http://docs.python.org/release/2.5.2/ref/node33.html
Classic classes don't have a common root, so essentially, your AClass doesn't inherit from any class.
Note that this is specific to Python versions before 3.0. In Python 3, all classes are new-style classes and inherit implicitly from object, if no other parent is declared.
class Config: vs class Config(object)
------------------------------------
If you define a class and don't declare any specific parent, the class becomes a "classic class", which behaves a bit differently than "new-style classes" inherited from object. See here for more details: http://docs.python.org/release/2.5.2/ref/node33.html
Classic classes don't have a common root, so essentially, your AClass doesn't inherit from any class.
Note that this is specific to Python versions before 3.0. In Python 3, all classes are new-style classes and inherit implicitly from object, if no other parent is declared.
while loop exit
while(True):
r=raw_input()
if r=='n':
print "Exiting"
break
else :
print "not time to break"
r=raw_input()
if r=='n':
print "Exiting"
break
else :
print "not time to break"
hanged subprocess and popen call
http://stackoverflow.com/questions/16768290/working-of-popen-communicate
https://docs.python.org/2/library/subprocess.html\
Use:
child loop after the print should fix your problem.
https://docs.python.org/2/library/subprocess.html\
Do not use stdout=PIPE or stderr=PIPE with this function
as that can deadlock based on the child process output volume.
Use Popen with the communicate() method when you
need pipes.
Use:
proc.stdin.flush()
in the main while loop and a sys.stdout.flush()
in thechild loop after the print should fix your problem.
Tuesday, October 27, 2015
lambda, filter, reduce and map
http://www.python-course.eu/lambda.php
http://www.secnetix.de/olli/Python/lambda_functions.hawk
Note: using lambda, a python function can be defined without def and there is no need to use return in the function. Also a anonymous function - a function without name.
=> No need to use def and return for creating a function and returning a value.
=> function define without def & return value without return
=> define anonymous function: a function without a name
=> one liner function definition
=> returning a function from other function
>>> def a(x): return lambda y: x+y
...
>>> b=a(2)
>>> b(3)
5
Python supports the creation of anonymous functions (i.e. functions that are not bound to a name) at runtime, using a construct called "lambda".
>>> def f (x): return x**2
...
>>> print f(8)
64
>>>
>>> g = lambda x: x**2
>>>
>>> print g(8)
64
http://www.secnetix.de/olli/Python/lambda_functions.hawk
Note: using lambda, a python function can be defined without def and there is no need to use return in the function. Also a anonymous function - a function without name.
=> No need to use def and return for creating a function and returning a value.
=> function define without def & return value without return
=> define anonymous function: a function without a name
=> one liner function definition
=> returning a function from other function
>>> def a(x): return lambda y: x+y
...
>>> b=a(2)
>>> b(3)
5
Python supports the creation of anonymous functions (i.e. functions that are not bound to a name) at runtime, using a construct called "lambda".
>>> def f (x): return x**2
...
>>> print f(8)
64
>>>
>>> g = lambda x: x**2
>>>
>>> print g(8)
64
crucible rest client in python
https://developer.atlassian.com/fecrudev/integration-tutorials/writing-a-rest-client-in-python
# import the standard JSON parser
import json
# import the REST library
from restful_lib import Connection
base_url = "http://localhost:6060/foo/rest-service/reviews-v1"
conn = Connection(base_url, username="admin", password="admin")
# the rest library can't distinguish between a property and a list of properties with one element.
# this function converts a json object into a list with many, one, or no elements
# o is the dictionary containing the list
# key is the key containing the list (if any)
def toList(o, key):
if isinstance(o,dict):
elements = o[key]
if not isinstance(elements,list):
return [elements]
else:
return elements
else:
return []
# a function to get the uncompleted reviwers for a single review
def uncompletedReviewers(review):
id = review[u'permaId'][u'id']
resp = conn.request_get("/" + id + "/reviewers/uncompleted", args={}, headers={'content-type':'application/json', 'accept':'application/json'})
status = resp[u'headers']['status']
if status == '200' or status == '304':
reviewers = toList(json.loads(resp[u'body'])[u'reviewers'],u'reviewer')
return map(lambda r: r[u'displayName'], reviewers)
else:
return []
# get a dictionary containing the response to the GET request
# we specify JSON as the format as that is easy to parse in Python
resp = conn.request_get("/filter/allOpenReviews", args={}, headers={'content-type':'application/json', 'accept':'application/json'})
status = resp[u'headers']['status']
# check that we either got a successful response (200) or a previously retrieved, but still valid response (304)
if status == '200' or status == '304':
reviews = toList(json.loads(resp[u'body'])[u'reviews'],u'reviewData')
reviewerLists = map(uncompletedReviewers,reviews)
reviewers = reduce(lambda a, b: set(a).union(set(b)), reviewerLists, set())
print 'Incomplete Reviewers: '
for r in reviewers:
print ' ',r
else:
print 'Error status code: ', status
# import the standard JSON parser
import json
# import the REST library
from restful_lib import Connection
base_url = "http://localhost:6060/foo/rest-service/reviews-v1"
conn = Connection(base_url, username="admin", password="admin")
# the rest library can't distinguish between a property and a list of properties with one element.
# this function converts a json object into a list with many, one, or no elements
# o is the dictionary containing the list
# key is the key containing the list (if any)
def toList(o, key):
if isinstance(o,dict):
elements = o[key]
if not isinstance(elements,list):
return [elements]
else:
return elements
else:
return []
# a function to get the uncompleted reviwers for a single review
def uncompletedReviewers(review):
id = review[u'permaId'][u'id']
resp = conn.request_get("/" + id + "/reviewers/uncompleted", args={}, headers={'content-type':'application/json', 'accept':'application/json'})
status = resp[u'headers']['status']
if status == '200' or status == '304':
reviewers = toList(json.loads(resp[u'body'])[u'reviewers'],u'reviewer')
return map(lambda r: r[u'displayName'], reviewers)
else:
return []
# get a dictionary containing the response to the GET request
# we specify JSON as the format as that is easy to parse in Python
resp = conn.request_get("/filter/allOpenReviews", args={}, headers={'content-type':'application/json', 'accept':'application/json'})
status = resp[u'headers']['status']
# check that we either got a successful response (200) or a previously retrieved, but still valid response (304)
if status == '200' or status == '304':
reviews = toList(json.loads(resp[u'body'])[u'reviews'],u'reviewData')
reviewerLists = map(uncompletedReviewers,reviews)
reviewers = reduce(lambda a, b: set(a).union(set(b)), reviewerLists, set())
print 'Incomplete Reviewers: '
for r in reviewers:
print ' ',r
else:
print 'Error status code: ', status
for in python
for x in range(0, 3):
print "We're on time %d" % (x)
print "We're on time %d" % (x)
python dictionary
http://learnpythonthehardway.org/book/ex39.html
http://www.tutorialspoint.com/python/python_dictionary.htm
http://stackoverflow.com/questions/10380992/get-python-dictionary-from-string-containing-key-value-pairs
You can use numbers to "index" into a list, meaning you can use numbers to find out what's in lists. You should know this about lists by now, but make sure you understand that you can only use numbers to get items out of a list.
What a dict does is let you use anything, not just numbers. Yes, a dict associates one thing to another, no matter what it is. Take a look:
>>> stuff = {'name': 'Zed', 'age': 39, 'height': 6 * 12 + 2}
>>> print stuff['name']
Zed
>>> print stuff['age']
39
>>> print stuff['height']
74
>>> stuff['city'] = "San Francisco"
>>> print stuff['city']
list in python
http://effbot.org/zone/python-list.htm
https://developers.google.com/edu/python/lists?hl=en
https://docs.python.org/2/tutorial/datastructures.html
http://www.tutorialspoint.com/python/python_lists.htm
List range (Important)
https://deron.meranda.us/python/tutorial-bazaar/chapter2
s[ first:last ]. The last index is not included, but included if last idex is omitted such as s[first:]
Sublist:
L = [ "A", "B", [ "X", "Y" ], "C" ]
print L[-1] --> "C"
print L[2] --> ["X", "Y"]
print L[2][-1] --> "Y"
print len(L) --> 4
Sequence types: strings, lists, and tuples
If you pass in a negative index, Python adds the length of the list to the index. L[-1] can be used to access the last item in a list.
a=[1,2,3]
a[-1] = a[3-1]
https://developers.google.com/edu/python/lists?hl=en
https://docs.python.org/2/tutorial/datastructures.html
http://www.tutorialspoint.com/python/python_lists.htm
List range (Important)
https://deron.meranda.us/python/tutorial-bazaar/chapter2
s[ first:last ]. The last index is not included, but included if last idex is omitted such as s[first:]
Sublist:
L = [ "A", "B", [ "X", "Y" ], "C" ]
print L[-1] --> "C"
print L[2] --> ["X", "Y"]
print L[2][-1] --> "Y"
print len(L) --> 4
Sequence types: strings, lists, and tuples
A = B = [] # both names will point to the same list A = [] B = A # both names will point to the same list A = []; B = [] # independent lists
If you pass in a negative index, Python adds the length of the list to the index. L[-1] can be used to access the last item in a list.
a=[1,2,3]
a[-1] = a[3-1]
If you need both the index and the item, use the enumerate function:
for index, item in enumerate(L): print index, item
The list object supports the iterator protocol. To explicitly create an iterator, use the built-in iter function:i = iter(L) item = i.next() # fetch first value item = i.next() # fetch second value
List Comprehension:
[x*x for x in [1,2,3]]
Subscribe to:
Posts (Atom)