- In the Administrator elevated command prompt, paste the following command to disable Microsoft Hyper V and press Enter:
dism.exe /Online /Disable-Feature:Microsoft-Hyper-V
- Once the command runs successfully, close the elevated Command Prompt and restart your computer.
- At the next startup, open a Virtualbox machine again and see if you’re still getting the same error message.
Welcome to my Technology Hotspot
Wednesday, May 13, 2020
How to resolve VT-X is not available error in Oracle VM VirtualBox when tried to start a VM
Tuesday, May 5, 2020
Go lang debugger delve installation error on Mac OS: xcrun: error: invalid active developer path, missing xcrun
Today I installed Go lang on my macbook Pro and installed VS Code and the Go lang extension for it.
When I tried to Run the first hello.go program from the VSCode Run menu got a prompt that Delve which is the debugger extension for Go lang to be installed.
When I tried to install Delve got this strange error:
Mac OS: xcrun: error: invalid active developer path, missing xcrun
On looking up the internet found that running the below command on the terminal will solve the issue:
Install the Xcode toolkit! Even if you had it installed before, you might have to re-register it or update it to the latest version.
go get -V github.com/go-delve/delve/cmd/dlv command and all was well. Now I am able to neatly run the Go lang programs from the Run menu of VS Code editor.
Alternatively using the below commands was always working without installing Delve as well which is the default option for you to build & run the Go programs.
go build hello.go
./hello
Enjoy coding Go!!
When I tried to Run the first hello.go program from the VSCode Run menu got a prompt that Delve which is the debugger extension for Go lang to be installed.
When I tried to install Delve got this strange error:
Mac OS: xcrun: error: invalid active developer path, missing xcrun
On looking up the internet found that running the below command on the terminal will solve the issue:
Install the Xcode toolkit! Even if you had it installed before, you might have to re-register it or update it to the latest version.
$ xcode-select --install
If that doesn’t work, force it to reset. You’ll need
sudo access for this one.$ sudo xcode-select --reset
And voila, Delve got installed successfully using the go get -V github.com/go-delve/delve/cmd/dlv command and all was well. Now I am able to neatly run the Go lang programs from the Run menu of VS Code editor.
Alternatively using the below commands was always working without installing Delve as well which is the default option for you to build & run the Go programs.
go build hello.go
./hello
Enjoy coding Go!!
Tuesday, July 31, 2018
SVN to Git migration retaining the history of checkins
This blog provides you with a useful information if you wish to migrate your source code from SVN to Git and still want to retain the checkin history information
https://smexyyweby.wordpress.com/2013/09/13/clone-svn-repository-to-git-using-tortoisegit-on-windows/
https://smexyyweby.wordpress.com/2013/09/13/clone-svn-repository-to-git-using-tortoisegit-on-windows/
Sunday, November 5, 2017
How to record your computer screen with Audio using Apps you might already have
If you would rather not download any additional software, there’s a good chance that some of the apps you have laying around can indeed record your screen, even if that’s not their primary purpose. Here are several apps that you probably have access to right now, and how they can record for you.
PowerPoint
Didn’t know you can record your screen with PowerPoint, the presentation software included with Microsoft Office 365? It’s true! The latest versions of PowerPoint include the capability. Start by heading over to the Insert tab, and select Screen Recording, with an icon of a recorder and a screen. You can then select the specific area of your screen y
ou want to record, and start the recording process whenever you want. When you’re done you can save the video as a separate file to access or embed as you see fit. Editing and control options are very limited after that, but it’s a great option for quick-and-dirty recording — especially if you’re doing it for a looming presentation.
ou want to record, and start the recording process whenever you want. When you’re done you can save the video as a separate file to access or embed as you see fit. Editing and control options are very limited after that, but it’s a great option for quick-and-dirty recording — especially if you’re doing it for a looming presentation.YouTube Live Streaming
If you don’t want to spend a ton of time recording but still want a video for your YouTube channel, or any other social media platform, then YouTube can help out. Sign into your account as you would normally, go to Upload, click Get Started under Live Streaming, and choose Events. Afterward, select New live event, fill out the required information, and click Go Live Now. A Google Hangouts page will open — keep in mind that you are now recording audio and video — and on the left you should see a button that says Screenshare. Select it, and choose a desktop window for recording. Then, click Start Screenshare, followed by Start Broadcast. You should now be recording! Select Stop Broadcast when finished, and save your Event as you wish.
QuickTime Player
If you’re on a Mac, you may prefer using QuickTime. Launch QuickTime, select File, and choose New Screen Recording. This will open up a small recording window that you can start, which will automatically encourage you to select either a part of your screen or the full screen for recording. Click Start Recording when you are ready. However, note that QuickTime recordings aren’t easy to edit in post, so be careful.

