Re: [Tutor] How do we create a GUI to run a simple calculation program in Python?
On 2017-04-15 01:04, Alan Gauld via Tutor wrote: Finally, if you can find a copy of my recent book "Python Projects" there is a rolling project within that which demonstrates how the same logic code can be used to build a CLI, a GUI and a Web app. [ In fact it goes even further by demonstrating how to break an app into 3 tiers - data, logic and UI - which is industry best practice, but usually overkill for small projects.] Thanks, Alan, for the guidance. As it happens, I have a copy of your Python Projects" book- time to get it off the shelf and have a closer look! Alex ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Do not understand code snippet from "26.8. test — Regression tests package for Python"
On 16/04/17 00:17, boB Stepp wrote: > -- > #!/usr/bin/env python3 > > def mySuperWhammyFunction(any_input): > return any_input This is a simple function, its not bound to an object > > import unittest > > class TestFuncAcceptsSequencesMixin: > > func = mySuperWhammyFunction > > def test_func(self): > self.func(self.arg) This is calling self.function which implies a method. Convert your function to a method and it should work. > ERROR: test_func (test_super.AcceptLists) > -- > Traceback (most recent call last): > File "c:\Projects\test_super.py", line 13, in test_func > self.func(self.arg) > TypeError: mySuperWhammyFunction() takes 1 positional argument but 2 were > given The missing self parameter... > I suspect that both an object instance and self.arg is getting passed Its self. When you do object.method() object gets passed as the first parameter (traditionally called self) But because your function is not a method it does not expect a self to be passed. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Do not understand code snippet from "26.8. test — Regression tests package for Python"
In the section https://docs.python.org/3/library/test.html#writing-unit-tests-for-the-test-package I have been trying to make sense of the given pointer and code snippet: Try to maximize code reuse. On occasion, tests will vary by something as small as what type of input is used. Minimize code duplication by subclassing a basic test class with a class that specifies the input: class TestFuncAcceptsSequencesMixin: func = mySuperWhammyFunction def test_func(self): self.func(self.arg) class AcceptLists(TestFuncAcceptsSequencesMixin, unittest.TestCase): arg = [1, 2, 3] class AcceptStrings(TestFuncAcceptsSequencesMixin, unittest.TestCase): arg = 'abc' class AcceptTuples(TestFuncAcceptsSequencesMixin, unittest.TestCase): arg = (1, 2, 3) When using this pattern, remember that all classes that inherit from unittest.TestCase are run as tests. The Mixin class in the example above does not have any data and so can’t be run by itself, thus it does not inherit from unittest.TestCase. I have tried to implement this in various ways, but cannot overcome a basic hurdle where I get an argument mismatch. Following is my simplest effort to implement the above which does not actually test anything yet, but demonstrates this mismatch hurdle: -- #!/usr/bin/env python3 def mySuperWhammyFunction(any_input): return any_input import unittest class TestFuncAcceptsSequencesMixin: func = mySuperWhammyFunction def test_func(self): self.func(self.arg) class AcceptLists(TestFuncAcceptsSequencesMixin, unittest.TestCase): arg = [1, 2, 3] class AcceptStrings(TestFuncAcceptsSequencesMixin, unittest.TestCase): arg = 'abc' class AcceptTuples(TestFuncAcceptsSequencesMixin, unittest.TestCase): arg = (1, 2, 3) if __name__ == '__main__': unittest.main() -- This gives me the following result: -- > python -m unittest test_super.py EEE == ERROR: test_func (test_super.AcceptLists) -- Traceback (most recent call last): File "c:\Projects\test_super.py", line 13, in test_func self.func(self.arg) TypeError: mySuperWhammyFunction() takes 1 positional argument but 2 were given == ERROR: test_func (test_super.AcceptStrings) -- Traceback (most recent call last): File "c:\Projects\test_super.py", line 13, in test_func self.func(self.arg) TypeError: mySuperWhammyFunction() takes 1 positional argument but 2 were given == ERROR: test_func (test_super.AcceptTuples) -- Traceback (most recent call last): File "c:\Projects\test_super.py", line 13, in test_func self.func(self.arg) TypeError: mySuperWhammyFunction() takes 1 positional argument but 2 were given -- Ran 3 tests in 0.000s FAILED (errors=3) -- I suspect that both an object instance and self.arg is getting passed to mySuperWhammyFunction(), but I am not seeing how the object instance is getting passed -- if my suspicion is indeed correct. I also am not truly understanding how this code is working within the unittest framework. Help! TIA! boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] understanding code testing
On 15/04/17 15:33, Rafael Knuth wrote: > can anyone point me to good learning resources on this subject? I'd recommend YouTube as your first port of call. There are a few python unit test videos but most of the best stuff is Java focused, but it translates to Python easily enough. Once you've watched half a dozen and had a play with your own code I'd then switch to a web site or book for the details. I'd also leave mocking till later, it can be confusing until you really get to grips with TDD. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] understanding code testing
For python specific I'd look at unittest: https://docs.python.org/3/library/unittest.html?highlight=test#module-unittest For testing in general then "Test Driven Development By Example" by Kent Beck. Examples are in Java but he explains the theory. I've been on a testing kick lately and am slogging through Binder's "Testing Object Oriented Systems". It's a great book, well written, but still a bit over my head. I wasn't a math whiz in school. Lots of web pages on testing, and probably some on python TDD if you make sure it's not focusing on Django. Unless you're doing Django, that is. Leam On Sat, Apr 15, 2017 at 1:10 PM, Joel Goldstickwrote: > On Sat, Apr 15, 2017 at 10:33 AM, Rafael Knuth > wrote: > > can anyone point me to good learning resources on this subject? > > (python 3) > > ___ > > Tutor maillist - Tutor@python.org > > To unsubscribe or change subscription options: > > https://mail.python.org/mailman/listinfo/tutor > > This looks like a good book: http://www.obeythetestinggoat.com/ > > > -- > Joel Goldstick > http://joelgoldstick.com/blog > http://cc-baseballstats.info/stats/birthdays > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] understanding code testing
On Sat, Apr 15, 2017 at 10:33 AM, Rafael Knuthwrote: > can anyone point me to good learning resources on this subject? > (python 3) > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor This looks like a good book: http://www.obeythetestinggoat.com/ -- Joel Goldstick http://joelgoldstick.com/blog http://cc-baseballstats.info/stats/birthdays ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] understanding code testing
can anyone point me to good learning resources on this subject? (python 3) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using Modules
Hi, Start with the tutorial at https://docs.python.org/3/tutorial/ It includes Brief Tour of the Standard Library: https://docs.python.org/3/tutorial/stdlib.html https://docs.python.org/3/tutorial/stdlib2.html Hope that helps. Sri On Sat, Apr 15, 2017 at 2:03 PM, Aero Maxx Dwrote: > Hi everyone, > > I'm very very new to Python, and trying to learn and I'm struggling with > the import statements. > > I understand they are importing modules that I can then use in my code, > what I'm struggling with though is how do I find out which modules I need > to use to do any given task? > > I do have a programming background in that I know PHP however this is a > scripting language, and one that doesn't require me to import modules or > anything, can easily just look at the functions available on the php.net > website and carry on as normal. > > With Python I'm not finding which modules I need if any as easy as that, > mainly due to the vast number of modules available. > > I'd like to start with something that in my mind is relatively simple in > php, so I thought I'd connect to a MySQL database read what was in a table > and insert some data into the table. > > I've done some googling and read some books, the book mentioned using > import dbm, however I understand this isn't for MySQL but my problem is the > book glosses over the import statement and just says use this, but gives no > explanation as to why I'm using that and/or how to find out that's what I > should use. Googling found some forum posts saying to use MySQLdb. > > I'm wanting to learn Python properly and find things out myself, without > relying on someone to of posted on a blog or a forum who may have done > something similar to what I may be trying to do at that particular time, > and then just blindly follow their example and use the same modules without > an understanding why I'm using those modules, or using modules that I'm > actually not using in the code. > > Sorry for the long email, I didn't initially intend or expect it to be > this long. > > Thanks, > Daniel > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] bracket issue
Palm Tree wrote: > hi all. i'm trying to write a simple program. i'm using python 3.4 > > let us say i have > > s="2*3+3(8-4(5+6+9))+2+3+3(4/4)" > > i want to find expression enclosed in brackets. > > i want outputs to be like: > (8-4(5+6+9)) > (5+6+9) > (4/4) > note : i'd like an answer involving recursion if possible No recursion, but a stack managed manually: >>> s = "2*3+3(8-4(5+6+9))+2+3+3(4/4)" >>> stack = [] >>> for i, c in enumerate(s): ... if c == "(": ... stack.append(i) ... elif c == ")": ... print(s[stack.pop():i+1]) ... (5+6+9) (8-4(5+6+9)) (4/4) The order is determined by the closing parenthesis, you could sort if you don't want that. I did not find a convincing translation using recursion, but I'll give one anyway: def find_closing(s): i = c = None pairs = enumerate(s) def step(start=None): nonlocal i, c for i, c in pairs: if c == "(": step(i) elif c == ")": if start is not None: print(s[start:i+1]) return step() ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using Modules
Aero Maxx D wrote: ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using Modules
On 15/04/17 09:33, Aero Maxx D wrote: > With Python I'm not finding which modules I need Search for the functionality within the python.org site. The documentation tells you which module you are looking for. > ...I thought I'd connect to a MySQL database There is a standard DB interface in Python for SQL based data. But there is a separate module for each database. The idea being that you should theoretically be able to write the code that uses the database and then change the database from, say, MySql to Oracle by just changing the import. In practice it's not quite that easy but you should be able to do it with only minor tweaks - usually around the login process. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] bracket issue
On 15/04/17 03:17, Palm Tree wrote: > s="2*3+3(8-4(5+6+9))+2+3+3(4/4)" > > i want to find expression enclosed in brackets. > > i want outputs to be like: > (8-4(5+6+9)) > (5+6+9) > (4/4) > You probably could do it with some fancy regex but personally I'd investigate a proper text parser. There is a module to support that in the standard library but I can't recall its name, however you need a bit of background in parsing to use it. Google(or bing or...) is your friend. If you really want to do it manually you need to count the brackets going in and then back out. So as you scan your sample data the counter will go 0,1,2,1,0,1,0 If you store those counts along with the string index where they change you can use that to slice your string accordingly. Its all a bit messy but should work... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using Modules
On Sat, Apr 15, 2017 at 4:33 AM, Aero Maxx Dwrote: > Hi everyone, > > I'm very very new to Python, and trying to learn and I'm struggling with the > import statements. > > I understand they are importing modules that I can then use in my code, what > I'm struggling with though is how do I find out which modules I need to use > to do any given task? > > I do have a programming background in that I know PHP however this is a > scripting language, and one that doesn't require me to import modules or > anything, can easily just look at the functions available on the php.net > website and carry on as normal. > > With Python I'm not finding which modules I need if any as easy as that, > mainly due to the vast number of modules available. > > I'd like to start with something that in my mind is relatively simple in php, > so I thought I'd connect to a MySQL database read what was in a table and > insert some data into the table. > > I've done some googling and read some books, the book mentioned using import > dbm, however I understand this isn't for MySQL but my problem is the book > glosses over the import statement and just says use this, but gives no > explanation as to why I'm using that and/or how to find out that's what I > should use. Googling found some forum posts saying to use MySQLdb. > > I'm wanting to learn Python properly and find things out myself, without > relying on someone to of posted on a blog or a forum who may have done > something similar to what I may be trying to do at that particular time, and > then just blindly follow their example and use the same modules without an > understanding why I'm using those modules, or using modules that I'm actually > not using in the code. > > Sorry for the long email, I didn't initially intend or expect it to be this > long. > > Thanks, > Daniel > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor Look at the tutorials on python.org for a start -- Joel Goldstick http://joelgoldstick.com/blog http://cc-baseballstats.info/stats/birthdays ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Using Modules
Hi everyone, I'm very very new to Python, and trying to learn and I'm struggling with the import statements. I understand they are importing modules that I can then use in my code, what I'm struggling with though is how do I find out which modules I need to use to do any given task? I do have a programming background in that I know PHP however this is a scripting language, and one that doesn't require me to import modules or anything, can easily just look at the functions available on the php.net website and carry on as normal. With Python I'm not finding which modules I need if any as easy as that, mainly due to the vast number of modules available. I'd like to start with something that in my mind is relatively simple in php, so I thought I'd connect to a MySQL database read what was in a table and insert some data into the table. I've done some googling and read some books, the book mentioned using import dbm, however I understand this isn't for MySQL but my problem is the book glosses over the import statement and just says use this, but gives no explanation as to why I'm using that and/or how to find out that's what I should use. Googling found some forum posts saying to use MySQLdb. I'm wanting to learn Python properly and find things out myself, without relying on someone to of posted on a blog or a forum who may have done something similar to what I may be trying to do at that particular time, and then just blindly follow their example and use the same modules without an understanding why I'm using those modules, or using modules that I'm actually not using in the code. Sorry for the long email, I didn't initially intend or expect it to be this long. Thanks, Daniel ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] sorted function
Thankyou. got it. On Sat, Apr 15, 2017 at 5:44 AM, Steven D'Apranowrote: > On Fri, Apr 14, 2017 at 11:59:25PM +0530, shubham goyal wrote: > > > sorted(ls) > > sorted(ls1) > > Here you sort ls and throw the result away, then you do the same to ls1. > > sorted() makes a copy of the list and sorts it. You need to write: > > ls = sorted(ls) > ls1 = sorted(ls1) > > but even better would be to sort in place: > > ls.sort() > ls1.sort() > > which doesn't make a copy. > > > > -- > Steve > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] bracket issue
hi all. i'm trying to write a simple program. i'm using python 3.4 let us say i have s="2*3+3(8-4(5+6+9))+2+3+3(4/4)" i want to find expression enclosed in brackets. i want outputs to be like: (8-4(5+6+9)) (5+6+9) (4/4) i've tried where denotes an indentation level : #first part sbracket=[ ] ebracket=[ ] for i in range(len(s)): global sbracket global ebracket if f[i] == "(": sbracket.append(i) elif f[i] == ")": ebracket.append(i) #second part for i in range(len(sbracket)): print(f[sbracket[i]:ebracket[i]]) however, the above code works well as long as there are no nested brackets like s="(1+2)(2+3)" but fails as soon as s="((3+2))2" prior to that i wrote it like: for i in range(len(f)): if f[i] == "(": sbracket.append(i) for x in range(len(f)): if f[i+x]==")": ebracket.append(x) break but that too failed to output correctly note : i'd like an answer involving recursion if possible ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How do we create a GUI to run a simple calculation program in Python?
On Wed, Apr 5, 2017 at 12:42 AM, Alan Gauld via Tutorwrote: > On 04/04/17 17:55, Lisa Hasler Waters wrote: > > > A middle school student of mine created a program to calculate simple and > > compound interest. He built it in PyCharm EDU using a Mac running > 10.11.6. > > > > He would like to create a GUI to run this program. Please, can you advise > > on how he could build this? > > He could use Tkinter, or he could create an HTML screen and > write a small server using a Framework like Flask. > > Whatever he does he will need to dsepsarate his UI from his > logic - a good programming skill regardless of UI. > > So his first step should be to create a CLI UI on top of > his existing code such that none of hi functions contain > print() or input() statements, they should all be in > new UI code. > > The next step is to convert it to event driven style. > For this code that should almost be a done deal. > > Finally decide on his GUI/Web framework and do a tutorial > to get up to speed and fit his new event-driven backend > code into that. > > > Here is his code: > > > > def simple(m, t, r): > > r = r/100 > > print("The interest is {} and the total is {} ".format(r*m*t, > m+r*m*t)) > > Should return a value not print a message > > > def compound(m, t, r): > > morg = m > > r = r/100 > > for x in range(0, t): > > m = m*r+m > > print("The interest is {} and the total is {} if compounded > > yearly.".format(m-morg, m)) > > m = morg > > r = r/12 > > for x in range(0, t*12): > > m = m*r+m > > print("The interest is {} and the total is {} if compounded > > monthly.".format(m-morg, m)) > > > > Possiobly should be two separate methods, and definitely > should be returning values not printing stuff. > > > > choice = str(input("Would you like to use simple or compound interest? > ")) > > m = int(input("Input the amount of money you would like to deposit > > (don't use the $ symbol): ")) > > t = int(input("Input the amount of time you will be keeping your money > > in the bank (in years): ")) > > r = int(input("Input the interest rate the bank offers (don't use the > > % symbol): ")) > > > > if choice == 'simple': > > simple(m, t, r) > > elif choice == 'compound': > > compound(m, t, r) > > else: > > print("Your input is invalid") > > This needs to turn into a UI event loop which it almost is > but with no loop and no exit option. > > I’m feeling inspired by Alan Gauld’s reply and using this as an opportunity to learn Flask. Thank you, Alan. I have written a similar calculator program and created two separate versions of CLI UI using argparse and Google’s Python Fire. I’m trying to create an HTML screen and use my Python script that imports the Flask module which will pass data to the HTML file. When the web page is loaded, it should run the code associated with the page. Once I can make this work, I will submit it for your kind comments :) Sri ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How do we create a GUI to run a simple calculation program in Python?
On 15/04/17 03:38, Alex Kleider wrote: >> Whatever he does he will need to separate his UI from his >> logic - a good programming skill regardless of UI. > > Can anyone suggest a good tutorial that explains exactly what this means > and how to achieve it? > (separate UI from logic I mean.) I don't know of a tutorial as such but the principles are enshrined in the Model-View-Controller (MVC) design pattern and there are lots of articles and tutorials on that. One caveat is that there are almost as many variations on MVC as there are articles so you can expect some contradiction in the details. That's ok, just focus on the big ideas. At the most basic just do as I suggested in the post. Identify the functions that do the work(the logic) and make sure they take all of their input via parameters and deliver a result back to the caller with no UI (eg input() or print()) statements inside the function. Then write the code that interacts with the user as a separate function which calls the logic functions as needed. You should be able to put the core functions into a separate module and import that into the UI module/main program. That's quite a good check that you have made your logic reusable. This is good practice for all programming projects but its essential for GUI and Web projects. Finally, if you can find a copy of my recent book "Python Projects" there is a rolling project within that which demonstrates how the same logic code can be used to build a CLI, a GUI and a Web app. [ In fact it goes even further by demonstrating how to break an app into 3 tiers - data, logic and UI - which is industry best practice, but usually overkill for small projects.] -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor