# Example using Python 2.x.
myName = raw_input("my name is: )
print(myName)
# Example using Python 3.x.
myName = input("my name is: )
print(myName)
````
* Inverting boolean in Python, [http://stackoverflow.com/questions/10678441/flipping-the-boolean-values-in-a-list-python](http://stackoverflow.com/questions/10678441/flipping-the-boolean-values-in-a-list-python).
```markdown
booleanVariable = True
invertBooleanVariable = not booleanVariable
def CheckIfStringIsBlank(_string):
return not (_string and _string.strip())
def CheckIfStringIsNotBlank(_string):
return (_string and _string.strip())
exampleList = ["one", "two", "three"]
thisIsTrue = "one" in exampleList
string.isalnum()
is used to check if a string is an alphabet or a numeric.string.isdigit()
is used to check if a string is a digit. For example "0019".isdigit()
will return True
.Python's string.isnumeric()
is used to check if a string is a digit. For example "0019".isnumeric()
will return False
. But "19".isnumeric()
will return True
.
Example of Docopt.
Naval Fate.
Usage:
naval_fate.py --version
naval_fate.py -h | --help
naval_fate.py mine (set|remove) <x> <y> [--moored|--drifting]
naval_fate.py ship <name> move <x> <y> [--speed=<kn>]
naval_fate.py ship new <name>...
naval_fate.py ship shoot <x> <y>
Options:
--drifting Drifting mine.
--moored Moored (anchored) mine.
--speed=<kn> Speed in knots [default: 10].
--version Show version.
-h --help Show this screen.
"""Sociometric Application
Usage:
main.py (--help | -h)
main.py (--version | -v)
main.py reset
main.py set (--cname=<cnamev>|--dba=<dbav>|--dbn=<dbnv>|--dbp=<dbpv>|--db|--faced|--ird|--log|--pvd)...
main.py show (--config)
main.py start
main.py start all-default [--save]
main.py start without (--db|--faced|--ird|--log|--pvd)... [--save]
main.py start wizard
Options:
--help -h Refer to help manual.
--version -v Refer to this version of application.
--cname=<cnamev> Refer to client/this device name. Value is
a must, [default: clientTest].
--dba=<dbav> Refer to RethinkDB database address. Value
is a must, [default: 127.0.0.1].
--dbn=<dbnv> Refer to RethinkDB database name. Value is
a must, [default: sociometric_server].
--dbp=<dbpv> Refer to RethinkDB database port. Value is
a must, [default: 28015].
--config Refer to config.ini in the root of this
application.
--db Refer to RethinkDB database.
--log Refer to log that sends JSON document to
database.
--cam Refer to cam/webcam.
--ir Refer to IR.
--mic Refer to microphone.
--faced Refer to face detection using `--cam`.
--ird Refer to IR detection using `--ir`.
--pvd Refer to pitch and volume detection using
`--mic`.
--save Write all values into configuration .ini
files. Otherwise, value will only for
current runtime.
check Check component(s). Additional argument(s)
is necessary.
reset Set configuration values in .ini file to
their default values. This command also
delete all database log and tables.
set Command to set client name, database
configurations variables and component flags.
The component flags will reverse between
True to False for each `set`.
show To show something :D.
start Start this application with values from
the configuration file.
start all-default Start this application with default values.
start without Start this application without component(s).
Additional argument(s) is necessary.
start wizard Start this application with wizard.
"""
configparser
in Python3, https://docs.python.org/3/library/configparser.html.ConfigParser
in Python2.configparser
.import ConfigParser
def ConfigSectionMap(section):
dict1 = {}
options = Config.options(section)
for option in options:
try:
dict1[option] = Config.get(section, option)
if dict1[option] == -1:
print("skip: " + str(option))
except:
print("exception on " + str(option) + "!")
dict1[option] = None
return dict1
Config = ConfigParser.ConfigParser()
Config.read("./config.ini")
print(Config.sections())
sectionSectionOne = Config.sections()[0]
sectionSectionTwo = Config.sections()[1]
sectionSectionThree = Config.sections()[2]
sectionOthers = Config.sections()[3]
print(sectionSectionOne)
print(sectionSectionTwo)
print(sectionSectionThree)
print(sectionOthers)
sectionSectionOneAge = Config.get(sectionSectionOne, "Age")
sectionSectionOneName = Config.get(sectionSectionOne, "Name")
sectionSectionOneSingle = Config.get(sectionSectionOne, "Single")
sectionSectionOneStatus = Config.get(sectionSectionOne, "Status")
sectionSectionOneValue = Config.get(sectionSectionOne, "Value")
sectionSectionTwoFavoriteColor = (
Config.get(sectionSectionTwo, "FavoriteColor"))
sectionSectionThreeFamilyName = Config.get(sectionSectionThree, "FamilyName")
sectionOthersRoute = Config.get(sectionOthers, "Route")
print(sectionSectionOneAge)
print(sectionSectionOneName)
print(sectionSectionOneSingle)
print(sectionSectionOneStatus)
print(sectionSectionOneValue)
print(sectionSectionTwoFavoriteColor)
print(sectionSectionThreeFamilyName)
print(sectionOthersRoute)
# Testing write config file.
configFile = open("./new_config.ini", "w")
configNew = ConfigParser.ConfigParser()
sectionCounter = "counter"
sectionCounterRunCounter = "first_run"
sectionCounterRunCounterValue = True
sectionInput = "input"
sectionInputMic = "mic"
sectionInputMicValue = True
sectionInputCam = "cam"
sectionInputCamValue = True
sectionInputIR = "ir"
sectionInputIRValue = True
sectionValue = "value"
sectionValueCName = "client_name"
sectionValueCNameValue = "clientTest"
sectionValueDBA = "db_address"
sectionValueDBAValue = "127.0.0.1"
sectionValueDBN = "db_name"
sectionValueDBNValue = "sociometric_server"
sectionValueDBP = "db_port"
sectionValueDBPValue = 28015
configNew.add_section(sectionCounter)
configNew.add_section(sectionInput)
configNew.add_section(sectionValue)
configNew.set(sectionCounter, sectionCounterRunCounter,
sectionCounterRunCounterValue)
configNew.set(sectionInput, sectionInputMic, sectionInputMicValue)
configNew.set(sectionInput, sectionInputCam, sectionInputCamValue)
configNew.set(sectionInput, sectionInputIR, sectionInputIRValue)
configNew.set(sectionValue, sectionValueCName, sectionValueCNameValue)
configNew.set(sectionValue, sectionValueDBA, sectionValueDBAValue)
configNew.set(sectionValue, sectionValueDBN, sectionValueDBNValue)
configNew.set(sectionValue, sectionValueDBP, sectionValueDBPValue)
configNew.write(configFile)
configFile.close()
config = ConfigParser.ConfigParser()
config.read("/example/config.ini")
config.set("video","abs_path","/path/to/video.mp4")
with open("/example/config.ini", "w") as configFile:
config.write(configFile)
def StringToBool(self, _string):
return str(_string).lower() in ("1", "t", "true", "yes")