Thursday, October 19, 2017
How to enable setting breakpoints in Python during command line execution
To set a breakpoint in your code use the native Python
import pdb;pdb.set_trace() call in your codeTuesday, September 23, 2014
JSONLint - The JSON Validator
http://jsonlint.com/
You can use this online JSONlint tool to validate & correct your JSON strings.
Found this to be a very useful resource in my day to day work.
You can use this online JSONlint tool to validate & correct your JSON strings.
Found this to be a very useful resource in my day to day work.
PYTHON REGULAR EXPRESSIONS
Proved to be a very useful resource for me !
http://www.tutorialspoint.com/python/python_reg_expressions.htm
PYTHON REGULAR EXPRESSIONS
http://www.tutorialspoint.com/python/python_reg_expressions.htm
Copyright © tutorialspoint.com
A regular expression is a special sequence of characters that helps you match or find other strings or sets of strings, using a specialized syntax held in a pattern. Regular expressions are widely used in UNIX world.
The module re provides full support for Perl-like regular expressions in Python. The re module raises the exception re.error if an error occurs while compiling or using a regular expression.
We would cover two important functions, which would be used to handle regular expressions. But a small thing first: There are various characters, which would have special meaning when they are used in regular expression. To avoid any confusion while dealing with regular expressions, we would use Raw Strings as r'expression'.
Here is the syntax for this function:
The re.match function returns a match object on success, None on failure. We would usegroup(num) or groups() function of match object to get matched expression.
Here is the syntax for this function:
The re.search function returns a match object on success, None on failure. We would usegroup(num) or groups() function of match object to get matched expression.
Following table lists the regular expression syntax that is available in Python:
The module re provides full support for Perl-like regular expressions in Python. The re module raises the exception re.error if an error occurs while compiling or using a regular expression.
We would cover two important functions, which would be used to handle regular expressions. But a small thing first: There are various characters, which would have special meaning when they are used in regular expression. To avoid any confusion while dealing with regular expressions, we would use Raw Strings as r'expression'.
The match Function
This function attempts to match RE pattern to string with optional flags.Here is the syntax for this function:
re.match(pattern, string, flags=0)Here is the description of the parameters:
| Parameter | Description |
|---|---|
| pattern | This is the regular expression to be matched. |
| string | This is the string, which would be searched to match the pattern at the beginning of string. |
| flags | You can specify different flags using bitwise OR (|). These are modifiers, which are listed in the table below. |
| Match Object Methods | Description |
|---|---|
| group(num=0) | This method returns entire match (or specific subgroup num) |
| groups() | This method returns all matching subgroups in a tuple (empty if there weren't any) |
Example:
#!/usr/bin/python import re line = "Cats are smarter than dogs" matchObj = re.match( r'(.*) are (.*?) .*', line, re.M|re.I) if matchObj: print "matchObj.group() : ", matchObj.group() print "matchObj.group(1) : ", matchObj.group(1) print "matchObj.group(2) : ", matchObj.group(2) else: print "No match!!"When the above code is executed, it produces following result:
matchObj.group() : Cats are smarter than dogs matchObj.group(1) : Cats matchObj.group(2) : smarter
The search Function
This function searches for first occurrence of RE pattern within string with optional flags.Here is the syntax for this function:
re.search(pattern, string, flags=0)Here is the description of the parameters:
| Parameter | Description |
|---|---|
| pattern | This is the regular expression to be matched. |
| string | This is the string, which would be searched to match the pattern anywhere in the string. |
| flags | You can specify different flags using bitwise OR (|). These are modifiers, which are listed in the table below. |
| Match Object Methods | Description |
|---|---|
| group(num=0) | This method returns entire match (or specific subgroup num) |
| groups() | This method returns all matching subgroups in a tuple (empty if there weren't any) |
Example:
#!/usr/bin/python import re line = "Cats are smarter than dogs"; searchObj = re.search( r'(.*) are (.*?) .*', line, re.M|re.I) if searchObj: print "searchObj.group() : ", searchObj.group() print "searchObj.group(1) : ", searchObj.group(1) print "searchObj.group(2) : ", searchObj.group(2) else: print "Nothing found!!"When the above code is executed, it produces following result:
matchObj.group() : Cats are smarter than dogs matchObj.group(1) : Cats matchObj.group(2) : smarter
Matching vs Searching:
Python offers two different primitive operations based on regular expressions: match checks for a match only at the beginning of the string, while search checks for a match anywhere in the string (this is what Perl does by default).Example:
#!/usr/bin/python import re line = "Cats are smarter than dogs"; matchObj = re.match( r'dogs', line, re.M|re.I) if matchObj: print "match --> matchObj.group() : ", matchObj.group() else: print "No match!!" searchObj = re.search( r'dogs', line, re.M|re.I) if searchObj: print "search --> searchObj.group() : ", searchObj.group() else: print "Nothing found!!"When the above code is executed, it produces the following result:
No match!! search --> matchObj.group() : dogs
Search and Replace:
Some of the most important re methods that use regular expressions is sub.Syntax:
re.sub(pattern, repl, string, max=0)This method replaces all occurrences of the RE pattern in string with repl, substituting all occurrences unless max provided. This method would return modified string.
Example:
Following is the example:#!/usr/bin/python import re phone = "2004-959-559 # This is Phone Number" # Delete Python-style comments num = re.sub(r'#.*$', "", phone) print "Phone Num : ", num # Remove anything other than digits num = re.sub(r'\D', "", phone) print "Phone Num : ", numWhen the above code is executed, it produces the following result:
Phone Num : 2004-959-559 Phone Num : 2004959559
Regular-expression Modifiers - Option Flags
Regular expression literals may include an optional modifier to control various aspects of matching. The modifiers are specified as an optional flag. You can provide multiple modifiers using exclusive OR (|), as shown previously and may be represented by one of these:| Modifier | Description |
|---|---|
| re.I | Performs case-insensitive matching. |
| re.L | Interprets words according to the current locale. This interpretation affects the alphabetic group (\w and \W), as well as word boundary behavior (\b and \B). |
| re.M | Makes $ match the end of a line (not just the end of the string) and makes ^ match the start of any line (not just the start of the string). |
| re.S | Makes a period (dot) match any character, including a newline. |
| re.U | Interprets letters according to the Unicode character set. This flag affects the behavior of \w, \W, \b, \B. |
| re.X | Permits "cuter" regular expression syntax. It ignores whitespace (except inside a set [] or when escaped by a backslash) and treats unescaped # as a comment marker. |
Regular-expression patterns:
Except for control characters, (+ ? . * ^ $ ( ) [ ] { } | \), all characters match themselves. You can escape a control character by preceding it with a backslash.Following table lists the regular expression syntax that is available in Python:
| Pattern | Description |
|---|---|
| ^ | Matches beginning of line. |
| $ | Matches end of line. |
| . | Matches any single character except newline. Using m option allows it to match newline as well. |
| [...] | Matches any single character in brackets. |
| [^...] | Matches any single character not in brackets |
| re* | Matches 0 or more occurrences of preceding expression. |
| re+ | Matches 1 or more occurrence of preceding expression. |
| re? | Matches 0 or 1 occurrence of preceding expression. |
| re{ n} | Matches exactly n number of occurrences of preceding expression. |
| re{ n,} | Matches n or more occurrences of preceding expression. |
| re{ n, m} | Matches at least n and at most m occurrences of preceding expression. |
| a| b | Matches either a or b. |
| (re) | Groups regular expressions and remembers matched text. |
| (?imx) | Temporarily toggles on i, m, or x options within a regular expression. If in parentheses, only that area is affected. |
| (?-imx) | Temporarily toggles off i, m, or x options within a regular expression. If in parentheses, only that area is affected. |
| (?: re) | Groups regular expressions without remembering matched text. |
| (?imx: re) | Temporarily toggles on i, m, or x options within parentheses. |
| (?-imx: re) | Temporarily toggles off i, m, or x options within parentheses. |
| (?#...) | Comment. |
| (?= re) | Specifies position using a pattern. Doesn't have a range. |
| (?! re) | Specifies position using pattern negation. Doesn't have a range. |
| (?> re) | Matches independent pattern without backtracking. |
| \w | Matches word characters. |
| \W | Matches nonword characters. |
| \s | Matches whitespace. Equivalent to [\t\n\r\f]. |
| \S | Matches nonwhitespace. |
| \d | Matches digits. Equivalent to [0-9]. |
| \D | Matches nondigits. |
| \A | Matches beginning of string. |
| \Z | Matches end of string. If a newline exists, it matches just before newline. |
| \z | Matches end of string. |
| \G | Matches point where last match finished. |
| \b | Matches word boundaries when outside brackets. Matches backspace (0x08) when inside brackets. |
| \B | Matches nonword boundaries. |
| \n, \t, etc. | Matches newlines, carriage returns, tabs, etc. |
| \1...\9 | Matches nth grouped subexpression. |
| \10 | Matches nth grouped subexpression if it matched already. Otherwise refers to the octal representation of a character code. |
REGULAR-EXPRESSION EXAMPLES
Literal characters:
| Example | Description |
|---|---|
| python | Match "python". |
Character classes:
| Example | Description |
|---|---|
| [Pp]ython | Match "Python" or "python" |
| rub[ye] | Match "ruby" or "rube" |
| [aeiou] | Match any one lowercase vowel |
| [0-9] | Match any digit; same as [0123456789] |
| [a-z] | Match any lowercase ASCII letter |
| [A-Z] | Match any uppercase ASCII letter |
| [a-zA-Z0-9] | Match any of the above |
| [^aeiou] | Match anything other than a lowercase vowel |
| [^0-9] | Match anything other than a digit |
Special Character Classes:
| Example | Description |
|---|---|
| . | Match any character except newline |
| \d | Match a digit: [0-9] |
| \D | Match a nondigit: [^0-9] |
| \s | Match a whitespace character: [ \t\r\n\f] |
| \S | Match nonwhitespace: [^ \t\r\n\f] |
| \w | Match a single word character: [A-Za-z0-9_] |
| \W | Match a nonword character: [^A-Za-z0-9_] |
Repetition Cases:
| Example | Description |
|---|---|
| ruby? | Match "rub" or "ruby": the y is optional |
| ruby* | Match "rub" plus 0 or more ys |
| ruby+ | Match "rub" plus 1 or more ys |
| \d{3} | Match exactly 3 digits |
| \d{3,} | Match 3 or more digits |
| \d{3,5} | Match 3, 4, or 5 digits |
Nongreedy repetition:
This matches the smallest number of repetitions:| Example | Description |
|---|---|
| <.*> | Greedy repetition: matches " |
| <.*?> | Nongreedy: matches " |
Grouping with parentheses:
| Example | Description |
|---|---|
| \D\d+ | No group: + repeats \d |
| (\D\d)+ | Grouped: + repeats \D\d pair |
| ([Pp]ython(, )?)+ | Match "Python", "Python, python, python", etc. |
Backreferences:
This matches a previously matched group again:| Example | Description |
|---|---|
| ([Pp])ython&\1ails | Match python&pails or Python&Pails |
| (['"])[^\1]*\1 | Single or double-quoted string. \1 matches whatever the 1st group matched . \2 matches whatever the 2nd group matched, etc. |
Alternatives:
| Example | Description |
|---|---|
| python|perl | Match "python" or "perl" |
| rub(y|le)) | Match "ruby" or "ruble" |
| Python(!+|\?) | "Python" followed by one or more ! or one ? |
Anchors:
This needs to specify match position.| Example | Description |
|---|---|
| ^Python | Match "Python" at the start of a string or internal line |
| Python$ | Match "Python" at the end of a string or line |
| \APython | Match "Python" at the start of a string |
| Python\Z | Match "Python" at the end of a string |
| \bPython\b | Match "Python" at a word boundary |
| \brub\B | \B is nonword boundary: match "rub" in "rube" and "ruby" but not alone |
| Python(?=!) | Match "Python", if followed by an exclamation point |
| Python(?!!) | Match "Python", if not followed by an exclamation point |
Special syntax with parentheses:
| Example | Description |
|---|---|
| R(?#comment) | Matches "R". All the rest is a comment |
| R(?i)uby | Case-insensitive while matching "uby" |
| R(?i:uby) | Same as above |
| rub(?:y|le)) | Group only without creating \1 backreference |
Saturday, August 23, 2014
Method argument Pass by Value & Pass by Reference behavior in Python
This is a very important concept in Python where the behavior of pass by value & pass by reference varies with lists compared to immutable data types like strings.
Source http://www.python-course.eu/passing_arguments.php
The most common evaluation strategy when passing arguments to a function has been call by value and call by reference:
There are books which call the strategy of Python call-by-value and others call it call-by-reference. You may ask yourself, what is right.
Humpty Dumpty supplies the explanation:
--- "When I use a word," Humpty Dumpty said, in a rather a scornful tone, "it means just what I choose it to mean - neither more nor less."
--- "The question is," said Alice, "whether you can make words mean so many different things."
--- "The question is," said Humpty Dumpty, "which is to be master - that's all." Lewis Carroll, Through the Looking-Glass
To come back to our initial question what is used in Python: The authors who call the mechanism call-by-value and those who call it call-by-reference are stretching the definitions until they fit.
Correctly speaking, Python uses a mechanism, which is known as "Call-by-Object", sometimes also called "Call by Object Reference" or "Call by Sharing".
If you pass immutable arguments like integers, strings or tuples to a function, the passing acts like call-by-value. The object reference is passed to the function parameters. They can't be changed within the function, because they can't be changed at all, i.e. they are immutable. It's different, if we pass mutable arguments. They are also passed by object reference, but they can be changed in place in the function. If we pass a list to a function, we have to consider two cases: Elements of a list can be changed in place, i.e. the list will be changed even in the caller's scope. If a new list is assigned to the name, the old list will not be affected, i.e. the list in the caller's scope will remain untouched.
First, let's have a look at the integer variables. The parameter inside of the function remains a reference to the arguments variable, as long as the parameter is not changed. As soon as a new value will be assigned to it, Python creates a separate local variable. The caller's variable will not be changed this way:
In the example above, we used the id() function, which takes an object as a parameter. id(obj) returns the "identity" of the object "obj". This identity, the return value of the function, is an integer which is unique and constant for this object during its lifetime. Two different objects with non-overlapping lifetimes may have the same id() value.
If you call the function ref_demo() - like we do in the green block further down - we can check with the id() function what happens to x. We can see that in the main scope, x has the identity 41902552. In the first print statement of the ref_demo() function, the x from the main scope is used, because we can see that we get the same identity. After we have assigned the value 42 to x, x gets a new identity 41903752, i.e. a separate memory location from the global x. So, when we are back in the main scope x has still the original value 9.
This means, that Python initially behaves like call-by-reference, but as soon as we are changing the value of such a variable, Python "switches" to call-by-value.
In many cases these side effects are wanted, i.e. they are part of the functions specification. But in other cases, they are not wanted , they are hidden side effects. In this chapter we are only interested in the side effects, which change global variables, which have been passed as arguments to a function.
Let's assume, we are passing a list to a function. We expect, that the function is not changing this list. First let's have a look at a function which has no side effects. As a new list is assigned to the parameter list in func1(), a new memory location is created for list and list becomes a local variable.
The following script (arguments.py) prints all arguments:
Sometimes, it's necessary to use positional parameters followed by an arbitrary number of parameters in a function definition. This is possible, but the positional parameters always have to precede the arbitrary parameters. In the following example, we have a positional parameter "city", - the main location, - which always have to be given, followed by an arbitrary number of other locations:
In the following interactive Python session, we can learn how to use this function. We assume that the function arithmetic_mean is saved in a file called statistics.py.
Parameter Passing
"call by value" and "call by name"
- Call by Value
The most common strategy is the call-by-value evaluation, sometimes also called pass-by-value. This strategy is used in C and C++ for example. In call-by-value, the argument expression is evaluated, and the result of this evaluation is bound to the corresponding variable in the function. So, if the expression is a variable, a local copy of its value will be used, i.e. the variable in the caller's scope will be unchanged when the function returns. - Call by Reference
In call-by-reference evaluation, which is also known as pass-by-reference, a function gets an implicit reference to the argument, rather than a copy of its value. As a consequence, the function can modify the argument, i.e. the value of the variable in the caller's scope can be changed. The advantage of call-by-reference consists in the advantage of greater time- and space-efficiency, because arguments do not need to be copied. On the other hand this harbours the disadvantage that variables can be "accidentally" changed in a function call. So special care has to be taken to "protect" the values, which shouldn't be changed.
Many programming language support call-by-reference, like C or C++, but Perl uses it as default.
and what about Python?
Humpty Dumpty supplies the explanation:
--- "When I use a word," Humpty Dumpty said, in a rather a scornful tone, "it means just what I choose it to mean - neither more nor less."
--- "The question is," said Alice, "whether you can make words mean so many different things."
--- "The question is," said Humpty Dumpty, "which is to be master - that's all." Lewis Carroll, Through the Looking-Glass
To come back to our initial question what is used in Python: The authors who call the mechanism call-by-value and those who call it call-by-reference are stretching the definitions until they fit.
Correctly speaking, Python uses a mechanism, which is known as "Call-by-Object", sometimes also called "Call by Object Reference" or "Call by Sharing".
First, let's have a look at the integer variables. The parameter inside of the function remains a reference to the arguments variable, as long as the parameter is not changed. As soon as a new value will be assigned to it, Python creates a separate local variable. The caller's variable will not be changed this way:
def ref_demo(x):
print "x=",x," id=",id(x)
x=42
print "x=",x," id=",id(x)
If you call the function ref_demo() - like we do in the green block further down - we can check with the id() function what happens to x. We can see that in the main scope, x has the identity 41902552. In the first print statement of the ref_demo() function, the x from the main scope is used, because we can see that we get the same identity. After we have assigned the value 42 to x, x gets a new identity 41903752, i.e. a separate memory location from the global x. So, when we are back in the main scope x has still the original value 9.
This means, that Python initially behaves like call-by-reference, but as soon as we are changing the value of such a variable, Python "switches" to call-by-value.
>>> x = 9 >>> id(x) 41902552 >>> ref_demo(x) x= 9 id= 41902552 x= 42 id= 41903752 >>> id(x) 41902552 >>>
Side effects
A function is said to have a side effect if, in addition to producing a value, it modifies the caller's environment in other ways. For example, a function might modify a global or static variable, modify one of its arguments, raise an exception, write data to a display or file and so on.In many cases these side effects are wanted, i.e. they are part of the functions specification. But in other cases, they are not wanted , they are hidden side effects. In this chapter we are only interested in the side effects, which change global variables, which have been passed as arguments to a function.
Let's assume, we are passing a list to a function. We expect, that the function is not changing this list. First let's have a look at a function which has no side effects. As a new list is assigned to the parameter list in func1(), a new memory location is created for list and list becomes a local variable.
>>> def func1(list): ... print list ... list = [47,11] ... print list ... >>> fib = [0,1,1,2,3,5,8] >>> func1(fib) [0, 1, 1, 2, 3, 5, 8] [47, 11] >>> print fib [0, 1, 1, 2, 3, 5, 8] >>>This changes drastically, if we include something in the list by using +=. To show this, we have a different function func2() in the following example:
>>> def func2(list): ... print list ... list += [47,11] ... print list ... >>> fib = [0,1,1,2,3,5,8] >>> func2(fib) [0, 1, 1, 2, 3, 5, 8] [0, 1, 1, 2, 3, 5, 8, 47, 11] >>> print fib [0, 1, 1, 2, 3, 5, 8, 47, 11] >>>The user of the function can prevent this by passing a copy to the function. In this case a shallow copy is sufficient:
>>> def func2(list): ... print list ... list += [47,11] ... print list ... >>> fib = [0,1,1,2,3,5,8] >>> func2(fib[:]) [0, 1, 1, 2, 3, 5, 8] [0, 1, 1, 2, 3, 5, 8, 47, 11] >>> print fib [0, 1, 1, 2, 3, 5, 8] >>>
Command Line Arguments
It's possible to write Python scripts using command line arguments. If you call a Python script from a shell, the arguments are placed after the script name. The arguments are separated by spaces. Inside of the script these argumetns are accessible through the list variable sys.argv. The name of the script is included in this list sys.argv[0]. sys.argv[1] contains the first parameter, sys.argv[2] the second and so on.The following script (arguments.py) prints all arguments:
# Module sys has to be imported:
import sys
# Iteration over all arguments:
for eachArg in sys.argv:
print eachArg
Example call to this script:python argumente.py python course for beginnersThis call creates the following output:
argumente.py python course for beginners
Variable Length of Parameters
We will introduce now functions, which can take an arbitrary number of arguments. Those who have some programming background in C or C++ know this from the varargs feature of these languages. The asterisk "*" is used in Python to define a variable number of arguments. The asterisk character has to precede a variable identifier in the parameter list.>>> def varpafu(*x): print(x) ... >>> varpafu() () >>> varpafu(34,"Do you like Python?", "Of course") (34, 'Do you like Python?', 'Of course') >>>We learn from the previous example, that the arguments passed to the function call of varpafu() are collected in a tuple, which can be accessed as a "normal" variable x within the body of the function. If the function is called without any arguments, the value of x is an empty tuple.
Sometimes, it's necessary to use positional parameters followed by an arbitrary number of parameters in a function definition. This is possible, but the positional parameters always have to precede the arbitrary parameters. In the following example, we have a positional parameter "city", - the main location, - which always have to be given, followed by an arbitrary number of other locations:
>>> def locations(city, *other_cities): print(city, other_cities)
...
>>> locations("Paris")
('Paris', ())
>>> locations("Paris", "Strasbourg", "Lyon", "Dijon", "Bordeaux", "Marseille")
('Paris', ('Strasbourg', 'Lyon', 'Dijon', 'Bordeaux', 'Marseille'))
>>>
Exercise
Write a function which calculates the arithmetic mean of a variable number of values.Solution
def arithmetic_mean(x, *l):
""" The function calculates the arithmetic mean of a non-empty
arbitrary number of numbers """
sum = x
for i in l:
sum += i
return sum / (1.0 + len(l))
You might ask yourself, why we used both a positional parameter "x" and the variable parameter "*l" in our function definition. We could have only used *l to contain all our numbers. The idea is, that we wanted to enforce, that we always have a non-empty list of numbers. This is necessary to prevent a division by zero error, because the average of a non-empty list of numbers is not defined. In the following interactive Python session, we can learn how to use this function. We assume that the function arithmetic_mean is saved in a file called statistics.py.
>>> from statistics import arithmetic_mean >>> arithmetic_mean(4,7,9) 6.666666666666667 >>> arithmetic_mean(4,7,9,45,-3.7,99) 26.71666666666667This works fine, but there is a catch. What if somebody wants to call the function with a list, instead of a variable number of numbers, as we have shown above? We can see in the following, that we raise an error, as most hopefully, you might expect:
>>> l = [4,7,9,45,-3.7,99] >>> arithmetic_mean(l) Traceback (most recent call last): File "The rescue is using another asterisk:", line 1, in File "statistics.py", line 8, in arithmetic_mean return sum / (1.0 + len(l)) TypeError: unsupported operand type(s) for /: 'list' and 'float'
>>> arithmetic_mean(*l) 26.71666666666667 >>>
* in Function Calls
A * can appear in function calls as well, as we have just seen in the previous exercise: The semantics is in this case "inverse" to a star in a function definition. An argument will be unpacked and not packed. In other words, the elements of the list or tuple are singularized:>>> def f(x,y,z): ... print(x,y,z) ... >>> p = (47,11,12) >>> f(*p) (47, 11, 12)There is hardly any need to mention that this way of calling our function is more comfortable the the following one:
>>> f(p[0],p[1],p[2]) (47, 11, 12) >>>Additionally to being less comfortable, the previous call (f(p[0],p[1],p[2])) doesn't work in the general case, i.e. lists of unknown lengths. "Unknown" mean, that the length is only known at runtime and not when we are writing the script.
Arbitrary Keyword Parameters
There is also a mechanism for an arbitrary number of keyword parameters. To do this, we use the double asterisk "**" notation:>>> def f(**args):
... print(args)
...
>>> f()
{}
>>> f(de="Germnan",en="English",fr="French")
{'fr': 'French', 'de': 'Germnan', 'en': 'English'}
>>>
Double Asterisk in Function Calls
The following example demonstrates the usage of ** in a function call:>>> def f(a,b,x,y):
... print(a,b,x,y)
...
>>> d = {'a':'append', 'b':'block','x':'extract','y':'yes'}
>>> f(**d)
('append', 'block', 'extract', 'yes')
and now in combination with *:>>> t = (47,11)
>>> d = {'x':'extract','y':'yes'}
>>> f(*t, **d)
(47, 11, 'extract', 'yes')
>>>
Subscribe to:
Comments (Atom)
Featured
TechBytes on Linux
This is a growing list of Linux commands which might come handy for the of Linux users. 1. Found out i had to set the date like this: ...
Popular Posts
-
This is a growing list of Linux commands which might come handy for the of Linux users. 1. Found out i had to set the date like this: ...
-
To set a PDF viewer as the default on Mac OS X: Select any PDF file from Finder. Control-click to open the menu. ... Choose Get...
-
ಮ್ಯಾಕ್ ಬುಕ್ ನಲ್ಲಿ ಕನ್ನಡ ದಲ್ಲಿ ಟೈಪ್ ಮಾಡಲು ಲಿಪಿಕ ಎನ್ನುವ ಈ ಕೆಳಗಿನ ಲಿಂಕ್ ಅನ್ನು ಕ್ಲಿಕ್ ಮಾಡಿ .pkg ಫೈಲ್ ಅನ್ನು ಡೌನ್ ಲೋಡ್ ಮಾಡಿ ಅದನ್ನು ಇನ್ಸ್ತಾಲ್ ಮಾಡಿ...