Now my Users attribute from my Configuration dictionary is a list containing 3 instances of the class User.
You can even write some code to test the Python indepently of the C#.
"""
Configuration of my application
"""
class User(object):
def __init__( self, userName, lastName, firstName):
self. UserName = userName
self. LastName = lastName
self. FirstName = firstName
def __str__( self): # Same as C# ToString()
return "UserName:%s, LastName:%s, FirstName:%s" % ( self. UserName, self. LastName, self. FirstName)
Configuration = {
"Server" : "TOTO",
"Database" : "Rene",
"Debug" : True,
"MaxUser" : 3,
"Users" : [
User("rdescartes" ,"rene" ,"descartes" ),
User("bpascal" ,"blaise" ,"pascal" ),
User("cmontesquieu" ,"charles" ,"montesquieu" )
]
}
# Code for quickly testing the user class. This Python code will work with IronPython or the C Python.
if __name__ == "" :
u = User("rdescartes" ,"rene" ,"descartes" )
u = User("bpascal" ,"blaise" ,"pascal" )
u = User("cmontesquieu" ,"charles" ,"montesquieu" )
print u
How to read this configuration variables in C# 4.0.
static void Demo() {
ScriptRuntime PythonScriptRuntime = Python .CreateRuntime();
dynamic PythonScript = PythonScriptRuntime.UseFile("Configuration3.py" );
Console.WriteLine(PythonScript.Configuration["Server" ]);
Console.WriteLine(PythonScript.Configuration["Database" ]);
Console.WriteLine(PythonScript.Configuration["Debug" ]);
Console.WriteLine(PythonScript.Configuration["MaxUser" ]);
foreach (var u in PythonScript.Configuration["Users" ]){
// Force to convert the User instance into a string which will call __str__
Console .WriteLine((string )u);
}
foreach (var u in PythonScript.Configuration["Users" ]){
Console .WriteLine(u.UserName);
}
Console .ReadLine();
}
No comments:
Post a Comment