Re: Correct Way to Write in Python
On Saturday, August 3, 2013 12:17:49 PM UTC+5:30, Peter Otten wrote: > punk.sa...@gmail.com wrote: > > > > > Hi All, > > > > > > Im new to Python. Im coming from C# background and want to learn Python. > > > I was used to do following thing in C# in my previous experiences. I want > > > to know how do I implement below example in Python. How these things are > > > done in Python. > > > [code] > > > public class Bank > > > { > > > > > > public List lstCustomers = new List(); > > > private string micrcode; > > > > > > public void Bank() > > > { > > > customer > > > } > > > > > > } > > > > > > public class Customer > > > { > > > private srting customername; > > > > > > public string CustomerName > > > > > > { > > > get { return customername; } > > > set { customername = value; } > > > } > > > } > > > > > > main() > > > { > > > Customer objCustomer = new Customer; > > > objCustomer.CustomerName = "XYZ" > > > > > > Bank objBank = new Bank(); > > > objBank.lstCustomer.Add(objCustomer); > > > > > > } > > > [/code] > > > > While I don't know C# I doubt that this is good C# code ;) > > Here's a moderately cleaned-up Python version: > > > > class DuplicateCustomerError(Exception): > > pass > > > > class Customer: > > def __init__(self, name): > > self.name = name > > > > class Bank: > > def __init__(self): > > self.customers = {} > > def add_customer(self, name): > > if name in self.customers: > > raise DuplicateCustomerError > > customer = Customer(name) > > self.customers[name] = customer > > return customer > > > > if __name__ == "__main__": > > bank = Bank() > > bank.add_customer("XYZ") > > > > I'm assuming a tiny bank where every customer has a unique name and only one > > program is running so that you can ignore concurrency issues. Thanks a lot Peter. I appreciate your Help. You mentioned that C# code above is not good. If you can point me why it is not good, would help me learn new approaches as this type of Code I use to see long back(when i was fresher). There may be better approaches or concepts i am not aware of. If you can point me in that direction it would be gr8. -- http://mail.python.org/mailman/listinfo/python-list
Re: Correct Way to Write in Python
On Saturday, August 3, 2013 1:50:41 PM UTC+5:30, Steven D'Aprano wrote: > On Fri, 02 Aug 2013 23:18:47 -0700, punk.sagar wrote: > > > > > Hi All, > > > > > > Im new to Python. Im coming from C# background and want to learn Python. > > > I was used to do following thing in C# in my previous experiences. I > > > want to know how do I implement below example in Python. How these > > > things are done in Python. > > > > I am not an expert on C#, but I'll try to translate the following code to > > Python. > > > > > > > [code] > > > public class Bank > > > { > > > > > > public List lstCustomers = new List(); > > > private string micrcode; > > > > > > public void Bank() > > > { > > > customer > > > } > > > > > > } > > > > > > public class Customer > > > { > > > private srting customername; > > > public string CustomerName > > > > Do you mean "private string" rather than "private srting"? > > > > > > > { > > > get { return customername; } > > > set { customername = value; } > > > } > > > } > > > > > > main() > > > { > > > Customer objCustomer = new Customer; > > > objCustomer.CustomerName = "XYZ" > > > > > > Bank objBank = new Bank(); > > > objBank.lstCustomer.Add(objCustomer); > > > > > > } > > > [/code] > > > > > > Here is a literally translation, as best as I can understand the C# code. > > (But note that this is not the best Python code.) > > > > > > class Bank: > > def __init__(self): > > self.lstCustomers = [] # Empty list of customers. > > self._micrcode = '' # Does this actually get used? > > > > class Customer: > > def __init__(self): > > self._customername = '' > > > > @property > > def CustomerName(self): > > return self._customername > > > > @CustomerName.setter > > def CustomerName(self, value): > > if not instance(value, str): > > raise TypeError('names must be strings') > > self._customername = value > > > > > > if __name__ == '__main__': > > # Running as a script, call the main function. > > objCustomer = Customer() > > objCustomer.CustomerName = "XYZ" > > > > objBank = Bank() > > objBank.lstCustomers.append(objCustomer) > > > > > > > > But this isn't how I would write it in Python. For starters, our naming > > conventions are different. Everything in Python is an object, even simple > > types like ints and strings, and even classes, so it isn't meaningful to > > prefix instances with "obj". > > > > We tend to avoid anything which even vaguely looks like Hungarian > > Notation, so "lstCustomer" is right out. Instead, we use plural for > > collections (lists, sets, dicts, whatever) of things, and singular for > > individual instances. > > > > Also, while we can use the "property" decorator to make computed > > attributes, we very rarely do just to enforce private/public variables. > > Our philosophy is, if you want to shoot yourself in the foot, we're not > > going to stop you. (People spend far too much time trying to work around > > private names in other languages for Python to spend too much effort in > > this area.) Instead, we have "private by convention": names starting with > > a single underscore are "private", so don't touch them, and if you do, > > you have nobody but yourself to blame when you shoot yourself in the foot. > > > > Similarly, the language doesn't spend much time enforcing type > > restrictions. Python is a dynamic language, and type restrictions go > > against that philosophy. If you have a good reason to put a non-string as > > the customer name, you can do so, but don't come crying to me if you > > break your code. So here is how I would write the above: > > > > > > > > class Bank: > > def __init__(self): > > self.customers = [] > > self._micrcode = '' # Does this actually get used? > > > > class Customer: > > def __init__(self, name): > > # This type-check is optional. > > if not instance(name, str): > > raise TypeError('names must be strings') > > self.name = name > > > > > > if __name__ == '__main__': > > # Running as a script, call the main function. > > customer = Customer("XYX") > > > > bank = Bank() > > bank.customers.append(customer) > > > > > > The above is still not what I call professional quality -- no doc strings > > (documentation), and the bank doesn't actually do anything, but it's a > > start. > > > > > > -- > > Steven Thanks Steven for your Time and Effort. You have cleared many doubts and concepts for that I was struggling, since I started learning python. But I am falling in love for Python. Your explanation for private and public access modifier was awesome as I w
Re: Correct Way to Write in Python
On Saturday, August 3, 2013 2:34:10 PM UTC+5:30, Peter Otten wrote: > Sagar Varule wrote: > > > > > On Saturday, August 3, 2013 12:17:49 PM UTC+5:30, Peter Otten wrote: > > >> punk.sa...@gmail.com wrote: > > > > > Thanks a lot Peter. I appreciate your Help. You mentioned that C# code > > > above is not good. If you can point me why it is not good, would help me > > > learn new approaches as this type of Code I use to see long back(when i > > > was fresher). There may be better approaches or concepts i am not aware > > > of. If you can point me in that direction it would be gr8. > > > > As I said, I don't know C# -- but I already tried to fix some of the > > potential issues in my code snippet. > > > > - A list is not the best choice to store the customers -- there should be a > > lookup by some kind of ID (I picked the name to keep it simple) > > - Is it really necessary to expose that container in a language that > > provides "privacy"? > > - Is there ever a customer without name/ID? I'd say no, so these should be > > passed as constructor arguments. > > - Renaming a customer is a delicate process, you may need to keep track of > > the old name, the reason for the name, update your database etc., so I > > wouldn't allow setting the attribute and instead add a method > > > > Bank.rename_customer(...) > > > > or > > > > Bank.customers.rename_customer(...) > > > > Asking to translate code might make sense if you are a wizzard in the > > "other" language and want to see how a particular construct is written > > idomatically in Python, but to rewrite very basic C# code in Python is a bit > > like trying to learn a natural language by replacing one word after another > > in a text with the word in the new language that you looked up in a dict. > > The result tends to be underwhelming. > > > > I recommend that you read the tutorial and then try to solve a simple task > > in Python. Whenever you run into a problem you can come here for help. > > Because there's a real problem behind your code there will be different ways > > to solve it in Python, and you'll learn much more about the possibilites > > Python has to offer while your code gradually becomes more idiomatic. Thanks Peter for helping me out, Your Questions and suggestions are thoughts provoking and will help me every time I write a new Class. I will keep your suggestions. I am happy and amazed that Im getting help from strangers, But I got none when I approached programmers in my officeThanks a Lot...! -- http://mail.python.org/mailman/listinfo/python-list
Paramiko Help. Execute command to Interactive Shell which is opened by SSHClient()
Hi All, Im using Paramiko for my SSH automation. Im using method that is shown in demo_simple.py example which comes with Paramiko. Below is code from demo_simple.py. As you can make out, below code opens SSH connection and opens Interactie Shell, and then wait for the command from user. I want to submit the command to this Interactive Shell using code. try: client = paramiko.SSHClient() client.load_system_host_keys() client.set_missing_host_key_policy(paramiko.WarningPolicy()) print '*** Connecting...' client.connect(hostname, port, username, password) chan = client.invoke_shell() print repr(client.get_transport()) print '*** Here we go!' print interactive.interactive_shell(chan) chan.close() client.close() Well Another approach I tried is instead of opening interactive_shell, directly issue command using; stdin, stdout, stderr = client.exec_command(bv_cmd) for line in stderr.readlines(): print line for line in stdout.readlines(): print line But problem here is client.exec_command(bv_cmd) waits for command to execute completely and then it returns to stdout,stderr. And I want to see the ouput from the command during its execution. Because my command takes long time for execution. Big Picture in My Mind: Big Picture I that want to achieve is, Opening different SSH connection to different host, and it will issue commands to all host, wait for execution. All execution should happen parallel.(just wanted to share my thought, and wanted to hear opinions from you all. Is this possible??). I am not big programmer, just 2 years experience with asp.net C# 2.0 so i would appreciate if discussion happens in simple english. -- http://mail.python.org/mailman/listinfo/python-list
Re: [GUI] Good frameworks for Windows/Mac?
On Tuesday, August 6, 2013 4:05:40 PM UTC+5:30, Gilles wrote: > Hello > > > > I need to write a small GUI application that should run on Windows and > > Mac. > > > > What open-source framework would you recommend? I just need basic > > widgets (button, listbox, etc.) and would rather a solution that can > > get me up and running fast. > > > > I know about wxWidgets and Qt: Are there other good options I should > > know about? > > > > Thank you. Pyside is also Good. It has a Designer which can be helpful. -- http://mail.python.org/mailman/listinfo/python-list
Re: Paramiko Help. Execute command to Interactive Shell which is opened by SSHClient()
On Thursday, August 8, 2013 12:50:25 PM UTC+5:30, sagar varule wrote: > Hi All, > > > > Im using Paramiko for my SSH automation. Im using method that is shown in > demo_simple.py example which comes with Paramiko. Below is code from > demo_simple.py. > > > > As you can make out, below code opens SSH connection and opens Interactie > Shell, and then wait for the command from user. > > I want to submit the command to this Interactive Shell using code. > > > > try: > > client = paramiko.SSHClient() > > client.load_system_host_keys() > > client.set_missing_host_key_policy(paramiko.WarningPolicy()) > > print '*** Connecting...' > > client.connect(hostname, port, username, password) > > chan = client.invoke_shell() > > print repr(client.get_transport()) > > print '*** Here we go!' > > print > > interactive.interactive_shell(chan) > > chan.close() > > client.close() > > > > Well Another approach I tried is instead of opening interactive_shell, > directly issue command using; > > > > stdin, stdout, stderr = client.exec_command(bv_cmd) > > for line in stderr.readlines(): > > print line > > for line in stdout.readlines(): > > print line > > But problem here is client.exec_command(bv_cmd) waits for command to execute > completely and then it returns to stdout,stderr. And I want to see the ouput > from the command during its execution. Because my command takes long time for > execution. > > > > Big Picture in My Mind: Big Picture I that want to achieve is, Opening > different SSH connection to different host, and it will issue commands to all > host, wait for execution. All execution should happen parallel.(just wanted > to share my thought, and wanted to hear opinions from you all. Is this > possible??). I am not big programmer, just 2 years experience with asp.net C# > 2.0 so i would appreciate if discussion happens in simple english. Can any one comment on this.. -- http://mail.python.org/mailman/listinfo/python-list
Re: Paramiko Help. Execute command to Interactive Shell which is opened by SSHClient()
On Sunday, August 11, 2013 11:28:31 AM UTC+5:30, Joshua Landau wrote: > On 11 August 2013 06:18, sagar varule wrote: > > > Can any one comment on this.. > > > > If you don't get replies here it's probably because no-one knows > > Paramiko. I suggest posting elsewhere to see if there are any Paramiko > > users in other places willing to help. There might be a Paramiko > > mailing list. > > > > You also didn't say what didn't work with the first block of code. > > Also, what is "interactive"? Submitting Command to Interactive Shell through code did not work. -- http://mail.python.org/mailman/listinfo/python-list